0

This is the error I'm getting while doing a code on assembly language in Linux Ubuntu. Can anyone help me resolve the error?

This is the error that's coming when I use the command ld -o quadratic quadratic.o

The image of Error that's coming.

the code to my asm file is this:

https://github.com/vedantdawange/ASM-Files/blob/main/quadratic.asm

  • That's not an error, it's a warning. That's why you can assemble+link a `.asm` containing just a single instruction you want to single-step in a debugger. Like `cat > foo.asm` / `mov rax, 1234` / control-d / nasm / ld / gdb ./a.out – Peter Cordes May 04 '21 at 17:05

1 Answers1

2

ld by itself links no libraries or startup code. It's suitable for a program where you use _start as an entry point and do I/O via direct calls to the kernel instead of standard C library functions. But your program uses main as its entry point, so it expects to be called by C startup code, and it calls library functions like printf. Hence you should link it like a C program:

gcc -no-pie -o quadratic quadratic.o

The -no-pie option is needed because your code makes absolute references to static data, e.g. fld qword[b]. gcc by default assumes you want to build a position-independent executable, which can't do that; you'd need to write fld qword[rel b] to produce an rip-relative effective address. So -no-pie asks gcc to link a non-position-independent executable. See Why use RIP-relative addressing in NASM? for more on this.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • Still dosent seem to work. This error is coming relocation R_X86_64_32S against `.bss' can not be used when making a PIE object; recompile with -fPIE collect2: error: ld returned 1 exit status – VEDANT DAWANGE May 04 '21 at 16:14
  • 1
    Perhaps `default rel` (before any of the memory accesses) would help. – ecm May 04 '21 at 16:17
  • @VEDANTDAWANGE: You need `-no-pie` for your code as it stands. See edit. – Nate Eldredge May 04 '21 at 16:20
  • Thank you! gcc -no-pie -o quadratic quadratic.o works! You are a lifesaver. Thank you so much. – VEDANT DAWANGE May 04 '21 at 16:27
  • 1
    @VEDANTDAWANGE: You should always use `default rel`; RIP-relative access is more efficient. You might need `-no-pie` anyway, though, if you call any library functions without manually indirecting through the PLT or GOT. Also, normally there's not much reason to use legacy x87 for floating point on x86-64; SSE2 for `movsd` / `addsd` etc. is guaranteed to be available. x87 is mostly only useful when you *need* more precision than 64-bit double, but 80-bit x87 is enough. – Peter Cordes May 04 '21 at 17:07
  • 1
    @ecm: Surprisingly, `default rel` affects memory references that appear before it, at least last I checked. IDK if that's intended or a NASM bug, but you can put it at the end of a file. – Peter Cordes May 04 '21 at 17:09