dpkg-shlibdeps.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. # Ignore warning for libm.so.6 if also linked against libstdc++
  245. next if ($soname =~ /^libm\.so\.\d+$/ and
  246. scalar grep(/^libstdc\+\+\.so\.\d+/, @sonames));
  247. warning(_g("%s shouldn't be linked with %s (it uses none of its " .
  248. "symbols)."), $file, $soname);
  249. }
  250. }
  251. }
  252. # Open substvars file
  253. my $fh;
  254. if ($stdout) {
  255. $fh = \*STDOUT;
  256. } else {
  257. open(NEW, ">", "$varlistfile.new") ||
  258. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  259. if (-e $varlistfile) {
  260. open(OLD, "<", $varlistfile) ||
  261. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  262. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  263. print(NEW $entry) ||
  264. syserr(_g("copy old entry to new varlist file \`%s'"),
  265. "$varlistfile.new");
  266. }
  267. close(OLD);
  268. }
  269. $fh = \*NEW;
  270. }
  271. # Write out the shlibs substvars
  272. my %depseen;
  273. sub filter_deps {
  274. my ($dep, $field) = @_;
  275. # Skip dependencies on excluded packages
  276. foreach my $exc (@exclude) {
  277. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  278. }
  279. # Don't include dependencies if they are already
  280. # mentionned in a higher priority field
  281. if (not defined($depseen{$dep})) {
  282. $depseen{$dep} = $dependencies{$field}{$dep};
  283. return 1;
  284. } else {
  285. # Since dependencies can be versionned, we have to
  286. # verify if the dependency is stronger than the
  287. # previously seen one
  288. if (vercmp($depseen{$dep}, $dependencies{$field}{$dep}) > 0) {
  289. return 0;
  290. } else {
  291. $depseen{$dep} = $dependencies{$field}{$dep};
  292. return 1;
  293. }
  294. }
  295. }
  296. foreach my $field (reverse @depfields) {
  297. my $dep = "";
  298. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  299. $dep = join ", ",
  300. map {
  301. # Translate dependency templates into real dependencies
  302. if ($dependencies{$field}{$_}) {
  303. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  304. } else {
  305. s/#MINVER#//g;
  306. }
  307. s/\s+/ /g;
  308. $_;
  309. } grep { filter_deps($_, $field) }
  310. keys %{$dependencies{$field}};
  311. }
  312. if ($dep) {
  313. print $fh "$varnameprefix:$field=$dep\n";
  314. }
  315. }
  316. # Replace old file by new one
  317. if (!$stdout) {
  318. close($fh);
  319. rename("$varlistfile.new",$varlistfile) ||
  320. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  321. }
  322. ##
  323. ## Functions
  324. ##
  325. sub version {
  326. printf _g("Debian %s version %s.\n"), $progname, $version;
  327. printf _g("
  328. Copyright (C) 1996 Ian Jackson.
  329. Copyright (C) 2000 Wichert Akkerman.
  330. Copyright (C) 2006 Frank Lichtenheld.
  331. Copyright (C) 2007 Raphael Hertzog.
  332. ");
  333. printf _g("
  334. This is free software; see the GNU General Public Licence version 2 or
  335. later for copying conditions. There is NO warranty.
  336. ");
  337. }
  338. sub usage {
  339. printf _g(
  340. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  341. Positional options (order is significant):
  342. <executable> include dependencies for <executable>,
  343. -e<executable> (use -e if <executable> starts with \`-')
  344. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  345. Options:
  346. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  347. -O print variable settings to stdout.
  348. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  349. -T<varlistfile> update variables here, not debian/substvars.
  350. -t<type> set package type (default is deb).
  351. -x<package> exclude package from the generated dependencies.
  352. --admindir=<directory> change the administrative directory.
  353. -h, --help show this help message.
  354. --version show the version.
  355. Dependency fields recognised are:
  356. %s
  357. "), $progname, join("/",@depfields);
  358. }
  359. sub add_shlibs_dep {
  360. my ($soname, $pkg) = @_;
  361. print "Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  362. foreach my $file ($shlibslocal, $shlibsoverride, @pkg_shlibs,
  363. "$admindir/info/$pkg.shlibs",
  364. $shlibsdefault)
  365. {
  366. next if not -e $file;
  367. my $dep = extract_from_shlibs($soname, $file);
  368. if (defined($dep)) {
  369. print "Found $dep in $file\n" if $debug;
  370. foreach (split(/,\s*/, $dep)) {
  371. $dependencies{$cur_field}{$_} = 1;
  372. }
  373. return 1;
  374. }
  375. }
  376. print "Found nothing\n" if $debug;
  377. return 0;
  378. }
  379. sub split_soname {
  380. my $soname = shift;
  381. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  382. return wantarray ? ($1, $2) : 1;
  383. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  384. return wantarray ? ($1, $2) : 1;
  385. } else {
  386. return wantarray ? () : 0;
  387. }
  388. }
  389. sub extract_from_shlibs {
  390. my ($soname, $shlibfile) = @_;
  391. # Split soname in name/version
  392. my ($libname, $libversion) = split_soname($soname);
  393. unless (defined $libname) {
  394. warning(_g("Can't extract name and version from library name \`%s'"),
  395. $soname);
  396. return;
  397. }
  398. # Open shlibs file
  399. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  400. open(SHLIBS, "<", $shlibfile) ||
  401. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  402. my $dep;
  403. while (<SHLIBS>) {
  404. s/\s*\n$//;
  405. next if m/^\#/;
  406. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  407. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  408. $shlibfile, $., $_);
  409. next;
  410. }
  411. my $depread = defined($4) ? $4 : '';
  412. if (($libname eq $2) && ($libversion eq $3)) {
  413. # Define dep and end here if the package type explicitely
  414. # matches. Otherwise if the packagetype is not specified, use
  415. # the dep only as a default that can be overriden by a later
  416. # line
  417. if (defined($1)) {
  418. if ($1 eq $packagetype) {
  419. $dep = $depread;
  420. last;
  421. }
  422. } else {
  423. $dep = $depread unless defined $dep;
  424. }
  425. }
  426. }
  427. close(SHLIBS);
  428. return $dep;
  429. }
  430. sub find_symbols_file {
  431. my ($pkg, $soname) = @_;
  432. foreach my $file (@pkg_symbols,
  433. "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  434. "/etc/dpkg/symbols/$pkg.symbols",
  435. "$admindir/info/$pkg.symbols")
  436. {
  437. if (-e $file and symfile_has_soname($file, $soname)) {
  438. return $file;
  439. }
  440. }
  441. return undef;
  442. }
  443. sub symfile_has_soname {
  444. my ($file, $soname) = @_;
  445. open(SYM_FILE, "<", $file) ||
  446. syserr(_g("cannot open file %s"), $file);
  447. my $result = 0;
  448. while (<SYM_FILE>) {
  449. if (/^\Q$soname\E /) {
  450. $result = 1;
  451. last;
  452. }
  453. }
  454. close(SYM_FILE);
  455. return $result;
  456. }
  457. # find_library ($soname, \@rpath, $format)
  458. sub my_find_library {
  459. my ($lib, $rpath, $format, $execfile) = @_;
  460. my $file;
  461. # Create real RPATH in case $ORIGIN is used
  462. # Note: ld.so also supports $PLATFORM and $LIB but they are
  463. # used in real case (yet)
  464. my $libdir = relative_to_pkg_root($execfile);
  465. my $origin;
  466. if (defined $libdir) {
  467. $origin = "/$libdir";
  468. $origin =~ s{/+[^/]*$}{};
  469. }
  470. my @RPATH = ();
  471. foreach my $path (@{$rpath}) {
  472. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  473. if (defined $origin) {
  474. $path =~ s/\$ORIGIN/$origin/g;
  475. $path =~ s/\$\{ORIGIN\}/$origin/g;
  476. } else {
  477. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  478. "directory could not be identified due to lack of DEBIAN " .
  479. "sub-directory in the root of package's build tree"), $execfile);
  480. }
  481. }
  482. push @RPATH, $path;
  483. }
  484. # Look into the packages we're currently building (but only those
  485. # that provides shlibs file and the one that contains the binary being
  486. # anlyzed...)
  487. # TODO: we should probably replace that by a cleaner way to look into
  488. # the various temporary build directories...
  489. my @copy = (@pkg_shlibs);
  490. my $pkg_root = guess_pkg_root_dir($execfile);
  491. unshift @copy, $pkg_root if defined $pkg_root;
  492. foreach my $builddir (map { s{/DEBIAN/shlibs$}{}; $_ } @copy) {
  493. $file = find_library($lib, \@RPATH, $format, $builddir);
  494. return $file if defined($file);
  495. }
  496. # Fallback in the root directory if we have not found what we were
  497. # looking for in the packages
  498. $file = find_library($lib, \@RPATH, $format, "");
  499. return $file if defined($file);
  500. return undef;
  501. }
  502. my %cached_pkgmatch = ();
  503. sub find_packages {
  504. my @files;
  505. my $pkgmatch = {};
  506. foreach (@_) {
  507. if (exists $cached_pkgmatch{$_}) {
  508. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  509. } else {
  510. push @files, $_;
  511. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  512. $pkgmatch->{$_} = [""]; # might be replaced later on
  513. }
  514. }
  515. return $pkgmatch unless scalar(@files);
  516. my $pid = open(DPKG, "-|");
  517. syserr(_g("cannot fork for dpkg --search")) unless defined($pid);
  518. if (!$pid) {
  519. # Child process running dpkg --search and discarding errors
  520. close STDERR;
  521. open STDERR, ">", "/dev/null";
  522. $ENV{LC_ALL} = "C";
  523. exec("dpkg", "--search", "--", @files)
  524. || syserr(_g("cannot exec dpkg"));
  525. }
  526. while(defined($_ = <DPKG>)) {
  527. chomp($_);
  528. if (m/^local diversion |^diversion by/) {
  529. warning(_g("diversions involved - output may be incorrect"));
  530. print(STDERR " $_\n")
  531. || syserr(_g("write diversion info to stderr"));
  532. } elsif (m/^([^:]+): (\S+)$/) {
  533. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  534. } else {
  535. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  536. }
  537. }
  538. close(DPKG);
  539. return $pkgmatch;
  540. }