tools/profiler/LulDwarf.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3
michael@0 4 // Copyright (c) 2010 Google Inc. All Rights Reserved.
michael@0 5 //
michael@0 6 // Redistribution and use in source and binary forms, with or without
michael@0 7 // modification, are permitted provided that the following conditions are
michael@0 8 // met:
michael@0 9 //
michael@0 10 // * Redistributions of source code must retain the above copyright
michael@0 11 // notice, this list of conditions and the following disclaimer.
michael@0 12 // * Redistributions in binary form must reproduce the above
michael@0 13 // copyright notice, this list of conditions and the following disclaimer
michael@0 14 // in the documentation and/or other materials provided with the
michael@0 15 // distribution.
michael@0 16 // * Neither the name of Google Inc. nor the names of its
michael@0 17 // contributors may be used to endorse or promote products derived from
michael@0 18 // this software without specific prior written permission.
michael@0 19 //
michael@0 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31
michael@0 32 // CFI reader author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
michael@0 33 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
michael@0 34
michael@0 35 // Implementation of dwarf2reader::LineInfo, dwarf2reader::CompilationUnit,
michael@0 36 // and dwarf2reader::CallFrameInfo. See dwarf2reader.h for details.
michael@0 37
michael@0 38 // This file is derived from the following files in
michael@0 39 // toolkit/crashreporter/google-breakpad:
michael@0 40 // src/common/dwarf/bytereader.cc
michael@0 41 // src/common/dwarf/dwarf2reader.cc
michael@0 42 // src/common/dwarf_cfi_to_module.cc
michael@0 43
michael@0 44 #include <stdint.h>
michael@0 45 #include <stdio.h>
michael@0 46 #include <string.h>
michael@0 47 #include <stdlib.h>
michael@0 48
michael@0 49 #include <map>
michael@0 50 #include <stack>
michael@0 51 #include <string>
michael@0 52
michael@0 53 #include "mozilla/Assertions.h"
michael@0 54
michael@0 55 #include "LulCommonExt.h"
michael@0 56 #include "LulDwarfInt.h"
michael@0 57
michael@0 58 // Set this to 1 for verbose logging
michael@0 59 #define DEBUG_DWARF 0
michael@0 60
michael@0 61
michael@0 62 namespace lul {
michael@0 63
michael@0 64 using std::string;
michael@0 65
michael@0 66 ByteReader::ByteReader(enum Endianness endian)
michael@0 67 :offset_reader_(NULL), address_reader_(NULL), endian_(endian),
michael@0 68 address_size_(0), offset_size_(0),
michael@0 69 have_section_base_(), have_text_base_(), have_data_base_(),
michael@0 70 have_function_base_() { }
michael@0 71
michael@0 72 ByteReader::~ByteReader() { }
michael@0 73
michael@0 74 void ByteReader::SetOffsetSize(uint8 size) {
michael@0 75 offset_size_ = size;
michael@0 76 MOZ_ASSERT(size == 4 || size == 8);
michael@0 77 if (size == 4) {
michael@0 78 this->offset_reader_ = &ByteReader::ReadFourBytes;
michael@0 79 } else {
michael@0 80 this->offset_reader_ = &ByteReader::ReadEightBytes;
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 void ByteReader::SetAddressSize(uint8 size) {
michael@0 85 address_size_ = size;
michael@0 86 MOZ_ASSERT(size == 4 || size == 8);
michael@0 87 if (size == 4) {
michael@0 88 this->address_reader_ = &ByteReader::ReadFourBytes;
michael@0 89 } else {
michael@0 90 this->address_reader_ = &ByteReader::ReadEightBytes;
michael@0 91 }
michael@0 92 }
michael@0 93
michael@0 94 uint64 ByteReader::ReadInitialLength(const char* start, size_t* len) {
michael@0 95 const uint64 initial_length = ReadFourBytes(start);
michael@0 96 start += 4;
michael@0 97
michael@0 98 // In DWARF2/3, if the initial length is all 1 bits, then the offset
michael@0 99 // size is 8 and we need to read the next 8 bytes for the real length.
michael@0 100 if (initial_length == 0xffffffff) {
michael@0 101 SetOffsetSize(8);
michael@0 102 *len = 12;
michael@0 103 return ReadOffset(start);
michael@0 104 } else {
michael@0 105 SetOffsetSize(4);
michael@0 106 *len = 4;
michael@0 107 }
michael@0 108 return initial_length;
michael@0 109 }
michael@0 110
michael@0 111 bool ByteReader::ValidEncoding(DwarfPointerEncoding encoding) const {
michael@0 112 if (encoding == DW_EH_PE_omit) return true;
michael@0 113 if (encoding == DW_EH_PE_aligned) return true;
michael@0 114 if ((encoding & 0x7) > DW_EH_PE_udata8)
michael@0 115 return false;
michael@0 116 if ((encoding & 0x70) > DW_EH_PE_funcrel)
michael@0 117 return false;
michael@0 118 return true;
michael@0 119 }
michael@0 120
michael@0 121 bool ByteReader::UsableEncoding(DwarfPointerEncoding encoding) const {
michael@0 122 switch (encoding & 0x70) {
michael@0 123 case DW_EH_PE_absptr: return true;
michael@0 124 case DW_EH_PE_pcrel: return have_section_base_;
michael@0 125 case DW_EH_PE_textrel: return have_text_base_;
michael@0 126 case DW_EH_PE_datarel: return have_data_base_;
michael@0 127 case DW_EH_PE_funcrel: return have_function_base_;
michael@0 128 default: return false;
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 uint64 ByteReader::ReadEncodedPointer(const char *buffer,
michael@0 133 DwarfPointerEncoding encoding,
michael@0 134 size_t *len) const {
michael@0 135 // UsableEncoding doesn't approve of DW_EH_PE_omit, so we shouldn't
michael@0 136 // see it here.
michael@0 137 MOZ_ASSERT(encoding != DW_EH_PE_omit);
michael@0 138
michael@0 139 // The Linux Standards Base 4.0 does not make this clear, but the
michael@0 140 // GNU tools (gcc/unwind-pe.h; readelf/dwarf.c; gdb/dwarf2-frame.c)
michael@0 141 // agree that aligned pointers are always absolute, machine-sized,
michael@0 142 // machine-signed pointers.
michael@0 143 if (encoding == DW_EH_PE_aligned) {
michael@0 144 MOZ_ASSERT(have_section_base_);
michael@0 145
michael@0 146 // We don't need to align BUFFER in *our* address space. Rather, we
michael@0 147 // need to find the next position in our buffer that would be aligned
michael@0 148 // when the .eh_frame section the buffer contains is loaded into the
michael@0 149 // program's memory. So align assuming that buffer_base_ gets loaded at
michael@0 150 // address section_base_, where section_base_ itself may or may not be
michael@0 151 // aligned.
michael@0 152
michael@0 153 // First, find the offset to START from the closest prior aligned
michael@0 154 // address.
michael@0 155 uint64 skew = section_base_ & (AddressSize() - 1);
michael@0 156 // Now find the offset from that aligned address to buffer.
michael@0 157 uint64 offset = skew + (buffer - buffer_base_);
michael@0 158 // Round up to the next boundary.
michael@0 159 uint64 aligned = (offset + AddressSize() - 1) & -AddressSize();
michael@0 160 // Convert back to a pointer.
michael@0 161 const char *aligned_buffer = buffer_base_ + (aligned - skew);
michael@0 162 // Finally, store the length and actually fetch the pointer.
michael@0 163 *len = aligned_buffer - buffer + AddressSize();
michael@0 164 return ReadAddress(aligned_buffer);
michael@0 165 }
michael@0 166
michael@0 167 // Extract the value first, ignoring whether it's a pointer or an
michael@0 168 // offset relative to some base.
michael@0 169 uint64 offset;
michael@0 170 switch (encoding & 0x0f) {
michael@0 171 case DW_EH_PE_absptr:
michael@0 172 // DW_EH_PE_absptr is weird, as it is used as a meaningful value for
michael@0 173 // both the high and low nybble of encoding bytes. When it appears in
michael@0 174 // the high nybble, it means that the pointer is absolute, not an
michael@0 175 // offset from some base address. When it appears in the low nybble,
michael@0 176 // as here, it means that the pointer is stored as a normal
michael@0 177 // machine-sized and machine-signed address. A low nybble of
michael@0 178 // DW_EH_PE_absptr does not imply that the pointer is absolute; it is
michael@0 179 // correct for us to treat the value as an offset from a base address
michael@0 180 // if the upper nybble is not DW_EH_PE_absptr.
michael@0 181 offset = ReadAddress(buffer);
michael@0 182 *len = AddressSize();
michael@0 183 break;
michael@0 184
michael@0 185 case DW_EH_PE_uleb128:
michael@0 186 offset = ReadUnsignedLEB128(buffer, len);
michael@0 187 break;
michael@0 188
michael@0 189 case DW_EH_PE_udata2:
michael@0 190 offset = ReadTwoBytes(buffer);
michael@0 191 *len = 2;
michael@0 192 break;
michael@0 193
michael@0 194 case DW_EH_PE_udata4:
michael@0 195 offset = ReadFourBytes(buffer);
michael@0 196 *len = 4;
michael@0 197 break;
michael@0 198
michael@0 199 case DW_EH_PE_udata8:
michael@0 200 offset = ReadEightBytes(buffer);
michael@0 201 *len = 8;
michael@0 202 break;
michael@0 203
michael@0 204 case DW_EH_PE_sleb128:
michael@0 205 offset = ReadSignedLEB128(buffer, len);
michael@0 206 break;
michael@0 207
michael@0 208 case DW_EH_PE_sdata2:
michael@0 209 offset = ReadTwoBytes(buffer);
michael@0 210 // Sign-extend from 16 bits.
michael@0 211 offset = (offset ^ 0x8000) - 0x8000;
michael@0 212 *len = 2;
michael@0 213 break;
michael@0 214
michael@0 215 case DW_EH_PE_sdata4:
michael@0 216 offset = ReadFourBytes(buffer);
michael@0 217 // Sign-extend from 32 bits.
michael@0 218 offset = (offset ^ 0x80000000ULL) - 0x80000000ULL;
michael@0 219 *len = 4;
michael@0 220 break;
michael@0 221
michael@0 222 case DW_EH_PE_sdata8:
michael@0 223 // No need to sign-extend; this is the full width of our type.
michael@0 224 offset = ReadEightBytes(buffer);
michael@0 225 *len = 8;
michael@0 226 break;
michael@0 227
michael@0 228 default:
michael@0 229 abort();
michael@0 230 }
michael@0 231
michael@0 232 // Find the appropriate base address.
michael@0 233 uint64 base;
michael@0 234 switch (encoding & 0x70) {
michael@0 235 case DW_EH_PE_absptr:
michael@0 236 base = 0;
michael@0 237 break;
michael@0 238
michael@0 239 case DW_EH_PE_pcrel:
michael@0 240 MOZ_ASSERT(have_section_base_);
michael@0 241 base = section_base_ + (buffer - buffer_base_);
michael@0 242 break;
michael@0 243
michael@0 244 case DW_EH_PE_textrel:
michael@0 245 MOZ_ASSERT(have_text_base_);
michael@0 246 base = text_base_;
michael@0 247 break;
michael@0 248
michael@0 249 case DW_EH_PE_datarel:
michael@0 250 MOZ_ASSERT(have_data_base_);
michael@0 251 base = data_base_;
michael@0 252 break;
michael@0 253
michael@0 254 case DW_EH_PE_funcrel:
michael@0 255 MOZ_ASSERT(have_function_base_);
michael@0 256 base = function_base_;
michael@0 257 break;
michael@0 258
michael@0 259 default:
michael@0 260 abort();
michael@0 261 }
michael@0 262
michael@0 263 uint64 pointer = base + offset;
michael@0 264
michael@0 265 // Remove inappropriate upper bits.
michael@0 266 if (AddressSize() == 4)
michael@0 267 pointer = pointer & 0xffffffff;
michael@0 268 else
michael@0 269 MOZ_ASSERT(AddressSize() == sizeof(uint64));
michael@0 270
michael@0 271 return pointer;
michael@0 272 }
michael@0 273
michael@0 274
michael@0 275 // A DWARF rule for recovering the address or value of a register, or
michael@0 276 // computing the canonical frame address. There is one subclass of this for
michael@0 277 // each '*Rule' member function in CallFrameInfo::Handler.
michael@0 278 //
michael@0 279 // It's annoying that we have to handle Rules using pointers (because
michael@0 280 // the concrete instances can have an arbitrary size). They're small,
michael@0 281 // so it would be much nicer if we could just handle them by value
michael@0 282 // instead of fretting about ownership and destruction.
michael@0 283 //
michael@0 284 // It seems like all these could simply be instances of std::tr1::bind,
michael@0 285 // except that we need instances to be EqualityComparable, too.
michael@0 286 //
michael@0 287 // This could logically be nested within State, but then the qualified names
michael@0 288 // get horrendous.
michael@0 289 class CallFrameInfo::Rule {
michael@0 290 public:
michael@0 291 virtual ~Rule() { }
michael@0 292
michael@0 293 // Tell HANDLER that, at ADDRESS in the program, REGISTER can be
michael@0 294 // recovered using this rule. If REGISTER is kCFARegister, then this rule
michael@0 295 // describes how to compute the canonical frame address. Return what the
michael@0 296 // HANDLER member function returned.
michael@0 297 virtual bool Handle(Handler *handler,
michael@0 298 uint64 address, int register) const = 0;
michael@0 299
michael@0 300 // Equality on rules. We use these to decide which rules we need
michael@0 301 // to report after a DW_CFA_restore_state instruction.
michael@0 302 virtual bool operator==(const Rule &rhs) const = 0;
michael@0 303
michael@0 304 bool operator!=(const Rule &rhs) const { return ! (*this == rhs); }
michael@0 305
michael@0 306 // Return a pointer to a copy of this rule.
michael@0 307 virtual Rule *Copy() const = 0;
michael@0 308
michael@0 309 // If this is a base+offset rule, change its base register to REG.
michael@0 310 // Otherwise, do nothing. (Ugly, but required for DW_CFA_def_cfa_register.)
michael@0 311 virtual void SetBaseRegister(unsigned reg) { }
michael@0 312
michael@0 313 // If this is a base+offset rule, change its offset to OFFSET. Otherwise,
michael@0 314 // do nothing. (Ugly, but required for DW_CFA_def_cfa_offset.)
michael@0 315 virtual void SetOffset(long long offset) { }
michael@0 316
michael@0 317 // A RTTI workaround, to make it possible to implement equality
michael@0 318 // comparisons on classes derived from this one.
michael@0 319 enum CFIRTag {
michael@0 320 CFIR_UNDEFINED_RULE,
michael@0 321 CFIR_SAME_VALUE_RULE,
michael@0 322 CFIR_OFFSET_RULE,
michael@0 323 CFIR_VAL_OFFSET_RULE,
michael@0 324 CFIR_REGISTER_RULE,
michael@0 325 CFIR_EXPRESSION_RULE,
michael@0 326 CFIR_VAL_EXPRESSION_RULE
michael@0 327 };
michael@0 328
michael@0 329 // Produce the tag that identifies the child class of this object.
michael@0 330 virtual CFIRTag getTag() const = 0;
michael@0 331 };
michael@0 332
michael@0 333 // Rule: the value the register had in the caller cannot be recovered.
michael@0 334 class CallFrameInfo::UndefinedRule: public CallFrameInfo::Rule {
michael@0 335 public:
michael@0 336 UndefinedRule() { }
michael@0 337 ~UndefinedRule() { }
michael@0 338 CFIRTag getTag() const { return CFIR_UNDEFINED_RULE; }
michael@0 339 bool Handle(Handler *handler, uint64 address, int reg) const {
michael@0 340 return handler->UndefinedRule(address, reg);
michael@0 341 }
michael@0 342 bool operator==(const Rule &rhs) const {
michael@0 343 if (rhs.getTag() != CFIR_UNDEFINED_RULE) return false;
michael@0 344 return true;
michael@0 345 }
michael@0 346 Rule *Copy() const { return new UndefinedRule(*this); }
michael@0 347 };
michael@0 348
michael@0 349 // Rule: the register's value is the same as that it had in the caller.
michael@0 350 class CallFrameInfo::SameValueRule: public CallFrameInfo::Rule {
michael@0 351 public:
michael@0 352 SameValueRule() { }
michael@0 353 ~SameValueRule() { }
michael@0 354 CFIRTag getTag() const { return CFIR_SAME_VALUE_RULE; }
michael@0 355 bool Handle(Handler *handler, uint64 address, int reg) const {
michael@0 356 return handler->SameValueRule(address, reg);
michael@0 357 }
michael@0 358 bool operator==(const Rule &rhs) const {
michael@0 359 if (rhs.getTag() != CFIR_SAME_VALUE_RULE) return false;
michael@0 360 return true;
michael@0 361 }
michael@0 362 Rule *Copy() const { return new SameValueRule(*this); }
michael@0 363 };
michael@0 364
michael@0 365 // Rule: the register is saved at OFFSET from BASE_REGISTER. BASE_REGISTER
michael@0 366 // may be CallFrameInfo::Handler::kCFARegister.
michael@0 367 class CallFrameInfo::OffsetRule: public CallFrameInfo::Rule {
michael@0 368 public:
michael@0 369 OffsetRule(int base_register, long offset)
michael@0 370 : base_register_(base_register), offset_(offset) { }
michael@0 371 ~OffsetRule() { }
michael@0 372 CFIRTag getTag() const { return CFIR_OFFSET_RULE; }
michael@0 373 bool Handle(Handler *handler, uint64 address, int reg) const {
michael@0 374 return handler->OffsetRule(address, reg, base_register_, offset_);
michael@0 375 }
michael@0 376 bool operator==(const Rule &rhs) const {
michael@0 377 if (rhs.getTag() != CFIR_OFFSET_RULE) return false;
michael@0 378 const OffsetRule *our_rhs = static_cast<const OffsetRule *>(&rhs);
michael@0 379 return (base_register_ == our_rhs->base_register_ &&
michael@0 380 offset_ == our_rhs->offset_);
michael@0 381 }
michael@0 382 Rule *Copy() const { return new OffsetRule(*this); }
michael@0 383 // We don't actually need SetBaseRegister or SetOffset here, since they
michael@0 384 // are only ever applied to CFA rules, for DW_CFA_def_cfa_offset, and it
michael@0 385 // doesn't make sense to use OffsetRule for computing the CFA: it
michael@0 386 // computes the address at which a register is saved, not a value.
michael@0 387 private:
michael@0 388 int base_register_;
michael@0 389 long offset_;
michael@0 390 };
michael@0 391
michael@0 392 // Rule: the value the register had in the caller is the value of
michael@0 393 // BASE_REGISTER plus offset. BASE_REGISTER may be
michael@0 394 // CallFrameInfo::Handler::kCFARegister.
michael@0 395 class CallFrameInfo::ValOffsetRule: public CallFrameInfo::Rule {
michael@0 396 public:
michael@0 397 ValOffsetRule(int base_register, long offset)
michael@0 398 : base_register_(base_register), offset_(offset) { }
michael@0 399 ~ValOffsetRule() { }
michael@0 400 CFIRTag getTag() const { return CFIR_VAL_OFFSET_RULE; }
michael@0 401 bool Handle(Handler *handler, uint64 address, int reg) const {
michael@0 402 return handler->ValOffsetRule(address, reg, base_register_, offset_);
michael@0 403 }
michael@0 404 bool operator==(const Rule &rhs) const {
michael@0 405 if (rhs.getTag() != CFIR_VAL_OFFSET_RULE) return false;
michael@0 406 const ValOffsetRule *our_rhs = static_cast<const ValOffsetRule *>(&rhs);
michael@0 407 return (base_register_ == our_rhs->base_register_ &&
michael@0 408 offset_ == our_rhs->offset_);
michael@0 409 }
michael@0 410 Rule *Copy() const { return new ValOffsetRule(*this); }
michael@0 411 void SetBaseRegister(unsigned reg) { base_register_ = reg; }
michael@0 412 void SetOffset(long long offset) { offset_ = offset; }
michael@0 413 private:
michael@0 414 int base_register_;
michael@0 415 long offset_;
michael@0 416 };
michael@0 417
michael@0 418 // Rule: the register has been saved in another register REGISTER_NUMBER_.
michael@0 419 class CallFrameInfo::RegisterRule: public CallFrameInfo::Rule {
michael@0 420 public:
michael@0 421 explicit RegisterRule(int register_number)
michael@0 422 : register_number_(register_number) { }
michael@0 423 ~RegisterRule() { }
michael@0 424 CFIRTag getTag() const { return CFIR_REGISTER_RULE; }
michael@0 425 bool Handle(Handler *handler, uint64 address, int reg) const {
michael@0 426 return handler->RegisterRule(address, reg, register_number_);
michael@0 427 }
michael@0 428 bool operator==(const Rule &rhs) const {
michael@0 429 if (rhs.getTag() != CFIR_REGISTER_RULE) return false;
michael@0 430 const RegisterRule *our_rhs = static_cast<const RegisterRule *>(&rhs);
michael@0 431 return (register_number_ == our_rhs->register_number_);
michael@0 432 }
michael@0 433 Rule *Copy() const { return new RegisterRule(*this); }
michael@0 434 private:
michael@0 435 int register_number_;
michael@0 436 };
michael@0 437
michael@0 438 // Rule: EXPRESSION evaluates to the address at which the register is saved.
michael@0 439 class CallFrameInfo::ExpressionRule: public CallFrameInfo::Rule {
michael@0 440 public:
michael@0 441 explicit ExpressionRule(const string &expression)
michael@0 442 : expression_(expression) { }
michael@0 443 ~ExpressionRule() { }
michael@0 444 CFIRTag getTag() const { return CFIR_EXPRESSION_RULE; }
michael@0 445 bool Handle(Handler *handler, uint64 address, int reg) const {
michael@0 446 return handler->ExpressionRule(address, reg, expression_);
michael@0 447 }
michael@0 448 bool operator==(const Rule &rhs) const {
michael@0 449 if (rhs.getTag() != CFIR_EXPRESSION_RULE) return false;
michael@0 450 const ExpressionRule *our_rhs = static_cast<const ExpressionRule *>(&rhs);
michael@0 451 return (expression_ == our_rhs->expression_);
michael@0 452 }
michael@0 453 Rule *Copy() const { return new ExpressionRule(*this); }
michael@0 454 private:
michael@0 455 string expression_;
michael@0 456 };
michael@0 457
michael@0 458 // Rule: EXPRESSION evaluates to the previous value of the register.
michael@0 459 class CallFrameInfo::ValExpressionRule: public CallFrameInfo::Rule {
michael@0 460 public:
michael@0 461 explicit ValExpressionRule(const string &expression)
michael@0 462 : expression_(expression) { }
michael@0 463 ~ValExpressionRule() { }
michael@0 464 CFIRTag getTag() const { return CFIR_VAL_EXPRESSION_RULE; }
michael@0 465 bool Handle(Handler *handler, uint64 address, int reg) const {
michael@0 466 return handler->ValExpressionRule(address, reg, expression_);
michael@0 467 }
michael@0 468 bool operator==(const Rule &rhs) const {
michael@0 469 if (rhs.getTag() != CFIR_VAL_EXPRESSION_RULE) return false;
michael@0 470 const ValExpressionRule *our_rhs =
michael@0 471 static_cast<const ValExpressionRule *>(&rhs);
michael@0 472 return (expression_ == our_rhs->expression_);
michael@0 473 }
michael@0 474 Rule *Copy() const { return new ValExpressionRule(*this); }
michael@0 475 private:
michael@0 476 string expression_;
michael@0 477 };
michael@0 478
michael@0 479 // A map from register numbers to rules.
michael@0 480 class CallFrameInfo::RuleMap {
michael@0 481 public:
michael@0 482 RuleMap() : cfa_rule_(NULL) { }
michael@0 483 RuleMap(const RuleMap &rhs) : cfa_rule_(NULL) { *this = rhs; }
michael@0 484 ~RuleMap() { Clear(); }
michael@0 485
michael@0 486 RuleMap &operator=(const RuleMap &rhs);
michael@0 487
michael@0 488 // Set the rule for computing the CFA to RULE. Take ownership of RULE.
michael@0 489 void SetCFARule(Rule *rule) { delete cfa_rule_; cfa_rule_ = rule; }
michael@0 490
michael@0 491 // Return the current CFA rule. Unlike RegisterRule, this RuleMap retains
michael@0 492 // ownership of the rule. We use this for DW_CFA_def_cfa_offset and
michael@0 493 // DW_CFA_def_cfa_register, and for detecting references to the CFA before
michael@0 494 // a rule for it has been established.
michael@0 495 Rule *CFARule() const { return cfa_rule_; }
michael@0 496
michael@0 497 // Return the rule for REG, or NULL if there is none. The caller takes
michael@0 498 // ownership of the result.
michael@0 499 Rule *RegisterRule(int reg) const;
michael@0 500
michael@0 501 // Set the rule for computing REG to RULE. Take ownership of RULE.
michael@0 502 void SetRegisterRule(int reg, Rule *rule);
michael@0 503
michael@0 504 // Make all the appropriate calls to HANDLER as if we were changing from
michael@0 505 // this RuleMap to NEW_RULES at ADDRESS. We use this to implement
michael@0 506 // DW_CFA_restore_state, where lots of rules can change simultaneously.
michael@0 507 // Return true if all handlers returned true; otherwise, return false.
michael@0 508 bool HandleTransitionTo(Handler *handler, uint64 address,
michael@0 509 const RuleMap &new_rules) const;
michael@0 510
michael@0 511 private:
michael@0 512 // A map from register numbers to Rules.
michael@0 513 typedef std::map<int, Rule *> RuleByNumber;
michael@0 514
michael@0 515 // Remove all register rules and clear cfa_rule_.
michael@0 516 void Clear();
michael@0 517
michael@0 518 // The rule for computing the canonical frame address. This RuleMap owns
michael@0 519 // this rule.
michael@0 520 Rule *cfa_rule_;
michael@0 521
michael@0 522 // A map from register numbers to postfix expressions to recover
michael@0 523 // their values. This RuleMap owns the Rules the map refers to.
michael@0 524 RuleByNumber registers_;
michael@0 525 };
michael@0 526
michael@0 527 CallFrameInfo::RuleMap &CallFrameInfo::RuleMap::operator=(const RuleMap &rhs) {
michael@0 528 Clear();
michael@0 529 // Since each map owns the rules it refers to, assignment must copy them.
michael@0 530 if (rhs.cfa_rule_) cfa_rule_ = rhs.cfa_rule_->Copy();
michael@0 531 for (RuleByNumber::const_iterator it = rhs.registers_.begin();
michael@0 532 it != rhs.registers_.end(); it++)
michael@0 533 registers_[it->first] = it->second->Copy();
michael@0 534 return *this;
michael@0 535 }
michael@0 536
michael@0 537 CallFrameInfo::Rule *CallFrameInfo::RuleMap::RegisterRule(int reg) const {
michael@0 538 MOZ_ASSERT(reg != Handler::kCFARegister);
michael@0 539 RuleByNumber::const_iterator it = registers_.find(reg);
michael@0 540 if (it != registers_.end())
michael@0 541 return it->second->Copy();
michael@0 542 else
michael@0 543 return NULL;
michael@0 544 }
michael@0 545
michael@0 546 void CallFrameInfo::RuleMap::SetRegisterRule(int reg, Rule *rule) {
michael@0 547 MOZ_ASSERT(reg != Handler::kCFARegister);
michael@0 548 MOZ_ASSERT(rule);
michael@0 549 Rule **slot = &registers_[reg];
michael@0 550 delete *slot;
michael@0 551 *slot = rule;
michael@0 552 }
michael@0 553
michael@0 554 bool CallFrameInfo::RuleMap::HandleTransitionTo(
michael@0 555 Handler *handler,
michael@0 556 uint64 address,
michael@0 557 const RuleMap &new_rules) const {
michael@0 558 // Transition from cfa_rule_ to new_rules.cfa_rule_.
michael@0 559 if (cfa_rule_ && new_rules.cfa_rule_) {
michael@0 560 if (*cfa_rule_ != *new_rules.cfa_rule_ &&
michael@0 561 !new_rules.cfa_rule_->Handle(handler, address, Handler::kCFARegister))
michael@0 562 return false;
michael@0 563 } else if (cfa_rule_) {
michael@0 564 // this RuleMap has a CFA rule but new_rules doesn't.
michael@0 565 // CallFrameInfo::Handler has no way to handle this --- and shouldn't;
michael@0 566 // it's garbage input. The instruction interpreter should have
michael@0 567 // detected this and warned, so take no action here.
michael@0 568 } else if (new_rules.cfa_rule_) {
michael@0 569 // This shouldn't be possible: NEW_RULES is some prior state, and
michael@0 570 // there's no way to remove entries.
michael@0 571 MOZ_ASSERT(0);
michael@0 572 } else {
michael@0 573 // Both CFA rules are empty. No action needed.
michael@0 574 }
michael@0 575
michael@0 576 // Traverse the two maps in order by register number, and report
michael@0 577 // whatever differences we find.
michael@0 578 RuleByNumber::const_iterator old_it = registers_.begin();
michael@0 579 RuleByNumber::const_iterator new_it = new_rules.registers_.begin();
michael@0 580 while (old_it != registers_.end() && new_it != new_rules.registers_.end()) {
michael@0 581 if (old_it->first < new_it->first) {
michael@0 582 // This RuleMap has an entry for old_it->first, but NEW_RULES
michael@0 583 // doesn't.
michael@0 584 //
michael@0 585 // This isn't really the right thing to do, but since CFI generally
michael@0 586 // only mentions callee-saves registers, and GCC's convention for
michael@0 587 // callee-saves registers is that they are unchanged, it's a good
michael@0 588 // approximation.
michael@0 589 if (!handler->SameValueRule(address, old_it->first))
michael@0 590 return false;
michael@0 591 old_it++;
michael@0 592 } else if (old_it->first > new_it->first) {
michael@0 593 // NEW_RULES has entry for new_it->first, but this RuleMap
michael@0 594 // doesn't. This shouldn't be possible: NEW_RULES is some prior
michael@0 595 // state, and there's no way to remove entries.
michael@0 596 MOZ_ASSERT(0);
michael@0 597 } else {
michael@0 598 // Both maps have an entry for this register. Report the new
michael@0 599 // rule if it is different.
michael@0 600 if (*old_it->second != *new_it->second &&
michael@0 601 !new_it->second->Handle(handler, address, new_it->first))
michael@0 602 return false;
michael@0 603 new_it++, old_it++;
michael@0 604 }
michael@0 605 }
michael@0 606 // Finish off entries from this RuleMap with no counterparts in new_rules.
michael@0 607 while (old_it != registers_.end()) {
michael@0 608 if (!handler->SameValueRule(address, old_it->first))
michael@0 609 return false;
michael@0 610 old_it++;
michael@0 611 }
michael@0 612 // Since we only make transitions from a rule set to some previously
michael@0 613 // saved rule set, and we can only add rules to the map, NEW_RULES
michael@0 614 // must have fewer rules than *this.
michael@0 615 MOZ_ASSERT(new_it == new_rules.registers_.end());
michael@0 616
michael@0 617 return true;
michael@0 618 }
michael@0 619
michael@0 620 // Remove all register rules and clear cfa_rule_.
michael@0 621 void CallFrameInfo::RuleMap::Clear() {
michael@0 622 delete cfa_rule_;
michael@0 623 cfa_rule_ = NULL;
michael@0 624 for (RuleByNumber::iterator it = registers_.begin();
michael@0 625 it != registers_.end(); it++)
michael@0 626 delete it->second;
michael@0 627 registers_.clear();
michael@0 628 }
michael@0 629
michael@0 630 // The state of the call frame information interpreter as it processes
michael@0 631 // instructions from a CIE and FDE.
michael@0 632 class CallFrameInfo::State {
michael@0 633 public:
michael@0 634 // Create a call frame information interpreter state with the given
michael@0 635 // reporter, reader, handler, and initial call frame info address.
michael@0 636 State(ByteReader *reader, Handler *handler, Reporter *reporter,
michael@0 637 uint64 address)
michael@0 638 : reader_(reader), handler_(handler), reporter_(reporter),
michael@0 639 address_(address), entry_(NULL), cursor_(NULL),
michael@0 640 saved_rules_(NULL) { }
michael@0 641
michael@0 642 ~State() {
michael@0 643 if (saved_rules_)
michael@0 644 delete saved_rules_;
michael@0 645 }
michael@0 646
michael@0 647 // Interpret instructions from CIE, save the resulting rule set for
michael@0 648 // DW_CFA_restore instructions, and return true. On error, report
michael@0 649 // the problem to reporter_ and return false.
michael@0 650 bool InterpretCIE(const CIE &cie);
michael@0 651
michael@0 652 // Interpret instructions from FDE, and return true. On error,
michael@0 653 // report the problem to reporter_ and return false.
michael@0 654 bool InterpretFDE(const FDE &fde);
michael@0 655
michael@0 656 private:
michael@0 657 // The operands of a CFI instruction, for ParseOperands.
michael@0 658 struct Operands {
michael@0 659 unsigned register_number; // A register number.
michael@0 660 uint64 offset; // An offset or address.
michael@0 661 long signed_offset; // A signed offset.
michael@0 662 string expression; // A DWARF expression.
michael@0 663 };
michael@0 664
michael@0 665 // Parse CFI instruction operands from STATE's instruction stream as
michael@0 666 // described by FORMAT. On success, populate OPERANDS with the
michael@0 667 // results, and return true. On failure, report the problem and
michael@0 668 // return false.
michael@0 669 //
michael@0 670 // Each character of FORMAT should be one of the following:
michael@0 671 //
michael@0 672 // 'r' unsigned LEB128 register number (OPERANDS->register_number)
michael@0 673 // 'o' unsigned LEB128 offset (OPERANDS->offset)
michael@0 674 // 's' signed LEB128 offset (OPERANDS->signed_offset)
michael@0 675 // 'a' machine-size address (OPERANDS->offset)
michael@0 676 // (If the CIE has a 'z' augmentation string, 'a' uses the
michael@0 677 // encoding specified by the 'R' argument.)
michael@0 678 // '1' a one-byte offset (OPERANDS->offset)
michael@0 679 // '2' a two-byte offset (OPERANDS->offset)
michael@0 680 // '4' a four-byte offset (OPERANDS->offset)
michael@0 681 // '8' an eight-byte offset (OPERANDS->offset)
michael@0 682 // 'e' a DW_FORM_block holding a (OPERANDS->expression)
michael@0 683 // DWARF expression
michael@0 684 bool ParseOperands(const char *format, Operands *operands);
michael@0 685
michael@0 686 // Interpret one CFI instruction from STATE's instruction stream, update
michael@0 687 // STATE, report any rule changes to handler_, and return true. On
michael@0 688 // failure, report the problem and return false.
michael@0 689 bool DoInstruction();
michael@0 690
michael@0 691 // The following Do* member functions are subroutines of DoInstruction,
michael@0 692 // factoring out the actual work of operations that have several
michael@0 693 // different encodings.
michael@0 694
michael@0 695 // Set the CFA rule to be the value of BASE_REGISTER plus OFFSET, and
michael@0 696 // return true. On failure, report and return false. (Used for
michael@0 697 // DW_CFA_def_cfa and DW_CFA_def_cfa_sf.)
michael@0 698 bool DoDefCFA(unsigned base_register, long offset);
michael@0 699
michael@0 700 // Change the offset of the CFA rule to OFFSET, and return true. On
michael@0 701 // failure, report and return false. (Subroutine for
michael@0 702 // DW_CFA_def_cfa_offset and DW_CFA_def_cfa_offset_sf.)
michael@0 703 bool DoDefCFAOffset(long offset);
michael@0 704
michael@0 705 // Specify that REG can be recovered using RULE, and return true. On
michael@0 706 // failure, report and return false.
michael@0 707 bool DoRule(unsigned reg, Rule *rule);
michael@0 708
michael@0 709 // Specify that REG can be found at OFFSET from the CFA, and return true.
michael@0 710 // On failure, report and return false. (Subroutine for DW_CFA_offset,
michael@0 711 // DW_CFA_offset_extended, and DW_CFA_offset_extended_sf.)
michael@0 712 bool DoOffset(unsigned reg, long offset);
michael@0 713
michael@0 714 // Specify that the caller's value for REG is the CFA plus OFFSET,
michael@0 715 // and return true. On failure, report and return false. (Subroutine
michael@0 716 // for DW_CFA_val_offset and DW_CFA_val_offset_sf.)
michael@0 717 bool DoValOffset(unsigned reg, long offset);
michael@0 718
michael@0 719 // Restore REG to the rule established in the CIE, and return true. On
michael@0 720 // failure, report and return false. (Subroutine for DW_CFA_restore and
michael@0 721 // DW_CFA_restore_extended.)
michael@0 722 bool DoRestore(unsigned reg);
michael@0 723
michael@0 724 // Return the section offset of the instruction at cursor. For use
michael@0 725 // in error messages.
michael@0 726 uint64 CursorOffset() { return entry_->offset + (cursor_ - entry_->start); }
michael@0 727
michael@0 728 // Report that entry_ is incomplete, and return false. For brevity.
michael@0 729 bool ReportIncomplete() {
michael@0 730 reporter_->Incomplete(entry_->offset, entry_->kind);
michael@0 731 return false;
michael@0 732 }
michael@0 733
michael@0 734 // For reading multi-byte values with the appropriate endianness.
michael@0 735 ByteReader *reader_;
michael@0 736
michael@0 737 // The handler to which we should report the data we find.
michael@0 738 Handler *handler_;
michael@0 739
michael@0 740 // For reporting problems in the info we're parsing.
michael@0 741 Reporter *reporter_;
michael@0 742
michael@0 743 // The code address to which the next instruction in the stream applies.
michael@0 744 uint64 address_;
michael@0 745
michael@0 746 // The entry whose instructions we are currently processing. This is
michael@0 747 // first a CIE, and then an FDE.
michael@0 748 const Entry *entry_;
michael@0 749
michael@0 750 // The next instruction to process.
michael@0 751 const char *cursor_;
michael@0 752
michael@0 753 // The current set of rules.
michael@0 754 RuleMap rules_;
michael@0 755
michael@0 756 // The set of rules established by the CIE, used by DW_CFA_restore
michael@0 757 // and DW_CFA_restore_extended. We set this after interpreting the
michael@0 758 // CIE's instructions.
michael@0 759 RuleMap cie_rules_;
michael@0 760
michael@0 761 // A stack of saved states, for DW_CFA_remember_state and
michael@0 762 // DW_CFA_restore_state.
michael@0 763 std::stack<RuleMap>* saved_rules_;
michael@0 764 };
michael@0 765
michael@0 766 bool CallFrameInfo::State::InterpretCIE(const CIE &cie) {
michael@0 767 entry_ = &cie;
michael@0 768 cursor_ = entry_->instructions;
michael@0 769 while (cursor_ < entry_->end)
michael@0 770 if (!DoInstruction())
michael@0 771 return false;
michael@0 772 // Note the rules established by the CIE, for use by DW_CFA_restore
michael@0 773 // and DW_CFA_restore_extended.
michael@0 774 cie_rules_ = rules_;
michael@0 775 return true;
michael@0 776 }
michael@0 777
michael@0 778 bool CallFrameInfo::State::InterpretFDE(const FDE &fde) {
michael@0 779 entry_ = &fde;
michael@0 780 cursor_ = entry_->instructions;
michael@0 781 while (cursor_ < entry_->end)
michael@0 782 if (!DoInstruction())
michael@0 783 return false;
michael@0 784 return true;
michael@0 785 }
michael@0 786
michael@0 787 bool CallFrameInfo::State::ParseOperands(const char *format,
michael@0 788 Operands *operands) {
michael@0 789 size_t len;
michael@0 790 const char *operand;
michael@0 791
michael@0 792 for (operand = format; *operand; operand++) {
michael@0 793 size_t bytes_left = entry_->end - cursor_;
michael@0 794 switch (*operand) {
michael@0 795 case 'r':
michael@0 796 operands->register_number = reader_->ReadUnsignedLEB128(cursor_, &len);
michael@0 797 if (len > bytes_left) return ReportIncomplete();
michael@0 798 cursor_ += len;
michael@0 799 break;
michael@0 800
michael@0 801 case 'o':
michael@0 802 operands->offset = reader_->ReadUnsignedLEB128(cursor_, &len);
michael@0 803 if (len > bytes_left) return ReportIncomplete();
michael@0 804 cursor_ += len;
michael@0 805 break;
michael@0 806
michael@0 807 case 's':
michael@0 808 operands->signed_offset = reader_->ReadSignedLEB128(cursor_, &len);
michael@0 809 if (len > bytes_left) return ReportIncomplete();
michael@0 810 cursor_ += len;
michael@0 811 break;
michael@0 812
michael@0 813 case 'a':
michael@0 814 operands->offset =
michael@0 815 reader_->ReadEncodedPointer(cursor_, entry_->cie->pointer_encoding,
michael@0 816 &len);
michael@0 817 if (len > bytes_left) return ReportIncomplete();
michael@0 818 cursor_ += len;
michael@0 819 break;
michael@0 820
michael@0 821 case '1':
michael@0 822 if (1 > bytes_left) return ReportIncomplete();
michael@0 823 operands->offset = static_cast<unsigned char>(*cursor_++);
michael@0 824 break;
michael@0 825
michael@0 826 case '2':
michael@0 827 if (2 > bytes_left) return ReportIncomplete();
michael@0 828 operands->offset = reader_->ReadTwoBytes(cursor_);
michael@0 829 cursor_ += 2;
michael@0 830 break;
michael@0 831
michael@0 832 case '4':
michael@0 833 if (4 > bytes_left) return ReportIncomplete();
michael@0 834 operands->offset = reader_->ReadFourBytes(cursor_);
michael@0 835 cursor_ += 4;
michael@0 836 break;
michael@0 837
michael@0 838 case '8':
michael@0 839 if (8 > bytes_left) return ReportIncomplete();
michael@0 840 operands->offset = reader_->ReadEightBytes(cursor_);
michael@0 841 cursor_ += 8;
michael@0 842 break;
michael@0 843
michael@0 844 case 'e': {
michael@0 845 size_t expression_length = reader_->ReadUnsignedLEB128(cursor_, &len);
michael@0 846 if (len > bytes_left || expression_length > bytes_left - len)
michael@0 847 return ReportIncomplete();
michael@0 848 cursor_ += len;
michael@0 849 operands->expression = string(cursor_, expression_length);
michael@0 850 cursor_ += expression_length;
michael@0 851 break;
michael@0 852 }
michael@0 853
michael@0 854 default:
michael@0 855 MOZ_ASSERT(0);
michael@0 856 }
michael@0 857 }
michael@0 858
michael@0 859 return true;
michael@0 860 }
michael@0 861
michael@0 862 bool CallFrameInfo::State::DoInstruction() {
michael@0 863 CIE *cie = entry_->cie;
michael@0 864 Operands ops;
michael@0 865
michael@0 866 // Our entry's kind should have been set by now.
michael@0 867 MOZ_ASSERT(entry_->kind != kUnknown);
michael@0 868
michael@0 869 // We shouldn't have been invoked unless there were more
michael@0 870 // instructions to parse.
michael@0 871 MOZ_ASSERT(cursor_ < entry_->end);
michael@0 872
michael@0 873 unsigned opcode = *cursor_++;
michael@0 874 if ((opcode & 0xc0) != 0) {
michael@0 875 switch (opcode & 0xc0) {
michael@0 876 // Advance the address.
michael@0 877 case DW_CFA_advance_loc: {
michael@0 878 size_t code_offset = opcode & 0x3f;
michael@0 879 address_ += code_offset * cie->code_alignment_factor;
michael@0 880 break;
michael@0 881 }
michael@0 882
michael@0 883 // Find a register at an offset from the CFA.
michael@0 884 case DW_CFA_offset:
michael@0 885 if (!ParseOperands("o", &ops) ||
michael@0 886 !DoOffset(opcode & 0x3f, ops.offset * cie->data_alignment_factor))
michael@0 887 return false;
michael@0 888 break;
michael@0 889
michael@0 890 // Restore the rule established for a register by the CIE.
michael@0 891 case DW_CFA_restore:
michael@0 892 if (!DoRestore(opcode & 0x3f)) return false;
michael@0 893 break;
michael@0 894
michael@0 895 // The 'if' above should have excluded this possibility.
michael@0 896 default:
michael@0 897 MOZ_ASSERT(0);
michael@0 898 }
michael@0 899
michael@0 900 // Return here, so the big switch below won't be indented.
michael@0 901 return true;
michael@0 902 }
michael@0 903
michael@0 904 switch (opcode) {
michael@0 905 // Set the address.
michael@0 906 case DW_CFA_set_loc:
michael@0 907 if (!ParseOperands("a", &ops)) return false;
michael@0 908 address_ = ops.offset;
michael@0 909 break;
michael@0 910
michael@0 911 // Advance the address.
michael@0 912 case DW_CFA_advance_loc1:
michael@0 913 if (!ParseOperands("1", &ops)) return false;
michael@0 914 address_ += ops.offset * cie->code_alignment_factor;
michael@0 915 break;
michael@0 916
michael@0 917 // Advance the address.
michael@0 918 case DW_CFA_advance_loc2:
michael@0 919 if (!ParseOperands("2", &ops)) return false;
michael@0 920 address_ += ops.offset * cie->code_alignment_factor;
michael@0 921 break;
michael@0 922
michael@0 923 // Advance the address.
michael@0 924 case DW_CFA_advance_loc4:
michael@0 925 if (!ParseOperands("4", &ops)) return false;
michael@0 926 address_ += ops.offset * cie->code_alignment_factor;
michael@0 927 break;
michael@0 928
michael@0 929 // Advance the address.
michael@0 930 case DW_CFA_MIPS_advance_loc8:
michael@0 931 if (!ParseOperands("8", &ops)) return false;
michael@0 932 address_ += ops.offset * cie->code_alignment_factor;
michael@0 933 break;
michael@0 934
michael@0 935 // Compute the CFA by adding an offset to a register.
michael@0 936 case DW_CFA_def_cfa:
michael@0 937 if (!ParseOperands("ro", &ops) ||
michael@0 938 !DoDefCFA(ops.register_number, ops.offset))
michael@0 939 return false;
michael@0 940 break;
michael@0 941
michael@0 942 // Compute the CFA by adding an offset to a register.
michael@0 943 case DW_CFA_def_cfa_sf:
michael@0 944 if (!ParseOperands("rs", &ops) ||
michael@0 945 !DoDefCFA(ops.register_number,
michael@0 946 ops.signed_offset * cie->data_alignment_factor))
michael@0 947 return false;
michael@0 948 break;
michael@0 949
michael@0 950 // Change the base register used to compute the CFA.
michael@0 951 case DW_CFA_def_cfa_register: {
michael@0 952 Rule *cfa_rule = rules_.CFARule();
michael@0 953 if (!cfa_rule) {
michael@0 954 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
michael@0 955 return false;
michael@0 956 }
michael@0 957 if (!ParseOperands("r", &ops)) return false;
michael@0 958 cfa_rule->SetBaseRegister(ops.register_number);
michael@0 959 if (!cfa_rule->Handle(handler_, address_, Handler::kCFARegister))
michael@0 960 return false;
michael@0 961 break;
michael@0 962 }
michael@0 963
michael@0 964 // Change the offset used to compute the CFA.
michael@0 965 case DW_CFA_def_cfa_offset:
michael@0 966 if (!ParseOperands("o", &ops) ||
michael@0 967 !DoDefCFAOffset(ops.offset))
michael@0 968 return false;
michael@0 969 break;
michael@0 970
michael@0 971 // Change the offset used to compute the CFA.
michael@0 972 case DW_CFA_def_cfa_offset_sf:
michael@0 973 if (!ParseOperands("s", &ops) ||
michael@0 974 !DoDefCFAOffset(ops.signed_offset * cie->data_alignment_factor))
michael@0 975 return false;
michael@0 976 break;
michael@0 977
michael@0 978 // Specify an expression whose value is the CFA.
michael@0 979 case DW_CFA_def_cfa_expression: {
michael@0 980 if (!ParseOperands("e", &ops))
michael@0 981 return false;
michael@0 982 Rule *rule = new ValExpressionRule(ops.expression);
michael@0 983 rules_.SetCFARule(rule);
michael@0 984 if (!rule->Handle(handler_, address_, Handler::kCFARegister))
michael@0 985 return false;
michael@0 986 break;
michael@0 987 }
michael@0 988
michael@0 989 // The register's value cannot be recovered.
michael@0 990 case DW_CFA_undefined: {
michael@0 991 if (!ParseOperands("r", &ops) ||
michael@0 992 !DoRule(ops.register_number, new UndefinedRule()))
michael@0 993 return false;
michael@0 994 break;
michael@0 995 }
michael@0 996
michael@0 997 // The register's value is unchanged from its value in the caller.
michael@0 998 case DW_CFA_same_value: {
michael@0 999 if (!ParseOperands("r", &ops) ||
michael@0 1000 !DoRule(ops.register_number, new SameValueRule()))
michael@0 1001 return false;
michael@0 1002 break;
michael@0 1003 }
michael@0 1004
michael@0 1005 // Find a register at an offset from the CFA.
michael@0 1006 case DW_CFA_offset_extended:
michael@0 1007 if (!ParseOperands("ro", &ops) ||
michael@0 1008 !DoOffset(ops.register_number,
michael@0 1009 ops.offset * cie->data_alignment_factor))
michael@0 1010 return false;
michael@0 1011 break;
michael@0 1012
michael@0 1013 // The register is saved at an offset from the CFA.
michael@0 1014 case DW_CFA_offset_extended_sf:
michael@0 1015 if (!ParseOperands("rs", &ops) ||
michael@0 1016 !DoOffset(ops.register_number,
michael@0 1017 ops.signed_offset * cie->data_alignment_factor))
michael@0 1018 return false;
michael@0 1019 break;
michael@0 1020
michael@0 1021 // The register is saved at an offset from the CFA.
michael@0 1022 case DW_CFA_GNU_negative_offset_extended:
michael@0 1023 if (!ParseOperands("ro", &ops) ||
michael@0 1024 !DoOffset(ops.register_number,
michael@0 1025 -ops.offset * cie->data_alignment_factor))
michael@0 1026 return false;
michael@0 1027 break;
michael@0 1028
michael@0 1029 // The register's value is the sum of the CFA plus an offset.
michael@0 1030 case DW_CFA_val_offset:
michael@0 1031 if (!ParseOperands("ro", &ops) ||
michael@0 1032 !DoValOffset(ops.register_number,
michael@0 1033 ops.offset * cie->data_alignment_factor))
michael@0 1034 return false;
michael@0 1035 break;
michael@0 1036
michael@0 1037 // The register's value is the sum of the CFA plus an offset.
michael@0 1038 case DW_CFA_val_offset_sf:
michael@0 1039 if (!ParseOperands("rs", &ops) ||
michael@0 1040 !DoValOffset(ops.register_number,
michael@0 1041 ops.signed_offset * cie->data_alignment_factor))
michael@0 1042 return false;
michael@0 1043 break;
michael@0 1044
michael@0 1045 // The register has been saved in another register.
michael@0 1046 case DW_CFA_register: {
michael@0 1047 if (!ParseOperands("ro", &ops) ||
michael@0 1048 !DoRule(ops.register_number, new RegisterRule(ops.offset)))
michael@0 1049 return false;
michael@0 1050 break;
michael@0 1051 }
michael@0 1052
michael@0 1053 // An expression yields the address at which the register is saved.
michael@0 1054 case DW_CFA_expression: {
michael@0 1055 if (!ParseOperands("re", &ops) ||
michael@0 1056 !DoRule(ops.register_number, new ExpressionRule(ops.expression)))
michael@0 1057 return false;
michael@0 1058 break;
michael@0 1059 }
michael@0 1060
michael@0 1061 // An expression yields the caller's value for the register.
michael@0 1062 case DW_CFA_val_expression: {
michael@0 1063 if (!ParseOperands("re", &ops) ||
michael@0 1064 !DoRule(ops.register_number, new ValExpressionRule(ops.expression)))
michael@0 1065 return false;
michael@0 1066 break;
michael@0 1067 }
michael@0 1068
michael@0 1069 // Restore the rule established for a register by the CIE.
michael@0 1070 case DW_CFA_restore_extended:
michael@0 1071 if (!ParseOperands("r", &ops) ||
michael@0 1072 !DoRestore( ops.register_number))
michael@0 1073 return false;
michael@0 1074 break;
michael@0 1075
michael@0 1076 // Save the current set of rules on a stack.
michael@0 1077 case DW_CFA_remember_state:
michael@0 1078 if (!saved_rules_) {
michael@0 1079 saved_rules_ = new std::stack<RuleMap>();
michael@0 1080 }
michael@0 1081 saved_rules_->push(rules_);
michael@0 1082 break;
michael@0 1083
michael@0 1084 // Pop the current set of rules off the stack.
michael@0 1085 case DW_CFA_restore_state: {
michael@0 1086 if (!saved_rules_ || saved_rules_->empty()) {
michael@0 1087 reporter_->EmptyStateStack(entry_->offset, entry_->kind,
michael@0 1088 CursorOffset());
michael@0 1089 return false;
michael@0 1090 }
michael@0 1091 const RuleMap &new_rules = saved_rules_->top();
michael@0 1092 if (rules_.CFARule() && !new_rules.CFARule()) {
michael@0 1093 reporter_->ClearingCFARule(entry_->offset, entry_->kind,
michael@0 1094 CursorOffset());
michael@0 1095 return false;
michael@0 1096 }
michael@0 1097 rules_.HandleTransitionTo(handler_, address_, new_rules);
michael@0 1098 rules_ = new_rules;
michael@0 1099 saved_rules_->pop();
michael@0 1100 break;
michael@0 1101 }
michael@0 1102
michael@0 1103 // No operation. (Padding instruction.)
michael@0 1104 case DW_CFA_nop:
michael@0 1105 break;
michael@0 1106
michael@0 1107 // A SPARC register window save: Registers 8 through 15 (%o0-%o7)
michael@0 1108 // are saved in registers 24 through 31 (%i0-%i7), and registers
michael@0 1109 // 16 through 31 (%l0-%l7 and %i0-%i7) are saved at CFA offsets
michael@0 1110 // (0-15 * the register size). The register numbers must be
michael@0 1111 // hard-coded. A GNU extension, and not a pretty one.
michael@0 1112 case DW_CFA_GNU_window_save: {
michael@0 1113 // Save %o0-%o7 in %i0-%i7.
michael@0 1114 for (int i = 8; i < 16; i++)
michael@0 1115 if (!DoRule(i, new RegisterRule(i + 16)))
michael@0 1116 return false;
michael@0 1117 // Save %l0-%l7 and %i0-%i7 at the CFA.
michael@0 1118 for (int i = 16; i < 32; i++)
michael@0 1119 // Assume that the byte reader's address size is the same as
michael@0 1120 // the architecture's register size. !@#%*^ hilarious.
michael@0 1121 if (!DoRule(i, new OffsetRule(Handler::kCFARegister,
michael@0 1122 (i - 16) * reader_->AddressSize())))
michael@0 1123 return false;
michael@0 1124 break;
michael@0 1125 }
michael@0 1126
michael@0 1127 // I'm not sure what this is. GDB doesn't use it for unwinding.
michael@0 1128 case DW_CFA_GNU_args_size:
michael@0 1129 if (!ParseOperands("o", &ops)) return false;
michael@0 1130 break;
michael@0 1131
michael@0 1132 // An opcode we don't recognize.
michael@0 1133 default: {
michael@0 1134 reporter_->BadInstruction(entry_->offset, entry_->kind, CursorOffset());
michael@0 1135 return false;
michael@0 1136 }
michael@0 1137 }
michael@0 1138
michael@0 1139 return true;
michael@0 1140 }
michael@0 1141
michael@0 1142 bool CallFrameInfo::State::DoDefCFA(unsigned base_register, long offset) {
michael@0 1143 Rule *rule = new ValOffsetRule(base_register, offset);
michael@0 1144 rules_.SetCFARule(rule);
michael@0 1145 return rule->Handle(handler_, address_, Handler::kCFARegister);
michael@0 1146 }
michael@0 1147
michael@0 1148 bool CallFrameInfo::State::DoDefCFAOffset(long offset) {
michael@0 1149 Rule *cfa_rule = rules_.CFARule();
michael@0 1150 if (!cfa_rule) {
michael@0 1151 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
michael@0 1152 return false;
michael@0 1153 }
michael@0 1154 cfa_rule->SetOffset(offset);
michael@0 1155 return cfa_rule->Handle(handler_, address_, Handler::kCFARegister);
michael@0 1156 }
michael@0 1157
michael@0 1158 bool CallFrameInfo::State::DoRule(unsigned reg, Rule *rule) {
michael@0 1159 rules_.SetRegisterRule(reg, rule);
michael@0 1160 return rule->Handle(handler_, address_, reg);
michael@0 1161 }
michael@0 1162
michael@0 1163 bool CallFrameInfo::State::DoOffset(unsigned reg, long offset) {
michael@0 1164 if (!rules_.CFARule()) {
michael@0 1165 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
michael@0 1166 return false;
michael@0 1167 }
michael@0 1168 return DoRule(reg,
michael@0 1169 new OffsetRule(Handler::kCFARegister, offset));
michael@0 1170 }
michael@0 1171
michael@0 1172 bool CallFrameInfo::State::DoValOffset(unsigned reg, long offset) {
michael@0 1173 if (!rules_.CFARule()) {
michael@0 1174 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
michael@0 1175 return false;
michael@0 1176 }
michael@0 1177 return DoRule(reg,
michael@0 1178 new ValOffsetRule(Handler::kCFARegister, offset));
michael@0 1179 }
michael@0 1180
michael@0 1181 bool CallFrameInfo::State::DoRestore(unsigned reg) {
michael@0 1182 // DW_CFA_restore and DW_CFA_restore_extended don't make sense in a CIE.
michael@0 1183 if (entry_->kind == kCIE) {
michael@0 1184 reporter_->RestoreInCIE(entry_->offset, CursorOffset());
michael@0 1185 return false;
michael@0 1186 }
michael@0 1187 Rule *rule = cie_rules_.RegisterRule(reg);
michael@0 1188 if (!rule) {
michael@0 1189 // This isn't really the right thing to do, but since CFI generally
michael@0 1190 // only mentions callee-saves registers, and GCC's convention for
michael@0 1191 // callee-saves registers is that they are unchanged, it's a good
michael@0 1192 // approximation.
michael@0 1193 rule = new SameValueRule();
michael@0 1194 }
michael@0 1195 return DoRule(reg, rule);
michael@0 1196 }
michael@0 1197
michael@0 1198 bool CallFrameInfo::ReadEntryPrologue(const char *cursor, Entry *entry) {
michael@0 1199 const char *buffer_end = buffer_ + buffer_length_;
michael@0 1200
michael@0 1201 // Initialize enough of ENTRY for use in error reporting.
michael@0 1202 entry->offset = cursor - buffer_;
michael@0 1203 entry->start = cursor;
michael@0 1204 entry->kind = kUnknown;
michael@0 1205 entry->end = NULL;
michael@0 1206
michael@0 1207 // Read the initial length. This sets reader_'s offset size.
michael@0 1208 size_t length_size;
michael@0 1209 uint64 length = reader_->ReadInitialLength(cursor, &length_size);
michael@0 1210 if (length_size > size_t(buffer_end - cursor))
michael@0 1211 return ReportIncomplete(entry);
michael@0 1212 cursor += length_size;
michael@0 1213
michael@0 1214 // In a .eh_frame section, a length of zero marks the end of the series
michael@0 1215 // of entries.
michael@0 1216 if (length == 0 && eh_frame_) {
michael@0 1217 entry->kind = kTerminator;
michael@0 1218 entry->end = cursor;
michael@0 1219 return true;
michael@0 1220 }
michael@0 1221
michael@0 1222 // Validate the length.
michael@0 1223 if (length > size_t(buffer_end - cursor))
michael@0 1224 return ReportIncomplete(entry);
michael@0 1225
michael@0 1226 // The length is the number of bytes after the initial length field;
michael@0 1227 // we have that position handy at this point, so compute the end
michael@0 1228 // now. (If we're parsing 64-bit-offset DWARF on a 32-bit machine,
michael@0 1229 // and the length didn't fit in a size_t, we would have rejected it
michael@0 1230 // above.)
michael@0 1231 entry->end = cursor + length;
michael@0 1232
michael@0 1233 // Parse the next field: either the offset of a CIE or a CIE id.
michael@0 1234 size_t offset_size = reader_->OffsetSize();
michael@0 1235 if (offset_size > size_t(entry->end - cursor)) return ReportIncomplete(entry);
michael@0 1236 entry->id = reader_->ReadOffset(cursor);
michael@0 1237
michael@0 1238 // Don't advance cursor past id field yet; in .eh_frame data we need
michael@0 1239 // the id's position to compute the section offset of an FDE's CIE.
michael@0 1240
michael@0 1241 // Now we can decide what kind of entry this is.
michael@0 1242 if (eh_frame_) {
michael@0 1243 // In .eh_frame data, an ID of zero marks the entry as a CIE, and
michael@0 1244 // anything else is an offset from the id field of the FDE to the start
michael@0 1245 // of the CIE.
michael@0 1246 if (entry->id == 0) {
michael@0 1247 entry->kind = kCIE;
michael@0 1248 } else {
michael@0 1249 entry->kind = kFDE;
michael@0 1250 // Turn the offset from the id into an offset from the buffer's start.
michael@0 1251 entry->id = (cursor - buffer_) - entry->id;
michael@0 1252 }
michael@0 1253 } else {
michael@0 1254 // In DWARF CFI data, an ID of ~0 (of the appropriate width, given the
michael@0 1255 // offset size for the entry) marks the entry as a CIE, and anything
michael@0 1256 // else is the offset of the CIE from the beginning of the section.
michael@0 1257 if (offset_size == 4)
michael@0 1258 entry->kind = (entry->id == 0xffffffff) ? kCIE : kFDE;
michael@0 1259 else {
michael@0 1260 MOZ_ASSERT(offset_size == 8);
michael@0 1261 entry->kind = (entry->id == 0xffffffffffffffffULL) ? kCIE : kFDE;
michael@0 1262 }
michael@0 1263 }
michael@0 1264
michael@0 1265 // Now advance cursor past the id.
michael@0 1266 cursor += offset_size;
michael@0 1267
michael@0 1268 // The fields specific to this kind of entry start here.
michael@0 1269 entry->fields = cursor;
michael@0 1270
michael@0 1271 entry->cie = NULL;
michael@0 1272
michael@0 1273 return true;
michael@0 1274 }
michael@0 1275
michael@0 1276 bool CallFrameInfo::ReadCIEFields(CIE *cie) {
michael@0 1277 const char *cursor = cie->fields;
michael@0 1278 size_t len;
michael@0 1279
michael@0 1280 MOZ_ASSERT(cie->kind == kCIE);
michael@0 1281
michael@0 1282 // Prepare for early exit.
michael@0 1283 cie->version = 0;
michael@0 1284 cie->augmentation.clear();
michael@0 1285 cie->code_alignment_factor = 0;
michael@0 1286 cie->data_alignment_factor = 0;
michael@0 1287 cie->return_address_register = 0;
michael@0 1288 cie->has_z_augmentation = false;
michael@0 1289 cie->pointer_encoding = DW_EH_PE_absptr;
michael@0 1290 cie->instructions = 0;
michael@0 1291
michael@0 1292 // Parse the version number.
michael@0 1293 if (cie->end - cursor < 1)
michael@0 1294 return ReportIncomplete(cie);
michael@0 1295 cie->version = reader_->ReadOneByte(cursor);
michael@0 1296 cursor++;
michael@0 1297
michael@0 1298 // If we don't recognize the version, we can't parse any more fields of the
michael@0 1299 // CIE. For DWARF CFI, we handle versions 1 through 3 (there was never a
michael@0 1300 // version 2 of CFI data). For .eh_frame, we handle versions 1 and 3 as well;
michael@0 1301 // the difference between those versions seems to be the same as for
michael@0 1302 // .debug_frame.
michael@0 1303 if (cie->version < 1 || cie->version > 3) {
michael@0 1304 reporter_->UnrecognizedVersion(cie->offset, cie->version);
michael@0 1305 return false;
michael@0 1306 }
michael@0 1307
michael@0 1308 const char *augmentation_start = cursor;
michael@0 1309 const void *augmentation_end =
michael@0 1310 memchr(augmentation_start, '\0', cie->end - augmentation_start);
michael@0 1311 if (! augmentation_end) return ReportIncomplete(cie);
michael@0 1312 cursor = static_cast<const char *>(augmentation_end);
michael@0 1313 cie->augmentation = string(augmentation_start,
michael@0 1314 cursor - augmentation_start);
michael@0 1315 // Skip the terminating '\0'.
michael@0 1316 cursor++;
michael@0 1317
michael@0 1318 // Is this CFI augmented?
michael@0 1319 if (!cie->augmentation.empty()) {
michael@0 1320 // Is it an augmentation we recognize?
michael@0 1321 if (cie->augmentation[0] == DW_Z_augmentation_start) {
michael@0 1322 // Linux C++ ABI 'z' augmentation, used for exception handling data.
michael@0 1323 cie->has_z_augmentation = true;
michael@0 1324 } else {
michael@0 1325 // Not an augmentation we recognize. Augmentations can have arbitrary
michael@0 1326 // effects on the form of rest of the content, so we have to give up.
michael@0 1327 reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation);
michael@0 1328 return false;
michael@0 1329 }
michael@0 1330 }
michael@0 1331
michael@0 1332 // Parse the code alignment factor.
michael@0 1333 cie->code_alignment_factor = reader_->ReadUnsignedLEB128(cursor, &len);
michael@0 1334 if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie);
michael@0 1335 cursor += len;
michael@0 1336
michael@0 1337 // Parse the data alignment factor.
michael@0 1338 cie->data_alignment_factor = reader_->ReadSignedLEB128(cursor, &len);
michael@0 1339 if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie);
michael@0 1340 cursor += len;
michael@0 1341
michael@0 1342 // Parse the return address register. This is a ubyte in version 1, and
michael@0 1343 // a ULEB128 in version 3.
michael@0 1344 if (cie->version == 1) {
michael@0 1345 if (cursor >= cie->end) return ReportIncomplete(cie);
michael@0 1346 cie->return_address_register = uint8(*cursor++);
michael@0 1347 } else {
michael@0 1348 cie->return_address_register = reader_->ReadUnsignedLEB128(cursor, &len);
michael@0 1349 if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie);
michael@0 1350 cursor += len;
michael@0 1351 }
michael@0 1352
michael@0 1353 // If we have a 'z' augmentation string, find the augmentation data and
michael@0 1354 // use the augmentation string to parse it.
michael@0 1355 if (cie->has_z_augmentation) {
michael@0 1356 uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &len);
michael@0 1357 if (size_t(cie->end - cursor) < len + data_size)
michael@0 1358 return ReportIncomplete(cie);
michael@0 1359 cursor += len;
michael@0 1360 const char *data = cursor;
michael@0 1361 cursor += data_size;
michael@0 1362 const char *data_end = cursor;
michael@0 1363
michael@0 1364 cie->has_z_lsda = false;
michael@0 1365 cie->has_z_personality = false;
michael@0 1366 cie->has_z_signal_frame = false;
michael@0 1367
michael@0 1368 // Walk the augmentation string, and extract values from the
michael@0 1369 // augmentation data as the string directs.
michael@0 1370 for (size_t i = 1; i < cie->augmentation.size(); i++) {
michael@0 1371 switch (cie->augmentation[i]) {
michael@0 1372 case DW_Z_has_LSDA:
michael@0 1373 // The CIE's augmentation data holds the language-specific data
michael@0 1374 // area pointer's encoding, and the FDE's augmentation data holds
michael@0 1375 // the pointer itself.
michael@0 1376 cie->has_z_lsda = true;
michael@0 1377 // Fetch the LSDA encoding from the augmentation data.
michael@0 1378 if (data >= data_end) return ReportIncomplete(cie);
michael@0 1379 cie->lsda_encoding = DwarfPointerEncoding(*data++);
michael@0 1380 if (!reader_->ValidEncoding(cie->lsda_encoding)) {
michael@0 1381 reporter_->InvalidPointerEncoding(cie->offset, cie->lsda_encoding);
michael@0 1382 return false;
michael@0 1383 }
michael@0 1384 // Don't check if the encoding is usable here --- we haven't
michael@0 1385 // read the FDE's fields yet, so we're not prepared for
michael@0 1386 // DW_EH_PE_funcrel, although that's a fine encoding for the
michael@0 1387 // LSDA to use, since it appears in the FDE.
michael@0 1388 break;
michael@0 1389
michael@0 1390 case DW_Z_has_personality_routine:
michael@0 1391 // The CIE's augmentation data holds the personality routine
michael@0 1392 // pointer's encoding, followed by the pointer itself.
michael@0 1393 cie->has_z_personality = true;
michael@0 1394 // Fetch the personality routine pointer's encoding from the
michael@0 1395 // augmentation data.
michael@0 1396 if (data >= data_end) return ReportIncomplete(cie);
michael@0 1397 cie->personality_encoding = DwarfPointerEncoding(*data++);
michael@0 1398 if (!reader_->ValidEncoding(cie->personality_encoding)) {
michael@0 1399 reporter_->InvalidPointerEncoding(cie->offset,
michael@0 1400 cie->personality_encoding);
michael@0 1401 return false;
michael@0 1402 }
michael@0 1403 if (!reader_->UsableEncoding(cie->personality_encoding)) {
michael@0 1404 reporter_->UnusablePointerEncoding(cie->offset,
michael@0 1405 cie->personality_encoding);
michael@0 1406 return false;
michael@0 1407 }
michael@0 1408 // Fetch the personality routine's pointer itself from the data.
michael@0 1409 cie->personality_address =
michael@0 1410 reader_->ReadEncodedPointer(data, cie->personality_encoding,
michael@0 1411 &len);
michael@0 1412 if (len > size_t(data_end - data))
michael@0 1413 return ReportIncomplete(cie);
michael@0 1414 data += len;
michael@0 1415 break;
michael@0 1416
michael@0 1417 case DW_Z_has_FDE_address_encoding:
michael@0 1418 // The CIE's augmentation data holds the pointer encoding to use
michael@0 1419 // for addresses in the FDE.
michael@0 1420 if (data >= data_end) return ReportIncomplete(cie);
michael@0 1421 cie->pointer_encoding = DwarfPointerEncoding(*data++);
michael@0 1422 if (!reader_->ValidEncoding(cie->pointer_encoding)) {
michael@0 1423 reporter_->InvalidPointerEncoding(cie->offset,
michael@0 1424 cie->pointer_encoding);
michael@0 1425 return false;
michael@0 1426 }
michael@0 1427 if (!reader_->UsableEncoding(cie->pointer_encoding)) {
michael@0 1428 reporter_->UnusablePointerEncoding(cie->offset,
michael@0 1429 cie->pointer_encoding);
michael@0 1430 return false;
michael@0 1431 }
michael@0 1432 break;
michael@0 1433
michael@0 1434 case DW_Z_is_signal_trampoline:
michael@0 1435 // Frames using this CIE are signal delivery frames.
michael@0 1436 cie->has_z_signal_frame = true;
michael@0 1437 break;
michael@0 1438
michael@0 1439 default:
michael@0 1440 // An augmentation we don't recognize.
michael@0 1441 reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation);
michael@0 1442 return false;
michael@0 1443 }
michael@0 1444 }
michael@0 1445 }
michael@0 1446
michael@0 1447 // The CIE's instructions start here.
michael@0 1448 cie->instructions = cursor;
michael@0 1449
michael@0 1450 return true;
michael@0 1451 }
michael@0 1452
michael@0 1453 bool CallFrameInfo::ReadFDEFields(FDE *fde) {
michael@0 1454 const char *cursor = fde->fields;
michael@0 1455 size_t size;
michael@0 1456
michael@0 1457 fde->address = reader_->ReadEncodedPointer(cursor, fde->cie->pointer_encoding,
michael@0 1458 &size);
michael@0 1459 if (size > size_t(fde->end - cursor))
michael@0 1460 return ReportIncomplete(fde);
michael@0 1461 cursor += size;
michael@0 1462 reader_->SetFunctionBase(fde->address);
michael@0 1463
michael@0 1464 // For the length, we strip off the upper nybble of the encoding used for
michael@0 1465 // the starting address.
michael@0 1466 DwarfPointerEncoding length_encoding =
michael@0 1467 DwarfPointerEncoding(fde->cie->pointer_encoding & 0x0f);
michael@0 1468 fde->size = reader_->ReadEncodedPointer(cursor, length_encoding, &size);
michael@0 1469 if (size > size_t(fde->end - cursor))
michael@0 1470 return ReportIncomplete(fde);
michael@0 1471 cursor += size;
michael@0 1472
michael@0 1473 // If the CIE has a 'z' augmentation string, then augmentation data
michael@0 1474 // appears here.
michael@0 1475 if (fde->cie->has_z_augmentation) {
michael@0 1476 uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &size);
michael@0 1477 if (size_t(fde->end - cursor) < size + data_size)
michael@0 1478 return ReportIncomplete(fde);
michael@0 1479 cursor += size;
michael@0 1480
michael@0 1481 // In the abstract, we should walk the augmentation string, and extract
michael@0 1482 // items from the FDE's augmentation data as we encounter augmentation
michael@0 1483 // string characters that specify their presence: the ordering of items
michael@0 1484 // in the augmentation string determines the arrangement of values in
michael@0 1485 // the augmentation data.
michael@0 1486 //
michael@0 1487 // In practice, there's only ever one value in FDE augmentation data
michael@0 1488 // that we support --- the LSDA pointer --- and we have to bail if we
michael@0 1489 // see any unrecognized augmentation string characters. So if there is
michael@0 1490 // anything here at all, we know what it is, and where it starts.
michael@0 1491 if (fde->cie->has_z_lsda) {
michael@0 1492 // Check whether the LSDA's pointer encoding is usable now: only once
michael@0 1493 // we've parsed the FDE's starting address do we call reader_->
michael@0 1494 // SetFunctionBase, so that the DW_EH_PE_funcrel encoding becomes
michael@0 1495 // usable.
michael@0 1496 if (!reader_->UsableEncoding(fde->cie->lsda_encoding)) {
michael@0 1497 reporter_->UnusablePointerEncoding(fde->cie->offset,
michael@0 1498 fde->cie->lsda_encoding);
michael@0 1499 return false;
michael@0 1500 }
michael@0 1501
michael@0 1502 fde->lsda_address =
michael@0 1503 reader_->ReadEncodedPointer(cursor, fde->cie->lsda_encoding, &size);
michael@0 1504 if (size > data_size)
michael@0 1505 return ReportIncomplete(fde);
michael@0 1506 // Ideally, we would also complain here if there were unconsumed
michael@0 1507 // augmentation data.
michael@0 1508 }
michael@0 1509
michael@0 1510 cursor += data_size;
michael@0 1511 }
michael@0 1512
michael@0 1513 // The FDE's instructions start after those.
michael@0 1514 fde->instructions = cursor;
michael@0 1515
michael@0 1516 return true;
michael@0 1517 }
michael@0 1518
michael@0 1519 bool CallFrameInfo::Start() {
michael@0 1520 const char *buffer_end = buffer_ + buffer_length_;
michael@0 1521 const char *cursor;
michael@0 1522 bool all_ok = true;
michael@0 1523 const char *entry_end;
michael@0 1524 bool ok;
michael@0 1525
michael@0 1526 // Traverse all the entries in buffer_, skipping CIEs and offering
michael@0 1527 // FDEs to the handler.
michael@0 1528 for (cursor = buffer_; cursor < buffer_end;
michael@0 1529 cursor = entry_end, all_ok = all_ok && ok) {
michael@0 1530 FDE fde;
michael@0 1531
michael@0 1532 // Make it easy to skip this entry with 'continue': assume that
michael@0 1533 // things are not okay until we've checked all the data, and
michael@0 1534 // prepare the address of the next entry.
michael@0 1535 ok = false;
michael@0 1536
michael@0 1537 // Read the entry's prologue.
michael@0 1538 if (!ReadEntryPrologue(cursor, &fde)) {
michael@0 1539 if (!fde.end) {
michael@0 1540 // If we couldn't even figure out this entry's extent, then we
michael@0 1541 // must stop processing entries altogether.
michael@0 1542 all_ok = false;
michael@0 1543 break;
michael@0 1544 }
michael@0 1545 entry_end = fde.end;
michael@0 1546 continue;
michael@0 1547 }
michael@0 1548
michael@0 1549 // The next iteration picks up after this entry.
michael@0 1550 entry_end = fde.end;
michael@0 1551
michael@0 1552 // Did we see an .eh_frame terminating mark?
michael@0 1553 if (fde.kind == kTerminator) {
michael@0 1554 // If there appears to be more data left in the section after the
michael@0 1555 // terminating mark, warn the user. But this is just a warning;
michael@0 1556 // we leave all_ok true.
michael@0 1557 if (fde.end < buffer_end) reporter_->EarlyEHTerminator(fde.offset);
michael@0 1558 break;
michael@0 1559 }
michael@0 1560
michael@0 1561 // In this loop, we skip CIEs. We only parse them fully when we
michael@0 1562 // parse an FDE that refers to them. This limits our memory
michael@0 1563 // consumption (beyond the buffer itself) to that needed to
michael@0 1564 // process the largest single entry.
michael@0 1565 if (fde.kind != kFDE) {
michael@0 1566 ok = true;
michael@0 1567 continue;
michael@0 1568 }
michael@0 1569
michael@0 1570 // Validate the CIE pointer.
michael@0 1571 if (fde.id > buffer_length_) {
michael@0 1572 reporter_->CIEPointerOutOfRange(fde.offset, fde.id);
michael@0 1573 continue;
michael@0 1574 }
michael@0 1575
michael@0 1576 CIE cie;
michael@0 1577
michael@0 1578 // Parse this FDE's CIE header.
michael@0 1579 if (!ReadEntryPrologue(buffer_ + fde.id, &cie))
michael@0 1580 continue;
michael@0 1581 // This had better be an actual CIE.
michael@0 1582 if (cie.kind != kCIE) {
michael@0 1583 reporter_->BadCIEId(fde.offset, fde.id);
michael@0 1584 continue;
michael@0 1585 }
michael@0 1586 if (!ReadCIEFields(&cie))
michael@0 1587 continue;
michael@0 1588
michael@0 1589 // We now have the values that govern both the CIE and the FDE.
michael@0 1590 cie.cie = &cie;
michael@0 1591 fde.cie = &cie;
michael@0 1592
michael@0 1593 // Parse the FDE's header.
michael@0 1594 if (!ReadFDEFields(&fde))
michael@0 1595 continue;
michael@0 1596
michael@0 1597 // Call Entry to ask the consumer if they're interested.
michael@0 1598 if (!handler_->Entry(fde.offset, fde.address, fde.size,
michael@0 1599 cie.version, cie.augmentation,
michael@0 1600 cie.return_address_register)) {
michael@0 1601 // The handler isn't interested in this entry. That's not an error.
michael@0 1602 ok = true;
michael@0 1603 continue;
michael@0 1604 }
michael@0 1605
michael@0 1606 if (cie.has_z_augmentation) {
michael@0 1607 // Report the personality routine address, if we have one.
michael@0 1608 if (cie.has_z_personality) {
michael@0 1609 if (!handler_
michael@0 1610 ->PersonalityRoutine(cie.personality_address,
michael@0 1611 IsIndirectEncoding(cie.personality_encoding)))
michael@0 1612 continue;
michael@0 1613 }
michael@0 1614
michael@0 1615 // Report the language-specific data area address, if we have one.
michael@0 1616 if (cie.has_z_lsda) {
michael@0 1617 if (!handler_
michael@0 1618 ->LanguageSpecificDataArea(fde.lsda_address,
michael@0 1619 IsIndirectEncoding(cie.lsda_encoding)))
michael@0 1620 continue;
michael@0 1621 }
michael@0 1622
michael@0 1623 // If this is a signal-handling frame, report that.
michael@0 1624 if (cie.has_z_signal_frame) {
michael@0 1625 if (!handler_->SignalHandler())
michael@0 1626 continue;
michael@0 1627 }
michael@0 1628 }
michael@0 1629
michael@0 1630 // Interpret the CIE's instructions, and then the FDE's instructions.
michael@0 1631 State state(reader_, handler_, reporter_, fde.address);
michael@0 1632 ok = state.InterpretCIE(cie) && state.InterpretFDE(fde);
michael@0 1633
michael@0 1634 // Tell the ByteReader that the function start address from the
michael@0 1635 // FDE header is no longer valid.
michael@0 1636 reader_->ClearFunctionBase();
michael@0 1637
michael@0 1638 // Report the end of the entry.
michael@0 1639 handler_->End();
michael@0 1640 }
michael@0 1641
michael@0 1642 return all_ok;
michael@0 1643 }
michael@0 1644
michael@0 1645 const char *CallFrameInfo::KindName(EntryKind kind) {
michael@0 1646 if (kind == CallFrameInfo::kUnknown)
michael@0 1647 return "entry";
michael@0 1648 else if (kind == CallFrameInfo::kCIE)
michael@0 1649 return "common information entry";
michael@0 1650 else if (kind == CallFrameInfo::kFDE)
michael@0 1651 return "frame description entry";
michael@0 1652 else {
michael@0 1653 MOZ_ASSERT (kind == CallFrameInfo::kTerminator);
michael@0 1654 return ".eh_frame sequence terminator";
michael@0 1655 }
michael@0 1656 }
michael@0 1657
michael@0 1658 bool CallFrameInfo::ReportIncomplete(Entry *entry) {
michael@0 1659 reporter_->Incomplete(entry->offset, entry->kind);
michael@0 1660 return false;
michael@0 1661 }
michael@0 1662
michael@0 1663 void CallFrameInfo::Reporter::Incomplete(uint64 offset,
michael@0 1664 CallFrameInfo::EntryKind kind) {
michael@0 1665 char buf[300];
michael@0 1666 snprintf(buf, sizeof(buf),
michael@0 1667 "%s: CFI %s at offset 0x%llx in '%s': entry ends early\n",
michael@0 1668 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
michael@0 1669 section_.c_str());
michael@0 1670 log_(buf);
michael@0 1671 }
michael@0 1672
michael@0 1673 void CallFrameInfo::Reporter::EarlyEHTerminator(uint64 offset) {
michael@0 1674 char buf[300];
michael@0 1675 snprintf(buf, sizeof(buf),
michael@0 1676 "%s: CFI at offset 0x%llx in '%s': saw end-of-data marker"
michael@0 1677 " before end of section contents\n",
michael@0 1678 filename_.c_str(), offset, section_.c_str());
michael@0 1679 log_(buf);
michael@0 1680 }
michael@0 1681
michael@0 1682 void CallFrameInfo::Reporter::CIEPointerOutOfRange(uint64 offset,
michael@0 1683 uint64 cie_offset) {
michael@0 1684 char buf[300];
michael@0 1685 snprintf(buf, sizeof(buf),
michael@0 1686 "%s: CFI frame description entry at offset 0x%llx in '%s':"
michael@0 1687 " CIE pointer is out of range: 0x%llx\n",
michael@0 1688 filename_.c_str(), offset, section_.c_str(), cie_offset);
michael@0 1689 log_(buf);
michael@0 1690 }
michael@0 1691
michael@0 1692 void CallFrameInfo::Reporter::BadCIEId(uint64 offset, uint64 cie_offset) {
michael@0 1693 char buf[300];
michael@0 1694 snprintf(buf, sizeof(buf),
michael@0 1695 "%s: CFI frame description entry at offset 0x%llx in '%s':"
michael@0 1696 " CIE pointer does not point to a CIE: 0x%llx\n",
michael@0 1697 filename_.c_str(), offset, section_.c_str(), cie_offset);
michael@0 1698 log_(buf);
michael@0 1699 }
michael@0 1700
michael@0 1701 void CallFrameInfo::Reporter::UnrecognizedVersion(uint64 offset, int version) {
michael@0 1702 char buf[300];
michael@0 1703 snprintf(buf, sizeof(buf),
michael@0 1704 "%s: CFI frame description entry at offset 0x%llx in '%s':"
michael@0 1705 " CIE specifies unrecognized version: %d\n",
michael@0 1706 filename_.c_str(), offset, section_.c_str(), version);
michael@0 1707 log_(buf);
michael@0 1708 }
michael@0 1709
michael@0 1710 void CallFrameInfo::Reporter::UnrecognizedAugmentation(uint64 offset,
michael@0 1711 const string &aug) {
michael@0 1712 char buf[300];
michael@0 1713 snprintf(buf, sizeof(buf),
michael@0 1714 "%s: CFI frame description entry at offset 0x%llx in '%s':"
michael@0 1715 " CIE specifies unrecognized augmentation: '%s'\n",
michael@0 1716 filename_.c_str(), offset, section_.c_str(), aug.c_str());
michael@0 1717 log_(buf);
michael@0 1718 }
michael@0 1719
michael@0 1720 void CallFrameInfo::Reporter::InvalidPointerEncoding(uint64 offset,
michael@0 1721 uint8 encoding) {
michael@0 1722 char buf[300];
michael@0 1723 snprintf(buf, sizeof(buf),
michael@0 1724 "%s: CFI common information entry at offset 0x%llx in '%s':"
michael@0 1725 " 'z' augmentation specifies invalid pointer encoding: 0x%02x\n",
michael@0 1726 filename_.c_str(), offset, section_.c_str(), encoding);
michael@0 1727 log_(buf);
michael@0 1728 }
michael@0 1729
michael@0 1730 void CallFrameInfo::Reporter::UnusablePointerEncoding(uint64 offset,
michael@0 1731 uint8 encoding) {
michael@0 1732 char buf[300];
michael@0 1733 snprintf(buf, sizeof(buf),
michael@0 1734 "%s: CFI common information entry at offset 0x%llx in '%s':"
michael@0 1735 " 'z' augmentation specifies a pointer encoding for which"
michael@0 1736 " we have no base address: 0x%02x\n",
michael@0 1737 filename_.c_str(), offset, section_.c_str(), encoding);
michael@0 1738 log_(buf);
michael@0 1739 }
michael@0 1740
michael@0 1741 void CallFrameInfo::Reporter::RestoreInCIE(uint64 offset, uint64 insn_offset) {
michael@0 1742 char buf[300];
michael@0 1743 snprintf(buf, sizeof(buf),
michael@0 1744 "%s: CFI common information entry at offset 0x%llx in '%s':"
michael@0 1745 " the DW_CFA_restore instruction at offset 0x%llx"
michael@0 1746 " cannot be used in a common information entry\n",
michael@0 1747 filename_.c_str(), offset, section_.c_str(), insn_offset);
michael@0 1748 log_(buf);
michael@0 1749 }
michael@0 1750
michael@0 1751 void CallFrameInfo::Reporter::BadInstruction(uint64 offset,
michael@0 1752 CallFrameInfo::EntryKind kind,
michael@0 1753 uint64 insn_offset) {
michael@0 1754 char buf[300];
michael@0 1755 snprintf(buf, sizeof(buf),
michael@0 1756 "%s: CFI %s at offset 0x%llx in section '%s':"
michael@0 1757 " the instruction at offset 0x%llx is unrecognized\n",
michael@0 1758 filename_.c_str(), CallFrameInfo::KindName(kind),
michael@0 1759 offset, section_.c_str(), insn_offset);
michael@0 1760 log_(buf);
michael@0 1761 }
michael@0 1762
michael@0 1763 void CallFrameInfo::Reporter::NoCFARule(uint64 offset,
michael@0 1764 CallFrameInfo::EntryKind kind,
michael@0 1765 uint64 insn_offset) {
michael@0 1766 char buf[300];
michael@0 1767 snprintf(buf, sizeof(buf),
michael@0 1768 "%s: CFI %s at offset 0x%llx in section '%s':"
michael@0 1769 " the instruction at offset 0x%llx assumes that a CFA rule has"
michael@0 1770 " been set, but none has been set\n",
michael@0 1771 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
michael@0 1772 section_.c_str(), insn_offset);
michael@0 1773 log_(buf);
michael@0 1774 }
michael@0 1775
michael@0 1776 void CallFrameInfo::Reporter::EmptyStateStack(uint64 offset,
michael@0 1777 CallFrameInfo::EntryKind kind,
michael@0 1778 uint64 insn_offset) {
michael@0 1779 char buf[300];
michael@0 1780 snprintf(buf, sizeof(buf),
michael@0 1781 "%s: CFI %s at offset 0x%llx in section '%s':"
michael@0 1782 " the DW_CFA_restore_state instruction at offset 0x%llx"
michael@0 1783 " should pop a saved state from the stack, but the stack is empty\n",
michael@0 1784 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
michael@0 1785 section_.c_str(), insn_offset);
michael@0 1786 log_(buf);
michael@0 1787 }
michael@0 1788
michael@0 1789 void CallFrameInfo::Reporter::ClearingCFARule(uint64 offset,
michael@0 1790 CallFrameInfo::EntryKind kind,
michael@0 1791 uint64 insn_offset) {
michael@0 1792 char buf[300];
michael@0 1793 snprintf(buf, sizeof(buf),
michael@0 1794 "%s: CFI %s at offset 0x%llx in section '%s':"
michael@0 1795 " the DW_CFA_restore_state instruction at offset 0x%llx"
michael@0 1796 " would clear the CFA rule in effect\n",
michael@0 1797 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
michael@0 1798 section_.c_str(), insn_offset);
michael@0 1799 log_(buf);
michael@0 1800 }
michael@0 1801
michael@0 1802
michael@0 1803 const unsigned int DwarfCFIToModule::RegisterNames::I386() {
michael@0 1804 /*
michael@0 1805 8 "$eax", "$ecx", "$edx", "$ebx", "$esp", "$ebp", "$esi", "$edi",
michael@0 1806 3 "$eip", "$eflags", "$unused1",
michael@0 1807 8 "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7",
michael@0 1808 2 "$unused2", "$unused3",
michael@0 1809 8 "$xmm0", "$xmm1", "$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7",
michael@0 1810 8 "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7",
michael@0 1811 3 "$fcw", "$fsw", "$mxcsr",
michael@0 1812 8 "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused4", "$unused5",
michael@0 1813 2 "$tr", "$ldtr"
michael@0 1814 */
michael@0 1815 return 8 + 3 + 8 + 2 + 8 + 8 + 3 + 8 + 2;
michael@0 1816 }
michael@0 1817
michael@0 1818 const unsigned int DwarfCFIToModule::RegisterNames::X86_64() {
michael@0 1819 /*
michael@0 1820 8 "$rax", "$rdx", "$rcx", "$rbx", "$rsi", "$rdi", "$rbp", "$rsp",
michael@0 1821 8 "$r8", "$r9", "$r10", "$r11", "$r12", "$r13", "$r14", "$r15",
michael@0 1822 1 "$rip",
michael@0 1823 8 "$xmm0","$xmm1","$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7",
michael@0 1824 8 "$xmm8","$xmm9","$xmm10","$xmm11","$xmm12","$xmm13","$xmm14","$xmm15",
michael@0 1825 8 "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7",
michael@0 1826 8 "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7",
michael@0 1827 1 "$rflags",
michael@0 1828 8 "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused1", "$unused2",
michael@0 1829 4 "$fs.base", "$gs.base", "$unused3", "$unused4",
michael@0 1830 2 "$tr", "$ldtr",
michael@0 1831 3 "$mxcsr", "$fcw", "$fsw"
michael@0 1832 */
michael@0 1833 return 8 + 8 + 1 + 8 + 8 + 8 + 8 + 1 + 8 + 4 + 2 + 3;
michael@0 1834 }
michael@0 1835
michael@0 1836 // Per ARM IHI 0040A, section 3.1
michael@0 1837 const unsigned int DwarfCFIToModule::RegisterNames::ARM() {
michael@0 1838 /*
michael@0 1839 8 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
michael@0 1840 8 "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
michael@0 1841 8 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
michael@0 1842 8 "fps", "cpsr", "", "", "", "", "", "",
michael@0 1843 8 "", "", "", "", "", "", "", "",
michael@0 1844 8 "", "", "", "", "", "", "", "",
michael@0 1845 8 "", "", "", "", "", "", "", "",
michael@0 1846 8 "", "", "", "", "", "", "", "",
michael@0 1847 8 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
michael@0 1848 8 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
michael@0 1849 8 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
michael@0 1850 8 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
michael@0 1851 8 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7"
michael@0 1852 */
michael@0 1853 return 13 * 8;
michael@0 1854 }
michael@0 1855
michael@0 1856 bool DwarfCFIToModule::Entry(size_t offset, uint64 address, uint64 length,
michael@0 1857 uint8 version, const string &augmentation,
michael@0 1858 unsigned return_address) {
michael@0 1859 if (DEBUG_DWARF)
michael@0 1860 printf("LUL.DW DwarfCFIToModule::Entry 0x%llx,+%lld\n", address, length);
michael@0 1861
michael@0 1862 summ_->Entry(address, length);
michael@0 1863
michael@0 1864 // If dwarf2reader::CallFrameInfo can handle this version and
michael@0 1865 // augmentation, then we should be okay with that, so there's no
michael@0 1866 // need to check them here.
michael@0 1867
michael@0 1868 // Get ready to collect entries.
michael@0 1869 return_address_ = return_address;
michael@0 1870
michael@0 1871 // Breakpad STACK CFI records must provide a .ra rule, but DWARF CFI
michael@0 1872 // may not establish any rule for .ra if the return address column
michael@0 1873 // is an ordinary register, and that register holds the return
michael@0 1874 // address on entry to the function. So establish an initial .ra
michael@0 1875 // rule citing the return address register.
michael@0 1876 if (return_address_ < num_dw_regs_) {
michael@0 1877 summ_->Rule(address, return_address_, return_address, 0, false);
michael@0 1878 }
michael@0 1879
michael@0 1880 return true;
michael@0 1881 }
michael@0 1882
michael@0 1883 const UniqueString* DwarfCFIToModule::RegisterName(int i) {
michael@0 1884 if (i < 0) {
michael@0 1885 MOZ_ASSERT(i == kCFARegister);
michael@0 1886 return ustr__ZDcfa();
michael@0 1887 }
michael@0 1888 unsigned reg = i;
michael@0 1889 if (reg == return_address_)
michael@0 1890 return ustr__ZDra();
michael@0 1891
michael@0 1892 char buf[30];
michael@0 1893 sprintf(buf, "dwarf_reg_%u", reg);
michael@0 1894 return ToUniqueString(buf);
michael@0 1895 }
michael@0 1896
michael@0 1897 bool DwarfCFIToModule::UndefinedRule(uint64 address, int reg) {
michael@0 1898 reporter_->UndefinedNotSupported(entry_offset_, RegisterName(reg));
michael@0 1899 // Treat this as a non-fatal error.
michael@0 1900 return true;
michael@0 1901 }
michael@0 1902
michael@0 1903 bool DwarfCFIToModule::SameValueRule(uint64 address, int reg) {
michael@0 1904 if (DEBUG_DWARF)
michael@0 1905 printf("LUL.DW 0x%llx: old r%d = Same\n", address, reg);
michael@0 1906 // reg + 0
michael@0 1907 summ_->Rule(address, reg, reg, 0, false);
michael@0 1908 return true;
michael@0 1909 }
michael@0 1910
michael@0 1911 bool DwarfCFIToModule::OffsetRule(uint64 address, int reg,
michael@0 1912 int base_register, long offset) {
michael@0 1913 if (DEBUG_DWARF)
michael@0 1914 printf("LUL.DW 0x%llx: old r%d = *(r%d + %ld)\n",
michael@0 1915 address, reg, base_register, offset);
michael@0 1916 // *(base_register + offset)
michael@0 1917 summ_->Rule(address, reg, base_register, offset, true);
michael@0 1918 return true;
michael@0 1919 }
michael@0 1920
michael@0 1921 bool DwarfCFIToModule::ValOffsetRule(uint64 address, int reg,
michael@0 1922 int base_register, long offset) {
michael@0 1923 if (DEBUG_DWARF)
michael@0 1924 printf("LUL.DW 0x%llx: old r%d = r%d + %ld\n",
michael@0 1925 address, reg, base_register, offset);
michael@0 1926 // base_register + offset
michael@0 1927 summ_->Rule(address, reg, base_register, offset, false);
michael@0 1928 return true;
michael@0 1929 }
michael@0 1930
michael@0 1931 bool DwarfCFIToModule::RegisterRule(uint64 address, int reg,
michael@0 1932 int base_register) {
michael@0 1933 if (DEBUG_DWARF)
michael@0 1934 printf("LUL.DW 0x%llx: old r%d = r%d\n", address, reg, base_register);
michael@0 1935 // base_register + 0
michael@0 1936 summ_->Rule(address, reg, base_register, 0, false);
michael@0 1937 return true;
michael@0 1938 }
michael@0 1939
michael@0 1940 bool DwarfCFIToModule::ExpressionRule(uint64 address, int reg,
michael@0 1941 const string &expression) {
michael@0 1942 reporter_->ExpressionsNotSupported(entry_offset_, RegisterName(reg));
michael@0 1943 // Treat this as a non-fatal error.
michael@0 1944 return true;
michael@0 1945 }
michael@0 1946
michael@0 1947 bool DwarfCFIToModule::ValExpressionRule(uint64 address, int reg,
michael@0 1948 const string &expression) {
michael@0 1949 reporter_->ExpressionsNotSupported(entry_offset_, RegisterName(reg));
michael@0 1950 // Treat this as a non-fatal error.
michael@0 1951 return true;
michael@0 1952 }
michael@0 1953
michael@0 1954 bool DwarfCFIToModule::End() {
michael@0 1955 //module_->AddStackFrameEntry(entry_);
michael@0 1956 if (DEBUG_DWARF)
michael@0 1957 printf("LUL.DW DwarfCFIToModule::End()\n");
michael@0 1958 summ_->End();
michael@0 1959 return true;
michael@0 1960 }
michael@0 1961
michael@0 1962 void DwarfCFIToModule::Reporter::UndefinedNotSupported(
michael@0 1963 size_t offset,
michael@0 1964 const UniqueString* reg) {
michael@0 1965 char buf[300];
michael@0 1966 snprintf(buf, sizeof(buf),
michael@0 1967 "DwarfCFIToModule::Reporter::UndefinedNotSupported()\n");
michael@0 1968 log_(buf);
michael@0 1969 //BPLOG(INFO) << file_ << ", section '" << section_
michael@0 1970 // << "': the call frame entry at offset 0x"
michael@0 1971 // << std::setbase(16) << offset << std::setbase(10)
michael@0 1972 // << " sets the rule for register '" << FromUniqueString(reg)
michael@0 1973 // << "' to 'undefined', but the Breakpad symbol file format cannot "
michael@0 1974 // << " express this";
michael@0 1975 }
michael@0 1976
michael@0 1977 // FIXME: move this somewhere sensible
michael@0 1978 static bool is_power_of_2(uint64_t n)
michael@0 1979 {
michael@0 1980 int i, nSetBits = 0;
michael@0 1981 for (i = 0; i < 8*(int)sizeof(n); i++) {
michael@0 1982 if ((n & ((uint64_t)1) << i) != 0)
michael@0 1983 nSetBits++;
michael@0 1984 }
michael@0 1985 return nSetBits <= 1;
michael@0 1986 }
michael@0 1987
michael@0 1988 void DwarfCFIToModule::Reporter::ExpressionsNotSupported(
michael@0 1989 size_t offset,
michael@0 1990 const UniqueString* reg) {
michael@0 1991 static uint64_t n_complaints = 0; // This isn't threadsafe
michael@0 1992 n_complaints++;
michael@0 1993 if (!is_power_of_2(n_complaints))
michael@0 1994 return;
michael@0 1995 char buf[300];
michael@0 1996 snprintf(buf, sizeof(buf),
michael@0 1997 "DwarfCFIToModule::Reporter::"
michael@0 1998 "ExpressionsNotSupported(shown %llu times)\n",
michael@0 1999 (unsigned long long int)n_complaints);
michael@0 2000 log_(buf);
michael@0 2001 //BPLOG(INFO) << file_ << ", section '" << section_
michael@0 2002 // << "': the call frame entry at offset 0x"
michael@0 2003 // << std::setbase(16) << offset << std::setbase(10)
michael@0 2004 // << " uses a DWARF expression to describe how to recover register '"
michael@0 2005 // << FromUniqueString(reg) << "', but this translator cannot yet "
michael@0 2006 // << "translate DWARF expressions to Breakpad postfix expressions (shown "
michael@0 2007 // << n_complaints << " times)";
michael@0 2008 }
michael@0 2009
michael@0 2010 } // namespace lul

mercurial