当前位置:网站首页>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 ......
边栏推荐
- How to use arbitrarygen code generator what are the characteristics of this generator
- [graduation season · attacking technology Er] three turns around the tree, what branch can we rely on?
- [deep learning][pytorch][original]crnn trains loss on the higher version of pytorch as a solution for Nan
- [206] use PHP language to generate the code of go language
- The record of 1300+ times of listing and the pursuit of ultimate happiness
- Today's sleep quality record 76 points
- [net action!] Cos data escort helps SMEs avoid content security risks!
- Any and typevar make the automatic completion of IDE better
- Jetpack Compose 教程之 从一开始就投资于良好的导航框架将帮助您在之后节省大量的迁移工作
- Reading at night -- about microservices and containers
猜你喜欢

New progress in the construction of meituan's Flink based real-time data warehouse platform

"Write once, run at all ends", Qualcomm released AI software stack!

GTest从入门到入门

【毕业季·进击的技术er】绕树三匝,何枝可依?

GLOG从入门到入门

First acquaintance with string+ simple usage (I)

计组-总复习

FreeRTOS概述与体验

Basic path test of software test on the function of the previous day

【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习
随机推荐
Using the collaboration database query of Poole in laravel5.6
RPM installation percona5.7.34
Adobe Photoshop using the box selection tool for selection tutorial
[206] use PHP language to generate the code of go language
图片的可视化呈现有效增强大屏吸引力
10 zeros of D
[activities this Saturday] NET Day in China
Basic path test of software test on the function of the previous day
About the unsupported instruction set SSE 4.2 of CPU in virtualization
Step 3: access the API interface for inquiry of SF express doc No. [express 100api interface]
Programmers spend most of their time not writing code, but...
Realization of alarm clock with AHK
【毕业季·进击的技术er】绕树三匝,何枝可依?
如何开发短信通知和语音功能医院信息系统(HIS系统)
链接器 --- Linker
Déplacer Tencent sur le cloud a guéri leur anxiété technologique
Audio knowledge (III) -- MFCCs code implementation
2021-06-03: Boolean operation. Given a Boolean expression and an expected cloth
"One good programmer is worth five ordinary programmers!"
工具及方法 - 在Source Insight中使用代码格式化工具