Skip to content

trans: Make names of internal symbols independent of CGU translation order #37328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2016
Merged
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
4 changes: 1 addition & 3 deletions src/librustc_trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> Va
s.as_ptr() as *const c_char,
s.len() as c_uint,
!null_terminated as Bool);

let gsym = token::gensym("str");
let sym = format!("str{}", gsym.0);
let sym = cx.generate_local_symbol_name("str");
let g = declare::define_global(cx, &sym[..], val_ty(sc)).unwrap_or_else(||{
bug!("symbol `{}` is already defined", sym);
});
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use rustc::hir;
use std::ffi::{CStr, CString};
use syntax::ast;
use syntax::attr;
use syntax::parse::token;

pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
unsafe {
Expand All @@ -44,10 +43,7 @@ pub fn addr_of_mut(ccx: &CrateContext,
kind: &str)
-> ValueRef {
unsafe {
// FIXME: this totally needs a better name generation scheme, perhaps a simple global
// counter? Also most other uses of gensym in trans.
let gsym = token::gensym("_");
let name = format!("{}{}", kind, gsym.0);
let name = ccx.generate_local_symbol_name(kind);
let gv = declare::define_global(ccx, &name[..], val_ty(cv)).unwrap_or_else(||{
bug!("symbol `{}` is already defined", name);
});
Expand Down
14 changes: 14 additions & 0 deletions src/librustc_trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub struct LocalCrateContext<'tcx> {
type_of_depth: Cell<usize>,

symbol_map: Rc<SymbolMap<'tcx>>,

/// A counter that is used for generating local symbol names
local_gen_sym_counter: Cell<usize>,
}

// Implement DepTrackingMapConfig for `trait_cache`
Expand Down Expand Up @@ -688,6 +691,7 @@ impl<'tcx> LocalCrateContext<'tcx> {
n_llvm_insns: Cell::new(0),
type_of_depth: Cell::new(0),
symbol_map: symbol_map,
local_gen_sym_counter: Cell::new(0),
};

let (int_type, opaque_vec_type, str_slice_ty, mut local_ccx) = {
Expand Down Expand Up @@ -1021,6 +1025,16 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
pub fn empty_substs_for_def_id(&self, item_def_id: DefId) -> &'tcx Substs<'tcx> {
self.shared().empty_substs_for_def_id(item_def_id)
}

/// Generate a new symbol name with the given prefix. This symbol name must
/// only be used for definitions with `internal` or `private` linkage.
pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
let idx = self.local().local_gen_sym_counter.get();
self.local().local_gen_sym_counter.set(idx + 1);
// Include a '.' character, so there can be no accidental conflicts with
// user defined names
format!("{}.{}", prefix, idx)
}
}

pub struct TypeOfDepthLock<'a, 'tcx: 'a>(&'a LocalCrateContext<'tcx>);
Expand Down
6 changes: 3 additions & 3 deletions src/test/codegen/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
// CHECK: @STATIC = {{.*}}, align 4

// This checks the constants from inline_enum_const
// CHECK: @ref{{[0-9]+}} = {{.*}}, align 2
// CHECK: @ref.{{[0-9]+}} = {{.*}}, align 2

// This checks the constants from {low,high}_align_const, they share the same
// constant, but the alignment differs, so the higher one should be used
// CHECK: [[LOW_HIGH:@ref[0-9]+]] = {{.*}}, align 4
// CHECK: [[LOW_HIGH_REF:@const[0-9]+]] = {{.*}} [[LOW_HIGH]]
// CHECK: [[LOW_HIGH:@ref.[0-9]+]] = {{.*}}, align 4
// CHECK: [[LOW_HIGH_REF:@const.[0-9]+]] = {{.*}} [[LOW_HIGH]]

#[derive(Copy, Clone)]

Expand Down
8 changes: 4 additions & 4 deletions src/test/run-make/symbols-are-reasonable/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-include ../tools.mk

# check that the compile generated symbols for strings, binaries,
# vtables, etc. have semisane names (e.g. `str1234`); it's relatively
# vtables, etc. have semisane names (e.g. `str.1234`); it's relatively
# easy to accidentally modify the compiler internals to make them
# become things like `str"str"(1234)`.

Expand All @@ -10,6 +10,6 @@ OUT=$(TMPDIR)/lib.s
all:
$(RUSTC) lib.rs --emit=asm --crate-type=staticlib
# just check for symbol declarations with the names we're expecting.
grep 'str[0-9][0-9]*:' $(OUT)
grep 'byte_str[0-9][0-9]*:' $(OUT)
grep 'vtable[0-9][0-9]*' $(OUT)
grep 'str.[0-9][0-9]*:' $(OUT)
grep 'byte_str.[0-9][0-9]*:' $(OUT)
grep 'vtable.[0-9][0-9]*' $(OUT)