0

I am learing assembly language recently and I want to run 64 bit assembly code file. but when I try assembling and linking my file with gcc -o test1 test1.s command, I encounter an error:

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

This is the code that I wrote.

        .section ".rodata"

printFormat:
     .asciz "%d\n"

### --------------------------------------------------------------------

        .section ".data"

### --------------------------------------------------------------------

        .section ".bss"

### --------------------------------------------------------------------

        .section ".text"



    .text
    .globl  main
    .type   main, @function


main:

    movq  $0x1111222233334444, %rax
    pushq %rax
    pushq $printFormat
    call printf
    addq $16, %rsp

    movq $0, %rax
    ret

I can assemble/link the file if I erase pushq $printFormat in the main function but the code written above makes error. How can I fix the problem?

  • 1
    64 bit calling convention does not use the stack to pass arguments (mostly, see ABI docs for details) so even if you fix the linker error the code will not work. TL;DR: what you want is `movq $0x1111222233334444, %rsi; lea format(%rip), %rdi; xor %eax, %eax; call printf`. the `addq $16, %rsp` should go, but note you should also align the stack by e.g. `push %rbp` at the beginning of `main` and don't forget to `pop` that. – Jester Mar 01 '23 at 15:59

0 Answers0