尝试对指令进行空操作

发布于 2024-07-18 03:59:38 字数 561 浏览 7 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2024-07-25 03:59:38

您可以使用 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. using sed), 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.

━╋う一瞬間旳綻放 2024-07-25 03:59:38

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文