2

https://cs.lmu.edu/~ray/notes/nasmtutorial/

I got the following error when I try to compile fib.asm on Linux. Could you let me know how to compile it? Thanks.

$ nasm -felf64 fib.asm && gcc fib.o && ./a.out
/usr/bin/ld: fib.o: warning: relocation in read-only section `.text'
/usr/bin/ld: fib.o: relocation R_X86_64_PC32 against symbol `printf@@GLIBC_2.2.5' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 3
    Provide `-no-pie` to the `gcc` command to fix this problem. Modern Linux systems default to PIE which many tutorials unfortunately don't cover. Turning off PIE makes the tutorial work again. – fuz Jan 27 '21 at 03:03
  • 1
    You're right, [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427) is more about data accesses like `[symbol + rdi]`, not about the fact that function calls don't implicitly turn `call foo` into `call foo wrt ..plt` for you when linking a PIE, only a normal executable. I picked that duplicate first because it's about PIEs and how that's now the default (and it mentions `-no-pie`), but the other Q&As I later added to the top of the duplicate list are much more exact fits, and discuss exactly how to call functions in shared libraries like `libc`. – Peter Cordes Jan 27 '21 at 03:29
  • My answer on [Can't call C standard library function on 64-bit Linux from assembly (yasm) code](https://stackoverflow.com/a/52131094) and Ciro's answer on [How to print a number in assembly NASM?](https://stackoverflow.com/a/32853546) both clearly explain what's going on, and that `-no-pie` is the easy way, or how to write NASM syntax that works in a PIE. My answer also tries to explain *why* it doesn't work when you do it this way, but Ciro's is more of a simple recipe that's easier to follow. Both are useful for different reasons, but each sufficient on its own, so I linked both duplicates. – Peter Cordes Jan 27 '21 at 03:44
  • But why the webpage clearly say `nasm -felf64 fib.asm && gcc fib.o && ./a.out` works. Does it work in some cases that you are not aware of? How can you be sure your answers cover 100% of the cases? – user1424739 Jan 27 '21 at 03:46
  • 1
    Because it's an old tutorial. As [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427) explained, Linux distros changes to making `-fPIE -pie` the default in about 2017, where it wasn't before, to enable ASLR for sections other than the stack. This fully explains 100% of what you're seeing, including that exact error message, and why the tutorial didn't use that option or PIE-compatible asm. (And that explanation is part of why I originally linked that as one of the duplicates.) – Peter Cordes Jan 27 '21 at 03:53
  • Could you just show the commands that can compile and liink the nasm file? the link that you post is not minimal. I first need a command that is working. – user1424739 Jan 27 '21 at 03:57
  • BTW, that tutorial looks fairly good, but randomly uses `lea rdi, [rel message]` (good) in some examples vs. `mov rdi, message` (inefficient) in others, including otherwise-equivalent Linux vs. MacOS `puts` examples which makes it look like one is the Linux way, one is the MacOS way. That's not true. [How to load address of function or label into register](https://stackoverflow.com/q/57212012) – Peter Cordes Jan 27 '21 at 03:57
  • Come on dude, if you just want something that works, have you not yet tried fuz's comment to use `gcc -no-pie fib.o`? That will work exactly the way it did when the tutorial was written, letting the code from those tutorials work. – Peter Cordes Jan 27 '21 at 03:59
  • 1
    OK. It works. I think you should just post `gcc -no-pie -o fib.exe fib.o` as an answer. My question is "how ..."? The answer is just a command that works. Instead, you posted too many previous answers that do not directly answer my questions. If you really want to link to those previous questions, you should first provide a working command then provide these previous answers to explain why the command works, rather than just provide links to those previous questions which does not directly answer my question. We are just wasting too much text on this simple question. – user1424739 Jan 27 '21 at 04:03

0 Answers0