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