1

My simple program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char argmore[]="Hello world";
int main ( int argc , char** argv) {
    /////// printf 
    asm ("movq $1, %rax\n"
         "movq $1, %rdi\n"
         "movq $argmore, %rsi\n"
         "movq $11, %rdx\n"
         "syscall");
    /////// exit
    asm ("movq $60, %rax\n"
         "movq $1, %rdi\n"
         "syscall");
    return 0;
}

When I compile this program as:

gcc -no-pie -o test test.c

It works correctly, prints "hello word" and exits successfully. But when I compile it as:

gcc -o test test.c

I receive this error:

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

I don't want to compile my program with -no-pie option. What should I do? How can I change my program to compile correctly without -no-pie option?

rturrado
  • 7,699
  • 6
  • 42
  • 62
taranom
  • 153
  • 2
  • 12
  • Why is this tagged C++ when the code and compiler are C? – stark Jan 01 '21 at 16:28
  • Have you tried `gcc -o test -fPIE test.c` as suggested by the linker error message? – rturrado Jan 01 '21 at 16:35
  • 1
    @rturrado yes. i get the same error – taranom Jan 01 '21 at 16:40
  • 3
    Note that your code is incorrect because you clobber registers without marking them in the clobber list. I believe a simple `lea argmore(%rip), %rsi` should address the issue you ask about. – fuz Jan 01 '21 at 16:56
  • @fuz yeeeeeeeees. it is true. my problem is solved. thank you. thank you. thank you. please send your comment as response so i can give you score – taranom Jan 01 '21 at 17:01
  • 4
    @taranom No, your problem is not solved. There are still clobbers missing and your code will fail spectacularly later on. – fuz Jan 01 '21 at 17:15
  • 1
    To see how to properly make a system call from inline assembly, including safe use of clobbers, see https://stackoverflow.com/questions/9506353/how-to-invoke-a-system-call-via-syscall-or-sysenter-in-inline-assembly/9508738#9508738 – Nate Eldredge Jan 01 '21 at 18:50

0 Answers0