Skip to content

fix(elf): accept the standard rustc GNU linker flags - #115

Closed
zackees wants to merge 8 commits into
mainfrom
fix/native-gnu-linker-flags
Closed

zackees wants to merge 8 commits into
mainfrom
fix/native-gnu-linker-flags

Conversation

@zackees

@zackees zackees commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

Progress toward making reld usable as a direct linker via SOLDR_LINKER=reld on native GNU targets. Five gaps fixed so far:

  1. Standard GNU-driver flags — -m64 maps to x86-64 (reject -m32 clearly), -Wl,<flags> pass-through is expanded, -B<prefix> is a no-op, -nodefaultlibs is ignored.
  2. Default library search paths — reld only searched -L; it now honors LIBRARY_PATH and the compiler driver's own cc -print-search-dirs libraries: list (so -lgcc_s/-lc/etc. resolve even in non-FHS environments like Nix), plus common system dirs. These driver dirs are kept out of the Nix RUNPATH derivation.
  3. PT_INTERP — dynamic_linker was unset, so dynamically-linked output had no interpreter and segfaulted at startup. Now defaults per architecture (/lib64/ld-linux-x86-64.so.2 on x86-64).
  4. CRT startup objects — a direct linker must supply crt1.o/crti.o/crtbegin.o (prefix) and crtend.o/crtn.o (suffix) itself, or the output has no _start and the entry point silently falls back to .text, segfaulting at startup. reld now locates them via cc -print-file-name and injects them around the user's inputs for executables.

Validation

  • cargo test -p reld-core — 188 passed.
  • CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=reld cargo build -p reld now produces a runnable binary: ./target/release/reld --version prints the version and exits 0. The output has a proper PT_INTERP and _start entry point (verified via readelf/nm), and no RUNPATH pollution from the driver dirs.

Co-Authored-By: Claude Code [email protected]

zackees and others added 7 commits September 15, 2026 18:53
rustc passes a handful of GNU-driver flags when reld is the direct
`CARGO_TARGET_*_LINKER` (as `SOLDR_LINKER=reld` makes it). reld's native
ELF parser rejected them, so the build failed before reaching a real link.

- `-m64` maps to x86-64 (and `-m32` is rejected with a clear message).
- Expand `-Wl,<flags>` pass-through args (split on commas) so
  `-Wl,-z,relro,-z,now` parses as `-z relro -z now`.
- `-B<prefix>`: no-op (reld is the linker, it never invokes gcc-ld).
- `-nodefaultlibs`: no-op (reld adds no default libraries of its own).

Progress toward `SOLDR_LINKER=reld` on native GNU targets.

Co-Authored-By: Claude Code <[email protected]>
reld had no default library search paths, so as a direct linker it
couldn't resolve `-lgcc_s`, `-lc`, etc. (which the gcc driver normally
supplies). Add `LIBRARY_PATH` and the common multiarch/system dirs.

Co-Authored-By: Claude Code <[email protected]>
reld left `dynamic_linker` unset, so dynamically-linked output had no
PT_INTERP segment and segfaulted at startup (the kernel can't load the
interpreter). Default to the per-architecture interpreter when
`--dynamic-linker` isn't given.

Co-Authored-By: Claude Code <[email protected]>
A direct linker (reld, as opposed to the gcc/clang driver) must supply the
CRT startup objects itself — otherwise the output has no `_start` symbol and
the entry point silently falls back to the .text start, producing a binary
that segfaults at startup (PC=0x0/0x1). Locate crt1/crti/crtbegin (prefix) and
crtend/crtn (suffix) via `cc -print-file-name` and inject them around the
user's inputs for executables only.

Co-Authored-By: Claude Code <[email protected]>
reld hardcoded /usr/lib, /lib, etc. — fine on FHS systems, but it could not
find libgcc_s/libc in non-FHS environments (Nix) where those live in store
paths reachable only through the gcc driver's own search dirs. Query
`cc -print-search-dirs` and add its `libraries:` list.

Those dirs are driver defaults, not user `-L` flags, so they must not seed the
Nix RUNPATH derivation (which mirrors nixpkgs' ld-wrapper and keys off `-L`
alone). Record the user-`-L` boundary in `user_lib_search_path_len` and have
the derivation honor it.

Co-Authored-By: Claude Code <[email protected]>
When reld is driven via `clang -fuse-ld=reld` (the consumer-acceptance
path), the clang driver already injects Scrt1.o/crti.o/crtbegin.o on the
command line. reld's own CRT injection then added them a second time,
producing 'Duplicate symbols detected: _IO_stdin_used'. Detect whether a CRT
startup object is already among the inputs and skip injection in that case —
direct `-C linker=reld` invocation (rustc, no driver) still injects.

Co-Authored-By: Claude Code <[email protected]>
@zackees
zackees force-pushed the fix/native-gnu-linker-flags branch from c6cdde9 to 180f02c Compare September 16, 2026 01:53
Standalone fixtures (e.g. the linker-diff corpus) compile their own `_start`
and link without CRT; injecting crt1.o then yields a duplicate `_start`. Scan
the plain object inputs for a `_start` definition and skip injection when
present, so reld matches the direct-linker behaviour of mold/wild/lld there
while still supplying CRT for rustc's `-C linker=reld` path.

Co-Authored-By: Claude Code <[email protected]>
@zackees

zackees commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: reld needs no changes. It already works as a pure linker via clang -fuse-ld=reld (verified for C and Rust). The CRT-injection/PT_INTERP approach broke reld's correctness oracle (lld/mold/wild parity). The correct fix is in soldr: use the clang driver (-fuse-ld=reld) instead of direct -C linker=reld.

@zackees zackees closed this Sep 16, 2026
@zackees
zackees deleted the fix/native-gnu-linker-flags branch September 16, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant