Symbol.pm 13 KB

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