dyld-832.7.1.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 $BUILD_DIR/libfoo-present.dylib -install_name $RUN_DIR/libfoo.dylib -DHAS_SYMBOL=1
5 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/libbar-missing.dylib -install_name $RUN_DIR/libbar-missing.dylib -DHAS_SYMBOL=1
6 // BUILD: $CC main.c -o $BUILD_DIR/lazy-symbol-missing.exe $BUILD_DIR/libfoo-present.dylib -Os
7 // BUILD: $CC main.c -o $BUILD_DIR/lazy-symbol-missing-flat.exe -undefined dynamic_lookup -Os -DFLAT=1
8 // BUILD: $CC main-call.c -o $BUILD_DIR/lazy-symbol-missing-called.exe $BUILD_DIR/libfoo-present.dylib -Os
9 // BUILD: $CC main-call.c -o $BUILD_DIR/lazy-symbol-missing-called-weak-lib.exe $BUILD_DIR/libbar-missing.dylib -Os -DWEAK=1
10 // BUILD: $CXX runner.cpp -o $BUILD_DIR/lazy-symbol-runner.exe -DRUN_DIR="$RUN_DIR"
11 // BUILD: $CXX runner.cpp -o $BUILD_DIR/lazy-symbol-runner-weak-lib.exe -DRUN_DIR="$RUN_DIR" -DWEAK=1
12
13 // BUILD: $SKIP_INSTALL $BUILD_DIR/libfoo-present.dylib
14 // BUILD: $SKIP_INSTALL $BUILD_DIR/libbar-missing.dylib
15
16 // NO_CRASH_LOG: lazy-symbol-missing-called.exe
17 // NO_CRASH_LOG: lazy-symbol-missing-called-weak-lib.exe
18
19 // RUN: ./lazy-symbol-missing.exe
20 // RUN: ./lazy-symbol-runner.exe
21 // RUN: ./lazy-symbol-missing-flat.exe
22 // RUN: ./lazy-symbol-runner-weak-lib.exe
23
24
25 #include <stdio.h>
26 #include <stdbool.h>
27 #include <mach-o/getsect.h>
28
29 #include "test_support.h"
30
31 #if __LP64__
32 extern struct mach_header_64 __dso_handle;
33 #else
34 extern struct mach_header __dso_handle;
35 #endif
36
37 extern int slipperySymbol();
38
39 #ifdef FLAT
40 #define TESTNAME "lazy-symbol-missing-flat"
41 #else
42 #define TESTNAME "lazy-symbol-missing"
43 #endif
44
45 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
46 #if __arm64e__
47 // arm64e always uses chained binds which does not support lazy binding
48 bool supportsLazyBinding = false;
49 #else
50 // other architectures may or may not use lazy binding
51 unsigned long sectSize = 0;
52 bool supportsLazyBinding = (getsectiondata(&__dso_handle, "__DATA", "__la_symbol_ptr", &sectSize) != NULL);
53 #if __ARM_ARCH_7K__
54 // armv7 has two names for lazy pointers section
55 if ( !supportsLazyBinding )
56 supportsLazyBinding = (getsectiondata(&__dso_handle, "__DATA", "__lazy_symbol", &sectSize) != NULL);
57 #endif
58 #endif
59
60 if ( supportsLazyBinding ) {
61 // add runtime check that results in the function never being called
62 if ( argc < 0 )
63 slipperySymbol();
64 }
65 PASS("%s", TESTNAME);
66 }
67