blob: e24ef90dd522d8faf9f2b9f48c64d1b485ccf684 [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"
17#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Clayton97d22182017-01-13 21:08:18 +000018#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000019#include "llvm/Support/DataExtractor.h"
David Blaikie07e22442013-09-23 22:44:40 +000020#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000021#include <algorithm>
22#include <cassert>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000023#include <cstddef>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000024#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000025#include <cstdio>
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000026#include <utility>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000027#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000028
Eugene Zelenko28db7e62017-03-01 01:14:23 +000029using namespace llvm;
David Blaikie07e22442013-09-23 22:44:40 +000030using namespace dwarf;
31
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000032void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
Rafael Espindolac398e672017-07-19 22:27:28 +000033 const DWARFObject &D = C.getDWARFObj();
34 parseImpl(C, Section, C.getDebugAbbrev(), &D.getRangeSection(),
35 D.getStringSection(), D.getStringOffsetSection(),
Paul Robinsond0c89f82018-01-29 22:02:56 +000036 &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false,
37 false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000038}
39
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000040void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikiee79dda32017-09-19 18:36:11 +000041 const DWARFSection &DWOSection, bool Lazy) {
Rafael Espindolac398e672017-07-19 22:27:28 +000042 const DWARFObject &D = C.getDWARFObj();
43 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(),
44 D.getStringDWOSection(), D.getStringOffsetDWOSection(),
Paul Robinsond0c89f82018-01-29 22:02:56 +000045 &D.getAddrSection(), D.getLineDWOSection(), C.isLittleEndian(),
46 true, Lazy);
Frederic Riss6005dbd2014-10-06 03:36:18 +000047}
48
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000049DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
George Rimarca532112017-04-24 10:19:45 +000050 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000051 StringRef SS, const DWARFSection &SOS,
Paul Robinsond0c89f82018-01-29 22:02:56 +000052 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
53 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
David Blaikie82641be2015-11-17 00:39:55 +000054 const DWARFUnitIndex::Entry *IndexEntry)
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000055 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
Paul Robinsond0c89f82018-01-29 22:02:56 +000056 LineSection(LS), StringSection(SS), StringOffsetSection(SOS),
57 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
58 UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000059 clear();
60}
61
Eugene Zelenko570e39a2016-11-23 23:16:32 +000062DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000063
Rafael Espindolac398e672017-07-19 22:27:28 +000064DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const {
65 return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian,
66 getAddressByteSize());
67}
68
David Blaikie07e22442013-09-23 22:44:40 +000069bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
70 uint64_t &Result) const {
Paul Robinson75c068c2017-06-26 18:43:01 +000071 uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize();
72 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize())
David Blaikie07e22442013-09-23 22:44:40 +000073 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000074 DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection,
75 isLittleEndian, getAddressByteSize());
Paul Robinson17536b92017-06-29 16:52:08 +000076 Result = DA.getRelocatedAddress(&Offset);
David Blaikie07e22442013-09-23 22:44:40 +000077 return true;
78}
79
80bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000081 uint64_t &Result) const {
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +000082 if (!StringOffsetsTableContribution)
83 return false;
84 unsigned ItemSize = getDwarfStringOffsetsByteSize();
85 uint32_t Offset = getStringOffsetsBase() + Index * ItemSize;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000086 if (StringOffsetSection.Data.size() < Offset + ItemSize)
David Blaikie07e22442013-09-23 22:44:40 +000087 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000088 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
89 isLittleEndian, 0);
Paul Robinson17536b92017-06-29 16:52:08 +000090 Result = DA.getRelocatedValue(ItemSize, &Offset);
David Blaikie07e22442013-09-23 22:44:40 +000091 return true;
92}
93
94bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
95 Length = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +000096 // FIXME: Support DWARF64.
97 FormParams.Format = DWARF32;
98 FormParams.Version = debug_info.getU16(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +000099 if (FormParams.Version >= 5) {
Paul Robinsoncddd6042017-02-28 20:24:55 +0000100 UnitType = debug_info.getU8(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000101 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000102 AbbrOffset = debug_info.getU32(offset_ptr);
103 } else {
104 AbbrOffset = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000105 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000106 }
David Blaikie82641be2015-11-17 00:39:55 +0000107 if (IndexEntry) {
108 if (AbbrOffset)
109 return false;
110 auto *UnitContrib = IndexEntry->getOffset();
111 if (!UnitContrib || UnitContrib->Length != (Length + 4))
112 return false;
113 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
114 if (!AbbrEntry)
115 return false;
116 AbbrOffset = AbbrEntry->Offset;
117 }
David Blaikie07e22442013-09-23 22:44:40 +0000118
Alexey Samsonov7682f812014-04-24 22:51:03 +0000119 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
Paul Robinson75c068c2017-06-26 18:43:01 +0000120 bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
121 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000122
Alexey Samsonov7682f812014-04-24 22:51:03 +0000123 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000124 return false;
125
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000126 // Keep track of the highest DWARF version we encounter across all units.
Paul Robinson75c068c2017-06-26 18:43:01 +0000127 Context.setMaxVersionIfGreater(getVersion());
David Blaikie485e01b2017-09-19 15:13:55 +0000128 return true;
David Blaikie07e22442013-09-23 22:44:40 +0000129}
130
131bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
132 clear();
133
134 Offset = *offset_ptr;
135
136 if (debug_info.isValidOffset(*offset_ptr)) {
137 if (extractImpl(debug_info, offset_ptr))
138 return true;
139
140 // reset the offset to where we tried to parse from if anything went wrong
141 *offset_ptr = Offset;
142 }
143
144 return false;
145}
146
David Blaikie07e22442013-09-23 22:44:40 +0000147bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
Paul Robinson17536b92017-06-29 16:52:08 +0000148 DWARFDebugRangeList &RangeList) const {
David Blaikie07e22442013-09-23 22:44:40 +0000149 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000150 assert(!DieArray.empty());
Rafael Espindolac398e672017-07-19 22:27:28 +0000151 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
152 isLittleEndian, getAddressByteSize());
David Blaikie07e22442013-09-23 22:44:40 +0000153 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
Paul Robinson17536b92017-06-29 16:52:08 +0000154 return RangeList.extract(RangesData, &ActualRangeListOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000155}
156
157void DWARFUnit::clear() {
158 Offset = 0;
159 Length = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000160 Abbrevs = nullptr;
Paul Robinson75c068c2017-06-26 18:43:01 +0000161 FormParams = DWARFFormParams({0, 0, DWARF32});
George Rimar2f95c8b2017-09-04 10:30:39 +0000162 BaseAddr.reset();
David Blaikie07e22442013-09-23 22:44:40 +0000163 RangeSectionBase = 0;
164 AddrOffsetSectionBase = 0;
165 clearDIEs(false);
166 DWO.reset();
167}
168
169const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000170 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000171}
172
Greg Clayton52fe1f62016-12-14 22:38:08 +0000173Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000174 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000175}
176
David Blaikie07e22442013-09-23 22:44:40 +0000177void DWARFUnit::extractDIEsToVector(
178 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000179 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000180 if (!AppendCUDie && !AppendNonCUDies)
181 return;
182
183 // Set the offset to that of the first DIE and calculate the start of the
184 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000185 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000186 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000187 DWARFDebugInfoEntry DIE;
Paul Robinson17536b92017-06-29 16:52:08 +0000188 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000189 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000190 bool IsCUDie = true;
191
Greg Clayton78a07bf2016-12-21 21:37:06 +0000192 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
193 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000194 if (IsCUDie) {
195 if (AppendCUDie)
196 Dies.push_back(DIE);
197 if (!AppendNonCUDies)
198 break;
199 // The average bytes per DIE entry has been seen to be
200 // around 14-20 so let's pre-reserve the needed memory for
201 // our DIE entries accordingly.
202 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
203 IsCUDie = false;
204 } else {
205 Dies.push_back(DIE);
206 }
207
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000208 if (const DWARFAbbreviationDeclaration *AbbrDecl =
209 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000210 // Normal DIE
211 if (AbbrDecl->hasChildren())
212 ++Depth;
213 } else {
214 // NULL DIE.
215 if (Depth > 0)
216 --Depth;
217 if (Depth == 0)
218 break; // We are done with this compile unit!
219 }
220 }
221
222 // Give a little bit of info if we encounter corrupt DWARF (our offset
223 // should always terminate at or before the start of the next compilation
224 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000225 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000226 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000227 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000228}
229
230size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000231 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000232 DieArray.size() > 1)
233 return 0; // Already parsed.
234
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000235 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000236 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
237
238 if (DieArray.empty())
239 return 0;
240
241 // If CU DIE was just parsed, copy several attribute values from it.
242 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000243 DWARFDie UnitDie = getUnitDIE();
George Rimar2f95c8b2017-09-04 10:30:39 +0000244 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc});
245 if (Optional<uint64_t> Addr = toAddress(PC))
246 setBaseAddress({*Addr, PC->getSectionIndex()});
247
David Blaikie22dc4472017-08-02 20:16:22 +0000248 if (!isDWO) {
249 assert(AddrOffsetSectionBase == 0);
250 assert(RangeSectionBase == 0);
251 AddrOffsetSectionBase =
252 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
253 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
254 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000255
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000256 // In general, in DWARF v5 and beyond we derive the start of the unit's
257 // contribution to the string offsets table from the unit DIE's
258 // DW_AT_str_offsets_base attribute. Split DWARF units do not use this
259 // attribute, so we assume that there is a contribution to the string
260 // offsets table starting at offset 0 of the debug_str_offsets.dwo section.
261 // In both cases we need to determine the format of the contribution,
262 // which may differ from the unit's format.
263 uint64_t StringOffsetsContributionBase =
264 isDWO ? 0 : toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000265 if (IndexEntry)
266 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000267 StringOffsetsContributionBase += C->Offset;
268
269 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
270 isLittleEndian, 0);
271 if (isDWO)
272 StringOffsetsTableContribution =
273 determineStringOffsetsTableContributionDWO(
274 DA, StringOffsetsContributionBase);
275 else if (getVersion() >= 5)
276 StringOffsetsTableContribution = determineStringOffsetsTableContribution(
277 DA, StringOffsetsContributionBase);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000278
Alexey Samsonovaa909982014-06-13 22:31:03 +0000279 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
280 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000281 }
282
David Blaikie07e22442013-09-23 22:44:40 +0000283 return DieArray.size();
284}
285
David Blaikie07e22442013-09-23 22:44:40 +0000286bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000287 if (isDWO)
288 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000289 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000290 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000291 DWARFDie UnitDie = getUnitDIE();
292 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000293 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000294 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000295 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000296 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000297 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000298 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000299 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
300 *CompilationDir) {
301 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000302 }
Greg Clayton97d22182017-01-13 21:08:18 +0000303 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000304 auto DWOId = getDWOId();
305 if (!DWOId)
306 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000307 auto DWOContext = Context.getDWOContext(AbsolutePath);
308 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000309 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000310
David Blaikie15d85fc2017-05-23 06:48:53 +0000311 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
312 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000313 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000314 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000315 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000316 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000317 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
David Blaikief9803fb2017-05-23 00:30:42 +0000318 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000319 return true;
320}
321
322void DWARFUnit::clearDIEs(bool KeepCUDie) {
323 if (DieArray.size() > (unsigned)KeepCUDie) {
Benjamin Kramer295cf4d2017-08-01 14:38:08 +0000324 DieArray.resize((unsigned)KeepCUDie);
325 DieArray.shrink_to_fit();
David Blaikie07e22442013-09-23 22:44:40 +0000326 }
327}
328
Alexey Samsonov762343d2014-04-18 17:25:46 +0000329void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000330 DWARFDie UnitDie = getUnitDIE();
331 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000332 return;
333 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000334 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000335 if (!CUDIERanges.empty()) {
336 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
337 return;
338 }
339
David Blaikie07e22442013-09-23 22:44:40 +0000340 // This function is usually called if there in no .debug_aranges section
341 // in order to produce a compile unit level set of address ranges that
342 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
343 // all compile units to stay loaded when they weren't needed. So we can end
344 // up parsing the DWARF and then throwing them all away to keep memory usage
345 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000346 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000347 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000348
349 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000350 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000351 if (DWO)
352 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000353 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000354 DWO.reset();
355
356 // Keep memory down by clearing DIEs if this generate function
357 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000358 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000359 clearDIEs(true);
360}
361
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000362void DWARFUnit::updateAddressDieMap(DWARFDie Die) {
363 if (Die.isSubroutineDIE()) {
Dehao Chena364f092017-04-19 20:09:38 +0000364 for (const auto &R : Die.getAddressRanges()) {
365 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000366 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000367 continue;
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000368 auto B = AddrDieMap.upper_bound(R.LowPC);
369 if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) {
370 // The range is a sub-range of existing ranges, we need to split the
371 // existing range.
372 if (R.HighPC < B->second.first)
373 AddrDieMap[R.HighPC] = B->second;
374 if (R.LowPC > B->first)
375 AddrDieMap[B->first].first = R.LowPC;
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000376 }
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000377 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000378 }
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000379 }
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000380 // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to
381 // simplify the logic to update AddrDieMap. The child's range will always
382 // be equal or smaller than the parent's range. With this assumption, when
383 // adding one range into the map, it will at most split a range into 3
384 // sub-ranges.
385 for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling())
386 updateAddressDieMap(Child);
Dehao Chena364f092017-04-19 20:09:38 +0000387}
388
389DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
390 extractDIEsIfNeeded(false);
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000391 if (AddrDieMap.empty())
392 updateAddressDieMap(getUnitDIE());
393 auto R = AddrDieMap.upper_bound(Address);
394 if (R == AddrDieMap.begin())
Dehao Chena364f092017-04-19 20:09:38 +0000395 return DWARFDie();
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000396 // upper_bound's previous item contains Address.
397 --R;
398 if (Address >= R->second.first)
Dehao Chena364f092017-04-19 20:09:38 +0000399 return DWARFDie();
David Blaikie3b6de6fe2018-02-13 01:52:30 +0000400 return R->second.second;
David Blaikie07e22442013-09-23 22:44:40 +0000401}
402
Greg Claytonc8c10322016-12-13 18:25:19 +0000403void
404DWARFUnit::getInlinedChainForAddress(uint64_t Address,
405 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000406 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000407 // Try to look for subprogram DIEs in the DWO file.
408 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000409 // First, find the subroutine that contains the given address (the leaf
410 // of inlined chain).
411 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000412 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000413
Dehao Chendb569ba2017-04-19 20:52:21 +0000414 while (SubroutineDIE) {
415 if (SubroutineDIE.isSubroutineDIE())
416 InlinedChain.push_back(SubroutineDIE);
417 SubroutineDIE = SubroutineDIE.getParent();
418 }
David Blaikie07e22442013-09-23 22:44:40 +0000419}
David Blaikie82641be2015-11-17 00:39:55 +0000420
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000421const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
422 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000423 if (Kind == DW_SECT_INFO)
424 return Context.getCUIndex();
425 assert(Kind == DW_SECT_TYPES);
426 return Context.getTUIndex();
427}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000428
Greg Clayton78a07bf2016-12-21 21:37:06 +0000429DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
430 if (!Die)
431 return DWARFDie();
432 const uint32_t Depth = Die->getDepth();
433 // Unit DIEs always have a depth of zero and never have parents.
434 if (Depth == 0)
435 return DWARFDie();
436 // Depth of 1 always means parent is the compile/type unit.
437 if (Depth == 1)
438 return getUnitDIE();
439 // Look for previous DIE with a depth that is one less than the Die's depth.
440 const uint32_t ParentDepth = Depth - 1;
441 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
442 if (DieArray[I].getDepth() == ParentDepth)
443 return DWARFDie(this, &DieArray[I]);
444 }
445 return DWARFDie();
446}
447
448DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
449 if (!Die)
450 return DWARFDie();
451 uint32_t Depth = Die->getDepth();
452 // Unit DIEs always have a depth of zero and never have siblings.
453 if (Depth == 0)
454 return DWARFDie();
455 // NULL DIEs don't have siblings.
456 if (Die->getAbbreviationDeclarationPtr() == nullptr)
457 return DWARFDie();
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000458
Greg Clayton78a07bf2016-12-21 21:37:06 +0000459 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000460 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
461 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000462 if (DieArray[I].getDepth() == Depth)
463 return DWARFDie(this, &DieArray[I]);
464 }
465 return DWARFDie();
466}
David Blaikie485e01b2017-09-19 15:13:55 +0000467
George Rimar0be860f2017-10-25 10:23:49 +0000468DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) {
469 if (!Die->hasChildren())
470 return DWARFDie();
471
472 // We do not want access out of bounds when parsing corrupted debug data.
473 size_t I = getDIEIndex(Die) + 1;
474 if (I >= DieArray.size())
475 return DWARFDie();
476 return DWARFDie(this, &DieArray[I]);
477}
478
David Blaikie485e01b2017-09-19 15:13:55 +0000479const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const {
480 if (!Abbrevs)
481 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
482 return Abbrevs;
483}
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000484
485Optional<StrOffsetsContributionDescriptor>
486StrOffsetsContributionDescriptor::validateContributionSize(
487 DWARFDataExtractor &DA) {
488 uint8_t EntrySize = getDwarfOffsetByteSize();
489 // In order to ensure that we don't read a partial record at the end of
490 // the section we validate for a multiple of the entry size.
491 uint64_t ValidationSize = alignTo(Size, EntrySize);
492 // Guard against overflow.
493 if (ValidationSize >= Size)
494 if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize))
495 return *this;
496 return Optional<StrOffsetsContributionDescriptor>();
497}
498
499// Look for a DWARF64-formatted contribution to the string offsets table
500// starting at a given offset and record it in a descriptor.
501static Optional<StrOffsetsContributionDescriptor>
502parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
503 if (!DA.isValidOffsetForDataOfSize(Offset, 16))
504 return Optional<StrOffsetsContributionDescriptor>();
505
506 if (DA.getU32(&Offset) != 0xffffffff)
507 return Optional<StrOffsetsContributionDescriptor>();
508
509 uint64_t Size = DA.getU64(&Offset);
510 uint8_t Version = DA.getU16(&Offset);
511 (void)DA.getU16(&Offset); // padding
512 return StrOffsetsContributionDescriptor(Offset, Size, Version, DWARF64);
513 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
514}
515
516// Look for a DWARF32-formatted contribution to the string offsets table
517// starting at a given offset and record it in a descriptor.
518static Optional<StrOffsetsContributionDescriptor>
519parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
520 if (!DA.isValidOffsetForDataOfSize(Offset, 8))
521 return Optional<StrOffsetsContributionDescriptor>();
522 uint32_t ContributionSize = DA.getU32(&Offset);
523 if (ContributionSize >= 0xfffffff0)
524 return Optional<StrOffsetsContributionDescriptor>();
525 uint8_t Version = DA.getU16(&Offset);
526 (void)DA.getU16(&Offset); // padding
527 return StrOffsetsContributionDescriptor(Offset, ContributionSize, Version, DWARF32);
528 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
529}
530
531Optional<StrOffsetsContributionDescriptor>
532DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA,
533 uint64_t Offset) {
534 Optional<StrOffsetsContributionDescriptor> Descriptor;
535 // Attempt to find a DWARF64 contribution 16 bytes before the base.
536 if (Offset >= 16)
537 Descriptor =
538 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset - 16);
539 // Try to find a DWARF32 contribution 8 bytes before the base.
540 if (!Descriptor && Offset >= 8)
541 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset - 8);
542 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
543}
544
545Optional<StrOffsetsContributionDescriptor>
546DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA,
547 uint64_t Offset) {
548 if (getVersion() >= 5) {
549 // Look for a valid contribution at the given offset.
550 auto Descriptor =
551 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset);
552 if (!Descriptor)
553 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset);
554 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
555 }
556 // Prior to DWARF v5, we derive the contribution size from the
557 // index table (in a package file). In a .dwo file it is simply
558 // the length of the string offsets section.
559 uint64_t Size = 0;
560 if (!IndexEntry)
561 Size = StringOffsetSection.Data.size();
562 else if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
563 Size = C->Length;
564 // Return a descriptor with the given offset as base, version 4 and
565 // DWARF32 format.
566 //return Optional<StrOffsetsContributionDescriptor>(
567 //StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32));
568 return StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32);
569}