How does the resolve function in ELF files know what libs are the symbols in?

巧了我就是萌 提交于 2019-12-11 18:52:40

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/15190185/how-does-the-resolve-function-in-elf-files-know-what-libs-are-the-symbols-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!