Shlibs.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /emul/ia32-linux/lib /emul/ia32-linux/usr/lib);
  42. my @librarypaths;
  43. my $librarypaths_init;
  44. my %visited;
  45. sub parse_ldso_conf {
  46. my $file = shift;
  47. local $_;
  48. open my $fh, '<', $file or syserr(g_('cannot open %s'), $file);
  49. $visited{$file}++;
  50. while (<$fh>) {
  51. next if /^\s*$/;
  52. chomp;
  53. s{/+$}{};
  54. if (/^include\s+(\S.*\S)\s*$/) {
  55. foreach my $include (glob($1)) {
  56. parse_ldso_conf($include) if -e $include
  57. && !$visited{$include};
  58. }
  59. } elsif (m{^\s*/}) {
  60. s/^\s+//;
  61. my $libdir = $_;
  62. if (none { $_ eq $libdir } @librarypaths) {
  63. push @librarypaths, $libdir;
  64. }
  65. }
  66. }
  67. close $fh;
  68. }
  69. sub blank_library_paths {
  70. @librarypaths = ();
  71. $librarypaths_init = 1;
  72. }
  73. sub setup_library_paths {
  74. @librarypaths = DEFAULT_LIBRARY_PATH;
  75. # Update library paths with ld.so config.
  76. parse_ldso_conf('/etc/ld.so.conf') if -e '/etc/ld.so.conf';
  77. push @librarypaths, DEFAULT_MULTILIB_PATH;
  78. # Adjust set of directories to consider when we're in a situation of a
  79. # cross-build or a build of a cross-compiler.
  80. my ($crossprefix, $multiarch);
  81. # Detect cross compiler builds.
  82. if ($ENV{DEB_TARGET_GNU_TYPE} and
  83. ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
  84. {
  85. $crossprefix = $ENV{DEB_TARGET_GNU_TYPE};
  86. $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
  87. }
  88. # Host for normal cross builds.
  89. if (get_build_arch() ne get_host_arch()) {
  90. $crossprefix = debarch_to_gnutriplet(get_host_arch());
  91. $multiarch = debarch_to_multiarch(get_host_arch());
  92. }
  93. # Define list of directories containing crossbuilt libraries.
  94. if ($multiarch) {
  95. push @librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
  96. }
  97. # XXX: Add deprecated sysroot and toolchain cross-compilation paths.
  98. if ($crossprefix) {
  99. push @librarypaths,
  100. "/$crossprefix/lib", "/usr/$crossprefix/lib",
  101. "/$crossprefix/lib32", "/usr/$crossprefix/lib32",
  102. "/$crossprefix/lib64", "/usr/$crossprefix/lib64";
  103. }
  104. # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
  105. if ($ENV{LD_LIBRARY_PATH}) {
  106. foreach my $path (reverse split( /:/, $ENV{LD_LIBRARY_PATH})) {
  107. $path =~ s{/+$}{};
  108. add_library_dir($path);
  109. }
  110. }
  111. $librarypaths_init = 1;
  112. }
  113. sub add_library_dir {
  114. my $dir = shift;
  115. unshift @librarypaths, $dir;
  116. }
  117. sub get_library_paths {
  118. setup_library_paths() if not $librarypaths_init;
  119. return @librarypaths;
  120. }
  121. # find_library ($soname, \@rpath, $format, $root)
  122. sub find_library {
  123. my ($lib, $rpath, $format, $root) = @_;
  124. setup_library_paths() if not $librarypaths_init;
  125. $root //= '';
  126. $root =~ s{/+$}{};
  127. my @rpath = @{$rpath};
  128. foreach my $dir (@rpath, @librarypaths) {
  129. my $checkdir = "$root$dir";
  130. # If the directory checked is a symlink, check if it doesn't
  131. # resolve to another public directory (which is then the canonical
  132. # directory to use instead of this one). Typical example
  133. # is /usr/lib64 -> /usr/lib on amd64.
  134. if (-l $checkdir) {
  135. my $newdir = resolve_symlink($checkdir);
  136. if (any { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  137. $checkdir = $newdir;
  138. }
  139. }
  140. if (-e "$checkdir/$lib") {
  141. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  142. if ($format eq $libformat) {
  143. return canonpath("$checkdir/$lib");
  144. }
  145. }
  146. }
  147. return;
  148. }
  149. 1;