Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembling with GCC causes weird relocation error with regards to .data

This is an issue that didn't used to ever occur. I'm pretty convinced it's probably an issue with my package repos (I recently reinstalled my Arch system and this has only just started happening).

I wrote a small hello world in x86_64:

.data
str:    .asciz  "Test"

.text
.globl main
main:
    sub $8, %rsp
    mov $str, %rdi
    call puts
    add $8, %rsp
    ret

and then I attempt to assembly and link using GCC - like I have done many times in the past - with, simply:

gcc test.s -o test

and then this error is outputted:

/usr/bin/ld: /tmp/ccAKVV4D.o: relocation R_X86_64_32S against `.data' 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

This error has never occured for me ever. I've tried to fix the issue by googling the same error message but it comes up with things that are so specific whereas I'd consider this a general issue. I've tried reinstalling base-devel and the entire GCC toolchain. I dunno what else I can do (please don't suggest using nasm, that's heresy).

I'd like to think I'm missing something obvious but I've used GCC for my assembly needs for a long time.

like image 618
oldjohn1994 Avatar asked Sep 08 '17 19:09

oldjohn1994


1 Answers

The way to get around this error is to generate a no-pie (Non Position Independent executable) executable :

gcc -no-pie test.s -o test

The reason for this behaviour is as explained by @Ped7g :

Debian switched to PIC/PIE binaries in 64-bits mode & GCC in your case is trying to link your object as PIC, but it will encounter absolute address in mov $str, %rdi.

like image 115
Karim Manaouil Avatar answered Nov 20 '22 07:11

Karim Manaouil