Shlibs.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2007-2008, 2012-2015 Guillem Jover <guillem@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package Dpkg::Shlibs;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '0.02';
  20. our @EXPORT_OK = qw(
  21. blank_library_paths
  22. setup_library_paths
  23. get_library_paths
  24. add_library_dir
  25. find_library
  26. );
  27. use Exporter qw(import);
  28. use File::Spec;
  29. use Dpkg::Gettext;
  30. use Dpkg::ErrorHandling;
  31. use Dpkg::Shlibs::Objdump;
  32. use Dpkg::Util qw(:list);
  33. use Dpkg::Path qw(resolve_symlink canonpath);
  34. use Dpkg::Arch qw(debarch_to_gnutriplet get_build_arch get_host_arch
  35. gnutriplet_to_multiarch debarch_to_multiarch);
  36. use constant DEFAULT_LIBRARY_PATH =>
  37. qw(/lib /usr/lib);
  38. # XXX: Deprecated multilib paths.
  39. use constant DEFAULT_MULTILIB_PATH =>
  40. qw(/lib32 /usr/lib32 /lib64 /usr/lib64);
  41. my @librarypaths;
  42. my $librarypaths_init;
  43. my %visited;
  44. sub parse_ldso_conf {
  45. my $file = shift;
  46. local $_;
  47. open my $fh, '<', $file or syserr(g_('cannot open %s'), $file);
  48. $visited{$file}++;
  49. while (<$fh>) {
  50. next if /^\s*$/;
  51. chomp;
  52. s{/+$}{};
  53. if (/^include\s+(\S.*\S)\s*$/) {
  54. foreach my $include (glob($1)) {
  55. parse_ldso_conf($include) if -e $include
  56. && !$visited{$include};
  57. }
  58. } elsif (m{^\s*/}) {
  59. s/^\s+//;
  60. my $libdir = $_;
  61. if (none { $_ eq $libdir } @librarypaths) {
  62. push @librarypaths, $libdir;
  63. }
  64. }
  65. }
  66. close $fh;
  67. }
  68. sub blank_library_paths {
  69. @librarypaths = ();
  70. $librarypaths_init = 1;
  71. }
  72. sub setup_library_paths {
  73. @librarypaths = DEFAULT_LIBRARY_PATH;
  74. # Update library paths with ld.so config.
  75. parse_ldso_conf('/etc/ld.so.conf') if -e '/etc/ld.so.conf';
  76. push @librarypaths, DEFAULT_MULTILIB_PATH;
  77. # Adjust set of directories to consider when we're in a situation of a
  78. # cross-build or a build of a cross-compiler.
  79. my $multiarch;
  80. # Detect cross compiler builds.
  81. if ($ENV{DEB_TARGET_GNU_TYPE} and
  82. ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
  83. {
  84. $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
  85. }
  86. # Host for normal cross builds.
  87. if (get_build_arch() ne get_host_arch()) {
  88. $multiarch = debarch_to_multiarch(get_host_arch());
  89. }
  90. # Define list of directories containing crossbuilt libraries.
  91. if ($multiarch) {
  92. push @librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
  93. }
  94. # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
  95. if ($ENV{LD_LIBRARY_PATH}) {
  96. foreach my $path (reverse split( /:/, $ENV{LD_LIBRARY_PATH})) {
  97. $path =~ s{/+$}{};
  98. add_library_dir($path);
  99. }
  100. }
  101. $librarypaths_init = 1;
  102. }
  103. sub add_library_dir {
  104. my $dir = shift;
  105. setup_library_paths() if not $librarypaths_init;
  106. unshift @librarypaths, $dir;
  107. }
  108. sub get_library_paths {
  109. setup_library_paths() if not $librarypaths_init;
  110. return @librarypaths;
  111. }
  112. # find_library ($soname, \@rpath, $format, $root)
  113. sub find_library {
  114. my ($lib, $rpath, $format, $root) = @_;
  115. setup_library_paths() if not $librarypaths_init;
  116. $root //= '';
  117. $root =~ s{/+$}{};
  118. my @rpath = @{$rpath};
  119. foreach my $dir (@rpath, @librarypaths) {
  120. my $checkdir = "$root$dir";
  121. # If the directory checked is a symlink, check if it doesn't
  122. # resolve to another public directory (which is then the canonical
  123. # directory to use instead of this one). Typical example
  124. # is /usr/lib64 -> /usr/lib on amd64.
  125. if (-l $checkdir) {
  126. my $newdir = resolve_symlink($checkdir);
  127. if (any { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  128. $checkdir = $newdir;
  129. }
  130. }
  131. if (-e "$checkdir/$lib") {
  132. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  133. if ($format eq $libformat) {
  134. return canonpath("$checkdir/$lib");
  135. }
  136. }
  137. }
  138. return;
  139. }
  140. 1;