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