Factory.ai

Open-Source Wikis

/

LLVM

/

Subprojects

/

libc

llvm/llvm-project

libc

libc/ is LLVM's retargetable implementation of the C standard library. It is not glibc, not musl, and not a wrapper. It is a from-scratch libc with strict per-function overlay support, designed so that distributions can take the pieces they want without forcing an all-or-nothing replacement.

Purpose

LLVM's libc exists to give the LLVM toolchain a libc that ships under the LLVM license, builds in tree, and supports modern compiler features (LTO, sanitizers, fuzzing) by default. It also provides a clean home for embedded and GPU libcs — the OpenMP/Offload story has a libc-on-GPU mode that uses this implementation directly. See libc/README.txt for the brief positioning statement.

Directory layout

libc/
├── include/        # Generated public headers (most are produced from *.h.def at build time)
├── src/            # Implementations — one subdirectory per <header.h>
├── hdrgen/         # Header-generator tool used during the build
├── config/         # Per-target configuration (Linux/x86_64, Linux/aarch64, baremetal, GPU, ...)
├── shared/         # Cross-target shared definitions
├── startup/        # CRT objects (crt1, crti, crtn) for hosted Linux
├── newhdrgen/      # Newer hdrgen experiment
├── utils/          # Build helpers, MPFR-based testing helpers, gpu utilities
├── docs/
├── examples/       # Standalone "use llvm-libc from your project" examples
├── benchmarks/
├── test/           # lit regression tests
├── cmake/
├── fuzzing/        # libFuzzer harnesses
├── triple/
└── README.txt

How it builds and what it ships

LLVM-libc supports multiple build modes:

  • Hosted Linux — full libc replacing glibc/musl
  • Overlay — only the functions you opt into; everything else falls through to the system libc
  • Bare-metal / embedded — minimal subsets for newlib-style targets
  • GPU (NVPTX, AMDGPU) — libc on the device side, used by OpenMP and Offload

Configuration files under libc/config/ describe which entrypoints are built into each target's libc. This is how the project provides a strict overlay: you configure exactly which functions you want, and the build emits only those.

Source layout under src/

The directory layout under libc/src/ mirrors the C standard's headers:

  • string/, stdio/, stdlib/, math/, time/, pthread/, signal/, unistd/
  • errno/, setjmp/, complex/, fenv/, inttypes/, wchar/, wctype/
  • sys/mman/, sys/stat/, sys/wait/, etc. for POSIX additions
  • __support/ — cross-cutting helpers (FP utilities, OSUtil, big-int math, ...)
  • gpu/ — GPU-specific helpers

Each function lives in its own .cpp (or pair of .cpp/.h) under the relevant directory, with a corresponding CMakeLists.txt declaring the target.

How it works

graph LR
    spec["Standard headers spec<br/>(*.h.def + config/)"] --> hdrgen[hdrgen]
    hdrgen --> headers["Generated public headers<br/>(libc/include/)"]
    src[src/<header>/<func>.cpp] --> obj[Object files]
    obj --> lib[libc.a / libc.so]
    lib --> link[User link line]

The header-generator tool (libc/hdrgen/) consumes the YAML descriptions in libc/include/ and the per-target config/* to produce the actual .h files installed to the user. This is how the build opts function-by-function decisions in and out.

Testing

Two test layers:

  • libc-test — lit-driven black-box tests that build small programs against the resulting libc.
  • Unit tests — Google-Test-style C++ tests that link against the libc internals directly. Many tests use MPFR for ground-truth math comparisons; the --with-mpfr cmake option toggles this.

check-libc runs both.

Integration points

  • Compiler-rt (compiler-rt) — sanitizer runtimes, profile runtimes, and builtins are common companions to libc on a hosted target.
  • Offload (offload) and OpenMP (openmp) — the GPU libc is used by offloaded code.
  • Clang drives compilation; the libc build assumes clang-host or recent gcc.

Entry points for modification

  • Adding a function: create libc/src/<dir>/<func>.cpp and <func>.h, declare it in the corresponding CMakeLists.txt, list it in the relevant libc/config/<triple>/entrypoints.txt, and add a unit test under libc/test/src/<dir>/.
  • Tweaking math precision: math functions live under libc/src/math/ with shared FP infrastructure under libc/src/__support/FPUtil/. Test correctness via MPFR.

Reference

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

libc – LLVM wiki | Factory