title: Dynamically load library from chroot with glibc dependency
body: <p>I'm trying to dynamically load a library from within a chroot.
  The said library depends on glibc and its version is different from my host (host has 2.26, chroot has 2.23).</p>
  
  <p>Is there a way to achieve that?</p>
  
  <p>Here is what I tried:</p>
  
  <p><code>$ curl http://cdimage.ubuntu.com/ubuntu-base/releases/16.04/release/ubuntu-base-16.04-core-amd64.tar.gz | tar -C ubuntu -xz</code></p>
  
  <pre><code>#include &lt;unistd.h&gt;                                                                                                                                                                                                 
  #include &lt;dlfcn.h&gt;
  
  int main(int argc, char *argv[])
  {
          chdir("ubuntu");
          chroot(".");
  
          // 1. This will fail
          dlopen("libpthread.so.0", RTLD_NOW);
  
          // 2. This will crash
          dlmopen(LM_ID_NEWLM, "libpthread.so.0", RTLD_NOW);
  
          return 0;
  }
  </code></pre>
  
  <ol>
  <li><p>Obviously fails because there is a glibc mismatch:
  </p>
  
  <pre class="lang-none prettyprint-override"><code>/lib/x86_64-linux-gnu/libpthread.so.0: symbol __libc_dl_error_tsd, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference
  </code></pre></li>
  <li><p>Crashes with the following output from valgrind:</p>
  
  <pre class="lang-none prettyprint-override"><code>==19923== Process terminating with default action of signal 4 (SIGILL): dumping core
  ==19923==  Illegal opcode at address 0x401D41C
  ==19923==    at 0x401D41C: ??? (in /usr/lib/ld-2.26.so)
  ==19923==    by 0x4013932: dl_open_worker (in /usr/lib/ld-2.26.so)
  ==19923==    by 0x516EB63: _dl_catch_error (in /usr/lib/libc-2.26.so)
  ==19923==    by 0x4013279: _dl_open (in /usr/lib/ld-2.26.so)
  ==19923==    by 0x4E3A8CF: ??? (in /usr/lib/libdl-2.26.so)
  ==19923==    by 0x516EB63: _dl_catch_error (in /usr/lib/libc-2.26.so)
  ==19923==    by 0x4E3A586: ??? (in /usr/lib/libdl-2.26.so)
  ==19923==    by 0x4E3A9B6: dlmopen (in /usr/lib/libdl-2.26.so)
  </code></pre></li>
  </ol>