Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / msan / dladdr1_test.c
blob534f5f9cc193a2f1aeb48e34adb19e0ffa3e7a8d
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
5 REQUIRES: glibc{{.*}}
6 */
8 #define _GNU_SOURCE
10 #ifndef BUILD_SO
11 #include <assert.h>
12 #include <dlfcn.h>
13 #include <elf.h>
14 #include <link.h>
15 #include <stdio.h>
16 #include <stdlib.h>
18 typedef volatile long *(* get_t)();
19 get_t GetTls;
21 int main(int argc, char *argv[]) {
22 char path[4096];
23 snprintf(path, sizeof(path), "%s-so.so", argv[0]);
24 int i;
26 void *handle = dlopen(path, RTLD_LAZY);
27 if (!handle) fprintf(stderr, "%s\n", dlerror());
28 assert(handle != 0);
29 GetTls = (get_t)dlsym(handle, "GetTls");
30 assert(dlerror() == 0);
33 printf("Testing RTLD_DL_LINKMAP\n");
34 fflush(stdout);
36 Dl_info info;
37 struct link_map *map_ptr;
38 int ret = dladdr1(GetTls, &info, (void**)(&map_ptr), RTLD_DL_LINKMAP);
39 assert(ret != 0);
40 printf("fname: %s\n", info.dli_fname);
41 printf("fbase: %p\n", info.dli_fbase);
42 printf("sname: %s\n", info.dli_sname);
43 // CHECK: sname: GetTls
44 printf("saddr: %p\n", info.dli_saddr);
46 assert(map_ptr != NULL);
47 printf("map_ptr: %p\n", map_ptr);
48 fflush(stdout);
50 // Find the start of the link map
51 while(map_ptr->l_prev != NULL) {
52 fflush(stdout);
53 map_ptr = map_ptr->l_prev;
56 fflush(stdout);
57 while(map_ptr != NULL) {
58 assert(map_ptr->l_name != NULL);
59 printf("0x%lx: '%s', %p\n", map_ptr->l_addr, map_ptr->l_name, map_ptr->l_ld);
60 fflush(stdout);
61 map_ptr = map_ptr->l_next;
63 // CHECK: libc{{[\-]*.*}}.so
64 // CHECK: dladdr1_test
67 // Test RTLD_DL_SYMENT
70 printf("Testing RTLD_DL_SYMENT\n");
71 fflush(stdout);
73 Dl_info info;
74 ElfW(Sym) *sym;
75 int ret = dladdr1(GetTls, &info, (void**)(&sym), RTLD_DL_SYMENT);
76 assert(ret != 0);
77 printf("fname: %s\n", info.dli_fname);
78 printf("fbase: %p\n", info.dli_fbase);
79 printf("sname: %s\n", info.dli_sname);
80 // CHECK: sname: GetTls
81 printf("saddr: %p\n", info.dli_saddr);
83 printf("sym: %d %d %d %d %lu %lu\n",
84 sym->st_name, sym->st_info, sym->st_other,
85 sym->st_shndx, sym->st_value, sym->st_size);
86 // CHECK: sym:
88 return 0;
90 #else // BUILD_SO
91 long var;
92 long *GetTls() {
93 return &var;
95 #endif