Skip to content

Commit c5495e9

Browse files
authored
Unrolled build for #151998
Rollup merge of #151998 - zmodem:naked_visibility.squash, r=saethlin,bjorn3 Set hidden visibility on naked functions in compiler-builtins 88b4646 made builtin functions hidden, but it doesn't apply to naked functions, which are generated through a different code path. This was discovered in #151486 where aarch64 outline atomics showed up in shared objects, overriding the symbols from compiler-rt.
2 parents 29fa07e + baca864 commit c5495e9

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

compiler/rustc_codegen_llvm/src/mono_item.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
4141
});
4242

4343
llvm::set_linkage(g, base::linkage_to_llvm(linkage));
44-
llvm::set_visibility(g, base::visibility_to_llvm(visibility));
44+
self.set_visibility(g, linkage, visibility);
45+
4546
self.assume_dso_local(g, false);
4647

4748
let attrs = self.tcx.codegen_instance_attrs(instance.def);
@@ -69,16 +70,7 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
6970
{
7071
llvm::SetUniqueComdat(self.llmod, lldecl);
7172
}
72-
73-
// If we're compiling the compiler-builtins crate, e.g., the equivalent of
74-
// compiler-rt, then we want to implicitly compile everything with hidden
75-
// visibility as we're going to link this object all over the place but
76-
// don't want the symbols to get exported.
77-
if linkage != Linkage::Internal && self.tcx.is_compiler_builtins(LOCAL_CRATE) {
78-
llvm::set_visibility(lldecl, llvm::Visibility::Hidden);
79-
} else {
80-
llvm::set_visibility(lldecl, base::visibility_to_llvm(visibility));
81-
}
73+
self.set_visibility(lldecl, linkage, visibility);
8274

8375
debug!("predefine_fn: instance = {:?}", instance);
8476

@@ -122,6 +114,18 @@ impl CodegenCx<'_, '_> {
122114
assume
123115
}
124116

117+
fn set_visibility(&self, lldecl: &llvm::Value, linkage: Linkage, visibility: Visibility) {
118+
// If we're compiling the compiler-builtins crate, i.e., the equivalent of
119+
// compiler-rt, then we want to implicitly compile everything with hidden
120+
// visibility as we're going to link this object all over the place but
121+
// don't want the symbols to get exported.
122+
if linkage != Linkage::Internal && self.tcx.is_compiler_builtins(LOCAL_CRATE) {
123+
llvm::set_visibility(lldecl, llvm::Visibility::Hidden);
124+
} else {
125+
llvm::set_visibility(lldecl, base::visibility_to_llvm(visibility));
126+
}
127+
}
128+
125129
fn should_assume_dso_local(&self, llval: &llvm::Value, is_declaration: bool) -> bool {
126130
let linkage = llvm::get_linkage(llval);
127131
let visibility = llvm::get_visibility(llval);

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_abi::{BackendRepr, Float, Integer, Primitive, RegKind};
22
use rustc_hir::attrs::{InstructionSetAttr, Linkage};
3+
use rustc_hir::def_id::LOCAL_CRATE;
34
use rustc_middle::mir::mono::{MonoItemData, Visibility};
45
use rustc_middle::mir::{InlineAsmOperand, START_BLOCK};
56
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
@@ -128,6 +129,15 @@ fn prefix_and_suffix<'tcx>(
128129
let is_arm = tcx.sess.target.arch == Arch::Arm;
129130
let is_thumb = tcx.sess.unstable_target_features.contains(&sym::thumb_mode);
130131

132+
// If we're compiling the compiler-builtins crate, e.g., the equivalent of
133+
// compiler-rt, then we want to implicitly compile everything with hidden
134+
// visibility as we're going to link this object all over the place but
135+
// don't want the symbols to get exported. For naked asm we set the visibility here.
136+
let mut visibility = item_data.visibility;
137+
if item_data.linkage != Linkage::Internal && tcx.is_compiler_builtins(LOCAL_CRATE) {
138+
visibility = Visibility::Hidden;
139+
}
140+
131141
let attrs = tcx.codegen_instance_attrs(instance.def);
132142
let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());
133143

@@ -217,7 +227,7 @@ fn prefix_and_suffix<'tcx>(
217227
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
218228
writeln!(begin, ".balign {align_bytes}").unwrap();
219229
write_linkage(&mut begin).unwrap();
220-
match item_data.visibility {
230+
match visibility {
221231
Visibility::Default => {}
222232
Visibility::Protected => writeln!(begin, ".protected {asm_name}").unwrap(),
223233
Visibility::Hidden => writeln!(begin, ".hidden {asm_name}").unwrap(),
@@ -243,7 +253,7 @@ fn prefix_and_suffix<'tcx>(
243253
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
244254
writeln!(begin, ".balign {align_bytes}").unwrap();
245255
write_linkage(&mut begin).unwrap();
246-
match item_data.visibility {
256+
match visibility {
247257
Visibility::Default | Visibility::Protected => {}
248258
Visibility::Hidden => writeln!(begin, ".private_extern {asm_name}").unwrap(),
249259
}
@@ -280,7 +290,7 @@ fn prefix_and_suffix<'tcx>(
280290
writeln!(begin, ".section {section},\"\",@").unwrap();
281291
// wasm functions cannot be aligned, so skip
282292
write_linkage(&mut begin).unwrap();
283-
if let Visibility::Hidden = item_data.visibility {
293+
if let Visibility::Hidden = visibility {
284294
writeln!(begin, ".hidden {asm_name}").unwrap();
285295
}
286296
writeln!(begin, ".type {asm_name}, @function").unwrap();
@@ -313,7 +323,7 @@ fn prefix_and_suffix<'tcx>(
313323
writeln!(begin, ".align {}", align_bytes).unwrap();
314324

315325
write_linkage(&mut begin).unwrap();
316-
if let Visibility::Hidden = item_data.visibility {
326+
if let Visibility::Hidden = visibility {
317327
// FIXME apparently `.globl {asm_name}, hidden` is valid
318328
// but due to limitations with `.weak` (see above) we can't really use that in general yet
319329
}

0 commit comments

Comments
 (0)