Skip to content

Emit a warning when gold is used as the linker #141748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Noratrieb opened this issue May 29, 2025 · 2 comments · Fixed by #141750
Closed

Emit a warning when gold is used as the linker #141748

Noratrieb opened this issue May 29, 2025 · 2 comments · Fixed by #141750
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-linkers Area: linkers... you gotta love linkers C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Noratrieb
Copy link
Member

ld.gold is officially deprecated and doesn't seem entirely work properly for Rust: #139425

Once rustc starts emitting SHF_GNU_RETAIN sections for #[used] (#140872), it will get even worse, as gold miscompiles SHF_GNU_RETAIN (it doesn't just ignore it, it breaks the program in other ways).

It may be possible for rustc to detect usage of gold an emit an appropriate warning, telling the user to switch to a different linker like BFD or LLD instead.

@bjorn3 suggested looking at .comment after linking in #139425 (comment). From local testing, it looks like gold only adds this if you pass --enable-linker-version, which cc doesn't seem to pass by default.

But it looks like gold always emits a note, .note.gnu.gold-version/NT_GNU_GOLD_VERSION, which seems like a reliable way to detect gold.

@Noratrieb Noratrieb added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-linkers Area: linkers... you gotta love linkers labels May 29, 2025
@Noratrieb
Copy link
Member Author

Noratrieb commented May 29, 2025

Oh, looks like -fuse-ld=gold is currently completely broken with the LLD default unless you disable it with -Zlinker-features=-lld:

error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "-m64" "/tmp/rustcFAdxzv/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,liballoc-*,librustc_std_workspace_core-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/tmp/rustcFAdxzv/raw-dylibs" "-B<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,-znostart-stop-gc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "a" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-fuse-ld=gold"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: /nix/store/v63bxfiacw082c7ijshf60alvvrpfxsq-binutils-2.44/bin/ld.gold: nostart-stop-gc: unknown -z option
          /nix/store/v63bxfiacw082c7ijshf60alvvrpfxsq-binutils-2.44/bin/ld.gold: use the --help option for usage information
          collect2: error: ld returned 1 exit status

that's really funny.

bors added a commit that referenced this issue May 29, 2025
Warn when gold was used as the linker

gold has been deprecated recently and is known to behave incorrectly around Rust programs, including miscompiling `#[used(linker)]`. Tell people to switch to a different linker instead.

closes #141748

opening mostly for perf for now, but feel free to review

r? bjorn3
@lqd
Copy link
Member

lqd commented May 29, 2025

People really want link args to remain opaque to rustc, and it’s becoming quite hard to handle these combinations of flags, driver versions, etc without understanding the link args or knowing which linker is going to be used. We can’t even really say "let’s not default to lld if there are any link args". We may need to retry linking without it if it causes issues instead. Very disheartening.

rust-bors bot added a commit that referenced this issue May 30, 2025
Warn when gold was used as the linker

gold has been deprecated recently and is known to behave incorrectly around Rust programs, including miscompiling `#[used(linker)]`. Tell people to switch to a different linker instead.

closes #141748

opening mostly for perf for now, but feel free to review

r? bjorn3
@bors bors closed this as completed in 99426c5 Jun 3, 2025
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Jun 3, 2025
Warn when gold was used as the linker

gold has been deprecated recently and is known to behave incorrectly around Rust programs, including miscompiling `#[used(linker)]`. Tell people to switch to a different linker instead.

closes rust-lang/rust#141748

r? bjorn3
jieyouxu pushed a commit to jieyouxu/rustc-dev-guide that referenced this issue Jun 3, 2025
Warn when gold was used as the linker

gold has been deprecated recently and is known to behave incorrectly around Rust programs, including miscompiling `#[used(linker)]`. Tell people to switch to a different linker instead.

closes rust-lang/rust#141748

r? bjorn3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-linkers Area: linkers... you gotta love linkers C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants