void SymbolFileDWARFDwo::LoadSectionData (lldb::SectionType sect_type, DWARFDataExtractor& data) { const SectionList* section_list = m_obj_file->GetSectionList(false /* update_module_section_list */); if (section_list) { SectionSP section_sp (section_list->FindSectionByType(sect_type, true)); if (section_sp) { // See if we memory mapped the DWARF segment? if (m_dwarf_data.GetByteSize()) { data.SetData(m_dwarf_data, section_sp->GetOffset(), section_sp->GetFileSize()); return; } if (m_obj_file->ReadSectionData(section_sp.get(), data) != 0) return; data.Clear(); } } SymbolFileDWARF::LoadSectionData(sect_type, data); }
bool DWARFAbbreviationDeclaration::Extract(const DWARFDataExtractor& data, lldb::offset_t *offset_ptr, dw_uleb128_t code) { m_code = code; m_attributes.clear(); if (m_code) { m_tag = data.GetULEB128(offset_ptr); m_has_children = data.GetU8(offset_ptr); while (data.ValidOffset(*offset_ptr)) { dw_attr_t attr = data.GetULEB128(offset_ptr); dw_form_t form = data.GetULEB128(offset_ptr); if (attr && form) m_attributes.push_back(DWARFAttribute(attr, form)); else break; } return m_tag != 0; } else { m_tag = 0; m_has_children = 0; } return false; }
Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data, uint32_t *offset_ptr) { clear(); if (!data.isValidOffset(*offset_ptr)) return createStringError(errc::invalid_argument, "invalid range list offset 0x%" PRIx32, *offset_ptr); AddressSize = data.getAddressSize(); if (AddressSize != 4 && AddressSize != 8) return createStringError(errc::invalid_argument, "invalid address size: %" PRIu8, AddressSize); Offset = *offset_ptr; while (true) { RangeListEntry Entry; Entry.SectionIndex = -1ULL; uint32_t prev_offset = *offset_ptr; Entry.StartAddress = data.getRelocatedAddress(offset_ptr); Entry.EndAddress = data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex); // Check that both values were extracted correctly. if (*offset_ptr != prev_offset + 2 * AddressSize) { clear(); return createStringError(errc::invalid_argument, "invalid range list entry at offset 0x%" PRIx32, prev_offset); } if (Entry.isEndOfListEntry()) break; Entries.push_back(Entry); } return Error::success(); }
bool DWARFDebugRangeList::extract(const DWARFDataExtractor &data, uint32_t *offset_ptr) { clear(); if (!data.isValidOffset(*offset_ptr)) return false; AddressSize = data.getAddressSize(); if (AddressSize != 4 && AddressSize != 8) return false; Offset = *offset_ptr; while (true) { RangeListEntry entry; uint32_t prev_offset = *offset_ptr; entry.StartAddress = data.getRelocatedAddress(offset_ptr, &entry.SectionIndex); entry.EndAddress = data.getRelocatedAddress(offset_ptr); // Check that both values were extracted correctly. if (*offset_ptr != prev_offset + 2 * AddressSize) { clear(); return false; } if (entry.isEndOfListEntry()) break; Entries.push_back(entry); } return true; }
// Parse v2-v4 directory and file tables. static void parseV2DirFileTables(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker &ContentTypes, std::vector<DWARFFormValue> &IncludeDirectories, std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { while (*OffsetPtr < EndPrologueOffset) { StringRef S = DebugLineData.getCStrRef(OffsetPtr); if (S.empty()) break; DWARFFormValue Dir = DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data()); IncludeDirectories.push_back(Dir); } while (*OffsetPtr < EndPrologueOffset) { StringRef Name = DebugLineData.getCStrRef(OffsetPtr); if (Name.empty()) break; DWARFDebugLine::FileNameEntry FileEntry; FileEntry.Name = DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data()); FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); FileNames.push_back(FileEntry); } ContentTypes.HasModTime = true; ContentTypes.HasLength = true; }
bool DWARFDebugPubnames::Extract(const DWARFDataExtractor& data) { Timer scoped_timer (__PRETTY_FUNCTION__, "DWARFDebugPubnames::Extract (byte_size = %" PRIu64 ")", (uint64_t)data.GetByteSize()); Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_PUBNAMES)); if (log) log->Printf("DWARFDebugPubnames::Extract (byte_size = %" PRIu64 ")", (uint64_t)data.GetByteSize()); if (data.ValidOffset(0)) { lldb::offset_t offset = 0; DWARFDebugPubnamesSet set; while (data.ValidOffset(offset)) { if (set.Extract(data, &offset)) { m_sets.push_back(set); offset = set.GetOffsetOfNextEntry(); } else break; } if (log) Dump (log); return true; } return false; }
bool DWARFTypeUnit::extractImpl(const DWARFDataExtractor &debug_info, uint32_t *offset_ptr) { if (!DWARFUnit::extractImpl(debug_info, offset_ptr)) return false; TypeHash = debug_info.getU64(offset_ptr); TypeOffset = debug_info.getU32(offset_ptr); // TypeOffset is relative to the beginning of the header, // so we have to account for the leading length field. // FIXME: The size of the length field is 12 in DWARF64. unsigned SizeOfLength = 4; return TypeOffset < getLength() + SizeOfLength; }
// Look for a DWARF32-formatted contribution to the string offsets table // starting at a given offset and record it in a descriptor. static Optional<StrOffsetsContributionDescriptor> parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) { if (!DA.isValidOffsetForDataOfSize(Offset, 8)) return None; uint32_t ContributionSize = DA.getU32(&Offset); if (ContributionSize >= 0xfffffff0) return None; uint8_t Version = DA.getU16(&Offset); (void)DA.getU16(&Offset); // padding // The encoded length includes the 2-byte version field and the 2-byte // padding, so we need to subtract them out when we populate the descriptor. return {{Offset, ContributionSize - 4, Version, DWARF32}}; }
void DWARFDebugLoc::parse(const DWARFDataExtractor &data) { IsLittleEndian = data.isLittleEndian(); AddressSize = data.getAddressSize(); uint32_t Offset = 0; while (data.isValidOffset(Offset + data.getAddressSize() - 1)) { if (auto LL = parseOneLocationList(data, &Offset)) Locations.push_back(std::move(*LL)); else break; } if (data.isValidOffset(Offset)) errs() << "error: failed to consume entire .debug_loc section\n"; }
void DWARFDebugMacroHeader::SkipOperandTable( const DWARFDataExtractor &debug_macro_data, lldb::offset_t *offset) { uint8_t entry_count = debug_macro_data.GetU8(offset); for (uint8_t i = 0; i < entry_count; i++) { // Skip over the opcode number. debug_macro_data.GetU8(offset); uint64_t operand_count = debug_macro_data.GetULEB128(offset); for (uint64_t j = 0; j < operand_count; j++) { // Skip over the operand form debug_macro_data.GetU8(offset); } } }
// Look for a DWARF64-formatted contribution to the string offsets table // starting at a given offset and record it in a descriptor. static Optional<StrOffsetsContributionDescriptor> parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) { if (!DA.isValidOffsetForDataOfSize(Offset, 16)) return Optional<StrOffsetsContributionDescriptor>(); if (DA.getU32(&Offset) != 0xffffffff) return Optional<StrOffsetsContributionDescriptor>(); uint64_t Size = DA.getU64(&Offset); uint8_t Version = DA.getU16(&Offset); (void)DA.getU16(&Offset); // padding // The encoded length includes the 2-byte version field and the 2-byte // padding, so we need to subtract them out when we populate the descriptor. return StrOffsetsContributionDescriptor(Offset, Size - 4, Version, DWARF64); //return Optional<StrOffsetsContributionDescriptor>(Descriptor); }
bool DWARFCompileUnit::Extract(const DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr) { Clear(); m_offset = *offset_ptr; if (debug_info.ValidOffset(*offset_ptr)) { dw_offset_t abbr_offset; const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev(); m_length = debug_info.GetDWARFInitialLength(offset_ptr); m_is_dwarf64 = debug_info.IsDWARF64(); m_version = debug_info.GetU16(offset_ptr); abbr_offset = debug_info.GetDWARFOffset(offset_ptr); m_addr_size = debug_info.GetU8 (offset_ptr); bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1); bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset); bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL) { m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset); return true; } // reset the offset to where we tried to parse from if anything went wrong *offset_ptr = m_offset; } return false; }
// Parse v5 directory/file entry content descriptions. // Returns the descriptors, or an empty vector if we did not find a path or // ran off the end of the prologue. static ContentDescriptors parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, uint64_t EndPrologueOffset, bool *HasMD5) { ContentDescriptors Descriptors; int FormatCount = DebugLineData.getU8(OffsetPtr); bool HasPath = false; for (int I = 0; I != FormatCount; ++I) { if (*OffsetPtr >= EndPrologueOffset) return ContentDescriptors(); ContentDescriptor Descriptor; Descriptor.Type = dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr)); Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr)); if (Descriptor.Type == dwarf::DW_LNCT_path) HasPath = true; else if (Descriptor.Type == dwarf::DW_LNCT_MD5 && HasMD5) *HasMD5 = true; Descriptors.push_back(Descriptor); } return HasPath ? Descriptors : ContentDescriptors(); }
Optional<StrOffsetsContributionDescriptor> StrOffsetsContributionDescriptor::validateContributionSize( DWARFDataExtractor &DA) { uint8_t EntrySize = getDwarfOffsetByteSize(); // In order to ensure that we don't read a partial record at the end of // the section we validate for a multiple of the entry size. uint64_t ValidationSize = alignTo(Size, EntrySize); // Guard against overflow. if (ValidationSize >= Size) if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize)) return *this; return Optional<StrOffsetsContributionDescriptor>(); }
//---------------------------------------------------------------------- // DWARFDebugAbbrev::Parse() //---------------------------------------------------------------------- void DWARFDebugAbbrev::Parse(const DWARFDataExtractor &data) { lldb::offset_t offset = 0; while (data.ValidOffset(offset)) { uint32_t initial_cu_offset = offset; DWARFAbbreviationDeclarationSet abbrevDeclSet; if (abbrevDeclSet.Extract(data, &offset)) m_abbrevCollMap[initial_cu_offset] = abbrevDeclSet; else break; } m_prev_abbr_offset_pos = m_abbrevCollMap.end(); }
// Parse v2-v4 directory and file tables. static void parseV2DirFileTables(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, uint64_t EndPrologueOffset, std::vector<StringRef> &IncludeDirectories, std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { while (*OffsetPtr < EndPrologueOffset) { StringRef S = DebugLineData.getCStrRef(OffsetPtr); if (S.empty()) break; IncludeDirectories.push_back(S); } while (*OffsetPtr < EndPrologueOffset) { StringRef Name = DebugLineData.getCStrRef(OffsetPtr); if (Name.empty()) break; DWARFDebugLine::FileNameEntry FileEntry; FileEntry.Name = Name; FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); FileNames.push_back(FileEntry); } }
DWARFDebugMacroHeader DWARFDebugMacroHeader::ParseHeader(const DWARFDataExtractor &debug_macro_data, lldb::offset_t *offset) { DWARFDebugMacroHeader header; // Skip over the version field in header. header.m_version = debug_macro_data.GetU16(offset); uint8_t flags = debug_macro_data.GetU8(offset); header.m_offset_is_64_bit = flags & OFFSET_SIZE_MASK ? true : false; if (flags & DEBUG_LINE_OFFSET_MASK) { if (header.m_offset_is_64_bit) header.m_debug_line_offset = debug_macro_data.GetU64(offset); else header.m_debug_line_offset = debug_macro_data.GetU32(offset); } // Skip over the operands table if it is present. if (flags & OPCODE_OPERANDS_TABLE_MASK) SkipOperandTable(debug_macro_data, offset); return header; }
void DWARFDebugMacinfo::Dump(Stream *s, const DWARFDataExtractor &macinfo_data, lldb::offset_t offset) { DWARFDebugMacinfoEntry maninfo_entry; if (macinfo_data.GetByteSize() == 0) { s->PutCString("< EMPTY >\n"); return; } if (offset == LLDB_INVALID_OFFSET) { offset = 0; while (maninfo_entry.Extract(macinfo_data, &offset)) maninfo_entry.Dump(s); } else { if (maninfo_entry.Extract(macinfo_data, &offset)) maninfo_entry.Dump(s); } }
Optional<DWARFDebugLoc::LocationList> DWARFDebugLoc::parseOneLocationList(DWARFDataExtractor Data, unsigned *Offset) { LocationList LL; LL.Offset = *Offset; // 2.6.2 Location Lists // A location list entry consists of: while (true) { Entry E; if (!Data.isValidOffsetForDataOfSize(*Offset, 2 * Data.getAddressSize())) { llvm::errs() << "Location list overflows the debug_loc section.\n"; return None; } // 1. A beginning address offset. ... E.Begin = Data.getRelocatedAddress(Offset); // 2. An ending address offset. ... E.End = Data.getRelocatedAddress(Offset); // The end of any given location list is marked by an end of list entry, // which consists of a 0 for the beginning address offset and a 0 for the // ending address offset. if (E.Begin == 0 && E.End == 0) return LL; if (!Data.isValidOffsetForDataOfSize(*Offset, 2)) { llvm::errs() << "Location list overflows the debug_loc section.\n"; return None; } unsigned Bytes = Data.getU16(Offset); if (!Data.isValidOffsetForDataOfSize(*Offset, Bytes)) { llvm::errs() << "Location list overflows the debug_loc section.\n"; return None; } // A single location description describing the location of the object... StringRef str = Data.getData().substr(*Offset, Bytes); *Offset += Bytes; E.Loc.reserve(str.size()); std::copy(str.begin(), str.end(), std::back_inserter(E.Loc)); LL.Entries.push_back(std::move(E)); } }
Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable( DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx, const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) { if (!DebugLineData.isValidOffset(Offset)) return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx32 " is not a valid debug line section offset", Offset); std::pair<LineTableIter, bool> Pos = LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable())); LineTable *LT = &Pos.first->second; if (Pos.second) { if (Error Err = LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback)) return std::move(Err); return LT; } return LT; }
//---------------------------------------------------------------------- // Extract //---------------------------------------------------------------------- bool DWARFDebugAranges::Extract(const DWARFDataExtractor &debug_aranges_data) { if (debug_aranges_data.ValidOffset(0)) { lldb::offset_t offset = 0; DWARFDebugArangeSet set; Range range; while (set.Extract(debug_aranges_data, &offset)) { const uint32_t num_descriptors = set.NumDescriptors(); if (num_descriptors > 0) { const dw_offset_t cu_offset = set.GetCompileUnitDIEOffset(); for (uint32_t i = 0; i < num_descriptors; ++i) { const DWARFDebugArangeSet::Descriptor &descriptor = set.GetDescriptorRef(i); m_aranges.Append(RangeToDIE::Entry(descriptor.address, descriptor.length, cu_offset)); } } set.Clear(); } } return false; }
bool DWARFFormValue::ExtractValue(const DWARFDataExtractor& data, lldb::offset_t* offset_ptr, const DWARFCompileUnit* cu) { bool indirect = false; bool is_block = false; m_value.data = NULL; uint8_t ref_addr_size; // Read the value for the form into value and follow and DW_FORM_indirect instances we run into do { indirect = false; switch (m_form) { case DW_FORM_addr: m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::GetAddressByteSize(cu)); break; case DW_FORM_block2: m_value.value.uval = data.GetU16(offset_ptr); is_block = true; break; case DW_FORM_block4: m_value.value.uval = data.GetU32(offset_ptr); is_block = true; break; case DW_FORM_data2: m_value.value.uval = data.GetU16(offset_ptr); break; case DW_FORM_data4: m_value.value.uval = data.GetU32(offset_ptr); break; case DW_FORM_data8: m_value.value.uval = data.GetU64(offset_ptr); break; case DW_FORM_string: m_value.value.cstr = data.GetCStr(offset_ptr); // Set the string value to also be the data for inlined cstr form values only // so we can tell the difference between DW_FORM_string and DW_FORM_strp form // values; m_value.data = (uint8_t*)m_value.value.cstr; break; case DW_FORM_exprloc: case DW_FORM_block: m_value.value.uval = data.GetULEB128(offset_ptr); is_block = true; break; case DW_FORM_block1: m_value.value.uval = data.GetU8(offset_ptr); is_block = true; break; case DW_FORM_data1: m_value.value.uval = data.GetU8(offset_ptr); break; case DW_FORM_flag: m_value.value.uval = data.GetU8(offset_ptr); break; case DW_FORM_sdata: m_value.value.sval = data.GetSLEB128(offset_ptr); break; case DW_FORM_strp: m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::IsDWARF64(cu) ? 8 : 4); break; // case DW_FORM_APPLE_db_str: case DW_FORM_udata: m_value.value.uval = data.GetULEB128(offset_ptr); break; case DW_FORM_ref_addr: ref_addr_size = 4; if (cu) { if (cu->GetVersion() <= 2) ref_addr_size = cu->GetAddressByteSize(); else ref_addr_size = cu->IsDWARF64() ? 8 : 4; } m_value.value.uval = data.GetMaxU64(offset_ptr, ref_addr_size); break; case DW_FORM_ref1: m_value.value.uval = data.GetU8(offset_ptr); break; case DW_FORM_ref2: m_value.value.uval = data.GetU16(offset_ptr); break; case DW_FORM_ref4: m_value.value.uval = data.GetU32(offset_ptr); break; case DW_FORM_ref8: m_value.value.uval = data.GetU64(offset_ptr); break; case DW_FORM_ref_udata: m_value.value.uval = data.GetULEB128(offset_ptr); break; case DW_FORM_indirect: m_form = data.GetULEB128(offset_ptr); indirect = true; break; case DW_FORM_sec_offset: m_value.value.uval = data.GetMaxU64(offset_ptr, DWARFCompileUnit::IsDWARF64(cu) ? 8 : 4); break; case DW_FORM_flag_present: m_value.value.uval = 1; break; case DW_FORM_ref_sig8: m_value.value.uval = data.GetU64(offset_ptr); break; default: return false; break; } } while (indirect); if (is_block) { m_value.data = data.PeekData(*offset_ptr, m_value.value.uval); if (m_value.data != NULL) { *offset_ptr += m_value.value.uval; } } return true; }
bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, const DWARFUnit *U) { const uint64_t PrologueOffset = *OffsetPtr; clear(); TotalLength = DebugLineData.getU32(OffsetPtr); if (TotalLength == UINT32_MAX) { FormParams.Format = dwarf::DWARF64; TotalLength = DebugLineData.getU64(OffsetPtr); } else if (TotalLength >= 0xffffff00) { return false; } FormParams.Version = DebugLineData.getU16(OffsetPtr); if (getVersion() < 2) return false; if (getVersion() >= 5) { FormParams.AddrSize = DebugLineData.getU8(OffsetPtr); assert((DebugLineData.getAddressSize() == 0 || DebugLineData.getAddressSize() == getAddressSize()) && "Line table header and data extractor disagree"); SegSelectorSize = DebugLineData.getU8(OffsetPtr); } PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength()); const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr; MinInstLength = DebugLineData.getU8(OffsetPtr); if (getVersion() >= 4) MaxOpsPerInst = DebugLineData.getU8(OffsetPtr); DefaultIsStmt = DebugLineData.getU8(OffsetPtr); LineBase = DebugLineData.getU8(OffsetPtr); LineRange = DebugLineData.getU8(OffsetPtr); OpcodeBase = DebugLineData.getU8(OffsetPtr); StandardOpcodeLengths.reserve(OpcodeBase - 1); for (uint32_t I = 1; I < OpcodeBase; ++I) { uint8_t OpLen = DebugLineData.getU8(OffsetPtr); StandardOpcodeLengths.push_back(OpLen); } if (getVersion() >= 5) { if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset, FormParams, U, HasMD5, IncludeDirectories, FileNames)) { fprintf(stderr, "warning: parsing line table prologue at 0x%8.8" PRIx64 " found an invalid directory or file table description at" " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr); return false; } } else parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset, IncludeDirectories, FileNames); if (*OffsetPtr != EndPrologueOffset) { fprintf(stderr, "warning: parsing line table prologue at 0x%8.8" PRIx64 " should have ended at 0x%8.8" PRIx64 " but it ended at 0x%8.8" PRIx64 "\n", PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr); return false; } return true; }
bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData, uint32_t *Offset, unsigned UnitIndex, uint8_t &UnitType, bool &isUnitDWARF64) { uint32_t AbbrOffset, Length; uint8_t AddrSize = 0; uint16_t Version; bool Success = true; bool ValidLength = false; bool ValidVersion = false; bool ValidAddrSize = false; bool ValidType = true; bool ValidAbbrevOffset = true; uint32_t OffsetStart = *Offset; Length = DebugInfoData.getU32(Offset); if (Length == UINT32_MAX) { isUnitDWARF64 = true; OS << format( "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n", UnitIndex); return false; } Version = DebugInfoData.getU16(Offset); if (Version >= 5) { UnitType = DebugInfoData.getU8(Offset); AddrSize = DebugInfoData.getU8(Offset); AbbrOffset = DebugInfoData.getU32(Offset); ValidType = dwarf::isUnitType(UnitType); } else { UnitType = 0; AbbrOffset = DebugInfoData.getU32(Offset); AddrSize = DebugInfoData.getU8(Offset); } if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset)) ValidAbbrevOffset = false; ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3); ValidVersion = DWARFContext::isSupportedVersion(Version); ValidAddrSize = AddrSize == 4 || AddrSize == 8; if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset || !ValidType) { Success = false; error() << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart); if (!ValidLength) note() << "The length for this unit is too " "large for the .debug_info provided.\n"; if (!ValidVersion) note() << "The 16 bit unit header version is not valid.\n"; if (!ValidType) note() << "The unit type encoding is not valid.\n"; if (!ValidAbbrevOffset) note() << "The offset into the .debug_abbrev section is " "not valid.\n"; if (!ValidAddrSize) note() << "The address size is unsupported.\n"; } *Offset = OffsetStart + Length + 4; return Success; }
bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, const DWARFUnit *U, raw_ostream *OS) { const uint32_t DebugLineOffset = *OffsetPtr; clear(); if (!Prologue.parse(DebugLineData, OffsetPtr, U)) { // Restore our offset and return false to indicate failure! *OffsetPtr = DebugLineOffset; return false; } if (OS) Prologue.dump(*OS); const uint32_t EndOffset = DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength(); // See if we should tell the data extractor the address size. if (DebugLineData.getAddressSize() == 0) DebugLineData.setAddressSize(Prologue.getAddressSize()); else assert(Prologue.getAddressSize() == 0 || Prologue.getAddressSize() == DebugLineData.getAddressSize()); ParsingState State(this); while (*OffsetPtr < EndOffset) { if (OS) *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr); uint8_t Opcode = DebugLineData.getU8(OffsetPtr); if (OS) *OS << format("%02.02" PRIx8 " ", Opcode); if (Opcode == 0) { // Extended Opcodes always start with a zero opcode followed by // a uleb128 length so you can skip ones you don't know about uint64_t Len = DebugLineData.getULEB128(OffsetPtr); uint32_t ExtOffset = *OffsetPtr; // Tolerate zero-length; assume length is correct and soldier on. if (Len == 0) { if (OS) *OS << "Badly formed extended line op (length 0)\n"; continue; } uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr); if (OS) *OS << LNExtendedString(SubOpcode); switch (SubOpcode) { case DW_LNE_end_sequence: // Set the end_sequence register of the state machine to true and // append a row to the matrix using the current values of the // state-machine registers. Then reset the registers to the initial // values specified above. Every statement program sequence must end // with a DW_LNE_end_sequence instruction which creates a row whose // address is that of the byte after the last target machine instruction // of the sequence. State.Row.EndSequence = true; State.appendRowToMatrix(*OffsetPtr); if (OS) { *OS << "\n"; OS->indent(12); State.Row.dump(*OS); } State.resetRowAndSequence(); break; case DW_LNE_set_address: // Takes a single relocatable address as an operand. The size of the // operand is the size appropriate to hold an address on the target // machine. Set the address register to the value given by the // relocatable address. All of the other statement program opcodes // that affect the address register add a delta to it. This instruction // stores a relocatable value into it instead. // // Make sure the extractor knows the address size. If not, infer it // from the size of the operand. if (DebugLineData.getAddressSize() == 0) DebugLineData.setAddressSize(Len - 1); else assert(DebugLineData.getAddressSize() == Len - 1); State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr); if (OS) *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address); break; case DW_LNE_define_file: // Takes 4 arguments. The first is a null terminated string containing // a source file name. The second is an unsigned LEB128 number // representing the directory index of the directory in which the file // was found. The third is an unsigned LEB128 number representing the // time of last modification of the file. The fourth is an unsigned // LEB128 number representing the length in bytes of the file. The time // and length fields may contain LEB128(0) if the information is not // available. // // The directory index represents an entry in the include_directories // section of the statement program prologue. The index is LEB128(0) // if the file was found in the current directory of the compilation, // LEB128(1) if it was found in the first directory in the // include_directories section, and so on. The directory index is // ignored for file names that represent full path names. // // The files are numbered, starting at 1, in the order in which they // appear; the names in the prologue come before names defined by // the DW_LNE_define_file instruction. These numbers are used in the // the file register of the state machine. { FileNameEntry FileEntry; FileEntry.Name = DebugLineData.getCStr(OffsetPtr); FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); Prologue.FileNames.push_back(FileEntry); if (OS) *OS << " (" << FileEntry.Name.str() << ", dir=" << FileEntry.DirIdx << ", mod_time=" << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime) << ", length=" << FileEntry.Length << ")"; } break; case DW_LNE_set_discriminator: State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr); if (OS) *OS << " (" << State.Row.Discriminator << ")"; break; default: if (OS) *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode) << format(" length %" PRIx64, Len); // Len doesn't include the zero opcode byte or the length itself, but // it does include the sub_opcode, so we have to adjust for that. (*OffsetPtr) += Len - 1; break; } // Make sure the stated and parsed lengths are the same. // Otherwise we have an unparseable line-number program. if (*OffsetPtr - ExtOffset != Len) { fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n", ExtOffset, Len, *OffsetPtr - ExtOffset); // Skip the rest of the line-number program. *OffsetPtr = EndOffset; return false; } } else if (Opcode < Prologue.OpcodeBase) { if (OS) *OS << LNStandardString(Opcode); switch (Opcode) { // Standard Opcodes case DW_LNS_copy: // Takes no arguments. Append a row to the matrix using the // current values of the state-machine registers. Then set // the basic_block register to false. State.appendRowToMatrix(*OffsetPtr); if (OS) { *OS << "\n"; OS->indent(12); State.Row.dump(*OS); *OS << "\n"; } break; case DW_LNS_advance_pc: // Takes a single unsigned LEB128 operand, multiplies it by the // min_inst_length field of the prologue, and adds the // result to the address register of the state machine. { uint64_t AddrOffset = DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength; State.Row.Address += AddrOffset; if (OS) *OS << " (" << AddrOffset << ")"; } break; case DW_LNS_advance_line: // Takes a single signed LEB128 operand and adds that value to // the line register of the state machine. State.Row.Line += DebugLineData.getSLEB128(OffsetPtr); if (OS) *OS << " (" << State.Row.Line << ")"; break; case DW_LNS_set_file: // Takes a single unsigned LEB128 operand and stores it in the file // register of the state machine. State.Row.File = DebugLineData.getULEB128(OffsetPtr); if (OS) *OS << " (" << State.Row.File << ")"; break; case DW_LNS_set_column: // Takes a single unsigned LEB128 operand and stores it in the // column register of the state machine. State.Row.Column = DebugLineData.getULEB128(OffsetPtr); if (OS) *OS << " (" << State.Row.Column << ")"; break; case DW_LNS_negate_stmt: // Takes no arguments. Set the is_stmt register of the state // machine to the logical negation of its current value. State.Row.IsStmt = !State.Row.IsStmt; break; case DW_LNS_set_basic_block: // Takes no arguments. Set the basic_block register of the // state machine to true State.Row.BasicBlock = true; break; case DW_LNS_const_add_pc: // Takes no arguments. Add to the address register of the state // machine the address increment value corresponding to special // opcode 255. The motivation for DW_LNS_const_add_pc is this: // when the statement program needs to advance the address by a // small amount, it can use a single special opcode, which occupies // a single byte. When it needs to advance the address by up to // twice the range of the last special opcode, it can use // DW_LNS_const_add_pc followed by a special opcode, for a total // of two bytes. Only if it needs to advance the address by more // than twice that range will it need to use both DW_LNS_advance_pc // and a special opcode, requiring three or more bytes. { uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase; uint64_t AddrOffset = (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; State.Row.Address += AddrOffset; if (OS) *OS << format(" (0x%16.16" PRIx64 ")", AddrOffset); } break; case DW_LNS_fixed_advance_pc: // Takes a single uhalf operand. Add to the address register of // the state machine the value of the (unencoded) operand. This // is the only extended opcode that takes an argument that is not // a variable length number. The motivation for DW_LNS_fixed_advance_pc // is this: existing assemblers cannot emit DW_LNS_advance_pc or // special opcodes because they cannot encode LEB128 numbers or // judge when the computation of a special opcode overflows and // requires the use of DW_LNS_advance_pc. Such assemblers, however, // can use DW_LNS_fixed_advance_pc instead, sacrificing compression. { uint16_t PCOffset = DebugLineData.getU16(OffsetPtr); State.Row.Address += PCOffset; if (OS) *OS << format(" (0x%16.16" PRIx64 ")", PCOffset); } break; case DW_LNS_set_prologue_end: // Takes no arguments. Set the prologue_end register of the // state machine to true State.Row.PrologueEnd = true; break; case DW_LNS_set_epilogue_begin: // Takes no arguments. Set the basic_block register of the // state machine to true State.Row.EpilogueBegin = true; break; case DW_LNS_set_isa: // Takes a single unsigned LEB128 operand and stores it in the // column register of the state machine. State.Row.Isa = DebugLineData.getULEB128(OffsetPtr); if (OS) *OS << " (" << State.Row.Isa << ")"; break; default: // Handle any unknown standard opcodes here. We know the lengths // of such opcodes because they are specified in the prologue // as a multiple of LEB128 operands for each opcode. { assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size()); uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1]; for (uint8_t I = 0; I < OpcodeLength; ++I) { uint64_t Value = DebugLineData.getULEB128(OffsetPtr); if (OS) *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n", Value); } } break; } } else { // Special Opcodes // A special opcode value is chosen based on the amount that needs // to be added to the line and address registers. The maximum line // increment for a special opcode is the value of the line_base // field in the header, plus the value of the line_range field, // minus 1 (line base + line range - 1). If the desired line // increment is greater than the maximum line increment, a standard // opcode must be used instead of a special opcode. The "address // advance" is calculated by dividing the desired address increment // by the minimum_instruction_length field from the header. The // special opcode is then calculated using the following formula: // // opcode = (desired line increment - line_base) + // (line_range * address advance) + opcode_base // // If the resulting opcode is greater than 255, a standard opcode // must be used instead. // // To decode a special opcode, subtract the opcode_base from the // opcode itself to give the adjusted opcode. The amount to // increment the address register is the result of the adjusted // opcode divided by the line_range multiplied by the // minimum_instruction_length field from the header. That is: // // address increment = (adjusted opcode / line_range) * // minimum_instruction_length // // The amount to increment the line register is the line_base plus // the result of the adjusted opcode modulo the line_range. That is: // // line increment = line_base + (adjusted opcode % line_range) uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase; uint64_t AddrOffset = (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; int32_t LineOffset = Prologue.LineBase + (AdjustOpcode % Prologue.LineRange); State.Row.Line += LineOffset; State.Row.Address += AddrOffset; if (OS) { *OS << "address += " << ((uint32_t)AdjustOpcode) << ", line += " << LineOffset << "\n"; OS->indent(12); State.Row.dump(*OS); } State.appendRowToMatrix(*OffsetPtr); // Reset discriminator to 0. State.Row.Discriminator = 0; } if(OS) *OS << "\n"; } if (!State.Sequence.Empty) { fprintf(stderr, "warning: last sequence in debug line table is not" "terminated!\n"); } // Sort all sequences so that address lookup will work faster. if (!Sequences.empty()) { std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC); // Note: actually, instruction address ranges of sequences should not // overlap (in shared objects and executables). If they do, the address // lookup would still work, though, but result would be ambiguous. // We don't report warning in this case. For example, // sometimes .so compiled from multiple object files contains a few // rudimentary sequences for address ranges [0x0, 0xsomething). } return EndOffset; }
bool DWARFUnitHeader::extract(DWARFContext &Context, const DWARFDataExtractor &debug_info, uint32_t *offset_ptr, DWARFSectionKind SectionKind, const DWARFUnitIndex *Index) { Offset = *offset_ptr; IndexEntry = Index ? Index->getFromOffset(*offset_ptr) : nullptr; Length = debug_info.getU32(offset_ptr); // FIXME: Support DWARF64. unsigned SizeOfLength = 4; FormParams.Format = DWARF32; FormParams.Version = debug_info.getU16(offset_ptr); if (FormParams.Version >= 5) { UnitType = debug_info.getU8(offset_ptr); FormParams.AddrSize = debug_info.getU8(offset_ptr); AbbrOffset = debug_info.getU32(offset_ptr); } else { AbbrOffset = debug_info.getRelocatedValue(4, offset_ptr); FormParams.AddrSize = debug_info.getU8(offset_ptr); // Fake a unit type based on the section type. This isn't perfect, // but distinguishing compile and type units is generally enough. if (SectionKind == DW_SECT_TYPES) UnitType = DW_UT_type; else UnitType = DW_UT_compile; } if (IndexEntry) { if (AbbrOffset) return false; auto *UnitContrib = IndexEntry->getOffset(); if (!UnitContrib || UnitContrib->Length != (Length + 4)) return false; auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV); if (!AbbrEntry) return false; AbbrOffset = AbbrEntry->Offset; } if (isTypeUnit()) { TypeHash = debug_info.getU64(offset_ptr); TypeOffset = debug_info.getU32(offset_ptr); } else if (UnitType == DW_UT_split_compile || UnitType == DW_UT_skeleton) DWOId = debug_info.getU64(offset_ptr); // Header fields all parsed, capture the size of this unit header. assert(*offset_ptr - Offset <= 255 && "unexpected header size"); Size = uint8_t(*offset_ptr - Offset); // Type offset is unit-relative; should be after the header and before // the end of the current unit. bool TypeOffsetOK = !isTypeUnit() ? true : TypeOffset >= Size && TypeOffset < getLength() + SizeOfLength; bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1); bool VersionOK = DWARFContext::isSupportedVersion(getVersion()); bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8; if (!LengthOK || !VersionOK || !AddrSizeOK || !TypeOffsetOK) return false; // Keep track of the highest DWARF version we encounter across all units. Context.setMaxVersionIfGreater(getVersion()); return true; }
bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, DWARFFormParams FP, const DWARFUnit *CU) { U = CU; bool Indirect = false; bool IsBlock = false; Value.data = nullptr; // Read the value for the form into value and follow and DW_FORM_indirect // instances we run into do { Indirect = false; switch (Form) { case DW_FORM_addr: case DW_FORM_ref_addr: { uint16_t Size = (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize(); Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex); break; } case DW_FORM_exprloc: case DW_FORM_block: Value.uval = Data.getULEB128(OffsetPtr); IsBlock = true; break; case DW_FORM_block1: Value.uval = Data.getU8(OffsetPtr); IsBlock = true; break; case DW_FORM_block2: Value.uval = Data.getU16(OffsetPtr); IsBlock = true; break; case DW_FORM_block4: Value.uval = Data.getU32(OffsetPtr); IsBlock = true; break; case DW_FORM_data1: case DW_FORM_ref1: case DW_FORM_flag: case DW_FORM_strx1: case DW_FORM_addrx1: Value.uval = Data.getU8(OffsetPtr); break; case DW_FORM_data2: case DW_FORM_ref2: case DW_FORM_strx2: case DW_FORM_addrx2: Value.uval = Data.getU16(OffsetPtr); break; case DW_FORM_strx3: Value.uval = Data.getU24(OffsetPtr); break; case DW_FORM_data4: case DW_FORM_ref4: case DW_FORM_ref_sup4: case DW_FORM_strx4: case DW_FORM_addrx4: Value.uval = Data.getRelocatedValue(4, OffsetPtr); break; case DW_FORM_data8: case DW_FORM_ref8: case DW_FORM_ref_sup8: Value.uval = Data.getU64(OffsetPtr); break; case DW_FORM_data16: // Treat this like a 16-byte block. Value.uval = 16; IsBlock = true; break; case DW_FORM_sdata: Value.sval = Data.getSLEB128(OffsetPtr); break; case DW_FORM_udata: case DW_FORM_ref_udata: Value.uval = Data.getULEB128(OffsetPtr); break; case DW_FORM_string: Value.cstr = Data.getCStr(OffsetPtr); break; case DW_FORM_indirect: Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr)); Indirect = true; break; case DW_FORM_strp: case DW_FORM_sec_offset: case DW_FORM_GNU_ref_alt: case DW_FORM_GNU_strp_alt: case DW_FORM_line_strp: case DW_FORM_strp_sup: { Value.uval = Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr); break; } case DW_FORM_flag_present: Value.uval = 1; break; case DW_FORM_ref_sig8: Value.uval = Data.getU64(OffsetPtr); break; case DW_FORM_GNU_addr_index: case DW_FORM_GNU_str_index: case DW_FORM_strx: Value.uval = Data.getULEB128(OffsetPtr); break; default: // DWARFFormValue::skipValue() will have caught this and caused all // DWARF DIEs to fail to be parsed, so this code is not be reachable. llvm_unreachable("unsupported form"); } } while (Indirect); if (IsBlock) { StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval); Value.data = nullptr; if (!Str.empty()) { Value.data = reinterpret_cast<const uint8_t *>(Str.data()); *OffsetPtr += Value.uval; } } return true; }
bool DWARFAbbreviationDeclaration::Extract(const DWARFDataExtractor& data, lldb::offset_t* offset_ptr) { return Extract(data, offset_ptr, data.GetULEB128(offset_ptr)); }
llvm::Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS, uint32_t *Offset) { // Check that we can read the fixed-size part. if (!AS.isValidOffset(*Offset + sizeof(HeaderPOD) - 1)) return make_error<StringError>("Section too small: cannot read header.", inconvertibleErrorCode()); UnitLength = AS.getU32(Offset); Version = AS.getU16(Offset); Padding = AS.getU16(Offset); CompUnitCount = AS.getU32(Offset); LocalTypeUnitCount = AS.getU32(Offset); ForeignTypeUnitCount = AS.getU32(Offset); BucketCount = AS.getU32(Offset); NameCount = AS.getU32(Offset); AbbrevTableSize = AS.getU32(Offset); AugmentationStringSize = AS.getU32(Offset); if (!AS.isValidOffsetForDataOfSize(*Offset, AugmentationStringSize)) return make_error<StringError>( "Section too small: cannot read header augmentation.", inconvertibleErrorCode()); AugmentationString.resize(AugmentationStringSize); AS.getU8(Offset, reinterpret_cast<uint8_t *>(AugmentationString.data()), AugmentationStringSize); *Offset = alignTo(*Offset, 4); return Error::success(); }
bool DWARFFormValue::SkipValue(dw_form_t form, const DWARFDataExtractor& debug_info_data, lldb::offset_t *offset_ptr, const DWARFCompileUnit* cu) { uint8_t ref_addr_size; switch (form) { // Blocks if inlined data that have a length field and the data bytes // inlined in the .debug_info case DW_FORM_exprloc: case DW_FORM_block: { dw_uleb128_t size = debug_info_data.GetULEB128(offset_ptr); *offset_ptr += size; } return true; case DW_FORM_block1: { dw_uleb128_t size = debug_info_data.GetU8(offset_ptr); *offset_ptr += size; } return true; case DW_FORM_block2: { dw_uleb128_t size = debug_info_data.GetU16(offset_ptr); *offset_ptr += size; } return true; case DW_FORM_block4: { dw_uleb128_t size = debug_info_data.GetU32(offset_ptr); *offset_ptr += size; } return true; // Inlined NULL terminated C-strings case DW_FORM_string: debug_info_data.GetCStr(offset_ptr); return true; // Compile unit address sized values case DW_FORM_addr: *offset_ptr += DWARFCompileUnit::GetAddressByteSize(cu); return true; case DW_FORM_ref_addr: ref_addr_size = 4; if (cu) { if (cu->GetVersion() <= 2) ref_addr_size = cu->GetAddressByteSize(); else ref_addr_size = cu->IsDWARF64() ? 8 : 4; } *offset_ptr += ref_addr_size; return true; // 0 bytes values (implied from DW_FORM) case DW_FORM_flag_present: return true; // 1 byte values case DW_FORM_data1: case DW_FORM_flag: case DW_FORM_ref1: *offset_ptr += 1; return true; // 2 byte values case DW_FORM_data2: case DW_FORM_ref2: *offset_ptr += 2; return true; // 32 bit for DWARF 32, 64 for DWARF 64 case DW_FORM_sec_offset: case DW_FORM_strp: *offset_ptr += (cu->IsDWARF64() ? 8 : 4); return true; // 4 byte values case DW_FORM_data4: case DW_FORM_ref4: *offset_ptr += 4; return true; // 8 byte values case DW_FORM_data8: case DW_FORM_ref8: case DW_FORM_ref_sig8: *offset_ptr += 8; return true; // signed or unsigned LEB 128 values case DW_FORM_sdata: case DW_FORM_udata: case DW_FORM_ref_udata: debug_info_data.Skip_LEB128(offset_ptr); return true; case DW_FORM_indirect: { dw_form_t indirect_form = debug_info_data.GetULEB128(offset_ptr); return DWARFFormValue::SkipValue (indirect_form, debug_info_data, offset_ptr, cu); } default: break; } return false; }