Skip to content
Newer
Older
100644 147 lines (130 sloc) 3.92 KB
ee99867 @ccgus oh geeze
authored Mar 4, 2009
1 /*******************************************************************************
2 load_bundle.c
3 Copyright (c) 2005 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
4 Some rights reserved: <http://creativecommons.org/licenses/by/2.0/>
5
6 ***************************************************************************/
7
8 #include "load_bundle.h"
9 #include <CoreServices/CoreServices.h>
10 #include <sys/syslimits.h> // for PATH_MAX.
11 #include <mach-o/dyld.h>
12
13 mach_error_t
14 load_bundle_package(
15 const char *bundlePackageFileSystemRepresentation )
16 {
17 assert( bundlePackageFileSystemRepresentation );
18 assert( strlen( bundlePackageFileSystemRepresentation ) );
19
20 mach_error_t err = err_none;
21
22 // Morph the FSR into a URL.
23 CFURLRef bundlePackageURL = NULL;
24 if( !err ) {
25 bundlePackageURL = CFURLCreateFromFileSystemRepresentation(
26 kCFAllocatorDefault,
27 (const UInt8*)bundlePackageFileSystemRepresentation,
28 strlen(bundlePackageFileSystemRepresentation),
29 true );
30 if( bundlePackageURL == NULL )
31 err = err_load_bundle_url_from_path;
32 }
33
34 // Create bundle.
35 CFBundleRef bundle = NULL;
36 if( !err ) {
37 bundle = CFBundleCreate( kCFAllocatorDefault, bundlePackageURL );
38 if( bundle == NULL )
39 err = err_load_bundle_create_bundle;
40 }
41
42 // Discover the bundle's executable file.
43 CFURLRef bundleExecutableURL = NULL;
44 if( !err ) {
45 assert( bundle );
46 bundleExecutableURL = CFBundleCopyExecutableURL( bundle );
47 if( bundleExecutableURL == NULL )
48 err = err_load_bundle_package_executable_url;
49 }
50
51 // Morph the executable's URL into an FSR.
52 char bundleExecutableFileSystemRepresentation[PATH_MAX];
53 if( !err ) {
54 assert( bundleExecutableURL );
55 if( !CFURLGetFileSystemRepresentation(
56 bundleExecutableURL,
57 true,
58 (UInt8*)bundleExecutableFileSystemRepresentation,
59 sizeof(bundleExecutableFileSystemRepresentation) ) )
60 {
61 err = err_load_bundle_path_from_url;
62 }
63 }
64
65 // Do the real work.
66 if( !err ) {
67 assert( strlen(bundleExecutableFileSystemRepresentation) );
68 err = load_bundle_executable( bundleExecutableFileSystemRepresentation);
69 }
70
71 // Clean up.
72 if( bundleExecutableURL )
73 CFRelease( bundleExecutableURL );
74 if( bundle )
75 CFRelease( bundle );
76 if( bundlePackageURL )
77 CFRelease( bundlePackageURL );
78
79 return err;
80 }
81
82 mach_error_t
83 load_bundle_executable(
84 const char *bundleExecutableFileSystemRepresentation )
85 {
86 assert( bundleExecutableFileSystemRepresentation );
87
88 mach_error_t err = err_none;
89
90 // Create the object file image.
91 NSObjectFileImage image;
92 if( !err ) {
93 NSObjectFileImageReturnCode imageErr = NSCreateObjectFileImageFromFile(
94 bundleExecutableFileSystemRepresentation, &image );
95 switch( imageErr ) {
96 case NSObjectFileImageFailure:
97 err = err_load_bundle_NSObjectFileImageFailure;
98 break;
99 case NSObjectFileImageSuccess:
100 // Not an error.
101 break;
102 case NSObjectFileImageInappropriateFile:
103 err = err_load_bundle_NSObjectFileImageInappropriateFile;
104 break;
105 case NSObjectFileImageArch:
106 err = err_load_bundle_NSObjectFileImageArch;
107 break;
108 case NSObjectFileImageFormat:
109 err = err_load_bundle_NSObjectFileImageFormat;
110 break;
111 case NSObjectFileImageAccess:
112 err = err_load_bundle_NSObjectFileImageAccess;
113 break;
114 default:
115 assert(0);
116 }
117 }
118
119 // Ensure we can link the image before actually attempting to link it.
120 if( !err ) {
121 assert( image );
122 unsigned long symbolIndex, symbolCount
123 = NSSymbolReferenceCountInObjectFileImage( image );
124 for( symbolIndex = 0; !err && symbolIndex < symbolCount;++symbolIndex) {
125 if( !NSIsSymbolNameDefined( NSSymbolReferenceNameInObjectFileImage(
126 image,
127 symbolIndex,
128 NULL ) ) )
129 {
130 err = err_load_bundle_undefined_symbol;
131 }
132 }
133 }
134
135 // Link.
136 if( !err ) {
137 NSModule module = NSLinkModule( image,
138 bundleExecutableFileSystemRepresentation,
139 NSLINKMODULE_OPTION_BINDNOW
140 |NSLINKMODULE_OPTION_PRIVATE
141 |NSLINKMODULE_OPTION_RETURN_ON_ERROR );
142 if( module == NULL )
143 err = err_load_bundle_link_failed;
144 }
145
146 return err;
147 }
Something went wrong with that request. Please try again.