dpkg-shlibdeps.pl 12 KB

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