File indexing completed on 2024-04-21 05:41:32

0001 #include <dlfcn.h>
0002 
0003 #include <cstdio>
0004 #include <cstdlib>
0005 
0006 int main()
0007 {
0008 #ifndef RTLD_DEEPBIND
0009     printf("SKIP (RTLD_DEEPBIND undefined)\n");
0010 #else
0011     fprintf(stderr, "malloc address: %p\n", dlsym(RTLD_NEXT, "malloc"));
0012     fprintf(stderr, "free address: %p\n", dlsym(RTLD_NEXT, "free"));
0013 
0014     auto p = malloc(10);
0015     fprintf(stderr, "p = %p\n", p);
0016     free(p);
0017 
0018     fprintf(stderr, "loading lib: %s\n", LIB_PATH);
0019     auto handle = dlopen(LIB_PATH, RTLD_DEEPBIND | RTLD_NOW | RTLD_GLOBAL);
0020     if (!handle) {
0021         fprintf(stderr, "dlopen error loading %s: %s\n", LIB_PATH, dlerror());
0022         return 1;
0023     }
0024 
0025     auto allocFromLib = reinterpret_cast<void (*)(bool)>(dlsym(RTLD_NEXT, "allocFromLib"));
0026     if (!allocFromLib) {
0027         fprintf(stderr, "allocFromLib not resolved: %s\n", dlerror());
0028         return 2;
0029     }
0030 
0031     allocFromLib(false);
0032 
0033     fprintf(stderr, "malloc address: %p\n", dlsym(RTLD_NEXT, "malloc"));
0034     fprintf(stderr, "free address: %p\n", dlsym(RTLD_NEXT, "free"));
0035 #endif
0036 
0037     return 0;
0038 }