简体   繁体   English

如何使用GCC生成IP相对寻址指令

[英]How to generate IP relative addressing instructions with GCC

I wrote a small program to test GCC's options. 我写了一个小程序来测试GCC的选项。

int main()
{
    int a=0;
    __asm__("movl %0,%%ecx\n"
            "jmp jmpsection\n"
            "inc %%ecx\n"
            "jmpsection: movl $1,%%eax\n"
            "movl $0,%%ebx\n"
            "int $0x80\n"::"a"(a):"ecx","ebx");
}

In order to keep var a equal to 1, skip the inc instruction. 为了使var等于1,跳过inc指令。 I want to force GCC to generate the jmp instruction using IP relative addressing method. 我想强制GCC使用IP相对寻址方法生成jmp指令。 I have searched the GCC manual to find a solution, but I feiled. 我已经搜索了GCC手册以找到解决方案,但我很满意。 Thanks for replay. 谢谢你重播。

For the x86, IP relative addressing is only possible in long mode (64 bit) and you seem to be writing 32 bit code. 对于x86,只能在长模式(64位)中进行IP相对寻址,并且您似乎正在编写32位代码。

Edit: Actually, in 32 bit mode it is possible to make jumps relative to the current IP and I think the compiler actually generates the correct code in your case. 编辑:其实,在32位模式下可以做出跳跃相对于当前的IP,我认为实际上编译器生成你的情况正确的代码。 Let's take a look at the relevant part of the generated assembly: 我们来看看生成的程序集的相关部分:

00000000 <main>:
  ...
  13:   eb 01                   jmp    16 <jmpsection>
  15:   41                      inc    %ecx

00000016 <jmpsection>:
  16:   b8 01 00 00 00          mov    $0x1,%eax
  ...

So, although it looks like the instruction at address 13 makes a direct jump to address 16, it actually is an IP relative jump. 因此,虽然看起来地址13处的指令直接跳转到地址16,但它实际上 IP相对跳转。 Take a look at the machine code: 看看机器代码:

eb 01

eb is the opcode for a short jmp with an 8-bit displacement. eb是具有8位位移的短jmp的操作码。 01 is this displacement. 01是这个位移。 The result of this instruction is a jump to %eip + 0x01 and, as the IP points to the next instruction to be executed, this will jump to address 16. 该指令的结果是跳转到%eip + 0x01 ,并且当IP指向要执行的下一条指令时,它将跳转到地址16。

When you use inline assembly in GCC the assembly code is just passed on directly to the assembler. 在GCC中使用内联汇编时,汇编代码将直接传递给汇编程序。 GCCs command line options do not change your assembly code. GCC命令行选项不会更改汇编代码。 You can verify that looking at the output (use the -S option). 您可以验证查看输出(使用-S选项)。 The compiler options only change the way GCC generates assembly from C Code. 编译器选项仅改变GCC从C代码生成程序集的方式。

So instead of your compilers manual you need to look at your assemblers manual . 因此,您需要查看汇编程序手册,而不是编译器手册 In your case GAS will probably select the short PC relative coding for jmp without the need to specify any options. 在您的情况下,GAS可能会为jmp选择短PC相对编码,而无需指定任何选项。

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

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