11

I have a very specific question about the gcc linker description file. I have an embedded project and have to make sure, that the main symbol or the address of the main symbol is present at a specific address (Elf File).

The reason is, that i have a microcontroller which has a bootloader on it. This bootloader should call the main routine after bootup. For that reason I have to provide the address to jump to after boot.

Is there a way using the linker description file to force a symbol to be always on top of the table or enter an address or can i even get the address of the symbol in some way to write it back to the specific location?

Thx in advance.

user3429251
  • 121
  • 1
  • 1
  • 4
  • Don't exactly know how, but perhaps look into using linker script and placing your variable in a specific section, then fixing the address of the section instead. – zhiayang Mar 17 '14 at 14:50
  • I already have a working linker script with specific sections. But if I change the source code there will be more symbols and the start address of the main routine changes. – user3429251 Mar 17 '14 at 14:54
  • You could put your main symbol in assembly, then surround it with like .section .mainfunc or something, and have that call your actual main function. Basically put the thing in its own section – zhiayang Mar 17 '14 at 14:57
  • 1
    [This](http://stackoverflow.com/q/19470666/912144) can solve your problem. The general approach is to put `main` in a specific section of its own and fix the address of that section in the linker script. The solution I sent you does the first part automatically. – Shahbaz Mar 17 '14 at 15:05
  • http://stackoverflow.com/questions/495262/linking-symbols-to-fixed-addresses-on-linux/495631#495631 – Ciro Santilli OurBigBook.com Oct 06 '15 at 21:50

2 Answers2

12

Sure there is a way. Your best option is to use a section just for your function:

int start(void) __attribute__((section(".start")));

int start(void)
{
}

And then in the linker script:

SECTIONS
{
    . = 0x1234; // <---- put here your address
    .start : 
    {
        *(.start)
    }
}

Or something like that (it's been quite a long time since I used that).

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • +1, Didn't know you could put linker properties in an attribute. EDIT: the attribute should go before the function name, just tested it. – zhiayang Mar 17 '14 at 15:02
  • @zhiayang: Actually the [documentation](http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html) recommends after the arguments, so I'm editing it that way. – rodrigo Mar 17 '14 at 15:07
  • "Warning - GCC does not allow section attribute in this position on a function definition [Disable with b'-Wno-gcc-compat']" – zhiayang Mar 17 '14 at 15:08
  • @zhiayang. My bad! Function attributes are not set to _function definitions_, but to _function declarations_. Corrected! – rodrigo Mar 17 '14 at 15:25
1

If you already have a linker control file (I assume you are using a gcc toolchain), then you just need to put your object file containing your main() function into the very first section (and leave it out of the Makefile).

Here is an example:

MEMORY
{
   rom (RX) : ORIGIN = START_ADDRESS, LENGTH = 0x0010000
}
SECTIONS
{
    .text :
    {
        main.o (.text) /* this is the program entry point */
        *(.text)
    }
    .data :
    {
        . = ALIGN(4);
        *(.data)
    }
 }

(this is simplified a bit, but I think you get the point). The .text section begins at START_ADDRESS and as long as you make sure your main.o is located there (and main() is the first function in it), you are set.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
mfro
  • 3,286
  • 1
  • 19
  • 28