Skip to content

Instantly share code, notes, and snippets.

@disconnect3d
Created August 15, 2018 09:02
Show Gist options
  • Save disconnect3d/ef191ce1350bf3fa7c4842b121c6411e to your computer and use it in GitHub Desktop.
Save disconnect3d/ef191ce1350bf3fa7c4842b121c6411e to your computer and use it in GitHub Desktop.

Based on https://stackoverflow.com/a/27522149

11:00:31 in ~/playground 
➜ cat check_dynlibs.cpp     
#include <iostream>
#include <dlfcn.h>
#include <link.h>

using UnknownStruct = struct unknown_struct {
   void*  pointers[3];
   struct unknown_struct* ptr;
};
using LinkMap = struct link_map;

int main() {
    auto* handle = dlopen(NULL, RTLD_NOW);
    auto* p = reinterpret_cast<UnknownStruct*>(handle)->ptr;
    auto* map = reinterpret_cast<LinkMap*>(p->ptr);

    while (map) {
      std::cout << map->l_name << std::endl;
      // do something with |map| like with handle, returned by |dlopen()|.
      map = map->l_next;
    }
}

11:00:33 in ~/playground 
➜ g++ check_dynlibs.cpp -ldl

11:00:34 in ~/playground 
➜ ./a.out 
/lib/x86_64-linux-gnu/libdl.so.2
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/lib/x86_64-linux-gnu/libc.so.6
/lib64/ld-linux-x86-64.so.2
/lib/x86_64-linux-gnu/libm.so.6
/lib/x86_64-linux-gnu/libgcc_s.so.1

11:00:38 in ~/playground 
➜ echo "void* malloc(unsigned long long s) { return 0; }" > fakeglibc.c && gcc -shared -o libfakeglibc.so -fPIC fakeglibc.c

11:00:40 in ~/playground 
➜ LD_PRELOAD=./libfakeglibc.so ./a.out
./libfakeglibc.so
/lib/x86_64-linux-gnu/libdl.so.2
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/lib/x86_64-linux-gnu/libc.so.6
/lib64/ld-linux-x86-64.so.2
/lib/x86_64-linux-gnu/libm.so.6
/lib/x86_64-linux-gnu/libgcc_s.so.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment