Skip to content

Force rlibs to be linked in whole for cdylib and bin #93791

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
wants to merge 2 commits into from
Closed
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
10 changes: 6 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
sess: &'a Session,
codegen_results: &CodegenResults,
tmpdir: &Path,
crate_type: CrateType,
_crate_type: CrateType,
cnum: CrateNum,
) {
let src = &codegen_results.crate_info.used_crate_source[&cnum];
Expand All @@ -2274,13 +2274,15 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
// whole of each object in our archive into that artifact. This is
// because a `dylib` can be reused as an intermediate artifact.
//
// NOTE(nbdd0121): Even for non-dylib, we still need to link rlibs
// in whole because so that `#[no_mangle]` and `#[used]` items from
// upstream need to be root (#47384, #50007).
//
// Note, though, that we don't want to include the whole of a
// compiler-builtins crate (e.g., compiler-rt) because it'll get
// repeatedly linked anyway.
let path = fix_windows_verbatim_for_gcc(path);
if crate_type == CrateType::Dylib
&& codegen_results.crate_info.compiler_builtins != Some(cnum)
{
if codegen_results.crate_info.compiler_builtins != Some(cnum) {
cmd.link_whole_rlib(&path);
} else {
cmd.link_rlib(&path);
Expand Down
12 changes: 12 additions & 0 deletions src/test/run-make/issue-47384/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-include ../../run-make-fulldeps/tools.mk

# ignore-windows
# ignore-cross-compile

all: main.rs
$(RUSTC) --crate-type lib lib.rs
$(RUSTC) --crate-type cdylib -Clink-args="-Tlinker.ld" main.rs
# Ensure `#[used]` and `KEEP`-ed section is there
objdump -s -j".static" $(TMPDIR)/libmain.so
# Ensure `#[no_mangle]` symbol is there
nm $(TMPDIR)/libmain.so | $(CGREP) bar
12 changes: 12 additions & 0 deletions src/test/run-make/issue-47384/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod foo {
#[link_section = ".rodata.STATIC"]
#[used]
static STATIC: [u32; 10] = [1; 10];
}

mod bar {
#[no_mangle]
extern "C" fn bar() -> i32 {
0
}
}
7 changes: 7 additions & 0 deletions src/test/run-make/issue-47384/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SECTIONS
{
.static : ALIGN(4)
{
KEEP(*(.rodata.STATIC));
}
}
1 change: 1 addition & 0 deletions src/test/run-make/issue-47384/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extern crate lib;