dpkg-shlibdeps.pl 16 KB

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