如何配置gcc默认使用-no-pie?

时间:2018-03-04 22:39:09

标签: gcc configuration ld gas position-independent-code

我想在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。

2 个答案:

答案 0 :(得分:3)

答案 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标签的毫无意义的指令。)