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