Symbol.pm 13 KB

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