Skip to content

Commit 47b2eee

Browse files
committed
Auto merge of rust-lang#102424 - sunfishcode:sunfishcode/hidden-main, r=nagisa
Declare `main` as visibility hidden on targets that default to hidden. On targets with `default_hidden_visibility` set, which is currrently just WebAssembly, declare the generated `main` function with visibility hidden. This makes it consistent with clang's WebAssembly target, where `main` is just a user function that gets the same visibility as any other user function, which is hidden on WebAssembly unless explicitly overridden. This will help simplify use cases which in the future may want to automatically wasm-export all visibility-"default" symbols. `main` isn't intended to be wasm-exported, and marking it hidden prevents it from being wasm-exported in that scenario.
2 parents ab37a83 + 72f1557 commit 47b2eee

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Diff for: compiler/rustc_codegen_llvm/src/declare.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn declare_raw_fn<'ll>(
3232
name: &str,
3333
callconv: llvm::CallConv,
3434
unnamed: llvm::UnnamedAddr,
35+
visibility: llvm::Visibility,
3536
ty: &'ll Type,
3637
) -> &'ll Value {
3738
debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
@@ -41,6 +42,7 @@ fn declare_raw_fn<'ll>(
4142

4243
llvm::SetFunctionCallConv(llfn, callconv);
4344
llvm::SetUnnamedAddress(llfn, unnamed);
45+
llvm::set_visibility(llfn, visibility);
4446

4547
let mut attrs = SmallVec::<[_; 4]>::new();
4648

@@ -78,7 +80,14 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
7880
unnamed: llvm::UnnamedAddr,
7981
fn_type: &'ll Type,
8082
) -> &'ll Value {
81-
declare_raw_fn(self, name, llvm::CCallConv, unnamed, fn_type)
83+
// Declare C ABI functions with the visibility used by C by default.
84+
let visibility = if self.tcx.sess.target.default_hidden_visibility {
85+
llvm::Visibility::Hidden
86+
} else {
87+
llvm::Visibility::Default
88+
};
89+
90+
declare_raw_fn(self, name, llvm::CCallConv, unnamed, visibility, fn_type)
8291
}
8392

8493
/// Declare a Rust function.
@@ -95,6 +104,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
95104
name,
96105
fn_abi.llvm_cconv(),
97106
llvm::UnnamedAddr::Global,
107+
llvm::Visibility::Default,
98108
fn_abi.llvm_type(self),
99109
);
100110
fn_abi.apply_attrs_llfn(self, llfn);

Diff for: src/test/codegen/abi-main-signature-32bit-c-int.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
fn main() {
88
}
99

10-
// CHECK: define i32 @main(i32{{( %0)?}}, {{i8\*\*|ptr}}{{( %1)?}})
10+
// CHECK: define{{( hidden)?}} i32 @main(i32{{( %0)?}}, {{i8\*\*|ptr}}{{( %1)?}})

0 commit comments

Comments
 (0)