Dynamic Linking
Resolve all the unresolved offsets. According to the library. Because it knows exactly where the library is sitting in the memory.
Two cases:
Dynamic Linking
The load time is longer: because the dynamic linker will resolve all the offsets. Happens at the load time.
- Using indirect addressing?
Lazy Linking
When call the library during execution, and it has unresolved offsets, it will call the dynamic linker to resolve all the offsets during the execution.
How the dynamic linker works
Example
sets off its own trampoline Imagine a stack, there is a stack, with a GOT global offset table. It’s called the procedure linkage table load double word is loading an address of the statick data segment, and load into the address of t3.
Look up PLT: procedure linkage table and GOT
Static Linking vs Dynamic Linking
- Static Linking: Libraries are compiled into the executable at compile time. Advantages include easier distribution and no dependency worries at runtime. Disadvantages: larger executable size, and updates to libraries require recompilation of the application.
- Dynamic Linking: Libraries are linked at runtime. Advantages include smaller executable size, ease of updating libraries without recompiling the application, and reduced memory usage. Disadvantages: dependency management can be complex, and there’s a risk of DLL hell.
Advantages
Advantages:
- Often-used libraries (for example the standard system libraries) need to be stored in only one location, not duplicated in every single executable file, thus saving limited memory and disk space.
- If a bug in a library function is corrected by replacing the library or performance is improved, all programs using it dynamically will benefit from the correction after restarting them. Programs that included this function by static linking would have to be re-linked first.