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