dpkg-shlibdeps.pl 12 KB

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