The Wayback Machine - https://web.archive.org/web/20240813191224/https://www.geeksforgeeks.org/working-with-shared-libraries-set-1/
Open In App

Working with Shared Libraries | Set 1

Last Updated : 22 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

This article is not for those algo geeks. If you are interested in systems related stuff, just read on… 

Shared libraries are useful in sharing code which is common across many applications. For example, it is more economic to pack all the code related to TCP/IP implementation in a shared library. However, data can’t be shared as every application needs its own set of data. Applications like, browser, ftp, telnet, etc… make use of the shared ‘network’ library to elevate specific functionality. 

Every operating system has its own representation and tool-set to create shared libraries. More or less the concepts are same. On Windows every object file (*.obj, *.dll, *.ocx, *.sys, *.exe etc…) follow a format called Portable Executable. Even shared libraries (called as Dynamic Linked Libraries or DLL in short) are also represented in PE format. The tool-set that is used to create these libraries need to understand the binary format. Linux variants follow a format called Executable and Linkable Format (ELF). The ELF files are position independent (PIC) format. Shared libraries in Linux are referred as shared objects (generally with extension *.so). These are similar to DLLs in Windows platform. Even shared object files follow the ELF binary format. 

Remember, the file extensions (*.dll, *.so, *.a, *.lib, etc…) are just for programmer convenience. They don’t have any significance. All these are binary files. You can name them as you wish. Yet ensure you provide absolute paths in building applications. 

In general, when we compile an application the steps are simple. Compile, Link and Load. However, it is not simple. These steps are more versatile on modern operating systems. 

When you link your application against static library, the code is part of your application. There is no dependency. Even though it causes the application size to increase, it has its own advantages. The primary one is speed as there will be no symbol (a program entity) resolution at runtime. Since every piece of code part of the binary image, such applications are independent of version mismatch issues. However, the cost is on fixing an issue in library code. If there is any bug in library code, entire application need to be recompiled and shipped to the client. In case of dynamic libraries, fixing or upgrading the libraries is easy. You just need to ship the updated shared libraries. The application need not to recompile, it only need to re-run. You can design a mechanism where we don’t need to restart the application. 

When we link an application against a shared library, the linker leaves some stubs (unresolved symbols) to be filled at application loading time. These stubs need to be filled by a tool called, dynamic linker at run time or at application loading time. Again loading of a library is of two types, static loading and dynamic loading. Don’t confuse between static loading vs static linking and dynamic loading vs dynamic linking. 

For example, you have built an application that depends on libstdc++.so which is a shared object (dynamic library). How does the application become aware of required shared libraries? (If you are interested, explore the tools tdump from Borland tool set, objdump or nm or readelf tools on Linux). 

Static loading: 
 

  • In static loading, all of those dependent shared libraries are loaded into memory even before the application starts execution. If loading of any shared library fails, the application won’t run.
  • A dynamic loader examines application’s dependency on shared libraries. If these libraries are already loaded into the memory, the library address space is mapped to application virtual address space (VAS) and the dynamic linker does relocation of unresolved symbols.
  • If these libraries are not loaded into memory (perhaps your application might be first to invoke the shared library), the loader searches in standard library paths and loads them into memory, then maps and resolves symbols. Again loading is big process, if you are interested write your own loader :). 
  • While resolving the symbols, if the dynamic linker not able to find any symbol (maybe due to older version of shared library), the application can’t be started.

Dynamic Loading: 
 

  • As the name indicates, dynamic loading is about loading of library on demand.
  • For example, if you want a small functionality from a shared library. Why should it be loaded at the application load time and sit in the memory? You can invoke loading of these shared libraries dynamically when you need their functionality. This is called dynamic loading. In this case, the programmer aware of situation ‘when should the library be loaded’. The tool-set and relevant kernel provides API to support dynamic loading, and querying of symbols in the shared library.

More details in later articles. 

Note: If you come across terms like loadable modules or equivalent terms, don’t mix them with shared libraries. They are different from shared libraries  The kernels provide framework to support loadable modules. 

Working with Shared Libraries | Set 2 

Exercise: 

1. Assuming you have understood the concepts, How do you design an application (e.g. Banking) which can upgrade to new shared libraries without re-running the application. 

— Venki.
 



Similar Reads

Working with Shared Libraries | Set 2
We have covered basic information about shared libraries in the previous post. In the current article, we will learn how to create shared libraries on Linux. Prior to that, we need to understand how a program is loaded into memory, and the various (basic) steps involved in the process. Let us see a typical "Hello World" program in C. Simple Hello W
4 min read
Algorithm for implementing Distributed Shared Memory
Distributed shared memory(DSM) system is a resource management component of distributed operating system that implements shared memory model in distributed system which have no physically shared memory. The shared memory model provides a virtual address space which is shared by all nodes in a distributed system. The central issues in implementing D
3 min read
Difference between Shared Memory Model and Message Passing Model in IPC
Prerequisite - Inter-Process Communication 1. Shared Memory Model: In this IPC model, a shared memory region is established which is used by the processes for data communication. This memory region is present in the address space of the process which creates the shared memory segment. The processes that want to communicate with this process should
2 min read
What is Distributed shared memory and its advantages
DSM is a mechanism that manages memory across multiple nodes and makes inter-process communications transparent to end-users. The applications will think that they are running on shared memory. DSM is a mechanism of allowing user processes to access shared data without using inter-process communications. In DSM every node has its own memory and pro
3 min read
IPC through shared memory
Inter Process Communication through shared memory is a concept where two or more process can access the common memory and communication is done via this shared memory where changes made by one process can be viewed by another process. The problem with pipes, fifo and message queue – is that for two process to exchange information. The information h
3 min read
fork() and memory shared b/w processes created using it
Prerequisite: fork() in C So when we do a fork() what are the sections the two process actually share? Is the heap memory per process? Are the global variables shared? Will malloc return the same address to both? Let us run the program below and take a look at the output of it to clear the questions above. C/C++ Code // C program to demonstrate wor
4 min read
Static and Dynamic Libraries | Set 1
When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. A linker can accomplish this task in two ways, by copying the code of library fun
4 min read
Thread Libraries
The Operating System is an interface between the user and hardware that enables the interaction of computer hardware and software or it can act as a bridge between the user and the application. It ensures that various software programs can run efficiently and interact with hardware as needed. Some Popular Operating Systems are Linux, Unix, Microsof
6 min read
Private bytes, Virtual bytes, Working set
In this article, we are going to understand private bytes, virtual bytes, and working sets. Private Bytes :As a result of completely paging out the process's private memory footprint to swap, Private Bytes summarizes the amount of RAM allocated to the process (though not necessarily used). Process executables use Private Bytes only to request memor
2 min read
Working Set in Paging
When pages are exchanged in and out of main memory too frequently for a process, slowing down its execution, it is called Thrashing. A process generally requires a lot of pages. But in a certain span of time, only a certain subset of these pages is used repeatedly. This is called the locality model of execution and has been widely observed. The req
4 min read
three90RightbarBannerImg