当前位置:网站首页>Linker --- linker
Linker --- linker
2022-06-24 11:34:00 【TangZhenye】
The linker
1. background
For regular use IDE The developer of the , Usually it's all right to click a button , Although this greatly simplifies the process , But for us C The language is very, very unfriendly to the underlying developers , A lot of details are masked , It's terrible not to know the inside details , therefore , It is very necessary to explore its principle .
2. Knowledge reserve
- take C The code generation executable object file needs to go through 4 A step
- We need to compile the tool chain to translate the code we write
- The compilation tool chain is just a program , It is just a program used to translate the code that we can read into machine code

- Preprocessing : Use the preprocessor to put the header file 、 Macro definition , Expand directly in the current file intact , And generate .i file
- compile : Use the compiler to .i File translated into assembly language file .s
- assembly : Using the assembler to .s Translate into a relocatable target file (relocatable obj file).o
- The linker : The debut ! This paper focuses on the next linker ld, Because linkers are very important , It needs to be used frequently , A set of .o The files are combined , Create an executable object file (executable object file)
3. What is a linker (Linker)
Borrow the explanation in the book "understanding computer systems in depth" : Take a set of relocatable target files and command line parameters as input , Generate a fully linked 、 Executable target files that can be loaded and run as output . The input relocatable object file consists of a variety of different code and data sections (section) form , Each section is a continuous sequence of bytes . Instructions in a section , The initialized global variables are in another section , Uninitialized variables are in another section .
A section is actually a region
It's kind of similar Linux Archive tool under tar, Assemble multiple files , But it's just Linker One of the functions of , Another function is to reposition !
4. What is in the relocatable object file ?

The above picture is the object file
- .text: Instructions ( Assembly instruction ,C Function after translation , A local variable is actually the operand of an instruction ) Put it in text paragraph
- .data: The initialized global variables and static variables are placed data paragraph
- .bss: Not initialized or initialized to 0 Global and static variables of
- .rodata: Use const Keyword modifies the variable
- .symtab: The symbol table ! It stores the definitions in the program 、 Referenced function 、 Global symbol information . every last .o They all have a symbol table , Record their own definitions and global variable symbols 、 Self defined function symbols and symbols referring to others ( Variables and functions ), Because of the existence of this table , The linker can put a large number of .o Files are linked to generate executable files .
- Others don't go into details
5. Symbols and symbol tables
Each relocatable target module m They all have a symbol table , It contains m Information about the symbols defined and quoted . In the context of the linker , There are three different symbols :
By module m Define global symbols that should be able to be referenced by other modules
Defined by other modules and defined by modules m The global symbol of the reference . These symbols are called external symbols , Corresponding to the non static... Defined in other modules C Functions and global optimizations
Only by module m Local symbols defined and referenced . They correspond to bands static Attribute C Functions and global variables The amount . These symbols are in the module m You can see it anywhere in the world , But it can't be referenced by other modules .
6. Symbol resolution
Symbol table Linker There are two kinds of information , One is the symbol that the current target file can provide to other target files , The other is the symbols that other files need to provide to the current target file . I have this information , The linker will be able to make symbolic decisions .
For those references defined in the same module ( Same target file ) A reference to a local symbol in , Symbolic parsing is very simple and clear . The compiler allows only one definition for each local symbol in each module . Static local variables also have local linker symbols , The compiler also ensures that they have unique names .
Reference resolution of global symbols is much more difficult , When the compiler encounters a symbol that is not defined in the current module , It is assumed that the symbol is defined in some other symbol , Generate a linker symbol table entry , And give it to the linker . If the linker cannot find the definition of the referenced symbol in any of its input modules , Just output an error message , And terminate .
7. Link static libraries
Not over and on ......
8. relocation
Not over and on ......
边栏推荐
- [the lottery in May has ended, and the list of winners has been announced] special session of techo youth university open course database
- Jenkins performance test
- Disaster recovery series (II) -- enterprises' one-stop disaster recovery construction with the help of cloud platform?
- Maui's way of learning -- Opening
- Investing in a good navigation framework from the beginning of the jetpack compose tutorial will help you save a lot of migration work later
- SwiftUI Swift 内功之 Swift 中的属性观察者 didSet 与 willSet
- A fault record of misoperation dhclient script
- 程序员大部分时间不是写代码,而是。。。
- 把腾讯搬到云上,治愈了他们的技术焦虑
- Cause analysis of frequent crash and restart of easynvr-arm cloud terminal
猜你喜欢

程序员大部分时间不是写代码,而是。。。

Tools and methods - use code formatting tools in source insight

Qt: 判断字符串是否为数字格式

How to develop hospital information system (his) with SMS notification and voice function

美团基于 Flink 的实时数仓平台建设新进展
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/fa/e52bd8a1a404a759ef6ba88e8da0f0.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

Qt: judge whether the string is in numeric format

Moving Tencent to the cloud cured their technical anxiety

【数字IC/FPGA】Booth乘法器

“一次编写,运行各端”,高通重磅发布 AI 软件栈!
随机推荐
I want 18K. Can I pass it?
《opencv学习笔记》-- CV::Mat类
AXI低功耗接口
Opencv optical flow prediction and remap remapping function usage
保险APP适老化服务评测分析2022第06期
《opencv学习笔记》-- 分离颜色通道、多通道混合
Oxylabs live online: website capture demo
u盘安装kali并且持久化
GLOG从入门到入门
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
It's so difficult for me. Have you met these interview questions?
qt -- QTabWidget 中支持拖拽TabBar项
[206] use PHP language to generate the code of go language
怎么申请打新债 开户是安全的吗
What is the bin file for? How to open the file correctly
System design: key features of distributed systems
New progress in the construction of meituan's Flink based real-time data warehouse platform
How to develop hospital information system (his) with SMS notification and voice function
集群控制管理
【206】使用php语言去生成go语言的代码