Skip to content

elf: linked binaries segfault at startup (dynamic relocations resolve to bad addresses) #117

Closed as not planned

Description

@zackees

reld's native ELF backend produces binaries that segfault at startup — the dynamic relocations resolve to a bogus address.

Evidence (after the flag / library-path / PT_INTERP fixes in #115)

CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=reld cargo build -p reld now links, but every linked build-script binary crashes on launch:

Program received signal SIGSEGV (PC = 0x0000000000000001)

The output is a DYN (PIE) with 453 R_X86_64_RELATIVE, 45 R_X86_64_GLOB_DAT, and 1 R_X86_64_64 dynamic relocations, plus a correct PT_INTERP (/lib64/ld-linux-x86-64.so.2). The PC landing at 0x1 means a GOT/PLT slot (or a R_X86_64_RELATIVE addend) resolves to an invalid address — the dynamic-relocation computation is wrong.

Fix

Trace reld's dynamic-relocation emission/resolution for R_X86_64_RELATIVE/R_X86_64_GLOB_DAT (and the GOT/PLT layout) so each slot resolves to load_base + addend (or the correct symbol address) rather than 0x1. A minimal repro is any -C relocation-model=pic Rust build linked with reld.

Refs #115.

Activity

  1. zackees commented on Sep 16, 2026

    @zackees
    CollaboratorAuthor

    Deeper findings from live debugging:

    1. __dso_handle emitted as a dynamic relocation for an undefined symbol. readelf -rW shows a lone R_X86_64_64 targeting __dso_handle, which is UND WEAK (dynsym Position reld as a wild successor; add test, stress, and benchmark infrastructure #1). Rust actually provides it via the local _rust_extern_with_linkage_…___dso_handle symbol (a LOCAL OBJECT at the same address). reld emits a dynamic relocation against the undefined __dso_handle instead of resolving it to that local definition — so at runtime the slot resolves to 0 (weak undef) rather than the real __dso_handle address.

    2. Startup crash is a jump to 0x1 (not 0): a GOT/PLT/function-pointer slot resolves to 0x1. Relocations present: 453 R_X86_64_RELATIVE, 45 R_X86_64_GLOB_DAT, 1 R_X86_64_64. No R_X86_64_JUMP_SLOT (expected for -z now/PIE). The 0x1 strongly suggests an unrelocated or mis-resolved GOT slot.

    Repro: CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=reld cargo build -p reld (any Rust build with reld as the direct linker).

  2. zackees commented on Sep 16, 2026

    @zackees
    CollaboratorAuthor

    Root cause found — it's not a relocation bug, it's missing CRT startup objects.

    • rustc passes -B<sysroot>/…/bin/gcc-ld and the object files, but not crt1.o////. The gcc driver normally injects those; a direct linker (reld) must supply them itself.
    • So reld's output has no _start symbol, and elf_entry_address falls back to the .text section start (0x12ad0 = __rust_alloc) instead of _start.
    • The program then starts executing __rust_alloc → jmp __rdl_alloc with garbage registers, which is the observed startup crash (PC eventually lands at 0x1).

    _start is confirmed absent from reld's output (readelf -sW) but present in cc-linked binaries (nm target/release/reld | grep _start → T _start), and absent from the std rlib (it comes from crt1.o).

    Fix: link the CRT startup objects for executables — locate crt1.o (or Scrt1.o for PIE) + crti.o/// (via the -B prefix / gcc -print-file-name / system CRT dirs) and add them to the link so _start is defined.

  3. zackees commented on Sep 16, 2026

    @zackees
    CollaboratorAuthor

    Re-diagnosed and resolved differently than filed. The 'segfault at startup' was not bad dynamic relocations — it was a missing _start: a direct -C linker=reld invocation has no CRT startup objects, so the entry point fell back to .text and the binary jumped to address 0/1.

    Resolution: reld is a pure linker and is not meant to inject the CRT itself (its correctness oracle in crates/reld/tests/acceptance.rs compares against lld/mold/wild, which also don't inject CRT). The fix is to drive reld through the clang driver (clang --ld-path=reld), which injects CRT + -dynamic-linker. This is done on the soldr side in soldr#3261.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions