DynaLoader.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # Generated from DynaLoader_pm.PL, this file is unique for every OS
  2. package DynaLoader;
  3. # And Gandalf said: 'Many folk like to know beforehand what is to
  4. # be set on the table; but those who have laboured to prepare the
  5. # feast like to keep their secret; for wonder makes the words of
  6. # praise louder.'
  7. # (Quote from Tolkien suggested by Anno Siegel.)
  8. #
  9. # See pod text at end of file for documentation.
  10. # See also ext/DynaLoader/README in source tree for other information.
  11. #
  12. # Tim.Bunce@ig.co.uk, August 1994
  13. BEGIN {
  14. $VERSION = '1.45';
  15. }
  16. use Config;
  17. # enable debug/trace messages from DynaLoader perl code
  18. $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
  19. #
  20. # Flags to alter dl_load_file behaviour. Assigned bits:
  21. # 0x01 make symbols available for linking later dl_load_file's.
  22. # (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL))
  23. # (ignored under VMS; effect is built-in to image linking)
  24. # (ignored under Android; the linker always uses RTLD_LOCAL)
  25. #
  26. # This is called as a class method $module->dl_load_flags. The
  27. # definition here will be inherited and result on "default" loading
  28. # behaviour unless a sub-class of DynaLoader defines its own version.
  29. #
  30. sub dl_load_flags { 0x00 }
  31. ($dl_dlext, $dl_so, $dlsrc) = @Config::Config{qw(dlext so dlsrc)};
  32. $do_expand = 0;
  33. @dl_require_symbols = (); # names of symbols we need
  34. @dl_library_path = (); # path to look for files
  35. #XSLoader.pm may have added elements before we were required
  36. #@dl_shared_objects = (); # shared objects for symbols we have
  37. #@dl_librefs = (); # things we have loaded
  38. #@dl_modules = (); # Modules we have loaded
  39. # Initialise @dl_library_path with the 'standard' library path
  40. # for this platform as determined by Configure.
  41. push(@dl_library_path, split(' ', $Config::Config{libpth}));
  42. my $ldlibpthname = $Config::Config{ldlibpthname};
  43. my $ldlibpthname_defined = defined $Config::Config{ldlibpthname};
  44. my $pthsep = $Config::Config{path_sep};
  45. # Add to @dl_library_path any extra directories we can gather from environment
  46. # during runtime.
  47. if ($ldlibpthname_defined &&
  48. exists $ENV{$ldlibpthname}) {
  49. push(@dl_library_path, split(/$pthsep/, $ENV{$ldlibpthname}));
  50. }
  51. # E.g. HP-UX supports both its native SHLIB_PATH *and* LD_LIBRARY_PATH.
  52. if ($ldlibpthname_defined &&
  53. $ldlibpthname ne 'LD_LIBRARY_PATH' &&
  54. exists $ENV{LD_LIBRARY_PATH}) {
  55. push(@dl_library_path, split(/$pthsep/, $ENV{LD_LIBRARY_PATH}));
  56. }
  57. # No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
  58. # NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB
  59. boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
  60. !defined(&dl_error);
  61. if ($dl_debug) {
  62. print STDERR "DynaLoader.pm loaded (@INC, @dl_library_path)\n";
  63. print STDERR "DynaLoader not linked into this perl\n"
  64. unless defined(&boot_DynaLoader);
  65. }
  66. 1; # End of main code
  67. sub croak { require Carp; Carp::croak(@_) }
  68. sub bootstrap_inherit {
  69. my $module = $_[0];
  70. local *isa = *{"$module\::ISA"};
  71. local @isa = (@isa, 'DynaLoader');
  72. # Cannot goto due to delocalization. Will report errors on a wrong line?
  73. bootstrap(@_);
  74. }
  75. sub bootstrap {
  76. # use local vars to enable $module.bs script to edit values
  77. local(@args) = @_;
  78. local($module) = $args[0];
  79. local(@dirs, $file);
  80. unless ($module) {
  81. require Carp;
  82. Carp::confess("Usage: DynaLoader::bootstrap(module)");
  83. }
  84. # A common error on platforms which don't support dynamic loading.
  85. # Since it's fatal and potentially confusing we give a detailed message.
  86. croak("Can't load module $module, dynamic loading not available in this perl.\n".
  87. " (You may need to build a new perl executable which either supports\n".
  88. " dynamic loading or has the $module module statically linked into it.)\n")
  89. unless defined(&dl_load_file);
  90. my @modparts = split(/::/,$module);
  91. my $modfname = $modparts[-1];
  92. my $modfname_orig = $modfname; # For .bs file search
  93. # Some systems have restrictions on files names for DLL's etc.
  94. # mod2fname returns appropriate file base name (typically truncated)
  95. # It may also edit @modparts if required.
  96. $modfname = &mod2fname(\@modparts) if defined &mod2fname;
  97. my $modpname = join('/',@modparts);
  98. print STDERR "DynaLoader::bootstrap for $module ",
  99. "(auto/$modpname/$modfname.$dl_dlext)\n"
  100. if $dl_debug;
  101. my $dir;
  102. foreach (@INC) {
  103. $dir = "$_/auto/$modpname";
  104. next unless -d $dir; # skip over uninteresting directories
  105. # check for common cases to avoid autoload of dl_findfile
  106. my $try = "$dir/$modfname.$dl_dlext";
  107. last if $file = ($do_expand) ? dl_expandspec($try) : ((-f $try) && $try);
  108. # no luck here, save dir for possible later dl_findfile search
  109. push @dirs, $dir;
  110. }
  111. # last resort, let dl_findfile have a go in all known locations
  112. $file = dl_findfile(map("-L$_",@dirs,@INC), $modfname) unless $file;
  113. croak("Can't locate loadable object for module $module in \@INC (\@INC contains: @INC)")
  114. unless $file; # wording similar to error from 'require'
  115. my $bootname = "boot_$module";
  116. $bootname =~ s/\W/_/g;
  117. @dl_require_symbols = ($bootname);
  118. # Execute optional '.bootstrap' perl script for this module.
  119. # The .bs file can be used to configure @dl_resolve_using etc to
  120. # match the needs of the individual module on this architecture.
  121. # N.B. The .bs file does not following the naming convention used
  122. # by mod2fname.
  123. my $bs = "$dir/$modfname_orig";
  124. $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
  125. if (-s $bs) { # only read file if it's not empty
  126. print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
  127. eval { local @INC = ('.'); do $bs; };
  128. warn "$bs: $@\n" if $@;
  129. }
  130. my $boot_symbol_ref;
  131. # Many dynamic extension loading problems will appear to come from
  132. # this section of code: XYZ failed at line 123 of DynaLoader.pm.
  133. # Often these errors are actually occurring in the initialisation
  134. # C code of the extension XS file. Perl reports the error as being
  135. # in this perl code simply because this was the last perl code
  136. # it executed.
  137. my $flags = $module->dl_load_flags;
  138. my $libref = dl_load_file($file, $flags) or
  139. croak("Can't load '$file' for module $module: ".dl_error());
  140. push(@dl_librefs,$libref); # record loaded object
  141. $boot_symbol_ref = dl_find_symbol($libref, $bootname) or
  142. croak("Can't find '$bootname' symbol in $file\n");
  143. push(@dl_modules, $module); # record loaded module
  144. boot:
  145. my $xs = dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file);
  146. # See comment block above
  147. push(@dl_shared_objects, $file); # record files loaded
  148. &$xs(@args);
  149. }
  150. sub dl_findfile {
  151. # This function does not automatically consider the architecture
  152. # or the perl library auto directories.
  153. my (@args) = @_;
  154. my (@dirs, $dir); # which directories to search
  155. my (@found); # full paths to real files we have found
  156. #my $dl_ext= 'so'; # $Config::Config{'dlext'} suffix for perl extensions
  157. #my $dl_so = 'so'; # $Config::Config{'so'} suffix for shared libraries
  158. print STDERR "dl_findfile(@args)\n" if $dl_debug;
  159. # accumulate directories but process files as they appear
  160. arg: foreach(@args) {
  161. # Special fast case: full filepath requires no search
  162. if (m:/: && -f $_) {
  163. push(@found,$_);
  164. last arg unless wantarray;
  165. next;
  166. }
  167. # Deal with directories first:
  168. # Using a -L prefix is the preferred option (faster and more robust)
  169. if ( s{^-L}{} ) { push(@dirs, $_); next; }
  170. # Otherwise we try to try to spot directories by a heuristic
  171. # (this is a more complicated issue than it first appears)
  172. if (m:/: && -d $_) { push(@dirs, $_); next; }
  173. # Only files should get this far...
  174. my(@names, $name); # what filenames to look for
  175. if ( s{^-l}{} ) { # convert -lname to appropriate library name
  176. push(@names, "lib$_.$dl_so", "lib$_.a");
  177. } else { # Umm, a bare name. Try various alternatives:
  178. # these should be ordered with the most likely first
  179. push(@names,"$_.$dl_dlext") unless m/\.$dl_dlext$/o;
  180. push(@names,"$_.$dl_so") unless m/\.$dl_so$/o;
  181. push(@names,"lib$_.$dl_so") unless m:/:;
  182. push(@names, $_);
  183. }
  184. my $dirsep = '/';
  185. foreach $dir (@dirs, @dl_library_path) {
  186. next unless -d $dir;
  187. foreach $name (@names) {
  188. my($file) = "$dir$dirsep$name";
  189. print STDERR " checking in $dir for $name\n" if $dl_debug;
  190. $file = ($do_expand) ? dl_expandspec($file) : (-f $file && $file);
  191. #$file = _check_file($file);
  192. if ($file) {
  193. push(@found, $file);
  194. next arg; # no need to look any further
  195. }
  196. }
  197. }
  198. }
  199. if ($dl_debug) {
  200. foreach(@dirs) {
  201. print STDERR " dl_findfile ignored non-existent directory: $_\n" unless -d $_;
  202. }
  203. print STDERR "dl_findfile found: @found\n";
  204. }
  205. return $found[0] unless wantarray;
  206. @found;
  207. }
  208. sub dl_expandspec {
  209. my($spec) = @_;
  210. # Optional function invoked if DynaLoader.pm sets $do_expand.
  211. # Most systems do not require or use this function.
  212. # Some systems may implement it in the dl_*.xs file in which case
  213. # this Perl version should be excluded at build time.
  214. # This function is designed to deal with systems which treat some
  215. # 'filenames' in a special way. For example VMS 'Logical Names'
  216. # (something like unix environment variables - but different).
  217. # This function should recognise such names and expand them into
  218. # full file paths.
  219. # Must return undef if $spec is invalid or file does not exist.
  220. my $file = $spec; # default output to input
  221. return undef unless -f $file;
  222. print STDERR "dl_expandspec($spec) => $file\n" if $dl_debug;
  223. $file;
  224. }
  225. sub dl_find_symbol_anywhere
  226. {
  227. my $sym = shift;
  228. my $libref;
  229. foreach $libref (@dl_librefs) {
  230. my $symref = dl_find_symbol($libref,$sym,1);
  231. return $symref if $symref;
  232. }
  233. return undef;
  234. }
  235. __END__