1
Fork 0

Use safe wrappers get_visibility and set_visibility

This commit is contained in:
Zalathar 2024-10-19 12:17:33 +11:00
parent 983d258be3
commit b114040afb
5 changed files with 20 additions and 33 deletions

View file

@ -77,20 +77,14 @@ pub(crate) unsafe fn codegen(
// __rust_alloc_error_handler_should_panic // __rust_alloc_error_handler_should_panic
let name = OomStrategy::SYMBOL; let name = OomStrategy::SYMBOL;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8); let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
llvm::LLVMRustSetVisibility( llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
ll_g,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
let val = tcx.sess.opts.unstable_opts.oom.should_panic(); let val = tcx.sess.opts.unstable_opts.oom.should_panic();
let llval = llvm::LLVMConstInt(i8, val as u64, False); let llval = llvm::LLVMConstInt(i8, val as u64, False);
llvm::LLVMSetInitializer(ll_g, llval); llvm::LLVMSetInitializer(ll_g, llval);
let name = NO_ALLOC_SHIM_IS_UNSTABLE; let name = NO_ALLOC_SHIM_IS_UNSTABLE;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8); let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
llvm::LLVMRustSetVisibility( llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
ll_g,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
let llval = llvm::LLVMConstInt(i8, 0, False); let llval = llvm::LLVMConstInt(i8, 0, False);
llvm::LLVMSetInitializer(ll_g, llval); llvm::LLVMSetInitializer(ll_g, llval);
} }
@ -134,10 +128,7 @@ fn create_wrapper_function(
None None
}; };
llvm::LLVMRustSetVisibility( llvm::set_visibility(llfn, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
llfn,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
if tcx.sess.must_emit_unwind_tables() { if tcx.sess.must_emit_unwind_tables() {
let uwtable = let uwtable =
@ -151,7 +142,7 @@ fn create_wrapper_function(
// -> ! DIFlagNoReturn // -> ! DIFlagNoReturn
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]); attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
} }
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden); llvm::set_visibility(callee, llvm::Visibility::Hidden);
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr()); let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr());

View file

@ -135,7 +135,7 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t
|| !cx.tcx.is_reachable_non_generic(instance_def_id)) || !cx.tcx.is_reachable_non_generic(instance_def_id))
}; };
if is_hidden { if is_hidden {
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); llvm::set_visibility(llfn, llvm::Visibility::Hidden);
} }
// MinGW: For backward compatibility we rely on the linker to decide whether it // MinGW: For backward compatibility we rely on the linker to decide whether it

View file

@ -288,9 +288,7 @@ impl<'ll> CodegenCx<'ll, '_> {
let g = self.declare_global(sym, llty); let g = self.declare_global(sym, llty);
if !self.tcx.is_reachable_non_generic(def_id) { if !self.tcx.is_reachable_non_generic(def_id) {
unsafe { llvm::set_visibility(g, llvm::Visibility::Hidden);
llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
}
} }
g g
@ -308,7 +306,7 @@ impl<'ll> CodegenCx<'ll, '_> {
llvm::set_thread_local_mode(g, self.tls_model); llvm::set_thread_local_mode(g, self.tls_model);
} }
let dso_local = unsafe { self.should_assume_dso_local(g, true) }; let dso_local = self.should_assume_dso_local(g, true);
if dso_local { if dso_local {
unsafe { unsafe {
llvm::LLVMRustSetDSOLocal(g, true); llvm::LLVMRustSetDSOLocal(g, true);
@ -398,7 +396,7 @@ impl<'ll> CodegenCx<'ll, '_> {
llvm::set_value_name(g, b""); llvm::set_value_name(g, b"");
let linkage = llvm::get_linkage(g); let linkage = llvm::get_linkage(g);
let visibility = llvm::LLVMRustGetVisibility(g); let visibility = llvm::get_visibility(g);
let new_g = llvm::LLVMRustGetOrInsertGlobal( let new_g = llvm::LLVMRustGetOrInsertGlobal(
self.llmod, self.llmod,
@ -408,7 +406,7 @@ impl<'ll> CodegenCx<'ll, '_> {
); );
llvm::set_linkage(new_g, linkage); llvm::set_linkage(new_g, linkage);
llvm::LLVMRustSetVisibility(new_g, visibility); llvm::set_visibility(new_g, visibility);
// The old global has had its name removed but is returned by // The old global has had its name removed but is returned by
// get_static since it is in the instance cache. Provide an // get_static since it is in the instance cache. Provide an

View file

@ -242,6 +242,10 @@ pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
} }
} }
pub fn get_visibility(llglobal: &Value) -> Visibility {
unsafe { LLVMRustGetVisibility(llglobal) }
}
pub fn set_visibility(llglobal: &Value, visibility: Visibility) { pub fn set_visibility(llglobal: &Value, visibility: Visibility) {
unsafe { unsafe {
LLVMRustSetVisibility(llglobal, visibility); LLVMRustSetVisibility(llglobal, visibility);

View file

@ -40,8 +40,8 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
}); });
llvm::set_linkage(g, base::linkage_to_llvm(linkage)); llvm::set_linkage(g, base::linkage_to_llvm(linkage));
llvm::set_visibility(g, base::visibility_to_llvm(visibility));
unsafe { unsafe {
llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
if self.should_assume_dso_local(g, false) { if self.should_assume_dso_local(g, false) {
llvm::LLVMRustSetDSOLocal(g, true); llvm::LLVMRustSetDSOLocal(g, true);
} }
@ -78,21 +78,15 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
&& linkage != Linkage::Private && linkage != Linkage::Private
&& self.tcx.is_compiler_builtins(LOCAL_CRATE) && self.tcx.is_compiler_builtins(LOCAL_CRATE)
{ {
unsafe { llvm::set_visibility(lldecl, llvm::Visibility::Hidden);
llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
}
} else { } else {
unsafe { llvm::set_visibility(lldecl, base::visibility_to_llvm(visibility));
llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
}
} }
debug!("predefine_fn: instance = {:?}", instance); debug!("predefine_fn: instance = {:?}", instance);
unsafe { if self.should_assume_dso_local(lldecl, false) {
if self.should_assume_dso_local(lldecl, false) { unsafe { llvm::LLVMRustSetDSOLocal(lldecl, true) };
llvm::LLVMRustSetDSOLocal(lldecl, true);
}
} }
self.instances.borrow_mut().insert(instance, lldecl); self.instances.borrow_mut().insert(instance, lldecl);
@ -102,13 +96,13 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
impl CodegenCx<'_, '_> { impl CodegenCx<'_, '_> {
/// Whether a definition or declaration can be assumed to be local to a group of /// Whether a definition or declaration can be assumed to be local to a group of
/// libraries that form a single DSO or executable. /// libraries that form a single DSO or executable.
pub(crate) unsafe fn should_assume_dso_local( pub(crate) fn should_assume_dso_local(
&self, &self,
llval: &llvm::Value, llval: &llvm::Value,
is_declaration: bool, is_declaration: bool,
) -> bool { ) -> bool {
let linkage = llvm::get_linkage(llval); let linkage = llvm::get_linkage(llval);
let visibility = unsafe { llvm::LLVMRustGetVisibility(llval) }; let visibility = llvm::get_visibility(llval);
if matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) { if matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) {
return true; return true;