1996-01-17 00:33:08 +00:00
|
|
|
/* objc-load - Dynamically load in Obj-C modules (Classes, Categories)
|
1995-04-03 20:49:14 +00:00
|
|
|
|
1997-01-06 22:00:31 +00:00
|
|
|
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
|
1996-01-17 00:33:08 +00:00
|
|
|
|
|
|
|
Written by: Adam Fedor, Pedja Bogdanovich
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1996-01-16 23:36:19 +00:00
|
|
|
|
1996-01-17 00:33:08 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1996-01-17 00:33:08 +00:00
|
|
|
*/
|
1995-04-03 20:49:14 +00:00
|
|
|
|
1996-01-16 23:36:19 +00:00
|
|
|
/*
|
|
|
|
CAVEATS:
|
1995-04-03 20:49:14 +00:00
|
|
|
- unloading modules not implemented
|
|
|
|
- would like to raise exceptions without having to turn this into
|
|
|
|
a .m file (right now NSBundle does this for us, ok?)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <objc/objc-api.h>
|
2001-05-31 22:39:16 +00:00
|
|
|
#ifndef NeXT_RUNTIME
|
1996-02-13 15:43:30 +00:00
|
|
|
#include <objc/objc-list.h>
|
2001-07-11 19:24:25 +00:00
|
|
|
#else
|
|
|
|
#include <objc/objc-load.h>
|
2001-05-31 22:39:16 +00:00
|
|
|
#endif
|
1997-11-12 15:37:27 +00:00
|
|
|
#include <config.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/objc-load.h>
|
2000-01-16 09:56:12 +00:00
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSDebug.h>
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
/* include the interface to the dynamic linker */
|
|
|
|
#include "dynamic-load.h"
|
|
|
|
|
|
|
|
/* From the objc runtime -- needed when invalidating the dtable */
|
2001-07-10 03:23:39 +00:00
|
|
|
#ifndef NeXT_RUNTIME
|
1995-04-03 20:49:14 +00:00
|
|
|
extern void __objc_install_premature_dtable(Class);
|
|
|
|
extern void sarray_free(struct sarray*);
|
2000-06-29 03:51:06 +00:00
|
|
|
#ifndef HAVE_OBJC_GET_UNINSTALLED_DTABLE
|
2000-06-30 22:42:48 +00:00
|
|
|
#ifndef objc_EXPORT
|
|
|
|
#define objc_EXPORT export
|
|
|
|
#endif
|
|
|
|
objc_EXPORT void *__objc_uninstalled_dtable;
|
2000-06-29 03:51:06 +00:00
|
|
|
static void *
|
|
|
|
objc_get_uninstalled_dtable()
|
|
|
|
{
|
|
|
|
return __objc_uninstalled_dtable;
|
|
|
|
}
|
|
|
|
#endif
|
2001-07-10 03:23:39 +00:00
|
|
|
#endif /* ! NeXT */
|
1995-04-03 20:49:14 +00:00
|
|
|
|
1996-01-21 01:36:47 +00:00
|
|
|
/* Declaration from NSBundle.m */
|
|
|
|
const char *objc_executable_location( void );
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
/* dynamic_loaded is YES if the dynamic loader was sucessfully initialized. */
|
|
|
|
static BOOL dynamic_loaded;
|
|
|
|
|
|
|
|
/* Our current callback function */
|
2001-07-17 02:56:43 +00:00
|
|
|
void (*_objc_load_load_callback)(Class, struct objc_category *) = 0;
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
/* List of modules we have loaded (by handle) */
|
2001-05-31 22:39:16 +00:00
|
|
|
#ifndef NeXT_RUNTIME
|
1995-04-03 20:49:14 +00:00
|
|
|
static struct objc_list *dynamic_handles = NULL;
|
2001-05-31 22:39:16 +00:00
|
|
|
#endif
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
/* Check to see if there are any undefined symbols. Print them out.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
objc_check_undefineds(FILE *errorStream)
|
|
|
|
{
|
2001-08-30 19:18:26 +00:00
|
|
|
int count = __objc_dynamic_undefined_symbol_count();
|
|
|
|
|
|
|
|
if (count != 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char **undefs;
|
1995-04-03 20:49:14 +00:00
|
|
|
|
2001-08-30 19:18:26 +00:00
|
|
|
undefs = __objc_dynamic_list_undefined_symbols();
|
|
|
|
if (errorStream)
|
|
|
|
{
|
|
|
|
fprintf(errorStream, "Undefined symbols:\n");
|
|
|
|
}
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (errorStream)
|
|
|
|
{
|
|
|
|
fprintf(errorStream, " %s\n", undefs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2001-08-30 19:18:26 +00:00
|
|
|
return 0;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Invalidate the dtable so it will be rebuild when a message is sent to
|
|
|
|
the object */
|
|
|
|
static void
|
|
|
|
objc_invalidate_dtable(Class class)
|
|
|
|
{
|
2001-07-10 03:23:39 +00:00
|
|
|
#ifndef NeXT_RUNTIME
|
2001-08-30 19:18:26 +00:00
|
|
|
Class s;
|
|
|
|
|
|
|
|
if (class->dtable == objc_get_uninstalled_dtable())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sarray_free(class->dtable);
|
|
|
|
__objc_install_premature_dtable(class);
|
|
|
|
for (s = class->subclass_list; s; s = s->sibling_class)
|
|
|
|
{
|
|
|
|
objc_invalidate_dtable(s);
|
|
|
|
}
|
2001-07-10 03:23:39 +00:00
|
|
|
#endif
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize for dynamic loading */
|
|
|
|
static int
|
|
|
|
objc_initialize_loading(FILE *errorStream)
|
|
|
|
{
|
2001-08-30 19:18:26 +00:00
|
|
|
const char *path;
|
|
|
|
|
|
|
|
dynamic_loaded = NO;
|
|
|
|
path = objc_executable_location();
|
|
|
|
|
|
|
|
NSDebugFLLog(@"NSBundle",
|
|
|
|
@"Debug (objc-load): initializing dynamic loader for %s",
|
|
|
|
path);
|
|
|
|
|
|
|
|
if (__objc_dynamic_init(path))
|
|
|
|
{
|
|
|
|
if (errorStream)
|
|
|
|
{
|
|
|
|
__objc_dynamic_error(errorStream,
|
|
|
|
"Error (objc-load): Cannot initialize dynamic linker");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dynamic_loaded = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A callback received from the Object initializer (_objc_exec_class).
|
|
|
|
Do what we need to do and call our own callback.
|
|
|
|
*/
|
|
|
|
static void
|
2001-07-17 02:56:43 +00:00
|
|
|
objc_load_callback(Class class, struct objc_category * category)
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-08-30 19:18:26 +00:00
|
|
|
/* Invalidate the dtable, so it will be rebuilt correctly */
|
|
|
|
if (class != 0 && category != 0)
|
|
|
|
{
|
|
|
|
objc_invalidate_dtable(class);
|
|
|
|
objc_invalidate_dtable(class->class_pointer);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2001-08-30 19:18:26 +00:00
|
|
|
if (_objc_load_load_callback)
|
|
|
|
{
|
|
|
|
_objc_load_load_callback(class, category);
|
|
|
|
}
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2001-08-30 19:18:26 +00:00
|
|
|
objc_load_module (const char *filename,
|
|
|
|
FILE *errorStream,
|
|
|
|
void (*loadCallback)(Class, struct objc_category *),
|
|
|
|
void **header,
|
|
|
|
char *debugFilename)
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-07-10 03:23:39 +00:00
|
|
|
#ifdef NeXT_RUNTIME
|
|
|
|
int errcode;
|
|
|
|
dynamic_loaded = YES;
|
|
|
|
return objc_loadModule(filename, loadCallback, &errcode);
|
|
|
|
#else
|
2001-08-30 19:18:26 +00:00
|
|
|
typedef void (*void_fn)();
|
|
|
|
dl_handle_t handle;
|
1996-05-08 14:56:44 +00:00
|
|
|
#if !defined(__ELF__) && !defined(CON_AUTOLOAD)
|
2001-08-30 19:18:26 +00:00
|
|
|
void_fn *ctor_list;
|
|
|
|
int i;
|
1996-05-08 14:56:44 +00:00
|
|
|
#endif
|
2001-08-30 19:18:26 +00:00
|
|
|
|
|
|
|
if (!dynamic_loaded)
|
|
|
|
{
|
|
|
|
if (objc_initialize_loading(errorStream))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2001-08-30 19:18:26 +00:00
|
|
|
|
|
|
|
_objc_load_load_callback = loadCallback;
|
|
|
|
_objc_load_callback = objc_load_callback;
|
|
|
|
|
|
|
|
/* Link in the object file */
|
|
|
|
NSDebugFLLog(@"NSBundle",
|
|
|
|
@"Debug (objc-load): Linking file %s\n", filename);
|
|
|
|
handle = __objc_dynamic_link(filename, 1, debugFilename);
|
|
|
|
if (handle == 0)
|
|
|
|
{
|
|
|
|
if (errorStream)
|
|
|
|
{
|
|
|
|
__objc_dynamic_error(errorStream, "Error (objc-load)");
|
|
|
|
}
|
2001-08-30 19:24:49 +00:00
|
|
|
_objc_load_load_callback = 0;
|
|
|
|
_objc_load_callback = 0;
|
2001-08-30 19:18:26 +00:00
|
|
|
return 1;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2001-08-30 19:18:26 +00:00
|
|
|
dynamic_handles = list_cons(handle, dynamic_handles);
|
|
|
|
|
|
|
|
/* If there are any undefined symbols, we can't load the bundle */
|
|
|
|
if (objc_check_undefineds(errorStream))
|
|
|
|
{
|
|
|
|
__objc_dynamic_unlink(handle);
|
2001-08-30 19:24:49 +00:00
|
|
|
_objc_load_load_callback = 0;
|
|
|
|
_objc_load_callback = 0;
|
2001-08-30 19:18:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1996-01-16 23:36:19 +00:00
|
|
|
#if !defined(__ELF__) && !defined(CON_AUTOLOAD)
|
2001-08-30 19:18:26 +00:00
|
|
|
/* Get the constructor list and load in the objects */
|
|
|
|
ctor_list = (void_fn *)__objc_dynamic_find_symbol(handle, CTOR_LIST);
|
|
|
|
if (!ctor_list)
|
|
|
|
{
|
|
|
|
if (errorStream)
|
|
|
|
{
|
|
|
|
fprintf(errorStream,
|
|
|
|
"Error (objc-load): Cannot load objects (no CTOR list)\n");
|
|
|
|
}
|
2001-08-30 19:24:49 +00:00
|
|
|
_objc_load_load_callback = 0;
|
|
|
|
_objc_load_callback = 0;
|
2001-08-30 19:18:26 +00:00
|
|
|
return 1;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2001-08-30 19:18:26 +00:00
|
|
|
|
|
|
|
NSDebugFLLog(@"NSBundle",
|
|
|
|
@"Debug (objc-load): %d modules\n", (int)ctor_list[0]);
|
|
|
|
for (i = 1; ctor_list[i]; i++)
|
|
|
|
{
|
|
|
|
NSDebugFLLog(@"NSBundle",
|
|
|
|
@"Debug (objc-load): Invoking CTOR %p\n", ctor_list[i]);
|
|
|
|
ctor_list[i]();
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
1995-05-26 17:54:55 +00:00
|
|
|
#endif /* not __ELF__ */
|
2001-08-30 19:18:26 +00:00
|
|
|
|
|
|
|
_objc_load_callback = 0;
|
|
|
|
_objc_load_load_callback = 0;
|
|
|
|
return 0;
|
2001-07-10 03:23:39 +00:00
|
|
|
#endif
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2001-08-30 19:18:26 +00:00
|
|
|
objc_unload_module(FILE *errorStream,
|
|
|
|
void (*unloadCallback)(Class, struct objc_category *))
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-08-30 19:18:26 +00:00
|
|
|
if (!dynamic_loaded)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorStream)
|
|
|
|
{
|
|
|
|
fprintf(errorStream, "Warning: unloading modules not implemented\n");
|
|
|
|
}
|
|
|
|
return 0;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1996-10-31 17:17:59 +00:00
|
|
|
long objc_load_modules(char *files[],FILE *errorStream,
|
2001-08-30 19:18:26 +00:00
|
|
|
void (*callback)(Class,struct objc_category *),
|
|
|
|
void **header,
|
|
|
|
char *debugFilename)
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-08-30 19:18:26 +00:00
|
|
|
while (*files)
|
|
|
|
{
|
1995-04-03 20:49:14 +00:00
|
|
|
if (objc_load_module(*files, errorStream, callback,
|
2001-08-30 19:18:26 +00:00
|
|
|
(void *)header, debugFilename))
|
|
|
|
{
|
1995-04-03 20:49:14 +00:00
|
|
|
return 1;
|
2001-08-30 19:18:26 +00:00
|
|
|
}
|
1995-04-03 20:49:14 +00:00
|
|
|
files++;
|
2001-08-30 19:18:26 +00:00
|
|
|
}
|
1995-04-03 20:49:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2001-08-30 19:18:26 +00:00
|
|
|
objc_unload_modules(FILE *errorStream,
|
|
|
|
void (*unloadCallback)(Class, struct objc_category *))
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-08-30 19:18:26 +00:00
|
|
|
if (!dynamic_loaded)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorStream)
|
|
|
|
{
|
|
|
|
fprintf(errorStream, "Warning: unloading modules not implemented\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2000-10-28 21:58:48 +00:00
|
|
|
|
|
|
|
NSString *
|
2001-07-10 03:23:39 +00:00
|
|
|
objc_get_symbol_path(Class theClass, struct objc_category *theCategory)
|
2000-10-28 21:58:48 +00:00
|
|
|
{
|
|
|
|
const char *ret;
|
|
|
|
char buf[125], *p = buf;
|
|
|
|
int len = strlen(theClass->name);
|
2001-08-30 19:18:26 +00:00
|
|
|
|
2000-10-28 21:58:48 +00:00
|
|
|
if (!theCategory)
|
|
|
|
{
|
|
|
|
if (len+sizeof(char)*19 > sizeof(buf))
|
|
|
|
{
|
|
|
|
p = malloc(len+sizeof(char)*19);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to allocate memory !!");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf, "__objc_class_name_", sizeof(char)*18);
|
|
|
|
memcpy(&buf[18*sizeof(char)], theClass->name,
|
|
|
|
strlen(theClass->name)+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len += strlen(theCategory->category_name);
|
|
|
|
|
2001-08-30 19:18:26 +00:00
|
|
|
if (len + sizeof(char)*23 > sizeof(buf))
|
2000-10-28 21:58:48 +00:00
|
|
|
{
|
2001-08-30 19:18:26 +00:00
|
|
|
p = malloc(len + sizeof(char)*23);
|
2000-10-28 21:58:48 +00:00
|
|
|
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to allocate memory !!");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf, "__objc_category_name_", sizeof(char)*21);
|
|
|
|
memcpy(&buf[21*sizeof(char)], theCategory->class_name,
|
|
|
|
strlen(theCategory->class_name)+1);
|
|
|
|
memcpy(&buf[strlen(p)], "_", 2*sizeof(char));
|
|
|
|
memcpy(&buf[strlen(p)], theCategory->category_name,
|
|
|
|
strlen(theCategory->category_name)+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = __objc_dynamic_get_symbol_path(0, p);
|
|
|
|
|
|
|
|
if (p != buf)
|
2001-08-30 19:18:26 +00:00
|
|
|
{
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2000-10-28 21:58:48 +00:00
|
|
|
if (ret)
|
2001-08-30 19:18:26 +00:00
|
|
|
{
|
|
|
|
return [NSString stringWithCString:ret];
|
|
|
|
}
|
|
|
|
|
2000-10-28 21:58:48 +00:00
|
|
|
return nil;
|
|
|
|
}
|