title: ELF Shared Object in x86-64 Assembly language
body: <p>I'm trying to create a Shared library (*.so) in ASM and I'm not sure that i do it correct...</p>
  
  <p>My code is:</p>
  
  <pre><code>    .section .data
      .globl var1
  var1:
      .quad     0x012345
  
      .section .text
      .globl func1
  func1:
      xor %rax, %rax
    # mov var1, %rcx       # this is commented
      ret
  </code></pre>
  
  <p>To compile it i run</p>
  
  <pre><code>gcc ker.s -g -fPIC -m64 -o ker.o
  gcc ker.o -shared -fPIC -m64 -o libker.so
  </code></pre>
  
  <p>I can access variable var1 and call func1 with dlopen() and dlsym() from a program in C.</p>
  
  <p>The problem is in variable var1. When i try to access it from func1, i.e. uncomment that line, the compiler generates an error:</p>
  
  <pre><code>/usr/bin/ld: ker.o: relocation R_X86_64_32S against `var1' can not be used when making a shared object; recompile with -fPIC
  ker.o: could not read symbols: Bad value
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I don't understand. I've already compiled with -fPIC, so what's wrong?</p>