michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: michael@0: // Copyright (c) 2010 Google Inc. All Rights Reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // CFI reader author: Jim Blandy michael@0: // Original author: Jim Blandy michael@0: michael@0: // Implementation of dwarf2reader::LineInfo, dwarf2reader::CompilationUnit, michael@0: // and dwarf2reader::CallFrameInfo. See dwarf2reader.h for details. michael@0: michael@0: // This file is derived from the following files in michael@0: // toolkit/crashreporter/google-breakpad: michael@0: // src/common/dwarf/bytereader.cc michael@0: // src/common/dwarf/dwarf2reader.cc michael@0: // src/common/dwarf_cfi_to_module.cc michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: michael@0: #include "LulCommonExt.h" michael@0: #include "LulDwarfInt.h" michael@0: michael@0: // Set this to 1 for verbose logging michael@0: #define DEBUG_DWARF 0 michael@0: michael@0: michael@0: namespace lul { michael@0: michael@0: using std::string; michael@0: michael@0: ByteReader::ByteReader(enum Endianness endian) michael@0: :offset_reader_(NULL), address_reader_(NULL), endian_(endian), michael@0: address_size_(0), offset_size_(0), michael@0: have_section_base_(), have_text_base_(), have_data_base_(), michael@0: have_function_base_() { } michael@0: michael@0: ByteReader::~ByteReader() { } michael@0: michael@0: void ByteReader::SetOffsetSize(uint8 size) { michael@0: offset_size_ = size; michael@0: MOZ_ASSERT(size == 4 || size == 8); michael@0: if (size == 4) { michael@0: this->offset_reader_ = &ByteReader::ReadFourBytes; michael@0: } else { michael@0: this->offset_reader_ = &ByteReader::ReadEightBytes; michael@0: } michael@0: } michael@0: michael@0: void ByteReader::SetAddressSize(uint8 size) { michael@0: address_size_ = size; michael@0: MOZ_ASSERT(size == 4 || size == 8); michael@0: if (size == 4) { michael@0: this->address_reader_ = &ByteReader::ReadFourBytes; michael@0: } else { michael@0: this->address_reader_ = &ByteReader::ReadEightBytes; michael@0: } michael@0: } michael@0: michael@0: uint64 ByteReader::ReadInitialLength(const char* start, size_t* len) { michael@0: const uint64 initial_length = ReadFourBytes(start); michael@0: start += 4; michael@0: michael@0: // In DWARF2/3, if the initial length is all 1 bits, then the offset michael@0: // size is 8 and we need to read the next 8 bytes for the real length. michael@0: if (initial_length == 0xffffffff) { michael@0: SetOffsetSize(8); michael@0: *len = 12; michael@0: return ReadOffset(start); michael@0: } else { michael@0: SetOffsetSize(4); michael@0: *len = 4; michael@0: } michael@0: return initial_length; michael@0: } michael@0: michael@0: bool ByteReader::ValidEncoding(DwarfPointerEncoding encoding) const { michael@0: if (encoding == DW_EH_PE_omit) return true; michael@0: if (encoding == DW_EH_PE_aligned) return true; michael@0: if ((encoding & 0x7) > DW_EH_PE_udata8) michael@0: return false; michael@0: if ((encoding & 0x70) > DW_EH_PE_funcrel) michael@0: return false; michael@0: return true; michael@0: } michael@0: michael@0: bool ByteReader::UsableEncoding(DwarfPointerEncoding encoding) const { michael@0: switch (encoding & 0x70) { michael@0: case DW_EH_PE_absptr: return true; michael@0: case DW_EH_PE_pcrel: return have_section_base_; michael@0: case DW_EH_PE_textrel: return have_text_base_; michael@0: case DW_EH_PE_datarel: return have_data_base_; michael@0: case DW_EH_PE_funcrel: return have_function_base_; michael@0: default: return false; michael@0: } michael@0: } michael@0: michael@0: uint64 ByteReader::ReadEncodedPointer(const char *buffer, michael@0: DwarfPointerEncoding encoding, michael@0: size_t *len) const { michael@0: // UsableEncoding doesn't approve of DW_EH_PE_omit, so we shouldn't michael@0: // see it here. michael@0: MOZ_ASSERT(encoding != DW_EH_PE_omit); michael@0: michael@0: // The Linux Standards Base 4.0 does not make this clear, but the michael@0: // GNU tools (gcc/unwind-pe.h; readelf/dwarf.c; gdb/dwarf2-frame.c) michael@0: // agree that aligned pointers are always absolute, machine-sized, michael@0: // machine-signed pointers. michael@0: if (encoding == DW_EH_PE_aligned) { michael@0: MOZ_ASSERT(have_section_base_); michael@0: michael@0: // We don't need to align BUFFER in *our* address space. Rather, we michael@0: // need to find the next position in our buffer that would be aligned michael@0: // when the .eh_frame section the buffer contains is loaded into the michael@0: // program's memory. So align assuming that buffer_base_ gets loaded at michael@0: // address section_base_, where section_base_ itself may or may not be michael@0: // aligned. michael@0: michael@0: // First, find the offset to START from the closest prior aligned michael@0: // address. michael@0: uint64 skew = section_base_ & (AddressSize() - 1); michael@0: // Now find the offset from that aligned address to buffer. michael@0: uint64 offset = skew + (buffer - buffer_base_); michael@0: // Round up to the next boundary. michael@0: uint64 aligned = (offset + AddressSize() - 1) & -AddressSize(); michael@0: // Convert back to a pointer. michael@0: const char *aligned_buffer = buffer_base_ + (aligned - skew); michael@0: // Finally, store the length and actually fetch the pointer. michael@0: *len = aligned_buffer - buffer + AddressSize(); michael@0: return ReadAddress(aligned_buffer); michael@0: } michael@0: michael@0: // Extract the value first, ignoring whether it's a pointer or an michael@0: // offset relative to some base. michael@0: uint64 offset; michael@0: switch (encoding & 0x0f) { michael@0: case DW_EH_PE_absptr: michael@0: // DW_EH_PE_absptr is weird, as it is used as a meaningful value for michael@0: // both the high and low nybble of encoding bytes. When it appears in michael@0: // the high nybble, it means that the pointer is absolute, not an michael@0: // offset from some base address. When it appears in the low nybble, michael@0: // as here, it means that the pointer is stored as a normal michael@0: // machine-sized and machine-signed address. A low nybble of michael@0: // DW_EH_PE_absptr does not imply that the pointer is absolute; it is michael@0: // correct for us to treat the value as an offset from a base address michael@0: // if the upper nybble is not DW_EH_PE_absptr. michael@0: offset = ReadAddress(buffer); michael@0: *len = AddressSize(); michael@0: break; michael@0: michael@0: case DW_EH_PE_uleb128: michael@0: offset = ReadUnsignedLEB128(buffer, len); michael@0: break; michael@0: michael@0: case DW_EH_PE_udata2: michael@0: offset = ReadTwoBytes(buffer); michael@0: *len = 2; michael@0: break; michael@0: michael@0: case DW_EH_PE_udata4: michael@0: offset = ReadFourBytes(buffer); michael@0: *len = 4; michael@0: break; michael@0: michael@0: case DW_EH_PE_udata8: michael@0: offset = ReadEightBytes(buffer); michael@0: *len = 8; michael@0: break; michael@0: michael@0: case DW_EH_PE_sleb128: michael@0: offset = ReadSignedLEB128(buffer, len); michael@0: break; michael@0: michael@0: case DW_EH_PE_sdata2: michael@0: offset = ReadTwoBytes(buffer); michael@0: // Sign-extend from 16 bits. michael@0: offset = (offset ^ 0x8000) - 0x8000; michael@0: *len = 2; michael@0: break; michael@0: michael@0: case DW_EH_PE_sdata4: michael@0: offset = ReadFourBytes(buffer); michael@0: // Sign-extend from 32 bits. michael@0: offset = (offset ^ 0x80000000ULL) - 0x80000000ULL; michael@0: *len = 4; michael@0: break; michael@0: michael@0: case DW_EH_PE_sdata8: michael@0: // No need to sign-extend; this is the full width of our type. michael@0: offset = ReadEightBytes(buffer); michael@0: *len = 8; michael@0: break; michael@0: michael@0: default: michael@0: abort(); michael@0: } michael@0: michael@0: // Find the appropriate base address. michael@0: uint64 base; michael@0: switch (encoding & 0x70) { michael@0: case DW_EH_PE_absptr: michael@0: base = 0; michael@0: break; michael@0: michael@0: case DW_EH_PE_pcrel: michael@0: MOZ_ASSERT(have_section_base_); michael@0: base = section_base_ + (buffer - buffer_base_); michael@0: break; michael@0: michael@0: case DW_EH_PE_textrel: michael@0: MOZ_ASSERT(have_text_base_); michael@0: base = text_base_; michael@0: break; michael@0: michael@0: case DW_EH_PE_datarel: michael@0: MOZ_ASSERT(have_data_base_); michael@0: base = data_base_; michael@0: break; michael@0: michael@0: case DW_EH_PE_funcrel: michael@0: MOZ_ASSERT(have_function_base_); michael@0: base = function_base_; michael@0: break; michael@0: michael@0: default: michael@0: abort(); michael@0: } michael@0: michael@0: uint64 pointer = base + offset; michael@0: michael@0: // Remove inappropriate upper bits. michael@0: if (AddressSize() == 4) michael@0: pointer = pointer & 0xffffffff; michael@0: else michael@0: MOZ_ASSERT(AddressSize() == sizeof(uint64)); michael@0: michael@0: return pointer; michael@0: } michael@0: michael@0: michael@0: // A DWARF rule for recovering the address or value of a register, or michael@0: // computing the canonical frame address. There is one subclass of this for michael@0: // each '*Rule' member function in CallFrameInfo::Handler. michael@0: // michael@0: // It's annoying that we have to handle Rules using pointers (because michael@0: // the concrete instances can have an arbitrary size). They're small, michael@0: // so it would be much nicer if we could just handle them by value michael@0: // instead of fretting about ownership and destruction. michael@0: // michael@0: // It seems like all these could simply be instances of std::tr1::bind, michael@0: // except that we need instances to be EqualityComparable, too. michael@0: // michael@0: // This could logically be nested within State, but then the qualified names michael@0: // get horrendous. michael@0: class CallFrameInfo::Rule { michael@0: public: michael@0: virtual ~Rule() { } michael@0: michael@0: // Tell HANDLER that, at ADDRESS in the program, REGISTER can be michael@0: // recovered using this rule. If REGISTER is kCFARegister, then this rule michael@0: // describes how to compute the canonical frame address. Return what the michael@0: // HANDLER member function returned. michael@0: virtual bool Handle(Handler *handler, michael@0: uint64 address, int register) const = 0; michael@0: michael@0: // Equality on rules. We use these to decide which rules we need michael@0: // to report after a DW_CFA_restore_state instruction. michael@0: virtual bool operator==(const Rule &rhs) const = 0; michael@0: michael@0: bool operator!=(const Rule &rhs) const { return ! (*this == rhs); } michael@0: michael@0: // Return a pointer to a copy of this rule. michael@0: virtual Rule *Copy() const = 0; michael@0: michael@0: // If this is a base+offset rule, change its base register to REG. michael@0: // Otherwise, do nothing. (Ugly, but required for DW_CFA_def_cfa_register.) michael@0: virtual void SetBaseRegister(unsigned reg) { } michael@0: michael@0: // If this is a base+offset rule, change its offset to OFFSET. Otherwise, michael@0: // do nothing. (Ugly, but required for DW_CFA_def_cfa_offset.) michael@0: virtual void SetOffset(long long offset) { } michael@0: michael@0: // A RTTI workaround, to make it possible to implement equality michael@0: // comparisons on classes derived from this one. michael@0: enum CFIRTag { michael@0: CFIR_UNDEFINED_RULE, michael@0: CFIR_SAME_VALUE_RULE, michael@0: CFIR_OFFSET_RULE, michael@0: CFIR_VAL_OFFSET_RULE, michael@0: CFIR_REGISTER_RULE, michael@0: CFIR_EXPRESSION_RULE, michael@0: CFIR_VAL_EXPRESSION_RULE michael@0: }; michael@0: michael@0: // Produce the tag that identifies the child class of this object. michael@0: virtual CFIRTag getTag() const = 0; michael@0: }; michael@0: michael@0: // Rule: the value the register had in the caller cannot be recovered. michael@0: class CallFrameInfo::UndefinedRule: public CallFrameInfo::Rule { michael@0: public: michael@0: UndefinedRule() { } michael@0: ~UndefinedRule() { } michael@0: CFIRTag getTag() const { return CFIR_UNDEFINED_RULE; } michael@0: bool Handle(Handler *handler, uint64 address, int reg) const { michael@0: return handler->UndefinedRule(address, reg); michael@0: } michael@0: bool operator==(const Rule &rhs) const { michael@0: if (rhs.getTag() != CFIR_UNDEFINED_RULE) return false; michael@0: return true; michael@0: } michael@0: Rule *Copy() const { return new UndefinedRule(*this); } michael@0: }; michael@0: michael@0: // Rule: the register's value is the same as that it had in the caller. michael@0: class CallFrameInfo::SameValueRule: public CallFrameInfo::Rule { michael@0: public: michael@0: SameValueRule() { } michael@0: ~SameValueRule() { } michael@0: CFIRTag getTag() const { return CFIR_SAME_VALUE_RULE; } michael@0: bool Handle(Handler *handler, uint64 address, int reg) const { michael@0: return handler->SameValueRule(address, reg); michael@0: } michael@0: bool operator==(const Rule &rhs) const { michael@0: if (rhs.getTag() != CFIR_SAME_VALUE_RULE) return false; michael@0: return true; michael@0: } michael@0: Rule *Copy() const { return new SameValueRule(*this); } michael@0: }; michael@0: michael@0: // Rule: the register is saved at OFFSET from BASE_REGISTER. BASE_REGISTER michael@0: // may be CallFrameInfo::Handler::kCFARegister. michael@0: class CallFrameInfo::OffsetRule: public CallFrameInfo::Rule { michael@0: public: michael@0: OffsetRule(int base_register, long offset) michael@0: : base_register_(base_register), offset_(offset) { } michael@0: ~OffsetRule() { } michael@0: CFIRTag getTag() const { return CFIR_OFFSET_RULE; } michael@0: bool Handle(Handler *handler, uint64 address, int reg) const { michael@0: return handler->OffsetRule(address, reg, base_register_, offset_); michael@0: } michael@0: bool operator==(const Rule &rhs) const { michael@0: if (rhs.getTag() != CFIR_OFFSET_RULE) return false; michael@0: const OffsetRule *our_rhs = static_cast(&rhs); michael@0: return (base_register_ == our_rhs->base_register_ && michael@0: offset_ == our_rhs->offset_); michael@0: } michael@0: Rule *Copy() const { return new OffsetRule(*this); } michael@0: // We don't actually need SetBaseRegister or SetOffset here, since they michael@0: // are only ever applied to CFA rules, for DW_CFA_def_cfa_offset, and it michael@0: // doesn't make sense to use OffsetRule for computing the CFA: it michael@0: // computes the address at which a register is saved, not a value. michael@0: private: michael@0: int base_register_; michael@0: long offset_; michael@0: }; michael@0: michael@0: // Rule: the value the register had in the caller is the value of michael@0: // BASE_REGISTER plus offset. BASE_REGISTER may be michael@0: // CallFrameInfo::Handler::kCFARegister. michael@0: class CallFrameInfo::ValOffsetRule: public CallFrameInfo::Rule { michael@0: public: michael@0: ValOffsetRule(int base_register, long offset) michael@0: : base_register_(base_register), offset_(offset) { } michael@0: ~ValOffsetRule() { } michael@0: CFIRTag getTag() const { return CFIR_VAL_OFFSET_RULE; } michael@0: bool Handle(Handler *handler, uint64 address, int reg) const { michael@0: return handler->ValOffsetRule(address, reg, base_register_, offset_); michael@0: } michael@0: bool operator==(const Rule &rhs) const { michael@0: if (rhs.getTag() != CFIR_VAL_OFFSET_RULE) return false; michael@0: const ValOffsetRule *our_rhs = static_cast(&rhs); michael@0: return (base_register_ == our_rhs->base_register_ && michael@0: offset_ == our_rhs->offset_); michael@0: } michael@0: Rule *Copy() const { return new ValOffsetRule(*this); } michael@0: void SetBaseRegister(unsigned reg) { base_register_ = reg; } michael@0: void SetOffset(long long offset) { offset_ = offset; } michael@0: private: michael@0: int base_register_; michael@0: long offset_; michael@0: }; michael@0: michael@0: // Rule: the register has been saved in another register REGISTER_NUMBER_. michael@0: class CallFrameInfo::RegisterRule: public CallFrameInfo::Rule { michael@0: public: michael@0: explicit RegisterRule(int register_number) michael@0: : register_number_(register_number) { } michael@0: ~RegisterRule() { } michael@0: CFIRTag getTag() const { return CFIR_REGISTER_RULE; } michael@0: bool Handle(Handler *handler, uint64 address, int reg) const { michael@0: return handler->RegisterRule(address, reg, register_number_); michael@0: } michael@0: bool operator==(const Rule &rhs) const { michael@0: if (rhs.getTag() != CFIR_REGISTER_RULE) return false; michael@0: const RegisterRule *our_rhs = static_cast(&rhs); michael@0: return (register_number_ == our_rhs->register_number_); michael@0: } michael@0: Rule *Copy() const { return new RegisterRule(*this); } michael@0: private: michael@0: int register_number_; michael@0: }; michael@0: michael@0: // Rule: EXPRESSION evaluates to the address at which the register is saved. michael@0: class CallFrameInfo::ExpressionRule: public CallFrameInfo::Rule { michael@0: public: michael@0: explicit ExpressionRule(const string &expression) michael@0: : expression_(expression) { } michael@0: ~ExpressionRule() { } michael@0: CFIRTag getTag() const { return CFIR_EXPRESSION_RULE; } michael@0: bool Handle(Handler *handler, uint64 address, int reg) const { michael@0: return handler->ExpressionRule(address, reg, expression_); michael@0: } michael@0: bool operator==(const Rule &rhs) const { michael@0: if (rhs.getTag() != CFIR_EXPRESSION_RULE) return false; michael@0: const ExpressionRule *our_rhs = static_cast(&rhs); michael@0: return (expression_ == our_rhs->expression_); michael@0: } michael@0: Rule *Copy() const { return new ExpressionRule(*this); } michael@0: private: michael@0: string expression_; michael@0: }; michael@0: michael@0: // Rule: EXPRESSION evaluates to the previous value of the register. michael@0: class CallFrameInfo::ValExpressionRule: public CallFrameInfo::Rule { michael@0: public: michael@0: explicit ValExpressionRule(const string &expression) michael@0: : expression_(expression) { } michael@0: ~ValExpressionRule() { } michael@0: CFIRTag getTag() const { return CFIR_VAL_EXPRESSION_RULE; } michael@0: bool Handle(Handler *handler, uint64 address, int reg) const { michael@0: return handler->ValExpressionRule(address, reg, expression_); michael@0: } michael@0: bool operator==(const Rule &rhs) const { michael@0: if (rhs.getTag() != CFIR_VAL_EXPRESSION_RULE) return false; michael@0: const ValExpressionRule *our_rhs = michael@0: static_cast(&rhs); michael@0: return (expression_ == our_rhs->expression_); michael@0: } michael@0: Rule *Copy() const { return new ValExpressionRule(*this); } michael@0: private: michael@0: string expression_; michael@0: }; michael@0: michael@0: // A map from register numbers to rules. michael@0: class CallFrameInfo::RuleMap { michael@0: public: michael@0: RuleMap() : cfa_rule_(NULL) { } michael@0: RuleMap(const RuleMap &rhs) : cfa_rule_(NULL) { *this = rhs; } michael@0: ~RuleMap() { Clear(); } michael@0: michael@0: RuleMap &operator=(const RuleMap &rhs); michael@0: michael@0: // Set the rule for computing the CFA to RULE. Take ownership of RULE. michael@0: void SetCFARule(Rule *rule) { delete cfa_rule_; cfa_rule_ = rule; } michael@0: michael@0: // Return the current CFA rule. Unlike RegisterRule, this RuleMap retains michael@0: // ownership of the rule. We use this for DW_CFA_def_cfa_offset and michael@0: // DW_CFA_def_cfa_register, and for detecting references to the CFA before michael@0: // a rule for it has been established. michael@0: Rule *CFARule() const { return cfa_rule_; } michael@0: michael@0: // Return the rule for REG, or NULL if there is none. The caller takes michael@0: // ownership of the result. michael@0: Rule *RegisterRule(int reg) const; michael@0: michael@0: // Set the rule for computing REG to RULE. Take ownership of RULE. michael@0: void SetRegisterRule(int reg, Rule *rule); michael@0: michael@0: // Make all the appropriate calls to HANDLER as if we were changing from michael@0: // this RuleMap to NEW_RULES at ADDRESS. We use this to implement michael@0: // DW_CFA_restore_state, where lots of rules can change simultaneously. michael@0: // Return true if all handlers returned true; otherwise, return false. michael@0: bool HandleTransitionTo(Handler *handler, uint64 address, michael@0: const RuleMap &new_rules) const; michael@0: michael@0: private: michael@0: // A map from register numbers to Rules. michael@0: typedef std::map RuleByNumber; michael@0: michael@0: // Remove all register rules and clear cfa_rule_. michael@0: void Clear(); michael@0: michael@0: // The rule for computing the canonical frame address. This RuleMap owns michael@0: // this rule. michael@0: Rule *cfa_rule_; michael@0: michael@0: // A map from register numbers to postfix expressions to recover michael@0: // their values. This RuleMap owns the Rules the map refers to. michael@0: RuleByNumber registers_; michael@0: }; michael@0: michael@0: CallFrameInfo::RuleMap &CallFrameInfo::RuleMap::operator=(const RuleMap &rhs) { michael@0: Clear(); michael@0: // Since each map owns the rules it refers to, assignment must copy them. michael@0: if (rhs.cfa_rule_) cfa_rule_ = rhs.cfa_rule_->Copy(); michael@0: for (RuleByNumber::const_iterator it = rhs.registers_.begin(); michael@0: it != rhs.registers_.end(); it++) michael@0: registers_[it->first] = it->second->Copy(); michael@0: return *this; michael@0: } michael@0: michael@0: CallFrameInfo::Rule *CallFrameInfo::RuleMap::RegisterRule(int reg) const { michael@0: MOZ_ASSERT(reg != Handler::kCFARegister); michael@0: RuleByNumber::const_iterator it = registers_.find(reg); michael@0: if (it != registers_.end()) michael@0: return it->second->Copy(); michael@0: else michael@0: return NULL; michael@0: } michael@0: michael@0: void CallFrameInfo::RuleMap::SetRegisterRule(int reg, Rule *rule) { michael@0: MOZ_ASSERT(reg != Handler::kCFARegister); michael@0: MOZ_ASSERT(rule); michael@0: Rule **slot = ®isters_[reg]; michael@0: delete *slot; michael@0: *slot = rule; michael@0: } michael@0: michael@0: bool CallFrameInfo::RuleMap::HandleTransitionTo( michael@0: Handler *handler, michael@0: uint64 address, michael@0: const RuleMap &new_rules) const { michael@0: // Transition from cfa_rule_ to new_rules.cfa_rule_. michael@0: if (cfa_rule_ && new_rules.cfa_rule_) { michael@0: if (*cfa_rule_ != *new_rules.cfa_rule_ && michael@0: !new_rules.cfa_rule_->Handle(handler, address, Handler::kCFARegister)) michael@0: return false; michael@0: } else if (cfa_rule_) { michael@0: // this RuleMap has a CFA rule but new_rules doesn't. michael@0: // CallFrameInfo::Handler has no way to handle this --- and shouldn't; michael@0: // it's garbage input. The instruction interpreter should have michael@0: // detected this and warned, so take no action here. michael@0: } else if (new_rules.cfa_rule_) { michael@0: // This shouldn't be possible: NEW_RULES is some prior state, and michael@0: // there's no way to remove entries. michael@0: MOZ_ASSERT(0); michael@0: } else { michael@0: // Both CFA rules are empty. No action needed. michael@0: } michael@0: michael@0: // Traverse the two maps in order by register number, and report michael@0: // whatever differences we find. michael@0: RuleByNumber::const_iterator old_it = registers_.begin(); michael@0: RuleByNumber::const_iterator new_it = new_rules.registers_.begin(); michael@0: while (old_it != registers_.end() && new_it != new_rules.registers_.end()) { michael@0: if (old_it->first < new_it->first) { michael@0: // This RuleMap has an entry for old_it->first, but NEW_RULES michael@0: // doesn't. michael@0: // michael@0: // This isn't really the right thing to do, but since CFI generally michael@0: // only mentions callee-saves registers, and GCC's convention for michael@0: // callee-saves registers is that they are unchanged, it's a good michael@0: // approximation. michael@0: if (!handler->SameValueRule(address, old_it->first)) michael@0: return false; michael@0: old_it++; michael@0: } else if (old_it->first > new_it->first) { michael@0: // NEW_RULES has entry for new_it->first, but this RuleMap michael@0: // doesn't. This shouldn't be possible: NEW_RULES is some prior michael@0: // state, and there's no way to remove entries. michael@0: MOZ_ASSERT(0); michael@0: } else { michael@0: // Both maps have an entry for this register. Report the new michael@0: // rule if it is different. michael@0: if (*old_it->second != *new_it->second && michael@0: !new_it->second->Handle(handler, address, new_it->first)) michael@0: return false; michael@0: new_it++, old_it++; michael@0: } michael@0: } michael@0: // Finish off entries from this RuleMap with no counterparts in new_rules. michael@0: while (old_it != registers_.end()) { michael@0: if (!handler->SameValueRule(address, old_it->first)) michael@0: return false; michael@0: old_it++; michael@0: } michael@0: // Since we only make transitions from a rule set to some previously michael@0: // saved rule set, and we can only add rules to the map, NEW_RULES michael@0: // must have fewer rules than *this. michael@0: MOZ_ASSERT(new_it == new_rules.registers_.end()); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Remove all register rules and clear cfa_rule_. michael@0: void CallFrameInfo::RuleMap::Clear() { michael@0: delete cfa_rule_; michael@0: cfa_rule_ = NULL; michael@0: for (RuleByNumber::iterator it = registers_.begin(); michael@0: it != registers_.end(); it++) michael@0: delete it->second; michael@0: registers_.clear(); michael@0: } michael@0: michael@0: // The state of the call frame information interpreter as it processes michael@0: // instructions from a CIE and FDE. michael@0: class CallFrameInfo::State { michael@0: public: michael@0: // Create a call frame information interpreter state with the given michael@0: // reporter, reader, handler, and initial call frame info address. michael@0: State(ByteReader *reader, Handler *handler, Reporter *reporter, michael@0: uint64 address) michael@0: : reader_(reader), handler_(handler), reporter_(reporter), michael@0: address_(address), entry_(NULL), cursor_(NULL), michael@0: saved_rules_(NULL) { } michael@0: michael@0: ~State() { michael@0: if (saved_rules_) michael@0: delete saved_rules_; michael@0: } michael@0: michael@0: // Interpret instructions from CIE, save the resulting rule set for michael@0: // DW_CFA_restore instructions, and return true. On error, report michael@0: // the problem to reporter_ and return false. michael@0: bool InterpretCIE(const CIE &cie); michael@0: michael@0: // Interpret instructions from FDE, and return true. On error, michael@0: // report the problem to reporter_ and return false. michael@0: bool InterpretFDE(const FDE &fde); michael@0: michael@0: private: michael@0: // The operands of a CFI instruction, for ParseOperands. michael@0: struct Operands { michael@0: unsigned register_number; // A register number. michael@0: uint64 offset; // An offset or address. michael@0: long signed_offset; // A signed offset. michael@0: string expression; // A DWARF expression. michael@0: }; michael@0: michael@0: // Parse CFI instruction operands from STATE's instruction stream as michael@0: // described by FORMAT. On success, populate OPERANDS with the michael@0: // results, and return true. On failure, report the problem and michael@0: // return false. michael@0: // michael@0: // Each character of FORMAT should be one of the following: michael@0: // michael@0: // 'r' unsigned LEB128 register number (OPERANDS->register_number) michael@0: // 'o' unsigned LEB128 offset (OPERANDS->offset) michael@0: // 's' signed LEB128 offset (OPERANDS->signed_offset) michael@0: // 'a' machine-size address (OPERANDS->offset) michael@0: // (If the CIE has a 'z' augmentation string, 'a' uses the michael@0: // encoding specified by the 'R' argument.) michael@0: // '1' a one-byte offset (OPERANDS->offset) michael@0: // '2' a two-byte offset (OPERANDS->offset) michael@0: // '4' a four-byte offset (OPERANDS->offset) michael@0: // '8' an eight-byte offset (OPERANDS->offset) michael@0: // 'e' a DW_FORM_block holding a (OPERANDS->expression) michael@0: // DWARF expression michael@0: bool ParseOperands(const char *format, Operands *operands); michael@0: michael@0: // Interpret one CFI instruction from STATE's instruction stream, update michael@0: // STATE, report any rule changes to handler_, and return true. On michael@0: // failure, report the problem and return false. michael@0: bool DoInstruction(); michael@0: michael@0: // The following Do* member functions are subroutines of DoInstruction, michael@0: // factoring out the actual work of operations that have several michael@0: // different encodings. michael@0: michael@0: // Set the CFA rule to be the value of BASE_REGISTER plus OFFSET, and michael@0: // return true. On failure, report and return false. (Used for michael@0: // DW_CFA_def_cfa and DW_CFA_def_cfa_sf.) michael@0: bool DoDefCFA(unsigned base_register, long offset); michael@0: michael@0: // Change the offset of the CFA rule to OFFSET, and return true. On michael@0: // failure, report and return false. (Subroutine for michael@0: // DW_CFA_def_cfa_offset and DW_CFA_def_cfa_offset_sf.) michael@0: bool DoDefCFAOffset(long offset); michael@0: michael@0: // Specify that REG can be recovered using RULE, and return true. On michael@0: // failure, report and return false. michael@0: bool DoRule(unsigned reg, Rule *rule); michael@0: michael@0: // Specify that REG can be found at OFFSET from the CFA, and return true. michael@0: // On failure, report and return false. (Subroutine for DW_CFA_offset, michael@0: // DW_CFA_offset_extended, and DW_CFA_offset_extended_sf.) michael@0: bool DoOffset(unsigned reg, long offset); michael@0: michael@0: // Specify that the caller's value for REG is the CFA plus OFFSET, michael@0: // and return true. On failure, report and return false. (Subroutine michael@0: // for DW_CFA_val_offset and DW_CFA_val_offset_sf.) michael@0: bool DoValOffset(unsigned reg, long offset); michael@0: michael@0: // Restore REG to the rule established in the CIE, and return true. On michael@0: // failure, report and return false. (Subroutine for DW_CFA_restore and michael@0: // DW_CFA_restore_extended.) michael@0: bool DoRestore(unsigned reg); michael@0: michael@0: // Return the section offset of the instruction at cursor. For use michael@0: // in error messages. michael@0: uint64 CursorOffset() { return entry_->offset + (cursor_ - entry_->start); } michael@0: michael@0: // Report that entry_ is incomplete, and return false. For brevity. michael@0: bool ReportIncomplete() { michael@0: reporter_->Incomplete(entry_->offset, entry_->kind); michael@0: return false; michael@0: } michael@0: michael@0: // For reading multi-byte values with the appropriate endianness. michael@0: ByteReader *reader_; michael@0: michael@0: // The handler to which we should report the data we find. michael@0: Handler *handler_; michael@0: michael@0: // For reporting problems in the info we're parsing. michael@0: Reporter *reporter_; michael@0: michael@0: // The code address to which the next instruction in the stream applies. michael@0: uint64 address_; michael@0: michael@0: // The entry whose instructions we are currently processing. This is michael@0: // first a CIE, and then an FDE. michael@0: const Entry *entry_; michael@0: michael@0: // The next instruction to process. michael@0: const char *cursor_; michael@0: michael@0: // The current set of rules. michael@0: RuleMap rules_; michael@0: michael@0: // The set of rules established by the CIE, used by DW_CFA_restore michael@0: // and DW_CFA_restore_extended. We set this after interpreting the michael@0: // CIE's instructions. michael@0: RuleMap cie_rules_; michael@0: michael@0: // A stack of saved states, for DW_CFA_remember_state and michael@0: // DW_CFA_restore_state. michael@0: std::stack* saved_rules_; michael@0: }; michael@0: michael@0: bool CallFrameInfo::State::InterpretCIE(const CIE &cie) { michael@0: entry_ = &cie; michael@0: cursor_ = entry_->instructions; michael@0: while (cursor_ < entry_->end) michael@0: if (!DoInstruction()) michael@0: return false; michael@0: // Note the rules established by the CIE, for use by DW_CFA_restore michael@0: // and DW_CFA_restore_extended. michael@0: cie_rules_ = rules_; michael@0: return true; michael@0: } michael@0: michael@0: bool CallFrameInfo::State::InterpretFDE(const FDE &fde) { michael@0: entry_ = &fde; michael@0: cursor_ = entry_->instructions; michael@0: while (cursor_ < entry_->end) michael@0: if (!DoInstruction()) michael@0: return false; michael@0: return true; michael@0: } michael@0: michael@0: bool CallFrameInfo::State::ParseOperands(const char *format, michael@0: Operands *operands) { michael@0: size_t len; michael@0: const char *operand; michael@0: michael@0: for (operand = format; *operand; operand++) { michael@0: size_t bytes_left = entry_->end - cursor_; michael@0: switch (*operand) { michael@0: case 'r': michael@0: operands->register_number = reader_->ReadUnsignedLEB128(cursor_, &len); michael@0: if (len > bytes_left) return ReportIncomplete(); michael@0: cursor_ += len; michael@0: break; michael@0: michael@0: case 'o': michael@0: operands->offset = reader_->ReadUnsignedLEB128(cursor_, &len); michael@0: if (len > bytes_left) return ReportIncomplete(); michael@0: cursor_ += len; michael@0: break; michael@0: michael@0: case 's': michael@0: operands->signed_offset = reader_->ReadSignedLEB128(cursor_, &len); michael@0: if (len > bytes_left) return ReportIncomplete(); michael@0: cursor_ += len; michael@0: break; michael@0: michael@0: case 'a': michael@0: operands->offset = michael@0: reader_->ReadEncodedPointer(cursor_, entry_->cie->pointer_encoding, michael@0: &len); michael@0: if (len > bytes_left) return ReportIncomplete(); michael@0: cursor_ += len; michael@0: break; michael@0: michael@0: case '1': michael@0: if (1 > bytes_left) return ReportIncomplete(); michael@0: operands->offset = static_cast(*cursor_++); michael@0: break; michael@0: michael@0: case '2': michael@0: if (2 > bytes_left) return ReportIncomplete(); michael@0: operands->offset = reader_->ReadTwoBytes(cursor_); michael@0: cursor_ += 2; michael@0: break; michael@0: michael@0: case '4': michael@0: if (4 > bytes_left) return ReportIncomplete(); michael@0: operands->offset = reader_->ReadFourBytes(cursor_); michael@0: cursor_ += 4; michael@0: break; michael@0: michael@0: case '8': michael@0: if (8 > bytes_left) return ReportIncomplete(); michael@0: operands->offset = reader_->ReadEightBytes(cursor_); michael@0: cursor_ += 8; michael@0: break; michael@0: michael@0: case 'e': { michael@0: size_t expression_length = reader_->ReadUnsignedLEB128(cursor_, &len); michael@0: if (len > bytes_left || expression_length > bytes_left - len) michael@0: return ReportIncomplete(); michael@0: cursor_ += len; michael@0: operands->expression = string(cursor_, expression_length); michael@0: cursor_ += expression_length; michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: MOZ_ASSERT(0); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool CallFrameInfo::State::DoInstruction() { michael@0: CIE *cie = entry_->cie; michael@0: Operands ops; michael@0: michael@0: // Our entry's kind should have been set by now. michael@0: MOZ_ASSERT(entry_->kind != kUnknown); michael@0: michael@0: // We shouldn't have been invoked unless there were more michael@0: // instructions to parse. michael@0: MOZ_ASSERT(cursor_ < entry_->end); michael@0: michael@0: unsigned opcode = *cursor_++; michael@0: if ((opcode & 0xc0) != 0) { michael@0: switch (opcode & 0xc0) { michael@0: // Advance the address. michael@0: case DW_CFA_advance_loc: { michael@0: size_t code_offset = opcode & 0x3f; michael@0: address_ += code_offset * cie->code_alignment_factor; michael@0: break; michael@0: } michael@0: michael@0: // Find a register at an offset from the CFA. michael@0: case DW_CFA_offset: michael@0: if (!ParseOperands("o", &ops) || michael@0: !DoOffset(opcode & 0x3f, ops.offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // Restore the rule established for a register by the CIE. michael@0: case DW_CFA_restore: michael@0: if (!DoRestore(opcode & 0x3f)) return false; michael@0: break; michael@0: michael@0: // The 'if' above should have excluded this possibility. michael@0: default: michael@0: MOZ_ASSERT(0); michael@0: } michael@0: michael@0: // Return here, so the big switch below won't be indented. michael@0: return true; michael@0: } michael@0: michael@0: switch (opcode) { michael@0: // Set the address. michael@0: case DW_CFA_set_loc: michael@0: if (!ParseOperands("a", &ops)) return false; michael@0: address_ = ops.offset; michael@0: break; michael@0: michael@0: // Advance the address. michael@0: case DW_CFA_advance_loc1: michael@0: if (!ParseOperands("1", &ops)) return false; michael@0: address_ += ops.offset * cie->code_alignment_factor; michael@0: break; michael@0: michael@0: // Advance the address. michael@0: case DW_CFA_advance_loc2: michael@0: if (!ParseOperands("2", &ops)) return false; michael@0: address_ += ops.offset * cie->code_alignment_factor; michael@0: break; michael@0: michael@0: // Advance the address. michael@0: case DW_CFA_advance_loc4: michael@0: if (!ParseOperands("4", &ops)) return false; michael@0: address_ += ops.offset * cie->code_alignment_factor; michael@0: break; michael@0: michael@0: // Advance the address. michael@0: case DW_CFA_MIPS_advance_loc8: michael@0: if (!ParseOperands("8", &ops)) return false; michael@0: address_ += ops.offset * cie->code_alignment_factor; michael@0: break; michael@0: michael@0: // Compute the CFA by adding an offset to a register. michael@0: case DW_CFA_def_cfa: michael@0: if (!ParseOperands("ro", &ops) || michael@0: !DoDefCFA(ops.register_number, ops.offset)) michael@0: return false; michael@0: break; michael@0: michael@0: // Compute the CFA by adding an offset to a register. michael@0: case DW_CFA_def_cfa_sf: michael@0: if (!ParseOperands("rs", &ops) || michael@0: !DoDefCFA(ops.register_number, michael@0: ops.signed_offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // Change the base register used to compute the CFA. michael@0: case DW_CFA_def_cfa_register: { michael@0: Rule *cfa_rule = rules_.CFARule(); michael@0: if (!cfa_rule) { michael@0: reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); michael@0: return false; michael@0: } michael@0: if (!ParseOperands("r", &ops)) return false; michael@0: cfa_rule->SetBaseRegister(ops.register_number); michael@0: if (!cfa_rule->Handle(handler_, address_, Handler::kCFARegister)) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // Change the offset used to compute the CFA. michael@0: case DW_CFA_def_cfa_offset: michael@0: if (!ParseOperands("o", &ops) || michael@0: !DoDefCFAOffset(ops.offset)) michael@0: return false; michael@0: break; michael@0: michael@0: // Change the offset used to compute the CFA. michael@0: case DW_CFA_def_cfa_offset_sf: michael@0: if (!ParseOperands("s", &ops) || michael@0: !DoDefCFAOffset(ops.signed_offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // Specify an expression whose value is the CFA. michael@0: case DW_CFA_def_cfa_expression: { michael@0: if (!ParseOperands("e", &ops)) michael@0: return false; michael@0: Rule *rule = new ValExpressionRule(ops.expression); michael@0: rules_.SetCFARule(rule); michael@0: if (!rule->Handle(handler_, address_, Handler::kCFARegister)) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // The register's value cannot be recovered. michael@0: case DW_CFA_undefined: { michael@0: if (!ParseOperands("r", &ops) || michael@0: !DoRule(ops.register_number, new UndefinedRule())) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // The register's value is unchanged from its value in the caller. michael@0: case DW_CFA_same_value: { michael@0: if (!ParseOperands("r", &ops) || michael@0: !DoRule(ops.register_number, new SameValueRule())) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // Find a register at an offset from the CFA. michael@0: case DW_CFA_offset_extended: michael@0: if (!ParseOperands("ro", &ops) || michael@0: !DoOffset(ops.register_number, michael@0: ops.offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // The register is saved at an offset from the CFA. michael@0: case DW_CFA_offset_extended_sf: michael@0: if (!ParseOperands("rs", &ops) || michael@0: !DoOffset(ops.register_number, michael@0: ops.signed_offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // The register is saved at an offset from the CFA. michael@0: case DW_CFA_GNU_negative_offset_extended: michael@0: if (!ParseOperands("ro", &ops) || michael@0: !DoOffset(ops.register_number, michael@0: -ops.offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // The register's value is the sum of the CFA plus an offset. michael@0: case DW_CFA_val_offset: michael@0: if (!ParseOperands("ro", &ops) || michael@0: !DoValOffset(ops.register_number, michael@0: ops.offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // The register's value is the sum of the CFA plus an offset. michael@0: case DW_CFA_val_offset_sf: michael@0: if (!ParseOperands("rs", &ops) || michael@0: !DoValOffset(ops.register_number, michael@0: ops.signed_offset * cie->data_alignment_factor)) michael@0: return false; michael@0: break; michael@0: michael@0: // The register has been saved in another register. michael@0: case DW_CFA_register: { michael@0: if (!ParseOperands("ro", &ops) || michael@0: !DoRule(ops.register_number, new RegisterRule(ops.offset))) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // An expression yields the address at which the register is saved. michael@0: case DW_CFA_expression: { michael@0: if (!ParseOperands("re", &ops) || michael@0: !DoRule(ops.register_number, new ExpressionRule(ops.expression))) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // An expression yields the caller's value for the register. michael@0: case DW_CFA_val_expression: { michael@0: if (!ParseOperands("re", &ops) || michael@0: !DoRule(ops.register_number, new ValExpressionRule(ops.expression))) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // Restore the rule established for a register by the CIE. michael@0: case DW_CFA_restore_extended: michael@0: if (!ParseOperands("r", &ops) || michael@0: !DoRestore( ops.register_number)) michael@0: return false; michael@0: break; michael@0: michael@0: // Save the current set of rules on a stack. michael@0: case DW_CFA_remember_state: michael@0: if (!saved_rules_) { michael@0: saved_rules_ = new std::stack(); michael@0: } michael@0: saved_rules_->push(rules_); michael@0: break; michael@0: michael@0: // Pop the current set of rules off the stack. michael@0: case DW_CFA_restore_state: { michael@0: if (!saved_rules_ || saved_rules_->empty()) { michael@0: reporter_->EmptyStateStack(entry_->offset, entry_->kind, michael@0: CursorOffset()); michael@0: return false; michael@0: } michael@0: const RuleMap &new_rules = saved_rules_->top(); michael@0: if (rules_.CFARule() && !new_rules.CFARule()) { michael@0: reporter_->ClearingCFARule(entry_->offset, entry_->kind, michael@0: CursorOffset()); michael@0: return false; michael@0: } michael@0: rules_.HandleTransitionTo(handler_, address_, new_rules); michael@0: rules_ = new_rules; michael@0: saved_rules_->pop(); michael@0: break; michael@0: } michael@0: michael@0: // No operation. (Padding instruction.) michael@0: case DW_CFA_nop: michael@0: break; michael@0: michael@0: // A SPARC register window save: Registers 8 through 15 (%o0-%o7) michael@0: // are saved in registers 24 through 31 (%i0-%i7), and registers michael@0: // 16 through 31 (%l0-%l7 and %i0-%i7) are saved at CFA offsets michael@0: // (0-15 * the register size). The register numbers must be michael@0: // hard-coded. A GNU extension, and not a pretty one. michael@0: case DW_CFA_GNU_window_save: { michael@0: // Save %o0-%o7 in %i0-%i7. michael@0: for (int i = 8; i < 16; i++) michael@0: if (!DoRule(i, new RegisterRule(i + 16))) michael@0: return false; michael@0: // Save %l0-%l7 and %i0-%i7 at the CFA. michael@0: for (int i = 16; i < 32; i++) michael@0: // Assume that the byte reader's address size is the same as michael@0: // the architecture's register size. !@#%*^ hilarious. michael@0: if (!DoRule(i, new OffsetRule(Handler::kCFARegister, michael@0: (i - 16) * reader_->AddressSize()))) michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: // I'm not sure what this is. GDB doesn't use it for unwinding. michael@0: case DW_CFA_GNU_args_size: michael@0: if (!ParseOperands("o", &ops)) return false; michael@0: break; michael@0: michael@0: // An opcode we don't recognize. michael@0: default: { michael@0: reporter_->BadInstruction(entry_->offset, entry_->kind, CursorOffset()); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool CallFrameInfo::State::DoDefCFA(unsigned base_register, long offset) { michael@0: Rule *rule = new ValOffsetRule(base_register, offset); michael@0: rules_.SetCFARule(rule); michael@0: return rule->Handle(handler_, address_, Handler::kCFARegister); michael@0: } michael@0: michael@0: bool CallFrameInfo::State::DoDefCFAOffset(long offset) { michael@0: Rule *cfa_rule = rules_.CFARule(); michael@0: if (!cfa_rule) { michael@0: reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); michael@0: return false; michael@0: } michael@0: cfa_rule->SetOffset(offset); michael@0: return cfa_rule->Handle(handler_, address_, Handler::kCFARegister); michael@0: } michael@0: michael@0: bool CallFrameInfo::State::DoRule(unsigned reg, Rule *rule) { michael@0: rules_.SetRegisterRule(reg, rule); michael@0: return rule->Handle(handler_, address_, reg); michael@0: } michael@0: michael@0: bool CallFrameInfo::State::DoOffset(unsigned reg, long offset) { michael@0: if (!rules_.CFARule()) { michael@0: reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); michael@0: return false; michael@0: } michael@0: return DoRule(reg, michael@0: new OffsetRule(Handler::kCFARegister, offset)); michael@0: } michael@0: michael@0: bool CallFrameInfo::State::DoValOffset(unsigned reg, long offset) { michael@0: if (!rules_.CFARule()) { michael@0: reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); michael@0: return false; michael@0: } michael@0: return DoRule(reg, michael@0: new ValOffsetRule(Handler::kCFARegister, offset)); michael@0: } michael@0: michael@0: bool CallFrameInfo::State::DoRestore(unsigned reg) { michael@0: // DW_CFA_restore and DW_CFA_restore_extended don't make sense in a CIE. michael@0: if (entry_->kind == kCIE) { michael@0: reporter_->RestoreInCIE(entry_->offset, CursorOffset()); michael@0: return false; michael@0: } michael@0: Rule *rule = cie_rules_.RegisterRule(reg); michael@0: if (!rule) { michael@0: // This isn't really the right thing to do, but since CFI generally michael@0: // only mentions callee-saves registers, and GCC's convention for michael@0: // callee-saves registers is that they are unchanged, it's a good michael@0: // approximation. michael@0: rule = new SameValueRule(); michael@0: } michael@0: return DoRule(reg, rule); michael@0: } michael@0: michael@0: bool CallFrameInfo::ReadEntryPrologue(const char *cursor, Entry *entry) { michael@0: const char *buffer_end = buffer_ + buffer_length_; michael@0: michael@0: // Initialize enough of ENTRY for use in error reporting. michael@0: entry->offset = cursor - buffer_; michael@0: entry->start = cursor; michael@0: entry->kind = kUnknown; michael@0: entry->end = NULL; michael@0: michael@0: // Read the initial length. This sets reader_'s offset size. michael@0: size_t length_size; michael@0: uint64 length = reader_->ReadInitialLength(cursor, &length_size); michael@0: if (length_size > size_t(buffer_end - cursor)) michael@0: return ReportIncomplete(entry); michael@0: cursor += length_size; michael@0: michael@0: // In a .eh_frame section, a length of zero marks the end of the series michael@0: // of entries. michael@0: if (length == 0 && eh_frame_) { michael@0: entry->kind = kTerminator; michael@0: entry->end = cursor; michael@0: return true; michael@0: } michael@0: michael@0: // Validate the length. michael@0: if (length > size_t(buffer_end - cursor)) michael@0: return ReportIncomplete(entry); michael@0: michael@0: // The length is the number of bytes after the initial length field; michael@0: // we have that position handy at this point, so compute the end michael@0: // now. (If we're parsing 64-bit-offset DWARF on a 32-bit machine, michael@0: // and the length didn't fit in a size_t, we would have rejected it michael@0: // above.) michael@0: entry->end = cursor + length; michael@0: michael@0: // Parse the next field: either the offset of a CIE or a CIE id. michael@0: size_t offset_size = reader_->OffsetSize(); michael@0: if (offset_size > size_t(entry->end - cursor)) return ReportIncomplete(entry); michael@0: entry->id = reader_->ReadOffset(cursor); michael@0: michael@0: // Don't advance cursor past id field yet; in .eh_frame data we need michael@0: // the id's position to compute the section offset of an FDE's CIE. michael@0: michael@0: // Now we can decide what kind of entry this is. michael@0: if (eh_frame_) { michael@0: // In .eh_frame data, an ID of zero marks the entry as a CIE, and michael@0: // anything else is an offset from the id field of the FDE to the start michael@0: // of the CIE. michael@0: if (entry->id == 0) { michael@0: entry->kind = kCIE; michael@0: } else { michael@0: entry->kind = kFDE; michael@0: // Turn the offset from the id into an offset from the buffer's start. michael@0: entry->id = (cursor - buffer_) - entry->id; michael@0: } michael@0: } else { michael@0: // In DWARF CFI data, an ID of ~0 (of the appropriate width, given the michael@0: // offset size for the entry) marks the entry as a CIE, and anything michael@0: // else is the offset of the CIE from the beginning of the section. michael@0: if (offset_size == 4) michael@0: entry->kind = (entry->id == 0xffffffff) ? kCIE : kFDE; michael@0: else { michael@0: MOZ_ASSERT(offset_size == 8); michael@0: entry->kind = (entry->id == 0xffffffffffffffffULL) ? kCIE : kFDE; michael@0: } michael@0: } michael@0: michael@0: // Now advance cursor past the id. michael@0: cursor += offset_size; michael@0: michael@0: // The fields specific to this kind of entry start here. michael@0: entry->fields = cursor; michael@0: michael@0: entry->cie = NULL; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool CallFrameInfo::ReadCIEFields(CIE *cie) { michael@0: const char *cursor = cie->fields; michael@0: size_t len; michael@0: michael@0: MOZ_ASSERT(cie->kind == kCIE); michael@0: michael@0: // Prepare for early exit. michael@0: cie->version = 0; michael@0: cie->augmentation.clear(); michael@0: cie->code_alignment_factor = 0; michael@0: cie->data_alignment_factor = 0; michael@0: cie->return_address_register = 0; michael@0: cie->has_z_augmentation = false; michael@0: cie->pointer_encoding = DW_EH_PE_absptr; michael@0: cie->instructions = 0; michael@0: michael@0: // Parse the version number. michael@0: if (cie->end - cursor < 1) michael@0: return ReportIncomplete(cie); michael@0: cie->version = reader_->ReadOneByte(cursor); michael@0: cursor++; michael@0: michael@0: // If we don't recognize the version, we can't parse any more fields of the michael@0: // CIE. For DWARF CFI, we handle versions 1 through 3 (there was never a michael@0: // version 2 of CFI data). For .eh_frame, we handle versions 1 and 3 as well; michael@0: // the difference between those versions seems to be the same as for michael@0: // .debug_frame. michael@0: if (cie->version < 1 || cie->version > 3) { michael@0: reporter_->UnrecognizedVersion(cie->offset, cie->version); michael@0: return false; michael@0: } michael@0: michael@0: const char *augmentation_start = cursor; michael@0: const void *augmentation_end = michael@0: memchr(augmentation_start, '\0', cie->end - augmentation_start); michael@0: if (! augmentation_end) return ReportIncomplete(cie); michael@0: cursor = static_cast(augmentation_end); michael@0: cie->augmentation = string(augmentation_start, michael@0: cursor - augmentation_start); michael@0: // Skip the terminating '\0'. michael@0: cursor++; michael@0: michael@0: // Is this CFI augmented? michael@0: if (!cie->augmentation.empty()) { michael@0: // Is it an augmentation we recognize? michael@0: if (cie->augmentation[0] == DW_Z_augmentation_start) { michael@0: // Linux C++ ABI 'z' augmentation, used for exception handling data. michael@0: cie->has_z_augmentation = true; michael@0: } else { michael@0: // Not an augmentation we recognize. Augmentations can have arbitrary michael@0: // effects on the form of rest of the content, so we have to give up. michael@0: reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // Parse the code alignment factor. michael@0: cie->code_alignment_factor = reader_->ReadUnsignedLEB128(cursor, &len); michael@0: if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); michael@0: cursor += len; michael@0: michael@0: // Parse the data alignment factor. michael@0: cie->data_alignment_factor = reader_->ReadSignedLEB128(cursor, &len); michael@0: if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); michael@0: cursor += len; michael@0: michael@0: // Parse the return address register. This is a ubyte in version 1, and michael@0: // a ULEB128 in version 3. michael@0: if (cie->version == 1) { michael@0: if (cursor >= cie->end) return ReportIncomplete(cie); michael@0: cie->return_address_register = uint8(*cursor++); michael@0: } else { michael@0: cie->return_address_register = reader_->ReadUnsignedLEB128(cursor, &len); michael@0: if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); michael@0: cursor += len; michael@0: } michael@0: michael@0: // If we have a 'z' augmentation string, find the augmentation data and michael@0: // use the augmentation string to parse it. michael@0: if (cie->has_z_augmentation) { michael@0: uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &len); michael@0: if (size_t(cie->end - cursor) < len + data_size) michael@0: return ReportIncomplete(cie); michael@0: cursor += len; michael@0: const char *data = cursor; michael@0: cursor += data_size; michael@0: const char *data_end = cursor; michael@0: michael@0: cie->has_z_lsda = false; michael@0: cie->has_z_personality = false; michael@0: cie->has_z_signal_frame = false; michael@0: michael@0: // Walk the augmentation string, and extract values from the michael@0: // augmentation data as the string directs. michael@0: for (size_t i = 1; i < cie->augmentation.size(); i++) { michael@0: switch (cie->augmentation[i]) { michael@0: case DW_Z_has_LSDA: michael@0: // The CIE's augmentation data holds the language-specific data michael@0: // area pointer's encoding, and the FDE's augmentation data holds michael@0: // the pointer itself. michael@0: cie->has_z_lsda = true; michael@0: // Fetch the LSDA encoding from the augmentation data. michael@0: if (data >= data_end) return ReportIncomplete(cie); michael@0: cie->lsda_encoding = DwarfPointerEncoding(*data++); michael@0: if (!reader_->ValidEncoding(cie->lsda_encoding)) { michael@0: reporter_->InvalidPointerEncoding(cie->offset, cie->lsda_encoding); michael@0: return false; michael@0: } michael@0: // Don't check if the encoding is usable here --- we haven't michael@0: // read the FDE's fields yet, so we're not prepared for michael@0: // DW_EH_PE_funcrel, although that's a fine encoding for the michael@0: // LSDA to use, since it appears in the FDE. michael@0: break; michael@0: michael@0: case DW_Z_has_personality_routine: michael@0: // The CIE's augmentation data holds the personality routine michael@0: // pointer's encoding, followed by the pointer itself. michael@0: cie->has_z_personality = true; michael@0: // Fetch the personality routine pointer's encoding from the michael@0: // augmentation data. michael@0: if (data >= data_end) return ReportIncomplete(cie); michael@0: cie->personality_encoding = DwarfPointerEncoding(*data++); michael@0: if (!reader_->ValidEncoding(cie->personality_encoding)) { michael@0: reporter_->InvalidPointerEncoding(cie->offset, michael@0: cie->personality_encoding); michael@0: return false; michael@0: } michael@0: if (!reader_->UsableEncoding(cie->personality_encoding)) { michael@0: reporter_->UnusablePointerEncoding(cie->offset, michael@0: cie->personality_encoding); michael@0: return false; michael@0: } michael@0: // Fetch the personality routine's pointer itself from the data. michael@0: cie->personality_address = michael@0: reader_->ReadEncodedPointer(data, cie->personality_encoding, michael@0: &len); michael@0: if (len > size_t(data_end - data)) michael@0: return ReportIncomplete(cie); michael@0: data += len; michael@0: break; michael@0: michael@0: case DW_Z_has_FDE_address_encoding: michael@0: // The CIE's augmentation data holds the pointer encoding to use michael@0: // for addresses in the FDE. michael@0: if (data >= data_end) return ReportIncomplete(cie); michael@0: cie->pointer_encoding = DwarfPointerEncoding(*data++); michael@0: if (!reader_->ValidEncoding(cie->pointer_encoding)) { michael@0: reporter_->InvalidPointerEncoding(cie->offset, michael@0: cie->pointer_encoding); michael@0: return false; michael@0: } michael@0: if (!reader_->UsableEncoding(cie->pointer_encoding)) { michael@0: reporter_->UnusablePointerEncoding(cie->offset, michael@0: cie->pointer_encoding); michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case DW_Z_is_signal_trampoline: michael@0: // Frames using this CIE are signal delivery frames. michael@0: cie->has_z_signal_frame = true; michael@0: break; michael@0: michael@0: default: michael@0: // An augmentation we don't recognize. michael@0: reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // The CIE's instructions start here. michael@0: cie->instructions = cursor; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool CallFrameInfo::ReadFDEFields(FDE *fde) { michael@0: const char *cursor = fde->fields; michael@0: size_t size; michael@0: michael@0: fde->address = reader_->ReadEncodedPointer(cursor, fde->cie->pointer_encoding, michael@0: &size); michael@0: if (size > size_t(fde->end - cursor)) michael@0: return ReportIncomplete(fde); michael@0: cursor += size; michael@0: reader_->SetFunctionBase(fde->address); michael@0: michael@0: // For the length, we strip off the upper nybble of the encoding used for michael@0: // the starting address. michael@0: DwarfPointerEncoding length_encoding = michael@0: DwarfPointerEncoding(fde->cie->pointer_encoding & 0x0f); michael@0: fde->size = reader_->ReadEncodedPointer(cursor, length_encoding, &size); michael@0: if (size > size_t(fde->end - cursor)) michael@0: return ReportIncomplete(fde); michael@0: cursor += size; michael@0: michael@0: // If the CIE has a 'z' augmentation string, then augmentation data michael@0: // appears here. michael@0: if (fde->cie->has_z_augmentation) { michael@0: uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &size); michael@0: if (size_t(fde->end - cursor) < size + data_size) michael@0: return ReportIncomplete(fde); michael@0: cursor += size; michael@0: michael@0: // In the abstract, we should walk the augmentation string, and extract michael@0: // items from the FDE's augmentation data as we encounter augmentation michael@0: // string characters that specify their presence: the ordering of items michael@0: // in the augmentation string determines the arrangement of values in michael@0: // the augmentation data. michael@0: // michael@0: // In practice, there's only ever one value in FDE augmentation data michael@0: // that we support --- the LSDA pointer --- and we have to bail if we michael@0: // see any unrecognized augmentation string characters. So if there is michael@0: // anything here at all, we know what it is, and where it starts. michael@0: if (fde->cie->has_z_lsda) { michael@0: // Check whether the LSDA's pointer encoding is usable now: only once michael@0: // we've parsed the FDE's starting address do we call reader_-> michael@0: // SetFunctionBase, so that the DW_EH_PE_funcrel encoding becomes michael@0: // usable. michael@0: if (!reader_->UsableEncoding(fde->cie->lsda_encoding)) { michael@0: reporter_->UnusablePointerEncoding(fde->cie->offset, michael@0: fde->cie->lsda_encoding); michael@0: return false; michael@0: } michael@0: michael@0: fde->lsda_address = michael@0: reader_->ReadEncodedPointer(cursor, fde->cie->lsda_encoding, &size); michael@0: if (size > data_size) michael@0: return ReportIncomplete(fde); michael@0: // Ideally, we would also complain here if there were unconsumed michael@0: // augmentation data. michael@0: } michael@0: michael@0: cursor += data_size; michael@0: } michael@0: michael@0: // The FDE's instructions start after those. michael@0: fde->instructions = cursor; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool CallFrameInfo::Start() { michael@0: const char *buffer_end = buffer_ + buffer_length_; michael@0: const char *cursor; michael@0: bool all_ok = true; michael@0: const char *entry_end; michael@0: bool ok; michael@0: michael@0: // Traverse all the entries in buffer_, skipping CIEs and offering michael@0: // FDEs to the handler. michael@0: for (cursor = buffer_; cursor < buffer_end; michael@0: cursor = entry_end, all_ok = all_ok && ok) { michael@0: FDE fde; michael@0: michael@0: // Make it easy to skip this entry with 'continue': assume that michael@0: // things are not okay until we've checked all the data, and michael@0: // prepare the address of the next entry. michael@0: ok = false; michael@0: michael@0: // Read the entry's prologue. michael@0: if (!ReadEntryPrologue(cursor, &fde)) { michael@0: if (!fde.end) { michael@0: // If we couldn't even figure out this entry's extent, then we michael@0: // must stop processing entries altogether. michael@0: all_ok = false; michael@0: break; michael@0: } michael@0: entry_end = fde.end; michael@0: continue; michael@0: } michael@0: michael@0: // The next iteration picks up after this entry. michael@0: entry_end = fde.end; michael@0: michael@0: // Did we see an .eh_frame terminating mark? michael@0: if (fde.kind == kTerminator) { michael@0: // If there appears to be more data left in the section after the michael@0: // terminating mark, warn the user. But this is just a warning; michael@0: // we leave all_ok true. michael@0: if (fde.end < buffer_end) reporter_->EarlyEHTerminator(fde.offset); michael@0: break; michael@0: } michael@0: michael@0: // In this loop, we skip CIEs. We only parse them fully when we michael@0: // parse an FDE that refers to them. This limits our memory michael@0: // consumption (beyond the buffer itself) to that needed to michael@0: // process the largest single entry. michael@0: if (fde.kind != kFDE) { michael@0: ok = true; michael@0: continue; michael@0: } michael@0: michael@0: // Validate the CIE pointer. michael@0: if (fde.id > buffer_length_) { michael@0: reporter_->CIEPointerOutOfRange(fde.offset, fde.id); michael@0: continue; michael@0: } michael@0: michael@0: CIE cie; michael@0: michael@0: // Parse this FDE's CIE header. michael@0: if (!ReadEntryPrologue(buffer_ + fde.id, &cie)) michael@0: continue; michael@0: // This had better be an actual CIE. michael@0: if (cie.kind != kCIE) { michael@0: reporter_->BadCIEId(fde.offset, fde.id); michael@0: continue; michael@0: } michael@0: if (!ReadCIEFields(&cie)) michael@0: continue; michael@0: michael@0: // We now have the values that govern both the CIE and the FDE. michael@0: cie.cie = &cie; michael@0: fde.cie = &cie; michael@0: michael@0: // Parse the FDE's header. michael@0: if (!ReadFDEFields(&fde)) michael@0: continue; michael@0: michael@0: // Call Entry to ask the consumer if they're interested. michael@0: if (!handler_->Entry(fde.offset, fde.address, fde.size, michael@0: cie.version, cie.augmentation, michael@0: cie.return_address_register)) { michael@0: // The handler isn't interested in this entry. That's not an error. michael@0: ok = true; michael@0: continue; michael@0: } michael@0: michael@0: if (cie.has_z_augmentation) { michael@0: // Report the personality routine address, if we have one. michael@0: if (cie.has_z_personality) { michael@0: if (!handler_ michael@0: ->PersonalityRoutine(cie.personality_address, michael@0: IsIndirectEncoding(cie.personality_encoding))) michael@0: continue; michael@0: } michael@0: michael@0: // Report the language-specific data area address, if we have one. michael@0: if (cie.has_z_lsda) { michael@0: if (!handler_ michael@0: ->LanguageSpecificDataArea(fde.lsda_address, michael@0: IsIndirectEncoding(cie.lsda_encoding))) michael@0: continue; michael@0: } michael@0: michael@0: // If this is a signal-handling frame, report that. michael@0: if (cie.has_z_signal_frame) { michael@0: if (!handler_->SignalHandler()) michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: // Interpret the CIE's instructions, and then the FDE's instructions. michael@0: State state(reader_, handler_, reporter_, fde.address); michael@0: ok = state.InterpretCIE(cie) && state.InterpretFDE(fde); michael@0: michael@0: // Tell the ByteReader that the function start address from the michael@0: // FDE header is no longer valid. michael@0: reader_->ClearFunctionBase(); michael@0: michael@0: // Report the end of the entry. michael@0: handler_->End(); michael@0: } michael@0: michael@0: return all_ok; michael@0: } michael@0: michael@0: const char *CallFrameInfo::KindName(EntryKind kind) { michael@0: if (kind == CallFrameInfo::kUnknown) michael@0: return "entry"; michael@0: else if (kind == CallFrameInfo::kCIE) michael@0: return "common information entry"; michael@0: else if (kind == CallFrameInfo::kFDE) michael@0: return "frame description entry"; michael@0: else { michael@0: MOZ_ASSERT (kind == CallFrameInfo::kTerminator); michael@0: return ".eh_frame sequence terminator"; michael@0: } michael@0: } michael@0: michael@0: bool CallFrameInfo::ReportIncomplete(Entry *entry) { michael@0: reporter_->Incomplete(entry->offset, entry->kind); michael@0: return false; michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::Incomplete(uint64 offset, michael@0: CallFrameInfo::EntryKind kind) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI %s at offset 0x%llx in '%s': entry ends early\n", michael@0: filename_.c_str(), CallFrameInfo::KindName(kind), offset, michael@0: section_.c_str()); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::EarlyEHTerminator(uint64 offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI at offset 0x%llx in '%s': saw end-of-data marker" michael@0: " before end of section contents\n", michael@0: filename_.c_str(), offset, section_.c_str()); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::CIEPointerOutOfRange(uint64 offset, michael@0: uint64 cie_offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI frame description entry at offset 0x%llx in '%s':" michael@0: " CIE pointer is out of range: 0x%llx\n", michael@0: filename_.c_str(), offset, section_.c_str(), cie_offset); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::BadCIEId(uint64 offset, uint64 cie_offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI frame description entry at offset 0x%llx in '%s':" michael@0: " CIE pointer does not point to a CIE: 0x%llx\n", michael@0: filename_.c_str(), offset, section_.c_str(), cie_offset); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::UnrecognizedVersion(uint64 offset, int version) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI frame description entry at offset 0x%llx in '%s':" michael@0: " CIE specifies unrecognized version: %d\n", michael@0: filename_.c_str(), offset, section_.c_str(), version); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::UnrecognizedAugmentation(uint64 offset, michael@0: const string &aug) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI frame description entry at offset 0x%llx in '%s':" michael@0: " CIE specifies unrecognized augmentation: '%s'\n", michael@0: filename_.c_str(), offset, section_.c_str(), aug.c_str()); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::InvalidPointerEncoding(uint64 offset, michael@0: uint8 encoding) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI common information entry at offset 0x%llx in '%s':" michael@0: " 'z' augmentation specifies invalid pointer encoding: 0x%02x\n", michael@0: filename_.c_str(), offset, section_.c_str(), encoding); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::UnusablePointerEncoding(uint64 offset, michael@0: uint8 encoding) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI common information entry at offset 0x%llx in '%s':" michael@0: " 'z' augmentation specifies a pointer encoding for which" michael@0: " we have no base address: 0x%02x\n", michael@0: filename_.c_str(), offset, section_.c_str(), encoding); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::RestoreInCIE(uint64 offset, uint64 insn_offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI common information entry at offset 0x%llx in '%s':" michael@0: " the DW_CFA_restore instruction at offset 0x%llx" michael@0: " cannot be used in a common information entry\n", michael@0: filename_.c_str(), offset, section_.c_str(), insn_offset); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::BadInstruction(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI %s at offset 0x%llx in section '%s':" michael@0: " the instruction at offset 0x%llx is unrecognized\n", michael@0: filename_.c_str(), CallFrameInfo::KindName(kind), michael@0: offset, section_.c_str(), insn_offset); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::NoCFARule(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI %s at offset 0x%llx in section '%s':" michael@0: " the instruction at offset 0x%llx assumes that a CFA rule has" michael@0: " been set, but none has been set\n", michael@0: filename_.c_str(), CallFrameInfo::KindName(kind), offset, michael@0: section_.c_str(), insn_offset); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::EmptyStateStack(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI %s at offset 0x%llx in section '%s':" michael@0: " the DW_CFA_restore_state instruction at offset 0x%llx" michael@0: " should pop a saved state from the stack, but the stack is empty\n", michael@0: filename_.c_str(), CallFrameInfo::KindName(kind), offset, michael@0: section_.c_str(), insn_offset); michael@0: log_(buf); michael@0: } michael@0: michael@0: void CallFrameInfo::Reporter::ClearingCFARule(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "%s: CFI %s at offset 0x%llx in section '%s':" michael@0: " the DW_CFA_restore_state instruction at offset 0x%llx" michael@0: " would clear the CFA rule in effect\n", michael@0: filename_.c_str(), CallFrameInfo::KindName(kind), offset, michael@0: section_.c_str(), insn_offset); michael@0: log_(buf); michael@0: } michael@0: michael@0: michael@0: const unsigned int DwarfCFIToModule::RegisterNames::I386() { michael@0: /* michael@0: 8 "$eax", "$ecx", "$edx", "$ebx", "$esp", "$ebp", "$esi", "$edi", michael@0: 3 "$eip", "$eflags", "$unused1", michael@0: 8 "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7", michael@0: 2 "$unused2", "$unused3", michael@0: 8 "$xmm0", "$xmm1", "$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7", michael@0: 8 "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7", michael@0: 3 "$fcw", "$fsw", "$mxcsr", michael@0: 8 "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused4", "$unused5", michael@0: 2 "$tr", "$ldtr" michael@0: */ michael@0: return 8 + 3 + 8 + 2 + 8 + 8 + 3 + 8 + 2; michael@0: } michael@0: michael@0: const unsigned int DwarfCFIToModule::RegisterNames::X86_64() { michael@0: /* michael@0: 8 "$rax", "$rdx", "$rcx", "$rbx", "$rsi", "$rdi", "$rbp", "$rsp", michael@0: 8 "$r8", "$r9", "$r10", "$r11", "$r12", "$r13", "$r14", "$r15", michael@0: 1 "$rip", michael@0: 8 "$xmm0","$xmm1","$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7", michael@0: 8 "$xmm8","$xmm9","$xmm10","$xmm11","$xmm12","$xmm13","$xmm14","$xmm15", michael@0: 8 "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7", michael@0: 8 "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7", michael@0: 1 "$rflags", michael@0: 8 "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused1", "$unused2", michael@0: 4 "$fs.base", "$gs.base", "$unused3", "$unused4", michael@0: 2 "$tr", "$ldtr", michael@0: 3 "$mxcsr", "$fcw", "$fsw" michael@0: */ michael@0: return 8 + 8 + 1 + 8 + 8 + 8 + 8 + 1 + 8 + 4 + 2 + 3; michael@0: } michael@0: michael@0: // Per ARM IHI 0040A, section 3.1 michael@0: const unsigned int DwarfCFIToModule::RegisterNames::ARM() { michael@0: /* michael@0: 8 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", michael@0: 8 "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc", michael@0: 8 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", michael@0: 8 "fps", "cpsr", "", "", "", "", "", "", michael@0: 8 "", "", "", "", "", "", "", "", michael@0: 8 "", "", "", "", "", "", "", "", michael@0: 8 "", "", "", "", "", "", "", "", michael@0: 8 "", "", "", "", "", "", "", "", michael@0: 8 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", michael@0: 8 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15", michael@0: 8 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23", michael@0: 8 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31", michael@0: 8 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7" michael@0: */ michael@0: return 13 * 8; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::Entry(size_t offset, uint64 address, uint64 length, michael@0: uint8 version, const string &augmentation, michael@0: unsigned return_address) { michael@0: if (DEBUG_DWARF) michael@0: printf("LUL.DW DwarfCFIToModule::Entry 0x%llx,+%lld\n", address, length); michael@0: michael@0: summ_->Entry(address, length); michael@0: michael@0: // If dwarf2reader::CallFrameInfo can handle this version and michael@0: // augmentation, then we should be okay with that, so there's no michael@0: // need to check them here. michael@0: michael@0: // Get ready to collect entries. michael@0: return_address_ = return_address; michael@0: michael@0: // Breakpad STACK CFI records must provide a .ra rule, but DWARF CFI michael@0: // may not establish any rule for .ra if the return address column michael@0: // is an ordinary register, and that register holds the return michael@0: // address on entry to the function. So establish an initial .ra michael@0: // rule citing the return address register. michael@0: if (return_address_ < num_dw_regs_) { michael@0: summ_->Rule(address, return_address_, return_address, 0, false); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: const UniqueString* DwarfCFIToModule::RegisterName(int i) { michael@0: if (i < 0) { michael@0: MOZ_ASSERT(i == kCFARegister); michael@0: return ustr__ZDcfa(); michael@0: } michael@0: unsigned reg = i; michael@0: if (reg == return_address_) michael@0: return ustr__ZDra(); michael@0: michael@0: char buf[30]; michael@0: sprintf(buf, "dwarf_reg_%u", reg); michael@0: return ToUniqueString(buf); michael@0: } michael@0: michael@0: bool DwarfCFIToModule::UndefinedRule(uint64 address, int reg) { michael@0: reporter_->UndefinedNotSupported(entry_offset_, RegisterName(reg)); michael@0: // Treat this as a non-fatal error. michael@0: return true; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::SameValueRule(uint64 address, int reg) { michael@0: if (DEBUG_DWARF) michael@0: printf("LUL.DW 0x%llx: old r%d = Same\n", address, reg); michael@0: // reg + 0 michael@0: summ_->Rule(address, reg, reg, 0, false); michael@0: return true; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::OffsetRule(uint64 address, int reg, michael@0: int base_register, long offset) { michael@0: if (DEBUG_DWARF) michael@0: printf("LUL.DW 0x%llx: old r%d = *(r%d + %ld)\n", michael@0: address, reg, base_register, offset); michael@0: // *(base_register + offset) michael@0: summ_->Rule(address, reg, base_register, offset, true); michael@0: return true; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::ValOffsetRule(uint64 address, int reg, michael@0: int base_register, long offset) { michael@0: if (DEBUG_DWARF) michael@0: printf("LUL.DW 0x%llx: old r%d = r%d + %ld\n", michael@0: address, reg, base_register, offset); michael@0: // base_register + offset michael@0: summ_->Rule(address, reg, base_register, offset, false); michael@0: return true; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::RegisterRule(uint64 address, int reg, michael@0: int base_register) { michael@0: if (DEBUG_DWARF) michael@0: printf("LUL.DW 0x%llx: old r%d = r%d\n", address, reg, base_register); michael@0: // base_register + 0 michael@0: summ_->Rule(address, reg, base_register, 0, false); michael@0: return true; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::ExpressionRule(uint64 address, int reg, michael@0: const string &expression) { michael@0: reporter_->ExpressionsNotSupported(entry_offset_, RegisterName(reg)); michael@0: // Treat this as a non-fatal error. michael@0: return true; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::ValExpressionRule(uint64 address, int reg, michael@0: const string &expression) { michael@0: reporter_->ExpressionsNotSupported(entry_offset_, RegisterName(reg)); michael@0: // Treat this as a non-fatal error. michael@0: return true; michael@0: } michael@0: michael@0: bool DwarfCFIToModule::End() { michael@0: //module_->AddStackFrameEntry(entry_); michael@0: if (DEBUG_DWARF) michael@0: printf("LUL.DW DwarfCFIToModule::End()\n"); michael@0: summ_->End(); michael@0: return true; michael@0: } michael@0: michael@0: void DwarfCFIToModule::Reporter::UndefinedNotSupported( michael@0: size_t offset, michael@0: const UniqueString* reg) { michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "DwarfCFIToModule::Reporter::UndefinedNotSupported()\n"); michael@0: log_(buf); michael@0: //BPLOG(INFO) << file_ << ", section '" << section_ michael@0: // << "': the call frame entry at offset 0x" michael@0: // << std::setbase(16) << offset << std::setbase(10) michael@0: // << " sets the rule for register '" << FromUniqueString(reg) michael@0: // << "' to 'undefined', but the Breakpad symbol file format cannot " michael@0: // << " express this"; michael@0: } michael@0: michael@0: // FIXME: move this somewhere sensible michael@0: static bool is_power_of_2(uint64_t n) michael@0: { michael@0: int i, nSetBits = 0; michael@0: for (i = 0; i < 8*(int)sizeof(n); i++) { michael@0: if ((n & ((uint64_t)1) << i) != 0) michael@0: nSetBits++; michael@0: } michael@0: return nSetBits <= 1; michael@0: } michael@0: michael@0: void DwarfCFIToModule::Reporter::ExpressionsNotSupported( michael@0: size_t offset, michael@0: const UniqueString* reg) { michael@0: static uint64_t n_complaints = 0; // This isn't threadsafe michael@0: n_complaints++; michael@0: if (!is_power_of_2(n_complaints)) michael@0: return; michael@0: char buf[300]; michael@0: snprintf(buf, sizeof(buf), michael@0: "DwarfCFIToModule::Reporter::" michael@0: "ExpressionsNotSupported(shown %llu times)\n", michael@0: (unsigned long long int)n_complaints); michael@0: log_(buf); michael@0: //BPLOG(INFO) << file_ << ", section '" << section_ michael@0: // << "': the call frame entry at offset 0x" michael@0: // << std::setbase(16) << offset << std::setbase(10) michael@0: // << " uses a DWARF expression to describe how to recover register '" michael@0: // << FromUniqueString(reg) << "', but this translator cannot yet " michael@0: // << "translate DWARF expressions to Breakpad postfix expressions (shown " michael@0: // << n_complaints << " times)"; michael@0: } michael@0: michael@0: } // namespace lul