title: avoid LD_PRELOAD: Wrap library and provide functionality requested from libc
body: <p>I have a shared library, say <code>somelib.so</code>, which uses <code>ioctl</code> from libc (according to <code>objdump</code>).</p>
  
  <p>My goal is to write a new library that wraps around <code>somelib.so</code> and provides a custom <code>ioctl</code>. I want to avoid preloading a library to ensure that only the calls in <code>somelib.so</code> use the custom <code>ioctl</code>.</p>
  
  <p>Here is my current snippet:</p>
  
  <pre><code>typedef int (*entryfunctionFromSomelib_t) (int par, int opt);
  typedef int (*ioctl_t) (int fd, int request, void *data);
  ioctl_t real_ioctl = NULL;
  
  int ioctl(int fd, int request, void *data )
  {
      fprintf( stderr, "trying to wrap ioctl\n" );
      void *handle = dlopen( "libc.so.6", RTLD_NOW );
      if (!handle)
          fprintf( stderr, "Error loading libc.so.6: %s\n", strerror(errno) );
  
      real_ioctl = (ioctl_t) dlsym( handle, "ioctl" );
      return real_ioctl( fd, request, data);
  }
  
  int entryfunctionFromSomelib( int par, int opt ) {
      void *handle = dlopen( "/.../somelib.so", RTLD_NOW );
      if (!handle)
          fprintf( stderr, "Error loading somelib.so: %s\n", strerror(errno) );
  
      real_entryfunctionFromSomelib = entryfunctionFromSomelib_t dlsym( handle, "entryfunctionFromSomelib" );
      return real_entryfunctionFromSomelib( par, opt );
  }
  </code></pre>
  
  <p>However, it does not in work in the sense that the calls to <code>ioctl</code> form <code>somelib.so</code> are not redirected to my custom <code>ioctl</code> implementation. How can I enforce that the wrapped <code>somelib.so</code> does so?</p>
  
  <p>======================</p>
  
  <p>Additional information added after @Nominal Animal post:</p>
  
  <p>Here some information from mylib.so (somelib.so after edit) obtained via <code>readelf -s | grep functionname</code>:</p>
  
  <pre><code>   246: 0000000000000000   121 FUNC    GLOBAL DEFAULT  UND dlsym@GLIBC_2.2.5 (11)
   42427: 0000000000000000   121 FUNC    GLOBAL DEFAULT  UND dlsym@@GLIBC_2.2.5
  
  
     184: 0000000000000000    37 FUNC    GLOBAL DEFAULT  UND ioctl@GLIBC_2.2.5 (6)
   42364: 0000000000000000    37 FUNC    GLOBAL DEFAULT  UND ioctl@@GLIBC_2.2.5
  </code></pre>
  
  <p>After 'patching' mylib.so it also shows the new function as:</p>
  
  <pre><code>   184: 0000000000000000    37 FUNC    GLOBAL DEFAULT  UND iqct1@GLIBC_2.2.5 (6)
  </code></pre>
  
  <p>I 'versioned' and exported the symbols from my wrap_mylib library for which <code>readelf</code> now shows:</p>
  
  <pre><code>25: 0000000000000d15   344 FUNC    GLOBAL DEFAULT   12 iqct1@GLIBC_2.2.5
  63: 0000000000000d15   344 FUNC    GLOBAL DEFAULT   12 iqct1@GLIBC_2.2.5
  </code></pre>
  
  <p>However, when I try to <code>dlopen</code> wrap_mylib I get the following error:</p>
  
  <pre><code>symbol iqct1, version GLIBC_2.2.5 not defined in file libc.so.6 with link time reference
  </code></pre>
  
  <p>Is that maybe because mylib.so tries to <code>dlsym</code> <code>iqct1</code> from libc.so.6 ?</p>