1 // BUILD(macos): $CC main.c -o $BUILD_DIR/NSAddressOfSymbol-basic.exe -Wno-deprecated-declarations
3 // BUILD(ios,tvos,watchos,bridgeos):
5 // RUN: ./NSAddressOfSymbol-basic.exe
12 #include <mach-o/dyld.h>
14 #include "test_support.h"
16 extern struct mach_header __dso_handle
;
22 int main(int argc
, const char* argv
[], const char* envp
[], const char* apple
[]) {
23 NSSymbol sym
= NSLookupSymbolInImage(&__dso_handle
, "_main", NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR
);
25 FAIL("can't find main");
27 void* mainAddr
= NSAddressOfSymbol(sym
);
28 if ( mainAddr
!= &main
) {
29 FAIL("address returned %p is not &main=%p", mainAddr
, &main
);
32 NSSymbol sym2
= NSLookupSymbolInImage(&__dso_handle
, "_patatino", NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR
);
34 FAIL("cant' find patatino");
36 void* funcAddr
= NSAddressOfSymbol(sym2
);
37 if ( funcAddr
== NULL
) {
38 FAIL("address returned for patatino is NULL");
40 // This returns a signed pointer, so we want to make sure we can call it without crashing.
41 int (*func_ptr
)(void) = funcAddr
;
42 int result
= (*func_ptr
)();
43 if ( result
!= 666 ) {
44 FAIL("can't call the function correctly");
48 if ( NSAddressOfSymbol(NULL
) != NULL
) {
49 FAIL("NULL not handle");