title: GCC, -flto, -fno-builtin and custom function implementation of glibc functions
body: <p>I'm observing unexpected behaviour (at least I cant find explanation for it) with GCC flag <code>-flto</code> and <code>jemalloc</code>/<code>tcmalloc</code>. Once <code>-flto</code> is used and I link with above libraries malloc/calloc and friends are not replaced by <code>je/tc malloc</code> implementation, the glibc implementation is called. Once I remove <code>-flto</code> flag, everything works as expected. I tried to use <code>-fno-builtin</code>/<code>-fno-builtin-*</code> with <code>-flto</code> but still, it doesnt pick the <code>je/tc malloc</code> implementation.</p>
  
  <p>How the <code>-flto</code> machinery works? Why the binary doesnt pick new implementation? How it even links with <code>-fno-builtin</code> when it should fail on unresolved external for, say, <code>printf</code>?</p>
  
  <p>EDIT001:<br>
  GCC 7.3<br>
  Sample code</p>
  
  <pre><code>int main()
  {
      auto p = malloc(1024);
      free(p);
      return 0;
  }
  </code></pre>
  
  <p>Compilation:<br></p>
  
  <blockquote>
    <p>/usr/bin/c++    -O2 -g -DNDEBUG   -flto -std=gnu++14 -o
    CMakeFiles/flto.dir/main.cpp.o -c
    /home/user/Development/CPPJunk/flto/main.cpp</p>
  </blockquote>
  
  <p>Linkage:<br></p>
  
  <blockquote>
    <p>/usr/bin/c++  -O2 -g -DNDEBUG  -flto CMakeFiles/flto.dir/main.cpp.o 
    -o flto  -L/home/user/Development/jemalloc -Wl,-rpath,/home/user/Development/jemalloc -ljemalloc</p>
  </blockquote>
  
  <p>EDIT002:<br>
  More suitable sample code</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  
  int main()
  {
      auto p = malloc(1024);
      if (p) {
          free(p);
      }
  
      auto p1 = new int;
      if (p1) {
          delete p1;
      }
  
      auto p2 = new int[32];
      if (p2) {
          delete[] p2;
      }
      return 0;
  }
  </code></pre>