Skip to content

Commit b8326fc

Browse files
committed
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.
1 parent f60a0f1 commit b8326fc

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

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)