AT&T 汇编入门 - 汇编使用printf

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段中的地址)就可以算出一个相对当前指令地址的偏移,那么就做到了代码位置无关的加载。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值