]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/NSCreateObjectFileImageFromFile-stress.dtest/main.cpp
54c676d7d081224080e25b97dd05cd8bfd14e6d2
[apple/dyld.git] / testing / test-cases / NSCreateObjectFileImageFromFile-stress.dtest / main.cpp
1 // BUILD_ONLY: MacOSX
2
3 // BUILD: $CXX main.cpp -o $BUILD_DIR/NSCreateObjectFileImageFromFile-stress.exe -Wno-deprecated-declarations
4 // BUILD: $CC foo.c -o $BUILD_DIR/foo.bundle -bundle
5
6 // RUN: ./NSCreateObjectFileImageFromFile-stress.exe $RUN_DIR/foo.bundle
7
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <dlfcn.h>
12 #include <mach-o/dyld.h>
13 #include <vector>
14
15 int main(int argc, const char* argv[])
16 {
17 printf("[BEGIN] NSCreateObjectFileImageFromFile-basic\n");
18
19 const char* path = argv[1];
20
21 std::vector<NSObjectFileImage> ofis;
22 for (unsigned i = 0; i != 32; ++i) {
23 NSObjectFileImage ofi;
24 if ( NSCreateObjectFileImageFromFile(path, &ofi) != NSObjectFileImageSuccess ) {
25 printf("[FAIL] NSCreateObjectFileImageFromFile failed\n");
26 return 0;
27 }
28 ofis.push_back(ofi);
29 }
30
31 for (unsigned i = 0; i != 32; ++i) {
32 NSObjectFileImage ofi = ofis[i];
33 NSModule mod = NSLinkModule(ofi, path, NSLINKMODULE_OPTION_NONE);
34 if ( mod == NULL ) {
35 printf("[FAIL] NSLinkModule failed\n");
36 return 0;
37 }
38
39 NSSymbol sym = NSLookupSymbolInModule(mod, "_fooInBundle");
40 if ( sym == NULL ) {
41 printf("[FAIL] NSLookupSymbolInModule failed\n");
42 return 0;
43 }
44
45 void* func = NSAddressOfSymbol(sym);
46 if ( func == NULL ) {
47 printf("[FAIL] NSAddressOfSymbol failed\n");
48 return 0;
49 }
50
51 Dl_info info;
52 if ( dladdr(func, &info) == 0 ) {
53 printf("[FAIL] dladdr(&p, xx) failed");
54 return 0;
55 }
56 //printf("_fooInBundle found in %s\n", info.dli_fname);
57
58 if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) {
59 printf("[FAIL] NSUnLinkModule failed\n");
60 return 0;
61 }
62
63 if ( dladdr(func, &info) != 0 ) {
64 printf("[FAIL] dladdr(&p, xx) found but should not have\n");
65 return 0;
66 }
67 }
68
69 for (unsigned i = 0; i != 32; ++i) {
70 NSObjectFileImage ofi = ofis[i];
71 if ( !NSDestroyObjectFileImage(ofi) ) {
72 printf("[FAIL] NSDestroyObjectFileImage failed\n");
73 return 0;
74 }
75 }
76
77 printf("[PASS] NSCreateObjectFileImageFromFile-basic\n");
78 return 0;
79 }
80