Designing Shared Libraries

written by: Lorent Konenbal; article published: year 2007, month 07;



In: Categories » Computers and technology » Linux » Designing Shared Libraries

Building shared libraries is only marginally harder than building normal static libraries. There are a few constraints, all of which are easy to manage. There is also one major feature, designed to manage binary compatibility across library versions, that is unique to shared libraries.

Shared libraries are intended to preserve backward compatibility. That is, a binary built against an older version of the library still works when run against a newer version of the library. However, there needs to be a way to mark libraries as incompatible with each other for cases in which developers find it necessary to modify interfaces in a non-backward-compatible manner.

Managing Compatibility

Every Linux shared library is assigned a special name, called a soname, that includes the name of the library and a version number. When developers change interfaces, they increment the version number, altering the name. Some libraries do not have stable interfaces; developers may change their interface in an incompatible way when a new version is released that has changed only a minor number. Most library developers attempt to maintain stable interfaces that change in an incompatible manner only when they release a new major version of the library.

For example, the developers and maintainers of the Linux C library attempt to maintain backward compatibility for all releases of the C library with the same major number. Version 5 of the C library has gone through five minor revisions, and with few exceptions, programs that worked with the first minor revision work with the fifth. (The exceptions have been poorly coded programs that took advantage of C library behavior that was not specified or that was buggy in older versions and fixed in newer versions.)

Because all version 5 C libraries are intended to be backward compatible with older versions, they all use the same soname—libc.so.5—which is related to the name of the file in which it is stored, /lib/libc.so.5. m. r, where m is the minor version number and r is the release number.

Applications that link against a shared library do not link directly against /lib/libc.so.6 (for instance), even though that file exists. The ldconfig program, a standard Linux system utility, creates a symbolic link from /lib/libc.so.6 (the soname) to /lib/libc-2.3.2.so, the real name of the library. This makes upgrading shared libraries easy. To upgrade from 2.3.2 to 2.3.3, it is necessary only to put the new libc-2.3.3.so into the /lib directory and run ldconfig. The ldconfig looks at all the libraries that provide the libc.so.6 soname and makes the symbolic link from the soname to the latest library that provides the soname. Then all the applications linked against /lib/libc.so.6 automatically use the new library the next time they are run, and /lib/libc-2.3.2.so can be removed immediately, since it is no longer in use.

That is, you can use the rm command to remove it from the directory structure immediately; programs that are still using it keep it on the disk automatically until they exit.

Unless you have a particular reason to do so, do not link against a specific version of a library. Always use the standard -l libname option to the C compiler or the linker, and you will never accidentally link against the wrong version. The linker will look for the file liblibname.so, which will be a symlink to the correct version of the library.

So, for linking against the C library, the linker finds /usr/lib/libc.so, which tells the linker to use /lib/libc.so.6, which is a link to the /lib/libc-2.3.2.so file. The application is linked against libc-2.3.2.so's soname, libc.so.6, so when the application is run, it finds /lib/libc.so.6 and links to libc-2.3.2.so, because libc.so.6 is a symlink to libc-2.3.2.so.

Incompatible Libraries

When a new version of a library needs to be incompatible with an old version, it should be given a different soname. For instance, to release a new version of the C library that is incompatible with the old one, developers used the soname libc.so.6 instead of libc.so.5, which shows that it is incompatible and also allows applications linked against either version to coexist on the same system. Applications linked against some version of libc.so.5 will continue to use the latest library version that provides the libc.so.5 soname, and applications linked against some version of libc.so.6 will use the latest library version that provides the libc.so.6 soname.

Designing Compatible Libraries

When you are designing your own libraries, you need to know what makes a library incompatible. There are three main causes of incompatibilities:

  1. Changing or removing exported function interfaces

  2. Changing exported data items, except adding optional items to the ends of structures that are allocated within the library

  3. Changing the behavior of functions to something outside the original specification

To keep new versions of your libraries compatible, you can:

  • Add new functions with different names rather than change the definitions or interfaces of existing functions.

  • When changing exported structure definitions, add items only to the end of the structures, and make the extra items optional or filled in by the library itself. Do not expand structures unless they are allocated within the library. Otherwise, applications will not allocate the right amount of data. Do not expand structures that are used in arrays

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. How to use PuTTY Passphrase Agents
STEP1: Use Pageant to store your private keys in memory To make public key authentication more convenient, the developers of PuTTY created Pageant. Pageant is a program included with PuTTY that will keep your decrypted private keys in memory so you only have to enter your passphrase once rather than every time you authenticate to a server using public key authentication. While this will make your day-to-day use more convenient, please keep in mind that it also poses a slight risk, since other applications (inc...

2. Interactively transfer files from the command line with PSFTP
One method to transfer files from the Windows command line is to use PSFTP. PSFTP creates an interactive SFTP file transfer session where you can use many of the commands available within a normal FTP session. Since PSFTP uses the SFTP protocol, which is only available with servers running protocol SSHv2, you may not be able to run it on every server. PSFTP is run from the command line and provides numerous options. To see the options available run PSFTP with the –h option: ...

3. Using Plink to initiate an SSH session from the command line or a script
Using PuTTY from the command line will create an SSH interactive session. This may not be what we want if for example we need to remain at the Windows command line or we want to issue an SSH command from within a script. In order to satisfy these types of needs, PuTTY provides a tool called Plink. Plink is a command line tool that will allow you to log in to a remote machine using SSH and either create an SSH session or execute a command, all from the command line and without opening another window. Plink comes with many comma...

4. How to Generate a Key Pair Using OpenSSH
Problem: How can a key-pair be created in OpenSSH?STEP1: Generating your public/private key-pairThe ssh-keygen command is utilized to generate your public and private keys. OpenSSH provides authentication methods via a choice of three public key "cryptosystems": RSA1, RSA, and DSA. RSA1 works with SSHv1 while RSA and DSA are for SSHv2. RSA and DSA use different techniques for authenticating and have different capabilities, but for purposes of this guide, either will suffice.To create a key-pair, r...

5. Transfer files from the command line with PSCP
A second method to transfer files from a Windows command line prompt is to use PSCP. Unlike PSFTP, PSCP is not interactive and is designed to transfer files "in one shot" and then exit, much like OpenSSH's scp command. PSCP also allows you to specify wildcards within filenames (PSFTP does not). Additionally, PSCP will work with any SSH server as it is not dependent on SSHv2 being present. Note  PSCP will blindly copy files to the remote server, overwriting any files with the same name, without prompting for veri...

6. Create an SSH session from the command line using PuTTY
There are multiple ways to create an SSH session from the command line using PuTTY. The first way involves using the PuTTY program itself. PuTTY comes with a number of options that can be used to invoke the graphical PuTTY terminal from the command line. A description of these options is available within the PuTTY help file. To run PuTTY from the command line: Note  ...

7. Install SSH Windows Clients to Access Remote Machines Securely
Problem: Many times administrators will find themselves on a Windows machine with no way to access a remote server securely since Microsoft does not yet package an SSH client. There are a number of excellent tools available that provide SSH client connectivity from a Windows platform. A list of these tools is available at http://www.openSSH.com/windows.html. ...

8. How to use OpenSSH Passphrase Agents
Problem: Using public key authentication makes logging in to a server with SSH more secure, but less convenient due to having to type in a longer and more complex passphrase. STEP1: Use ssh-agent and ssh-add to store your private keys in memory To make public key authentication more convenient to use, the OpenSSH developers created the ssh-agent and ssh-add programs. These programs are designed to keep your private keys decrypted in memory for your current session. With ssh-agent, you will not ne...

9. Buffer Overflow
A buffer overflow occurs when a program or process tries to store more data in a temporary data storage area than it was intended to hold. Since buffers are created to contain a finite amount of data, the extra information can overflow into adjacent buffers, corrupting or overwriting the valid data held in them. Buffer overflows are a fertile source of bugs and malicious attacks. They occur when a program attempts to write data past the end of a buffer. A buffer is a contiguous allocated chunk of memory, such as an array ...