在64位的Linux系统使用gcc的-m32选项编译32位的程序得到了多余的代码(多余指令call和add)、有多余的.text.__x86.get_pc_thunk.ax

问题解决于该网站


首先讲一下问题是怎么发生的:

最近在学计算机系统,用到的教材是32位的Linux系统,而在我的64位的Ubuntu上,我想以gcc编译出32位的程序并查看其汇编代码,于是百度查了下,应在gcc中使用-m32的选项

test.c:
int add(int x, int y) {
    return x+y;
}
int caller() {
    int t1 = 125;
    int t2 = 80;
    int sum = add(t1, t2);
    return sum;
}

于是我便使用以下两个命令来得到test.c的汇编代码:

gcc -c test.c -m32
objdump -d test.o

得到的反汇编得到的代码为:

$ gcc -c test.c -m32
$ objdump -d test.o

test.o:     文件格式 elf32-i386


Disassembly of section .text:

00000000 <add>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   e8 fc ff ff ff          call   4 <add+0x4>			# 多余代码
   8:   05 01 00 00 00          add    $0x1,%eax			# 多余代码
   d:   8b 55 08                mov    0x8(%ebp),%edx
  10:   8b 45 0c                mov    0xc(%ebp),%eax
  13:   01 d0                   add    %edx,%eax
  15:   5d                      pop    %ebp
  16:   c3                      ret    

00000017 <caller>:
  17:   55                      push   %ebp
  18:   89 e5                   mov    %esp,%ebp
  1a:   83 ec 10                sub    $0x10,%esp
  1d:   e8 fc ff ff ff          call   1e <caller+0x7>		# 多余代码
  22:   05 01 00 00 00          add    $0x1,%eax			# 多余代码
  27:   c7 45 f4 7d 00 00 00    movl   $0x7d,-0xc(%ebp)
  2e:   c7 45 f8 50 00 00 00    movl   $0x50,-0x8(%ebp)
  35:   ff 75 f8                pushl  -0x8(%ebp)        	
  38:   ff 75 f4                pushl  -0xc(%ebp)	
  3b:   e8 fc ff ff ff          call   3c <caller+0x25>
  40:   83 c4 08                add    $0x8,%esp	
  43:   89 45 fc                mov    %eax,-0x4(%ebp)
  46:   8b 45 fc                mov    -0x4(%ebp),%eax
  49:   c9                      leave  
  4a:   c3                      ret    

# 下面的啥玩意儿?
Disassembly of section .text.__x86.get_pc_thunk.ax:

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

发现有很多不符合预期的明显是多余的汇编代码,于是搜索.text.__x86.get_pc_thunk.ax找到了解决办法

解决办法:

在gcc命令中使用-fno-pie选项

gcc -c test.c -m32 -fno-pie

objdump -d test.o

于是去掉了多余代码,得到的了符合预期的汇编代码:

$ gcc -c test.c -m32 -fno-pie
$ objdump -d test.o

test.o:     文件格式 elf32-i386


Disassembly of section .text:

00000000 <add>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 55 08                mov    0x8(%ebp),%edx
   6:   8b 45 0c                mov    0xc(%ebp),%eax
   9:   01 d0                   add    %edx,%eax
   b:   5d                      pop    %ebp
   c:   c3                      ret    

0000000d <caller>:
   d:   55                      push   %ebp
   e:   89 e5                   mov    %esp,%ebp
  10:   83 ec 10                sub    $0x10,%esp
  13:   c7 45 f4 7d 00 00 00    movl   $0x7d,-0xc(%ebp)
  1a:   c7 45 f8 50 00 00 00    movl   $0x50,-0x8(%ebp)
  21:   ff 75 f8                pushl  -0x8(%ebp)
  24:   ff 75 f4                pushl  -0xc(%ebp)
  27:   e8 fc ff ff ff          call   28 <caller+0x1b>
  2c:   83 c4 08                add    $0x8,%esp
  2f:   89 45 fc                mov    %eax,-0x4(%ebp)
  32:   8b 45 fc                mov    -0x4(%ebp),%eax
  35:   c9                      leave  
  36:   c3                      ret

具体原因不想深究,想深究看开头的网站或者搜索.text.__x86.get_pc_thunk.ax去了解

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值