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
|
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h>
/*
* This is a minimal implementation of the ELF dlopen, dlclose, dlsym
* and dlerror routines based on HP's shl_load, shl_unload and
* shl_findsym. */
/*
* Reference Counting.
*
* Empirically it looks like the HP routines do not mainain a
* reference count, so I maintain one here.
*/
typedef struct lib_entry {
shl_t handle;
int count;
struct lib_entry *next;
} *LibEntry;
#define lib_entry_handle(e) ((e)->handle)
#define lib_entry_count(e) ((e)->count)
#define lib_entry_next(e) ((e)->next)
#define set_lib_entry_handle(e,v) ((e)->handle = (v))
#define set_lib_entry_count(e,v) ((e)->count = (v))
#define set_lib_entry_next(e,v) ((e)->next = (v))
#define increment_lib_entry_count(e) ((e)->count++)
#define decrement_lib_entry_count(e) ((e)->count--)
static LibEntry Entries = NULL;
static LibEntry find_lib_entry(shl_t handle)
{
LibEntry entry;
for (entry = Entries; entry != NULL; entry = lib_entry_next(entry))
if (lib_entry_handle(entry) == handle)
return entry;
return NULL;
}
static LibEntry new_lib_entry(shl_t handle)
{
LibEntry entry;
if ((entry = (LibEntry) malloc(sizeof(struct lib_entry))) != NULL) {
set_lib_entry_handle(entry, handle);
set_lib_entry_count(entry, 1);
set_lib_entry_next(entry, Entries);
Entries = entry;
}
return entry;
}
static void free_lib_entry(LibEntry entry)
{
if (entry == Entries)
Entries = lib_entry_next(entry);
else {
LibEntry last, next;
for (last = Entries, next = lib_entry_next(last);
next != NULL;
last = next, next = lib_entry_next(last)) {
if (entry == next) {
set_lib_entry_next(last, lib_entry_next(entry));
break;
}
}
}
free(entry);
}
/*
* Error Handling.
*/
#define ERRBUFSIZE 1000
static char errbuf[ERRBUFSIZE];
static int dlerrno = 0;
char *dlerror(void)
{
return dlerrno ? errbuf : NULL;
}
/*
* Opening and Closing Liraries.
*/
void *dlopen(const char *fname, int mode)
{
shl_t handle;
LibEntry entry = NULL;
dlerrno = 0;
if (fname == NULL)
handle = PROG_HANDLE;
else {
handle = shl_load(fname, mode, 0L);
if (handle != NULL) {
if ((entry = find_lib_entry(handle)) == NULL) {
if ((entry = new_lib_entry(handle)) == NULL) {
shl_unload(handle);
handle = NULL;
}
}
else
increment_lib_entry_count(entry);
}
if (handle == NULL) {
char *errstr;
dlerrno = errno;
errstr = strerror(errno);
if (errno == NULL) errstr = "???";
sprintf(errbuf, "can't open %s: %s", fname, errstr);
}
}
#ifdef DEBUG
printf("opening library %s, handle = %x, count = %d\n",
fname, handle, entry ? lib_entry_count(entry) : -1);
if (dlerrno) printf("%s\n", dlerror());
#endif
return (void *) handle;
}
int dlclose(void *handle)
{
LibEntry entry;
#ifdef DEBUG
entry = find_lib_entry(handle);
printf("closing library handle = %x, count = %d\n",
handle, entry ? lib_entry_count(entry) : -1);
#endif
dlerrno = 0;
if ((shl_t) handle == PROG_HANDLE)
return 0; /* ignore attempts to close main program */
else {
if ((entry = find_lib_entry((shl_t) handle)) != NULL) {
decrement_lib_entry_count(entry);
if (lib_entry_count(entry) > 0)
return 0;
else {
/* unload once reference count reaches zero */
free_lib_entry(entry);
if (shl_unload((shl_t) handle) == 0)
return 0;
}
}
/* if you get to here, an error has occurred */
dlerrno = 1;
sprintf(errbuf, "attempt to close library failed");
#ifdef DEBUG
printf("%s\n", dlerror());
#endif
return -1;
}
}
/*
* Symbol Lookup.
*/
void *dlsym(void *handle, const char *name)
{
void *f;
shl_t myhandle;
dlerrno = 0;
myhandle = (handle == NULL) ? PROG_HANDLE : (shl_t) handle;
if (shl_findsym(&myhandle, name, TYPE_PROCEDURE, &f) != 0) {
dlerrno = 1;
sprintf(errbuf, "symbol %s not found", name);
f = NULL;
}
return(f);
}
|