Chapter7 Linking(2)——CSAPP读书笔记

How linkers resolve Multiply defined global symbols 

Rules1. Multiply strong symbols are not allowed

2. Given a strong symbol and multiple weak symbols, choose the strong one.

3. Given multiple weak symbols, choose any of them.



在IA32 linux上进行编译连接时不会报错,但是实际上是有潜在问题的。由于32位linux上int是4字节而double是8个字节,此时bar5中会将x提升到double类型,此时会造成x在内存中地址的改变‼这点是我们通常会忽视的状况,很有可能很多莫名其妙的错误来源于此…以后需要注意。
GCC -fno-common

加上此标志后会就重复声明的全局变量进行提示,实际上,我感觉最好是避免全局变量的重复定义,感觉纯粹的OO语言里面这种现象出现的概率会比较小,但是还是需要注意的地方啊。
此时我们可能会比较疑惑的是…
Overload的function肿么办?因为名字是一样的嘛。因为它们的arg list是不一样的,因此编译器会根名字和arg list重新打包命名交给linker处理。这个重新命名的过程就是mangling,反之称为demangling。
mangling的机制java和c++是互相兼容的。首先类名由原类名和类名的字符个数组成。foo就是foo3。
例如
Foo:: bar(int,long)

变成了 bar__3Fooil
也就是说,mangling的方法是:
OriginalMethodName__MangledClassNameAndLettersOfEachArguments“The general rules for libraries is to place them at end of command line
也就是说,静态链接库最好放在编译命令行的最末,不然的话,若是首先对.a文件进行编译连接,则此时集合U(unresolved symbols)中没有任何元素,连接器不会将该静态库添加到集合E(生成可执行文件需要的文件)中,接着若是链接一个object文件中引用到了静态库中的文件时,连接器就会报告unresolved symbol这种错误。
所以在编译连接时,越是基本的库约要放在较后的位置进行处理。若是各个库之间没有相互引用,则无所谓放置的位置。
Libraries can be repeated on the command line if necessary to satisfy the dependence requirements

也就是说…要是你愿意…同一个库你可以在命令行重复输入多遍,但是最好不要这么做…这样的话建议将能合并的库都合并了(ーー;)
例题:写个最短的命令行解决编译问题,要解决各种依赖关系。a->b表示a依赖b
p.o->libx.a->liby.a and liby.a->libx.a->p.o
solution:
 gcc p.o libx.a liby.a libx.a

7.7 Relocation
Relocation consists of two steps 1. Relocating sections and symbol definitions. After this step, every instruction and global variable in the program has a unique run-time memory address. 2. Relocating symbol reference within sections. To perform this step, the linker relies on data structures in the relocatable object module known as the relocatable entries.
根据deassembler课题看到,重新定位需要相应的重定位类型,地址以及symbol。
之后成为一个可执行文件,unix shell会根据其类型调用系统驻留程序,即loader,进行加载运行。
Disadvantage :
Static libraries, like all software, need to be maintained and updated periodically
7.7 Relocation
Relocation consists of two steps 1. Relocating sections and symbol definitions. After this step, every instruction and global variable in the program has a unique run-time memory address. 2. Relocating symbol reference within sections. To perform this step, the linker relies on data structures in the relocatable object module known as the relocatable entries.
根据deassembler课题看到,重新定位需要相应的重定位类型,地址以及symbol。
之后成为一个可执行文件,unix shell会根据其类型调用系统驻留程序,即loader,进行加载运行。
Disadvantage :
Static libraries, like all software, need to be maintained and updated periodically

7.10 Dynamic linking with shared libraries
Shared libraries are shared in 2 different ways:
First, in any given file system, there is exactly one .so file for a particular library. Second, a single copy of the .text section of a shared library in memory can be shared by different running processes.
Linux系统下允许应用程序调用外部的动态链接库的接口是:
#include <dlfcn.h>
void *dlopen(const char *filename, int flag);
该函数用于加载动态链接库filename
#include <dlfcn.h>
void *dlsym(void *handle, char *symbol)
该函数通过前一个打开的动态链接库的句柄,以及symbol名,返回symbol的地址,没有的话返回NULL
#include <dlfcn.h>
int dlclose(void *handle);
若无其他动态链接库占用,可对当前动态链接库进行卸载操作。
#include <dlfcn.h>
const char *dlerror(void);
对于上三个函数调用过程中错误信息的返回。
7.12 Position Independent Code (PIC)
How can multiple process share a single copy of code? Compile library code that it can be loaded and executed at any address. Such code is known as position-independent code(PIC)
不管在何处加载何种object模块,data段永远紧跟在code段之后进行地址的分配。
这样保证了地址的差值是一定的,也就是说实际上使用的是相对地址,这样就不会有绝对地址会造成的问题了。所以编译器在段一开始处便会有一个偏移地址对照表,方便之后根据对照表进行地址的查找。
Tools for manipulating object files AR: Creates static libraries, and inserts, deletes, lists, and extracts members. STRINGS: Lists all of the printable strings contained in an object file. STRIP: Delete symbol table information from an object file. NM: Lists the names and sizes of the sections in an object file. SIZE: Lists the name and sections in an object file. READELF: Displays the complete structure of an object file, including all of the information encoded in the ELF header, subsumed the functionality of SIZE and NM. OBJDUMP: The mother of all library tools. Can display all the information in an object file. It's most useful function is disassembling the binary instructions in the .text section. LDD: list the shared libraries that is needed at run time.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章