ELF format of the linux target file
<Span style = "font-family: Arial, Helvetica, sans-serif; background-color: rgb (255,255,255 ); "> </span> <span style =" font-family: Arial, Helvetica, sans-serif; background-color: rgb (255,255,255 ); "> currently, the most popular executable file formats on PCs are PE files in windows and ELF files in linux. They are all COFF file variants. The intermediate files whose source code is compiled but not linked at the time of the target file have almost no difference with the format of the executable file. Therefore, the target file (. o files) use the same storage format as executable files. </Span>
In linux, in addition to executable files, there are several files stored in the ELF format, including dynamic library files (. so), relocable file (. o), Core Dump File (the system stores some process information to this type of file when the process is terminated ). We can use the file command to view the file type eg:
xiang@xiang:~/workspace/linux$ file primprim: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xd54ce2ecedb22482c79f597093ff8c0b8f7c7113, not strippedxiang@xiang:~/workspace/linux$
The format of the ELF file contains the file header, the code segment (. text), data segment (. data) data segment not initialized (. through the objdump command, you can view the information of each segment.
xiang@xiang:~/workspace/algorithm$ g++ -c prim.cpp -o prim.oxiang@xiang:~/workspace/algorithm$ objdump -h prim.oprim.o: file format elf32-i386Sections:Idx Name Size VMA LMA File off Algn 0 .text 0000031f 00000000 00000000 00000034 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data 00000000 00000000 00000000 00000354 2**2 CONTENTS, ALLOC, LOAD, DATA 2 .bss 003d3c21 00000000 00000000 00000360 2**5 ALLOC 3 .rodata 00000008 00000000 00000000 00000360 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .ctors 00000004 00000000 00000000 00000368 2**2 CONTENTS, ALLOC, LOAD, RELOC, DATA 5 .comment 0000002c 00000000 00000000 0000036c 2**0 CONTENTS, READONLY 6 .note.GNU-stack 00000000 00000000 00000000 00000398 2**0 CONTENTS, READONLY 7 .eh_frame 000000dc 00000000 00000000 00000398 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATAxiang@xiang:~/workspace/algorithm$ objdump -h prim
1. the file header contains the magic number, machine length, version, running platform, file type, entry address, and short message of the file, here, the magic number is used to tell the system file type. For example, the magic number of the ELF file is the DEL character + ELF,
2. The code and data segments are very familiar. Note that the. data Segment contains initialized global variables and static variables. The global face changing and static variables that are not initialized when the bss segment contains. Some files still exist. The rodata segment that stores read-only data segments. In addition, the default value of the variable in the. bss segment is 0. Therefore, the. bss does not actually occupy space in the elf file, but only needs to allocate virtual memory space during loading.
3. A field table is the most important data structure in addition to the file header in the elf File. It contains information about each segment of elf, such as the segment name and length of each segment, in the file offset, read and write permissions, the segment structure in the elf file is determined by the segment table. The compiler and the linker loader access each segment through the segment table, the position of the field table is determined by e_shoff in the file header, which is the relative offset in the file.
4. Relocate a table. The linker is used to link Multiple Target files. variable references or function references in one file may be defined in other target files, these symbolic references must use an absolute address. These symbol references need to be placed in a specific segment, which is to relocate the table. The relocation information of the code segment is placed in. rel. text, and the relocation information of the data segment is placed in. rel. data.
5. The string table contains many strings in the ELF File, such as segment names and variable names. because the length of the string is often uncertain, it is difficult to use a fixed structure to represent it, A common method is to place all strings in a separate table. To use this string, you only need to reference it.
6. symbol table: In order to link different target files, mutual reference between different target files must be solved. To solve this problem, there are many symbol tables in each file, including the global symbol table, external symbol table, and local symbol. The global symbols include the Global symbols defined in the target file. These symbols can be referenced by other target files. External symbols include references in this target file, but it is not defined in the target file.
Local symbols are only visible within the compilation unit. They are not actually used in the Link process and are generally ignored by the linker.
Symbol also involves issues such as function signature, strong symbol, and weak symbol.