]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/lazy-symbol-missing.dtest/main.c
dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / lazy-symbol-missing.dtest / main.c
1 // note: -Os is needed for armv7 to work around compiler issue where at -O0 it computes pointer to function and calls that
2
3 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib
4 // BUILD: $CC foo.c -dynamiclib -o $TEMP_DIR/libfoo-present.dylib -install_name $RUN_DIR/libfoo.dylib -DHAS_SYMBOL=1
5 // BUILD: $CC main.c -o $BUILD_DIR/lazy-symbol-missing.exe $TEMP_DIR/libfoo-present.dylib -Os
6 // BUILD: $CC main.c -o $BUILD_DIR/lazy-symbol-missing-flat.exe -undefined dynamic_lookup -Os -DFLAT=1
7 // BUILD: $CC main-call.c -o $BUILD_DIR/lazy-symbol-missing-called.exe $TEMP_DIR/libfoo-present.dylib -Os
8 // BUILD: $CC runner.c -o $BUILD_DIR/lazy-symbol-runner.exe -DRUN_DIR="$RUN_DIR"
9
10 // NO_CRASH_LOG: lazy-symbol-missing-called.exe
11
12 // RUN: ./lazy-symbol-missing.exe
13 // RUN: ./lazy-symbol-runner.exe
14 // RUN: ./lazy-symbol-missing-flat.exe
15
16
17 #include <stdio.h>
18 #include <stdbool.h>
19 #include <mach-o/getsect.h>
20
21 #if __LP64__
22 extern struct mach_header_64 __dso_handle;
23 #else
24 extern struct mach_header __dso_handle;
25 #endif
26
27 extern int slipperySymbol();
28
29 #ifdef FLAT
30 #define TESTNAME "lazy-symbol-missing-flat"
31 #else
32 #define TESTNAME "lazy-symbol-missing"
33 #endif
34
35 int main(int argc, const char* argv[])
36 {
37 printf("[BEGIN] " TESTNAME "\n");
38
39 #if __arm64e__
40 // arm64e always uses chained binds which does not support lazy binding
41 bool supportsLazyBinding = false;
42 #else
43 // other architectures may or may not use lazy binding
44 unsigned long sectSize = 0;
45 bool supportsLazyBinding = (getsectiondata(&__dso_handle, "__DATA", "__la_symbol_ptr", &sectSize) != NULL);
46 #if __ARM_ARCH_7K__
47 // armv7 has two names for lazy pointers section
48 if ( !supportsLazyBinding )
49 supportsLazyBinding = (getsectiondata(&__dso_handle, "__DATA", "__lazy_symbol", &sectSize) != NULL);
50 #endif
51 #endif
52
53 if ( supportsLazyBinding ) {
54 // add runtime check that results in the function never being called
55 if ( argc < 0 )
56 slipperySymbol();
57 }
58
59 printf("[PASS] " TESTNAME "\n");
60
61 return 0;
62 }
63