title: GCC promotes weird relocation R_X86_64_32S error but not in manually linking
body: <p>I'm trying to invoke <a href="https://elixir.bootlin.com/linux/latest/source/include/linux/syscalls.h#L499" rel="nofollow noreferrer"><code>sys_write</code></a> via the <a href="https://www.felixcloutier.com/x86/syscall" rel="nofollow noreferrer"><code>syscall</code></a> instruction under Linux environment on an x86_64 machine with inline assembly in C files. To achieve this, I wrote the following code:</p>
  <pre><code>[tjm@ArchPad poc]$ cat poc.c 
  const char S[] = &quot;abcd\n&quot;;
  
  int main() {
      __asm __volatile(&quot; \
              movq $1, %%rax; \
              movq $1, %%rdi; \
              movq %0, %%rsi; \
              movq $5, %%rdx; \
              syscall;&quot;
              :
              : &quot;i&quot;(S)
              );
      return 0;
  }
  </code></pre>
  <p>While my compiling it with GCC, the following error promoted:</p>
  <pre><code>[tjm@ArchPad poc]$ gcc -O0 poc.c -o poc
  /usr/bin/ld: /tmp/ccWYxmSb.o: relocation R_X86_64_32S against symbol `S' can not be used when making a PIE object; recompile with -fPIE
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>Then I added <code>-fPIE</code> to the compiler command. Unfortunately, nothing changed:</p>
  <pre><code>[tjm@ArchPad poc]$ gcc -fPIE -O0 poc.c -o poc 
  /usr/bin/ld: /tmp/ccdK36lx.o: relocation R_X86_64_32S against symbol `S' can not be used when making a PIE object; recompile with -fPIE
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>This is so weird that it made me curious to compile and link it manually:</p>
  <pre><code>[tjm@ArchPad poc]$ gcc -fPIE -O0 poc.c -S
  [tjm@ArchPad poc]$ as -O0 poc.s -o poc.o
  [tjm@ArchPad poc]$ ld -O0 -melf_x86_64 --dynamic-linker /usr/lib/ld-linux-x86-64.so.2 /usr/lib/crt{1,i,n}.o -lc poc.o -o poc
  </code></pre>
  <p>Surprisingly, no error occurred during the attempt above. I then tested the executable's functionality, which seems working:</p>
  <pre><code>[tjm@ArchPad poc]$ ./poc 
  abcd
  </code></pre>
  <p>Any ideas of why this happens and how to prevent GCC from promoting the error above?</p>