-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Only compile #[used] as llvm.compiler.used for ELF targets #93718
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
Changes from all commits
54133cf
16c2b39
da72295
a64b2a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2850,7 +2850,37 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs { | |
) | ||
.emit(); | ||
} | ||
None => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED, | ||
None => { | ||
// Unfortunately, unconditionally using `llvm.used` causes | ||
// issues in handling `.init_array` with the gold linker, | ||
// but using `llvm.compiler.used` caused a nontrival amount | ||
// of unintentional ecosystem breakage -- particularly on | ||
// Mach-O targets. | ||
// | ||
// As a result, we emit `llvm.compiler.used` only on ELF | ||
// targets. This is somewhat ad-hoc, but actually follows | ||
// our pre-LLVM 13 behavior (prior to the ecosystem | ||
// breakage), and seems to match `clang`'s behavior as well | ||
// (both before and after LLVM 13), possibly because they | ||
// have similar compatibility concerns to us. See | ||
// https://github.com/rust-lang/rust/issues/47384#issuecomment-1019080146 | ||
// and following comments for some discussion of this, as | ||
// well as the comments in `rustc_codegen_llvm` where these | ||
// flags are handled. | ||
// | ||
// Anyway, to be clear: this is still up in the air | ||
// somewhat, and is subject to change in the future (which | ||
// is a good thing, because this would ideally be a bit | ||
// more firmed up). | ||
let is_like_elf = !(tcx.sess.target.is_like_osx | ||
|| tcx.sess.target.is_like_windows | ||
|| tcx.sess.target.is_like_wasm); | ||
codegen_fn_attrs.flags = if is_like_elf { | ||
CodegenFnAttrFlags::USED | ||
} else { | ||
CodegenFnAttrFlags::USED_LINKER | ||
Comment on lines
+2879
to
+2881
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm understanding all this correctly, it sounds like If the above is true, then I personally would prefer But having said that, I don't want to delay this PR even longer than it already has been delayed based on a cleanup task like that. Its just a passing comment on how I found this code a little confusing as written. |
||
}; | ||
} | ||
} | ||
} else if attr.has_name(sym::cmse_nonsecure_entry) { | ||
if !matches!(tcx.fn_sig(did).abi(), abi::Abi::C { .. }) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
-include ../tools.mk | ||
|
||
# only-macos | ||
# | ||
# This checks that `#[used]` passes through to the linker on | ||
# darwin. This is subject to change in the future, see | ||
# https://github.com/rust-lang/rust/pull/93718 for discussion | ||
|
||
all: | ||
$(RUSTC) -Copt-level=3 dylib_used.rs | ||
nm $(TMPDIR)/libdylib_used.dylib | $(CGREP) VERY_IMPORTANT_SYMBOL |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![crate_type = "cdylib"] | ||
|
||
#[used] | ||
static VERY_IMPORTANT_SYMBOL: u32 = 12345; |
Uh oh!
There was an error while loading. Please reload this page.