Skip to content

clang_macro_fallback doesn't propagate implicit --target to fallback translation unit #3352

@zRedShift

Description

@zRedShift

When bindgen infers the target triple from the Rust target (no explicit --target in clang args), it inserts --target= into clang_args in Bindings::generate() (lib.rs ~line 789). However, fallback_clang_args is populated from clang_args earlier in Builder::generate() (lib.rs ~line 345), before this insertion. The fallback translation unit therefore uses the host architecture instead of the target architecture.

Impact: For cross-compilation, sizeof-dependent macros evaluate with the host's struct layouts instead of the target's, producing silently wrong values. This affects any project that cross-compiles bindings and relies on clang_macro_fallback — for example, linux-raw-sys generates bindings for 22 architectures, and ioctl constants defined via _IOR/_IOW/_IOWR (which embed sizeof(struct)) would get wrong values for any arch where struct sizes differ from the host.

Note: this only affects the implicit target case. If the user explicitly passes --target= via .clang_arg(), it's already present in clang_args when fallback_clang_args is populated, so the bug doesn't manifest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions