Symbol.pm 13 KB

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