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