1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <deque>
#include <string>
#include <android-base/stringprintf.h>
#include <unwindstack/Log.h>
#include <unwindstack/Memory.h>
#include <unwindstack/Regs.h>
#include "ArmExidx.h"
#include "Check.h"
#include "Machine.h"
namespace unwindstack {
void ArmExidx::LogRawData() {
std::string log_str("Raw Data:");
for (const uint8_t data : data_) {
log_str += android::base::StringPrintf(" 0x%02x", data);
}
log(log_indent_, log_str.c_str());
}
bool ArmExidx::ExtractEntryData(uint32_t entry_offset) {
data_.clear();
status_ = ARM_STATUS_NONE;
if (entry_offset & 1) {
// The offset needs to be at least two byte aligned.
status_ = ARM_STATUS_INVALID_ALIGNMENT;
return false;
}
// Each entry is a 32 bit prel31 offset followed by 32 bits
// of unwind information. If bit 31 of the unwind data is zero,
// then this is a prel31 offset to the start of the unwind data.
// If the unwind data is 1, then this is a cant unwind entry.
// Otherwise, this data is the compact form of the unwind information.
uint32_t data;
if (!elf_memory_->Read32(entry_offset + 4, &data)) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
if (data == 1) {
// This is a CANT UNWIND entry.
status_ = ARM_STATUS_NO_UNWIND;
if (log_) {
log(log_indent_, "Raw Data: 0x00 0x00 0x00 0x01");
log(log_indent_, "[cantunwind]");
}
return false;
}
if (data & (1UL << 31)) {
// This is a compact table entry.
if ((data >> 24) & 0xf) {
// This is a non-zero index, this code doesn't support
// other formats.
status_ = ARM_STATUS_INVALID_PERSONALITY;
return false;
}
data_.push_back((data >> 16) & 0xff);
data_.push_back((data >> 8) & 0xff);
uint8_t last_op = data & 0xff;
data_.push_back(last_op);
if (last_op != ARM_OP_FINISH) {
// If this didn't end with a finish op, add one.
data_.push_back(ARM_OP_FINISH);
}
if (log_) {
LogRawData();
}
return true;
}
// Get the address of the ops.
// Sign extend the data value if necessary.
int32_t signed_data = static_cast<int32_t>(data << 1) >> 1;
uint32_t addr = (entry_offset + 4) + signed_data;
if (!elf_memory_->Read32(addr, &data)) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
size_t num_table_words;
if (data & (1UL << 31)) {
// Compact model.
switch ((data >> 24) & 0xf) {
case 0:
num_table_words = 0;
data_.push_back((data >> 16) & 0xff);
break;
case 1:
case 2:
num_table_words = (data >> 16) & 0xff;
addr += 4;
break;
default:
// Only a personality of 0, 1, 2 is valid.
status_ = ARM_STATUS_INVALID_PERSONALITY;
return false;
}
data_.push_back((data >> 8) & 0xff);
data_.push_back(data & 0xff);
} else {
// Generic model.
// Skip the personality routine data, it doesn't contain any data
// needed to decode the unwind information.
addr += 4;
if (!elf_memory_->Read32(addr, &data)) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
num_table_words = (data >> 24) & 0xff;
data_.push_back((data >> 16) & 0xff);
data_.push_back((data >> 8) & 0xff);
data_.push_back(data & 0xff);
addr += 4;
}
if (num_table_words > 5) {
status_ = ARM_STATUS_MALFORMED;
return false;
}
for (size_t i = 0; i < num_table_words; i++) {
if (!elf_memory_->Read32(addr, &data)) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
data_.push_back((data >> 24) & 0xff);
data_.push_back((data >> 16) & 0xff);
data_.push_back((data >> 8) & 0xff);
data_.push_back(data & 0xff);
addr += 4;
}
if (data_.back() != ARM_OP_FINISH) {
// If this didn't end with a finish op, add one.
data_.push_back(ARM_OP_FINISH);
}
if (log_) {
LogRawData();
}
return true;
}
inline bool ArmExidx::GetByte(uint8_t* byte) {
if (data_.empty()) {
status_ = ARM_STATUS_TRUNCATED;
return false;
}
*byte = data_.front();
data_.pop_front();
return true;
}
inline bool ArmExidx::DecodePrefix_10_00(uint8_t byte) {
CHECK((byte >> 4) == 0x8);
uint16_t registers = (byte & 0xf) << 8;
if (!GetByte(&byte)) {
return false;
}
registers |= byte;
if (registers == 0) {
// 10000000 00000000: Refuse to unwind
if (log_) {
log(log_indent_, "Refuse to unwind");
}
status_ = ARM_STATUS_NO_UNWIND;
return false;
}
// 1000iiii iiiiiiii: Pop up to 12 integer registers under masks {r15-r12}, {r11-r4}
if (log_) {
bool add_comma = false;
std::string msg = "pop {";
for (size_t i = 0; i < 12; i++) {
if (registers & (1 << i)) {
if (add_comma) {
msg += ", ";
}
msg += android::base::StringPrintf("r%zu", i + 4);
add_comma = true;
}
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
registers <<= 4;
for (size_t reg = 4; reg < 16; reg++) {
if (registers & (1 << reg)) {
if (!process_memory_->Read32(cfa_, &(*regs_)[reg])) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
cfa_ += 4;
}
}
// If the sp register is modified, change the cfa value.
if (registers & (1 << ARM_REG_SP)) {
cfa_ = (*regs_)[ARM_REG_SP];
}
// Indicate if the pc register was set.
if (registers & (1 << ARM_REG_PC)) {
pc_set_ = true;
}
return true;
}
inline bool ArmExidx::DecodePrefix_10_01(uint8_t byte) {
CHECK((byte >> 4) == 0x9);
uint8_t bits = byte & 0xf;
if (bits == 13 || bits == 15) {
// 10011101: Reserved as prefix for ARM register to register moves
// 10011111: Reserved as prefix for Intel Wireless MMX register to register moves
if (log_) {
log(log_indent_, "[Reserved]");
}
status_ = ARM_STATUS_RESERVED;
return false;
}
// 1001nnnn: Set vsp = r[nnnn] (nnnn != 13, 15)
if (log_) {
log(log_indent_, "vsp = r%d", bits);
if (log_skip_execution_) {
return true;
}
}
// It is impossible for bits to be larger than the total number of
// arm registers, so don't bother checking if bits is a valid register.
cfa_ = (*regs_)[bits];
return true;
}
inline bool ArmExidx::DecodePrefix_10_10(uint8_t byte) {
CHECK((byte >> 4) == 0xa);
// 10100nnn: Pop r4-r[4+nnn]
// 10101nnn: Pop r4-r[4+nnn], r14
if (log_) {
std::string msg = "pop {r4";
uint8_t end_reg = byte & 0x7;
if (end_reg) {
msg += android::base::StringPrintf("-r%d", 4 + end_reg);
}
if (byte & 0x8) {
log(log_indent_, "%s, r14}", msg.c_str());
} else {
log(log_indent_, "%s}", msg.c_str());
}
if (log_skip_execution_) {
return true;
}
}
for (size_t i = 4; i <= 4 + (byte & 0x7); i++) {
if (!process_memory_->Read32(cfa_, &(*regs_)[i])) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
cfa_ += 4;
}
if (byte & 0x8) {
if (!process_memory_->Read32(cfa_, &(*regs_)[ARM_REG_R14])) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
cfa_ += 4;
}
return true;
}
inline bool ArmExidx::DecodePrefix_10_11_0000() {
// 10110000: Finish
if (log_) {
log(log_indent_, "finish");
if (log_skip_execution_) {
status_ = ARM_STATUS_FINISH;
return false;
}
}
status_ = ARM_STATUS_FINISH;
return false;
}
inline bool ArmExidx::DecodePrefix_10_11_0001() {
uint8_t byte;
if (!GetByte(&byte)) {
return false;
}
if (byte == 0) {
// 10110001 00000000: Spare
if (log_) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
}
if (byte >> 4) {
// 10110001 xxxxyyyy: Spare (xxxx != 0000)
if (log_) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
}
// 10110001 0000iiii: Pop integer registers under mask {r3, r2, r1, r0}
if (log_) {
bool add_comma = false;
std::string msg = "pop {";
for (size_t i = 0; i < 4; i++) {
if (byte & (1 << i)) {
if (add_comma) {
msg += ", ";
}
msg += android::base::StringPrintf("r%zu", i);
add_comma = true;
}
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
for (size_t reg = 0; reg < 4; reg++) {
if (byte & (1 << reg)) {
if (!process_memory_->Read32(cfa_, &(*regs_)[reg])) {
status_ = ARM_STATUS_READ_FAILED;
return false;
}
cfa_ += 4;
}
}
return true;
}
inline bool ArmExidx::DecodePrefix_10_11_0010() {
// 10110010 uleb128: vsp = vsp + 0x204 + (uleb128 << 2)
uint32_t result = 0;
uint32_t shift = 0;
uint8_t byte;
do {
if (!GetByte(&byte)) {
return false;
}
result |= (byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
result <<= 2;
if (log_) {
log(log_indent_, "vsp = vsp + %d", 0x204 + result);
if (log_skip_execution_) {
return true;
}
}
cfa_ += 0x204 + result;
return true;
}
inline bool ArmExidx::DecodePrefix_10_11_0011() {
// 10110011 sssscccc: Pop VFP double precision registers D[ssss]-D[ssss+cccc] by FSTMFDX
uint8_t byte;
if (!GetByte(&byte)) {
return false;
}
if (log_) {
uint8_t start_reg = byte >> 4;
std::string msg = android::base::StringPrintf("pop {d%d", start_reg);
uint8_t end_reg = start_reg + (byte & 0xf);
if (end_reg) {
msg += android::base::StringPrintf("-d%d", end_reg);
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
cfa_ += (byte & 0xf) * 8 + 12;
return true;
}
inline bool ArmExidx::DecodePrefix_10_11_01nn() {
// 101101nn: Spare
if (log_) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
}
inline bool ArmExidx::DecodePrefix_10_11_1nnn(uint8_t byte) {
CHECK((byte & ~0x07) == 0xb8);
// 10111nnn: Pop VFP double-precision registers D[8]-D[8+nnn] by FSTMFDX
if (log_) {
std::string msg = "pop {d8";
uint8_t last_reg = (byte & 0x7);
if (last_reg) {
msg += android::base::StringPrintf("-d%d", last_reg + 8);
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
// Only update the cfa.
cfa_ += (byte & 0x7) * 8 + 12;
return true;
}
inline bool ArmExidx::DecodePrefix_10(uint8_t byte) {
CHECK((byte >> 6) == 0x2);
switch ((byte >> 4) & 0x3) {
case 0:
return DecodePrefix_10_00(byte);
case 1:
return DecodePrefix_10_01(byte);
case 2:
return DecodePrefix_10_10(byte);
default:
switch (byte & 0xf) {
case 0:
return DecodePrefix_10_11_0000();
case 1:
return DecodePrefix_10_11_0001();
case 2:
return DecodePrefix_10_11_0010();
case 3:
return DecodePrefix_10_11_0011();
default:
if (byte & 0x8) {
return DecodePrefix_10_11_1nnn(byte);
} else {
return DecodePrefix_10_11_01nn();
}
}
}
}
inline bool ArmExidx::DecodePrefix_11_000(uint8_t byte) {
CHECK((byte & ~0x07) == 0xc0);
uint8_t bits = byte & 0x7;
if (bits == 6) {
if (!GetByte(&byte)) {
return false;
}
// 11000110 sssscccc: Intel Wireless MMX pop wR[ssss]-wR[ssss+cccc]
if (log_) {
uint8_t start_reg = byte >> 4;
std::string msg = android::base::StringPrintf("pop {wR%d", start_reg);
uint8_t end_reg = byte & 0xf;
if (end_reg) {
msg += android::base::StringPrintf("-wR%d", start_reg + end_reg);
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
// Only update the cfa.
cfa_ += (byte & 0xf) * 8 + 8;
} else if (bits == 7) {
if (!GetByte(&byte)) {
return false;
}
if (byte == 0) {
// 11000111 00000000: Spare
if (log_) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
} else if ((byte >> 4) == 0) {
// 11000111 0000iiii: Intel Wireless MMX pop wCGR registers {wCGR0,1,2,3}
if (log_) {
bool add_comma = false;
std::string msg = "pop {";
for (size_t i = 0; i < 4; i++) {
if (byte & (1 << i)) {
if (add_comma) {
msg += ", ";
}
msg += android::base::StringPrintf("wCGR%zu", i);
add_comma = true;
}
}
log(log_indent_, "%s}", msg.c_str());
}
// Only update the cfa.
cfa_ += __builtin_popcount(byte) * 4;
} else {
// 11000111 xxxxyyyy: Spare (xxxx != 0000)
if (log_) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
}
} else {
// 11000nnn: Intel Wireless MMX pop wR[10]-wR[10+nnn] (nnn != 6, 7)
if (log_) {
std::string msg = "pop {wR10";
uint8_t nnn = byte & 0x7;
if (nnn) {
msg += android::base::StringPrintf("-wR%d", 10 + nnn);
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
// Only update the cfa.
cfa_ += (byte & 0x7) * 8 + 8;
}
return true;
}
inline bool ArmExidx::DecodePrefix_11_001(uint8_t byte) {
CHECK((byte & ~0x07) == 0xc8);
uint8_t bits = byte & 0x7;
if (bits == 0) {
// 11001000 sssscccc: Pop VFP double precision registers D[16+ssss]-D[16+ssss+cccc] by VPUSH
if (!GetByte(&byte)) {
return false;
}
if (log_) {
uint8_t start_reg = byte >> 4;
std::string msg = android::base::StringPrintf("pop {d%d", 16 + start_reg);
uint8_t end_reg = byte & 0xf;
if (end_reg) {
msg += android::base::StringPrintf("-d%d", 16 + start_reg + end_reg);
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
// Only update the cfa.
cfa_ += (byte & 0xf) * 8 + 8;
} else if (bits == 1) {
// 11001001 sssscccc: Pop VFP double precision registers D[ssss]-D[ssss+cccc] by VPUSH
if (!GetByte(&byte)) {
return false;
}
if (log_) {
uint8_t start_reg = byte >> 4;
std::string msg = android::base::StringPrintf("pop {d%d", start_reg);
uint8_t end_reg = byte & 0xf;
if (end_reg) {
msg += android::base::StringPrintf("-d%d", start_reg + end_reg);
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
// Only update the cfa.
cfa_ += (byte & 0xf) * 8 + 8;
} else {
// 11001yyy: Spare (yyy != 000, 001)
if (log_) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
}
return true;
}
inline bool ArmExidx::DecodePrefix_11_010(uint8_t byte) {
CHECK((byte & ~0x07) == 0xd0);
// 11010nnn: Pop VFP double precision registers D[8]-D[8+nnn] by VPUSH
if (log_) {
std::string msg = "pop {d8";
uint8_t end_reg = byte & 0x7;
if (end_reg) {
msg += android::base::StringPrintf("-d%d", 8 + end_reg);
}
log(log_indent_, "%s}", msg.c_str());
if (log_skip_execution_) {
return true;
}
}
cfa_ += (byte & 0x7) * 8 + 8;
return true;
}
inline bool ArmExidx::DecodePrefix_11(uint8_t byte) {
CHECK((byte >> 6) == 0x3);
switch ((byte >> 3) & 0x7) {
case 0:
return DecodePrefix_11_000(byte);
case 1:
return DecodePrefix_11_001(byte);
case 2:
return DecodePrefix_11_010(byte);
default:
// 11xxxyyy: Spare (xxx != 000, 001, 010)
if (log_) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
}
}
bool ArmExidx::Decode() {
status_ = ARM_STATUS_NONE;
uint8_t byte;
if (!GetByte(&byte)) {
return false;
}
switch (byte >> 6) {
case 0:
// 00xxxxxx: vsp = vsp + (xxxxxxx << 2) + 4
if (log_) {
log(log_indent_, "vsp = vsp + %d", ((byte & 0x3f) << 2) + 4);
if (log_skip_execution_) {
break;
}
}
cfa_ += ((byte & 0x3f) << 2) + 4;
break;
case 1:
// 01xxxxxx: vsp = vsp - (xxxxxxx << 2) + 4
if (log_) {
log(log_indent_, "vsp = vsp - %d", ((byte & 0x3f) << 2) + 4);
if (log_skip_execution_) {
break;
}
}
cfa_ -= ((byte & 0x3f) << 2) + 4;
break;
case 2:
return DecodePrefix_10(byte);
default:
return DecodePrefix_11(byte);
}
return true;
}
bool ArmExidx::Eval() {
pc_set_ = false;
while (Decode());
return status_ == ARM_STATUS_FINISH;
}
} // namespace unwindstack
|