dyld-851.27.tar.gz
[apple/dyld.git] / testing / test-cases / NSAddressOfSymbol-basic.dtest / main.c
1 // BUILD(macos): $CC main.c -o $BUILD_DIR/NSAddressOfSymbol-basic.exe -Wno-deprecated-declarations
2
3 // BUILD(ios,tvos,watchos,bridgeos):
4
5 // RUN: ./NSAddressOfSymbol-basic.exe
6
7
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <dlfcn.h>
12 #include <mach-o/dyld.h>
13
14 #include "test_support.h"
15
16 extern struct mach_header __dso_handle;
17
18 int patatino(void) {
19 return 666;
20 }
21
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);
24 if ( sym == NULL ) {
25 FAIL("can't find main");
26 }
27 void* mainAddr = NSAddressOfSymbol(sym);
28 if ( mainAddr != &main ) {
29 FAIL("address returned %p is not &main=%p", mainAddr, &main);
30 }
31
32 NSSymbol sym2 = NSLookupSymbolInImage(&__dso_handle, "_patatino", NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
33 if ( sym2 == NULL ) {
34 FAIL("cant' find patatino");
35 }
36 void* funcAddr = NSAddressOfSymbol(sym2);
37 if ( funcAddr == NULL ) {
38 FAIL("address returned for patatino is NULL");
39 }
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");
45 }
46
47 // verify NULL works
48 if ( NSAddressOfSymbol(NULL) != NULL ) {
49 FAIL("NULL not handle");
50 }
51
52 PASS("Success");
53 }
54