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

Label targets are not represented correctly for llvm-as consumption #114

Closed
kquick opened this issue Jul 5, 2023 · 3 comments
Closed

Label targets are not represented correctly for llvm-as consumption #114

kquick opened this issue Jul 5, 2023 · 3 comments
Labels
bug Something isn't working good first issue A good issue to get started with

Comments

@kquick
Copy link
Member

kquick commented Jul 5, 2023

Bitcode generated by clang and then parsed via llvm-pretty-bc-parser and then output via this library into .ll files may not be usable with llvm-as because of the representation of the label targets.

Generated code contains call void @llvm.db.label(metadata !DILabel(scope: !8, name: loop, which fails on loop (presumably because it needs to be quoted).

This error can be seen running `cabal run disasm-test -- -p '/callbr.c/' (requires https://github.com/galoisinc/llvm-pretty-bc-parser/pulls/230).

@kquick
Copy link
Member Author

kquick commented Jul 5, 2023

[From Ryan Scott:]

OK, I've extracted a test case that confirms that this is in fact an llvm-pretty pretty-printer issue:

// callbr.c
int main(void) {
  int cond = 0;
  asm volatile goto("testl %0, %0; jne %l1;"
                    : /* No outputs */
                    : "r"(cond)
                    : /* No clobbers */
                    : label_true, loop);
  return 0;
loop:
  return 0;
label_true:
  return 1;
}

First, note how LLVM itself formats DILabels:

$ clang -S -emit-llvm -g -O0 callbr.c -o callbr.ll
$ cat callbr.ll | grep "DILabel"
!17 = !DILabel(scope: !7, name: "loop", file: !1, line: 14)
!20 = !DILabel(scope: !7, name: "label_true", file: !1, line: 16)

Now note how it appears after round-tripping through llvm-pretty:

-- Main.hs
module Main (main) where

import Data.LLVM.BitCode
import Text.LLVM.PP

main :: IO ()
main = do
  res <- parseBitCodeFromFile "callbr.bc"
  case res of
    Left err -> fail $ formatError err
    Right bc -> print $ ppLLVM38 ppModule bc

Running this program after compiling with clang -c -emit-llvm -g -O0 callbr.c -o callbr.bc gives:

  call void @llvm.dbg.label(metadata !DILabel(scope: !7, name: loop,
  ...
  call void @llvm.dbg.label(metadata !DILabel(scope: !7,
                                              name: label_true,

Which lacks double quotes around the name values.

@RyanGlScott
Copy link
Collaborator

I've opened #115, which I believe should address this. Can you give it a try, @kquick?

@kquick
Copy link
Member Author

kquick commented Jul 10, 2023

This does seem to fix those issues (in conjunction with the llvm-pretty-bc-parser adaptation on GaloisInc/llvm-pretty-bc-parser#233), but runs into another one: #116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue A good issue to get started with
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants