Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc linker description file force symbol to be at specific address

Tags:

c++

c

gcc

linker

elf

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.

like image 266
user3429251 Avatar asked Mar 17 '14 14:03

user3429251


People also ask

How do you define a section in a linker script?

In the linker script, the contents of these segments are specified by directing allocated output sections to be placed in the segment. To do this, the command describing the output section in the SECTIONS command should use `: name ' , where name is the name of the program header as it appears in the PHDRS command.

How do you declare a variable in linker?

Linker symbols that represent a data address: In C code, declare the variable as an extern variable. Then, refer to the value of the linker symbol using the & operator. Because the variable is at a valid data address, we know that a data pointer can represent the value.

What is align in linker file?

ALIGN doesn't change the value of the location counter--it just does arithmetic on it. As an example, to align the output . data section to the next 0x2000 byte boundary after the preceding section and to set a variable within the section to the next 0x8000 boundary after the input sections: SECTIONS{ ... .

What is absolute in linker?

An absolute expression type is one in which the symbol contains the value that it will have in the output file; a relocatable expression type is one in which the value is expressed as a fixed offset from the base of a section. The type of the expression is controlled by its position in the script file.


2 Answers

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.

like image 116
mfro Avatar answered Oct 28 '22 15:10

mfro


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

like image 43
rodrigo Avatar answered Oct 28 '22 15:10

rodrigo