I am trying to track this topic , which, unfortunately, does not completely solve my problem. The code I'm trying to run is as follows:
; File hello.asm section .data msg: db "Hello World!",0x0a,0 section .text global main extern printf main: push rbp mov rbp, rsp lea rdi, [msg] ; parameter 1 for printf xor eax, eax ; 0 floating point parameter call printf xor eax, eax ; returns 0 pop rbp ret
My system is debian stretch:
$ uname -a Linux <host> 4.8.0-1-amd64
I use yasm assembler as follows:
$ yasm -f elf64 -g dwarf2 hello.asm
Since my entry point in the above main source is with the final ret command, I assume I need to link to gcc , not ld -e main :
$ gcc -lc hello.o
However, I get the following error message:
/usr/bin/ld: hello.o: relocation R_X86_64_32 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 mentions something about recompiling with -fPIC , but this is a gcc compiler option, not a valid version of the yasm assembler. So I donβt know what to do here.
Just for testing, I tried linking to ld :
$ ld -e main -lc hello.o
which is successful, but I get the same error as in the above thread on startup:
$ ./a.out bash: ./a.out: No such file or directory
(after the stream response, I tried to compare the .so library specified in the ld binary with my system library, and they are both /lib64/ld-linux-x86-64.so.2 .)
I also tried replacing the main entry point with _start (forgetting the problem of correctly exiting the program at the moment) and the link with ld -lc hello.o , but I get the same error "There is no such file or directory" as before. I will continue to play with this, but thought I would ask too.
Any working suggestion (with main or _start , gcc or ld ) will be warmly appreciated.
EDIT: As Jim suggested, I added default rel at the top of hello.asm and I get another error message when linking to gcc (no change with ld -e main -lc )
$ gcc -lc hello.o /usr/bin/ld: hello.o: relocation R_X86_64_PC32 against symbol ` printf@ @GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: Bad value collect2: error: ld returned 1 exit status
EDIT2: This message refers to an error on debian stretch :
Linux: 4.8.0-1-amd64
After Jim's comment, I just tested the same code on debian jessie , which works fine with gcc -lc hello.o and the following versions:
Linux: 3.16.0-4-amd64
EDIT 3: Before Michael Petch's official answer: the problem is solved with gcc -static hello.o