Object files come in three forms:
1. Relocatable object file. Contains binary code and data in a form that can be combined with other
relocatable object files at compile time to create an executable object file.
2. Executable object file. Contains binary code and data in a form that can be copied directly into
memory and executed.
3. Shared object file. A special type of relocatable object file that can be loaded into memory and linked
dynamically, at either load time or run time.
Object file的格式有很多种。不同的平台和规范有不同的格式。 这里主要介绍ELF的格式
______________________________
| ELF Header |
|______________________________|
| .text |
|______________________________|
| .rodata |
|______________________________|
| .data |
|______________________________|
| .bss |
|______________________________|
| .symtab |
|______________________________|
| .rel.text |
|______________________________|
| .debug |
|______________________________|
| .line |
|______________________________|
| .strtab |
|______________________________|
| Setction header table |
|______________________________|
.text : The machine code of the compiled program.
.rodata: Read-only data such as the format strings in printf statements, and jump tables for switch
statements (see Problem 7.14).
.data: Initialized global C variables. Local C variables are maintained at run time on the stack, and do
not appear in either the .data or .bss sections.
.bss: Uninitialized global C variables. This section occupies no actual space in the object file; it is merely
a place holder. Object file formats distinguish between initialized and uninitialized variables for space
efficiency: uninitialized variables do not have to occupy any actual disk space in the object file.
.symtab : A symbol table with information about functions and global variables that are defined and
referenced in the program. Some programmers mistakenly believe that a program must be compiled
with the -g option to get symbol table information. In fact, every relocatable object file has a symbol
table in .symtab. However, unlike the symbol table inside a compiler, the .symtab symbol table
does not contain entries for local variables.
.rel.text: A list of locations in the .text section that will need to be modified when the linker
combines this object file with others. In general, any instruction that calls an external function or
references a global variable will need to be modified. On the other hand, instructions that call local
functions do not need to be modified. Note that relocation information is not needed in executable
object files, and is usually omitted unless the user explicitly instructs the linker to include it.
.rel.data: Relocation information for any global variables that are referenced or defined by the module.
In general, any initialized global variable whose initial value is the address of a global variable
or externally defined function will need to be modified.
.debug: A debugging symbol table with entries for local variables and typedefs defined in the program,
global variables defined and referenced in the program, and the original C source file. It is only
present if the compiler driver is invoked with the -g option.
.line: A mapping between line numbers in the original C source program and machine code instructions
in the .text section. It is only present if the compiler driver is invoked with the -g option.
.strtab: A string table for the symbol tables in the .symtab and .debug sections, and for the section
names in the section headers. A string table is a sequence of null-terminated character strings.