Skip to content

Commit 3564f58

Browse files
committed
[WIP] Use weak aliases for the allocator shim
1 parent 7b3150e commit 3564f58

File tree

12 files changed

+176
-124
lines changed

12 files changed

+176
-124
lines changed

compiler/rustc_codegen_gcc/src/mono_item.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ impl<'gcc, 'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
7676
self.functions.borrow_mut().insert(symbol_name.to_string(), decl);
7777
self.function_instances.borrow_mut().insert(instance, decl);
7878
}
79+
80+
fn weak_alias(&self, _aliasee: Self::Function, _aliasee_name: &str, _name: &str) {}
7981
}

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use libc::c_uint;
22
use rustc_ast::expand::allocator::{
3-
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
4-
alloc_error_handler_name, default_fn_name, global_fn_name,
3+
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
54
};
65
use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
76
use rustc_middle::bug;
@@ -18,7 +17,6 @@ pub(crate) unsafe fn codegen(
1817
tcx: TyCtxt<'_>,
1918
cx: SimpleCx<'_>,
2019
module_name: &str,
21-
kind: AllocatorKind,
2220
alloc_error_handler_kind: AllocatorKind,
2321
) {
2422
let usize = match tcx.sess.target.pointer_width {
@@ -28,38 +26,6 @@ pub(crate) unsafe fn codegen(
2826
tws => bug!("Unsupported target word size for int: {}", tws),
2927
};
3028
let i8 = cx.type_i8();
31-
let i8p = cx.type_ptr();
32-
33-
if kind == AllocatorKind::Default {
34-
for method in ALLOCATOR_METHODS {
35-
let mut args = Vec::with_capacity(method.inputs.len());
36-
for input in method.inputs.iter() {
37-
match input.ty {
38-
AllocatorTy::Layout => {
39-
args.push(usize); // size
40-
args.push(usize); // align
41-
}
42-
AllocatorTy::Ptr => args.push(i8p),
43-
AllocatorTy::Usize => args.push(usize),
44-
45-
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
46-
}
47-
}
48-
let output = match method.output {
49-
AllocatorTy::ResultPtr => Some(i8p),
50-
AllocatorTy::Unit => None,
51-
52-
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
53-
panic!("invalid allocator output")
54-
}
55-
};
56-
57-
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
58-
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
59-
60-
create_wrapper_function(tcx, &cx, &from_name, &to_name, &args, output, false);
61-
}
62-
}
6329

6430
// rust alloc error handler
6531
create_wrapper_function(

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
108108
&self,
109109
tcx: TyCtxt<'tcx>,
110110
module_name: &str,
111-
kind: AllocatorKind,
111+
_kind: AllocatorKind,
112112
alloc_error_handler_kind: AllocatorKind,
113113
) -> ModuleLlvm {
114114
let module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
115115
let cx =
116116
SimpleCx::new(module_llvm.llmod(), &module_llvm.llcx, tcx.data_layout.pointer_size);
117117
unsafe {
118-
allocator::codegen(tcx, cx, module_name, kind, alloc_error_handler_kind);
118+
allocator::codegen(tcx, cx, module_name, alloc_error_handler_kind);
119119
}
120120
module_llvm
121121
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,11 +2671,11 @@ unsafe extern "C" {
26712671
pub(crate) fn LLVMRustSetNoSanitizeAddress(Global: &Value);
26722672
pub(crate) fn LLVMRustSetNoSanitizeHWAddress(Global: &Value);
26732673

2674-
pub(crate) fn LLVMAddAlias2(
2675-
M: &Module,
2674+
pub(crate) fn LLVMAddAlias2<'ll>(
2675+
M: &'ll Module,
26762676
ValueTy: &Type,
26772677
AddressSpace: c_uint,
26782678
Aliasee: &Value,
26792679
Name: *const c_char,
2680-
);
2680+
) -> &'ll Value;
26812681
}

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,14 @@ impl Intrinsic {
351351
}
352352

353353
/// Safe wrapper for `LLVMAddAlias2`
354-
pub(crate) fn add_alias(
355-
module: &Module,
354+
pub(crate) fn add_alias<'ll>(
355+
module: &'ll Module,
356356
ty: &Type,
357357
address_space: AddressSpace,
358358
aliasee: &Value,
359-
name: &[u8],
360-
) {
361-
unsafe {
362-
let data = name.as_c_char_ptr();
363-
LLVMAddAlias2(value, data, address_space, aliasee, name.len());
364-
}
359+
name: &CStr,
360+
) -> &'ll Value {
361+
unsafe { LLVMAddAlias2(module, ty, address_space.0, aliasee, name.as_ptr()) }
365362
}
366363

367364
/// Safe wrapper for `LLVMSetValueName2` from a byte slice

compiler/rustc_codegen_llvm/src/mono_item.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::ffi::CString;
2+
3+
use rustc_abi::AddressSpace;
14
use rustc_codegen_ssa::traits::*;
25
use rustc_hir::def::DefKind;
36
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
@@ -6,6 +9,7 @@ use rustc_middle::mir::mono::{Linkage, Visibility};
69
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv, LayoutOf};
710
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
811
use rustc_session::config::CrateType;
12+
use rustc_symbol_mangling::mangle_internal_symbol;
913
use rustc_target::spec::RelocModel;
1014
use tracing::debug;
1115

@@ -77,8 +81,51 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
7781

7882
self.assume_dso_local(lldecl, false);
7983

84+
let symbol_name = self.tcx.symbol_name(instance);
85+
if symbol_name.name.contains("__rdl_alloc") {
86+
self.weak_alias(
87+
lldecl,
88+
symbol_name.name,
89+
&mangle_internal_symbol(self.tcx, "__rust_alloc"),
90+
);
91+
}
92+
if symbol_name.name.contains("__rdl_dealloc") {
93+
self.weak_alias(
94+
lldecl,
95+
symbol_name.name,
96+
&mangle_internal_symbol(self.tcx, "__rust_dealloc"),
97+
);
98+
}
99+
if symbol_name.name.contains("__rdl_realloc") {
100+
self.weak_alias(
101+
lldecl,
102+
symbol_name.name,
103+
&mangle_internal_symbol(self.tcx, "__rust_realloc"),
104+
);
105+
}
106+
if symbol_name.name.contains("__rdl_alloc_zeroed") {
107+
self.weak_alias(
108+
lldecl,
109+
symbol_name.name,
110+
&mangle_internal_symbol(self.tcx, "__rust_alloc_zeroed"),
111+
);
112+
}
113+
80114
self.instances.borrow_mut().insert(instance, lldecl);
81115
}
116+
117+
fn weak_alias(&self, aliasee: Self::Function, _aliasee_name: &str, name: &str) {
118+
let ty = self.get_type_of_global(aliasee);
119+
let alias = llvm::add_alias(
120+
self.llmod,
121+
ty,
122+
AddressSpace::DATA,
123+
aliasee,
124+
&CString::new(name).unwrap(),
125+
);
126+
127+
llvm::set_linkage(alias, llvm::Linkage::WeakAnyLinkage);
128+
}
82129
}
83130

84131
impl CodegenCx<'_, '_> {

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,9 +1976,10 @@ fn add_linked_symbol_object(
19761976
cmd: &mut dyn Linker,
19771977
sess: &Session,
19781978
tmpdir: &Path,
1979-
symbols: &[(String, SymbolExportKind)],
1979+
linked_symbols: &[(String, SymbolExportKind)],
1980+
exported_symbols: &[(String, SymbolExportKind)],
19801981
) {
1981-
if symbols.is_empty() {
1982+
if linked_symbols.is_empty() {
19821983
return;
19831984
}
19841985

@@ -2015,7 +2016,7 @@ fn add_linked_symbol_object(
20152016
None
20162017
};
20172018

2018-
for (sym, kind) in symbols.iter() {
2019+
for (sym, kind) in linked_symbols.iter() {
20192020
let symbol = file.add_symbol(object::write::Symbol {
20202021
name: sym.clone().into(),
20212022
value: 0,
@@ -2073,6 +2074,23 @@ fn add_linked_symbol_object(
20732074
}
20742075
}
20752076

2077+
if sess.target.is_like_msvc {
2078+
let drectve = exported_symbols
2079+
.into_iter()
2080+
.map(|(sym, kind)| {
2081+
if *kind == SymbolExportKind::Text {
2082+
format!(" /EXPORT:\"{sym}\"")
2083+
} else {
2084+
format!(" /EXPORT:\"{sym}\",DATA")
2085+
}
2086+
})
2087+
.collect::<Vec<_>>()
2088+
.join("");
2089+
2090+
let section = file.add_section(vec![], b".drectve".to_vec(), object::SectionKind::Linker);
2091+
file.append_section_data(section, drectve.as_bytes(), 1);
2092+
}
2093+
20762094
let path = tmpdir.join("symbols.o");
20772095
let result = std::fs::write(&path, file.write().unwrap());
20782096
if let Err(error) = result {
@@ -2248,6 +2266,7 @@ fn linker_with_args(
22482266
sess,
22492267
tmpdir,
22502268
&codegen_results.crate_info.linked_symbols[&crate_type],
2269+
&codegen_results.crate_info.exported_symbols[&crate_type],
22512270
);
22522271

22532272
// Sanitizer libraries.

0 commit comments

Comments
 (0)