]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/NSCreateObjectFileImageFromFile-basic.dtest/main.c
dyld-851.27.tar.gz
[apple/dyld.git] / testing / test-cases / NSCreateObjectFileImageFromFile-basic.dtest / main.c
1 // BUILD(macos): $CC main.c -o $BUILD_DIR/NSCreateObjectFileImageFromFile-basic.exe -Wno-deprecated-declarations
2 // BUILD(macos): $CC foo.c -o $BUILD_DIR/foo.bundle -bundle
3
4 // BUILD(ios,tvos,watchos,bridgeos):
5
6 // RUN: ./NSCreateObjectFileImageFromFile-basic.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
14 #include "test_support.h"
15
16 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
17 const char* path = argv[1];
18
19 NSObjectFileImage ofi;
20 if ( NSCreateObjectFileImageFromFile(path, &ofi) != NSObjectFileImageSuccess ) {
21 FAIL("NSCreateObjectFileImageFromFile failed");
22 }
23
24 NSModule mod = NSLinkModule(ofi, path, NSLINKMODULE_OPTION_NONE);
25 if ( mod == NULL ) {
26 FAIL("NSLinkModule failed");
27 }
28
29 NSSymbol sym = NSLookupSymbolInModule(mod, "_fooInBundle");
30 if ( sym == NULL ) {
31 FAIL("NSLookupSymbolInModule failed");
32 }
33
34 void* func = NSAddressOfSymbol(sym);
35 if ( func == NULL ) {
36 FAIL("NSAddressOfSymbol failed");
37 }
38
39 Dl_info info;
40 if ( dladdr(func, &info) == 0 ) {
41 FAIL("dladdr(&p, xx) fail");
42 }
43 LOG("_fooInBundle found in %s", info.dli_fname);
44
45 if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) {
46 FAIL("NSUnLinkModule failed");
47 }
48
49 if ( dladdr(func, &info) != 0 ) {
50 FAIL("dladdr(&p, xx) found but should not have");
51 }
52
53 if ( !NSDestroyObjectFileImage(ofi) ) {
54 FAIL("NSDestroyObjectFileImage failed");
55 }
56
57 PASS("Success");
58 }
59