使用LLVM工具链进行编译,组装和反汇编 [英] Compile, Assemble and Disassemble Using the LLVM Tool Chain

查看:809
本文介绍了使用LLVM工具链进行编译,组装和反汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下示例进行编译,组装和反汇编使用LLVM工具链的小程序.

我的目的是学习它的工作原理,因此在进一步尝试中,我可以通过更改和/或重新排列组装指令来进行一些性能测试.

首先,我首先获得一个虚拟程序的叮咬代码:

    % llvm-gcc -O3 -emit-llvm hello.c -c -o hello.bc

一旦我得到了叮咬的代码,我就会尝试使用llvm-dis实用程序来查看LLVM汇编代码和llc,以便使用LLC代码生成器将程序编译回本机汇编(只是为了尝试而已) ):

    % llvm-dis < hello.bc | less
    % llc hello.bc -o hello.s

但是在两种情况下,我都会遇到以下错误:

    llvm-dis: Invalid MODULE_CODE_GLOBALVAR record

关于如何解决此问题的任何想法?

我已经用Google搜索,但没有找到解决方案.我也尝试使用

    otool -tV hello

但是输出与llvm不兼容.而不是获取以下程序集格式:

.section    __TEXT,__text,regular,pure_instructions
.globl  _main
.align  4, 0x90
    _main:
    Leh_func_begin1:
pushq   %rbp
    Ltmp0:
movq    %rsp, %rbp
    Ltmp1:
subq    $16, %rsp
    Ltmp2:
leaq    L_.str(%rip), %rax

我得到:

__TEXT,__text) section
start:
0000000100000eb0    pushq   $0x00
0000000100000eb2    movq    %rsp,%rbp
0000000100000eb5    andq    $0xf0,%rsp
0000000100000eb9    movq    0x08(%rbp),%rdi
0000000100000ebd    leaq    0x10(%rbp),%rsi
0000000100000ec1    movl    %edi,%edx
0000000100000ec3    addl    $0x01,%edx

这对我无效,例如,我用以下代码编译后一个程序集:

    % gcc hello.s -o hello.native

谢谢.

解决方案

请确保您的llvm-gcc的版本与您安装的LLVM的版本匹配-二进制IR格式更改速度非常快,并且在多个版本之间都不向后兼容.

或者,您可以尝试从llvm-gcc发出LLVM IR的 text 表示形式,并通过llvm-as进行组装.

类似这样的东西:

llvm-gcc -emit-llvm -S foo.c -o foo.ll
llvm-as foo.ll -o foo.bc
llc foo.ll -o foo.S

I'm trying to run the following example to Compile, Assemble and Disassemble an small program using the LLVM tool chain.

My intention is to learn how it works so in further tries I could do some performance test by changing and/or reordering the assmbly's instructions.

To start with, I first get the bite code of a dummy program:

    % llvm-gcc -O3 -emit-llvm hello.c -c -o hello.bc

Once I have the bite code, i try to use the llvm-dis utility to take a look at the LLVM assembly code and llc to compile the program back to native assembly using the LLC code generator (just for the shake of trying):

    % llvm-dis < hello.bc | less
    % llc hello.bc -o hello.s

But in both cases I get the following error:

    llvm-dis: Invalid MODULE_CODE_GLOBALVAR record

Any ideas on how to solve this problem?

I've googled and I haven't found a solution. I have also tried to use

    otool -tV hello

But the output is not compatible with llvm. Instead of getting the following assembly format:

.section    __TEXT,__text,regular,pure_instructions
.globl  _main
.align  4, 0x90
    _main:
    Leh_func_begin1:
pushq   %rbp
    Ltmp0:
movq    %rsp, %rbp
    Ltmp1:
subq    $16, %rsp
    Ltmp2:
leaq    L_.str(%rip), %rax

I get:

__TEXT,__text) section
start:
0000000100000eb0    pushq   $0x00
0000000100000eb2    movq    %rsp,%rbp
0000000100000eb5    andq    $0xf0,%rsp
0000000100000eb9    movq    0x08(%rbp),%rdi
0000000100000ebd    leaq    0x10(%rbp),%rsi
0000000100000ec1    movl    %edi,%edx
0000000100000ec3    addl    $0x01,%edx

Which is not valid for me as I cat compile the latter assembly with for example:

    % gcc hello.s -o hello.native

Thanks in advance.

解决方案

Make sure your llvm-gcc's version match the version of LLVM you installed - the binary IR format changes quite fast and is not backward compatible across several version.

Alternatively, you might try to emit text representation of LLVM IR out of llvm-gcc and assemble it through llvm-as.

Something like this:

llvm-gcc -emit-llvm -S foo.c -o foo.ll
llvm-as foo.ll -o foo.bc
llc foo.ll -o foo.S

etc.

这篇关于使用LLVM工具链进行编译,组装和反汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆