Determine the symbol references (symbol resolution)
Merger related. (o) file
Identify new address of each symbol
In order to fill the new address
2 and 4 collectively known as heavy positioning
The ELF three kinds of target file
1. The relocatable object files (. O)
Each. O documents are made by the corresponding. C file generated by the compiler and assembler, contain binary data, can be at compile time with other relocatable object files (. O) link, creating a executable (a.out).
$GCC -c test. C
2. The executable (a.out)
Target executable file generated by the linker, contains the binary code and data, can be directly copied into memory and execute,
: $GCC -o test test. C
3. The Shared object file. (so)
A special type of relocatable object files, can be dynamically loaded when loading or run into memory and link (dynamic link),
The ELF relocatable object files
1. The relocatable object file format
ELF header: contains 16 bytes identity information, file types (. O, the exec,. So), machine type, section head table offset, section head table table size and table item number,
Text: the compiled program machine code (binary instruction), (code)
Rodata: read-only data, such as printf format strings, switch jump table, etc.,
.data: global and static variables are initialized, local variables are stored in the stack, at run-time. Neither appear in the data, also don't appear in. BSS section,
BSS: an uninitialized global and static variables, is just a placeholder, does not occupy any actual disk space,
. Symtab: a symbol table (symbol table), it is stored in defined in the program and reference functions and global variables of information, does not contain table of local variables,
. The rel. Text: text section of relocation information, used in the instruction to modify the code in the address information,
. The rel. Data: data relocation information, used to be module USES or global variables for relocation of the definition of information,
. The debug: a debug symbol table, the entry is defined type of local variables and definition, procedure and references to global variables, defined in C and the original file (GCC -g),
Strtab: a string table, its content includes. Symtab and. The debug section in the symbol table, as well as the section head of section name, string table is null-terminated string sequence,
Line: the original line and in the C program. The text section mapping between machine instructions,
Section head table, each section of the section name and offset,
We can see it through the readelf relocatable object files of the ELF header contains information, the first a simple code
//the main int o the sum (int * a, int n); An int array [2]={1, 2}; Int mian () {int val=sum (array, 2); Return val. }
2. The ELF header
We can use the readelf -h command to ELF header information
Magic: Magic number, which is used to name the file is an ELF object files, the first byte 7 f is a fixed number of; At the back of the 3 bytes is E, L, F ASCII form of three letters,
From which we can see the basic information, such as file type, data format, the ELF header version number, the type of operating system (OS/ABI), the ELF file types (here is a relocatable object files), platform type and the version number of the target machine, etc.,
Relocatable file not to perform, no program header table, so that head start program, program size, Number of program headers (program header table where the item Number) to 0,
The Start of section headers: offset of section head table (in this case 720 hexadecimal representation for 2 d0, so the location of the table from 0 x2d0 head section,)
This example section head table has 12 sections, each section head of 64 bytes, namely the section header table total size is 64 * 12 bytes
String table index section: section head string table in the table (strtab) index value,
3. The section header table
We can use the readelf -s command to view the section head table information
Relating to a total of 12 tables for each table to 64 bytes, because it is a link to view, so the corresponding virtual address all zero,
Given a field is pointed out in the ELF header section head mark starting position, here is 0 x2d0, is under the instruction that line,
Only. Text, data,, BSS, rodata allocates storage,
ELF header account for 64 - byte 0 x40 byte, so. The text of the offset value of 0 x40 plus his size, 0 x21 available then offset value,
Section head in the table for all the starting position and size of section record down,
The distribution of section 4.
Now, by the ELF header section header table of information, as well as the section head table offset and the size of the values in each table, we can draw the distribution of different section file
On the right side of the size of the section Numbers, left the figures show that the migration, () is expressed in hexadecimal
Section head table size calculated in the ELF header,
5. The symbol table
The symbol table holds the program realize all or the use of global variables and functions,
The Value of the Value is the address of symbols,
Ndx: index, the section head table index,
(UNDEF undefined symbols, ABS said should not be redefine symbols, COMMON says not assigned position uninitialized symbol)
CodePudding user response:
Thanks for sharing, BBS is used to discuss problems, basic please write personal blog,