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