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) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 33 | const DWARFObject &D = C.getDWARFObj(); |
| 34 | parseImpl(C, Section, C.getDebugAbbrev(), &D.getRangeSection(), |
| 35 | D.getStringSection(), D.getStringOffsetSection(), |
Paul Robinson | d0c89f8 | 2018-01-29 22:02:56 +0000 | [diff] [blame] | 36 | &D.getAddrSection(), D.getLineSection(), D.isLittleEndian(), false, |
| 37 | 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 | e79dda3 | 2017-09-19 18:36:11 +0000 | [diff] [blame] | 41 | const DWARFSection &DWOSection, bool Lazy) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 42 | const DWARFObject &D = C.getDWARFObj(); |
| 43 | parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), &D.getRangeDWOSection(), |
| 44 | D.getStringDWOSection(), D.getStringOffsetDWOSection(), |
Paul Robinson | d0c89f8 | 2018-01-29 22:02:56 +0000 | [diff] [blame] | 45 | &D.getAddrSection(), D.getLineDWOSection(), C.isLittleEndian(), |
| 46 | true, Lazy); |
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, |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 51 | StringRef SS, const DWARFSection &SOS, |
Paul Robinson | d0c89f8 | 2018-01-29 22:02:56 +0000 | [diff] [blame] | 52 | const DWARFSection *AOS, const DWARFSection &LS, bool LE, |
| 53 | bool IsDWO, 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), |
Paul Robinson | d0c89f8 | 2018-01-29 22:02:56 +0000 | [diff] [blame] | 56 | LineSection(LS), StringSection(SS), StringOffsetSection(SOS), |
| 57 | AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO), |
| 58 | UnitSection(UnitSection), IndexEntry(IndexEntry) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 59 | clear(); |
| 60 | } |
| 61 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 62 | DWARFUnit::~DWARFUnit() = default; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 63 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 64 | DWARFDataExtractor DWARFUnit::getDebugInfoExtractor() const { |
| 65 | return DWARFDataExtractor(Context.getDWARFObj(), InfoSection, isLittleEndian, |
| 66 | getAddressByteSize()); |
| 67 | } |
| 68 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 69 | bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index, |
| 70 | uint64_t &Result) const { |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 71 | uint32_t Offset = AddrOffsetSectionBase + Index * getAddressByteSize(); |
| 72 | if (AddrOffsetSection->Data.size() < Offset + getAddressByteSize()) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 73 | return false; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 74 | DWARFDataExtractor DA(Context.getDWARFObj(), *AddrOffsetSection, |
| 75 | isLittleEndian, getAddressByteSize()); |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 76 | Result = DA.getRelocatedAddress(&Offset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index, |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 81 | uint64_t &Result) const { |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 82 | if (!StringOffsetsTableContribution) |
| 83 | return false; |
| 84 | unsigned ItemSize = getDwarfStringOffsetsByteSize(); |
| 85 | uint32_t Offset = getStringOffsetsBase() + Index * ItemSize; |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 86 | if (StringOffsetSection.Data.size() < Offset + ItemSize) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 87 | return false; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 88 | DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, |
| 89 | isLittleEndian, 0); |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 90 | Result = DA.getRelocatedValue(ItemSize, &Offset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) { |
| 95 | Length = debug_info.getU32(offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 96 | // FIXME: Support DWARF64. |
| 97 | FormParams.Format = DWARF32; |
| 98 | FormParams.Version = debug_info.getU16(offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 99 | if (FormParams.Version >= 5) { |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 100 | UnitType = debug_info.getU8(offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 101 | FormParams.AddrSize = debug_info.getU8(offset_ptr); |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 102 | AbbrOffset = debug_info.getU32(offset_ptr); |
| 103 | } else { |
| 104 | AbbrOffset = debug_info.getU32(offset_ptr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 105 | FormParams.AddrSize = debug_info.getU8(offset_ptr); |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 106 | } |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 107 | if (IndexEntry) { |
| 108 | if (AbbrOffset) |
| 109 | return false; |
| 110 | auto *UnitContrib = IndexEntry->getOffset(); |
| 111 | if (!UnitContrib || UnitContrib->Length != (Length + 4)) |
| 112 | return false; |
| 113 | auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV); |
| 114 | if (!AbbrEntry) |
| 115 | return false; |
| 116 | AbbrOffset = AbbrEntry->Offset; |
| 117 | } |
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 | bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 120 | bool VersionOK = DWARFContext::isSupportedVersion(getVersion()); |
| 121 | bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 122 | |
Alexey Samsonov | 7682f81 | 2014-04-24 22:51:03 +0000 | [diff] [blame] | 123 | if (!LengthOK || !VersionOK || !AddrSizeOK) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 124 | return false; |
| 125 | |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 126 | // Keep track of the highest DWARF version we encounter across all units. |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 127 | Context.setMaxVersionIfGreater(getVersion()); |
David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 128 | return true; |
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, |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 148 | DWARFDebugRangeList &RangeList) const { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 149 | // Require that compile unit is extracted. |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 150 | assert(!DieArray.empty()); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 151 | DWARFDataExtractor RangesData(Context.getDWARFObj(), *RangeSection, |
| 152 | isLittleEndian, getAddressByteSize()); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 153 | uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 154 | return RangeList.extract(RangesData, &ActualRangeListOffset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void DWARFUnit::clear() { |
| 158 | Offset = 0; |
| 159 | Length = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 160 | Abbrevs = nullptr; |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 161 | FormParams = DWARFFormParams({0, 0, DWARF32}); |
George Rimar | 2f95c8b | 2017-09-04 10:30:39 +0000 | [diff] [blame] | 162 | BaseAddr.reset(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 163 | RangeSectionBase = 0; |
| 164 | AddrOffsetSectionBase = 0; |
| 165 | clearDIEs(false); |
| 166 | DWO.reset(); |
| 167 | } |
| 168 | |
| 169 | const char *DWARFUnit::getCompilationDir() { |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 170 | return dwarf::toString(getUnitDIE().find(DW_AT_comp_dir), nullptr); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 173 | Optional<uint64_t> DWARFUnit::getDWOId() { |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 174 | return toUnsigned(getUnitDIE().find(DW_AT_GNU_dwo_id)); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 175 | } |
| 176 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 177 | void DWARFUnit::extractDIEsToVector( |
| 178 | bool AppendCUDie, bool AppendNonCUDies, |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 179 | std::vector<DWARFDebugInfoEntry> &Dies) const { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 180 | if (!AppendCUDie && !AppendNonCUDies) |
| 181 | return; |
| 182 | |
| 183 | // Set the offset to that of the first DIE and calculate the start of the |
| 184 | // next compilation unit header. |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 185 | uint32_t DIEOffset = Offset + getHeaderSize(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 186 | uint32_t NextCUOffset = getNextUnitOffset(); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 187 | DWARFDebugInfoEntry DIE; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 188 | DWARFDataExtractor DebugInfoData = getDebugInfoExtractor(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 189 | uint32_t Depth = 0; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 190 | bool IsCUDie = true; |
| 191 | |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 192 | while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset, |
| 193 | Depth)) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 194 | if (IsCUDie) { |
| 195 | if (AppendCUDie) |
| 196 | Dies.push_back(DIE); |
| 197 | if (!AppendNonCUDies) |
| 198 | break; |
| 199 | // The average bytes per DIE entry has been seen to be |
| 200 | // around 14-20 so let's pre-reserve the needed memory for |
| 201 | // our DIE entries accordingly. |
| 202 | Dies.reserve(Dies.size() + getDebugInfoSize() / 14); |
| 203 | IsCUDie = false; |
| 204 | } else { |
| 205 | Dies.push_back(DIE); |
| 206 | } |
| 207 | |
Alexey Samsonov | 8e4cf3b | 2014-04-29 17:12:42 +0000 | [diff] [blame] | 208 | if (const DWARFAbbreviationDeclaration *AbbrDecl = |
| 209 | DIE.getAbbreviationDeclarationPtr()) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 210 | // Normal DIE |
| 211 | if (AbbrDecl->hasChildren()) |
| 212 | ++Depth; |
| 213 | } else { |
| 214 | // NULL DIE. |
| 215 | if (Depth > 0) |
| 216 | --Depth; |
| 217 | if (Depth == 0) |
| 218 | break; // We are done with this compile unit! |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Give a little bit of info if we encounter corrupt DWARF (our offset |
| 223 | // should always terminate at or before the start of the next compilation |
| 224 | // unit header). |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 225 | if (DIEOffset > NextCUOffset) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 226 | fprintf(stderr, "warning: DWARF compile unit extends beyond its " |
Alexey Samsonov | 19f76f2 | 2014-04-24 23:08:56 +0000 | [diff] [blame] | 227 | "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) { |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 231 | if ((CUDieOnly && !DieArray.empty()) || |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 232 | DieArray.size() > 1) |
| 233 | return 0; // Already parsed. |
| 234 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 235 | bool HasCUDie = !DieArray.empty(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 236 | extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray); |
| 237 | |
| 238 | if (DieArray.empty()) |
| 239 | return 0; |
| 240 | |
| 241 | // If CU DIE was just parsed, copy several attribute values from it. |
| 242 | if (!HasCUDie) { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 243 | DWARFDie UnitDie = getUnitDIE(); |
George Rimar | 2f95c8b | 2017-09-04 10:30:39 +0000 | [diff] [blame] | 244 | Optional<DWARFFormValue> PC = UnitDie.find({DW_AT_low_pc, DW_AT_entry_pc}); |
| 245 | if (Optional<uint64_t> Addr = toAddress(PC)) |
| 246 | setBaseAddress({*Addr, PC->getSectionIndex()}); |
| 247 | |
David Blaikie | 22dc447 | 2017-08-02 20:16:22 +0000 | [diff] [blame] | 248 | if (!isDWO) { |
| 249 | assert(AddrOffsetSectionBase == 0); |
| 250 | assert(RangeSectionBase == 0); |
| 251 | AddrOffsetSectionBase = |
| 252 | toSectionOffset(UnitDie.find(DW_AT_GNU_addr_base), 0); |
| 253 | RangeSectionBase = toSectionOffset(UnitDie.find(DW_AT_rnglists_base), 0); |
| 254 | } |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 255 | |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 256 | // In general, in DWARF v5 and beyond we derive the start of the unit's |
| 257 | // contribution to the string offsets table from the unit DIE's |
| 258 | // DW_AT_str_offsets_base attribute. Split DWARF units do not use this |
| 259 | // attribute, so we assume that there is a contribution to the string |
| 260 | // offsets table starting at offset 0 of the debug_str_offsets.dwo section. |
| 261 | // In both cases we need to determine the format of the contribution, |
| 262 | // which may differ from the unit's format. |
| 263 | uint64_t StringOffsetsContributionBase = |
| 264 | isDWO ? 0 : toSectionOffset(UnitDie.find(DW_AT_str_offsets_base), 0); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 265 | if (IndexEntry) |
| 266 | if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS)) |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 267 | StringOffsetsContributionBase += C->Offset; |
| 268 | |
| 269 | DWARFDataExtractor DA(Context.getDWARFObj(), StringOffsetSection, |
| 270 | isLittleEndian, 0); |
| 271 | if (isDWO) |
| 272 | StringOffsetsTableContribution = |
| 273 | determineStringOffsetsTableContributionDWO( |
| 274 | DA, StringOffsetsContributionBase); |
| 275 | else if (getVersion() >= 5) |
| 276 | StringOffsetsTableContribution = determineStringOffsetsTableContribution( |
| 277 | DA, StringOffsetsContributionBase); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 278 | |
Alexey Samsonov | aa90998 | 2014-06-13 22:31:03 +0000 | [diff] [blame] | 279 | // Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for |
| 280 | // 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] | 281 | } |
| 282 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 283 | return DieArray.size(); |
| 284 | } |
| 285 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 286 | bool DWARFUnit::parseDWO() { |
David Blaikie | e438cff | 2016-04-22 22:50:56 +0000 | [diff] [blame] | 287 | if (isDWO) |
| 288 | return false; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 289 | if (DWO.get()) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 290 | return false; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 291 | DWARFDie UnitDie = getUnitDIE(); |
| 292 | if (!UnitDie) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 293 | return false; |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 294 | auto DWOFileName = dwarf::toString(UnitDie.find(DW_AT_GNU_dwo_name)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 295 | if (!DWOFileName) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 296 | return false; |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 297 | auto CompilationDir = dwarf::toString(UnitDie.find(DW_AT_comp_dir)); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 298 | SmallString<16> AbsolutePath; |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 299 | if (sys::path::is_relative(*DWOFileName) && CompilationDir && |
| 300 | *CompilationDir) { |
| 301 | sys::path::append(AbsolutePath, *CompilationDir); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 302 | } |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 303 | sys::path::append(AbsolutePath, *DWOFileName); |
David Blaikie | 8d039d4 | 2017-05-20 03:32:49 +0000 | [diff] [blame] | 304 | auto DWOId = getDWOId(); |
| 305 | if (!DWOId) |
| 306 | return false; |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 307 | auto DWOContext = Context.getDWOContext(AbsolutePath); |
| 308 | if (!DWOContext) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 309 | return false; |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 310 | |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 311 | DWARFCompileUnit *DWOCU = DWOContext->getDWOCompileUnitForHash(*DWOId); |
| 312 | if (!DWOCU) |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 313 | return false; |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 314 | DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 315 | // Share .debug_addr and .debug_ranges section with compile unit in .dwo |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 316 | DWO->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase); |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 317 | auto DWORangesBase = UnitDie.getRangesBaseAttribute(); |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 318 | DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | |
| 322 | void DWARFUnit::clearDIEs(bool KeepCUDie) { |
| 323 | if (DieArray.size() > (unsigned)KeepCUDie) { |
Benjamin Kramer | 295cf4d | 2017-08-01 14:38:08 +0000 | [diff] [blame] | 324 | DieArray.resize((unsigned)KeepCUDie); |
| 325 | DieArray.shrink_to_fit(); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 329 | void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 330 | DWARFDie UnitDie = getUnitDIE(); |
| 331 | if (!UnitDie) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 332 | return; |
| 333 | // First, check if unit DIE describes address ranges for the whole unit. |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 334 | const auto &CUDIERanges = UnitDie.getAddressRanges(); |
Alexey Samsonov | 84e2423 | 2014-04-18 20:30:27 +0000 | [diff] [blame] | 335 | if (!CUDIERanges.empty()) { |
| 336 | CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end()); |
| 337 | return; |
| 338 | } |
| 339 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 340 | // This function is usually called if there in no .debug_aranges section |
| 341 | // in order to produce a compile unit level set of address ranges that |
| 342 | // is accurate. If the DIEs weren't parsed, then we don't want all dies for |
| 343 | // all compile units to stay loaded when they weren't needed. So we can end |
| 344 | // up parsing the DWARF and then throwing them all away to keep memory usage |
| 345 | // down. |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 346 | const bool ClearDIEs = extractDIEsIfNeeded(false) > 1; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 347 | getUnitDIE().collectChildrenAddressRanges(CURanges); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 348 | |
| 349 | // Collect address ranges from DIEs in .dwo if necessary. |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 350 | bool DWOCreated = parseDWO(); |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 351 | if (DWO) |
| 352 | DWO->collectAddressRanges(CURanges); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 353 | if (DWOCreated) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 354 | DWO.reset(); |
| 355 | |
| 356 | // Keep memory down by clearing DIEs if this generate function |
| 357 | // caused them to be parsed. |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 358 | if (ClearDIEs) |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 359 | clearDIEs(true); |
| 360 | } |
| 361 | |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame^] | 362 | void DWARFUnit::updateAddressDieMap(DWARFDie Die) { |
| 363 | if (Die.isSubroutineDIE()) { |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 364 | for (const auto &R : Die.getAddressRanges()) { |
| 365 | // Ignore 0-sized ranges. |
George Rimar | 4671f2e | 2017-05-16 12:30:59 +0000 | [diff] [blame] | 366 | if (R.LowPC == R.HighPC) |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 367 | continue; |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame^] | 368 | auto B = AddrDieMap.upper_bound(R.LowPC); |
| 369 | if (B != AddrDieMap.begin() && R.LowPC < (--B)->second.first) { |
| 370 | // The range is a sub-range of existing ranges, we need to split the |
| 371 | // existing range. |
| 372 | if (R.HighPC < B->second.first) |
| 373 | AddrDieMap[R.HighPC] = B->second; |
| 374 | if (R.LowPC > B->first) |
| 375 | AddrDieMap[B->first].first = R.LowPC; |
Chandler Carruth | 54a5ad3 | 2017-12-22 06:41:23 +0000 | [diff] [blame] | 376 | } |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame^] | 377 | AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die); |
Chandler Carruth | 54a5ad3 | 2017-12-22 06:41:23 +0000 | [diff] [blame] | 378 | } |
Chandler Carruth | 54a5ad3 | 2017-12-22 06:41:23 +0000 | [diff] [blame] | 379 | } |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame^] | 380 | // Parent DIEs are added to the AddrDieMap prior to the Children DIEs to |
| 381 | // simplify the logic to update AddrDieMap. The child's range will always |
| 382 | // be equal or smaller than the parent's range. With this assumption, when |
| 383 | // adding one range into the map, it will at most split a range into 3 |
| 384 | // sub-ranges. |
| 385 | for (DWARFDie Child = Die.getFirstChild(); Child; Child = Child.getSibling()) |
| 386 | updateAddressDieMap(Child); |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | DWARFDie DWARFUnit::getSubroutineForAddress(uint64_t Address) { |
| 390 | extractDIEsIfNeeded(false); |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame^] | 391 | if (AddrDieMap.empty()) |
| 392 | updateAddressDieMap(getUnitDIE()); |
| 393 | auto R = AddrDieMap.upper_bound(Address); |
| 394 | if (R == AddrDieMap.begin()) |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 395 | return DWARFDie(); |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame^] | 396 | // upper_bound's previous item contains Address. |
| 397 | --R; |
| 398 | if (Address >= R->second.first) |
Dehao Chen | a364f09 | 2017-04-19 20:09:38 +0000 | [diff] [blame] | 399 | return DWARFDie(); |
David Blaikie | 3b6de6fe | 2018-02-13 01:52:30 +0000 | [diff] [blame^] | 400 | return R->second.second; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 403 | void |
| 404 | DWARFUnit::getInlinedChainForAddress(uint64_t Address, |
| 405 | SmallVectorImpl<DWARFDie> &InlinedChain) { |
Dehao Chen | db569ba | 2017-04-19 20:52:21 +0000 | [diff] [blame] | 406 | assert(InlinedChain.empty()); |
David Blaikie | 9a4f3cb | 2016-04-22 21:32:59 +0000 | [diff] [blame] | 407 | // Try to look for subprogram DIEs in the DWO file. |
| 408 | parseDWO(); |
Dehao Chen | db569ba | 2017-04-19 20:52:21 +0000 | [diff] [blame] | 409 | // First, find the subroutine that contains the given address (the leaf |
| 410 | // of inlined chain). |
| 411 | DWARFDie SubroutineDIE = |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 412 | (DWO ? DWO.get() : this)->getSubroutineForAddress(Address); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 413 | |
Dehao Chen | db569ba | 2017-04-19 20:52:21 +0000 | [diff] [blame] | 414 | while (SubroutineDIE) { |
| 415 | if (SubroutineDIE.isSubroutineDIE()) |
| 416 | InlinedChain.push_back(SubroutineDIE); |
| 417 | SubroutineDIE = SubroutineDIE.getParent(); |
| 418 | } |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 419 | } |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 420 | |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 421 | const DWARFUnitIndex &llvm::getDWARFUnitIndex(DWARFContext &Context, |
| 422 | DWARFSectionKind Kind) { |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 423 | if (Kind == DW_SECT_INFO) |
| 424 | return Context.getCUIndex(); |
| 425 | assert(Kind == DW_SECT_TYPES); |
| 426 | return Context.getTUIndex(); |
| 427 | } |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 428 | |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 429 | DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) { |
| 430 | if (!Die) |
| 431 | return DWARFDie(); |
| 432 | const uint32_t Depth = Die->getDepth(); |
| 433 | // Unit DIEs always have a depth of zero and never have parents. |
| 434 | if (Depth == 0) |
| 435 | return DWARFDie(); |
| 436 | // Depth of 1 always means parent is the compile/type unit. |
| 437 | if (Depth == 1) |
| 438 | return getUnitDIE(); |
| 439 | // Look for previous DIE with a depth that is one less than the Die's depth. |
| 440 | const uint32_t ParentDepth = Depth - 1; |
| 441 | for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) { |
| 442 | if (DieArray[I].getDepth() == ParentDepth) |
| 443 | return DWARFDie(this, &DieArray[I]); |
| 444 | } |
| 445 | return DWARFDie(); |
| 446 | } |
| 447 | |
| 448 | DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) { |
| 449 | if (!Die) |
| 450 | return DWARFDie(); |
| 451 | uint32_t Depth = Die->getDepth(); |
| 452 | // Unit DIEs always have a depth of zero and never have siblings. |
| 453 | if (Depth == 0) |
| 454 | return DWARFDie(); |
| 455 | // NULL DIEs don't have siblings. |
| 456 | if (Die->getAbbreviationDeclarationPtr() == nullptr) |
| 457 | return DWARFDie(); |
Jonas Devlieghere | f63ee64 | 2017-10-25 21:56:41 +0000 | [diff] [blame] | 458 | |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 459 | // 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] | 460 | for (size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size(); I < EndIdx; |
| 461 | ++I) { |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 462 | if (DieArray[I].getDepth() == Depth) |
| 463 | return DWARFDie(this, &DieArray[I]); |
| 464 | } |
| 465 | return DWARFDie(); |
| 466 | } |
David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 467 | |
George Rimar | 0be860f | 2017-10-25 10:23:49 +0000 | [diff] [blame] | 468 | DWARFDie DWARFUnit::getFirstChild(const DWARFDebugInfoEntry *Die) { |
| 469 | if (!Die->hasChildren()) |
| 470 | return DWARFDie(); |
| 471 | |
| 472 | // We do not want access out of bounds when parsing corrupted debug data. |
| 473 | size_t I = getDIEIndex(Die) + 1; |
| 474 | if (I >= DieArray.size()) |
| 475 | return DWARFDie(); |
| 476 | return DWARFDie(this, &DieArray[I]); |
| 477 | } |
| 478 | |
David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 479 | const DWARFAbbreviationDeclarationSet *DWARFUnit::getAbbreviations() const { |
| 480 | if (!Abbrevs) |
| 481 | Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset); |
| 482 | return Abbrevs; |
| 483 | } |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 484 | |
| 485 | Optional<StrOffsetsContributionDescriptor> |
| 486 | StrOffsetsContributionDescriptor::validateContributionSize( |
| 487 | DWARFDataExtractor &DA) { |
| 488 | uint8_t EntrySize = getDwarfOffsetByteSize(); |
| 489 | // In order to ensure that we don't read a partial record at the end of |
| 490 | // the section we validate for a multiple of the entry size. |
| 491 | uint64_t ValidationSize = alignTo(Size, EntrySize); |
| 492 | // Guard against overflow. |
| 493 | if (ValidationSize >= Size) |
| 494 | if (DA.isValidOffsetForDataOfSize((uint32_t)Base, ValidationSize)) |
| 495 | return *this; |
| 496 | return Optional<StrOffsetsContributionDescriptor>(); |
| 497 | } |
| 498 | |
| 499 | // Look for a DWARF64-formatted contribution to the string offsets table |
| 500 | // starting at a given offset and record it in a descriptor. |
| 501 | static Optional<StrOffsetsContributionDescriptor> |
| 502 | parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) { |
| 503 | if (!DA.isValidOffsetForDataOfSize(Offset, 16)) |
| 504 | return Optional<StrOffsetsContributionDescriptor>(); |
| 505 | |
| 506 | if (DA.getU32(&Offset) != 0xffffffff) |
| 507 | return Optional<StrOffsetsContributionDescriptor>(); |
| 508 | |
| 509 | uint64_t Size = DA.getU64(&Offset); |
| 510 | uint8_t Version = DA.getU16(&Offset); |
| 511 | (void)DA.getU16(&Offset); // padding |
| 512 | return StrOffsetsContributionDescriptor(Offset, Size, Version, DWARF64); |
| 513 | //return Optional<StrOffsetsContributionDescriptor>(Descriptor); |
| 514 | } |
| 515 | |
| 516 | // Look for a DWARF32-formatted contribution to the string offsets table |
| 517 | // starting at a given offset and record it in a descriptor. |
| 518 | static Optional<StrOffsetsContributionDescriptor> |
| 519 | parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint32_t Offset) { |
| 520 | if (!DA.isValidOffsetForDataOfSize(Offset, 8)) |
| 521 | return Optional<StrOffsetsContributionDescriptor>(); |
| 522 | uint32_t ContributionSize = DA.getU32(&Offset); |
| 523 | if (ContributionSize >= 0xfffffff0) |
| 524 | return Optional<StrOffsetsContributionDescriptor>(); |
| 525 | uint8_t Version = DA.getU16(&Offset); |
| 526 | (void)DA.getU16(&Offset); // padding |
| 527 | return StrOffsetsContributionDescriptor(Offset, ContributionSize, Version, DWARF32); |
| 528 | //return Optional<StrOffsetsContributionDescriptor>(Descriptor); |
| 529 | } |
| 530 | |
| 531 | Optional<StrOffsetsContributionDescriptor> |
| 532 | DWARFUnit::determineStringOffsetsTableContribution(DWARFDataExtractor &DA, |
| 533 | uint64_t Offset) { |
| 534 | Optional<StrOffsetsContributionDescriptor> Descriptor; |
| 535 | // Attempt to find a DWARF64 contribution 16 bytes before the base. |
| 536 | if (Offset >= 16) |
| 537 | Descriptor = |
| 538 | parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset - 16); |
| 539 | // Try to find a DWARF32 contribution 8 bytes before the base. |
| 540 | if (!Descriptor && Offset >= 8) |
| 541 | Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset - 8); |
| 542 | return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor; |
| 543 | } |
| 544 | |
| 545 | Optional<StrOffsetsContributionDescriptor> |
| 546 | DWARFUnit::determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA, |
| 547 | uint64_t Offset) { |
| 548 | if (getVersion() >= 5) { |
| 549 | // Look for a valid contribution at the given offset. |
| 550 | auto Descriptor = |
| 551 | parseDWARF64StringOffsetsTableHeader(DA, (uint32_t)Offset); |
| 552 | if (!Descriptor) |
| 553 | Descriptor = parseDWARF32StringOffsetsTableHeader(DA, (uint32_t)Offset); |
| 554 | return Descriptor ? Descriptor->validateContributionSize(DA) : Descriptor; |
| 555 | } |
| 556 | // Prior to DWARF v5, we derive the contribution size from the |
| 557 | // index table (in a package file). In a .dwo file it is simply |
| 558 | // the length of the string offsets section. |
| 559 | uint64_t Size = 0; |
| 560 | if (!IndexEntry) |
| 561 | Size = StringOffsetSection.Data.size(); |
| 562 | else if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS)) |
| 563 | Size = C->Length; |
| 564 | // Return a descriptor with the given offset as base, version 4 and |
| 565 | // DWARF32 format. |
| 566 | //return Optional<StrOffsetsContributionDescriptor>( |
| 567 | //StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32)); |
| 568 | return StrOffsetsContributionDescriptor(Offset, Size, 4, DWARF32); |
| 569 | } |