我想在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。
答案 0 :(得分:3)
我想只是不要使用--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。
答案 1 :(得分:0)
为此,您必须重新编译gcc以禁用默认PIE。否则,每次要编译与位置相关的汇编代码时,您都需要function Student() {
return (
<div >
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">
</div>
);
}
export default Student;
。
但是,仅就您提供的示例而言,更好的方法是使用诸如-no-pie
之类的相对寻址。
相对寻址允许PIE正常运行。
我将您的内容修改为:(请参见label_name(%rip)
行)
leaq
(我之所以添加.global _start
.text
_start:
movq $1, %rax
movq $1, %rdi
leaq msg(%rip), %rsi
movq $13, %rdx
syscall
movq $60, %rax
xorq %rdi, %rdi
syscall
.section .rodata
msg:
.ascii "Hello World!\n"
只是因为通常应将其放在rodata节中。您的版本运行良好,但是.section .rodata
的输出包含来自objdump -d
标签的毫无意义的指令。)