dpkg-shlibdeps.pl 14 KB

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