2

I have this simple program to compute the square root of a floating point number

global main
extern printf
section .data
        float_t db '%f',0x0
        val dq 123.45
        res dq 0x0
section .text
main:
        fld qword[val]
        fsqrt
        fst qword[res]
        xor rax,rax
        mov rdi, float_t
        mov rsi, [res]
        call printf
        mov rax,60
        mov rdi,0
        syscall

I assembled it by

$  nasm -f elf64 fpu.asm -o fpu.o

and then tried to link to glibc with gcc as

$  gcc fpu.o -o fpu

GCC complains with:

/usr/bin/ld: fpu.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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Sigma
  • 742
  • 2
  • 9
  • 24
  • What OS (Linux) and version? That error suggests the command line you are using to build the executable (using GCC) is using `-shared` option. You could try building with the `-static` option. I suspect you are using a Linux distro that is based on Ubuntu 16.10 and higher or a recent Debian (a testing version). – Michael Petch Mar 28 '17 at 15:50
  • @MichaelPetch I am using debian sid. – Sigma Mar 29 '17 at 18:23
  • Yeah I suspected the possibility of a testing or unstable release. Ubuntu 16.10 and higher (as well as Debian testing and sid) build position independent objects and executable by default (on 64-bit platforms). I would expect building with `-static` may work. – Michael Petch Mar 29 '17 at 18:38
  • @MichaelPetch, This should be an (accepted) answer. – Neowizard Nov 27 '18 at 07:49
  • There are a boatload of things wrong with this example (which you didn't get a chance to debug yet because you got stuck on linking.) `printf` takes a `double` by *value* in an XMM reg (with AL=1), not by pointer. – Peter Cordes Feb 19 '19 at 07:04

1 Answers1

0

As stated by Michael Petch in the comments above,

I would expect building with -static may work

I had the same problem and this fixed it.

Nuclear_Man_D
  • 142
  • 11
  • `-no-pie` would be a better choice than `-static`. – Peter Cordes Feb 19 '19 at 07:02
  • @PeterCordes I will have to check that out. Why is that? – Nuclear_Man_D Feb 21 '19 at 17:37
  • Because you don't need to statically link libc, bloating the executable and making it harder to find your own code if you look at `objdump -drwC -Mintel` output. `-no-pie` is sufficient to get the linker to automatically generate PLT stubs and replace `call printf` with `call printf@plt`. IDK why it doesn't do that for PIE executables; maybe it's a side-effect of the fact that PIE executables are sort of shared libraries. See also [32-bit absolute addresses no longer allowed in x86-64 Linux?](//stackoverflow.com/q/43367427) – Peter Cordes Feb 21 '19 at 17:49