Skip to content

detect: Use weakref instead of dlsym - #243

Open
taiki-e wants to merge 1 commit into
mainfrom
weakref
Open

detect: Use weakref instead of dlsym#243
taiki-e wants to merge 1 commit into
mainfrom
weakref

Conversation

@taiki-e

@taiki-e taiki-e commented Aug 14, 2026

Copy link
Copy Markdown
Owner

This is similar to dlsym and unstable #[linkage = "extern_weak"], but async-signal-safe unlike dlsym and works on stable and sound with fat LTO unlike extern_weak.

When statically linked, weakref may resolve to null even if the symbol is actually available (as with dlsym and extern_weak).
However, unlike dlsym it works if there are other strong references to the same symbol, so this PR enables detection at all times for targets where it was previously enabled only with dynamic linking.

Unlike extern_weak, this can be soundly used with "fat LTO + other strong reference + symbol resolved to null" case: rust-lang/rust#154439

With rust-lang/rust#154439 (comment) example on aarch64-unknown-linux-gnu:

(A weak reference is promoted to a strong reference as a strong reference already exists in deps.rs, and since there is no my_extern, it is correct that a link error occurs in this example.)

$ # With #[linkage = "extern_weak"] static my_extern: Option<unsafe extern "C" fn() -> i32>;
$ rustc --crate-name vmm --crate-type lib -C opt-level=3 dep.rs -g; rustc main.rs -C opt-level=3 -C lto --extern vmm=libvmm.rlib -g; ./main
zsh: segmentation fault  ./main

$ # With weakref!(my_extern, Option<unsafe extern "C" fn() -> i32>);
$ rustc --crate-name vmm --crate-type lib -C opt-level=3 dep.rs -g; rustc main.rs -C opt-level=3 -C lto --extern vmm=libvmm.rlib -g; ./main
error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "-Wl,--fix-cortex-a53-843419" "<1 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcompiler_builtins-*.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/mnt/mac/Users/taiki/projects/tmp/repro/rustch92wgI/raw-dylibs" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/aarch64-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: /usr/bin/ld: main.main.624f8795ab9c9f81-cgu.0.rcgu.o:(.data.rel.ro.__portable_atomic__weak_my_extern+0x0): undefined reference to `my_extern'
          /usr/bin/ld: main.main.624f8795ab9c9f81-cgu.0.rcgu.o: in function `main::main':
          /mnt/mac/Users/taiki/projects/tmp/repro/dep.rs:7:(.text._RNvCs8riVLv9cHGn_4main4main+0x20): undefined reference to `my_extern'
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link

$ rustc -vV
rustc 1.99.0-nightly (84b36a78a 2026-08-06)
binary: rustc
commit-hash: 84b36a78a28a63f134171c670be1932ffa2485f8
commit-date: 2026-08-06
host: aarch64-unknown-linux-gnu
release: 1.99.0-nightly
LLVM version: 23.1.0

See comments on weakref! macro added to src/utils.rs for more details.

Refs: https://sourceware.org/binutils/docs/as/Weakref.html

@taiki-e taiki-e added O-arm Target: 32-bit Arm processors (armv6, armv7, thumb...), including 64-bit Arm in AArch32 state O-powerpc Target: PowerPC processors O-aarch64 Target: Armv8-A, Armv8-R, or later processors in AArch64 mode labels Aug 14, 2026
@taiki-e
taiki-e force-pushed the main branch 3 times, most recently from 609936b to 5bc3902 Compare August 14, 2026 19:38
Repository owner deleted a comment from chatgpt-codex-connector Bot Aug 14, 2026
Repository owner deleted a comment from chatgpt-codex-connector Bot Aug 14, 2026
@taiki-e
taiki-e force-pushed the weakref branch 8 times, most recently from cc196f5 to deb39f1 Compare August 15, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-aarch64 Target: Armv8-A, Armv8-R, or later processors in AArch64 mode O-arm Target: 32-bit Arm processors (armv6, armv7, thumb...), including 64-bit Arm in AArch32 state O-powerpc Target: PowerPC processors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant