dpkg-shlibdeps.pl 12 KB

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