Shlibs.pm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright © 2007, 2016 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. use feature qw(state);
  20. our $VERSION = '0.03';
  21. our @EXPORT_OK = qw(
  22. blank_library_paths
  23. setup_library_paths
  24. get_library_paths
  25. add_library_dir
  26. find_library
  27. );
  28. use Exporter qw(import);
  29. use File::Spec;
  30. use Dpkg::Gettext;
  31. use Dpkg::ErrorHandling;
  32. use Dpkg::Shlibs::Objdump;
  33. use Dpkg::Util qw(:list);
  34. use Dpkg::Path qw(resolve_symlink canonpath);
  35. use Dpkg::Arch qw(get_build_arch get_host_arch :mappers);
  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. # Library paths set by the user.
  42. my @custom_librarypaths;
  43. # Library paths from the system.
  44. my @system_librarypaths;
  45. my $librarypaths_init;
  46. sub parse_ldso_conf {
  47. my $file = shift;
  48. state %visited;
  49. local $_;
  50. open my $fh, '<', $file or syserr(g_('cannot open %s'), $file);
  51. $visited{$file}++;
  52. while (<$fh>) {
  53. next if /^\s*$/;
  54. chomp;
  55. s{/+$}{};
  56. if (/^include\s+(\S.*\S)\s*$/) {
  57. foreach my $include (glob($1)) {
  58. parse_ldso_conf($include) if -e $include
  59. && !$visited{$include};
  60. }
  61. } elsif (m{^\s*/}) {
  62. s/^\s+//;
  63. my $libdir = $_;
  64. if (none { $_ eq $libdir } (@custom_librarypaths, @system_librarypaths)) {
  65. push @system_librarypaths, $libdir;
  66. }
  67. }
  68. }
  69. close $fh;
  70. }
  71. sub blank_library_paths {
  72. @custom_librarypaths = ();
  73. @system_librarypaths = ();
  74. $librarypaths_init = 1;
  75. }
  76. sub setup_library_paths {
  77. @custom_librarypaths = ();
  78. @system_librarypaths = ();
  79. # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
  80. if ($ENV{LD_LIBRARY_PATH}) {
  81. foreach my $path (split /:/, $ENV{LD_LIBRARY_PATH}) {
  82. $path =~ s{/+$}{};
  83. # XXX: This should be added to @custom_librarypaths, but as this
  84. # is deprecated we do not care as the code will go away.
  85. push @system_librarypaths, $path;
  86. }
  87. }
  88. # Adjust set of directories to consider when we're in a situation of a
  89. # cross-build or a build of a cross-compiler.
  90. my $multiarch;
  91. # Detect cross compiler builds.
  92. if ($ENV{DEB_TARGET_GNU_TYPE} and
  93. ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
  94. {
  95. $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
  96. }
  97. # Host for normal cross builds.
  98. if (get_build_arch() ne get_host_arch()) {
  99. $multiarch = debarch_to_multiarch(get_host_arch());
  100. }
  101. # Define list of directories containing crossbuilt libraries.
  102. if ($multiarch) {
  103. push @system_librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
  104. }
  105. push @system_librarypaths, DEFAULT_LIBRARY_PATH;
  106. # Update library paths with ld.so config.
  107. parse_ldso_conf('/etc/ld.so.conf') if -e '/etc/ld.so.conf';
  108. push @system_librarypaths, DEFAULT_MULTILIB_PATH;
  109. $librarypaths_init = 1;
  110. }
  111. sub add_library_dir {
  112. my $dir = shift;
  113. setup_library_paths() if not $librarypaths_init;
  114. push @custom_librarypaths, $dir;
  115. }
  116. sub get_library_paths {
  117. setup_library_paths() if not $librarypaths_init;
  118. return (@custom_librarypaths, @system_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. my @librarypaths = (@{$rpath}, @custom_librarypaths, @system_librarypaths);
  125. my @libs;
  126. $root //= '';
  127. $root =~ s{/+$}{};
  128. foreach my $dir (@librarypaths) {
  129. my $checkdir = "$root$dir";
  130. if (-e "$checkdir/$lib") {
  131. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  132. if ($format eq $libformat) {
  133. push @libs, canonpath("$checkdir/$lib");
  134. } else {
  135. debug(1, "Skipping lib $checkdir/$lib, libabi=0x%s != objabi=0x%s",
  136. unpack('H*', $libformat), unpack('H*', $format));
  137. }
  138. }
  139. }
  140. return @libs;
  141. }
  142. 1;