Linker
主要功能
- 符号解析:将符号应用和符号定义关联起来
- 重定位:生成从地址0开始的代码和数据节
ELF (object file)
executable and linkable format
. 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.
. data Initialized global and static 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 and static C variables, along with any global or static variables that are initialized to zero. This section occupies no actual space in the pbject file; it is merely a placeholder.
. symtab A symbol table with information about functions and global variables that are defined and referenced in the program.
. 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 pre~~nt 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.
符号和符号表
- Global symbols that are defined by module m and that can be referenced by other modules.
- Global symbols"that are referenced by module m but defined by some other module.
- Local symbols that are defined and referenced exclusively by module m.
在符号解析时,编译器不会报错,认为这个函数定义在其他地方,linker在链接时找不到会报错
Rule 1. Multiple strong symbols with the same name are not allowed.
Rule 2. Given a strong symbol and multiple weak symbols with the same name, choose the strong symbol.
Rule 3. Given multiple weak symbols with the same name, choose any of the weak symbols.
静态库
相关函数编译为独立的目标模块,封装成单独的静态库文件。linker值复制被程序引用的目标模块,减少了可执行文件在磁盘和内存中的大小。
.a 存档文件 -> .o 只有相关的函数
动态库
目标模块在运行或加载时,可以加载到任意内存地址。在静态链接时需要加载一些symbol table
重定位
- 合并节和符号定义
- 重定位符号引用
executable file
Each' program in a Linux , system runs in the context of a process with its own virtual address space. When the shell runs a program, the parent shell process forks a child process that is a duplicate of the parent. The child process invokes the loader via the execve system call. The loader deletes the child's existing virtual memory segments and creates a neW set of code, data, heap, and stack segments, The new stack and heap segments are initialized to zero. The new code and data segments are initialized to the contents of the executable file by mapping pages in the virtual address space to page-size chunks of the executable file. Finally, the loader jumps to the start address, which eventually calls the application's main routine. Aside from some header information, there is no copying of data from disk to memory during loading. The copying is deferred until the CPU referencb a mapped virtual page, at which point the operating system automatically transfers the page from disk to memory using its paging mechanism.