gcc linker description file force symbol to be at

2019-02-13 00:09发布

问题:

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.

回答1:

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).



回答2:

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.



标签: c++ c gcc linker elf