Ruby 3.5.0dev (2025-06-07 revision 42cf301254d4bfec30c07a221f34f3fa85a29f33)
dln.c (42cf301254d4bfec30c07a221f34f3fa85a29f33)
1/**********************************************************************
2
3 dln.c -
4
5 $Author$
6 created at: Tue Jan 18 17:05:06 JST 1994
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#ifdef RUBY_EXPORT
13#include "ruby/ruby.h"
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
19#else
20#define dln_notimplement --->>> dln not implemented <<<---
21#define dln_memerror abort
22#define dln_exit exit
23static void dln_loaderror(const char *format, ...);
24#define dln_fatalerror dln_loaderror
25#endif
26#include "dln.h"
27#include "internal.h"
28#include "internal/compilers.h"
29#include "internal/namespace.h"
30
31#ifdef HAVE_STDLIB_H
32# include <stdlib.h>
33#endif
34
35#if defined(HAVE_ALLOCA_H)
36#include <alloca.h>
37#endif
38
39#ifdef HAVE_STRING_H
40# include <string.h>
41#else
42# include <strings.h>
43#endif
44
45#if defined __APPLE__
46# include <AvailabilityMacros.h>
47#endif
48
49#ifndef xmalloc
50void *xmalloc();
51void *xcalloc();
52void *xrealloc();
53#endif
54
55#undef free
56#define free(x) xfree(x)
57
58#include <stdio.h>
59#if defined(_WIN32)
60#include "missing/file.h"
61#endif
62#include <sys/types.h>
63#include <sys/stat.h>
64
65#ifndef S_ISDIR
66# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
67#endif
68
69#ifdef HAVE_SYS_PARAM_H
70# include <sys/param.h>
71#endif
72#ifndef MAXPATHLEN
73# define MAXPATHLEN 1024
74#endif
75
76#ifdef HAVE_UNISTD_H
77# include <unistd.h>
78#endif
79
80#ifndef dln_loaderror
81static void
82dln_loaderror(const char *format, ...)
83{
84 va_list ap;
85 va_start(ap, format);
86 vfprintf(stderr, format, ap);
87 va_end(ap);
88 abort();
89}
90#endif
91
92#if defined(HAVE_DLOPEN) && !defined(_AIX) && !defined(_UNICOSMP)
93/* dynamic load with dlopen() */
94# define USE_DLN_DLOPEN
95#endif
96
97#if defined(__hp9000s300) || ((defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)) && !defined(__ELF__)) || defined(NeXT)
98# define EXTERNAL_PREFIX "_"
99#else
100# define EXTERNAL_PREFIX ""
101#endif
102#define FUNCNAME_PREFIX EXTERNAL_PREFIX"Init_"
103
104#if defined __CYGWIN__ || defined DOSISH
105#define isdirsep(x) ((x) == '/' || (x) == '\\')
106#else
107#define isdirsep(x) ((x) == '/')
108#endif
109
110#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
111struct string_part {
112 const char *ptr;
113 size_t len;
114};
115
116static struct string_part
117init_funcname_len(const char *file)
118{
119 const char *p = file, *base, *dot = NULL;
120
121 /* Load the file as an object one */
122 for (base = p; *p; p++) { /* Find position of last '/' */
123 if (*p == '.' && !dot) dot = p;
124 if (isdirsep(*p)) base = p+1, dot = NULL;
125 }
126 /* Delete suffix if it exists */
127 const size_t len = (dot ? dot : p) - base;
128 return (struct string_part){base, len};
129}
130
131static inline char *
132concat_funcname(char *buf, const char *prefix, size_t plen, const struct string_part base)
133{
134 if (!buf) {
135 dln_memerror();
136 }
137 memcpy(buf, prefix, plen);
138 memcpy(buf + plen, base.ptr, base.len);
139 buf[plen + base.len] = '\0';
140 return buf;
141}
142
143#define build_funcname(prefix, buf, file) do {\
144 const struct string_part f = init_funcname_len(file);\
145 const size_t plen = sizeof(prefix "") - 1;\
146 *(buf) = concat_funcname(ALLOCA_N(char, plen+f.len+1), prefix, plen, f);\
147} while (0)
148
149#define init_funcname(buf, file) build_funcname(FUNCNAME_PREFIX, buf, file)
150#endif
151
152#ifdef USE_DLN_DLOPEN
153# include <dlfcn.h>
154#endif
155
156#if defined(_AIX)
157#include <ctype.h> /* for isdigit() */
158#include <errno.h> /* for global errno */
159#include <sys/ldr.h>
160#endif
161
162#ifdef NeXT
163#if NS_TARGET_MAJOR < 4
164#include <mach-o/rld.h>
165#else
166#include <mach-o/dyld.h>
167#ifndef NSLINKMODULE_OPTION_BINDNOW
168#define NSLINKMODULE_OPTION_BINDNOW 1
169#endif
170#endif
171#endif
172
173#ifdef _WIN32
174#include <windows.h>
175#include <imagehlp.h>
176#endif
177
178#ifdef _WIN32
179static const char *
180dln_strerror(char *message, size_t size)
181{
182 int error = GetLastError();
183 char *p = message;
184 size_t len = snprintf(message, size, "%d: ", error);
185
186#define format_message(sublang) FormatMessage(\
187 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
188 NULL, error, MAKELANGID(LANG_NEUTRAL, (sublang)), \
189 message + len, size - len, NULL)
190 if (format_message(SUBLANG_ENGLISH_US) == 0)
191 format_message(SUBLANG_DEFAULT);
192 for (p = message + len; *p; p++) {
193 if (*p == '\n' || *p == '\r')
194 *p = ' ';
195 }
196 return message;
197}
198#define dln_strerror() dln_strerror(message, sizeof message)
199#elif defined USE_DLN_DLOPEN
200static const char *
201dln_strerror(void)
202{
203 return (char*)dlerror();
204}
205#endif
206
207#if defined(_AIX)
208static void
209aix_loaderror(const char *pathname)
210{
211 char *message[1024], errbuf[1024];
212 int i;
213#define ERRBUF_APPEND(s) strlcat(errbuf, (s), sizeof(errbuf))
214 snprintf(errbuf, sizeof(errbuf), "load failed - %s. ", pathname);
215
216 if (loadquery(L_GETMESSAGES, &message[0], sizeof(message)) != -1) {
217 ERRBUF_APPEND("Please issue below command for detailed reasons:\n\t");
218 ERRBUF_APPEND("/usr/sbin/execerror ruby ");
219 for (i=0; message[i]; i++) {
220 ERRBUF_APPEND("\"");
221 ERRBUF_APPEND(message[i]);
222 ERRBUF_APPEND("\" ");
223 }
224 ERRBUF_APPEND("\n");
225 }
226 else {
227 ERRBUF_APPEND(strerror(errno));
228 ERRBUF_APPEND("[loadquery failed]");
229 }
230 dln_loaderror("%s", errbuf);
231}
232#endif
233
234#if defined _WIN32 && defined RUBY_EXPORT
235HANDLE rb_libruby_handle(void);
236
237static int
238rb_w32_check_imported(HMODULE ext, HMODULE mine)
239{
240 ULONG size;
241 const IMAGE_IMPORT_DESCRIPTOR *desc;
242
243 desc = ImageDirectoryEntryToData(ext, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size);
244 if (!desc) return 0;
245 while (desc->Name) {
246 PIMAGE_THUNK_DATA pint = (PIMAGE_THUNK_DATA)((char *)ext + desc->Characteristics);
247 PIMAGE_THUNK_DATA piat = (PIMAGE_THUNK_DATA)((char *)ext + desc->FirstThunk);
248 for (; piat->u1.Function; piat++, pint++) {
249 static const char prefix[] = "rb_";
250 PIMAGE_IMPORT_BY_NAME pii;
251 const char *name;
252
253 if (IMAGE_SNAP_BY_ORDINAL(pint->u1.Ordinal)) continue;
254 pii = (PIMAGE_IMPORT_BY_NAME)((char *)ext + (size_t)pint->u1.AddressOfData);
255 name = (const char *)pii->Name;
256 if (strncmp(name, prefix, sizeof(prefix) - 1) == 0) {
257 FARPROC addr = GetProcAddress(mine, name);
258 if (addr) return (FARPROC)piat->u1.Function == addr;
259 }
260 }
261 desc++;
262 }
263 return 1;
264}
265#endif
266
267#if defined(DLN_NEEDS_ALT_SEPARATOR) && DLN_NEEDS_ALT_SEPARATOR
268#define translit_separator(src) do { \
269 char *tmp = ALLOCA_N(char, strlen(src) + 1), *p = tmp, c; \
270 do { \
271 *p++ = ((c = *file++) == '/') ? DLN_NEEDS_ALT_SEPARATOR : c; \
272 } while (c); \
273 (src) = tmp; \
274 } while (0)
275#else
276#define translit_separator(str) (void)(str)
277#endif
278
279#ifdef USE_DLN_DLOPEN
280# include "ruby/internal/stdbool.h"
281# include "internal/warnings.h"
282static bool
283dln_incompatible_func(void *handle, const char *funcname, void *const fp, const char **libname)
284{
285 void *ex = dlsym(handle, funcname);
286 if (!ex) return false;
287 if (ex == fp) return false;
288# if defined(HAVE_DLADDR) && !defined(__CYGWIN__)
289 Dl_info dli;
290 if (dladdr(ex, &dli)) {
291 *libname = dli.dli_fname;
292 }
293# endif
294 return true;
295}
296
297COMPILER_WARNING_PUSH
298#if defined(__clang__) || GCC_VERSION_SINCE(4, 2, 0)
299COMPILER_WARNING_IGNORED(-Wpedantic)
300#endif
301static bool
302dln_incompatible_library_p(void *handle, const char **libname)
303{
304#define check_func(func) \
305 if (dln_incompatible_func(handle, EXTERNAL_PREFIX #func, (void *)&func, libname)) \
306 return true
307 check_func(ruby_xmalloc);
308 return false;
309}
310COMPILER_WARNING_POP
311#endif
312
313#if !defined(MAC_OS_X_VERSION_MIN_REQUIRED)
314/* assume others than old Mac OS X have no problem */
315# define dln_disable_dlclose() false
316
317#elif !defined(MAC_OS_X_VERSION_10_11) || \
318 (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11)
319/* targeting older versions only */
320# define dln_disable_dlclose() true
321
322#elif MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11
323/* targeting newer versions only */
324# define dln_disable_dlclose() false
325
326#else
327/* support both versions, and check at runtime */
328# include <sys/sysctl.h>
329
330static bool
331dln_disable_dlclose(void)
332{
333 int mib[] = {CTL_KERN, KERN_OSREV};
334 int32_t rev;
335 size_t size = sizeof(rev);
336 if (sysctl(mib, numberof(mib), &rev, &size, NULL, 0)) return true;
337 if (rev < MAC_OS_X_VERSION_10_11) return true;
338 return false;
339}
340#endif
341
342#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
343static void *
344dln_open(const char *file)
345{
346 static const char incompatible[] = "incompatible library version";
347 const char *error = NULL;
348 void *handle;
349
350#if defined(_WIN32)
351 char message[1024];
352
353 /* Convert the file path to wide char */
354 WCHAR *winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);
355 if (!winfile) {
356 dln_memerror();
357 }
358
359 /* Load file */
360 handle = LoadLibraryW(winfile);
361 free(winfile);
362
363 if (!handle) {
364 error = dln_strerror();
365 goto failed;
366 }
367
368# if defined(RUBY_EXPORT)
369 if (!rb_w32_check_imported(handle, rb_libruby_handle())) {
370 FreeLibrary(handle);
371 error = incompatible;
372 goto failed;
373 }
374# endif
375
376#elif defined(USE_DLN_DLOPEN)
377
378# ifndef RTLD_LAZY
379# define RTLD_LAZY 1
380# endif
381# ifdef __INTERIX
382# undef RTLD_GLOBAL
383# endif
384# ifndef RTLD_GLOBAL
385# define RTLD_GLOBAL 0
386# endif
387# ifndef RTLD_LOCAL
388# define RTLD_LOCAL 0 /* TODO: 0??? some systems (including libc) use 0x00100 for RTLD_GLOBAL, 0x00000 for RTLD_LOCAL */
389# endif
390
391 /* Load file */
392 int mode = rb_namespace_available() ? RTLD_LAZY|RTLD_LOCAL : RTLD_LAZY|RTLD_GLOBAL;
393 handle = dlopen(file, mode);
394 if (handle == NULL) {
395 error = dln_strerror();
396 goto failed;
397 }
398
399# if defined(RUBY_EXPORT)
400 {
401 const char *libruby_name = NULL;
402 if (dln_incompatible_library_p(handle, &libruby_name)) {
403 if (dln_disable_dlclose()) {
404 /* dlclose() segfaults */
405 if (libruby_name) {
406 dln_fatalerror("linked to incompatible %s - %s", libruby_name, file);
407 }
408 dln_fatalerror("%s - %s", incompatible, file);
409 }
410 else {
411 if (libruby_name) {
412 const size_t len = strlen(libruby_name);
413 char *const tmp = ALLOCA_N(char, len + 1);
414 if (tmp) memcpy(tmp, libruby_name, len + 1);
415 libruby_name = tmp;
416 }
417 dlclose(handle);
418 if (libruby_name) {
419 dln_loaderror("linked to incompatible %s - %s", libruby_name, file);
420 }
421 error = incompatible;
422 goto failed;
423 }
424 }
425 }
426# endif
427#endif
428
429 return handle;
430
431 failed:
432 dln_loaderror("%s - %s", error, file);
433}
434
435static void *
436dln_sym(void *handle, const char *symbol)
437{
438#if defined(_WIN32)
439 return GetProcAddress(handle, symbol);
440#elif defined(USE_DLN_DLOPEN)
441 return dlsym(handle, symbol);
442#endif
443}
444
445static uintptr_t
446dln_sym_func(void *handle, const char *symbol)
447{
448 void *func = dln_sym(handle, symbol);
449
450 if (func == NULL) {
451 const char *error;
452#if defined(_WIN32)
453 char message[1024];
454 error = dln_strerror();
455#elif defined(USE_DLN_DLOPEN)
456 const size_t errlen = strlen(error = dln_strerror()) + 1;
457 error = memcpy(ALLOCA_N(char, errlen), error, errlen);
458#endif
459 dln_loaderror("%s - %s", error, symbol);
460 }
461 return (uintptr_t)func;
462}
463
464#define dln_sym_callable(rettype, argtype, handle, symbol) \
465 (*(rettype (*)argtype)dln_sym_func(handle, symbol))
466#endif
467
468void *
469dln_symbol(void *handle, const char *symbol)
470{
471#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
472 if (EXTERNAL_PREFIX[0]) {
473 const size_t symlen = strlen(symbol);
474 char *const tmp = ALLOCA_N(char, symlen + sizeof(EXTERNAL_PREFIX));
475 if (!tmp) dln_memerror();
476 memcpy(tmp, EXTERNAL_PREFIX, sizeof(EXTERNAL_PREFIX) - 1);
477 memcpy(tmp + sizeof(EXTERNAL_PREFIX) - 1, symbol, symlen + 1);
478 symbol = tmp;
479 }
480 if (handle == NULL) {
481# if defined(USE_DLN_DLOPEN)
482 handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
483# elif defined(_WIN32)
484 handle = rb_libruby_handle();
485# else
486 return NULL;
487# endif
488 }
489 return dln_sym(handle, symbol);
490#else
491 return NULL;
492#endif
493}
494
495
496#if defined(RUBY_DLN_CHECK_ABI) && defined(USE_DLN_DLOPEN)
497static bool
498abi_check_enabled_p(void)
499{
500 const char *val = getenv("RUBY_ABI_CHECK");
501 return val == NULL || !(val[0] == '0' && val[1] == '\0');
502}
503#endif
504
505static void *
506dln_load_and_init(const char *file, const char *init_fct_name)
507{
508#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
509 void *handle = dln_open(file);
510
511#ifdef RUBY_DLN_CHECK_ABI
512 typedef unsigned long long abi_version_number;
513 abi_version_number binary_abi_version =
514 dln_sym_callable(abi_version_number, (void), handle, EXTERNAL_PREFIX "ruby_abi_version")();
515 if (binary_abi_version != RUBY_ABI_VERSION && abi_check_enabled_p()) {
516 dln_loaderror("incompatible ABI version of binary - %s", file);
517 }
518#endif
519
520 /* Call the init code */
521 dln_sym_callable(void, (void), handle, init_fct_name)();
522
523 return handle;
524
525#elif defined(_AIX)
526 {
527 void (*init_fct)(void);
528
529 /* TODO: check - AIX's load system call will return the first/last symbol/function? */
530 init_fct = (void(*)(void))load((char*)file, 1, 0);
531 if (init_fct == NULL) {
532 aix_loaderror(file);
533 }
534 if (loadbind(0, (void*)dln_load, (void*)init_fct) == -1) {
535 aix_loaderror(file);
536 }
537 (*init_fct)();
538 return (void*)init_fct;
539 }
540#else
541 dln_notimplement();
542#endif
543
544 return 0; /* dummy return */
545}
546
547void *
548dln_load(const char *file)
549{
550#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
551 char *init_fct_name;
552 init_funcname(&init_fct_name, file);
553 return dln_load_and_init(file, init_fct_name);
554#else
555 dln_notimplement();
556 return 0;
557#endif
558}
559
560void *
561dln_load_feature(const char *file, const char *fname)
562{
563#if defined(_WIN32) || defined(USE_DLN_DLOPEN)
564 char *init_fct_name;
565 init_funcname(&init_fct_name, fname);
566 return dln_load_and_init(file, init_fct_name);
567#else
568 dln_notimplement();
569 return 0;
570#endif
571}
#define xrealloc
Old name of ruby_xrealloc.
Definition xmalloc.h:56
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
int len
Length of the buffer.
Definition io.h:8
#define ALLOCA_N(type, n)
Definition memory.h:292
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
C99 shim for <stdbool.h>