grassleaff/xld
2
1
Fork
You've already forked xld
0
xld is a ELF64 x86-64 arch static linker for Linux. It links GCC-generated (and other compilers) object files into standalone executables without relying on the system linker, focusing on clarity, low-level control, and a deep understanding of the ELF format and program loading process.
  • C 94.8%
  • Makefile 5.2%
2026年04月12日 03:00:36 -03:00
assets ADD: New logo and update README.md 2026年04月11日 03:51:55 -03:00
examples ADD: .text, .rodata, .data and .bss support + relocations implemantation 2026年02月15日 01:35:39 -03:00
src ADD: New flags options for compiler driver linking with cc 2026年04月12日 03:00:36 -03:00
.gitignore Initial commit 2026年02月10日 03:35:40 -03:00
LICENSE Add MIT License 2026年02月10日 06:38:33 +00:00
Makefile Initial commit 2026年02月10日 03:35:40 -03:00
README.md FIX: fix the honorable mention again </3 2026年04月11日 09:10:24 +02:00

xld

xld is a static linker for ELF64 x86-64 on Linux. It links GCC-generated object files into standalone executables without relying on the system linker. This project emphasizes clarity, low-level control, and a deep understanding of the ELF format and program loading process.

Features

  • Static linking for ELF64 x86-64 object files
  • Fully independent of the system linker
  • Reads ELF headers, section headers, symbol tables, and relocations manually
  • Supports .text, .rodata, .data and .bss sections
  • Generates standalone executables with custom memory layout

Memory Layout

xld sets a static memory layout for the executable:

  • BASE_ADDR: 0x400000
  • PAGE_SIZE: 0x1000
  • TEXT_ADDR: BASE_ADDR + PAGE_SIZE

Sections are mapped as:

Section Runtime Address
.text TEXT_ADDR
.rodata Immediately after .text
.data Aligned to PAGE_SIZE after .rodata
.bss Immediately after .data

The linker calculates addresses using align_up() to ensure proper page alignment.

ELF Parsing

xld reads ELF files manually:

  1. Reads the ELF header (Elf64_Ehdr) to determine section table offsets and number of sections.
  2. Reads section headers (Elf64_Shdr) to identify .text, .rodata, .data, .bss.
  3. Reads symbol tables (SHT_SYMTAB) to locate the entry symbol or other symbols needed for relocations.
  4. Reads relocation sections (SHT_RELA) and applies them manually to the .text section.
  5. Functions like find_sections(), find_entry_symbol(), and apply_relocations() handle these tasks.

Relocation Handling

  • Currently supports only R_X86_64_PC32 relocations.
  • For each relocation:
    1. Compute S, the runtime address of the target symbol.
    2. Compute P, the runtime address of the relocation location.
    3. Compute A, the addend from the relocation entry.
    4. Write the 32-bit value (S + A - P) into the .text buffer. This ensures that all relative calls and jumps point to the correct runtime addresses.

Program Headers

xld generates two program headers:

  1. LOAD 1 (RX) – Includes headers, .text and .rodata.
  2. LOAD 2 (RW) – Includes .data and .bss

Each header specifies:

  • Virtual and physical addresses (p_vaddr and p_paddr)
  • File size (p_filesz) and memory size (p_memsz)
  • Permissions (PF_R | PF_X for code, PF_R | PF_W for data)
  • Alignment (PAGE_SIZE)

Installation

Clone and build:

git clone https://codeberg.org/grassleaff/xld
cd xld
make # or sudo make install (system-wide installation)

Makefile Options

Command Description
make or make all Build xld (same as make build)
make build Compile the xld executable from src/
make install Install xld to /usr/local/bin
make uninstall Remove the installed executable
make examples Build example .S files located on examples/ into .out executables
make run-examples Build and run all examples sequentially
make clean Remove xld binary, object files, and compiled examples

Usage

Link object files:

xld input1.o input2.o -o my_program

Run examples:

make run-examples

Honorable mention

1 - 0xdd / eighty_twenty

xld is used in this project as the linker for the minimalist C toolchain that supports the et language ecosystem. It has been adapted to work with the etstd library, allowing it to be compiled and executed in an extremely minimal environment, without dependencies such as libc or complex toolchains.

In this context, xld is responsible for linking the generated binaries (including the compiler itself, etc.), supporting specific relocations required for this simplified environment and enabling the construction of fully static executables in a nearly self-contained system.


© 2026 grassleaff. All rights reserved.
Licensed under the MIT License.