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 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; }