Commit | Line | Data |
03c9e98c |
1 | use Config; |
2 | |
3 | sub to_string { |
4 | my ($value) = @_; |
146174a9 |
5 | $value =~ s/\\/\\\\/g; |
03c9e98c |
6 | $value =~ s/'/\\'/g; |
7 | return "'$value'"; |
8 | } |
9 | |
10 | unlink "DynaLoader.pm" if -f "DynaLoader.pm"; |
11 | open OUT, ">DynaLoader.pm" or die $!; |
12 | print OUT <<'EOT'; |
13 | |
d0a5ed9f |
14 | # Generated from DynaLoader.pm.PL |
03c9e98c |
15 | |
a0d0e21e |
16 | package DynaLoader; |
17 | |
8e07c86e |
18 | # And Gandalf said: 'Many folk like to know beforehand what is to |
19 | # be set on the table; but those who have laboured to prepare the |
20 | # feast like to keep their secret; for wonder makes the words of |
21 | # praise louder.' |
22 | |
6662521e |
23 | # (Quote from Tolkien suggested by Anno Siegel.) |
8e07c86e |
24 | # |
25 | # See pod text at end of file for documentation. |
26 | # See also ext/DynaLoader/README in source tree for other information. |
27 | # |
28 | # Tim.Bunce@ig.co.uk, August 1994 |
29 | |
632a9a7f |
30 | use vars qw($VERSION *AUTOLOAD); |
31 | |
32 | $VERSION = 1.04; # avoid typo warning |
dc848c6f |
33 | |
dc848c6f |
34 | require AutoLoader; |
35 | *AUTOLOAD = \&AutoLoader::AUTOLOAD; |
8e07c86e |
36 | |
7e9f3af8 |
37 | use Config; |
38 | |
fb73857a |
39 | # The following require can't be removed during maintenance |
40 | # releases, sadly, because of the risk of buggy code that does |
41 | # require Carp; Carp::croak "..."; without brackets dying |
42 | # if Carp hasn't been loaded in earlier compile time. :-( |
43 | # We'll let those bugs get found on the development track. |
44 | require Carp if $] < 5.00450; |
45 | |
8e07c86e |
46 | # enable debug/trace messages from DynaLoader perl code |
47 | $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug; |
48 | |
ff7f3c60 |
49 | # |
50 | # Flags to alter dl_load_file behaviour. Assigned bits: |
51 | # 0x01 make symbols available for linking later dl_load_file's. |
52 | # (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL)) |
f86702cc |
53 | # (ignored under VMS; effect is built-in to image linking) |
ff7f3c60 |
54 | # |
55 | # This is called as a class method $module->dl_load_flags. The |
56 | # definition here will be inherited and result on "default" loading |
57 | # behaviour unless a sub-class of DynaLoader defines its own version. |
58 | # |
59 | |
60 | sub dl_load_flags { 0x00 } |
61 | |
03c9e98c |
62 | # ($dl_dlext, $dlsrc) |
63 | # = @Config::Config{'dlext', 'dlsrc'}; |
64 | EOT |
65 | |
66 | print OUT " (\$dl_dlext, \$dlsrc) = (", |
67 | to_string($Config::Config{'dlext'}), ",", |
68 | to_string($Config::Config{'dlsrc'}), ")\n;" ; |
ff7f3c60 |
69 | |
03c9e98c |
70 | print OUT <<'EOT'; |
8e07c86e |
71 | |
72 | # Some systems need special handling to expand file specifications |
73 | # (VMS support by Charles Bailey <bailey@HMIVAX.HUMGEN.UPENN.EDU>) |
74 | # See dl_expandspec() for more details. Should be harmless but |
75 | # inefficient to define on systems that don't need it. |
632a9a7f |
76 | $Is_VMS = $^O eq 'VMS'; |
77 | $do_expand = $Is_VMS; |
78 | $Is_MacOS = $^O eq 'MacOS'; |
8e07c86e |
79 | |
80 | @dl_require_symbols = (); # names of symbols we need |
81 | @dl_resolve_using = (); # names of files to link with |
82 | @dl_library_path = (); # path to look for files |
bff25830 |
83 | |
84 | #XSLoader.pm may have added elements before we were required |
85 | #@dl_librefs = (); # things we have loaded |
86 | #@dl_modules = (); # Modules we have loaded |
8e07c86e |
87 | |
88 | # This is a fix to support DLD's unfortunate desire to relink -lc |
89 | @dl_resolve_using = dl_findfile('-lc') if $dlsrc eq "dl_dld.xs"; |
90 | |
d0a5ed9f |
91 | EOT |
03c9e98c |
92 | |
d0a5ed9f |
93 | my $cfg_dl_library_path = <<'EOT'; |
632a9a7f |
94 | push(@dl_library_path, split(' ', $Config::Config{libpth})); |
03c9e98c |
95 | EOT |
96 | |
d0a5ed9f |
97 | sub dquoted_comma_list { |
98 | join(", ", map {qq("$_")} @_); |
99 | } |
03c9e98c |
100 | |
d0a5ed9f |
101 | if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) { |
102 | eval $cfg_dl_library_path; |
103 | if (!$ENV{PERL_BUILD_EXPAND_ENV_VARS}) { |
104 | my $dl_library_path = dquoted_comma_list(@dl_library_path); |
105 | print OUT <<EOT; |
7e9f3af8 |
106 | # The below \@dl_library_path has been expanded (%Config) in Perl build time. |
d0a5ed9f |
107 | |
108 | \@dl_library_path = ($dl_library_path); |
109 | |
110 | EOT |
111 | } |
112 | } |
113 | else { |
114 | print OUT <<EOT; |
115 | # Initialise \@dl_library_path with the 'standard' library path |
116 | # for this platform as determined by Configure. |
117 | |
118 | $cfg_dl_library_path |
119 | |
120 | EOT |
121 | } |
8e07c86e |
122 | |
8225e35f |
123 | my $ldlibpthname; |
7e9f3af8 |
124 | my $ldlibpthname_defined; |
8225e35f |
125 | my $pthsep; |
126 | |
127 | if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) { |
7e9f3af8 |
128 | $ldlibpthname = $Config::Config{ldlibpthname}; |
129 | $ldlibpthname_defined = defined $Config::Config{ldlibpthname} ? 1 : 0; |
130 | $pthsep = $Config::Config{path_sep}; |
8225e35f |
131 | } |
132 | else { |
7e9f3af8 |
133 | $ldlibpthname = q($Config::Config{ldlibpthname}); |
134 | $ldlibpthname_defined = q(defined $Config::Config{ldlibpthname}); |
135 | $pthsep = q($Config::Config{path_sep}); |
8225e35f |
136 | print OUT <<EOT; |
7e9f3af8 |
137 | my \$ldlibpthname = $ldlibpthname; |
138 | my \$ldlibpthname_defined = $ldlibpthname_defined; |
139 | my \$pthsep = $pthsep; |
8225e35f |
140 | |
141 | EOT |
142 | } |
143 | |
7e9f3af8 |
144 | my $env_dl_library_path = <<'EOT'; |
145 | if ($ldlibpthname_defined && |
146 | exists $ENV{$ldlibpthname}) { |
147 | push(@dl_library_path, split(/$pthsep/, $ENV{$ldlibpthname})); |
8225e35f |
148 | } |
149 | |
5cf1d1f1 |
150 | # E.g. HP-UX supports both its native SHLIB_PATH *and* LD_LIBRARY_PATH. |
8225e35f |
151 | |
7e9f3af8 |
152 | if ($ldlibpthname_defined && |
153 | $ldlibpthname ne 'LD_LIBRARY_PATH' && |
154 | exists $ENV{LD_LIBRARY_PATH}) { |
155 | push(@dl_library_path, split(/$pthsep/, $ENV{LD_LIBRARY_PATH})); |
d0a5ed9f |
156 | } |
e6197cab |
157 | EOT |
158 | |
d0a5ed9f |
159 | if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS} && $ENV{PERL_BUILD_EXPAND_ENV_VARS}) { |
160 | eval $env_dl_library_path; |
e6197cab |
161 | } |
d0a5ed9f |
162 | else { |
163 | print OUT <<EOT; |
164 | # Add to \@dl_library_path any extra directories we can gather from environment |
165 | # during runtime. |
e6197cab |
166 | |
d0a5ed9f |
167 | $env_dl_library_path |
168 | |
169 | EOT |
e6197cab |
170 | } |
171 | |
d0a5ed9f |
172 | if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS} && $ENV{PERL_BUILD_EXPAND_ENV_VARS}) { |
173 | my $dl_library_path = dquoted_comma_list(@dl_library_path); |
174 | print OUT <<EOT; |
7e9f3af8 |
175 | # The below \@dl_library_path has been expanded (%Config, %ENV) |
176 | # in Perl build time. |
d0a5ed9f |
177 | |
178 | \@dl_library_path = ($dl_library_path); |
179 | |
180 | EOT |
146174a9 |
181 | } |
8e07c86e |
182 | |
d0a5ed9f |
183 | print OUT <<'EOT'; |
8e07c86e |
184 | # No prizes for guessing why we don't say 'bootstrap DynaLoader;' here. |
b4cea227 |
185 | # NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB |
bb3913c7 |
186 | boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) && |
b4cea227 |
187 | !defined(&dl_error); |
8e07c86e |
188 | |
189 | if ($dl_debug) { |
190 | print STDERR "DynaLoader.pm loaded (@INC, @dl_library_path)\n"; |
191 | print STDERR "DynaLoader not linked into this perl\n" |
192 | unless defined(&boot_DynaLoader); |
193 | } |
194 | |
195 | 1; # End of main code |
196 | |
197 | |
fb73857a |
198 | sub croak { require Carp; Carp::croak(@_) } |
199 | |
146174a9 |
200 | sub bootstrap_inherit { |
201 | my $module = $_[0]; |
202 | local *isa = *{"$module\::ISA"}; |
203 | local @isa = (@isa, 'DynaLoader'); |
204 | # Cannot goto due to delocalization. Will report errors on a wrong line? |
205 | bootstrap(@_); |
206 | } |
207 | |
8e07c86e |
208 | # The bootstrap function cannot be autoloaded (without complications) |
209 | # so we define it here: |
210 | |
211 | sub bootstrap { |
212 | # use local vars to enable $module.bs script to edit values |
213 | local(@args) = @_; |
214 | local($module) = $args[0]; |
215 | local(@dirs, $file); |
216 | |
fb73857a |
217 | unless ($module) { |
218 | require Carp; |
219 | Carp::confess("Usage: DynaLoader::bootstrap(module)"); |
220 | } |
8e07c86e |
221 | |
222 | # A common error on platforms which don't support dynamic loading. |
223 | # Since it's fatal and potentially confusing we give a detailed message. |
fb73857a |
224 | croak("Can't load module $module, dynamic loading not available in this perl.\n". |
8e07c86e |
225 | " (You may need to build a new perl executable which either supports\n". |
226 | " dynamic loading or has the $module module statically linked into it.)\n") |
227 | unless defined(&dl_load_file); |
228 | |
229 | my @modparts = split(/::/,$module); |
230 | my $modfname = $modparts[-1]; |
231 | |
232 | # Some systems have restrictions on files names for DLL's etc. |
233 | # mod2fname returns appropriate file base name (typically truncated) |
234 | # It may also edit @modparts if required. |
235 | $modfname = &mod2fname(\@modparts) if defined &mod2fname; |
236 | |
146174a9 |
237 | my $modpname = join(($Is_MacOS ? ':' : '/'),@modparts); |
8e07c86e |
238 | |
239 | print STDERR "DynaLoader::bootstrap for $module ", |
146174a9 |
240 | ($Is_MacOS |
6662521e |
241 | ? "(:auto:$modpname:$modfname.$dl_dlext)\n" : |
242 | "(auto/$modpname/$modfname.$dl_dlext)\n") |
146174a9 |
243 | if $dl_debug; |
8e07c86e |
244 | |
245 | foreach (@INC) { |
54d6a3b3 |
246 | chop($_ = VMS::Filespec::unixpath($_)) if $Is_VMS; |
146174a9 |
247 | my $dir; |
248 | if ($Is_MacOS) { |
e69a2255 |
249 | my $path = $_; |
250 | $path .= ":" unless /:$/; |
251 | $dir = "${path}auto:$modpname"; |
146174a9 |
252 | } else { |
253 | $dir = "$_/auto/$modpname"; |
254 | } |
8e07c86e |
255 | next unless -d $dir; # skip over uninteresting directories |
256 | |
257 | # check for common cases to avoid autoload of dl_findfile |
146174a9 |
258 | my $try = $Is_MacOS ? "$dir:$modfname.$dl_dlext" : "$dir/$modfname.$dl_dlext"; |
fb73857a |
259 | last if $file = ($do_expand) ? dl_expandspec($try) : (-f $try && $try); |
8e07c86e |
260 | |
261 | # no luck here, save dir for possible later dl_findfile search |
fb73857a |
262 | push @dirs, $dir; |
8e07c86e |
263 | } |
264 | # last resort, let dl_findfile have a go in all known locations |
fb73857a |
265 | $file = dl_findfile(map("-L$_",@dirs,@INC), $modfname) unless $file; |
8e07c86e |
266 | |
fb73857a |
267 | croak("Can't locate loadable object for module $module in \@INC (\@INC contains: @INC)") |
268 | unless $file; # wording similar to error from 'require' |
8e07c86e |
269 | |
7e9f3af8 |
270 | $file = uc($file) if $Is_VMS && $Config::Config{d_vms_case_sensitive_symbols}; |
8e07c86e |
271 | my $bootname = "boot_$module"; |
272 | $bootname =~ s/\W/_/g; |
273 | @dl_require_symbols = ($bootname); |
274 | |
275 | # Execute optional '.bootstrap' perl script for this module. |
276 | # The .bs file can be used to configure @dl_resolve_using etc to |
277 | # match the needs of the individual module on this architecture. |
278 | my $bs = $file; |
3f45a6dd |
279 | $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library |
8e07c86e |
280 | if (-s $bs) { # only read file if it's not empty |
d404d5bf |
281 | print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug; |
8e07c86e |
282 | eval { do $bs; }; |
283 | warn "$bs: $@\n" if $@; |
284 | } |
285 | |
286 | # Many dynamic extension loading problems will appear to come from |
287 | # this section of code: XYZ failed at line 123 of DynaLoader.pm. |
288 | # Often these errors are actually occurring in the initialisation |
289 | # C code of the extension XS file. Perl reports the error as being |
290 | # in this perl code simply because this was the last perl code |
291 | # it executed. |
292 | |
ff7f3c60 |
293 | my $libref = dl_load_file($file, $module->dl_load_flags) or |
549a6b10 |
294 | croak("Can't load '$file' for module $module: ".dl_error()); |
8e07c86e |
295 | |
ff7f3c60 |
296 | push(@dl_librefs,$libref); # record loaded object |
297 | |
8e07c86e |
298 | my @unresolved = dl_undef_symbols(); |
fb73857a |
299 | if (@unresolved) { |
300 | require Carp; |
301 | Carp::carp("Undefined symbols present after loading $file: @unresolved\n"); |
302 | } |
8e07c86e |
303 | |
304 | my $boot_symbol_ref = dl_find_symbol($libref, $bootname) or |
fb73857a |
305 | croak("Can't find '$bootname' symbol in $file\n"); |
8e07c86e |
306 | |
307 | my $xs = dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file); |
308 | |
ff7f3c60 |
309 | push(@dl_modules, $module); # record loaded module |
310 | |
8e07c86e |
311 | # See comment block above |
312 | &$xs(@args); |
313 | } |
314 | |
315 | |
fb73857a |
316 | #sub _check_file { # private utility to handle dl_expandspec vs -f tests |
317 | # my($file) = @_; |
318 | # return $file if (!$do_expand && -f $file); # the common case |
319 | # return $file if ( $do_expand && ($file=dl_expandspec($file))); |
320 | # return undef; |
321 | #} |
8e07c86e |
322 | |
323 | |
324 | # Let autosplit and the autoloader deal with these functions: |
325 | __END__ |
326 | |
327 | |
328 | sub dl_findfile { |
329 | # Read ext/DynaLoader/DynaLoader.doc for detailed information. |
330 | # This function does not automatically consider the architecture |
331 | # or the perl library auto directories. |
332 | my (@args) = @_; |
333 | my (@dirs, $dir); # which directories to search |
334 | my (@found); # full paths to real files we have found |
03c9e98c |
335 | EOT |
336 | |
337 | print OUT ' my $dl_ext= ' . to_string($Config::Config{'dlext'}) . |
338 | "; # \$Config::Config{'dlext'} suffix for perl extensions\n"; |
339 | print OUT ' my $dl_so = ' . to_string($Config::Config{'so'}) . |
340 | "; # \$Config::Config{'so'} suffix for shared libraries\n"; |
341 | |
342 | print OUT <<'EOT'; |
8e07c86e |
343 | |
344 | print STDERR "dl_findfile(@args)\n" if $dl_debug; |
345 | |
346 | # accumulate directories but process files as they appear |
347 | arg: foreach(@args) { |
348 | # Special fast case: full filepath requires no search |
54d6a3b3 |
349 | if ($Is_VMS && m%[:>/\]]% && -f $_) { |
350 | push(@found,dl_expandspec(VMS::Filespec::vmsify($_))); |
351 | last arg unless wantarray; |
352 | next; |
353 | } |
146174a9 |
354 | elsif ($Is_MacOS) { |
355 | if (m/:/ && -f $_) { |
356 | push(@found,$_); |
357 | last arg unless wantarray; |
358 | } |
359 | } |
54d6a3b3 |
360 | elsif (m:/: && -f $_ && !$do_expand) { |
8e07c86e |
361 | push(@found,$_); |
362 | last arg unless wantarray; |
363 | next; |
364 | } |
365 | |
366 | # Deal with directories first: |
367 | # Using a -L prefix is the preferred option (faster and more robust) |
368 | if (m:^-L:) { s/^-L//; push(@dirs, $_); next; } |
369 | |
146174a9 |
370 | if ($Is_MacOS) { |
371 | # Otherwise we try to try to spot directories by a heuristic |
372 | # (this is a more complicated issue than it first appears) |
373 | if (m/:/ && -d $_) { push(@dirs, $_); next; } |
374 | # Only files should get this far... |
375 | my(@names, $name); # what filenames to look for |
376 | s/^-l//; |
377 | push(@names, $_); |
378 | foreach $dir (@dirs, @dl_library_path) { |
379 | next unless -d $dir; |
380 | $dir =~ s/^([^:]+)$/:$1/; |
381 | $dir =~ s/:$//; |
382 | foreach $name (@names) { |
383 | my($file) = "$dir:$name"; |
384 | print STDERR " checking in $dir for $name\n" if $dl_debug; |
385 | if (-f $file) { |
386 | push(@found, $file); |
387 | next arg; # no need to look any further |
388 | } |
389 | } |
390 | } |
391 | next; |
392 | } |
393 | |
8e07c86e |
394 | # Otherwise we try to try to spot directories by a heuristic |
395 | # (this is a more complicated issue than it first appears) |
396 | if (m:/: && -d $_) { push(@dirs, $_); next; } |
397 | |
7e9f3af8 |
398 | # VMS: we may be using native VMS directory syntax instead of |
8e07c86e |
399 | # Unix emulation, so check this as well |
54d6a3b3 |
400 | if ($Is_VMS && /[:>\]]/ && -d $_) { push(@dirs, $_); next; } |
8e07c86e |
401 | |
402 | # Only files should get this far... |
403 | my(@names, $name); # what filenames to look for |
404 | if (m:-l: ) { # convert -lname to appropriate library name |
405 | s/-l//; |
406 | push(@names,"lib$_.$dl_so"); |
407 | push(@names,"lib$_.a"); |
408 | } else { # Umm, a bare name. Try various alternatives: |
409 | # these should be ordered with the most likely first |
d404d5bf |
410 | push(@names,"$_.$dl_ext") unless m/\.$dl_ext$/o; |
8e07c86e |
411 | push(@names,"$_.$dl_so") unless m/\.$dl_so$/o; |
412 | push(@names,"lib$_.$dl_so") unless m:/:; |
8e07c86e |
413 | push(@names,"$_.a") if !m/\.a$/ and $dlsrc eq "dl_dld.xs"; |
414 | push(@names, $_); |
415 | } |
416 | foreach $dir (@dirs, @dl_library_path) { |
417 | next unless -d $dir; |
54d6a3b3 |
418 | chop($dir = VMS::Filespec::unixpath($dir)) if $Is_VMS; |
8e07c86e |
419 | foreach $name (@names) { |
420 | my($file) = "$dir/$name"; |
421 | print STDERR " checking in $dir for $name\n" if $dl_debug; |
fb73857a |
422 | $file = ($do_expand) ? dl_expandspec($file) : (-f $file && $file); |
423 | #$file = _check_file($file); |
8e07c86e |
424 | if ($file) { |
425 | push(@found, $file); |
426 | next arg; # no need to look any further |
427 | } |
428 | } |
429 | } |
430 | } |
431 | if ($dl_debug) { |
432 | foreach(@dirs) { |
433 | print STDERR " dl_findfile ignored non-existent directory: $_\n" unless -d $_; |
434 | } |
435 | print STDERR "dl_findfile found: @found\n"; |
436 | } |
437 | return $found[0] unless wantarray; |
438 | @found; |
439 | } |
440 | |
441 | |
442 | sub dl_expandspec { |
443 | my($spec) = @_; |
444 | # Optional function invoked if DynaLoader.pm sets $do_expand. |
445 | # Most systems do not require or use this function. |
446 | # Some systems may implement it in the dl_*.xs file in which case |
447 | # this autoload version will not be called but is harmless. |
448 | |
449 | # This function is designed to deal with systems which treat some |
450 | # 'filenames' in a special way. For example VMS 'Logical Names' |
451 | # (something like unix environment variables - but different). |
452 | # This function should recognise such names and expand them into |
453 | # full file paths. |
454 | # Must return undef if $spec is invalid or file does not exist. |
455 | |
456 | my $file = $spec; # default output to input |
457 | |
d404d5bf |
458 | if ($Is_VMS) { # dl_expandspec should be defined in dl_vms.xs |
fb73857a |
459 | require Carp; |
4633a7c4 |
460 | Carp::croak("dl_expandspec: should be defined in XS file!\n"); |
8e07c86e |
461 | } else { |
462 | return undef unless -f $file; |
463 | } |
464 | print STDERR "dl_expandspec($spec) => $file\n" if $dl_debug; |
465 | $file; |
466 | } |
467 | |
ff7f3c60 |
468 | sub dl_find_symbol_anywhere |
469 | { |
470 | my $sym = shift; |
471 | my $libref; |
472 | foreach $libref (@dl_librefs) { |
473 | my $symref = dl_find_symbol($libref,$sym); |
474 | return $symref if $symref; |
475 | } |
476 | return undef; |
477 | } |
8e07c86e |
478 | |
3b35bae3 |
479 | =head1 NAME |
480 | |
481 | DynaLoader - Dynamically load C libraries into Perl code |
482 | |
abb9e9dc |
483 | dl_error(), dl_findfile(), dl_expandspec(), dl_load_file(), dl_unload_file(), dl_find_symbol(), dl_find_symbol_anywhere(), dl_undef_symbols(), dl_install_xsub(), dl_load_flags(), bootstrap() - routines used by DynaLoader modules |
3b35bae3 |
484 | |
485 | =head1 SYNOPSIS |
486 | |
8e07c86e |
487 | package YourPackage; |
3b35bae3 |
488 | require DynaLoader; |
c2960299 |
489 | @ISA = qw(... DynaLoader ...); |
8e07c86e |
490 | bootstrap YourPackage; |
3b35bae3 |
491 | |
ff7f3c60 |
492 | # optional method for 'global' loading |
493 | sub dl_load_flags { 0x01 } |
494 | |
3b35bae3 |
495 | |
496 | =head1 DESCRIPTION |
497 | |
c2960299 |
498 | This document defines a standard generic interface to the dynamic |
3b35bae3 |
499 | linking mechanisms available on many platforms. Its primary purpose is |
500 | to implement automatic dynamic loading of Perl modules. |
501 | |
c2960299 |
502 | This document serves as both a specification for anyone wishing to |
503 | implement the DynaLoader for a new platform and as a guide for |
504 | anyone wishing to use the DynaLoader directly in an application. |
505 | |
3b35bae3 |
506 | The DynaLoader is designed to be a very simple high-level |
507 | interface that is sufficiently general to cover the requirements |
508 | of SunOS, HP-UX, NeXT, Linux, VMS and other platforms. |
509 | |
c2960299 |
510 | It is also hoped that the interface will cover the needs of OS/2, NT |
511 | etc and also allow pseudo-dynamic linking (using C<ld -A> at runtime). |
3b35bae3 |
512 | |
513 | It must be stressed that the DynaLoader, by itself, is practically |
514 | useless for accessing non-Perl libraries because it provides almost no |
515 | Perl-to-C 'glue'. There is, for example, no mechanism for calling a C |
03c9e98c |
516 | library function or supplying arguments. A C::DynaLib module |
90248788 |
517 | is available from CPAN sites which performs that function for some |
518 | common system types. |
3b35bae3 |
519 | |
520 | DynaLoader Interface Summary |
521 | |
522 | @dl_library_path |
523 | @dl_resolve_using |
524 | @dl_require_symbols |
525 | $dl_debug |
ff7f3c60 |
526 | @dl_librefs |
527 | @dl_modules |
3b35bae3 |
528 | Implemented in: |
529 | bootstrap($modulename) Perl |
530 | @filepaths = dl_findfile(@names) Perl |
ff7f3c60 |
531 | $flags = $modulename->dl_load_flags Perl |
532 | $symref = dl_find_symbol_anywhere($symbol) Perl |
3b35bae3 |
533 | |
ff7f3c60 |
534 | $libref = dl_load_file($filename, $flags) C |
abb9e9dc |
535 | $status = dl_unload_file($libref) C |
3b35bae3 |
536 | $symref = dl_find_symbol($libref, $symbol) C |
537 | @symbols = dl_undef_symbols() C |
538 | dl_install_xsub($name, $symref [, $filename]) C |
539 | $message = dl_error C |
540 | |
541 | =over 4 |
542 | |
543 | =item @dl_library_path |
544 | |
545 | The standard/default list of directories in which dl_findfile() will |
546 | search for libraries etc. Directories are searched in order: |
547 | $dl_library_path[0], [1], ... etc |
548 | |
549 | @dl_library_path is initialised to hold the list of 'normal' directories |
550 | (F</usr/lib>, etc) determined by B<Configure> (C<$Config{'libpth'}>). This should |
551 | ensure portability across a wide range of platforms. |
552 | |
553 | @dl_library_path should also be initialised with any other directories |
554 | that can be determined from the environment at runtime (such as |
555 | LD_LIBRARY_PATH for SunOS). |
556 | |
557 | After initialisation @dl_library_path can be manipulated by an |
558 | application using push and unshift before calling dl_findfile(). |
559 | Unshift can be used to add directories to the front of the search order |
560 | either to save search time or to override libraries with the same name |
561 | in the 'normal' directories. |
562 | |
563 | The load function that dl_load_file() calls may require an absolute |
564 | pathname. The dl_findfile() function and @dl_library_path can be |
565 | used to search for and return the absolute pathname for the |
566 | library/object that you wish to load. |
567 | |
568 | =item @dl_resolve_using |
569 | |
570 | A list of additional libraries or other shared objects which can be |
571 | used to resolve any undefined symbols that might be generated by a |
572 | later call to load_file(). |
573 | |
574 | This is only required on some platforms which do not handle dependent |
7a2e2cd6 |
575 | libraries automatically. For example the Socket Perl extension |
576 | library (F<auto/Socket/Socket.so>) contains references to many socket |
577 | functions which need to be resolved when it's loaded. Most platforms |
578 | will automatically know where to find the 'dependent' library (e.g., |
579 | F</usr/lib/libsocket.so>). A few platforms need to be told the |
580 | location of the dependent library explicitly. Use @dl_resolve_using |
581 | for this. |
3b35bae3 |
582 | |
583 | Example usage: |
584 | |
585 | @dl_resolve_using = dl_findfile('-lsocket'); |
586 | |
587 | =item @dl_require_symbols |
588 | |
589 | A list of one or more symbol names that are in the library/object file |
590 | to be dynamically loaded. This is only required on some platforms. |
591 | |
ff7f3c60 |
592 | =item @dl_librefs |
593 | |
594 | An array of the handles returned by successful calls to dl_load_file(), |
595 | made by bootstrap, in the order in which they were loaded. |
596 | Can be used with dl_find_symbol() to look for a symbol in any of |
597 | the loaded files. |
598 | |
599 | =item @dl_modules |
600 | |
601 | An array of module (package) names that have been bootstrap'ed. |
602 | |
3b35bae3 |
603 | =item dl_error() |
604 | |
605 | Syntax: |
606 | |
607 | $message = dl_error(); |
608 | |
609 | Error message text from the last failed DynaLoader function. Note |
610 | that, similar to errno in unix, a successful function call does not |
611 | reset this message. |
612 | |
613 | Implementations should detect the error as soon as it occurs in any of |
614 | the other functions and save the corresponding message for later |
615 | retrieval. This will avoid problems on some platforms (such as SunOS) |
616 | where the error message is very temporary (e.g., dlerror()). |
617 | |
618 | =item $dl_debug |
619 | |
620 | Internal debugging messages are enabled when $dl_debug is set true. |
621 | Currently setting $dl_debug only affects the Perl side of the |
622 | DynaLoader. These messages should help an application developer to |
623 | resolve any DynaLoader usage problems. |
624 | |
625 | $dl_debug is set to C<$ENV{'PERL_DL_DEBUG'}> if defined. |
626 | |
627 | For the DynaLoader developer/porter there is a similar debugging |
628 | variable added to the C code (see dlutils.c) and enabled if Perl was |
629 | built with the B<-DDEBUGGING> flag. This can also be set via the |
630 | PERL_DL_DEBUG environment variable. Set to 1 for minimal information or |
631 | higher for more. |
632 | |
633 | =item dl_findfile() |
634 | |
635 | Syntax: |
636 | |
637 | @filepaths = dl_findfile(@names) |
638 | |
639 | Determine the full paths (including file suffix) of one or more |
640 | loadable files given their generic names and optionally one or more |
641 | directories. Searches directories in @dl_library_path by default and |
642 | returns an empty list if no files were found. |
643 | |
644 | Names can be specified in a variety of platform independent forms. Any |
645 | names in the form B<-lname> are converted into F<libname.*>, where F<.*> is |
646 | an appropriate suffix for the platform. |
647 | |
648 | If a name does not already have a suitable prefix and/or suffix then |
649 | the corresponding file will be searched for by trying combinations of |
650 | prefix and suffix appropriate to the platform: "$name.o", "lib$name.*" |
651 | and "$name". |
652 | |
653 | If any directories are included in @names they are searched before |
c2960299 |
654 | @dl_library_path. Directories may be specified as B<-Ldir>. Any other |
655 | names are treated as filenames to be searched for. |
3b35bae3 |
656 | |
657 | Using arguments of the form C<-Ldir> and C<-lname> is recommended. |
658 | |
659 | Example: |
660 | |
661 | @dl_resolve_using = dl_findfile(qw(-L/usr/5lib -lposix)); |
662 | |
663 | |
664 | =item dl_expandspec() |
665 | |
666 | Syntax: |
667 | |
668 | $filepath = dl_expandspec($spec) |
669 | |
670 | Some unusual systems, such as VMS, require special filename handling in |
671 | order to deal with symbolic names for files (i.e., VMS's Logical Names). |
672 | |
673 | To support these systems a dl_expandspec() function can be implemented |
674 | either in the F<dl_*.xs> file or code can be added to the autoloadable |
c2960299 |
675 | dl_expandspec() function in F<DynaLoader.pm>. See F<DynaLoader.pm> for |
676 | more information. |
3b35bae3 |
677 | |
678 | =item dl_load_file() |
679 | |
680 | Syntax: |
681 | |
ff7f3c60 |
682 | $libref = dl_load_file($filename, $flags) |
3b35bae3 |
683 | |
684 | Dynamically load $filename, which must be the path to a shared object |
685 | or library. An opaque 'library reference' is returned as a handle for |
686 | the loaded object. Returns undef on error. |
687 | |
ff7f3c60 |
688 | The $flags argument to alters dl_load_file behaviour. |
689 | Assigned bits: |
690 | |
691 | 0x01 make symbols available for linking later dl_load_file's. |
692 | (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL)) |
f86702cc |
693 | (ignored under VMS; this is a normal part of image linking) |
ff7f3c60 |
694 | |
3b35bae3 |
695 | (On systems that provide a handle for the loaded object such as SunOS |
696 | and HPUX, $libref will be that handle. On other systems $libref will |
697 | typically be $filename or a pointer to a buffer containing $filename. |
698 | The application should not examine or alter $libref in any way.) |
699 | |
ff7f3c60 |
700 | This is the function that does the real work. It should use the |
701 | current values of @dl_require_symbols and @dl_resolve_using if required. |
3b35bae3 |
702 | |
703 | SunOS: dlopen($filename) |
704 | HP-UX: shl_load($filename) |
705 | Linux: dld_create_reference(@dl_require_symbols); dld_link($filename) |
706 | NeXT: rld_load($filename, @dl_resolve_using) |
707 | VMS: lib$find_image_symbol($filename,$dl_require_symbols[0]) |
708 | |
ff7f3c60 |
709 | (The dlopen() function is also used by Solaris and some versions of |
710 | Linux, and is a common choice when providing a "wrapper" on other |
711 | mechanisms as is done in the OS/2 port.) |
712 | |
abb9e9dc |
713 | =item dl_unload_file() |
714 | |
715 | Syntax: |
716 | |
717 | $status = dl_unload_file($libref) |
718 | |
719 | Dynamically unload $libref, which must be an opaque 'library reference' as |
720 | returned from dl_load_file. Returns one on success and zero on failure. |
721 | |
722 | This function is optional and may not necessarily be provided on all platforms. |
723 | If it is defined, it is called automatically when the interpreter exits for |
724 | every shared object or library loaded by DynaLoader::bootstrap. All such |
725 | library references are stored in @dl_librefs by DynaLoader::Bootstrap as it |
22851543 |
726 | loads the libraries. The files are unloaded in last-in, first-out order. |
abb9e9dc |
727 | |
728 | This unloading is usually necessary when embedding a shared-object perl (e.g. |
729 | one configured with -Duseshrplib) within a larger application, and the perl |
730 | interpreter is created and destroyed several times within the lifetime of the |
731 | application. In this case it is possible that the system dynamic linker will |
732 | unload and then subsequently reload the shared libperl without relocating any |
733 | references to it from any files DynaLoaded by the previous incarnation of the |
734 | interpreter. As a result, any shared objects opened by DynaLoader may point to |
735 | a now invalid 'ghost' of the libperl shared object, causing apparently random |
736 | memory corruption and crashes. This behaviour is most commonly seen when using |
737 | Apache and mod_perl built with the APXS mechanism. |
738 | |
739 | SunOS: dlclose($libref) |
740 | HP-UX: ??? |
741 | Linux: ??? |
742 | NeXT: ??? |
743 | VMS: ??? |
744 | |
745 | (The dlclose() function is also used by Solaris and some versions of |
746 | Linux, and is a common choice when providing a "wrapper" on other |
747 | mechanisms as is done in the OS/2 port.) |
748 | |
ff7f3c60 |
749 | =item dl_loadflags() |
750 | |
751 | Syntax: |
752 | |
753 | $flags = dl_loadflags $modulename; |
754 | |
755 | Designed to be a method call, and to be overridden by a derived class |
756 | (i.e. a class which has DynaLoader in its @ISA). The definition in |
757 | DynaLoader itself returns 0, which produces standard behavior from |
758 | dl_load_file(). |
3b35bae3 |
759 | |
760 | =item dl_find_symbol() |
761 | |
762 | Syntax: |
763 | |
764 | $symref = dl_find_symbol($libref, $symbol) |
765 | |
766 | Return the address of the symbol $symbol or C<undef> if not found. If the |
767 | target system has separate functions to search for symbols of different |
768 | types then dl_find_symbol() should search for function symbols first and |
769 | then other types. |
770 | |
771 | The exact manner in which the address is returned in $symref is not |
772 | currently defined. The only initial requirement is that $symref can |
773 | be passed to, and understood by, dl_install_xsub(). |
774 | |
775 | SunOS: dlsym($libref, $symbol) |
776 | HP-UX: shl_findsym($libref, $symbol) |
777 | Linux: dld_get_func($symbol) and/or dld_get_symbol($symbol) |
778 | NeXT: rld_lookup("_$symbol") |
779 | VMS: lib$find_image_symbol($libref,$symbol) |
780 | |
781 | |
ff7f3c60 |
782 | =item dl_find_symbol_anywhere() |
783 | |
784 | Syntax: |
785 | |
786 | $symref = dl_find_symbol_anywhere($symbol) |
787 | |
788 | Applies dl_find_symbol() to the members of @dl_librefs and returns |
789 | the first match found. |
790 | |
3b35bae3 |
791 | =item dl_undef_symbols() |
792 | |
793 | Example |
794 | |
795 | @symbols = dl_undef_symbols() |
796 | |
797 | Return a list of symbol names which remain undefined after load_file(). |
798 | Returns C<()> if not known. Don't worry if your platform does not provide |
c2960299 |
799 | a mechanism for this. Most do not need it and hence do not provide it, |
800 | they just return an empty list. |
3b35bae3 |
801 | |
802 | |
803 | =item dl_install_xsub() |
804 | |
805 | Syntax: |
806 | |
807 | dl_install_xsub($perl_name, $symref [, $filename]) |
808 | |
809 | Create a new Perl external subroutine named $perl_name using $symref as |
810 | a pointer to the function which implements the routine. This is simply |
811 | a direct call to newXSUB(). Returns a reference to the installed |
812 | function. |
813 | |
814 | The $filename parameter is used by Perl to identify the source file for |
815 | the function if required by die(), caller() or the debugger. If |
816 | $filename is not defined then "DynaLoader" will be used. |
817 | |
818 | |
1fef88e7 |
819 | =item bootstrap() |
3b35bae3 |
820 | |
821 | Syntax: |
822 | |
823 | bootstrap($module) |
824 | |
825 | This is the normal entry point for automatic dynamic loading in Perl. |
826 | |
827 | It performs the following actions: |
828 | |
829 | =over 8 |
830 | |
831 | =item * |
832 | |
833 | locates an auto/$module directory by searching @INC |
834 | |
835 | =item * |
836 | |
837 | uses dl_findfile() to determine the filename to load |
838 | |
839 | =item * |
840 | |
841 | sets @dl_require_symbols to C<("boot_$module")> |
842 | |
843 | =item * |
844 | |
845 | executes an F<auto/$module/$module.bs> file if it exists |
846 | (typically used to add to @dl_resolve_using any files which |
847 | are required to load the module on the current platform) |
848 | |
849 | =item * |
850 | |
ff7f3c60 |
851 | calls dl_load_flags() to determine how to load the file. |
852 | |
853 | =item * |
854 | |
3b35bae3 |
855 | calls dl_load_file() to load the file |
856 | |
857 | =item * |
858 | |
859 | calls dl_undef_symbols() and warns if any symbols are undefined |
860 | |
861 | =item * |
862 | |
863 | calls dl_find_symbol() for "boot_$module" |
864 | |
865 | =item * |
866 | |
867 | calls dl_install_xsub() to install it as "${module}::bootstrap" |
868 | |
869 | =item * |
870 | |
8e07c86e |
871 | calls &{"${module}::bootstrap"} to bootstrap the module (actually |
872 | it uses the function reference returned by dl_install_xsub for speed) |
3b35bae3 |
873 | |
874 | =back |
875 | |
876 | =back |
877 | |
878 | |
879 | =head1 AUTHOR |
880 | |
c2960299 |
881 | Tim Bunce, 11 August 1994. |
882 | |
3b35bae3 |
883 | This interface is based on the work and comments of (in no particular |
884 | order): Larry Wall, Robert Sanders, Dean Roehrich, Jeff Okamoto, Anno |
c2960299 |
885 | Siegel, Thomas Neumann, Paul Marquess, Charles Bailey, myself and others. |
3b35bae3 |
886 | |
887 | Larry Wall designed the elegant inherited bootstrap mechanism and |
888 | implemented the first Perl 5 dynamic loader using it. |
889 | |
ff7f3c60 |
890 | Solaris global loading added by Nick Ing-Simmons with design/coding |
891 | assistance from Tim Bunce, January 1996. |
892 | |
3b35bae3 |
893 | =cut |
03c9e98c |
894 | EOT |
895 | |
896 | close OUT or die $!; |
897 | |