dpkg-shlibdeps.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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, @execfield);
  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(@execfield,$dependencyfield);
  66. } elsif (m/^-t/) {
  67. $packagetype= $POSTMATCH;
  68. } elsif (m/^-/) {
  69. usageerr("unknown option \`$_'");
  70. } else {
  71. push(@exec,$_); push(@execfield,$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 { $_ => 'default' } @librarypaths;
  97. if ($ENV{LD_LIBRARY_PATH}) {
  98. foreach (reverse split( /:/, $ENV{LD_LIBRARY_PATH} )) {
  99. s,/+$,,;
  100. unless (exists $librarypaths{$_}) {
  101. $librarypaths{$_} = 'env';
  102. unshift @librarypaths, $_;
  103. }
  104. }
  105. }
  106. # Support system library directories.
  107. my $ldconfigdir = '/lib/ldconfig';
  108. if (opendir(DIR, $ldconfigdir)) {
  109. my @dirents = readdir(DIR);
  110. closedir(DIR);
  111. for (@dirents) {
  112. next if /^\./;
  113. my $d = `readlink -f $ldconfigdir/$_`;
  114. chomp $d;
  115. unless (exists $librarypaths{$d}) {
  116. $librarypaths{$d} = 'ldconfig';
  117. push @librarypaths, $d;
  118. }
  119. }
  120. }
  121. open CONF, '</etc/ld.so.conf' or
  122. warn( "couldn't open /etc/ld.so.conf: $!" );
  123. while( <CONF> ) {
  124. next if /^\s*$/;
  125. chomp;
  126. s,/+$,,;
  127. unless (exists $librarypaths{$_}) {
  128. $librarypaths{$_} = 'conf';
  129. push @librarypaths, $_;
  130. }
  131. }
  132. close CONF;
  133. my (%rpaths, %format);
  134. my (@libfiles, @libname, @libsoname, @libfield, @libexec);
  135. for ($i=0;$i<=$#exec;$i++) {
  136. if (!isbin ($exec[$i])) { next; }
  137. # Now we get the direct deps of the program
  138. defined(my $c= open(P,"-|")) || syserr("cannot fork for objdump");
  139. if (!$c) {
  140. exec("objdump","-p","--",$exec[$i]);
  141. syserr("cannot exec objdump");
  142. }
  143. while (<P>) {
  144. chomp;
  145. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  146. $format{$exec[$i]} = $1;
  147. } elsif (m,^\s*NEEDED\s+,) {
  148. if (m,^\s*NEEDED\s+((\S+)\.so\.(\S+))$,) {
  149. push(@libname,$2); push(@libsoname,$3);
  150. push(@libfield,$execfield[$i]);
  151. push(@libfiles,$1);
  152. push(@libexec,$exec[$i]);
  153. } elsif (m,^\s*NEEDED\s+((\S+)-(\S+)\.so)$,) {
  154. push(@libname,$2); push(@libsoname,$3);
  155. push(@libfield,$execfield[$i]);
  156. push(@libfiles,$1);
  157. push(@libexec,$exec[$i]);
  158. } else {
  159. m,^\s*NEEDED\s+(\S+)$,;
  160. &warn("format of \`NEEDED $1' not recognized");
  161. }
  162. } elsif (/^\s*RPATH\s+(\S+)\s*$/) {
  163. push @{$rpaths{$exec[$i]}}, $1;
  164. }
  165. }
  166. close(P) or subprocerr("objdump on \`$exec[$i]'");
  167. }
  168. # Now: See if it is in this package. See if it is in any other package.
  169. my @curshlibs;
  170. sub searchdir {
  171. my $dir = shift;
  172. if(opendir(DIR, $dir)) {
  173. my @dirents = readdir(DIR);
  174. closedir(DIR);
  175. for (@dirents) {
  176. if ( -f "$dir/$_/DEBIAN/shlibs" ) {
  177. push(@curshlibs, "$dir/$_/DEBIAN/shlibs");
  178. next;
  179. } elsif ( $_ !~ /^\./ && ! -e "$dir/$_/DEBIAN" &&
  180. -d "$dir/$_" && ! -l "$dir/$_" ) {
  181. &searchdir("$dir/$_");
  182. }
  183. }
  184. }
  185. }
  186. my $searchdir = $exec[0];
  187. my $curpackdir = "debian/tmp";
  188. do { $searchdir =~ s,/[^/]*$,,; } while($searchdir =~ m,/,
  189. && ! -d "$searchdir/DEBIAN");
  190. if ($searchdir =~ m,/,) {
  191. $curpackdir = $searchdir;
  192. $searchdir =~ s,/[^/]*,,;
  193. &searchdir($searchdir);
  194. }
  195. if (1 || $#curshlibs >= 0) {
  196. PRELIB:
  197. for ($i=0;$i<=$#libname;$i++) {
  198. if(scanshlibsfile($shlibslocal,$libname[$i],$libsoname[$i],$libfield[$i])
  199. || scanshlibsfile($shlibsoverride,$libname[$i],$libsoname[$i],$libfield[$i])) {
  200. splice(@libname, $i, 1);
  201. splice(@libsoname, $i, 1);
  202. splice(@libfield, $i, 1);
  203. splice(@libfiles, $i, 1);
  204. splice(@libexec, $i, 1);
  205. $i--;
  206. next PRELIB;
  207. }
  208. for my $shlibsfile (@curshlibs) {
  209. if(scanshlibsfile($shlibsfile, $libname[$i], $libsoname[$i], $libfield[$i])) {
  210. splice(@libname, $i, 1);
  211. splice(@libsoname, $i, 1);
  212. splice(@libfield, $i, 1);
  213. splice(@libfiles, $i, 1);
  214. splice(@libexec, $i, 1);
  215. $i--;
  216. next PRELIB;
  217. }
  218. }
  219. }
  220. }
  221. my %pathpackages;
  222. if ($#libfiles >= 0) {
  223. grep(s/\[\?\*/\\$&/g, @libname);
  224. defined(my $c= open(P,"-|")) || syserr("cannot fork for dpkg --search");
  225. if (!$c) {
  226. close STDERR; # we don't need to see dpkg's errors
  227. open STDERR, "> /dev/null";
  228. $ENV{LC_ALL} = "C";
  229. exec("dpkg","--search","--",@libfiles);
  230. syserr("cannot exec dpkg");
  231. }
  232. while (<P>) {
  233. chomp;
  234. if (m/^local diversion |^diversion by/) {
  235. &warn("diversions involved - output may be incorrect");
  236. print(STDERR " $_\n") || syserr("write diversion info to stderr");
  237. } elsif (m=^(\S+(, \S+)*): (\S+)$=) {
  238. push @{$pathpackages{$LAST_PAREN_MATCH}}, split(/, /, $1);
  239. } else {
  240. &warn("unknown output from dpkg --search: \`$_'");
  241. }
  242. }
  243. close(P);
  244. }
  245. LIB:
  246. for ($i=0;$i<=$#libname;$i++) {
  247. my $file = $libfiles[$i];
  248. my @packages;
  249. foreach my $rpath (@{$rpaths{$libexec[$i]}}) {
  250. if (exists $pathpackages{"$rpath/$file"}
  251. && format_matches($libexec[$i],"$rpath/$file")) {
  252. push @packages, @{$pathpackages{"$rpath/$file"}};
  253. }
  254. }
  255. foreach my $path (@librarypaths) {
  256. if (exists $pathpackages{"$path/$file"}
  257. && format_matches($libexec[$i],"$path/$file")) {
  258. push @packages, @{$pathpackages{"$path/$file"}};
  259. }
  260. }
  261. if (!@packages) {
  262. &warn("could not find any packages for $libfiles[$i]");
  263. } else {
  264. for my $p (@packages) {
  265. scanshlibsfile("$shlibsppdir/$p$shlibsppext",
  266. $libname[$i],$libsoname[$i],$libfield[$i])
  267. && next LIB;
  268. }
  269. }
  270. scanshlibsfile($shlibsdefault,$libname[$i],$libsoname[$i],$libfield[$i])
  271. && next;
  272. &warn("unable to find dependency information for ".
  273. "shared library $libname[$i] (soname $libsoname[$i], ".
  274. "path $libfiles[$i], dependency field $libfield[$i])");
  275. }
  276. sub format_matches {
  277. my ($file1, $file2) = @_;
  278. my ($format1, $format2) = (get_format($file1),get_format($file2));
  279. return $format1 eq $format2;
  280. }
  281. sub get_format {
  282. my ($file) = @_;
  283. if ($format{$file}) {
  284. return $format{$file};
  285. } else {
  286. defined(my $c= open(P,"-|")) || syserr("cannot fork for objdump");
  287. if (!$c) {
  288. exec("objdump","-a","--",$file);
  289. syserr("cannot exec objdump");
  290. }
  291. while (<P>) {
  292. chomp;
  293. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  294. $format{$file} = $1;
  295. return $format{$file};
  296. }
  297. }
  298. close(P) or subprocerr("objdump on \`$file'");
  299. }
  300. }
  301. my (%predefdepfdep, %unkdepfdone, %unkdepf);
  302. sub scanshlibsfile {
  303. my ($fn,$ln,$lsn,$lf) = @_;
  304. my ($da,$dk);
  305. $fn= "./$fn" if $fn =~ m/^\s/;
  306. if (!open(SLF,"< $fn")) {
  307. $! == ENOENT || syserr("unable to open shared libs info file \`$fn'");
  308. return 0;
  309. }
  310. while (<SLF>) {
  311. s/\s*\n$//; next if m/^\#/;
  312. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)/) {
  313. &warn("shared libs info file \`$fn' line $.: bad line \`$_'");
  314. next;
  315. }
  316. next if defined $1 && $1 ne $packagetype;
  317. next if $2 ne $ln || $3 ne $lsn;
  318. return 1 if $fn eq "$curpackdir/DEBIAN/shlibs";
  319. $da= $POSTMATCH;
  320. last if defined $1; # exact match, otherwise keep looking
  321. }
  322. close(SLF);
  323. return 0 unless defined $da;
  324. for my $dv (split(/,/,$da)) {
  325. $dv =~ s/^\s+//; $dv =~ s/\s+$//;
  326. if (defined($depstrength{$lf})) {
  327. if (!defined($predefdepfdep{$dv}) ||
  328. $depstrength{$predefdepfdep{$dv}} < $depstrength{$lf}) {
  329. $predefdepfdep{$dv}= $lf;
  330. }
  331. } else {
  332. $dk= "$lf: $dv";
  333. if (!defined($unkdepfdone{$dk})) {
  334. $unkdepfdone{$dk}= 1;
  335. $unkdepf{$lf}.= ', ' if length($unkdepf{$lf});
  336. $unkdepf{$lf}.= $dv;
  337. }
  338. }
  339. }
  340. return 1;
  341. }
  342. my $fh;
  343. if (!$stdout) {
  344. open(Y,"> $varlistfile.new") ||
  345. syserr("open new substvars file \`$varlistfile.new'");
  346. unless ($REAL_USER_ID) {
  347. chown(@fowner, "$varlistfile.new") ||
  348. syserr("chown of \`$varlistfile.new'");
  349. }
  350. if (open(X,"< $varlistfile")) {
  351. while (<X>) {
  352. s/\n$//;
  353. next if m/^(\w[-:0-9A-Za-z]*):/ && $1 eq $varnameprefix;
  354. print(Y "$_\n") ||
  355. syserr("copy old entry to new varlist file \`$varlistfile.new'");
  356. }
  357. } elsif ($! != ENOENT) {
  358. syserr("open old varlist file \`$varlistfile' for reading");
  359. }
  360. $fh= 'Y';
  361. } else {
  362. $fh= 'STDOUT';
  363. }
  364. my %defdepf;
  365. for my $dv (sort keys %predefdepfdep) {
  366. my $lf= $predefdepfdep{$dv};
  367. $defdepf{$lf}.= ', ' if length($defdepf{$lf});
  368. $defdepf{$lf}.= $dv;
  369. }
  370. for my $lf (reverse @depfields) {
  371. next unless defined($defdepf{$lf});
  372. print($fh "$varnameprefix:$lf=$defdepf{$lf}\n")
  373. || syserr("write output entry");
  374. }
  375. for my $lf (sort keys %unkdepf) {
  376. print($fh "$varnameprefix:$lf=$unkdepf{$lf}\n")
  377. || syserr("write userdef output entry");
  378. }
  379. close($fh) || syserr("close output");
  380. if (!$stdout) {
  381. rename("$varlistfile.new",$varlistfile) ||
  382. syserr("install new varlist file \`$varlistfile'");
  383. }