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

jal ra, printf
.
.
.
jal ra, printf
.
.
.
printf ld t3, "got printf"(gp)
jalr ra, t3
stub1: li ID_4_printf
jal dynamic_linker
 
 
 
dynamic linker

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

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.