From fbf98697021173a30b84d9145df0966a23a2f9d2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 1 Nov 2017 13:16:36 -0700 Subject: [PATCH] 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. --- src/liballoc_jemalloc/lib.rs | 20 +++++++++---------- src/librustc_allocator/expand.rs | 4 ++++ src/librustc_trans/back/symbol_export.rs | 11 +++++++++- src/libstd/heap.rs | 11 ++++++++++ src/libsyntax/feature_gate.rs | 6 ++++++ .../run-make/cdylib-fewer-symbols/Makefile | 17 ++++++++++++++++ src/test/run-make/cdylib-fewer-symbols/foo.rs | 16 +++++++++++++++ 7 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 src/test/run-make/cdylib-fewer-symbols/Makefile create mode 100644 src/test/run-make/cdylib-fewer-symbols/foo.rs diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index bc2ed1f9c456..f060f6d79c17 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -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 { @@ -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) { @@ -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) { @@ -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, @@ -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 { @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/src/librustc_allocator/expand.rs b/src/librustc_allocator/expand.rs index eafb4c5c8007..352184c1efa7 100644 --- a/src/librustc_allocator/expand.rs +++ b/src/librustc_allocator/expand.rs @@ -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), ] } diff --git a/src/librustc_trans/back/symbol_export.rs b/src/librustc_trans/back/symbol_export.rs index eb1c5cb78815..b9519bba2dd8 100644 --- a/src/librustc_trans/back/symbol_export.rs +++ b/src/librustc_trans/back/symbol_export.rs @@ -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, @@ -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 diff --git a/src/libstd/heap.rs b/src/libstd/heap.rs index d76ab31862bf..4d5e4df6f95b 100644 --- a/src/libstd/heap.rs +++ b/src/libstd/heap.rs @@ -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; @@ -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 { @@ -42,11 +44,13 @@ 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) { @@ -54,6 +58,7 @@ pub mod __default_lib_allocator { } #[no_mangle] + #[rustc_std_internal_symbol] pub unsafe extern fn __rdl_usable_size(layout: *const u8, min: *mut usize, max: *mut usize) { @@ -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, @@ -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 { @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 6339600059e2..8b564605c1ef 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -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), diff --git a/src/test/run-make/cdylib-fewer-symbols/Makefile b/src/test/run-make/cdylib-fewer-symbols/Makefile new file mode 100644 index 000000000000..954ee792460a --- /dev/null +++ b/src/test/run-make/cdylib-fewer-symbols/Makefile @@ -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 diff --git a/src/test/run-make/cdylib-fewer-symbols/foo.rs b/src/test/run-make/cdylib-fewer-symbols/foo.rs new file mode 100644 index 000000000000..4ec8d4ee8607 --- /dev/null +++ b/src/test/run-make/cdylib-fewer-symbols/foo.rs @@ -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 or the MIT license +// , 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 +}