添加新的 GNU 汇编程序 x86 指令和重新编译的源代码
Source code for adding new GNU assembler x86 instruction and recompile
我试图弄清楚我需要在 binutils 项目中修改哪个文件,以便我可以添加一个新的 x86 指令 mov2
,它将是 mov
的精确副本。搜索源文件夹得到了数百个结果,但我仍然找不到任何包含指令引用的文件。
谢谢
作为@Jester mentioned file needs to be modified is opcodes/i386-opc.tbl,我也不得不在 IRC 上打扰他了解更多细节,但他很友好地指导了我。我可能会注意到我只能在 binutils v2.25 上生成,我相信后来的分支开始使用不同的解决方案,可能与 "make run-cgen" 目标有关,但它需要改天再研究:)
以下是我遵循的步骤:
创建临时目录,一个用于构建,一个用于二进制文件:
$mkdir -pv /tmp/{instruction-test,tools}
$cd /tmp/instruction-test/
克隆 binutils repo 或者你可以直接从 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 操作码 table:
$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 , %rax # system call 1 is write
mov2 , %rdi # file handle 1 is stdout
mov2 $message, %rsi # address of string to output
mov2 , %rdx # number of bytes
syscall # invoke operating system to do the write
# exit(0)
mov2 , %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 问题?或者我可能需要处理使用的标准输入 here 但我没有太多时间
我试图弄清楚我需要在 binutils 项目中修改哪个文件,以便我可以添加一个新的 x86 指令 mov2
,它将是 mov
的精确副本。搜索源文件夹得到了数百个结果,但我仍然找不到任何包含指令引用的文件。
谢谢
作为@Jester mentioned file needs to be modified is opcodes/i386-opc.tbl,我也不得不在 IRC 上打扰他了解更多细节,但他很友好地指导了我。我可能会注意到我只能在 binutils v2.25 上生成,我相信后来的分支开始使用不同的解决方案,可能与 "make run-cgen" 目标有关,但它需要改天再研究:)
以下是我遵循的步骤:
创建临时目录,一个用于构建,一个用于二进制文件:
$mkdir -pv /tmp/{instruction-test,tools}
$cd /tmp/instruction-test/
克隆 binutils repo 或者你可以直接从 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 操作码 table:
$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 , %rax # system call 1 is write
mov2 , %rdi # file handle 1 is stdout
mov2 $message, %rsi # address of string to output
mov2 , %rdx # number of bytes
syscall # invoke operating system to do the write
# exit(0)
mov2 , %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 问题?或者我可能需要处理使用的标准输入 here 但我没有太多时间