Skip to content

Commit 0a41331

Browse files
committed
[WIP] Use protected visibility for all rust symbols
TODO: This produces linker errors with ld.bfd, but works with ld.gold and ld.lld
1 parent 77e24f9 commit 0a41331

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

compiler/rustc_codegen_gcc/src/allocator.rs

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
5757
if tcx.sess.target.options.default_hidden_visibility {
5858
#[cfg(feature="master")]
5959
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
60+
} else {
61+
#[cfg(feature="master")]
62+
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Protected));
6063
}
6164
if tcx.sess.must_emit_unwind_tables() {
6265
// TODO(antoyo): emit unwind tables.
@@ -101,6 +104,9 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
101104
if tcx.sess.target.default_hidden_visibility {
102105
#[cfg(feature="master")]
103106
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
107+
} else {
108+
#[cfg(feature="master")]
109+
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Protected));
104110
}
105111

106112
let callee = alloc_error_handler_name(alloc_error_handler_kind);

compiler/rustc_codegen_llvm/src/allocator.rs

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub(crate) unsafe fn codegen(
6666

6767
if tcx.sess.target.default_hidden_visibility {
6868
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
69+
} else {
70+
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Protected);
6971
}
7072
if tcx.sess.must_emit_unwind_tables() {
7173
let uwtable = attributes::uwtable_attr(llcx);
@@ -117,6 +119,8 @@ pub(crate) unsafe fn codegen(
117119

118120
if tcx.sess.target.default_hidden_visibility {
119121
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
122+
} else {
123+
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Protected);
120124
}
121125
if tcx.sess.must_emit_unwind_tables() {
122126
let uwtable = attributes::uwtable_attr(llcx);
@@ -156,6 +160,8 @@ pub(crate) unsafe fn codegen(
156160
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
157161
if tcx.sess.target.default_hidden_visibility {
158162
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
163+
} else {
164+
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Protected);
159165
}
160166
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
161167
let llval = llvm::LLVMConstInt(i8, val as u64, False);
@@ -165,6 +171,8 @@ pub(crate) unsafe fn codegen(
165171
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
166172
if tcx.sess.target.default_hidden_visibility {
167173
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
174+
} else {
175+
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Protected);
168176
}
169177
let llval = llvm::LLVMConstInt(i8, 0, False);
170178
llvm::LLVMSetInitializer(ll_g, llval);

compiler/rustc_monomorphize/src/partitioning.rs

+29-18
Original file line numberDiff line numberDiff line change
@@ -884,26 +884,37 @@ fn mono_item_visibility<'tcx>(
884884
}
885885

886886
fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibility {
887-
if !tcx.sess.target.default_hidden_visibility {
888-
return Visibility::Default;
889-
}
887+
let export_level = if is_generic {
888+
// Generic functions never have export-level C.
889+
SymbolExportLevel::Rust
890+
} else if !id.is_local() {
891+
// Things with export level C don't get instantiated in
892+
// downstream crates.
893+
SymbolExportLevel::Rust
894+
} else {
895+
match tcx.reachable_non_generics(id.krate).get(&id) {
896+
Some(SymbolExportInfo { level: SymbolExportLevel::C, .. }) => SymbolExportLevel::C,
897+
_ => SymbolExportLevel::Rust,
898+
}
899+
};
890900

891-
// Generic functions never have export-level C.
892-
if is_generic {
893-
return Visibility::Hidden;
894-
}
901+
match export_level {
902+
// C-export level items remain at `Default` to allow C code to
903+
// access and interpose them.
904+
SymbolExportLevel::C => Visibility::Default,
895905

896-
// Things with export level C don't get instantiated in
897-
// downstream crates.
898-
if !id.is_local() {
899-
return Visibility::Hidden;
900-
}
901-
902-
// C-export level items remain at `Default`, all other internal
903-
// items become `Hidden`.
904-
match tcx.reachable_non_generics(id.krate).get(&id) {
905-
Some(SymbolExportInfo { level: SymbolExportLevel::C, .. }) => Visibility::Default,
906-
_ => Visibility::Hidden,
906+
// All other items become `Hidden` or `Protected` depending on
907+
// `default_hidden_visibility`.
908+
SymbolExportLevel::Rust => {
909+
if tcx.sess.target.default_hidden_visibility {
910+
Visibility::Hidden
911+
} else {
912+
// Items with mangled symbols don't have a predictable name,
913+
// so no dylib that doesn't depend on compiler internals can
914+
// do interposition on rust items.
915+
Visibility::Protected
916+
}
917+
}
907918
}
908919
}
909920

0 commit comments

Comments
 (0)