Professional Documents
Culture Documents
LINKER
Object file is organized into segment for executable code and data.
Executable code (text)
Data
Initialized data (data)
Un-initialized data (bbs)
Each segment is assigned a base address matching with memory
model
Objects defined in segment is relatively referred to this base address
Segments are not allowed to overlap
Linker calculates size of each segment of the input object file, pairs
segments having the similar role into a single segment, and plans
memory for each of these segments on the output object file.
The base address for segment is assigned exactly
Avoid redundancy
0 +-----------+
1017 | |module A| main +-----------+
1016 +-----------+ 0| Code of A |0
1016| |1016
0 +-----------+ +-----------+
615 | |module B| add 1017| |1018
614 +-----------+ 162b| B |162c
+-----------+
0 +-----------+ 162c| |1630
920 | |module C| printf 1f4b| C |1f4f
91f +-----------+ +-----------+
Structure of the object file ELF is more complex than that of a.out
(sections)
.text
.data
.bbs
.init
.fini
.rodata
.data1
Dynamic linking supported sections
Segments are not loaded from the address 0
Stack is loaded above the program code section
Heap is loaded below the program code section
Memory planning for ELF
Similar to a.out, but combining sections which have the same feature
struct sym {
struct sym *next;
int fullhash; ... hash value
char *symname; ... symbol
long value;
int size;
char defined; ... times defined
char referenced; ... times referred
};
1000 ----------------+
+------o B A o----+ 1100
| | o<---+
| r | |
1c00 a| e+---o C D o----+
1d00 b| l| |- - - - - - - -| |
s| a| | | |
o| t| | func o<---+ 2200 ( = 1d00 + 0500)
3000 l| i| +---------------+
u| v| | |
3100 t| e+-->o C+1500 | = 3000 + 100 - 1c00
3500 e+----->o | = 3000 + 500
+---------------+
struct relocation_info {
int address; /* offset */
unsigned int r_symbolnum:24,
r_pcrel:1,
r_length:2,
r_extern:1,
r_baserel:1,
r_jmptable:1,
r_relative:1,
r_copy:1;
};