简体   繁体   English

使用“jmp *%esp”时操作数类型不匹配

[英]Operand type mismatch when using “jmp *%esp”

I have this snippet in my code 我的代码中有这个代码段

void jmp_esp()
{
    __asm__("jmp *%esp");
}

when compiling with gcc 用gcc编译时

gcc aslr.c -o aslr -ggdb -fno-stack-protector -z execstack

i get this error. 我收到这个错误。

aslr.c: Assembler messages:
aslr.c:6: Error: operand type mismatch for `jmp'

Why this line is failing to compile although the assembly instruction is valid ? 尽管汇编指令有效,为什么这行无法编译?

I've read about DEP (Data Execution Prevention). 我读过有关DEP(数据执行预防)的文章。 could it be that this feature is creating this compilation error ? 可能是这个功能正在创建这个编译错误? if so, how to disable it ? 如果是的话,如何禁用它?

The instruction jmp *%esp is available only in 16 and 32 bit modes. 指令jmp *%esp仅在16位和32位模式下可用。 In 64 bit mode, jmp r/m32 cannot be encoded. 在64位模式下, jmp r/m32无法编码。 Depending on what your intent is, there are two ways to fix your code: 根据您的意图,有两种方法可以修复您的代码:

  • if your intent is to write a 32 bit x86 program, compile and link with -m32 to make the compiler emit 32 bit code. 如果您的目的是编写32位x86程序,请编译并链接-m32以使编译器发出32位代码。
  • if your intent is to write a 64 bit x86 program, change the instruction to jmp *%rsp to jump to the address contained in the rsp register instead. 如果您的目的是编写64位x86程序,请将指令更改为jmp *%rsp以跳转到rsp寄存器中包含的地址。

Note that this is independent of DEP. 请注意,这与DEP无关。 DEP prevents the execution of memory regions not specifically marked as executable. DEP阻止执行未明确标记为可执行的内存区域。 This happens at runtime, not at compile time. 这在运行时发生,而不是在编译时发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM