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