dpkg-gensymbols.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-gensymbols
  4. #
  5. # Copyright © 2007 Raphaël Hertzog
  6. # Copyright © 2007-2013 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 <https://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(get_library_paths);
  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"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  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 directory 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 stdout (or <file>), not .../DEBIAN/symbols.
  73. -t write in template mode (tags are not
  74. processed and included in output).
  75. -V verbose output; write deprecated symbols and pattern
  76. matching symbols as comments (in template mode only).
  77. -a<arch> assume <arch> as host architecture when processing
  78. symbol files.
  79. -d display debug information during work.
  80. -?, --help show this help message.
  81. --version show the version.
  82. '), $Dpkg::PROGNAME;
  83. }
  84. my @files;
  85. while (@ARGV) {
  86. $_ = shift(@ARGV);
  87. if (m/^-p/p) {
  88. $oppackage = ${^POSTMATCH};
  89. my $err = pkg_name_is_illegal($oppackage);
  90. error(g_("illegal package name '%s': %s"), $oppackage, $err) if $err;
  91. } elsif (m/^-c(\d)?$/) {
  92. $compare = $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. my @to_add = glob($file);
  105. push @files, @to_add;
  106. warning(g_("pattern '%s' did not match any file"), $file)
  107. unless scalar(@to_add);
  108. }
  109. } elsif (m/^-P(.+)$/) {
  110. $packagebuilddir = $1;
  111. $packagebuilddir =~ s{/+$}{};
  112. } elsif (m/^-O$/) {
  113. $stdout = 1;
  114. } elsif (m/^-I(.+)$/) {
  115. $input = $1;
  116. } elsif (m/^-O(.+)$/) {
  117. $output = $1;
  118. } elsif (m/^-t$/) {
  119. $template_mode = 1;
  120. } elsif (m/^-V$/) {
  121. $verbose_output = 1;
  122. } elsif (m/^-a(.+)$/) {
  123. $host_arch = $1;
  124. } elsif (m/^-(?:\?|-help)$/) {
  125. usage();
  126. exit(0);
  127. } elsif (m/^--version$/) {
  128. version();
  129. exit(0);
  130. } else {
  131. usageerr(g_("unknown option '%s'"), $_);
  132. }
  133. }
  134. umask 0022; # ensure sane default permissions for created files
  135. if (exists $ENV{DPKG_GENSYMBOLS_CHECK_LEVEL}) {
  136. $compare = $ENV{DPKG_GENSYMBOLS_CHECK_LEVEL};
  137. }
  138. if (not defined($sourceversion)) {
  139. my $changelog = changelog_parse();
  140. $sourceversion = $changelog->{'Version'};
  141. }
  142. if (not defined($oppackage)) {
  143. my $control = Dpkg::Control::Info->new();
  144. my @packages = map { $_->{'Package'} } $control->get_packages();
  145. if (@packages == 0) {
  146. error(g_('no package stanza found in control info'));
  147. } elsif (@packages > 1) {
  148. error(g_('must specify package since control info has many (%s)'),
  149. "@packages");
  150. }
  151. $oppackage = $packages[0];
  152. }
  153. my $symfile = Dpkg::Shlibs::SymbolFile->new(arch => $host_arch);
  154. my $ref_symfile = Dpkg::Shlibs::SymbolFile->new(arch => $host_arch);
  155. # Load source-provided symbol information
  156. foreach my $file ($input, $output, "debian/$oppackage.symbols.$host_arch",
  157. "debian/symbols.$host_arch", "debian/$oppackage.symbols",
  158. 'debian/symbols')
  159. {
  160. if (defined $file and -e $file) {
  161. print "Using references symbols from $file\n" if $debug;
  162. $symfile->load($file);
  163. $ref_symfile->load($file) if $compare || ! $quiet;
  164. last;
  165. }
  166. }
  167. # Scan package build dir looking for libraries
  168. if (not scalar @files) {
  169. PATH: foreach my $path (get_library_paths()) {
  170. my $libdir = "$packagebuilddir$path";
  171. $libdir =~ s{/+}{/}g;
  172. lstat $libdir;
  173. next if not -d _;
  174. next if -l _; # Skip directories which are symlinks
  175. # Skip any directory _below_ a symlink as well
  176. my $updir = $libdir;
  177. while (($updir =~ s{/[^/]*$}{}) and
  178. not check_files_are_the_same($packagebuilddir, $updir)) {
  179. next PATH if -l $updir;
  180. }
  181. opendir(my $libdir_dh, "$libdir")
  182. or syserr(g_("can't read directory %s: %s"), $libdir, $!);
  183. push @files, grep {
  184. /(\.so\.|\.so$)/ && -f &&
  185. Dpkg::Shlibs::Objdump::is_elf($_);
  186. } map { "$libdir/$_" } readdir($libdir_dh);
  187. closedir $libdir_dh;
  188. }
  189. }
  190. # Merge symbol information
  191. my $od = Dpkg::Shlibs::Objdump->new();
  192. foreach my $file (@files) {
  193. print "Scanning $file for symbol information\n" if $debug;
  194. my $objid = $od->analyze($file);
  195. unless (defined($objid) && $objid) {
  196. warning(g_("Dpkg::Shlibs::Objdump couldn't parse %s\n"), $file);
  197. next;
  198. }
  199. my $object = $od->get_object($objid);
  200. if ($object->{SONAME}) { # Objects without soname are of no interest
  201. print "Merging symbols from $file as $object->{SONAME}\n" if $debug;
  202. if (not $symfile->has_object($object->{SONAME})) {
  203. $symfile->create_object($object->{SONAME}, "$oppackage #MINVER#");
  204. }
  205. $symfile->merge_symbols($object, $sourceversion);
  206. } else {
  207. print "File $file doesn't have a soname. Ignoring.\n" if $debug;
  208. }
  209. }
  210. $symfile->clear_except(keys %{$od->{objects}});
  211. # Write out symbols files
  212. if ($stdout) {
  213. $output = g_('<standard output>');
  214. $symfile->output(\*STDOUT, package => $oppackage,
  215. template_mode => $template_mode,
  216. with_pattern_matches => $verbose_output,
  217. with_deprecated => $verbose_output);
  218. } else {
  219. unless (defined($output)) {
  220. unless ($symfile->is_empty()) {
  221. $output = "$packagebuilddir/DEBIAN/symbols";
  222. mkdir("$packagebuilddir/DEBIAN") if not -e "$packagebuilddir/DEBIAN";
  223. }
  224. }
  225. if (defined($output)) {
  226. print "Storing symbols in $output.\n" if $debug;
  227. $symfile->save($output, package => $oppackage,
  228. template_mode => $template_mode,
  229. with_pattern_matches => $verbose_output,
  230. with_deprecated => $verbose_output);
  231. } else {
  232. print "No symbol information to store.\n" if $debug;
  233. }
  234. }
  235. # Check if generated files differs from reference file
  236. my $exitcode = 0;
  237. if ($compare || ! $quiet) {
  238. # Compare
  239. if (my @libs = $symfile->get_new_libs($ref_symfile)) {
  240. warning(g_('new libraries appeared in the symbols file: %s'), "@libs")
  241. unless $quiet;
  242. $exitcode = 4 if ($compare >= 4);
  243. }
  244. if (my @libs = $symfile->get_lost_libs($ref_symfile)) {
  245. warning(g_('some libraries disappeared in the symbols file: %s'), "@libs")
  246. unless $quiet;
  247. $exitcode = 3 if ($compare >= 3);
  248. }
  249. if ($symfile->get_new_symbols($ref_symfile)) {
  250. warning(g_('some new symbols appeared in the symbols file: %s'),
  251. g_('see diff output below')) unless $quiet;
  252. $exitcode = 2 if ($compare >= 2);
  253. }
  254. if ($symfile->get_lost_symbols($ref_symfile)) {
  255. warning(g_('some symbols or patterns disappeared in the symbols file: %s'),
  256. g_('see diff output below')) unless $quiet;
  257. $exitcode = 1 if ($compare >= 1);
  258. }
  259. }
  260. unless ($quiet) {
  261. require File::Temp;
  262. require Digest::MD5;
  263. my $file_label;
  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. if ($ref_symfile->{file}) {
  268. $file_label = $ref_symfile->{file};
  269. } else {
  270. $file_label = 'new_symbol_file';
  271. }
  272. $ref_symfile->output($before, package => $oppackage, template_mode => 1);
  273. $symfile->output($after, package => $oppackage, template_mode => 1);
  274. seek $before, 0, 0;
  275. seek $after, 0, 0;
  276. my ($md5_before, $md5_after) = (Digest::MD5->new(), Digest::MD5->new());
  277. $md5_before->addfile($before);
  278. $md5_after->addfile($after);
  279. # Output diffs between symbols files if any
  280. if ($md5_before->hexdigest() ne $md5_after->hexdigest()) {
  281. if (not defined($output)) {
  282. warning(g_('the generated symbols file is empty'));
  283. } elsif (defined($ref_symfile->{file})) {
  284. warning(g_("%s doesn't match completely %s"),
  285. $output, $ref_symfile->{file});
  286. } else {
  287. warning(g_('no debian/symbols file used as basis for generating %s'),
  288. $output);
  289. }
  290. my ($a, $b) = ($before->filename, $after->filename);
  291. my $diff_label = sprintf('%s (%s_%s_%s)', $file_label, $oppackage,
  292. $sourceversion, $host_arch);
  293. system('diff', '-u', '-L', $diff_label, $a, $b) if find_command('diff');
  294. }
  295. }
  296. exit($exitcode);