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