Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | // BUILD: $CC main.c -o $BUILD_DIR/dladdr-basic.exe // RUN: ./dladdr-basic.exe #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dlfcn.h> #include <mach-o/dyld_priv.h> #if __has_feature(ptrauth_calls) #include <ptrauth.h> #endif int bar() { return 2; } static int foo() { return 3; } __attribute__((visibility("hidden"))) int hide() { return 4; } static const void *stripPointer(const void *ptr) { #if __has_feature(ptrauth_calls) return __builtin_ptrauth_strip(ptr, ptrauth_key_asia); #else return ptr; #endif } // checks global symbol static void verifybar() { Dl_info info; if ( dladdr(&bar, &info) == 0 ) { printf("[FAIL] dladdr(&bar, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "bar") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"bar\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&bar) ) { printf("[FAIL] dladdr()->dli_saddr is not &bar\n"); exit(0); } if ( info.dli_fbase != dyld_image_header_containing_address(&bar) ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &bar\n"); exit(0); } } // checks local symbol static void verifyfoo() { Dl_info info; if ( dladdr(&foo, &info) == 0 ) { printf("[FAIL] dladdr(&foo, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "foo") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"foo\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&foo) ) { printf("[FAIL] dladdr()->dli_saddr is not &foo\n"); exit(0); } if ( info.dli_fbase != dyld_image_header_containing_address(&foo) ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &foo\n"); exit(0); } } // checks hidden symbol static void verifyhide() { Dl_info info; if ( dladdr(&hide, &info) == 0 ) { printf("[FAIL] dladdr(&hide, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "hide") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"hide\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&hide) ) { printf("[FAIL] dladdr()->dli_saddr is not &hide\n"); exit(0); } if ( info.dli_fbase != dyld_image_header_containing_address(&hide) ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &hide\n"); exit(0); } } // checks dylib symbol static void verifymalloc() { Dl_info info; if ( dladdr(&malloc, &info) == 0 ) { printf("[FAIL] dladdr(&malloc, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "malloc") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"malloc\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&malloc) ) { printf("[FAIL] dladdr()->dli_saddr is not &malloc\n"); exit(0); } if ( info.dli_fbase != dyld_image_header_containing_address(&malloc) ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &malloc\n"); exit(0); } } // checks passing NULL for info parameter gracefully fails static void verifyNULL() { Dl_info info; if ( dladdr(&malloc, NULL) != 0 ) { printf("[FAIL] dladdr(&malloc, NULL) did not fail\n"); exit(0); } if ( dladdr(NULL, NULL) != 0 ) { printf("[FAIL] dladdr(NULL, NULL) did not fail\n"); exit(0); } } int main() { printf("[BEGIN] dladdr-basic\n"); verifybar(); verifyhide(); verifyfoo(); verifymalloc(); verifyNULL(); printf("[PASS] dladdr-basic\n"); return 0; } |