ELF文件中的resolve函数如何知道什么是libs是符号? [英] How does the resolve function in ELF files know what libs are the symbols in?
问题描述
在符号表中只有符号名称的偏移,但是没有符号所属的lib的信息。
typedef struct {
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info; / * bind,type:ELF_32_ST _... * /
unsigned char st_other;
Elf32_Half st_shndx; / * SHN _... * /
} Elf32_Sym;
当在运行时调用解析函数时,符号表和另一个DWORD的偏移量被传递给它。这个DWORD与符号的lib有什么关系吗?如果不是,那么解析函数如何找到符号的库?
解析函数查找符号的lib
通过对加载到进程(加载器维护)的库进行线性搜索。 / p>
定义给定函数wins的第一个库。这允许例如。 libtcmalloc.so
定义 malloc
, calloc
等并在之前使用
LD_PRELOAD
或链接 libtcmalloc
code>,因此 libc
之前的加载器列表中出现 libtcmalloc
。
搜索在库的数量上是线性的,但在每个库内是 O(1)
,因为每个库都有一个散列表其符号(在 .hash
或 .gnu_hash
部分)。
In the symbol table there is only the offset of symbol name but no information on which lib the symbol belongs to.
typedef struct { Elf32_Word st_name; Elf32_Addr st_value; Elf32_Word st_size; unsigned char st_info; /* bind, type: ELF_32_ST_... */ unsigned char st_other; Elf32_Half st_shndx; /* SHN_... */ } Elf32_Sym;
When the resolving function is called at runtime, the offset of the symbol table and another DWORD are passed to it. Does that DWORD have something to do with the symbol's lib? If not then how does the resolving function find the lib of a symbol?
how does the resolving function find the lib of a symbol
By linear search of the libraries loaded into the the process (which the loader maintains).
The first library to define the given function "wins". This allows e.g. libtcmalloc.so
to define malloc
, calloc
, etc. and to override these symbols (you either use LD_PRELOAD
, or link libtcmalloc
before libc
, so libtcmalloc
appears in the loader list before libc
does).
The search is linear on the number of libraries, but is O(1)
inside each library, because each library has a hash table of its symbols (in the .hash
or .gnu_hash
section).
这篇关于ELF文件中的resolve函数如何知道什么是libs是符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!