c - How does dynamic linker changes text segment of process? -


if understand correctly when user tries execute dynamically linked executable (with execve("foo", "", "")) instead of loading text segment of "foo" dynamic linker loaded (ld-linux.so.2) , executed. have load libraries required program ("foo") run , change addresses in "foo" , pass control foo, how acomplished?

how (what system call uses) , dynamic loader load libraries , "foo"s code , data in memory (i guessing can't use malloc or mmap , jump code since should impossible, right? seems unlikely creates temp file whith complete executable (like staticlly linked one) , calls exceve again.).

the actual implementation quite complex builds on top of elf, quite complex tries accommodate many scenarios, conceptually it's quite simple.

basically (after library dependencies located , opened) it's couple of mmaps, mprotects, modifications implement linking binding symbols (can deferred), , jump code.

ideally, linked shared libraries compiled -fpic/-fpic allow linker place them anywhere in processes address space without having write .text section (=executable code) of library. such library/executable call functions other libraries via modifiable table, linker fix (probably lazily) point actual locations has loaded dependent library. access variables 1 shared library indirected.

limiting modifying library data/code as possible allows marking sections of code marked read (via mmu / mprotect system call) , mapped memory that's shared among processes use particular library.


to idea of happens @ syscall level, can try e.g.:

strace /bin/echo hello world 

and syscalls sbrk included (=setting heap / .data segment) should doings of dynamic linker.


(malloc indeed unavailable linker malloc feature of c library, not system. malloc growing , managing heap section , potentially mmapping other separate blocks , managing writable "heap", , dynamic linker isn't concerned these sections of process image, writable indirection tables , maps libraries).


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -