dpkg-shlibdeps.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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(vercmp);
  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. # By increasing importance
  19. my @depfields = qw(Suggests Recommends Depends Pre-Depends);
  20. my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
  21. textdomain("dpkg-dev");
  22. my $shlibsoverride = '/etc/dpkg/shlibs.override';
  23. my $shlibsdefault = '/etc/dpkg/shlibs.default';
  24. my $shlibslocal = 'debian/shlibs.local';
  25. my $packagetype = 'deb';
  26. my $dependencyfield = 'Depends';
  27. my $varlistfile = 'debian/substvars';
  28. my $varnameprefix = 'shlibs';
  29. my $ignore_missing_info = 0;
  30. my $debug = 0;
  31. my @exclude = ();
  32. my $host_arch = get_host_arch();
  33. my (@pkg_shlibs, @pkg_symbols);
  34. if (-d "debian") {
  35. push @pkg_symbols, <debian/*/DEBIAN/symbols>;
  36. push @pkg_shlibs, <debian/*/DEBIAN/shlibs>;
  37. }
  38. my ($stdout, %exec);
  39. foreach (@ARGV) {
  40. if (m/^-T(.*)$/) {
  41. $varlistfile = $1;
  42. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  43. $varnameprefix = $1;
  44. } elsif (m/^-L(.*)$/) {
  45. $shlibslocal = $1;
  46. } elsif (m/^-O$/) {
  47. $stdout = 1;
  48. } elsif (m/^-(h|-help)$/) {
  49. usage(); exit(0);
  50. } elsif (m/^--version$/) {
  51. version(); exit(0);
  52. } elsif (m/^--admindir=(.*)$/) {
  53. $admindir = $1;
  54. -d $admindir ||
  55. error(_g("administrative directory '%s' does not exist"), $admindir);
  56. } elsif (m/^-d(.*)$/) {
  57. $dependencyfield = capit($1);
  58. defined($depstrength{$dependencyfield}) ||
  59. warning(_g("unrecognised dependency field \`%s'"), $dependencyfield);
  60. } elsif (m/^-e(.*)$/) {
  61. $exec{$1} = $dependencyfield;
  62. } elsif (m/^--ignore-missing-info$/) {
  63. $ignore_missing_info = 1;
  64. } elsif (m/^-t(.*)$/) {
  65. $packagetype = $1;
  66. } elsif (m/^-v$/) {
  67. $debug = 1;
  68. } elsif (m/^-x(.*)$/) {
  69. push @exclude, $1;
  70. } elsif (m/^-/) {
  71. usageerr(_g("unknown option \`%s'"), $_);
  72. } else {
  73. $exec{$_} = $dependencyfield;
  74. }
  75. }
  76. scalar keys %exec || usageerr(_g("need at least one executable"));
  77. my %dependencies;
  78. my %shlibs;
  79. my $cur_field;
  80. foreach my $file (keys %exec) {
  81. $cur_field = $exec{$file};
  82. print "Scanning $file (for $cur_field field)\n" if $debug;
  83. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  84. my @sonames = $obj->get_needed_libraries;
  85. # Load symbols files for all needed libraries (identified by SONAME)
  86. my %libfiles;
  87. my %altlibfiles;
  88. my %soname_notfound;
  89. foreach my $soname (@sonames) {
  90. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  91. unless (defined $lib) {
  92. $soname_notfound{$soname} = 1;
  93. my $msg = _g("couldn't find library %s needed by %s (its RPATH is '%s').\n" .
  94. "Note: libraries are not searched in other binary packages " .
  95. "that do not have any shlibs file.\nTo help dpkg-shlibdeps " .
  96. "find private libraries, you might need to set LD_LIBRARY_PATH.");
  97. if (scalar(split_soname($soname))) {
  98. failure($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  99. } else {
  100. warning($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  101. }
  102. next;
  103. }
  104. $libfiles{$lib} = $soname;
  105. my $reallib = realpath($lib);
  106. if ($reallib ne $lib) {
  107. $altlibfiles{$reallib} = $soname;
  108. }
  109. print "Library $soname found in $lib\n" if $debug;
  110. }
  111. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  112. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  113. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  114. my @soname_wo_symfile;
  115. foreach my $lib (keys %libfiles) {
  116. my $soname = $libfiles{$lib};
  117. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  118. # The path of the library as calculated is not the
  119. # official path of a packaged file, try to fallback on
  120. # on the realpath() first, maybe this one is part of a package
  121. my $reallib = realpath($lib);
  122. if (exists $file2pkg->{$reallib}) {
  123. $file2pkg->{$lib} = $file2pkg->{$reallib};
  124. }
  125. }
  126. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  127. # If the library is really not available in an installed package,
  128. # it's because it's in the process of being built
  129. # Empty package name will lead to consideration of symbols
  130. # file from the package being built only
  131. $file2pkg->{$lib} = [""];
  132. print "No associated package found for $lib\n" if $debug;
  133. }
  134. # Load symbols/shlibs files from packages providing libraries
  135. foreach my $pkg (@{$file2pkg->{$lib}}) {
  136. my $dpkg_symfile;
  137. if ($packagetype eq "deb") {
  138. # Use fine-grained dependencies only on real deb
  139. $dpkg_symfile = find_symbols_file($pkg, $soname);
  140. if (defined $dpkg_symfile) {
  141. # Load symbol information
  142. print "Using symbols file $dpkg_symfile for $soname\n" if $debug;
  143. $symfile->load($dpkg_symfile);
  144. }
  145. }
  146. if (defined($dpkg_symfile) && $symfile->has_object($soname)) {
  147. # Initialize dependencies as an unversioned dependency
  148. my $dep = $symfile->get_dependency($soname);
  149. foreach my $subdep (split /\s*,\s*/, $dep) {
  150. if (not exists $dependencies{$cur_field}{$subdep}) {
  151. $dependencies{$cur_field}{$subdep} = '';
  152. }
  153. }
  154. } else {
  155. # No symbol file found, fall back to standard shlibs
  156. my $id = $dumplibs_wo_symfile->parse($lib);
  157. push @soname_wo_symfile, $soname;
  158. my $libobj = $dumplibs_wo_symfile->get_object($id);
  159. # Only try to generate a dependency for libraries with a SONAME
  160. if ($libobj->is_public_library() and not add_shlibs_dep($soname, $pkg)) {
  161. # This failure is fairly new, try to be kind by
  162. # ignoring as many cases that can be safely ignored
  163. my $ignore = 0;
  164. # 1/ when the lib and the binary are in the same
  165. # package
  166. my $root_file = guess_pkg_root_dir($file);
  167. my $root_lib = guess_pkg_root_dir($lib);
  168. $ignore++ if defined $root_file and defined $root_lib
  169. and check_files_are_the_same($root_file, $root_lib);
  170. # 2/ when the lib is not versioned and can't be
  171. # handled by shlibs
  172. $ignore++ unless scalar(split_soname($soname));
  173. # 3/ when we have been asked to do so
  174. $ignore++ if $ignore_missing_info;
  175. failure(_g("no dependency information found for %s " .
  176. "(used by %s)."), $lib, $file)
  177. unless $ignore;
  178. }
  179. }
  180. }
  181. }
  182. # Scan all undefined symbols of the binary and resolve to a
  183. # dependency
  184. my %used_sonames = map { $_ => 0 } @sonames;
  185. my $nb_warnings = 0;
  186. my $nb_skipped_warnings = 0;
  187. # Disable warnings about missing symbols when we have not been able to
  188. # find all libs
  189. my $disable_warnings = scalar(keys(%soname_notfound));
  190. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  191. my $name = $sym->{name};
  192. if ($sym->{version}) {
  193. $name .= "\@$sym->{version}";
  194. } else {
  195. $name .= "\@Base";
  196. }
  197. my $symdep = $symfile->lookup_symbol($name, \@sonames);
  198. if (defined($symdep)) {
  199. my ($d, $m) = ($symdep->{depends}, $symdep->{minver});
  200. $used_sonames{$symdep->{soname}}++;
  201. foreach my $subdep (split /\s*,\s*/, $d) {
  202. if (exists $dependencies{$cur_field}{$subdep} and
  203. defined($dependencies{$cur_field}{$subdep}))
  204. {
  205. if ($dependencies{$cur_field}{$subdep} eq '' or
  206. vercmp($m, $dependencies{$cur_field}{$subdep}) > 0)
  207. {
  208. $dependencies{$cur_field}{$subdep} = $m;
  209. }
  210. } else {
  211. $dependencies{$cur_field}{$subdep} = $m;
  212. }
  213. }
  214. } else {
  215. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  216. if (not defined($syminfo)) {
  217. next if $disable_warnings;
  218. # Complain about missing symbols only for executables
  219. # and public libraries
  220. if ($obj->is_executable() or $obj->is_public_library()) {
  221. my $print_name = $name;
  222. # Drop the default suffix for readability
  223. $print_name =~ s/\@Base$//;
  224. unless ($sym->{weak}) {
  225. if ($debug or $nb_warnings < 10) {
  226. warning(_g("symbol %s used by %s found in none of the " .
  227. "libraries."), $print_name, $file);
  228. $nb_warnings++;
  229. } else {
  230. $nb_skipped_warnings++;
  231. }
  232. }
  233. }
  234. } else {
  235. $used_sonames{$syminfo->{soname}}++;
  236. }
  237. }
  238. }
  239. warning(_g("%d other similar warnings have been skipped (use -v to see " .
  240. "them all)."), $nb_skipped_warnings) if $nb_skipped_warnings;
  241. # Warn about un-NEEDED libraries
  242. foreach my $soname (@sonames) {
  243. unless ($soname_notfound{$soname} or $used_sonames{$soname}) {
  244. warning(_g("%s shouldn't be linked with %s (it uses none of its " .
  245. "symbols)."), $file, $soname);
  246. }
  247. }
  248. }
  249. # Open substvars file
  250. my $fh;
  251. if ($stdout) {
  252. $fh = \*STDOUT;
  253. } else {
  254. open(NEW, ">", "$varlistfile.new") ||
  255. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  256. if (-e $varlistfile) {
  257. open(OLD, "<", $varlistfile) ||
  258. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  259. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  260. print(NEW $entry) ||
  261. syserr(_g("copy old entry to new varlist file \`%s'"),
  262. "$varlistfile.new");
  263. }
  264. close(OLD);
  265. }
  266. $fh = \*NEW;
  267. }
  268. # Write out the shlibs substvars
  269. my %depseen;
  270. sub filter_deps {
  271. my ($dep, $field) = @_;
  272. # Skip dependencies on excluded packages
  273. foreach my $exc (@exclude) {
  274. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  275. }
  276. # Don't include dependencies if they are already
  277. # mentionned in a higher priority field
  278. if (not defined($depseen{$dep})) {
  279. $depseen{$dep} = $dependencies{$field}{$dep};
  280. return 1;
  281. } else {
  282. # Since dependencies can be versionned, we have to
  283. # verify if the dependency is stronger than the
  284. # previously seen one
  285. if (vercmp($depseen{$dep}, $dependencies{$field}{$dep}) > 0) {
  286. return 0;
  287. } else {
  288. $depseen{$dep} = $dependencies{$field}{$dep};
  289. return 1;
  290. }
  291. }
  292. }
  293. foreach my $field (reverse @depfields) {
  294. my $dep = "";
  295. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  296. $dep = join ", ",
  297. map {
  298. # Translate dependency templates into real dependencies
  299. if ($dependencies{$field}{$_}) {
  300. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  301. } else {
  302. s/#MINVER#//g;
  303. }
  304. s/\s+/ /g;
  305. $_;
  306. } grep { filter_deps($_, $field) }
  307. keys %{$dependencies{$field}};
  308. }
  309. if ($dep) {
  310. print $fh "$varnameprefix:$field=$dep\n";
  311. }
  312. }
  313. # Replace old file by new one
  314. if (!$stdout) {
  315. close($fh);
  316. rename("$varlistfile.new",$varlistfile) ||
  317. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  318. }
  319. ##
  320. ## Functions
  321. ##
  322. sub version {
  323. printf _g("Debian %s version %s.\n"), $progname, $version;
  324. printf _g("
  325. Copyright (C) 1996 Ian Jackson.
  326. Copyright (C) 2000 Wichert Akkerman.
  327. Copyright (C) 2006 Frank Lichtenheld.
  328. Copyright (C) 2007 Raphael Hertzog.
  329. ");
  330. printf _g("
  331. This is free software; see the GNU General Public Licence version 2 or
  332. later for copying conditions. There is NO warranty.
  333. ");
  334. }
  335. sub usage {
  336. printf _g(
  337. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  338. Positional options (order is significant):
  339. <executable> include dependencies for <executable>,
  340. -e<executable> (use -e if <executable> starts with \`-')
  341. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  342. Options:
  343. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  344. -O print variable settings to stdout.
  345. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  346. -T<varlistfile> update variables here, not debian/substvars.
  347. -t<type> set package type (default is deb).
  348. -x<package> exclude package from the generated dependencies.
  349. --admindir=<directory> change the administrative directory.
  350. -h, --help show this help message.
  351. --version show the version.
  352. Dependency fields recognised are:
  353. %s
  354. "), $progname, join("/",@depfields);
  355. }
  356. sub add_shlibs_dep {
  357. my ($soname, $pkg) = @_;
  358. print "Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  359. foreach my $file ($shlibslocal, $shlibsoverride, @pkg_shlibs,
  360. "$admindir/info/$pkg.shlibs",
  361. $shlibsdefault)
  362. {
  363. next if not -e $file;
  364. my $dep = extract_from_shlibs($soname, $file);
  365. if (defined($dep)) {
  366. print "Found $dep in $file\n" if $debug;
  367. foreach (split(/,\s*/, $dep)) {
  368. $dependencies{$cur_field}{$_} = 1;
  369. }
  370. return 1;
  371. }
  372. }
  373. print "Found nothing\n" if $debug;
  374. return 0;
  375. }
  376. sub split_soname {
  377. my $soname = shift;
  378. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  379. return wantarray ? ($1, $2) : 1;
  380. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  381. return wantarray ? ($1, $2) : 1;
  382. } else {
  383. return wantarray ? () : 0;
  384. }
  385. }
  386. sub extract_from_shlibs {
  387. my ($soname, $shlibfile) = @_;
  388. # Split soname in name/version
  389. my ($libname, $libversion) = split_soname($soname);
  390. unless (defined $libname) {
  391. warning(_g("Can't extract name and version from library name \`%s'"),
  392. $soname);
  393. return;
  394. }
  395. # Open shlibs file
  396. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  397. open(SHLIBS, "<", $shlibfile) ||
  398. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  399. my $dep;
  400. while (<SHLIBS>) {
  401. s/\s*\n$//;
  402. next if m/^\#/;
  403. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  404. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  405. $shlibfile, $., $_);
  406. next;
  407. }
  408. my $depread = defined($4) ? $4 : '';
  409. if (($libname eq $2) && ($libversion eq $3)) {
  410. # Define dep and end here if the package type explicitely
  411. # matches. Otherwise if the packagetype is not specified, use
  412. # the dep only as a default that can be overriden by a later
  413. # line
  414. if (defined($1)) {
  415. if ($1 eq $packagetype) {
  416. $dep = $depread;
  417. last;
  418. }
  419. } else {
  420. $dep = $depread unless defined $dep;
  421. }
  422. }
  423. }
  424. close(SHLIBS);
  425. return $dep;
  426. }
  427. sub find_symbols_file {
  428. my ($pkg, $soname) = @_;
  429. foreach my $file (@pkg_symbols,
  430. "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  431. "/etc/dpkg/symbols/$pkg.symbols",
  432. "$admindir/info/$pkg.symbols")
  433. {
  434. if (-e $file and symfile_has_soname($file, $soname)) {
  435. return $file;
  436. }
  437. }
  438. return undef;
  439. }
  440. sub symfile_has_soname {
  441. my ($file, $soname) = @_;
  442. open(SYM_FILE, "<", $file) ||
  443. syserr(_g("cannot open file %s"), $file);
  444. my $result = 0;
  445. while (<SYM_FILE>) {
  446. if (/^\Q$soname\E /) {
  447. $result = 1;
  448. last;
  449. }
  450. }
  451. close(SYM_FILE);
  452. return $result;
  453. }
  454. # find_library ($soname, \@rpath, $format)
  455. sub my_find_library {
  456. my ($lib, $rpath, $format, $execfile) = @_;
  457. my $file;
  458. # Create real RPATH in case $ORIGIN is used
  459. # Note: ld.so also supports $PLATFORM and $LIB but they are
  460. # used in real case (yet)
  461. my $libdir = relative_to_pkg_root($execfile);
  462. my $origin;
  463. if (defined $libdir) {
  464. $origin = "/$libdir";
  465. $origin =~ s{/+[^/]*$}{};
  466. }
  467. my @RPATH = ();
  468. foreach my $path (@{$rpath}) {
  469. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  470. if (defined $origin) {
  471. $path =~ s/\$ORIGIN/$origin/g;
  472. $path =~ s/\$\{ORIGIN\}/$origin/g;
  473. } else {
  474. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  475. "directory could not be identified due to lack of DEBIAN " .
  476. "sub-directory in the root of package's build tree"), $execfile);
  477. }
  478. }
  479. push @RPATH, $path;
  480. }
  481. # Look into the packages we're currently building (but only those
  482. # that provides shlibs file and the one that contains the binary being
  483. # anlyzed...)
  484. # TODO: we should probably replace that by a cleaner way to look into
  485. # the various temporary build directories...
  486. my @copy = (@pkg_shlibs);
  487. my $pkg_root = guess_pkg_root_dir($execfile);
  488. unshift @copy, $pkg_root if defined $pkg_root;
  489. foreach my $builddir (map { s{/DEBIAN/shlibs$}{}; $_ } @copy) {
  490. $file = find_library($lib, \@RPATH, $format, $builddir);
  491. return $file if defined($file);
  492. }
  493. # Fallback in the root directory if we have not found what we were
  494. # looking for in the packages
  495. $file = find_library($lib, \@RPATH, $format, "");
  496. return $file if defined($file);
  497. return undef;
  498. }
  499. my %cached_pkgmatch = ();
  500. sub find_packages {
  501. my @files;
  502. my $pkgmatch = {};
  503. foreach (@_) {
  504. if (exists $cached_pkgmatch{$_}) {
  505. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  506. } else {
  507. push @files, $_;
  508. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  509. $pkgmatch->{$_} = [""]; # might be replaced later on
  510. }
  511. }
  512. return $pkgmatch unless scalar(@files);
  513. my $pid = open(DPKG, "-|");
  514. syserr(_g("cannot fork for dpkg --search")) unless defined($pid);
  515. if (!$pid) {
  516. # Child process running dpkg --search and discarding errors
  517. close STDERR;
  518. open STDERR, ">", "/dev/null";
  519. $ENV{LC_ALL} = "C";
  520. exec("dpkg", "--search", "--", @files)
  521. || syserr(_g("cannot exec dpkg"));
  522. }
  523. while(defined($_ = <DPKG>)) {
  524. chomp($_);
  525. if (m/^local diversion |^diversion by/) {
  526. warning(_g("diversions involved - output may be incorrect"));
  527. print(STDERR " $_\n")
  528. || syserr(_g("write diversion info to stderr"));
  529. } elsif (m/^([^:]+): (\S+)$/) {
  530. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  531. } else {
  532. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  533. }
  534. }
  535. close(DPKG);
  536. return $pkgmatch;
  537. }