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

查看:26
本文介绍了如何在 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 && 将其组装成静态 64 位 ELF 可执行文件ld foo.o -o foo,然后用 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 重定位计算从 0x400080+5 到达 0x1234567 的正确 rel32:>

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 inline-asm 做到这一点?

MSVC 不接受 _asm { call 1234567h;}.错误是C2415:不正确的操作数类型.我找到的唯一 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 inline-asm(这不是一回事)Godbolt 编译器浏览器.

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.

你能设置一个标签的地址并将其用作调用符号的目标吗?就像 GAS 的 .set 符号 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) 发出编码吗?可能不是,在 NASM 中仅适用于 label - $,不是 absolute - 标签

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 到绝对地址的规范答案中:在 x86 机器代码中调用绝对指针 绝对不是我想实际使用的任何代码.

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 inline-asm 中,您可以使用 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天全站免登陆