Shlibs.pm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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
  40. or syserr(sprintf(_g("couldn't open %s"), $file));
  41. $visited{$file}++;
  42. while (<$fh>) {
  43. next if /^\s*$/;
  44. chomp;
  45. s{/+$}{};
  46. if (/^include\s+(\S.*\S)\s*$/) {
  47. foreach my $include (glob($1)) {
  48. parse_ldso_conf($include) if -e $include
  49. && !$visited{$include};
  50. }
  51. } elsif (m{^\s*/}) {
  52. s/^\s+//;
  53. my $libdir = $_;
  54. unless (scalar grep { $_ eq $libdir } @librarypaths) {
  55. push @librarypaths, $libdir;
  56. }
  57. }
  58. }
  59. close $fh or syserr(sprintf(_g("couldn't close %s"), $file));;
  60. }
  61. # find_library ($soname, \@rpath, $format, $root)
  62. sub find_library {
  63. my ($lib, $rpath, $format, $root) = @_;
  64. $root = "" if not defined($root);
  65. $root =~ s{/+$}{};
  66. my @rpath = @{$rpath};
  67. foreach my $dir (@rpath, @librarypaths) {
  68. my $checkdir = "$root$dir";
  69. # If the directory checked is a symlink, check if it doesn't
  70. # resolve to another public directory (which is then the canonical
  71. # directory to use instead of this one). Typical example
  72. # is /usr/lib64 -> /usr/lib on amd64.
  73. if (-l $checkdir) {
  74. my $newdir = resolve_symlink($checkdir);
  75. if (grep { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  76. $checkdir = $newdir;
  77. }
  78. }
  79. if (-e "$checkdir/$lib") {
  80. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  81. if ($format eq $libformat) {
  82. return canonpath("$checkdir/$lib");
  83. }
  84. }
  85. }
  86. return undef;
  87. }
  88. 1;