2.3. Creating libraries with GCC
Learn about the steps to create libraries and the concepts the Linux operating system uses for libraries, including sonames, symbolic links, and library file naming conventions.
2.3.1. The soname mechanism 复制链接链接已复制到粘贴板!
To manage multiple compatible versions of a library, dynamically loaded libraries (shared objects) use the soname mechanism.
- You must understand dynamic linking and libraries.
- You must understand the concept of ABI compatibility.
- You must understand library naming conventions.
You must understand symbolic links.
- Problem introduction
- A dynamically loaded library (shared object) exists as an independent executable file. This makes it possible to update the library without updating the applications that depend on it. However, the following problems arise with this concept:
- Identification of the actual version of the library
- Need for multiple versions of the same library present
Signalling ABI compatibility of each of the multiple versions
- The soname mechanism
-
The soname mechanism resolves these problems by using naming conventions to indicate compatibility. A
foolibrary version X.Y is ABI-compatible with other versions with the same value of X in a version number. Minor changes preserving compatibility increase the number Y. Major changes that break compatibility increase the number X.
The actual foo library version X.Y exists as a file libfoo.so.x.y. Inside the library file, a soname is recorded with value libfoo.so.x to signal the compatibility.
During the build, the linker searches for a symbolic link named libfoo.so that points to the library file. A symbolic link with this name must exist, pointing to the actual library file. The linker then reads the soname from the library file and records it into the application executable file. Finally, the linker creates the application that declares dependency on the library by using the soname, not a name or a file name.
When the runtime dynamic linker links an application before running, it reads the soname from application’s executable file. This soname is libfoo.so.x. A symbolic link with this name must exist, pointing to the actual library file. This allows loading the library, regardless of the Y component of a version, because the soname does not change.
The Y component of the version number is not limited to just a single number. Additionally, some libraries encode their version in their name.
- Reading soname from a file
To display the soname of a library file
somelibrary:$ objdump -p somelibrary | grep SONAME
Replace somelibrary with the actual file name of the library that you want to examine.
- Finding a library name and version in a file name
As an example, consider a library which is present as a file
libevent-2.0.so.5.1.9. To find the actual components:-
Start by ignoring the standard library file name prefix
lib. -
Break the remainder into the two parts preceding and following the string
.so.. -
The first part is
event-2.0, which is the name of the library. -
The second part is
5.1.9. To find the X version component, take everything before first dot:5. -
The rest is the Y version component:
1.9.
-
Start by ignoring the standard library file name prefix
Therefore the library’s name is event-2.0, the X version component is 5, and Y is 1.9.
The soname of this library file is everything up to the Y component: libevent-2.0.so.5.
When a newer but still compatible version of the library is released, it uses the same soname, and the Y version component is increased. The new file name is libevent-2.0.so.5.1.10.