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