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