为什么gcc会生成没有标志-fno-pie的奇怪代码? [英] Why does gcc generates strange code without flag -fno-pie?

查看:575
本文介绍了为什么gcc会生成没有标志-fno-pie的奇怪代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在带有标志-fno-pie且不包含标志的gcc中编译伪函数.

I am trying to compile dummy function in gcc with flag -fno-pie and without.

void dummy_test_entrypoint() { }

当我在没有标志的情况下进行编译.

When i compile without the flag.

gcc -m32 -ffreestanding -c test.c -o test.o

我得到以下反汇编代码.

I get the following disassembled code.

00000000 <dummy_test_entrypoint>:
0:  55                      push   ebp
1:  89 e5                   mov    ebp,esp
3:  e8 fc ff ff ff          call   4 <dummy_test_entrypoint+0x4>
8:  05 01 00 00 00          add    eax,0x1
d:  90                      nop
e:  5d                      pop    ebp
f:  c3                      ret    

当我用标志编译时.

00000000 <dummy_test_entrypoint>:
0:  55                      push   ebp
1:  89 e5                   mov    ebp,esp
3:  90                      nop
4:  5d                      pop    ebp
5:  c3                      ret 

我的问题.

这是什么?

3:  e8 fc ff ff ff          call   4 <dummy_test_entrypoint+0x4>
8:  05 01 00 00 00          add    eax,0x1

推荐答案

您在没有--reloc标志的情况下反汇编了目标文件,因此输出具有误导性.使用--reloc标志,您将看到:

You disassembled the object file without the --reloc flag, so the output is misleading. With the --reloc flag, you'll see this:

   3:   e8 fc ff ff ff          call   4 <dummy_test_entrypoint+0x4>
            4: R_386_PC32   __x86.get_pc_thunk.ax
   8:   05 01 00 00 00          add    $0x1,%eax
            9: R_386_GOTPC  _GLOBAL_OFFSET_TABLE_

子例程如下所示:

00000000 <__x86.get_pc_thunk.ax>:
   0:   8b 04 24                mov    (%esp),%eax
   3:   c3                      ret    

此函数将GOT指针加载到%eax中,以防函数需要引用全局数据.该函数不包含此类引用,但是由于您在未经优化的情况下编译了代码,因此GCC并未删除无效代码.

This construct loads the GOT pointer into %eax, in case the function needs to reference global data. The function does not contain such a reference, but because you compiled the code without optimization, GCC did not remove the dead code.

这篇关于为什么gcc会生成没有标志-fno-pie的奇怪代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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