Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 1 | //===- DWARFContext.cpp ---------------------------------------------------===// |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +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 | |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 11 | #include "llvm/ADT/STLExtras.h" |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 16 | #include "llvm/BinaryFormat/Dwarf.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
Victor Leschuk | 58d3399 | 2018-07-31 22:19:19 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DWARF/DWARFDebugAddr.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" |
| 24 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
| 25 | #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" |
| 26 | #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" |
James Henderson | 3fcc745 | 2018-02-02 12:35:52 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/DWARF/DWARFDie.h" |
| 31 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 32 | #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h" |
| 33 | #include "llvm/DebugInfo/DWARF/DWARFSection.h" |
David Blaikie | 65a8efe | 2015-11-11 19:28:21 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/DWARF/DWARFVerifier.h" |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCRegisterInfo.h" |
George Rimar | 4bf3083 | 2017-01-11 15:26:41 +0000 | [diff] [blame] | 37 | #include "llvm/Object/Decompressor.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 38 | #include "llvm/Object/MachO.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 39 | #include "llvm/Object/ObjectFile.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 40 | #include "llvm/Object/RelocVisitor.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Casting.h" |
| 42 | #include "llvm/Support/DataExtractor.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Error.h" |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Format.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 45 | #include "llvm/Support/MemoryBuffer.h" |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Path.h" |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 47 | #include "llvm/Support/TargetRegistry.h" |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 48 | #include "llvm/Support/WithColor.h" |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 51 | #include <cstdint> |
Fangrui Song | 5bad9d8 | 2018-07-23 23:27:45 +0000 | [diff] [blame] | 52 | #include <deque> |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 53 | #include <map> |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 54 | #include <string> |
| 55 | #include <utility> |
| 56 | #include <vector> |
| 57 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 58 | using namespace llvm; |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 59 | using namespace dwarf; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 60 | using namespace object; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 62 | #define DEBUG_TYPE "dwarf" |
| 63 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 64 | using DWARFLineTable = DWARFDebugLine::LineTable; |
| 65 | using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; |
| 66 | using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind; |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 67 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 68 | DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj, |
| 69 | std::string DWPName) |
| 70 | : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {} |
| 71 | |
| 72 | DWARFContext::~DWARFContext() = default; |
| 73 | |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 74 | /// Dump the UUID load command. |
| 75 | static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) { |
| 76 | auto *MachO = dyn_cast<MachOObjectFile>(&Obj); |
| 77 | if (!MachO) |
| 78 | return; |
| 79 | for (auto LC : MachO->load_commands()) { |
| 80 | raw_ostream::uuid_t UUID; |
| 81 | if (LC.C.cmd == MachO::LC_UUID) { |
| 82 | if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) { |
| 83 | OS << "error: UUID load command is too short.\n"; |
| 84 | return; |
| 85 | } |
| 86 | OS << "UUID: "; |
| 87 | memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID)); |
| 88 | OS.write_uuid(UUID); |
Adrian Prantl | 146ed40 | 2018-01-05 21:44:17 +0000 | [diff] [blame] | 89 | Triple T = MachO->getArchTriple(); |
| 90 | OS << " (" << T.getArchName() << ')'; |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 91 | OS << ' ' << MachO->getFileName() << '\n'; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 96 | using ContributionCollection = |
| 97 | std::vector<Optional<StrOffsetsContributionDescriptor>>; |
| 98 | |
| 99 | // Collect all the contributions to the string offsets table from all units, |
| 100 | // sort them by their starting offsets and remove duplicates. |
| 101 | static ContributionCollection |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 102 | collectContributionData(DWARFContext::unit_iterator_range Units) { |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 103 | ContributionCollection Contributions; |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 104 | for (const auto &U : Units) |
| 105 | Contributions.push_back(U->getStringOffsetsTableContribution()); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 106 | // Sort the contributions so that any invalid ones are placed at |
| 107 | // the start of the contributions vector. This way they are reported |
| 108 | // first. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 109 | llvm::sort(Contributions, |
Mandeep Singh Grang | fe1d28e | 2018-04-01 16:18:49 +0000 | [diff] [blame] | 110 | [](const Optional<StrOffsetsContributionDescriptor> &L, |
| 111 | const Optional<StrOffsetsContributionDescriptor> &R) { |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 112 | if (L && R) |
| 113 | return L->Base < R->Base; |
Mandeep Singh Grang | fe1d28e | 2018-04-01 16:18:49 +0000 | [diff] [blame] | 114 | return R.hasValue(); |
| 115 | }); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 116 | |
| 117 | // Uniquify contributions, as it is possible that units (specifically |
| 118 | // type units in dwo or dwp files) share contributions. We don't want |
| 119 | // to report them more than once. |
| 120 | Contributions.erase( |
| 121 | std::unique(Contributions.begin(), Contributions.end(), |
| 122 | [](const Optional<StrOffsetsContributionDescriptor> &L, |
| 123 | const Optional<StrOffsetsContributionDescriptor> &R) { |
| 124 | if (L && R) |
| 125 | return L->Base == R->Base && L->Size == R->Size; |
| 126 | return false; |
| 127 | }), |
| 128 | Contributions.end()); |
| 129 | return Contributions; |
| 130 | } |
| 131 | |
| 132 | static void dumpDWARFv5StringOffsetsSection( |
| 133 | raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj, |
| 134 | const DWARFSection &StringOffsetsSection, StringRef StringSection, |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 135 | DWARFContext::unit_iterator_range Units, bool LittleEndian) { |
| 136 | auto Contributions = collectContributionData(Units); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 137 | DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 138 | DataExtractor StrData(StringSection, LittleEndian, 0); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 139 | uint64_t SectionSize = StringOffsetsSection.Data.size(); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 140 | uint32_t Offset = 0; |
| 141 | for (auto &Contribution : Contributions) { |
| 142 | // Report an ill-formed contribution. |
| 143 | if (!Contribution) { |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 144 | OS << "error: invalid contribution to string offsets table in section ." |
| 145 | << SectionName << ".\n"; |
| 146 | return; |
| 147 | } |
| 148 | |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 149 | dwarf::DwarfFormat Format = Contribution->getFormat(); |
| 150 | uint16_t Version = Contribution->getVersion(); |
| 151 | uint64_t ContributionHeader = Contribution->Base; |
| 152 | // In DWARF v5 there is a contribution header that immediately precedes |
| 153 | // the string offsets base (the location we have previously retrieved from |
| 154 | // the CU DIE's DW_AT_str_offsets attribute). The header is located either |
| 155 | // 8 or 16 bytes before the base, depending on the contribution's format. |
| 156 | if (Version >= 5) |
| 157 | ContributionHeader -= Format == DWARF32 ? 8 : 16; |
| 158 | |
| 159 | // Detect overlapping contributions. |
| 160 | if (Offset > ContributionHeader) { |
| 161 | OS << "error: overlapping contributions to string offsets table in " |
| 162 | "section ." |
| 163 | << SectionName << ".\n"; |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 166 | // Report a gap in the table. |
| 167 | if (Offset < ContributionHeader) { |
| 168 | OS << format("0x%8.8x: Gap, length = ", Offset); |
| 169 | OS << (ContributionHeader - Offset) << "\n"; |
| 170 | } |
Wolfgang Pieb | b4ba1aa | 2017-12-22 01:12:24 +0000 | [diff] [blame] | 171 | OS << format("0x%8.8x: ", (uint32_t)ContributionHeader); |
Wolfgang Pieb | f2b6915e | 2018-05-10 20:02:34 +0000 | [diff] [blame] | 172 | // In DWARF v5 the contribution size in the descriptor does not equal |
| 173 | // the originally encoded length (it does not contain the length of the |
| 174 | // version field and the padding, a total of 4 bytes). Add them back in |
| 175 | // for reporting. |
| 176 | OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4)) |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 177 | << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64") |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 178 | << ", Version = " << Version << "\n"; |
| 179 | |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 180 | Offset = Contribution->Base; |
| 181 | unsigned EntrySize = Contribution->getDwarfOffsetByteSize(); |
| 182 | while (Offset - Contribution->Base < Contribution->Size) { |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 183 | OS << format("0x%8.8x: ", Offset); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 184 | // FIXME: We can only extract strings if the offset fits in 32 bits. |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 185 | uint64_t StringOffset = |
| 186 | StrOffsetExt.getRelocatedValue(EntrySize, &Offset); |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 187 | // Extract the string if we can and display it. Otherwise just report |
| 188 | // the offset. |
| 189 | if (StringOffset <= std::numeric_limits<uint32_t>::max()) { |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 190 | uint32_t StringOffset32 = (uint32_t)StringOffset; |
Simon Dardis | b1b52c0 | 2017-08-07 16:08:11 +0000 | [diff] [blame] | 191 | OS << format("%8.8x ", StringOffset32); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 192 | const char *S = StrData.getCStr(&StringOffset32); |
| 193 | if (S) |
| 194 | OS << format("\"%s\"", S); |
| 195 | } else |
Simon Dardis | ec4ea99 | 2017-08-07 13:30:03 +0000 | [diff] [blame] | 196 | OS << format("%16.16" PRIx64 " ", StringOffset); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 197 | OS << "\n"; |
| 198 | } |
| 199 | } |
Wolfgang Pieb | 6ecd6a8 | 2017-12-21 19:38:13 +0000 | [diff] [blame] | 200 | // Report a gap at the end of the table. |
| 201 | if (Offset < SectionSize) { |
| 202 | OS << format("0x%8.8x: Gap, length = ", Offset); |
| 203 | OS << (SectionSize - Offset) << "\n"; |
| 204 | } |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Dump a DWARF string offsets section. This may be a DWARF v5 formatted |
| 208 | // string offsets section, where each compile or type unit contributes a |
| 209 | // number of entries (string offsets), with each contribution preceded by |
| 210 | // a header containing size and version number. Alternatively, it may be a |
| 211 | // monolithic series of string offsets, as generated by the pre-DWARF v5 |
| 212 | // implementation of split DWARF. |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 213 | static void dumpStringOffsetsSection(raw_ostream &OS, StringRef SectionName, |
| 214 | const DWARFObject &Obj, |
| 215 | const DWARFSection &StringOffsetsSection, |
| 216 | StringRef StringSection, |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 217 | DWARFContext::unit_iterator_range Units, |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 218 | bool LittleEndian, unsigned MaxVersion) { |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 219 | // If we have at least one (compile or type) unit with DWARF v5 or greater, |
| 220 | // we assume that the section is formatted like a DWARF v5 string offsets |
| 221 | // section. |
| 222 | if (MaxVersion >= 5) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 223 | dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection, |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 224 | StringSection, Units, LittleEndian); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 225 | else { |
| 226 | DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0); |
| 227 | uint32_t offset = 0; |
| 228 | uint64_t size = StringOffsetsSection.Data.size(); |
| 229 | // Ensure that size is a multiple of the size of an entry. |
| 230 | if (size & ((uint64_t)(sizeof(uint32_t) - 1))) { |
| 231 | OS << "error: size of ." << SectionName << " is not a multiple of " |
| 232 | << sizeof(uint32_t) << ".\n"; |
| 233 | size &= -(uint64_t)sizeof(uint32_t); |
| 234 | } |
| 235 | DataExtractor StrData(StringSection, LittleEndian, 0); |
| 236 | while (offset < size) { |
| 237 | OS << format("0x%8.8x: ", offset); |
| 238 | uint32_t StringOffset = strOffsetExt.getU32(&offset); |
| 239 | OS << format("%8.8x ", StringOffset); |
| 240 | const char *S = StrData.getCStr(&StringOffset); |
| 241 | if (S) |
| 242 | OS << format("\"%s\"", S); |
| 243 | OS << "\n"; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Victor Leschuk | 58d3399 | 2018-07-31 22:19:19 +0000 | [diff] [blame] | 248 | // Dump the .debug_addr section. |
| 249 | static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData, |
| 250 | DIDumpOptions DumpOpts, uint16_t Version, |
| 251 | uint8_t AddrSize) { |
Victor Leschuk | 58d3399 | 2018-07-31 22:19:19 +0000 | [diff] [blame] | 252 | uint32_t Offset = 0; |
| 253 | while (AddrData.isValidOffset(Offset)) { |
| 254 | DWARFDebugAddrTable AddrTable; |
| 255 | uint32_t TableOffset = Offset; |
Victor Leschuk | cf1f714 | 2018-08-23 12:43:33 +0000 | [diff] [blame] | 256 | if (Error Err = AddrTable.extract(AddrData, &Offset, Version, AddrSize, |
| 257 | DWARFContext::dumpWarning)) { |
Victor Leschuk | 58d3399 | 2018-07-31 22:19:19 +0000 | [diff] [blame] | 258 | WithColor::error() << toString(std::move(Err)) << '\n'; |
| 259 | // Keep going after an error, if we can, assuming that the length field |
| 260 | // could be read. If it couldn't, stop reading the section. |
| 261 | if (!AddrTable.hasValidLength()) |
| 262 | break; |
| 263 | uint64_t Length = AddrTable.getLength(); |
| 264 | Offset = TableOffset + Length; |
| 265 | } else { |
| 266 | AddrTable.dump(OS, DumpOpts); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 271 | // Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5). |
| 272 | static void dumpRnglistsSection(raw_ostream &OS, |
| 273 | DWARFDataExtractor &rnglistData, |
| 274 | DIDumpOptions DumpOpts) { |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 275 | uint32_t Offset = 0; |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 276 | while (rnglistData.isValidOffset(Offset)) { |
| 277 | llvm::DWARFDebugRnglistTable Rnglists; |
| 278 | uint32_t TableOffset = Offset; |
| 279 | if (Error Err = Rnglists.extract(rnglistData, &Offset)) { |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 280 | WithColor::error() << toString(std::move(Err)) << '\n'; |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 281 | uint64_t Length = Rnglists.length(); |
| 282 | // Keep going after an error, if we can, assuming that the length field |
| 283 | // could be read. If it couldn't, stop reading the section. |
| 284 | if (Length == 0) |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 285 | break; |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 286 | Offset = TableOffset + Length; |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 287 | } else { |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 288 | Rnglists.dump(OS, DumpOpts); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 293 | void DWARFContext::dump( |
| 294 | raw_ostream &OS, DIDumpOptions DumpOpts, |
| 295 | std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) { |
| 296 | |
| 297 | Optional<uint64_t> DumpOffset; |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 298 | uint64_t DumpType = DumpOpts.DumpType; |
Adrian Prantl | f4bc1f7 | 2017-06-01 18:18:23 +0000 | [diff] [blame] | 299 | |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 300 | StringRef Extension = sys::path::extension(DObj->getFileName()); |
| 301 | bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp"); |
| 302 | |
| 303 | // Print UUID header. |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 304 | const auto *ObjFile = DObj->getFile(); |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 305 | if (DumpType & DIDT_UUID) |
| 306 | dumpUUID(OS, *ObjFile); |
| 307 | |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 308 | // Print a header for each explicitly-requested section. |
| 309 | // Otherwise just print one for non-empty sections. |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 310 | // Only print empty .dwo section headers when dumping a .dwo file. |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 311 | bool Explicit = DumpType != DIDT_All && !IsDWO; |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 312 | bool ExplicitDWO = Explicit && IsDWO; |
| 313 | auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID, |
| 314 | StringRef Section) { |
| 315 | DumpOffset = DumpOffsets[ID]; |
| 316 | unsigned Mask = 1U << ID; |
| 317 | bool Should = (DumpType & Mask) && (Explicit || !Section.empty()); |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 318 | if (Should) |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 319 | OS << "\n" << Name << " contents:\n"; |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 320 | return Should; |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 321 | }; |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 322 | |
| 323 | // Dump individual sections. |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 324 | if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev, |
| 325 | DObj->getAbbrevSection())) |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 326 | getDebugAbbrev()->dump(OS); |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 327 | if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev, |
| 328 | DObj->getAbbrevDWOSection())) |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 329 | getDebugAbbrevDWO()->dump(OS); |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 330 | |
David Blaikie | fdada09 | 2018-10-05 20:55:20 +0000 | [diff] [blame^] | 331 | auto dumpDebugInfo = [&](unit_iterator_range Units) { |
| 332 | if (DumpOffset) |
| 333 | getDIEForOffset(DumpOffset.getValue()) |
| 334 | .dump(OS, 0, DumpOpts.noImplicitRecursion()); |
| 335 | else |
| 336 | for (const auto &U : Units) |
| 337 | U->dump(OS, DumpOpts); |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 338 | }; |
David Blaikie | fdada09 | 2018-10-05 20:55:20 +0000 | [diff] [blame^] | 339 | if (shouldDump(Explicit, ".debug_info", DIDT_ID_DebugInfo, |
| 340 | DObj->getInfoSection().Data)) |
| 341 | dumpDebugInfo(info_section_units()); |
| 342 | if (shouldDump(ExplicitDWO, ".debug_info.dwo", DIDT_ID_DebugInfo, |
| 343 | DObj->getInfoDWOSection().Data)) |
| 344 | dumpDebugInfo(dwo_info_section_units()); |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 345 | |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 346 | auto dumpDebugType = [&](const char *Name, unit_iterator_range Units) { |
Adrian Prantl | 099d7e4 | 2017-09-16 16:58:18 +0000 | [diff] [blame] | 347 | OS << '\n' << Name << " contents:\n"; |
| 348 | DumpOffset = DumpOffsets[DIDT_ID_DebugTypes]; |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 349 | for (const auto &U : Units) |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 350 | if (DumpOffset) |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 351 | U->getDIEForOffset(*DumpOffset) |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 352 | .dump(OS, 0, DumpOpts.noImplicitRecursion()); |
| 353 | else |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 354 | U->dump(OS, DumpOpts); |
Adrian Prantl | 099d7e4 | 2017-09-16 16:58:18 +0000 | [diff] [blame] | 355 | }; |
| 356 | if ((DumpType & DIDT_DebugTypes)) { |
| 357 | if (Explicit || getNumTypeUnits()) |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 358 | dumpDebugType(".debug_types", types_section_units()); |
Adrian Prantl | 099d7e4 | 2017-09-16 16:58:18 +0000 | [diff] [blame] | 359 | if (ExplicitDWO || getNumDWOTypeUnits()) |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 360 | dumpDebugType(".debug_types.dwo", dwo_types_section_units()); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 363 | if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 364 | DObj->getLocSection().Data)) { |
Jonas Devlieghere | 622c563 | 2017-09-27 09:33:36 +0000 | [diff] [blame] | 365 | getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset); |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 366 | } |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 367 | if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 368 | DObj->getLocDWOSection().Data)) { |
Jonas Devlieghere | 622c563 | 2017-09-27 09:33:36 +0000 | [diff] [blame] | 369 | getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset); |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 372 | if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, |
Adrian Prantl | 62528e6 | 2017-09-21 18:52:03 +0000 | [diff] [blame] | 373 | DObj->getDebugFrameSection())) |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 374 | getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset); |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 375 | |
| 376 | if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame, |
Adrian Prantl | 62528e6 | 2017-09-21 18:52:03 +0000 | [diff] [blame] | 377 | DObj->getEHFrameSection())) |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 378 | getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 379 | |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 380 | if (DumpType & DIDT_DebugMacro) { |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 381 | if (Explicit || !getDebugMacro()->empty()) { |
| 382 | OS << "\n.debug_macinfo contents:\n"; |
| 383 | getDebugMacro()->dump(OS); |
| 384 | } |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 387 | if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 388 | DObj->getARangeSection())) { |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 389 | uint32_t offset = 0; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 390 | DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 391 | DWARFDebugArangeSet set; |
| 392 | while (set.extract(arangesData, &offset)) |
| 393 | set.dump(OS); |
| 394 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 395 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 396 | auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser, |
| 397 | DIDumpOptions DumpOpts) { |
| 398 | while (!Parser.done()) { |
| 399 | if (DumpOffset && Parser.getOffset() != *DumpOffset) { |
Victor Leschuk | cf1f714 | 2018-08-23 12:43:33 +0000 | [diff] [blame] | 400 | Parser.skip(dumpWarning); |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 401 | continue; |
Paul Robinson | 63811a4 | 2017-11-22 15:33:17 +0000 | [diff] [blame] | 402 | } |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 403 | OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n"; |
Paul Robinson | 63811a4 | 2017-11-22 15:33:17 +0000 | [diff] [blame] | 404 | if (DumpOpts.Verbose) { |
Victor Leschuk | cf1f714 | 2018-08-23 12:43:33 +0000 | [diff] [blame] | 405 | Parser.parseNext(dumpWarning, dumpWarning, &OS); |
Paul Robinson | 63811a4 | 2017-11-22 15:33:17 +0000 | [diff] [blame] | 406 | } else { |
Victor Leschuk | cf1f714 | 2018-08-23 12:43:33 +0000 | [diff] [blame] | 407 | DWARFDebugLine::LineTable LineTable = |
| 408 | Parser.parseNext(dumpWarning, dumpWarning); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 409 | LineTable.dump(OS, DumpOpts); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 410 | } |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 411 | } |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine, |
| 415 | DObj->getLineSection().Data)) { |
| 416 | DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(), |
| 417 | 0); |
| 418 | DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(), |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 419 | type_units()); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 420 | DumpLineSection(Parser, DumpOpts); |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 421 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 422 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 423 | if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 424 | DObj->getLineDWOSection().Data)) { |
Paul Robinson | 63811a4 | 2017-11-22 15:33:17 +0000 | [diff] [blame] | 425 | DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(), |
| 426 | isLittleEndian(), 0); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 427 | DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(), |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 428 | dwo_type_units()); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 429 | DumpLineSection(Parser, DumpOpts); |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 432 | if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 433 | DObj->getCUIndexSection())) { |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 434 | getCUIndex().dump(OS); |
| 435 | } |
| 436 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 437 | if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 438 | DObj->getTUIndexSection())) { |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 439 | getTUIndex().dump(OS); |
| 440 | } |
| 441 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 442 | if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 443 | DObj->getStringSection())) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 444 | DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0); |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 445 | uint32_t offset = 0; |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 446 | uint32_t strOffset = 0; |
| 447 | while (const char *s = strData.getCStr(&offset)) { |
| 448 | OS << format("0x%8.8x: \"%s\"\n", strOffset, s); |
| 449 | strOffset = offset; |
| 450 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 451 | } |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 452 | if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 453 | DObj->getStringDWOSection())) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 454 | DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0); |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 455 | uint32_t offset = 0; |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 456 | uint32_t strDWOOffset = 0; |
| 457 | while (const char *s = strDWOData.getCStr(&offset)) { |
| 458 | OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s); |
| 459 | strDWOOffset = offset; |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 460 | } |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 461 | } |
Paul Robinson | b6aa01c | 2018-01-25 22:02:36 +0000 | [diff] [blame] | 462 | if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr, |
| 463 | DObj->getLineStringSection())) { |
| 464 | DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0); |
| 465 | uint32_t offset = 0; |
| 466 | uint32_t strOffset = 0; |
| 467 | while (const char *s = strData.getCStr(&offset)) { |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 468 | OS << format("0x%8.8x: \"", strOffset); |
| 469 | OS.write_escaped(s); |
| 470 | OS << "\"\n"; |
Paul Robinson | b6aa01c | 2018-01-25 22:02:36 +0000 | [diff] [blame] | 471 | strOffset = offset; |
| 472 | } |
| 473 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 474 | |
Victor Leschuk | 58d3399 | 2018-07-31 22:19:19 +0000 | [diff] [blame] | 475 | if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr, |
| 476 | DObj->getAddrSection().Data)) { |
| 477 | DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(), |
| 478 | isLittleEndian(), 0); |
| 479 | dumpAddrSection(OS, AddrData, DumpOpts, getMaxVersion(), getCUAddrSize()); |
| 480 | } |
| 481 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 482 | if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 483 | DObj->getRangeSection().Data)) { |
Victor Leschuk | 58d3399 | 2018-07-31 22:19:19 +0000 | [diff] [blame] | 484 | uint8_t savedAddressByteSize = getCUAddrSize(); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 485 | DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(), |
| 486 | isLittleEndian(), savedAddressByteSize); |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 487 | uint32_t offset = 0; |
| 488 | DWARFDebugRangeList rangeList; |
| 489 | while (rangesData.isValidOffset(offset)) { |
| 490 | if (Error E = rangeList.extract(rangesData, &offset)) { |
| 491 | WithColor::error() << toString(std::move(E)) << '\n'; |
| 492 | break; |
| 493 | } |
| 494 | rangeList.dump(OS); |
| 495 | } |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 496 | } |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 497 | |
James Henderson | 3fcc745 | 2018-02-02 12:35:52 +0000 | [diff] [blame] | 498 | if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists, |
| 499 | DObj->getRnglistsSection().Data)) { |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 500 | DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(), |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 501 | isLittleEndian(), 0); |
| 502 | dumpRnglistsSection(OS, RnglistData, DumpOpts); |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists, |
| 506 | DObj->getRnglistsDWOSection().Data)) { |
| 507 | DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(), |
Alexander Kornienko | e74e0f1 | 2018-09-17 15:40:01 +0000 | [diff] [blame] | 508 | isLittleEndian(), 0); |
| 509 | dumpRnglistsSection(OS, RnglistData, DumpOpts); |
James Henderson | 3fcc745 | 2018-02-02 12:35:52 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 512 | if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 513 | DObj->getPubNamesSection())) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 514 | DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false) |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 515 | .dump(OS); |
Krzysztof Parzyszek | 97438dc | 2013-02-12 16:20:28 +0000 | [diff] [blame] | 516 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 517 | if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 518 | DObj->getPubTypesSection())) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 519 | DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false) |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 520 | .dump(OS); |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 521 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 522 | if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 523 | DObj->getGnuPubNamesSection())) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 524 | DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(), |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 525 | true /* GnuStyle */) |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 526 | .dump(OS); |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 527 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 528 | if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 529 | DObj->getGnuPubTypesSection())) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 530 | DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(), |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 531 | true /* GnuStyle */) |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 532 | .dump(OS); |
David Blaikie | 404d304 | 2013-09-19 23:01:29 +0000 | [diff] [blame] | 533 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 534 | if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 535 | DObj->getStringOffsetSection().Data)) |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 536 | dumpStringOffsetsSection(OS, "debug_str_offsets", *DObj, |
| 537 | DObj->getStringOffsetSection(), |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 538 | DObj->getStringSection(), normal_units(), |
| 539 | isLittleEndian(), getMaxVersion()); |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 540 | if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 541 | DObj->getStringOffsetDWOSection().Data)) |
Paul Robinson | 96545db | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 542 | dumpStringOffsetsSection(OS, "debug_str_offsets.dwo", *DObj, |
| 543 | DObj->getStringOffsetDWOSection(), |
| 544 | DObj->getStringDWOSection(), dwo_units(), |
David Blaikie | fdada09 | 2018-10-05 20:55:20 +0000 | [diff] [blame^] | 545 | isLittleEndian(), getMaxDWOVersion()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 546 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 547 | if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 548 | DObj->getGdbIndexSection())) { |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 549 | getGdbIndex().dump(OS); |
| 550 | } |
| 551 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 552 | if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 553 | DObj->getAppleNamesSection().Data)) |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 554 | getAppleNames().dump(OS); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 555 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 556 | if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 557 | DObj->getAppleTypesSection().Data)) |
Adrian Prantl | 714ee4d | 2017-09-29 00:33:22 +0000 | [diff] [blame] | 558 | getAppleTypes().dump(OS); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 559 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 560 | if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 561 | DObj->getAppleNamespacesSection().Data)) |
Adrian Prantl | f51e780 | 2017-09-29 00:52:33 +0000 | [diff] [blame] | 562 | getAppleNamespaces().dump(OS); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 563 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 564 | if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC, |
Adrian Prantl | 8416802 | 2017-09-15 17:39:50 +0000 | [diff] [blame] | 565 | DObj->getAppleObjCSection().Data)) |
Adrian Prantl | f51e780 | 2017-09-29 00:52:33 +0000 | [diff] [blame] | 566 | getAppleObjC().dump(OS); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 567 | if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames, |
| 568 | DObj->getDebugNamesSection().Data)) |
| 569 | getDebugNames().dump(OS); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 570 | } |
| 571 | |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 572 | DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) { |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 573 | parseDWOUnits(LazyParse); |
David Blaikie | a62f1cb | 2017-07-30 15:15:58 +0000 | [diff] [blame] | 574 | |
David Blaikie | ebac0b9 | 2017-07-30 08:12:07 +0000 | [diff] [blame] | 575 | if (const auto &CUI = getCUIndex()) { |
| 576 | if (const auto *R = CUI.getFromHash(Hash)) |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 577 | return dyn_cast_or_null<DWARFCompileUnit>( |
| 578 | DWOUnits.getUnitForIndexEntry(*R)); |
David Blaikie | ebac0b9 | 2017-07-30 08:12:07 +0000 | [diff] [blame] | 579 | return nullptr; |
| 580 | } |
| 581 | |
| 582 | // If there's no index, just search through the CUs in the DWO - there's |
| 583 | // probably only one unless this is something like LTO - though an in-process |
| 584 | // built/cached lookup table could be used in that case to improve repeated |
| 585 | // lookups of different CUs in the DWO. |
Paul Robinson | 543c0e1 | 2018-05-22 17:27:31 +0000 | [diff] [blame] | 586 | for (const auto &DWOCU : dwo_compile_units()) { |
| 587 | // Might not have parsed DWO ID yet. |
| 588 | if (!DWOCU->getDWOId()) { |
| 589 | if (Optional<uint64_t> DWOId = |
| 590 | toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id))) |
| 591 | DWOCU->setDWOId(*DWOId); |
| 592 | else |
| 593 | // No DWO ID? |
| 594 | continue; |
| 595 | } |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 596 | if (DWOCU->getDWOId() == Hash) |
Paul Robinson | 143eaea | 2018-08-01 20:43:47 +0000 | [diff] [blame] | 597 | return dyn_cast<DWARFCompileUnit>(DWOCU.get()); |
Paul Robinson | 543c0e1 | 2018-05-22 17:27:31 +0000 | [diff] [blame] | 598 | } |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 599 | return nullptr; |
| 600 | } |
| 601 | |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 602 | DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) { |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 603 | parseNormalUnits(); |
| 604 | if (auto *CU = NormalUnits.getUnitForOffset(Offset)) |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 605 | return CU->getDIEForOffset(Offset); |
| 606 | return DWARFDie(); |
| 607 | } |
| 608 | |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 609 | bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) { |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 610 | bool Success = true; |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 611 | DWARFVerifier verifier(OS, *this, DumpOpts); |
Spyridoula Gravani | 73e1796 | 2017-07-27 00:59:33 +0000 | [diff] [blame] | 612 | |
Spyridoula Gravani | 364b535 | 2017-07-20 02:06:52 +0000 | [diff] [blame] | 613 | Success &= verifier.handleDebugAbbrev(); |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 614 | if (DumpOpts.DumpType & DIDT_DebugInfo) |
Spyridoula Gravani | 73e1796 | 2017-07-27 00:59:33 +0000 | [diff] [blame] | 615 | Success &= verifier.handleDebugInfo(); |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 616 | if (DumpOpts.DumpType & DIDT_DebugLine) |
Spyridoula Gravani | 73e1796 | 2017-07-27 00:59:33 +0000 | [diff] [blame] | 617 | Success &= verifier.handleDebugLine(); |
Spyridoula Gravani | dc635f4 | 2017-07-26 00:52:31 +0000 | [diff] [blame] | 618 | Success &= verifier.handleAccelTables(); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 619 | return Success; |
| 620 | } |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 621 | |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 622 | const DWARFUnitIndex &DWARFContext::getCUIndex() { |
| 623 | if (CUIndex) |
| 624 | return *CUIndex; |
| 625 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 626 | DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 627 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 628 | CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 629 | CUIndex->parse(CUIndexData); |
| 630 | return *CUIndex; |
| 631 | } |
| 632 | |
| 633 | const DWARFUnitIndex &DWARFContext::getTUIndex() { |
| 634 | if (TUIndex) |
| 635 | return *TUIndex; |
| 636 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 637 | DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 638 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 639 | TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 640 | TUIndex->parse(TUIndexData); |
| 641 | return *TUIndex; |
| 642 | } |
| 643 | |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 644 | DWARFGdbIndex &DWARFContext::getGdbIndex() { |
| 645 | if (GdbIndex) |
| 646 | return *GdbIndex; |
| 647 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 648 | DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0); |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 649 | GdbIndex = llvm::make_unique<DWARFGdbIndex>(); |
| 650 | GdbIndex->parse(GdbIndexData); |
| 651 | return *GdbIndex; |
| 652 | } |
| 653 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 654 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { |
| 655 | if (Abbrev) |
| 656 | return Abbrev.get(); |
| 657 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 658 | DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 659 | |
| 660 | Abbrev.reset(new DWARFDebugAbbrev()); |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 661 | Abbrev->extract(abbrData); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 662 | return Abbrev.get(); |
| 663 | } |
| 664 | |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 665 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { |
| 666 | if (AbbrevDWO) |
| 667 | return AbbrevDWO.get(); |
| 668 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 669 | DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 670 | AbbrevDWO.reset(new DWARFDebugAbbrev()); |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 671 | AbbrevDWO->extract(abbrData); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 672 | return AbbrevDWO.get(); |
| 673 | } |
| 674 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 675 | const DWARFDebugLoc *DWARFContext::getDebugLoc() { |
| 676 | if (Loc) |
| 677 | return Loc.get(); |
| 678 | |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 679 | Loc.reset(new DWARFDebugLoc); |
Paul Robinson | 143eaea | 2018-08-01 20:43:47 +0000 | [diff] [blame] | 680 | // Assume all units have the same address byte size. |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 681 | if (getNumCompileUnits()) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 682 | DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(), |
Paul Robinson | 143eaea | 2018-08-01 20:43:47 +0000 | [diff] [blame] | 683 | getUnitAtIndex(0)->getAddressByteSize()); |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 684 | Loc->parse(LocData); |
| 685 | } |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 686 | return Loc.get(); |
| 687 | } |
| 688 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 689 | const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() { |
| 690 | if (LocDWO) |
| 691 | return LocDWO.get(); |
| 692 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 693 | LocDWO.reset(new DWARFDebugLocDWO()); |
Pavel Labath | 54ca2d6 | 2018-04-06 08:49:57 +0000 | [diff] [blame] | 694 | // Assume all compile units have the same address byte size. |
| 695 | if (getNumCompileUnits()) { |
| 696 | DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(), |
Paul Robinson | 143eaea | 2018-08-01 20:43:47 +0000 | [diff] [blame] | 697 | getUnitAtIndex(0)->getAddressByteSize()); |
Pavel Labath | 54ca2d6 | 2018-04-06 08:49:57 +0000 | [diff] [blame] | 698 | LocDWO->parse(LocData); |
| 699 | } |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 700 | return LocDWO.get(); |
| 701 | } |
| 702 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 703 | const DWARFDebugAranges *DWARFContext::getDebugAranges() { |
| 704 | if (Aranges) |
| 705 | return Aranges.get(); |
| 706 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 707 | Aranges.reset(new DWARFDebugAranges()); |
Alexey Samsonov | a1694c1 | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 708 | Aranges->generate(this); |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 709 | return Aranges.get(); |
| 710 | } |
| 711 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 712 | const DWARFDebugFrame *DWARFContext::getDebugFrame() { |
| 713 | if (DebugFrame) |
| 714 | return DebugFrame.get(); |
| 715 | |
| 716 | // There's a "bug" in the DWARFv3 standard with respect to the target address |
| 717 | // size within debug frame sections. While DWARF is supposed to be independent |
| 718 | // of its container, FDEs have fields with size being "target address size", |
| 719 | // which isn't specified in DWARF in general. It's only specified for CUs, but |
| 720 | // .eh_frame can appear without a .debug_info section. Follow the example of |
| 721 | // other tools (libdwarf) and extract this from the container (ObjectFile |
| 722 | // provides this information). This problem is fixed in DWARFv4 |
| 723 | // See this dwarf-discuss discussion for more details: |
| 724 | // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 725 | DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(), |
| 726 | isLittleEndian(), DObj->getAddressSize()); |
Luke Cheeseman | 10981cc | 2018-09-28 17:01:50 +0000 | [diff] [blame] | 727 | DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */)); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 728 | DebugFrame->parse(debugFrameData); |
| 729 | return DebugFrame.get(); |
| 730 | } |
| 731 | |
| 732 | const DWARFDebugFrame *DWARFContext::getEHFrame() { |
| 733 | if (EHFrame) |
| 734 | return EHFrame.get(); |
| 735 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 736 | DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(), |
| 737 | DObj->getAddressSize()); |
Luke Cheeseman | 10981cc | 2018-09-28 17:01:50 +0000 | [diff] [blame] | 738 | DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 739 | DebugFrame->parse(debugFrameData); |
| 740 | return DebugFrame.get(); |
| 741 | } |
| 742 | |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 743 | const DWARFDebugMacro *DWARFContext::getDebugMacro() { |
| 744 | if (Macro) |
| 745 | return Macro.get(); |
| 746 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 747 | DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0); |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 748 | Macro.reset(new DWARFDebugMacro()); |
| 749 | Macro->parse(MacinfoData); |
| 750 | return Macro.get(); |
| 751 | } |
| 752 | |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 753 | template <typename T> |
| 754 | static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj, |
| 755 | const DWARFSection &Section, StringRef StringSection, |
| 756 | bool IsLittleEndian) { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 757 | if (Cache) |
| 758 | return *Cache; |
| 759 | DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0); |
| 760 | DataExtractor StrData(StringSection, IsLittleEndian, 0); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 761 | Cache.reset(new T(AccelSection, StrData)); |
Jonas Devlieghere | ba91589 | 2017-12-11 18:22:47 +0000 | [diff] [blame] | 762 | if (Error E = Cache->extract()) |
| 763 | llvm::consumeError(std::move(E)); |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 764 | return *Cache; |
| 765 | } |
| 766 | |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 767 | const DWARFDebugNames &DWARFContext::getDebugNames() { |
| 768 | return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(), |
| 769 | DObj->getStringSection(), isLittleEndian()); |
| 770 | } |
| 771 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 772 | const AppleAcceleratorTable &DWARFContext::getAppleNames() { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 773 | return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(), |
| 774 | DObj->getStringSection(), isLittleEndian()); |
| 775 | } |
| 776 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 777 | const AppleAcceleratorTable &DWARFContext::getAppleTypes() { |
Adrian Prantl | 714ee4d | 2017-09-29 00:33:22 +0000 | [diff] [blame] | 778 | return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(), |
| 779 | DObj->getStringSection(), isLittleEndian()); |
| 780 | } |
| 781 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 782 | const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() { |
Adrian Prantl | f51e780 | 2017-09-29 00:52:33 +0000 | [diff] [blame] | 783 | return getAccelTable(AppleNamespaces, *DObj, |
| 784 | DObj->getAppleNamespacesSection(), |
| 785 | DObj->getStringSection(), isLittleEndian()); |
| 786 | } |
| 787 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 788 | const AppleAcceleratorTable &DWARFContext::getAppleObjC() { |
Adrian Prantl | f51e780 | 2017-09-29 00:52:33 +0000 | [diff] [blame] | 789 | return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(), |
| 790 | DObj->getStringSection(), isLittleEndian()); |
| 791 | } |
| 792 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 793 | const DWARFDebugLine::LineTable * |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 794 | DWARFContext::getLineTableForUnit(DWARFUnit *U) { |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 795 | Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable = |
Victor Leschuk | cf1f714 | 2018-08-23 12:43:33 +0000 | [diff] [blame] | 796 | getLineTableForUnit(U, dumpWarning); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 797 | if (!ExpectedLineTable) { |
Victor Leschuk | cf1f714 | 2018-08-23 12:43:33 +0000 | [diff] [blame] | 798 | dumpWarning(ExpectedLineTable.takeError()); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 799 | return nullptr; |
| 800 | } |
| 801 | return *ExpectedLineTable; |
| 802 | } |
| 803 | |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 804 | Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit( |
| 805 | DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) { |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 806 | if (!Line) |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 807 | Line.reset(new DWARFDebugLine); |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 808 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 809 | auto UnitDIE = U->getUnitDIE(); |
| 810 | if (!UnitDIE) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 811 | return nullptr; |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 812 | |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 813 | auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list)); |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 814 | if (!Offset) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 815 | return nullptr; // No line table for this compile unit. |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 816 | |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 817 | uint32_t stmtOffset = *Offset + U->getLineTableOffset(); |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 818 | // See if the line table is cached. |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 819 | if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 820 | return lt; |
| 821 | |
Greg Clayton | 48ff66a | 2017-05-04 18:29:44 +0000 | [diff] [blame] | 822 | // Make sure the offset is good before we try to parse. |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 823 | if (stmtOffset >= U->getLineSection().Data.size()) |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 824 | return nullptr; |
Greg Clayton | 48ff66a | 2017-05-04 18:29:44 +0000 | [diff] [blame] | 825 | |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 826 | // We have to parse it first. |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 827 | DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(), |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 828 | U->getAddressByteSize()); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 829 | return Line->getOrParseLineTable(lineData, stmtOffset, *this, U, |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 830 | RecoverableErrorCallback); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 833 | void DWARFContext::parseNormalUnits() { |
| 834 | if (!NormalUnits.empty()) |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 835 | return; |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 836 | NormalUnits.addUnitsForSection(*this, DObj->getInfoSection(), DW_SECT_INFO); |
| 837 | NormalUnits.finishedInfoUnits(); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 838 | DObj->forEachTypesSections([&](const DWARFSection &S) { |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 839 | NormalUnits.addUnitsForSection(*this, S, DW_SECT_TYPES); |
Rafael Espindola | 5e5dfa1 | 2017-07-12 21:08:24 +0000 | [diff] [blame] | 840 | }); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 843 | void DWARFContext::parseDWOUnits(bool Lazy) { |
| 844 | if (!DWOUnits.empty()) |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 845 | return; |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 846 | DWOUnits.addUnitsForDWOSection(*this, DObj->getInfoDWOSection(), DW_SECT_INFO, |
| 847 | Lazy); |
| 848 | DWOUnits.finishedInfoUnits(); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 849 | DObj->forEachTypesDWOSections([&](const DWARFSection &S) { |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 850 | DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_TYPES, Lazy); |
Rafael Espindola | 5e5dfa1 | 2017-07-12 21:08:24 +0000 | [diff] [blame] | 851 | }); |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 854 | DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) { |
Paul Robinson | 2c25f34 | 2018-08-01 20:54:11 +0000 | [diff] [blame] | 855 | parseNormalUnits(); |
| 856 | return dyn_cast_or_null<DWARFCompileUnit>( |
| 857 | NormalUnits.getUnitForOffset(Offset)); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 860 | DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { |
Benjamin Kramer | 112ec17 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 861 | // First, get the offset of the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 862 | uint32_t CUOffset = getDebugAranges()->findAddress(Address); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 863 | // Retrieve the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 864 | return getCompileUnitForOffset(CUOffset); |
| 865 | } |
| 866 | |
Jonas Devlieghere | f63ee64 | 2017-10-25 21:56:41 +0000 | [diff] [blame] | 867 | DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) { |
| 868 | DIEsForAddress Result; |
| 869 | |
| 870 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 871 | if (!CU) |
| 872 | return Result; |
| 873 | |
| 874 | Result.CompileUnit = CU; |
| 875 | Result.FunctionDIE = CU->getSubroutineForAddress(Address); |
| 876 | |
| 877 | std::vector<DWARFDie> Worklist; |
| 878 | Worklist.push_back(Result.FunctionDIE); |
| 879 | while (!Worklist.empty()) { |
| 880 | DWARFDie DIE = Worklist.back(); |
| 881 | Worklist.pop_back(); |
| 882 | |
| 883 | if (DIE.getTag() == DW_TAG_lexical_block && |
| 884 | DIE.addressRangeContainsAddress(Address)) { |
| 885 | Result.BlockDIE = DIE; |
| 886 | break; |
| 887 | } |
| 888 | |
| 889 | for (auto Child : DIE) |
| 890 | Worklist.push_back(Child); |
| 891 | } |
| 892 | |
| 893 | return Result; |
| 894 | } |
| 895 | |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 896 | static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU, |
| 897 | uint64_t Address, |
| 898 | FunctionNameKind Kind, |
| 899 | std::string &FunctionName, |
| 900 | uint32_t &StartLine) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 901 | // The address may correspond to instruction in some inlined function, |
| 902 | // so we have to build the chain of inlined functions and take the |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 903 | // name of the topmost function in it. |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 904 | SmallVector<DWARFDie, 4> InlinedChain; |
| 905 | CU->getInlinedChainForAddress(Address, InlinedChain); |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 906 | if (InlinedChain.empty()) |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 907 | return false; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 908 | |
| 909 | const DWARFDie &DIE = InlinedChain[0]; |
| 910 | bool FoundResult = false; |
| 911 | const char *Name = nullptr; |
| 912 | if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 913 | FunctionName = Name; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 914 | FoundResult = true; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 915 | } |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 916 | if (auto DeclLineResult = DIE.getDeclLine()) { |
| 917 | StartLine = DeclLineResult; |
| 918 | FoundResult = true; |
| 919 | } |
| 920 | |
| 921 | return FoundResult; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 924 | DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address, |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 925 | DILineInfoSpecifier Spec) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 926 | DILineInfo Result; |
| 927 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 928 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 929 | if (!CU) |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 930 | return Result; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 931 | getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, |
| 932 | Result.FunctionName, |
| 933 | Result.StartLine); |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 934 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 935 | if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) |
| 936 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 937 | Spec.FLIKind, Result); |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 938 | } |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 939 | return Result; |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 940 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 941 | |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 942 | DILineInfoTable |
| 943 | DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size, |
| 944 | DILineInfoSpecifier Spec) { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 945 | DILineInfoTable Lines; |
| 946 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 947 | if (!CU) |
| 948 | return Lines; |
| 949 | |
| 950 | std::string FunctionName = "<invalid>"; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 951 | uint32_t StartLine = 0; |
| 952 | getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName, |
| 953 | StartLine); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 954 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 955 | // If the Specifier says we don't need FileLineInfo, just |
| 956 | // return the top-most function at the starting address. |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 957 | if (Spec.FLIKind == FileLineInfoKind::None) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 958 | DILineInfo Result; |
| 959 | Result.FunctionName = FunctionName; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 960 | Result.StartLine = StartLine; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 961 | Lines.push_back(std::make_pair(Address, Result)); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 962 | return Lines; |
| 963 | } |
| 964 | |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 965 | const DWARFLineTable *LineTable = getLineTableForUnit(CU); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 966 | |
| 967 | // Get the index of row we're looking for in the line table. |
| 968 | std::vector<uint32_t> RowVector; |
| 969 | if (!LineTable->lookupAddressRange(Address, Size, RowVector)) |
| 970 | return Lines; |
| 971 | |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 972 | for (uint32_t RowIndex : RowVector) { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 973 | // Take file number and line/column from the row. |
| 974 | const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 975 | DILineInfo Result; |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 976 | LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(), |
| 977 | Spec.FLIKind, Result.FileName); |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 978 | Result.FunctionName = FunctionName; |
| 979 | Result.Line = Row.Line; |
| 980 | Result.Column = Row.Column; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 981 | Result.StartLine = StartLine; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 982 | Lines.push_back(std::make_pair(Row.Address, Result)); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | return Lines; |
| 986 | } |
| 987 | |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 988 | DIInliningInfo |
| 989 | DWARFContext::getInliningInfoForAddress(uint64_t Address, |
| 990 | DILineInfoSpecifier Spec) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 991 | DIInliningInfo InliningInfo; |
| 992 | |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 993 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 994 | if (!CU) |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 995 | return InliningInfo; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 996 | |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 997 | const DWARFLineTable *LineTable = nullptr; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 998 | SmallVector<DWARFDie, 4> InlinedChain; |
| 999 | CU->getInlinedChainForAddress(Address, InlinedChain); |
| 1000 | if (InlinedChain.size() == 0) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 1001 | // If there is no DIE for address (e.g. it is in unavailable .dwo file), |
| 1002 | // try to at least get file/line info from symbol table. |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 1003 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 1004 | DILineInfo Frame; |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 1005 | LineTable = getLineTableForUnit(CU); |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1006 | if (LineTable && |
| 1007 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 1008 | Spec.FLIKind, Frame)) |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 1009 | InliningInfo.addFrame(Frame); |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 1010 | } |
| 1011 | return InliningInfo; |
| 1012 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1013 | |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 1014 | uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 1015 | for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) { |
| 1016 | DWARFDie &FunctionDIE = InlinedChain[i]; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 1017 | DILineInfo Frame; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1018 | // Get function name if necessary. |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 1019 | if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind)) |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 1020 | Frame.FunctionName = Name; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 1021 | if (auto DeclLineResult = FunctionDIE.getDeclLine()) |
| 1022 | Frame.StartLine = DeclLineResult; |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 1023 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1024 | if (i == 0) { |
| 1025 | // For the topmost frame, initialize the line table of this |
| 1026 | // compile unit and fetch file/line info from it. |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 1027 | LineTable = getLineTableForUnit(CU); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1028 | // For the topmost routine, get file/line info from line table. |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1029 | if (LineTable) |
| 1030 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 1031 | Spec.FLIKind, Frame); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1032 | } else { |
| 1033 | // Otherwise, use call file, call line and call column from |
| 1034 | // previous DIE in inlined chain. |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1035 | if (LineTable) |
| 1036 | LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(), |
| 1037 | Spec.FLIKind, Frame.FileName); |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 1038 | Frame.Line = CallLine; |
| 1039 | Frame.Column = CallColumn; |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 1040 | Frame.Discriminator = CallDiscriminator; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1041 | } |
| 1042 | // Get call file/line/column of a current DIE. |
| 1043 | if (i + 1 < n) { |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 1044 | FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn, |
| 1045 | CallDiscriminator); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 1048 | InliningInfo.addFrame(Frame); |
| 1049 | } |
| 1050 | return InliningInfo; |
| 1051 | } |
| 1052 | |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 1053 | std::shared_ptr<DWARFContext> |
| 1054 | DWARFContext::getDWOContext(StringRef AbsolutePath) { |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 1055 | if (auto S = DWP.lock()) { |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 1056 | DWARFContext *Ctxt = S->Context.get(); |
| 1057 | return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); |
| 1058 | } |
| 1059 | |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 1060 | std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath]; |
| 1061 | |
| 1062 | if (auto S = Entry->lock()) { |
| 1063 | DWARFContext *Ctxt = S->Context.get(); |
| 1064 | return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); |
| 1065 | } |
| 1066 | |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 1067 | Expected<OwningBinary<ObjectFile>> Obj = [&] { |
| 1068 | if (!CheckedForDWP) { |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 1069 | SmallString<128> DWPName; |
| 1070 | auto Obj = object::ObjectFile::createObjectFile( |
| 1071 | this->DWPName.empty() |
| 1072 | ? (DObj->getFileName() + ".dwp").toStringRef(DWPName) |
| 1073 | : StringRef(this->DWPName)); |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 1074 | if (Obj) { |
| 1075 | Entry = &DWP; |
| 1076 | return Obj; |
| 1077 | } else { |
| 1078 | CheckedForDWP = true; |
| 1079 | // TODO: Should this error be handled (maybe in a high verbosity mode) |
| 1080 | // before falling back to .dwo files? |
| 1081 | consumeError(Obj.takeError()); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | return object::ObjectFile::createObjectFile(AbsolutePath); |
| 1086 | }(); |
| 1087 | |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 1088 | if (!Obj) { |
| 1089 | // TODO: Actually report errors helpfully. |
| 1090 | consumeError(Obj.takeError()); |
| 1091 | return nullptr; |
| 1092 | } |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 1093 | |
| 1094 | auto S = std::make_shared<DWOFile>(); |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 1095 | S->File = std::move(Obj.get()); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1096 | S->Context = DWARFContext::create(*S->File.getBinary()); |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 1097 | *Entry = S; |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 1098 | auto *Ctxt = S->Context.get(); |
| 1099 | return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); |
| 1100 | } |
| 1101 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1102 | static Error createError(const Twine &Reason, llvm::Error E) { |
| 1103 | return make_error<StringError>(Reason + toString(std::move(E)), |
| 1104 | inconvertibleErrorCode()); |
| 1105 | } |
| 1106 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 1107 | /// SymInfo contains information about symbol: it's address |
| 1108 | /// and section index which is -1LL for absolute symbols. |
| 1109 | struct SymInfo { |
| 1110 | uint64_t Address; |
| 1111 | uint64_t SectionIndex; |
| 1112 | }; |
| 1113 | |
| 1114 | /// Returns the address of symbol relocation used against and a section index. |
| 1115 | /// Used for futher relocations computation. Symbol's section load address is |
| 1116 | static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj, |
| 1117 | const RelocationRef &Reloc, |
| 1118 | const LoadedObjectInfo *L, |
| 1119 | std::map<SymbolRef, SymInfo> &Cache) { |
| 1120 | SymInfo Ret = {0, (uint64_t)-1LL}; |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1121 | object::section_iterator RSec = Obj.section_end(); |
| 1122 | object::symbol_iterator Sym = Reloc.getSymbol(); |
| 1123 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 1124 | std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end(); |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1125 | // First calculate the address of the symbol or section as it appears |
| 1126 | // in the object file |
| 1127 | if (Sym != Obj.symbol_end()) { |
George Rimar | 958b01a | 2017-05-15 11:45:28 +0000 | [diff] [blame] | 1128 | bool New; |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 1129 | std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}}); |
George Rimar | 958b01a | 2017-05-15 11:45:28 +0000 | [diff] [blame] | 1130 | if (!New) |
| 1131 | return CacheIt->second; |
| 1132 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1133 | Expected<uint64_t> SymAddrOrErr = Sym->getAddress(); |
| 1134 | if (!SymAddrOrErr) |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 1135 | return createError("failed to compute symbol address: ", |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1136 | SymAddrOrErr.takeError()); |
| 1137 | |
| 1138 | // Also remember what section this symbol is in for later |
| 1139 | auto SectOrErr = Sym->getSection(); |
| 1140 | if (!SectOrErr) |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 1141 | return createError("failed to get symbol section: ", |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1142 | SectOrErr.takeError()); |
| 1143 | |
| 1144 | RSec = *SectOrErr; |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 1145 | Ret.Address = *SymAddrOrErr; |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1146 | } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) { |
| 1147 | RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl()); |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 1148 | Ret.Address = RSec->getAddress(); |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 1151 | if (RSec != Obj.section_end()) |
| 1152 | Ret.SectionIndex = RSec->getIndex(); |
| 1153 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1154 | // If we are given load addresses for the sections, we need to adjust: |
| 1155 | // SymAddr = (Address of Symbol Or Section in File) - |
| 1156 | // (Address of Section in File) + |
| 1157 | // (Load Address of Section) |
| 1158 | // RSec is now either the section being targeted or the section |
| 1159 | // containing the symbol being targeted. In either case, |
| 1160 | // we need to perform the same computation. |
| 1161 | if (L && RSec != Obj.section_end()) |
| 1162 | if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec)) |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 1163 | Ret.Address += SectionLoadAddress - RSec->getAddress(); |
George Rimar | 958b01a | 2017-05-15 11:45:28 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (CacheIt != Cache.end()) |
| 1166 | CacheIt->second = Ret; |
| 1167 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1168 | return Ret; |
| 1169 | } |
| 1170 | |
| 1171 | static bool isRelocScattered(const object::ObjectFile &Obj, |
| 1172 | const RelocationRef &Reloc) { |
George Rimar | d4998b0 | 2017-04-13 09:52:50 +0000 | [diff] [blame] | 1173 | const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj); |
| 1174 | if (!MachObj) |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1175 | return false; |
| 1176 | // MachO also has relocations that point to sections and |
| 1177 | // scattered relocations. |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 1178 | auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl()); |
| 1179 | return MachObj->isRelocationScattered(RelocInfo); |
| 1180 | } |
| 1181 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1182 | ErrorPolicy DWARFContext::defaultErrorHandler(Error E) { |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 1183 | WithColor::error() << toString(std::move(E)) << '\n'; |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 1184 | return ErrorPolicy::Continue; |
| 1185 | } |
| 1186 | |
Rafael Espindola | 87c3f4a9 | 2017-07-24 19:34:26 +0000 | [diff] [blame] | 1187 | namespace { |
| 1188 | struct DWARFSectionMap final : public DWARFSection { |
| 1189 | RelocAddrMap Relocs; |
| 1190 | }; |
Rafael Espindola | 87c3f4a9 | 2017-07-24 19:34:26 +0000 | [diff] [blame] | 1191 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1192 | class DWARFObjInMemory final : public DWARFObject { |
| 1193 | bool IsLittleEndian; |
| 1194 | uint8_t AddressSize; |
| 1195 | StringRef FileName; |
George Rimar | 6957ab5 | 2017-08-15 12:32:54 +0000 | [diff] [blame] | 1196 | const object::ObjectFile *Obj = nullptr; |
George Rimar | e1c30f7 | 2017-08-15 15:54:43 +0000 | [diff] [blame] | 1197 | std::vector<SectionName> SectionNames; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 1198 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1199 | using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap, |
| 1200 | std::map<object::SectionRef, unsigned>>; |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 1201 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1202 | TypeSectionMap TypesSections; |
| 1203 | TypeSectionMap TypesDWOSections; |
| 1204 | |
| 1205 | DWARFSectionMap InfoSection; |
| 1206 | DWARFSectionMap LocSection; |
| 1207 | DWARFSectionMap LineSection; |
| 1208 | DWARFSectionMap RangeSection; |
James Henderson | 3fcc745 | 2018-02-02 12:35:52 +0000 | [diff] [blame] | 1209 | DWARFSectionMap RnglistsSection; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1210 | DWARFSectionMap StringOffsetSection; |
| 1211 | DWARFSectionMap InfoDWOSection; |
| 1212 | DWARFSectionMap LineDWOSection; |
| 1213 | DWARFSectionMap LocDWOSection; |
| 1214 | DWARFSectionMap StringOffsetDWOSection; |
| 1215 | DWARFSectionMap RangeDWOSection; |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 1216 | DWARFSectionMap RnglistsDWOSection; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1217 | DWARFSectionMap AddrSection; |
| 1218 | DWARFSectionMap AppleNamesSection; |
| 1219 | DWARFSectionMap AppleTypesSection; |
| 1220 | DWARFSectionMap AppleNamespacesSection; |
| 1221 | DWARFSectionMap AppleObjCSection; |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 1222 | DWARFSectionMap DebugNamesSection; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1223 | |
| 1224 | DWARFSectionMap *mapNameToDWARFSection(StringRef Name) { |
| 1225 | return StringSwitch<DWARFSectionMap *>(Name) |
| 1226 | .Case("debug_info", &InfoSection) |
| 1227 | .Case("debug_loc", &LocSection) |
| 1228 | .Case("debug_line", &LineSection) |
| 1229 | .Case("debug_str_offsets", &StringOffsetSection) |
| 1230 | .Case("debug_ranges", &RangeSection) |
James Henderson | 3fcc745 | 2018-02-02 12:35:52 +0000 | [diff] [blame] | 1231 | .Case("debug_rnglists", &RnglistsSection) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1232 | .Case("debug_info.dwo", &InfoDWOSection) |
| 1233 | .Case("debug_loc.dwo", &LocDWOSection) |
| 1234 | .Case("debug_line.dwo", &LineDWOSection) |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 1235 | .Case("debug_names", &DebugNamesSection) |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 1236 | .Case("debug_rnglists.dwo", &RnglistsDWOSection) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1237 | .Case("debug_str_offsets.dwo", &StringOffsetDWOSection) |
| 1238 | .Case("debug_addr", &AddrSection) |
| 1239 | .Case("apple_names", &AppleNamesSection) |
| 1240 | .Case("apple_types", &AppleTypesSection) |
| 1241 | .Case("apple_namespaces", &AppleNamespacesSection) |
| 1242 | .Case("apple_namespac", &AppleNamespacesSection) |
| 1243 | .Case("apple_objc", &AppleObjCSection) |
| 1244 | .Default(nullptr); |
| 1245 | } |
| 1246 | |
| 1247 | StringRef AbbrevSection; |
| 1248 | StringRef ARangeSection; |
| 1249 | StringRef DebugFrameSection; |
| 1250 | StringRef EHFrameSection; |
| 1251 | StringRef StringSection; |
| 1252 | StringRef MacinfoSection; |
| 1253 | StringRef PubNamesSection; |
| 1254 | StringRef PubTypesSection; |
| 1255 | StringRef GnuPubNamesSection; |
| 1256 | StringRef AbbrevDWOSection; |
| 1257 | StringRef StringDWOSection; |
| 1258 | StringRef GnuPubTypesSection; |
| 1259 | StringRef CUIndexSection; |
| 1260 | StringRef GdbIndexSection; |
| 1261 | StringRef TUIndexSection; |
Paul Robinson | b6aa01c | 2018-01-25 22:02:36 +0000 | [diff] [blame] | 1262 | StringRef LineStringSection; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1263 | |
Fangrui Song | 5bad9d8 | 2018-07-23 23:27:45 +0000 | [diff] [blame] | 1264 | // A deque holding section data whose iterators are not invalidated when |
| 1265 | // new decompressed sections are inserted at the end. |
| 1266 | std::deque<SmallString<0>> UncompressedSections; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1267 | |
| 1268 | StringRef *mapSectionToMember(StringRef Name) { |
| 1269 | if (DWARFSection *Sec = mapNameToDWARFSection(Name)) |
| 1270 | return &Sec->Data; |
| 1271 | return StringSwitch<StringRef *>(Name) |
| 1272 | .Case("debug_abbrev", &AbbrevSection) |
| 1273 | .Case("debug_aranges", &ARangeSection) |
| 1274 | .Case("debug_frame", &DebugFrameSection) |
| 1275 | .Case("eh_frame", &EHFrameSection) |
| 1276 | .Case("debug_str", &StringSection) |
| 1277 | .Case("debug_macinfo", &MacinfoSection) |
| 1278 | .Case("debug_pubnames", &PubNamesSection) |
| 1279 | .Case("debug_pubtypes", &PubTypesSection) |
| 1280 | .Case("debug_gnu_pubnames", &GnuPubNamesSection) |
| 1281 | .Case("debug_gnu_pubtypes", &GnuPubTypesSection) |
| 1282 | .Case("debug_abbrev.dwo", &AbbrevDWOSection) |
| 1283 | .Case("debug_str.dwo", &StringDWOSection) |
| 1284 | .Case("debug_cu_index", &CUIndexSection) |
| 1285 | .Case("debug_tu_index", &TUIndexSection) |
| 1286 | .Case("gdb_index", &GdbIndexSection) |
Paul Robinson | b6aa01c | 2018-01-25 22:02:36 +0000 | [diff] [blame] | 1287 | .Case("debug_line_str", &LineStringSection) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1288 | // Any more debug info sections go here. |
| 1289 | .Default(nullptr); |
| 1290 | } |
| 1291 | |
| 1292 | /// If Sec is compressed section, decompresses and updates its contents |
| 1293 | /// provided by Data. Otherwise leaves it unchanged. |
| 1294 | Error maybeDecompress(const object::SectionRef &Sec, StringRef Name, |
| 1295 | StringRef &Data) { |
| 1296 | if (!Decompressor::isCompressed(Sec)) |
| 1297 | return Error::success(); |
| 1298 | |
| 1299 | Expected<Decompressor> Decompressor = |
| 1300 | Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8); |
| 1301 | if (!Decompressor) |
| 1302 | return Decompressor.takeError(); |
| 1303 | |
Fangrui Song | 5bad9d8 | 2018-07-23 23:27:45 +0000 | [diff] [blame] | 1304 | SmallString<0> Out; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1305 | if (auto Err = Decompressor->resizeAndDecompress(Out)) |
| 1306 | return Err; |
| 1307 | |
Fangrui Song | 5bad9d8 | 2018-07-23 23:27:45 +0000 | [diff] [blame] | 1308 | UncompressedSections.push_back(std::move(Out)); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1309 | Data = UncompressedSections.back(); |
| 1310 | |
| 1311 | return Error::success(); |
| 1312 | } |
| 1313 | |
| 1314 | public: |
| 1315 | DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, |
| 1316 | uint8_t AddrSize, bool IsLittleEndian) |
| 1317 | : IsLittleEndian(IsLittleEndian) { |
| 1318 | for (const auto &SecIt : Sections) { |
| 1319 | if (StringRef *SectionData = mapSectionToMember(SecIt.first())) |
| 1320 | *SectionData = SecIt.second->getBuffer(); |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 1321 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1322 | } |
| 1323 | DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L, |
| 1324 | function_ref<ErrorPolicy(Error)> HandleError) |
| 1325 | : IsLittleEndian(Obj.isLittleEndian()), |
George Rimar | 6957ab5 | 2017-08-15 12:32:54 +0000 | [diff] [blame] | 1326 | AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()), |
| 1327 | Obj(&Obj) { |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 1328 | |
George Rimar | e1c30f7 | 2017-08-15 15:54:43 +0000 | [diff] [blame] | 1329 | StringMap<unsigned> SectionAmountMap; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1330 | for (const SectionRef &Section : Obj.sections()) { |
| 1331 | StringRef Name; |
| 1332 | Section.getName(Name); |
George Rimar | e1c30f7 | 2017-08-15 15:54:43 +0000 | [diff] [blame] | 1333 | ++SectionAmountMap[Name]; |
| 1334 | SectionNames.push_back({ Name, true }); |
| 1335 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1336 | // Skip BSS and Virtual sections, they aren't interesting. |
| 1337 | if (Section.isBSS() || Section.isVirtual()) |
George Rimar | fed9f09 | 2017-05-17 12:10:51 +0000 | [diff] [blame] | 1338 | continue; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 1339 | |
Jonas Devlieghere | 8af2387 | 2017-09-26 14:22:35 +0000 | [diff] [blame] | 1340 | // Skip sections stripped by dsymutil. |
| 1341 | if (Section.isStripped()) |
| 1342 | continue; |
| 1343 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1344 | StringRef Data; |
| 1345 | section_iterator RelocatedSection = Section.getRelocatedSection(); |
| 1346 | // Try to obtain an already relocated version of this section. |
| 1347 | // Else use the unrelocated section from the object file. We'll have to |
| 1348 | // apply relocations ourselves later. |
| 1349 | if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) |
| 1350 | Section.getContents(Data); |
George Rimar | fed9f09 | 2017-05-17 12:10:51 +0000 | [diff] [blame] | 1351 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1352 | if (auto Err = maybeDecompress(Section, Name, Data)) { |
| 1353 | ErrorPolicy EP = HandleError(createError( |
| 1354 | "failed to decompress '" + Name + "', ", std::move(Err))); |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 1355 | if (EP == ErrorPolicy::Halt) |
| 1356 | return; |
George Rimar | fed9f09 | 2017-05-17 12:10:51 +0000 | [diff] [blame] | 1357 | continue; |
| 1358 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1359 | |
| 1360 | // Compressed sections names in GNU style starts from ".z", |
| 1361 | // at this point section is decompressed and we drop compression prefix. |
| 1362 | Name = Name.substr( |
| 1363 | Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes. |
| 1364 | |
| 1365 | // Map platform specific debug section names to DWARF standard section |
| 1366 | // names. |
| 1367 | Name = Obj.mapDebugSectionName(Name); |
| 1368 | |
| 1369 | if (StringRef *SectionData = mapSectionToMember(Name)) { |
| 1370 | *SectionData = Data; |
| 1371 | if (Name == "debug_ranges") { |
| 1372 | // FIXME: Use the other dwo range section when we emit it. |
| 1373 | RangeDWOSection.Data = Data; |
| 1374 | } |
| 1375 | } else if (Name == "debug_types") { |
| 1376 | // Find debug_types data by section rather than name as there are |
| 1377 | // multiple, comdat grouped, debug_types sections. |
| 1378 | TypesSections[Section].Data = Data; |
| 1379 | } else if (Name == "debug_types.dwo") { |
| 1380 | TypesDWOSections[Section].Data = Data; |
| 1381 | } |
| 1382 | |
| 1383 | if (RelocatedSection == Obj.section_end()) |
| 1384 | continue; |
| 1385 | |
| 1386 | StringRef RelSecName; |
| 1387 | StringRef RelSecData; |
| 1388 | RelocatedSection->getName(RelSecName); |
| 1389 | |
| 1390 | // If the section we're relocating was relocated already by the JIT, |
| 1391 | // then we used the relocated version above, so we do not need to process |
| 1392 | // relocations for it now. |
| 1393 | if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData)) |
| 1394 | continue; |
| 1395 | |
| 1396 | // In Mach-o files, the relocations do not need to be applied if |
| 1397 | // there is no load offset to apply. The value read at the |
| 1398 | // relocation point already factors in the section address |
| 1399 | // (actually applying the relocations will produce wrong results |
| 1400 | // as the section address will be added twice). |
| 1401 | if (!L && isa<MachOObjectFile>(&Obj)) |
| 1402 | continue; |
| 1403 | |
| 1404 | RelSecName = RelSecName.substr( |
| 1405 | RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes. |
| 1406 | |
| 1407 | // TODO: Add support for relocations in other sections as needed. |
| 1408 | // Record relocations for the debug_info and debug_line sections. |
| 1409 | DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName); |
| 1410 | RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr; |
| 1411 | if (!Map) { |
| 1412 | // Find debug_types relocs by section rather than name as there are |
| 1413 | // multiple, comdat grouped, debug_types sections. |
| 1414 | if (RelSecName == "debug_types") |
| 1415 | Map = |
| 1416 | &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection]) |
| 1417 | .Relocs; |
| 1418 | else if (RelSecName == "debug_types.dwo") |
| 1419 | Map = &static_cast<DWARFSectionMap &>( |
| 1420 | TypesDWOSections[*RelocatedSection]) |
| 1421 | .Relocs; |
| 1422 | else |
| 1423 | continue; |
| 1424 | } |
| 1425 | |
| 1426 | if (Section.relocation_begin() == Section.relocation_end()) |
| 1427 | continue; |
| 1428 | |
| 1429 | // Symbol to [address, section index] cache mapping. |
| 1430 | std::map<SymbolRef, SymInfo> AddrCache; |
| 1431 | for (const RelocationRef &Reloc : Section.relocations()) { |
| 1432 | // FIXME: it's not clear how to correctly handle scattered |
| 1433 | // relocations. |
| 1434 | if (isRelocScattered(Obj, Reloc)) |
| 1435 | continue; |
| 1436 | |
| 1437 | Expected<SymInfo> SymInfoOrErr = |
| 1438 | getSymbolInfo(Obj, Reloc, L, AddrCache); |
| 1439 | if (!SymInfoOrErr) { |
| 1440 | if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt) |
| 1441 | return; |
| 1442 | continue; |
| 1443 | } |
| 1444 | |
| 1445 | object::RelocVisitor V(Obj); |
| 1446 | uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address); |
| 1447 | if (V.error()) { |
| 1448 | SmallString<32> Type; |
| 1449 | Reloc.getTypeName(Type); |
| 1450 | ErrorPolicy EP = HandleError( |
| 1451 | createError("failed to compute relocation: " + Type + ", ", |
| 1452 | errorCodeToError(object_error::parse_failed))); |
| 1453 | if (EP == ErrorPolicy::Halt) |
| 1454 | return; |
| 1455 | continue; |
| 1456 | } |
| 1457 | RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val}; |
| 1458 | Map->insert({Reloc.getOffset(), Rel}); |
| 1459 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 1460 | } |
George Rimar | e1c30f7 | 2017-08-15 15:54:43 +0000 | [diff] [blame] | 1461 | |
| 1462 | for (SectionName &S : SectionNames) |
| 1463 | if (SectionAmountMap[S.Name] > 1) |
| 1464 | S.IsNameUnique = false; |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 1465 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 1466 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1467 | Optional<RelocAddrEntry> find(const DWARFSection &S, |
| 1468 | uint64_t Pos) const override { |
| 1469 | auto &Sec = static_cast<const DWARFSectionMap &>(S); |
| 1470 | RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos); |
| 1471 | if (AI == Sec.Relocs.end()) |
| 1472 | return None; |
| 1473 | return AI->second; |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 1474 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1475 | |
George Rimar | 6957ab5 | 2017-08-15 12:32:54 +0000 | [diff] [blame] | 1476 | const object::ObjectFile *getFile() const override { return Obj; } |
| 1477 | |
George Rimar | e1c30f7 | 2017-08-15 15:54:43 +0000 | [diff] [blame] | 1478 | ArrayRef<SectionName> getSectionNames() const override { |
| 1479 | return SectionNames; |
| 1480 | } |
| 1481 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1482 | bool isLittleEndian() const override { return IsLittleEndian; } |
| 1483 | StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; } |
| 1484 | const DWARFSection &getLineDWOSection() const override { |
| 1485 | return LineDWOSection; |
| 1486 | } |
| 1487 | const DWARFSection &getLocDWOSection() const override { |
| 1488 | return LocDWOSection; |
| 1489 | } |
| 1490 | StringRef getStringDWOSection() const override { return StringDWOSection; } |
| 1491 | const DWARFSection &getStringOffsetDWOSection() const override { |
| 1492 | return StringOffsetDWOSection; |
| 1493 | } |
| 1494 | const DWARFSection &getRangeDWOSection() const override { |
| 1495 | return RangeDWOSection; |
| 1496 | } |
Wolfgang Pieb | ad60559 | 2018-05-18 20:12:54 +0000 | [diff] [blame] | 1497 | const DWARFSection &getRnglistsDWOSection() const override { |
| 1498 | return RnglistsDWOSection; |
| 1499 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1500 | const DWARFSection &getAddrSection() const override { return AddrSection; } |
| 1501 | StringRef getCUIndexSection() const override { return CUIndexSection; } |
| 1502 | StringRef getGdbIndexSection() const override { return GdbIndexSection; } |
| 1503 | StringRef getTUIndexSection() const override { return TUIndexSection; } |
| 1504 | |
| 1505 | // DWARF v5 |
| 1506 | const DWARFSection &getStringOffsetSection() const override { |
| 1507 | return StringOffsetSection; |
| 1508 | } |
Paul Robinson | b6aa01c | 2018-01-25 22:02:36 +0000 | [diff] [blame] | 1509 | StringRef getLineStringSection() const override { return LineStringSection; } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Sections for DWARF5 split dwarf proposal. |
| 1512 | const DWARFSection &getInfoDWOSection() const override { |
| 1513 | return InfoDWOSection; |
| 1514 | } |
| 1515 | void forEachTypesDWOSections( |
| 1516 | function_ref<void(const DWARFSection &)> F) const override { |
| 1517 | for (auto &P : TypesDWOSections) |
| 1518 | F(P.second); |
| 1519 | } |
| 1520 | |
| 1521 | StringRef getAbbrevSection() const override { return AbbrevSection; } |
| 1522 | const DWARFSection &getLocSection() const override { return LocSection; } |
| 1523 | StringRef getARangeSection() const override { return ARangeSection; } |
| 1524 | StringRef getDebugFrameSection() const override { return DebugFrameSection; } |
| 1525 | StringRef getEHFrameSection() const override { return EHFrameSection; } |
| 1526 | const DWARFSection &getLineSection() const override { return LineSection; } |
| 1527 | StringRef getStringSection() const override { return StringSection; } |
| 1528 | const DWARFSection &getRangeSection() const override { return RangeSection; } |
James Henderson | 3fcc745 | 2018-02-02 12:35:52 +0000 | [diff] [blame] | 1529 | const DWARFSection &getRnglistsSection() const override { |
| 1530 | return RnglistsSection; |
| 1531 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1532 | StringRef getMacinfoSection() const override { return MacinfoSection; } |
| 1533 | StringRef getPubNamesSection() const override { return PubNamesSection; } |
| 1534 | StringRef getPubTypesSection() const override { return PubTypesSection; } |
| 1535 | StringRef getGnuPubNamesSection() const override { |
| 1536 | return GnuPubNamesSection; |
| 1537 | } |
| 1538 | StringRef getGnuPubTypesSection() const override { |
| 1539 | return GnuPubTypesSection; |
| 1540 | } |
| 1541 | const DWARFSection &getAppleNamesSection() const override { |
| 1542 | return AppleNamesSection; |
| 1543 | } |
| 1544 | const DWARFSection &getAppleTypesSection() const override { |
| 1545 | return AppleTypesSection; |
| 1546 | } |
| 1547 | const DWARFSection &getAppleNamespacesSection() const override { |
| 1548 | return AppleNamespacesSection; |
| 1549 | } |
| 1550 | const DWARFSection &getAppleObjCSection() const override { |
| 1551 | return AppleObjCSection; |
| 1552 | } |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 1553 | const DWARFSection &getDebugNamesSection() const override { |
| 1554 | return DebugNamesSection; |
| 1555 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1556 | |
| 1557 | StringRef getFileName() const override { return FileName; } |
| 1558 | uint8_t getAddressSize() const override { return AddressSize; } |
| 1559 | const DWARFSection &getInfoSection() const override { return InfoSection; } |
| 1560 | void forEachTypesSections( |
| 1561 | function_ref<void(const DWARFSection &)> F) const override { |
| 1562 | for (auto &P : TypesSections) |
| 1563 | F(P.second); |
| 1564 | } |
| 1565 | }; |
Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 1566 | } // namespace |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1567 | |
| 1568 | std::unique_ptr<DWARFContext> |
| 1569 | DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L, |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 1570 | function_ref<ErrorPolicy(Error)> HandleError, |
| 1571 | std::string DWPName) { |
Reid Kleckner | 388f880 | 2017-07-19 23:42:53 +0000 | [diff] [blame] | 1572 | auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError); |
Luke Cheeseman | 10981cc | 2018-09-28 17:01:50 +0000 | [diff] [blame] | 1573 | return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName)); |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 1576 | std::unique_ptr<DWARFContext> |
| 1577 | DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, |
| 1578 | uint8_t AddrSize, bool isLittleEndian) { |
Reid Kleckner | 388f880 | 2017-07-19 23:42:53 +0000 | [diff] [blame] | 1579 | auto DObj = |
| 1580 | llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian); |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 1581 | return llvm::make_unique<DWARFContext>(std::move(DObj), ""); |
Rafael Espindola | 77e87b3 | 2017-07-07 05:36:53 +0000 | [diff] [blame] | 1582 | } |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 1583 | |
Luke Cheeseman | 10981cc | 2018-09-28 17:01:50 +0000 | [diff] [blame] | 1584 | Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) { |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 1585 | // Detect the architecture from the object file. We usually don't need OS |
| 1586 | // info to lookup a target and create register info. |
| 1587 | Triple TT; |
| 1588 | TT.setArch(Triple::ArchType(Obj.getArch())); |
| 1589 | TT.setVendor(Triple::UnknownVendor); |
| 1590 | TT.setOS(Triple::UnknownOS); |
| 1591 | std::string TargetLookupError; |
| 1592 | const Target *TheTarget = |
| 1593 | TargetRegistry::lookupTarget(TT.str(), TargetLookupError); |
| 1594 | if (!TargetLookupError.empty()) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 1595 | return createStringError(errc::invalid_argument, |
| 1596 | TargetLookupError.c_str()); |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 1597 | RegInfo.reset(TheTarget->createMCRegInfo(TT.str())); |
| 1598 | return Error::success(); |
| 1599 | } |
Victor Leschuk | 58d3399 | 2018-07-31 22:19:19 +0000 | [diff] [blame] | 1600 | |
| 1601 | uint8_t DWARFContext::getCUAddrSize() { |
| 1602 | // In theory, different compile units may have different address byte |
| 1603 | // sizes, but for simplicity we just use the address byte size of the |
| 1604 | // last compile unit. In practice the address size field is repeated across |
| 1605 | // various DWARF headers (at least in version 5) to make it easier to dump |
| 1606 | // them independently, not to enable varying the address size. |
| 1607 | uint8_t Addr = 0; |
| 1608 | for (const auto &CU : compile_units()) { |
| 1609 | Addr = CU->getAddressByteSize(); |
| 1610 | break; |
| 1611 | } |
| 1612 | return Addr; |
| 1613 | } |
Victor Leschuk | cf1f714 | 2018-08-23 12:43:33 +0000 | [diff] [blame] | 1614 | |
| 1615 | void DWARFContext::dumpWarning(Error Warning) { |
| 1616 | handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) { |
| 1617 | WithColor::warning() << Info.message() << '\n'; |
| 1618 | }); |
| 1619 | } |