Skip to content
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

Use Default visibility for rustc-generated C symbol declarations #123994

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 6 additions & 10 deletions compiler/rustc_codegen_llvm/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,16 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
unnamed: llvm::UnnamedAddr,
fn_type: &'ll Type,
) -> &'ll Value {
// Declare C ABI functions with the visibility used by C by default.
let visibility = if self.tcx.sess.default_hidden_visibility() {
llvm::Visibility::Hidden
} else {
llvm::Visibility::Default
};

declare_raw_fn(self, name, llvm::CCallConv, unnamed, visibility, fn_type)
// Declare C ABI functions with Default visibility to allow them to link
// dynamically with shared object-provided symbols later on. This is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May check tcx.sess.target.dynamic_linking here as well if dynamic linking is the intended use case.
(Ideally all this would be configurable per function somehow.)

// needed to link intrinsic-generated calls to e.g. libc.so symbols like
// memcmp.
declare_raw_fn(self, name, llvm::CCallConv, unnamed, llvm::Visibility::Default, fn_type)
}

/// Declare an entry Function
///
/// The ABI of this function can change depending on the target (although for now the same as
/// `declare_cfn`)
/// The ABI of this function can change depending on the target.
///
/// If there’s a value with the same name already declared, the function will
/// update the declaration and return existing Value instead.
Expand Down
15 changes: 15 additions & 0 deletions tests/codegen/default-hidden-visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ pub static tested_symbol: [u8; 6] = *b"foobar";
// DEFAULT: @{{.*}}default_hidden_visibility{{.*}}tested_symbol{{.*}} = constant
// YES: @{{.*}}default_hidden_visibility{{.*}}tested_symbol{{.*}} = hidden constant
// NO: @{{.*}}default_hidden_visibility{{.*}}tested_symbol{{.*}} = constant

pub fn do_memcmp(left: &[u8], right: &[u8]) -> i32 {
left.cmp(right) as i32
}

// CHECK: define {{.*}} @{{.*}}do_memcmp{{.*}} {
// CHECK: }

// `do_memcmp` should invoke core::intrinsic::compare_bytes which emits a call
// to the C symbol `memcmp` (at least on x86_64-unknown-linux-gnu). This symbol
// should *not* be `declare hidden`.

// DEFAULT: declare i32 @memcmp
// YES: declare i32 @memcmp
// NO: declare i32 @memcmp
8 changes: 8 additions & 0 deletions tests/ui/intrinsics/default-hidden-visibility-intrinsic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ build-pass
//@ compile-flags: -Zdefault-hidden-visibility=yes

#![crate_type = "dylib"]

pub fn do_memcmp(left: &[u8], right: &[u8]) -> i32 {
left.cmp(right) as i32
}
Loading