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: michael@0: // Implementation of dwarf2reader::LineInfo, dwarf2reader::CompilationUnit, michael@0: // and dwarf2reader::CallFrameInfo. See dwarf2reader.h for details. michael@0: michael@0: #include "common/dwarf/dwarf2reader.h" 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: #include michael@0: #include michael@0: michael@0: #include "common/dwarf/bytereader-inl.h" michael@0: #include "common/dwarf/bytereader.h" michael@0: #include "common/dwarf/line_state_machine.h" michael@0: #include "common/using_std_string.h" michael@0: michael@0: namespace dwarf2reader { michael@0: michael@0: CompilationUnit::CompilationUnit(const SectionMap& sections, uint64 offset, michael@0: ByteReader* reader, Dwarf2Handler* handler) michael@0: : offset_from_section_start_(offset), reader_(reader), michael@0: sections_(sections), handler_(handler), abbrevs_(NULL), michael@0: string_buffer_(NULL), string_buffer_length_(0) {} michael@0: michael@0: // Read a DWARF2/3 abbreviation section. michael@0: // Each abbrev consists of a abbreviation number, a tag, a byte michael@0: // specifying whether the tag has children, and a list of michael@0: // attribute/form pairs. michael@0: // The list of forms is terminated by a 0 for the attribute, and a michael@0: // zero for the form. The entire abbreviation section is terminated michael@0: // by a zero for the code. michael@0: michael@0: void CompilationUnit::ReadAbbrevs() { michael@0: if (abbrevs_) michael@0: return; michael@0: michael@0: // First get the debug_abbrev section. ".debug_abbrev" is the name michael@0: // recommended in the DWARF spec, and used on Linux; michael@0: // "__debug_abbrev" is the name used in Mac OS X Mach-O files. michael@0: SectionMap::const_iterator iter = sections_.find(".debug_abbrev"); michael@0: if (iter == sections_.end()) michael@0: iter = sections_.find("__debug_abbrev"); michael@0: assert(iter != sections_.end()); michael@0: michael@0: abbrevs_ = new std::vector; michael@0: abbrevs_->resize(1); michael@0: michael@0: // The only way to check whether we are reading over the end of the michael@0: // buffer would be to first compute the size of the leb128 data by michael@0: // reading it, then go back and read it again. michael@0: const char* abbrev_start = iter->second.first + michael@0: header_.abbrev_offset; michael@0: const char* abbrevptr = abbrev_start; michael@0: #ifndef NDEBUG michael@0: const uint64 abbrev_length = iter->second.second - header_.abbrev_offset; michael@0: #endif michael@0: michael@0: while (1) { michael@0: CompilationUnit::Abbrev abbrev; michael@0: size_t len; michael@0: const uint64 number = reader_->ReadUnsignedLEB128(abbrevptr, &len); michael@0: michael@0: if (number == 0) michael@0: break; michael@0: abbrev.number = number; michael@0: abbrevptr += len; michael@0: michael@0: assert(abbrevptr < abbrev_start + abbrev_length); michael@0: const uint64 tag = reader_->ReadUnsignedLEB128(abbrevptr, &len); michael@0: abbrevptr += len; michael@0: abbrev.tag = static_cast(tag); michael@0: michael@0: assert(abbrevptr < abbrev_start + abbrev_length); michael@0: abbrev.has_children = reader_->ReadOneByte(abbrevptr); michael@0: abbrevptr += 1; michael@0: michael@0: assert(abbrevptr < abbrev_start + abbrev_length); michael@0: michael@0: while (1) { michael@0: const uint64 nametemp = reader_->ReadUnsignedLEB128(abbrevptr, &len); michael@0: abbrevptr += len; michael@0: michael@0: assert(abbrevptr < abbrev_start + abbrev_length); michael@0: const uint64 formtemp = reader_->ReadUnsignedLEB128(abbrevptr, &len); michael@0: abbrevptr += len; michael@0: if (nametemp == 0 && formtemp == 0) michael@0: break; michael@0: michael@0: const enum DwarfAttribute name = michael@0: static_cast(nametemp); michael@0: const enum DwarfForm form = static_cast(formtemp); michael@0: abbrev.attributes.push_back(std::make_pair(name, form)); michael@0: } michael@0: assert(abbrev.number == abbrevs_->size()); michael@0: abbrevs_->push_back(abbrev); michael@0: } michael@0: } michael@0: michael@0: // Skips a single DIE's attributes. michael@0: const char* CompilationUnit::SkipDIE(const char* start, michael@0: const Abbrev& abbrev) { michael@0: for (AttributeList::const_iterator i = abbrev.attributes.begin(); michael@0: i != abbrev.attributes.end(); michael@0: i++) { michael@0: start = SkipAttribute(start, i->second); michael@0: } michael@0: return start; michael@0: } michael@0: michael@0: // Skips a single attribute form's data. michael@0: const char* CompilationUnit::SkipAttribute(const char* start, michael@0: enum DwarfForm form) { michael@0: size_t len; michael@0: michael@0: switch (form) { michael@0: case DW_FORM_indirect: michael@0: form = static_cast(reader_->ReadUnsignedLEB128(start, michael@0: &len)); michael@0: start += len; michael@0: return SkipAttribute(start, form); michael@0: michael@0: case DW_FORM_flag_present: michael@0: return start; michael@0: case DW_FORM_data1: michael@0: case DW_FORM_flag: michael@0: case DW_FORM_ref1: michael@0: return start + 1; michael@0: case DW_FORM_ref2: michael@0: case DW_FORM_data2: michael@0: return start + 2; michael@0: case DW_FORM_ref4: michael@0: case DW_FORM_data4: michael@0: return start + 4; michael@0: case DW_FORM_ref8: michael@0: case DW_FORM_data8: michael@0: case DW_FORM_ref_sig8: michael@0: return start + 8; michael@0: case DW_FORM_string: michael@0: return start + strlen(start) + 1; michael@0: case DW_FORM_udata: michael@0: case DW_FORM_ref_udata: michael@0: reader_->ReadUnsignedLEB128(start, &len); michael@0: return start + len; michael@0: michael@0: case DW_FORM_sdata: michael@0: reader_->ReadSignedLEB128(start, &len); michael@0: return start + len; michael@0: case DW_FORM_addr: michael@0: return start + reader_->AddressSize(); michael@0: case DW_FORM_ref_addr: michael@0: // DWARF2 and 3 differ on whether ref_addr is address size or michael@0: // offset size. michael@0: assert(header_.version == 2 || header_.version == 3); michael@0: if (header_.version == 2) { michael@0: return start + reader_->AddressSize(); michael@0: } else if (header_.version == 3) { michael@0: return start + reader_->OffsetSize(); michael@0: } michael@0: michael@0: case DW_FORM_block1: michael@0: return start + 1 + reader_->ReadOneByte(start); michael@0: case DW_FORM_block2: michael@0: return start + 2 + reader_->ReadTwoBytes(start); michael@0: case DW_FORM_block4: michael@0: return start + 4 + reader_->ReadFourBytes(start); michael@0: case DW_FORM_block: michael@0: case DW_FORM_exprloc: { michael@0: uint64 size = reader_->ReadUnsignedLEB128(start, &len); michael@0: return start + size + len; michael@0: } michael@0: case DW_FORM_strp: michael@0: case DW_FORM_sec_offset: michael@0: return start + reader_->OffsetSize(); michael@0: } michael@0: fprintf(stderr,"Unhandled form type"); michael@0: return NULL; michael@0: } michael@0: michael@0: // Read a DWARF2/3 header. michael@0: // The header is variable length in DWARF3 (and DWARF2 as extended by michael@0: // most compilers), and consists of an length field, a version number, michael@0: // the offset in the .debug_abbrev section for our abbrevs, and an michael@0: // address size. michael@0: void CompilationUnit::ReadHeader() { michael@0: const char* headerptr = buffer_; michael@0: size_t initial_length_size; michael@0: michael@0: assert(headerptr + 4 < buffer_ + buffer_length_); michael@0: const uint64 initial_length michael@0: = reader_->ReadInitialLength(headerptr, &initial_length_size); michael@0: headerptr += initial_length_size; michael@0: header_.length = initial_length; michael@0: michael@0: assert(headerptr + 2 < buffer_ + buffer_length_); michael@0: header_.version = reader_->ReadTwoBytes(headerptr); michael@0: headerptr += 2; michael@0: michael@0: assert(headerptr + reader_->OffsetSize() < buffer_ + buffer_length_); michael@0: header_.abbrev_offset = reader_->ReadOffset(headerptr); michael@0: headerptr += reader_->OffsetSize(); michael@0: michael@0: assert(headerptr + 1 < buffer_ + buffer_length_); michael@0: header_.address_size = reader_->ReadOneByte(headerptr); michael@0: reader_->SetAddressSize(header_.address_size); michael@0: headerptr += 1; michael@0: michael@0: after_header_ = headerptr; michael@0: michael@0: // This check ensures that we don't have to do checking during the michael@0: // reading of DIEs. header_.length does not include the size of the michael@0: // initial length. michael@0: assert(buffer_ + initial_length_size + header_.length <= michael@0: buffer_ + buffer_length_); michael@0: } michael@0: michael@0: uint64 CompilationUnit::Start() { michael@0: // First get the debug_info section. ".debug_info" is the name michael@0: // recommended in the DWARF spec, and used on Linux; "__debug_info" michael@0: // is the name used in Mac OS X Mach-O files. michael@0: SectionMap::const_iterator iter = sections_.find(".debug_info"); michael@0: if (iter == sections_.end()) michael@0: iter = sections_.find("__debug_info"); michael@0: assert(iter != sections_.end()); michael@0: michael@0: // Set up our buffer michael@0: buffer_ = iter->second.first + offset_from_section_start_; michael@0: buffer_length_ = iter->second.second - offset_from_section_start_; michael@0: michael@0: // Read the header michael@0: ReadHeader(); michael@0: michael@0: // Figure out the real length from the end of the initial length to michael@0: // the end of the compilation unit, since that is the value we michael@0: // return. michael@0: uint64 ourlength = header_.length; michael@0: if (reader_->OffsetSize() == 8) michael@0: ourlength += 12; michael@0: else michael@0: ourlength += 4; michael@0: michael@0: // See if the user wants this compilation unit, and if not, just return. michael@0: if (!handler_->StartCompilationUnit(offset_from_section_start_, michael@0: reader_->AddressSize(), michael@0: reader_->OffsetSize(), michael@0: header_.length, michael@0: header_.version)) michael@0: return ourlength; michael@0: michael@0: // Otherwise, continue by reading our abbreviation entries. michael@0: ReadAbbrevs(); michael@0: michael@0: // Set the string section if we have one. ".debug_str" is the name michael@0: // recommended in the DWARF spec, and used on Linux; "__debug_str" michael@0: // is the name used in Mac OS X Mach-O files. michael@0: iter = sections_.find(".debug_str"); michael@0: if (iter == sections_.end()) michael@0: iter = sections_.find("__debug_str"); michael@0: if (iter != sections_.end()) { michael@0: string_buffer_ = iter->second.first; michael@0: string_buffer_length_ = iter->second.second; michael@0: } michael@0: michael@0: // Now that we have our abbreviations, start processing DIE's. michael@0: ProcessDIEs(); michael@0: michael@0: return ourlength; michael@0: } michael@0: michael@0: // If one really wanted, you could merge SkipAttribute and michael@0: // ProcessAttribute michael@0: // This is all boring data manipulation and calling of the handler. michael@0: const char* CompilationUnit::ProcessAttribute( michael@0: uint64 dieoffset, const char* start, enum DwarfAttribute attr, michael@0: enum DwarfForm form) { michael@0: size_t len; michael@0: michael@0: switch (form) { michael@0: // DW_FORM_indirect is never used because it is such a space michael@0: // waster. michael@0: case DW_FORM_indirect: michael@0: form = static_cast(reader_->ReadUnsignedLEB128(start, michael@0: &len)); michael@0: start += len; michael@0: return ProcessAttribute(dieoffset, start, attr, form); michael@0: michael@0: case DW_FORM_flag_present: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1); michael@0: return start; michael@0: case DW_FORM_data1: michael@0: case DW_FORM_flag: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, michael@0: reader_->ReadOneByte(start)); michael@0: return start + 1; michael@0: case DW_FORM_data2: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, michael@0: reader_->ReadTwoBytes(start)); michael@0: return start + 2; michael@0: case DW_FORM_data4: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, michael@0: reader_->ReadFourBytes(start)); michael@0: return start + 4; michael@0: case DW_FORM_data8: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, michael@0: reader_->ReadEightBytes(start)); michael@0: return start + 8; michael@0: case DW_FORM_string: { michael@0: const char* str = start; michael@0: handler_->ProcessAttributeString(dieoffset, attr, form, michael@0: str); michael@0: return start + strlen(str) + 1; michael@0: } michael@0: case DW_FORM_udata: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, michael@0: reader_->ReadUnsignedLEB128(start, michael@0: &len)); michael@0: return start + len; michael@0: michael@0: case DW_FORM_sdata: michael@0: handler_->ProcessAttributeSigned(dieoffset, attr, form, michael@0: reader_->ReadSignedLEB128(start, &len)); michael@0: return start + len; michael@0: case DW_FORM_addr: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, michael@0: reader_->ReadAddress(start)); michael@0: return start + reader_->AddressSize(); michael@0: case DW_FORM_sec_offset: michael@0: handler_->ProcessAttributeUnsigned(dieoffset, attr, form, michael@0: reader_->ReadOffset(start)); michael@0: return start + reader_->OffsetSize(); michael@0: michael@0: case DW_FORM_ref1: michael@0: handler_->ProcessAttributeReference(dieoffset, attr, form, michael@0: reader_->ReadOneByte(start) michael@0: + offset_from_section_start_); michael@0: return start + 1; michael@0: case DW_FORM_ref2: michael@0: handler_->ProcessAttributeReference(dieoffset, attr, form, michael@0: reader_->ReadTwoBytes(start) michael@0: + offset_from_section_start_); michael@0: return start + 2; michael@0: case DW_FORM_ref4: michael@0: handler_->ProcessAttributeReference(dieoffset, attr, form, michael@0: reader_->ReadFourBytes(start) michael@0: + offset_from_section_start_); michael@0: return start + 4; michael@0: case DW_FORM_ref8: michael@0: handler_->ProcessAttributeReference(dieoffset, attr, form, michael@0: reader_->ReadEightBytes(start) michael@0: + offset_from_section_start_); michael@0: return start + 8; michael@0: case DW_FORM_ref_udata: michael@0: handler_->ProcessAttributeReference(dieoffset, attr, form, michael@0: reader_->ReadUnsignedLEB128(start, michael@0: &len) michael@0: + offset_from_section_start_); michael@0: return start + len; michael@0: case DW_FORM_ref_addr: michael@0: // DWARF2 and 3 differ on whether ref_addr is address size or michael@0: // offset size. michael@0: assert(header_.version == 2 || header_.version == 3); michael@0: if (header_.version == 2) { michael@0: handler_->ProcessAttributeReference(dieoffset, attr, form, michael@0: reader_->ReadAddress(start)); michael@0: return start + reader_->AddressSize(); michael@0: } else if (header_.version == 3) { michael@0: handler_->ProcessAttributeReference(dieoffset, attr, form, michael@0: reader_->ReadOffset(start)); michael@0: return start + reader_->OffsetSize(); michael@0: } michael@0: break; michael@0: case DW_FORM_ref_sig8: michael@0: handler_->ProcessAttributeSignature(dieoffset, attr, form, michael@0: reader_->ReadEightBytes(start)); michael@0: return start + 8; michael@0: michael@0: case DW_FORM_block1: { michael@0: uint64 datalen = reader_->ReadOneByte(start); michael@0: handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 1, michael@0: datalen); michael@0: return start + 1 + datalen; michael@0: } michael@0: case DW_FORM_block2: { michael@0: uint64 datalen = reader_->ReadTwoBytes(start); michael@0: handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 2, michael@0: datalen); michael@0: return start + 2 + datalen; michael@0: } michael@0: case DW_FORM_block4: { michael@0: uint64 datalen = reader_->ReadFourBytes(start); michael@0: handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 4, michael@0: datalen); michael@0: return start + 4 + datalen; michael@0: } michael@0: case DW_FORM_block: michael@0: case DW_FORM_exprloc: { michael@0: uint64 datalen = reader_->ReadUnsignedLEB128(start, &len); michael@0: handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + len, michael@0: datalen); michael@0: return start + datalen + len; michael@0: } michael@0: case DW_FORM_strp: { michael@0: assert(string_buffer_ != NULL); michael@0: michael@0: const uint64 offset = reader_->ReadOffset(start); michael@0: assert(string_buffer_ + offset < string_buffer_ + string_buffer_length_); michael@0: michael@0: const char* str = string_buffer_ + offset; michael@0: handler_->ProcessAttributeString(dieoffset, attr, form, michael@0: str); michael@0: return start + reader_->OffsetSize(); michael@0: } michael@0: } michael@0: fprintf(stderr, "Unhandled form type\n"); michael@0: return NULL; michael@0: } michael@0: michael@0: const char* CompilationUnit::ProcessDIE(uint64 dieoffset, michael@0: const char* start, michael@0: const Abbrev& abbrev) { michael@0: for (AttributeList::const_iterator i = abbrev.attributes.begin(); michael@0: i != abbrev.attributes.end(); michael@0: i++) { michael@0: start = ProcessAttribute(dieoffset, start, i->first, i->second); michael@0: } michael@0: return start; michael@0: } michael@0: michael@0: void CompilationUnit::ProcessDIEs() { michael@0: const char* dieptr = after_header_; michael@0: size_t len; michael@0: michael@0: // lengthstart is the place the length field is based on. michael@0: // It is the point in the header after the initial length field michael@0: const char* lengthstart = buffer_; michael@0: michael@0: // In 64 bit dwarf, the initial length is 12 bytes, because of the michael@0: // 0xffffffff at the start. michael@0: if (reader_->OffsetSize() == 8) michael@0: lengthstart += 12; michael@0: else michael@0: lengthstart += 4; michael@0: michael@0: std::stack die_stack; michael@0: michael@0: while (dieptr < (lengthstart + header_.length)) { michael@0: // We give the user the absolute offset from the beginning of michael@0: // debug_info, since they need it to deal with ref_addr forms. michael@0: uint64 absolute_offset = (dieptr - buffer_) + offset_from_section_start_; michael@0: michael@0: uint64 abbrev_num = reader_->ReadUnsignedLEB128(dieptr, &len); michael@0: michael@0: dieptr += len; michael@0: michael@0: // Abbrev == 0 represents the end of a list of children, or padding michael@0: // at the end of the compilation unit. michael@0: if (abbrev_num == 0) { michael@0: if (die_stack.size() == 0) michael@0: // If it is padding, then we are done with the compilation unit's DIEs. michael@0: return; michael@0: const uint64 offset = die_stack.top(); michael@0: die_stack.pop(); michael@0: handler_->EndDIE(offset); michael@0: continue; michael@0: } michael@0: michael@0: const Abbrev& abbrev = abbrevs_->at(static_cast(abbrev_num)); michael@0: const enum DwarfTag tag = abbrev.tag; michael@0: if (!handler_->StartDIE(absolute_offset, tag)) { michael@0: dieptr = SkipDIE(dieptr, abbrev); michael@0: } else { michael@0: dieptr = ProcessDIE(absolute_offset, dieptr, abbrev); michael@0: } michael@0: michael@0: if (abbrev.has_children) { michael@0: die_stack.push(absolute_offset); michael@0: } else { michael@0: handler_->EndDIE(absolute_offset); michael@0: } michael@0: } michael@0: } michael@0: michael@0: LineInfo::LineInfo(const char* buffer, uint64 buffer_length, michael@0: ByteReader* reader, LineInfoHandler* handler): michael@0: handler_(handler), reader_(reader), buffer_(buffer), michael@0: buffer_length_(buffer_length) { michael@0: header_.std_opcode_lengths = NULL; michael@0: } michael@0: michael@0: uint64 LineInfo::Start() { michael@0: ReadHeader(); michael@0: ReadLines(); michael@0: return after_header_ - buffer_; michael@0: } michael@0: michael@0: // The header for a debug_line section is mildly complicated, because michael@0: // the line info is very tightly encoded. michael@0: void LineInfo::ReadHeader() { michael@0: const char* lineptr = buffer_; michael@0: size_t initial_length_size; michael@0: michael@0: const uint64 initial_length michael@0: = reader_->ReadInitialLength(lineptr, &initial_length_size); michael@0: michael@0: lineptr += initial_length_size; michael@0: header_.total_length = initial_length; michael@0: assert(buffer_ + initial_length_size + header_.total_length <= michael@0: buffer_ + buffer_length_); michael@0: michael@0: // Address size *must* be set by CU ahead of time. michael@0: assert(reader_->AddressSize() != 0); michael@0: michael@0: header_.version = reader_->ReadTwoBytes(lineptr); michael@0: lineptr += 2; michael@0: michael@0: header_.prologue_length = reader_->ReadOffset(lineptr); michael@0: lineptr += reader_->OffsetSize(); michael@0: michael@0: header_.min_insn_length = reader_->ReadOneByte(lineptr); michael@0: lineptr += 1; michael@0: michael@0: header_.default_is_stmt = reader_->ReadOneByte(lineptr); michael@0: lineptr += 1; michael@0: michael@0: header_.line_base = *reinterpret_cast(lineptr); michael@0: lineptr += 1; michael@0: michael@0: header_.line_range = reader_->ReadOneByte(lineptr); michael@0: lineptr += 1; michael@0: michael@0: header_.opcode_base = reader_->ReadOneByte(lineptr); michael@0: lineptr += 1; michael@0: michael@0: header_.std_opcode_lengths = new std::vector; michael@0: header_.std_opcode_lengths->resize(header_.opcode_base + 1); michael@0: (*header_.std_opcode_lengths)[0] = 0; michael@0: for (int i = 1; i < header_.opcode_base; i++) { michael@0: (*header_.std_opcode_lengths)[i] = reader_->ReadOneByte(lineptr); michael@0: lineptr += 1; michael@0: } michael@0: michael@0: // It is legal for the directory entry table to be empty. michael@0: if (*lineptr) { michael@0: uint32 dirindex = 1; michael@0: while (*lineptr) { michael@0: const char* dirname = lineptr; michael@0: handler_->DefineDir(dirname, dirindex); michael@0: lineptr += strlen(dirname) + 1; michael@0: dirindex++; michael@0: } michael@0: } michael@0: lineptr++; michael@0: michael@0: // It is also legal for the file entry table to be empty. michael@0: if (*lineptr) { michael@0: uint32 fileindex = 1; michael@0: size_t len; michael@0: while (*lineptr) { michael@0: const char* filename = lineptr; michael@0: lineptr += strlen(filename) + 1; michael@0: michael@0: uint64 dirindex = reader_->ReadUnsignedLEB128(lineptr, &len); michael@0: lineptr += len; michael@0: michael@0: uint64 mod_time = reader_->ReadUnsignedLEB128(lineptr, &len); michael@0: lineptr += len; michael@0: michael@0: uint64 filelength = reader_->ReadUnsignedLEB128(lineptr, &len); michael@0: lineptr += len; michael@0: handler_->DefineFile(filename, fileindex, static_cast(dirindex), michael@0: mod_time, filelength); michael@0: fileindex++; michael@0: } michael@0: } michael@0: lineptr++; michael@0: michael@0: after_header_ = lineptr; michael@0: } michael@0: michael@0: /* static */ michael@0: bool LineInfo::ProcessOneOpcode(ByteReader* reader, michael@0: LineInfoHandler* handler, michael@0: const struct LineInfoHeader &header, michael@0: const char* start, michael@0: struct LineStateMachine* lsm, michael@0: size_t* len, michael@0: uintptr pc, michael@0: bool *lsm_passes_pc) { michael@0: size_t oplen = 0; michael@0: size_t templen; michael@0: uint8 opcode = reader->ReadOneByte(start); michael@0: oplen++; michael@0: start++; michael@0: michael@0: // If the opcode is great than the opcode_base, it is a special michael@0: // opcode. Most line programs consist mainly of special opcodes. michael@0: if (opcode >= header.opcode_base) { michael@0: opcode -= header.opcode_base; michael@0: const int64 advance_address = (opcode / header.line_range) michael@0: * header.min_insn_length; michael@0: const int32 advance_line = (opcode % header.line_range) michael@0: + header.line_base; michael@0: michael@0: // Check if the lsm passes "pc". If so, mark it as passed. michael@0: if (lsm_passes_pc && michael@0: lsm->address <= pc && pc < lsm->address + advance_address) { michael@0: *lsm_passes_pc = true; michael@0: } michael@0: michael@0: lsm->address += advance_address; michael@0: lsm->line_num += advance_line; michael@0: lsm->basic_block = true; michael@0: *len = oplen; michael@0: return true; michael@0: } michael@0: michael@0: // Otherwise, we have the regular opcodes michael@0: switch (opcode) { michael@0: case DW_LNS_copy: { michael@0: lsm->basic_block = false; michael@0: *len = oplen; michael@0: return true; michael@0: } michael@0: michael@0: case DW_LNS_advance_pc: { michael@0: uint64 advance_address = reader->ReadUnsignedLEB128(start, &templen); michael@0: oplen += templen; michael@0: michael@0: // Check if the lsm passes "pc". If so, mark it as passed. michael@0: if (lsm_passes_pc && lsm->address <= pc && michael@0: pc < lsm->address + header.min_insn_length * advance_address) { michael@0: *lsm_passes_pc = true; michael@0: } michael@0: michael@0: lsm->address += header.min_insn_length * advance_address; michael@0: } michael@0: break; michael@0: case DW_LNS_advance_line: { michael@0: const int64 advance_line = reader->ReadSignedLEB128(start, &templen); michael@0: oplen += templen; michael@0: lsm->line_num += static_cast(advance_line); michael@0: michael@0: // With gcc 4.2.1, we can get the line_no here for the first time michael@0: // since DW_LNS_advance_line is called after DW_LNE_set_address is michael@0: // called. So we check if the lsm passes "pc" here, not in michael@0: // DW_LNE_set_address. michael@0: if (lsm_passes_pc && lsm->address == pc) { michael@0: *lsm_passes_pc = true; michael@0: } michael@0: } michael@0: break; michael@0: case DW_LNS_set_file: { michael@0: const uint64 fileno = reader->ReadUnsignedLEB128(start, &templen); michael@0: oplen += templen; michael@0: lsm->file_num = static_cast(fileno); michael@0: } michael@0: break; michael@0: case DW_LNS_set_column: { michael@0: const uint64 colno = reader->ReadUnsignedLEB128(start, &templen); michael@0: oplen += templen; michael@0: lsm->column_num = static_cast(colno); michael@0: } michael@0: break; michael@0: case DW_LNS_negate_stmt: { michael@0: lsm->is_stmt = !lsm->is_stmt; michael@0: } michael@0: break; michael@0: case DW_LNS_set_basic_block: { michael@0: lsm->basic_block = true; michael@0: } michael@0: break; michael@0: case DW_LNS_fixed_advance_pc: { michael@0: const uint16 advance_address = reader->ReadTwoBytes(start); michael@0: oplen += 2; michael@0: michael@0: // Check if the lsm passes "pc". If so, mark it as passed. michael@0: if (lsm_passes_pc && michael@0: lsm->address <= pc && pc < lsm->address + advance_address) { michael@0: *lsm_passes_pc = true; michael@0: } michael@0: michael@0: lsm->address += advance_address; michael@0: } michael@0: break; michael@0: case DW_LNS_const_add_pc: { michael@0: const int64 advance_address = header.min_insn_length michael@0: * ((255 - header.opcode_base) michael@0: / header.line_range); michael@0: michael@0: // Check if the lsm passes "pc". If so, mark it as passed. michael@0: if (lsm_passes_pc && michael@0: lsm->address <= pc && pc < lsm->address + advance_address) { michael@0: *lsm_passes_pc = true; michael@0: } michael@0: michael@0: lsm->address += advance_address; michael@0: } michael@0: break; michael@0: case DW_LNS_extended_op: { michael@0: const uint64 extended_op_len = reader->ReadUnsignedLEB128(start, michael@0: &templen); michael@0: start += templen; michael@0: oplen += templen + extended_op_len; michael@0: michael@0: const uint64 extended_op = reader->ReadOneByte(start); michael@0: start++; michael@0: michael@0: switch (extended_op) { michael@0: case DW_LNE_end_sequence: { michael@0: lsm->end_sequence = true; michael@0: *len = oplen; michael@0: return true; michael@0: } michael@0: break; michael@0: case DW_LNE_set_address: { michael@0: // With gcc 4.2.1, we cannot tell the line_no here since michael@0: // DW_LNE_set_address is called before DW_LNS_advance_line is michael@0: // called. So we do not check if the lsm passes "pc" here. See michael@0: // also the comment in DW_LNS_advance_line. michael@0: uint64 address = reader->ReadAddress(start); michael@0: lsm->address = address; michael@0: } michael@0: break; michael@0: case DW_LNE_define_file: { michael@0: const char* filename = start; michael@0: michael@0: templen = strlen(filename) + 1; michael@0: start += templen; michael@0: michael@0: uint64 dirindex = reader->ReadUnsignedLEB128(start, &templen); michael@0: oplen += templen; michael@0: michael@0: const uint64 mod_time = reader->ReadUnsignedLEB128(start, michael@0: &templen); michael@0: oplen += templen; michael@0: michael@0: const uint64 filelength = reader->ReadUnsignedLEB128(start, michael@0: &templen); michael@0: oplen += templen; michael@0: michael@0: if (handler) { michael@0: handler->DefineFile(filename, -1, static_cast(dirindex), michael@0: mod_time, filelength); michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: break; michael@0: michael@0: default: { michael@0: // Ignore unknown opcode silently michael@0: if (header.std_opcode_lengths) { michael@0: for (int i = 0; i < (*header.std_opcode_lengths)[opcode]; i++) { michael@0: reader->ReadUnsignedLEB128(start, &templen); michael@0: start += templen; michael@0: oplen += templen; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: *len = oplen; michael@0: return false; michael@0: } michael@0: michael@0: void LineInfo::ReadLines() { michael@0: struct LineStateMachine lsm; michael@0: michael@0: // lengthstart is the place the length field is based on. michael@0: // It is the point in the header after the initial length field michael@0: const char* lengthstart = buffer_; michael@0: michael@0: // In 64 bit dwarf, the initial length is 12 bytes, because of the michael@0: // 0xffffffff at the start. michael@0: if (reader_->OffsetSize() == 8) michael@0: lengthstart += 12; michael@0: else michael@0: lengthstart += 4; michael@0: michael@0: const char* lineptr = after_header_; michael@0: lsm.Reset(header_.default_is_stmt); michael@0: michael@0: // The LineInfoHandler interface expects each line's length along michael@0: // with its address, but DWARF only provides addresses (sans michael@0: // length), and an end-of-sequence address; one infers the length michael@0: // from the next address. So we report a line only when we get the michael@0: // next line's address, or the end-of-sequence address. michael@0: bool have_pending_line = false; michael@0: uint64 pending_address = 0; michael@0: uint32 pending_file_num = 0, pending_line_num = 0, pending_column_num = 0; michael@0: michael@0: while (lineptr < lengthstart + header_.total_length) { michael@0: size_t oplength; michael@0: bool add_row = ProcessOneOpcode(reader_, handler_, header_, michael@0: lineptr, &lsm, &oplength, (uintptr)-1, michael@0: NULL); michael@0: if (add_row) { michael@0: if (have_pending_line) michael@0: handler_->AddLine(pending_address, lsm.address - pending_address, michael@0: pending_file_num, pending_line_num, michael@0: pending_column_num); michael@0: if (lsm.end_sequence) { michael@0: lsm.Reset(header_.default_is_stmt); michael@0: have_pending_line = false; michael@0: } else { michael@0: pending_address = lsm.address; michael@0: pending_file_num = lsm.file_num; michael@0: pending_line_num = lsm.line_num; michael@0: pending_column_num = lsm.column_num; michael@0: have_pending_line = true; michael@0: } michael@0: } michael@0: lineptr += oplength; michael@0: } michael@0: michael@0: after_header_ = lengthstart + header_.total_length; 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 address at which the register is saved. 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: 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: assert(reg != Handler::kCFARegister); michael@0: 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, michael@0: 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: 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: 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: 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: 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: 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: 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: 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: 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_, michael@0: 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_, michael@0: 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: 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_.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_, michael@0: 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_, michael@0: 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: 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: 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: 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: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::EarlyEHTerminator(uint64 offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::CIEPointerOutOfRange(uint64 offset, michael@0: uint64 cie_offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::BadCIEId(uint64 offset, uint64 cie_offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::UnrecognizedVersion(uint64 offset, int version) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::UnrecognizedAugmentation(uint64 offset, michael@0: const string &aug) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::InvalidPointerEncoding(uint64 offset, michael@0: uint8 encoding) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::UnusablePointerEncoding(uint64 offset, michael@0: uint8 encoding) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::RestoreInCIE(uint64 offset, uint64 insn_offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::BadInstruction(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::NoCFARule(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::EmptyStateStack(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: void CallFrameInfo::Reporter::ClearingCFARule(uint64 offset, michael@0: CallFrameInfo::EntryKind kind, michael@0: uint64 insn_offset) { michael@0: fprintf(stderr, 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: } michael@0: michael@0: } // namespace dwarf2reader