title: "Accessing a corrupted shared library"
body: <p>I'm trying to dynamically link a 64-bit <code>nasm</code> program using <code>ld</code> instead of <code>gcc</code>, in a 64-bit Linux system. The assembly code is this:</p>
  
  <pre><code>extern printf
  extern exit
  
  section .data
    msg: db "Hello x%d", 10, 0   
  
  section .text
  global _start
  
  _start:
    mov rdi, [rel msg]
    mov rsi, 64
    call printf
  
    mov rdi, 0
    call exit
  </code></pre>
  
  <p>I'm trying to call <code>printf</code> and <code>exit</code> from <code>libc</code>. I assemble and build with:</p>
  
  <pre><code>$ nasm -felf64 src/printf.asm -o bin/printf.o
  $ ld bin/printf.o -lc -I /lib/ld-linux.so.2 -o bin/printf
  </code></pre>
  
  <p>Then I run and get an error:</p>
  
  <pre><code>$ bin/printf
  bash: bin/printf: Accessing a corrupted shared library
  </code></pre>
  
  <p>There is a question with a similar problem <a href="https://stackoverflow.com/questions/30419857/accessing-a-corrupted-shared-library">here</a>, but the problem there is the opposite: they try to create a 32-bit program in a 64-bit machine. I'm just trying to make a 64-bit program.</p>
  
  <hr>
  
  <p>I found the solution: there is a /lib64 directory with a ld-linux-x86-64.so.2 that you should use to link with 64-bit libraries. I'm still getting a segmentation fault though.</p>