Skip to content

Commit 02190f3

Browse files
Make globals with private linkage unnamed. Fixes #50862.
1 parent e94df4a commit 02190f3

File tree

12 files changed

+42
-51
lines changed

12 files changed

+42
-51
lines changed

src/librustc_codegen_llvm/consts.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,22 @@ pub fn addr_of_mut(
6666
cx: &CodegenCx<'ll, '_>,
6767
cv: &'ll Value,
6868
align: Align,
69-
kind: &str,
69+
kind: Option<&str>,
7070
) -> &'ll Value {
7171
unsafe {
72-
let name = cx.generate_local_symbol_name(kind);
73-
let gv = declare::define_global(cx, &name[..], val_ty(cv)).unwrap_or_else(||{
74-
bug!("symbol `{}` is already defined", name);
75-
});
72+
let gv = match kind {
73+
Some(kind) if !cx.tcx.sess.fewer_names() => {
74+
let name = cx.generate_local_symbol_name(kind);
75+
let gv = declare::define_global(cx, &name[..], val_ty(cv)).unwrap_or_else(||{
76+
bug!("symbol `{}` is already defined", name);
77+
});
78+
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
79+
gv
80+
},
81+
_ => declare::define_private_global(cx, val_ty(cv)),
82+
};
7683
llvm::LLVMSetInitializer(gv, cv);
7784
set_global_alignment(cx, gv, align);
78-
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
7985
SetUnnamedAddr(gv, true);
8086
gv
8187
}
@@ -85,7 +91,7 @@ pub fn addr_of(
8591
cx: &CodegenCx<'ll, '_>,
8692
cv: &'ll Value,
8793
align: Align,
88-
kind: &str,
94+
kind: Option<&str>,
8995
) -> &'ll Value {
9096
if let Some(&gv) = cx.const_globals.borrow().get(&cv) {
9197
unsafe {

src/librustc_codegen_llvm/declare.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use value::Value;
3535

3636
use std::ffi::CString;
3737

38-
3938
/// Declare a global value.
4039
///
4140
/// If there’s a value with the same name already declared, the function will
@@ -170,6 +169,15 @@ pub fn define_global(cx: &CodegenCx<'ll, '_>, name: &str, ty: &'ll Type) -> Opti
170169
}
171170
}
172171

172+
/// Declare a private global
173+
///
174+
/// Use this function when you intend to define a global without a name.
175+
pub fn define_private_global(cx: &CodegenCx<'ll, '_>, ty: &'ll Type) -> &'ll Value {
176+
unsafe {
177+
llvm::LLVMRustInsertPrivateGlobal(cx.llmod, ty)
178+
}
179+
}
180+
173181
/// Declare a Rust function with an intention to define it.
174182
///
175183
/// Use this function when you intend to define a function. This function will

src/librustc_codegen_llvm/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ extern "C" {
622622
pub fn LLVMAddGlobal(M: &'a Module, Ty: &'a Type, Name: *const c_char) -> &'a Value;
623623
pub fn LLVMGetNamedGlobal(M: &Module, Name: *const c_char) -> Option<&Value>;
624624
pub fn LLVMRustGetOrInsertGlobal(M: &'a Module, Name: *const c_char, T: &'a Type) -> &'a Value;
625+
pub fn LLVMRustInsertPrivateGlobal(M: &'a Module, T: &'a Type) -> &'a Value;
625626
pub fn LLVMGetFirstGlobal(M: &Module) -> Option<&Value>;
626627
pub fn LLVMGetNextGlobal(GlobalVar: &Value) -> Option<&Value>;
627628
pub fn LLVMDeleteGlobal(GlobalVar: &Value);

src/librustc_codegen_llvm/meth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn get_vtable(
106106

107107
let vtable_const = C_struct(cx, &components, false);
108108
let align = cx.data_layout().pointer_align;
109-
let vtable = consts::addr_of(cx, vtable_const, align, "vtable");
109+
let vtable = consts::addr_of(cx, vtable_const, align, Some("vtable"));
110110

111111
debuginfo::create_vtable_metadata(cx, ty, vtable);
112112

src/librustc_codegen_llvm/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
377377
let file_line_col = consts::addr_of(bx.cx,
378378
file_line_col,
379379
align,
380-
"panic_bounds_check_loc");
380+
Some("panic_bounds_check_loc"));
381381
(lang_items::PanicBoundsCheckFnLangItem,
382382
vec![file_line_col, index, len])
383383
}
@@ -391,7 +391,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
391391
let msg_file_line_col = consts::addr_of(bx.cx,
392392
msg_file_line_col,
393393
align,
394-
"panic_loc");
394+
Some("panic_loc"));
395395
(lang_items::PanicFnLangItem,
396396
vec![msg_file_line_col])
397397
}

src/librustc_codegen_llvm/mir/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ pub fn scalar_to_llvm(
5656
Some(AllocType::Memory(alloc)) => {
5757
let init = const_alloc_to_llvm(cx, alloc);
5858
if alloc.runtime_mutability == Mutability::Mutable {
59-
consts::addr_of_mut(cx, init, alloc.align, "byte_str")
59+
consts::addr_of_mut(cx, init, alloc.align, None)
6060
} else {
61-
consts::addr_of(cx, init, alloc.align, "byte_str")
61+
consts::addr_of(cx, init, alloc.align, None)
6262
}
6363
}
6464
Some(AllocType::Function(fn_instance)) => {

src/librustc_codegen_llvm/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl PlaceRef<'ll, 'tcx> {
6363
offset: Size,
6464
) -> PlaceRef<'ll, 'tcx> {
6565
let init = const_alloc_to_llvm(bx.cx, alloc);
66-
let base_addr = consts::addr_of(bx.cx, init, layout.align, "byte_str");
66+
let base_addr = consts::addr_of(bx.cx, init, layout.align, None);
6767

6868
let llval = unsafe { LLVMConstInBoundsGEP(
6969
consts::bitcast(base_addr, Type::i8p(bx.cx)),

src/rustllvm/RustWrapper.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/IR/DebugInfoMetadata.h"
1313
#include "llvm/IR/DiagnosticInfo.h"
1414
#include "llvm/IR/DiagnosticPrinter.h"
15+
#include "llvm/IR/GlobalVariable.h"
1516
#include "llvm/IR/Instructions.h"
1617
#include "llvm/Object/Archive.h"
1718
#include "llvm/Object/ObjectFile.h"
@@ -116,6 +117,15 @@ LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
116117
return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
117118
}
118119

120+
extern "C" LLVMValueRef
121+
LLVMRustInsertPrivateGlobal(LLVMModuleRef M, LLVMTypeRef Ty) {
122+
return wrap(new GlobalVariable(*unwrap(M),
123+
unwrap(Ty),
124+
false,
125+
GlobalValue::PrivateLinkage,
126+
nullptr));
127+
}
128+
119129
extern "C" LLVMTypeRef LLVMRustMetadataTypeInContext(LLVMContextRef C) {
120130
return wrap(Type::getMetadataTy(*unwrap(C)));
121131
}

src/test/codegen/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
// CHECK: @STATIC = {{.*}}, align 4
2222

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

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

3030
#[derive(Copy, Clone)]
3131

src/test/codegen/remap_path_prefix/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod aux_mod;
2222
include!("aux_mod.rs");
2323

2424
// Here we check that the expansion of the file!() macro is mapped.
25-
// CHECK: @byte_str.1 = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }>, align 1
25+
// CHECK: @0 = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }>, align 1
2626
pub static FILE_PATH: &'static str = file!();
2727

2828
fn main() {

src/test/run-make-fulldeps/symbols-are-reasonable/Makefile

-13
This file was deleted.

src/test/run-make-fulldeps/symbols-are-reasonable/lib.rs

-21
This file was deleted.

0 commit comments

Comments
 (0)