2 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/libbar.dylib -install_name $RUN_DIR/libbar.dylib
3 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/libbar-present.dylib -install_name $RUN_DIR/libbar.dylib -DHAS_SYMBOL=1
4 // BUILD: $CC foo.c -dynamiclib -Os -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib $BUILD_DIR/libbar-present.dylib
5 // BUILD: $CC main.c -o $BUILD_DIR/dlopen-RTLD_NOW.exe -DRUN_DIR="$RUN_DIR"
7 // BUILD: $SKIP_INSTALL $BUILD_DIR/libbar-present.dylib
9 // RUN: ./dlopen-RTLD_NOW.exe
16 #include <mach-o/getsect.h>
18 #include "test_support.h"
21 extern struct mach_header_64 __dso_handle
;
23 extern struct mach_header __dso_handle
;
26 int main(int argc
, const char* argv
[], const char* envp
[], const char* apple
[]) {
28 /// This tests that RTLD_NOW on dlopen() will return NULL because call from libfoo to libbar could not be bound
30 void* handle
= dlopen(RUN_DIR
"/libfoo.dylib", RTLD_NOW
);
31 if ( handle
!= NULL
) {
32 FAIL("dlopen(\"libfoo.dylib\", RTLD_NOW) should have failed");
37 // arm64e always uses chained binds which does not support lazy binding
38 bool supportsLazyBinding
= false;
40 // other architectures may or may not use lazy binding
41 unsigned long sectSize
= 0;
42 bool supportsLazyBinding
= (getsectiondata(&__dso_handle
, "__DATA", "__la_symbol_ptr", §Size
) != NULL
);
44 // armv7 has two names for lazy pointers section
45 if ( !supportsLazyBinding
)
46 supportsLazyBinding
= (getsectiondata(&__dso_handle
, "__DATA", "__lazy_symbol", §Size
) != NULL
);
51 /// This tests that RTLD_LAZY on dlopen() will succeed if libfoo.dylib
53 handle
= dlopen(RUN_DIR
"/libfoo.dylib", RTLD_LAZY
);
54 if ( supportsLazyBinding
) {
55 if ( handle
== NULL
) {
56 FAIL("dlopen(\"libfoo.dylib\", RTLD_LAZY) should have succeeded: %s", dlerror());
60 if ( handle
!= NULL
) {
61 FAIL("dlopen(\"libfoo.dylib\", RTLD_LAZY) should have failed becuase a symbol was missing");