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