blob: 081163ba61f37c4d1df825d3f9d37d436c105f78 [file] [log] [blame]
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +00001//===- DWARFUnit.cpp ------------------------------------------------------===//
David Blaikie07e22442013-09-23 22:44:40 +00002//
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 Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "llvm/ADT/SmallString.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000012#include "llvm/ADT/StringRef.h"
13#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Paul Robinson143eaea2018-08-01 20:43:47 +000014#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
Zachary Turner82af9432015-01-30 18:07:45 +000015#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000016#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000017#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
Wolfgang Piebad605592018-05-18 20:12:54 +000018#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000019#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Clayton97d22182017-01-13 21:08:18 +000020#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Paul Robinson143eaea2018-08-01 20:43:47 +000021#include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000022#include "llvm/Support/DataExtractor.h"
Victor Leschukcba595d2018-08-20 09:59:08 +000023#include "llvm/Support/Errc.h"
David Blaikie07e22442013-09-23 22:44:40 +000024#include "llvm/Support/Path.h"
Jonas Devlieghere84e99262018-04-14 22:07:23 +000025#include "llvm/Support/WithColor.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000026#include <algorithm>
27#include <cassert>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000028#include <cstddef>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000029#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000030#include <cstdio>
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000031#include <utility>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000032#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000033
Eugene Zelenko28db7e62017-03-01 01:14:23 +000034using namespace llvm;
David Blaikie07e22442013-09-23 22:44:40 +000035using namespace dwarf;
36
Paul Robinson11307fa2018-08-01 20:49:44 +000037void DWARFUnitVector::addUnitsForSection(DWARFContext &C,
38 const DWARFSection &Section,
39 DWARFSectionKind SectionKind) {
Rafael Espindolac398e672017-07-19 22:27:28 +000040 const DWARFObject &D = C.getDWARFObj();
Paul Robinson7f330942018-08-01 20:46:46 +000041 addUnitsImpl(C, D, Section, C.getDebugAbbrev(), &D.getRangeSection(),
Wolfgang Pieb6214c112018-10-19 19:23:16 +000042 &D.getLocSection(), D.getStringSection(),
43 D.getStringOffsetSection(), &D.getAddrSection(),
44 D.getLineSection(), D.isLittleEndian(), false, false,
45 SectionKind);
Frederic Riss6005dbd2014-10-06 03:36:18 +000046}
47
Paul Robinson11307fa2018-08-01 20:49:44 +000048void DWARFUnitVector::addUnitsForDWOSection(DWARFContext &C,
49 const DWARFSection &DWOSection,
50 DWARFSectionKind SectionKind,
51 bool Lazy) {
Rafael Espindolac398e672017-07-19 22:27:28 +000052 const DWARFObject &D = C.getDWARFObj();
Paul Robinson7f330942018-08-01 20:46:46 +000053 addUnitsImpl(C, D, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(),
Wolfgang Pieb6214c112018-10-19 19:23:16 +000054 &D.getLocDWOSection(), D.getStringDWOSection(),
55 D.getStringOffsetDWOSection(), &D.getAddrSection(),
56 D.getLineDWOSection(), C.isLittleEndian(), true, Lazy,
57 SectionKind);
Paul Robinson143eaea2018-08-01 20:43:47 +000058}
59
Paul Robinson11307fa2018-08-01 20:49:44 +000060void DWARFUnitVector::addUnitsImpl(
Paul Robinson7f330942018-08-01 20:46:46 +000061 DWARFContext &Context, const DWARFObject &Obj, const DWARFSection &Section,
Wolfgang Pieb6214c112018-10-19 19:23:16 +000062 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
63 const DWARFSection *LocSection, StringRef SS, const DWARFSection &SOS,
64 const DWARFSection *AOS, const DWARFSection &LS, bool LE, bool IsDWO,
65 bool Lazy, DWARFSectionKind SectionKind) {
Paul Robinson143eaea2018-08-01 20:43:47 +000066 DWARFDataExtractor Data(Obj, Section, LE, 0);
67 // Lazy initialization of Parser, now that we have all section info.
68 if (!Parser) {
Paul Robinson7f330942018-08-01 20:46:46 +000069 Parser = [=, &Context, &Obj, &Section, &SOS, &LS](
Paul Robinson2c25f342018-08-01 20:54:11 +000070 uint32_t Offset, DWARFSectionKind SectionKind,
Paul Robinson7f330942018-08-01 20:46:46 +000071 const DWARFSection *CurSection) -> std::unique_ptr<DWARFUnit> {
72 const DWARFSection &InfoSection = CurSection ? *CurSection : Section;
73 DWARFDataExtractor Data(Obj, InfoSection, LE, 0);
Paul Robinson143eaea2018-08-01 20:43:47 +000074 if (!Data.isValidOffset(Offset))
75 return nullptr;
Paul Robinson2c25f342018-08-01 20:54:11 +000076 const DWARFUnitIndex *Index = nullptr;
77 if (IsDWO)
78 Index = &getDWARFUnitIndex(Context, SectionKind);
Paul Robinson143eaea2018-08-01 20:43:47 +000079 DWARFUnitHeader Header;
80 if (!Header.extract(Context, Data, &Offset, SectionKind, Index))
81 return nullptr;
82 std::unique_ptr<DWARFUnit> U;
83 if (Header.isTypeUnit())
Paul Robinson7f330942018-08-01 20:46:46 +000084 U = llvm::make_unique<DWARFTypeUnit>(Context, InfoSection, Header, DA,
Wolfgang Pieb6214c112018-10-19 19:23:16 +000085 RS, LocSection, SS, SOS, AOS, LS,
86 LE, IsDWO, *this);
Paul Robinson143eaea2018-08-01 20:43:47 +000087 else
Paul Robinson7f330942018-08-01 20:46:46 +000088 U = llvm::make_unique<DWARFCompileUnit>(Context, InfoSection, Header,
Wolfgang Pieb6214c112018-10-19 19:23:16 +000089 DA, RS, LocSection, SS, SOS,
90 AOS, LS, LE, IsDWO, *this);
Paul Robinson143eaea2018-08-01 20:43:47 +000091 return U;
92 };
93 }
94 if (Lazy)
95 return;
Paul Robinson2c25f342018-08-01 20:54:11 +000096 // Find a reasonable insertion point within the vector. We skip over
97 // (a) units from a different section, (b) units from the same section
98 // but with lower offset-within-section. This keeps units in order
99 // within a section, although not necessarily within the object file,
100 // even if we do lazy parsing.
Paul Robinson143eaea2018-08-01 20:43:47 +0000101 auto I = this->begin();
102 uint32_t Offset = 0;
103 while (Data.isValidOffset(Offset)) {
Paul Robinson2c25f342018-08-01 20:54:11 +0000104 if (I != this->end() &&
105 (&(*I)->getInfoSection() != &Section || (*I)->getOffset() == Offset)) {
Paul Robinson143eaea2018-08-01 20:43:47 +0000106 ++I;
107 continue;
108 }
Paul Robinson2c25f342018-08-01 20:54:11 +0000109 auto U = Parser(Offset, SectionKind, &Section);
110 // If parsing failed, we're done with this section.
Paul Robinson143eaea2018-08-01 20:43:47 +0000111 if (!U)
112 break;
113 Offset = U->getNextUnitOffset();
114 I = std::next(this->insert(I, std::move(U)));
115 }
Paul Robinson143eaea2018-08-01 20:43:47 +0000116}
117
Jonas Devlieghere7ef2c202018-09-21 07:49:29 +0000118DWARFUnit *DWARFUnitVector::addUnit(std::unique_ptr<DWARFUnit> Unit) {
119 auto I = std::upper_bound(begin(), end(), Unit,
120 [](const std::unique_ptr<DWARFUnit> &LHS,
121 const std::unique_ptr<DWARFUnit> &RHS) {
122 return LHS->getOffset() < RHS->getOffset();
123 });
124 return this->insert(I, std::move(Unit))->get();
125}
126
Paul Robinson11307fa2018-08-01 20:49:44 +0000127DWARFUnit *DWARFUnitVector::getUnitForOffset(uint32_t Offset) const {
David Blaikie4ec5a912018-09-11 02:04:45 +0000128 auto end = begin() + getNumInfoUnits();
129 auto *CU =
130 std::upper_bound(begin(), end, Offset,
131 [](uint32_t LHS, const std::unique_ptr<DWARFUnit> &RHS) {
132 return LHS < RHS->getNextUnitOffset();
133 });
134 if (CU != end && (*CU)->getOffset() <= Offset)
Paul Robinson143eaea2018-08-01 20:43:47 +0000135 return CU->get();
136 return nullptr;
137}
138
139DWARFUnit *
Paul Robinson11307fa2018-08-01 20:49:44 +0000140DWARFUnitVector::getUnitForIndexEntry(const DWARFUnitIndex::Entry &E) {
Paul Robinson143eaea2018-08-01 20:43:47 +0000141 const auto *CUOff = E.getOffset(DW_SECT_INFO);
142 if (!CUOff)
143 return nullptr;
144
145 auto Offset = CUOff->Offset;
David Blaikie4ec5a912018-09-11 02:04:45 +0000146 auto end = begin() + getNumInfoUnits();
Paul Robinson143eaea2018-08-01 20:43:47 +0000147
David Blaikie4ec5a912018-09-11 02:04:45 +0000148 auto *CU =
149 std::upper_bound(begin(), end, CUOff->Offset,
150 [](uint32_t LHS, const std::unique_ptr<DWARFUnit> &RHS) {
151 return LHS < RHS->getNextUnitOffset();
152 });
153 if (CU != end && (*CU)->getOffset() <= Offset)
Paul Robinson143eaea2018-08-01 20:43:47 +0000154 return CU->get();
155
156 if (!Parser)
157 return nullptr;
158
Paul Robinson2c25f342018-08-01 20:54:11 +0000159 auto U = Parser(Offset, DW_SECT_INFO, nullptr);
Paul Robinson143eaea2018-08-01 20:43:47 +0000160 if (!U)
161 U = nullptr;
162
163 auto *NewCU = U.get();
164 this->insert(CU, std::move(U));
David Blaikie4ec5a912018-09-11 02:04:45 +0000165 ++NumInfoUnits;
Paul Robinson143eaea2018-08-01 20:43:47 +0000166 return NewCU;
Frederic Riss6005dbd2014-10-06 03:36:18 +0000167}
168
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +0000169DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
Wolfgang Pieb6214c112018-10-19 19:23:16 +0000170 const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA,
171 const DWARFSection *RS, const DWARFSection *LocSection,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000172 StringRef SS, const DWARFSection &SOS,
Paul Robinsond0c89f82018-01-29 22:02:56 +0000173 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
Paul Robinson11307fa2018-08-01 20:49:44 +0000174 bool IsDWO, const DWARFUnitVector &UnitVector)
Paul Robinson5f53f072018-05-14 20:32:31 +0000175 : Context(DC), InfoSection(Section), Header(Header), Abbrev(DA),
Wolfgang Pieb6214c112018-10-19 19:23:16 +0000176 RangeSection(RS), LocSection(LocSection), LineSection(LS),
177 StringSection(SS), StringOffsetSection(SOS), AddrOffsetSection(AOS),
178 isLittleEndian(LE), isDWO(IsDWO), UnitVector(UnitVector) {
David Blaikie07e22442013-09-23 22:44:40 +0000179 clear();
Wolfgang Pieb6214c112018-10-19 19:23:16 +0000180 // For split DWARF we only need to keep track of the location list section's
181 // data (no relocations), and if we are reading a package file, we need to
182 // adjust the location list data based on the index entries.
183 if (IsDWO) {
184 LocSectionData = LocSection->Data;
185 if (auto *IndexEntry = Header.getIndexEntry())
186 if (const auto *C = IndexEntry->getOffset(DW_SECT_LOC))
187 LocSectionData = LocSectionData.substr(C->Offset, C->Length);
188 }
David Blaikie07e22442013-09-23 22:44:40 +0000189}
190
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000191DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +0000192
Rafael Espindolac398e672017-07-19 22:27:28 +0000193DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const {
194 return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian,
195 getAddressByteSize());
196}
197
David Blaikie161dd3c2018-10-20 06:02:15 +0000198Optional<SectionedAddress>
199DWARFUnit::getAddrOffsetSectionItem(uint32_t Index) const {
Paul Robinson75c068c2017-06-26 18:43:01 +0000200 uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize();
201 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize())
David Blaikie161dd3c2018-10-20 06:02:15 +0000202 return None;
Rafael Espindolac398e672017-07-19 22:27:28 +0000203 DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection,
204 isLittleEndian, getAddressByteSize());
David Blaikie161dd3c2018-10-20 06:02:15 +0000205 uint64_t Section;
206 uint64_t Address = DA.getRelocatedAddress(&Offset, &Section);
207 return {{Address, Section}};
David Blaikie07e22442013-09-23 22:44:40 +0000208}
209
210bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000211 uint64_t &Result) const {
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000212 if (!StringOffsetsTableContribution)
213 return false;
214 unsigned ItemSize = getDwarfStringOffsetsByteSize();
215 uint32_t Offset = getStringOffsetsBase() + Index * ItemSize;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000216 if (StringOffsetSection.Data.size() < Offset + ItemSize)
David Blaikie07e22442013-09-23 22:44:40 +0000217 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +0000218 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
219 isLittleEndian, 0);
Paul Robinson17536b92017-06-29 16:52:08 +0000220 Result = DA.getRelocatedValue(ItemSize, &Offset);
David Blaikie07e22442013-09-23 22:44:40 +0000221 return true;
222}
223
Paul Robinson5f53f072018-05-14 20:32:31 +0000224bool DWARFUnitHeader::extract(DWARFContext &Context,
225 const DWARFDataExtractor &debug_info,
226 uint32_t *offset_ptr,
227 DWARFSectionKind SectionKind,
228 const DWARFUnitIndex *Index) {
229 Offset = *offset_ptr;
230 IndexEntry = Index ? Index->getFromOffset(*offset_ptr) : nullptr;
David Blaikie07e22442013-09-23 22:44:40 +0000231 Length = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000232 // FIXME: Support DWARF64.
Paul Robinson5f53f072018-05-14 20:32:31 +0000233 unsigned SizeOfLength = 4;
Paul Robinson75c068c2017-06-26 18:43:01 +0000234 FormParams.Format = DWARF32;
235 FormParams.Version = debug_info.getU16(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000236 if (FormParams.Version >= 5) {
Paul Robinsoncddd6042017-02-28 20:24:55 +0000237 UnitType = debug_info.getU8(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000238 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000239 AbbrOffset = debug_info.getU32(offset_ptr);
240 } else {
Rafael Espindolac51dc902018-03-21 21:31:25 +0000241 AbbrOffset = debug_info.getRelocatedValue(4, offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000242 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinson5f53f072018-05-14 20:32:31 +0000243 // Fake a unit type based on the section type. This isn't perfect,
244 // but distinguishing compile and type units is generally enough.
245 if (SectionKind == DW_SECT_TYPES)
246 UnitType = DW_UT_type;
247 else
248 UnitType = DW_UT_compile;
Paul Robinsoncddd6042017-02-28 20:24:55 +0000249 }
David Blaikie82641be2015-11-17 00:39:55 +0000250 if (IndexEntry) {
251 if (AbbrOffset)
252 return false;
253 auto *UnitContrib = IndexEntry->getOffset();
254 if (!UnitContrib || UnitContrib->Length != (Length + 4))
255 return false;
256 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
257 if (!AbbrEntry)
258 return false;
259 AbbrOffset = AbbrEntry->Offset;
260 }
Paul Robinson5f53f072018-05-14 20:32:31 +0000261 if (isTypeUnit()) {
262 TypeHash = debug_info.getU64(offset_ptr);
263 TypeOffset = debug_info.getU32(offset_ptr);
Paul Robinson543c0e12018-05-22 17:27:31 +0000264 } else if (UnitType == DW_UT_split_compile || UnitType == DW_UT_skeleton)
265 DWOId = debug_info.getU64(offset_ptr);
266
267 // Header fields all parsed, capture the size of this unit header.
268 assert(*offset_ptr - Offset <= 255 && "unexpected header size");
269 Size = uint8_t(*offset_ptr - Offset);
270
271 // Type offset is unit-relative; should be after the header and before
272 // the end of the current unit.
273 bool TypeOffsetOK =
274 !isTypeUnit()
275 ? true
276 : TypeOffset >= Size && TypeOffset < getLength() + SizeOfLength;
Alexey Samsonov7682f812014-04-24 22:51:03 +0000277 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
Paul Robinson75c068c2017-06-26 18:43:01 +0000278 bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
279 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000280
Paul Robinson5f53f072018-05-14 20:32:31 +0000281 if (!LengthOK || !VersionOK || !AddrSizeOK || !TypeOffsetOK)
David Blaikie07e22442013-09-23 22:44:40 +0000282 return false;
283
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000284 // Keep track of the highest DWARF version we encounter across all units.
Paul Robinson75c068c2017-06-26 18:43:01 +0000285 Context.setMaxVersionIfGreater(getVersion());
David Blaikie485e01b2017-09-19 15:13:55 +0000286 return true;
David Blaikie07e22442013-09-23 22:44:40 +0000287}
288
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000289// Parse the rangelist table header, including the optional array of offsets
Wolfgang Piebad605592018-05-18 20:12:54 +0000290// following it (DWARF v5 and later).
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000291static Expected<DWARFDebugRnglistTable>
292parseRngListTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
Wolfgang Piebad605592018-05-18 20:12:54 +0000293 // TODO: Support DWARF64
294 // We are expected to be called with Offset 0 or pointing just past the table
295 // header, which is 12 bytes long for DWARF32.
296 if (Offset > 0) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000297 if (Offset < 12U)
298 return createStringError(errc::invalid_argument, "Did not detect a valid"
299 " range list table with base = 0x%" PRIu32,
300 Offset);
Wolfgang Piebad605592018-05-18 20:12:54 +0000301 Offset -= 12U;
302 }
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000303 llvm::DWARFDebugRnglistTable Table;
Wolfgang Piebad605592018-05-18 20:12:54 +0000304 if (Error E = Table.extractHeaderAndOffsets(DA, &Offset))
Wolfgang Piebda716392018-05-18 20:35:13 +0000305 return std::move(E);
Wolfgang Piebad605592018-05-18 20:12:54 +0000306 return Table;
307}
308
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000309Error DWARFUnit::extractRangeList(uint32_t RangeListOffset,
310 DWARFDebugRangeList &RangeList) const {
311 // Require that compile unit is extracted.
312 assert(!DieArray.empty());
313 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
314 isLittleEndian, getAddressByteSize());
315 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
316 return RangeList.extract(RangesData, &ActualRangeListOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000317}
318
319void DWARFUnit::clear() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000320 Abbrevs = nullptr;
George Rimar2f95c8b2017-09-04 10:30:39 +0000321 BaseAddr.reset();
David Blaikie07e22442013-09-23 22:44:40 +0000322 RangeSectionBase = 0;
323 AddrOffsetSectionBase = 0;
324 clearDIEs(false);
325 DWO.reset();
326}
327
328const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000329 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000330}
331
David Blaikie07e22442013-09-23 22:44:40 +0000332void DWARFUnit::extractDIEsToVector(
333 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000334 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000335 if (!AppendCUDie && !AppendNonCUDies)
336 return;
337
338 // Set the offset to that of the first DIE and calculate the start of the
339 // next compilation unit header.
Paul Robinson5f53f072018-05-14 20:32:31 +0000340 uint32_t DIEOffset = getOffset() + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000341 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000342 DWARFDebugInfoEntry DIE;
Paul Robinson17536b92017-06-29 16:52:08 +0000343 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000344 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000345 bool IsCUDie = true;
346
Greg Clayton78a07bf2016-12-21 21:37:06 +0000347 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
348 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000349 if (IsCUDie) {
350 if (AppendCUDie)
351 Dies.push_back(DIE);
352 if (!AppendNonCUDies)
353 break;
354 // The average bytes per DIE entry has been seen to be
355 // around 14-20 so let's pre-reserve the needed memory for
356 // our DIE entries accordingly.
357 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
358 IsCUDie = false;
359 } else {
360 Dies.push_back(DIE);
361 }
362
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000363 if (const DWARFAbbreviationDeclaration *AbbrDecl =
364 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000365 // Normal DIE
366 if (AbbrDecl->hasChildren())
367 ++Depth;
368 } else {
369 // NULL DIE.
370 if (Depth > 0)
371 --Depth;
372 if (Depth == 0)
373 break; // We are done with this compile unit!
374 }
375 }
376
377 // Give a little bit of info if we encounter corrupt DWARF (our offset
378 // should always terminate at or before the start of the next compilation
379 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000380 if (DIEOffset > NextCUOffset)
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000381 WithColor::warning() << format("DWARF compile unit extends beyond its "
382 "bounds cu 0x%8.8x at 0x%8.8x\n",
383 getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000384}
385
386size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000387 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000388 DieArray.size() > 1)
389 return 0; // Already parsed.
390
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000391 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000392 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
393
394 if (DieArray.empty())
395 return 0;
396
397 // If CU DIE was just parsed, copy several attribute values from it.
398 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000399 DWARFDie UnitDie = getUnitDIE();
Paul Robinson543c0e12018-05-22 17:27:31 +0000400 if (Optional<uint64_t> DWOId = toUnsigned(UnitDie.find(DW_AT_GNU_dwo_id)))
401 Header.setDWOId(*DWOId);
David Blaikie22dc4472017-08-02 20:16:22 +0000402 if (!isDWO) {
403 assert(AddrOffsetSectionBase == 0);
404 assert(RangeSectionBase == 0);
David Blaikie161dd3c2018-10-20 06:02:15 +0000405 AddrOffsetSectionBase = toSectionOffset(UnitDie.find(DW_AT_addr_base), 0);
406 if (!AddrOffsetSectionBase)
407 AddrOffsetSectionBase =
408 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
David Blaikie22dc4472017-08-02 20:16:22 +0000409 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
410 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000411
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000412 // In general, in DWARF v5 and beyond we derive the start of the unit's
413 // contribution to the string offsets table from the unit DIE's
414 // DW_AT_str_offsets_base attribute. Split DWARF units do not use this
415 // attribute, so we assume that there is a contribution to the string
416 // offsets table starting at offset 0 of the debug_str_offsets.dwo section.
417 // In both cases we need to determine the format of the contribution,
418 // which may differ from the unit's format.
419 uint64_t StringOffsetsContributionBase =
420 isDWO ? 0 : toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
Paul Robinson5f53f072018-05-14 20:32:31 +0000421 auto IndexEntry = Header.getIndexEntry();
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000422 if (IndexEntry)
423 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000424 StringOffsetsContributionBase += C->Offset;
425
426 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
427 isLittleEndian, 0);
428 if (isDWO)
429 StringOffsetsTableContribution =
430 determineStringOffsetsTableContributionDWO(
431 DA, StringOffsetsContributionBase);
432 else if (getVersion() >= 5)
433 StringOffsetsTableContribution = determineStringOffsetsTableContribution(
434 DA, StringOffsetsContributionBase);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000435
Wolfgang Piebad605592018-05-18 20:12:54 +0000436 // DWARF v5 uses the .debug_rnglists and .debug_rnglists.dwo sections to
437 // describe address ranges.
438 if (getVersion() >= 5) {
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000439 if (isDWO)
Wolfgang Piebad605592018-05-18 20:12:54 +0000440 setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0);
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000441 else
Wolfgang Piebad605592018-05-18 20:12:54 +0000442 setRangesSection(&Context.getDWARFObj().getRnglistsSection(),
443 toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0));
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000444 if (RangeSection->Data.size()) {
445 // Parse the range list table header. Individual range lists are
446 // extracted lazily.
447 DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection,
448 isLittleEndian, 0);
449 if (auto TableOrError =
450 parseRngListTableHeader(RangesDA, RangeSectionBase))
451 RngListTable = TableOrError.get();
452 else
453 WithColor::error() << "parsing a range list table: "
454 << toString(TableOrError.takeError())
455 << '\n';
456
457 // In a split dwarf unit, there is no DW_AT_rnglists_base attribute.
458 // Adjust RangeSectionBase to point past the table header.
459 if (isDWO && RngListTable)
460 RangeSectionBase = RngListTable->getHeaderSize();
Wolfgang Pieb1d56b4a2018-07-26 01:12:41 +0000461 }
Wolfgang Piebad605592018-05-18 20:12:54 +0000462 }
463
Alexey Samsonovaa909982014-06-13 22:31:03 +0000464 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
465 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000466 }
467
David Blaikie07e22442013-09-23 22:44:40 +0000468 return DieArray.size();
469}
470
David Blaikie07e22442013-09-23 22:44:40 +0000471bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000472 if (isDWO)
473 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000474 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000475 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000476 DWARFDie UnitDie = getUnitDIE();
477 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000478 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000479 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000480 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000481 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000482 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000483 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000484 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
485 *CompilationDir) {
486 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000487 }
Greg Clayton97d22182017-01-13 21:08:18 +0000488 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000489 auto DWOId = getDWOId();
490 if (!DWOId)
491 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000492 auto DWOContext = Context.getDWOContext(AbsolutePath);
493 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000494 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000495
David Blaikie15d85fc2017-05-23 06:48:53 +0000496 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
497 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000498 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000499 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000500 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000501 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Wolfgang Piebad605592018-05-18 20:12:54 +0000502 if (getVersion() >= 5) {
503 DWO->setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0);
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000504 DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection,
505 isLittleEndian, 0);
506 if (auto TableOrError = parseRngListTableHeader(RangesDA, RangeSectionBase))
507 DWO->RngListTable = TableOrError.get();
508 else
509 WithColor::error() << "parsing a range list table: "
510 << toString(TableOrError.takeError())
511 << '\n';
512 if (DWO->RngListTable)
513 DWO->RangeSectionBase = DWO->RngListTable->getHeaderSize();
Wolfgang Piebad605592018-05-18 20:12:54 +0000514 } else {
515 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
516 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
517 }
518
David Blaikie07e22442013-09-23 22:44:40 +0000519 return true;
520}
521
522void DWARFUnit::clearDIEs(bool KeepCUDie) {
523 if (DieArray.size() > (unsigned)KeepCUDie) {
Benjamin Kramer295cf4d2017-08-01 14:38:08 +0000524 DieArray.resize((unsigned)KeepCUDie);
525 DieArray.shrink_to_fit();
David Blaikie07e22442013-09-23 22:44:40 +0000526 }
527}
528
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000529Expected<DWARFAddressRangesVector>
530DWARFUnit::findRnglistFromOffset(uint32_t Offset) {
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +0000531 if (getVersion() <= 4) {
532 DWARFDebugRangeList RangeList;
533 if (Error E = extractRangeList(Offset, RangeList))
534 return std::move(E);
535 return RangeList.getAbsoluteRanges(getBaseAddress());
536 }
Wolfgang Piebad605592018-05-18 20:12:54 +0000537 if (RngListTable) {
538 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
539 isLittleEndian, RngListTable->getAddrSize());
Wolfgang Pieb439801b2018-07-23 22:37:17 +0000540 auto RangeListOrError = RngListTable->findList(RangesData, Offset);
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000541 if (RangeListOrError)
542 return RangeListOrError.get().getAbsoluteRanges(getBaseAddress());
543 return RangeListOrError.takeError();
Wolfgang Piebad605592018-05-18 20:12:54 +0000544 }
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000545
Victor Leschukcba595d2018-08-20 09:59:08 +0000546 return createStringError(errc::invalid_argument,
547 "missing or invalid range list table");
Wolfgang Piebad605592018-05-18 20:12:54 +0000548}
549
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000550Expected<DWARFAddressRangesVector>
551DWARFUnit::findRnglistFromIndex(uint32_t Index) {
Wolfgang Piebad605592018-05-18 20:12:54 +0000552 if (auto Offset = getRnglistOffset(Index))
553 return findRnglistFromOffset(*Offset + RangeSectionBase);
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000554
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000555 if (RngListTable)
Victor Leschukcba595d2018-08-20 09:59:08 +0000556 return createStringError(errc::invalid_argument,
557 "invalid range list table index %d", Index);
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000558 else
Victor Leschukcba595d2018-08-20 09:59:08 +0000559 return createStringError(errc::invalid_argument,
560 "missing or invalid range list table");
Wolfgang Piebad605592018-05-18 20:12:54 +0000561}
562
Alexey Samsonov762343d2014-04-18 17:25:46 +0000563void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000564 DWARFDie UnitDie = getUnitDIE();
565 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000566 return;
567 // First, check if unit DIE describes address ranges for the whole unit.
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000568 auto CUDIERangesOrError = UnitDie.getAddressRanges();
569 if (CUDIERangesOrError) {
570 if (!CUDIERangesOrError.get().empty()) {
571 CURanges.insert(CURanges.end(), CUDIERangesOrError.get().begin(),
572 CUDIERangesOrError.get().end());
573 return;
574 }
575 } else
576 WithColor::error() << "decoding address ranges: "
577 << toString(CUDIERangesOrError.takeError()) << '\n';
Alexey Samsonov84e24232014-04-18 20:30:27 +0000578
David Blaikie07e22442013-09-23 22:44:40 +0000579 // This function is usually called if there in no .debug_aranges section
580 // in order to produce a compile unit level set of address ranges that
581 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
582 // all compile units to stay loaded when they weren't needed. So we can end
583 // up parsing the DWARF and then throwing them all away to keep memory usage
584 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000585 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000586 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000587
588 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000589 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000590 if (DWO)
591 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000592 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000593 DWO.reset();
594
595 // Keep memory down by clearing DIEs if this generate function
596 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000597 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000598 clearDIEs(true);
599}
600
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000601void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
602 if (Die.isSubroutineDIE()) {
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000603 auto DIERangesOrError = Die.getAddressRanges();
604 if (DIERangesOrError) {
605 for (const auto &R : DIERangesOrError.get()) {
606 // Ignore 0-sized ranges.
607 if (R.LowPC == R.HighPC)
608 continue;
609 auto B = AddrDieMap.upper_bound(R.LowPC);
610 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
611 // The range is a sub-range of existing ranges, we need to split the
612 // existing range.
613 if (R.HighPC < B->second.first)
614 AddrDieMap[R.HighPC] = B->second;
615 if (R.LowPC > B->first)
616 AddrDieMap[B->first].first = R.LowPC;
617 }
618 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000619 }
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000620 } else
621 llvm::consumeError(DIERangesOrError.takeError());
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000622 }
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000623 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
624 // simplify the logic to update AddrDieMap. The child's range will always
625 // be equal or smaller than the parent's range. With this assumption, when
626 // adding one range into the map, it will at most split a range into 3
627 // sub-ranges.
628 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
629 updateAddressDieMap(Child);
Dehao Chena364f092017-04-19 20:09:38 +0000630}
631
632DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
633 extractDIEsIfNeeded(false);
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000634 if (AddrDieMap.empty())
635 updateAddressDieMap(getUnitDIE());
636 auto R = AddrDieMap.upper_bound(Address);
637 if (R == AddrDieMap.begin())
Dehao Chena364f092017-04-19 20:09:38 +0000638 return DWARFDie();
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000639 // upper_bound's previous item contains Address.
640 --R;
641 if (Address >= R->second.first)
Dehao Chena364f092017-04-19 20:09:38 +0000642 return DWARFDie();
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000643 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000644}
645
Greg Claytonc8c10322016-12-13 18:25:19 +0000646void
647DWARFUnit::getInlinedChainForAddress(uint64_t Address,
648 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000649 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000650 // Try to look for subprogram DIEs in the DWO file.
651 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000652 // First, find the subroutine that contains the given address (the leaf
653 // of inlined chain).
654 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000655 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000656
David Blaikieaa537da2018-05-01 18:08:45 +0000657 if (!SubroutineDIE)
658 return;
659
660 while (!SubroutineDIE.isSubprogramDIE()) {
661 if (SubroutineDIE.getTag() == DW_TAG_inlined_subroutine)
Dehao Chendb569ba2017-04-19 20:52:21 +0000662 InlinedChain.push_back(SubroutineDIE);
663 SubroutineDIE = SubroutineDIE.getParent();
664 }
David Blaikieaa537da2018-05-01 18:08:45 +0000665 InlinedChain.push_back(SubroutineDIE);
David Blaikie07e22442013-09-23 22:44:40 +0000666}
David Blaikie82641be2015-11-17 00:39:55 +0000667
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000668const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
669 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000670 if (Kind == DW_SECT_INFO)
671 return Context.getCUIndex();
672 assert(Kind == DW_SECT_TYPES);
673 return Context.getTUIndex();
674}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000675
Greg Clayton78a07bf2016-12-21 21:37:06 +0000676DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
677 if (!Die)
678 return DWARFDie();
679 const uint32_t Depth = Die->getDepth();
680 // Unit DIEs always have a depth of zero and never have parents.
681 if (Depth == 0)
682 return DWARFDie();
683 // Depth of 1 always means parent is the compile/type unit.
684 if (Depth == 1)
685 return getUnitDIE();
686 // Look for previous DIE with a depth that is one less than the Die's depth.
687 const uint32_t ParentDepth = Depth - 1;
688 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
689 if (DieArray[I].getDepth() == ParentDepth)
690 return DWARFDie(this, &DieArray[I]);
691 }
692 return DWARFDie();
693}
694
695DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
696 if (!Die)
697 return DWARFDie();
698 uint32_t Depth = Die->getDepth();
699 // Unit DIEs always have a depth of zero and never have siblings.
700 if (Depth == 0)
701 return DWARFDie();
702 // NULL DIEs don't have siblings.
703 if (Die->getAbbreviationDeclarationPtr() == nullptr)
704 return DWARFDie();
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000705
Greg Clayton78a07bf2016-12-21 21:37:06 +0000706 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000707 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
708 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000709 if (DieArray[I].getDepth() == Depth)
710 return DWARFDie(this, &DieArray[I]);
711 }
712 return DWARFDie();
713}
David Blaikie485e01b2017-09-19 15:13:55 +0000714
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000715DWARFDie DWARFUnit::getPreviousSibling(const DWARFDebugInfoEntry *Die) {
716 if (!Die)
717 return DWARFDie();
718 uint32_t Depth = Die->getDepth();
719 // Unit DIEs always have a depth of zero and never have siblings.
720 if (Depth == 0)
721 return DWARFDie();
722
723 // Find the previous DIE whose depth is the same as the Die's depth.
Fangrui Song24452312018-07-11 19:09:37 +0000724 for (size_t I = getDIEIndex(Die); I > 0;) {
725 --I;
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000726 if (DieArray[I].getDepth() == Depth - 1)
727 return DWARFDie();
728 if (DieArray[I].getDepth() == Depth)
729 return DWARFDie(this, &DieArray[I]);
730 }
731 return DWARFDie();
732}
733
George Rimar0be860f2017-10-25 10:23:49 +0000734DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) {
735 if (!Die->hasChildren())
736 return DWARFDie();
737
738 // We do not want access out of bounds when parsing corrupted debug data.
739 size_t I = getDIEIndex(Die) + 1;
740 if (I >= DieArray.size())
741 return DWARFDie();
742 return DWARFDie(this, &DieArray[I]);
743}
744
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000745DWARFDie DWARFUnit::getLastChild(const DWARFDebugInfoEntry *Die) {
746 if (!Die->hasChildren())
747 return DWARFDie();
748
749 uint32_t Depth = Die->getDepth();
750 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
751 ++I) {
752 if (DieArray[I].getDepth() == Depth + 1 &&
753 DieArray[I].getTag() == dwarf::DW_TAG_null)
754 return DWARFDie(this, &DieArray[I]);
755 assert(DieArray[I].getDepth() > Depth && "Not processing children?");
756 }
757 return DWARFDie();
758}
759
David Blaikie485e01b2017-09-19 15:13:55 +0000760const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const {
761 if (!Abbrevs)
Paul Robinson5f53f072018-05-14 20:32:31 +0000762 Abbrevs = Abbrev->getAbbreviationDeclarationSet(Header.getAbbrOffset());
David Blaikie485e01b2017-09-19 15:13:55 +0000763 return Abbrevs;
764}
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000765
David Blaikie161dd3c2018-10-20 06:02:15 +0000766llvm::Optional<SectionedAddress> DWARFUnit::getBaseAddress() {
Jonas Devliegherec1113822018-05-21 19:36:54 +0000767 if (BaseAddr)
768 return BaseAddr;
769
770 DWARFDie UnitDie = getUnitDIE();
771 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc});
David Blaikie161dd3c2018-10-20 06:02:15 +0000772 BaseAddr = toSectionedAddress(PC);
Jonas Devliegherec1113822018-05-21 19:36:54 +0000773 return BaseAddr;
774}
775
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000776Optional<StrOffsetsContributionDescriptor>
777StrOffsetsContributionDescriptor::validateContributionSize(
778 DWARFDataExtractor &DA) {
779 uint8_t EntrySize = getDwarfOffsetByteSize();
780 // In order to ensure that we don't read a partial record at the end of
781 // the section we validate for a multiple of the entry size.
782 uint64_t ValidationSize = alignTo(Size, EntrySize);
783 // Guard against overflow.
784 if (ValidationSize >= Size)
785 if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize))
786 return *this;
787 return Optional<StrOffsetsContributionDescriptor>();
788}
789
790// Look for a DWARF64-formatted contribution to the string offsets table
791// starting at a given offset and record it in a descriptor.
792static Optional<StrOffsetsContributionDescriptor>
793parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
794 if (!DA.isValidOffsetForDataOfSize(Offset, 16))
795 return Optional<StrOffsetsContributionDescriptor>();
796
797 if (DA.getU32(&Offset) != 0xffffffff)
798 return Optional<StrOffsetsContributionDescriptor>();
799
800 uint64_t Size = DA.getU64(&Offset);
801 uint8_t Version = DA.getU16(&Offset);
802 (void)DA.getU16(&Offset); // padding
Wolfgang Piebf2b6915e2018-05-10 20:02:34 +0000803 // The encoded length includes the 2-byte version field and the 2-byte
804 // padding, so we need to subtract them out when we populate the descriptor.
805 return StrOffsetsContributionDescriptor(Offset, Size - 4, Version, DWARF64);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000806 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
807}
808
809// Look for a DWARF32-formatted contribution to the string offsets table
810// starting at a given offset and record it in a descriptor.
811static Optional<StrOffsetsContributionDescriptor>
812parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
813 if (!DA.isValidOffsetForDataOfSize(Offset, 8))
814 return Optional<StrOffsetsContributionDescriptor>();
815 uint32_t ContributionSize = DA.getU32(&Offset);
816 if (ContributionSize >= 0xfffffff0)
817 return Optional<StrOffsetsContributionDescriptor>();
818 uint8_t Version = DA.getU16(&Offset);
819 (void)DA.getU16(&Offset); // padding
Wolfgang Piebf2b6915e2018-05-10 20:02:34 +0000820 // The encoded length includes the 2-byte version field and the 2-byte
821 // padding, so we need to subtract them out when we populate the descriptor.
822 return StrOffsetsContributionDescriptor(Offset, ContributionSize - 4, Version,
823 DWARF32);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000824 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
825}
826
827Optional<StrOffsetsContributionDescriptor>
828DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA,
829 uint64_t Offset) {
830 Optional<StrOffsetsContributionDescriptor> Descriptor;
831 // Attempt to find a DWARF64 contribution 16 bytes before the base.
832 if (Offset >= 16)
833 Descriptor =
834 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset - 16);
835 // Try to find a DWARF32 contribution 8 bytes before the base.
836 if (!Descriptor && Offset >= 8)
837 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset - 8);
838 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
839}
840
841Optional<StrOffsetsContributionDescriptor>
842DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA,
843 uint64_t Offset) {
844 if (getVersion() >= 5) {
845 // Look for a valid contribution at the given offset.
846 auto Descriptor =
847 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset);
848 if (!Descriptor)
849 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset);
850 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
851 }
852 // Prior to DWARF v5, we derive the contribution size from the
853 // index table (in a package file). In a .dwo file it is simply
854 // the length of the string offsets section.
855 uint64_t Size = 0;
Paul Robinson5f53f072018-05-14 20:32:31 +0000856 auto IndexEntry = Header.getIndexEntry();
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000857 if (!IndexEntry)
858 Size = StringOffsetSection.Data.size();
859 else if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
860 Size = C->Length;
861 // Return a descriptor with the given offset as base, version 4 and
862 // DWARF32 format.
863 //return Optional<StrOffsetsContributionDescriptor>(
864 //StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32));
865 return StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32);
866}