dpkg-gensymbols.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-gensymbols
  4. #
  5. # Copyright © 2007 Raphaël Hertzog
  6. # Copyright © 2007-2012 Guillem Jover <guillem@debian.org>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. use strict;
  21. use warnings;
  22. use Dpkg;
  23. use Dpkg::Arch qw(get_host_arch);
  24. use Dpkg::Package;
  25. use Dpkg::Shlibs qw(@librarypaths);
  26. use Dpkg::Shlibs::Objdump;
  27. use Dpkg::Shlibs::SymbolFile;
  28. use Dpkg::Gettext;
  29. use Dpkg::ErrorHandling;
  30. use Dpkg::Control::Info;
  31. use Dpkg::Changelog::Parse;
  32. use Dpkg::Path qw(check_files_are_the_same find_command);
  33. textdomain("dpkg-dev");
  34. my $packagebuilddir = 'debian/tmp';
  35. my $sourceversion;
  36. my $stdout;
  37. my $oppackage;
  38. my $compare = 1; # Bail on missing symbols by default
  39. my $quiet = 0;
  40. my $input;
  41. my $output;
  42. my $template_mode = 0; # non-template mode by default
  43. my $verbose_output = 0;
  44. my $debug = 0;
  45. my $host_arch = get_host_arch();
  46. sub version {
  47. printf _g("Debian %s version %s.\n"), $progname, $version;
  48. printf _g("
  49. This is free software; see the GNU General Public License version 2 or
  50. later for copying conditions. There is NO warranty.
  51. ");
  52. }
  53. sub usage {
  54. printf _g(
  55. "Usage: %s [<option>...]")
  56. . "\n\n" . _g(
  57. "Options:
  58. -p<package> generate symbols file for package.
  59. -P<package-build-dir> temporary build dir instead of debian/tmp.
  60. -e<library> explicitly list libraries to scan.
  61. -v<version> version of the packages (defaults to
  62. version extracted from debian/changelog).
  63. -c<level> compare generated symbols file with the reference
  64. template in the debian directory and fail if
  65. difference is too important; level goes from 0 for
  66. no check, to 4 for all checks (default level is 1).
  67. -q keep quiet and never emit any warnings or
  68. generate a diff between generated symbols
  69. file and the reference template.
  70. -I<file> force usage of <file> as reference symbols
  71. file instead of the default file.
  72. -O<file> write to <file>, not .../DEBIAN/symbols.
  73. -O write to stdout, not .../DEBIAN/symbols.
  74. -t write in template mode (tags are not
  75. processed and included in output).
  76. -V verbose output; write deprecated symbols and pattern
  77. matching symbols as comments (in template mode only).
  78. -a<arch> assume <arch> as host architecture when processing
  79. symbol files.
  80. -d display debug information during work.
  81. -?, --help show this help message.
  82. --version show the version.
  83. "), $progname;
  84. }
  85. my @files;
  86. while (@ARGV) {
  87. $_ = shift(@ARGV);
  88. if (m/^-p/) {
  89. $oppackage = $';
  90. my $err = pkg_name_is_illegal($oppackage);
  91. error(_g("illegal package name '%s': %s"), $oppackage, $err) if $err;
  92. } elsif (m/^-c(\d)?$/) {
  93. $compare = defined($1) ? $1 : 1;
  94. } elsif (m/^-q$/) {
  95. $quiet = 1;
  96. } elsif (m/^-d$/) {
  97. $debug = 1;
  98. } elsif (m/^-v(.+)$/) {
  99. $sourceversion = $1;
  100. } elsif (m/^-e(.+)$/) {
  101. my $file = $1;
  102. if (-e $file) {
  103. push @files, $file;
  104. } else {
  105. my @to_add = glob($file);
  106. push @files, @to_add;
  107. warning(_g("pattern '%s' did not match any file"), $file)
  108. unless scalar(@to_add);
  109. }
  110. } elsif (m/^-P(.+)$/) {
  111. $packagebuilddir = $1;
  112. $packagebuilddir =~ s{/+$}{};
  113. } elsif (m/^-O$/) {
  114. $stdout = 1;
  115. } elsif (m/^-I(.+)$/) {
  116. $input = $1;
  117. } elsif (m/^-O(.+)$/) {
  118. $output = $1;
  119. } elsif (m/^-t$/) {
  120. $template_mode = 1;
  121. } elsif (m/^-V$/) {
  122. $verbose_output = 1;
  123. } elsif (m/^-a(.+)$/) {
  124. $host_arch = $1;
  125. } elsif (m/^-(\?|-help)$/) {
  126. usage();
  127. exit(0);
  128. } elsif (m/^--version$/) {
  129. version();
  130. exit(0);
  131. } else {
  132. usageerr(_g("unknown option \`%s'"), $_);
  133. }
  134. }
  135. umask 0022; # ensure sane default permissions for created files
  136. if (exists $ENV{DPKG_GENSYMBOLS_CHECK_LEVEL}) {
  137. $compare = $ENV{DPKG_GENSYMBOLS_CHECK_LEVEL};
  138. }
  139. if (not defined($sourceversion)) {
  140. my $changelog = changelog_parse();
  141. $sourceversion = $changelog->{"Version"};
  142. }
  143. if (not defined($oppackage)) {
  144. my $control = Dpkg::Control::Info->new();
  145. my @packages = map { $_->{'Package'} } $control->get_packages();
  146. if (@packages == 0) {
  147. error(_g("no package stanza found in control info"));
  148. } elsif (@packages > 1) {
  149. error(_g("must specify package since control info has many (%s)"),
  150. "@packages");
  151. }
  152. $oppackage = $packages[0];
  153. }
  154. my $symfile = Dpkg::Shlibs::SymbolFile->new(arch => $host_arch);
  155. my $ref_symfile = Dpkg::Shlibs::SymbolFile->new(arch => $host_arch);
  156. # Load source-provided symbol information
  157. foreach my $file ($input, $output, "debian/$oppackage.symbols.$host_arch",
  158. "debian/symbols.$host_arch", "debian/$oppackage.symbols",
  159. "debian/symbols")
  160. {
  161. if (defined $file and -e $file) {
  162. print "Using references symbols from $file\n" if $debug;
  163. $symfile->load($file);
  164. $ref_symfile->load($file) if $compare || ! $quiet;
  165. last;
  166. }
  167. }
  168. # Scan package build dir looking for libraries
  169. if (not scalar @files) {
  170. PATH: foreach my $path (@librarypaths) {
  171. my $libdir = "$packagebuilddir$path";
  172. $libdir =~ s{/+}{/}g;
  173. lstat $libdir;
  174. next if not -d _;
  175. next if -l _; # Skip directories which are symlinks
  176. # Skip any directory _below_ a symlink as well
  177. my $updir = $libdir;
  178. while (($updir =~ s{/[^/]*$}{}) and
  179. not check_files_are_the_same($packagebuilddir, $updir)) {
  180. next PATH if -l $updir;
  181. }
  182. opendir(my $libdir_dh, "$libdir") ||
  183. syserr(_g("Can't read directory %s: %s"), $libdir, $!);
  184. push @files, grep {
  185. /(\.so\.|\.so$)/ && -f $_ &&
  186. Dpkg::Shlibs::Objdump::is_elf($_);
  187. } map { "$libdir/$_" } readdir($libdir_dh);
  188. closedir $libdir_dh;
  189. }
  190. }
  191. # Merge symbol information
  192. my $od = Dpkg::Shlibs::Objdump->new();
  193. foreach my $file (@files) {
  194. print "Scanning $file for symbol information\n" if $debug;
  195. my $objid = $od->analyze($file);
  196. unless (defined($objid) && $objid) {
  197. warning(_g("Objdump couldn't parse %s\n"), $file);
  198. next;
  199. }
  200. my $object = $od->get_object($objid);
  201. if ($object->{SONAME}) { # Objects without soname are of no interest
  202. print "Merging symbols from $file as $object->{SONAME}\n" if $debug;
  203. if (not $symfile->has_object($object->{SONAME})) {
  204. $symfile->create_object($object->{SONAME}, "$oppackage #MINVER#");
  205. }
  206. $symfile->merge_symbols($object, $sourceversion);
  207. } else {
  208. print "File $file doesn't have a soname. Ignoring.\n" if $debug;
  209. }
  210. }
  211. $symfile->clear_except(keys %{$od->{objects}});
  212. # Write out symbols files
  213. if ($stdout) {
  214. $output = _g("<standard output>");
  215. $symfile->output(\*STDOUT, package => $oppackage,
  216. template_mode => $template_mode,
  217. with_pattern_matches => $verbose_output,
  218. with_deprecated => $verbose_output);
  219. } else {
  220. unless (defined($output)) {
  221. unless($symfile->is_empty()) {
  222. $output = "$packagebuilddir/DEBIAN/symbols";
  223. mkdir("$packagebuilddir/DEBIAN") if not -e "$packagebuilddir/DEBIAN";
  224. }
  225. }
  226. if (defined($output)) {
  227. print "Storing symbols in $output.\n" if $debug;
  228. $symfile->save($output, package => $oppackage,
  229. template_mode => $template_mode,
  230. with_pattern_matches => $verbose_output,
  231. with_deprecated => $verbose_output);
  232. } else {
  233. print "No symbol information to store.\n" if $debug;
  234. }
  235. }
  236. # Check if generated files differs from reference file
  237. my $exitcode = 0;
  238. if ($compare || ! $quiet) {
  239. # Compare
  240. if (my @libs = $symfile->get_new_libs($ref_symfile)) {
  241. warning(_g("new libraries appeared in the symbols file: %s"), "@libs")
  242. unless $quiet;
  243. $exitcode = 4 if ($compare >= 4);
  244. }
  245. if (my @libs = $symfile->get_lost_libs($ref_symfile)) {
  246. warning(_g("some libraries disappeared in the symbols file: %s"), "@libs")
  247. unless $quiet;
  248. $exitcode = 3 if ($compare >= 3);
  249. }
  250. if ($symfile->get_new_symbols($ref_symfile)) {
  251. warning(_g("some new symbols appeared in the symbols file: %s"),
  252. _g("see diff output below")) unless $quiet;
  253. $exitcode = 2 if ($compare >= 2);
  254. }
  255. if ($symfile->get_lost_symbols($ref_symfile)) {
  256. warning(_g("some symbols or patterns disappeared in the symbols file: %s"),
  257. _g("see diff output below")) unless $quiet;
  258. $exitcode = 1 if ($compare >= 1);
  259. }
  260. }
  261. unless ($quiet) {
  262. use File::Temp;
  263. use Digest::MD5;
  264. # Compare template symbols files before and after
  265. my $before = File::Temp->new(TEMPLATE=>'dpkg-gensymbolsXXXXXX');
  266. my $after = File::Temp->new(TEMPLATE=>'dpkg-gensymbolsXXXXXX');
  267. $ref_symfile->output($before, package => $oppackage, template_mode => 1);
  268. $symfile->output($after, package => $oppackage, template_mode => 1);
  269. seek($before, 0, 0); seek($after, 0, 0);
  270. my ($md5_before, $md5_after) = (Digest::MD5->new(), Digest::MD5->new());
  271. $md5_before->addfile($before);
  272. $md5_after->addfile($after);
  273. # Output diffs between symbols files if any
  274. if ($md5_before->hexdigest() ne $md5_after->hexdigest()) {
  275. if (not defined($output)) {
  276. warning(_g("the generated symbols file is empty"));
  277. } elsif (defined($ref_symfile->{file})) {
  278. warning(_g("%s doesn't match completely %s"),
  279. $output, $ref_symfile->{file});
  280. } else {
  281. warning(_g("no debian/symbols file used as basis for generating %s"),
  282. $output);
  283. }
  284. my ($a, $b) = ($before->filename, $after->filename);
  285. my $diff_label = sprintf("%s (%s_%s_%s)",
  286. ($ref_symfile->{file}) ? $ref_symfile->{file} : "new_symbol_file",
  287. $oppackage, $sourceversion, $host_arch);
  288. system("diff", "-u", "-L", $diff_label, $a, $b) if find_command("diff");
  289. }
  290. }
  291. exit($exitcode);