Linker
What is linker
What does the linker do?
- Collect together all pieces of a program
- Coalesce like segments
- Fix addresses of code and data so the program can run
So linker is for linking object code and library to elf file, like the following figure.
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Why can’t compiler do this?
- Limited world view: sees one file, rather than all files
jserv ppt
Link script
The linker needs a script to discript how does the different sections in object should be merged.
Linker script includes:
- code and data map to memory address and memory size information.
- how do sections in these object files map to create a binary file.
main command section:
ENTRY:
- this is used to set "Entry point" information for elf file header
- Not mandatory, if not existing, the linker will use .text section or address 0 for start entry.
MEMORY:
- Describing the different memories present in and their start address and size information
- The linker uses these informations mentioned in this command to assign addresses to merged sections.
- This also helps linker to calculate total code and data memory consumed
- Calculating and fine-tune various memorise available and allow different sections to use different memory.
SECTIONS:
- Creating different output sections in the final elf
- Instructing the linker how to merge the input sections to an output section.
- Controling different output sections ordering appear in the elf file.
- Placement the section (.text/ .bss…etc) into specific memory region.
.:Location counter
- This symbol means the current location(address)
- The location counter is incremented by the szie of the output section
Detail command section:
ENTRY
MEMORY
ORIGIN, LENGTH are keywords.
$name, $start_address, $memory_size are varialbes
Attribute will be:
attirbute |
meaning |
R |
read-only |
W |
read/write |
X |
contain executeable code |
A |
Allocated section |
I |
Initialize section |
L |
the same as I |
! |
invert ther sense of any of the following attributes. |
Example: linkcmds.memory
You can use expression to Calculate the starting address.
SECTIONS
|
LMA (Load Memory Address) |
VMA (Virtual Memory Address) |
memory |
ROM/Flash |
RAM |
meaning |
saving the code section |
executing the code section |
- AT(LMA): this section should load data form LAM to VMA
- AT>lma_region: load the section to LAM region.
Example:
for export value, we can use these symbol.
PROVIDE(_sdata = .): using the command can prevent redefine problem in C and linker script.
After linker script
Let we take a example
test.ld
Use ld -T test.ld main.o test.o to build output file, the using objdump -h a.out to get sections.
we can see that ".text" at VMA address is 0000000008000000
, this is mapping to FLASH(rx): ORIGIN = 0x08000000, LENGTH = 128K
and .text : { *(.text) } > FLASH
the ".data" section is the same, VMA is following prevous section and LMA maps to 0000000020000000
So, the the program will be loaded by follow layout

gcc -Wl,-verbose file.c
can help you to read gcc linker script.
Reference
from Source to Binary: How GNU Toolchain Works - jserv
How A Compiler Works: GNU Toolchain - jserv
Link Script doc
linker script 簡單教學
Linker script 簡介
Linker Script初探 - GNU Linker Ld手冊略讀
Bare metal embedded lecture-4: Writing linker scripts and section placement