rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.39k stars 691 forks source link

`size_t` incorrectly used when stdlib.h is included for aarch64-unknown-none #2776

Closed MasterAwesome closed 7 months ago

MasterAwesome commented 7 months ago

I tried to generate bindings for:

// foo.h
#include <stdlib.h>

size_t foo(void);

By running $ bindgen foo.h -- --target=aarch64-unknown-none. This fails with:

  thread 'main' panicked at 'assertion failed: `(left == right)`
    left: `4`,
   right: `8`: Target platform requires `--no-size_t-is-usize`. The size of `ssize_t` (4) does not match the target pointer size (8)', /home/wtf/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.68.1/codegen/mod.rs:908:25

Although the interesting part is if stddef.h is used instead things work as expected.

MasterAwesome commented 7 months ago

It looks like it uses the system stdlib.h where it gets __extension__ typedef int size_t and gets typedef long unsigned int size_t from stddef.h from LLVM's toolchain include path.

MasterAwesome commented 7 months ago

Turns out this was because LLVM defaulted to using GCC host machine's headers which were specific to x86_64 in typesizes.h.

I'm guessing there's a way to build LLVM with libc types so the correct headers are used. In the meanwhile using aarch64-none-gcc which has its sysroot to not use system headers and generating an intermediate with -E works as expected.

Side question: Does anyone know how to approach the problem when a library meant for bare metal use, uses stdlib.h and the only available compiler for that target is clang and I had the capability to build clang from source?

MasterAwesome commented 7 months ago

BINDGEN_EXTRA_CLANG_ARGS and sysrooting correctly works

guohuan78 commented 5 months ago

BINDGEN_EXTRA_CLANG_ARGS and sysrooting correctly works

I encountered the same problem, do you have any solution?Can you explain these in more detail?