]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/NSCreateObjectFileImageFromMemory-basic.dtest/main.c
888c043c2a0f8120c01b24b835b5cb5d20d46674
[apple/dyld.git] / testing / test-cases / NSCreateObjectFileImageFromMemory-basic.dtest / main.c
1 // BUILD_ONLY: MacOSX
2
3 // BUILD: $CC main.c -o $BUILD_DIR/NSCreateObjectFileImageFromMemory-basic.exe -Wno-deprecated-declarations
4 // BUILD: $CC foo.c -o $BUILD_DIR/foo.bundle -bundle
5
6 // RUN: ./NSCreateObjectFileImageFromMemory-basic.exe $RUN_DIR/foo.bundle
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/mman.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <dlfcn.h>
18 #include <mach-o/dyld.h>
19
20
21 static void checkBundle(const char* path, bool unlinkBeforeDestroy)
22 {
23 int fd = open(path, O_RDONLY, 0);
24 if ( fd == -1 ) {
25 printf("[FAIL] open(%s) failed", path);
26 exit(0);
27 }
28
29 struct stat stat_buf;
30 if ( fstat(fd, &stat_buf) == -1) {
31 printf("[FAIL] fstat() failed\n");
32 exit(0);
33 }
34
35 void* loadAddress = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
36 if ( loadAddress == ((void*)(-1)) ) {
37 printf("[FAIL] mmap() failed\n");
38 exit(0);
39 }
40
41 close(fd);
42
43 NSObjectFileImage ofi;
44 if ( NSCreateObjectFileImageFromMemory(loadAddress, stat_buf.st_size, &ofi) != NSObjectFileImageSuccess ) {
45 printf("[FAIL] NSCreateObjectFileImageFromMemory failed\n");
46 exit(0);
47 }
48
49 NSModule mod = NSLinkModule(ofi, path, NSLINKMODULE_OPTION_NONE);
50 if ( mod == NULL ) {
51 printf("[FAIL] NSLinkModule failed\n");
52 exit(0);
53 }
54
55 if ( !unlinkBeforeDestroy ) {
56 // API lets you destroy ofi and NSModule lives on
57 if ( !NSDestroyObjectFileImage(ofi) ) {
58 printf("[FAIL] NSDestroyObjectFileImage failed\n");
59 exit(0);
60 }
61 }
62
63 NSSymbol sym = NSLookupSymbolInModule(mod, "_fooInBundle");
64 if ( sym == NULL ) {
65 printf("[FAIL] NSLookupSymbolInModule failed\n");
66 exit(0);
67 }
68
69 void* func = NSAddressOfSymbol(sym);
70 if ( func == NULL ) {
71 printf("[FAIL] NSAddressOfSymbol failed\n");
72 exit(0);
73 }
74
75 Dl_info info;
76 if ( dladdr(func, &info) == 0 ) {
77 printf("[FAIL] dladdr(&p, xx) failed\n");
78 exit(0);
79 }
80 //printf("_fooInBundle found in %s\n", info.dli_fname);
81
82 if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) {
83 printf("[FAIL] NSUnLinkModule failed\n");
84 exit(0);
85 }
86
87 if ( dladdr(func, &info) != 0 ) {
88 printf("[FAIL] dladdr(&p, xx) found but should not have\n");
89 exit(0);
90 }
91
92 if ( unlinkBeforeDestroy ) {
93 if ( !NSDestroyObjectFileImage(ofi) ) {
94 printf("[FAIL] NSDestroyObjectFileImage failed\n");
95 exit(0);
96 }
97 }
98 }
99
100
101 int main(int argc, const char* argv[])
102 {
103 printf("[BEGIN] NSCreateObjectFileImageFromMemory-basic\n");
104
105 checkBundle(argv[1], true);
106 checkBundle(argv[1], false);
107
108 // Now go again enough times to flush out any limits in our dlopen encodings.
109 for (unsigned i = 0; i != 255; ++i)
110 checkBundle(argv[1], false);
111
112 printf("[PASS] NSCreateObjectFileImageFromMemory-basic\n");
113 return 0;
114 }
115