Skip to content

Fix relocate stripping RPATH on cross-compiled builds (#37) - #352

Merged
dwoz merged 2 commits into
mainfrom
fix-issue-37-cross-compile-relocate
Nov 21, 2026
Merged

dwoz merged 2 commits into
mainfrom
fix-issue-37-cross-compile-relocate

Conversation

@dwoz

@dwoz dwoz commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Closes #37.

Problem

On a cross build (e.g. amd64 host building aarch64), relenv.relocate.handle_elf shells out to the host ldd on every built ELF. The host dynamic linker refuses to load a foreign-arch ELF -- it exits non-zero with empty stdout, or prints not a dynamic executable. The parser's for line in proc.stdout.decode().splitlines() iterates zero lines, needs_rpath stays False, and handle_elf falls through to remove_rpath(path).

Net effect: RPATH gets stripped from every single cross-compiled binary, so the cross-built onedir can't locate its own bundled libraries at runtime.

Issue filed 2022-11; survived because build-cross is commented out of CI so this path never gets exercised in practice.

Fix

Replace the host ldd with a DT_NEEDED parse of readelf -d. readelf just parses ELF metadata (arch-agnostic by construction), and _get_readelf_binary() already prefers the target toolchain's readelf when RELENV_BUILDENV points at a cross toolchain -- so even an x86_64 host building aarch64 picks up the right tool.

In rpath_only=True (the default -- what finalize uses) we don't need the host linker's path resolution at all: we just need to know which DT_NEEDED SONAMEs refer to libraries already laid down under libs/. That's a filesystem-local check that works identically for native and cross builds.

New helpers in relenv/relocate.py:

  • parse_readelf_d_needed(stdout) -- extract SONAMEs from NEEDED lines.
  • read_needed(path) -- routes through _get_readelf_binary().

handle_elf rewritten to use them. The non-rpath-only branch (manual relenv relocate CLI on native onedirs) retains the ldd fallback for copying missing libs from the host, which is only reachable from a native invocation.

Tests

  • New test_parse_readelf_d_needed + _empty + _malformed unit tests on the parser.
  • Existing test_handle_elf* updated to mock read_needed instead of subprocess.run (semantic contract unchanged).
  • New test_handle_elf_keeps_rpath_on_cross_build -- the specific relocate fails on cross compiled builds #37 regression. Simulates host ldd exiting non-zero with empty stdout, asserts the binary's RPATH is patched (not removed) because DT_NEEDED shows a bundled dep.

All 28 test_relocate.py tests pass locally.

On a cross build (e.g. amd64 host building aarch64), ``handle_elf``'s
original ``ldd`` call fails -- the host dynamic linker either refuses to
load a foreign-arch ELF ("not a dynamic executable") or exits non-zero
with empty stdout.  The parser silently iterates zero lines,
``needs_rpath`` stays ``False``, and the else-branch calls
``remove_rpath(path)`` on every single cross-built binary.  Net effect:
RPATH gets stripped from an entire cross-compiled onedir, which then
can't find its own bundled libs at runtime.  Issue is from 2022-11; it
has survived because CI's ``build-cross`` job is currently commented
out and nobody exercises the path.

Replace the host ``ldd`` with a DT_NEEDED parse of ``readelf -d``:

* ``parse_readelf_d_needed(stdout)`` -- extract SONAMEs from the dynamic
  section's ``NEEDED`` entries.  Pure parser over text we already know
  how to produce.
* ``read_needed(path)`` -- call site that routes through the existing
  ``_get_readelf_binary()`` helper, so toolchain-prefixed ``readelf``
  (e.g. ``aarch64-linux-gnu-readelf``) is used when ``RELENV_BUILDENV``
  points at a cross toolchain.  ``readelf`` just parses ELF metadata,
  so it's arch-agnostic by construction.

``handle_elf`` rewritten to:

1. Pull DT_NEEDED SONAMEs via ``read_needed`` (toolchain-aware).
2. For each SONAME, skip the glibc allow-list, then check if a file of
   that name exists under ``libs/`` (or anywhere under ``root/`` as a
   shallow fallback for arch-specific subdirs).  Found -> needs_rpath.
3. In ``rpath_only=True`` mode (the default, what finalize uses) a
   missing file is just a warning -- the binary links to something we
   don't bundle; leave it to the system loader.  No host ``ldd``
   involvement at all.
4. In ``rpath_only=False`` mode (the manual ``relenv relocate`` CLI on
   already-installed native onedirs) we still fall back to host ``ldd``
   to resolve the SONAME for copying, since that's the only way to find
   the source file on a native host.  Cross builds never use this path.

Tests in ``tests/test_relocate.py``:

* New ``test_parse_readelf_d_needed`` + ``_empty`` + ``_malformed``
  covering the parser.
* Existing ``test_handle_elf*`` updated to mock ``read_needed`` instead
  of ``subprocess.run`` (which now goes to ``readelf`` + an optional
  ``ldd`` fallback).  Semantics unchanged.
* New ``test_handle_elf_keeps_rpath_on_cross_build`` -- the specific
  gh-37 regression: simulate host ``ldd`` exiting non-zero with empty
  stdout, assert the binary's RPATH is patched (not removed) because
  the DT_NEEDED entries show a bundled dependency.

All 28 relocate tests pass locally.  CI's native builds will keep
exercising the native path; cross CI remains out of scope here but the
bug it was always going to surface is now fixed.
This PR replaced handle_elf's ldd shell-out with a readelf -d DT_NEEDED
parse via read_needed(), but the existing unit test was still shimming
subprocess.run for ldd and raising AssertionError on anything else.
Stub read_needed directly so the test exercises the new arch-agnostic
path.
@dwoz
dwoz merged commit 8db8f10 into main Nov 21, 2026
89 checks passed
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.

relocate fails on cross compiled builds

1 participant