Linux Shared Libraries in a nutshell

Junrong Lin - Aug 6, 2023 - Programming / Linux / C/C++
Post on:Aug 6, 2023|Last edited: Aug 9, 2023|
type
status
date
slug
summary
tags
category
icon
password
TL;DR: 1).

Real Name and SONAME

💡
Real Name: lib[name].so.[main].[minor].[release], e.g., libcudart.so.11.8.89 SONAME: lib[name].so.[main], e.g. libcudart.so.11.0
In short, Real Name is the file name, SONMAE is a “logical name” used by dynamic linker to search for libraries.
Every shared library has a special name called the SONAME. For example, in CUDA toolkit-11.8, libcudart.so.11.8.89 has SONAME libcudart.so.11.0 while libcublas.so.11.11.3.6 having libcublas.so.11
Dynamic linker uses SONAME instead of file name when loading libraries. It is designed for the purpose of library version control. Libraries with the same SONAME should have unchanged interface (imagine Python3.10.*). (as a special exception, the lowest-level C libraries don't start with lib). A fully-qualified SONAME includes as a prefix the directory it's in; on a working system a fully-qualified SONAME is simply a symbolic link to the shared library's real name.

dynamic linker GNU ld.so

At runtime
  1. Directories specified in DT_RPATH if present DT_RUNPATH does not exist (deprecated)
  1. path specified in LD_LIBRARY_PATH unless in secure-execution mode
  1. Directories specified in -rpath (DT_RUNPATH)
  1. Directories specified in /etc/ld.so.conf
  1. Default path /lib or /lib64
  1. /usr/lib or /usr/lib64
This answer provides a excellent explanation

ldconfig

ldconfig generates a cache file /etc/ld.so.cache for dynamic linker run-time bindings. of the directories specified in the library path /lib and /usr/lib , /etc/ld.so.conf, and in the trusted directories.
ldconfig and generate a cache file /etc/ld.so.cache to make the setting in /etc/ld.conf.d active
  1. Search /usr/lib, /lib and /etc/ld.so.conf
lib

LD_LIBRARY_PATH

 
 

Reference

  1. https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
  1. https://stackoverflow.com/questions/4071962/how-does-linker-find-shared-library-without-soname
  1. https://www.rapidtables.com/code/linux/gcc/gcc-i.html
  1. https://unix.stackexchange.com/questions/367600/what-is-the-order-that-linuxs-dynamic-linker-searches-paths-in
  1. https://man7.org/linux/man-pages/man8/ld.so.8.html
  1. https://stackoverflow.com/questions/67131565/how-do-i-set-dt-rpath-or-dt-runpath
  1. https://stackoverflow.com/questions/49138195/whats-the-difference-between-rpath-link-and-l
 
Why current LLM uses left padding?Why current LLM uses left padding?