Shlibs.pm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2007 Raphael Hertzog
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Shlibs;
  14. use strict;
  15. use warnings;
  16. use base qw(Exporter);
  17. our @EXPORT_OK = qw(@librarypaths find_library);
  18. use File::Spec;
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling qw(syserr);
  21. use Dpkg::Shlibs::Objdump;
  22. use Dpkg::Path qw(resolve_symlink canonpath);
  23. use constant DEFAULT_LIBRARY_PATH =>
  24. qw(/lib /usr/lib /lib32 /usr/lib32 /lib64 /usr/lib64
  25. /emul/ia32-linux/lib /emul/ia32-linux/usr/lib);
  26. our @librarypaths = (DEFAULT_LIBRARY_PATH);
  27. # Update library paths with LD_LIBRARY_PATH
  28. if ($ENV{LD_LIBRARY_PATH}) {
  29. foreach my $path (reverse split( /:/, $ENV{LD_LIBRARY_PATH} )) {
  30. $path =~ s{/+$}{};
  31. unshift @librarypaths, $path;
  32. }
  33. }
  34. # Update library paths with ld.so config
  35. parse_ldso_conf("/etc/ld.so.conf") if -e "/etc/ld.so.conf";
  36. my %visited;
  37. sub parse_ldso_conf {
  38. my $file = shift;
  39. open my $fh, "<", $file or syserr(_g("cannot open %s"), $file);
  40. $visited{$file}++;
  41. while (<$fh>) {
  42. next if /^\s*$/;
  43. chomp;
  44. s{/+$}{};
  45. if (/^include\s+(\S.*\S)\s*$/) {
  46. foreach my $include (glob($1)) {
  47. parse_ldso_conf($include) if -e $include
  48. && !$visited{$include};
  49. }
  50. } elsif (m{^\s*/}) {
  51. s/^\s+//;
  52. my $libdir = $_;
  53. unless (scalar grep { $_ eq $libdir } @librarypaths) {
  54. push @librarypaths, $libdir;
  55. }
  56. }
  57. }
  58. close $fh;
  59. }
  60. # find_library ($soname, \@rpath, $format, $root)
  61. sub find_library {
  62. my ($lib, $rpath, $format, $root) = @_;
  63. $root = "" if not defined($root);
  64. $root =~ s{/+$}{};
  65. my @rpath = @{$rpath};
  66. foreach my $dir (@rpath, @librarypaths) {
  67. my $checkdir = "$root$dir";
  68. # If the directory checked is a symlink, check if it doesn't
  69. # resolve to another public directory (which is then the canonical
  70. # directory to use instead of this one). Typical example
  71. # is /usr/lib64 -> /usr/lib on amd64.
  72. if (-l $checkdir) {
  73. my $newdir = resolve_symlink($checkdir);
  74. if (grep { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  75. $checkdir = $newdir;
  76. }
  77. }
  78. if (-e "$checkdir/$lib") {
  79. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  80. if ($format eq $libformat) {
  81. return canonpath("$checkdir/$lib");
  82. }
  83. }
  84. }
  85. return undef;
  86. }
  87. 1;