Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: how to tell GCC to put the 'main' function at the start of the .text section?

I've just started learning some ARM programming and I've got stuck in a slightly annoying problem. The toolchain I'm using to compile my sources is Sourcery CodeBench Lite 2013.05-23 (can be found here: https://sourcery.mentor.com/GNUToolchain/release2449)

What I would need is to tell GCC or LD or OBJCOPY to put the compiled bytecode of the 'main' function at the beginning of the .text section.

Is there any way to achieve this? (maybe through a linker script?)

Thank you

like image 930
Zuzu Corneliu Avatar asked Oct 19 '13 20:10

Zuzu Corneliu


1 Answers

First, see how is the .text section defined in your gcc's default linker script (so you don't have to make your own), by calling it as:

gcc -Wl,-verbose

that will print out the default linker script. Mine shows this for the .text section:

/* text: Program code section */
  .text : 
  {
    *(.text)
    *(.text.*)
    *(.gnu.linkonce.t.*)
  }

So in order to have the "main" function be the first in the .text section (and the rest be contiguous), you have to set the "section" attribute for all other functions. For example:

void main(void);
void funct1(....) __attribute__ ((section (".text.A")));
void funct2(....) __attribute__ ((section (".text.A")));
void funct3(....) __attribute__ ((section (".text.A")));

It's enough with "attributing" the function prototypes. That way, when you compile now, the "main" function will be the first one in the ".text" section and all others will follow on the immediately consecutive addresses.

If you want to place the ".text" section (i.e. "main" function) at a specific address (for example 0x1000), remember to link with:

gcc .... -Wl,-Ttext=0x1000
like image 148
Galland Avatar answered Oct 31 '22 13:10

Galland