2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
28 * Copyright 1988-1996, NeXT Software, Inc.
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>
39 //#import <streams/streams.h>
41 #include <mach-o/dyld.h>
46 extern char * getsectdatafromheader (const headerType * mhp, const char * segname, const char * sectname, int * size);
49 OBJC_EXPORT void (*callbackFunction)( Class, const char * );
52 /**********************************************************************************
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.
61 * Error handling is still somewhat crude. If we encounter errors while
62 * linking up classes or categories, we will not recover correctly.
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)
73 NSObjectFileImage objectFileImage;
74 NSObjectFileImageReturnCode code;
76 // So we don't have to check this everywhere
77 if (errorCode == NULL)
78 errorCode = &locErrorCode;
80 if (moduleName == NULL)
82 *errorCode = NSObjectFileImageInappropriateFile;
86 if (_dyld_present () == 0)
88 *errorCode = NSObjectFileImageFailure;
92 callbackFunction = class_callback;
93 code = NSCreateObjectFileImageFromFile (moduleName, &objectFileImage);
94 if (code != NSObjectFileImageSuccess)
100 if (NSLinkModule(objectFileImage, moduleName, NSLINKMODULE_OPTION_RETURN_ON_ERROR) == NULL) {
101 NSLinkEditErrors error;
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.
109 callbackFunction = NULL;
115 /**********************************************************************************
117 **********************************************************************************/
118 /* Lock for dynamic loading and unloading. */
119 // static OBJC_DECLARE_LOCK (loadLock);
122 long objc_loadModules (char * modlist[],
124 void (*class_callback) (Class, const char *),
125 headerType ** hdr_addr,
135 for (modules = &modlist[0]; *modules != 0; modules++)
137 itWorked = objc_loadModule (*modules, class_callback, &code);
141 // NXPrintf ((NXStream *) errStream, "objc_loadModules(%s) code = %d\n", *modules, code);
152 /**********************************************************************************
153 * objc_unloadModules.
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 **********************************************************************************/
160 long objc_unloadModules (void * errStream,
161 void (*unload_callback) (Class, Category))
163 headerType * header_addr = 0;
166 // TODO: to make unloading work, should get the current header
170 ; // TODO: unload the current header