Skip to content
Open
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
26 changes: 15 additions & 11 deletions compiler/rustc_codegen_llvm/src/mono_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
});

llvm::set_linkage(g, base::linkage_to_llvm(linkage));
llvm::set_visibility(g, base::visibility_to_llvm(visibility));
self.set_visibility(g, linkage, visibility);

self.assume_dso_local(g, false);

let attrs = self.tcx.codegen_instance_attrs(instance.def);
Expand Down Expand Up @@ -69,16 +70,7 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
{
llvm::SetUniqueComdat(self.llmod, lldecl);
}

// If we're compiling the compiler-builtins crate, e.g., the equivalent of
// compiler-rt, then we want to implicitly compile everything with hidden
// visibility as we're going to link this object all over the place but
// don't want the symbols to get exported.
if linkage != Linkage::Internal && self.tcx.is_compiler_builtins(LOCAL_CRATE) {
llvm::set_visibility(lldecl, llvm::Visibility::Hidden);
} else {
llvm::set_visibility(lldecl, base::visibility_to_llvm(visibility));
}
self.set_visibility(lldecl, linkage, visibility);

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

Expand Down Expand Up @@ -122,6 +114,18 @@ impl CodegenCx<'_, '_> {
assume
}

fn set_visibility(&self, lldecl: &llvm::Value, linkage: Linkage, visibility: Visibility) {
// If we're compiling the compiler-builtins crate, i.e., the equivalent of
// compiler-rt, then we want to implicitly compile everything with hidden
// visibility as we're going to link this object all over the place but
// don't want the symbols to get exported.
if linkage != Linkage::Internal && self.tcx.is_compiler_builtins(LOCAL_CRATE) {
llvm::set_visibility(lldecl, llvm::Visibility::Hidden);
} else {
llvm::set_visibility(lldecl, base::visibility_to_llvm(visibility));
}
}

fn should_assume_dso_local(&self, llval: &llvm::Value, is_declaration: bool) -> bool {
let linkage = llvm::get_linkage(llval);
let visibility = llvm::get_visibility(llval);
Expand Down
18 changes: 14 additions & 4 deletions compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_abi::{BackendRepr, Float, Integer, Primitive, RegKind};
use rustc_hir::attrs::{InstructionSetAttr, Linkage};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::mir::mono::{MonoItemData, Visibility};
use rustc_middle::mir::{InlineAsmOperand, START_BLOCK};
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
Expand Down Expand Up @@ -128,6 +129,15 @@ fn prefix_and_suffix<'tcx>(
let is_arm = tcx.sess.target.arch == Arch::Arm;
let is_thumb = tcx.sess.unstable_target_features.contains(&sym::thumb_mode);

// If we're compiling the compiler-builtins crate, e.g., the equivalent of
// compiler-rt, then we want to implicitly compile everything with hidden
// visibility as we're going to link this object all over the place but
// don't want the symbols to get exported. For naked asm we set the visibility here.
let mut visibility = item_data.visibility;
if item_data.linkage != Linkage::Internal && tcx.is_compiler_builtins(LOCAL_CRATE) {
visibility = Visibility::Hidden;
}

let attrs = tcx.codegen_instance_attrs(instance.def);
let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());

Expand Down Expand Up @@ -217,7 +227,7 @@ fn prefix_and_suffix<'tcx>(
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
writeln!(begin, ".balign {align_bytes}").unwrap();
write_linkage(&mut begin).unwrap();
match item_data.visibility {
match visibility {
Visibility::Default => {}
Visibility::Protected => writeln!(begin, ".protected {asm_name}").unwrap(),
Visibility::Hidden => writeln!(begin, ".hidden {asm_name}").unwrap(),
Expand All @@ -243,7 +253,7 @@ fn prefix_and_suffix<'tcx>(
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
writeln!(begin, ".balign {align_bytes}").unwrap();
write_linkage(&mut begin).unwrap();
match item_data.visibility {
match visibility {
Visibility::Default | Visibility::Protected => {}
Visibility::Hidden => writeln!(begin, ".private_extern {asm_name}").unwrap(),
}
Expand Down Expand Up @@ -280,7 +290,7 @@ fn prefix_and_suffix<'tcx>(
writeln!(begin, ".section {section},\"\",@").unwrap();
// wasm functions cannot be aligned, so skip
write_linkage(&mut begin).unwrap();
if let Visibility::Hidden = item_data.visibility {
if let Visibility::Hidden = visibility {
writeln!(begin, ".hidden {asm_name}").unwrap();
}
writeln!(begin, ".type {asm_name}, @function").unwrap();
Expand Down Expand Up @@ -313,7 +323,7 @@ fn prefix_and_suffix<'tcx>(
writeln!(begin, ".align {}", align_bytes).unwrap();

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