Linux - Shared Library (so, sl)

1 - About

so means shared object file and are shared library in Linux

There format is the Executable and Linkable Format.

As Shared Library, so files are open file opened by a process.

The shared library extension is operating system dependent:

  • .so for Linux, Solaris, or AIX,
  • .sl for HP-UX.

3 - Management

3.1 - Search Path

When an executable is looking for a dynamic library (.so file), the linker searches in order:

  • in the directories listed in the LD_LIBRARY_PATH environment variable
  • in the directories listed in the executable's rpath. /usr/lib
  • directories on the system search path. For instance:

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

3.2 - PATH

Dynamically linked libraries are typically placed in directories.

The usual directories include:

Path Description
/lib
/usr/lib Standard system libraries
/gnu/lib Gnu libraries
/lib/security PAM modules
/usr/X11R6/lib or /usr/openwin/lib X-windows
/usr/local/lib 3rd party libraries

On GNU glibc-based systems, including all Linux systems, the list of directories automatically searched during program start-up is stored in the file /etc/ld.so.conf.


cat /etc/ld.so.conf


include ld.so.conf.d/*.conf

To control this process, you can use the LD_PATH environment variable (for instance: LD_LIBRARY_PATH for Linux/Solaris)

3.3 - Content (Unpack)


readelf -a -W /path/To/SoFile

3.4 - Version

3.4.1 - Soname

In an ELF file:

  • The version info in not explicitly stored . The full version is usually stored as a part of the library file name.
  • The soname contains the name of the library which includes the major version. If there was an API change, the library creator is supposed to change the soname. Multiple libraries can then be on a single system.

Example: libtest.so

  • libtest.so.1.0.1 - The library file itself, containing the full version
  • libtest.so.1 - Symlink to libtest.so.1.0.1, having the same name as soname
  • libtest.so - Symlink to libtest.so.1 used for linking.

To get the soname:


readelf -d  /path/to/library.so | grep SONAME

3.4.2 - File

The version is just to be found in the symbolic link.

libc below is the version 2.5


file /lib64/libc.so.6


/lib64/libc.so.6: symbolic link to `libc-2.5.so'

File System - ( Symbolic | ln | Soft) link - File Alias - Symlink - Reparse Points


file libodbc.so


# if it's a symlink
libodbc.so: symbolic link to `libodbc.so.2.0.0'
# if it's file
libodbc.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped

3.6 - Load Test

dltest /path/to/driverFile.so

3.7 - Dependency

  • ldd (list dynamic dependencies) print shared library dependencies

ldd ../product/fmw/Oracle_BI1/bifoundation/odbc/lib/libodbc.so


ldd: warning: you do not have execution permission for `../product/fmw/Oracle_BI1/bifoundation/odbc/lib/libodbc.so'
        linux-vdso.so.1 =>  (0x00007fff375c4000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f7bc07ba000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f7bc05b0000)
        libodbcinst.so => /u01/app/oracle/product/fmw/Oracle_BI1/bifoundation/odbc/lib/libodbcinst.so (0x00007f7bc03ca000)
        libSEicu23.so => /u01/app/oracle/product/fmw/Oracle_BI1/bifoundation/odbc/lib/libSEicu23.so (0x00007f7bbf7b9000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f7bbf5b4000)
        libstdc++.so.5 => /usr/lib64/libstdc++.so.5 (0x00007f7bbf2d9000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f7bbf056000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f7bbecfd000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f7bbeaef000)
        /lib64/ld-linux-x86-64.so.2 (0x00000035bb600000)

3.8 - List Shared Library used by processes

With the lsof

  • one process

lsof -p $(pgrep nqsserver) | awk '{print $9}' | grep .so$

  • all process from a root directory

lsof /path/to*

3.9 - Find a shared library


sudo find / | grep whatever.so

  • Example

sudo find / | grep libodbc.so


/tmp/infa_rpm/Domain/ce908432ad08b4beffd072ef5344365b/infa_rpm.tar/ODBC7.1/lib/libodbc.so
/tmp/unixODBC-2.3.6/DriverManager/.libs/libodbc.so.2.0.0
/tmp/unixODBC-2.3.6/DriverManager/.libs/libodbc.so.2
/tmp/unixODBC-2.3.6/DriverManager/.libs/libodbc.so
/usr/local/lib/libodbc.so.2.0.0
/usr/local/lib/libodbc.so.2
/usr/local/lib/libodbc.so

3.10 - Creation

Example with Sqlite and an amalgam adapted from the Ref documentation


gcc \
-Wl,-soname,libsqlite3.so.0 \
-DDISABLE_DIRSYNC \
-DENABLE_COLUMN_METADATA \
-DENABLE_FTS3 \
-DENABLE_RTREE \
-DENABLE_JSON1 \
-DENABLE_UNLOCK_NOTIFY \
-DSECURE_DELETE \
-DTEMP_STORE=1 \
-DTHREADSAFE=1 \
-shared \
-o libsqlite3.so \
-fPIC \
sqlite3.c

4 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap