
WTF is `glibc`?
C is a language + a standard that lists out certain features that has to be provided. There are 2 standareds -
ANSI
orstandard
C (open, read, write, printf etc)POSIX
C (pthreads semaphores, mutexes etc)
Each standard provides a set of functionality, a lot of which need to call operating specific functions (System calls).
glibc
(or GNU C library), and other C libraries, provide the interface headers
and functions
that call the operating system specific routines/system calls etc to provide the functionality defined in ANSI
or POSIX
Note; ANSI
and POSIX
seem to be the most standard. OSes like Linux have additional APIs that are also provided by glibc
, but they are probably not standard across operating systems.
WTF is musl
?
glibc
is heavy, because it provides A LOT of features.
musl
is an alternative glibc
implementation that is more lightweight and can also be statically linked.
statically
linked => no need of a libc.so
(dynamic/shared library) needed at runtime to run the executable.
On Linux, it is possible to call system calls from assembly instructions to directly run functions like read
, write
etc. Refer to the sycall manual on Linux. The syscall interface is guaranteed by Linux to be stable and almost never broken. For example, this is the Syscall ABI for Linux on ARM
macOS, OTOH does not guarantee a syscall interface. Therefore macOS mandates that all calls that require kernel functionality go via the shared libc
For more information look at how Executables in Linux are actually executed in [[From Source Code to Hello World/ELFs and Loaders]]
How to link an application with musl
glibc
The GNU C library
TODO:
Musl documentation
Start from Part III - Programmer’s Manual
Once we link an application, how can we tell the difference ?
ldd /path/to/executable
lists all the shared libraries your executable depends on. You will find that libc is one of them.
If your application is compiled with musl
, then you will not see libc
linked to your executable
On macOS, otool -L executable
will list the linked dynamic libraries. macOS does not support statically linked executables. All executables have to compulsorily link to the libc implementation
$ otool -L ./cloop
./cloop:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)