title: How to force LD to link a PIC assembly code?
body: <p>So I want to assemble this very simple code:</p>
  
  <pre><code>extern printf
  
  Segment .data
  string: 
      db "foo", 0
  
  Segment .text
  extern printf
  
  
  global main
  main:
  
      push rbp
      xor rax, rax
      mov rdi, string
  
      call printf 
      pop rbp
      ret
  </code></pre>
  
  <p>but I wanted to make it a PIE so I changed </p>
  
  <blockquote>
    <p>mov rdi, string</p>
  </blockquote>
  
  <p>to</p>
  
  <blockquote>
    <p>lea rdi, [rel+string]</p>
  </blockquote>
  
  <p>but after doing that I get the following errors from ld/gcc:</p>
  
  <blockquote>
    <p><strong>relocation R_X86_64_PC32 against symbol `printf@GLIBC_2.2.5' can not</strong>
    <strong>be used when making a PIE object; recompile with -fPIC /usr/bin/ld:</strong>
    <strong>final link failed: bad value</strong></p>
  </blockquote>
  
  <p>I'm compiling and assembling this with:
  <em>nasm -f elf64 bar.s -o bar.o; gcc -fPIC bar.o -o bar</em></p>
  
  <p>Why am  I getting this error? It compiles just fine with the -no-pie flag</p>