通过修改LLVM后端注册Clobber X86 [英] Clobber X86 register by modifying LLVM Backend

查看:74
本文介绍了通过修改LLVM后端注册Clobber X86的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对X86目标的LLVM后端进行一些更改,以产生一些所需的行为.

I am trying to alter a little bit the LLVM Backend for X86 target, to produce some desired behaviour.

更具体地说,我想模拟gcc的fcall-used- reg 这样的标记

More specifically, I would like to emulate a flag like gcc's fcall-used-reg option, which instructs the compiler to convert a callee-saved register into a clobbered register (meaning that it may be altered during a function call).

让我们专注于 r14 .我手动破坏寄存器,例如这个答案:

Let's focus on r14. I manually clobber the register, like in this answer:

#include <inttypes.h>

uint64_t inc(uint64_t i) {
    __asm__ __volatile__(
        ""
        : "+m" (i)
        :
        : "r14"
    );
    return i + 1;
}

int main(int argc, char **argv) {
    (void)argv;
    return inc(argc);
}

编译和反汇编:

 gcc -std=gnu99 -O3 -ggdb3 -Wall -Wextra -pedantic -o main.out main.c
 objdump -d main.out

反汇编包含:

0000000000001150 <inc>:                                                                                                                                                                                            
    1150:       41 56                   push   %r14                                                                                                                                                                
    1152:       48 89 7c 24 f8          mov    %rdi,-0x8(%rsp)                                                                                                                                                     
    1157:       48 8b 44 24 f8          mov    -0x8(%rsp),%rax                                                                                                                                                     
    115c:       41 5e                   pop    %r14                                                                                                                                                                
    115e:       48 83 c0 01             add    $0x1,%rax                                                                                                                                                           
    1162:       c3                      retq                                                                                                                                                                       
    1163:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)                                                                                                                                                
    116a:       00 00 00
    116d:       0f 1f 00                nopl   (%rax)

我们可以看到 r14 因为被篡改而被压入堆栈,然后弹出以恢复其原始值.现在,重复 -fcall-used-r14 标志:

where we can see that r14, because it is tampered with, is pushed to the stack, and then popped to regain its original value. Now, repeat with the -fcall-used-r14 flag:

 gcc -std=gnu99 -O3 -ggdb3 -fcall-used-r14 -Wall -Wextra -pedantic -o main.out main.c
 objdump -d main.out

反汇编包含:

0000000000001150 <inc>:                                                                                                                                                                                            
    1150:       48 89 7c 24 f8          mov    %rdi,-0x8(%rsp)
    1155:       48 8b 44 24 f8          mov    -0x8(%rsp),%rax
    115a:       48 83 c0 01             add    $0x1,%rax
    115e:       c3                      retq
    115f:       90                      nop

没有推送/弹出的地方.

where no push/pop happens.

现在,我已经修改了一些LLVM目标文件,编译了源文件,并将此功能添加到了 llc 工具中:

Now, I have modified some LLVM Target files, compiled the source, and added(?) this functionality to the llc tool:

clang-11 -emit-llvm -S -c main.c -o main.ll
llc-11 main.ll -o main.s

现在, main.s 包含:

# %bb.0:
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        pushq   %r14
        .cfi_offset %r14, -24
        movq    %rdi, -16(%rbp)
        #APP
        #NO_APP
        movq    -16(%rbp), %rax
        addq    $1, %rax
        popq    %r14
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq

显然, r14 仍然被呼叫者保存.

Apparently, r14 is still callee-saved.

llvm/lib/Target/X86/X86CallingConv.td 内部,我修改了以下几行(删除了R14),因为它们似乎与Linux和C调用约定的System V ABI唯一相关我感兴趣的:

Inside llvm/lib/Target/X86/X86CallingConv.td I have modified the following lines (removing R14), because they seemed the only relevant to the System V ABI for Linux and C calling conventions that I was interested in:

def CSR_64 : CalleeSavedRegs<(add R12, R13, R15, RBP)>;
...
def CSR_64_MostRegs : CalleeSavedRegs<(add RBX, RCX, RDX, RSI, RDI, R8, R9, R10,
                                           R11, R12, R13, R15, RBP,
...
def CSR_64_AllRegs_NoSSE : CalleeSavedRegs<(add RAX, RBX, RCX, RDX, RSI, RDI, R8, R9,
                                                R10, R11, R12, R13, R15, RBP)>;

我的问题是:

  • X86CallingConv.td 是我唯一需要修改的文件吗?我想是的,但也许我错了.
  • 我专注于正确的路线吗?也许这很难回答,但至少一个方向可能会有所帮助.
  • Is X86CallingConv.td the only file I should modify? I think yes, but maybe I'm wrong.
  • Am I focusing on the correct lines? Maybe this is more difficult to answer, but at least a direction could be helpful.

我正在Debian 10.5中运行LLVM 11.

I am running LLVM 11 inside Debian 10.5.

改变线,从隐藏的" R 14中除去R 14.定义:

Changing the line, removing R14 from "hidden" definition:

def CSR_SysV64_RegCall_NoSSE : CalleeSavedRegs<(add RBX, RBP, RSP,
                                               (sequence "R%u", 12, 13), R15)>;

正如玛格丽特正确指出的那样,也无济于事.

as Margaret correctly pointed out did not help either.

推荐答案

结果是,最小的修改是这一行:

Turns out, the minimum modification was the line:

def CSR_64 : CalleeSavedRegs<(add RBX, R12, R13, R15, RBP)>;

问题出在我如何构建源代码上.

The problem was with how I built the source.

通过在原始安装后再次运行 cmake --build llc 工具未进行全局修改(我认为这样做是因为我正在构建默认体系结构-X86-但这无关紧要).因此,我打电话给了未经修改的 llc-11 工具.因此,当我跑步时:

By running cmake --build . again after the original installation, the llc tool was not modified globally (I thought it would have because I was building the default architecture - X86 - but that was irrelevant). So, I was calling an unmodified llc-11 tool. Thus, when I ran:

/path/to/llvm-project/build/bin/lcc main.ll -o main.s

main.s 包含:

# %bb.0:
        movq    %rdi, -8(%rsp)
        #APP
        #NO_APP
        movq    -8(%rsp), %rax
        addq    $1, %rax
        retq

这是我首先要的.

这篇关于通过修改LLVM后端注册Clobber X86的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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