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 | #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 extern void* __dso_handle; static const void* stripPointer(const void* ptr) { #if __has_feature(ptrauth_calls) return __builtin_ptrauth_strip(ptr, ptrauth_key_asia); #else return ptr; #endif } int dylib_bar() { return 2; } static int dylib_foo() { return 3; } __attribute__((visibility("hidden"))) int dylib_hide() { return 4; } // checks global symbol static void verifybar() { Dl_info info; if ( dladdr(&dylib_bar, &info) == 0 ) { printf("[FAIL] dladdr(&dylib_bar, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "dylib_bar") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"dylib_bar\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&dylib_bar) ) { printf("[FAIL] dladdr()->dli_saddr is not &dylib_bar\n"); exit(0); } if ( info.dli_fbase != &__dso_handle ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &dylib_bar\n"); exit(0); } } // checks local symbol static void verifyfoo() { Dl_info info; if ( dladdr(&dylib_foo, &info) == 0 ) { printf("[FAIL] dladdr(&dylib_foo, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "dylib_foo") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"dylib_foo\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&dylib_foo) ) { printf("[FAIL] dladdr()->dli_saddr is not &dylib_foo\n"); exit(0); } if ( info.dli_fbase != &__dso_handle ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &dylib_foo\n"); exit(0); } } // checks hidden symbol static void verifyhide() { Dl_info info; if ( dladdr(&dylib_hide, &info) == 0 ) { printf("[FAIL] dladdr(&dylib_hide, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "dylib_hide") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"dylib_hide\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&dylib_hide) ) { printf("[FAIL] dladdr()->dli_saddr is not &dylib_hide\n"); exit(0); } if ( info.dli_fbase != &__dso_handle ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &dylib_hide\n"); exit(0); } } // checks DSO handle static void verifyDSOHandle() { Dl_info info; if ( dladdr(&__dso_handle, &info) == 0 ) { printf("[FAIL] dladdr(&__dso_handle, xx) failed\n"); exit(0); } if ( strcmp(info.dli_sname, "__dso_handle") != 0 ) { printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"__dso_handle\"\n", info.dli_sname); exit(0); } if ( info.dli_saddr != stripPointer(&__dso_handle) ) { printf("[FAIL] dladdr()->dli_saddr is not &__dso_handle\n"); exit(0); } if ( info.dli_fbase != &__dso_handle ) { printf("[FAIL] dladdr()->dli_fbase is not image that contains &__dso_handle\n"); exit(0); } } void verifyDylib() { verifybar(); verifyfoo(); verifyhide(); verifyDSOHandle(); } |