dpkg-shlibdeps.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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="/usr/lib/dpkg"; # This line modified by Makefile
  12. push(@INC,$dpkglibdir);
  13. }
  14. use Dpkg::Version qw(compare_versions);
  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. my @depfields= qw(Suggests Recommends Depends Pre-Depends); # By increasing importance
  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. find sub {
  36. push @pkg_shlibs, $File::Find::name if ($File::Find::name =~ m{/DEBIAN/shlibs$});
  37. push @pkg_symbols, $File::Find::name if ($File::Find::name =~ m{/DEBIAN/symbols$});
  38. }, "debian";
  39. }
  40. my ($stdout, %exec);
  41. foreach (@ARGV) {
  42. if (m/^-T(.*)$/) {
  43. $varlistfile= $1;
  44. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  45. $varnameprefix= $1;
  46. } elsif (m/^-L(.*)$/) {
  47. $shlibslocal= $1;
  48. } elsif (m/^-O$/) {
  49. $stdout= 1;
  50. } elsif (m/^-(h|-help)$/) {
  51. usage(); exit(0);
  52. } elsif (m/^--version$/) {
  53. version(); exit(0);
  54. } elsif (m/^--admindir=(.*)$/) {
  55. $admindir = $1;
  56. -d $admindir ||
  57. error(sprintf(_g("administrative directory '%s' does not exist"),
  58. $admindir));
  59. } elsif (m/^-d(.*)$/) {
  60. $dependencyfield= capit($1);
  61. defined($depstrength{$dependencyfield}) ||
  62. warning(sprintf(_g("unrecognised dependency field \`%s'"), $dependencyfield));
  63. } elsif (m/^-e(.*)$/) {
  64. $exec{$1} = $dependencyfield;
  65. } elsif (m/^-t(.*)$/) {
  66. $packagetype = $1;
  67. } elsif (m/-v$/) {
  68. $debug = 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 $dump = Dpkg::Shlibs::Objdump->new();
  83. my $id = $dump->parse($file);
  84. my $obj = $dump->get_object($id);
  85. # Load symbols files for all needed libraries (identified by SONAME)
  86. my %libfiles;
  87. foreach my $soname ($obj->get_needed_libraries) {
  88. my $file = my_find_library($soname, $obj->{RPATH}, $obj->{format});
  89. warning("Couldn't find library $soname.") unless defined($file);
  90. $libfiles{$file} = $soname if defined($file);
  91. }
  92. my $file2pkg = find_packages(keys %libfiles);
  93. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  94. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  95. my @soname_wo_symfile;
  96. foreach my $file (keys %libfiles) {
  97. my $soname = $libfiles{$file};
  98. if (not exists $file2pkg->{$file}) {
  99. # If the library is not available in an installed package,
  100. # it's because it's in the process of being built
  101. # Empty package name will lead to consideration of symbols
  102. # file from the package being built only
  103. $file2pkg->{$file} = [""];
  104. }
  105. # Load symbols/shlibs files from packages providing libraries
  106. foreach my $pkg (@{$file2pkg->{$file}}) {
  107. my $dpkg_symfile;
  108. if ($packagetype eq "deb") {
  109. # Use fine-grained dependencies only on real deb
  110. $dpkg_symfile = find_symbols_file($pkg, $soname);
  111. }
  112. if (defined $dpkg_symfile) {
  113. # Load symbol information
  114. print "Using symbols file $dpkg_symfile for $soname\n" if $debug;
  115. $symfile->load($dpkg_symfile);
  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. $dumplibs_wo_symfile->parse($file);
  126. push @soname_wo_symfile, $soname;
  127. add_shlibs_dep($soname, $pkg);
  128. }
  129. }
  130. }
  131. # Scan all undefined symbols of the binary and resolve to a
  132. # dependency
  133. my @sonames = $obj->get_needed_libraries;
  134. my %used_sonames = map { $_ => 0 } @sonames;
  135. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  136. my $name = $sym->{name};
  137. if ($sym->{version}) {
  138. $name .= "\@$sym->{version}";
  139. } else {
  140. $name .= "\@Base";
  141. }
  142. my $symdep = $symfile->lookup_symbol($name, \@sonames);
  143. if (defined($symdep)) {
  144. my ($d, $m) = ($symdep->{depends}, $symdep->{minver});
  145. $used_sonames{$symdep->{soname}}++;
  146. foreach my $subdep (split /\s*,\s*/, $d) {
  147. if (exists $dependencies{$cur_field}{$subdep} and
  148. defined($dependencies{$cur_field}{$subdep}))
  149. {
  150. if ($dependencies{$cur_field}{$subdep} eq '' or
  151. compare_versions($m, "gt", $dependencies{$cur_field}{$subdep}))
  152. {
  153. $dependencies{$cur_field}{$subdep} = $m;
  154. }
  155. } else {
  156. $dependencies{$cur_field}{$subdep} = $m;
  157. }
  158. }
  159. } else {
  160. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  161. if (not defined($syminfo)) {
  162. my $print_name = $name;
  163. $print_name =~ s/\@Base$//; # Drop the default suffix for readability
  164. warning(sprintf(
  165. _g("symbol %s used by %s found in none of the libraries."),
  166. $print_name, $file)) unless $sym->{weak};
  167. } else {
  168. $used_sonames{$syminfo->{soname}}++;
  169. }
  170. }
  171. }
  172. # Warn about un-NEEDED libraries
  173. foreach my $soname (@sonames) {
  174. unless ($used_sonames{$soname}) {
  175. warning(sprintf(
  176. _g("%s shouldn't be linked with %s (it uses none of its symbols)."),
  177. $file, $soname));
  178. }
  179. }
  180. }
  181. # Open substvars file
  182. my $fh;
  183. if ($stdout) {
  184. $fh = \*STDOUT;
  185. } else {
  186. open(NEW,"> $varlistfile.new") ||
  187. syserr(sprintf(_g("open new substvars file \`%s'"), "$varlistfile.new"));
  188. if (-e $varlistfile) {
  189. open(OLD,"< $varlistfile") ||
  190. syserr(sprintf(_g("open old varlist file \`%s' for reading"), $varlistfile));
  191. foreach my $entry (grep { not /^\Q$varnameprefix\E:/ } (<OLD>)) {
  192. print(NEW $entry) ||
  193. syserr(sprintf(_g("copy old entry to new varlist file \`%s'"), "$varlistfile.new"));
  194. }
  195. }
  196. $fh = \*NEW;
  197. }
  198. # Write out the shlibs substvars
  199. my %depseen;
  200. foreach my $field (reverse @depfields) {
  201. my $dep = "";
  202. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  203. $dep = join ", ",
  204. map {
  205. # Translate dependency templates into real dependencies
  206. if ($dependencies{$field}{$_}) {
  207. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  208. } else {
  209. s/#MINVER#//g;
  210. }
  211. s/\s+/ /g;
  212. $_;
  213. } grep {
  214. # Don't include dependencies if they are already
  215. # mentionned in a higher priority field
  216. if (not defined($depseen{$_})) {
  217. $depseen{$_} = $dependencies{$field}{$_};
  218. 1;
  219. } else {
  220. # Since dependencies can be versionned, we have to
  221. # verify if the dependency is stronger than the
  222. # previously seen one
  223. if (compare_versions($depseen{$_}, "gt",
  224. $dependencies{$field}{$_})) {
  225. 0;
  226. } else {
  227. $depseen{$_} = $dependencies{$field}{$_};
  228. 1;
  229. }
  230. }
  231. } keys %{$dependencies{$field}};
  232. }
  233. if ($dep) {
  234. print $fh "$varnameprefix:$field=$dep\n";
  235. }
  236. }
  237. # Replace old file by new one
  238. if (!$stdout) {
  239. close($fh);
  240. rename("$varlistfile.new",$varlistfile) ||
  241. syserr(sprintf(_g("install new varlist file \`%s'"), $varlistfile));
  242. }
  243. ##
  244. ## Functions
  245. ##
  246. sub version {
  247. printf _g("Debian %s version %s.\n"), $progname, $version;
  248. printf _g("
  249. Copyright (C) 1996 Ian Jackson.
  250. Copyright (C) 2000 Wichert Akkerman.
  251. Copyright (C) 2006 Frank Lichtenheld.
  252. Copyright (C) 2007 Raphael Hertzog.
  253. ");
  254. printf _g("
  255. This is free software; see the GNU General Public Licence version 2 or
  256. later for copying conditions. There is NO warranty.
  257. ");
  258. }
  259. sub usage {
  260. printf _g(
  261. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  262. Positional options (order is significant):
  263. <executable> include dependencies for <executable>,
  264. -e<executable> (use -e if <executable> starts with \`-')
  265. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  266. Options:
  267. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  268. -O print variable settings to stdout.
  269. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  270. -T<varlistfile> update variables here, not debian/substvars.
  271. -t<type> set package type (default is deb).
  272. --admindir=<directory> change the administrative directory.
  273. -h, --help show this help message.
  274. --version show the version.
  275. Dependency fields recognised are:
  276. %s
  277. "), $progname, join("/",@depfields);
  278. }
  279. sub add_shlibs_dep {
  280. my ($soname, $pkg) = @_;
  281. foreach my $file ($shlibslocal, $shlibsoverride, @pkg_shlibs,
  282. "$admindir/info/$pkg.shlibs")
  283. {
  284. next if not -e $file;
  285. my $dep = extract_from_shlibs($soname, $file);
  286. if (defined($dep)) {
  287. foreach (split(/,\s*/, $dep)) {
  288. $dependencies{$cur_field}{$_} = 1;
  289. }
  290. last;
  291. }
  292. }
  293. }
  294. sub extract_from_shlibs {
  295. my ($soname, $shlibfile) = @_;
  296. my ($libname, $libversion);
  297. # Split soname in name/version
  298. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  299. $libname = $1; $libversion = $2;
  300. } elsif ($soname =~ /^(.*)-(.*)\.so$/) {
  301. $libname = $1; $libversion = $2;
  302. } else {
  303. warning(sprintf(_g("Can't extract name and version from library name \`%s'"), $soname));
  304. return;
  305. }
  306. # Open shlibs file
  307. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  308. open(SHLIBS, "< $shlibfile") || syserr(sprintf(_g("unable to open shared libs info file \`%s'"), $shlibfile));
  309. my $dep;
  310. while (<SHLIBS>) {
  311. s/\s*\n$//; 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") || syserr("can't open file $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. open(DPKG, "dpkg --search -- @files 2>/dev/null |") ||
  371. syserr(sprintf(_g("Can't execute dpkg --search: %s"), $!));
  372. while(defined($_ = <DPKG>)) {
  373. chomp($_);
  374. if (m/^local diversion |^diversion by/) {
  375. warning(_g("diversions involved - output may be incorrect"));
  376. print(STDERR " $_\n") || syserr(_g("write diversion info to stderr"));
  377. } elsif (m/^([^:]+): (\S+)$/) {
  378. $pkgmatch->{$2} = [ split(/, /, $1) ];
  379. } else {
  380. warning(sprintf(_g("unknown output from dpkg --search: '%s'"), $_));
  381. }
  382. }
  383. close(DPKG);
  384. return $pkgmatch;
  385. }