I am attempting to follow up on this thread which unfortunately does not quite solve my problem. The code I am 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 #1 SMP Debian 4.8.7-1 (2016-11-13) x86_64 GNU/Linux
I am using the yasm
assembler as follows:
$ yasm -f elf64 -g dwarf2 hello.asm
Because my entry point in the above source is main
with a final ret
instruction, I am guessing I need to link with gcc
rather than ld -e main
:
$ gcc -lc hello.o
However, I am getting 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 option of the assembler yasm
. So I don't know what to do here.
Just for the sake of testing, I have attempted to link with ld
:
$ ld -e main -lc hello.o
which is successful, but I obtain the same error as mentioned in the above thread when running:
$ ./a.out
bash: ./a.out: No such file or directory # The file *is* there ...
(following the answer of the thread, I have attempted to compare the .so
library mentioned in the ld
binary file with my system's library, and they are both /lib64/ld-linux-x86-64.so.2
.)
I have also attempted to replace the main
entry point with _start
(forgetting the issue of properly exiting the program for now) and link with ld -lc hello.o
but I get the same error 'No such file or directory' as before. I will continue playing with this, but thought I would ask also.
Any working suggestion (with main
or _start
, gcc
or ld
) would be warmly appreciated.
EDIT:
As suggested by Jim I have added default rel
at the top of hello.asm
and I obtain a different error message when linking with 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 post relates to a failure on debian stretch
:
Linux: 4.8.0-1-amd64 #1 SMP Debian 4.8.7-1 (2016-11-13) x86_64 GNU/Linux
yasm: 1.3.0
gcc: (Debian 6.2.1-5) 6.2.1 20161124
ld: GNU ld (GNU Binutils for Debian) 2.27.51.20161220
Following on Jim's comment I have just tested the same code on debian jessie
which works perfectly fine with gcc -lc hello.o
and the following versions:
Linux: 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
yasm: 1.2.0
gcc: (Debian 4.9.2-10) 4.9.2
ld: GNU ld (GNU Binutils for Debian) 2.25
EDIT 3: Pending a formal answer from Michael Petch: issue resolved with gcc -static hello.o
GCC in Debian Stretch defaults to building position independent executables, to build executable linked to specific address as is traditional pass -no-pie
to GCC.
Alternatively specify the correct relocation type, I don't know how to do this in yasm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With