dpkg-shlibdeps.pl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h :signal_h);
  6. use Cwd qw(realpath);
  7. use Dpkg;
  8. use Dpkg::Gettext;
  9. use Dpkg::ErrorHandling qw(warning error failure syserr usageerr);
  10. use Dpkg::Path qw(relative_to_pkg_root guess_pkg_root_dir
  11. check_files_are_the_same);
  12. use Dpkg::Version qw(compare_versions);
  13. use Dpkg::Shlibs qw(find_library);
  14. use Dpkg::Shlibs::Objdump;
  15. use Dpkg::Shlibs::SymbolFile;
  16. use Dpkg::Arch qw(get_host_arch);
  17. use Dpkg::Fields qw(capit);
  18. use Dpkg::Deps;
  19. use Dpkg::Control;
  20. use constant {
  21. WARN_SYM_NOT_FOUND => 1,
  22. WARN_DEP_AVOIDABLE => 2,
  23. WARN_NOT_NEEDED => 4,
  24. };
  25. # By increasing importance
  26. my @depfields = qw(Suggests Recommends Depends Pre-Depends);
  27. my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
  28. textdomain("dpkg-dev");
  29. my $shlibsoverride = '/etc/dpkg/shlibs.override';
  30. my $shlibsdefault = '/etc/dpkg/shlibs.default';
  31. my $shlibslocal = 'debian/shlibs.local';
  32. my $packagetype = 'deb';
  33. my $dependencyfield = 'Depends';
  34. my $varlistfile = 'debian/substvars';
  35. my $varnameprefix = 'shlibs';
  36. my $ignore_missing_info = 0;
  37. my $warnings = 3;
  38. my $debug = 0;
  39. my @exclude = ();
  40. my @pkg_dir_to_search = ();
  41. my $host_arch = get_host_arch();
  42. my (@pkg_shlibs, @pkg_symbols, @pkg_root_dirs);
  43. if (-d "debian") {
  44. push @pkg_symbols, <debian/*/DEBIAN/symbols>;
  45. push @pkg_shlibs, <debian/*/DEBIAN/shlibs>;
  46. my %uniq = map { guess_pkg_root_dir($_) => 1 } (@pkg_symbols, @pkg_shlibs);
  47. push @pkg_root_dirs, keys %uniq;
  48. }
  49. my ($stdout, %exec);
  50. foreach (@ARGV) {
  51. if (m/^-T(.*)$/) {
  52. $varlistfile = $1;
  53. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  54. $varnameprefix = $1;
  55. } elsif (m/^-L(.*)$/) {
  56. $shlibslocal = $1;
  57. } elsif (m/^-S(.*)$/) {
  58. push @pkg_dir_to_search, $1;
  59. } elsif (m/^-O$/) {
  60. $stdout = 1;
  61. } elsif (m/^-(h|-help)$/) {
  62. usage(); exit(0);
  63. } elsif (m/^--version$/) {
  64. version(); exit(0);
  65. } elsif (m/^--admindir=(.*)$/) {
  66. $admindir = $1;
  67. -d $admindir ||
  68. error(_g("administrative directory '%s' does not exist"), $admindir);
  69. } elsif (m/^-d(.*)$/) {
  70. $dependencyfield = capit($1);
  71. defined($depstrength{$dependencyfield}) ||
  72. warning(_g("unrecognised dependency field \`%s'"), $dependencyfield);
  73. } elsif (m/^-e(.*)$/) {
  74. if (exists $exec{$1}) {
  75. # Affect the binary to the most important field
  76. if ($depstrength{$dependencyfield} > $depstrength{$exec{$1}}) {
  77. $exec{$1} = $dependencyfield;
  78. }
  79. } else {
  80. $exec{$1} = $dependencyfield;
  81. }
  82. } elsif (m/^--ignore-missing-info$/) {
  83. $ignore_missing_info = 1;
  84. } elsif (m/^--warnings=(\d+)$/) {
  85. $warnings = $1;
  86. } elsif (m/^-t(.*)$/) {
  87. $packagetype = $1;
  88. } elsif (m/^-v$/) {
  89. $debug++;
  90. } elsif (m/^-x(.*)$/) {
  91. push @exclude, $1;
  92. } elsif (m/^-/) {
  93. usageerr(_g("unknown option \`%s'"), $_);
  94. } else {
  95. if (exists $exec{$_}) {
  96. # Affect the binary to the most important field
  97. if ($depstrength{$dependencyfield} > $depstrength{$exec{$_}}) {
  98. $exec{$_} = $dependencyfield;
  99. }
  100. } else {
  101. $exec{$_} = $dependencyfield;
  102. }
  103. }
  104. }
  105. scalar keys %exec || usageerr(_g("need at least one executable"));
  106. my $control = Dpkg::Control->new();
  107. my $fields = $control->get_source();
  108. my $build_depends = defined($fields->{"Build-Depends"}) ?
  109. $fields->{"Build-Depends"} : "";
  110. my $build_deps = Dpkg::Deps::parse($build_depends, reduce_arch => 1);
  111. my %dependencies;
  112. my %shlibs;
  113. # Statictics on soname seen in the whole run (with multiple analysis of
  114. # binaries)
  115. my %global_soname_notfound;
  116. my %global_soname_used;
  117. my %global_soname_needed;
  118. my $cur_field;
  119. foreach my $file (keys %exec) {
  120. $cur_field = $exec{$file};
  121. print ">> Scanning $file (for $cur_field field)\n" if $debug;
  122. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  123. my @sonames = $obj->get_needed_libraries;
  124. # Load symbols files for all needed libraries (identified by SONAME)
  125. my %libfiles;
  126. my %altlibfiles;
  127. my %soname_notfound;
  128. my %alt_soname;
  129. foreach my $soname (@sonames) {
  130. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  131. unless (defined $lib) {
  132. $soname_notfound{$soname} = 1;
  133. $global_soname_notfound{$soname} = 1;
  134. my $msg = _g("couldn't find library %s needed by %s (its RPATH is '%s').\n" .
  135. "Note: libraries are not searched in other binary packages " .
  136. "that do not have any shlibs or symbols file.\nTo help dpkg-shlibdeps " .
  137. "find private libraries, you might need to set LD_LIBRARY_PATH.");
  138. if (scalar(split_soname($soname))) {
  139. failure($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  140. } else {
  141. warning($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  142. }
  143. next;
  144. }
  145. $libfiles{$lib} = $soname;
  146. my $reallib = realpath($lib);
  147. if ($reallib ne $lib) {
  148. $altlibfiles{$reallib} = $soname;
  149. }
  150. print "Library $soname found in $lib\n" if $debug;
  151. }
  152. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  153. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  154. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  155. my @soname_wo_symfile;
  156. foreach my $lib (keys %libfiles) {
  157. my $soname = $libfiles{$lib};
  158. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  159. # The path of the library as calculated is not the
  160. # official path of a packaged file, try to fallback on
  161. # on the realpath() first, maybe this one is part of a package
  162. my $reallib = realpath($lib);
  163. if (exists $file2pkg->{$reallib}) {
  164. $file2pkg->{$lib} = $file2pkg->{$reallib};
  165. }
  166. }
  167. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  168. # If the library is really not available in an installed package,
  169. # it's because it's in the process of being built
  170. # Empty package name will lead to consideration of symbols
  171. # file from the package being built only
  172. $file2pkg->{$lib} = [""];
  173. print "No associated package found for $lib\n" if $debug;
  174. }
  175. # Load symbols/shlibs files from packages providing libraries
  176. foreach my $pkg (@{$file2pkg->{$lib}}) {
  177. my $dpkg_symfile;
  178. if ($packagetype eq "deb") {
  179. # Use fine-grained dependencies only on real deb
  180. $dpkg_symfile = find_symbols_file($pkg, $soname, $lib);
  181. if (defined $dpkg_symfile) {
  182. # Load symbol information
  183. print "Using symbols file $dpkg_symfile for $soname\n" if $debug;
  184. $symfile->load($dpkg_symfile);
  185. }
  186. }
  187. if (defined($dpkg_symfile) && $symfile->has_object($soname)) {
  188. # Initialize dependencies as an unversioned dependency
  189. my $dep = $symfile->get_dependency($soname);
  190. foreach my $subdep (split /\s*,\s*/, $dep) {
  191. if (not exists $dependencies{$cur_field}{$subdep}) {
  192. $dependencies{$cur_field}{$subdep} = '';
  193. }
  194. }
  195. } else {
  196. # No symbol file found, fall back to standard shlibs
  197. my $id = $dumplibs_wo_symfile->parse($lib);
  198. if (($id ne $soname) and ($id ne $lib)) {
  199. warning(_g("%s has an unexpected SONAME (%s)"), $lib, $id);
  200. $alt_soname{$id} = $soname;
  201. }
  202. push @soname_wo_symfile, $soname;
  203. my $libobj = $dumplibs_wo_symfile->get_object($id);
  204. # Only try to generate a dependency for libraries with a SONAME
  205. if ($libobj->is_public_library() and not
  206. add_shlibs_dep($soname, $pkg, $lib)) {
  207. # This failure is fairly new, try to be kind by
  208. # ignoring as many cases that can be safely ignored
  209. my $ignore = 0;
  210. # 1/ when the lib and the binary are in the same
  211. # package
  212. my $root_file = guess_pkg_root_dir($file);
  213. my $root_lib = guess_pkg_root_dir($lib);
  214. $ignore++ if defined $root_file and defined $root_lib
  215. and check_files_are_the_same($root_file, $root_lib);
  216. # 2/ when the lib is not versioned and can't be
  217. # handled by shlibs
  218. $ignore++ unless scalar(split_soname($soname));
  219. # 3/ when we have been asked to do so
  220. $ignore++ if $ignore_missing_info;
  221. failure(_g("no dependency information found for %s " .
  222. "(used by %s)."), $lib, $file)
  223. unless $ignore;
  224. }
  225. }
  226. }
  227. }
  228. # Scan all undefined symbols of the binary and resolve to a
  229. # dependency
  230. my %soname_used;
  231. foreach (@sonames) {
  232. # Initialize statistics
  233. $soname_used{$_} = 0;
  234. $global_soname_used{$_} = 0 unless exists $global_soname_used{$_};
  235. if (exists $global_soname_needed{$_}) {
  236. push @{$global_soname_needed{$_}}, $file;
  237. } else {
  238. $global_soname_needed{$_} = [ $file ];
  239. }
  240. }
  241. my $nb_warnings = 0;
  242. my $nb_skipped_warnings = 0;
  243. # Disable warnings about missing symbols when we have not been able to
  244. # find all libs
  245. my $disable_warnings = scalar(keys(%soname_notfound));
  246. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  247. my $name = $sym->{name};
  248. if ($sym->{version}) {
  249. $name .= "\@$sym->{version}";
  250. } else {
  251. $name .= "\@Base";
  252. }
  253. print " Looking up symbol $name\n" if $debug > 1;
  254. my $symdep = $symfile->lookup_symbol($name, \@sonames);
  255. if (defined($symdep)) {
  256. print " Found in symbols file of $symdep->{soname}\n" if $debug > 1;
  257. $soname_used{$symdep->{soname}}++;
  258. $global_soname_used{$symdep->{soname}}++;
  259. if (exists $alt_soname{$symdep->{soname}}) {
  260. # Also count usage on alternate soname
  261. $soname_used{$alt_soname{$symdep->{soname}}}++;
  262. $global_soname_used{$alt_soname{$symdep->{soname}}}++;
  263. }
  264. update_dependency_version($symdep->{depends},
  265. $symdep->{minver});
  266. } else {
  267. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  268. if (not defined($syminfo)) {
  269. print " Not found\n" if $debug > 1;
  270. next unless ($warnings & WARN_SYM_NOT_FOUND);
  271. next if $disable_warnings;
  272. # Complain about missing symbols only for executables
  273. # and public libraries
  274. if ($obj->is_executable() or $obj->is_public_library()) {
  275. my $print_name = $name;
  276. # Drop the default suffix for readability
  277. $print_name =~ s/\@Base$//;
  278. unless ($sym->{weak}) {
  279. if ($debug or $nb_warnings < 10) {
  280. warning(_g("symbol %s used by %s found in none of the " .
  281. "libraries."), $print_name, $file);
  282. $nb_warnings++;
  283. } else {
  284. $nb_skipped_warnings++;
  285. }
  286. }
  287. }
  288. } else {
  289. print " Found in $syminfo->{soname} ($syminfo->{objid})\n" if $debug > 1;
  290. if (exists $alt_soname{$syminfo->{soname}}) {
  291. # Also count usage on alternate soname
  292. $soname_used{$alt_soname{$syminfo->{soname}}}++;
  293. $global_soname_used{$alt_soname{$syminfo->{soname}}}++;
  294. }
  295. $soname_used{$syminfo->{soname}}++;
  296. $global_soname_used{$syminfo->{soname}}++;
  297. }
  298. }
  299. }
  300. warning(_g("%d other similar warnings have been skipped (use -v to see " .
  301. "them all)."), $nb_skipped_warnings) if $nb_skipped_warnings;
  302. foreach my $soname (@sonames) {
  303. # Adjust minimal version of dependencies with information
  304. # extracted from build-dependencies
  305. my $dev_pkg = $symfile->get_field($soname, 'Build-Depends-Package');
  306. if (defined $dev_pkg) {
  307. my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
  308. if (defined $minver) {
  309. foreach my $dep ($symfile->get_dependencies($soname)) {
  310. update_dependency_version($dep, $minver);
  311. }
  312. }
  313. }
  314. # Warn about un-NEEDED libraries
  315. unless ($soname_notfound{$soname} or $soname_used{$soname}) {
  316. # Ignore warning for libm.so.6 if also linked against libstdc++
  317. next if ($soname =~ /^libm\.so\.\d+$/ and
  318. scalar grep(/^libstdc\+\+\.so\.\d+/, @sonames));
  319. next unless ($warnings & WARN_NOT_NEEDED);
  320. warning(_g("%s shouldn't be linked with %s (it uses none of its " .
  321. "symbols)."), $file, $soname);
  322. }
  323. }
  324. }
  325. # Warn of unneeded libraries at the "package" level (i.e. over all
  326. # binaries that we have inspected)
  327. foreach my $soname (keys %global_soname_needed) {
  328. unless ($global_soname_notfound{$soname} or $global_soname_used{$soname}) {
  329. next if ($soname =~ /^libm\.so\.\d+$/ and scalar(
  330. grep(/^libstdc\+\+\.so\.\d+/, keys %global_soname_needed)));
  331. next unless ($warnings & WARN_DEP_AVOIDABLE);
  332. warning(_g("dependency on %s could be avoided if \"%s\" were not " .
  333. "uselessly linked against it (they use none of its " .
  334. "symbols)."), $soname,
  335. join(" ", @{$global_soname_needed{$soname}}));
  336. }
  337. }
  338. # Open substvars file
  339. my $fh;
  340. if ($stdout) {
  341. $fh = \*STDOUT;
  342. } else {
  343. open(NEW, ">", "$varlistfile.new") ||
  344. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  345. if (-e $varlistfile) {
  346. open(OLD, "<", $varlistfile) ||
  347. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  348. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  349. print(NEW $entry) ||
  350. syserr(_g("copy old entry to new varlist file \`%s'"),
  351. "$varlistfile.new");
  352. }
  353. close(OLD);
  354. }
  355. $fh = \*NEW;
  356. }
  357. # Write out the shlibs substvars
  358. my %depseen;
  359. sub filter_deps {
  360. my ($dep, $field) = @_;
  361. # Skip dependencies on excluded packages
  362. foreach my $exc (@exclude) {
  363. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  364. }
  365. # Don't include dependencies if they are already
  366. # mentionned in a higher priority field
  367. if (not exists($depseen{$dep})) {
  368. $depseen{$dep} = $dependencies{$field}{$dep};
  369. return 1;
  370. } else {
  371. # Since dependencies can be versionned, we have to
  372. # verify if the dependency is stronger than the
  373. # previously seen one
  374. my $stronger;
  375. if ($depseen{$dep} eq $dependencies{$field}{$dep}) {
  376. # If both versions are the same (possibly unversionned)
  377. $stronger = 0;
  378. } elsif ($dependencies{$field}{$dep} eq '') {
  379. $stronger = 0; # If the dep is unversionned
  380. } elsif ($depseen{$dep} eq '') {
  381. $stronger = 1; # If the dep seen is unversionned
  382. } elsif (compare_versions($depseen{$dep}, '>>', $dependencies{$field}{$dep})) {
  383. # The version of the dep seen is stronger...
  384. $stronger = 0;
  385. } else {
  386. $stronger = 1;
  387. }
  388. $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
  389. return $stronger;
  390. }
  391. }
  392. foreach my $field (reverse @depfields) {
  393. my $dep = "";
  394. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  395. $dep = join ", ",
  396. map {
  397. # Translate dependency templates into real dependencies
  398. if ($dependencies{$field}{$_}) {
  399. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  400. } else {
  401. s/#MINVER#//g;
  402. }
  403. s/\s+/ /g;
  404. $_;
  405. } grep { filter_deps($_, $field) }
  406. keys %{$dependencies{$field}};
  407. }
  408. if ($dep) {
  409. my $obj = Dpkg::Deps::parse($dep);
  410. error(_g("invalid dependency got generated: %s"), $dep) unless defined $obj;
  411. $obj->sort();
  412. print $fh "$varnameprefix:$field=" . $obj->dump() . "\n";
  413. }
  414. }
  415. # Replace old file by new one
  416. if (!$stdout) {
  417. close($fh);
  418. rename("$varlistfile.new",$varlistfile) ||
  419. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  420. }
  421. ##
  422. ## Functions
  423. ##
  424. sub version {
  425. printf _g("Debian %s version %s.\n"), $progname, $version;
  426. printf _g("
  427. Copyright (C) 1996 Ian Jackson.
  428. Copyright (C) 2000 Wichert Akkerman.
  429. Copyright (C) 2006 Frank Lichtenheld.
  430. Copyright (C) 2007 Raphael Hertzog.
  431. ");
  432. printf _g("
  433. This is free software; see the GNU General Public Licence version 2 or
  434. later for copying conditions. There is NO warranty.
  435. ");
  436. }
  437. sub usage {
  438. printf _g(
  439. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  440. Positional options (order is significant):
  441. <executable> include dependencies for <executable>,
  442. -e<executable> (use -e if <executable> starts with \`-')
  443. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  444. Options:
  445. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  446. -O print variable settings to stdout.
  447. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  448. -T<varlistfile> update variables here, not debian/substvars.
  449. -t<type> set package type (default is deb).
  450. -x<package> exclude package from the generated dependencies.
  451. -S<pkgbuilddir> search needed libraries in the given
  452. package build directory first.
  453. -v enable verbose mode (can be used multiple times).
  454. --ignore-missing-info don't fail if dependency information can't be found.
  455. --warnings=<value> define set of active warnings (see manual page).
  456. --admindir=<directory> change the administrative directory.
  457. -h, --help show this help message.
  458. --version show the version.
  459. Dependency fields recognised are:
  460. %s
  461. "), $progname, join("/",@depfields);
  462. }
  463. sub get_min_version_from_deps {
  464. my ($dep, $pkg) = @_;
  465. if ($dep->isa('Dpkg::Deps::Simple')) {
  466. if (($dep->{package} eq $pkg) &&
  467. defined($dep->{relation}) &&
  468. (($dep->{relation} eq ">=") ||
  469. ($dep->{relation} eq ">>")))
  470. {
  471. return $dep->{version};
  472. }
  473. return undef;
  474. } else {
  475. my $res;
  476. foreach my $subdep ($dep->get_deps()) {
  477. my $minver = get_min_version_from_deps($subdep, $pkg);
  478. next if not defined $minver;
  479. if (defined $res) {
  480. if (compare_versions($minver, '>>', $res)) {
  481. $res = $minver;
  482. }
  483. } else {
  484. $res = $minver;
  485. }
  486. }
  487. return $res;
  488. }
  489. }
  490. sub update_dependency_version {
  491. my ($dep, $minver) = @_;
  492. return if not defined($minver);
  493. foreach my $subdep (split /\s*,\s*/, $dep) {
  494. if (exists $dependencies{$cur_field}{$subdep} and
  495. defined($dependencies{$cur_field}{$subdep}))
  496. {
  497. if ($dependencies{$cur_field}{$subdep} eq '' or
  498. compare_versions($minver, '>>',
  499. $dependencies{$cur_field}{$subdep}))
  500. {
  501. $dependencies{$cur_field}{$subdep} = $minver;
  502. }
  503. } else {
  504. $dependencies{$cur_field}{$subdep} = $minver;
  505. }
  506. }
  507. }
  508. sub add_shlibs_dep {
  509. my ($soname, $pkg, $libfile) = @_;
  510. my @shlibs = ($shlibslocal, $shlibsoverride);
  511. if ($pkg eq "") {
  512. # If the file is not packaged, try to find out the shlibs file in
  513. # the package being built where the lib has been found
  514. my $pkg_root = guess_pkg_root_dir($libfile);
  515. if (defined $pkg_root) {
  516. push @shlibs, "$pkg_root/DEBIAN/shlibs";
  517. }
  518. # Fallback to other shlibs files but it shouldn't be necessary
  519. push @shlibs, @pkg_shlibs;
  520. } else {
  521. push @shlibs, "$admindir/info/$pkg.shlibs";
  522. }
  523. push @shlibs, $shlibsdefault;
  524. print "Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  525. foreach my $file (@shlibs) {
  526. next if not -e $file;
  527. my $dep = extract_from_shlibs($soname, $file);
  528. if (defined($dep)) {
  529. print "Found $dep in $file\n" if $debug;
  530. foreach (split(/,\s*/, $dep)) {
  531. # Note: the value is empty for shlibs based dependency
  532. # symbol based dependency will put a version as value
  533. $dependencies{$cur_field}{$_} = '';
  534. }
  535. return 1;
  536. }
  537. }
  538. print "Found nothing\n" if $debug;
  539. return 0;
  540. }
  541. sub split_soname {
  542. my $soname = shift;
  543. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  544. return wantarray ? ($1, $2) : 1;
  545. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  546. return wantarray ? ($1, $2) : 1;
  547. } else {
  548. return wantarray ? () : 0;
  549. }
  550. }
  551. sub extract_from_shlibs {
  552. my ($soname, $shlibfile) = @_;
  553. # Split soname in name/version
  554. my ($libname, $libversion) = split_soname($soname);
  555. unless (defined $libname) {
  556. warning(_g("Can't extract name and version from library name \`%s'"),
  557. $soname);
  558. return;
  559. }
  560. # Open shlibs file
  561. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  562. open(SHLIBS, "<", $shlibfile) ||
  563. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  564. my $dep;
  565. while (<SHLIBS>) {
  566. s/\s*\n$//;
  567. next if m/^\#/;
  568. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  569. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  570. $shlibfile, $., $_);
  571. next;
  572. }
  573. my $depread = defined($4) ? $4 : '';
  574. if (($libname eq $2) && ($libversion eq $3)) {
  575. # Define dep and end here if the package type explicitely
  576. # matches. Otherwise if the packagetype is not specified, use
  577. # the dep only as a default that can be overriden by a later
  578. # line
  579. if (defined($1)) {
  580. if ($1 eq $packagetype) {
  581. $dep = $depread;
  582. last;
  583. }
  584. } else {
  585. $dep = $depread unless defined $dep;
  586. }
  587. }
  588. }
  589. close(SHLIBS);
  590. return $dep;
  591. }
  592. sub find_symbols_file {
  593. my ($pkg, $soname, $libfile) = @_;
  594. my @files;
  595. if ($pkg eq "") {
  596. # If the file is not packaged, try to find out the symbols file in
  597. # the package being built where the lib has been found
  598. my $pkg_root = guess_pkg_root_dir($libfile);
  599. if (defined $pkg_root) {
  600. push @files, "$pkg_root/DEBIAN/symbols";
  601. }
  602. # Fallback to other symbols files but it shouldn't be necessary
  603. push @files, @pkg_symbols;
  604. } else {
  605. push @files, "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  606. "/etc/dpkg/symbols/$pkg.symbols",
  607. "$admindir/info/$pkg.symbols";
  608. }
  609. foreach my $file (@files) {
  610. if (-e $file and symfile_has_soname($file, $soname)) {
  611. return $file;
  612. }
  613. }
  614. return undef;
  615. }
  616. sub symfile_has_soname {
  617. my ($file, $soname) = @_;
  618. open(SYM_FILE, "<", $file) ||
  619. syserr(_g("cannot open file %s"), $file);
  620. my $result = 0;
  621. while (<SYM_FILE>) {
  622. if (/^\Q$soname\E /) {
  623. $result = 1;
  624. last;
  625. }
  626. }
  627. close(SYM_FILE);
  628. return $result;
  629. }
  630. # find_library ($soname, \@rpath, $format)
  631. sub my_find_library {
  632. my ($lib, $rpath, $format, $execfile) = @_;
  633. my $file;
  634. # Create real RPATH in case $ORIGIN is used
  635. # Note: ld.so also supports $PLATFORM and $LIB but they are
  636. # used in real case (yet)
  637. my $libdir = relative_to_pkg_root($execfile);
  638. my $origin;
  639. if (defined $libdir) {
  640. $origin = "/$libdir";
  641. $origin =~ s{/+[^/]*$}{};
  642. }
  643. my @RPATH = ();
  644. foreach my $path (@{$rpath}) {
  645. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  646. if (defined $origin) {
  647. $path =~ s/\$ORIGIN/$origin/g;
  648. $path =~ s/\$\{ORIGIN\}/$origin/g;
  649. } else {
  650. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  651. "directory could not be identified due to lack of DEBIAN " .
  652. "sub-directory in the root of package's build tree"), $execfile);
  653. }
  654. }
  655. push @RPATH, $path;
  656. }
  657. # Look into the packages we're currently building in the following
  658. # order:
  659. # - package build tree of the binary which is analyzed
  660. # - package build tree given on the command line (option -S)
  661. # - other package build trees that contain either a shlibs or a
  662. # symbols file
  663. my @builddirs;
  664. my $pkg_root = guess_pkg_root_dir($execfile);
  665. push @builddirs, $pkg_root if defined $pkg_root;
  666. push @builddirs, @pkg_dir_to_search;
  667. push @builddirs, @pkg_root_dirs;
  668. my %dir_checked;
  669. foreach my $builddir (@builddirs) {
  670. next if defined($dir_checked{$builddir});
  671. $file = find_library($lib, \@RPATH, $format, $builddir);
  672. return $file if defined($file);
  673. $dir_checked{$builddir} = 1;
  674. }
  675. # Fallback in the root directory if we have not found what we were
  676. # looking for in the packages
  677. $file = find_library($lib, \@RPATH, $format, "");
  678. return $file if defined($file);
  679. return undef;
  680. }
  681. my %cached_pkgmatch = ();
  682. sub find_packages {
  683. my @files;
  684. my $pkgmatch = {};
  685. foreach (@_) {
  686. if (exists $cached_pkgmatch{$_}) {
  687. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  688. } else {
  689. push @files, $_;
  690. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  691. $pkgmatch->{$_} = [""]; # might be replaced later on
  692. }
  693. }
  694. return $pkgmatch unless scalar(@files);
  695. my $pid = open(DPKG, "-|");
  696. syserr(_g("cannot fork for dpkg --search")) unless defined($pid);
  697. if (!$pid) {
  698. # Child process running dpkg --search and discarding errors
  699. close STDERR;
  700. open STDERR, ">", "/dev/null";
  701. $ENV{LC_ALL} = "C";
  702. exec("dpkg", "--search", "--", @files)
  703. || syserr(_g("cannot exec dpkg"));
  704. }
  705. while(defined($_ = <DPKG>)) {
  706. chomp($_);
  707. if (m/^local diversion |^diversion by/) {
  708. warning(_g("diversions involved - output may be incorrect"));
  709. print(STDERR " $_\n")
  710. || syserr(_g("write diversion info to stderr"));
  711. } elsif (m/^([^:]+): (\S+)$/) {
  712. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  713. } else {
  714. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  715. }
  716. }
  717. close(DPKG);
  718. return $pkgmatch;
  719. }