X86 64-bit Linux Link Hello Hello

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 #1 SMP Debian 4.8.7-1 (2016-11-13) x86_64 GNU/Linux 

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 # The file *is* there ... 

(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 #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 

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 #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: Before Michael Petch's official answer: the problem is solved with gcc -static hello.o

+5
source share
1 answer

GCC in Debian Stretch by default creates independent position executables to create an executable file associated with a specific address, like the traditional -no-pie pass in GCC.

As an alternative, specify the correct type of movement, I do not know how to do this in yasm.

+6
source

Source: https://habr.com/ru/post/1263303/


All Articles