ares94
ares94

Reputation: 163

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

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?

Upvotes: 0

Views: 619

Answers (1)

Employed Russian
Employed Russian

Reputation: 213897

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

Upvotes: 1

Related Questions