Skip to content

set subsections_via_symbols for ld64 helper sections #139752

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ fn add_linked_symbol_object(
return;
}

let Some(mut file) = super::metadata::create_object_file(sess) else {
let Some(mut file) = super::metadata::create_object_file(sess, true) else {
return;
};

Expand Down
14 changes: 11 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a
}
}

pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
pub(crate) fn create_object_file(
sess: &Session,
set_subsections_via_symbols: bool,
) -> Option<write::Object<'static>> {
let endianness = match sess.target.options.endian {
Endian::Little => Endianness::Little,
Endian::Big => Endianness::Big,
Expand All @@ -221,6 +224,11 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static

file.set_macho_build_version(macho_object_build_version_for_target(sess))
}
if binary_format == BinaryFormat::MachO {
if set_subsections_via_symbols {
file.set_subsections_via_symbols();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs a documentation comment IMO explaining why we do this.

}
}
if binary_format == BinaryFormat::Coff {
// Disable the default mangler to avoid mangling the special "@feat.00" symbol name.
let original_mangling = file.mangling();
Expand Down Expand Up @@ -455,7 +463,7 @@ pub(crate) fn create_wrapper_file(
section_name: String,
data: &[u8],
) -> (Vec<u8>, MetadataPosition) {
let Some(mut file) = create_object_file(sess) else {
let Some(mut file) = create_object_file(sess, false) else {
if sess.target.is_like_wasm {
return (
create_metadata_file_for_wasm(sess, data, &section_name),
Expand Down Expand Up @@ -543,7 +551,7 @@ pub fn create_compressed_metadata_file(
packed_metadata.write_all(&(metadata.stub_or_full().len() as u64).to_le_bytes()).unwrap();
packed_metadata.extend(metadata.stub_or_full());

let Some(mut file) = create_object_file(sess) else {
let Some(mut file) = create_object_file(sess, false) else {
if sess.target.is_like_wasm {
return create_metadata_file_for_wasm(sess, &packed_metadata, ".rustc");
}
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/linking/cdylib-no-mangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ only-apple
//@ build-fail
//@ dont-check-compiler-stderr
//@ dont-check-compiler-stdout

// Regression test for <https://github.com/rust-lang/rust/issues/139744>.
// Functions in the dynamic library marked with no_mangle should not be GC-ed.

#![crate_type = "cdylib"]

unsafe extern "C" {
unsafe static THIS_SYMBOL_SHOULD_BE_UNDEFINED: usize;
}

#[unsafe(no_mangle)]
pub unsafe fn function_marked_with_no_mangle() {
println!("FUNCTION_MARKED_WITH_NO_MANGLE = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
}

//~? ERROR linking
25 changes: 25 additions & 0 deletions tests/ui/linking/executable-no-mangle-strip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ build-pass

// Regression test for <https://github.com/rust-lang/rust/issues/139744>.
// Functions in the binary marked with no_mangle should be GC-ed if they
// are not indirectly referenced by main.

#![feature(used_with_arg)]

unsafe extern "C" {
unsafe static THIS_SYMBOL_SHOULD_BE_UNDEFINED: usize;
}

#[unsafe(no_mangle)]
pub unsafe fn function_marked_with_no_mangle() {
println!("FUNCTION_MARKED_WITH_NO_MANGLE = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I know that #[no_mangle] implies it, but could we also add an explicit test for #[used]?

Copy link
Contributor Author

@usamoi usamoi Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[used] emits errors, while #[no_mangle] does not, on MacOS.

Don't know why.

Edit: using pr + lld, nightly + lld, stable + lld, stable + ld64, #[used] emits errors, too.

Copy link
Contributor Author

@usamoi usamoi Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know why now.

On MacOS, #[used] emits llvm.used. So #[used(compiler)] is needed here. Is it expected behavior?

Copy link
Member

@bjorn3 bjorn3 Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL that #[used] is an alias for #[used(linker)] on some platforms:

// Unfortunately, unconditionally using `llvm.used` causes
// issues in handling `.init_array` with the gold linker,
// but using `llvm.compiler.used` caused a nontrivial 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_darwin
|| 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
};
Eventually #[used] should be changed to #[used(linker)] unconditionally anyway. Gold is deprecated upstream and broken with current rustc versions anyway: #139425


#[used(compiler)]
pub static FUNCTION_MARKED_WITH_USED: unsafe fn() = || {
println!("FUNCTION_MARKED_WITH_USED = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
};

fn main() {
println!("MAIN");
}
Loading