dpkg-shlibdeps.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #! /usr/bin/perl
  2. #
  3. # dpkg-shlibdeps
  4. # $Id: dpkg-shlibdeps.pl,v 1.19.2.2 2004/04/25 17:11:41 keybuk Exp $
  5. my $dpkglibdir="/usr/lib/dpkg";
  6. my $version="1.4.1.19"; # This line modified by Makefile
  7. use English;
  8. use POSIX qw(:errno_h :signal_h);
  9. my $shlibsoverride= '/etc/dpkg/shlibs.override';
  10. my $shlibsdefault= '/etc/dpkg/shlibs.default';
  11. my $shlibslocal= 'debian/shlibs.local';
  12. my $shlibsppdir= '/var/lib/dpkg/info';
  13. my $shlibsppext= '.shlibs';
  14. my $varnameprefix= 'shlibs';
  15. my $dependencyfield= 'Depends';
  16. my $varlistfile= 'debian/substvars';
  17. my $packagetype= 'deb';
  18. my @depfields= qw(Suggests Recommends Depends Pre-Depends);
  19. my %depstrength;
  20. my $i=0; grep($depstrength{$_}= ++$i, @depfields);
  21. push(@INC,$dpkglibdir);
  22. require 'controllib.pl';
  23. #use strict;
  24. #use warnings;
  25. sub usageversion {
  26. print STDERR
  27. "Debian dpkg-shlibdeps $version.
  28. Copyright (C) 1996 Ian Jackson.
  29. Copyright (C) 2000 Wichert Akkerman.
  30. Copyright (C) 2006 Frank Lichtenheld.
  31. This is free software; see the GNU General Public Licence version 2 or
  32. later for copying conditions. There is NO warranty.
  33. Usage:
  34. dpkg-shlibdeps [<option> ...] <executable>|-e<executable> [<option>] ...
  35. Positional arguments/options (order is significant):
  36. <executable> } include dependencies for <executable>
  37. -e<executable> } (use -e if <executable> starts with \`-')
  38. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>
  39. Overall options (have global effect no matter where placed):
  40. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  41. -O print variable settings to stdout
  42. -L<localshlibsfile> shlibs override file, not debian/shlibs.local
  43. -T<varlistfile> update variables here, not debian/substvars
  44. -t<type> set package type (default is deb)
  45. Dependency fields recognised are ".join("/",@depfields)."
  46. ";
  47. }
  48. my ($stdout, @exec, @execf);
  49. foreach (@ARGV) {
  50. if (m/^-T/) {
  51. $varlistfile= $POSTMATCH;
  52. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  53. $varnameprefix= $1;
  54. } elsif (m/^-L/) {
  55. $shlibslocal= $POSTMATCH;
  56. } elsif (m/^-O$/) {
  57. $stdout= 1;
  58. } elsif (m/^-h$/) {
  59. usageversion; exit(0);
  60. } elsif (m/^-d/) {
  61. $dependencyfield= capit($POSTMATCH);
  62. defined($depstrength{$dependencyfield}) ||
  63. &warn("unrecognised dependency field \`$dependencyfield'");
  64. } elsif (m/^-e/) {
  65. push(@exec,$POSTMATCH); push(@execf,$dependencyfield);
  66. } elsif (m/^-t/) {
  67. $packagetype= $POSTMATCH;
  68. } elsif (m/^-/) {
  69. usageerr("unknown option \`$_'");
  70. } else {
  71. push(@exec,$_); push(@execf,$dependencyfield);
  72. }
  73. }
  74. @exec || usageerr("need at least one executable");
  75. sub isbin {
  76. open (F, $_[0]) || die("unable to open '$_[0]' for test");
  77. my $d;
  78. if (read (F, $d, 4) != 4) {
  79. die ("unable to read first four bytes of '$_[0]' as magic number");
  80. }
  81. if ($d =~ /^\177ELF$/) { # ELF binary
  82. return 1;
  83. } elsif (unpack ('N', $d) == 0x8086010B) { # obsd dyn bin
  84. return 1;
  85. } elsif (unpack ('N', $d) == 0x86010B) { # obsd stat bin
  86. return 1;
  87. } elsif ($d =~ /^\#\!..$/) { # shell script
  88. return 0;
  89. } elsif (unpack ('N', $d) == 0xcafebabe) { # JAVA binary
  90. return 0;
  91. } else {
  92. die("unrecognized file type for '$_[0]'");
  93. }
  94. }
  95. my @librarypaths = qw( /lib /usr/lib /lib64 /usr/lib64 );
  96. my %librarypaths = map { $_ => 1 } @librarypaths;
  97. open CONF, '</etc/ld.so.conf' or
  98. warn( "couldn't open /etc/ld.so.conf: $!" );
  99. while( <CONF> ) {
  100. next if /^\s*$/;
  101. chomp;
  102. s,/+$,,;
  103. unless ($librarypaths{$_}++) {
  104. push @librarypaths, $_;
  105. }
  106. }
  107. close CONF;
  108. my (%rpaths, %format);
  109. my (@libfiles, @libname, @libsoname, @libf);
  110. for ($i=0;$i<=$#exec;$i++) {
  111. if (!isbin ($exec[$i])) { next; }
  112. # Now we get the direct deps of the program
  113. defined(my $c= open(P,"-|")) || syserr("cannot fork for objdump");
  114. if (!$c) {
  115. exec("objdump","-p","--",$exec[$i]);
  116. syserr("cannot exec objdump");
  117. }
  118. while (<P>) {
  119. chomp;
  120. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  121. $format{$execf[$i]} = $1;
  122. } elsif (m,^\s*NEEDED\s+,) {
  123. if (m,^\s*NEEDED\s+((\S+)\.so\.(\S+))$,) {
  124. push(@libname,$2); push(@libsoname,$3);
  125. push(@libf,$execf[$i]);
  126. push(@libfiles,$1);
  127. } elsif (m,^\s*NEEDED\s+((\S+)-(\S+)\.so)$,) {
  128. push(@libname,$2); push(@libsoname,$3);
  129. push(@libf,$execf[$i]);
  130. push(@libfiles,$1);
  131. } else {
  132. m,^\s*NEEDED\s+(\S+)$,;
  133. &warn("format of \`NEEDED $1' not recognized");
  134. }
  135. } elsif (/^\s*RPATH\s+(\S+)\s*$/) {
  136. push @{$rpaths{$execf[$i]}}, $1;
  137. }
  138. }
  139. close(P) or subprocerr("objdump on \`$exec[$i]'");
  140. }
  141. # Now: See if it is in this package. See if it is in any other package.
  142. my @curshlibs;
  143. sub searchdir {
  144. my $dir = shift;
  145. if(opendir(DIR, $dir)) {
  146. my @dirents = readdir(DIR);
  147. closedir(DIR);
  148. for (@dirents) {
  149. if ( -f "$dir/$_/DEBIAN/shlibs" ) {
  150. push(@curshlibs, "$dir/$_/DEBIAN/shlibs");
  151. next;
  152. } elsif ( $_ !~ /^\./ && -d "$dir/$_" && ! -l "$dir/$_" ) {
  153. &searchdir("$dir/$_");
  154. }
  155. }
  156. }
  157. }
  158. my $searchdir = $exec[0];
  159. my $curpackdir = "debian/tmp";
  160. do { $searchdir =~ s,/[^/]*$,,; } while($searchdir =~ m,/,
  161. && ! -d "$searchdir/DEBIAN");
  162. if ($searchdir =~ m,/,) {
  163. $curpackdir = $searchdir;
  164. $searchdir =~ s,/[^/]*,,;
  165. &searchdir($searchdir);
  166. }
  167. if (1 || $#curshlibs >= 0) {
  168. PRELIB:
  169. for ($i=0;$i<=$#libname;$i++) {
  170. if(scanshlibsfile($shlibslocal,$libname[$i],$libsoname[$i],$libf[$i])
  171. || scanshlibsfile($shlibsoverride,$libname[$i],$libsoname[$i],$libf[$i])) {
  172. splice(@libname, $i, 1);
  173. splice(@libsoname, $i, 1);
  174. splice(@libf, $i, 1);
  175. splice(@libfiles, $i, 1);
  176. $i--;
  177. next PRELIB;
  178. }
  179. for my $shlibsfile (@curshlibs) {
  180. if(scanshlibsfile($shlibsfile, $libname[$i], $libsoname[$i], $libf[$i])) {
  181. splice(@libname, $i, 1);
  182. splice(@libsoname, $i, 1);
  183. splice(@libf, $i, 1);
  184. splice(@libfiles, $i, 1);
  185. $i--;
  186. next PRELIB;
  187. }
  188. }
  189. }
  190. }
  191. my %pathpackages;
  192. if ($#libfiles >= 0) {
  193. # what does this line do? -- djpig
  194. grep(s/\[\?\*/\\$&/g, @libname);
  195. defined(my $c= open(P,"-|")) || syserr("cannot fork for dpkg --search");
  196. if (!$c) {
  197. close STDERR; # we don't need to see dpkg's errors
  198. open STDERR, "> /dev/null";
  199. $ENV{LC_ALL} = "C";
  200. exec("dpkg","--search","--",@libfiles);
  201. syserr("cannot exec dpkg");
  202. }
  203. while (<P>) {
  204. chomp;
  205. if (m/^local diversion |^diversion by/) {
  206. &warn("diversions involved - output may be incorrect");
  207. print(STDERR " $_\n") || syserr("write diversion info to stderr");
  208. } elsif (m=^(\S+(, \S+)*): (\S+)$=) {
  209. push @{$pathpackages{$LAST_PAREN_MATCH}}, split(/, /, $1);
  210. } else {
  211. &warn("unknown output from dpkg --search: \`$_'");
  212. }
  213. }
  214. close(P);
  215. }
  216. LIB:
  217. for ($i=0;$i<=$#libname;$i++) {
  218. my $file = $libfiles[$i];
  219. my @packages;
  220. foreach my $rpath (@{$rpaths{$libf[$i]}}) {
  221. if (exists $pathpackages{"$rpath/$file"}
  222. && format_matches($libf[$i],"$rpath/$file")) {
  223. push @packages, @{$pathpackages{"$rpath/$file"}};
  224. }
  225. }
  226. foreach my $path (@librarypaths) {
  227. if (exists $pathpackages{"$path/$file"}
  228. && format_matches($libf[$i],"$path/$file")) {
  229. push @packages, @{$pathpackages{"$path/$file"}};
  230. }
  231. }
  232. if (!@packages) {
  233. &warn("could not find any packages for $libfiles[$i]");
  234. } else {
  235. for my $p (@packages) {
  236. scanshlibsfile("$shlibsppdir/$p$shlibsppext",
  237. $libname[$i],$libsoname[$i],$libf[$i])
  238. && next LIB;
  239. }
  240. }
  241. scanshlibsfile($shlibsdefault,$libname[$i],$libsoname[$i],$libf[$i])
  242. && next;
  243. &warn("unable to find dependency information for ".
  244. "shared library $libname[$i] (soname $libsoname[$i], ".
  245. "path $libfiles[$i], dependency field $libf[$i])");
  246. }
  247. sub format_matches {
  248. my ($file1, $file2) = @_;
  249. my ($format1, $format2) = (get_format($file1),get_format($file2));
  250. return $format1 eq $format2;
  251. }
  252. sub get_format {
  253. my ($file) = @_;
  254. if ($format{$file}) {
  255. return $format{$file};
  256. } else {
  257. defined(my $c= open(P,"-|")) || syserr("cannot fork for objdump");
  258. if (!$c) {
  259. exec("objdump","-a","--",$file);
  260. syserr("cannot exec objdump");
  261. }
  262. while (<P>) {
  263. chomp;
  264. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  265. $format{$file} = $1;
  266. return $format{$file};
  267. }
  268. }
  269. close(P) or subprocerr("objdump on \`$file'");
  270. }
  271. }
  272. my (%predefdepfdep, %unkdepfdone, %unkdepf);
  273. sub scanshlibsfile {
  274. my ($fn,$ln,$lsn,$lf) = @_;
  275. my ($da,$dk);
  276. $fn= "./$fn" if $fn =~ m/^\s/;
  277. if (!open(SLF,"< $fn")) {
  278. $! == ENOENT || syserr("unable to open shared libs info file \`$fn'");
  279. return 0;
  280. }
  281. while (<SLF>) {
  282. s/\s*\n$//; next if m/^\#/;
  283. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)/) {
  284. &warn("shared libs info file \`$fn' line $.: bad line \`$_'");
  285. next;
  286. }
  287. next if defined $1 && $1 ne $packagetype;
  288. next if $2 ne $ln || $3 ne $lsn;
  289. return 1 if $fn eq "$curpackdir/DEBIAN/shlibs";
  290. $da= $POSTMATCH;
  291. last if defined $1; # exact match, otherwise keep looking
  292. }
  293. close(SLF);
  294. return 0 unless defined $da;
  295. for my $dv (split(/,/,$da)) {
  296. $dv =~ s/^\s+//; $dv =~ s/\s+$//;
  297. if (defined($depstrength{$lf})) {
  298. if (!defined($predefdepfdep{$dv}) ||
  299. $depstrength{$predefdepfdep{$dv}} < $depstrength{$lf}) {
  300. $predefdepfdep{$dv}= $lf;
  301. }
  302. } else {
  303. $dk= "$lf: $dv";
  304. if (!defined($unkdepfdone{$dk})) {
  305. $unkdepfdone{$dk}= 1;
  306. $unkdepf{$lf}.= ', ' if length($unkdepf{$lf});
  307. $unkdepf{$lf}.= $dv;
  308. }
  309. }
  310. }
  311. return 1;
  312. }
  313. my $fh;
  314. if (!$stdout) {
  315. open(Y,"> $varlistfile.new") ||
  316. syserr("open new substvars file \`$varlistfile.new'");
  317. unless ($REAL_USER_ID) {
  318. chown(@fowner, "$varlistfile.new") ||
  319. syserr("chown of \`$varlistfile.new'");
  320. }
  321. if (open(X,"< $varlistfile")) {
  322. while (<X>) {
  323. s/\n$//;
  324. next if m/^(\w[-:0-9A-Za-z]*):/ && $1 eq $varnameprefix;
  325. print(Y "$_\n") ||
  326. syserr("copy old entry to new varlist file \`$varlistfile.new'");
  327. }
  328. } elsif ($! != ENOENT) {
  329. syserr("open old varlist file \`$varlistfile' for reading");
  330. }
  331. $fh= 'Y';
  332. } else {
  333. $fh= 'STDOUT';
  334. }
  335. my %defdepf;
  336. for my $dv (sort keys %predefdepfdep) {
  337. my $lf= $predefdepfdep{$dv};
  338. $defdepf{$lf}.= ', ' if length($defdepf{$lf});
  339. $defdepf{$lf}.= $dv;
  340. }
  341. for my $lf (reverse @depfields) {
  342. next unless defined($defdepf{$lf});
  343. print($fh "$varnameprefix:$lf=$defdepf{$lf}\n")
  344. || syserr("write output entry");
  345. }
  346. for my $lf (sort keys %unkdepf) {
  347. print($fh "$varnameprefix:$lf=$unkdepf{$lf}\n")
  348. || syserr("write userdef output entry");
  349. }
  350. close($fh) || syserr("close output");
  351. if (!$stdout) {
  352. rename("$varlistfile.new",$varlistfile) ||
  353. syserr("install new varlist file \`$varlistfile'");
  354. }