导出符号是什么?
符号,是软件链接过程的用到的术语。
我们编写自己的软件生成目标文件,但是通常情况下,只有自己的目标文件是不够的。
比如我们用c++编写的程序,必然要与C++的运行时库链接在一起才能工作,否则我们在代码中使用的fopen或者std::cout之类的符号要到哪里去找。
在链接的过程中,相当于是让目标文件之间镶嵌到一起,因此最重要的是找到精确的接入点,这就是符号。
符号是目标文件之间对地址的引用,即对函数和变量的地址的引用。
通常是一个目标文件A定义符号,而另外一个目标文件B引用A定义的符号,符号就成了目标文件A和B镶嵌到一起的接入点。
符号在A中是导出符号,在B中是导入符号。
换句话说,符号就是地址,当CPU在执行链接后的目标文件组合体时,它并不关心符号的问题,而是关心某一条函数调用指令指向的具体地址。
从编写代码时的符号到程序执行时的地址,要经过以下两个步骤:
- 链接,作用是将分散在几个不同的目标文件中的符号进行解析,将符号解析成合并后的目标文件内部的符号(是在文件层次)
- 加载,作用是将目标文件按照其指明的方式,加载到物理内存(实际上是线性内存)中的某一个范围,这时符号就真正变成了地址,可以作为call指令的参数了
CALL Instruction Description
Saves procedure linking information on the stack and branches to the called procedure specified using the target operand. The target operand specifies the address of
the first instruction in the called procedure. The operand can be an immediate value,
a general-purpose register, or a memory location.
This instruction can be used to execute four types of calls:
• Near Call — A call to a procedure in the current code segment (the segment
currently pointed to by the CS register), sometimes referred to as an intrasegment call.
• Far Call — A call to a procedure located in a different segment than the current
code segment, sometimes referred to as an inter-segment call.
• Inter-privilege-level far call — A far call to a procedure in a segment at a different
privilege level than that of the currently executing program or procedure.
• Task switch — A