This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move DynaLoader.o into libperl.so.
[perl5.git] / ext / DynaLoader / dlutils.c
CommitLineData
a0d0e21e
LW
1/* dlutils.c - handy functions and definitions for dl_*.xs files
2 *
3 * Currently this file is simply #included into dl_*.xs/.c files.
4 * It should really be split into a dlutils.h and dlutils.c
5 *
abb9e9dc
GS
6 * Modified:
7 * 29th Feburary 2000 - Alan Burlison: Added functionality to close dlopen'd
8 * files when the interpreter exits
a0d0e21e
LW
9 */
10
27da23d5
JH
11#ifndef START_MY_CXT /* Some IDEs try compiling this standalone. */
12# include "EXTERN.h"
13# include "perl.h"
14# include "XSUB.h"
15#endif
16
89ca4ac7
JH
17#ifndef XS_VERSION
18# define XS_VERSION "0"
19#endif
39c19e8a 20#define MY_CXT_KEY "DynaLoader::_guts" XS_VERSION
a0d0e21e 21
cdc73a10 22typedef struct {
591022fb 23 SV* x_dl_last_error; /* pointer to allocated memory for
cdc73a10
JH
24 last error message */
25 int x_dl_nonlazy; /* flag for immediate rather than lazy
26 linking (spots unresolved symbol) */
27#ifdef DL_LOADONCEONLY
28 HV * x_dl_loaded_files; /* only needed on a few systems */
29#endif
30#ifdef DL_CXT_EXTRA
31 my_cxtx_t x_dl_cxtx; /* extra platform-specific data */
32#endif
33#ifdef DEBUGGING
34 int x_dl_debug; /* value copied from $DynaLoader::dl_debug */
35#endif
36} my_cxt_t;
37
89ca4ac7 38START_MY_CXT
cdc73a10 39
591022fb 40#define dl_last_error (SvPVX(MY_CXT.x_dl_last_error))
89ca4ac7 41#define dl_nonlazy (MY_CXT.x_dl_nonlazy)
cdc73a10 42#ifdef DL_LOADONCEONLY
89ca4ac7 43#define dl_loaded_files (MY_CXT.x_dl_loaded_files)
cdc73a10
JH
44#endif
45#ifdef DL_CXT_EXTRA
89ca4ac7 46#define dl_cxtx (MY_CXT.x_dl_cxtx)
cdc73a10
JH
47#endif
48#ifdef DEBUGGING
89ca4ac7 49#define dl_debug (MY_CXT.x_dl_debug)
cdc73a10
JH
50#endif
51
a0d0e21e 52#ifdef DEBUGGING
cdc73a10
JH
53#define DLDEBUG(level,code) \
54 STMT_START { \
55 dMY_CXT; \
56 if (dl_debug>=level) { code; } \
57 } STMT_END
a0d0e21e 58#else
cdc73a10 59#define DLDEBUG(level,code) NOOP
a0d0e21e
LW
60#endif
61
c6c619a9 62#ifdef DL_UNLOAD_ALL_AT_EXIT
abb9e9dc
GS
63/* Close all dlopen'd files */
64static void
acfe0abc 65dl_unload_all_files(pTHX_ void *unused)
abb9e9dc
GS
66{
67 CV *sub;
68 AV *dl_librefs;
69 SV *dl_libref;
70
71 if ((sub = get_cv("DynaLoader::dl_unload_file", FALSE)) != NULL) {
72 dl_librefs = get_av("DynaLoader::dl_librefs", FALSE);
73 while ((dl_libref = av_pop(dl_librefs)) != &PL_sv_undef) {
74 dSP;
75 ENTER;
76 SAVETMPS;
77 PUSHMARK(SP);
78 XPUSHs(sv_2mortal(dl_libref));
79 PUTBACK;
22851543 80 call_sv((SV*)sub, G_DISCARD | G_NODEBUG);
abb9e9dc
GS
81 FREETMPS;
82 LEAVE;
83 }
84 }
85}
c6c619a9 86#endif
abb9e9dc 87
a0d0e21e 88static void
acfe0abc 89dl_generic_private_init(pTHX) /* called by dl_*.xs dl_private_init() */
a0d0e21e 90{
8e07c86e 91 char *perl_dl_nonlazy;
89ca4ac7 92 MY_CXT_INIT;
cdc73a10 93
591022fb 94 MY_CXT.x_dl_last_error = newSVpvn("", 0);
cdc73a10
JH
95 dl_nonlazy = 0;
96#ifdef DL_LOADONCEONLY
5c284bb0 97 dl_loaded_files = NULL;
cdc73a10 98#endif
a0d0e21e 99#ifdef DEBUGGING
cdc73a10
JH
100 {
101 SV *sv = get_sv("DynaLoader::dl_debug", 0);
102 dl_debug = sv ? SvIV(sv) : 0;
103 }
8e07c86e
AD
104#endif
105 if ( (perl_dl_nonlazy = getenv("PERL_DL_NONLAZY")) != NULL )
106 dl_nonlazy = atoi(perl_dl_nonlazy);
107 if (dl_nonlazy)
bf49b057 108 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "DynaLoader bind mode is 'non-lazy'\n"));
8e07c86e
AD
109#ifdef DL_LOADONCEONLY
110 if (!dl_loaded_files)
111 dl_loaded_files = newHV(); /* provide cache for dl_*.xs if needed */
a0d0e21e 112#endif
23d2500b 113#ifdef DL_UNLOAD_ALL_AT_EXIT
abb9e9dc 114 call_atexit(&dl_unload_all_files, (void*)0);
23d2500b 115#endif
a0d0e21e
LW
116}
117
118
27da23d5 119#ifndef SYMBIAN
cdc73a10 120/* SaveError() takes printf style args and saves the result in dl_last_error */
a0d0e21e 121static void
dd374669 122SaveError(pTHX_ const char* pat, ...)
a0d0e21e 123{
cdc73a10 124 dMY_CXT;
a0d0e21e 125 va_list args;
a6c40364 126 SV *msv;
dd374669 127 const char *message;
a6c40364 128 STRLEN len;
a0d0e21e 129
8e07c86e 130 /* This code is based on croak/warn, see mess() in util.c */
a0d0e21e 131
a0d0e21e 132 va_start(args, pat);
5a844595 133 msv = vmess(pat, &args);
a0d0e21e
LW
134 va_end(args);
135
a6c40364
GS
136 message = SvPV(msv,len);
137 len++; /* include terminating null char */
a0d0e21e 138
cdc73a10 139 /* Copy message into dl_last_error (including terminating null char) */
591022fb 140 sv_setpvn(MY_CXT.x_dl_last_error, message, len) ;
cdc73a10 141 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",dl_last_error));
a0d0e21e 142}
27da23d5 143#endif
a0d0e21e 144