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