David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 1 | //===-- DWARFUnit.cpp -----------------------------------------------------===// |
| 2 | // |
| 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 | |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFUnit.h" |
| 11 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 12 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Dwarf.h" |
| 14 | #include "llvm/Support/Path.h" |
David Blaikie | 8de5e98 | 2013-09-23 23:15:57 +0000 | [diff] [blame] | 15 | #include <cstdio> |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 16 | |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 17 | namespace llvm { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 18 | using namespace dwarf; |
| 19 | |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 20 | void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) { |
| 21 | parseImpl(C, Section, C.getDebugAbbrev(), C.getRangeSection(), |
| 22 | C.getStringSection(), StringRef(), C.getAddrSection(), |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 23 | C.getLineSection().Data, C.isLittleEndian()); |
Frederic Riss | 6005dbd | 2014-10-06 03:36:18 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 26 | void DWARFUnitSectionBase::parseDWO(DWARFContext &C, |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 27 | const DWARFSection &DWOSection, |
| 28 | DWARFUnitIndex *Index) { |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 29 | parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), C.getRangeDWOSection(), |
Frederic Riss | 6005dbd | 2014-10-06 03:36:18 +0000 | [diff] [blame] | 30 | C.getStringDWOSection(), C.getStringOffsetDWOSection(), |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 31 | C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian()); |
Frederic Riss | 6005dbd | 2014-10-06 03:36:18 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Alexey Samsonov | 4b4d64b | 2014-10-08 00:24:41 +0000 | [diff] [blame] | 34 | DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section, |
| 35 | const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS, |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 36 | StringRef SOS, StringRef AOS, StringRef LS, bool LE, |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 37 | const DWARFUnitSectionBase &UnitSection, |
| 38 | const DWARFUnitIndex::Entry *IndexEntry) |
Alexey Samsonov | 4b4d64b | 2014-10-08 00:24:41 +0000 | [diff] [blame] | 39 | : Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS), |
David Blaikie | 35c2eeb | 2015-11-17 22:39:23 +0000 | [diff] [blame] | 40 | LineSection(LS), StringSection(SS), StringOffsetSection([&]() { |
David Blaikie | 4689ef5 | 2015-11-17 22:39:26 +0000 | [diff] [blame] | 41 | if (IndexEntry) |
| 42 | if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS)) |
| 43 | return SOS.slice(C->Offset, C->Offset + C->Length); |
David Blaikie | 35c2eeb | 2015-11-17 22:39:23 +0000 | [diff] [blame] | 44 | return SOS; |
| 45 | }()), |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 46 | AddrOffsetSection(AOS), isLittleEndian(LE), UnitSection(UnitSection), |
| 47 | IndexEntry(IndexEntry) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 48 | clear(); |
| 49 | } |
| 50 | |
| 51 | DWARFUnit::~DWARFUnit() { |
| 52 | } |
| 53 | |
| 54 | bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index, |
| 55 | uint64_t &Result) const { |
| 56 | uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize; |
| 57 | if (AddrOffsetSection.size() < Offset + AddrSize) |
| 58 | return false; |
| 59 | DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize); |
| 60 | Result = DA.getAddress(&Offset); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index, |
| 65 | uint32_t &Result) const { |
| 66 | // FIXME: string offset section entries are 8-byte for DWARF64. |
| 67 | const uint32_t ItemSize = 4; |
| 68 | uint32_t Offset = Index * ItemSize; |
| 69 | if (StringOffsetSection.size() < Offset + ItemSize) |
| 70 | return false; |
| 71 | DataExtractor DA(StringOffsetSection, isLittleEndian, 0); |
| 72 | Result = DA.getU32(&Offset); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) { |
| 77 | Length = debug_info.getU32(offset_ptr); |
| 78 | Version = debug_info.getU16(offset_ptr); |
Alexey Samsonov | 7682f81 | 2014-04-24 22:51:03 +0000 | [diff] [blame] | 79 | uint64_t AbbrOffset = debug_info.getU32(offset_ptr); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 80 | if (IndexEntry) { |
| 81 | if (AbbrOffset) |
| 82 | return false; |
| 83 | auto *UnitContrib = IndexEntry->getOffset(); |
| 84 | if (!UnitContrib || UnitContrib->Length != (Length + 4)) |
| 85 | return false; |
| 86 | auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV); |
| 87 | if (!AbbrEntry) |
| 88 | return false; |
| 89 | AbbrOffset = AbbrEntry->Offset; |
| 90 | } |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 91 | AddrSize = debug_info.getU8(offset_ptr); |
| 92 | |
Alexey Samsonov | 7682f81 | 2014-04-24 22:51:03 +0000 | [diff] [blame] | 93 | bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1); |
| 94 | bool VersionOK = DWARFContext::isSupportedVersion(Version); |
| 95 | bool AddrSizeOK = AddrSize == 4 || AddrSize == 8; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 96 | |
Alexey Samsonov | 7682f81 | 2014-04-24 22:51:03 +0000 | [diff] [blame] | 97 | if (!LengthOK || !VersionOK || !AddrSizeOK) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 98 | return false; |
| 99 | |
Alexey Samsonov | 7682f81 | 2014-04-24 22:51:03 +0000 | [diff] [blame] | 100 | Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset); |
Benjamin Kramer | 68a2956 | 2015-05-25 13:28:03 +0000 | [diff] [blame] | 101 | return Abbrevs != nullptr; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) { |
| 105 | clear(); |
| 106 | |
| 107 | Offset = *offset_ptr; |
| 108 | |
| 109 | if (debug_info.isValidOffset(*offset_ptr)) { |
| 110 | if (extractImpl(debug_info, offset_ptr)) |
| 111 | return true; |
| 112 | |
| 113 | // reset the offset to where we tried to parse from if anything went wrong |
| 114 | *offset_ptr = Offset; |
| 115 | } |
| 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 120 | bool DWARFUnit::extractRangeList(uint32_t RangeListOffset, |
| 121 | DWARFDebugRangeList &RangeList) const { |
| 122 | // Require that compile unit is extracted. |
| 123 | assert(DieArray.size() > 0); |
| 124 | DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize); |
| 125 | uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset; |
| 126 | return RangeList.extract(RangesData, &ActualRangeListOffset); |
| 127 | } |
| 128 | |
| 129 | void DWARFUnit::clear() { |
| 130 | Offset = 0; |
| 131 | Length = 0; |
| 132 | Version = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 133 | Abbrevs = nullptr; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 134 | AddrSize = 0; |
| 135 | BaseAddr = 0; |
| 136 | RangeSectionBase = 0; |
| 137 | AddrOffsetSectionBase = 0; |
| 138 | clearDIEs(false); |
| 139 | DWO.reset(); |
| 140 | } |
| 141 | |
| 142 | const char *DWARFUnit::getCompilationDir() { |
| 143 | extractDIEsIfNeeded(true); |
| 144 | if (DieArray.empty()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 145 | return nullptr; |
| 146 | return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | uint64_t DWARFUnit::getDWOId() { |
| 150 | extractDIEsIfNeeded(true); |
| 151 | const uint64_t FailValue = -1ULL; |
| 152 | if (DieArray.empty()) |
| 153 | return FailValue; |
| 154 | return DieArray[0] |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 155 | .getAttributeValueAsUnsignedConstant(this, DW_AT_GNU_dwo_id, FailValue); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void DWARFUnit::setDIERelations() { |
Alexey Samsonov | 8e4cf3b | 2014-04-29 17:12:42 +0000 | [diff] [blame] | 159 | if (DieArray.size() <= 1) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 160 | return; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 161 | |
Alexey Samsonov | 8e4cf3b | 2014-04-29 17:12:42 +0000 | [diff] [blame] | 162 | std::vector<DWARFDebugInfoEntryMinimal *> ParentChain; |
| 163 | DWARFDebugInfoEntryMinimal *SiblingChain = nullptr; |
| 164 | for (auto &DIE : DieArray) { |
| 165 | if (SiblingChain) { |
| 166 | SiblingChain->setSibling(&DIE); |
| 167 | } |
| 168 | if (const DWARFAbbreviationDeclaration *AbbrDecl = |
| 169 | DIE.getAbbreviationDeclarationPtr()) { |
| 170 | // Normal DIE. |
| 171 | if (AbbrDecl->hasChildren()) { |
| 172 | ParentChain.push_back(&DIE); |
| 173 | SiblingChain = nullptr; |
| 174 | } else { |
| 175 | SiblingChain = &DIE; |
| 176 | } |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 177 | } else { |
Alexey Samsonov | 8e4cf3b | 2014-04-29 17:12:42 +0000 | [diff] [blame] | 178 | // NULL entry terminates the sibling chain. |
| 179 | SiblingChain = ParentChain.back(); |
| 180 | ParentChain.pop_back(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Alexey Samsonov | 8e4cf3b | 2014-04-29 17:12:42 +0000 | [diff] [blame] | 183 | assert(SiblingChain == nullptr || SiblingChain == &DieArray[0]); |
| 184 | assert(ParentChain.empty()); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void DWARFUnit::extractDIEsToVector( |
| 188 | bool AppendCUDie, bool AppendNonCUDies, |
| 189 | std::vector<DWARFDebugInfoEntryMinimal> &Dies) const { |
| 190 | if (!AppendCUDie && !AppendNonCUDies) |
| 191 | return; |
| 192 | |
| 193 | // Set the offset to that of the first DIE and calculate the start of the |
| 194 | // next compilation unit header. |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 195 | uint32_t DIEOffset = Offset + getHeaderSize(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 196 | uint32_t NextCUOffset = getNextUnitOffset(); |
| 197 | DWARFDebugInfoEntryMinimal DIE; |
| 198 | uint32_t Depth = 0; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 199 | bool IsCUDie = true; |
| 200 | |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 201 | while (DIEOffset < NextCUOffset && DIE.extractFast(this, &DIEOffset)) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 202 | if (IsCUDie) { |
| 203 | if (AppendCUDie) |
| 204 | Dies.push_back(DIE); |
| 205 | if (!AppendNonCUDies) |
| 206 | break; |
| 207 | // The average bytes per DIE entry has been seen to be |
| 208 | // around 14-20 so let's pre-reserve the needed memory for |
| 209 | // our DIE entries accordingly. |
| 210 | Dies.reserve(Dies.size() + getDebugInfoSize() / 14); |
| 211 | IsCUDie = false; |
| 212 | } else { |
| 213 | Dies.push_back(DIE); |
| 214 | } |
| 215 | |
Alexey Samsonov | 8e4cf3b | 2014-04-29 17:12:42 +0000 | [diff] [blame] | 216 | if (const DWARFAbbreviationDeclaration *AbbrDecl = |
| 217 | DIE.getAbbreviationDeclarationPtr()) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 218 | // Normal DIE |
| 219 | if (AbbrDecl->hasChildren()) |
| 220 | ++Depth; |
| 221 | } else { |
| 222 | // NULL DIE. |
| 223 | if (Depth > 0) |
| 224 | --Depth; |
| 225 | if (Depth == 0) |
| 226 | break; // We are done with this compile unit! |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Give a little bit of info if we encounter corrupt DWARF (our offset |
| 231 | // should always terminate at or before the start of the next compilation |
| 232 | // unit header). |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 233 | if (DIEOffset > NextCUOffset) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 234 | fprintf(stderr, "warning: DWARF compile unit extends beyond its " |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 235 | "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) { |
| 239 | if ((CUDieOnly && DieArray.size() > 0) || |
| 240 | DieArray.size() > 1) |
| 241 | return 0; // Already parsed. |
| 242 | |
| 243 | bool HasCUDie = DieArray.size() > 0; |
| 244 | extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray); |
| 245 | |
| 246 | if (DieArray.empty()) |
| 247 | return 0; |
| 248 | |
| 249 | // If CU DIE was just parsed, copy several attribute values from it. |
| 250 | if (!HasCUDie) { |
| 251 | uint64_t BaseAddr = |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 252 | DieArray[0].getAttributeValueAsAddress(this, DW_AT_low_pc, -1ULL); |
| 253 | if (BaseAddr == -1ULL) |
| 254 | BaseAddr = DieArray[0].getAttributeValueAsAddress(this, DW_AT_entry_pc, 0); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 255 | setBaseAddress(BaseAddr); |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 256 | AddrOffsetSectionBase = DieArray[0].getAttributeValueAsSectionOffset( |
| 257 | this, DW_AT_GNU_addr_base, 0); |
Alexey Samsonov | aa90998 | 2014-06-13 22:31:03 +0000 | [diff] [blame] | 258 | RangeSectionBase = DieArray[0].getAttributeValueAsSectionOffset( |
| 259 | this, DW_AT_ranges_base, 0); |
| 260 | // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for |
| 261 | // skeleton CU DIE, so that DWARF users not aware of it are not broken. |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | setDIERelations(); |
| 265 | return DieArray.size(); |
| 266 | } |
| 267 | |
Alexey Samsonov | d3e1213 | 2014-09-05 19:29:45 +0000 | [diff] [blame] | 268 | DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath) |
| 269 | : DWOFile(), DWOContext(), DWOU(nullptr) { |
| 270 | auto Obj = object::ObjectFile::createObjectFile(DWOPath); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 271 | if (!Obj) { |
| 272 | // TODO: Actually report errors helpfully. |
| 273 | consumeError(Obj.takeError()); |
Alexey Samsonov | d3e1213 | 2014-09-05 19:29:45 +0000 | [diff] [blame] | 274 | return; |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 275 | } |
Alexey Samsonov | d3e1213 | 2014-09-05 19:29:45 +0000 | [diff] [blame] | 276 | DWOFile = std::move(Obj.get()); |
| 277 | DWOContext.reset( |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 278 | cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary()))); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 279 | if (DWOContext->getNumDWOCompileUnits() > 0) |
| 280 | DWOU = DWOContext->getDWOCompileUnitAtIndex(0); |
| 281 | } |
| 282 | |
| 283 | bool DWARFUnit::parseDWO() { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 284 | if (DWO.get()) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 285 | return false; |
| 286 | extractDIEsIfNeeded(true); |
| 287 | if (DieArray.empty()) |
| 288 | return false; |
| 289 | const char *DWOFileName = |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 290 | DieArray[0].getAttributeValueAsString(this, DW_AT_GNU_dwo_name, nullptr); |
| 291 | if (!DWOFileName) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 292 | return false; |
| 293 | const char *CompilationDir = |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 294 | DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, nullptr); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 295 | SmallString<16> AbsolutePath; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 296 | if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 297 | sys::path::append(AbsolutePath, CompilationDir); |
| 298 | } |
| 299 | sys::path::append(AbsolutePath, DWOFileName); |
Alexey Samsonov | d3e1213 | 2014-09-05 19:29:45 +0000 | [diff] [blame] | 300 | DWO = llvm::make_unique<DWOHolder>(AbsolutePath); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 301 | DWARFUnit *DWOCU = DWO->getUnit(); |
| 302 | // Verify that compile unit in .dwo file is valid. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 303 | if (!DWOCU || DWOCU->getDWOId() != getDWOId()) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 304 | DWO.reset(); |
| 305 | return false; |
| 306 | } |
| 307 | // Share .debug_addr and .debug_ranges section with compile unit in .dwo |
| 308 | DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase); |
Alexey Samsonov | aa90998 | 2014-06-13 22:31:03 +0000 | [diff] [blame] | 309 | uint32_t DWORangesBase = DieArray[0].getRangesBaseAttribute(this, 0); |
Alexey Samsonov | f0e4034 | 2014-06-12 18:52:35 +0000 | [diff] [blame] | 310 | DWOCU->setRangesSection(RangeSection, DWORangesBase); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | |
| 314 | void DWARFUnit::clearDIEs(bool KeepCUDie) { |
| 315 | if (DieArray.size() > (unsigned)KeepCUDie) { |
| 316 | // std::vectors never get any smaller when resized to a smaller size, |
| 317 | // or when clear() or erase() are called, the size will report that it |
| 318 | // is smaller, but the memory allocated remains intact (call capacity() |
| 319 | // to see this). So we need to create a temporary vector and swap the |
| 320 | // contents which will cause just the internal pointers to be swapped |
| 321 | // so that when temporary vector goes out of scope, it will destroy the |
| 322 | // contents. |
| 323 | std::vector<DWARFDebugInfoEntryMinimal> TmpArray; |
| 324 | DieArray.swap(TmpArray); |
| 325 | // Save at least the compile unit DIE |
| 326 | if (KeepCUDie) |
| 327 | DieArray.push_back(TmpArray.front()); |
| 328 | } |
| 329 | } |
| 330 | |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 331 | void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) { |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 332 | const auto *U = getUnitDIE(); |
| 333 | if (U == nullptr) |
| 334 | return; |
| 335 | // First, check if unit DIE describes address ranges for the whole unit. |
| 336 | const auto &CUDIERanges = U->getAddressRanges(this); |
Alexey Samsonov | 84e2423 | 2014-04-18 20:30:27 +0000 | [diff] [blame] | 337 | if (!CUDIERanges.empty()) { |
| 338 | CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end()); |
| 339 | return; |
| 340 | } |
| 341 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 342 | // This function is usually called if there in no .debug_aranges section |
| 343 | // in order to produce a compile unit level set of address ranges that |
| 344 | // is accurate. If the DIEs weren't parsed, then we don't want all dies for |
| 345 | // all compile units to stay loaded when they weren't needed. So we can end |
| 346 | // up parsing the DWARF and then throwing them all away to keep memory usage |
| 347 | // down. |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 348 | const bool ClearDIEs = extractDIEsIfNeeded(false) > 1; |
| 349 | DieArray[0].collectChildrenAddressRanges(this, CURanges); |
| 350 | |
| 351 | // Collect address ranges from DIEs in .dwo if necessary. |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 352 | bool DWOCreated = parseDWO(); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 353 | if (DWO.get()) |
| 354 | DWO->getUnit()->collectAddressRanges(CURanges); |
| 355 | if (DWOCreated) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 356 | DWO.reset(); |
| 357 | |
| 358 | // Keep memory down by clearing DIEs if this generate function |
| 359 | // caused them to be parsed. |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 360 | if (ClearDIEs) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 361 | clearDIEs(true); |
| 362 | } |
| 363 | |
| 364 | const DWARFDebugInfoEntryMinimal * |
| 365 | DWARFUnit::getSubprogramForAddress(uint64_t Address) { |
| 366 | extractDIEsIfNeeded(false); |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 367 | for (const DWARFDebugInfoEntryMinimal &DIE : DieArray) { |
| 368 | if (DIE.isSubprogramDIE() && |
| 369 | DIE.addressRangeContainsAddress(this, Address)) { |
| 370 | return &DIE; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 371 | } |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 372 | } |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 373 | return nullptr; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | DWARFDebugInfoEntryInlinedChain |
| 377 | DWARFUnit::getInlinedChainForAddress(uint64_t Address) { |
| 378 | // First, find a subprogram that contains the given address (the root |
| 379 | // of inlined chain). |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 380 | const DWARFUnit *ChainCU = nullptr; |
David Blaikie | 9a4f3cb | 2016-04-22 21:32:59 +0000 | [diff] [blame^] | 381 | const DWARFDebugInfoEntryMinimal *SubprogramDIE; |
| 382 | // Try to look for subprogram DIEs in the DWO file. |
| 383 | parseDWO(); |
| 384 | if (DWO) { |
| 385 | if ((SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address))) |
| 386 | ChainCU = DWO->getUnit(); |
| 387 | } else if ((SubprogramDIE = getSubprogramForAddress(Address))) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 388 | ChainCU = this; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 389 | |
| 390 | // Get inlined chain rooted at this subprogram DIE. |
| 391 | if (!SubprogramDIE) |
| 392 | return DWARFDebugInfoEntryInlinedChain(); |
| 393 | return SubprogramDIE->getInlinedChainForAddress(ChainCU, Address); |
| 394 | } |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 395 | |
| 396 | const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context, |
| 397 | DWARFSectionKind Kind) { |
| 398 | if (Kind == DW_SECT_INFO) |
| 399 | return Context.getCUIndex(); |
| 400 | assert(Kind == DW_SECT_TYPES); |
| 401 | return Context.getTUIndex(); |
| 402 | } |
| 403 | } |