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