首页 > 解决方案 > 添加新的 GNU 汇编器 x86 指令并重新编译的源代码

问题描述

我试图找出我需要在 binutils 项目中修改哪个文件,以便我可以添加一个新的 x86 指令mov2,该指令将是mov. 搜索源文件夹产生了数百个结果,但我仍然找不到任何包含指令引用的文件。

谢谢

标签: assemblyx86gnu-assembler

解决方案


正如@Jester提到的需要修改的文件是opcodes/i386-opc.tbl我也不得不在 IRC 上打扰他以获取更多详细信息,但他很友好地指导了我。我可能会注意到我只能在 binutils v2.25 上生成,我相信后来的分支开始使用不同的解决方案,可能与“make run-cgen”目标有关,但它的研究改天了:)

以下是我遵循的步骤:

创建临时目录,一个用于构建,一个用于二进制文件:

$mkdir -pv /tmp/{instruction-test,tools}
$cd /tmp/instruction-test/

克隆 binutils 存储库,或者您可以直接从 gnu ftp 下载:

$git clone http://sourceware.org/git/binutils-gdb.git

切换到分支:

$cd binutils/
$git checkout binutils-2_25

预建项目:

$mkdir -v build
$cd build
$../configure --prefix=/tmp/tools  --with-sysroot=/tmp/tools  --with-lib-path=/tmp/tools/lib  --target=x86_64-custom-linux-gnu  --disable-nls  --disable-werror

$make clean
$time make -s -j XX > make.log || { echo "can not make project, please check logs installation aborted" ; exit 1; }

现在添加 mov2 指令作为 mov 的精确副本

$cd ../opcodes
$cat i386-opc.tbl | egrep -e '^mov,' | sed 's/mov/mov2/g' >> i386-opc.tbl

现在你可以在这里停下来看看我们添加了什么:

$git diff i386-opc.tbl

现在我们还需要更新 i386 操作码表:

$cd ../build/opcodes
$make i386-gen
$./i386-gen --srcdir=$(pwd)/../../opcodes

现在我们可以重建并安装到 /tmp/tools 中:

$cd ..
$time make -s > make.log
$make -s install > install.log

是时候测试了!这是带有新说明的小示例:

$cat > /tmp/hello << EOF
        .global _start

        .text
_start:
        # write(1, message, 13)
        mov2     $1, %rax                # system call 1 is write
        mov2     $1, %rdi                # file handle 1 is stdout
        mov2     $message, %rsi          # address of string to output
        mov2     $13, %rdx               # number of bytes
        syscall                         # invoke operating system to do the write

        # exit(0)
        mov2     $60, %rax               # system call 60 is exit
        xor     %rdi, %rdi              # we want return code 0
        syscall                         # invoke operating system to exit
message:
        .ascii  "Hello, world\n"
EOF
$cd /tmp/tools/bin
$./x86_64-custom-linux-gnu-as -o /tmp/hello.o /tmp/hello
$ld /tmp/hello.o -o /tmp/hello.bin
$cd /tmp/
$./hello.bin

现在您应该能够看到输出:

Hello, world

如果您遇到我怀疑它与 cat/EOF 命令有关的问题,您可以简单地尝试使用您自己的文件。正如我所提到的,这不是最新的解决方案,我在我认为由 cgen 管理的后来的分支上运行 i386-gen 时遇到了麻烦?或者也许我需要处理这里使用的标准输入, 但我没有太多时间


推荐阅读