如何在MASM中为近乎直接的相对调用/jmp编写绝对目标 [英] How to write an absolute target for a near direct relative call/jmp in MASM

查看:126
本文介绍了如何在MASM中为近乎直接的相对调用/jmp编写绝对目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要使用绝对地址(几乎直接相对)call到绝对地址,请使用NASM或AT& T语法编写call 0x1234567,然后汇编程序+链接程序将计算rel32以达到该目标从链接器放置call指令的任何位置开始.

To make a normal (near direct relative) call to an absolute address, in NASM or AT&T syntax you write call 0x1234567, and the assembler + linker take care of calculating a rel32 to reach that target from wherever the linker puts the call instruction.

例如在Linux上,使用yasm -felf64 foo.asm && ld foo.o -o foo将其组装成静态的64位ELF可执行文件,然后使用objdump -drwC -Mintel foo进行反汇编可以得到:

e.g. on Linux assembling that into a static 64-bit ELF executable with yasm -felf64 foo.asm && ld foo.o -o foo, then disassembled with objdump -drwC -Mintel foo gives you:

foo:     file format elf64-x86-64
Disassembly of section .text:
0000000000400080 <_start>:
  400080:       e8 e2 44 e3 00          call   1234567 <_end+0xc344df>

基于目标文件中的R_X86_64_PC32重定位,链接器计算了正确的rel32从0x400080+5到达0x1234567:

The linker calculated the right rel32 to reach 0x1234567 from 0x400080+5, based on a R_X86_64_PC32 relocation in the object file:

  0:   e8 00 00 00 00          call   5 <_start+0x5>   1: R_X86_64_PC32        *ABS*+0x1234563


您如何通过MASM和/或MSVC内联汇编来做到这一点?

MSVC不接受_asm { call 1234567h; }.错误是C2415: improper operand type. 我找到的唯一的SO答案建议使用一种解决方法具有内存或寄存器中的地址的间接jmp的方法,但是由于难以使用的工具而导致低效率的机器代码并不是一个很好的解决方案.

MSVC doesn't accept _asm { call 1234567h; }. The error is C2415: improper operand type. The only SO answer I've found suggests using a workaround of an indirect jmp with the address in memory or a register, but making inefficient machine code because of hard-to-use tools isn't a very good solution.

我根本没有MASM,所以我只能尝试MSVC内联式asm(这是不一样的事情)

I don't have MASM at all, so I've only been able to try MSVC inline-asm (which is not the same thing) on the Godbolt compiler explorer.

您可以设置标签的地址并将其用作call symbol的目标吗?就像GAS的.set symbol, 0x1234567一样,它使您可以给符号赋予地址 ,而不必在任何地方实际编写symbol:.

Can you set the address of a label and use it as a target for call symbol? Like with GAS's .set symbol, 0x1234567 which lets you give a symbol an address without having to actually writing symbol: anywhere.

您可以直接使用db 0E8h/dd 1234567h - ($ + 4)发出编码吗?可能不是,在仅适用于label - $ 而不是

Can you emit the encoding directly with db 0E8h / dd 1234567h - ($ + 4)? Probably not, in NASM that only works with label - $, not absolute - label

我对答案最感兴趣,因此可以将其包含在有关jmp/call的规范答案中,以绝对地址:

I'm mostly interested in the answer so I can include it in my canonical answer about jmp/call to an absolute address: Call an absolute pointer in x86 machine code Definitely not for any code I want to actually use.

推荐答案

MASM不支持此操作,因为COFF对象文件格式不支持必要的重定位. (或者正确不支持它?根据NASM错误消息.)

MASM doesn't support this because the COFF object file format doesn't support the necessary relocation. (Or doesn't correctly support it? According to a NASM error message.)

nasm -f win32中使用call 0x76cd75c0语法会出现错误:

Using the call 0x76cd75c0 syntax in nasm -f win32 gives an error:

error: Win32 COFF does not correctly support relative references to absolute addresses

我不知道面向实模式平面二进制文件的MASM是否可以做到(没有目标文件必须描述链接器的重定位),但是不幸的是,MASM的设计根本没有语法.

I don't know if MASM targeting real mode flat binaries could do it (where there are no object files that have to describe the relocations to the linker), but probably MASM was just designed without syntax for it at all, unfortunately.

另请参见在user32.dll中直接调用函数时发生错误.我确实在自己的Linux桌面上尝试了nasm -fwin32 2.13.02并遇到了相同的错误.

See also Error when calling function in user32.dll directly. I did try nasm -fwin32 2.13.02 myself on my Linux desktop and got the same error.

未经测试的可能解决方法可能是创建具有该绝对地址的符号定义的.obj,例如

org 0deadbeefH
global my_target
my_target:

在NASM中,或者您在MASM中进行.

in NASM or however you do that in MASM.

然后在MASM或MSVC内联汇编中,您可以使用jmp my_target并与该.obj链接.从理论上讲,这可能可以解决在目标文件中表示重定位的问题,并获得链接器以计算相对分支位移.

Then in MASM or MSVC inline-asm you can use jmp my_target and link with that .obj. In theory that might work around the problem of representing relocations in the object file, and get the linker to calculate the relative branch displacement.

这篇关于如何在MASM中为近乎直接的相对调用/jmp编写绝对目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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