Freestanding builds
A freestanding build produces code that runs without a hosted C standard library, without crt0 and without an operating
system, e.g. kernels, bootloaders, firmware or wasm32-unknown-unknown modules. This is the equivalent of
-ffreestanding -nostdlib in C or #![no_std] in Rust.
Enabling the freestanding mode¶
The freestanding mode is enabled with the --freestanding option of the build, run
and install subcommands. Targets without an operating system enable it implicitly, e.g.
x86_64-unknown-none, aarch64-none-elf, thumbv7em-none-eabihf, riscv32-unknown-none-elf or
wasm32-unknown-unknown. Kernel code for a Linux target passes --freestanding explicitly.
| Bash | |
|---|---|
In freestanding mode, the compiler:
- does not require a
mainfunction. The user defines the entry point instead (see below). Amainfunction is still generated, if there is one, e.g. for a self-written crt0. - rejects the
printfandstderrbuiltins, since they need a hosted libc. Thesyscallandasmbuiltins as well as__volatile_loadand__volatile_storestay available. They are the main way to talk to the hardware or to a host kernel. Thesyscallbuiltin is only available for targets with an operating system. - routes panics, failed assertions to the panic handler (see below).
- prints no stack traces and emits no unwind tables (no
.eh_framesection). - generates static code by default (
--relocation-model=static). Shared libraries staypic, and the TPDE backend as well as Windows and Darwin targets staypie, since they only support position independent code. An explicitly given relocation model wins, as long as it is supported for the output container and the target. - links with
-nostdlib -staticand without the PIE and RELRO handling. For other targets than the host, it links withld.lld, since the linkers of the host cannot link for bare-metal targets. WebAssembly modules are linked withwasm-ld, without-static, but without entry point and with all symbols exported (--no-entry --export-all), see Compile for WebAssembly. - links the builtins of the compiler runtime, if they are available for the target (see Compiler runtime builtins).
- rejects imports of modules, that need an operating system (see Standard library tiers).
Sanitizers (--sanitizer), code coverage instrumentation (--coverage) and the test subcommand are not supported in
freestanding mode, since their runtimes need a hosted libc. --output-container=obj emits an object file, if you want to
link it yourself.
Entry point¶
The entry point is an unmangled procedure, that never returns. Its name depends on the linker script or the platform,
for a Linux executable it is _start:
| Spice | |
|---|---|
This program runs on x86_64 Linux without libc:
Panic handler¶
Without an operating system, there is no stderr to print a panic message to and no process to exit. Therefore, panics,
failed assertions and accesses to inactive union fields call the panic handler of the program. It is a procedure with the
core.compiler.panicHandler attribute:
| Spice | |
|---|---|
The handler must have exactly this signature and must not return. There is at most one panic handler per program, but
it can live in any source file. Without a panic handler, panics lower to a trap instruction (llvm.trap).
Heap allocations¶
The heap types of the standard library, e.g. String, Vector or Result, allocate their memory on the heap. In
freestanding mode, there is no libc, that could allocate it. So the program provides the allocator itself, as three
functions with these attributes and signatures:
They can be defined in any source file, but there is at most one of each kind per program. The compiler exports them
under the names of the C functions malloc, realloc and free, which the runtime of the standard library calls. If
the program uses a heap type without defining all three, the compiler reports an error. How the allocator gets its memory
depends on the platform, e.g. from a static region, that the linker script reserves, or from a host kernel via mmap.
C functions¶
The code, that LLVM generates, may call memcpy, memmove, memset, memcmp and bcmp, even if the program never
calls them, e.g. for copying a struct. The runtime of the standard library also calls strlen and strcmp. In
freestanding mode, the compiler links implementations of these functions into the program. They are weak symbols, so the
program can bring its own implementations instead, e.g. optimized ones:
| Spice | |
|---|---|
The core.compiler.noBuiltins attribute keeps LLVM from turning the loop of such an implementation into a call to the
function itself.
Error return tracing (#![core.compiler.errorReturnTracing]) is disabled in freestanding mode, since the trace is stored
thread-local and printed to stderr. Error.dumpTrace() and Result.dumpTrace() do nothing in freestanding mode.
Platform specific source files¶
The compiler picks platform specific source files by their suffix, e.g. foo_linux.spice or foo_linux_x86_64.spice.
In freestanding mode, the OS part of the suffix is freestanding. So an import "foo"; resolves to the first one of
these files, that exists:
foo_freestanding_x86_64.spicefoo_freestanding.spicefoo_x86_64.spicefoo.spice
Standard library tiers¶
The modules of the standard library fall into three tiers:
| Tier | Requirement | Examples |
|---|---|---|
| Core | none | primitive types, Result and Error, iterators, std/text/analysis, std/os/volatile |
| Alloc | allocator functions | String, Vector, Map, UnorderedMap, Queue and the other types of std/data |
| OS | hosted operating system | std/io, std/net, std/time, std/os/thread, std/os/env, std/type/type-conversion |
The modules of the OS tier, that call into the operating system or the C standard library, have the
#![core.compiler.requiresOs] module attribute. Importing one of them in freestanding mode is a compile error, as well as
importing a module, that depends on one of them:
| Text Only | |
|---|---|
Your own modules can have the attribute as well, e.g. the hosted part of a library, that supports both modes. Since such a module is rejected anyway, its own imports are not resolved in freestanding mode.
Note
Not all modules of the OS tier have the attribute yet, e.g. std/io/file, std/os/thread and
std/type/type-conversion do not. The compiler itself uses them and is built with the previous release, which does
not know the attribute. Using them in freestanding mode fails later, e.g. with unresolved symbols like snprintf at link
time.
Compiler runtime builtins¶
LLVM lowers some operations to calls into the builtins library of the compiler runtime, if the target has no instructions
for them, e.g. 128-bit division (__udivti3), floating point arithmetic on targets without FPU or the __aeabi_* helpers of
32-bit Arm. Hosted builds get the library from the linker invoker. In freestanding mode, the compiler asks the linker invoker
for the library of the target (clang --target=<triple> --rtlib=compiler-rt -print-libgcc-file-name, or libgcc for GCC)
and links it, if it exists. Distributions often ship compiler-rt for the host only. For other targets, build compiler-rt
for the target or take the library of the toolchain of the target, and pass it yourself:
| Spice | |
|---|---|
Embedded targets¶
This program runs on the virt machine of QEMU, emulating a bare-metal AArch64 board. It writes to the UART of the board
and stops QEMU via semihosting, which
reports the exit code of the program:
QEMU starts the program without a stack and with the MMU off, so a few lines of assembly set up the stack, before they
call the Spice code. They also
allow FP and SIMD instructions, which LLVM uses e.g. for copying memory. The file is compiled by the linker invoker via the
core.linker.additionalSource attribute:
| GAS | |
|---|---|
The linker script places the program in the RAM of the board, which starts at 0x40000000, and reserves the stack:
| Text Only | |
|---|---|
With the MMU off, all data accesses are Device memory accesses, which fault, if they are unaligned. So the code is
generated without unaligned accesses (--cpu-features=+strict-align), until the program sets up the MMU: