Skip to content

patchelf does not update DT_VERDEF when it relocates .gnu.version_d, so glibc segfaults in _dl_check_map_versions #664

Closed

Description

@epfister-cyncly

Describe the bug

When patchelf relocates the dynamic-section cluster, it rewrites DT_SYMTAB, DT_STRTAB, DT_VERSYM and DT_VERNEED to the new section addresses, but it never rewrites DT_VERDEF. That tag keeps the pre-relocation address.

If the object defines its own symbol versions, glibc follows the stale DT_VERDEF in _dl_check_map_versions and the process dies with SIGSEGV before main() runs.

Root cause

src/patchelf.cc, in the loop that updates the virtual addresses in .dynamic (currently lines 1334-1431 on master):

else if (d_tag == DT_VERNEED)
    dyn->d_un.d_ptr = findSectionHeader(".gnu.version_r").sh_addr;
else if (d_tag == DT_VERSYM)
    dyn->d_un.d_ptr = findSectionHeader(".gnu.version").sh_addr;

DT_VERDEF has no branch, so .gnu.version_d is the only relocated version section whose tag is left behind. DT_VERDEF is already defined in src/elf.h:969.

Steps To Reproduce

Any bun build --compile single-file executable reproduces this. Those are LLD-linked, and LLD places .dynstr after the .gnu.version* sections. When patchelf grows .dynstr it therefore drags .gnu.version_d along. GNU ld places .dynstr before the version sections, and the small ld.bfd binaries I tried did not relocate .gnu.version_d at all.

No Nix and no patchelf build are needed:

curl -fsSLO https://github.com/oven-sh/bun/releases/download/bun-v1.4.0/bun-linux-aarch64.zip
unzip -j bun-linux-aarch64.zip

echo 'console.log("hello from compiled bun");' > hello.js
./bun build --compile hello.js --outfile hello-exe

./hello-exe                 # prints: hello from compiled bun

cp hello-exe probe
patchelf --set-rpath /x probe

readelf -S probe | grep '\.gnu\.version_d'
readelf -d probe | grep VERDEF
./probe                     # SIGSEGV

Observed:

  [ 6] .gnu.version_d    VERDEF           00000000001f6788  00006788
 0x000000006ffffffc (VERDEF)              0x2066d8
 0x000000006ffffffd (VERDEFNUM)           2

.gnu.version_d moved to 0x1f6788. DT_VERDEF still reads 0x2066d8.

The upstream bun binary itself reproduces this too, so the appended payload in the compiled output is not involved.

patchelf --debug shows the move plainly:

replacing section '.dynamic' with size 512
replacing section '.dynstr' with size 16549
replacing section '.gnu.version_d' which is in the way
...
rewriting section '.gnu.version_d' from offset 0x166d8 (size 56) to offset 0xb898 (size 56)

Expected behavior

DT_VERDEF points at .gnu.version_d after patching, and the binary still runs.

patchelf --version output

Reproduced with all three of:

  • patchelf 0.15.2 (current nixpkgs default)
  • patchelf 0.18.0-unstable-2025-08-13 (nixpkgs patchelfUnstable)
  • patchelf 0.19.1 built from master 7688b17c18d16f67fa8d5a82a2404c2e3a18648d

Which operations trigger it

I applied one operation at a time to a pristine copy. Anything that grows .dynamic triggers the relocation and leaves DT_VERDEF stale:

operation result
--set-interpreter safe, does not grow .dynamic
--set-rpath stale DT_VERDEF, SIGSEGV
--add-rpath stale DT_VERDEF, SIGSEGV
--add-needed stale DT_VERDEF, SIGSEGV

RPATH length does not matter. A two-character /x is enough.

Evidence that the loader dies before program code

LD_TRACE_LOADED_OBJECTS=1 ./probe also segfaults. That variable normally makes ld.so print the object list and exit without running the program, so nothing in the program has executed yet.

gdb backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x0000fffff7fd1640 in _dl_check_map_versions () from .../lib/ld-linux-aarch64.so.1
#0  _dl_check_map_versions ()
#1  _dl_check_all_versions ()
#2  version_check_doit ()
#3  _dl_receive_error ()
#4  dl_main ()
#5  _dl_sysdep_start ()
#6  _dl_start ()
#7  _start ()

readelf -V also reads out of bounds when the stale address happens to fall outside the file, reporting Reading 20 bytes extends past end of file for version def. That symptom is layout dependent, so the reliable indicators are the DT_VERDEF mismatch and the crash.

Proposed fix

Three lines, mirroring the existing DT_VERNEED branch:

             else if (d_tag == DT_VERNEED)
                 dyn->d_un.d_ptr = findSectionHeader(".gnu.version_r").sh_addr;
+            else if (d_tag == DT_VERDEF)
+                dyn->d_un.d_ptr = findSectionHeader(".gnu.version_d").sh_addr;
             else if (d_tag == DT_VERSYM)
                 dyn->d_un.d_ptr = findSectionHeader(".gnu.version").sh_addr;

I built master with and without this change and ran the reproducer against both:

=== patchelf-unfixed (patchelf 0.19.1) ===
  version_d sh_addr=0x1d67c0   DT_VERDEF=0x2066d8
  run: exit=139 out=[]

=== patchelf-fixed (patchelf 0.19.1) ===
  version_d sh_addr=0x1d67c0   DT_VERDEF=0x1d67c0
  run: exit=0 out=[hello from compiled bun]

make check gives the same result before and after the change: 63 pass, 4 skip, 2 fail. The two failures, no-rpath.sh and shared-rpath.sh, are already present on unmodified master in my environment, so the change introduces no regression.

I am happy to open a pull request for this if you want it.

Additional context

Environment: aarch64-linux, NixOS 26.11, glibc 2.42, Linux 6.18.46. I have only tested aarch64. The missing branch is architecture independent, so I would expect x86_64 to behave the same whenever the linker emits .dynstr after .gnu.version_d.

This is what makes the failure hard to attribute in practice. Downstream, nixpkgs' autoPatchelfHook sets an RPATH on every prebuilt ELF, so any packaged bun --compile application segfaults on startup with no diagnostic. nixpkgs' own bun package escapes it only because it needs no RPATH: it carries rpath: [], its .gnu.version_d is never relocated, and DT_VERDEF stays correct.

I searched for duplicates first. #170 and #192 report the same _dl_check_map_versions symptom but were closed after a different root cause was found in the PT_LOAD/e_phoff handling. #84, #214 and #158 concern .gnu.version_r contents under --replace-needed and --remove-needed, not DT_VERDEF. #583 and #585 are shiftFile assertion failures. I did not find an existing report of the missing DT_VERDEF update.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions