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 | // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib // BUILD: $CC main.c $BUILD_DIR/libfoo.dylib -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> extern void* __dso_handle; extern void verifyDylib(); int bar() { return 2; } static int foo() { return 3; } __attribute__((visibility("hidden"))) int hide() { return 4; } // 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 != &bar) { printf("[FAIL] dladdr()->dli_saddr is not &bar\n"); exit(0); } if ( info.dli_fbase != &__dso_handle ) { 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 != &foo) { printf("[FAIL] dladdr()->dli_saddr is not &foo\n"); exit(0); } if ( info.dli_fbase != &__dso_handle ) { 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 != &hide) { printf("[FAIL] dladdr()->dli_saddr is not &hide\n"); exit(0); } if ( info.dli_fbase != &__dso_handle ) { 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 != &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); } } int main() { printf("[BEGIN] dladdr-basic\n"); verifybar(); verifyhide(); verifyfoo(); verifymalloc(); verifyDylib(); printf("[PASS] dladdr-basic\n"); return 0; } |