rust-lang / rust-lang/rust-bindgen

can't parse msvc inline __asm

Open
#2,034 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-libclang bug
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

I'm trying to cross-compile from x64 ubuntu to i686-pc-windows-msvc using the include files from msvc-wine and bingen fails to parse the msvc inline asm blocks in winnt.h.

The inline asm blocks in question use the __asm syntax specific to MSVC (official doc), allowed on 32bit only, which should be supported by llvm.

The error I get is very easily reproducible with the following header file:

// input.h
void test() {
    int i = 42;
    __asm    {
        mov     ecx, i
        mov     eax, ecx
    }
}
// build.rs
fn main() {
    bindgen::Builder::default()
        .header("test.h")
        .generate()
        .unwrap();
}

Trying to compile it with cargo build --target i686-pc-windows-msvc shows the following error:

   Compiling asm_bindgen v0.1.0 (/home/orycterope/workspace/asm_bindgen)
error: failed to run custom build command for `asm_bindgen v0.1.0 (/home/orycterope/workspace/asm_bindgen)`

Caused by:
  process didn't exit successfully: `/home/orycterope/workspace/asm_bindgen/target/debug/build/asm_bindgen-7d7435a9639ad2dc/build-script-build` (exit code: 101)
  --- stderr
  test.h:21:5: error: MS-style inline assembly is not available: Unable to find target for this triple (no targets are registered)
  test.h:21:5: error: MS-style inline assembly is not available: Unable to find target for this triple (no targets are registered), err: true
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ()', build.rs:5:10

My knowledge of llvm and clang is very limited, but I found this llvm mailing thread from 2013 when this used to make the lib crash, and added the error that we see. It suggests calling

llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();

to initialize the MS target Assembly Parser so it can recognize __asm blocks, which presumably is always done by clang but not by libclang. It also mentions that this is not needed for gcc style inline asm because they are not handled by a target Assembly Parser.

I don't really know how to go about this issue. Any advice ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with the input.h and build.rs example using cargo build --target i686-pc-windows-msvc. Trace how bindgen invokes libclang for the __asm block in winnt.h and investigate the reported missing target assembly parser. Done means the sample header parses successfully and binding generation completes for the target.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.