dpkg-shlibdeps.pl 14 KB

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