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