-
Notifications
You must be signed in to change notification settings - Fork 0
/
dln.c
550 lines (476 loc) · 13.4 KB
/
dln.c
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/**********************************************************************
dln.c -
$Author$
created at: Tue Jan 18 17:05:06 JST 1994
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#ifdef RUBY_EXPORT
#include "ruby/ruby.h"
#define dln_notimplement rb_notimplement
#define dln_memerror rb_memerror
#define dln_exit rb_exit
#define dln_loaderror rb_loaderror
#define dln_fatalerror rb_fatal
#else
#define dln_notimplement --->>> dln not implemented <<<---
#define dln_memerror abort
#define dln_exit exit
static void dln_loaderror(const char *format, ...);
#define dln_fatalerror dln_loaderror
#endif
#include "dln.h"
#include "internal.h"
#include "internal/compilers.h"
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if defined(HAVE_ALLOCA_H)
#include <alloca.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#else
# include <strings.h>
#endif
#if defined __APPLE__
# include <AvailabilityMacros.h>
#endif
#ifndef xmalloc
void *xmalloc();
void *xcalloc();
void *xrealloc();
#endif
#undef free
#define free(x) xfree(x)
#include <stdio.h>
#if defined(_WIN32)
#include "missing/file.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifndef S_ISDIR
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
bool
dln_supported_p(void)
{
return true;
}
#ifndef dln_loaderror
static void
dln_loaderror(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
abort();
}
#endif
#if defined(HAVE_DLOPEN) && !defined(_AIX) && !defined(_UNICOSMP)
/* dynamic load with dlopen() */
# define USE_DLN_DLOPEN
#endif
#if defined(__hp9000s300) || ((defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)) && !defined(__ELF__)) || defined(NeXT)
# define EXTERNAL_PREFIX "_"
#else
# define EXTERNAL_PREFIX ""
#endif
#define FUNCNAME_PREFIX EXTERNAL_PREFIX"Init_"
#if defined __CYGWIN__ || defined DOSISH
#define isdirsep(x) ((x) == '/' || (x) == '\\')
#else
#define isdirsep(x) ((x) == '/')
#endif
#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
struct string_part {
const char *ptr;
size_t len;
};
static struct string_part
init_funcname_len(const char *file)
{
const char *p = file, *base, *dot = NULL;
/* Load the file as an object one */
for (base = p; *p; p++) { /* Find position of last '/' */
if (*p == '.' && !dot) dot = p;
if (isdirsep(*p)) base = p+1, dot = NULL;
}
/* Delete suffix if it exists */
const size_t len = (dot ? dot : p) - base;
return (struct string_part){base, len};
}
static inline char *
concat_funcname(char *buf, const char *prefix, size_t plen, const struct string_part base)
{
if (!buf) {
dln_memerror();
}
memcpy(buf, prefix, plen);
memcpy(buf + plen, base.ptr, base.len);
buf[plen + base.len] = '\0';
return buf;
}
#define build_funcname(prefix, buf, file) do {\
const struct string_part f = init_funcname_len(file);\
const size_t plen = sizeof(prefix "") - 1;\
*(buf) = concat_funcname(ALLOCA_N(char, plen+f.len+1), prefix, plen, f);\
} while (0)
#define init_funcname(buf, file) build_funcname(FUNCNAME_PREFIX, buf, file)
#endif
#ifdef USE_DLN_DLOPEN
# include <dlfcn.h>
#endif
#if defined(_AIX)
#include <ctype.h> /* for isdigit() */
#include <errno.h> /* for global errno */
#include <sys/ldr.h>
#endif
#ifdef NeXT
#if NS_TARGET_MAJOR < 4
#include <mach-o/rld.h>
#else
#include <mach-o/dyld.h>
#ifndef NSLINKMODULE_OPTION_BINDNOW
#define NSLINKMODULE_OPTION_BINDNOW 1
#endif
#endif
#endif
#ifdef _WIN32
#include <windows.h>
#include <imagehlp.h>
#endif
#ifdef _WIN32
static const char *
dln_strerror(char *message, size_t size)
{
int error = GetLastError();
char *p = message;
size_t len = snprintf(message, size, "%d: ", error);
#define format_message(sublang) FormatMessage(\
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
NULL, error, MAKELANGID(LANG_NEUTRAL, (sublang)), \
message + len, size - len, NULL)
if (format_message(SUBLANG_ENGLISH_US) == 0)
format_message(SUBLANG_DEFAULT);
for (p = message + len; *p; p++) {
if (*p == '\n' || *p == '\r')
*p = ' ';
}
return message;
}
#elif defined USE_DLN_DLOPEN
static const char *
dln_strerror(void)
{
return (char*)dlerror();
}
#endif
#if defined(_AIX)
static void
aix_loaderror(const char *pathname)
{
char *message[1024], errbuf[1024];
int i;
#define ERRBUF_APPEND(s) strlcat(errbuf, (s), sizeof(errbuf))
snprintf(errbuf, sizeof(errbuf), "load failed - %s. ", pathname);
if (loadquery(L_GETMESSAGES, &message[0], sizeof(message)) != -1) {
ERRBUF_APPEND("Please issue below command for detailed reasons:\n\t");
ERRBUF_APPEND("/usr/sbin/execerror ruby ");
for (i=0; message[i]; i++) {
ERRBUF_APPEND("\"");
ERRBUF_APPEND(message[i]);
ERRBUF_APPEND("\" ");
}
ERRBUF_APPEND("\n");
}
else {
ERRBUF_APPEND(strerror(errno));
ERRBUF_APPEND("[loadquery failed]");
}
dln_loaderror("%s", errbuf);
}
#endif
#if defined _WIN32 && defined RUBY_EXPORT
HANDLE rb_libruby_handle(void);
static int
rb_w32_check_imported(HMODULE ext, HMODULE mine)
{
ULONG size;
const IMAGE_IMPORT_DESCRIPTOR *desc;
desc = ImageDirectoryEntryToData(ext, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size);
if (!desc) return 0;
while (desc->Name) {
PIMAGE_THUNK_DATA pint = (PIMAGE_THUNK_DATA)((char *)ext + desc->Characteristics);
PIMAGE_THUNK_DATA piat = (PIMAGE_THUNK_DATA)((char *)ext + desc->FirstThunk);
for (; piat->u1.Function; piat++, pint++) {
static const char prefix[] = "rb_";
PIMAGE_IMPORT_BY_NAME pii;
const char *name;
if (IMAGE_SNAP_BY_ORDINAL(pint->u1.Ordinal)) continue;
pii = (PIMAGE_IMPORT_BY_NAME)((char *)ext + (size_t)pint->u1.AddressOfData);
name = (const char *)pii->Name;
if (strncmp(name, prefix, sizeof(prefix) - 1) == 0) {
FARPROC addr = GetProcAddress(mine, name);
if (addr) return (FARPROC)piat->u1.Function == addr;
}
}
desc++;
}
return 1;
}
#endif
#if defined(DLN_NEEDS_ALT_SEPARATOR) && DLN_NEEDS_ALT_SEPARATOR
#define translit_separator(src) do { \
char *tmp = ALLOCA_N(char, strlen(src) + 1), *p = tmp, c; \
do { \
*p++ = ((c = *file++) == '/') ? DLN_NEEDS_ALT_SEPARATOR : c; \
} while (c); \
(src) = tmp; \
} while (0)
#else
#define translit_separator(str) (void)(str)
#endif
#ifdef USE_DLN_DLOPEN
# include "ruby/internal/stdbool.h"
# include "internal/warnings.h"
static bool
dln_incompatible_func(void *handle, const char *funcname, void *const fp, const char **libname)
{
void *ex = dlsym(handle, funcname);
if (!ex) return false;
if (ex == fp) return false;
# if defined(HAVE_DLADDR)
Dl_info dli;
if (dladdr(ex, &dli)) {
*libname = dli.dli_fname;
}
# endif
return true;
}
COMPILER_WARNING_PUSH
#if defined(__clang__) || GCC_VERSION_SINCE(4, 2, 0)
COMPILER_WARNING_IGNORED(-Wpedantic)
#endif
static bool
dln_incompatible_library_p(void *handle, const char **libname)
{
#define check_func(func) \
if (dln_incompatible_func(handle, EXTERNAL_PREFIX #func, (void *)&func, libname)) \
return true
check_func(ruby_xmalloc);
return false;
}
COMPILER_WARNING_POP
#endif
#if !defined(MAC_OS_X_VERSION_MIN_REQUIRED)
/* assume others than old Mac OS X have no problem */
# define dln_disable_dlclose() false
#elif !defined(MAC_OS_X_VERSION_10_11) || \
(MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11)
/* targeting older versions only */
# define dln_disable_dlclose() true
#elif MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11
/* targeting newer versions only */
# define dln_disable_dlclose() false
#else
/* support both versions, and check at runtime */
# include <sys/sysctl.h>
static bool
dln_disable_dlclose(void)
{
int mib[] = {CTL_KERN, KERN_OSREV};
int32_t rev;
size_t size = sizeof(rev);
if (sysctl(mib, numberof(mib), &rev, &size, NULL, 0)) return true;
if (rev < MAC_OS_X_VERSION_10_11) return true;
return false;
}
#endif
#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
void *
dln_open(const char *file, char *error, size_t size)
{
static const char incompatible[] = "incompatible library version";
void *handle;
#if defined(_WIN32)
/* Convert the file path to wide char */
WCHAR *winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);
if (!winfile) {
dln_memerror();
}
/* Load file */
handle = LoadLibraryW(winfile);
free(winfile);
if (!handle) {
strlcpy(error, dln_strerror(error, size), size);
return NULL;
}
# if defined(RUBY_EXPORT)
if (!rb_w32_check_imported(handle, rb_libruby_handle())) {
FreeLibrary(handle);
strlcpy(error, incompatible, size);
return NULL;
}
# endif
#elif defined(USE_DLN_DLOPEN)
# ifndef RTLD_LAZY
# define RTLD_LAZY 1
# endif
# ifdef __INTERIX
# undef RTLD_GLOBAL
# endif
# ifndef RTLD_GLOBAL
# define RTLD_GLOBAL 0
# endif
/* Load file */
handle = dlopen(file, RTLD_LAZY|RTLD_GLOBAL);
if (handle == NULL) {
strlcpy(error, dln_strerror(), size);
return NULL;
}
# if defined(RUBY_EXPORT)
{
const char *libruby_name = NULL;
if (dln_incompatible_library_p(handle, &libruby_name)) {
if (dln_disable_dlclose()) {
/* dlclose() segfaults */
if (libruby_name) {
dln_fatalerror("linked to incompatible %s - %s", libruby_name, file);
}
dln_fatalerror("%s - %s", incompatible, file);
}
else {
if (libruby_name) {
const size_t len = strlen(libruby_name);
char *const tmp = ALLOCA_N(char, len + 1);
if (tmp) memcpy(tmp, libruby_name, len + 1);
libruby_name = tmp;
}
dlclose(handle);
if (libruby_name) {
snprintf(error, size, "linked to incompatible %s - %s", libruby_name, file);
}
else {
strlcpy(error, incompatible, size);
}
return NULL;
}
}
}
# endif
#endif
return handle;
}
void *
dln_sym(void *handle, const char *symbol)
{
#if defined(_WIN32)
return GetProcAddress(handle, symbol);
#elif defined(USE_DLN_DLOPEN)
return dlsym(handle, symbol);
#endif
}
static void *
dln_sym_func(void *handle, const char *symbol)
{
void *func = dln_sym(handle, symbol);
if (func == NULL) {
const char *error;
#if defined(_WIN32)
char message[1024];
error = dln_strerror(message, sizeof(message));
#elif defined(USE_DLN_DLOPEN)
const size_t errlen = strlen(error = dln_strerror()) + 1;
error = memcpy(ALLOCA_N(char, errlen), error, errlen);
#endif
dln_loaderror("%s - %s", error, symbol);
}
return func;
}
#define dln_sym_callable(rettype, argtype, handle, symbol) \
(*(rettype (*)argtype)dln_sym_func(handle, symbol))
#endif
void *
dln_symbol(void *handle, const char *symbol)
{
#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
if (EXTERNAL_PREFIX[0]) {
const size_t symlen = strlen(symbol);
char *const tmp = ALLOCA_N(char, symlen + sizeof(EXTERNAL_PREFIX));
if (!tmp) dln_memerror();
memcpy(tmp, EXTERNAL_PREFIX, sizeof(EXTERNAL_PREFIX) - 1);
memcpy(tmp + sizeof(EXTERNAL_PREFIX) - 1, symbol, symlen + 1);
symbol = tmp;
}
if (handle == NULL) {
# if defined(USE_DLN_DLOPEN)
handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
# elif defined(_WIN32)
handle = rb_libruby_handle();
# else
return NULL;
# endif
}
return dln_sym(handle, symbol);
#else
return NULL;
#endif
}
#if defined(RUBY_DLN_CHECK_ABI) && defined(USE_DLN_DLOPEN)
static bool
abi_check_enabled_p(void)
{
const char *val = getenv("RUBY_ABI_CHECK");
return val == NULL || !(val[0] == '0' && val[1] == '\0');
}
#endif
void *
dln_load(const char *file)
{
#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
char error[1024];
void *handle = dln_open(file, error, sizeof(error));
if (handle == NULL) {
dln_loaderror("%s - %s", error, file);
}
#ifdef RUBY_DLN_CHECK_ABI
typedef unsigned long long abi_version_number;
abi_version_number binary_abi_version =
dln_sym_callable(abi_version_number, (void), handle, EXTERNAL_PREFIX "ruby_abi_version")();
if (binary_abi_version != RUBY_ABI_VERSION && abi_check_enabled_p()) {
dln_loaderror("incompatible ABI version of binary - %s", file);
}
#endif
char *init_fct_name;
init_funcname(&init_fct_name, file);
/* Call the init code */
dln_sym_callable(void, (void), handle, init_fct_name)();
return handle;
#elif defined(_AIX)
{
void (*init_fct)(void);
init_fct = (void(*)(void))load((char*)file, 1, 0);
if (init_fct == NULL) {
aix_loaderror(file);
}
if (loadbind(0, (void*)dln_load, (void*)init_fct) == -1) {
aix_loaderror(file);
}
(*init_fct)();
return (void*)init_fct;
}
#else
dln_notimplement();
#endif
return 0; /* dummy return */
}