0

Code:

new1.asm-

section .text
    global main
    extern printf

main:

    mov rax, 5
    mov rbx, 55
    add rax,rbx
    PUSH rax
    PUSH message
    call printf WRT ..plt
    add esp,8
    ret

message db "Sum = %08X", 10, 0

Command:

nasm -f elf64 new1.asm -o new1.o
Above runs without error
gcc -fpie -m64 -o new1 new1.o

Error:

/usr/bin/ld: new1.o: relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status

System Specs:

Ubuntu 21.04
Kernel: 5.11.0-34-generic
Machine: x86_64
Processor: x86_64
Operating system : GNU/Linux

I have also tried to use what is discussed in this
What mistake am I making and how do I fix this.
Also I cannot do -m32 because it returns cannot find -lgcc and I want to use 64 bit registers

Edit 1:

After reading this NASM printf print 64 bit double segfaults I changed my code to-

section .text
    global main
    extern printf

main:

    mov rax, 5
    mov rbx, 55
    add rax,rbx
    mov RDI,rax
    call printf WRT ..plt
    add rsp,8
    ret

message db "Sum = %08X", 10, 0

It compiles and links but now I get segmentation fault. I compiled with gcc -fpic -m64 -o new1 new1.o this time

Hello-World
  • 72
  • 10
  • 1
    Where does this code come from? Shouldn't you pass `printf`'s arguments through `rdi` and `rsi`? – Michael Sep 21 '21 at 11:50
  • Even if that did link, `add esp,8` truncates RSP to 32-bit so the `ret` would crash. (If you hadn't already for leaving printf's first 6 args (RDI, RSI, RDX, RCX, R8, R9) unset.) – Peter Cordes Sep 21 '21 at 12:08
  • Anyway, it's not the `call` that's the problem (you're correctly calling through the PLT), it's the `push message`. You can verify that by removing the `call` instruction to make a [mcve] of *just* the code that won't link. It wouldn't be runnable, but bisecting your problem to see what the tools are choking on is a useful technique. Anyway, `push` isn't part of a correct way to call printf on x86-64 with less than 7 args. – Peter Cordes Sep 21 '21 at 17:51
  • Also a duplicate of [How to push label in stack in a relocatable shared library which its objects should be compiled with -fPIC?](https://stackoverflow.com/q/51542401) except you don't want `push`. – Peter Cordes Sep 21 '21 at 18:23
  • if i remove push and use `lea rax, [rel rax]` then it compiles and links but it gives `segmentation fault(core dumped)`. Even if i change the esp line to `add rsp, 8` I still get segmentation fault – Hello-World Sep 22 '21 at 12:54
  • You have other bugs too, like in this case [glibc scanf Segmentation faults when called from a function that doesn't align RSP](https://stackoverflow.com/q/51070716) not aligning RSP by 16. Your current code, as well as not aligning the stack, is passing an integer instead of a valid `char*` as the first arg (in RDI) – Peter Cordes Sep 22 '21 at 13:03
  • You might want to look at C compiler output for an example (GCC Intel syntax has some differences from NASM, but the general idea of what instructions it uses will still be there.) https://godbolt.org/z/PE5Ybh38c / [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Sep 22 '21 at 13:05
  • Also, wait a minute, `lea rax, [rel rax]`? Surprised that even assembles (using `rel` on a register, not a symbol), and you're definitely not putting a pointer to `message` into RDI. – Peter Cordes Sep 22 '21 at 13:07
  • oh ok a bunch of errors then. – Hello-World Sep 22 '21 at 13:13

0 Answers0