5 # Copyright © 1996 Ian Jackson
6 # Copyright © 2000 Wichert Akkerman
7 # Copyright © 2006 Frank Lichtenheld
8 # Copyright © 2006-2010,2012-2015 Guillem Jover <guillem@debian.org>
9 # Copyright © 2007, 2016 Raphaël Hertzog <hertzog@debian.org>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <https://www.gnu.org/licenses/>.
26 use feature
qw(state);
28 use List
::Util
qw(any none);
30 use File
::Basename
qw(dirname);
34 use Dpkg
::ErrorHandling
;
35 use Dpkg
::Path
qw(relative_to_pkg_root guess_pkg_root_dir
36 check_files_are_the_same get_control_path);
38 use Dpkg
::Shlibs
qw(find_library get_library_paths);
39 use Dpkg
::Shlibs
::Objdump
;
40 use Dpkg
::Shlibs
::SymbolFile
;
42 use Dpkg
::Arch
qw(get_host_arch);
44 use Dpkg
::Control
::Info
;
45 use Dpkg
::Control
::Fields
;
49 WARN_SYM_NOT_FOUND
=> 1,
50 WARN_DEP_AVOIDABLE
=> 2,
54 # By increasing importance
55 my @depfields = qw(Suggests Recommends Depends Pre-Depends);
56 my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
58 textdomain
('dpkg-dev');
60 my $admindir = $Dpkg::ADMINDIR
;
61 my $shlibsoverride = "$Dpkg::CONFDIR/shlibs.override";
62 my $shlibsdefault = "$Dpkg::CONFDIR/shlibs.default";
63 my $shlibslocal = 'debian/shlibs.local';
64 my $packagetype = 'deb';
65 my $dependencyfield = 'Depends';
66 my $varlistfile = 'debian/substvars';
68 my $varnameprefix = 'shlibs';
69 my $ignore_missing_info = 0;
70 my $warnings = WARN_SYM_NOT_FOUND
| WARN_DEP_AVOIDABLE
;
73 my @pkg_dir_to_search = ();
74 my @pkg_dir_to_ignore = ();
75 my $host_arch = get_host_arch
();
77 my (@pkg_shlibs, @pkg_symbols, @pkg_root_dirs);
83 } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
85 } elsif (m/^-L(.*)$/) {
87 } elsif (m/^-l(.*)$/) {
88 Dpkg
::Shlibs
::add_library_dir
($1);
89 } elsif (m/^-S(.*)$/) {
90 push @pkg_dir_to_search, $1;
91 } elsif (m/^-I(.*)$/) {
92 push @pkg_dir_to_ignore, $1;
95 } elsif (m/^-O(.+)$/) {
97 } elsif (m/^-(?:\?|-help)$/) {
99 } elsif (m/^--version$/) {
101 } elsif (m/^--admindir=(.*)$/) {
103 if (not -d
$admindir) {
104 error
(g_
("administrative directory '%s' does not exist"), $admindir);
106 $ENV{DPKG_ADMINDIR
} = $admindir;
107 } elsif (m/^-d(.*)$/) {
108 $dependencyfield = field_capitalize
($1);
109 if (not defined $depstrength{$dependencyfield}) {
110 warning
(g_
("unrecognized dependency field '%s'"), $dependencyfield);
112 } elsif (m/^-e(.*)$/) {
113 if (exists $exec{$1}) {
114 # Affect the binary to the most important field
115 if ($depstrength{$dependencyfield} > $depstrength{$exec{$1}}) {
116 $exec{$1} = $dependencyfield;
119 $exec{$1} = $dependencyfield;
121 } elsif (m/^--ignore-missing-info$/) {
122 $ignore_missing_info = 1;
123 } elsif (m/^--warnings=(\d+)$/) {
125 } elsif (m/^-t(.*)$/) {
129 } elsif (m/^-x(.*)$/) {
132 usageerr
(g_
("unknown option '%s'"), $_);
134 if (exists $exec{$_}) {
135 # Affect the binary to the most important field
136 if ($depstrength{$dependencyfield} > $depstrength{$exec{$_}}) {
137 $exec{$_} = $dependencyfield;
140 $exec{$_} = $dependencyfield;
144 usageerr
(g_
('need at least one executable')) unless scalar keys %exec;
146 report_options
(debug_level
=> $debug);
150 return any
{ $path =~ /^\Q$_\E/ } @pkg_dir_to_ignore;
154 push @pkg_symbols, grep { !ignore_pkgdir
($_) } glob 'debian/*/DEBIAN/symbols';
155 push @pkg_shlibs, grep { !ignore_pkgdir
($_) } glob 'debian/*/DEBIAN/shlibs';
156 my %uniq = map { guess_pkg_root_dir
($_) => 1 } (@pkg_symbols, @pkg_shlibs);
157 push @pkg_root_dirs, keys %uniq;
160 my $control = Dpkg
::Control
::Info
->new();
161 my $fields = $control->get_source();
162 my $bd_value = deps_concat
($fields->{'Build-Depends'}, $fields->{'Build-Depends-Arch'});
163 my $build_deps = deps_parse
($bd_value, build_dep
=> 1, reduce_restrictions
=> 1);
164 error
(g_
('cannot parse %s field'), 'Build-Depends/Build-Depends-Arch')
165 unless defined $build_deps;
169 # Statistics on soname seen in the whole run (with multiple analysis of
171 my %global_soname_notfound;
172 my %global_soname_used;
173 my %global_soname_needed;
179 my %symfile_has_soname_cache;
181 # Used to count errors due to missing libraries
185 foreach my $file (keys %exec) {
186 $cur_field = $exec{$file};
187 debug
(1, ">> Scanning $file (for $cur_field field)");
189 my $obj = Dpkg
::Shlibs
::Objdump
::Object
->new($file);
190 my @sonames = $obj->get_needed_libraries;
192 # Load symbols files for all needed libraries (identified by SONAME)
198 foreach my $soname (@sonames) {
199 my @libs = my_find_library
($soname, $obj->{RPATH
}, $obj->{exec_abi
}, $file);
200 unless (scalar @libs) {
201 $soname_notfound{$soname} = 1;
202 $global_soname_notfound{$soname} = 1;
203 my $msg = g_
('cannot find library %s needed by %s (ELF ' .
204 "format: '%s' abi: '%s'; RPATH: '%s')");
205 if (scalar(split_soname
($soname))) {
206 errormsg
($msg, $soname, $file, $obj->{format
}, $obj->{exec_abi
},
207 join(':', @
{$obj->{RPATH
}}));
210 warning
($msg, $soname, $file, $obj->{format
}, $obj->{exec_abi
},
211 join(':', @
{$obj->{RPATH
}}));
216 # Track shared libraries for a given SONAME.
217 push @
{$soname_libs{$soname}}, @libs;
219 # Track shared libraries for package mapping.
220 foreach my $lib (@libs) {
221 $libfiles{$lib} = $soname;
222 my $reallib = realpath
($lib);
223 if ($reallib ne $lib) {
224 $altlibfiles{$reallib} = $soname;
226 debug
(1, "Library $soname found in $lib");
229 my $file2pkg = find_packages
(keys %libfiles, keys %altlibfiles);
230 my $symfile = Dpkg
::Shlibs
::SymbolFile
->new();
231 my $dumplibs_wo_symfile = Dpkg
::Shlibs
::Objdump
->new();
232 SONAME
: foreach my $soname (@sonames) {
233 # Select the first good entry from the ordered list that we got from
234 # find_library(), and skip to the next SONAME.
236 foreach my $lib (@
{$soname_libs{$soname}}) {
237 if (none
{ $_ ne '' } @
{$file2pkg->{$lib}}) {
238 # The path of the library as calculated is not the
239 # official path of a packaged file, try to fallback on
240 # the realpath() first, maybe this one is part of a package
241 my $reallib = realpath
($lib);
242 if (exists $file2pkg->{$reallib}) {
243 $file2pkg->{$lib} = $file2pkg->{$reallib};
246 if (none
{ $_ ne '' } @
{$file2pkg->{$lib}}) {
247 # If the library is really not available in an installed package,
248 # it's because it's in the process of being built
249 # Empty package name will lead to consideration of symbols
250 # file from the package being built only
251 $file2pkg->{$lib} = [''];
252 debug
(1, "No associated package found for $lib");
255 # Load symbols/shlibs files from packages providing libraries
256 my $missing_wanted_shlibs_info = 0;
257 foreach my $pkg (@
{$file2pkg->{$lib}}) {
260 if (-e
$shlibslocal and
261 defined(extract_from_shlibs
($soname, $shlibslocal)))
265 if ($packagetype eq 'deb' and not $haslocaldep) {
266 # Use fine-grained dependencies only on real deb
267 # and only if the dependency is not provided by shlibs.local
268 $symfile_path = find_symbols_file
($pkg, $soname, $lib);
270 if (defined($symfile_path)) {
271 # Load symbol information
272 debug
(1, "Using symbols file $symfile_path for $soname");
273 $symfile_cache{$symfile_path} //=
274 Dpkg
::Shlibs
::SymbolFile
->new(file
=> $symfile_path);
275 $symfile->merge_object_from_symfile($symfile_cache{$symfile_path}, $soname);
277 if (defined($symfile_path) && $symfile->has_object($soname)) {
278 # Initialize dependencies with the smallest minimal version
279 # of all symbols (unversioned dependency is not ok as the
280 # library might not have always been available in the
281 # package and we really need it)
282 my $dep = $symfile->get_dependency($soname);
283 my $minver = $symfile->get_smallest_version($soname) || '';
284 update_dependency_version
($dep, $minver);
285 debug
(2, " Minimal version of ($dep) initialized with ($minver)");
287 # Found a symbols file for the SONAME.
290 # No symbol file found, fall back to standard shlibs
291 debug
(1, "Using shlibs+objdump for $soname (file $lib)");
292 $objdump_cache{$lib} //= Dpkg
::Shlibs
::Objdump
::Object
->new($lib);
293 my $libobj = $objdump_cache{$lib};
294 my $id = $dumplibs_wo_symfile->add_object($libobj);
295 if (($id ne $soname) and ($id ne $lib)) {
296 warning
(g_
('%s has an unexpected SONAME (%s)'), $lib, $id);
297 $alt_soname{$id} = $soname;
300 # Only try to generate a dependency for libraries with a SONAME
301 if (not $libobj->is_public_library()) {
302 debug
(1, "Skipping shlibs+objdump info for private library $lib");
306 # If we found a shlibs file for the SONAME, skip to the next.
307 next SONAME
if add_shlibs_dep
($soname, $pkg, $lib);
309 $missing_wanted_shlibs_info = 1;
311 debug
(1, "No shlibs+objdump info available, trying next package for $lib");
315 next if not $missing_wanted_shlibs_info;
317 # We will only reach this point, if we have found no symbols nor
318 # shlibs files for the given SONAME.
320 # This failure is fairly new, try to be kind by
321 # ignoring as many cases that can be safely ignored
323 # 1/ when the lib and the binary are in the same
325 my $root_file = guess_pkg_root_dir
($file);
326 my $root_lib = guess_pkg_root_dir
($lib);
327 $ignore++ if defined $root_file and defined $root_lib
328 and check_files_are_the_same
($root_file, $root_lib);
329 # 2/ when the lib is not versioned and can't be
331 $ignore++ unless scalar split_soname
($soname);
332 # 3/ when we have been asked to do so
333 $ignore++ if $ignore_missing_info;
334 error
(g_
('no dependency information found for %s ' .
336 'Hint: check if the library actually comes ' .
337 'from a package.'), $lib, $file)
342 # Scan all undefined symbols of the binary and resolve to a
345 foreach my $soname (@sonames) {
346 # Initialize statistics
347 $soname_used{$soname} = 0;
348 $global_soname_used{$soname} //= 0;
349 if (exists $global_soname_needed{$soname}) {
350 push @
{$global_soname_needed{$soname}}, $file;
352 $global_soname_needed{$soname} = [ $file ];
356 my $nb_skipped_warnings = 0;
357 # Disable warnings about missing symbols when we have not been able to
359 my $disable_warnings = scalar(keys(%soname_notfound));
360 my $in_public_dir = 1;
361 if (my $relname = relative_to_pkg_root
($file)) {
362 my $parent_dir = '/' . dirname
($relname);
363 $in_public_dir = any
{ $parent_dir eq $_ } get_library_paths
();
365 warning
(g_
('binaries to analyze should already be ' .
366 "installed in their package's directory"));
368 debug
(2, 'Analyzing all undefined symbols');
369 foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
370 my $name = $sym->{name
};
371 if ($sym->{version
}) {
372 $name .= '@' . "$sym->{version}";
374 $name .= '@' . 'Base';
376 debug
(2, " Looking up symbol $name");
377 my %symdep = $symfile->lookup_symbol($name, \
@sonames);
379 my $depends = $symfile->get_dependency($symdep{soname
},
380 $symdep{symbol
}{dep_id
});
381 debug
(2, " Found in symbols file of $symdep{soname} (minver: " .
382 "$symdep{symbol}{minver}, dep: $depends)");
383 $soname_used{$symdep{soname
}}++;
384 $global_soname_used{$symdep{soname
}}++;
385 if (exists $alt_soname{$symdep{soname
}}) {
386 # Also count usage on alternate soname
387 $soname_used{$alt_soname{$symdep{soname
}}}++;
388 $global_soname_used{$alt_soname{$symdep{soname
}}}++;
390 update_dependency_version
($depends, $symdep{symbol
}{minver
});
392 my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
393 if (not defined($syminfo)) {
394 debug
(2, ' Not found');
395 next unless ($warnings & WARN_SYM_NOT_FOUND
);
396 next if $disable_warnings;
397 # Complain about missing symbols only for executables
398 # and public libraries
399 if ($obj->is_executable() or $obj->is_public_library()) {
400 my $print_name = $name;
401 # Drop the default suffix for readability
402 $print_name =~ s/\@Base$//;
403 unless ($sym->{weak
}) {
404 if ($debug or ($in_public_dir and $nb_warnings < 10)
405 or (not $in_public_dir and $nb_warnings < 1))
407 if ($in_public_dir) {
408 warning
(g_
('symbol %s used by %s found in none of the ' .
409 'libraries'), $print_name, $file);
411 warning
(g_
('%s contains an unresolvable reference to ' .
412 "symbol %s: it's probably a plugin"),
417 $nb_skipped_warnings++;
422 debug
(2, " Found in $syminfo->{soname} ($syminfo->{objid})");
423 if (exists $alt_soname{$syminfo->{soname
}}) {
424 # Also count usage on alternate soname
425 $soname_used{$alt_soname{$syminfo->{soname
}}}++;
426 $global_soname_used{$alt_soname{$syminfo->{soname
}}}++;
428 $soname_used{$syminfo->{soname
}}++;
429 $global_soname_used{$syminfo->{soname
}}++;
433 warning
(P_
('%d similar warning has been skipped (use -v to see it)',
434 '%d other similar warnings have been skipped (use -v to see ' .
435 'them all)', $nb_skipped_warnings), $nb_skipped_warnings)
436 if $nb_skipped_warnings;
437 foreach my $soname (@sonames) {
438 # Adjust minimal version of dependencies with information
439 # extracted from build-dependencies
440 my $dev_pkgs = $symfile->get_field($soname, 'Build-Depends-Packages') //
441 $symfile->get_field($soname, 'Build-Depends-Package');
442 foreach my $dev_pkg (split /[,\s]+/, $dev_pkgs // '') {
443 debug
(1, "Updating dependencies of $soname with build-dependencies");
444 my $minver = get_min_version_from_deps
($build_deps, $dev_pkg);
445 if (defined $minver) {
446 foreach my $dep ($symfile->get_dependencies($soname)) {
447 update_dependency_version
($dep, $minver, 1);
448 debug
(1, " Minimal version of $dep updated with $minver");
451 debug
(1, " No minimal version found in $dev_pkg build-dependency");
455 # Warn about un-NEEDED libraries
456 unless ($soname_notfound{$soname} or $soname_used{$soname}) {
457 # Ignore warning for libm.so.6 if also linked against libstdc++
458 next if ($soname =~ /^libm\.so\.\d+$/ and
459 any
{ m/^libstdc\+\+\.so\.\d+/ } @sonames);
460 next unless ($warnings & WARN_NOT_NEEDED
);
461 warning
(g_
('%s should not be linked against %s (it uses none of ' .
462 "the library's symbols)"), $file, $soname);
467 # Warn of unneeded libraries at the "package" level (i.e. over all
468 # binaries that we have inspected)
469 foreach my $soname (keys %global_soname_needed) {
470 unless ($global_soname_notfound{$soname} or $global_soname_used{$soname}) {
471 next if ($soname =~ /^libm\.so\.\d+$/ and
472 any
{ m/^libstdc\+\+\.so\.\d+/ } keys %global_soname_needed);
473 next unless ($warnings & WARN_DEP_AVOIDABLE
);
474 warning
(P_
('package could avoid a useless dependency if %s was not ' .
475 "linked against %s (it uses none of the library's symbols)",
476 'package could avoid a useless dependency if %s were not ' .
477 "linked against %s (they use none of the library's symbols)",
478 scalar @
{$global_soname_needed{$soname}}),
479 join(' ', @
{$global_soname_needed{$soname}}), $soname);
483 # Quit now if any missing libraries
484 if ($error_count >= 1) {
485 my $note = g_
('Note: libraries are not searched in other binary packages ' .
486 "that do not have any shlibs or symbols file.\n" .
487 'To help dpkg-shlibdeps find private libraries, you might ' .
489 error
(P_
('cannot continue due to the error above',
490 'cannot continue due to the errors listed above',
491 $error_count) . "\n" . $note);
494 # Open substvars file
496 my $substvars = Dpkg
::Substvars
->new();
498 $varlistfilenew = '-';
500 $substvars->load($varlistfile) if -e
$varlistfile;
501 $substvars->filter(remove
=> sub { $_[0] =~ m/^\Q$varnameprefix\E:/ });
503 $varlistfilenew = "$varlistfile.new";
506 # Write out the shlibs substvars
510 my ($dep, $field) = @_;
511 # Skip dependencies on excluded packages
512 foreach my $exc (@exclude) {
513 return 0 if $dep =~ /^\s*\Q$exc\E\b/;
515 # Don't include dependencies if they are already
516 # mentioned in a higher priority field
517 if (not exists($depseen{$dep})) {
518 $depseen{$dep} = $dependencies{$field}{$dep};
521 # Since dependencies can be versioned, we have to
522 # verify if the dependency is stronger than the
523 # previously seen one
525 if ($depseen{$dep} eq $dependencies{$field}{$dep}) {
526 # If both versions are the same (possibly unversioned)
528 } elsif ($dependencies{$field}{$dep} eq '') {
529 $stronger = 0; # If the dep is unversioned
530 } elsif ($depseen{$dep} eq '') {
531 $stronger = 1; # If the dep seen is unversioned
532 } elsif (version_compare_relation
($depseen{$dep}, REL_GT
,
533 $dependencies{$field}{$dep})) {
534 # The version of the dep seen is stronger...
539 $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
544 foreach my $field (reverse @depfields) {
546 if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
549 # Translate dependency templates into real dependencies
551 if ($dependencies{$field}{$templ}->is_valid() and
552 $dependencies{$field}{$templ}->as_string()) {
553 $templ =~ s/#MINVER#/(>= $dependencies{$field}{$templ})/g;
555 $templ =~ s/#MINVER#//g;
560 filter_deps
($_, $field)
561 } keys %{$dependencies{$field}};
564 my $obj = deps_parse
($dep);
565 error
(g_
('invalid dependency got generated: %s'), $dep) unless defined $obj;
567 $substvars->set_as_used("$varnameprefix:$field", "$obj");
571 $substvars->save($varlistfilenew);
573 # Replace old file by new one
575 rename $varlistfilenew, $varlistfile
576 or syserr
(g_
("install new varlist file '%s'"), $varlistfile);
584 printf g_
("Debian %s version %s.\n"), $Dpkg::PROGNAME
, $Dpkg::PROGVERSION
;
587 This is free software; see the GNU General Public License version 2 or
588 later for copying conditions. There is NO warranty.
594 'Usage: %s [<option>...] <executable>|-e<executable> [<option>...]')
596 "Positional options (order is significant):
597 <executable> include dependencies for <executable>,
598 -e<executable> (use -e if <executable> starts with '-')
599 -d<dependency-field> next executable(s) set shlibs:<dependency-field>.")
602 -l<library-dir> add directory to private shared library search list.
603 -p<varname-prefix> set <varname-prefix>:* instead of shlibs:*.
604 -O[<file>] write variable settings to stdout (or <file>).
605 -L<local-shlibs-file> shlibs override file, not debian/shlibs.local.
606 -T<substvars-file> update variables here, not debian/substvars.
607 -t<type> set package type (default is deb).
608 -x<package> exclude package from the generated dependencies.
609 -S<package-build-dir> search needed libraries in the given
610 package build directory first.
611 -I<package-build-dir> ignore needed libraries, shlibs and symbols files
612 in the given build directory.
613 -v enable verbose mode (can be used multiple times).
614 --ignore-missing-info don't fail if dependency information can't be found.
615 --warnings=<value> define set of active warnings (see manual page).
616 --admindir=<directory> change the administrative directory.
617 -?, --help show this help message.
618 --version show the version.")
620 'Dependency fields recognized are:
622 '), $Dpkg::PROGNAME
, join('/', @depfields);
625 sub get_min_version_from_deps
{
626 my ($dep, $pkg) = @_;
627 if ($dep->isa('Dpkg::Deps::Simple')) {
628 if (($dep->{package} eq $pkg) &&
629 defined($dep->{relation
}) &&
630 (($dep->{relation
} eq REL_GE
) ||
631 ($dep->{relation
} eq REL_GT
)))
633 return $dep->{version
};
638 foreach my $subdep ($dep->get_deps()) {
639 my $minver = get_min_version_from_deps
($subdep, $pkg);
640 next if not defined $minver;
642 if (version_compare_relation
($minver, REL_GT
, $res)) {
653 sub update_dependency_version
{
654 my ($dep, $minver, $existing_only) = @_;
655 return if not defined($minver);
656 $minver = Dpkg
::Version
->new($minver);
657 foreach my $subdep (split /\s*,\s*/, $dep) {
658 if (exists $dependencies{$cur_field}{$subdep} and
659 defined($dependencies{$cur_field}{$subdep}))
661 if ($dependencies{$cur_field}{$subdep} eq '' or $minver ne '' and
662 version_compare_relation
($minver, REL_GT
,
663 $dependencies{$cur_field}{$subdep}))
665 $dependencies{$cur_field}{$subdep} = $minver;
667 } elsif (!$existing_only) {
668 $dependencies{$cur_field}{$subdep} = $minver;
674 my ($soname, $pkg, $libfile) = @_;
675 my @shlibs = ($shlibslocal, $shlibsoverride);
677 # If the file is not packaged, try to find out the shlibs file in
678 # the package being built where the lib has been found
679 my $pkg_root = guess_pkg_root_dir
($libfile);
680 if (defined $pkg_root) {
681 push @shlibs, "$pkg_root/DEBIAN/shlibs";
683 # Fallback to other shlibs files but it shouldn't be necessary
684 push @shlibs, @pkg_shlibs;
686 my $control_file = get_control_path
($pkg, 'shlibs');
687 push @shlibs, $control_file if defined $control_file;
689 push @shlibs, $shlibsdefault;
690 debug
(1, " Looking up shlibs dependency of $soname provided by '$pkg'");
691 foreach my $file (@shlibs) {
692 next if not -e
$file;
693 my $dep = extract_from_shlibs
($soname, $file);
695 debug
(1, " Found $dep in $file");
696 foreach (split(/,\s*/, $dep)) {
697 # Note: the value is empty for shlibs based dependency
698 # symbol based dependency will put a valid version as value
699 $dependencies{$cur_field}{$_} = Dpkg
::Version
->new('');
704 debug
(1, ' Found nothing');
711 if ($soname =~ /^(.+)\.so\.(.+)$/) {
712 # Shared library with stable <name>.so.<version> format.
713 return wantarray ?
($1, $2) : 1;
714 } elsif ($soname =~ /^(.+)-(\d.*)\.so$/) {
715 # Shared library/module with unstable <name>-<version>.so format.
716 return wantarray ?
($1, $2) : 1;
719 return wantarray ?
() : 0;
723 sub extract_from_shlibs
{
724 my ($soname, $shlibfile) = @_;
726 if (exists $shlibs_cache{$shlibfile}{$soname}) {
727 return $shlibs_cache{$shlibfile}{$soname};
732 (?
:(\S
+):\s
+)?
# Optional type
737 (\S
.*\S
) # Dependencies
742 # Split soname in name/version
743 my ($libname, $libversion) = split_soname
($soname);
744 unless (defined $libname) {
745 warning
(g_
("can't extract name and version from library name '%s'"),
747 $shlibs_cache{$shlibfile}{$soname} = undef;
751 open(my $shlibs_fh, '<', $shlibfile)
752 or syserr
(g_
("unable to open shared libs info file '%s'"), $shlibfile);
754 while (<$shlibs_fh>) {
757 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
758 if (!m/$shlibs_re/) {
759 warning
(g_
("shared libs info file '%s' line %d: bad line '%s'"),
763 my $depread = $4 // '';
764 if (($libname eq $2) && ($libversion eq $3)) {
765 # Define dep and end here if the package type explicitly
766 # matches. Otherwise if the packagetype is not specified, use
767 # the dep only as a default that can be overridden by a later
770 if ($1 eq $packagetype) {
780 $shlibs_cache{$shlibfile}{$soname} = $dep;
784 sub find_symbols_file
{
785 my ($pkg, $soname, $libfile) = @_;
788 # If the file is not packaged, try to find out the symbols file in
789 # the package being built where the lib has been found
790 my $pkg_root = guess_pkg_root_dir
($libfile);
791 if (defined $pkg_root) {
792 push @files, "$pkg_root/DEBIAN/symbols";
794 # Fallback to other symbols files but it shouldn't be necessary
795 push @files, @pkg_symbols;
797 push @files, "$Dpkg::CONFDIR/symbols/$pkg.symbols.$host_arch",
798 "$Dpkg::CONFDIR/symbols/$pkg.symbols";
800 state %control_file_cache;
801 if (not exists $control_file_cache{$pkg}) {
802 $control_file_cache{$pkg} = get_control_path
($pkg, 'symbols');
804 my $control_file = $control_file_cache{$pkg};
805 push @files, $control_file if defined $control_file;
808 foreach my $file (@files) {
809 if (-e
$file and symfile_has_soname
($file, $soname)) {
816 sub symfile_has_soname
{
817 my ($file, $soname) = @_;
819 if (exists $symfile_has_soname_cache{$file}{$soname}) {
820 return $symfile_has_soname_cache{$file}{$soname};
823 open(my $symfile_fh, '<', $file)
824 or syserr
(g_
('cannot open file %s'), $file);
826 while (<$symfile_fh>) {
827 if (/^\Q$soname\E /) {
833 $symfile_has_soname_cache{$file}{$soname} = $result;
837 # find_library ($soname, \@rpath, $format)
838 sub my_find_library
{
839 my ($lib, $rpath, $format, $execfile) = @_;
841 # Create real RPATH in case $ORIGIN is used
842 # Note: ld.so also supports $PLATFORM and $LIB but they are
843 # used in real case (yet)
844 my $libdir = relative_to_pkg_root
($execfile);
846 if (defined $libdir) {
847 $origin = "/$libdir";
848 $origin =~ s{/+[^/]*$}{};
851 foreach my $path (@
{$rpath}) {
852 if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
853 if (defined $origin) {
854 $path =~ s/\$ORIGIN/$origin/g;
855 $path =~ s/\$\{ORIGIN\}/$origin/g;
857 warning
(g_
('$ORIGIN is used in RPATH of %s and the corresponding ' .
858 'directory could not be identified due to lack of DEBIAN ' .
859 "sub-directory in the root of package's build tree"), $execfile);
865 # Look into the packages we're currently building in the following
867 # - package build tree of the binary which is analyzed
868 # - package build tree given on the command line (option -S)
869 # - other package build trees that contain either a shlibs or a
872 # - package build tree given on the command line (option -I)
875 my $pkg_root = guess_pkg_root_dir
($execfile);
876 push @builddirs, $pkg_root if defined $pkg_root;
877 push @builddirs, @pkg_dir_to_search;
878 push @builddirs, @pkg_root_dirs;
880 foreach my $builddir (@builddirs) {
881 next if defined($dir_checked{$builddir});
882 next if ignore_pkgdir
($builddir);
883 my @libs = find_library
($lib, \
@RPATH, $format, $builddir);
884 return @libs if scalar @libs;
885 $dir_checked{$builddir} = 1;
888 # Fallback in the root directory if we have not found what we were
889 # looking for in the packages
890 return find_library
($lib, \
@RPATH, $format, '');
893 my %cached_pkgmatch = ();
901 foreach my $path (@paths) {
902 if (exists $cached_pkgmatch{$path}) {
903 $pkgmatch->{$path} = $cached_pkgmatch{$path};
906 $cached_pkgmatch{$path} = ['']; # placeholder to cache misses too.
907 $pkgmatch->{$path} = ['']; # might be replaced later on
910 return $pkgmatch unless scalar(@files);
912 my $pid = open(my $dpkg_fh, '-|');
913 syserr
(g_
('cannot fork for %s'), 'dpkg-query --search') unless defined $pid;
915 # Child process running dpkg --search and discarding errors
917 open STDERR
, '>', '/dev/null'
918 or syserr
(g_
('cannot open file %s'), '/dev/null');
920 exec 'dpkg-query', '--search', '--', @files
921 or syserr
(g_
('unable to execute %s'), 'dpkg');
925 if (m/^local diversion |^diversion by/) {
926 warning
(g_
('diversions involved - output may be incorrect'));
927 print { *STDERR
} " $_\n"
928 or syserr
(g_
('write diversion info to stderr'));
929 } elsif (m/^([-a-z0-9+.:, ]+): (\/.*)$/) {
930 my ($pkgs, $path) = ($1, $2);
931 my $realpath = realpath
($path);
932 $cached_pkgmatch{$path} = $pkgmatch->{$path} = [ split /, /, $pkgs ];
933 $cached_pkgmatch{$realpath} = $pkgmatch->{$realpath} = [ split /, /, $pkgs ];
935 warning
(g_
("unknown output from dpkg --search: '%s'"), $_);