Symbol.pm 13 KB

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