SRK
SRK

Reputation: 53

x86-64 assembly program linking fails with gcc

From seyfarth's book:

        segment .data
a       dw      175

b       dw      4097

        segment .text
        global  main
main:

        mov     rax, [a]    ; mov a (175)into rax

        add     rax, [b]    ; add b to rax
        xor     rax, rax
        ret

It fails to link using the commands given in seyfarth's book:

yasm -P ebe.inc -Worphan-labels -f elf64 -g dwarf2 -l add1.lst add1.asm
gcc -g -o add1 add1.o
/usr/bin/ld: add1.o: relocation R_X86_64_32 against `.data' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Makefile:20: recipe for target 'add1' failed
make: *** [add1] Error 1

If I replace main with _start and then assemble using yasm and then link using ld it works.

Upvotes: 2

Views: 437

Answers (1)

fuz
fuz

Reputation: 93044

Link with -no-pie.

PIE is a rather recent security feature which requires you to write position-independent code. Your code is not position-independent, so your code can't link. Turning the feature off is the best solution for a beginner. Alternatively, you can also make your code position-independent by using appropriate addressing modes:

mov rax, [rel a]
add rax, [rel b]

Upvotes: 3

Related Questions