pike.git/
src/
dynamic_load.c
Branch:
Tag:
Non-build tags
All tags
No tags
2004-03-21
2004-03-21 17:59:24 by Henrik Grubbström (Grubba) <grubba@grubba.org>
fac0415207239702df876d7062257332a1122892 (
88
lines) (+
63
/-
25
)
[
Show
|
Annotate
]
Branch:
7.9
Code cleanups in the dyld case.
Rev: src/dynamic_load.c:1.77
2:
|| This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information.
-
|| $Id: dynamic_load.c,v 1.
76
2004/03/21 17:
19
:
40
grubba Exp $
+
|| $Id: dynamic_load.c,v 1.
77
2004/03/21 17:
59
:
24
grubba Exp $
*/ #ifdef TESTING
24:
# include "language.h" # include "lex.h"
-
RCSID("$Id: dynamic_load.c,v 1.
76
2004/03/21 17:
19
:
40
grubba Exp $");
+
RCSID("$Id: dynamic_load.c,v 1.
77
2004/03/21 17:
59
:
24
grubba Exp $");
#else /* TESTING */
243:
#define dlinit() _dyld_present()
+
struct pike_dl_handle
+
{
+
NSObjectFileImage image;
+
NSModule module;
+
};
+
+
static void *dlclose(void *handle_)
+
{
+
struct pike_dl_handle *handle = handle_;
+
if (handle) {
+
if (handle->module)
+
NSUnLinkModule(handle->module, NSUNLINKMODULE_OPTION_NONE);
+
handle->module = NULL;
+
if (handle->image)
+
NSDestroyObjectFileImage(handle->image);
+
handle->image = NULL;
+
free(handle);
+
}
+
return NULL;
+
}
+
+
static char *pike_dl_error = NULL;
+
static void *dlopen(const char *module_name, int how) {
-
+
struct pike_dl_handle *handle = malloc(sizeof(struct pike_dl_handle));
NSObjectFileImageReturnCode code = 0;
-
NSObjectFileImage image = NULL;
+
-
+
pike_dl_error = NULL;
+
if (!handle) {
+
pike_dl_error = "Out of memory.";
+
return NULL;
+
}
+
+
handle->image = NULL;
+
handle->module = NULL;
+
/* FIXME: Should be fixed to detect if the module already is loaded. */
-
if ((code = NSCreateObjectFileImageFromFile(module_name, &image)) !=
+
if ((code = NSCreateObjectFileImageFromFile(module_name,
+
&
handle->
image)) !=
NSObjectFileImageSuccess) {
-
+
#ifdef PIKE_DEBUG
fprintf(stderr, "NSCreateObjectFileImageFromFile(\"%s\") failed with %d\n", module_name, code);
-
+
#endif /* PIKE_DEBUG */
+
pike_dl_error = "NSCreateObjectFileImageFromFile() failed.";
+
dlclose(handle);
return NULL; }
-
/* FIXME: image should be freed somewhere! */
+
-
fprintf(stderr, "dlopen(\"%s\") ==
>
image:%p\n",
-
module
_name,
image);
-
-
return
NSLinkModule(image, module_name,
+
handle-
>module
=
NSLinkModule(
handle->
image, module_name,
how | NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE);
-
+
if (!handle->module) {
+
dlclose(handle);
+
return NULL;
}
-
-
static
char
*dlerror(void)
-
{
-
NSLinkEditErrors class = 0;
-
int error_number = 0;
-
char *file_name = NULL;
-
char *error_string = NULL;
-
NSLinkEditError(&class, &error_number, &file_name, &error_string);
-
return
error_string
;
+
return
handle
;
}
-
static void *dlsym(void *
module
, char *function)
+
static void *dlsym(void *
handle
, char *function)
{
-
NSSymbol symbol = NSLookupSymbolInModule(module, function);
+
NSSymbol symbol =
+
NSLookupSymbolInModule(
((struct pike_dl_handle *)handle)->
module,
+
function);
return symbol?NSAddressOfSymbol(symbol):NULL; }
-
static
void
*
dlclose
(void
*module
)
+
static
const
char
*
dlerror
(void)
{
-
NSUnLinkModule
(
module
,
NSUNLINKMODULE
_
OPTION
_
NONE
);
-
return
NULL
;
+
NSLinkEditErrors class = 0;
+
int error_number = 0;
+
const char *file_name = NULL;
+
const char *error_string = NULL;
+
+
if
(
pike_dl_error) return pike_dl_error;
+
+
NSLinkEditError(&class
,
&error
_
number, &file
_
name, &error_string
);
+
return
error_string
;
} #endif /* USE_DYLD */