【发布时间】:2015-04-08 12:36:58
【问题描述】:
我用 C 写了这个 Hello World:
#include<stdio.h>
int main() {
printf("Hello world !\n");
return 0;
}
用gcc编译成汇编代码 我明白了:
.file "file.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Hello world !"
.section .text.unlikely,"ax",@progbits
.LCOLDB1:
.section .text.startup,"ax",@progbits
.LHOTB1:
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB11:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %edi
call puts
xorl %eax, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE11:
.size main, .-main
.section .text.unlikely
.LCOLDE1:
.section .text.startup
.LHOTE1:
.ident "GCC: (GNU) 4.9.2 20150304 (prerelease)"
.section .note.GNU-stack,"",@progbits
这里没问题。但是现在,我想将汇编代码与 objdump 反汇编的代码进行比较:
对于主要功能,我得到了这个:
0000000000000000 <main>:
0: 48 83 ec 08 sub $0x8,%rsp
4: bf 00 00 00 00 mov $0x0,%edi
5: R_X86_64_32 .rodata.str1.1
9: e8 00 00 00 00 callq e <main+0xe>
a: R_X86_64_PC32 puts-0x4
e: 31 c0 xor %eax,%eax
10: 48 83 c4 08 add $0x8,%rsp
14: c3 retq
我不明白两件事:
为什么将edi上的数字0移到加载字符串“Hello world”?
此外,指令callq调用地址e。但是地址e 处的指令不是函数puts 而是xor。那么真实地址是什么?
【问题讨论】:
-
发生这种情况是因为 relocation,这是我写的教程:stackoverflow.com/a/30507725/895245
标签: gcc assembly disassembly