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.
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.
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.
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{ ... .
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With