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