musl 1.0.0 Reference Manual

Part I - Introduction

musl is an implementation of the userspace portion of the standard library functionality described in the ISO C and POSIX standards, plus common extensions. It can be used both as the system-wide C library for operating system installations and distributions, and as a tool for building individual application binaries deployable on a wide range of systems compatible with the Linux system calls API.

This manual covers many details of musl which may be of interest to programmers, systems integrators, and end users. It is a work in progress.

Conformance

The interfaces in musl are modeled upon and intended to conform to the requirements of the ISO C99 standard (ISO/IEC 9899-1999), including Annex F, and POSIX 2008 / Single Unix Standard Version 4, with all current technical corrigenda applied. However, musl has not been certified by any standards body, and no guarantee of conformance is made by the copyright holders or any other party with an interest in musl.

Moreover, since musl provides only the userspace portion of the standard system interfaces, conformance to the requirements of POSIX depends in part on the behavior of the underlying kernel. Linux 2.6.39 or later is believed to be sufficient; earlier versions in the 2.6 series will work, but with varying degrees of non-conformance, particularly in the area of signal handling behavior and close-on-exec race conditions.

Likewise, conformance to the requirements of ISO C, and especially Annex F (IEEE floating point semantics), depends in part on both the compiler used to build musl and the compiler used when building applications against musl. At this time there is no known fully conforming compiler.

Supported Targets

Experimental Targets

The following additional targets are available for build, but may not work correctly and may not yet have ABI stability:

Part II - Setup and Usage

Build and Installation

The build system for musl uses the well-known ./configure idiom. musl's configure script is not based on GNU autoconf, but is intended to closely match the configure command line interface documented in the GNU Coding Standards. Running configure produces a config.mak file which can further be edited by hand, if necessary.

Prerequisites

The only build-time prerequisites for musl are the standard POSIX shell and utilities, GNU Make (version 3.81 or later) and a freestanding C99 compiler toolchain targeting the desired instruction set architecture and ABI, with support for gcc-style inline assembly, weak aliases, and stand-alone assembly source files.

The system used to build musl does not need to be Linux-based, nor do the Linux kernel headers need to be available.

If support for dynamic linking is desired, some further requirements are placed on the compiler and linker. In particular, the linker must support the -Bsymbolic-functions option, and the compiler must not generate gratuitous GOT relocations where GOT-relative or PC-relative addressing could be used instead.

Supported compilers

A recent version of GCC or LLVM/clang is recommended for compiling musl. However, the following compilers are known to have built musl successfully:

Build options

Running ./configure --help from the top-level source directory will print usage information for configure. In most cases, the only options which should be needed are:

Both --prefix and --syslibdir should reflect the final runtime location where musl will be installed. If musl should be installed to a different location to prepare a package file or new target system image, the DESTDIR variable can be set when running make install. In this case, DESTDIR will be prepended to all installation paths, but will not be saved anywhere in the files installed.

Other build options of interest are:

See ./configure --help for additional options.

Compiling and Installing

After running configure, run make to compile and make install to install. If desired, make install can be invoked directly without first running make, but it may be desirable to do these as separate steps if elevated privileges are needed to install to the final destination. musl's makefile is fully declarative and non-recursive, and may be arbitrarily parallelized with the -j option.

Note: The install target in musl's Makefile is fully declaratory, and its proper operation depends on file timestamps being correct. If files with newer/future timestamps already exist in the destination, updated files may fail to be installed. This can be avoided by deleting the offending files, fixing their timestamps, or installing first to a fresh DESTDIR then moving the files into place.

After Installation

If installing for the first time and using dynamic linking, it may be necessary to create a path file for the dynamic linker. See ../etc/ld-musl-$(ARCH).path under the heading Additional Files Used later in this part of the manual.

Installed Components

In the following, $(syslibdir), $(includedir), and $(libdir) refer to the paths chosen at build time (by default, /lib, $(prefix)/include, and $(prefix)/lib, respectively) and $(ARCH) refers to the full name for the target CPU architecture/ABI, including the "subarch" component.

Dynamic linking runtime

$(syslibdir)/ld-musl-$(ARCH).so.1 provides the dynamic linker, or "program interpreter", for dynamically linked ELF programs using musl. The absolute pathname to this file must be stored in all such programs. The build and installation system provided with musl sets it up as a symbolic link to $(libdir)/libc.so, but system integrators may choose to make it available in whichever ways they find suitable.

Development environment

Header files for use by the C compiler are installed in $(includedir). The standard headers are fully self-contained, and do not make use of kernel-provided or compiler-provided headers or otherwise require such headers to be present.

The file libc.a installed in $(libdir) provides the entire standard library implementation for static linking. The file libc.so provides the linker with access to the standard library's symbols for use at link-time in producing dynamic-linked binaries. It is not searched at runtime; the standard library is resolved as part of the program interpreter at $(syslibdir)/ld-musl-$(ARCH).so.1.

Additional files libm.a, librt.a, libpthread.a, libcrypt.a, libutil.a, libxnet.a, libresolv.a, and libdl.a are provided in $(libdir) as empty library archives. They contain no code, but are present to satisfy the POSIX requirement that options of the form -lm, -lpthread, etc. be accepted by the c99 compiler.

Several bare object files are also included in $(libdir): crt1.o and Scrt1.o are the normal and position-independent versions, respectively, of the entry point code linked into every program. crti.o and crtn.o, also linked into every program and into shared libraries, provide support for legacy means by which the compiler can arrange for global constructors and destructors to be executed. It is possible to setup a legacy-free compiler toolchain that does not need the crti.o and crtn.o files if desired.

Compiler wrapper

Included with musl is a wrapper script musl-gcc which can be used with an existing GCC compiler toolchain to build programs using musl. If installed, the script itself is located at $(bindir)/musl-gcc, and a supporting GCC specs file it uses is located at $(libdir)/musl-gcc.specs.

Filesystem Layout Dependencies

musl aims to avoid imposing filesystem policy; however, the following minimal set of filesystems dependencies must be met in order for programs using musl to function correctly:

While some programs may operate correctly even without some or all of the above, musl's behavior in their absence is unspecified.

Additional Files Used

Environment Variables

Part III - Programmers' Manual

Compiler Support

All public interfaces in musl, at both the header file and library level, are intended to be mostly compatible with any C99, C11, or C++ compiler targeting the same CPU architecture and ABI musl was built for. C89 compilers are also supported provided that they accept the long long type and wide character literals as extensions. A few public header files do, however, require compiler-specific extensions in order to provide the mandated standard features:

In addition, the definitions of NAN (in math.h) and offsetof (in stddef.h) require the __builtin_nanf and __builtin_offsetof extensions, respectively, to provide fully conforming definitions. When used with compilers which do not predefine __GNUC__, these headers will fallback to alternate definitions.

System Header Files

Introduction to Namespace Issues

Any C program using a library, whether the standard library or a third-party library, needs to observe a contract with the library regarding usage of identifiers - in particular, which identifiers are used as part of the library's public interface or header file implementation, and which identifiers are used by the application. Having a clear contract is especially important when the library being used is not a single fixed implementation, but may have multiple versions or multiple independent implementations. The canonical example of such a library is the standard library.

ISO C reserves all identifiers which are not explicitly defined or reserved by the standard for use by the application. POSIX, however, exposes a number of additional identifiers, and popular extensions outside of the standards define even more. In order to support applications which are written with different expectations on which identifiers may be used for the application's purposes, and which ones are defined by the system, a mechanism must be provided for choosing which contract will be used.

Introduction to Feature Test Macros

To solve this problem, POSIX introduced the concept of feature test macros. These are macros which an application may define prior to the inclusion of any system header (either at the source level, or via -D options passed as arguments to the compiler) in order to request a particular namespace contract. POSIX 2008 specifies two such feature test macros:

No requirements are placed on the namespace when neither of these macros is defined by the application. If one or both of these macros is defined by the application, two constraints are placed on the system headers:

There is, however, an exception to the second rule: since the standard does not define behavior when the application has defined macros whose names are reserved for system use, implementations may specify their own feature test macros to expose additional identifiers alongside the standard ones.

This is what musl, and most other implementations of the standard library, do.

Feature Test Macros Supported by musl

If no feature test macros are defined, musl's headers operate in "default features" mode, exposing the equivalent of the _BSD_SOURCE option below. This corresponds fairly well to what most applications unaware of feature test macros expect, and also provides a number of more modern features.

Otherwise, if at least one of the below-listed feature test macros is defined, they are treated additively, starting from pure ISO C as a base. Unless otherwise specified, musl ignores the value of the macro and only checks whether it is defined.

Documentation of specific extensions provided by the nonstandard feature test macros will be added in a future edition of this manual.

Library Interfaces

For all interfaces provided by musl that are specified by standards to which musl aims for conformance, the relevant standards documents are the official documentation.

This portion of the manual is incomplete. Future editions will document musl's behavior where the standards specify that it is implementation-defined, non-standard extensions musl implements, and additional properties of musl's implementation.