2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
27 * Copyright 1988-1996, NeXT Software, Inc.
32 #import "objc-private.h"
33 #import <objc/objc-runtime.h>
34 #import <objc/hashtable2.h>
35 #import <objc/Object.h>
36 #import <objc/Protocol.h>
38 //#import <streams/streams.h>
40 #include <mach-o/dyld.h>
45 extern char * getsectdatafromheader (const headerType * mhp, const char * segname, const char * sectname, int * size);
48 OBJC_EXPORT void (*callbackFunction)( Class, const char * );
51 struct objc_method_list **get_base_method_list(Class cls) {
52 struct objc_method_list **ptr = ((struct objc_class * )cls)->methodLists;
53 if (!*ptr) return NULL;
54 while ( *ptr != 0 && *ptr != END_OF_METHODS_LIST ) { ptr++; }
60 /**********************************************************************************
63 * NOTE: Loading isn't really thread safe. If a load message recursively calls
64 * objc_loadModules() both sets will be loaded correctly, but if the original
65 * caller calls objc_unloadModules() it will probably unload the wrong modules.
66 * If a load message calls objc_unloadModules(), then it will unload
67 * the modules currently being loaded, which will probably cause a crash.
69 * Error handling is still somewhat crude. If we encounter errors while
70 * linking up classes or categories, we will not recover correctly.
72 * I removed attempts to lock the class hashtable, since this introduced
73 * deadlock which was hard to remove. The only way you can get into trouble
74 * is if one thread loads a module while another thread tries to access the
75 * loaded classes (using objc_lookUpClass) before the load is complete.
76 **********************************************************************************/
77 int objc_loadModule(const char *moduleName, void (*class_callback) (Class, const char *categoryName), int *errorCode)
81 NSObjectFileImage objectFileImage;
82 NSObjectFileImageReturnCode code;
84 // So we don't have to check this everywhere
85 if (errorCode == NULL)
86 errorCode = &locErrorCode;
88 if (moduleName == NULL)
90 *errorCode = NSObjectFileImageInappropriateFile;
94 if (_dyld_present () == 0)
96 *errorCode = NSObjectFileImageFailure;
100 callbackFunction = class_callback;
101 code = NSCreateObjectFileImageFromFile (moduleName, &objectFileImage);
102 if (code != NSObjectFileImageSuccess)
108 if (NSLinkModule(objectFileImage, moduleName, NSLINKMODULE_OPTION_RETURN_ON_ERROR) == NULL) {
109 NSLinkEditErrors error;
111 char *fileName, *errorString;
112 NSLinkEditError(&error, &errorNum, &fileName, &errorString);
113 // These errors may overlap with other errors that objc_loadModule returns in other failure cases.
117 callbackFunction = NULL;
123 /**********************************************************************************
125 **********************************************************************************/
126 /* Lock for dynamic loading and unloading. */
127 // static OBJC_DECLARE_LOCK (loadLock);
130 long objc_loadModules (char * modlist[],
132 void (*class_callback) (Class, const char *),
133 headerType ** hdr_addr,
143 for (modules = &modlist[0]; *modules != 0; modules++)
145 itWorked = objc_loadModule (*modules, class_callback, &code);
149 // NXPrintf ((NXStream *) errStream, "objc_loadModules(%s) code = %d\n", *modules, code);
160 /**********************************************************************************
161 * objc_unloadModules.
163 * NOTE: Unloading isn't really thread safe. If an unload message calls
164 * objc_loadModules() or objc_unloadModules(), then the current call
165 * to objc_unloadModules() will probably unload the wrong stuff.
166 **********************************************************************************/
168 long objc_unloadModules (void * errStream,
169 void (*unload_callback) (Class, Category))
171 headerType * header_addr = 0;
174 // TODO: to make unloading work, should get the current header
178 ; // TODO: unload the current header