| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "makeint.h" |
| 18 |
|
| 19 |
#if MAKE_LOAD |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
#include <ctype.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <dlfcn.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#define SYMBOL_EXTENSION "_gmk_setup" |
| 28 |
|
| 29 |
#include "debug.h" |
| 30 |
#include "filedef.h" |
| 31 |
#include "variable.h" |
| 32 |
|
| 33 |
|
| 34 |
#ifndef RTLD_GLOBAL |
| 35 |
# define RTLD_GLOBAL 0 |
| 36 |
#endif |
| 37 |
|
| 38 |
struct load_list |
| 39 |
{ |
| 40 |
struct load_list *next; |
| 41 |
const char *name; |
| 42 |
void *dlp; |
| 43 |
}; |
| 44 |
|
| 45 |
static struct load_list *loaded_syms = NULL; |
| 46 |
|
| 47 |
static load_func_t |
| 48 |
load_object (const gmk_floc *flocp, int noerror, const char *ldname, |
| 49 |
const char *symname) |
| 50 |
{ |
| 51 |
static void *global_dl = NULL; |
| 52 |
load_func_t symp; |
| 53 |
|
| 54 |
if (! global_dl) |
| 55 |
{ |
| 56 |
global_dl = dlopen (NULL, RTLD_NOW|RTLD_GLOBAL); |
| 57 |
if (! global_dl) |
| 58 |
{ |
| 59 |
const char *err = dlerror (); |
| 60 |
OS (fatal, flocp, _("Failed to open global symbol table: %s"), err); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
symp = (load_func_t) dlsym (global_dl, symname); |
| 65 |
if (! symp) |
| 66 |
{ |
| 67 |
struct load_list *new; |
| 68 |
void *dlp = NULL; |
| 69 |
|
| 70 |
|
| 71 |
if (! strchr (ldname, '/') |
| 72 |
#ifdef HAVE_DOS_PATHS |
| 73 |
&& ! strchr (ldname, '\\') |
| 74 |
#endif |
| 75 |
) |
| 76 |
dlp = dlopen (concat (2, "./", ldname), RTLD_LAZY|RTLD_GLOBAL); |
| 77 |
|
| 78 |
|
| 79 |
if (! dlp) |
| 80 |
dlp = dlopen (ldname, RTLD_LAZY|RTLD_GLOBAL); |
| 81 |
|
| 82 |
|
| 83 |
if (! dlp) |
| 84 |
{ |
| 85 |
const char *err = dlerror (); |
| 86 |
if (noerror) |
| 87 |
DB (DB_BASIC, ("%s", err)); |
| 88 |
else |
| 89 |
OS (error, flocp, "%s", err); |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
symp = (load_func_t) dlsym (dlp, "plugin_is_GPL_compatible"); |
| 95 |
if (! symp) |
| 96 |
OS (fatal, flocp, |
| 97 |
_("Loaded object %s is not declared to be GPL compatible"), |
| 98 |
ldname); |
| 99 |
|
| 100 |
symp = (load_func_t) dlsym (dlp, symname); |
| 101 |
if (! symp) |
| 102 |
{ |
| 103 |
const char *err = dlerror (); |
| 104 |
OSSS (fatal, flocp, _("Failed to load symbol %s from %s: %s"), |
| 105 |
symname, ldname, err); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
new = xmalloc (sizeof (struct load_list)); |
| 112 |
new->name = xstrdup (ldname); |
| 113 |
new->dlp = dlp; |
| 114 |
new->next = loaded_syms; |
| 115 |
loaded_syms = new; |
| 116 |
} |
| 117 |
|
| 118 |
return symp; |
| 119 |
} |
| 120 |
|
| 121 |
int |
| 122 |
load_file (const gmk_floc *flocp, const char **ldname, int noerror) |
| 123 |
{ |
| 124 |
size_t nmlen = strlen (*ldname); |
| 125 |
char *new = alloca (nmlen + CSTRLEN (SYMBOL_EXTENSION) + 1); |
| 126 |
char *symname = NULL; |
| 127 |
char *loaded; |
| 128 |
const char *fp; |
| 129 |
int r; |
| 130 |
load_func_t symp; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
fp = strchr (*ldname, '('); |
| 135 |
if (fp) |
| 136 |
{ |
| 137 |
const char *ep; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
ep = strchr (fp+1, ')'); |
| 143 |
if (ep && ep[1] == '\0') |
| 144 |
{ |
| 145 |
size_t l = fp - *ldname;; |
| 146 |
|
| 147 |
++fp; |
| 148 |
if (fp == ep) |
| 149 |
OS (fatal, flocp, _("Empty symbol name for load: %s"), *ldname); |
| 150 |
|
| 151 |
|
| 152 |
memcpy (new, *ldname, l); |
| 153 |
new[l] = '\0'; |
| 154 |
*ldname = new; |
| 155 |
nmlen = l; |
| 156 |
|
| 157 |
|
| 158 |
symname = new + l + 1; |
| 159 |
memcpy (symname, fp, ep - fp); |
| 160 |
symname[ep - fp] = '\0'; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
*ldname = strcache_add (*ldname); |
| 166 |
|
| 167 |
|
| 168 |
loaded = allocated_variable_expand ("$(.LOADED)"); |
| 169 |
fp = strstr (loaded, *ldname); |
| 170 |
r = fp && (fp==loaded || fp[-1]==' ') && (fp[nmlen]=='\0' || fp[nmlen]==' '); |
| 171 |
if (r) |
| 172 |
goto exit; |
| 173 |
|
| 174 |
|
| 175 |
if (! symname) |
| 176 |
{ |
| 177 |
char *p = new; |
| 178 |
|
| 179 |
fp = strrchr (*ldname, '/'); |
| 180 |
#ifdef HAVE_DOS_PATHS |
| 181 |
if (fp) |
| 182 |
{ |
| 183 |
const char *fp2 = strchr (fp, '\\'); |
| 184 |
|
| 185 |
if (fp2 > fp) |
| 186 |
fp = fp2; |
| 187 |
} |
| 188 |
else |
| 189 |
fp = strrchr (*ldname, '\\'); |
| 190 |
|
| 191 |
if (fp && *fp && fp[1] == ':') |
| 192 |
fp++; |
| 193 |
#endif |
| 194 |
if (!fp) |
| 195 |
fp = *ldname; |
| 196 |
else |
| 197 |
++fp; |
| 198 |
while (isalnum (*fp) || *fp == '_') |
| 199 |
*(p++) = *(fp++); |
| 200 |
strcpy (p, SYMBOL_EXTENSION); |
| 201 |
symname = new; |
| 202 |
} |
| 203 |
|
| 204 |
DB (DB_VERBOSE, (_("Loading symbol %s from %s\n"), symname, *ldname)); |
| 205 |
|
| 206 |
|
| 207 |
symp = load_object (flocp, noerror, *ldname, symname); |
| 208 |
if (! symp) |
| 209 |
return 0; |
| 210 |
|
| 211 |
|
| 212 |
r = (*symp) (flocp); |
| 213 |
|
| 214 |
|
| 215 |
if (r > 0) |
| 216 |
{ |
| 217 |
size_t loadlen = strlen (loaded); |
| 218 |
char *newval = alloca (loadlen + strlen (*ldname) + 2); |
| 219 |
|
| 220 |
if (loadlen) |
| 221 |
{ |
| 222 |
memcpy (newval, loaded, loadlen); |
| 223 |
newval[loadlen++] = ' '; |
| 224 |
} |
| 225 |
strcpy (&newval[loadlen], *ldname); |
| 226 |
do_variable_definition (flocp, ".LOADED", newval, o_default, f_simple, 0); |
| 227 |
} |
| 228 |
|
| 229 |
exit: |
| 230 |
free (loaded); |
| 231 |
return r; |
| 232 |
} |
| 233 |
|
| 234 |
void |
| 235 |
unload_file (const char *name) |
| 236 |
{ |
| 237 |
struct load_list *d; |
| 238 |
|
| 239 |
for (d = loaded_syms; d != NULL; d = d->next) |
| 240 |
if (streq (d->name, name) && d->dlp) |
| 241 |
{ |
| 242 |
if (dlclose (d->dlp)) |
| 243 |
perror_with_name ("dlclose: ", d->name); |
| 244 |
d->dlp = NULL; |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
#else |
| 250 |
|
| 251 |
int |
| 252 |
load_file (const floc *flocp, const char **ldname UNUSED, int noerror) |
| 253 |
{ |
| 254 |
if (! noerror) |
| 255 |
O (fatal, flocp, |
| 256 |
_("The 'load' operation is not supported on this platform.")); |
| 257 |
|
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
unload_file (const char *name UNUSED) |
| 263 |
{ |
| 264 |
O (fatal, NILF, "INTERNAL: Cannot unload when load is not supported!"); |
| 265 |
} |
| 266 |
|
| 267 |
#endif |