继续获取 e8 00 00 00 00 作为机器代码以在汇编中调用函数 [英] Keep getting e8 00 00 00 00 as the machine code to call a function in assembly

查看:29
本文介绍了继续获取 e8 00 00 00 00 作为机器代码以在汇编中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在我的文件中使用 objdump -drcall 在机器代码中显示为 e8 00 00 00 00 因为它没有尚未被链接.但我需要找出链接器完成工作后 00 00 00 00 会变成什么.我知道它应该计算偏移量,但我对此有点困惑.

I know when using objdump -dr in my file call shows up in machine code as e8 00 00 00 00 because it has not yet been linked. But I need to find out what the 00 00 00 00 will turn into after the linker has done it's job. I know it should calculate the offset, but I'm a little confused about that.

以下面的代码为例,链接器部分完成后,e8 00 00 00 00应该如何?我如何得到这个答案?

As an example with the code below, after the linker part is done, how should the e8 00 00 00 00 be? And how do I get to that answer?

我正在使用此示例代码进行测试:(我正在尝试调用 moo)

I'm testing out with this sample code: (I'm trying to call moo)

Disassembly of section .text:

0000000000000000 <foo>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 45 fc                mov    -0x4(%rbp),%eax
   a:   83 e8 0a                sub    $0xa,%eax
   d:   5d                      pop    %rbp
   e:   c3                      retq   

000000000000000f <moo>:
   f:   55                      push   %rbp
  10:   48 89 e5                mov    %rsp,%rbp
  13:   89 7d fc                mov    %edi,-0x4(%rbp)
  16:   b8 01 00 00 00          mov    $0x1,%eax
  1b:   5d                      pop    %rbp
  1c:   c3                      retq   

000000000000001d <main>:
  1d:   55                      push   %rbp
  1e:   48 89 e5                mov    %rsp,%rbp
  21:   48 83 ec 10             sub    $0x10,%rsp
  25:   c7 45 fc 8e 0c 00 00    movl   $0xc8e,-0x4(%rbp)
  2c:   8b 45 fc                mov    -0x4(%rbp),%eax
  2f:   89 c7                   mov    %eax,%edi
  31:   e8 00 00 00 00          callq  36 <main+0x19>
            32: R_X86_64_PC32   moo-0x4
  36:   89 45 fc                mov    %eax,-0x4(%rbp)
  39:   b8 00 00 00 00          mov    $0x0,%eax
  3e:   c9                      leaveq 
  3f:   c3                      retq

推荐答案

使用 objdump -r 你已经打印了 Relocations 和你的反汇编 -d:

With objdump -r you have Relocations printed with your disassembly -d:

  31:   e8 00 00 00 00          callq  36 <main+0x19>
            32: R_X86_64_PC32   moo-0x4

ld-linux.so.2 加载器将重定位对象(在现代世界中,它甚至会将可执行文件重定位到随机地址)并用正确的地址填充重定位.

ld-linux.so.2 loader will relocate objects (in modern world it will relocate even executable to random address) and fill the relocations with correct address.

通过在 main 处添加断点并启动程序来检查 gdb(链接器在 main 函数启动之前工作):

Check with gdb by adding breakpoint at main and starting program (linker works before main function is started):

gdb ./program
(gdb) start
(gdb) disassemble main

如果您想在不重定位的情况下编译代码,请显示源代码和编译选项.

If you want to compile the code without relocations, show source code and compilation options.

这篇关于继续获取 e8 00 00 00 00 作为机器代码以在汇编中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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