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

--crate-type=bin causes no_mangle symbols to be ommitted. #123946

Closed
lolbinarycat opened this issue Apr 14, 2024 · 5 comments
Closed

--crate-type=bin causes no_mangle symbols to be ommitted. #123946

lolbinarycat opened this issue Apr 14, 2024 · 5 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lolbinarycat
Copy link
Contributor

I tried this code:

fn main() {}

#[no_mangle]
extern "C" fn somefunction() {}

I expected to see this happen: the outputted ELF binary should contain a symbol named "somefunction".

Instead, this happened: rustc --crate-type=bin rustc-bug.rs && objdump -t rustc-bug | grep somefunction returned zero results.

Meta

rustc --version --verbose:

rustc 1.76.0 (07dca489a 2024-02-04) (built from a source tarball)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6
@lolbinarycat lolbinarycat added the C-bug Category: This is a bug. label Apr 14, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 14, 2024
@lolbinarycat
Copy link
Contributor Author

here's the documentation that makes me believe this behavior is unintentional: https://doc.rust-lang.org/reference/abi.html

i ran into this problem when disassembling a program, but it could also cause issues for some dynamic libraries that expect the program that links against them to define some symbol for configuration.

workaround is to make sure that function is referenced from somewhere else in the program.

@eggyal
Copy link
Contributor

eggyal commented Apr 14, 2024

The function is unused/dead code, so it's not being codegen'd (this has nothing to do with no_mangle: it'd be the same for any function). Perhaps you want it to be pub in a library crate?

@workingjubilee
Copy link
Member

@lolbinarycat A close examination of the page you reference suggests its definition of "object file" excludes linked binaries, so this does seem to be "working as intended" currently, though the reference is confusingly written on this point.

@workingjubilee workingjubilee added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Apr 15, 2024
@bjorn3
Copy link
Member

bjorn3 commented Apr 15, 2024

You need the unstable -Zexport-executable-symbols to export symbols from an executable.

@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 15, 2024
@Noratrieb Noratrieb added the A-linkage Area: linking into static, shared libraries and binaries label Apr 16, 2024
@saethlin
Copy link
Member

I don't think there is a bug here; the current behavior is intentional; we have a flag to get the behavior that OP wants. I'd be quite surprised if we decide the default behavior is to export such symbols.

@saethlin saethlin closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. 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

8 participants