| 1 |
# Copyright (C) 2008-2010 Modestas Vainius <[email protected]> |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 15 |
|
| 16 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst; |
| 17 |
|
| 18 |
# Do not produce subroutine redefined warnings when running this through |
| 19 |
# syntax check. Based on http://www.perlmonks.org/?node_id=389286 |
| 20 |
BEGIN { |
| 21 |
$INC{'Debian/PkgKde/SymbolsHelper/Substs/TypeSubst.pm'} ||= __FILE__; |
| 22 |
} |
| 23 |
|
| 24 |
# Operates on %l% etc. same length types that cannot be presented in demangled |
| 25 |
# symbols. Used by ::Cpp wrapper. |
| 26 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::CppPrivate; |
| 27 |
|
| 28 |
use strict; |
| 29 |
use warnings; |
| 30 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 31 |
|
| 32 |
sub new { |
| 33 |
my ($class, $base) = @_; |
| 34 |
my $self = $class->SUPER::new(); |
| 35 |
$self->{base} = $base; |
| 36 |
$self->{'length'} = 3; # raw type + length('%%') |
| 37 |
$self->{substvar} = '{' . $self->get_name() . '}'; |
| 38 |
$self->{types} = [ map { '%' . $_ . '%' } @{$base->{types}} ]; |
| 39 |
return $self;; |
| 40 |
} |
| 41 |
|
| 42 |
sub get_name { |
| 43 |
my $self = shift; |
| 44 |
return "c++:" . $self->{base}->get_name(); |
| 45 |
} |
| 46 |
|
| 47 |
sub get_types_re { |
| 48 |
my $self = shift; |
| 49 |
unless (exists $self->{types_re}) { |
| 50 |
my $s = '%[' . join("", @{$self->{base}{types}}) . ']%'; |
| 51 |
$self->{types_re} = qr/$s/; |
| 52 |
} |
| 53 |
return $self->{types_re}; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
sub _expand { |
| 58 |
my ($self, $arch) = @_; |
| 59 |
return '%'.$self->{base}->_expand($arch).'%'; |
| 60 |
} |
| 61 |
|
| 62 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::Cpp; |
| 63 |
|
| 64 |
use strict; |
| 65 |
use warnings; |
| 66 |
use base 'Debian::PkgKde::SymbolsHelper::Subst'; |
| 67 |
|
| 68 |
my %CPP_MAP = ( |
| 69 |
m => 'unsigned long', |
| 70 |
j => 'unsigned int', |
| 71 |
i => 'int', |
| 72 |
l => 'long', |
| 73 |
x => 'long long', |
| 74 |
y => 'unsigned long long', |
| 75 |
f => 'float', |
| 76 |
d => 'double', |
| 77 |
); |
| 78 |
|
| 79 |
my %CPPRE_MAP = ( |
| 80 |
'%m%' => qr/\bunsigned long(?! long)\b/, |
| 81 |
'%j%' => qr/\bunsigned int\b/, |
| 82 |
'%i%' => qr/\b(?<!unsigned )int\b/, |
| 83 |
'%l%' => qr/\b(?<!unsigned )long(?! long)\b/, |
| 84 |
'%x%' => qr/\b(?<!unsigned )long long\b/, |
| 85 |
'%y%' => qr/\bunsigned long long\b/, |
| 86 |
'%f%' => qr/\bfloat\b/, |
| 87 |
'%d%' => qr/\bdouble\b/, |
| 88 |
); |
| 89 |
|
| 90 |
sub new { |
| 91 |
my ($class, $base) = @_; |
| 92 |
my $self = $class->SUPER::new(); |
| 93 |
$self->{private} = |
| 94 |
Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::CppPrivate->new($base); |
| 95 |
return $self; |
| 96 |
} |
| 97 |
|
| 98 |
sub _expand { |
| 99 |
my ($self, $arch) = @_; |
| 100 |
return $CPP_MAP{$self->{private}{base}->_expand($arch)}; |
| 101 |
} |
| 102 |
|
| 103 |
sub get_name { |
| 104 |
my $self = shift; |
| 105 |
return $self->{private}->get_name(); |
| 106 |
} |
| 107 |
|
| 108 |
# In order for detect()/neutralize() to work, all substs must be of the same |
| 109 |
# length. Therefore replace demangled names with %l% etc. |
| 110 |
sub prep { |
| 111 |
my ($self, $rawname, $arch) = @_; |
| 112 |
|
| 113 |
# We need to prepare $rawname only once for all Cpp substs |
| 114 |
return if exists $rawname->{cpp_prepped}; |
| 115 |
|
| 116 |
my $str = "$rawname"; |
| 117 |
foreach my $key (keys %CPPRE_MAP) { |
| 118 |
my $re = $CPPRE_MAP{$key}; |
| 119 |
while ($str =~ /$re/g) { |
| 120 |
my $l = length($&); |
| 121 |
$rawname->substr(pos($str)-$l, $l, $key, $&); |
| 122 |
$str = "$rawname" if $l != length($key); |
| 123 |
} |
| 124 |
} |
| 125 |
$rawname->{cpp_prepped} = 1; |
| 126 |
} |
| 127 |
|
| 128 |
sub detect { |
| 129 |
my $self = shift; |
| 130 |
return $self->{private}->detect(@_); |
| 131 |
} |
| 132 |
|
| 133 |
sub neutralize { |
| 134 |
my $self = shift; |
| 135 |
return $self->{private}->neutralize(@_); |
| 136 |
} |
| 137 |
|
| 138 |
sub hinted_neutralize { |
| 139 |
my $self = shift; |
| 140 |
return $self->{private}->hinted_neutralize(@_); |
| 141 |
} |
| 142 |
|
| 143 |
sub verify_at { |
| 144 |
my $self = shift; |
| 145 |
return $self->{private}->verify_at(@_); |
| 146 |
} |
| 147 |
|
| 148 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::size_t; |
| 149 |
|
| 150 |
use strict; |
| 151 |
use warnings; |
| 152 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 153 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 154 |
|
| 155 |
sub new { |
| 156 |
my $class = shift; |
| 157 |
my $self = $class->SUPER::new(@_); |
| 158 |
$self->{substvar} = "{size_t}"; |
| 159 |
$self->{types} = [ qw(m j) ]; # unsigned long / unsigned int |
| 160 |
return $self; |
| 161 |
} |
| 162 |
|
| 163 |
sub _expand { |
| 164 |
my ($self, $arch) = @_; |
| 165 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 166 |
return $bits == 64 ? 'm' : 'j'; |
| 167 |
} |
| 168 |
|
| 169 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::ssize_t; |
| 170 |
|
| 171 |
use strict; |
| 172 |
use warnings; |
| 173 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 174 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 175 |
|
| 176 |
sub new { |
| 177 |
my $class = shift; |
| 178 |
my $self = $class->SUPER::new(@_); |
| 179 |
$self->{substvar} = "{ssize_t}"; |
| 180 |
$self->{types} = [ qw(l i) ]; # long / int |
| 181 |
return $self; |
| 182 |
} |
| 183 |
|
| 184 |
sub _expand { |
| 185 |
my ($self, $arch) = @_; |
| 186 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 187 |
return $bits == 64 ? 'l' : 'i'; |
| 188 |
} |
| 189 |
|
| 190 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::int64_t; |
| 191 |
|
| 192 |
use strict; |
| 193 |
use warnings; |
| 194 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 195 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 196 |
|
| 197 |
sub new { |
| 198 |
my $class = shift; |
| 199 |
my $self = $class->SUPER::new(@_); |
| 200 |
$self->{substvar} = "{int64_t}"; |
| 201 |
$self->{types} = [ qw(l x) ]; # long / long long |
| 202 |
return $self; |
| 203 |
} |
| 204 |
|
| 205 |
sub _expand { |
| 206 |
my ($self, $arch) = @_; |
| 207 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 208 |
return $bits == 64 ? 'l' : 'x'; |
| 209 |
} |
| 210 |
|
| 211 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::uint64_t; |
| 212 |
|
| 213 |
use strict; |
| 214 |
use warnings; |
| 215 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 216 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 217 |
|
| 218 |
sub new { |
| 219 |
my $class = shift; |
| 220 |
my $self = $class->SUPER::new(@_); |
| 221 |
$self->{substvar} = "{uint64_t}"; |
| 222 |
$self->{types} = [ qw(m y) ]; # unsigned long / unsigned long long |
| 223 |
return $self; |
| 224 |
} |
| 225 |
|
| 226 |
sub _expand { |
| 227 |
my ($self, $arch) = @_; |
| 228 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 229 |
return $bits == 64 ? 'm' : 'y'; |
| 230 |
} |
| 231 |
|
| 232 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::qptrdiff; |
| 233 |
|
| 234 |
use strict; |
| 235 |
use warnings; |
| 236 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 237 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 238 |
|
| 239 |
sub new { |
| 240 |
my $class = shift; |
| 241 |
my $self = $class->SUPER::new(@_); |
| 242 |
$self->{substvar} = "{qptrdiff}"; |
| 243 |
$self->{types} = [ qw(x i) ]; # long long / int |
| 244 |
return $self; |
| 245 |
} |
| 246 |
|
| 247 |
sub _expand { |
| 248 |
my ($self, $arch) = @_; |
| 249 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 250 |
return $bits == 64 ? 'x' : 'i'; |
| 251 |
} |
| 252 |
|
| 253 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::quintptr; |
| 254 |
|
| 255 |
use strict; |
| 256 |
use warnings; |
| 257 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 258 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 259 |
|
| 260 |
sub new { |
| 261 |
my $class = shift; |
| 262 |
my $self = $class->SUPER::new(@_); |
| 263 |
$self->{substvar} = "{quintptr}"; |
| 264 |
$self->{types} = [ qw(y j) ]; # unsigned long long / unsigned int |
| 265 |
return $self; |
| 266 |
} |
| 267 |
|
| 268 |
sub _expand { |
| 269 |
my ($self, $arch) = @_; |
| 270 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 271 |
return $bits == 64 ? 'y' : 'j'; |
| 272 |
} |
| 273 |
|
| 274 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::intptr_t; |
| 275 |
|
| 276 |
use strict; |
| 277 |
use warnings; |
| 278 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 279 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 280 |
|
| 281 |
sub new { |
| 282 |
my $class = shift; |
| 283 |
my $self = $class->SUPER::new(@_); |
| 284 |
$self->{substvar} = "{intptr_t}"; |
| 285 |
$self->{types} = [ qw(l i) ]; # long / int |
| 286 |
return $self; |
| 287 |
} |
| 288 |
|
| 289 |
sub _expand { |
| 290 |
my ($self, $arch) = @_; |
| 291 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 292 |
return $bits == 64 ? 'l' : 'i'; |
| 293 |
} |
| 294 |
|
| 295 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::qreal; |
| 296 |
|
| 297 |
use strict; |
| 298 |
use warnings; |
| 299 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 300 |
|
| 301 |
sub new { |
| 302 |
my $class = shift; |
| 303 |
my $self = $class->SUPER::new(@_); |
| 304 |
$self->{substvar} = "{qreal}"; |
| 305 |
$self->{types} = [ qw(d f) ]; # double / float |
| 306 |
return $self; |
| 307 |
} |
| 308 |
|
| 309 |
sub _expand { |
| 310 |
my ($self, $arch) = @_; |
| 311 |
return ($arch =~ /^(arm|armeb|armel|armhf|sh4)$/) ? 'f' : 'd'; |
| 312 |
} |
| 313 |
|
| 314 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::long_double; |
| 315 |
|
| 316 |
use strict; |
| 317 |
use warnings; |
| 318 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 319 |
|
| 320 |
sub new { |
| 321 |
my $class = shift; |
| 322 |
my $self = $class->SUPER::new(@_); |
| 323 |
$self->{substvar} = "{long_double}"; |
| 324 |
$self->{types} = [ qw(e g) ]; # native long double / __float128 |
| 325 |
return $self; |
| 326 |
} |
| 327 |
|
| 328 |
sub _expand { |
| 329 |
my ($self, $arch) = @_; |
| 330 |
return ($arch =~ /^(alpha|powerpc|powerpcspe|ppc64|ppc64el|s390x)$/) ? 'g' : 'e'; |
| 331 |
} |
| 332 |
|
| 333 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst::time_t; |
| 334 |
|
| 335 |
use strict; |
| 336 |
use warnings; |
| 337 |
use base 'Debian::PkgKde::SymbolsHelper::Substs::TypeSubst'; |
| 338 |
use Dpkg::Arch qw(debarch_to_abiattrs); |
| 339 |
|
| 340 |
sub new { |
| 341 |
my $class = shift; |
| 342 |
my $self = $class->SUPER::new(@_); |
| 343 |
$self->{substvar} = "{time_t}"; |
| 344 |
$self->{types} = [ qw(x l) ]; # long long / long |
| 345 |
return $self; |
| 346 |
} |
| 347 |
|
| 348 |
sub _expand { |
| 349 |
my ($self, $arch) = @_; |
| 350 |
my ($bits, $endian) = debarch_to_abiattrs($arch); |
| 351 |
# long long on 32-bit architectures except i386, long on other architectures |
| 352 |
return ($bits == 32 && $arch !~ /^(hurd-|kfreebsd-)?i386$/) ? 'x' : 'l'; |
| 353 |
} |
| 354 |
|
| 355 |
package Debian::PkgKde::SymbolsHelper::Substs::TypeSubst; |
| 356 |
|
| 357 |
use strict; |
| 358 |
use warnings; |
| 359 |
use base 'Debian::PkgKde::SymbolsHelper::Subst'; |
| 360 |
|
| 361 |
# NOTE: recursive |
| 362 |
use Debian::PkgKde::SymbolsHelper::Substs; |
| 363 |
|
| 364 |
sub new { |
| 365 |
my $class = shift; |
| 366 |
my $self = $class->SUPER::new(@_); |
| 367 |
$self->{'length'} = 1; # Basic typesubt must be one letter |
| 368 |
return $self; |
| 369 |
} |
| 370 |
|
| 371 |
sub get_name { |
| 372 |
my $self = shift; |
| 373 |
return substr($self->{substvar}, 1, -1); |
| 374 |
} |
| 375 |
|
| 376 |
sub get_types_re { |
| 377 |
my $self = shift; |
| 378 |
unless (exists $self->{types_re}) { |
| 379 |
my $s = '[' . join("", @{$self->{types}}) . ']'; |
| 380 |
$self->{types_re} = qr/$s/; |
| 381 |
} |
| 382 |
return $self->{types_re}; |
| 383 |
} |
| 384 |
|
| 385 |
sub neutralize { |
| 386 |
my ($self, $rawname) = @_; |
| 387 |
my $ret = 0; |
| 388 |
my $str = "$rawname"; |
| 389 |
my $l = $self->{'length'}; |
| 390 |
my $re = $self->get_types_re(); |
| 391 |
|
| 392 |
while ($str =~ /$re/g) { |
| 393 |
$rawname->substr(pos($str)-$l, $l, $self->{types}->[0]); |
| 394 |
$ret = 1; |
| 395 |
} |
| 396 |
return ($ret) ? $rawname : undef; |
| 397 |
} |
| 398 |
|
| 399 |
sub hinted_neutralize { |
| 400 |
my ($self, $rawname, $hint) = @_; |
| 401 |
my $hintstr = $hint->{str2}; |
| 402 |
my $ret = 1; |
| 403 |
my $l = $self->{'length'}; |
| 404 |
|
| 405 |
for (my $i = 0; $i < @$hintstr; $i++) { |
| 406 |
if (defined $hintstr->[$i] && $hintstr->[$i] eq $self->{substvar}) { |
| 407 |
$rawname->substr($i, $l, $self->{types}->[0], $self->{substvar}); |
| 408 |
$ret = 1; |
| 409 |
} |
| 410 |
} |
| 411 |
return ($ret) ? $rawname : undef; |
| 412 |
} |
| 413 |
|
| 414 |
sub detect { |
| 415 |
my ($self, $rawname, $arch, $arch_rawnames) = @_; |
| 416 |
|
| 417 |
my $l = $self->{'length'}; |
| 418 |
my $s1 = $rawname; |
| 419 |
my $t1 = $self->expand($arch); |
| 420 |
my ($s2, $t2); |
| 421 |
|
| 422 |
# Find architecture with other type |
| 423 |
my @keys = keys %$arch_rawnames; |
| 424 |
# Reorder the keys so *-i386 architectures come first. They are the only architectures |
| 425 |
# where the results of int64_t and time_t substitutions differ, so we must look there |
| 426 |
# to decide which of them to use. |
| 427 |
@keys = (grep(/^(hurd-|kfreebsd-)?i386$/, @keys), grep(!/^(hurd-|kfreebsd-)?i386$/, @keys)); |
| 428 |
foreach my $a2 (@keys) { |
| 429 |
$t2 = $self->expand($a2); |
| 430 |
if ($t2 ne $t1) { |
| 431 |
$s2 = $arch_rawnames->{$a2}; |
| 432 |
last; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
return 0 unless defined $s2; |
| 437 |
|
| 438 |
# Verify subst and replace it with types[0] and substvar |
| 439 |
my $ret = 0; |
| 440 |
search_next: for (my $pos = 0; ($pos = index($s1, $t1, $pos)) != -1; $pos++) { |
| 441 |
# Verify on the selected $a2 |
| 442 |
if ($t2 eq substr($s2, $pos, $l)) { |
| 443 |
# Maybe subst is already there? |
| 444 |
if ($rawname->has_string2() && |
| 445 |
(my $char = $rawname->get_string2_char($pos))) |
| 446 |
{ |
| 447 |
if ($char ne $self->{substvar} && $char =~ /^{(.*)}$/) { |
| 448 |
# Another subst. Verify it |
| 449 |
# NOTE: %SUBSTS might not work here due to recursive "use" |
| 450 |
my $othersubst = $Debian::PkgKde::SymbolsHelper::Substs::SUBSTS{$1}; |
| 451 |
if (defined $othersubst && $othersubst->verify_at($pos, $arch_rawnames)) { |
| 452 |
$ret = 1; |
| 453 |
next search_next; |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
# Now verify detection on other arches |
| 458 |
if ($self->verify_at($pos, $arch_rawnames)) { |
| 459 |
$rawname->substr($pos, $l, $self->{types}->[0], $self->{substvar}); |
| 460 |
$ret = 1; |
| 461 |
$pos += $l-1; |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
return $ret; |
| 466 |
} |
| 467 |
|
| 468 |
sub verify_at { |
| 469 |
my ($self, $pos, $arch_rawnames) = @_; |
| 470 |
my $l = $self->{'length'}; |
| 471 |
my $verified = 1; |
| 472 |
foreach my $a (keys %$arch_rawnames) { |
| 473 |
my $t = $self->expand($a); |
| 474 |
if ($t ne substr($arch_rawnames->{$a}, $pos, $l)) { |
| 475 |
$verified = 0; |
| 476 |
last; |
| 477 |
} |
| 478 |
} |
| 479 |
return $verified; |
| 480 |
} |
| 481 |
|
| 482 |
1; |