尝试对指令进行空操作
是否可以使用 GNU 工具(gcc、binutils 等)将所有出现的汇编指令修改为无操作? 具体来说,带 -pg 选项的 gcc 生成以下程序集(ARM):
0x0: e1a0c00d mov ip, sp
0x4: e92dd800 stmdb sp!, {fp, ip, lr, pc}
0x8: e24cb004 sub fp, ip, #4 ; 0x4
0xc: ebfffffe bl 0 <mcount>
我想记录最后一条指令的地址,然后将其更改为 nop,如以下代码所示
0x0: e1a0c00d mov ip, sp
0x4: e92dd800 stmdb sp!, {fp, ip, lr, pc}
0x8: e24cb004 sub fp, ip, #4 ; 0x4
0xc: e1a00000 nop (mov r0,r0)
Linux 内核可以在运行时执行类似的操作-时间,但我正在寻找构建时解决方案。
Is it possible using GNU tools (gcc, binutils, etc) to modify all occurrences of an assembly instruction into a no-op? Specifically, gcc with the -pg option generates the following assembly (ARM):
0x0: e1a0c00d mov ip, sp
0x4: e92dd800 stmdb sp!, {fp, ip, lr, pc}
0x8: e24cb004 sub fp, ip, #4 ; 0x4
0xc: ebfffffe bl 0 <mcount>
I want to record the address of this last instruction, and then change it to a nop like in the following code
0x0: e1a0c00d mov ip, sp
0x4: e92dd800 stmdb sp!, {fp, ip, lr, pc}
0x8: e24cb004 sub fp, ip, #4 ; 0x4
0xc: e1a00000 nop (mov r0,r0)
The Linux kernel can do something similar to this at run-time, but I'm looking for a build-time solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 gcc -S 编译代码以输出汇编程序列表,而不是完全编译为目标文件或可执行文件。 然后,只需用无操作替换所需的指令(例如使用
sed
),然后从那里继续编译。如果您还想对没有原始源代码的目标文件或库执行此操作,则必须使用诸如
objdump(1)
反汇编它们并获取您想要替换的指令的地址。 然后,解析目标文件头以查找这些指令在文件内的偏移量,然后直接在目标文件中将机器指令替换为无操作。 这有点棘手,但可行。You can compile the code with
gcc -S
to output an assembler listing, instead of compiling fully into an object file or executable. Then, just replace the desired instructions with no-ops (e.g. usingsed
), and continue compilation from there.If you also want to do this for object files or libraries that you don't have the original source code for, you'll instead have to use a tool such as
objdump(1)
to disassemble them and get the addresses of the instructions you wish to replace. Then, parse the object file headers to find the offsets within the file of those instructions, and then replace the machine instructions with no-ops directly in the object files. This is a little trickier, but doable.使用 RISC-ish 固定长度指令格式肯定比 x86 更容易。
使用 libelf 应该相对简单(这里有很好的教程:http:// people.freebsd.org/~jkoshy/download/libelf/article.html) 或 libbfd (http://sourceware.org/binutils/docs-2.19/bfd/index.html) 打开目标文件,修改 .text 部分中的指令,然后使用再次将其写出提供的API。 是否值得付出努力将取决于非技术考虑(不过我有点好奇......)。
值得一提的是,如果需要在交叉开发环境中工作,则使用 libelf 或 libbfd 可能会出现一些问题。
This will certainly be easier with a RISC-ish fixed-length instruction format than for e.g. x86.
It should be relatively straightforward to use libelf (nice tutorial here: http://people.freebsd.org/~jkoshy/download/libelf/article.html) or libbfd (http://sourceware.org/binutils/docs-2.19/bfd/index.html) to open the object file, modify instructions within the .text section, and write it out again using provided APIs. Whether it's worth the effort or not will depend on non-technical considerations (I am a bit curious though...).
It's worth mentioning that there might be a few wrinkles with using libelf or libbfd if this needs to work in a cross-development environment.