Journeying into the Dynamic and Static Library Rabbit Hole: Exploring a New Kingdom
In the Name of Allah (SWT) the Merciful, the Compassionate or [ ﷽ ] Bismillahir Rahmanir Raheem (Arabic: ‘بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ)
Today was a dive into the realm of Dynamic Linking and Static Linking. As I revisited static and dynamic libraries, I found myself wrestling with questions that took me deeper into the intricate workings under the hood of these fundamental software components. Rather than simply recounting what they are and how they are used, I’ve opted to delve into the questions that emerged during my study session, materials exist out there that go into how to use them, i think i might have written about it, i’ll link to blog post that can easily guide you.
Q: How do dynamic libraries work?
While studying dynamic libraries, I was struck by the additional layers of abstraction that come into play. Here’s a glimpse into what I uncovered:
- Library Installation: Most operating systems come pre-equipped with a standard set of libraries. Users also have the freedom to install additional libraries. Dynamic libraries don’t compile with the program; instead, they act as references because the operating system already has these stored up. When the OS encounters a library reference, it loads that library into memory.
- Library Resolution: When a dynamically linked program is executed, the OS hunts for the required libraries in predefined paths such as `/lib`, `/usr/lib`, and other specified environmental variables.
- Dependency Management: If a required library is missing, outdated, or incompatible, it can lead to program breakdowns. Understanding this aspect sheds light on why managing dependencies is critical in dynamic linking.
Q: How does the operating system differentiate between static and dynamic libraries?
The operating system loader, also known as the dynamic linker/loader, plays a pivotal role in this distinction:
Dependency Identification: After loading the executable into memory, the OS loader scrutinizes the executable’s header to identify dynamic dependencies (shared libraries) required by the application. These dependencies, specified during compilation, are stored within the executable.
Metadata Examination: The dynamic loader pores over metadata or header information stored within the executable file to ascertain whether it has dependencies on shared libraries and whether those libraries are dynamically linked.
Compilation Insights: When an executable is compiled with dynamic linking, metadata containing information about its dependencies on shared libraries is embedded within the executable file. Analyzing this metadata allows the dynamic loader to effectively manage the loading and resolution of shared library dependencies, ensuring successful execution on the target system.
The dynamic loader is designed to recognize when an executable has dependencies on shared libraries and is equipped to handle the loading and resolution of those dependencies at runtime.
When an executable is compiled to be dynamically linked, information about its dependencies on shared libraries is typically stored in its executable file’s header or metadata. This information includes the names and paths of the shared libraries that the executable requires.
When the operating system loads the dynamically linked executable into memory, the dynamic loader examines this metadata to identify the required shared libraries. It then proceeds to search for these libraries in the predefined library search paths and load them into memory as needed.
Once the shared libraries are loaded, the dynamic loader resolves any unresolved symbols referenced by the executable by mapping them to their corresponding addresses in the loaded libraries. This allows the executable to access the functionality provided by the shared libraries.
Q: How would the compiler behave when #include <lib.h> exist on both the statically and dynamically linked libraries ?
In C and C++ programming, the `#include <stdio.h>` directive is a preprocessor directive that tells the compiler to include the contents of the `stdio.h` header file during the compilation process. This directive is used to provide declarations for functions like `printf()` and `scanf()` and other input/output-related functionality.
When you compile a C or C++ program, the compiler processes `#include` directives by locating and including the contents of the specified header files. However, the inclusion of standard library headers like `stdio.h` is unrelated to the concept of dynamic or static linking.
In the context of dynamic and static linking:
1. Static Linking: If you statically link a C or C++ program, the compiler includes the necessary code from the standard library (such as the code for `printf()` and `scanf()`) directly into the executable binary. This means that the contents of `stdio.h` are not directly embedded into the executable; rather, the compiler includes the compiled code for the functions declared in `stdio.h`.
2. Dynamic Linking: In the case of dynamic linking, the situation is different. When a program is dynamically linked, the program’s executable contains information about the shared libraries it depends on, but it doesn’t contain the actual code of those libraries. Instead, the operating system’s dynamic loader (or linker) is responsible for loading the necessary shared libraries into memory at runtime and resolving references to functions and variables defined in those libraries.
In either case, whether the program is statically linked or dynamically linked, the `#include <stdio.h>` directive is used during the compilation process to provide declarations for standard library functions. However, the actual linking process (static or dynamic) determines how the code for those functions is included in or referenced by the executable binary.
phew!, that was one lengthy article sorry i know i said “short”, but i do hope you enjoyed it and learnt something new, if you made this far, that you so much, means a lot to me. Have a nice day and do come back to read again.
Resources
- https://medium.com/@4318_26766/creating-and-using-dynamic-libraries-in-c-language-912b078b7c52#:~:text=To%20create%20a%20Dynamic%20Library%20in%20Linux%20we%20are%20using,make%20the%20code%20position%20independent.
- https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/developer_guide/creating-libraries-gcc
- https://cylab.be/blog/234/creating-a-dynamic-library-in-c