dpkg-gensymbols.pl 9.9 KB

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