Preprocessing -> Compiling -> Assembling -> Linking
A linker mainly does two tasks: symbol resolution, and relocation. Symbol resolution relates every symbols referenced in the target object file (both code and data) to their definition. Relocation generate an absolute address for every symbols according their relative addresses, since code starts from address 0x00 after assembling.
Every object file will contain their own symbol tables. There are symbols defined in and not in the current object file. Function and initialized variables are strong symbols, and uninitialized variables are weak symbols. Rules for symbol resolution in Unix linker are defined as followes:
Rule.1 Only one strong symbol is allowed, otherwise is a linker error.
Rule.2 If there is a strong symbol and other weak symbols, choose strong symbol.
Rule.3 If there is no strong symbol, then choose any weak symbol at random.
Once the choice is made, symbols in object files not in the object file where choosen one stays will be resolved to the choosen one, i.e, reference to the choosen one.
Several important sections in executable files' headers are: .text, .rodata, .data, .bss. .rel.text, .rel.data. Section .text contains executable machine codes, .rodata contains readonly data, while section .data and .bss contain initialized and uninitialized global variables, respectively. Section .rel.text and .rel.data contains table needed for code and data relocation respectively.
Static libraries take additional space and memory because object file needed in final executables will be linked, dynamic libraries spare space and memory at the cost of time overhead because of dynamic linking at loading times.
(this is a summary on chapter 7 of the book "computer systems, a programmer's perspective")
评论