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

Fail to generate bindings for functions with calling convention "aapcs-vfp" for armv7a #1607

Closed
andersrein opened this issue Aug 6, 2019 · 4 comments

Comments

@andersrein
Copy link

Input C/C++ Header

__attribute__((pcs("aapcs-vfp"))) float square_root(float input);

Bindgen Invocation


    bindgen::Builder::default()
        .header("fail.h")
        .whitelist_function("square_root")
        .generate()
        .expect("Failed to generate bindings")
        .write_to_file(out_path.join("square_root.rs"))
        .expect("Failed to WRITE bindings");

NOTE! The bug only occurs when cargo is invoked with the --target armv7-linux-androideabi flag because aapcs-vfp is ignored for all other platforms.

Actual Results

   Compiling bindgen_fail v0.1.0 (/home/are/source/bindgen_fail)
error: failed to run custom build command for `bindgen_fail v0.1.0 (/home/are/source/bindgen_fail)`

Caused by:
  process didn't exit successfully: `/home/are/source/bindgen_fail/target/debug/build/bindgen_fail-d45863190521fcb5/build-script-build` (exit code: 101)
--- stdout
cargo:warning=couldn't execute `llvm-config --prefix` (error: No such file or directory (os error 2))
cargo:warning=set the LLVM_CONFIG_PATH environment variable to a valid `llvm-config` executable

--- stderr
thread 'main' panicked at 'Invalid or unknown abi 7 for function "square_root" (Function { name: "square_root", mangled_name: Some("square_root"), signature: TypeId(ItemId(1)), comment: None, kind: Function, linkage: External })', /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:3415:17
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:59
             at src/libstd/panicking.rs:197
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:211
   4: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:474
   5: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:381
   6: std::panicking::begin_panic_fmt
             at src/libstd/panicking.rs:336
   7: <bindgen::ir::function::Function as bindgen::codegen::CodeGenerator>::codegen
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:3415
   8: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:364
   9: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen::{{closure}}
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:392
  10: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:422
  11: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:361
  12: bindgen::codegen::codegen::{{closure}}
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:3587
  13: bindgen::ir::context::BindgenContext::gen
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/ir/context.rs:1233
  14: bindgen::codegen::codegen
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/codegen/mod.rs:3564
  15: bindgen::Bindings::generate
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/lib.rs:1843
  16: bindgen::Builder::generate
             at /home/are/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.51.0/src/lib.rs:1228
  17: build_script_build::main
             at ./build.rs:8
  18: std::rt::lang_start::{{closure}}
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/rt.rs:64
  19: std::panicking::try::do_call
             at src/libstd/rt.rs:49
             at src/libstd/panicking.rs:293
  20: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:85
  21: std::rt::lang_start_internal
             at src/libstd/panicking.rs:272
             at src/libstd/panic.rs:394
             at src/libstd/rt.rs:48
  22: std::rt::lang_start
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/rt.rs:64
  23: main
  24: __libc_start_main
  25: _start

Expected Results

Expect it to be able to generate rust bindings of this simple header.

@emilio
Copy link
Contributor

emilio commented Aug 6, 2019

Does Rust support this ABI?

@andersrein
Copy link
Author

Honestly I don't know. I've never heard of it. I just encountered it while trying to create rust bindings from the vulkan headers in the Android NDK. Rust does support armv7-linux-androideabi, however I am not sure about this exact calling convention. Somewhat difficult to find any documentation about what it is or whether or not rust supports it. I found this post which talks about adding support for aapcs-vfp: rust-lang/rust#37810

@emilio
Copy link
Contributor

emilio commented Aug 7, 2019

Oh so it looks like extern "aapcs" fn is a thing... But not extern "aapcs-vfp" unfortunately:

https://github.com/rust-lang/rust/blob/5421d94960018235654c7fb39aa1c502a3564621/src/librustc_target/abi/call/mod.rs#L503

You need to add extern "aapcs-vfp" fn support to rustc (should be trivial by looking at related patches there). Once that's done, adding support to bindgen is trivial. Something like this should do:

diff --git a/src/ir/function.rs b/src/ir/function.rs
index b8af9213..de3c3e8b 100644
--- a/src/ir/function.rs
+++ b/src/ir/function.rs
@@ -175,6 +175,8 @@ pub enum Abi {
     ThisCall,
     /// The "aapcs" ABI.
     Aapcs,
+    /// The "aapcs-vfp" ABI.
+    AapcsVfp,
     /// The "win64" ABI.
     Win64,
     /// An unknown or invalid ABI.
@@ -199,6 +201,7 @@ impl quote::ToTokens for Abi {
             Abi::Fastcall => quote! { "fastcall" },
             Abi::ThisCall => quote! { "thiscall" },
             Abi::Aapcs => quote! { "aapcs" },
+            Abi::AapcsVfp => quote! { "aapcs-vfp" },
             Abi::Win64 => quote! { "win64" },
             Abi::Unknown(cc) => panic!(
                 "Cannot turn unknown calling convention to tokens: {:?}",
@@ -237,6 +240,7 @@ fn get_abi(cc: CXCallingConv) -> Abi {
         CXCallingConv_X86FastCall => Abi::Fastcall,
         CXCallingConv_X86ThisCall => Abi::ThisCall,
         CXCallingConv_AAPCS => Abi::Aapcs,
+        CXCallingConv_AAPCS_VFP => Abi::AapcsVfp,
         CXCallingConv_X86_64Win64 => Abi::Win64,
         other => Abi::Unknown(other),
     }

But making bindgen generate the extern fn without support from rust is kinda useless.

@pvdrz
Copy link
Contributor

pvdrz commented Sep 21, 2022

Closing this as aapcs-vfp support has not been added to rustc yet. Feel free to reopen it if that changes.

@pvdrz pvdrz closed this as completed Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants