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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1997-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* provide dlopen/dlsym/dlerror interface
*
* David Korn
* Glenn Fowler
* AT&T Research
*/
#include <ast.h>
#include <dlldefs.h>
#include <error.h>
#define T(x) ERROR_dictionary(x)
#if _BLD_dll && defined(__EXPORT__)
#define extern __EXPORT__
#endif
#if _hdr_dlfcn && _lib_dlopen
/*
* standard
*/
# include <dlfcn.h>
#else
#if _hdr_dl
/*
* HP-UX
*/
# include <dl.h>
# ifndef BIND_FIRST
# define BIND_FIRST 0x4
# endif
# ifndef BIND_NOSTART
# define BIND_NOSTART 0x10
# endif
static shl_t all;
static int err;
extern void* dlopen(const char* path, int mode)
{
void* dll;
if (!path)
return (void*)&all;
if (mode)
mode = (BIND_IMMEDIATE|BIND_FIRST|BIND_NOSTART);
if (!(dll = (void*)shl_load(path, mode, 0L)))
err = errno;
return dll;
}
extern int dlclose(void* dll)
{
return 0;
}
extern void* dlsym(void* dll, const char* name)
{
shl_t handle;
long addr;
handle = dll == (void*)&all ? (shl_t)0 : (shl_t)dll;
if (shl_findsym(&handle, name, TYPE_UNDEFINED, &addr))
{
err = errno;
return 0;
}
return (void*)addr;
}
extern char* dlerror(void)
{
char* msg;
if (!err)
return 0;
msg = fmterror(err);
err = 0;
return msg;
}
#else
#if _sys_ldr && _lib_loadbind
/*
* rs6000
*/
# include <sys/ldr.h>
# include <xcoff.h>
/* xcoff module header */
struct hdr
{
struct filehdr f;
struct aouthdr a;
struct scnhdr s[1];
};
static struct ld_info* ld_info;
static unsigned int ld_info_size = 1024;
static void* last_module;
static int err;
extern void* dlopen(const char* path, int mode)
{
void* dll;
if (!(dll = (void*)load((char*)path, mode, getenv("LIBPATH"))))
err = errno;
return dll;
}
extern int dlclose(void* dll)
{
return 0;
}
static int getquery(void)
{
if (!ld_info)
ld_info = malloc(ld_info_size);
for (;;)
{
if (!ld_info)
return 1;
if (!loadquery(L_GETINFO, ld_info, ld_info_size))
return 0;
if (errno != ENOMEM)
return 1;
ld_info = realloc(ld_info, ld_info_size *= 2);
}
}
/* find the loaded module whose data area contains the
* address passed in. Remember that procedure pointers
* are implemented as pointers to descriptors in the
* data area of the module defining the procedure
*/
static struct ld_info* getinfo(void* module)
{
struct ld_info* info = ld_info;
register int n = 1;
if (!ld_info || module != last_module)
{
last_module = module;
if (getquery())
return 0;
info = ld_info;
}
while (n)
{
if ((char*)(info->ldinfo_dataorg) <= (char*)module &&
(char*)module <= ((char*)(info->ldinfo_dataorg)
+ (unsigned)(info->ldinfo_datasize)))
return info;
if (n=info->ldinfo_next)
info = (void*)((char*)info + n);
}
return 0;
}
static char* getloc(struct hdr* hdr, char* data, char* name)
{
struct ldhdr* ldhdr;
struct ldsym* ldsym;
ulong datareloc;
ulong textreloc;
int i;
/* data is relocated by the difference between
* its virtual origin and where it was
* actually placed
*/
/*N.B. o_sndata etc. are one based */
datareloc = (ulong)data - hdr->s[hdr->a.o_sndata-1].s_vaddr;
/*hdr is address of header, not text, so add text s_scnptr */
textreloc = (ulong)hdr + hdr->s[hdr->a.o_sntext-1].s_scnptr
- hdr->s[hdr->a.o_sntext-1].s_vaddr;
ldhdr = (void*)((char*)hdr+ hdr->s[hdr->a.o_snloader-1].s_scnptr);
ldsym = (void*) (ldhdr+1);
/* search the exports symbols */
for(i=0; i < ldhdr->l_nsyms;ldsym++,i++)
{
char *symname,symbuf[9];
char *loc;
/* the symbol name representation is a nuisance since
* 8 character names appear in l_name but may
* not be null terminated. This code works around
* that by brute force
*/
if (ldsym->l_zeroes)
{
symname = symbuf;
memcpy(symbuf,ldsym->l_name,8);
symbuf[8] = 0;
}
else
symname = (void*)(ldsym->l_offset + (ulong)ldhdr + ldhdr->l_stoff);
if (strcmp(symname,name))
continue;
loc = (char*)ldsym->l_value;
if ((ldsym->l_scnum==hdr->a.o_sndata) ||
(ldsym->l_scnum==hdr->a.o_snbss))
loc += datareloc;
else if (ldsym->l_scnum==hdr->a.o_sntext)
loc += textreloc;
return loc;
}
return 0;
}
extern void* dlsym(void* handle, const char* name)
{
void* addr;
struct ld_info* info;
if (!(info = getinfo(handle)) || !(addr = getloc(info->ldinfo_textorg,info->ldinfo_dataorg,(char*)name)))
{
err = errno;
return 0;
}
return addr;
}
extern char* dlerror(void)
{
char* msg;
if (!err)
return 0;
msg = fmterror(err);
err = 0;
return msg;
}
#else
#if _hdr_dll && _lib_dllload
/*
* MVS
*/
# include <dll.h>
static int err;
extern void* dlopen(const char* path, int mode)
{
void* dll;
NoP(mode);
if (!(dll = (void*)dllload(path)))
err = errno;
return dll;
}
extern int dlclose(void* dll)
{
return 0;
}
extern void* dlsym(void* handle, const char* name)
{
void* addr;
if (!(addr = (void*)dllqueryfn(handle, (char*)name)))
err = errno;
return addr;
}
extern char* dlerror(void)
{
char* msg;
if (!err)
return 0;
msg = fmterror(err);
err = 0;
return msg;
}
#else
#if _hdr_mach_o_dyld
/*
* mac[h]
*/
# include <mach-o/dyld.h>
typedef const struct mach_header* NSImage;
typedef struct Dll_s
{
unsigned long magic;
NSImage image;
NSModule module;
char path[1];
} Dll_t;
#define DL_MAGIC 0x04190c04
#define DL_NEXT ((Dll_t*)RTLD_NEXT)
static const char* dlmessage = "no error";
static const char e_cover[] = T("cannot access covered library");
static const char e_handle[] = T("invalid handle");
static const char e_space[] = T("out of space");
static const char e_static[] = T("image statically linked");
static const char e_undefined[] = T("undefined symbol");
static Dll_t global = { DL_MAGIC };
static void undefined(const char* name)
{
}
static NSModule multiple(NSSymbol sym, NSModule om, NSModule nm)
{
return om;
}
static void linkedit(NSLinkEditErrors c, int n, const char* f, const char* m)
{
dlmessage = m;
}
static NSLinkEditErrorHandlers handlers =
{
undefined, multiple, linkedit
};
extern void* dlopen(const char* path, int mode)
{
Dll_t* dll;
int i;
NSObjectFileImage image;
static int init = 0;
if (!_dyld_present())
{
dlmessage = e_static;
return 0;
}
if (!init)
{
init = 1;
NSInstallLinkEditErrorHandlers(&handlers);
}
if (!path)
dll = &global;
else if (!(dll = newof(0, Dll_t, 1, strlen(path))))
{
dlmessage = e_space;
return 0;
}
else
{
switch (NSCreateObjectFileImageFromFile(path, &image))
{
case NSObjectFileImageSuccess:
dll->module = NSLinkModule(image, path, (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW);
NSDestroyObjectFileImage(image);
if (!dll->module)
{
free(dll);
return 0;
}
break;
case NSObjectFileImageInappropriateFile:
dll->image = NSAddImage(path, 0);
if (!dll->image)
{
free(dll);
return 0;
}
break;
default:
free(dll);
return 0;
}
strcpy(dll->path, path);
dll->magic = DL_MAGIC;
}
return (void*)dll;
}
extern int dlclose(void* handle)
{
Dll_t* dll = (Dll_t*)handle;
if (!dll || dll == DL_NEXT || dll->magic != DL_MAGIC)
{
dlmessage = e_handle;
return -1;
}
if (dll->module)
NSUnLinkModule(dll->module, 0);
free(dll);
return 0;
}
static NSSymbol
lookup(Dll_t* dll, const char* name)
{
unsigned long pun;
void* address;
if (dll == DL_NEXT)
{
if (!_dyld_func_lookup(name, &pun))
return 0;
address = (NSSymbol)pun;
}
else if (dll->module)
address = NSLookupSymbolInModule(dll->module, name);
else if (dll->image)
{
if (!NSIsSymbolNameDefinedInImage(dll->image, name))
return 0;
address = NSLookupSymbolInImage(dll->image, name, 0);
}
else
{
if (!NSIsSymbolNameDefined(name))
return 0;
address = NSLookupAndBindSymbol(name);
}
if (address)
address = NSAddressOfSymbol(address);
return address;
}
extern void* dlsym(void* handle, const char* name)
{
Dll_t* dll = (Dll_t*)handle;
NSSymbol address;
char buf[1024];
if (!dll || dll != DL_NEXT && (dll->magic != DL_MAGIC || !dll->image && !dll->module))
{
dlmessage = e_handle;
return 0;
}
if (!(address = lookup(dll, name)) && name[0] != '_' && strlen(name) < (sizeof(buf) - 1))
{
buf[0] = '_';
strcpy(buf + 1, name);
address = lookup(dll, buf);
}
if (!address)
{
dlmessage = dll == DL_NEXT ? e_cover : e_undefined;
return 0;
}
return (void*)address;
}
extern char* dlerror(void)
{
char* msg;
msg = (char*)dlmessage;
dlmessage = 0;
return msg;
}
#else
/*
* punt
*/
static int err;
extern void* dlopen(const char* path, int mode)
{
err = 1;
return 0;
}
extern int dlclose(void* dll)
{
err = 1;
return 0;
}
extern void* dlsym(void* handle, const char* name)
{
err = 1;
return 0;
}
extern char* dlerror(void)
{
if (!err)
return 0;
err = 0;
return "dynamic linking not supported";
}
#endif
#endif
#endif
#endif
#endif
|