Skip to content

5 3 x86 64 code generation and zero libc elf linking

TransCompilator edited this page Nov 26, 2026 · 1 revision

5.3 x86-64 Code Generation and Zero-libc ELF Linking

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/5-3-x86-64-code-generation-and-zero-libc-elf-linking

x86-64 Code Generation and Zero-libc ELF Linking

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

This page covers the architecture, implementation, and verification of the x86-64 backend (tc-codegen-x86). The backend translates verified Core-7 intermediate representation (tc_ir::Module) into Intel-syntax x86_64 assembly, compiles it via system assemblers and linkers (as and ld), and emits standalone, statically linked ELF64 binaries that require zero C library dependencies (libc). It also documents the differential parity and fuzzing infrastructure used to guarantee semantic equivalence between native execution and the reference interpreter.


1. Instruction Emission and Intel-Syntax Generation

The code generation pipeline begins with emit_module, which constructs an Emitter instance over a verified Module crates/tc-codegen-x86/src/emit.rs:18-25. The output assembly defaults to Intel syntax using .intel_syntax noprefix crates/tc-codegen-x86/src/emit.rs:35 and includes sections for read-only data (.rodata), initialized globals (.data), zero-initialized uninitialized storage (.bss), and executable instructions (.text) crates/tc-codegen-x86/src/emit.rs:36-116.

Register Allocation and Calling Convention

The emitter adheres to the System V AMD64 ABI for integer and floating-point registers:

Program Entry and Syscall Bootstrap

The entry point of the generated binary is explicitly defined as _start crates/tc-codegen-x86/src/emit.rs:122-123. If the entry function accepts parameters, _start unpacks argc, argv, and environment pointers from the stack (rsp), aligns the stack pointer to a 16-byte boundary via and rsp, -16, and invokes the lower-level function wrapper tc_fn_{id} crates/tc-codegen-x86/src/emit.rs:124-135. Upon return, it moves the exit code into edi, loads the exit system call number (60), and issues a raw syscall instruction without depending on libc initialization hooks crates/tc-codegen-x86v/src/emit.rs:136-149 (cite: crates/tc-codegen-x86/src/emit.rs:136-149).

graph TD
    subcode["Code Entity Space"]
    Module[tc_ir::Module] --> Emitter[tc_codegen_x86::emit_module]
    Emitter --> FunctionEmitter[FunctionEmitter::emit]
    FunctionEmitter --> Assembly["\".intel_syntax noprefix\""]
    Assembly --> Entry["_start Bootstrap"]
    Entry --> Syscall["Raw Syscall 60 (exit)"]

    Sources["Sources: crates/tc-codegen-x86/src/emit.rs"]
Loading

Sources: crates/tc-codegen-x86/src/emit.rs:18-150


2. Zero-libc Assembler and Linker Invocation

Once the assembly text is generated, assemble_and_link handles translation to machine code without linking against standard dynamic or static C libraries crates/tc-codegen-x86/src/link.rs:13-14.

Temporary Workspace Isolation

To support concurrent compilation tasks and test runners, assemble_and_link generates a unique temporary directory using process identifiers and an atomic counter (TEMP_COUNTER) crates/tc-codegen-x86/src/link.rs:10-17. It writes the assembly to module.s and invokes external GNU toolchain utilities crates/tc-codegen-x86/src/link.rs:20-22:

  1. Assembler (as): Invoked with --64 to produce an ELF object file (module.o) crates/tc-codegen-x86/src/link.rs:30-40. Errors are captured and mapped to CodegenError::Assembler crates/tc-codegen-x86/src/link.rs:37-39.
  2. Linker (ld): Invoked with -static to produce a fully self-contained static ELF64 binary (out_path) crates/tc-codegen-x86/src/link.rs:42-47. Errors are mapped to CodegenError::Linker crates/tc-codegen-x86/src/link.rs:48-52.
graph TD
    subcode["Code Entity Space"]
    AssemblyText[AssemblyString] --> WriteAsm["fs::write(module.s)"]
    WriteAsm --> AsCommand["Command::new(\"as\") --64"]
    AsCommand --> ObjFile[module.o]
    ObjFile --> LdCommand["Command::new(\"ld\") -static"]
    LdCommand --> StaticELF[StaticELF64Binary]

    Sources["Sources: crates/tc-codegen-x86/src/link.rs"]
Loading

Sources: crates/tc-codegen-x86/src/link.rs:13-58


3. Differential Parity Testing and IR Fuzzing

To verify code generation correctness, TransCompilator employs strict differential testing between the static ELF64 binary execution and the byte-accurate reference interpreter (tc_ir::interpret) crates/tc-ir/src/interp.rs:1, crates/tc-codegen-x86/tests/phase0_core7_parity.rs:1-13 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:1-13).

Parity Assertions

The test harness function assert_parity verifies modules through a rigorous sequence crates/tc-codegen-x86/tests/phase0_core7_parity.rs:59-109:

  1. Verifies module structural and typing correctness using verify_module crates/tc-codegen-x86/tests/phase0_core7_parity.rs:66 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:66).
  2. Executes the module in the reference interpreter (interpret) to capture expected exit codes, stdout, and stderr crates/tc-codegen-x86/tests/phase0_core7_parity.rs:68-82 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:68-82).
  3. Compiles the module to a temporary ELF64 binary using compile_to_elf crates/tc-codegen-x86/tests/phase0_core7_parity.rs:85 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:85).
  4. Spawns the compiled binary (run_elf) with controlled argv and stdin streams crates/tc-codegen-x86/tests/phase0_core7_parity.rs:34-57, 86 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:34-57, 86).
  5. Asserts exact byte-for-byte equality of exit codes, standard output, and standard error crates/tc-codegen-x86/tests/phase0_core7_parity.rs:89-108 (cite: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:89-108).
graph TD
    subcode["Code Entity Space"]
    ModuleInput[Module] --> Verify["tc_ir::verify_module"]
    Verify --> Interpret["tc_ir::interpret"]
    Verify --> Compile["tc_codegen_x86::compile_to_elf"]
    Interpret --> InterpResult[InterpOutput]
    Compile --> ELFBinary[BinaryPath]
    ELFBinary --> RunELF["run_elf()"]
    RunELF --> ELFResult[RunResult]
    InterpResult --> AssertEqual["assert_eq!(ELFResult, InterpResult)"]
    ELFResult --> AssertEqual

    Sources["Sources: crates/tc-codegen-x86/tests/phase0_core7_parity.rs"]
Loading

Sources: crates/tc-codegen-x86/tests/phase0_core7_parity.rs:59-109

Differential IR Fuzzer

The fuzzer binary (battletest_fuzz_ir) generates deterministic, verified IR modules across multiple categories (scalars, floating-point operations, control flow loops, indirect calls, structs, and raw syscalls) based on pseudo-random seeds crates/tc-codegen-x86/src/bin/battletest_fuzz_ir.rs:58-68 (cite: crates/tc-codegen-x86/src/bin/battletest_fuzz_ir.rs:58-68). Each generated module is compiled to an ELF64 binary and run against the reference interpreter to catch edge cases in register allocation, sign/zero extensions, float-to-int saturations, and stack layout offsets.

Sources: [crates/tc-codegen-x86/src/bin/battletest_fuzz_ir.rs:1-520], [crates/tc-codegen-x86/tests/battletest_core_regressions.rs:1-259]*


Clone this wiki locally