Skip to content

Commit 30857ae

Browse files
authored
Auto merge of #37714 - alexcrichton:builtins-hidden, r=nikomatsakis
rustc: Flag all builtins functions as hidden When compiling compiler-rt you typically compile with `-fvisibility=hidden` which to ensure that all symbols are hidden in shared objects and don't show up in symbol tables. This is important for these intrinsics being linked in every crate to ensure that we're not unnecessarily bloating the public ABI of Rust crates. This should help allow the compiler-builtins project with Rust-defined builtins start landing in-tree as well.
2 parents c8867f8 + 88b4646 commit 30857ae

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/libcompiler_builtins/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "0.0.0"
77
[lib]
88
name = "compiler_builtins"
99
path = "lib.rs"
10+
test = false
1011

1112
[dependencies]
1213
core = { path = "../libcore" }

src/librustc_llvm/ffi.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ pub type OperandBundleDefRef = *mut OperandBundleDef_opaque;
426426
pub type DiagnosticHandler = unsafe extern "C" fn(DiagnosticInfoRef, *mut c_void);
427427
pub type InlineAsmDiagHandler = unsafe extern "C" fn(SMDiagnosticRef, *const c_void, c_uint);
428428

429+
/// LLVMVisibility
430+
#[repr(C)]
431+
pub enum Visibility {
432+
Default,
433+
Hidden,
434+
Protected,
435+
}
436+
429437
pub mod debuginfo {
430438
pub use self::DIDescriptorFlags::*;
431439
use super::MetadataRef;
@@ -746,8 +754,8 @@ extern "C" {
746754
pub fn LLVMRustSetLinkage(Global: ValueRef, RustLinkage: Linkage);
747755
pub fn LLVMGetSection(Global: ValueRef) -> *const c_char;
748756
pub fn LLVMSetSection(Global: ValueRef, Section: *const c_char);
749-
pub fn LLVMGetVisibility(Global: ValueRef) -> c_uint;
750-
pub fn LLVMSetVisibility(Global: ValueRef, Viz: c_uint);
757+
pub fn LLVMGetVisibility(Global: ValueRef) -> Visibility;
758+
pub fn LLVMSetVisibility(Global: ValueRef, Viz: Visibility);
751759
pub fn LLVMGetAlignment(Global: ValueRef) -> c_uint;
752760
pub fn LLVMSetAlignment(Global: ValueRef, Bytes: c_uint);
753761
pub fn LLVMSetDLLStorageClass(V: ValueRef, C: DLLStorageClass);

src/librustc_trans/declare.rs

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! interested in defining the ValueRef they return.
2020
//! * Use define_* family of methods when you might be defining the ValueRef.
2121
//! * When in doubt, define.
22+
2223
use llvm::{self, ValueRef};
2324
use llvm::AttributePlace::Function;
2425
use rustc::ty;
@@ -28,6 +29,7 @@ use context::CrateContext;
2829
use common;
2930
use type_::Type;
3031
use value::Value;
32+
use syntax::attr;
3133

3234
use std::ffi::CString;
3335

@@ -70,6 +72,16 @@ fn declare_raw_fn(ccx: &CrateContext, name: &str, callconv: llvm::CallConv, ty:
7072
llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
7173
}
7274

75+
// If we're compiling the compiler-builtins crate, e.g. the equivalent of
76+
// compiler-rt, then we want to implicitly compile everything with hidden
77+
// visibility as we're going to link this object all over the place but
78+
// don't want the symbols to get exported.
79+
if attr::contains_name(ccx.tcx().map.krate_attrs(), "compiler_builtins") {
80+
unsafe {
81+
llvm::LLVMSetVisibility(llfn, llvm::Visibility::Hidden);
82+
}
83+
}
84+
7385
match ccx.tcx().sess.opts.cg.opt_level.as_ref().map(String::as_ref) {
7486
Some("s") => {
7587
llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);

0 commit comments

Comments
 (0)