[383a00]: src / runtime / ppc-darwin-dlshim.c Maximize Restore History

Download this file

ppc-darwin-dlshim.c    200 lines (185 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* These functions emulate a small subset of the dlopen / dlsym
* functionality under Darwin's Mach-O dyld system.
*/
/*
* This software is part of the SBCL system. See the README file for
* more information.
*
* This software is derived from the CMU CL system, which was
* written at Carnegie Mellon University and released into the
* public domain. The software is in the public domain and is
* provided with absolutely no warranty. See the COPYING and CREDITS
* files for more information.
*/
#include <mach-o/dyld.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ppc-darwin-dlshim.h"
/* Darwin does not define the standard ELF
* dlopen/dlclose/dlsym/dlerror interface to shared libraries, so this
* is an attempt at a minimal wrapper to allow SBCL to work without
* external dependency on pogma's dlcompat library.
*/
/* For now, there is no RTLD_GLOBAL emulation either. */
static char dl_self; /* I'm going to abuse this */
static int callback_count;
static struct mach_header* last_header;
static int last_was_error = 0;
void dlshim_image_callback(struct mach_header* ptr, unsigned long phooey)
{
callback_count++;
last_header = ptr;
}
int lib_path_count(void)
{
char* libpath;
int i;
int count;
libpath = getenv("DYLD_LIBRARY_PATH");
count = 1;
if (libpath) {
for (i = 0; libpath[i] != '\0'; i++) {
if (libpath[i] == ':') count++;
}
}
return count;
}
const char* lib_path_prefixify(int index, const char* filename)
{
static char* retbuf = NULL;
int fi, li, i, count;
char* libpath;
if (!retbuf) {
retbuf = (char*) malloc(1024*sizeof(char));
}
count = 0;
fi = 0;
li = -1;
libpath = getenv("DYLD_LIBRARY_PATH");
if (libpath) {
i = 0;
while (count != index && libpath[i] != '\0') {
if (libpath[i] == ':') count++;
i++;
}
fi = i;
while (libpath[i] != '\0' && libpath[i] != ':') {
i++;
}
li = i - 1;
}
if (li - fi > 0) {
if (li - fi + 1 > 1022 - strlen(filename)) {
retbuf = (char*) realloc(retbuf, (li - fi + 3 + strlen(filename))*sizeof(char));
}
memcpy(retbuf, libpath + fi, (li - fi + 1)*sizeof(char));
retbuf[li - fi + 1] = '/';
memcpy(retbuf + li - fi + 2, filename, strlen(filename) + 1);
return retbuf;
} else {
return filename;
}
}
void* dlopen(const char* filename, int flags)
{
static char has_callback = 0;
if (!has_callback) {
_dyld_register_func_for_add_image(dlshim_image_callback);
}
if (!filename) {
return &dl_self;
} else {
struct mach_header* img = NULL;
if (!img) img = NSAddImage(filename, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
if (!img) img = NSAddImage(filename, NSADDIMAGE_OPTION_RETURN_ON_ERROR | NSADDIMAGE_OPTION_WITH_SEARCHING);
if (!img) {
NSObjectFileImage fileImage;
callback_count = 0;
last_header = NULL;
if (NSCreateObjectFileImageFromFile(filename, &fileImage) == NSObjectFileImageSuccess) {
NSLinkModule(fileImage, filename, NSLINKMODULE_OPTION_BINDNOW | ((flags & RTLD_GLOBAL)?NSLINKMODULE_OPTION_PRIVATE:0) | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
if (callback_count && last_header) img = last_header;
}
}
if (!img) {
NSObjectFileImage fileImage;
int i, maxi;
char* prefixfilename;
maxi = lib_path_count();
for (i = 0; i < maxi && !img; i++) {
prefixfilename = lib_path_prefixify(i, filename);
callback_count = 0;
last_header = NULL;
if (NSCreateObjectFileImageFromFile(prefixfilename, &fileImage) == NSObjectFileImageSuccess) {
NSLinkModule(fileImage, filename, NSLINKMODULE_OPTION_BINDNOW | ((flags & RTLD_GLOBAL)?NSLINKMODULE_OPTION_PRIVATE:0) | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
if (callback_count && last_header) img = last_header;
}
}
}
if (img) {
if (flags & RTLD_NOW) {
NSLookupSymbolInImage(img, "", NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
}
if (NSIsSymbolNameDefinedInImage(img, "__init")) {
NSSymbol* initsymbol;
void (*initfunc) (void);
initsymbol = NSLookupSymbolInImage(img, "__init", 0);
initfunc = NSAddressOfSymbol(initsymbol);
initfunc();
}
}
return img;
}
}
const char* dlerror()
{
static char* errbuf = NULL;
NSLinkEditErrors a;
int b;
char *c, *d;
NSLinkEditError(&a, &b, &c, &d);
if (!errbuf) {
errbuf = (char*) malloc(256*sizeof(char));
}
if (!(c || d)) {
last_was_error = 0;
snprintf(errbuf, 255, "%s in %s: %d %d", c, d, a, b);
return errbuf;
} else if (last_was_error) {
last_was_error = 0;
snprintf(errbuf, 255, "Can't find symbol");
return errbuf;
}
last_was_error = 0;
return NULL;
}
void* dlsym(void* handle, char* symbol)
{
if (handle == &dl_self) {
if (NSIsSymbolNameDefined(symbol)) {
NSSymbol* retsym;
retsym = NSLookupAndBindSymbol(symbol);
return NSAddressOfSymbol(retsym);
} else {
last_was_error = 1;
return NULL;
}
} else {
if (NSIsSymbolNameDefinedInImage(handle, symbol)) {
NSSymbol* retsym;
retsym = NSLookupSymbolInImage(handle, symbol, 0);
return NSAddressOfSymbol(retsym);
} else {
last_was_error = 1;
return NULL;
}
}
}
int dlclose(void *handle)
{
/* dlclose is not implemented, and never will be for dylibs.
* return -1 to signal an error; it's not used by SBCL anyhow */
return -1;
}