Shlibs.pm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package Dpkg::Shlibs;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.02';
  19. our @EXPORT_OK = qw(
  20. blank_library_paths
  21. setup_library_paths
  22. get_library_paths
  23. add_library_dir
  24. find_library
  25. );
  26. use Exporter qw(import);
  27. use File::Spec;
  28. use Dpkg::Gettext;
  29. use Dpkg::ErrorHandling;
  30. use Dpkg::Shlibs::Objdump;
  31. use Dpkg::Util qw(:list);
  32. use Dpkg::Path qw(resolve_symlink canonpath);
  33. use Dpkg::Arch qw(debarch_to_gnutriplet get_build_arch get_host_arch
  34. gnutriplet_to_multiarch debarch_to_multiarch);
  35. use constant DEFAULT_LIBRARY_PATH =>
  36. qw(/lib /usr/lib);
  37. # XXX: Deprecated multilib paths.
  38. use constant DEFAULT_MULTILIB_PATH =>
  39. qw(/lib32 /usr/lib32 /lib64 /usr/lib64
  40. /emul/ia32-linux/lib /emul/ia32-linux/usr/lib);
  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 ($crossprefix, $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. $crossprefix = $ENV{DEB_TARGET_GNU_TYPE};
  85. $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
  86. }
  87. # Host for normal cross builds.
  88. if (get_build_arch() ne get_host_arch()) {
  89. $crossprefix = debarch_to_gnutriplet(get_host_arch());
  90. $multiarch = debarch_to_multiarch(get_host_arch());
  91. }
  92. # Define list of directories containing crossbuilt libraries.
  93. if ($multiarch) {
  94. push @librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
  95. }
  96. # XXX: Add deprecated sysroot and toolchain cross-compilation paths.
  97. if ($crossprefix) {
  98. push @librarypaths,
  99. "/$crossprefix/lib", "/usr/$crossprefix/lib",
  100. "/$crossprefix/lib32", "/usr/$crossprefix/lib32",
  101. "/$crossprefix/lib64", "/usr/$crossprefix/lib64";
  102. }
  103. # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
  104. if ($ENV{LD_LIBRARY_PATH}) {
  105. foreach my $path (reverse split( /:/, $ENV{LD_LIBRARY_PATH})) {
  106. $path =~ s{/+$}{};
  107. add_library_dir($path);
  108. }
  109. }
  110. $librarypaths_init = 1;
  111. }
  112. sub add_library_dir {
  113. my $dir = shift;
  114. unshift @librarypaths, $dir;
  115. }
  116. sub get_library_paths {
  117. setup_library_paths() if not $librarypaths_init;
  118. return @librarypaths;
  119. }
  120. # find_library ($soname, \@rpath, $format, $root)
  121. sub find_library {
  122. my ($lib, $rpath, $format, $root) = @_;
  123. setup_library_paths() if not $librarypaths_init;
  124. $root //= '';
  125. $root =~ s{/+$}{};
  126. my @rpath = @{$rpath};
  127. foreach my $dir (@rpath, @librarypaths) {
  128. my $checkdir = "$root$dir";
  129. # If the directory checked is a symlink, check if it doesn't
  130. # resolve to another public directory (which is then the canonical
  131. # directory to use instead of this one). Typical example
  132. # is /usr/lib64 -> /usr/lib on amd64.
  133. if (-l $checkdir) {
  134. my $newdir = resolve_symlink($checkdir);
  135. if (any { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  136. $checkdir = $newdir;
  137. }
  138. }
  139. if (-e "$checkdir/$lib") {
  140. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  141. if ($format eq $libformat) {
  142. return canonpath("$checkdir/$lib");
  143. }
  144. }
  145. }
  146. return;
  147. }
  148. 1;