Skip to content

Commit

Permalink
Auto merge of rust-lang#102424 - sunfishcode:sunfishcode/hidden-main,…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
bors committed Oct 2, 2022
2 parents ab37a83 + 72f1557 commit 47b2eee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion compiler/rustc_codegen_llvm/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn declare_raw_fn<'ll>(
name: &str,
callconv: llvm::CallConv,
unnamed: llvm::UnnamedAddr,
visibility: llvm::Visibility,
ty: &'ll Type,
) -> &'ll Value {
debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
Expand All @@ -41,6 +42,7 @@ fn declare_raw_fn<'ll>(

llvm::SetFunctionCallConv(llfn, callconv);
llvm::SetUnnamedAddress(llfn, unnamed);
llvm::set_visibility(llfn, visibility);

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

Expand Down Expand Up @@ -78,7 +80,14 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
unnamed: llvm::UnnamedAddr,
fn_type: &'ll Type,
) -> &'ll Value {
declare_raw_fn(self, name, llvm::CCallConv, unnamed, fn_type)
// Declare C ABI functions with the visibility used by C by default.
let visibility = if self.tcx.sess.target.default_hidden_visibility {
llvm::Visibility::Hidden
} else {
llvm::Visibility::Default
};

declare_raw_fn(self, name, llvm::CCallConv, unnamed, visibility, fn_type)
}

/// Declare a Rust function.
Expand All @@ -95,6 +104,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
name,
fn_abi.llvm_cconv(),
llvm::UnnamedAddr::Global,
llvm::Visibility::Default,
fn_abi.llvm_type(self),
);
fn_abi.apply_attrs_llfn(self, llfn);
Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/abi-main-signature-32bit-c-int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
fn main() {
}

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

0 comments on commit 47b2eee

Please sign in to comment.