Skip to content

Commit

Permalink
Auto merge of #45710 - alexcrichton:std-symbols, r=michaelwoerister
Browse files Browse the repository at this point in the history
rustc: Handle some libstd symbole exports better

Right now symbol exports, particularly in a cdylib, are handled by
assuming that `pub extern` combined with `#[no_mangle]` means "export
this". This isn't actually what we want for some symbols that the
standard library uses to implement itself, for example symbols related
to allocation. Additionally other special symbols like
`rust_eh_personallity` have no need to be exported from cdylib crate
types (only needed in dylib crate types).

This commit updates how rustc handles these special symbols by adding to
the hardcoded logic of symbols like `rust_eh_personallity` but also
adding a new attribute, `#[rustc_std_internal_symbol]`, which forces the
export level to be considered the same as all other Rust functions
instead of looking like a C function.

The eventual goal here is to prevent functions like `__rdl_alloc` from
showing up as part of a Rust cdylib as it's just an internal
implementation detail. This then further allows such symbols to get gc'd
by the linker when creating a cdylib.
  • Loading branch information
bors committed Nov 5, 2017
2 parents 12e6b53 + fbf9869 commit 44183f5
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/liballoc_jemalloc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod contents {
// ABI

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_alloc(size: usize,
align: usize,
err: *mut u8) -> *mut u8 {
Expand All @@ -122,13 +122,13 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_oom(err: *const u8) -> ! {
System.oom((*(err as *const AllocErr)).clone())
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_dealloc(ptr: *mut u8,
size: usize,
align: usize) {
Expand All @@ -137,7 +137,7 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_usable_size(layout: *const u8,
min: *mut usize,
max: *mut usize) {
Expand All @@ -153,7 +153,7 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_realloc(ptr: *mut u8,
_old_size: usize,
old_align: usize,
Expand All @@ -177,7 +177,7 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_alloc_zeroed(size: usize,
align: usize,
err: *mut u8) -> *mut u8 {
Expand All @@ -196,7 +196,7 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_alloc_excess(size: usize,
align: usize,
excess: *mut usize,
Expand All @@ -210,7 +210,7 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_realloc_excess(ptr: *mut u8,
old_size: usize,
old_align: usize,
Expand All @@ -227,7 +227,7 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_grow_in_place(ptr: *mut u8,
old_size: usize,
old_align: usize,
Expand All @@ -237,7 +237,7 @@ mod contents {
}

#[no_mangle]
#[linkage = "external"]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_shrink_in_place(ptr: *mut u8,
_old_size: usize,
old_align: usize,
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_allocator/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,13 @@ impl<'a> AllocFnFactory<'a> {

let no_mangle = Symbol::intern("no_mangle");
let no_mangle = self.cx.meta_word(self.span, no_mangle);

let special = Symbol::intern("rustc_std_internal_symbol");
let special = self.cx.meta_word(self.span, special);
vec![
self.cx.attribute(self.span, linkage),
self.cx.attribute(self.span, no_mangle),
self.cx.attribute(self.span, special),
]
}

Expand Down
11 changes: 10 additions & 1 deletion src/librustc_trans/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rustc::ty::TyCtxt;
use rustc::ty::maps::Providers;
use rustc::util::nodemap::FxHashMap;
use rustc_allocator::ALLOCATOR_METHODS;
use syntax::attr;

pub type ExportedSymbols = FxHashMap<
CrateNum,
Expand Down Expand Up @@ -180,7 +181,15 @@ pub fn provide_extern(providers: &mut Providers) {
}

fn export_level(tcx: TyCtxt, sym_def_id: DefId) -> SymbolExportLevel {
if tcx.contains_extern_indicator(sym_def_id) {
// We export anything that's not mangled at the "C" layer as it probably has
// to do with ABI concerns. We do not, however, apply such treatment to
// special symbols in the standard library for various plumbing between
// core/std/allocators/etc. For example symbols used to hook up allocation
// are not considered for export
let is_extern = tcx.contains_extern_indicator(sym_def_id);
let std_internal = attr::contains_name(&tcx.get_attrs(sym_def_id),
"rustc_std_internal_symbol");
if is_extern && !std_internal {
SymbolExportLevel::C
} else {
SymbolExportLevel::Rust
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use alloc_system::System;

#[cfg(not(test))]
#[doc(hidden)]
#[allow(unused_attributes)]
pub mod __default_lib_allocator {
use super::{System, Layout, Alloc, AllocErr};
use ptr;
Expand All @@ -28,6 +29,7 @@ pub mod __default_lib_allocator {
// ABI

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_alloc(size: usize,
align: usize,
err: *mut u8) -> *mut u8 {
Expand All @@ -42,18 +44,21 @@ pub mod __default_lib_allocator {
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_oom(err: *const u8) -> ! {
System.oom((*(err as *const AllocErr)).clone())
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_dealloc(ptr: *mut u8,
size: usize,
align: usize) {
System.dealloc(ptr, Layout::from_size_align_unchecked(size, align))
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_usable_size(layout: *const u8,
min: *mut usize,
max: *mut usize) {
Expand All @@ -63,6 +68,7 @@ pub mod __default_lib_allocator {
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_realloc(ptr: *mut u8,
old_size: usize,
old_align: usize,
Expand All @@ -81,6 +87,7 @@ pub mod __default_lib_allocator {
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_alloc_zeroed(size: usize,
align: usize,
err: *mut u8) -> *mut u8 {
Expand All @@ -95,6 +102,7 @@ pub mod __default_lib_allocator {
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_alloc_excess(size: usize,
align: usize,
excess: *mut usize,
Expand All @@ -113,6 +121,7 @@ pub mod __default_lib_allocator {
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_realloc_excess(ptr: *mut u8,
old_size: usize,
old_align: usize,
Expand All @@ -135,6 +144,7 @@ pub mod __default_lib_allocator {
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_grow_in_place(ptr: *mut u8,
old_size: usize,
old_align: usize,
Expand All @@ -149,6 +159,7 @@ pub mod __default_lib_allocator {
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_shrink_in_place(ptr: *mut u8,
old_size: usize,
old_align: usize,
Expand Down
6 changes: 6 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,12 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG
"allow_fail attribute is currently unstable",
cfg_fn!(allow_fail))),

("rustc_std_internal_symbol", Whitelisted, Gated(Stability::Unstable,
"rustc_attrs",
"this is an internal attribute that will \
never be stable",
cfg_fn!(rustc_attrs))),

// Crate level attributes
("crate_name", CrateLevel, Ungated),
("crate_type", CrateLevel, Ungated),
Expand Down
17 changes: 17 additions & 0 deletions src/test/run-make/cdylib-fewer-symbols/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Test that allocator-related symbols don't show up as exported from a cdylib as
# they're internal to Rust and not part of the public ABI.

-include ../tools.mk

ifdef IS_MSVC
all:
true
else
all:
$(RUSTC) foo.rs
nm -g "$(call DYLIB,foo)"
nm -g "$(call DYLIB,foo)" | grep -vq __rdl_
nm -g "$(call DYLIB,foo)" | grep -vq __rde_
nm -g "$(call DYLIB,foo)" | grep -vq __rg_
nm -g "$(call DYLIB,foo)" | grep -vq __rust_
endif
16 changes: 16 additions & 0 deletions src/test/run-make/cdylib-fewer-symbols/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_type = "cdylib"]

#[no_mangle]
pub extern fn foo() -> u32 {
3
}

0 comments on commit 44183f5

Please sign in to comment.