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