linuxdlloperating-systemshared-librariesvirtual-memory

Is lazy binding of shared library achieved using lazy allocation of virtual memory by OS?


In Linux ( unsure for Windows, any knowledge regarding is appreciated too ), a technique named lazy binding is used for boosting performance when only a small portion of shared library is required, imagine when you invoke a program with -h --help arg, etc.

I was wondering how is it implemented? is it related to lazy allocation technique of OS virtual memory management? i.e., instead of loading all instructions into memory, a mapping to unmapped memory is conducted first, and the valid memory is loaded only when it is really needed/referred during run time.


Solution

  • No, those are unrelated, although both contribute to performance in that case.

    Library binding is implemented with an array of function pointers. The array itself is statically allocated by the linker, and is a part of the executable. Binding involves filling this array with the actual addresses of functions.

    Lazy binding skips the array filling. Instead, the array is pre-filled with a pointer to a binding routine. When a function is being called - it gets bound: their array slot is replaced with an actual address. Functions that are never called, never get bound.