blob: 9bdcfcb4cdcbb4fa04b44e2f96286b050ca380fd [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"
Zachary Turner82af9432015-01-30 18:07:45 +000014#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000015#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000016#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
Wolfgang Piebad605592018-05-18 20:12:54 +000017#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000018#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Clayton97d22182017-01-13 21:08:18 +000019#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000020#include "llvm/Support/DataExtractor.h"
David Blaikie07e22442013-09-23 22:44:40 +000021#include "llvm/Support/Path.h"
Jonas Devlieghere84e99262018-04-14 22:07:23 +000022#include "llvm/Support/WithColor.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000023#include <algorithm>
24#include <cassert>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000025#include <cstddef>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000026#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000027#include <cstdio>
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000028#include <utility>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000029#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000030
Eugene Zelenko28db7e62017-03-01 01:14:23 +000031using namespace llvm;
David Blaikie07e22442013-09-23 22:44:40 +000032using namespace dwarf;
33
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000034void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
Rafael Espindolac398e672017-07-19 22:27:28 +000035 const DWARFObject &D = C.getDWARFObj();
Rafael Espindolac51dc902018-03-21 21:31:25 +000036 parseImpl(C, D, Section, C.getDebugAbbrev(), &D.getRangeSection(),
Rafael Espindolac398e672017-07-19 22:27:28 +000037 D.getStringSection(), D.getStringOffsetSection(),
Paul Robinsond0c89f82018-01-29 22:02:56 +000038 &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false,
39 false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000040}
41
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000042void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikiee79dda32017-09-19 18:36:11 +000043 const DWARFSection &DWOSection, bool Lazy) {
Rafael Espindolac398e672017-07-19 22:27:28 +000044 const DWARFObject &D = C.getDWARFObj();
Rafael Espindolac51dc902018-03-21 21:31:25 +000045 parseImpl(C, D, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(),
Rafael Espindolac398e672017-07-19 22:27:28 +000046 D.getStringDWOSection(), D.getStringOffsetDWOSection(),
Paul Robinsond0c89f82018-01-29 22:02:56 +000047 &D.getAddrSection(), D.getLineDWOSection(), C.isLittleEndian(),
48 true, Lazy);
Frederic Riss6005dbd2014-10-06 03:36:18 +000049}
50
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000051DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
Paul Robinson5f53f072018-05-14 20:32:31 +000052 const DWARFUnitHeader &Header,
George Rimarca532112017-04-24 10:19:45 +000053 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000054 StringRef SS, const DWARFSection &SOS,
Paul Robinsond0c89f82018-01-29 22:02:56 +000055 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
Paul Robinson5f53f072018-05-14 20:32:31 +000056 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 Blaikie07e22442013-09-23 22:44:40 +000061 clear();
62}
63
Eugene Zelenko570e39a2016-11-23 23:16:32 +000064DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000065
Rafael Espindolac398e672017-07-19 22:27:28 +000066DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const {
67 return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian,
68 getAddressByteSize());
69}
70
David Blaikie07e22442013-09-23 22:44:40 +000071bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
72 uint64_t &Result) const {
Paul Robinson75c068c2017-06-26 18:43:01 +000073 uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize();
74 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize())
David Blaikie07e22442013-09-23 22:44:40 +000075 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000076 DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection,
77 isLittleEndian, getAddressByteSize());
Paul Robinson17536b92017-06-29 16:52:08 +000078 Result = DA.getRelocatedAddress(&Offset);
David Blaikie07e22442013-09-23 22:44:40 +000079 return true;
80}
81
82bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000083 uint64_t &Result) const {
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +000084 if (!StringOffsetsTableContribution)
85 return false;
86 unsigned ItemSize = getDwarfStringOffsetsByteSize();
87 uint32_t Offset = getStringOffsetsBase() + Index * ItemSize;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000088 if (StringOffsetSection.Data.size() < Offset + ItemSize)
David Blaikie07e22442013-09-23 22:44:40 +000089 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000090 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
91 isLittleEndian, 0);
Paul Robinson17536b92017-06-29 16:52:08 +000092 Result = DA.getRelocatedValue(ItemSize, &Offset);
David Blaikie07e22442013-09-23 22:44:40 +000093 return true;
94}
95
Paul Robinson5f53f072018-05-14 20:32:31 +000096bool 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 Blaikie07e22442013-09-23 22:44:40 +0000103 Length = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000104 // FIXME: Support DWARF64.
Paul Robinson5f53f072018-05-14 20:32:31 +0000105 unsigned SizeOfLength = 4;
Paul Robinson75c068c2017-06-26 18:43:01 +0000106 FormParams.Format = DWARF32;
107 FormParams.Version = debug_info.getU16(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000108 if (FormParams.Version >= 5) {
Paul Robinsoncddd6042017-02-28 20:24:55 +0000109 UnitType = debug_info.getU8(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000110 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000111 AbbrOffset = debug_info.getU32(offset_ptr);
112 } else {
Rafael Espindolac51dc902018-03-21 21:31:25 +0000113 AbbrOffset = debug_info.getRelocatedValue(4, offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000114 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinson5f53f072018-05-14 20:32:31 +0000115 // 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 Robinsoncddd6042017-02-28 20:24:55 +0000121 }
David Blaikie82641be2015-11-17 00:39:55 +0000122 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 Robinson5f53f072018-05-14 20:32:31 +0000133 bool TypeOffsetOK = true;
134 if (isTypeUnit()) {
135 TypeHash = debug_info.getU64(offset_ptr);
136 TypeOffset = debug_info.getU32(offset_ptr);
137 // Type offset is unit-relative; should be after the header and before
138 // the end of the current unit.
139 TypeOffsetOK = TypeOffset >= *offset_ptr - Offset &&
140 TypeOffset < getLength() + SizeOfLength;
141 }
Alexey Samsonov7682f812014-04-24 22:51:03 +0000142 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
Paul Robinson75c068c2017-06-26 18:43:01 +0000143 bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
144 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000145
Paul Robinson5f53f072018-05-14 20:32:31 +0000146 if (!LengthOK || !VersionOK || !AddrSizeOK || !TypeOffsetOK)
David Blaikie07e22442013-09-23 22:44:40 +0000147 return false;
148
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000149 // Keep track of the highest DWARF version we encounter across all units.
Paul Robinson75c068c2017-06-26 18:43:01 +0000150 Context.setMaxVersionIfGreater(getVersion());
David Blaikie485e01b2017-09-19 15:13:55 +0000151 return true;
David Blaikie07e22442013-09-23 22:44:40 +0000152}
153
Wolfgang Piebad605592018-05-18 20:12:54 +0000154// Parse the rangelist table header, including the optional array of offsets
155// following it (DWARF v5 and later).
156static Expected<DWARFDebugRnglistTable>
157parseRngListTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
158 // TODO: Support DWARF64
159 // We are expected to be called with Offset 0 or pointing just past the table
160 // header, which is 12 bytes long for DWARF32.
161 if (Offset > 0) {
162 if (Offset < 12U) {
163 std::string Buffer;
164 raw_string_ostream Stream(Buffer);
165 Stream << format(
166 "Did not detect a valid range list table with base = 0x%x", Offset);
167 return make_error<StringError>(Stream.str(), inconvertibleErrorCode());
168 }
169 Offset -= 12U;
170 }
171 llvm::DWARFDebugRnglistTable Table;
172 if (Error E = Table.extractHeaderAndOffsets(DA, &Offset))
Wolfgang Piebda716392018-05-18 20:35:13 +0000173 return std::move(E);
Wolfgang Piebad605592018-05-18 20:12:54 +0000174 return Table;
175}
176
David Blaikie07e22442013-09-23 22:44:40 +0000177bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
Paul Robinson17536b92017-06-29 16:52:08 +0000178 DWARFDebugRangeList &RangeList) const {
David Blaikie07e22442013-09-23 22:44:40 +0000179 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000180 assert(!DieArray.empty());
Rafael Espindolac398e672017-07-19 22:27:28 +0000181 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
182 isLittleEndian, getAddressByteSize());
David Blaikie07e22442013-09-23 22:44:40 +0000183 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
Paul Robinson17536b92017-06-29 16:52:08 +0000184 return RangeList.extract(RangesData, &ActualRangeListOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000185}
186
187void DWARFUnit::clear() {
Craig Topper2617dcc2014-04-15 06:32:26 +0000188 Abbrevs = nullptr;
George Rimar2f95c8b2017-09-04 10:30:39 +0000189 BaseAddr.reset();
David Blaikie07e22442013-09-23 22:44:40 +0000190 RangeSectionBase = 0;
191 AddrOffsetSectionBase = 0;
192 clearDIEs(false);
193 DWO.reset();
194}
195
196const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000197 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000198}
199
Greg Clayton52fe1f62016-12-14 22:38:08 +0000200Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000201 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000202}
203
David Blaikie07e22442013-09-23 22:44:40 +0000204void DWARFUnit::extractDIEsToVector(
205 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000206 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000207 if (!AppendCUDie && !AppendNonCUDies)
208 return;
209
210 // Set the offset to that of the first DIE and calculate the start of the
211 // next compilation unit header.
Paul Robinson5f53f072018-05-14 20:32:31 +0000212 uint32_t DIEOffset = getOffset() + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000213 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000214 DWARFDebugInfoEntry DIE;
Paul Robinson17536b92017-06-29 16:52:08 +0000215 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000216 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000217 bool IsCUDie = true;
218
Greg Clayton78a07bf2016-12-21 21:37:06 +0000219 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
220 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000221 if (IsCUDie) {
222 if (AppendCUDie)
223 Dies.push_back(DIE);
224 if (!AppendNonCUDies)
225 break;
226 // The average bytes per DIE entry has been seen to be
227 // around 14-20 so let's pre-reserve the needed memory for
228 // our DIE entries accordingly.
229 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
230 IsCUDie = false;
231 } else {
232 Dies.push_back(DIE);
233 }
234
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000235 if (const DWARFAbbreviationDeclaration *AbbrDecl =
236 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000237 // Normal DIE
238 if (AbbrDecl->hasChildren())
239 ++Depth;
240 } else {
241 // NULL DIE.
242 if (Depth > 0)
243 --Depth;
244 if (Depth == 0)
245 break; // We are done with this compile unit!
246 }
247 }
248
249 // Give a little bit of info if we encounter corrupt DWARF (our offset
250 // should always terminate at or before the start of the next compilation
251 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000252 if (DIEOffset > NextCUOffset)
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000253 WithColor::warning() << format("DWARF compile unit extends beyond its "
254 "bounds cu 0x%8.8x at 0x%8.8x\n",
255 getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000256}
257
258size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000259 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000260 DieArray.size() > 1)
261 return 0; // Already parsed.
262
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000263 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000264 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
265
266 if (DieArray.empty())
267 return 0;
268
269 // If CU DIE was just parsed, copy several attribute values from it.
270 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000271 DWARFDie UnitDie = getUnitDIE();
George Rimar2f95c8b2017-09-04 10:30:39 +0000272 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc});
273 if (Optional<uint64_t> Addr = toAddress(PC))
274 setBaseAddress({*Addr, PC->getSectionIndex()});
275
David Blaikie22dc4472017-08-02 20:16:22 +0000276 if (!isDWO) {
277 assert(AddrOffsetSectionBase == 0);
278 assert(RangeSectionBase == 0);
279 AddrOffsetSectionBase =
280 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
281 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
282 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000283
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000284 // In general, in DWARF v5 and beyond we derive the start of the unit's
285 // contribution to the string offsets table from the unit DIE's
286 // DW_AT_str_offsets_base attribute. Split DWARF units do not use this
287 // attribute, so we assume that there is a contribution to the string
288 // offsets table starting at offset 0 of the debug_str_offsets.dwo section.
289 // In both cases we need to determine the format of the contribution,
290 // which may differ from the unit's format.
291 uint64_t StringOffsetsContributionBase =
292 isDWO ? 0 : toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
Paul Robinson5f53f072018-05-14 20:32:31 +0000293 auto IndexEntry = Header.getIndexEntry();
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000294 if (IndexEntry)
295 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000296 StringOffsetsContributionBase += C->Offset;
297
298 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
299 isLittleEndian, 0);
300 if (isDWO)
301 StringOffsetsTableContribution =
302 determineStringOffsetsTableContributionDWO(
303 DA, StringOffsetsContributionBase);
304 else if (getVersion() >= 5)
305 StringOffsetsTableContribution = determineStringOffsetsTableContribution(
306 DA, StringOffsetsContributionBase);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000307
Wolfgang Piebad605592018-05-18 20:12:54 +0000308 // DWARF v5 uses the .debug_rnglists and .debug_rnglists.dwo sections to
309 // describe address ranges.
310 if (getVersion() >= 5) {
311 if (isDWO)
312 setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0);
313 else
314 setRangesSection(&Context.getDWARFObj().getRnglistsSection(),
315 toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0));
316 // Parse the range list table header. Individual range lists are
317 // extracted lazily.
318 DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection,
319 isLittleEndian, 0);
320 if (auto TableOrError =
321 parseRngListTableHeader(RangesDA, RangeSectionBase))
322 RngListTable = TableOrError.get();
323 else
324 WithColor::error() << "parsing a range list table: "
325 << toString(std::move(TableOrError.takeError()))
326 << '\n';
327
328 // In a split dwarf unit, there is no DW_AT_rnglists_base attribute.
329 // Adjust RangeSectionBase to point past the table header.
330 if (isDWO && RngListTable)
331 RangeSectionBase = RngListTable->getHeaderSize();
332 }
333
Alexey Samsonovaa909982014-06-13 22:31:03 +0000334 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
335 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000336 }
337
David Blaikie07e22442013-09-23 22:44:40 +0000338 return DieArray.size();
339}
340
David Blaikie07e22442013-09-23 22:44:40 +0000341bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000342 if (isDWO)
343 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000344 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000345 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000346 DWARFDie UnitDie = getUnitDIE();
347 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000348 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000349 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000350 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000351 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000352 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000353 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000354 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
355 *CompilationDir) {
356 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000357 }
Greg Clayton97d22182017-01-13 21:08:18 +0000358 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000359 auto DWOId = getDWOId();
360 if (!DWOId)
361 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000362 auto DWOContext = Context.getDWOContext(AbsolutePath);
363 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000364 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000365
David Blaikie15d85fc2017-05-23 06:48:53 +0000366 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
367 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000368 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000369 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000370 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000371 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Wolfgang Piebad605592018-05-18 20:12:54 +0000372 if (getVersion() >= 5) {
373 DWO->setRangesSection(&Context.getDWARFObj().getRnglistsDWOSection(), 0);
374 DWARFDataExtractor RangesDA(Context.getDWARFObj(), *RangeSection,
375 isLittleEndian, 0);
376 if (auto TableOrError = parseRngListTableHeader(RangesDA, RangeSectionBase))
377 DWO->RngListTable = TableOrError.get();
378 else
379 WithColor::error() << "parsing a range list table: "
380 << toString(std::move(TableOrError.takeError()))
381 << '\n';
382 if (DWO->RngListTable)
383 DWO->RangeSectionBase = DWO->RngListTable->getHeaderSize();
384 } else {
385 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
386 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
387 }
388
David Blaikie07e22442013-09-23 22:44:40 +0000389 return true;
390}
391
392void DWARFUnit::clearDIEs(bool KeepCUDie) {
393 if (DieArray.size() > (unsigned)KeepCUDie) {
Benjamin Kramer295cf4d2017-08-01 14:38:08 +0000394 DieArray.resize((unsigned)KeepCUDie);
395 DieArray.shrink_to_fit();
David Blaikie07e22442013-09-23 22:44:40 +0000396 }
397}
398
Wolfgang Piebad605592018-05-18 20:12:54 +0000399DWARFAddressRangesVector DWARFUnit::findRnglistFromOffset(uint32_t Offset) {
400 if (getVersion() <= 4) {
401 DWARFDebugRangeList RangeList;
402 if (extractRangeList(Offset, RangeList))
403 return RangeList.getAbsoluteRanges(getBaseAddress());
404 return DWARFAddressRangesVector();
405 }
406 if (RngListTable) {
407 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
408 isLittleEndian, RngListTable->getAddrSize());
409 if (auto RangeList = RngListTable->findRangeList(RangesData, Offset))
410 return RangeList->getAbsoluteRanges(getBaseAddress());
411 }
412 return DWARFAddressRangesVector();
413}
414
415DWARFAddressRangesVector DWARFUnit::findRnglistFromIndex(uint32_t Index) {
416 if (auto Offset = getRnglistOffset(Index))
417 return findRnglistFromOffset(*Offset + RangeSectionBase);
418 return DWARFAddressRangesVector();
419}
420
Alexey Samsonov762343d2014-04-18 17:25:46 +0000421void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000422 DWARFDie UnitDie = getUnitDIE();
423 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000424 return;
425 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000426 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000427 if (!CUDIERanges.empty()) {
428 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
429 return;
430 }
431
David Blaikie07e22442013-09-23 22:44:40 +0000432 // This function is usually called if there in no .debug_aranges section
433 // in order to produce a compile unit level set of address ranges that
434 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
435 // all compile units to stay loaded when they weren't needed. So we can end
436 // up parsing the DWARF and then throwing them all away to keep memory usage
437 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000438 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000439 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000440
441 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000442 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000443 if (DWO)
444 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000445 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000446 DWO.reset();
447
448 // Keep memory down by clearing DIEs if this generate function
449 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000450 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000451 clearDIEs(true);
452}
453
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000454void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
455 if (Die.isSubroutineDIE()) {
Dehao Chena364f092017-04-19 20:09:38 +0000456 for (const auto &R : Die.getAddressRanges()) {
457 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000458 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000459 continue;
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000460 auto B = AddrDieMap.upper_bound(R.LowPC);
461 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
462 // The range is a sub-range of existing ranges, we need to split the
463 // existing range.
464 if (R.HighPC < B->second.first)
465 AddrDieMap[R.HighPC] = B->second;
466 if (R.LowPC > B->first)
467 AddrDieMap[B->first].first = R.LowPC;
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000468 }
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000469 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000470 }
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000471 }
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000472 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
473 // simplify the logic to update AddrDieMap. The child's range will always
474 // be equal or smaller than the parent's range. With this assumption, when
475 // adding one range into the map, it will at most split a range into 3
476 // sub-ranges.
477 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
478 updateAddressDieMap(Child);
Dehao Chena364f092017-04-19 20:09:38 +0000479}
480
481DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
482 extractDIEsIfNeeded(false);
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000483 if (AddrDieMap.empty())
484 updateAddressDieMap(getUnitDIE());
485 auto R = AddrDieMap.upper_bound(Address);
486 if (R == AddrDieMap.begin())
Dehao Chena364f092017-04-19 20:09:38 +0000487 return DWARFDie();
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000488 // upper_bound's previous item contains Address.
489 --R;
490 if (Address >= R->second.first)
Dehao Chena364f092017-04-19 20:09:38 +0000491 return DWARFDie();
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000492 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000493}
494
Greg Claytonc8c10322016-12-13 18:25:19 +0000495void
496DWARFUnit::getInlinedChainForAddress(uint64_t Address,
497 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000498 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000499 // Try to look for subprogram DIEs in the DWO file.
500 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000501 // First, find the subroutine that contains the given address (the leaf
502 // of inlined chain).
503 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000504 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000505
David Blaikieaa537da2018-05-01 18:08:45 +0000506 if (!SubroutineDIE)
507 return;
508
509 while (!SubroutineDIE.isSubprogramDIE()) {
510 if (SubroutineDIE.getTag() == DW_TAG_inlined_subroutine)
Dehao Chendb569ba2017-04-19 20:52:21 +0000511 InlinedChain.push_back(SubroutineDIE);
512 SubroutineDIE = SubroutineDIE.getParent();
513 }
David Blaikieaa537da2018-05-01 18:08:45 +0000514 InlinedChain.push_back(SubroutineDIE);
David Blaikie07e22442013-09-23 22:44:40 +0000515}
David Blaikie82641be2015-11-17 00:39:55 +0000516
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000517const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
518 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000519 if (Kind == DW_SECT_INFO)
520 return Context.getCUIndex();
521 assert(Kind == DW_SECT_TYPES);
522 return Context.getTUIndex();
523}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000524
Greg Clayton78a07bf2016-12-21 21:37:06 +0000525DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
526 if (!Die)
527 return DWARFDie();
528 const uint32_t Depth = Die->getDepth();
529 // Unit DIEs always have a depth of zero and never have parents.
530 if (Depth == 0)
531 return DWARFDie();
532 // Depth of 1 always means parent is the compile/type unit.
533 if (Depth == 1)
534 return getUnitDIE();
535 // Look for previous DIE with a depth that is one less than the Die's depth.
536 const uint32_t ParentDepth = Depth - 1;
537 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
538 if (DieArray[I].getDepth() == ParentDepth)
539 return DWARFDie(this, &DieArray[I]);
540 }
541 return DWARFDie();
542}
543
544DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
545 if (!Die)
546 return DWARFDie();
547 uint32_t Depth = Die->getDepth();
548 // Unit DIEs always have a depth of zero and never have siblings.
549 if (Depth == 0)
550 return DWARFDie();
551 // NULL DIEs don't have siblings.
552 if (Die->getAbbreviationDeclarationPtr() == nullptr)
553 return DWARFDie();
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000554
Greg Clayton78a07bf2016-12-21 21:37:06 +0000555 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000556 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
557 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000558 if (DieArray[I].getDepth() == Depth)
559 return DWARFDie(this, &DieArray[I]);
560 }
561 return DWARFDie();
562}
David Blaikie485e01b2017-09-19 15:13:55 +0000563
George Rimar0be860f2017-10-25 10:23:49 +0000564DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) {
565 if (!Die->hasChildren())
566 return DWARFDie();
567
568 // We do not want access out of bounds when parsing corrupted debug data.
569 size_t I = getDIEIndex(Die) + 1;
570 if (I >= DieArray.size())
571 return DWARFDie();
572 return DWARFDie(this, &DieArray[I]);
573}
574
David Blaikie485e01b2017-09-19 15:13:55 +0000575const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const {
576 if (!Abbrevs)
Paul Robinson5f53f072018-05-14 20:32:31 +0000577 Abbrevs = Abbrev->getAbbreviationDeclarationSet(Header.getAbbrOffset());
David Blaikie485e01b2017-09-19 15:13:55 +0000578 return Abbrevs;
579}
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000580
581Optional<StrOffsetsContributionDescriptor>
582StrOffsetsContributionDescriptor::validateContributionSize(
583 DWARFDataExtractor &DA) {
584 uint8_t EntrySize = getDwarfOffsetByteSize();
585 // In order to ensure that we don't read a partial record at the end of
586 // the section we validate for a multiple of the entry size.
587 uint64_t ValidationSize = alignTo(Size, EntrySize);
588 // Guard against overflow.
589 if (ValidationSize >= Size)
590 if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize))
591 return *this;
592 return Optional<StrOffsetsContributionDescriptor>();
593}
594
595// Look for a DWARF64-formatted contribution to the string offsets table
596// starting at a given offset and record it in a descriptor.
597static Optional<StrOffsetsContributionDescriptor>
598parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
599 if (!DA.isValidOffsetForDataOfSize(Offset, 16))
600 return Optional<StrOffsetsContributionDescriptor>();
601
602 if (DA.getU32(&Offset) != 0xffffffff)
603 return Optional<StrOffsetsContributionDescriptor>();
604
605 uint64_t Size = DA.getU64(&Offset);
606 uint8_t Version = DA.getU16(&Offset);
607 (void)DA.getU16(&Offset); // padding
Wolfgang Piebf2b6915e2018-05-10 20:02:34 +0000608 // The encoded length includes the 2-byte version field and the 2-byte
609 // padding, so we need to subtract them out when we populate the descriptor.
610 return StrOffsetsContributionDescriptor(Offset, Size - 4, Version, DWARF64);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000611 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
612}
613
614// Look for a DWARF32-formatted contribution to the string offsets table
615// starting at a given offset and record it in a descriptor.
616static Optional<StrOffsetsContributionDescriptor>
617parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
618 if (!DA.isValidOffsetForDataOfSize(Offset, 8))
619 return Optional<StrOffsetsContributionDescriptor>();
620 uint32_t ContributionSize = DA.getU32(&Offset);
621 if (ContributionSize >= 0xfffffff0)
622 return Optional<StrOffsetsContributionDescriptor>();
623 uint8_t Version = DA.getU16(&Offset);
624 (void)DA.getU16(&Offset); // padding
Wolfgang Piebf2b6915e2018-05-10 20:02:34 +0000625 // The encoded length includes the 2-byte version field and the 2-byte
626 // padding, so we need to subtract them out when we populate the descriptor.
627 return StrOffsetsContributionDescriptor(Offset, ContributionSize - 4, Version,
628 DWARF32);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000629 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
630}
631
632Optional<StrOffsetsContributionDescriptor>
633DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA,
634 uint64_t Offset) {
635 Optional<StrOffsetsContributionDescriptor> Descriptor;
636 // Attempt to find a DWARF64 contribution 16 bytes before the base.
637 if (Offset >= 16)
638 Descriptor =
639 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset - 16);
640 // Try to find a DWARF32 contribution 8 bytes before the base.
641 if (!Descriptor && Offset >= 8)
642 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset - 8);
643 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
644}
645
646Optional<StrOffsetsContributionDescriptor>
647DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA,
648 uint64_t Offset) {
649 if (getVersion() >= 5) {
650 // Look for a valid contribution at the given offset.
651 auto Descriptor =
652 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset);
653 if (!Descriptor)
654 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset);
655 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
656 }
657 // Prior to DWARF v5, we derive the contribution size from the
658 // index table (in a package file). In a .dwo file it is simply
659 // the length of the string offsets section.
660 uint64_t Size = 0;
Paul Robinson5f53f072018-05-14 20:32:31 +0000661 auto IndexEntry = Header.getIndexEntry();
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000662 if (!IndexEntry)
663 Size = StringOffsetSection.Data.size();
664 else if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
665 Size = C->Length;
666 // Return a descriptor with the given offset as base, version 4 and
667 // DWARF32 format.
668 //return Optional<StrOffsetsContributionDescriptor>(
669 //StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32));
670 return StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32);
671}