2 // BUILD: $CC main.c -o $BUILD_DIR/dladdr-basic.exe
4 // RUN: ./dladdr-basic.exe
10 #include <mach-o/dyld_priv.h>
12 #if __has_feature(ptrauth_calls)
27 __attribute__((visibility("hidden"))) int hide()
32 static const void *stripPointer(const void *ptr
) {
33 #if __has_feature(ptrauth_calls)
34 return __builtin_ptrauth_strip(ptr
, ptrauth_key_asia
);
40 // checks global symbol
41 static void verifybar()
44 if ( dladdr(&bar
, &info
) == 0 ) {
45 printf("[FAIL] dladdr(&bar, xx) failed\n");
48 if ( strcmp(info
.dli_sname
, "bar") != 0 ) {
49 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"bar\"\n", info
.dli_sname
);
52 if ( info
.dli_saddr
!= stripPointer(&bar
) ) {
53 printf("[FAIL] dladdr()->dli_saddr is not &bar\n");
56 if ( info
.dli_fbase
!= dyld_image_header_containing_address(&bar
) ) {
57 printf("[FAIL] dladdr()->dli_fbase is not image that contains &bar\n");
62 // checks local symbol
63 static void verifyfoo()
66 if ( dladdr(&foo
, &info
) == 0 ) {
67 printf("[FAIL] dladdr(&foo, xx) failed\n");
70 if ( strcmp(info
.dli_sname
, "foo") != 0 ) {
71 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"foo\"\n", info
.dli_sname
);
74 if ( info
.dli_saddr
!= stripPointer(&foo
) ) {
75 printf("[FAIL] dladdr()->dli_saddr is not &foo\n");
78 if ( info
.dli_fbase
!= dyld_image_header_containing_address(&foo
) ) {
79 printf("[FAIL] dladdr()->dli_fbase is not image that contains &foo\n");
84 // checks hidden symbol
85 static void verifyhide()
88 if ( dladdr(&hide
, &info
) == 0 ) {
89 printf("[FAIL] dladdr(&hide, xx) failed\n");
92 if ( strcmp(info
.dli_sname
, "hide") != 0 ) {
93 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"hide\"\n", info
.dli_sname
);
96 if ( info
.dli_saddr
!= stripPointer(&hide
) ) {
97 printf("[FAIL] dladdr()->dli_saddr is not &hide\n");
100 if ( info
.dli_fbase
!= dyld_image_header_containing_address(&hide
) ) {
101 printf("[FAIL] dladdr()->dli_fbase is not image that contains &hide\n");
106 // checks dylib symbol
107 static void verifymalloc()
110 if ( dladdr(&malloc
, &info
) == 0 ) {
111 printf("[FAIL] dladdr(&malloc, xx) failed\n");
114 if ( strcmp(info
.dli_sname
, "malloc") != 0 ) {
115 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"malloc\"\n", info
.dli_sname
);
118 if ( info
.dli_saddr
!= stripPointer(&malloc
) ) {
119 printf("[FAIL] dladdr()->dli_saddr is not &malloc\n");
122 if ( info
.dli_fbase
!= dyld_image_header_containing_address(&malloc
) ) {
123 printf("[FAIL] dladdr()->dli_fbase is not image that contains &malloc\n");
128 // checks passing NULL for info parameter gracefully fails
129 static void verifyNULL()
132 if ( dladdr(&malloc
, NULL
) != 0 ) {
133 printf("[FAIL] dladdr(&malloc, NULL) did not fail\n");
136 if ( dladdr(NULL
, NULL
) != 0 ) {
137 printf("[FAIL] dladdr(NULL, NULL) did not fail\n");
144 printf("[BEGIN] dladdr-basic\n");
151 printf("[PASS] dladdr-basic\n");