dpkg-shlibdeps.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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::Path qw(relative_to_pkg_root);
  9. use Dpkg::Version qw(vercmp);
  10. use Dpkg::Shlibs qw(find_library);
  11. use Dpkg::Shlibs::Objdump;
  12. use Dpkg::Shlibs::SymbolFile;
  13. our $host_arch= `dpkg-architecture -qDEB_HOST_ARCH`;
  14. chomp $host_arch;
  15. # By increasing importance
  16. my @depfields= qw(Suggests Recommends Depends Pre-Depends);
  17. my $i=0; my %depstrength = map { $_ => $i++ } @depfields;
  18. push(@INC, $dpkglibdir);
  19. require 'controllib.pl';
  20. textdomain("dpkg-dev");
  21. my $shlibsoverride= '/etc/dpkg/shlibs.override';
  22. my $shlibsdefault= '/etc/dpkg/shlibs.default';
  23. my $shlibslocal= 'debian/shlibs.local';
  24. my $packagetype= 'deb';
  25. my $dependencyfield= 'Depends';
  26. my $varlistfile= 'debian/substvars';
  27. my $varnameprefix= 'shlibs';
  28. my $ignore_missing_info= 0;
  29. my $debug= 0;
  30. my @exclude = ();
  31. my (@pkg_shlibs, @pkg_symbols);
  32. if (-d "debian") {
  33. push @pkg_symbols, <debian/*/DEBIAN/symbols>;
  34. push @pkg_shlibs, <debian/*/DEBIAN/shlibs>;
  35. }
  36. my ($stdout, %exec);
  37. foreach (@ARGV) {
  38. if (m/^-T(.*)$/) {
  39. $varlistfile= $1;
  40. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  41. $varnameprefix= $1;
  42. } elsif (m/^-L(.*)$/) {
  43. $shlibslocal= $1;
  44. } elsif (m/^-O$/) {
  45. $stdout= 1;
  46. } elsif (m/^-(h|-help)$/) {
  47. usage(); exit(0);
  48. } elsif (m/^--version$/) {
  49. version(); exit(0);
  50. } elsif (m/^--admindir=(.*)$/) {
  51. $admindir = $1;
  52. -d $admindir ||
  53. error(sprintf(_g("administrative directory '%s' does not exist"),
  54. $admindir));
  55. } elsif (m/^-d(.*)$/) {
  56. $dependencyfield= capit($1);
  57. defined($depstrength{$dependencyfield}) ||
  58. warning(sprintf(_g("unrecognised dependency field \`%s'"), $dependencyfield));
  59. } elsif (m/^-e(.*)$/) {
  60. $exec{$1} = $dependencyfield;
  61. } elsif (m/^--ignore-missing-info$/) {
  62. $ignore_missing_info = 1;
  63. } elsif (m/^-t(.*)$/) {
  64. $packagetype = $1;
  65. } elsif (m/^-v$/) {
  66. $debug = 1;
  67. } elsif (m/^-x(.*)$/) {
  68. push @exclude, $1;
  69. } elsif (m/^-/) {
  70. usageerr(sprintf(_g("unknown option \`%s'"), $_));
  71. } else {
  72. $exec{$_} = $dependencyfield;
  73. }
  74. }
  75. scalar keys %exec || usageerr(_g("need at least one executable"));
  76. my %dependencies;
  77. my %shlibs;
  78. my $cur_field;
  79. foreach my $file (keys %exec) {
  80. $cur_field = $exec{$file};
  81. print "Scanning $file (for $cur_field field)\n" if $debug;
  82. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  83. my @sonames = $obj->get_needed_libraries;
  84. # Load symbols files for all needed libraries (identified by SONAME)
  85. my %libfiles;
  86. foreach my $soname (@sonames) {
  87. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  88. failure(sprintf(_g("couldn't find library %s (note: only packages with 'shlibs' files are looked into)."), $soname)) 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(sprintf(
  132. _g("No dependency information found for %s (used by %s)."),
  133. $soname, $file)) 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(sprintf(
  175. _g("symbol %s used by %s found in none of the libraries."),
  176. $print_name, $file)) 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(sprintf(
  187. _g("%s shouldn't be linked with %s (it uses none of its symbols)."),
  188. $file, $soname));
  189. }
  190. }
  191. }
  192. # Open substvars file
  193. my $fh;
  194. if ($stdout) {
  195. $fh = \*STDOUT;
  196. } else {
  197. open(NEW, ">", "$varlistfile.new") ||
  198. syserr(sprintf(_g("open new substvars file \`%s'"), "$varlistfile.new"));
  199. if (-e $varlistfile) {
  200. open(OLD, "<", $varlistfile) ||
  201. syserr(sprintf(_g("open old varlist file \`%s' for reading"), $varlistfile));
  202. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  203. print(NEW $entry) ||
  204. syserr(sprintf(_g("copy old entry to new varlist file \`%s'"), "$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(sprintf(_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(sprintf(_g("Can't extract name and version from library name \`%s'"), $soname));
  325. return;
  326. }
  327. # Open shlibs file
  328. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  329. open(SHLIBS, "<", $shlibfile)
  330. || syserr(sprintf(_g("unable to open shared libs info file \`%s'"), $shlibfile));
  331. my $dep;
  332. while (<SHLIBS>) {
  333. s/\s*\n$//;
  334. next if m/^\#/;
  335. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)\s+(\S.*\S)\s*$/) {
  336. warning(sprintf(_g("shared libs info file \`%s' line %d: bad line \`%s'"), $shlibfile, $., $_));
  337. next;
  338. }
  339. if (($libname eq $2) && ($libversion eq $3)) {
  340. # Define dep and end here if the package type explicitely
  341. # matches. Otherwise if the packagetype is not specified, use
  342. # the dep only as a default that can be overriden by a later
  343. # line
  344. if (defined($1)) {
  345. if ($1 eq $packagetype) {
  346. $dep = $4;
  347. last;
  348. }
  349. } else {
  350. $dep = $4 unless defined $dep;
  351. }
  352. }
  353. }
  354. close(SHLIBS);
  355. return $dep;
  356. }
  357. sub find_symbols_file {
  358. my ($pkg, $soname) = @_;
  359. foreach my $file (@pkg_symbols,
  360. "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  361. "/etc/dpkg/symbols/$pkg.symbols",
  362. "$admindir/info/$pkg.symbols")
  363. {
  364. if (-e $file and symfile_has_soname($file, $soname)) {
  365. return $file;
  366. }
  367. }
  368. return undef;
  369. }
  370. sub symfile_has_soname {
  371. my ($file, $soname) = @_;
  372. open(SYM_FILE, "<", $file)
  373. || syserr(sprintf(_g("cannot open file %s"), $file));
  374. my $result = 0;
  375. while (<SYM_FILE>) {
  376. if (/^\Q$soname\E /) {
  377. $result = 1;
  378. last;
  379. }
  380. }
  381. close(SYM_FILE);
  382. return $result;
  383. }
  384. # find_library ($soname, \@rpath, $format)
  385. sub my_find_library {
  386. my ($lib, $rpath, $format, $execfile) = @_;
  387. my $file;
  388. # Create real RPATH in case $ORIGIN is used
  389. # Note: ld.so also supports $PLATFORM and $LIB but they are
  390. # used in real case (yet)
  391. my $origin = "/" . relative_to_pkg_root($execfile);
  392. $origin =~ s{/+[^/]*$}{};
  393. my @RPATH = ();
  394. foreach my $path (@{$rpath}) {
  395. $path =~ s/\$ORIGIN/$origin/g;
  396. $path =~ s/\$\{ORIGIN\}/$origin/g;
  397. push @RPATH, $path;
  398. }
  399. # Look into the packages we're currently building (but only those
  400. # that provides shlibs file...)
  401. # TODO: we should probably replace that by a cleaner way to look into
  402. # the various temporary build directories...
  403. foreach my $builddir (map { s{/DEBIAN/shlibs$}{}; $_ } @pkg_shlibs) {
  404. $file = find_library($lib, \@RPATH, $format, $builddir);
  405. return $file if defined($file);
  406. }
  407. # Fallback in the root directory if we have not found what we were
  408. # looking for in the packages
  409. $file = find_library($lib, \@RPATH, $format, "");
  410. return $file if defined($file);
  411. return undef;
  412. }
  413. sub find_packages {
  414. my @files = (@_);
  415. my $pkgmatch = {};
  416. my $pid = open(DPKG, "-|");
  417. syserr(_g("cannot fork for dpkg --search")) unless defined($pid);
  418. if (!$pid) {
  419. # Child process running dpkg --search and discarding errors
  420. close STDERR;
  421. open STDERR, ">", "/dev/null";
  422. $ENV{LC_ALL} = "C";
  423. exec("dpkg", "--search", "--", @files)
  424. || syserr(_g("cannot exec dpkg"));
  425. }
  426. while(defined($_ = <DPKG>)) {
  427. chomp($_);
  428. if (m/^local diversion |^diversion by/) {
  429. warning(_g("diversions involved - output may be incorrect"));
  430. print(STDERR " $_\n")
  431. || syserr(_g("write diversion info to stderr"));
  432. } elsif (m/^([^:]+): (\S+)$/) {
  433. $pkgmatch->{$2} = [ split(/, /, $1) ];
  434. } else {
  435. warning(sprintf(_g("unknown output from dpkg --search: '%s'"), $_));
  436. }
  437. }
  438. close(DPKG);
  439. return $pkgmatch;
  440. }