blob: df55d7debf92628460a449cc8479fff4aa61fe67 [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 Carruth54a5ad32017-12-22 06:41:23 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000012#include "llvm/ADT/SmallString.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000013#include "llvm/ADT/StringRef.h"
14#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.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"
18#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"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000022#include <algorithm>
23#include <cassert>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000024#include <cstddef>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000025#include <cstdint>
David Blaikie8de5e982013-09-23 23:15:57 +000026#include <cstdio>
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000027#include <utility>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000028#include <vector>
David Blaikie07e22442013-09-23 22:44:40 +000029
Eugene Zelenko28db7e62017-03-01 01:14:23 +000030using namespace llvm;
David Blaikie07e22442013-09-23 22:44:40 +000031using namespace dwarf;
32
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000033void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
Rafael Espindolac398e672017-07-19 22:27:28 +000034 const DWARFObject &D = C.getDWARFObj();
35 parseImpl(C, Section, C.getDebugAbbrev(), &D.getRangeSection(),
36 D.getStringSection(), D.getStringOffsetSection(),
David Blaikiee79dda32017-09-19 18:36:11 +000037 &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false,
38 false);
Frederic Riss6005dbd2014-10-06 03:36:18 +000039}
40
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +000041void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
David Blaikiee79dda32017-09-19 18:36:11 +000042 const DWARFSection &DWOSection, bool Lazy) {
Rafael Espindolac398e672017-07-19 22:27:28 +000043 const DWARFObject &D = C.getDWARFObj();
44 parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(),
45 D.getStringDWOSection(), D.getStringOffsetDWOSection(),
46 &D.getAddrSection(), D.getLineDWOSection(), C.isLittleEndian(),
David Blaikiee79dda32017-09-19 18:36:11 +000047 true, Lazy);
Frederic Riss6005dbd2014-10-06 03:36:18 +000048}
49
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000050DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
George Rimarca532112017-04-24 10:19:45 +000051 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000052 StringRef SS, const DWARFSection &SOS,
Paul Robinson17536b92017-06-29 16:52:08 +000053 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
54 bool IsDWO, const DWARFUnitSectionBase &UnitSection,
David Blaikie82641be2015-11-17 00:39:55 +000055 const DWARFUnitIndex::Entry *IndexEntry)
Alexey Samsonov4b4d64b2014-10-08 00:24:41 +000056 : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000057 LineSection(LS), StringSection(SS), StringOffsetSection(SOS),
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000058 AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
59 UnitSection(UnitSection), IndexEntry(IndexEntry) {
David Blaikie07e22442013-09-23 22:44:40 +000060 clear();
61}
62
Eugene Zelenko570e39a2016-11-23 23:16:32 +000063DWARFUnit::~DWARFUnit() = default;
David Blaikie07e22442013-09-23 22:44:40 +000064
Rafael Espindolac398e672017-07-19 22:27:28 +000065DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const {
66 return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian,
67 getAddressByteSize());
68}
69
David Blaikie07e22442013-09-23 22:44:40 +000070bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
71 uint64_t &Result) const {
Paul Robinson75c068c2017-06-26 18:43:01 +000072 uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize();
73 if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize())
David Blaikie07e22442013-09-23 22:44:40 +000074 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000075 DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection,
76 isLittleEndian, getAddressByteSize());
Paul Robinson17536b92017-06-29 16:52:08 +000077 Result = DA.getRelocatedAddress(&Offset);
David Blaikie07e22442013-09-23 22:44:40 +000078 return true;
79}
80
81bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000082 uint64_t &Result) const {
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +000083 if (!StringOffsetsTableContribution)
84 return false;
85 unsigned ItemSize = getDwarfStringOffsetsByteSize();
86 uint32_t Offset = getStringOffsetsBase() + Index * ItemSize;
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000087 if (StringOffsetSection.Data.size() < Offset + ItemSize)
David Blaikie07e22442013-09-23 22:44:40 +000088 return false;
Rafael Espindolac398e672017-07-19 22:27:28 +000089 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
90 isLittleEndian, 0);
Paul Robinson17536b92017-06-29 16:52:08 +000091 Result = DA.getRelocatedValue(ItemSize, &Offset);
David Blaikie07e22442013-09-23 22:44:40 +000092 return true;
93}
94
95bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
96 Length = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +000097 // FIXME: Support DWARF64.
98 FormParams.Format = DWARF32;
99 FormParams.Version = debug_info.getU16(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000100 if (FormParams.Version >= 5) {
Paul Robinsoncddd6042017-02-28 20:24:55 +0000101 UnitType = debug_info.getU8(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000102 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000103 AbbrOffset = debug_info.getU32(offset_ptr);
104 } else {
105 AbbrOffset = debug_info.getU32(offset_ptr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000106 FormParams.AddrSize = debug_info.getU8(offset_ptr);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000107 }
David Blaikie82641be2015-11-17 00:39:55 +0000108 if (IndexEntry) {
109 if (AbbrOffset)
110 return false;
111 auto *UnitContrib = IndexEntry->getOffset();
112 if (!UnitContrib || UnitContrib->Length != (Length + 4))
113 return false;
114 auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
115 if (!AbbrEntry)
116 return false;
117 AbbrOffset = AbbrEntry->Offset;
118 }
David Blaikie07e22442013-09-23 22:44:40 +0000119
Alexey Samsonov7682f812014-04-24 22:51:03 +0000120 bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
Paul Robinson75c068c2017-06-26 18:43:01 +0000121 bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
122 bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;
David Blaikie07e22442013-09-23 22:44:40 +0000123
Alexey Samsonov7682f812014-04-24 22:51:03 +0000124 if (!LengthOK || !VersionOK || !AddrSizeOK)
David Blaikie07e22442013-09-23 22:44:40 +0000125 return false;
126
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000127 // Keep track of the highest DWARF version we encounter across all units.
Paul Robinson75c068c2017-06-26 18:43:01 +0000128 Context.setMaxVersionIfGreater(getVersion());
David Blaikie485e01b2017-09-19 15:13:55 +0000129 return true;
David Blaikie07e22442013-09-23 22:44:40 +0000130}
131
132bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
133 clear();
134
135 Offset = *offset_ptr;
136
137 if (debug_info.isValidOffset(*offset_ptr)) {
138 if (extractImpl(debug_info, offset_ptr))
139 return true;
140
141 // reset the offset to where we tried to parse from if anything went wrong
142 *offset_ptr = Offset;
143 }
144
145 return false;
146}
147
David Blaikie07e22442013-09-23 22:44:40 +0000148bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
Paul Robinson17536b92017-06-29 16:52:08 +0000149 DWARFDebugRangeList &RangeList) const {
David Blaikie07e22442013-09-23 22:44:40 +0000150 // Require that compile unit is extracted.
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000151 assert(!DieArray.empty());
Rafael Espindolac398e672017-07-19 22:27:28 +0000152 DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection,
153 isLittleEndian, getAddressByteSize());
David Blaikie07e22442013-09-23 22:44:40 +0000154 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
Paul Robinson17536b92017-06-29 16:52:08 +0000155 return RangeList.extract(RangesData, &ActualRangeListOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000156}
157
158void DWARFUnit::clear() {
159 Offset = 0;
160 Length = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +0000161 Abbrevs = nullptr;
Paul Robinson75c068c2017-06-26 18:43:01 +0000162 FormParams = DWARFFormParams({0, 0, DWARF32});
George Rimar2f95c8b2017-09-04 10:30:39 +0000163 BaseAddr.reset();
David Blaikie07e22442013-09-23 22:44:40 +0000164 RangeSectionBase = 0;
165 AddrOffsetSectionBase = 0;
166 clearDIEs(false);
167 DWO.reset();
168}
169
170const char *DWARFUnit::getCompilationDir() {
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000171 return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr);
David Blaikie07e22442013-09-23 22:44:40 +0000172}
173
Greg Clayton52fe1f62016-12-14 22:38:08 +0000174Optional<uint64_t> DWARFUnit::getDWOId() {
Greg Clayton97d22182017-01-13 21:08:18 +0000175 return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id));
David Blaikie07e22442013-09-23 22:44:40 +0000176}
177
David Blaikie07e22442013-09-23 22:44:40 +0000178void DWARFUnit::extractDIEsToVector(
179 bool AppendCUDie, bool AppendNonCUDies,
Greg Claytonc8c10322016-12-13 18:25:19 +0000180 std::vector<DWARFDebugInfoEntry> &Dies) const {
David Blaikie07e22442013-09-23 22:44:40 +0000181 if (!AppendCUDie && !AppendNonCUDies)
182 return;
183
184 // Set the offset to that of the first DIE and calculate the start of the
185 // next compilation unit header.
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000186 uint32_t DIEOffset = Offset + getHeaderSize();
David Blaikie07e22442013-09-23 22:44:40 +0000187 uint32_t NextCUOffset = getNextUnitOffset();
Greg Claytonc8c10322016-12-13 18:25:19 +0000188 DWARFDebugInfoEntry DIE;
Paul Robinson17536b92017-06-29 16:52:08 +0000189 DWARFDataExtractor DebugInfoData = getDebugInfoExtractor();
David Blaikie07e22442013-09-23 22:44:40 +0000190 uint32_t Depth = 0;
David Blaikie07e22442013-09-23 22:44:40 +0000191 bool IsCUDie = true;
192
Greg Clayton78a07bf2016-12-21 21:37:06 +0000193 while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
194 Depth)) {
David Blaikie07e22442013-09-23 22:44:40 +0000195 if (IsCUDie) {
196 if (AppendCUDie)
197 Dies.push_back(DIE);
198 if (!AppendNonCUDies)
199 break;
200 // The average bytes per DIE entry has been seen to be
201 // around 14-20 so let's pre-reserve the needed memory for
202 // our DIE entries accordingly.
203 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
204 IsCUDie = false;
205 } else {
206 Dies.push_back(DIE);
207 }
208
Alexey Samsonov8e4cf3b2014-04-29 17:12:42 +0000209 if (const DWARFAbbreviationDeclaration *AbbrDecl =
210 DIE.getAbbreviationDeclarationPtr()) {
David Blaikie07e22442013-09-23 22:44:40 +0000211 // Normal DIE
212 if (AbbrDecl->hasChildren())
213 ++Depth;
214 } else {
215 // NULL DIE.
216 if (Depth > 0)
217 --Depth;
218 if (Depth == 0)
219 break; // We are done with this compile unit!
220 }
221 }
222
223 // Give a little bit of info if we encounter corrupt DWARF (our offset
224 // should always terminate at or before the start of the next compilation
225 // unit header).
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000226 if (DIEOffset > NextCUOffset)
David Blaikie07e22442013-09-23 22:44:40 +0000227 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000228 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
David Blaikie07e22442013-09-23 22:44:40 +0000229}
230
231size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000232 if ((CUDieOnly && !DieArray.empty()) ||
David Blaikie07e22442013-09-23 22:44:40 +0000233 DieArray.size() > 1)
234 return 0; // Already parsed.
235
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000236 bool HasCUDie = !DieArray.empty();
David Blaikie07e22442013-09-23 22:44:40 +0000237 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
238
239 if (DieArray.empty())
240 return 0;
241
242 // If CU DIE was just parsed, copy several attribute values from it.
243 if (!HasCUDie) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000244 DWARFDie UnitDie = getUnitDIE();
George Rimar2f95c8b2017-09-04 10:30:39 +0000245 Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc});
246 if (Optional<uint64_t> Addr = toAddress(PC))
247 setBaseAddress({*Addr, PC->getSectionIndex()});
248
David Blaikie22dc4472017-08-02 20:16:22 +0000249 if (!isDWO) {
250 assert(AddrOffsetSectionBase == 0);
251 assert(RangeSectionBase == 0);
252 AddrOffsetSectionBase =
253 toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0);
254 RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0);
255 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000256
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000257 // In general, in DWARF v5 and beyond we derive the start of the unit's
258 // contribution to the string offsets table from the unit DIE's
259 // DW_AT_str_offsets_base attribute. Split DWARF units do not use this
260 // attribute, so we assume that there is a contribution to the string
261 // offsets table starting at offset 0 of the debug_str_offsets.dwo section.
262 // In both cases we need to determine the format of the contribution,
263 // which may differ from the unit's format.
264 uint64_t StringOffsetsContributionBase =
265 isDWO ? 0 : toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000266 if (IndexEntry)
267 if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000268 StringOffsetsContributionBase += C->Offset;
269
270 DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection,
271 isLittleEndian, 0);
272 if (isDWO)
273 StringOffsetsTableContribution =
274 determineStringOffsetsTableContributionDWO(
275 DA, StringOffsetsContributionBase);
276 else if (getVersion() >= 5)
277 StringOffsetsTableContribution = determineStringOffsetsTableContribution(
278 DA, StringOffsetsContributionBase);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000279
Alexey Samsonovaa909982014-06-13 22:31:03 +0000280 // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
281 // skeleton CU DIE, so that DWARF users not aware of it are not broken.
David Blaikie07e22442013-09-23 22:44:40 +0000282 }
283
David Blaikie07e22442013-09-23 22:44:40 +0000284 return DieArray.size();
285}
286
David Blaikie07e22442013-09-23 22:44:40 +0000287bool DWARFUnit::parseDWO() {
David Blaikiee438cff2016-04-22 22:50:56 +0000288 if (isDWO)
289 return false;
Craig Topper2617dcc2014-04-15 06:32:26 +0000290 if (DWO.get())
David Blaikie07e22442013-09-23 22:44:40 +0000291 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000292 DWARFDie UnitDie = getUnitDIE();
293 if (!UnitDie)
David Blaikie07e22442013-09-23 22:44:40 +0000294 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000295 auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name));
Craig Topper2617dcc2014-04-15 06:32:26 +0000296 if (!DWOFileName)
David Blaikie07e22442013-09-23 22:44:40 +0000297 return false;
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000298 auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir));
David Blaikie07e22442013-09-23 22:44:40 +0000299 SmallString<16> AbsolutePath;
Greg Clayton97d22182017-01-13 21:08:18 +0000300 if (sys::path::is_relative(*DWOFileName) && CompilationDir &&
301 *CompilationDir) {
302 sys::path::append(AbsolutePath, *CompilationDir);
David Blaikie07e22442013-09-23 22:44:40 +0000303 }
Greg Clayton97d22182017-01-13 21:08:18 +0000304 sys::path::append(AbsolutePath, *DWOFileName);
David Blaikie8d039d42017-05-20 03:32:49 +0000305 auto DWOId = getDWOId();
306 if (!DWOId)
307 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000308 auto DWOContext = Context.getDWOContext(AbsolutePath);
309 if (!DWOContext)
David Blaikie07e22442013-09-23 22:44:40 +0000310 return false;
David Blaikief9803fb2017-05-23 00:30:42 +0000311
David Blaikie15d85fc2017-05-23 06:48:53 +0000312 DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId);
313 if (!DWOCU)
David Blaikief9803fb2017-05-23 00:30:42 +0000314 return false;
David Blaikie15d85fc2017-05-23 06:48:53 +0000315 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
David Blaikie07e22442013-09-23 22:44:40 +0000316 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
David Blaikief9803fb2017-05-23 00:30:42 +0000317 DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
Greg Clayton52fe1f62016-12-14 22:38:08 +0000318 auto DWORangesBase = UnitDie.getRangesBaseAttribute();
David Blaikief9803fb2017-05-23 00:30:42 +0000319 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
David Blaikie07e22442013-09-23 22:44:40 +0000320 return true;
321}
322
323void DWARFUnit::clearDIEs(bool KeepCUDie) {
324 if (DieArray.size() > (unsigned)KeepCUDie) {
Benjamin Kramer295cf4d2017-08-01 14:38:08 +0000325 DieArray.resize((unsigned)KeepCUDie);
326 DieArray.shrink_to_fit();
David Blaikie07e22442013-09-23 22:44:40 +0000327 }
328}
329
Alexey Samsonov762343d2014-04-18 17:25:46 +0000330void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000331 DWARFDie UnitDie = getUnitDIE();
332 if (!UnitDie)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000333 return;
334 // First, check if unit DIE describes address ranges for the whole unit.
Greg Claytonc8c10322016-12-13 18:25:19 +0000335 const auto &CUDIERanges = UnitDie.getAddressRanges();
Alexey Samsonov84e24232014-04-18 20:30:27 +0000336 if (!CUDIERanges.empty()) {
337 CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
338 return;
339 }
340
David Blaikie07e22442013-09-23 22:44:40 +0000341 // This function is usually called if there in no .debug_aranges section
342 // in order to produce a compile unit level set of address ranges that
343 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
344 // all compile units to stay loaded when they weren't needed. So we can end
345 // up parsing the DWARF and then throwing them all away to keep memory usage
346 // down.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000347 const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
Greg Claytonc8c10322016-12-13 18:25:19 +0000348 getUnitDIE().collectChildrenAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000349
350 // Collect address ranges from DIEs in .dwo if necessary.
David Blaikie07e22442013-09-23 22:44:40 +0000351 bool DWOCreated = parseDWO();
David Blaikief9803fb2017-05-23 00:30:42 +0000352 if (DWO)
353 DWO->collectAddressRanges(CURanges);
Alexey Samsonov762343d2014-04-18 17:25:46 +0000354 if (DWOCreated)
David Blaikie07e22442013-09-23 22:44:40 +0000355 DWO.reset();
356
357 // Keep memory down by clearing DIEs if this generate function
358 // caused them to be parsed.
Alexey Samsonov762343d2014-04-18 17:25:46 +0000359 if (ClearDIEs)
David Blaikie07e22442013-09-23 22:44:40 +0000360 clearDIEs(true);
361}
362
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000363// Populates a map from PC addresses to subprogram DIEs.
364//
365// This routine tries to look at the smallest amount of the debug info it can
366// to locate the DIEs. This is because many subprograms will never end up being
367// read or needed at all. We want to be as lazy as possible.
368void DWARFUnit::buildSubprogramDIEAddrMap() {
369 assert(SubprogramDIEAddrMap.empty() && "Must only build this map once!");
370 SmallVector<DWARFDie, 16> Worklist;
371 Worklist.push_back(getUnitDIE());
372 do {
373 DWARFDie Die = Worklist.pop_back_val();
374
375 // Queue up child DIEs to recurse through.
376 // FIXME: This causes us to read a lot more debug info than we really need.
377 // We should look at pruning out DIEs which cannot transitively hold
378 // separate subprograms.
379 for (DWARFDie Child : Die.children())
380 Worklist.push_back(Child);
381
382 // If handling a non-subprogram DIE, nothing else to do.
383 if (!Die.isSubprogramDIE())
384 continue;
385
386 // For subprogram DIEs, store them, and insert relevant markers into the
387 // address map. We don't care about overlap at all here as DWARF doesn't
388 // meaningfully support that, so we simply will insert a range with no DIE
389 // starting from the high PC. In the event there are overlaps, sorting
390 // these may truncate things in surprising ways but still will allow
391 // lookups to proceed.
392 int DIEIndex = SubprogramDIEAddrInfos.size();
393 SubprogramDIEAddrInfos.push_back({Die, (uint64_t)-1, {}});
Dehao Chena364f092017-04-19 20:09:38 +0000394 for (const auto &R : Die.getAddressRanges()) {
395 // Ignore 0-sized ranges.
George Rimar4671f2e2017-05-16 12:30:59 +0000396 if (R.LowPC == R.HighPC)
Dehao Chena364f092017-04-19 20:09:38 +0000397 continue;
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000398
399 SubprogramDIEAddrMap.push_back({R.LowPC, DIEIndex});
400 SubprogramDIEAddrMap.push_back({R.HighPC, -1});
401
402 if (R.LowPC < SubprogramDIEAddrInfos.back().SubprogramBasePC)
403 SubprogramDIEAddrInfos.back().SubprogramBasePC = R.LowPC;
David Blaikie07e22442013-09-23 22:44:40 +0000404 }
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000405 } while (!Worklist.empty());
406
407 if (SubprogramDIEAddrMap.empty()) {
408 // If we found no ranges, create a no-op map so that lookups remain simple
409 // but never find anything.
410 SubprogramDIEAddrMap.push_back({0, -1});
411 return;
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000412 }
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000413
414 // Next, sort the ranges and remove both exact duplicates and runs with the
415 // same DIE index. We order the ranges so that non-empty ranges are
416 // preferred. Because there may be ties, we also need to use stable sort.
417 std::stable_sort(SubprogramDIEAddrMap.begin(), SubprogramDIEAddrMap.end(),
418 [](const std::pair<uint64_t, int64_t> &LHS,
419 const std::pair<uint64_t, int64_t> &RHS) {
420 if (LHS.first < RHS.first)
421 return true;
422 if (LHS.first > RHS.first)
423 return false;
424
425 // For ranges that start at the same address, keep the one
426 // with a DIE.
427 if (LHS.second != -1 && RHS.second == -1)
428 return true;
429
430 return false;
431 });
432 SubprogramDIEAddrMap.erase(
433 std::unique(SubprogramDIEAddrMap.begin(), SubprogramDIEAddrMap.end(),
434 [](const std::pair<uint64_t, int64_t> &LHS,
435 const std::pair<uint64_t, int64_t> &RHS) {
436 // If the start addresses are exactly the same, we can
437 // remove all but the first one as it is the only one that
438 // will be found and used.
439 //
440 // If the DIE indices are the same, we can "merge" the
441 // ranges by eliminating the second.
442 return LHS.first == RHS.first || LHS.second == RHS.second;
443 }),
444 SubprogramDIEAddrMap.end());
445
446 assert(SubprogramDIEAddrMap.back().second == -1 &&
447 "The last interval must not have a DIE as each DIE's address range is "
448 "bounded.");
449}
450
451// Build the second level of mapping from PC to DIE, specifically one that maps
452// a PC *within* a particular DWARF subprogram into a precise, maximally nested
453// inlined subroutine DIE (if any exists). We build a separate map for each
454// subprogram because many subprograms will never get queried for an address
455// and this allows us to be significantly lazier in reading the DWARF itself.
456void DWARFUnit::buildInlinedSubroutineDIEAddrMap(
457 SubprogramDIEAddrInfo &SPInfo) {
458 auto &AddrMap = SPInfo.InlinedSubroutineDIEAddrMap;
459 uint64_t BasePC = SPInfo.SubprogramBasePC;
460
461 auto SubroutineAddrMapSorter = [](const std::pair<int, int> &LHS,
462 const std::pair<int, int> &RHS) {
463 if (LHS.first < RHS.first)
464 return true;
465 if (LHS.first > RHS.first)
466 return false;
467
468 // For ranges that start at the same address, keep the
469 // non-empty one.
470 if (LHS.second != -1 && RHS.second == -1)
471 return true;
472
473 return false;
474 };
475 auto SubroutineAddrMapUniquer = [](const std::pair<int, int> &LHS,
476 const std::pair<int, int> &RHS) {
477 // If the start addresses are exactly the same, we can
478 // remove all but the first one as it is the only one that
479 // will be found and used.
480 //
481 // If the DIE indices are the same, we can "merge" the
482 // ranges by eliminating the second.
483 return LHS.first == RHS.first || LHS.second == RHS.second;
484 };
485
486 struct DieAndParentIntervalRange {
487 DWARFDie Die;
488 int ParentIntervalsBeginIdx, ParentIntervalsEndIdx;
489 };
490
491 SmallVector<DieAndParentIntervalRange, 16> Worklist;
492 auto EnqueueChildDIEs = [&](const DWARFDie &Die, int ParentIntervalsBeginIdx,
493 int ParentIntervalsEndIdx) {
494 for (DWARFDie Child : Die.children())
495 Worklist.push_back(
496 {Child, ParentIntervalsBeginIdx, ParentIntervalsEndIdx});
497 };
498 EnqueueChildDIEs(SPInfo.SubprogramDIE, 0, 0);
499 while (!Worklist.empty()) {
500 DWARFDie Die = Worklist.back().Die;
501 int ParentIntervalsBeginIdx = Worklist.back().ParentIntervalsBeginIdx;
502 int ParentIntervalsEndIdx = Worklist.back().ParentIntervalsEndIdx;
503 Worklist.pop_back();
504
505 // If we encounter a nested subprogram, simply ignore it. We map to
506 // (disjoint) subprograms before arriving here and we don't want to examine
507 // any inlined subroutines of an unrelated subpragram.
508 if (Die.getTag() == DW_TAG_subprogram)
509 continue;
510
511 // For non-subroutines, just recurse to keep searching for inlined
512 // subroutines.
513 if (Die.getTag() != DW_TAG_inlined_subroutine) {
514 EnqueueChildDIEs(Die, ParentIntervalsBeginIdx, ParentIntervalsEndIdx);
515 continue;
516 }
517
518 // Capture the inlined subroutine DIE that we will reference from the map.
519 int DIEIndex = InlinedSubroutineDIEs.size();
520 InlinedSubroutineDIEs.push_back(Die);
521
522 int DieIntervalsBeginIdx = AddrMap.size();
523 // First collect the PC ranges for this DIE into our subroutine interval
524 // map.
525 for (auto R : Die.getAddressRanges()) {
526 // Clamp the PCs to be above the base.
527 R.LowPC = std::max(R.LowPC, BasePC);
528 R.HighPC = std::max(R.HighPC, BasePC);
529 // Compute relative PCs from the subprogram base and drop down to an
530 // unsigned 32-bit int to represent them within the data structure. This
531 // lets us cover a 4gb single subprogram. Because subprograms may be
532 // partitioned into distant parts of a binary (think hot/cold
533 // partitioning) we want to preserve as much as we can here without
534 // burning extra memory. Past that, we will simply truncate and lose the
535 // ability to map those PCs to a DIE more precise than the subprogram.
536 const uint32_t MaxRelativePC = std::numeric_limits<uint32_t>::max();
537 uint32_t RelativeLowPC = (R.LowPC - BasePC) > (uint64_t)MaxRelativePC
538 ? MaxRelativePC
539 : (uint32_t)(R.LowPC - BasePC);
540 uint32_t RelativeHighPC = (R.HighPC - BasePC) > (uint64_t)MaxRelativePC
541 ? MaxRelativePC
542 : (uint32_t)(R.HighPC - BasePC);
543 // Ignore empty or bogus ranges.
544 if (RelativeLowPC >= RelativeHighPC)
545 continue;
546 AddrMap.push_back({RelativeLowPC, DIEIndex});
547 AddrMap.push_back({RelativeHighPC, -1});
548 }
549
550 // If there are no address ranges, there is nothing to do to map into them
551 // and there cannot be any child subroutine DIEs with address ranges of
552 // interest as those would all be required to nest within this DIE's
553 // non-existent ranges, so we can immediately continue to the next DIE in
554 // the worklist.
555 if (DieIntervalsBeginIdx == (int)AddrMap.size())
556 continue;
557
558 // The PCs from this DIE should never overlap, so we can easily sort them
559 // here.
560 std::sort(AddrMap.begin() + DieIntervalsBeginIdx, AddrMap.end(),
561 SubroutineAddrMapSorter);
562 // Remove any dead ranges. These should only come from "empty" ranges that
563 // were clobbered by some other range.
564 AddrMap.erase(std::unique(AddrMap.begin() + DieIntervalsBeginIdx,
565 AddrMap.end(), SubroutineAddrMapUniquer),
566 AddrMap.end());
567
568 // Compute the end index of this DIE's addr map intervals.
569 int DieIntervalsEndIdx = AddrMap.size();
570
571 assert(DieIntervalsBeginIdx != DieIntervalsEndIdx &&
572 "Must not have an empty map for this layer!");
573 assert(AddrMap.back().second == -1 && "Must end with an empty range!");
574 assert(std::is_sorted(AddrMap.begin() + DieIntervalsBeginIdx, AddrMap.end(),
575 less_first()) &&
576 "Failed to sort this DIE's interals!");
577
578 // If we have any parent intervals, walk the newly added ranges and find
579 // the parent ranges they were inserted into. Both of these are sorted and
580 // neither has any overlaps. We need to append new ranges to split up any
581 // parent ranges these new ranges would overlap when we merge them.
582 if (ParentIntervalsBeginIdx != ParentIntervalsEndIdx) {
583 int ParentIntervalIdx = ParentIntervalsBeginIdx;
584 for (int i = DieIntervalsBeginIdx, e = DieIntervalsEndIdx - 1; i < e;
585 ++i) {
586 const uint32_t IntervalStart = AddrMap[i].first;
587 const uint32_t IntervalEnd = AddrMap[i + 1].first;
588 const int IntervalDieIdx = AddrMap[i].second;
589 if (IntervalDieIdx == -1) {
590 // For empty intervals, nothing is required. This is a bit surprising
591 // however. If the prior interval overlaps a parent interval and this
592 // would be necessary to mark the end, we will synthesize a new end
593 // that switches back to the parent DIE below. And this interval will
594 // get dropped in favor of one with a DIE attached. However, we'll
595 // still include this and so worst-case, it will still end the prior
596 // interval.
597 continue;
598 }
599
600 // We are walking the new ranges in order, so search forward from the
601 // last point for a parent range that might overlap.
602 auto ParentIntervalsRange =
603 make_range(AddrMap.begin() + ParentIntervalIdx,
604 AddrMap.begin() + ParentIntervalsEndIdx);
605 assert(std::is_sorted(ParentIntervalsRange.begin(),
606 ParentIntervalsRange.end(), less_first()) &&
607 "Unsorted parent intervals can't be searched!");
608 auto PI = std::upper_bound(
609 ParentIntervalsRange.begin(), ParentIntervalsRange.end(),
610 IntervalStart,
611 [](uint32_t LHS, const std::pair<uint32_t, int32_t> &RHS) {
612 return LHS < RHS.first;
613 });
614 if (PI == ParentIntervalsRange.begin() ||
615 PI == ParentIntervalsRange.end())
616 continue;
617
618 ParentIntervalIdx = PI - AddrMap.begin();
619 int32_t &ParentIntervalDieIdx = std::prev(PI)->second;
620 uint32_t &ParentIntervalStart = std::prev(PI)->first;
621 const uint32_t ParentIntervalEnd = PI->first;
622
623 // If the new range starts exactly at the position of the parent range,
624 // we need to adjust the parent range. Note that these collisions can
625 // only happen with the original parent range because we will merge any
626 // adjacent ranges in the child.
627 if (IntervalStart == ParentIntervalStart) {
628 // If there will be a tail, just shift the start of the parent
629 // forward. Note that this cannot change the parent ordering.
630 if (IntervalEnd < ParentIntervalEnd) {
631 ParentIntervalStart = IntervalEnd;
632 continue;
633 }
634 // Otherwise, mark this as becoming empty so we'll remove it and
635 // prefer the child range.
636 ParentIntervalDieIdx = -1;
637 continue;
638 }
639
640 // Finally, if the parent interval will need to remain as a prefix to
641 // this one, insert a new interval to cover any tail.
642 if (IntervalEnd < ParentIntervalEnd)
643 AddrMap.push_back({IntervalEnd, ParentIntervalDieIdx});
644 }
645 }
646
647 // Note that we don't need to re-sort even this DIE's address map intervals
648 // after this. All of the newly added intervals actually fill in *gaps* in
649 // this DIE's address map, and we know that children won't need to lookup
650 // into those gaps.
651
652 // Recurse through its children, giving them the interval map range of this
653 // DIE to use as their parent intervals.
654 EnqueueChildDIEs(Die, DieIntervalsBeginIdx, DieIntervalsEndIdx);
655 }
656
657 if (AddrMap.empty()) {
658 AddrMap.push_back({0, -1});
659 return;
660 }
661
662 // Now that we've added all of the intervals needed, we need to resort and
663 // unique them. Most notably, this will remove all the empty ranges that had
664 // a parent range covering, etc. We only expect a single non-empty interval
665 // at any given start point, so we just use std::sort. This could potentially
666 // produce non-deterministic maps for invalid DWARF.
667 std::sort(AddrMap.begin(), AddrMap.end(), SubroutineAddrMapSorter);
668 AddrMap.erase(
669 std::unique(AddrMap.begin(), AddrMap.end(), SubroutineAddrMapUniquer),
670 AddrMap.end());
Dehao Chena364f092017-04-19 20:09:38 +0000671}
672
673DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) {
674 extractDIEsIfNeeded(false);
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000675
676 // We use a two-level mapping structure to locate subroutines for a given PC
677 // address.
678 //
679 // First, we map the address to a subprogram. This can be done more cheaply
680 // because subprograms cannot nest within each other. It also allows us to
681 // avoid detailed examination of many subprograms, instead only focusing on
682 // the ones which we end up actively querying.
683 if (SubprogramDIEAddrMap.empty())
684 buildSubprogramDIEAddrMap();
685
686 assert(!SubprogramDIEAddrMap.empty() &&
687 "We must always end up with a non-empty map!");
688
689 auto I = std::upper_bound(
690 SubprogramDIEAddrMap.begin(), SubprogramDIEAddrMap.end(), Address,
691 [](uint64_t LHS, const std::pair<uint64_t, int64_t> &RHS) {
692 return LHS < RHS.first;
693 });
694 // If we find the beginning, then the address is before the first subprogram.
695 if (I == SubprogramDIEAddrMap.begin())
Dehao Chena364f092017-04-19 20:09:38 +0000696 return DWARFDie();
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000697 // Back up to the interval containing the address and see if it
698 // has a DIE associated with it.
699 --I;
700 if (I->second == -1)
Dehao Chena364f092017-04-19 20:09:38 +0000701 return DWARFDie();
Chandler Carruth54a5ad32017-12-22 06:41:23 +0000702
703 auto &SPInfo = SubprogramDIEAddrInfos[I->second];
704
705 // Now that we have the subprogram for this address, we do the second level
706 // mapping by building a map within a subprogram's PC range to any specific
707 // inlined subroutine.
708 if (SPInfo.InlinedSubroutineDIEAddrMap.empty())
709 buildInlinedSubroutineDIEAddrMap(SPInfo);
710
711 // We lookup within the inlined subroutine using a subprogram-relative
712 // address.
713 assert(Address >= SPInfo.SubprogramBasePC &&
714 "Address isn't above the start of the subprogram!");
715 uint32_t RelativeAddr = ((Address - SPInfo.SubprogramBasePC) >
716 (uint64_t)std::numeric_limits<uint32_t>::max())
717 ? std::numeric_limits<uint32_t>::max()
718 : (uint32_t)(Address - SPInfo.SubprogramBasePC);
719
720 auto J =
721 std::upper_bound(SPInfo.InlinedSubroutineDIEAddrMap.begin(),
722 SPInfo.InlinedSubroutineDIEAddrMap.end(), RelativeAddr,
723 [](uint32_t LHS, const std::pair<uint32_t, int32_t> &RHS) {
724 return LHS < RHS.first;
725 });
726 // If we find the beginning, the address is before any inlined subroutine so
727 // return the subprogram DIE.
728 if (J == SPInfo.InlinedSubroutineDIEAddrMap.begin())
729 return SPInfo.SubprogramDIE;
730 // Back up `J` and return the inlined subroutine if we have one or the
731 // subprogram if we don't.
732 --J;
733 return J->second == -1 ? SPInfo.SubprogramDIE
734 : InlinedSubroutineDIEs[J->second];
David Blaikie07e22442013-09-23 22:44:40 +0000735}
736
Greg Claytonc8c10322016-12-13 18:25:19 +0000737void
738DWARFUnit::getInlinedChainForAddress(uint64_t Address,
739 SmallVectorImpl<DWARFDie> &InlinedChain) {
Dehao Chendb569ba2017-04-19 20:52:21 +0000740 assert(InlinedChain.empty());
David Blaikie9a4f3cb2016-04-22 21:32:59 +0000741 // Try to look for subprogram DIEs in the DWO file.
742 parseDWO();
Dehao Chendb569ba2017-04-19 20:52:21 +0000743 // First, find the subroutine that contains the given address (the leaf
744 // of inlined chain).
745 DWARFDie SubroutineDIE =
David Blaikief9803fb2017-05-23 00:30:42 +0000746 (DWO ? DWO.get() : this)->getSubroutineForAddress(Address);
David Blaikie07e22442013-09-23 22:44:40 +0000747
Dehao Chendb569ba2017-04-19 20:52:21 +0000748 while (SubroutineDIE) {
749 if (SubroutineDIE.isSubroutineDIE())
750 InlinedChain.push_back(SubroutineDIE);
751 SubroutineDIE = SubroutineDIE.getParent();
752 }
David Blaikie07e22442013-09-23 22:44:40 +0000753}
David Blaikie82641be2015-11-17 00:39:55 +0000754
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000755const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context,
756 DWARFSectionKind Kind) {
David Blaikie82641be2015-11-17 00:39:55 +0000757 if (Kind == DW_SECT_INFO)
758 return Context.getCUIndex();
759 assert(Kind == DW_SECT_TYPES);
760 return Context.getTUIndex();
761}
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000762
Greg Clayton78a07bf2016-12-21 21:37:06 +0000763DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
764 if (!Die)
765 return DWARFDie();
766 const uint32_t Depth = Die->getDepth();
767 // Unit DIEs always have a depth of zero and never have parents.
768 if (Depth == 0)
769 return DWARFDie();
770 // Depth of 1 always means parent is the compile/type unit.
771 if (Depth == 1)
772 return getUnitDIE();
773 // Look for previous DIE with a depth that is one less than the Die's depth.
774 const uint32_t ParentDepth = Depth - 1;
775 for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
776 if (DieArray[I].getDepth() == ParentDepth)
777 return DWARFDie(this, &DieArray[I]);
778 }
779 return DWARFDie();
780}
781
782DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
783 if (!Die)
784 return DWARFDie();
785 uint32_t Depth = Die->getDepth();
786 // Unit DIEs always have a depth of zero and never have siblings.
787 if (Depth == 0)
788 return DWARFDie();
789 // NULL DIEs don't have siblings.
790 if (Die->getAbbreviationDeclarationPtr() == nullptr)
791 return DWARFDie();
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000792
Greg Clayton78a07bf2016-12-21 21:37:06 +0000793 // Find the next DIE whose depth is the same as the Die's depth.
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000794 for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx;
795 ++I) {
Greg Clayton78a07bf2016-12-21 21:37:06 +0000796 if (DieArray[I].getDepth() == Depth)
797 return DWARFDie(this, &DieArray[I]);
798 }
799 return DWARFDie();
800}
David Blaikie485e01b2017-09-19 15:13:55 +0000801
George Rimar0be860f2017-10-25 10:23:49 +0000802DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) {
803 if (!Die->hasChildren())
804 return DWARFDie();
805
806 // We do not want access out of bounds when parsing corrupted debug data.
807 size_t I = getDIEIndex(Die) + 1;
808 if (I >= DieArray.size())
809 return DWARFDie();
810 return DWARFDie(this, &DieArray[I]);
811}
812
David Blaikie485e01b2017-09-19 15:13:55 +0000813const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const {
814 if (!Abbrevs)
815 Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
816 return Abbrevs;
817}
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000818
819Optional<StrOffsetsContributionDescriptor>
820StrOffsetsContributionDescriptor::validateContributionSize(
821 DWARFDataExtractor &DA) {
822 uint8_t EntrySize = getDwarfOffsetByteSize();
823 // In order to ensure that we don't read a partial record at the end of
824 // the section we validate for a multiple of the entry size.
825 uint64_t ValidationSize = alignTo(Size, EntrySize);
826 // Guard against overflow.
827 if (ValidationSize >= Size)
828 if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize))
829 return *this;
830 return Optional<StrOffsetsContributionDescriptor>();
831}
832
833// Look for a DWARF64-formatted contribution to the string offsets table
834// starting at a given offset and record it in a descriptor.
835static Optional<StrOffsetsContributionDescriptor>
836parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
837 if (!DA.isValidOffsetForDataOfSize(Offset, 16))
838 return Optional<StrOffsetsContributionDescriptor>();
839
840 if (DA.getU32(&Offset) != 0xffffffff)
841 return Optional<StrOffsetsContributionDescriptor>();
842
843 uint64_t Size = DA.getU64(&Offset);
844 uint8_t Version = DA.getU16(&Offset);
845 (void)DA.getU16(&Offset); // padding
846 return StrOffsetsContributionDescriptor(Offset, Size, Version, DWARF64);
847 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
848}
849
850// Look for a DWARF32-formatted contribution to the string offsets table
851// starting at a given offset and record it in a descriptor.
852static Optional<StrOffsetsContributionDescriptor>
853parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) {
854 if (!DA.isValidOffsetForDataOfSize(Offset, 8))
855 return Optional<StrOffsetsContributionDescriptor>();
856 uint32_t ContributionSize = DA.getU32(&Offset);
857 if (ContributionSize >= 0xfffffff0)
858 return Optional<StrOffsetsContributionDescriptor>();
859 uint8_t Version = DA.getU16(&Offset);
860 (void)DA.getU16(&Offset); // padding
861 return StrOffsetsContributionDescriptor(Offset, ContributionSize, Version, DWARF32);
862 //return Optional<StrOffsetsContributionDescriptor>(Descriptor);
863}
864
865Optional<StrOffsetsContributionDescriptor>
866DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA,
867 uint64_t Offset) {
868 Optional<StrOffsetsContributionDescriptor> Descriptor;
869 // Attempt to find a DWARF64 contribution 16 bytes before the base.
870 if (Offset >= 16)
871 Descriptor =
872 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset - 16);
873 // Try to find a DWARF32 contribution 8 bytes before the base.
874 if (!Descriptor && Offset >= 8)
875 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset - 8);
876 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
877}
878
879Optional<StrOffsetsContributionDescriptor>
880DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA,
881 uint64_t Offset) {
882 if (getVersion() >= 5) {
883 // Look for a valid contribution at the given offset.
884 auto Descriptor =
885 parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset);
886 if (!Descriptor)
887 Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset);
888 return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor;
889 }
890 // Prior to DWARF v5, we derive the contribution size from the
891 // index table (in a package file). In a .dwo file it is simply
892 // the length of the string offsets section.
893 uint64_t Size = 0;
894 if (!IndexEntry)
895 Size = StringOffsetSection.Data.size();
896 else if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
897 Size = C->Length;
898 // Return a descriptor with the given offset as base, version 4 and
899 // DWARF32 format.
900 //return Optional<StrOffsetsContributionDescriptor>(
901 //StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32));
902 return StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32);
903}