1 //===-- DWARFUnit.cpp -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFUnit.h"
11 #include "DWARFContext.h"
12 #include "llvm/DebugInfo/DWARFFormValue.h"
13 #include "llvm/Support/Dwarf.h"
14 #include "llvm/Support/Path.h"
18 using namespace dwarf;
20 DWARFUnit::DWARFUnit(const DWARFDebugAbbrev *DA, StringRef IS, StringRef AS,
21 StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
22 const RelocAddrMap *M, bool LE)
23 : Abbrev(DA), InfoSection(IS), AbbrevSection(AS), RangeSection(RS),
24 StringSection(SS), StringOffsetSection(SOS), AddrOffsetSection(AOS),
25 RelocMap(M), isLittleEndian(LE) {
29 DWARFUnit::~DWARFUnit() {
32 bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
33 uint64_t &Result) const {
34 uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
35 if (AddrOffsetSection.size() < Offset + AddrSize)
37 DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
38 Result = DA.getAddress(&Offset);
42 bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
43 uint32_t &Result) const {
44 // FIXME: string offset section entries are 8-byte for DWARF64.
45 const uint32_t ItemSize = 4;
46 uint32_t Offset = Index * ItemSize;
47 if (StringOffsetSection.size() < Offset + ItemSize)
49 DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
50 Result = DA.getU32(&Offset);
54 bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
55 Length = debug_info.getU32(offset_ptr);
56 Version = debug_info.getU16(offset_ptr);
57 uint64_t abbrOffset = debug_info.getU32(offset_ptr);
58 AddrSize = debug_info.getU8(offset_ptr);
60 bool lengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
61 bool versionOK = DWARFContext::isSupportedVersion(Version);
62 bool abbrOffsetOK = AbbrevSection.size() > abbrOffset;
63 bool addrSizeOK = AddrSize == 4 || AddrSize == 8;
65 if (!lengthOK || !versionOK || !addrSizeOK || !abbrOffsetOK)
68 Abbrevs = Abbrev->getAbbreviationDeclarationSet(abbrOffset);
72 bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
77 if (debug_info.isValidOffset(*offset_ptr)) {
78 if (extractImpl(debug_info, offset_ptr))
81 // reset the offset to where we tried to parse from if anything went wrong
89 DWARFUnit::extract(uint32_t offset, DataExtractor debug_info_data,
90 const DWARFAbbreviationDeclarationSet *abbrevs) {
95 if (debug_info_data.isValidOffset(offset)) {
96 Length = debug_info_data.getU32(&offset);
97 Version = debug_info_data.getU16(&offset);
98 bool abbrevsOK = debug_info_data.getU32(&offset) == abbrevs->getOffset();
100 AddrSize = debug_info_data.getU8(&offset);
102 bool versionOK = DWARFContext::isSupportedVersion(Version);
103 bool addrSizeOK = AddrSize == 4 || AddrSize == 8;
105 if (versionOK && addrSizeOK && abbrevsOK &&
106 debug_info_data.isValidOffset(offset))
112 bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
113 DWARFDebugRangeList &RangeList) const {
114 // Require that compile unit is extracted.
115 assert(DieArray.size() > 0);
116 DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
117 uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
118 return RangeList.extract(RangesData, &ActualRangeListOffset);
121 void DWARFUnit::clear() {
128 RangeSectionBase = 0;
129 AddrOffsetSectionBase = 0;
134 const char *DWARFUnit::getCompilationDir() {
135 extractDIEsIfNeeded(true);
136 if (DieArray.empty())
138 return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, 0);
141 uint64_t DWARFUnit::getDWOId() {
142 extractDIEsIfNeeded(true);
143 const uint64_t FailValue = -1ULL;
144 if (DieArray.empty())
147 .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue);
150 void DWARFUnit::setDIERelations() {
151 if (DieArray.empty())
153 DWARFDebugInfoEntryMinimal *die_array_begin = &DieArray.front();
154 DWARFDebugInfoEntryMinimal *die_array_end = &DieArray.back();
155 DWARFDebugInfoEntryMinimal *curr_die;
156 // We purposely are skipping the last element in the array in the loop below
157 // so that we can always have a valid next item
158 for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die) {
159 // Since our loop doesn't include the last element, we can always
160 // safely access the next die in the array.
161 DWARFDebugInfoEntryMinimal *next_die = curr_die + 1;
163 const DWARFAbbreviationDeclaration *curr_die_abbrev =
164 curr_die->getAbbreviationDeclarationPtr();
166 if (curr_die_abbrev) {
168 if (curr_die_abbrev->hasChildren())
169 next_die->setParent(curr_die);
171 curr_die->setSibling(next_die);
173 // NULL DIE that terminates a sibling chain
174 DWARFDebugInfoEntryMinimal *parent = curr_die->getParent();
176 parent->setSibling(next_die);
180 // Since we skipped the last element, we need to fix it up!
181 if (die_array_begin < die_array_end)
182 curr_die->setParent(die_array_begin);
185 void DWARFUnit::extractDIEsToVector(
186 bool AppendCUDie, bool AppendNonCUDies,
187 std::vector<DWARFDebugInfoEntryMinimal> &Dies) const {
188 if (!AppendCUDie && !AppendNonCUDies)
191 // Set the offset to that of the first DIE and calculate the start of the
192 // next compilation unit header.
193 uint32_t Offset = getFirstDIEOffset();
194 uint32_t NextCUOffset = getNextUnitOffset();
195 DWARFDebugInfoEntryMinimal DIE;
199 while (Offset < NextCUOffset && DIE.extractFast(this, &Offset)) {
203 if (!AppendNonCUDies)
205 // The average bytes per DIE entry has been seen to be
206 // around 14-20 so let's pre-reserve the needed memory for
207 // our DIE entries accordingly.
208 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
214 const DWARFAbbreviationDeclaration *AbbrDecl =
215 DIE.getAbbreviationDeclarationPtr();
218 if (AbbrDecl->hasChildren())
225 break; // We are done with this compile unit!
229 // Give a little bit of info if we encounter corrupt DWARF (our offset
230 // should always terminate at or before the start of the next compilation
232 if (Offset > NextCUOffset)
233 fprintf(stderr, "warning: DWARF compile unit extends beyond its "
234 "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), Offset);
237 size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
238 if ((CUDieOnly && DieArray.size() > 0) ||
240 return 0; // Already parsed.
242 bool HasCUDie = DieArray.size() > 0;
243 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
245 if (DieArray.empty())
248 // If CU DIE was just parsed, copy several attribute values from it.
251 DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL);
252 if (BaseAddr == -1ULL)
253 BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0);
254 setBaseAddress(BaseAddr);
255 AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
256 this, DW_AT_GNU_addr_base, 0);
257 RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset(
258 this, DW_AT_GNU_ranges_base, 0);
262 return DieArray.size();
265 DWARFUnit::DWOHolder::DWOHolder(object::ObjectFile *DWOFile)
267 DWOContext(cast<DWARFContext>(DIContext::getDWARFContext(DWOFile))),
269 if (DWOContext->getNumDWOCompileUnits() > 0)
270 DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
273 bool DWARFUnit::parseDWO() {
276 extractDIEsIfNeeded(true);
277 if (DieArray.empty())
279 const char *DWOFileName =
280 DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, 0);
281 if (DWOFileName == 0)
283 const char *CompilationDir =
284 DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, 0);
285 SmallString<16> AbsolutePath;
286 if (sys::path::is_relative(DWOFileName) && CompilationDir != 0) {
287 sys::path::append(AbsolutePath, CompilationDir);
289 sys::path::append(AbsolutePath, DWOFileName);
290 object::ObjectFile *DWOFile =
291 object::ObjectFile::createObjectFile(AbsolutePath);
295 DWO.reset(new DWOHolder(DWOFile));
296 DWARFUnit *DWOCU = DWO->getUnit();
297 // Verify that compile unit in .dwo file is valid.
298 if (DWOCU == 0 || DWOCU->getDWOId() != getDWOId()) {
302 // Share .debug_addr and .debug_ranges section with compile unit in .dwo
303 DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
304 DWOCU->setRangesSection(RangeSection, RangeSectionBase);
308 void DWARFUnit::clearDIEs(bool KeepCUDie) {
309 if (DieArray.size() > (unsigned)KeepCUDie) {
310 // std::vectors never get any smaller when resized to a smaller size,
311 // or when clear() or erase() are called, the size will report that it
312 // is smaller, but the memory allocated remains intact (call capacity()
313 // to see this). So we need to create a temporary vector and swap the
314 // contents which will cause just the internal pointers to be swapped
315 // so that when temporary vector goes out of scope, it will destroy the
317 std::vector<DWARFDebugInfoEntryMinimal> TmpArray;
318 DieArray.swap(TmpArray);
319 // Save at least the compile unit DIE
321 DieArray.push_back(TmpArray.front());
326 DWARFUnit::buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
327 bool clear_dies_if_already_not_parsed,
328 uint32_t CUOffsetInAranges) {
329 // This function is usually called if there in no .debug_aranges section
330 // in order to produce a compile unit level set of address ranges that
331 // is accurate. If the DIEs weren't parsed, then we don't want all dies for
332 // all compile units to stay loaded when they weren't needed. So we can end
333 // up parsing the DWARF and then throwing them all away to keep memory usage
335 const bool clear_dies = extractDIEsIfNeeded(false) > 1 &&
336 clear_dies_if_already_not_parsed;
337 DieArray[0].buildAddressRangeTable(this, debug_aranges, CUOffsetInAranges);
338 bool DWOCreated = parseDWO();
340 // If there is a .dwo file for this compile unit, then skeleton CU DIE
341 // doesn't have children, and we should instead build address range table
342 // from DIEs in the .debug_info.dwo section of .dwo file.
343 DWO->getUnit()->buildAddressRangeTable(
344 debug_aranges, clear_dies_if_already_not_parsed, CUOffsetInAranges);
346 if (DWOCreated && clear_dies_if_already_not_parsed)
349 // Keep memory down by clearing DIEs if this generate function
350 // caused them to be parsed.
355 const DWARFDebugInfoEntryMinimal *
356 DWARFUnit::getSubprogramForAddress(uint64_t Address) {
357 extractDIEsIfNeeded(false);
358 for (size_t i = 0, n = DieArray.size(); i != n; i++)
359 if (DieArray[i].isSubprogramDIE() &&
360 DieArray[i].addressRangeContainsAddress(this, Address)) {
366 DWARFDebugInfoEntryInlinedChain
367 DWARFUnit::getInlinedChainForAddress(uint64_t Address) {
368 // First, find a subprogram that contains the given address (the root
369 // of inlined chain).
370 const DWARFUnit *ChainCU = 0;
371 const DWARFDebugInfoEntryMinimal *SubprogramDIE =
372 getSubprogramForAddress(Address);
376 // Try to look for subprogram DIEs in the DWO file.
379 SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
381 ChainCU = DWO->getUnit();
385 // Get inlined chain rooted at this subprogram DIE.
387 return DWARFDebugInfoEntryInlinedChain();
388 return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address);