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

Generate bindings for private extern functions by default #822

Closed
mversic opened this issue Mar 16, 2023 · 2 comments
Closed

Generate bindings for private extern functions by default #822

mversic opened this issue Mar 16, 2023 · 2 comments

Comments

@mversic
Copy link
Contributor

mversic commented Mar 16, 2023

Rust compiler will always export private extern functions. We can say that visibility rules don't apply for extern functions. Considering this, generating C bindings for private extern functions should be opt-out. In the meantime, is there some way to opt-in generating bindings for private functions?

@mversic
Copy link
Contributor Author

mversic commented Mar 16, 2023

to make a more convincing case I'm providing steps to reproduce:

  1. I made a project as follows:
[package]
name = "example"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ['cdylib']

[profile.release]
lto = true           # NOTE: I've even enabled LTO 
#[no_mangle]        // NOTE: If `no_mangle` is removed the function will not be exported
extern fn i_am_exported() {}
  1. I built the library with cargo build --release which produced target/release/libexample.dylib
  2. I inspected the produced library with objdump -t target/release/libexample.dylib which shows:

target/release/libexample.dylib:	file format mach-o arm64

SYMBOL TABLE:
0000000000000000      d  *UND* 
0000000000000000      d  *UND* /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/alloc/src/lib.rs/@/
0000000000000000      d  *UND* alloc.515a7714-cgu.0
0000000000000000      d  *UND* /Users/mversic/soramitsu/example/target/release/deps/example.example.ae59a36c-cgu.0.rcgu.o
0000000000003fb4      d  *UND* 
0000000000003fb4      d  *UND* _i_am_exported
0000000000000004      d  *UND* 
0000000000003fb4      d  *UND* 
0000000000000000      d  *UND* 
0000000000003fb4 g     F __TEXT,__text _i_am_exported

I hope this makes a convincing enough case that current default behavior of cbindgen is not aligned with rusts default behavior

NOTES:
  1. you can see in the symbol table that _i_am_exported is gF -> a global function
  2. It is interesting to note that no_mangle affects the final result. If this attribute is not present, the function will never be exported regardless if it's public or private.

@mversic
Copy link
Contributor Author

mversic commented Aug 25, 2023

merged

@mversic mversic closed this as completed Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant