Skip to content

Commit 88b4646

Browse files
committed
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.
1 parent 4da129d commit 88b4646

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;
@@ -27,6 +28,7 @@ use attributes;
2728
use context::CrateContext;
2829
use type_::Type;
2930
use value::Value;
31+
use syntax::attr;
3032

3133
use std::ffi::CString;
3234

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

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

0 commit comments

Comments
 (0)