title: How to link malloc when compiling with GCC?
body: <p>How do I compile/link malloc in assembly when using object files with GCC?<br>
  I'm doing something like this:</p>
  <pre><code>global test
  extern malloc
  section .text
  
  test:
      mov rdi, 40
      call malloc
      ret
  
  </code></pre>
  <pre><code>#include &lt;iostream&gt;
  extern &quot;C&quot; void*
  test(void);
  
  int
  main()
  {
      std::cout &lt;&lt; test() &lt;&lt; &quot;\n&quot;;
      return 0;
  }
  </code></pre>
  <p>How do I compile this?
  Doing this:</p>
  <pre class="lang-sh prettyprint-override"><code>nasm -f elf64 test.asm
  g++ test.o test.cpp
  </code></pre>
  <p>Makes gcc (or ld rather) throw this error:</p>
  <pre><code>/usr/bin/ld: test.o: warning: relocation against `malloc@@GLIBC_2.2.5' in read-only section `.text'
  /usr/bin/ld: test.o: relocation R_X86_64_PC32 against symbol `malloc@@GLIBC_2.2.5' can not be used when making a PIE object; recompile with -fPIE
  /usr/bin/ld: final link failed: bad value
  collect2: error: ld returned 1 exit status
  </code></pre>