Symbol.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2009-2010 Modestas Vainius <modax@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package Dpkg::Shlibs::Symbol;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '0.01';
  20. use Dpkg::Gettext;
  21. use Dpkg::Deps;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Version;
  24. use Storable qw();
  25. use Dpkg::Shlibs::Cppfilt;
  26. # Supported alias types in the order of matching preference
  27. use constant ALIAS_TYPES => qw(c++ symver);
  28. sub new {
  29. my $this = shift;
  30. my $class = ref($this) || $this;
  31. my %args = @_;
  32. my $self = bless {
  33. symbol => undef,
  34. symbol_templ => undef,
  35. minver => undef,
  36. dep_id => 0,
  37. deprecated => 0,
  38. tags => {},
  39. tagorder => [],
  40. }, $class;
  41. $self->{$_} = $args{$_} foreach keys %args;
  42. return $self;
  43. }
  44. # Deep clone
  45. sub clone {
  46. my $self = shift;
  47. my $clone = Storable::dclone($self);
  48. if (@_) {
  49. my %args=@_;
  50. $clone->{$_} = $args{$_} foreach keys %args;
  51. }
  52. return $clone;
  53. }
  54. sub parse_tagspec {
  55. my ($self, $tagspec) = @_;
  56. if ($tagspec =~ /^\s*\((.*?)\)(.*)$/ && $1) {
  57. # (tag1=t1 value|tag2|...|tagN=tNp)
  58. # Symbols ()|= cannot appear in the tag names and values
  59. my $tagspec = $1;
  60. my $rest = ($2) ? $2 : '';
  61. my @tags = split(/\|/, $tagspec);
  62. # Parse each tag
  63. for my $tag (@tags) {
  64. if ($tag =~ /^(.*)=(.*)$/) {
  65. # Tag with value
  66. $self->add_tag($1, $2);
  67. } else {
  68. # Tag without value
  69. $self->add_tag($tag, undef);
  70. }
  71. }
  72. return $rest;
  73. }
  74. return;
  75. }
  76. sub parse_symbolspec {
  77. my ($self, $symbolspec, %opts) = @_;
  78. my $symbol;
  79. my $symbol_templ;
  80. my $symbol_quoted;
  81. my $rest;
  82. if (defined($symbol = $self->parse_tagspec($symbolspec))) {
  83. # (tag1=t1 value|tag2|...|tagN=tNp)"Foo::Bar::foobar()"@Base 1.0 1
  84. # Symbols ()|= cannot appear in the tag names and values
  85. # If the tag specification exists symbol name template might be quoted too
  86. if ($symbol =~ /^(['"])/ && $symbol =~ /^($1)(.*?)$1(.*)$/) {
  87. $symbol_quoted = $1;
  88. $symbol_templ = $2;
  89. $symbol = $2;
  90. $rest = $3;
  91. } else {
  92. if ($symbol =~ m/^(\S+)(.*)$/) {
  93. $symbol_templ = $1;
  94. $symbol = $1;
  95. $rest = $2;
  96. }
  97. }
  98. error(_g('symbol name unspecified: %s'), $symbolspec) if (!$symbol);
  99. } else {
  100. # No tag specification. Symbol name is up to the first space
  101. # foobarsymbol@Base 1.0 1
  102. if ($symbolspec =~ m/^(\S+)(.*)$/) {
  103. $symbol = $1;
  104. $rest = $2;
  105. } else {
  106. return 0;
  107. }
  108. }
  109. $self->{symbol} = $symbol;
  110. $self->{symbol_templ} = $symbol_templ;
  111. $self->{symbol_quoted} = $symbol_quoted if ($symbol_quoted);
  112. # Now parse "the rest" (minver and dep_id)
  113. if ($rest =~ /^\s(\S+)(?:\s(\d+))?/) {
  114. $self->{minver} = $1;
  115. $self->{dep_id} = defined($2) ? $2 : 0;
  116. } elsif (defined $opts{default_minver}) {
  117. $self->{minver} = $opts{default_minver};
  118. $self->{dep_id} = 0;
  119. } else {
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. # A hook for symbol initialization (typically processing of tags). The code
  125. # here may even change symbol name. Called from
  126. # Dpkg::Shlibs::SymbolFile::create_symbol().
  127. sub initialize {
  128. my $self = shift;
  129. # Look for tags marking symbol patterns. The pattern may match multiple
  130. # real symbols.
  131. my $type;
  132. if ($self->has_tag('c++')) {
  133. # Raw symbol name is always demangled to the same alias while demangled
  134. # symbol name cannot be reliably converted back to raw symbol name.
  135. # Therefore, we can use hash for mapping.
  136. $type = 'alias-c++';
  137. }
  138. # Support old style wildcard syntax. That's basically a symver
  139. # with an optional tag.
  140. if ($self->get_symbolname() =~ /^\*@(.*)$/) {
  141. $self->add_tag('symver') unless $self->has_tag('symver');
  142. $self->add_tag('optional') unless $self->has_tag('optional');
  143. $self->{symbol} = $1;
  144. }
  145. if ($self->has_tag('symver')) {
  146. # Each symbol is matched against its version rather than full
  147. # name@version string.
  148. $type = (defined $type) ? 'generic' : 'alias-symver';
  149. if ($self->get_symbolname() eq 'Base') {
  150. error(_g("you can't use symver tag to catch unversioned symbols: %s"),
  151. $self->get_symbolspec(1));
  152. }
  153. }
  154. # As soon as regex is involved, we need to match each real
  155. # symbol against each pattern (aka 'generic' pattern).
  156. if ($self->has_tag('regex')) {
  157. $type = 'generic';
  158. # Pre-compile regular expression for better performance.
  159. my $regex = $self->get_symbolname();
  160. $self->{pattern}{regex} = qr/$regex/;
  161. }
  162. if (defined $type) {
  163. $self->init_pattern($type);
  164. }
  165. }
  166. sub get_symbolname {
  167. return $_[0]->{symbol};
  168. }
  169. sub get_symboltempl {
  170. return $_[0]->{symbol_templ} || $_[0]->{symbol};
  171. }
  172. sub set_symbolname {
  173. my ($self, $name, $templ, $quoted) = @_;
  174. unless (defined $name) {
  175. $name = $self->{symbol};
  176. }
  177. if (!defined $templ && $name =~ /\s/) {
  178. $templ = $name;
  179. }
  180. if (!defined $quoted && defined $templ && $templ =~ /\s/) {
  181. $quoted = '"';
  182. }
  183. $self->{symbol} = $name;
  184. $self->{symbol_templ} = $templ;
  185. if ($quoted) {
  186. $self->{symbol_quoted} = $quoted;
  187. } else {
  188. delete $self->{symbol_quoted};
  189. }
  190. }
  191. sub has_tags {
  192. my $self = shift;
  193. return scalar (@{$self->{tagorder}});
  194. }
  195. sub add_tag {
  196. my ($self, $tagname, $tagval) = @_;
  197. if (exists $self->{tags}{$tagname}) {
  198. $self->{tags}{$tagname} = $tagval;
  199. return 0;
  200. } else {
  201. $self->{tags}{$tagname} = $tagval;
  202. push @{$self->{tagorder}}, $tagname;
  203. }
  204. return 1;
  205. }
  206. sub delete_tag {
  207. my ($self, $tagname) = @_;
  208. if (exists $self->{tags}{$tagname}) {
  209. delete $self->{tags}{$tagname};
  210. $self->{tagorder} = [ grep { $_ ne $tagname } @{$self->{tagorder}} ];
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. sub has_tag {
  216. my ($self, $tag) = @_;
  217. return exists $self->{tags}{$tag};
  218. }
  219. sub get_tag_value {
  220. my ($self, $tag) = @_;
  221. return $self->{tags}{$tag};
  222. }
  223. # Checks if the symbol is equal to another one (by name and optionally,
  224. # tag sets, versioning info (minver and depid))
  225. sub equals {
  226. my ($self, $other, %opts) = @_;
  227. $opts{versioning} = 1 unless exists $opts{versioning};
  228. $opts{tags} = 1 unless exists $opts{tags};
  229. return 0 if $self->{symbol} ne $other->{symbol};
  230. if ($opts{versioning}) {
  231. return 0 if $self->{minver} ne $other->{minver};
  232. return 0 if $self->{dep_id} ne $other->{dep_id};
  233. }
  234. if ($opts{tags}) {
  235. return 0 if scalar(@{$self->{tagorder}}) != scalar(@{$other->{tagorder}});
  236. for (my $i = 0; $i < scalar(@{$self->{tagorder}}); $i++) {
  237. my $tag = $self->{tagorder}->[$i];
  238. return 0 if $tag ne $other->{tagorder}->[$i];
  239. if (defined $self->{tags}{$tag} && defined $other->{tags}{$tag}) {
  240. return 0 if $self->{tags}{$tag} ne $other->{tags}{$tag};
  241. } elsif (defined $self->{tags}{$tag} || defined $other->{tags}{$tag}) {
  242. return 0;
  243. }
  244. }
  245. }
  246. return 1;
  247. }
  248. sub is_optional {
  249. my $self = shift;
  250. return $self->has_tag('optional');
  251. }
  252. sub is_arch_specific {
  253. my $self = shift;
  254. return $self->has_tag('arch');
  255. }
  256. sub arch_is_concerned {
  257. my ($self, $arch) = @_;
  258. my $arches = $self->{tags}{arch};
  259. if (defined $arch && defined $arches) {
  260. my $dep = Dpkg::Deps::Simple->new();
  261. my @arches = split(/[\s,]+/, $arches);
  262. $dep->{package} = 'dummy';
  263. $dep->{arches} = \@arches;
  264. return $dep->arch_is_concerned($arch);
  265. }
  266. return 1;
  267. }
  268. # Get reference to the pattern the symbol matches (if any)
  269. sub get_pattern {
  270. return $_[0]->{matching_pattern};
  271. }
  272. ### NOTE: subroutines below require (or initialize) $self to be a pattern ###
  273. # Initializes this symbol as a pattern of the specified type.
  274. sub init_pattern {
  275. my ($self, $type) = @_;
  276. $self->{pattern}{type} = $type;
  277. # To be filled with references to symbols matching this pattern.
  278. $self->{pattern}{matches} = [];
  279. }
  280. # Is this symbol a pattern or not?
  281. sub is_pattern {
  282. return exists $_[0]->{pattern};
  283. }
  284. # Get pattern type if this symbol is a pattern.
  285. sub get_pattern_type {
  286. return $_[0]->{pattern}{type} || '';
  287. }
  288. # Get (sub)type of the alias pattern. Returns empty string if current
  289. # pattern is not alias.
  290. sub get_alias_type {
  291. return ($_[0]->get_pattern_type() =~ /^alias-(.+)/ && $1) || '';
  292. }
  293. # Get a list of symbols matching this pattern if this symbol is a pattern
  294. sub get_pattern_matches {
  295. return @{$_[0]->{pattern}{matches}};
  296. }
  297. # Create a new symbol based on the pattern (i.e. $self)
  298. # and add it to the pattern matches list.
  299. sub create_pattern_match {
  300. my $self = shift;
  301. return unless $self->is_pattern();
  302. # Leave out 'pattern' subfield while deep-cloning
  303. my $pattern_stuff = $self->{pattern};
  304. delete $self->{pattern};
  305. my $newsym = $self->clone(@_);
  306. $self->{pattern} = $pattern_stuff;
  307. # Clean up symbol name related internal fields
  308. $newsym->set_symbolname();
  309. # Set newsym pattern reference, add to pattern matches list
  310. $newsym->{matching_pattern} = $self;
  311. push @{$self->{pattern}{matches}}, $newsym;
  312. return $newsym;
  313. }
  314. ### END of pattern subroutines ###
  315. # Given a raw symbol name the call returns its alias according to the rules of
  316. # the current pattern ($self). Returns undef if the supplied raw name is not
  317. # transformable to alias.
  318. sub convert_to_alias {
  319. my ($self, $rawname, $type) = @_;
  320. $type = $self->get_alias_type() unless $type;
  321. if ($type) {
  322. if ($type eq 'symver') {
  323. # In case of symver, alias is symbol version. Extract it from the
  324. # rawname.
  325. return "$1" if ($rawname =~ /\@([^@]+)$/);
  326. } elsif ($rawname =~ /^_Z/ && $type eq 'c++') {
  327. return cppfilt_demangle_cpp($rawname);
  328. }
  329. }
  330. return;
  331. }
  332. sub get_tagspec {
  333. my ($self) = @_;
  334. if ($self->has_tags()) {
  335. my @tags;
  336. for my $tagname (@{$self->{tagorder}}) {
  337. my $tagval = $self->{tags}{$tagname};
  338. if (defined $tagval) {
  339. push @tags, $tagname . '=' . $tagval;
  340. } else {
  341. push @tags, $tagname;
  342. }
  343. }
  344. return '(' . join('|', @tags) . ')';
  345. }
  346. return '';
  347. }
  348. sub get_symbolspec {
  349. my $self = shift;
  350. my $template_mode = shift;
  351. my $spec = '';
  352. $spec .= "#MISSING: $self->{deprecated}#" if $self->{deprecated};
  353. $spec .= ' ';
  354. if ($template_mode) {
  355. if ($self->has_tags()) {
  356. $spec .= sprintf('%s%3$s%s%3$s', $self->get_tagspec(),
  357. $self->get_symboltempl(), $self->{symbol_quoted} || '');
  358. } else {
  359. $spec .= $self->get_symboltempl();
  360. }
  361. } else {
  362. $spec .= $self->get_symbolname();
  363. }
  364. $spec .= " $self->{minver}";
  365. $spec .= " $self->{dep_id}" if $self->{dep_id};
  366. return $spec;
  367. }
  368. # Sanitize the symbol when it is confirmed to be found in
  369. # the respective library.
  370. sub mark_found_in_library {
  371. my ($self, $minver, $arch) = @_;
  372. if ($self->{deprecated}) {
  373. # Symbol reappeared somehow
  374. $self->{deprecated} = 0;
  375. $self->{minver} = $minver if (not $self->is_optional());
  376. } else {
  377. # We assume that the right dependency information is already
  378. # there.
  379. if (version_compare($minver, $self->{minver}) < 0) {
  380. $self->{minver} = $minver;
  381. }
  382. }
  383. # Never remove arch tags from patterns
  384. if (not $self->is_pattern()) {
  385. if (not $self->arch_is_concerned($arch)) {
  386. # Remove arch tag because it is incorrect.
  387. $self->delete_tag('arch');
  388. }
  389. }
  390. }
  391. # Sanitize the symbol when it is confirmed to be NOT found in
  392. # the respective library.
  393. # Mark as deprecated those that are no more provided (only if the
  394. # minver is later than the version where the symbol was introduced)
  395. sub mark_not_found_in_library {
  396. my ($self, $minver, $arch) = @_;
  397. # Ignore symbols from foreign arch
  398. return if not $self->arch_is_concerned($arch);
  399. if ($self->{deprecated}) {
  400. # Bump deprecated if the symbol is optional so that it
  401. # keeps reappering in the diff while it's missing
  402. $self->{deprecated} = $minver if $self->is_optional();
  403. } elsif (version_compare($minver, $self->{minver}) > 0) {
  404. $self->{deprecated} = $minver;
  405. }
  406. }
  407. # Checks if the symbol (or pattern) is legitimate as a real symbol for the
  408. # specified architecture.
  409. sub is_legitimate {
  410. my ($self, $arch) = @_;
  411. return ! $self->{deprecated} &&
  412. $self->arch_is_concerned($arch);
  413. }
  414. # Determine whether a supplied raw symbol name matches against current ($self)
  415. # symbol or pattern.
  416. sub matches_rawname {
  417. my ($self, $rawname) = @_;
  418. my $target = $rawname;
  419. my $ok = 1;
  420. my $do_eq_match = 1;
  421. if ($self->is_pattern()) {
  422. # Process pattern tags in the order they were specified.
  423. for my $tag (@{$self->{tagorder}}) {
  424. if (grep { $tag eq $_ } ALIAS_TYPES) {
  425. $ok = not not ($target = $self->convert_to_alias($target, $tag));
  426. } elsif ($tag eq 'regex') {
  427. # Symbol name is a regex. Match it against the target
  428. $do_eq_match = 0;
  429. $ok = ($target =~ $self->{pattern}{regex});
  430. }
  431. last if not $ok;
  432. }
  433. }
  434. # Equality match by default
  435. if ($ok && $do_eq_match) {
  436. $ok = $target eq $self->get_symbolname();
  437. }
  438. return $ok;
  439. }
  440. 1;