2 * Copyright (c) 1999-2001, 2004-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 * Copyright 1988-1996, NeXT Software, Inc.
31 #include "objc-private.h"
32 #include "objc-load.h"
34 #if !__OBJC2__ && !TARGET_OS_WIN32
36 extern void (*callbackFunction)( Class, Category );
39 /**********************************************************************************
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.
48 * Error handling is still somewhat crude. If we encounter errors while
49 * linking up classes or categories, we will not recover correctly.
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)
60 NSObjectFileImage objectFileImage;
61 NSObjectFileImageReturnCode code;
63 // So we don't have to check this everywhere
64 if (errorCode == NULL)
65 errorCode = &locErrorCode;
67 if (moduleName == NULL)
69 *errorCode = NSObjectFileImageInappropriateFile;
73 if (_dyld_present () == 0)
75 *errorCode = NSObjectFileImageFailure;
79 callbackFunction = class_callback;
80 code = NSCreateObjectFileImageFromFile (moduleName, &objectFileImage);
81 if (code != NSObjectFileImageSuccess)
87 if (NSLinkModule(objectFileImage, moduleName, NSLINKMODULE_OPTION_RETURN_ON_ERROR) == NULL) {
88 NSLinkEditErrors error;
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.
96 callbackFunction = NULL;
102 /**********************************************************************************
104 **********************************************************************************/
105 /* Lock for dynamic loading and unloading. */
106 // static OBJC_DECLARE_LOCK (loadLock);
109 long objc_loadModules (char * modlist[],
111 void (*class_callback) (Class, Category),
112 headerType ** hdr_addr,
122 for (modules = &modlist[0]; *modules != 0; modules++)
124 itWorked = objc_loadModule (*modules, class_callback, &code);
128 // NXPrintf ((NXStream *) errStream, "objc_loadModules(%s) code = %d\n", *modules, code);
139 /**********************************************************************************
140 * objc_unloadModules.
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 **********************************************************************************/
147 long objc_unloadModules (void * errStream,
148 void (*unload_callback) (Class, Category))
150 headerType * header_addr = 0;
153 // TODO: to make unloading work, should get the current header
157 ; // TODO: unload the current header