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