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
let name = OomStrategy::SYMBOL;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
llvm::LLVMRustSetVisibility(
ll_g,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
let llval = llvm::LLVMConstInt(i8, val as u64, False);
llvm::LLVMSetInitializer(ll_g, llval);
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
llvm::LLVMRustSetVisibility(
ll_g,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
let llval = llvm::LLVMConstInt(i8, 0, False);
llvm::LLVMSetInitializer(ll_g, llval);
}
@ -134,10 +128,7 @@ fn create_wrapper_function(
None
};
llvm::LLVMRustSetVisibility(
llfn,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
llvm::set_visibility(llfn, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
if tcx.sess.must_emit_unwind_tables() {
let uwtable =
@ -151,7 +142,7 @@ fn create_wrapper_function(
// -> ! DIFlagNoReturn
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());

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))
};
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

View File

@ -288,9 +288,7 @@ fn get_static_inner(&self, def_id: DefId, llty: &'ll Type) -> &'ll Value {
let g = self.declare_global(sym, llty);
if !self.tcx.is_reachable_non_generic(def_id) {
unsafe {
llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
}
llvm::set_visibility(g, llvm::Visibility::Hidden);
}
g
@ -308,7 +306,7 @@ fn get_static_inner(&self, def_id: DefId, llty: &'ll Type) -> &'ll Value {
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 {
unsafe {
llvm::LLVMRustSetDSOLocal(g, true);
@ -398,7 +396,7 @@ fn codegen_static_item(&self, def_id: DefId) {
llvm::set_value_name(g, b"");
let linkage = llvm::get_linkage(g);
let visibility = llvm::LLVMRustGetVisibility(g);
let visibility = llvm::get_visibility(g);
let new_g = llvm::LLVMRustGetOrInsertGlobal(
self.llmod,
@ -408,7 +406,7 @@ fn codegen_static_item(&self, def_id: DefId) {
);
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
// 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) {
unsafe {
LLVMRustSetVisibility(llglobal, visibility);

View File

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