File Coverage

blib/lib/Dpkg/Shlibs.pm
Criterion Covered Total %
statement 84 103 81.5
branch 20 36 55.5
condition 3 8 37.5
subroutine 19 20 95.0
pod 0 6 0.0
total 126 173 72.8


line stmt bran cond sub pod time code
1             # Copyright © 2007, 2016 Raphaël Hertzog
2             # Copyright © 2007-2008, 2012-2015 Guillem Jover
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 .
16              
17             package Dpkg::Shlibs;
18              
19 2     2   3514 use strict;
  2         4  
  2         62  
20 2     2   10 use warnings;
  2         4  
  2         84  
21 2     2   12 use feature qw(state);
  2         4  
  2         254  
22              
23             our $VERSION = '0.03';
24             our @EXPORT_OK = qw(
25             blank_library_paths
26             setup_library_paths
27             get_library_paths
28             add_library_dir
29             find_library
30             );
31              
32 2     2   16 use Exporter qw(import);
  2         2  
  2         96  
33 2     2   12 use List::Util qw(none);
  2         4  
  2         142  
34 2     2   24 use File::Spec;
  2         4  
  2         44  
35              
36 2     2   10 use Dpkg::Gettext;
  2         4  
  2         160  
37 2     2   16 use Dpkg::ErrorHandling;
  2         4  
  2         168  
38 2     2   1448 use Dpkg::Shlibs::Objdump;
  2         6  
  2         76  
39 2     2   14 use Dpkg::Path qw(resolve_symlink canonpath);
  2         4  
  2         110  
40 2     2   14 use Dpkg::Arch qw(get_build_arch get_host_arch :mappers);
  2         8  
  2         398  
41              
42 2         166 use constant DEFAULT_LIBRARY_PATH =>
43 2     2   16 qw(/lib /usr/lib);
  2         4  
44             # XXX: Deprecated multilib paths.
45 2         2534 use constant DEFAULT_MULTILIB_PATH =>
46 2     2   14 qw(/lib32 /usr/lib32 /lib64 /usr/lib64);
  2         4  
47              
48             # Library paths set by the user.
49             my @custom_librarypaths;
50             # Library paths from the system.
51             my @system_librarypaths;
52             my $librarypaths_init;
53              
54             sub parse_ldso_conf {
55 18     18 0 138 my $file = shift;
56 18         26 state %visited;
57 18         26 local $_;
58              
59 18 50       608 open my $fh, '<', $file or syserr(g_('cannot open %s'), $file);
60 18         84 $visited{$file}++;
61 18         328 while (<$fh>) {
62 42 100       204 next if /^\s*$/;
63 34         54 chomp;
64 34         82 s{/+$}{};
65 34 100       190 if (/^include\s+(\S.*\S)\s*$/) {
    100          
66 10         898 foreach my $include (glob($1)) {
67             parse_ldso_conf($include) if -e $include
68 16 100 66     318 && !$visited{$include};
69             }
70             } elsif (m{^\s*/}) {
71 16         128 s/^\s+//;
72 16         40 my $libdir = $_;
73 16 50   48   106 if (none { $_ eq $libdir } (@custom_librarypaths, @system_librarypaths)) {
  48         76  
74 16         144 push @system_librarypaths, $libdir;
75             }
76             }
77             }
78 18         224 close $fh;
79             }
80              
81             sub blank_library_paths {
82 2     2 0 1400 @custom_librarypaths = ();
83 2         6 @system_librarypaths = ();
84 2         6 $librarypaths_init = 1;
85             }
86              
87             sub setup_library_paths {
88 2     2 0 4 @custom_librarypaths = ();
89 2         2 @system_librarypaths = ();
90              
91             # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
92 2 50       12 if ($ENV{LD_LIBRARY_PATH}) {
93 2         12 require Cwd;
94 2         26 my $cwd = Cwd::getcwd;
95              
96 2         14 foreach my $path (split /:/, $ENV{LD_LIBRARY_PATH}) {
97 2         8 $path =~ s{/+$}{};
98              
99 2         132 my $realpath = Cwd::realpath($path);
100 2 50       8 next unless defined $realpath;
101 2 50       36 if ($realpath =~ m/^\Q$cwd\E/) {
102 0         0 warning(g_('deprecated use of LD_LIBRARY_PATH with private ' .
103             'library directory which interferes with ' .
104             'cross-building, please use -l option instead'));
105             }
106              
107             # XXX: This should be added to @custom_librarypaths, but as this
108             # is deprecated we do not care as the code will go away.
109 2         10 push @system_librarypaths, $path;
110             }
111             }
112              
113             # Adjust set of directories to consider when we're in a situation of a
114             # cross-build or a build of a cross-compiler.
115 2         4 my $multiarch;
116              
117             # Detect cross compiler builds.
118 2 50 33     8 if ($ENV{DEB_TARGET_GNU_TYPE} and
119             ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
120             {
121 0         0 $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
122             }
123             # Host for normal cross builds.
124 2 50       8 if (get_build_arch() ne get_host_arch()) {
125 0         0 $multiarch = debarch_to_multiarch(get_host_arch());
126             }
127             # Define list of directories containing crossbuilt libraries.
128 2 50       8 if ($multiarch) {
129 0         0 push @system_librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
130             }
131              
132 2         8 push @system_librarypaths, DEFAULT_LIBRARY_PATH;
133              
134             # Update library paths with ld.so config.
135 2 50       124 parse_ldso_conf('/etc/ld.so.conf') if -e '/etc/ld.so.conf';
136              
137 2         28 push @system_librarypaths, DEFAULT_MULTILIB_PATH;
138              
139 2         8 $librarypaths_init = 1;
140             }
141              
142             sub add_library_dir {
143 4     4 0 2480 my $dir = shift;
144              
145 4 100       16 setup_library_paths() if not $librarypaths_init;
146              
147 4         24 push @custom_librarypaths, $dir;
148             }
149              
150             sub get_library_paths {
151 6 50   6 0 88 setup_library_paths() if not $librarypaths_init;
152              
153 6         44 return (@custom_librarypaths, @system_librarypaths);
154             }
155              
156             # find_library ($soname, \@rpath, $format, $root)
157             sub find_library {
158 0     0 0   my ($lib, $rpath, $format, $root) = @_;
159              
160 0 0         setup_library_paths() if not $librarypaths_init;
161              
162 0           my @librarypaths = (@{$rpath}, @custom_librarypaths, @system_librarypaths);
  0            
163 0           my @libs;
164              
165 0   0       $root //= '';
166 0           $root =~ s{/+$}{};
167 0           foreach my $dir (@librarypaths) {
168 0           my $checkdir = "$root$dir";
169 0 0         if (-e "$checkdir/$lib") {
170 0           my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
171 0 0         if ($format eq $libformat) {
172 0           push @libs, canonpath("$checkdir/$lib");
173             } else {
174 0           debug(1, "Skipping lib $checkdir/$lib, libabi=0x%s != objabi=0x%s",
175             unpack('H*', $libformat), unpack('H*', $format));
176             }
177             }
178             }
179 0           return @libs;
180             }
181              
182             1;