xref: /llvm-project/compiler-rt/test/msan/dladdr_test.c (revision 850daa51f0b75c96af821ae9d4da5ec69f52e1c1)
1 /* RUN: %clang_msan -g %s -o %t
2    RUN: %clang_msan -g %s -DBUILD_SO -fPIC -o %t-so.so -shared
3    RUN: %run %t 2>&1 | FileCheck %s
4 
5    REQUIRES: glibc{{.*}}
6 */
7 
8 #define _GNU_SOURCE
9 
10 #ifndef BUILD_SO
11 #include <assert.h>
12 #include <dlfcn.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 typedef volatile long *(* get_t)();
17 get_t GetTls;
18 
main(int argc,char * argv[])19 int main(int argc, char *argv[]) {
20   char path[4096];
21   snprintf(path, sizeof(path), "%s-so.so", argv[0]);
22   int i;
23 
24   void *handle = dlopen(path, RTLD_LAZY);
25   if (!handle) fprintf(stderr, "%s\n", dlerror());
26   assert(handle != 0);
27   GetTls = (get_t)dlsym(handle, "GetTls");
28   assert(dlerror() == 0);
29 
30   Dl_info info;
31   int ret = dladdr(GetTls, &info);
32   assert (ret != 0);
33   printf ("fname: %s\n", info.dli_fname);
34   printf ("fbase: %p\n", info.dli_fbase);
35   printf ("sname: %s\n", info.dli_sname);
36   printf ("saddr: %p\n", info.dli_saddr);
37 
38   // CHECK: sname: GetTls
39 
40   return 0;
41 }
42 #else  // BUILD_SO
43 long var;
GetTls()44 long *GetTls() {
45   return &var;
46 }
47 #endif
48