我想在Linux上编译以下程序:
.global _start
.text
_start:
mov $1, %rax
mov $1, %rdi
mov $msg, %rsi
mov $13, %rdx
syscall
mov $60, %rax
xor %rdi, %rdi
syscall
msg:
.ascii "Hello World!\n"
但是,它给我以下链接器错误:
$ gcc -nostdlib hello.s
/usr/bin/ld: /tmp/ccMNQrOF.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
我认为它不起作用的原因是因为gcc默认使用-pie
生成共享对象。因此,使用-no-pie
修复它:
$ gcc -no-pie -nostdlib hello.s
$ ./a.out
Hello World!
如何配置gcc默认使用-no-pie
?我正在使用Arch Linux。
我想只是不要用--enable-default-pie
配置gcc。
请参阅此博客文章:http://nanxiao.me/en/gccs-enable-enable-default-pie-option-make-you-stuck-at-relocation-r_x86_64_32s-against-error/,以及默认启用饼图的Arch补丁:https://git.archlinux.org/svntogit/packages.git/commit/trunk?h=packages/gcc&id=5936710c764016ce306f9cb975056e5b7605a65b。