Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 1 | //===- DWARFUnit.cpp ------------------------------------------------------===// |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFUnit.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFDie.h" |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 20 | #include "llvm/Support/DataExtractor.h" |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/WithColor.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <cassert> |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 25 | #include <cstddef> |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 26 | #include <cstdint> |
David Blaikie | 8de5e98 | 2013-09-23 23:15:57 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 28 | #include <utility> |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 29 | #include <vector> |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 30 | |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 31 | using namespace llvm; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 32 | using namespace dwarf; |
| 33 | |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 34 | void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 35 | const DWARFObject &D = C.getDWARFObj(); |
Rafael Espindola | c51dc90 | 2018-03-21 21:31:25 +0000 | [diff] [blame] | 36 | parseImpl(C, D, Section, C.getDebugAbbrev(), &D.getRangeSection(), |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 37 | D.getStringSection(), D.getStringOffsetSection(), |
Paul Robinson | d0c89f8 | 2018-01-29 22:02:56 +0000 | [diff] [blame] | 38 | &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false, |
| 39 | false); |
Frederic Riss | 6005dbd | 2014-10-06 03:36:18 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 42 | void DWARFUnitSectionBase::parseDWO(DWARFContext &C, |
David Blaikie | e79dda3 | 2017-09-19 18:36:11 +0000 | [diff] [blame] | 43 | const DWARFSection &DWOSection, bool Lazy) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 44 | const DWARFObject &D = C.getDWARFObj(); |
Rafael Espindola | c51dc90 | 2018-03-21 21:31:25 +0000 | [diff] [blame] | 45 | parseImpl(C, D, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(), |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 46 | D.getStringDWOSection(), D.getStringOffsetDWOSection(), |
Paul Robinson | d0c89f8 | 2018-01-29 22:02:56 +0000 | [diff] [blame] | 47 | &D.getAddrSection(), D.getLineDWOSection(), C.isLittleEndian(), |
| 48 | true, Lazy); |
Frederic Riss | 6005dbd | 2014-10-06 03:36:18 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Alexey Samsonov | 4b4d64b | 2014-10-08 00:24:41 +0000 | [diff] [blame] | 51 | DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section, |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 52 | const DWARFUnitHeader &Header, |
George Rimar | ca53211 | 2017-04-24 10:19:45 +0000 | [diff] [blame] | 53 | const DWARFDebugAbbrev *DA, const DWARFSection *RS, |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 54 | StringRef SS, const DWARFSection &SOS, |
Paul Robinson | d0c89f8 | 2018-01-29 22:02:56 +0000 | [diff] [blame] | 55 | const DWARFSection *AOS, const DWARFSection &LS, bool LE, |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 56 | bool IsDWO, const DWARFUnitSectionBase &UnitSection) |
| 57 | : Context(DC), InfoSection(Section), Header(Header), Abbrev(DA), |
| 58 | RangeSection(RS), LineSection(LS), StringSection(SS), |
| 59 | StringOffsetSection(SOS), AddrOffsetSection(AOS), isLittleEndian(LE), |
| 60 | isDWO(IsDWO), UnitSection(UnitSection) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 61 | clear(); |
| 62 | } |
| 63 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 64 | DWARFUnit::~DWARFUnit() = default; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 65 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 66 | DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const { |
| 67 | return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian, |
| 68 | getAddressByteSize()); |
| 69 | } |
| 70 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 71 | bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index, |
| 72 | uint64_t &Result) const { |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 73 | uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize(); |
| 74 | if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize()) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 75 | return false; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 76 | DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection, |
| 77 | isLittleEndian, getAddressByteSize()); |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 78 | Result = DA.getRelocatedAddress(&Offset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index, |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 83 | uint64_t &Result) const { |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 84 | if (!StringOffsetsTableContribution) |
| 85 | return false; |
| 86 | unsigned ItemSize = getDwarfStringOffsetsByteSize(); |
| 87 | uint32_t Offset = getStringOffsetsBase() + Index * ItemSize; |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 88 | if (StringOffsetSection.Data.size() < Offset + ItemSize) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 89 | return false; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 90 | DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, |
| 91 | isLittleEndian, 0); |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 92 | Result = DA.getRelocatedValue(ItemSize, &Offset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 96 | bool DWARFUnitHeader::extract(DWARFContext &Context, |
| 97 | const DWARFDataExtractor &debug_info, |
| 98 | uint32_t *offset_ptr, |
| 99 | DWARFSectionKind SectionKind, |
| 100 | const DWARFUnitIndex *Index) { |
| 101 | Offset = *offset_ptr; |
| 102 | IndexEntry = Index ? Index->getFromOffset(*offset_ptr) : nullptr; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 103 | Length = debug_info.getU32(offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 104 | // FIXME: Support DWARF64. |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 105 | unsigned SizeOfLength = 4; |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 106 | FormParams.Format = DWARF32; |
| 107 | FormParams.Version = debug_info.getU16(offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 108 | if (FormParams.Version >= 5) { |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 109 | UnitType = debug_info.getU8(offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 110 | FormParams.AddrSize = debug_info.getU8(offset_ptr); |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 111 | AbbrOffset = debug_info.getU32(offset_ptr); |
| 112 | } else { |
Rafael Espindola | c51dc90 | 2018-03-21 21:31:25 +0000 | [diff] [blame] | 113 | AbbrOffset = debug_info.getRelocatedValue(4, offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 114 | FormParams.AddrSize = debug_info.getU8(offset_ptr); |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 115 | // Fake a unit type based on the section type. This isn't perfect, |
| 116 | // but distinguishing compile and type units is generally enough. |
| 117 | if (SectionKind == DW_SECT_TYPES) |
| 118 | UnitType = DW_UT_type; |
| 119 | else |
| 120 | UnitType = DW_UT_compile; |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 121 | } |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 122 | if (IndexEntry) { |
| 123 | if (AbbrOffset) |
| 124 | return false; |
| 125 | auto *UnitContrib = IndexEntry->getOffset(); |
| 126 | if (!UnitContrib || UnitContrib->Length != (Length + 4)) |
| 127 | return false; |
| 128 | auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV); |
| 129 | if (!AbbrEntry) |
| 130 | return false; |
| 131 | AbbrOffset = AbbrEntry->Offset; |
| 132 | } |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 133 | if (isTypeUnit()) { |
| 134 | TypeHash = debug_info.getU64(offset_ptr); |
| 135 | TypeOffset = debug_info.getU32(offset_ptr); |
Paul Robinson | 543c0e1 | 2018-05-22 17:27:31 +0000 | [diff] [blame] | 136 | } else if (UnitType == DW_UT_split_compile || UnitType == DW_UT_skeleton) |
| 137 | DWOId = debug_info.getU64(offset_ptr); |
| 138 | |
| 139 | // Header fields all parsed, capture the size of this unit header. |
| 140 | assert(*offset_ptr - Offset <= 255 && "unexpected header size"); |
| 141 | Size = uint8_t(*offset_ptr - Offset); |
| 142 | |
| 143 | // Type offset is unit-relative; should be after the header and before |
| 144 | // the end of the current unit. |
| 145 | bool TypeOffsetOK = |
| 146 | !isTypeUnit() |
| 147 | ? true |
| 148 | : TypeOffset >= Size && TypeOffset < getLength() + SizeOfLength; |
Alexey Samsonov | 7682f81 | 2014-04-24 22:51:03 +0000 | [diff] [blame] | 149 | bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 150 | bool VersionOK = DWARFContext::isSupportedVersion(getVersion()); |
| 151 | bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 152 | |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 153 | if (!LengthOK || !VersionOK || !AddrSizeOK || !TypeOffsetOK) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 154 | return false; |
| 155 | |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 156 | // Keep track of the highest DWARF version we encounter across all units. |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 157 | Context.setMaxVersionIfGreater(getVersion()); |
David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 158 | return true; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 161 | // Parse the rangelist table header, including the optional array of offsets |
| 162 | // following it (DWARF v5 and later). |
| 163 | static Expected<DWARFDebugRnglistTable> |
| 164 | parseRngListTableHeader(DWARFDataExtractor &DA, uint32_t Offset) { |
| 165 | // TODO: Support DWARF64 |
| 166 | // We are expected to be called with Offset 0 or pointing just past the table |
| 167 | // header, which is 12 bytes long for DWARF32. |
| 168 | if (Offset > 0) { |
| 169 | if (Offset < 12U) { |
| 170 | std::string Buffer; |
| 171 | raw_string_ostream Stream(Buffer); |
| 172 | Stream << format( |
| 173 | "Did not detect a valid range list table with base = 0x%x", Offset); |
| 174 | return make_error<StringError>(Stream.str(), inconvertibleErrorCode()); |
| 175 | } |
| 176 | Offset -= 12U; |
| 177 | } |
| 178 | llvm::DWARFDebugRnglistTable Table; |
| 179 | if (Error E = Table.extractHeaderAndOffsets(DA, &Offset)) |
Wolfgang Pieb | da71639 | 2018-05-18 20:35:13 +0000 | [diff] [blame] | 180 | return std::move(E); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 181 | return Table; |
| 182 | } |
| 183 | |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 184 | Error DWARFUnit::extractRangeList(uint32_t RangeListOffset, |
| 185 | DWARFDebugRangeList &RangeList) const { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 186 | // Require that compile unit is extracted. |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 187 | assert(!DieArray.empty()); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 188 | DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection, |
| 189 | isLittleEndian, getAddressByteSize()); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 190 | uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 191 | return RangeList.extract(RangesData, &ActualRangeListOffset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void DWARFUnit::clear() { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 195 | Abbrevs = nullptr; |
George Rimar | 2f95c8b | 2017-09-04 10:30:39 +0000 | [diff] [blame] | 196 | BaseAddr.reset(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 197 | RangeSectionBase = 0; |
| 198 | AddrOffsetSectionBase = 0; |
| 199 | clearDIEs(false); |
| 200 | DWO.reset(); |
| 201 | } |
| 202 | |
| 203 | const char *DWARFUnit::getCompilationDir() { |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 204 | return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 205 | } |
| 206 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 207 | void DWARFUnit::extractDIEsToVector( |
| 208 | bool AppendCUDie, bool AppendNonCUDies, |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 209 | std::vector<DWARFDebugInfoEntry> &Dies) const { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 210 | if (!AppendCUDie && !AppendNonCUDies) |
| 211 | return; |
| 212 | |
| 213 | // Set the offset to that of the first DIE and calculate the start of the |
| 214 | // next compilation unit header. |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 215 | uint32_t DIEOffset = getOffset() + getHeaderSize(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 216 | uint32_t NextCUOffset = getNextUnitOffset(); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 217 | DWARFDebugInfoEntry DIE; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 218 | DWARFDataExtractor DebugInfoData = getDebugInfoExtractor(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 219 | uint32_t Depth = 0; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 220 | bool IsCUDie = true; |
| 221 | |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 222 | while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset, |
| 223 | Depth)) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 224 | if (IsCUDie) { |
| 225 | if (AppendCUDie) |
| 226 | Dies.push_back(DIE); |
| 227 | if (!AppendNonCUDies) |
| 228 | break; |
| 229 | // The average bytes per DIE entry has been seen to be |
| 230 | // around 14-20 so let's pre-reserve the needed memory for |
| 231 | // our DIE entries accordingly. |
| 232 | Dies.reserve(Dies.size() + getDebugInfoSize() / 14); |
| 233 | IsCUDie = false; |
| 234 | } else { |
| 235 | Dies.push_back(DIE); |
| 236 | } |
| 237 | |
Alexey Samsonov | 8e4cf3b | 2014-04-29 17:12:42 +0000 | [diff] [blame] | 238 | if (const DWARFAbbreviationDeclaration *AbbrDecl = |
| 239 | DIE.getAbbreviationDeclarationPtr()) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 240 | // Normal DIE |
| 241 | if (AbbrDecl->hasChildren()) |
| 242 | ++Depth; |
| 243 | } else { |
| 244 | // NULL DIE. |
| 245 | if (Depth > 0) |
| 246 | --Depth; |
| 247 | if (Depth == 0) |
| 248 | break; // We are done with this compile unit! |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Give a little bit of info if we encounter corrupt DWARF (our offset |
| 253 | // should always terminate at or before the start of the next compilation |
| 254 | // unit header). |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 255 | if (DIEOffset > NextCUOffset) |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 256 | WithColor::warning() << format("DWARF compile unit extends beyond its " |
| 257 | "bounds cu 0x%8.8x at 0x%8.8x\n", |
| 258 | getOffset(), DIEOffset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) { |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 262 | if ((CUDieOnly && !DieArray.empty()) || |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 263 | DieArray.size() > 1) |
| 264 | return 0; // Already parsed. |
| 265 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 266 | bool HasCUDie = !DieArray.empty(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 267 | extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray); |
| 268 | |
| 269 | if (DieArray.empty()) |
| 270 | return 0; |
| 271 | |
| 272 | // If CU DIE was just parsed, copy several attribute values from it. |
| 273 | if (!HasCUDie) { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 274 | DWARFDie UnitDie = getUnitDIE(); |
Paul Robinson | 543c0e1 | 2018-05-22 17:27:31 +0000 | [diff] [blame] | 275 | if (Optional<uint64_t> DWOId = toUnsigned(UnitDie.find(DW_AT_GNU_dwo_id))) |
| 276 | Header.setDWOId(*DWOId); |
David Blaikie | 22dc447 | 2017-08-02 20:16:22 +0000 | [diff] [blame] | 277 | if (!isDWO) { |
| 278 | assert(AddrOffsetSectionBase == 0); |
| 279 | assert(RangeSectionBase == 0); |
| 280 | AddrOffsetSectionBase = |
| 281 | toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0); |
| 282 | RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0); |
| 283 | } |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 284 | |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 285 | // In general, in DWARF v5 and beyond we derive the start of the unit's |
| 286 | // contribution to the string offsets table from the unit DIE's |
| 287 | // DW_AT_str_offsets_base attribute. Split DWARF units do not use this |
| 288 | // attribute, so we assume that there is a contribution to the string |
| 289 | // offsets table starting at offset 0 of the debug_str_offsets.dwo section. |
| 290 | // In both cases we need to determine the format of the contribution, |
| 291 | // which may differ from the unit's format. |
| 292 | uint64_t StringOffsetsContributionBase = |
| 293 | isDWO ? 0 : toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0); |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 294 | auto IndexEntry = Header.getIndexEntry(); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 295 | if (IndexEntry) |
| 296 | if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS)) |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 297 | StringOffsetsContributionBase += C->Offset; |
| 298 | |
| 299 | DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, |
| 300 | isLittleEndian, 0); |
| 301 | if (isDWO) |
| 302 | StringOffsetsTableContribution = |
| 303 | determineStringOffsetsTableContributionDWO( |
| 304 | DA, StringOffsetsContributionBase); |
| 305 | else if (getVersion() >= 5) |
| 306 | StringOffsetsTableContribution = determineStringOffsetsTableContribution( |
| 307 | DA, StringOffsetsContributionBase); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 308 | |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 309 | // DWARF v5 uses the .debug_rnglists and .debug_rnglists.dwo sections to |
| 310 | // describe address ranges. |
| 311 | if (getVersion() >= 5) { |
| 312 | if (isDWO) |
| 313 | setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0); |
| 314 | else |
| 315 | setRangesSection(&Context.getDWARFObj().getRnglistsSection(), |
| 316 | toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0)); |
| 317 | // Parse the range list table header. Individual range lists are |
| 318 | // extracted lazily. |
| 319 | DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection, |
| 320 | isLittleEndian, 0); |
| 321 | if (auto TableOrError = |
| 322 | parseRngListTableHeader(RangesDA, RangeSectionBase)) |
| 323 | RngListTable = TableOrError.get(); |
| 324 | else |
| 325 | WithColor::error() << "parsing a range list table: " |
Wolfgang Pieb | 401b5ec | 2018-05-18 20:51:16 +0000 | [diff] [blame] | 326 | << toString(TableOrError.takeError()) |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 327 | << '\n'; |
| 328 | |
| 329 | // In a split dwarf unit, there is no DW_AT_rnglists_base attribute. |
| 330 | // Adjust RangeSectionBase to point past the table header. |
| 331 | if (isDWO && RngListTable) |
| 332 | RangeSectionBase = RngListTable->getHeaderSize(); |
| 333 | } |
| 334 | |
Alexey Samsonov | aa90998 | 2014-06-13 22:31:03 +0000 | [diff] [blame] | 335 | // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for |
| 336 | // skeleton CU DIE, so that DWARF users not aware of it are not broken. |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 337 | } |
| 338 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 339 | return DieArray.size(); |
| 340 | } |
| 341 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 342 | bool DWARFUnit::parseDWO() { |
David Blaikie | e438cff | 2016-04-22 22:50:56 +0000 | [diff] [blame] | 343 | if (isDWO) |
| 344 | return false; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 345 | if (DWO.get()) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 346 | return false; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 347 | DWARFDie UnitDie = getUnitDIE(); |
| 348 | if (!UnitDie) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 349 | return false; |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 350 | auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 351 | if (!DWOFileName) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 352 | return false; |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 353 | auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir)); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 354 | SmallString<16> AbsolutePath; |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 355 | if (sys::path::is_relative(*DWOFileName) && CompilationDir && |
| 356 | *CompilationDir) { |
| 357 | sys::path::append(AbsolutePath, *CompilationDir); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 358 | } |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 359 | sys::path::append(AbsolutePath, *DWOFileName); |
David Blaikie | 8d039d4 | 2017-05-20 03:32:49 +0000 | [diff] [blame] | 360 | auto DWOId = getDWOId(); |
| 361 | if (!DWOId) |
| 362 | return false; |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 363 | auto DWOContext = Context.getDWOContext(AbsolutePath); |
| 364 | if (!DWOContext) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 365 | return false; |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 366 | |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 367 | DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId); |
| 368 | if (!DWOCU) |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 369 | return false; |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 370 | DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 371 | // Share .debug_addr and .debug_ranges section with compile unit in .dwo |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 372 | DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 373 | if (getVersion() >= 5) { |
| 374 | DWO->setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0); |
| 375 | DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection, |
| 376 | isLittleEndian, 0); |
| 377 | if (auto TableOrError = parseRngListTableHeader(RangesDA, RangeSectionBase)) |
| 378 | DWO->RngListTable = TableOrError.get(); |
| 379 | else |
| 380 | WithColor::error() << "parsing a range list table: " |
Wolfgang Pieb | 401b5ec | 2018-05-18 20:51:16 +0000 | [diff] [blame] | 381 | << toString(TableOrError.takeError()) |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 382 | << '\n'; |
| 383 | if (DWO->RngListTable) |
| 384 | DWO->RangeSectionBase = DWO->RngListTable->getHeaderSize(); |
| 385 | } else { |
| 386 | auto DWORangesBase = UnitDie.getRangesBaseAttribute(); |
| 387 | DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0); |
| 388 | } |
| 389 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
| 393 | void DWARFUnit::clearDIEs(bool KeepCUDie) { |
| 394 | if (DieArray.size() > (unsigned)KeepCUDie) { |
Benjamin Kramer | 295cf4d | 2017-08-01 14:38:08 +0000 | [diff] [blame] | 395 | DieArray.resize((unsigned)KeepCUDie); |
| 396 | DieArray.shrink_to_fit(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 400 | Expected<DWARFAddressRangesVector> |
| 401 | DWARFUnit::findRnglistFromOffset(uint32_t Offset) { |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 402 | if (getVersion() <= 4) { |
| 403 | DWARFDebugRangeList RangeList; |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 404 | if (Error E = extractRangeList(Offset, RangeList)) |
| 405 | return std::move(E); |
| 406 | return RangeList.getAbsoluteRanges(getBaseAddress()); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 407 | } |
| 408 | if (RngListTable) { |
| 409 | DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection, |
| 410 | isLittleEndian, RngListTable->getAddrSize()); |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 411 | auto RangeListOrError = RngListTable->findRangeList(RangesData, Offset); |
| 412 | if (RangeListOrError) |
| 413 | return RangeListOrError.get().getAbsoluteRanges(getBaseAddress()); |
| 414 | return RangeListOrError.takeError(); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 415 | } |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 416 | |
| 417 | return make_error<StringError>("missing or invalid range list table", |
| 418 | inconvertibleErrorCode()); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 421 | Expected<DWARFAddressRangesVector> |
| 422 | DWARFUnit::findRnglistFromIndex(uint32_t Index) { |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 423 | if (auto Offset = getRnglistOffset(Index)) |
| 424 | return findRnglistFromOffset(*Offset + RangeSectionBase); |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 425 | |
| 426 | std::string Buffer; |
| 427 | raw_string_ostream Stream(Buffer); |
| 428 | if (RngListTable) |
| 429 | Stream << format("invalid range list table index %d", Index); |
| 430 | else |
| 431 | Stream << "missing or invalid range list table"; |
| 432 | return make_error<StringError>(Stream.str(), inconvertibleErrorCode()); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 435 | void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 436 | DWARFDie UnitDie = getUnitDIE(); |
| 437 | if (!UnitDie) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 438 | return; |
| 439 | // First, check if unit DIE describes address ranges for the whole unit. |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 440 | auto CUDIERangesOrError = UnitDie.getAddressRanges(); |
| 441 | if (CUDIERangesOrError) { |
| 442 | if (!CUDIERangesOrError.get().empty()) { |
| 443 | CURanges.insert(CURanges.end(), CUDIERangesOrError.get().begin(), |
| 444 | CUDIERangesOrError.get().end()); |
| 445 | return; |
| 446 | } |
| 447 | } else |
| 448 | WithColor::error() << "decoding address ranges: " |
| 449 | << toString(CUDIERangesOrError.takeError()) << '\n'; |
Alexey Samsonov | 84e2423 | 2014-04-18 20:30:27 +0000 | [diff] [blame] | 450 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 451 | // This function is usually called if there in no .debug_aranges section |
| 452 | // in order to produce a compile unit level set of address ranges that |
| 453 | // is accurate. If the DIEs weren't parsed, then we don't want all dies for |
| 454 | // all compile units to stay loaded when they weren't needed. So we can end |
| 455 | // up parsing the DWARF and then throwing them all away to keep memory usage |
| 456 | // down. |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 457 | const bool ClearDIEs = extractDIEsIfNeeded(false) > 1; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 458 | getUnitDIE().collectChildrenAddressRanges(CURanges); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 459 | |
| 460 | // Collect address ranges from DIEs in .dwo if necessary. |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 461 | bool DWOCreated = parseDWO(); |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 462 | if (DWO) |
| 463 | DWO->collectAddressRanges(CURanges); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 464 | if (DWOCreated) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 465 | DWO.reset(); |
| 466 | |
| 467 | // Keep memory down by clearing DIEs if this generate function |
| 468 | // caused them to be parsed. |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 469 | if (ClearDIEs) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 470 | clearDIEs(true); |
| 471 | } |
| 472 | |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame] | 473 | void DWARFUnit::updateAddressDieMap(DWARFDie Die) { |
| 474 | if (Die.isSubroutineDIE()) { |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 475 | auto DIERangesOrError = Die.getAddressRanges(); |
| 476 | if (DIERangesOrError) { |
| 477 | for (const auto &R : DIERangesOrError.get()) { |
| 478 | // Ignore 0-sized ranges. |
| 479 | if (R.LowPC == R.HighPC) |
| 480 | continue; |
| 481 | auto B = AddrDieMap.upper_bound(R.LowPC); |
| 482 | if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) { |
| 483 | // The range is a sub-range of existing ranges, we need to split the |
| 484 | // existing range. |
| 485 | if (R.HighPC < B->second.first) |
| 486 | AddrDieMap[R.HighPC] = B->second; |
| 487 | if (R.LowPC > B->first) |
| 488 | AddrDieMap[B->first].first = R.LowPC; |
| 489 | } |
| 490 | AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die); |
Chandler Carruth | 54a5ad3 | 2017-12-22 06:41:23 +0000 | [diff] [blame] | 491 | } |
Wolfgang Pieb | 61d8c8d | 2018-06-20 22:56:37 +0000 | [diff] [blame] | 492 | } else |
| 493 | llvm::consumeError(DIERangesOrError.takeError()); |
Chandler Carruth | 54a5ad3 | 2017-12-22 06:41:23 +0000 | [diff] [blame] | 494 | } |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame] | 495 | // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to |
| 496 | // simplify the logic to update AddrDieMap. The child's range will always |
| 497 | // be equal or smaller than the parent's range. With this assumption, when |
| 498 | // adding one range into the map, it will at most split a range into 3 |
| 499 | // sub-ranges. |
| 500 | for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling()) |
| 501 | updateAddressDieMap(Child); |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) { |
| 505 | extractDIEsIfNeeded(false); |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame] | 506 | if (AddrDieMap.empty()) |
| 507 | updateAddressDieMap(getUnitDIE()); |
| 508 | auto R = AddrDieMap.upper_bound(Address); |
| 509 | if (R == AddrDieMap.begin()) |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 510 | return DWARFDie(); |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame] | 511 | // upper_bound's previous item contains Address. |
| 512 | --R; |
| 513 | if (Address >= R->second.first) |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 514 | return DWARFDie(); |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame] | 515 | return R->second.second; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 518 | void |
| 519 | DWARFUnit::getInlinedChainForAddress(uint64_t Address, |
| 520 | SmallVectorImpl<DWARFDie> &InlinedChain) { |
Dehao Chen | db569ba | 2017-04-19 20:52:21 +0000 | [diff] [blame] | 521 | assert(InlinedChain.empty()); |
David Blaikie | 9a4f3cb | 2016-04-22 21:32:59 +0000 | [diff] [blame] | 522 | // Try to look for subprogram DIEs in the DWO file. |
| 523 | parseDWO(); |
Dehao Chen | db569ba | 2017-04-19 20:52:21 +0000 | [diff] [blame] | 524 | // First, find the subroutine that contains the given address (the leaf |
| 525 | // of inlined chain). |
| 526 | DWARFDie SubroutineDIE = |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 527 | (DWO ? DWO.get() : this)->getSubroutineForAddress(Address); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 528 | |
David Blaikie | aa537da | 2018-05-01 18:08:45 +0000 | [diff] [blame] | 529 | if (!SubroutineDIE) |
| 530 | return; |
| 531 | |
| 532 | while (!SubroutineDIE.isSubprogramDIE()) { |
| 533 | if (SubroutineDIE.getTag() == DW_TAG_inlined_subroutine) |
Dehao Chen | db569ba | 2017-04-19 20:52:21 +0000 | [diff] [blame] | 534 | InlinedChain.push_back(SubroutineDIE); |
| 535 | SubroutineDIE = SubroutineDIE.getParent(); |
| 536 | } |
David Blaikie | aa537da | 2018-05-01 18:08:45 +0000 | [diff] [blame] | 537 | InlinedChain.push_back(SubroutineDIE); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 538 | } |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 539 | |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 540 | const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context, |
| 541 | DWARFSectionKind Kind) { |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 542 | if (Kind == DW_SECT_INFO) |
| 543 | return Context.getCUIndex(); |
| 544 | assert(Kind == DW_SECT_TYPES); |
| 545 | return Context.getTUIndex(); |
| 546 | } |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 547 | |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 548 | DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) { |
| 549 | if (!Die) |
| 550 | return DWARFDie(); |
| 551 | const uint32_t Depth = Die->getDepth(); |
| 552 | // Unit DIEs always have a depth of zero and never have parents. |
| 553 | if (Depth == 0) |
| 554 | return DWARFDie(); |
| 555 | // Depth of 1 always means parent is the compile/type unit. |
| 556 | if (Depth == 1) |
| 557 | return getUnitDIE(); |
| 558 | // Look for previous DIE with a depth that is one less than the Die's depth. |
| 559 | const uint32_t ParentDepth = Depth - 1; |
| 560 | for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) { |
| 561 | if (DieArray[I].getDepth() == ParentDepth) |
| 562 | return DWARFDie(this, &DieArray[I]); |
| 563 | } |
| 564 | return DWARFDie(); |
| 565 | } |
| 566 | |
| 567 | DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) { |
| 568 | if (!Die) |
| 569 | return DWARFDie(); |
| 570 | uint32_t Depth = Die->getDepth(); |
| 571 | // Unit DIEs always have a depth of zero and never have siblings. |
| 572 | if (Depth == 0) |
| 573 | return DWARFDie(); |
| 574 | // NULL DIEs don't have siblings. |
| 575 | if (Die->getAbbreviationDeclarationPtr() == nullptr) |
| 576 | return DWARFDie(); |
Jonas Devlieghere | f63ee64 | 2017-10-25 21:56:41 +0000 | [diff] [blame] | 577 | |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 578 | // Find the next DIE whose depth is the same as the Die's depth. |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 579 | for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx; |
| 580 | ++I) { |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 581 | if (DieArray[I].getDepth() == Depth) |
| 582 | return DWARFDie(this, &DieArray[I]); |
| 583 | } |
| 584 | return DWARFDie(); |
| 585 | } |
David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 586 | |
Jonas Devlieghere | 3f27e57 | 2018-07-11 17:11:11 +0000 | [diff] [blame^] | 587 | DWARFDie DWARFUnit::getPreviousSibling(const DWARFDebugInfoEntry *Die) { |
| 588 | if (!Die) |
| 589 | return DWARFDie(); |
| 590 | uint32_t Depth = Die->getDepth(); |
| 591 | // Unit DIEs always have a depth of zero and never have siblings. |
| 592 | if (Depth == 0) |
| 593 | return DWARFDie(); |
| 594 | |
| 595 | // Find the previous DIE whose depth is the same as the Die's depth. |
| 596 | for (size_t I = getDIEIndex(Die) - 1; I >= 0; --I) { |
| 597 | if (DieArray[I].getDepth() == Depth - 1) |
| 598 | return DWARFDie(); |
| 599 | if (DieArray[I].getDepth() == Depth) |
| 600 | return DWARFDie(this, &DieArray[I]); |
| 601 | } |
| 602 | return DWARFDie(); |
| 603 | } |
| 604 | |
George Rimar | 0be860f | 2017-10-25 10:23:49 +0000 | [diff] [blame] | 605 | DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) { |
| 606 | if (!Die->hasChildren()) |
| 607 | return DWARFDie(); |
| 608 | |
| 609 | // We do not want access out of bounds when parsing corrupted debug data. |
| 610 | size_t I = getDIEIndex(Die) + 1; |
| 611 | if (I >= DieArray.size()) |
| 612 | return DWARFDie(); |
| 613 | return DWARFDie(this, &DieArray[I]); |
| 614 | } |
| 615 | |
Jonas Devlieghere | 3f27e57 | 2018-07-11 17:11:11 +0000 | [diff] [blame^] | 616 | DWARFDie DWARFUnit::getLastChild(const DWARFDebugInfoEntry *Die) { |
| 617 | if (!Die->hasChildren()) |
| 618 | return DWARFDie(); |
| 619 | |
| 620 | uint32_t Depth = Die->getDepth(); |
| 621 | for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx; |
| 622 | ++I) { |
| 623 | if (DieArray[I].getDepth() == Depth + 1 && |
| 624 | DieArray[I].getTag() == dwarf::DW_TAG_null) |
| 625 | return DWARFDie(this, &DieArray[I]); |
| 626 | assert(DieArray[I].getDepth() > Depth && "Not processing children?"); |
| 627 | } |
| 628 | return DWARFDie(); |
| 629 | } |
| 630 | |
David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 631 | const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const { |
| 632 | if (!Abbrevs) |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 633 | Abbrevs = Abbrev->getAbbreviationDeclarationSet(Header.getAbbrOffset()); |
David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 634 | return Abbrevs; |
| 635 | } |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 636 | |
Jonas Devlieghere | c111382 | 2018-05-21 19:36:54 +0000 | [diff] [blame] | 637 | llvm::Optional<BaseAddress> DWARFUnit::getBaseAddress() { |
| 638 | if (BaseAddr) |
| 639 | return BaseAddr; |
| 640 | |
| 641 | DWARFDie UnitDie = getUnitDIE(); |
| 642 | Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}); |
| 643 | if (Optional<uint64_t> Addr = toAddress(PC)) |
| 644 | BaseAddr = {*Addr, PC->getSectionIndex()}; |
| 645 | |
| 646 | return BaseAddr; |
| 647 | } |
| 648 | |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 649 | Optional<StrOffsetsContributionDescriptor> |
| 650 | StrOffsetsContributionDescriptor::validateContributionSize( |
| 651 | DWARFDataExtractor &DA) { |
| 652 | uint8_t EntrySize = getDwarfOffsetByteSize(); |
| 653 | // In order to ensure that we don't read a partial record at the end of |
| 654 | // the section we validate for a multiple of the entry size. |
| 655 | uint64_t ValidationSize = alignTo(Size, EntrySize); |
| 656 | // Guard against overflow. |
| 657 | if (ValidationSize >= Size) |
| 658 | if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize)) |
| 659 | return *this; |
| 660 | return Optional<StrOffsetsContributionDescriptor>(); |
| 661 | } |
| 662 | |
| 663 | // Look for a DWARF64-formatted contribution to the string offsets table |
| 664 | // starting at a given offset and record it in a descriptor. |
| 665 | static Optional<StrOffsetsContributionDescriptor> |
| 666 | parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) { |
| 667 | if (!DA.isValidOffsetForDataOfSize(Offset, 16)) |
| 668 | return Optional<StrOffsetsContributionDescriptor>(); |
| 669 | |
| 670 | if (DA.getU32(&Offset) != 0xffffffff) |
| 671 | return Optional<StrOffsetsContributionDescriptor>(); |
| 672 | |
| 673 | uint64_t Size = DA.getU64(&Offset); |
| 674 | uint8_t Version = DA.getU16(&Offset); |
| 675 | (void)DA.getU16(&Offset); // padding |
Wolfgang Pieb | f2b6915e | 2018-05-10 20:02:34 +0000 | [diff] [blame] | 676 | // The encoded length includes the 2-byte version field and the 2-byte |
| 677 | // padding, so we need to subtract them out when we populate the descriptor. |
| 678 | return StrOffsetsContributionDescriptor(Offset, Size - 4, Version, DWARF64); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 679 | //return Optional<StrOffsetsContributionDescriptor>(Descriptor); |
| 680 | } |
| 681 | |
| 682 | // Look for a DWARF32-formatted contribution to the string offsets table |
| 683 | // starting at a given offset and record it in a descriptor. |
| 684 | static Optional<StrOffsetsContributionDescriptor> |
| 685 | parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) { |
| 686 | if (!DA.isValidOffsetForDataOfSize(Offset, 8)) |
| 687 | return Optional<StrOffsetsContributionDescriptor>(); |
| 688 | uint32_t ContributionSize = DA.getU32(&Offset); |
| 689 | if (ContributionSize >= 0xfffffff0) |
| 690 | return Optional<StrOffsetsContributionDescriptor>(); |
| 691 | uint8_t Version = DA.getU16(&Offset); |
| 692 | (void)DA.getU16(&Offset); // padding |
Wolfgang Pieb | f2b6915e | 2018-05-10 20:02:34 +0000 | [diff] [blame] | 693 | // The encoded length includes the 2-byte version field and the 2-byte |
| 694 | // padding, so we need to subtract them out when we populate the descriptor. |
| 695 | return StrOffsetsContributionDescriptor(Offset, ContributionSize - 4, Version, |
| 696 | DWARF32); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 697 | //return Optional<StrOffsetsContributionDescriptor>(Descriptor); |
| 698 | } |
| 699 | |
| 700 | Optional<StrOffsetsContributionDescriptor> |
| 701 | DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA, |
| 702 | uint64_t Offset) { |
| 703 | Optional<StrOffsetsContributionDescriptor> Descriptor; |
| 704 | // Attempt to find a DWARF64 contribution 16 bytes before the base. |
| 705 | if (Offset >= 16) |
| 706 | Descriptor = |
| 707 | parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset - 16); |
| 708 | // Try to find a DWARF32 contribution 8 bytes before the base. |
| 709 | if (!Descriptor && Offset >= 8) |
| 710 | Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset - 8); |
| 711 | return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor; |
| 712 | } |
| 713 | |
| 714 | Optional<StrOffsetsContributionDescriptor> |
| 715 | DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA, |
| 716 | uint64_t Offset) { |
| 717 | if (getVersion() >= 5) { |
| 718 | // Look for a valid contribution at the given offset. |
| 719 | auto Descriptor = |
| 720 | parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset); |
| 721 | if (!Descriptor) |
| 722 | Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset); |
| 723 | return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor; |
| 724 | } |
| 725 | // Prior to DWARF v5, we derive the contribution size from the |
| 726 | // index table (in a package file). In a .dwo file it is simply |
| 727 | // the length of the string offsets section. |
| 728 | uint64_t Size = 0; |
Paul Robinson | 5f53f07 | 2018-05-14 20:32:31 +0000 | [diff] [blame] | 729 | auto IndexEntry = Header.getIndexEntry(); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 730 | if (!IndexEntry) |
| 731 | Size = StringOffsetSection.Data.size(); |
| 732 | else if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS)) |
| 733 | Size = C->Length; |
| 734 | // Return a descriptor with the given offset as base, version 4 and |
| 735 | // DWARF32 format. |
| 736 | //return Optional<StrOffsetsContributionDescriptor>( |
| 737 | //StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32)); |
| 738 | return StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32); |
| 739 | } |