6

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

Sven Williamson
  • 1,094
  • 1
  • 10
  • 19
  • 1
    Your code actually works for me (on a different distro). Your problem seems to be related to the relocation type of `hello.o`. I would suggest adding the directive `default rel` to the top of `hello.asm` and see if that works or provides a different error. Are you deliberately trying to use R_X86_64_32 relocation? – JimD. Jan 25 '17 at 07:32
  • @JimD. thank you very much for your help. I have edited my post to indicate new error message. – Sven Williamson Jan 25 '17 at 07:43
  • 2
    What happens if you build statically with `gcc -static hello.o` – Michael Petch Jan 25 '17 at 07:54
  • @MichaelPetch You got it !!! `gcc -lc -static hello.o` works perfectly (on the `debian stretch` machine). Incidentally, it also works on `debian jessie`. If you want to write an answer, I will gladly validate it. Any insight as to what may be happening is also very welcome (but i don't want to take too much of your time). Thank you very much !!! – Sven Williamson Jan 25 '17 at 08:04
  • OK, so now we made it past the data section, but have trouble with the text relocation. I like @MichaelPetch suggestion of trying an explicitly static build. But why are you explicitly adding `-lc` when linking? Maybe try just `gcc hello.o` and see if you get a different result? – JimD. Jan 25 '17 at 08:06
  • @JimD.good point the `-lc` option is unnecessary with `gcc` (got it as a habit from `ld`). However `gcc hello.o` leads to the same errors (with or without `default rel` on top of `hello.asm`), while `gcc -static hello.o` is successful. – Sven Williamson Jan 25 '17 at 08:12

1 Answers1

8

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.

Timothy Baldwin
  • 3,551
  • 1
  • 14
  • 23