dpkg-shlibdeps.pl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-shlibdeps
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. use strict;
  18. use warnings;
  19. use POSIX qw(:errno_h :signal_h);
  20. use Cwd qw(realpath);
  21. use File::Basename qw(dirname);
  22. use Dpkg;
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Path qw(relative_to_pkg_root guess_pkg_root_dir
  26. check_files_are_the_same);
  27. use Dpkg::Version;
  28. use Dpkg::Shlibs qw(find_library @librarypaths);
  29. use Dpkg::Shlibs::Objdump;
  30. use Dpkg::Shlibs::SymbolFile;
  31. use Dpkg::Arch qw(get_host_arch);
  32. use Dpkg::Deps;
  33. use Dpkg::Control::Info;
  34. use Dpkg::Control::Fields;
  35. use constant {
  36. WARN_SYM_NOT_FOUND => 1,
  37. WARN_DEP_AVOIDABLE => 2,
  38. WARN_NOT_NEEDED => 4,
  39. };
  40. # By increasing importance
  41. my @depfields = qw(Suggests Recommends Depends Pre-Depends);
  42. my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
  43. textdomain("dpkg-dev");
  44. my $shlibsoverride = '/etc/dpkg/shlibs.override';
  45. my $shlibsdefault = '/etc/dpkg/shlibs.default';
  46. my $shlibslocal = 'debian/shlibs.local';
  47. my $packagetype = 'deb';
  48. my $dependencyfield = 'Depends';
  49. my $varlistfile = 'debian/substvars';
  50. my $varnameprefix = 'shlibs';
  51. my $ignore_missing_info = 0;
  52. my $warnings = 3;
  53. my $debug = 0;
  54. my @exclude = ();
  55. my @pkg_dir_to_search = ();
  56. my $host_arch = get_host_arch();
  57. my (@pkg_shlibs, @pkg_symbols, @pkg_root_dirs);
  58. if (-d "debian") {
  59. push @pkg_symbols, <debian/*/DEBIAN/symbols>;
  60. push @pkg_shlibs, <debian/*/DEBIAN/shlibs>;
  61. my %uniq = map { guess_pkg_root_dir($_) => 1 } (@pkg_symbols, @pkg_shlibs);
  62. push @pkg_root_dirs, keys %uniq;
  63. }
  64. my ($stdout, %exec);
  65. foreach (@ARGV) {
  66. if (m/^-T(.*)$/) {
  67. $varlistfile = $1;
  68. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  69. $varnameprefix = $1;
  70. } elsif (m/^-L(.*)$/) {
  71. $shlibslocal = $1;
  72. } elsif (m/^-S(.*)$/) {
  73. push @pkg_dir_to_search, $1;
  74. } elsif (m/^-O$/) {
  75. $stdout = 1;
  76. } elsif (m/^-(h|-help)$/) {
  77. usage(); exit(0);
  78. } elsif (m/^--version$/) {
  79. version(); exit(0);
  80. } elsif (m/^--admindir=(.*)$/) {
  81. $admindir = $1;
  82. -d $admindir ||
  83. error(_g("administrative directory '%s' does not exist"), $admindir);
  84. } elsif (m/^-d(.*)$/) {
  85. $dependencyfield = field_capitalize($1);
  86. defined($depstrength{$dependencyfield}) ||
  87. warning(_g("unrecognised dependency field \`%s'"), $dependencyfield);
  88. } elsif (m/^-e(.*)$/) {
  89. if (exists $exec{$1}) {
  90. # Affect the binary to the most important field
  91. if ($depstrength{$dependencyfield} > $depstrength{$exec{$1}}) {
  92. $exec{$1} = $dependencyfield;
  93. }
  94. } else {
  95. $exec{$1} = $dependencyfield;
  96. }
  97. } elsif (m/^--ignore-missing-info$/) {
  98. $ignore_missing_info = 1;
  99. } elsif (m/^--warnings=(\d+)$/) {
  100. $warnings = $1;
  101. } elsif (m/^-t(.*)$/) {
  102. $packagetype = $1;
  103. } elsif (m/^-v$/) {
  104. $debug++;
  105. } elsif (m/^-x(.*)$/) {
  106. push @exclude, $1;
  107. } elsif (m/^-/) {
  108. usageerr(_g("unknown option \`%s'"), $_);
  109. } else {
  110. if (exists $exec{$_}) {
  111. # Affect the binary to the most important field
  112. if ($depstrength{$dependencyfield} > $depstrength{$exec{$_}}) {
  113. $exec{$_} = $dependencyfield;
  114. }
  115. } else {
  116. $exec{$_} = $dependencyfield;
  117. }
  118. }
  119. }
  120. scalar keys %exec || usageerr(_g("need at least one executable"));
  121. my $control = Dpkg::Control::Info->new();
  122. my $fields = $control->get_source();
  123. my $build_depends = defined($fields->{"Build-Depends"}) ?
  124. $fields->{"Build-Depends"} : "";
  125. my $build_deps = deps_parse($build_depends, reduce_arch => 1);
  126. my %dependencies;
  127. my %shlibs;
  128. # Statictics on soname seen in the whole run (with multiple analysis of
  129. # binaries)
  130. my %global_soname_notfound;
  131. my %global_soname_used;
  132. my %global_soname_needed;
  133. # Symfile and objdump caches
  134. my %symfile_cache;
  135. my %objdump_cache;
  136. my %symfile_has_soname_cache;
  137. # Used to count errors due to missing libraries
  138. my $error_count = 0;
  139. my $cur_field;
  140. foreach my $file (keys %exec) {
  141. $cur_field = $exec{$file};
  142. print ">> Scanning $file (for $cur_field field)\n" if $debug;
  143. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  144. my @sonames = $obj->get_needed_libraries;
  145. # Load symbols files for all needed libraries (identified by SONAME)
  146. my %libfiles;
  147. my %altlibfiles;
  148. my %soname_notfound;
  149. my %alt_soname;
  150. foreach my $soname (@sonames) {
  151. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  152. unless (defined $lib) {
  153. $soname_notfound{$soname} = 1;
  154. $global_soname_notfound{$soname} = 1;
  155. my $msg = _g("couldn't find library %s needed by %s (ELF " .
  156. "format: '%s'; RPATH: '%s').");
  157. if (scalar(split_soname($soname))) {
  158. errormsg($msg, $soname, $file, $obj->{format}, join(":", @{$obj->{RPATH}}));
  159. $error_count++;
  160. } else {
  161. warning($msg, $soname, $file, $obj->{format}, join(":", @{$obj->{RPATH}}));
  162. }
  163. next;
  164. }
  165. $libfiles{$lib} = $soname;
  166. my $reallib = realpath($lib);
  167. if ($reallib ne $lib) {
  168. $altlibfiles{$reallib} = $soname;
  169. }
  170. print "Library $soname found in $lib\n" if $debug;
  171. }
  172. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  173. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  174. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  175. my @soname_wo_symfile;
  176. foreach my $lib (keys %libfiles) {
  177. my $soname = $libfiles{$lib};
  178. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  179. # The path of the library as calculated is not the
  180. # official path of a packaged file, try to fallback on
  181. # on the realpath() first, maybe this one is part of a package
  182. my $reallib = realpath($lib);
  183. if (exists $file2pkg->{$reallib}) {
  184. $file2pkg->{$lib} = $file2pkg->{$reallib};
  185. }
  186. }
  187. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  188. # If the library is really not available in an installed package,
  189. # it's because it's in the process of being built
  190. # Empty package name will lead to consideration of symbols
  191. # file from the package being built only
  192. $file2pkg->{$lib} = [""];
  193. print "No associated package found for $lib\n" if $debug;
  194. }
  195. # Load symbols/shlibs files from packages providing libraries
  196. foreach my $pkg (@{$file2pkg->{$lib}}) {
  197. my $symfile_path;
  198. my $haslocaldep = 0;
  199. if (-e $shlibslocal and
  200. defined(extract_from_shlibs($soname, $shlibslocal)))
  201. {
  202. $haslocaldep = 1;
  203. }
  204. if ($packagetype eq "deb" and not $haslocaldep) {
  205. # Use fine-grained dependencies only on real deb
  206. # and only if the dependency is not provided by shlibs.local
  207. $symfile_path = find_symbols_file($pkg, $soname, $lib);
  208. }
  209. if (defined($symfile_path)) {
  210. # Load symbol information
  211. print "Using symbols file $symfile_path for $soname\n" if $debug;
  212. unless (exists $symfile_cache{$symfile_path}) {
  213. $symfile_cache{$symfile_path} =
  214. Dpkg::Shlibs::SymbolFile->new(file => $symfile_path);
  215. }
  216. $symfile->merge_object_from_symfile($symfile_cache{$symfile_path}, $soname);
  217. }
  218. if (defined($symfile_path) && $symfile->has_object($soname)) {
  219. # Initialize dependencies with the smallest minimal version
  220. # of all symbols (unversioned dependency is not ok as the
  221. # library might not have always been available in the
  222. # package and we really need it)
  223. my $dep = $symfile->get_dependency($soname);
  224. my $minver = $symfile->get_smallest_version($soname) || '';
  225. foreach my $subdep (split /\s*,\s*/, $dep) {
  226. if (not exists $dependencies{$cur_field}{$subdep}) {
  227. $dependencies{$cur_field}{$subdep} = Dpkg::Version->new($minver);
  228. print " Initialize dependency ($subdep) with minimal " .
  229. "version ($minver)\n" if $debug > 1;
  230. }
  231. }
  232. } else {
  233. # No symbol file found, fall back to standard shlibs
  234. print "Using shlibs+objdump for $soname (file $lib)\n" if $debug;
  235. unless (exists $objdump_cache{$lib}) {
  236. $objdump_cache{$lib} = Dpkg::Shlibs::Objdump::Object->new($lib);
  237. }
  238. my $libobj = $objdump_cache{$lib};
  239. my $id = $dumplibs_wo_symfile->add_object($libobj);
  240. if (($id ne $soname) and ($id ne $lib)) {
  241. warning(_g("%s has an unexpected SONAME (%s)"), $lib, $id);
  242. $alt_soname{$id} = $soname;
  243. }
  244. push @soname_wo_symfile, $soname;
  245. # Only try to generate a dependency for libraries with a SONAME
  246. if ($libobj->is_public_library() and not
  247. add_shlibs_dep($soname, $pkg, $lib)) {
  248. # This failure is fairly new, try to be kind by
  249. # ignoring as many cases that can be safely ignored
  250. my $ignore = 0;
  251. # 1/ when the lib and the binary are in the same
  252. # package
  253. my $root_file = guess_pkg_root_dir($file);
  254. my $root_lib = guess_pkg_root_dir($lib);
  255. $ignore++ if defined $root_file and defined $root_lib
  256. and check_files_are_the_same($root_file, $root_lib);
  257. # 2/ when the lib is not versioned and can't be
  258. # handled by shlibs
  259. $ignore++ unless scalar(split_soname($soname));
  260. # 3/ when we have been asked to do so
  261. $ignore++ if $ignore_missing_info;
  262. error(_g("no dependency information found for %s " .
  263. "(used by %s)."), $lib, $file)
  264. unless $ignore;
  265. }
  266. }
  267. }
  268. }
  269. # Scan all undefined symbols of the binary and resolve to a
  270. # dependency
  271. my %soname_used;
  272. foreach (@sonames) {
  273. # Initialize statistics
  274. $soname_used{$_} = 0;
  275. $global_soname_used{$_} = 0 unless exists $global_soname_used{$_};
  276. if (exists $global_soname_needed{$_}) {
  277. push @{$global_soname_needed{$_}}, $file;
  278. } else {
  279. $global_soname_needed{$_} = [ $file ];
  280. }
  281. }
  282. my $nb_warnings = 0;
  283. my $nb_skipped_warnings = 0;
  284. # Disable warnings about missing symbols when we have not been able to
  285. # find all libs
  286. my $disable_warnings = scalar(keys(%soname_notfound));
  287. my $in_public_dir = 1;
  288. if (my $relname = relative_to_pkg_root($file)) {
  289. my $parent_dir = "/" . dirname($relname);
  290. $in_public_dir = (grep { $parent_dir eq $_ } @librarypaths) ? 1 : 0;
  291. } else {
  292. warning(_g("binaries to analyze should already be " .
  293. "installed in their package's directory."));
  294. }
  295. print "Analyzing all undefined symbols\n" if $debug > 1;
  296. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  297. my $name = $sym->{name};
  298. if ($sym->{version}) {
  299. $name .= "\@$sym->{version}";
  300. } else {
  301. $name .= "\@Base";
  302. }
  303. print " Looking up symbol $name\n" if $debug > 1;
  304. my %symdep = $symfile->lookup_symbol($name, \@sonames);
  305. if (keys %symdep) {
  306. my $depends = $symfile->get_dependency($symdep{soname},
  307. $symdep{symbol}{dep_id});
  308. print " Found in symbols file of $symdep{soname} (minver: " .
  309. "$symdep{symbol}{minver}, dep: $depends)\n" if $debug > 1;
  310. $soname_used{$symdep{soname}}++;
  311. $global_soname_used{$symdep{soname}}++;
  312. if (exists $alt_soname{$symdep{soname}}) {
  313. # Also count usage on alternate soname
  314. $soname_used{$alt_soname{$symdep{soname}}}++;
  315. $global_soname_used{$alt_soname{$symdep{soname}}}++;
  316. }
  317. update_dependency_version($depends, $symdep{symbol}{minver});
  318. } else {
  319. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  320. if (not defined($syminfo)) {
  321. print " Not found\n" if $debug > 1;
  322. next unless ($warnings & WARN_SYM_NOT_FOUND);
  323. next if $disable_warnings;
  324. # Complain about missing symbols only for executables
  325. # and public libraries
  326. if ($obj->is_executable() or $obj->is_public_library()) {
  327. my $print_name = $name;
  328. # Drop the default suffix for readability
  329. $print_name =~ s/\@Base$//;
  330. unless ($sym->{weak}) {
  331. if ($debug or ($in_public_dir and $nb_warnings < 10)
  332. or (!$in_public_dir and $nb_warnings < 1))
  333. {
  334. if ($in_public_dir) {
  335. warning(_g("symbol %s used by %s found in none of the " .
  336. "libraries."), $print_name, $file);
  337. } else {
  338. warning(_g("%s contains an unresolvable reference to " .
  339. "symbol %s: it's probably a plugin."),
  340. $file, $print_name);
  341. }
  342. $nb_warnings++;
  343. } else {
  344. $nb_skipped_warnings++;
  345. }
  346. }
  347. }
  348. } else {
  349. print " Found in $syminfo->{soname} ($syminfo->{objid})\n" if $debug > 1;
  350. if (exists $alt_soname{$syminfo->{soname}}) {
  351. # Also count usage on alternate soname
  352. $soname_used{$alt_soname{$syminfo->{soname}}}++;
  353. $global_soname_used{$alt_soname{$syminfo->{soname}}}++;
  354. }
  355. $soname_used{$syminfo->{soname}}++;
  356. $global_soname_used{$syminfo->{soname}}++;
  357. }
  358. }
  359. }
  360. warning(P_("%d similar warning has been skipped (use -v to see it).",
  361. "%d other similar warnings have been skipped (use -v to see " .
  362. "them all).", $nb_skipped_warnings), $nb_skipped_warnings)
  363. if $nb_skipped_warnings;
  364. foreach my $soname (@sonames) {
  365. # Adjust minimal version of dependencies with information
  366. # extracted from build-dependencies
  367. my $dev_pkg = $symfile->get_field($soname, 'Build-Depends-Package');
  368. if (defined $dev_pkg) {
  369. print "Updating dependencies of $soname with build-dependencies\n" if $debug;
  370. my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
  371. if (defined $minver) {
  372. foreach my $dep ($symfile->get_dependencies($soname)) {
  373. update_dependency_version($dep, $minver, 1);
  374. print " Minimal version of $dep updated with $minver\n" if $debug;
  375. }
  376. } else {
  377. print " No minimal version found in $dev_pkg build-dependency\n" if $debug;
  378. }
  379. }
  380. # Warn about un-NEEDED libraries
  381. unless ($soname_notfound{$soname} or $soname_used{$soname}) {
  382. # Ignore warning for libm.so.6 if also linked against libstdc++
  383. next if ($soname =~ /^libm\.so\.\d+$/ and
  384. scalar grep(/^libstdc\+\+\.so\.\d+/, @sonames));
  385. next unless ($warnings & WARN_NOT_NEEDED);
  386. warning(_g("%s shouldn't be linked with %s (it uses none of its " .
  387. "symbols)."), $file, $soname);
  388. }
  389. }
  390. }
  391. # Warn of unneeded libraries at the "package" level (i.e. over all
  392. # binaries that we have inspected)
  393. foreach my $soname (keys %global_soname_needed) {
  394. unless ($global_soname_notfound{$soname} or $global_soname_used{$soname}) {
  395. next if ($soname =~ /^libm\.so\.\d+$/ and scalar(
  396. grep(/^libstdc\+\+\.so\.\d+/, keys %global_soname_needed)));
  397. next unless ($warnings & WARN_DEP_AVOIDABLE);
  398. warning(_g("dependency on %s could be avoided if \"%s\" were not " .
  399. "uselessly linked against it (they use none of its " .
  400. "symbols)."), $soname,
  401. join(" ", @{$global_soname_needed{$soname}}));
  402. }
  403. }
  404. # Quit now if any missing libraries
  405. if ($error_count >= 1) {
  406. my $note = _g("Note: libraries are not searched in other binary packages " .
  407. "that do not have any shlibs or symbols file.\nTo help dpkg-shlibdeps " .
  408. "find private libraries, you might need to set LD_LIBRARY_PATH.");
  409. error(P_("Cannot continue due to the error above.",
  410. "Cannot continue due to the errors listed above.",
  411. $error_count) . "\n" . $note);
  412. }
  413. # Open substvars file
  414. my $fh;
  415. if ($stdout) {
  416. $fh = \*STDOUT;
  417. } else {
  418. open(NEW, ">", "$varlistfile.new") ||
  419. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  420. if (-e $varlistfile) {
  421. open(OLD, "<", $varlistfile) ||
  422. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  423. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  424. print(NEW $entry) ||
  425. syserr(_g("copy old entry to new varlist file \`%s'"),
  426. "$varlistfile.new");
  427. }
  428. close(OLD);
  429. }
  430. $fh = \*NEW;
  431. }
  432. # Write out the shlibs substvars
  433. my %depseen;
  434. sub filter_deps {
  435. my ($dep, $field) = @_;
  436. # Skip dependencies on excluded packages
  437. foreach my $exc (@exclude) {
  438. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  439. }
  440. # Don't include dependencies if they are already
  441. # mentionned in a higher priority field
  442. if (not exists($depseen{$dep})) {
  443. $depseen{$dep} = $dependencies{$field}{$dep};
  444. return 1;
  445. } else {
  446. # Since dependencies can be versionned, we have to
  447. # verify if the dependency is stronger than the
  448. # previously seen one
  449. my $stronger;
  450. if ($depseen{$dep} eq $dependencies{$field}{$dep}) {
  451. # If both versions are the same (possibly unversionned)
  452. $stronger = 0;
  453. } elsif ($dependencies{$field}{$dep} eq '') {
  454. $stronger = 0; # If the dep is unversionned
  455. } elsif ($depseen{$dep} eq '') {
  456. $stronger = 1; # If the dep seen is unversionned
  457. } elsif (version_compare_relation($depseen{$dep}, REL_GT,
  458. $dependencies{$field}{$dep})) {
  459. # The version of the dep seen is stronger...
  460. $stronger = 0;
  461. } else {
  462. $stronger = 1;
  463. }
  464. $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
  465. return $stronger;
  466. }
  467. }
  468. foreach my $field (reverse @depfields) {
  469. my $dep = "";
  470. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  471. $dep = join ", ",
  472. map {
  473. # Translate dependency templates into real dependencies
  474. if ($dependencies{$field}{$_}) {
  475. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  476. } else {
  477. s/#MINVER#//g;
  478. }
  479. s/\s+/ /g;
  480. $_;
  481. } grep { filter_deps($_, $field) }
  482. keys %{$dependencies{$field}};
  483. }
  484. if ($dep) {
  485. my $obj = deps_parse($dep);
  486. error(_g("invalid dependency got generated: %s"), $dep) unless defined $obj;
  487. $obj->sort();
  488. print $fh "$varnameprefix:$field=$obj\n";
  489. }
  490. }
  491. # Replace old file by new one
  492. if (!$stdout) {
  493. close($fh);
  494. rename("$varlistfile.new",$varlistfile) ||
  495. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  496. }
  497. ##
  498. ## Functions
  499. ##
  500. sub version {
  501. printf _g("Debian %s version %s.\n"), $progname, $version;
  502. printf _g("
  503. Copyright (C) 1996 Ian Jackson.
  504. Copyright (C) 2000 Wichert Akkerman.
  505. Copyright (C) 2006 Frank Lichtenheld.
  506. Copyright (C) 2007 Raphael Hertzog.
  507. ");
  508. printf _g("
  509. This is free software; see the GNU General Public License version 2 or
  510. later for copying conditions. There is NO warranty.
  511. ");
  512. }
  513. sub usage {
  514. printf _g(
  515. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  516. Positional options (order is significant):
  517. <executable> include dependencies for <executable>,
  518. -e<executable> (use -e if <executable> starts with \`-')
  519. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  520. Options:
  521. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  522. -O print variable settings to stdout.
  523. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  524. -T<varlistfile> update variables here, not debian/substvars.
  525. -t<type> set package type (default is deb).
  526. -x<package> exclude package from the generated dependencies.
  527. -S<pkgbuilddir> search needed libraries in the given
  528. package build directory first.
  529. -v enable verbose mode (can be used multiple times).
  530. --ignore-missing-info don't fail if dependency information can't be found.
  531. --warnings=<value> define set of active warnings (see manual page).
  532. --admindir=<directory> change the administrative directory.
  533. -h, --help show this help message.
  534. --version show the version.
  535. Dependency fields recognised are:
  536. %s
  537. "), $progname, join("/",@depfields);
  538. }
  539. sub get_min_version_from_deps {
  540. my ($dep, $pkg) = @_;
  541. if ($dep->isa('Dpkg::Deps::Simple')) {
  542. if (($dep->{package} eq $pkg) &&
  543. defined($dep->{relation}) &&
  544. (($dep->{relation} eq REL_GE) ||
  545. ($dep->{relation} eq REL_GT)))
  546. {
  547. return $dep->{version};
  548. }
  549. return undef;
  550. } else {
  551. my $res;
  552. foreach my $subdep ($dep->get_deps()) {
  553. my $minver = get_min_version_from_deps($subdep, $pkg);
  554. next if not defined $minver;
  555. if (defined $res) {
  556. if (version_compare_relation($minver, REL_GT, $res)) {
  557. $res = $minver;
  558. }
  559. } else {
  560. $res = $minver;
  561. }
  562. }
  563. return $res;
  564. }
  565. }
  566. sub update_dependency_version {
  567. my ($dep, $minver, $existing_only) = @_;
  568. return if not defined($minver);
  569. $minver = Dpkg::Version->new($minver);
  570. foreach my $subdep (split /\s*,\s*/, $dep) {
  571. if (exists $dependencies{$cur_field}{$subdep} and
  572. defined($dependencies{$cur_field}{$subdep}))
  573. {
  574. if ($dependencies{$cur_field}{$subdep} eq '' or
  575. version_compare_relation($minver, REL_GT,
  576. $dependencies{$cur_field}{$subdep}))
  577. {
  578. $dependencies{$cur_field}{$subdep} = $minver;
  579. }
  580. } elsif (!$existing_only) {
  581. $dependencies{$cur_field}{$subdep} = $minver;
  582. }
  583. }
  584. }
  585. sub add_shlibs_dep {
  586. my ($soname, $pkg, $libfile) = @_;
  587. my @shlibs = ($shlibslocal, $shlibsoverride);
  588. if ($pkg eq "") {
  589. # If the file is not packaged, try to find out the shlibs file in
  590. # the package being built where the lib has been found
  591. my $pkg_root = guess_pkg_root_dir($libfile);
  592. if (defined $pkg_root) {
  593. push @shlibs, "$pkg_root/DEBIAN/shlibs";
  594. }
  595. # Fallback to other shlibs files but it shouldn't be necessary
  596. push @shlibs, @pkg_shlibs;
  597. } else {
  598. push @shlibs, "$admindir/info/$pkg.shlibs";
  599. }
  600. push @shlibs, $shlibsdefault;
  601. print " Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  602. foreach my $file (@shlibs) {
  603. next if not -e $file;
  604. my $dep = extract_from_shlibs($soname, $file);
  605. if (defined($dep)) {
  606. print " Found $dep in $file\n" if $debug;
  607. foreach (split(/,\s*/, $dep)) {
  608. # Note: the value is empty for shlibs based dependency
  609. # symbol based dependency will put a valid version as value
  610. $dependencies{$cur_field}{$_} = Dpkg::Version->new('');
  611. }
  612. return 1;
  613. }
  614. }
  615. print " Found nothing\n" if $debug;
  616. return 0;
  617. }
  618. sub split_soname {
  619. my $soname = shift;
  620. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  621. return wantarray ? ($1, $2) : 1;
  622. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  623. return wantarray ? ($1, $2) : 1;
  624. } else {
  625. return wantarray ? () : 0;
  626. }
  627. }
  628. sub extract_from_shlibs {
  629. my ($soname, $shlibfile) = @_;
  630. # Split soname in name/version
  631. my ($libname, $libversion) = split_soname($soname);
  632. unless (defined $libname) {
  633. warning(_g("Can't extract name and version from library name \`%s'"),
  634. $soname);
  635. return;
  636. }
  637. # Open shlibs file
  638. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  639. open(SHLIBS, "<", $shlibfile) ||
  640. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  641. my $dep;
  642. while (<SHLIBS>) {
  643. s/\s*\n$//;
  644. next if m/^\#/;
  645. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  646. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  647. $shlibfile, $., $_);
  648. next;
  649. }
  650. my $depread = defined($4) ? $4 : '';
  651. if (($libname eq $2) && ($libversion eq $3)) {
  652. # Define dep and end here if the package type explicitly
  653. # matches. Otherwise if the packagetype is not specified, use
  654. # the dep only as a default that can be overriden by a later
  655. # line
  656. if (defined($1)) {
  657. if ($1 eq $packagetype) {
  658. $dep = $depread;
  659. last;
  660. }
  661. } else {
  662. $dep = $depread unless defined $dep;
  663. }
  664. }
  665. }
  666. close(SHLIBS);
  667. return $dep;
  668. }
  669. sub find_symbols_file {
  670. my ($pkg, $soname, $libfile) = @_;
  671. my @files;
  672. if ($pkg eq "") {
  673. # If the file is not packaged, try to find out the symbols file in
  674. # the package being built where the lib has been found
  675. my $pkg_root = guess_pkg_root_dir($libfile);
  676. if (defined $pkg_root) {
  677. push @files, "$pkg_root/DEBIAN/symbols";
  678. }
  679. # Fallback to other symbols files but it shouldn't be necessary
  680. push @files, @pkg_symbols;
  681. } else {
  682. push @files, "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  683. "/etc/dpkg/symbols/$pkg.symbols",
  684. "$admindir/info/$pkg.symbols";
  685. }
  686. foreach my $file (@files) {
  687. if (-e $file and symfile_has_soname($file, $soname)) {
  688. return $file;
  689. }
  690. }
  691. return undef;
  692. }
  693. sub symfile_has_soname {
  694. my ($file, $soname) = @_;
  695. if (exists $symfile_has_soname_cache{$file}{$soname}) {
  696. return $symfile_has_soname_cache{$file}{$soname};
  697. }
  698. open(SYM_FILE, "<", $file) ||
  699. syserr(_g("cannot open file %s"), $file);
  700. my $result = 0;
  701. while (<SYM_FILE>) {
  702. if (/^\Q$soname\E /) {
  703. $result = 1;
  704. last;
  705. }
  706. }
  707. close(SYM_FILE);
  708. $symfile_has_soname_cache{$file}{$soname} = $result;
  709. return $result;
  710. }
  711. # find_library ($soname, \@rpath, $format)
  712. sub my_find_library {
  713. my ($lib, $rpath, $format, $execfile) = @_;
  714. my $file;
  715. # Create real RPATH in case $ORIGIN is used
  716. # Note: ld.so also supports $PLATFORM and $LIB but they are
  717. # used in real case (yet)
  718. my $libdir = relative_to_pkg_root($execfile);
  719. my $origin;
  720. if (defined $libdir) {
  721. $origin = "/$libdir";
  722. $origin =~ s{/+[^/]*$}{};
  723. }
  724. my @RPATH = ();
  725. foreach my $path (@{$rpath}) {
  726. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  727. if (defined $origin) {
  728. $path =~ s/\$ORIGIN/$origin/g;
  729. $path =~ s/\$\{ORIGIN\}/$origin/g;
  730. } else {
  731. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  732. "directory could not be identified due to lack of DEBIAN " .
  733. "sub-directory in the root of package's build tree"), $execfile);
  734. }
  735. }
  736. push @RPATH, $path;
  737. }
  738. # Look into the packages we're currently building in the following
  739. # order:
  740. # - package build tree of the binary which is analyzed
  741. # - package build tree given on the command line (option -S)
  742. # - other package build trees that contain either a shlibs or a
  743. # symbols file
  744. my @builddirs;
  745. my $pkg_root = guess_pkg_root_dir($execfile);
  746. push @builddirs, $pkg_root if defined $pkg_root;
  747. push @builddirs, @pkg_dir_to_search;
  748. push @builddirs, @pkg_root_dirs;
  749. my %dir_checked;
  750. foreach my $builddir (@builddirs) {
  751. next if defined($dir_checked{$builddir});
  752. $file = find_library($lib, \@RPATH, $format, $builddir);
  753. return $file if defined($file);
  754. $dir_checked{$builddir} = 1;
  755. }
  756. # Fallback in the root directory if we have not found what we were
  757. # looking for in the packages
  758. $file = find_library($lib, \@RPATH, $format, "");
  759. return $file if defined($file);
  760. return undef;
  761. }
  762. my %cached_pkgmatch = ();
  763. sub find_packages {
  764. my @files;
  765. my $pkgmatch = {};
  766. foreach (@_) {
  767. if (exists $cached_pkgmatch{$_}) {
  768. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  769. } else {
  770. push @files, $_;
  771. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  772. $pkgmatch->{$_} = [""]; # might be replaced later on
  773. }
  774. }
  775. return $pkgmatch unless scalar(@files);
  776. my $pid = open(DPKG, "-|");
  777. syserr(_g("cannot fork for %s"), "dpkg --search") unless defined($pid);
  778. if (!$pid) {
  779. # Child process running dpkg --search and discarding errors
  780. close STDERR;
  781. open STDERR, ">", "/dev/null";
  782. $ENV{LC_ALL} = "C";
  783. exec("dpkg", "--search", "--", @files)
  784. || syserr(_g("unable to execute %s"), "dpkg");
  785. }
  786. while(defined($_ = <DPKG>)) {
  787. chomp($_);
  788. if (m/^local diversion |^diversion by/) {
  789. warning(_g("diversions involved - output may be incorrect"));
  790. print(STDERR " $_\n")
  791. || syserr(_g("write diversion info to stderr"));
  792. } elsif (m/^([^:]+): (\S+)$/) {
  793. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  794. } else {
  795. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  796. }
  797. }
  798. close(DPKG);
  799. return $pkgmatch;
  800. }