title: Generating position independent code with gas (-fPIC)
body: <p>I try to create a shared library on x86-64 but fail. The problem boils down to the following code (please don't mind, that it does not make a lot of sense):</p>
  
  <pre><code>.section .data
  newline:
      .ascii "\n"
  
  .section .text
  .globl write_newline
      .type write_newline, @function
  write_newline: 
      mov  $newline, %rax    
      ret
  </code></pre>
  
  <p>building as follows:</p>
  
  <pre><code>as minimal.s -o minimal.o
  ld -shared minimal.o -o libmin.so
  </code></pre>
  
  <p>leads to the following error:</p>
  
  <pre><code>ld: minimal.o: relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
  minimal.o: error adding symbols: Bad value
  </code></pre>
  
  <p>But gas doesn't know option -fPIC, so I cannot recompile with it:</p>
  
  <pre><code>as -fPIC minimal.s -o minimal.o
  as: unrecognized option '-PIC'
  </code></pre>
  
  <p>What can be done instead?</p>