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