Skip to content
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

Fix linker failures when #[global_allocator] is used in a dependency #112794

Merged
merged 1 commit into from
Jun 20, 2023
Merged
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
18 changes: 16 additions & 2 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::mir::place::PlaceRef;
use crate::traits::*;
use crate::{CachedModuleCodegen, CompiledModule, CrateInfo, MemFlags, ModuleCodegen, ModuleKind};

use rustc_ast::expand::allocator::AllocatorKind;
use rustc_ast::expand::allocator::{global_fn_name, AllocatorKind, ALLOCATOR_METHODS};
use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
Expand Down Expand Up @@ -921,7 +921,21 @@ impl CrateInfo {
missing_weak_lang_items
.iter()
.map(|item| (format!("{prefix}{item}"), SymbolExportKind::Text)),
)
);
if tcx.allocator_kind(()).is_some() {
// At least one crate needs a global allocator. This crate may be placed
// after the crate that defines it in the linker order, in which case some
// linkers return an error. By adding the global allocator shim methods to
// the linked_symbols list, linking the generated symbols.o will ensure that
// circular dependencies involving the global allocator don't lead to linker
// errors.
linked_symbols.extend(ALLOCATOR_METHODS.iter().map(|method| {
(
format!("{prefix}{}", global_fn_name(method.name).as_str()),
SymbolExportKind::Text,
)
}));
}
});
}

Expand Down
7 changes: 7 additions & 0 deletions tests/run-make/allocator-shim-circular-deps/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ignore-cross-compile
include ../tools.mk

all:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) my_lib.rs
$(RUSTC) main.rs --test --extern my_lib=$(TMPDIR)/libmy_lib.rlib
5 changes: 5 additions & 0 deletions tests/run-make/allocator-shim-circular-deps/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![crate_type = "bin"]

fn main() {
my_lib::do_something();
}
10 changes: 10 additions & 0 deletions tests/run-make/allocator-shim-circular-deps/my_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![crate_type = "lib"]

use std::alloc::System;

#[global_allocator]
static ALLOCATOR: System = System;

pub fn do_something() {
format!("allocating a string!");
}