AT&T 汇编入门 - 汇编使用printf
一般 C 语言的hello world 我们都是通过printf 打印的,而不是直接往stdout 写入字符串.
那我们现在就来试试如何在汇编中调用libc的printf 函数。
.section .data
msg:
.ascii "hello world!\n"
.section .text
.global _start # must be declared for linker
_start:
movq $msg, %rdi
call printf
movq $60, %rax # 'exit' syscall number
xor %rdi, %rdi # set rdi to zero
syscall
那让我们来编译一下这个汇编文件,出现如下错误。
➜ learning-asm git:(master) ✗ as hello3.s -o hello3.o
➜ learning-asm git:(master) ✗ ld hello3.o -o hello3
hello3.o: In function `_start':
(.text+0x8): undefined reference to `printf'
这是因为这个汇编的目标文件找不到printf函数,大家都知道printf函数是在libc里面。让我们来修改一下编译命令
➜ learning-asm git:(master) ✗ ld hello3.o -o hello3 -lc -I /lib64/ld-linux-x86-64.so.2
➜ learning-asm git:(master) ✗ ./hello3
hello world!
细心的朋友应该发现了,这次的代码我们使用的movq $msg, %rdi
来获取hello world 字符串的地址。不是使用之前的lea msg(%rip), %rdi
指令, 这个就跟代码位置无关性的选项有关了。如果我们开发位置无关的链接选项,那么就会出错。
➜ learning-asm git:(master) ✗ ld hello3.o -o hello3 -lc -I /lib64/ld-linux-x86-64.so.2 -pie
ld: hello3.o: relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
hello3.o: error adding symbols: Bad value
那么我们要如何修改呢?
.section .data
msg:
.ascii "hello world!\n"
.section .text
.global _start # must be declared for linker
_start:
lea msg(%rip), %rdi
call printf@PLT
movq $60, %rax # 'exit' syscall number
xor %rdi, %rdi # set rdi to zero
syscall
➜ learning-asm git:(master) ✗ ld hello2.o -o hello2 -lc -I /lib64/ld-linux-x86-64.so.2
➜ learning-asm git:(master) ✗ ./hello2
hello world!
lea msg(%rip), %rdi
多干了些什么事情?它实际上通过因为链接器是知道每个section的位置的,所以text 和 data section的偏移,链接器是知道的,那么根据当前指令的地址,rip寄存器(这个地址也是text段中的地址)就可以算出一个相对当前指令地址的偏移,那么就做到了代码位置无关的加载。