| 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 | |
| 19 | int main(int argc, char *argv[]) { |
| 20 | char path[4096]; |
| 21 | snprintf(s: path, maxlen: sizeof(path), format: "%s-so.so" , argv[0]); |
| 22 | int i; |
| 23 | |
| 24 | void *handle = dlopen(file: path, RTLD_LAZY); |
| 25 | if (!handle) fprintf(stderr, format: "%s\n" , dlerror()); |
| 26 | assert(handle != 0); |
| 27 | GetTls = (get_t)dlsym(handle: handle, name: "GetTls" ); |
| 28 | assert(dlerror() == 0); |
| 29 | |
| 30 | Dl_info info; |
| 31 | int ret = dladdr(address: GetTls, info: &info); |
| 32 | assert (ret != 0); |
| 33 | printf (format: "fname: %s\n" , info.dli_fname); |
| 34 | printf (format: "fbase: %p\n" , info.dli_fbase); |
| 35 | printf (format: "sname: %s\n" , info.dli_sname); |
| 36 | printf (format: "saddr: %p\n" , info.dli_saddr); |
| 37 | |
| 38 | // CHECK: sname: GetTls |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | #else // BUILD_SO |
| 43 | long var; |
| 44 | long *GetTls() { |
| 45 | return &var; |
| 46 | } |
| 47 | #endif |
| 48 | |