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

Conversation

chbaker0
Copy link
Contributor

Previously, visibility for these symbols was determined by the default-hidden-visibility target option or the presence of -Zdefault-hidden-visibility. This leads to issue #123427, where use of the flag leads to undefined hidden symbols (i.e., references that can never be resolved to an exported symbol from another shared library) for functions often provided by a platform shared library, such as memcpy and memcmp from libc.so.

References to symbols provided by shared libraries must have default visibility. Hidden visibility is mostly useful for defined symbols.

@rustbot
Copy link
Collaborator

rustbot commented Apr 16, 2024

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 16, 2024
@chbaker0
Copy link
Contributor Author

tagging @alexcrichton because their wasm additions introduced the original default-hidden-visibility field. I'm still unsure of all the nuances of how it should work for wasm.

@alexcrichton
Copy link
Member

At the time the wasm target was added I vaguely recall that this was basically required to get the target working. For example it was just a quirk that was wasm-specific and wasn't intended to affect any other target. I don't recall the exact reason why it was necessary, but I vaguely recall it being required as otherwise all symbols were always exported from all wasm binaries. I just tried a local compilation with RUSTFLAGS=-Zdefault-hidden-visibility=no for wasm and it only exported the symbols I expected to be exported. Given that it should be ok to just flat out remove this option if it's causing problems.

@chbaker0
Copy link
Contributor Author

We do actually want the command line flag for another use case: controlling symbol export when the final link step is not performed by rustc. Related: rust-lang/compiler-team#656 and #73295

If you build a staticlib or rlib without this option, then link it with C code into a shared object, all Rust symbols are visible in the SO. This is an issue if Rust is an internal implementation detail of the SO, and especially if you have more than one SO linked with Rust code.

@alexcrichton
Copy link
Member

Ah ok makes sense! I can clarify then that at least for wasm-related stuff it's ok to disgregard this, I'll send a parallel PR to disable this option for wasm instead of enable it by default.

@lcnr
Copy link
Contributor

lcnr commented Apr 23, 2024

r? compiler

@rustbot rustbot assigned petrochenkov and unassigned lcnr Apr 23, 2024

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.)

@petrochenkov
Copy link
Contributor

This looks like a reasonable default for foreign functions.

The change have very little impact on unsuspecting users now because none of the built-in targets use default_hidden_visibility now (wasm was migrated off it), and -Zdefault-hidden-visibility is unstable and opt-in.

r=me after considering #123994 (comment).
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants