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

-Zexport-executable-symbols alone does not export symbols #101610

Open
yshui opened this issue Sep 9, 2022 · 7 comments
Open

-Zexport-executable-symbols alone does not export symbols #101610

yshui opened this issue Sep 9, 2022 · 7 comments
Labels
C-bug Category: This is a bug. F-export_executable_symbols `#![feature(export_executable_symbols)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@yshui
Copy link
Contributor

yshui commented Sep 9, 2022

I tried:

#[no_mangle]
pub fn exported() {}

building with:

rustc -Zexport-executable-symbols --crate-type bin

I expected to see this happen:

`exported` is in the list of dynamic symbols

Instead, this happened:

no symbol is exported

in addition, this test case here:

nm $(TMPDIR)/main | $(CGREP) exported_symbol

is insufficient, as nm prints out every symbol, not only the exported ones.

Meta

Tested on:

  • rustc 1.65.0-nightly (c07a8b4e0 2022-08-26)
  • rustc 1.65.0-nightly (c2804e6ec 2022-09-07)
@yshui yshui added the C-bug Category: This is a bug. label Sep 9, 2022
@yshui
Copy link
Contributor Author

yshui commented Sep 9, 2022

the test should have at least used nm -T, but i am not an expert on these tools.

@yshui
Copy link
Contributor Author

yshui commented Sep 9, 2022

seems like an additional -C link-arg=-rdynamic is needed.

@Enselic Enselic added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-export_executable_symbols `#![feature(export_executable_symbols)]` and removed needs-triage-legacy labels Jul 13, 2024
@GoldsteinE
Copy link
Contributor

An easy solution would be to just emit -rdynamic when export-executable-symbols is specified. I think this is a bit too aggressive though: it makes all the symbols exported, not only ones Rust considers used (so marked with #[no_mangle] and potentially #[export]).

A more precise solution would be to use --dynamic-list with a precise list of symbols rustc considers “exported”. I’m not yet sure whether it’s feasible to generate such a list.

@bjorn3
Copy link
Member

bjorn3 commented Jul 16, 2024

We do already generate a version script for dynamic libraries to restrict the set of exported symbols. We just omit it for executables.

@GoldsteinE
Copy link
Contributor

GoldsteinE commented Jul 16, 2024

Thanks for the clarification.

Looking at the relevant code, it looks like version script does get generated for executables if -Zexport-executable-symbols is specified:

if crate_type == CrateType::Executable {
let should_export_executable_symbols =
self.sess.opts.unstable_opts.export_executable_symbols;
if self.sess.target.override_export_symbols.is_none()
&& !should_export_executable_symbols
{
return;
}
}

I’ll look into this code some more. I’m not sure version script is enough to get a symbol dynamically exported, I think --dynamic-list or --export-dynamic is also required.

@bjorn3
Copy link
Member

bjorn3 commented Jul 16, 2024

Have you verified that the input object files export the symbols in the first place? I wouldn't be surprised if the mono collector skips them or internalizes them.

@GoldsteinE
Copy link
Contributor

-rdynamic fixes the issue for me, so it looks like the problem is with the linker invocation.

Calling

rustc --emit=obj -Zexport-executable-symbols --crate-type=bin ttt.rs

gives me an object file with a global exported symbol:

$ objdump --syms ttt.o | rg exported
0000000000000000 l    d  .text.exported	0000000000000000 .text.exported
0000000000000000 g     F .text.exported	0000000000000001 exported

similar to a C function with external linkage:

$ cat t.c
void exported(void) {}
$ gcc -c t.c
$ objdump --syms t.o | rg exported
0000000000000000 g     F .text	0000000000000007 exported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-export_executable_symbols `#![feature(export_executable_symbols)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants