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