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