当前位置:网站首页>3. compilation and linking principle

3. compilation and linking principle

2022-06-23 01:40:00 For financial freedom!

 Insert picture description here
 Insert picture description here
 Insert picture description here
precompile ( Preprocessing ):# The first command handles
compile :gcc -O1(O2 O3) Optimization level
assembly : Generate .o Relocatable binaries ( Platform specific machine code 0101 such )

.a :Linux Static library files .lib :Windows Static library file under
link : all .o file + Static library files .lib Join the link

  1. Step one : all .o Merging of file segments , After the symbol table is merged , Perform symbol parsing
  2. Step two : Relocation of symbols ( relocation ), This is the core of the link

 Insert picture description here
main In file gdata、sum It refers to external variables 、 function , stay main.o Will symbols be generated in the ?
 Insert picture description here
objdump -t command : Look at the symbol table :
 Insert picture description here
l:local
g:global
stay main.o In the segment information of ,gdata 、_z3sumii stay main They are used in the document , But I don't know how to define it , So the symbol can only be given to them for the time being UND(undefined). And in the sum It is known in the document that sum function gdata Know how to define , So there's a sign !
During the compilation process , Symbols are not assigned virtual addresses ! You can see that after compilation ,main.o These places are located at 0:
 Insert picture description here
readelf -S main.o: View segment information

link :
main.o sum.o
The segments will be merged ,.text .data .bss And so on ,UND When merging , You will find this variable in other files , Find the place where the symbol is defined (text data perhaps bss etc. ) After successful symbol parsing , Assign virtual addresses to all symbols ,data gdata sum These must have addresses , All those places were 0000, Now you need to write down the specific address , This is called symbol redirection !

Manual link : The entry function is specified as main, You can see all the variables 、 Functions have specific addresses ,*UND* It's gone !
 Insert picture description here
 Insert picture description here
 Insert picture description here
a.out On the disk ,elf header Contains the entry address of the program ,program headers Tell the system to load text data paragraph , Load into memory ! hold text,data bss Segments are mapped to the virtual address space of the process ( Several blogs have talked about the virtual space of processes ), Execute the address mapping page exception handler to allocate physical memory !

原网站

版权声明
本文为[For financial freedom!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514414182.html