:::meta
description: API reference for lightc, a freestanding C runtime for Linux — no libc, no external dependencies, everything from _start to exit on raw syscalls. Targets x86_64 and aarch64.
:::
:::use
https://cdn.jsdelivr.net/gh/OzkrAC/tern@0.4/themes/github.css
https://cdn.jsdelivr.net/gh/OzkrAC/tern@0.4/addons/code.js
lightc-docs.js
docs.css
:::
::site-nav
# lightc {#top}
{.brief}
A freestanding C runtime for Linux. No libc, no system linker scripts, no external dependencies — everything from `_start` to `exit` is implemented on raw Linux syscalls. Targets **x86_64** and **aarch64**.
::toc
:::div{.facts cols="1 1 1 1"}
**997 B**
tiny hello in CI, x86_64, GCC 14 (measured)
+++
**2**
architectures
+++
**0**
runtime dependencies
+++
**~12 ns**
coroutine switch, x86_64 (measured)
:::
## Overview {#overview}
lightc is the layer a hosted program normally receives from libc and the C runtime, re-implemented as a small, inspectable library. It provides process startup and teardown, memory allocation, threads and synchronization, coroutines, synchronous and asynchronous I/O, and the usual building blocks (strings, formatting, logging, containers, time, sockets) — each as a module under `include/lightc/` or `include/lightio/`.
Two invariants hold throughout the API:
- **No hidden state.** There is no global `errno` and no implicit allocator. A fallible call returns its status in an `lc_result` or `lc_result_ptr` value (see [the error model](guide.html#errors)); memory comes from an allocator you name.
- **The kernel ABI is the only dependency.** Every external effect is a syscall. Only two source files are per-architecture: the startup stub and the syscall trampoline.
Coming from 0.1.1? [Migrating to 0.2.0-rc1](migration-0.2.0-rc1.html) gives what to change in a program, what was added and what behaves differently. Coming from 0.2.0-rc1? [Migrating to 0.2.0-rc2](migration-0.2.0-rc2.html) gives the same for the next release candidate. Coming from 0.2.0-rc2? [Migrating to 0.2.0-rc3](migration-0.2.0-rc3.html) gives the same for the next release candidate. Coming from 0.2.0-rc3 or 0.2.0? [Migrating to 0.2.1](migration-0.2.1.html) gives the same for 0.2.1, which comes out before 0.2.0, as 0.2.0 is rc3 unchanged.
## Modules {#modules}
Each module is one public header. Linked modules have a reference page; the rest are documented at their headers.
:::div{.cards cols="1 1"}
### [Core & conventions](guide.html)
`result.h`, `syscall.h`, `types.h`, `lifecycle.h`, `assert.h` — the error model, the raw syscall layer, startup, exit and crashes, and the fixed-width types, from the compiler's own freestanding `` and ``.
+++
### [Heap](heap.html)
`heap.h` — general-purpose allocator: per-thread bucket allocator over slab pages and segments, lock-free cross-thread frees, a large-block cache, 16-byte alignment.
+++
### [Threads](threads.html)
`thread.h` — threads over `clone` with guard-paged stacks, and the futex-based synchronization set: spinlock, mutex, condition variable, read/write lock, barrier, once, and thread-local keys.
+++
### [Coroutines](coroutines.html)
`coroutine.h` — stackful coroutines and a cooperative round-robin scheduler, with an assembly context switch over the callee-saved registers.
+++
### [Async & lightio](async.html)
`async.h`, `lightio/lightio.h` — the `io_uring` submission/completion ring, and an event loop that runs one coroutine per connection.
+++
### Further modules
`socket.h`, `io.h`, `format.h`, `print.h`, `log.h`, `time.h`, `library.h`, `signal.h`, and the `lightdata/` containers are documented at their headers; reference pages follow.
:::
## Conventions {#conventions}
| Topic | Summary |
|---|---|
| Error reporting | `lc_result` (`int64_t value`, `int32_t error`) and `lc_result_ptr` (`void *value`, `int32_t error`); `error` is `LC_OK` or an `LC_ERR_*` code. See [Error handling](guide.html#errors). |
| Naming | Public symbols are prefixed `lc_` (`lio_` for lightio); raw syscall wrappers are `lc_kernel_*`; compile-time constants are `LC_*`. |
| Strings | Lengths are explicit; APIs do not assume NUL termination. |
| Allocation | Allocating calls are `[[nodiscard]]` and return `lc_result_ptr`. |
| Thread safety | Noted per function. The heap and synchronization primitives are thread-safe; most data-structure handles are not, unless stated. |
[Contracts](contracts.html) gives the rest of what a program can count on and what it must do itself: how to link, what a signal handler may call, how EINTR and SIGPIPE are handled, close-on-exec, fork and exit, plugins, rings and the kernel lightc needs. Each default there names the test that enforces it.
## Building {#building}
A shell `configure` script selects a build mode and writes `build.ninja`; `ninja` builds. The toolchain requirement is GCC and ninja only.
| Mode | Flags |
|---|---|
| `./configure debug` | `-O0 -g3`, safety checks on |
| `./configure release` | `-O2 -DNDEBUG`, stripped (default) |
| `./configure tiny` | `-Os`, smallest binary |
| `./configure ubsan` | `-O1 -g -fsanitize=undefined -fsanitize-trap=all`, safety checks on |
| `./configure faultinject` | `-O1 -g -DLC_FAULTINJECT`, safety checks on: system calls that fail on request, and chaos points, for tests |
| Target | Builds |
|---|---|
| `ninja` | the static library and the examples, `test_plugin.so` among them (every program of `examples/` but `bench_startup` and the libc comparisons `*_musl.c`); not the test suites |
| `ninja tests` | the test suites, without running them |
| `ninja test` | build and run the test suites |
| `ninja regress` | run the regress programs of fixed defects, listed in `tests/regress/manifest` |
| `ninja smoke` | run the examples that check their own results, and `lightio_test` |
| `ninja stress` | run the fork stress test, `tests/stress_fork.c`: 2000 children made while eight threads allocate and log |
| `ninja bench` | run the benchmarks |
| `ninja api-check` | check that `api/` is the snapshot of the public API: symbols, declarations, struct layouts, and the index of every function with the system calls it makes (`api/index.md`, `api/syscalls.tsv`) |
The output links statically with lightc's own linker script (`lightc.ld`, or `lightc-tiny.ld` in tiny mode), at fixed addresses (`-static -no-pie`), with no interpreter and no dynamic dependencies. Any mode given `--pie`, as `./configure release --pie`, links static PIEs instead (`-static-pie`, with `lightc-pie.ld` or `lightc-tiny-pie.ld`): no interpreter either, but loaded at a random base, and entered at `_start_pie`, which relocates the program to that base and then goes on as `_start` ([Contracts](contracts.html#linking)). `--no-pie` links static executables whatever the default. In 0.2.1 a static PIE is opt-in; from the release after, every mode but tiny links static PIEs unless given `--no-pie`. It carries a GNU build ID, a SHA-1 of its contents, which `-Wl,--build-id=sha1` asks for: debug files and crash reports are matched to the build by it.
Every source compiles with `-ffunction-sections` and `-fdata-sections`, and every link passes `-Wl,--gc-sections`, so a program keeps only the functions and data that `_start` reaches. `_start` reaches the heap's setup of the thread pointer and the keeping of the startup data through weak references: a program that never allocates links no `heap.o` or `thread.o`, and one that also reads neither the startup data nor the page size and uses no arena links no `memory.o`. The linker scripts keep what a program needs that nothing refers to, such as the build ID and the references to the ABI tag ([Contracts](contracts.html#linking)).
The size at the top of this page is a measurement: it is that of `build/hello` in CI's `x86_64 gcc 14 tiny` job, built by Ubuntu 24.04's GCC 14.2 and binutils 2.42, with the setup of the stack protector, which its `main` needs. The job reports it as a notice on every run, and fails when it is over 2048 bytes, the ceiling of `ci/size-ceiling.sh`, as the `x86_64 gcc 13 tiny` job and the aarch64 cross build's tiny do. That GCC turns on `-fcf-protection` by default, which adds `endbr64` instructions to the code, though in this hello the padding that aligns the read-only data after the code takes up their bytes, so a GCC 14 without that default, such as upstream GCC, can build a hello of the same size. Another compiler, binutils or change to the sources can give another size.
`configure` builds for the target of `CC` (default `gcc`): the arch is the first field of `$CC -dumpmachine`, x86_64 or aarch64. `AR`, `STRIP` and `OBJCOPY` default to the tools with `CC`'s prefix, or to the plain names when those are not installed, so `CC=aarch64-linux-gnu-gcc` alone sets up a cross build. `CFLAGS` and `LDFLAGS` are added after configure's own flags, and `SYSROOT` is passed as `--sysroot`. `ARCH` is still read for older callers, but it must equal the compiler's arch, or `configure` stops. A cross build can build the test suites without running them with `ninja tests`:
```sh
CC=aarch64-linux-gnu-gcc ./configure release
ninja all tests
```
In ubsan mode each undefined behavior check is a trap instruction, so no sanitizer runtime is linked: a program that hits one stops with SIGILL on x86_64 or SIGTRAP on aarch64. The binaries are not stripped, so `addr2line` maps the `ip` or `pc` that the kernel logs for it to the check's source line, less the base the kernel loaded it at for a static PIE, which `ci/crash-report.sh` subtracts. ubsan links with `lightc.ld`, or `lightc-pie.ld` with `--pie`, and the installed `lightc.pc` is the same in every mode.
In faultinject mode, `lc_syscall0` to `lc_syscall6`, `lc_kernel_clone` and the clone that starts a thread are C functions that check a table of fault rules before they make their system call. A rule armed with `lc_fault_arm` fails the Nth call of one system call, and as many after it as it says, with the errno it names, without making it: a test can make mmap fail with ENOMEM, or clone with EAGAIN, and see what the library does then. The mode also compiles chaos points into `lc_spinlock_acquire` and the heap's lock-free remote free, which yield or spin at random once `lc_chaos_seed` seeds them, to make threads interleave in more ways; lightc's thread suites take the seed from `LC_CHAOS` in the environment. `` declares this API only when `LC_FAULTINJECT` is defined, as the mode defines it, and no other mode compiles any of it, so a release library has none of its symbols.
## Example {#example}
A program defines `main` and links against the runtime, which supplies `_start`. No system headers are included.
```c [hello.c]{#hello}
#include
int main(int argc, char **argv, char **envp) {
(void)argc; (void)argv; (void)envp;
const char msg[] = "hello from lightc — no libc, no problem\n";
lc_kernel_write_bytes(STDOUT, msg, sizeof(msg) - 1);
return 0;
}
```
```sh
gcc -ffreestanding -nostdlib -nostartfiles -Iinclude \
hello.c build/liblightc.a -T lightc.ld -static -no-pie \
-Wl,--build-id=sha1 -Wl,--gc-sections -o hello
```
As a static PIE, with code compiled `-fPIE`, as most distributions' GCC and clang do by default, and entered at `_start_pie`, which `lightc-pie.ld` names and `-e` names whatever script follows:
```sh
gcc -ffreestanding -fPIE -nostdlib -nostartfiles -Iinclude \
hello.c build/liblightc.a -T lightc-pie.ld -static-pie -Wl,-e,_start_pie \
-Wl,--build-id=sha1 -Wl,--gc-sections -o hello
```
Neither names a stack protector flag, so the compiler's default holds: with most distributions' GCC, `-fstack-protector-strong`, which instruments `main` here for its array. lightc is built with it too, and seeds its canary from `AT_RANDOM` before `main` ([The stack protector](guide.html#stack-protector)).
`ninja install` puts the library, the headers, `lightc.pc` and the four linker scripts (in `lib/lightc/`) under `/usr/local`, below `$DESTDIR` if set. pkg-config then gives the same flags, with `-mno-outline-atomics` added on aarch64:
```sh
gcc $(pkg-config --cflags lightc) hello.c $(pkg-config --libs lightc) -o hello
```
The `--libs` flags are those of a static executable, the variable `libs_nopie`, in 0.2.1, and those of a static PIE, `libs_pie`, from the release after. `pkg-config --variable=libs_pie lightc` gives a static PIE's flags now: `-static-pie`, `-e _start_pie` and `-T` with `lightc-pie.ld` (`ldscript_tiny_pie` names its tiny twin), in place of `--libs`. Its code is compiled `-fPIE`, which `pkg-config --variable=cflags_pie lightc` gives for a compiler that does not by default. The `--cflags` flags give neither `-fPIE` nor `-fPIC`, so a plugin's `-fPIC` holds wherever it is among them. The `--libs` flags already pass `-T` with `lightc.ld`. A second `-T` is added to the first, not used in its place, so to link with `lightc-tiny.ld` swap its path in. `pkg-config --variable=ldscript_tiny lightc` prints that path:
```sh
gcc $(pkg-config --cflags lightc) hello.c \
$(pkg-config --libs lightc | sed "s|-T[^ ]*lightc\.ld|-T$(pkg-config --variable=ldscript_tiny lightc)|") -o hello
```
The `--cflags` flags also pass `include/lightc/compat/` with `-I`. `ninja install` puts lightc's `mm_malloc.h` there, which GCC's `` then finds in place of the compiler's, as that one includes the C library's ``: see [Intrinsics headers](guide.html#intrinsics).
See [Core & conventions](guide.html) for the startup sequence, the error model and the syscall layer.