1DynaLoader(3pm) Perl Programmers Reference Guide DynaLoader(3pm)
2
3
4
6 DynaLoader - Dynamically load C libraries into Perl code
7
9 package YourPackage;
10 require DynaLoader;
11 @ISA = qw(... DynaLoader ...);
12 __PACKAGE__->bootstrap;
13
14 # optional method for 'global' loading
15 sub dl_load_flags { 0x01 }
16
18 This document defines a standard generic interface to the dynamic
19 linking mechanisms available on many platforms. Its primary purpose is
20 to implement automatic dynamic loading of Perl modules.
21
22 This document serves as both a specification for anyone wishing to
23 implement the DynaLoader for a new platform and as a guide for anyone
24 wishing to use the DynaLoader directly in an application.
25
26 The DynaLoader is designed to be a very simple high-level interface
27 that is sufficiently general to cover the requirements of SunOS, HP-UX,
28 Linux, VMS and other platforms.
29
30 It is also hoped that the interface will cover the needs of OS/2, NT
31 etc and also allow pseudo-dynamic linking (using "ld -A" at runtime).
32
33 It must be stressed that the DynaLoader, by itself, is practically
34 useless for accessing non-Perl libraries because it provides almost no
35 Perl-to-C 'glue'. There is, for example, no mechanism for calling a C
36 library function or supplying arguments. A C::DynaLib module is
37 available from CPAN sites which performs that function for some common
38 system types. And since the year 2000, there's also Inline::C, a
39 module that allows you to write Perl subroutines in C. Also available
40 from your local CPAN site.
41
42 DynaLoader Interface Summary
43
44 @dl_library_path
45 @dl_resolve_using
46 @dl_require_symbols
47 $dl_debug
48 $dl_dlext
49 @dl_librefs
50 @dl_modules
51 @dl_shared_objects
52 Implemented in:
53 bootstrap($modulename) Perl
54 @filepaths = dl_findfile(@names) Perl
55 $flags = $modulename->dl_load_flags Perl
56 $symref = dl_find_symbol_anywhere($symbol) Perl
57
58 $libref = dl_load_file($filename, $flags) C
59 $status = dl_unload_file($libref) C
60 $symref = dl_find_symbol($libref, $symbol) C
61 @symbols = dl_undef_symbols() C
62 dl_install_xsub($name, $symref [, $filename]) C
63 $message = dl_error C
64
65 @dl_library_path
66 The standard/default list of directories in which dl_findfile()
67 will search for libraries etc. Directories are searched in order:
68 $dl_library_path[0], [1], ... etc
69
70 @dl_library_path is initialised to hold the list of 'normal'
71 directories (/usr/lib, etc) determined by Configure
72 ($Config{'libpth'}). This should ensure portability across a wide
73 range of platforms.
74
75 @dl_library_path should also be initialised with any other
76 directories that can be determined from the environment at runtime
77 (such as LD_LIBRARY_PATH for SunOS).
78
79 After initialisation @dl_library_path can be manipulated by an
80 application using push and unshift before calling dl_findfile().
81 Unshift can be used to add directories to the front of the search
82 order either to save search time or to override libraries with the
83 same name in the 'normal' directories.
84
85 The load function that dl_load_file() calls may require an absolute
86 pathname. The dl_findfile() function and @dl_library_path can be
87 used to search for and return the absolute pathname for the
88 library/object that you wish to load.
89
90 @dl_resolve_using
91 A list of additional libraries or other shared objects which can be
92 used to resolve any undefined symbols that might be generated by a
93 later call to load_file().
94
95 This is only required on some platforms which do not handle
96 dependent libraries automatically. For example the Socket Perl
97 extension library (auto/Socket/Socket.so) contains references to
98 many socket functions which need to be resolved when it's loaded.
99 Most platforms will automatically know where to find the
100 'dependent' library (e.g., /usr/lib/libsocket.so). A few platforms
101 need to be told the location of the dependent library explicitly.
102 Use @dl_resolve_using for this.
103
104 Example usage:
105
106 @dl_resolve_using = dl_findfile('-lsocket');
107
108 @dl_require_symbols
109 A list of one or more symbol names that are in the library/object
110 file to be dynamically loaded. This is only required on some
111 platforms.
112
113 @dl_librefs
114 An array of the handles returned by successful calls to
115 dl_load_file(), made by bootstrap, in the order in which they were
116 loaded. Can be used with dl_find_symbol() to look for a symbol in
117 any of the loaded files.
118
119 @dl_modules
120 An array of module (package) names that have been bootstrap'ed.
121
122 @dl_shared_objects
123 An array of file names for the shared objects that were loaded.
124
125 dl_error()
126 Syntax:
127
128 $message = dl_error();
129
130 Error message text from the last failed DynaLoader function. Note
131 that, similar to errno in unix, a successful function call does not
132 reset this message.
133
134 Implementations should detect the error as soon as it occurs in any
135 of the other functions and save the corresponding message for later
136 retrieval. This will avoid problems on some platforms (such as
137 SunOS) where the error message is very temporary (e.g., dlerror()).
138
139 $dl_debug
140 Internal debugging messages are enabled when $dl_debug is set true.
141 Currently setting $dl_debug only affects the Perl side of the
142 DynaLoader. These messages should help an application developer to
143 resolve any DynaLoader usage problems.
144
145 $dl_debug is set to $ENV{'PERL_DL_DEBUG'} if defined.
146
147 For the DynaLoader developer/porter there is a similar debugging
148 variable added to the C code (see dlutils.c) and enabled if Perl
149 was built with the -DDEBUGGING flag. This can also be set via the
150 PERL_DL_DEBUG environment variable. Set to 1 for minimal
151 information or higher for more.
152
153 $dl_dlext
154 When specified (localised) in a module's .pm file, indicates the
155 extension which the module's loadable object will have. For
156 example:
157
158 local $DynaLoader::dl_dlext = 'unusual_ext';
159
160 would indicate that the module's loadable object has an extension
161 of "unusual_ext" instead of the more usual $Config{dlext}. NOTE:
162 This also requires that the module's Makefile.PL specify (in
163 "WriteMakefile()"):
164
165 DLEXT => 'unusual_ext',
166
167 dl_findfile()
168 Syntax:
169
170 @filepaths = dl_findfile(@names)
171
172 Determine the full paths (including file suffix) of one or more
173 loadable files given their generic names and optionally one or more
174 directories. Searches directories in @dl_library_path by default
175 and returns an empty list if no files were found.
176
177 Names can be specified in a variety of platform independent forms.
178 Any names in the form -lname are converted into libname.*, where .*
179 is an appropriate suffix for the platform.
180
181 If a name does not already have a suitable prefix and/or suffix
182 then the corresponding file will be searched for by trying
183 combinations of prefix and suffix appropriate to the platform:
184 "$name.o", "lib$name.*" and "$name".
185
186 If any directories are included in @names they are searched before
187 @dl_library_path. Directories may be specified as -Ldir. Any
188 other names are treated as filenames to be searched for.
189
190 Using arguments of the form "-Ldir" and "-lname" is recommended.
191
192 Example:
193
194 @dl_resolve_using = dl_findfile(qw(-L/usr/5lib -lposix));
195
196 dl_expandspec()
197 Syntax:
198
199 $filepath = dl_expandspec($spec)
200
201 Some unusual systems, such as VMS, require special filename
202 handling in order to deal with symbolic names for files (i.e.,
203 VMS's Logical Names).
204
205 To support these systems a dl_expandspec() function can be
206 implemented either in the dl_*.xs file or code can be added to the
207 dl_expandspec() function in DynaLoader.pm. See DynaLoader_pm.PL
208 for more information.
209
210 dl_load_file()
211 Syntax:
212
213 $libref = dl_load_file($filename, $flags)
214
215 Dynamically load $filename, which must be the path to a shared
216 object or library. An opaque 'library reference' is returned as a
217 handle for the loaded object. Returns undef on error.
218
219 The $flags argument to alters dl_load_file behaviour. Assigned
220 bits:
221
222 0x01 make symbols available for linking later dl_load_file's.
223 (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL))
224 (ignored under VMS; this is a normal part of image linking)
225
226 (On systems that provide a handle for the loaded object such as
227 SunOS and HPUX, $libref will be that handle. On other systems
228 $libref will typically be $filename or a pointer to a buffer
229 containing $filename. The application should not examine or alter
230 $libref in any way.)
231
232 This is the function that does the real work. It should use the
233 current values of @dl_require_symbols and @dl_resolve_using if
234 required.
235
236 SunOS: dlopen($filename)
237 HP-UX: shl_load($filename)
238 Linux: dld_create_reference(@dl_require_symbols); dld_link($filename)
239 VMS: lib$find_image_symbol($filename,$dl_require_symbols[0])
240
241 (The dlopen() function is also used by Solaris and some versions of
242 Linux, and is a common choice when providing a "wrapper" on other
243 mechanisms as is done in the OS/2 port.)
244
245 dl_unload_file()
246 Syntax:
247
248 $status = dl_unload_file($libref)
249
250 Dynamically unload $libref, which must be an opaque 'library
251 reference' as returned from dl_load_file. Returns one on success
252 and zero on failure. This function is optional and may not
253 necessarily be provided on all platforms.
254
255 If it is defined and perl is compiled with the C macro
256 "DL_UNLOAD_ALL_AT_EXIT" defined, then it is called automatically
257 when the interpreter exits for every shared object or library
258 loaded by DynaLoader::bootstrap. All such library references are
259 stored in @dl_librefs by DynaLoader::Bootstrap as it loads the
260 libraries. The files are unloaded in last-in, first-out order.
261
262 This unloading is usually necessary when embedding a shared-object
263 perl (e.g. one configured with -Duseshrplib) within a larger
264 application, and the perl interpreter is created and destroyed
265 several times within the lifetime of the application. In this case
266 it is possible that the system dynamic linker will unload and then
267 subsequently reload the shared libperl without relocating any
268 references to it from any files DynaLoaded by the previous
269 incarnation of the interpreter. As a result, any shared objects
270 opened by DynaLoader may point to a now invalid 'ghost' of the
271 libperl shared object, causing apparently random memory corruption
272 and crashes. This behaviour is most commonly seen when using
273 Apache and mod_perl built with the APXS mechanism.
274
275 SunOS: dlclose($libref)
276 HP-UX: ???
277 Linux: ???
278 VMS: ???
279
280 (The dlclose() function is also used by Solaris and some versions
281 of Linux, and is a common choice when providing a "wrapper" on
282 other mechanisms as is done in the OS/2 port.)
283
284 dl_load_flags()
285 Syntax:
286
287 $flags = dl_load_flags $modulename;
288
289 Designed to be a method call, and to be overridden by a derived
290 class (i.e. a class which has DynaLoader in its @ISA). The
291 definition in DynaLoader itself returns 0, which produces standard
292 behavior from dl_load_file().
293
294 dl_find_symbol()
295 Syntax:
296
297 $symref = dl_find_symbol($libref, $symbol)
298
299 Return the address of the symbol $symbol or "undef" if not found.
300 If the target system has separate functions to search for symbols
301 of different types then dl_find_symbol() should search for function
302 symbols first and then other types.
303
304 The exact manner in which the address is returned in $symref is not
305 currently defined. The only initial requirement is that $symref
306 can be passed to, and understood by, dl_install_xsub().
307
308 SunOS: dlsym($libref, $symbol)
309 HP-UX: shl_findsym($libref, $symbol)
310 Linux: dld_get_func($symbol) and/or dld_get_symbol($symbol)
311 VMS: lib$find_image_symbol($libref,$symbol)
312
313 dl_find_symbol_anywhere()
314 Syntax:
315
316 $symref = dl_find_symbol_anywhere($symbol)
317
318 Applies dl_find_symbol() to the members of @dl_librefs and returns
319 the first match found.
320
321 dl_undef_symbols()
322 Example
323
324 @symbols = dl_undef_symbols()
325
326 Return a list of symbol names which remain undefined after
327 load_file(). Returns "()" if not known. Don't worry if your
328 platform does not provide a mechanism for this. Most do not need
329 it and hence do not provide it, they just return an empty list.
330
331 dl_install_xsub()
332 Syntax:
333
334 dl_install_xsub($perl_name, $symref [, $filename])
335
336 Create a new Perl external subroutine named $perl_name using
337 $symref as a pointer to the function which implements the routine.
338 This is simply a direct call to newXS()/newXS_flags(). Returns a
339 reference to the installed function.
340
341 The $filename parameter is used by Perl to identify the source file
342 for the function if required by die(), caller() or the debugger.
343 If $filename is not defined then "DynaLoader" will be used.
344
345 bootstrap()
346 Syntax:
347
348 bootstrap($module [...])
349
350 This is the normal entry point for automatic dynamic loading in
351 Perl.
352
353 It performs the following actions:
354
355 • locates an auto/$module directory by searching @INC
356
357 • uses dl_findfile() to determine the filename to load
358
359 • sets @dl_require_symbols to "("boot_$module")"
360
361 • executes an auto/$module/$module.bs file if it exists
362 (typically used to add to @dl_resolve_using any files which
363 are required to load the module on the current platform)
364
365 • calls dl_load_flags() to determine how to load the file.
366
367 • calls dl_load_file() to load the file
368
369 • calls dl_undef_symbols() and warns if any symbols are
370 undefined
371
372 • calls dl_find_symbol() for "boot_$module"
373
374 • calls dl_install_xsub() to install it as
375 "${module}::bootstrap"
376
377 • calls &{"${module}::bootstrap"} to bootstrap the module
378 (actually it uses the function reference returned by
379 dl_install_xsub for speed)
380
381 All arguments to bootstrap() are passed to the module's bootstrap
382 function. The default code generated by xsubpp expects $module [,
383 $version] If the optional $version argument is not given, it
384 defaults to "$XS_VERSION // $VERSION" in the module's symbol table.
385 The default code compares the Perl-space version with the version
386 of the compiled XS code, and croaks with an error if they do not
387 match.
388
390 Tim Bunce, 11 August 1994.
391
392 This interface is based on the work and comments of (in no particular
393 order): Larry Wall, Robert Sanders, Dean Roehrich, Jeff Okamoto, Anno
394 Siegel, Thomas Neumann, Paul Marquess, Charles Bailey, myself and
395 others.
396
397 Larry Wall designed the elegant inherited bootstrap mechanism and
398 implemented the first Perl 5 dynamic loader using it.
399
400 Solaris global loading added by Nick Ing-Simmons with design/coding
401 assistance from Tim Bunce, January 1996.
402
403
404
405perl v5.36.0 2022-08-30 DynaLoader(3pm)