]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-load.m
objc4-493.11.tar.gz
[apple/objc4.git] / runtime / objc-load.m
1 /*
2 * Copyright (c) 1999-2001, 2004-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * objc-load.m
26 * Copyright 1988-1996, NeXT Software, Inc.
27 * Author: s. naroff
28 *
29 */
30
31 #include "objc-private.h"
32 #include "objc-load.h"
33
34 #if !__OBJC2__ && !TARGET_OS_WIN32
35
36 extern void (*callbackFunction)( Class, Category );
37
38
39 /**********************************************************************************
40 * objc_loadModule.
41 *
42 * NOTE: Loading isn't really thread safe. If a load message recursively calls
43 * objc_loadModules() both sets will be loaded correctly, but if the original
44 * caller calls objc_unloadModules() it will probably unload the wrong modules.
45 * If a load message calls objc_unloadModules(), then it will unload
46 * the modules currently being loaded, which will probably cause a crash.
47 *
48 * Error handling is still somewhat crude. If we encounter errors while
49 * linking up classes or categories, we will not recover correctly.
50 *
51 * I removed attempts to lock the class hashtable, since this introduced
52 * deadlock which was hard to remove. The only way you can get into trouble
53 * is if one thread loads a module while another thread tries to access the
54 * loaded classes (using objc_lookUpClass) before the load is complete.
55 **********************************************************************************/
56 int objc_loadModule(char *moduleName, void (*class_callback) (Class, Category), int *errorCode)
57 {
58 int successFlag = 1;
59 int locErrorCode;
60 NSObjectFileImage objectFileImage;
61 NSObjectFileImageReturnCode code;
62
63 // So we don't have to check this everywhere
64 if (errorCode == NULL)
65 errorCode = &locErrorCode;
66
67 if (moduleName == NULL)
68 {
69 *errorCode = NSObjectFileImageInappropriateFile;
70 return 0;
71 }
72
73 if (_dyld_present () == 0)
74 {
75 *errorCode = NSObjectFileImageFailure;
76 return 0;
77 }
78
79 callbackFunction = class_callback;
80 code = NSCreateObjectFileImageFromFile (moduleName, &objectFileImage);
81 if (code != NSObjectFileImageSuccess)
82 {
83 *errorCode = code;
84 return 0;
85 }
86
87 if (NSLinkModule(objectFileImage, moduleName, NSLINKMODULE_OPTION_RETURN_ON_ERROR) == NULL) {
88 NSLinkEditErrors error;
89 int errorNum;
90 const char *fileName, *errorString;
91 NSLinkEditError(&error, &errorNum, &fileName, &errorString);
92 // These errors may overlap with other errors that objc_loadModule returns in other failure cases.
93 *errorCode = error;
94 return 0;
95 }
96 callbackFunction = NULL;
97
98
99 return successFlag;
100 }
101
102 /**********************************************************************************
103 * objc_loadModules.
104 **********************************************************************************/
105 /* Lock for dynamic loading and unloading. */
106 // static OBJC_DECLARE_LOCK (loadLock);
107
108
109 long objc_loadModules (char * modlist[],
110 void * errStream,
111 void (*class_callback) (Class, Category),
112 headerType ** hdr_addr,
113 char * debug_file)
114 {
115 char ** modules;
116 int code;
117 int itWorked;
118
119 if (modlist == 0)
120 return 0;
121
122 for (modules = &modlist[0]; *modules != 0; modules++)
123 {
124 itWorked = objc_loadModule (*modules, class_callback, &code);
125 if (itWorked == 0)
126 {
127 //if (errStream)
128 // NXPrintf ((NXStream *) errStream, "objc_loadModules(%s) code = %d\n", *modules, code);
129 return 1;
130 }
131
132 if (hdr_addr)
133 *(hdr_addr++) = 0;
134 }
135
136 return 0;
137 }
138
139 /**********************************************************************************
140 * objc_unloadModules.
141 *
142 * NOTE: Unloading isn't really thread safe. If an unload message calls
143 * objc_loadModules() or objc_unloadModules(), then the current call
144 * to objc_unloadModules() will probably unload the wrong stuff.
145 **********************************************************************************/
146
147 long objc_unloadModules (void * errStream,
148 void (*unload_callback) (Class, Category))
149 {
150 headerType * header_addr = 0;
151 int errflag = 0;
152
153 // TODO: to make unloading work, should get the current header
154
155 if (header_addr)
156 {
157 ; // TODO: unload the current header
158 }
159 else
160 {
161 errflag = 1;
162 }
163
164 return errflag;
165 }
166
167 #endif