title: relocation R_X86_64_32S against symbol `stdout@@GLIBC_2.2.5' can not be used when making a PIE object
body: <h2>The problem</h2>
  <p>I'm currently reading <a href="https://link.springer.com/book/10.1007/978-1-4842-7437-8" rel="nofollow noreferrer">this book</a> and with the chapter about dynamic linking with the following code:</p>
  <p><code>link_example.s</code></p>
  <pre><code>.globl main
  
  .section .data
  
  output:
      .ascii &quot;Yeet\n\0&quot;
  
  .section .text
  main:
      enter $0, $0
      movq stdout, %rdi
      movq $output, %rsi
      call fprintf
  
      movq $0, %rax
      leave
      ret
  </code></pre>
  <p>Now according to the book, I need to compile it as follows to link the C-library dynamically:</p>
  <pre><code>gcc -rdynamic link_example.s -o link_example
  </code></pre>
  <p>But I'm getting the following error message:</p>
  <pre><code>/usr/bin/ld: /tmp/cchUlvqS.o: relocation R_X86_64_32S against symbol `stdout@@GLIBC_2.2.5' can not be used when making a PIE object; recompile with -fPIE
  /usr/bin/ld: failed to set dynamic section sizes: bad value
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>What am I doing wrong?</p>
  <h2>What have you tried?</h2>
  <h3>Adding <code>-fPIE</code> flag</h3>
  <p>I tried what the compiler suggested by adding the <code>-fPIE</code> flag:</p>
  <pre><code>gcc -rdynamic -fPIE link_example.s -o link_example
  </code></pre>
  <p>but I'm still getting the same error again.</p>
  <h3>Searching similar posts</h3>
  <p>I found a <a href="https://stackoverflow.com/questions/57957080/relocation-r-x86-64-pc32-against-undefined-hidden-symbol-dso-handle-can-not">similar post</a> which said, that I just need to use the <code>-shared</code> flag:</p>
  <pre><code>gcc -shared link_example.s -o link_example
  </code></pre>
  <p>but this gives me:</p>
  <pre><code>/usr/bin/ld: /tmp/ccxktZan.o: relocation R_X86_64_32S against symbol `stdout@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
  /usr/bin/ld: failed to set dynamic section sizes: bad value
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>and if I add the <code>-fPIC</code> flag:</p>
  <pre><code>gcc -shared -fPIC link_example.s -o link_example
  </code></pre>
  <p>then I'm getting this:</p>
  <pre><code>/usr/bin/ld: /tmp/ccKIQ9sl.o: relocation R_X86_64_32S against symbol `stdout@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
  /usr/bin/ld: failed to set dynamic section sizes: bad value
  collect2: error: ld returned 1 exit status
  </code></pre>