Daily Shaarli

All links of one day in a single page.

May 26, 2014

Eli Bendersky's website » Load-time relocation of shared libraries

This article’s aim is to explain how a modern operating system makes it possible to use shared libraries with load-time relocation. It focuses on the Linux OS running on 32-bit x86, but the general principles apply to other OSes and CPUs as well.

Obtaining the Address of the GOT

Each code module in your shared library should define the GOT as an external symbol:

extern _GLOBAL_OFFSETTABLE ; in ELF
extern __GLOBAL_OFFSETTABLE ; in BSD a.out

At the beginning of any function in your shared library which plans to access your data or BSS sections, you must first calculate the address of the GOT. This is typically done by writing the function in this form:

func: push ebp
mov ebp,esp
push ebx
call .get_GOT
.get_GOT:
pop ebx
add ebx,_GLOBAL_OFFSETTABLE+$$-.get_GOT wrt ..gotpc

    ; the function body comes here 

    mov     ebx,[ebp-4] 
    mov     esp,ebp 
    pop     ebp 
    ret