Visibility attributes on extern global variables are dropped from generated bindings
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
Research direction
Start with the top-level variable path in bindgen's code generation and compare it with the function parsing path that checks cursor.visibility(). Reproduce the hidden and protected cases from the reported bindgen invocation, then verify that generated bindings no longer emit an ordinary default-visible extern static when the C declaration has non-default visibility.
Written by the indexing model from the issue text.
Description
Input C/C++ Header
#if defined(PHASE8_PROTECTED)
#define TEST_VISIBILITY __attribute__((visibility("protected")))
#else
#define TEST_VISIBILITY __attribute__((visibility("hidden")))
#endif
extern int phase8_visibility_global TEST_VISIBILITY;
Bindgen Invocation
$ bindgen input.h \
--output bindings.rs \
--no-layout-tests \
--no-doc-comments \
--no-rustfmt-bindings \
--rust-target 1.75 \
-- -x c -std=gnu11
I ran the protected case again with -DPHASE8_PROTECTED=1.
Actual Results
Bindgen emits the same Rust declaration for both inputs:
extern "C" {
pub static mut phase8_visibility_global: ::std::os::raw::c_int;
}
The C and Rust objects have different ELF visibility on the undefined symbol:
input C object Rust object
hidden GLOBAL HIDDEN UND GLOBAL DEFAULT UND
protected GLOBAL PROTECTED UND GLOBAL DEFAULT UND
The LLVM declarations show the same difference:
@phase8_visibility_global = external hidden global i32
@phase8_visibility_global = external protected global i32
; Rust, for either input
@phase8_visibility_global = external global i32
This changes link behavior. A C caller carrying the hidden or protected
undefined reference cannot satisfy it from a default-visible definition in a
shared library. A Rust caller using the generated binding can. In my test, the
C link failed while the Rust program linked and printed value=31337.
As a control, I compiled a local definition that includes the same header.
Both callers then linked and printed the same value, and the provider object
retained the requested visibility.
Expected Results
Bindgen should not turn a non-default ELF reference into a default reference.
Rust has no general source attribute for ELF visibility on an extern static,
so omitting the variable with a diagnostic would be a reasonable conservative
result. A target-specific way to preserve the visibility would also work.
Emitting an ordinary extern static is not equivalent to either C declaration.
Environment
bindgen current main: 9d26c6eddeff9192ddedb563192abe3128fc5aae
bindgen release: 0.72.1
clang: 15.0.7
rustc: 1.75.0
target: x86_64-unknown-linux-gnu
object inspection: GNU readelf
I repeated hidden and protected at O0 and O2 with both bindgen versions. The C
objects retained HIDDEN or PROTECTED in every run; the objects produced from
the generated Rust binding used DEFAULT in every run.
Additional notes
Functions and variables currently take different paths here. Function parsing
checks cursor.visibility() and filters non-default declarations. The
top-level variable path does not keep the Clang visibility, so code generation
has no way to distinguish these inputs.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 846
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 11
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-lang/rust-bindgen
-
--enable-cxx-namespaces panics on namespace [[deprecated]] { ("[" is not a valid Ident)Possibly taken @lwz23 claimed this 47 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
rust-lang/rust-bindgen#3485 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
rust-lang/rust-bindgen#3483 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
rust-lang/rust-bindgen#3370 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
rust-lang/rust-bindgen#3363 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
rust-lang/rust-bindgen#3540 ·
Maintainers usually reply within 1 day
All issues in rust-lang/rust-bindgen
Similar issues
-
good first issue help wanted program
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
hffgzktbcc-blip/ounce-core#297 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nautechsystems/nautilus_trader#5631 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
bytecodealliance/rustix#1736 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
longbridge/gpui-kit#3871 ·
Maintainers usually reply within 1 day
-
area:models enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day