Table of contents
1. Understand links and drivers
1. Static library and its symbol analysis
2. The linker uses the static library to resolve the reference process
Series Article Directory
This series of blogs focuses on the core content of the computer system (2) course of Shenzhen University, and the reference bibliography "In-depth understanding of computer systems" (if you have any questions, please discuss and point out in the comment area, or contact me directly by private message).
The first chapter in-depth understanding of computer system 01 - computer system roaming
The second chapter in-depth understanding of computer systems 02 - the representation and processing of information_@李敬如的博客-CSDN博客
Chapter 3 In-depth understanding of computer systems 03 - machine-level representation of programs
Chapter 4 In-depth understanding of computer system 04 - storage
Chapter 5 In-depth understanding of computer systems 05 - link
synopsis
This blog mainly introduces the related knowledge of in-depth computer system bibliography Chapter 7 link.
1. Understand links and drivers
Linking: The process of collecting and combining various pieces of code and data into a single file that can be loaded (copied) into memory and executed. Executable at compile, load, and run time.
1. Compiler Driver
The process of compiling and executing the program is as follows:
2. link
Introduce links as follows:
Link tasks:
0 ) Implicit task - layout of sections in memory space
1 ) Symbol resolution ( symbol resolution ): establish a link between symbolic references and definitions
2 ) relocation ( relocation ): determine the address for each reference
3. ELF object file
The three common target files for linking are shown in the figure:
Tips: Common name: ELF binary file
4. Relocatable object files
The classic ELF relocatable object file structure is as follows:
The structure of the classic ELF executable object file is as follows (multi-program header table, .init section, two less .rel):
2. Symbols and symbol tables
Each relocatable object module m has a symbol table, which contains symbol information defined and referenced by m, and there are the following three link symbols.
1. Symbol related definitions
Take the following C language program as an example:
Tips: Pay attention to the distinction between stored attributes and scopes - global variables, static global variables, static local variables, and local variables.
Storage on the heap (global variables, static local variables, static global variables)
Storage on the stack (local variables)
global role (global variable)
Local effects (static global variables, static local variables, local variables)
2. Symbol table example
The symbol representation is as shown in the figure:
Tips: data is initialized, bss is an uninitialized static variable, text is only defined (eg. function name).
3. Symbol analysis
The linker rules for resolving multiple defined global symbols are as follows:
Some examples of connection errors are:
How to avoid the above problems:
3. Links to libraries
preprocessing
All comments are replaced with spaces;
Delete all #define, expand all macro definitions;
Handles conditional compilation directives #if, # ifdef , # elif , #else, # endif
Expand #include included files
Preserves the #pragma directives that the compiler needs to use
1. Static library and its symbol analysis
Static library use case and analysis are as follows:
2. The linker uses the static library to resolve the reference process
With the following custom static library
Data structure : maintain three dynamically changing collections E , U and D
E : A collection of relocatable object files, the referenced object files will be copied into the executable file;
U : The collection of unresolved symbols found as the link expands , and the collection is finally empty after a successful link ;
D : a collection of resolved symbols (whether strong or weak) in all input files ;
Algorithm points :
1 ) Initialize E/U/D as empty;
2 ) Scan the file f given on the command line one by one ;
a ) f is an object file : E = EU{f} , D = DU{ defined symbols in f } ,
The symbols corresponding to the relocation entries are matched with D , and the unmatched ones are added to U ;
b ) f is a static library , match the symbols in U with the symbols defined by f , if there is a symbol on matching module m , E = EU {m} , otherwise discard the library.
3 ) Scan ends
U is not empty: the link fails, and the symbols that U cannot resolve are output to the user.
U is empty, the link is successful, the modules in the layout E are spliced into an executable file, and the symbol interpretation
Tips: The order in which libraries appear is important.
An example of the specific analysis process is as follows:
Analysis result:
There are main.o , myproc1.o and the modules they call in E
D has main , myproc1 and the symbols it references
Tips: Linked modules should be specified in the calling order!
An example of a link order problem is as follows:
3. Dynamic library
Disadvantages of static libraries
Library functions are copied to the code segment of each running process, which causes a huge waste of memory space for a system running hundreds of processes concurrently
Library functions are merged into executable object files , and thousands of executable files are stored on the disk , resulting in a huge waste of disk space
Programmers need to pay attention to whether there is a new version of the function library, and must download , recompile and link regularly , which is inconvenient to use and time-consuming to compile
Dynamic link shared library ( shared library , also known as shared library or dynamic link library)
Object files, containing code and data
Separated from the program, there is only one copy both on disk and in memory
Can be dynamically loaded and linked at load time or run time
Window calls it a Dynamic Link Library ( Dynamic Link Libraries , .dll file )
Linux calls it a dynamic shared object ( Dynamic Shared Objects, .so file )
An example of building a dynamic library is as follows:
The basic linking process is summarized in the following figure:
Summarize
The above is the core knowledge for in-depth understanding of the seventh chapter of computer systems-links.