X64 Linux汇编初试

今天小小地试验了下x64 Linux汇编编程,有很多疑问。

下面是一个输出Hello world的汇编小程序:

.section .data
msg:
        .string "Hello, world.\n"

.section .text
.global main
main:
        leaq msg(%rip), %rdi
        call puts@PLT

        movq $0, %rax
        ret                                     

编译运行:

madngyun@madngyun-VirtualBox:~$ gcc x64.s -o x64
madngyun@madngyun-VirtualBox:~$ ./x64
Hello, world.

有两个地方不明白:

1. 如果把leaq msg(%rip), %rdi,改成movq $msg, %rdi,编译报错:

/usr/bin/x86_64-linux-gnu-ld: /tmp/cc9gs8u7.o: relocation R_X86_64_32S against `.data' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: 最后的链结失败: 输出不可表示的节
collect2: error: ld returned 1 exit status

不懂什么意思。

2. 如果把call puts#PLT,改成call puts,编译成功,但执行的时候出错:

madngyun@madngyun-VirtualBox:~$ ./x64
./x64: Symbol `puts' causes overflow in R_X86_64_PC32 relocation
段错误 (核心已转储)


如有哪位大佬知道原因,烦请留言指教。

猜你喜欢

转载自blog.csdn.net/missgya/article/details/82888620