14#define dln_notimplement rb_notimplement
15#define dln_memerror rb_memerror
16#define dln_exit rb_exit
17#define dln_loaderror rb_loaderror
18#define dln_fatalerror rb_fatal
20#define dln_notimplement --->>> dln not implemented <<<---
21#define dln_memerror abort
23static void dln_loaderror(
const char *format, ...);
24#define dln_fatalerror dln_loaderror
28#include "internal/compilers.h"
34#if defined(HAVE_ALLOCA_H)
45# include <AvailabilityMacros.h>
55#define free(x) xfree(x)
59#include "missing/file.h"
65# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
68#ifdef HAVE_SYS_PARAM_H
69# include <sys/param.h>
72# define MAXPATHLEN 1024
81dln_loaderror(
const char *format, ...)
85 vfprintf(stderr, format, ap);
91#if defined(HAVE_DLOPEN) && !defined(_AIX) && !defined(_UNICOSMP)
93# define USE_DLN_DLOPEN
96#if defined(__hp9000s300) || ((defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)) && !defined(__ELF__)) || defined(NeXT)
97# define EXTERNAL_PREFIX "_"
99# define EXTERNAL_PREFIX ""
101#define FUNCNAME_PREFIX EXTERNAL_PREFIX"Init_"
103#if defined __CYGWIN__ || defined DOSISH
104#define isdirsep(x) ((x) == '/' || (x) == '\\')
106#define isdirsep(x) ((x) == '/')
109#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
116init_funcname_len(const char *file)
118 const char *p = file, *base, *dot = NULL;
121 for (base = p; *p; p++) {
122 if (*p ==
'.' && !dot) dot = p;
123 if (isdirsep(*p)) base = p+1, dot = NULL;
126 const size_t len = (dot ? dot : p) - base;
131concat_funcname(
char *buf,
const char *prefix,
size_t plen,
const struct string_part base)
136 memcpy(buf, prefix, plen);
137 memcpy(buf + plen, base.ptr, base.len);
138 buf[plen + base.len] =
'\0';
142#define build_funcname(prefix, buf, file) do {\
143 const struct string_part f = init_funcname_len(file);\
144 const size_t plen = sizeof(prefix "") - 1;\
145 *(buf) = concat_funcname(ALLOCA_N(char, plen+f.len+1), prefix, plen, f);\
148#define init_funcname(buf, file) build_funcname(FUNCNAME_PREFIX, buf, file)
162#if NS_TARGET_MAJOR < 4
163#include <mach-o/rld.h>
165#include <mach-o/dyld.h>
166#ifndef NSLINKMODULE_OPTION_BINDNOW
167#define NSLINKMODULE_OPTION_BINDNOW 1
179dln_strerror(
char *message,
size_t size)
181 int error = GetLastError();
183 size_t len = snprintf(message, size,
"%d: ", error);
185#define format_message(sublang) FormatMessage(\
186 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
187 NULL, error, MAKELANGID(LANG_NEUTRAL, (sublang)), \
188 message + len, size - len, NULL)
189 if (format_message(SUBLANG_ENGLISH_US) == 0)
190 format_message(SUBLANG_DEFAULT);
191 for (p = message +
len; *p; p++) {
192 if (*p ==
'\n' || *p ==
'\r')
197#define dln_strerror() dln_strerror(message, sizeof message)
198#elif defined USE_DLN_DLOPEN
202 return (
char*)dlerror();
208aix_loaderror(
const char *pathname)
210 char *message[1024], errbuf[1024];
212#define ERRBUF_APPEND(s) strlcat(errbuf, (s), sizeof(errbuf))
213 snprintf(errbuf,
sizeof(errbuf),
"load failed - %s. ", pathname);
215 if (loadquery(L_GETMESSAGES, &message[0],
sizeof(message)) != -1) {
216 ERRBUF_APPEND(
"Please issue below command for detailed reasons:\n\t");
217 ERRBUF_APPEND(
"/usr/sbin/execerror ruby ");
218 for (i=0; message[i]; i++) {
220 ERRBUF_APPEND(message[i]);
221 ERRBUF_APPEND(
"\" ");
226 ERRBUF_APPEND(strerror(
errno));
227 ERRBUF_APPEND(
"[loadquery failed]");
229 dln_loaderror(
"%s", errbuf);
233#if defined _WIN32 && defined RUBY_EXPORT
234HANDLE rb_libruby_handle(
void);
237rb_w32_check_imported(HMODULE ext, HMODULE mine)
240 const IMAGE_IMPORT_DESCRIPTOR *desc;
242 desc = ImageDirectoryEntryToData(ext, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size);
245 PIMAGE_THUNK_DATA pint = (PIMAGE_THUNK_DATA)((
char *)ext + desc->Characteristics);
246 PIMAGE_THUNK_DATA piat = (PIMAGE_THUNK_DATA)((
char *)ext + desc->FirstThunk);
247 for (; piat->u1.Function; piat++, pint++) {
248 static const char prefix[] =
"rb_";
249 PIMAGE_IMPORT_BY_NAME pii;
252 if (IMAGE_SNAP_BY_ORDINAL(pint->u1.Ordinal))
continue;
253 pii = (PIMAGE_IMPORT_BY_NAME)((
char *)ext + (size_t)pint->u1.AddressOfData);
254 name = (
const char *)pii->Name;
255 if (strncmp(name, prefix,
sizeof(prefix) - 1) == 0) {
256 FARPROC addr = GetProcAddress(mine, name);
257 if (addr)
return (FARPROC)piat->u1.Function == addr;
266#if defined(DLN_NEEDS_ALT_SEPARATOR) && DLN_NEEDS_ALT_SEPARATOR
267#define translit_separator(src) do { \
268 char *tmp = ALLOCA_N(char, strlen(src) + 1), *p = tmp, c; \
270 *p++ = ((c = *file++) == '/') ? DLN_NEEDS_ALT_SEPARATOR : c; \
275#define translit_separator(str) (void)(str)
280# include "internal/warnings.h"
282dln_incompatible_func(
void *handle,
const char *funcname,
void *
const fp,
const char **libname)
284 void *ex = dlsym(handle, funcname);
285 if (!ex)
return false;
286 if (ex == fp)
return false;
287# if defined(HAVE_DLADDR) && !defined(__CYGWIN__)
289 if (dladdr(ex, &dli)) {
290 *libname = dli.dli_fname;
297#if defined(__clang__) || GCC_VERSION_SINCE(4, 2, 0)
298COMPILER_WARNING_IGNORED(-Wpedantic)
301dln_incompatible_library_p(
void *handle,
const char **libname)
303#define check_func(func) \
304 if (dln_incompatible_func(handle, EXTERNAL_PREFIX #func, (void *)&func, libname)) \
306 check_func(ruby_xmalloc);
312#if !defined(MAC_OS_X_VERSION_MIN_REQUIRED)
314# define dln_disable_dlclose() false
316#elif !defined(MAC_OS_X_VERSION_10_11) || \
317 (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11)
319# define dln_disable_dlclose() true
321#elif MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11
323# define dln_disable_dlclose() false
327# include <sys/sysctl.h>
330dln_disable_dlclose(
void)
332 int mib[] = {CTL_KERN, KERN_OSREV};
334 size_t size =
sizeof(rev);
335 if (sysctl(mib, numberof(mib), &rev, &size, NULL, 0))
return true;
336 if (rev < MAC_OS_X_VERSION_10_11)
return true;
341#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
343dln_open(
const char *file)
345 static const char incompatible[] =
"incompatible library version";
346 const char *error = NULL;
353 WCHAR *winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);
359 handle = LoadLibraryW(winfile);
363 error = dln_strerror();
367# if defined(RUBY_EXPORT)
368 if (!rb_w32_check_imported(handle, rb_libruby_handle())) {
370 error = incompatible;
375#elif defined(USE_DLN_DLOPEN)
384# define RTLD_GLOBAL 0
388 handle = dlopen(file, RTLD_LAZY|RTLD_GLOBAL);
389 if (handle == NULL) {
390 error = dln_strerror();
394# if defined(RUBY_EXPORT)
396 const char *libruby_name = NULL;
397 if (dln_incompatible_library_p(handle, &libruby_name)) {
398 if (dln_disable_dlclose()) {
401 dln_fatalerror(
"linked to incompatible %s - %s", libruby_name, file);
403 dln_fatalerror(
"%s - %s", incompatible, file);
407 const size_t len = strlen(libruby_name);
409 if (tmp) memcpy(tmp, libruby_name,
len + 1);
414 dln_loaderror(
"linked to incompatible %s - %s", libruby_name, file);
416 error = incompatible;
427 dln_loaderror(
"%s - %s", error, file);
431dln_sym(
void *handle,
const char *symbol)
434 return GetProcAddress(handle, symbol);
435#elif defined(USE_DLN_DLOPEN)
436 return dlsym(handle, symbol);
441dln_sym_func(
void *handle,
const char *symbol)
443 void *func = dln_sym(handle, symbol);
449 error = dln_strerror();
450#elif defined(USE_DLN_DLOPEN)
451 const size_t errlen = strlen(error = dln_strerror()) + 1;
452 error = memcpy(
ALLOCA_N(
char, errlen), error, errlen);
454 dln_loaderror(
"%s - %s", error, symbol);
456 return (uintptr_t)func;
459#define dln_sym_callable(rettype, argtype, handle, symbol) \
460 (*(rettype (*)argtype)dln_sym_func(handle, symbol))
464dln_symbol(
void *handle,
const char *symbol)
466#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
467 if (EXTERNAL_PREFIX[0]) {
468 const size_t symlen = strlen(symbol);
469 char *
const tmp =
ALLOCA_N(
char, symlen +
sizeof(EXTERNAL_PREFIX));
470 if (!tmp) dln_memerror();
471 memcpy(tmp, EXTERNAL_PREFIX,
sizeof(EXTERNAL_PREFIX) - 1);
472 memcpy(tmp +
sizeof(EXTERNAL_PREFIX) - 1, symbol, symlen + 1);
475 if (handle == NULL) {
476# if defined(USE_DLN_DLOPEN)
477 handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
478# elif defined(_WIN32)
479 handle = rb_libruby_handle();
484 return dln_sym(handle, symbol);
491#if defined(RUBY_DLN_CHECK_ABI) && defined(USE_DLN_DLOPEN)
493abi_check_enabled_p(
void)
495 const char *val = getenv(
"RUBY_ABI_CHECK");
496 return val == NULL || !(val[0] ==
'0' && val[1] ==
'\0');
501dln_load(
const char *file)
503#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
504 void *handle = dln_open(file);
506#ifdef RUBY_DLN_CHECK_ABI
507 typedef unsigned long long abi_version_number;
508 abi_version_number binary_abi_version =
509 dln_sym_callable(abi_version_number, (
void), handle, EXTERNAL_PREFIX
"ruby_abi_version")();
510 if (binary_abi_version != RUBY_ABI_VERSION && abi_check_enabled_p()) {
511 dln_loaderror(
"incompatible ABI version of binary - %s", file);
516 init_funcname(&init_fct_name, file);
519 dln_sym_callable(
void, (
void), handle, init_fct_name)();
525 void (*init_fct)(void);
527 init_fct = (void(*)(void))load((
char*)file, 1, 0);
528 if (init_fct == NULL) {
531 if (loadbind(0, (
void*)dln_load, (
void*)init_fct) == -1) {
535 return (
void*)init_fct;
#define xrealloc
Old name of ruby_xrealloc.
#define xmalloc
Old name of ruby_xmalloc.
#define xcalloc
Old name of ruby_xcalloc.
int len
Length of the buffer.
#define ALLOCA_N(type, n)
#define errno
Ractor-aware version of errno.