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 | |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
| 12 | #include "llvm/ADT/STLExtras.h" |
Alexey Samsonov | 3d0e3ed | 2013-04-17 14:27:04 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringSwitch.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" |
| 17 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 18 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
| 19 | #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.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" |
George Rimar | 4bf3083 | 2017-01-11 15:26:41 +0000 | [diff] [blame] | 32 | #include "llvm/Object/Decompressor.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 33 | #include "llvm/Object/MachO.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 34 | #include "llvm/Object/ObjectFile.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 35 | #include "llvm/Object/RelocVisitor.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Casting.h" |
| 37 | #include "llvm/Support/DataExtractor.h" |
| 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/Error.h" |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Format.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 43 | #include <algorithm> |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 44 | #include <cstdint> |
| 45 | #include <string> |
| 46 | #include <utility> |
| 47 | #include <vector> |
| 48 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 49 | using namespace llvm; |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 50 | using namespace dwarf; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 51 | using namespace object; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 52 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 53 | #define DEBUG_TYPE "dwarf" |
| 54 | |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 55 | typedef DWARFDebugLine::LineTable DWARFLineTable; |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 56 | typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind; |
| 57 | typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind; |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 58 | |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 59 | static void dumpAccelSection(raw_ostream &OS, StringRef Name, |
| 60 | const DWARFSection& Section, StringRef StringSection, |
| 61 | bool LittleEndian) { |
| 62 | DataExtractor AccelSection(Section.Data, LittleEndian, 0); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 63 | DataExtractor StrData(StringSection, LittleEndian, 0); |
| 64 | OS << "\n." << Name << " contents:\n"; |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 65 | DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 66 | if (!Accel.extract()) |
| 67 | return; |
| 68 | Accel.dump(OS); |
| 69 | } |
| 70 | |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 71 | void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH, |
| 72 | bool SummarizeTypes) { |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 73 | if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) { |
| 74 | OS << ".debug_abbrev contents:\n"; |
| 75 | getDebugAbbrev()->dump(OS); |
| 76 | } |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 77 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 78 | if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) |
| 79 | if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) { |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 80 | OS << "\n.debug_abbrev.dwo contents:\n"; |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 81 | D->dump(OS); |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 82 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 83 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 84 | if (DumpType == DIDT_All || DumpType == DIDT_Info) { |
| 85 | OS << "\n.debug_info contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 86 | for (const auto &CU : compile_units()) |
| 87 | CU->dump(OS); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 88 | } |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 89 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 90 | if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) && |
| 91 | getNumDWOCompileUnits()) { |
| 92 | OS << "\n.debug_info.dwo contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 93 | for (const auto &DWOCU : dwo_compile_units()) |
| 94 | DWOCU->dump(OS); |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 95 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 96 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 97 | if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) { |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 98 | OS << "\n.debug_types contents:\n"; |
Frederic Riss | 312a02e | 2014-09-29 13:56:39 +0000 | [diff] [blame] | 99 | for (const auto &TUS : type_unit_sections()) |
| 100 | for (const auto &TU : TUS) |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 101 | TU->dump(OS, SummarizeTypes); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 104 | if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) && |
| 105 | getNumDWOTypeUnits()) { |
| 106 | OS << "\n.debug_types.dwo contents:\n"; |
Frederic Riss | 312a02e | 2014-09-29 13:56:39 +0000 | [diff] [blame] | 107 | for (const auto &DWOTUS : dwo_type_unit_sections()) |
| 108 | for (const auto &DWOTU : DWOTUS) |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 109 | DWOTU->dump(OS, SummarizeTypes); |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 110 | } |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 111 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 112 | if (DumpType == DIDT_All || DumpType == DIDT_Loc) { |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 113 | OS << "\n.debug_loc contents:\n"; |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 114 | getDebugLoc()->dump(OS); |
| 115 | } |
| 116 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 117 | if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) { |
| 118 | OS << "\n.debug_loc.dwo contents:\n"; |
| 119 | getDebugLocDWO()->dump(OS); |
| 120 | } |
| 121 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 122 | if (DumpType == DIDT_All || DumpType == DIDT_Frames) { |
| 123 | OS << "\n.debug_frame contents:\n"; |
| 124 | getDebugFrame()->dump(OS); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 125 | if (DumpEH) { |
| 126 | OS << "\n.eh_frame contents:\n"; |
| 127 | getEHFrame()->dump(OS); |
| 128 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 131 | if (DumpType == DIDT_All || DumpType == DIDT_Macro) { |
| 132 | OS << "\n.debug_macinfo contents:\n"; |
| 133 | getDebugMacro()->dump(OS); |
| 134 | } |
| 135 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 136 | uint32_t offset = 0; |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 137 | if (DumpType == DIDT_All || DumpType == DIDT_Aranges) { |
| 138 | OS << "\n.debug_aranges contents:\n"; |
| 139 | DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0); |
| 140 | DWARFDebugArangeSet set; |
| 141 | while (set.extract(arangesData, &offset)) |
| 142 | set.dump(OS); |
| 143 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 144 | |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 145 | uint8_t savedAddressByteSize = 0; |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 146 | if (DumpType == DIDT_All || DumpType == DIDT_Line) { |
| 147 | OS << "\n.debug_line contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 148 | for (const auto &CU : compile_units()) { |
| 149 | savedAddressByteSize = CU->getAddressByteSize(); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 150 | auto CUDIE = CU->getUnitDIE(); |
| 151 | if (!CUDIE) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 152 | continue; |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 153 | if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) { |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 154 | DataExtractor lineData(getLineSection().Data, isLittleEndian(), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 155 | savedAddressByteSize); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 156 | DWARFDebugLine::LineTable LineTable; |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 157 | uint32_t Offset = *StmtOffset; |
| 158 | LineTable.parse(lineData, &getLineSection().Relocs, &Offset); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 159 | LineTable.dump(OS); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 160 | } |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 163 | |
David Blaikie | 65a8efe | 2015-11-11 19:28:21 +0000 | [diff] [blame] | 164 | if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) { |
| 165 | OS << "\n.debug_cu_index contents:\n"; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 166 | getCUIndex().dump(OS); |
David Blaikie | 65a8efe | 2015-11-11 19:28:21 +0000 | [diff] [blame] | 167 | } |
| 168 | |
David Blaikie | 51c4028 | 2015-11-11 19:40:49 +0000 | [diff] [blame] | 169 | if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) { |
| 170 | OS << "\n.debug_tu_index contents:\n"; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 171 | getTUIndex().dump(OS); |
David Blaikie | 51c4028 | 2015-11-11 19:40:49 +0000 | [diff] [blame] | 172 | } |
| 173 | |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 174 | if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) { |
| 175 | OS << "\n.debug_line.dwo contents:\n"; |
| 176 | unsigned stmtOffset = 0; |
| 177 | DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(), |
| 178 | savedAddressByteSize); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 179 | DWARFDebugLine::LineTable LineTable; |
| 180 | while (LineTable.Prologue.parse(lineData, &stmtOffset)) { |
| 181 | LineTable.dump(OS); |
| 182 | LineTable.clear(); |
| 183 | } |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 186 | if (DumpType == DIDT_All || DumpType == DIDT_Str) { |
| 187 | OS << "\n.debug_str contents:\n"; |
| 188 | DataExtractor strData(getStringSection(), isLittleEndian(), 0); |
| 189 | offset = 0; |
| 190 | uint32_t strOffset = 0; |
| 191 | while (const char *s = strData.getCStr(&offset)) { |
| 192 | OS << format("0x%8.8x: \"%s\"\n", strOffset, s); |
| 193 | strOffset = offset; |
| 194 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 195 | } |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 196 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 197 | if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) && |
| 198 | !getStringDWOSection().empty()) { |
| 199 | OS << "\n.debug_str.dwo contents:\n"; |
| 200 | DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0); |
| 201 | offset = 0; |
| 202 | uint32_t strDWOOffset = 0; |
| 203 | while (const char *s = strDWOData.getCStr(&offset)) { |
| 204 | OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s); |
| 205 | strDWOOffset = offset; |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 206 | } |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 207 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 208 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 209 | if (DumpType == DIDT_All || DumpType == DIDT_Ranges) { |
| 210 | OS << "\n.debug_ranges contents:\n"; |
| 211 | // In fact, different compile units may have different address byte |
| 212 | // sizes, but for simplicity we just use the address byte size of the last |
| 213 | // compile unit (there is no easy and fast way to associate address range |
| 214 | // list and the compile unit it describes). |
| 215 | DataExtractor rangesData(getRangeSection(), isLittleEndian(), |
| 216 | savedAddressByteSize); |
| 217 | offset = 0; |
| 218 | DWARFDebugRangeList rangeList; |
| 219 | while (rangeList.extract(rangesData, &offset)) |
| 220 | rangeList.dump(OS); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 221 | } |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 222 | |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 223 | if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 224 | DWARFDebugPubTable(getPubNamesSection(), isLittleEndian(), false) |
| 225 | .dump("debug_pubnames", OS); |
Krzysztof Parzyszek | 97438dc | 2013-02-12 16:20:28 +0000 | [diff] [blame] | 226 | |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 227 | if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes) |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 228 | DWARFDebugPubTable(getPubTypesSection(), isLittleEndian(), false) |
| 229 | .dump("debug_pubtypes", OS); |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 230 | |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 231 | if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames) |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 232 | DWARFDebugPubTable(getGnuPubNamesSection(), isLittleEndian(), |
| 233 | true /* GnuStyle */) |
| 234 | .dump("debug_gnu_pubnames", OS); |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 235 | |
| 236 | if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes) |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 237 | DWARFDebugPubTable(getGnuPubTypesSection(), isLittleEndian(), |
| 238 | true /* GnuStyle */) |
| 239 | .dump("debug_gnu_pubtypes", OS); |
David Blaikie | 404d304 | 2013-09-19 23:01:29 +0000 | [diff] [blame] | 240 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 241 | if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) && |
| 242 | !getStringOffsetDWOSection().empty()) { |
| 243 | OS << "\n.debug_str_offsets.dwo contents:\n"; |
| 244 | DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), |
| 245 | 0); |
| 246 | offset = 0; |
| 247 | uint64_t size = getStringOffsetDWOSection().size(); |
| 248 | while (offset < size) { |
| 249 | OS << format("0x%8.8x: ", offset); |
| 250 | OS << format("%8.8x\n", strOffsetExt.getU32(&offset)); |
Eric Christopher | 92f3c0b | 2013-05-06 17:50:42 +0000 | [diff] [blame] | 251 | } |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 252 | } |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 253 | |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 254 | if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) && |
| 255 | !getGdbIndexSection().empty()) { |
| 256 | OS << "\n.gnu_index contents:\n"; |
| 257 | getGdbIndex().dump(OS); |
| 258 | } |
| 259 | |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 260 | if (DumpType == DIDT_All || DumpType == DIDT_AppleNames) |
| 261 | dumpAccelSection(OS, "apple_names", getAppleNamesSection(), |
| 262 | getStringSection(), isLittleEndian()); |
| 263 | |
| 264 | if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes) |
| 265 | dumpAccelSection(OS, "apple_types", getAppleTypesSection(), |
| 266 | getStringSection(), isLittleEndian()); |
| 267 | |
| 268 | if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces) |
| 269 | dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(), |
| 270 | getStringSection(), isLittleEndian()); |
| 271 | |
| 272 | if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC) |
| 273 | dumpAccelSection(OS, "apple_objc", getAppleObjCSection(), |
| 274 | getStringSection(), isLittleEndian()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 275 | } |
| 276 | |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 277 | const DWARFUnitIndex &DWARFContext::getCUIndex() { |
| 278 | if (CUIndex) |
| 279 | return *CUIndex; |
| 280 | |
| 281 | DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0); |
| 282 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 283 | CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 284 | CUIndex->parse(CUIndexData); |
| 285 | return *CUIndex; |
| 286 | } |
| 287 | |
| 288 | const DWARFUnitIndex &DWARFContext::getTUIndex() { |
| 289 | if (TUIndex) |
| 290 | return *TUIndex; |
| 291 | |
| 292 | DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0); |
| 293 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 294 | TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 295 | TUIndex->parse(TUIndexData); |
| 296 | return *TUIndex; |
| 297 | } |
| 298 | |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 299 | DWARFGdbIndex &DWARFContext::getGdbIndex() { |
| 300 | if (GdbIndex) |
| 301 | return *GdbIndex; |
| 302 | |
| 303 | DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0); |
| 304 | GdbIndex = llvm::make_unique<DWARFGdbIndex>(); |
| 305 | GdbIndex->parse(GdbIndexData); |
| 306 | return *GdbIndex; |
| 307 | } |
| 308 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 309 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { |
| 310 | if (Abbrev) |
| 311 | return Abbrev.get(); |
| 312 | |
| 313 | DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0); |
| 314 | |
| 315 | Abbrev.reset(new DWARFDebugAbbrev()); |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 316 | Abbrev->extract(abbrData); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 317 | return Abbrev.get(); |
| 318 | } |
| 319 | |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 320 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { |
| 321 | if (AbbrevDWO) |
| 322 | return AbbrevDWO.get(); |
| 323 | |
| 324 | DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0); |
| 325 | AbbrevDWO.reset(new DWARFDebugAbbrev()); |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 326 | AbbrevDWO->extract(abbrData); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 327 | return AbbrevDWO.get(); |
| 328 | } |
| 329 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 330 | const DWARFDebugLoc *DWARFContext::getDebugLoc() { |
| 331 | if (Loc) |
| 332 | return Loc.get(); |
| 333 | |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 334 | DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0); |
| 335 | Loc.reset(new DWARFDebugLoc(getLocSection().Relocs)); |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 336 | // assume all compile units have the same address byte size |
| 337 | if (getNumCompileUnits()) |
| 338 | Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize()); |
| 339 | return Loc.get(); |
| 340 | } |
| 341 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 342 | const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() { |
| 343 | if (LocDWO) |
| 344 | return LocDWO.get(); |
| 345 | |
| 346 | DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0); |
| 347 | LocDWO.reset(new DWARFDebugLocDWO()); |
| 348 | LocDWO->parse(LocData); |
| 349 | return LocDWO.get(); |
| 350 | } |
| 351 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 352 | const DWARFDebugAranges *DWARFContext::getDebugAranges() { |
| 353 | if (Aranges) |
| 354 | return Aranges.get(); |
| 355 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 356 | Aranges.reset(new DWARFDebugAranges()); |
Alexey Samsonov | a1694c1 | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 357 | Aranges->generate(this); |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 358 | return Aranges.get(); |
| 359 | } |
| 360 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 361 | const DWARFDebugFrame *DWARFContext::getDebugFrame() { |
| 362 | if (DebugFrame) |
| 363 | return DebugFrame.get(); |
| 364 | |
| 365 | // There's a "bug" in the DWARFv3 standard with respect to the target address |
| 366 | // size within debug frame sections. While DWARF is supposed to be independent |
| 367 | // of its container, FDEs have fields with size being "target address size", |
| 368 | // which isn't specified in DWARF in general. It's only specified for CUs, but |
| 369 | // .eh_frame can appear without a .debug_info section. Follow the example of |
| 370 | // other tools (libdwarf) and extract this from the container (ObjectFile |
| 371 | // provides this information). This problem is fixed in DWARFv4 |
| 372 | // See this dwarf-discuss discussion for more details: |
| 373 | // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html |
| 374 | DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(), |
| 375 | getAddressSize()); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 376 | DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */)); |
| 377 | DebugFrame->parse(debugFrameData); |
| 378 | return DebugFrame.get(); |
| 379 | } |
| 380 | |
| 381 | const DWARFDebugFrame *DWARFContext::getEHFrame() { |
| 382 | if (EHFrame) |
| 383 | return EHFrame.get(); |
| 384 | |
| 385 | DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(), |
| 386 | getAddressSize()); |
| 387 | DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 388 | DebugFrame->parse(debugFrameData); |
| 389 | return DebugFrame.get(); |
| 390 | } |
| 391 | |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 392 | const DWARFDebugMacro *DWARFContext::getDebugMacro() { |
| 393 | if (Macro) |
| 394 | return Macro.get(); |
| 395 | |
| 396 | DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0); |
| 397 | Macro.reset(new DWARFDebugMacro()); |
| 398 | Macro->parse(MacinfoData); |
| 399 | return Macro.get(); |
| 400 | } |
| 401 | |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 402 | const DWARFLineTable * |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 403 | DWARFContext::getLineTableForUnit(DWARFUnit *U) { |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 404 | if (!Line) |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 405 | Line.reset(new DWARFDebugLine(&getLineSection().Relocs)); |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 406 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 407 | auto UnitDIE = U->getUnitDIE(); |
| 408 | if (!UnitDIE) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 409 | return nullptr; |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 410 | |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 411 | auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list)); |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 412 | if (!Offset) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 413 | return nullptr; // No line table for this compile unit. |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 414 | |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 415 | uint32_t stmtOffset = *Offset + U->getLineTableOffset(); |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 416 | // See if the line table is cached. |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 417 | if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 418 | return lt; |
| 419 | |
| 420 | // We have to parse it first. |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 421 | DataExtractor lineData(U->getLineSection(), isLittleEndian(), |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 422 | U->getAddressByteSize()); |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 423 | return Line->getOrParseLineTable(lineData, stmtOffset); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 426 | void DWARFContext::parseCompileUnits() { |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 427 | CUs.parse(*this, getInfoSection()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 428 | } |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 429 | |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 430 | void DWARFContext::parseTypeUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 431 | if (!TUs.empty()) |
| 432 | return; |
| 433 | for (const auto &I : getTypesSections()) { |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 434 | TUs.emplace_back(); |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 435 | TUs.back().parse(*this, I.second); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 439 | void DWARFContext::parseDWOCompileUnits() { |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 440 | DWOCUs.parseDWO(*this, getInfoDWOSection()); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 441 | } |
| 442 | |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 443 | void DWARFContext::parseDWOTypeUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 444 | if (!DWOTUs.empty()) |
| 445 | return; |
| 446 | for (const auto &I : getTypesDWOSections()) { |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 447 | DWOTUs.emplace_back(); |
Alexey Samsonov | 8cd4c9d | 2014-10-08 00:07:53 +0000 | [diff] [blame] | 448 | DWOTUs.back().parseDWO(*this, I.second); |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 452 | DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 453 | parseCompileUnits(); |
Frederic Riss | 4e126a0 | 2014-09-15 07:50:27 +0000 | [diff] [blame] | 454 | return CUs.getUnitForOffset(Offset); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 457 | DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { |
Benjamin Kramer | 112ec17 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 458 | // First, get the offset of the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 459 | uint32_t CUOffset = getDebugAranges()->findAddress(Address); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 460 | // Retrieve the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 461 | return getCompileUnitForOffset(CUOffset); |
| 462 | } |
| 463 | |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 464 | static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU, |
| 465 | uint64_t Address, |
| 466 | FunctionNameKind Kind, |
| 467 | std::string &FunctionName, |
| 468 | uint32_t &StartLine) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 469 | // The address may correspond to instruction in some inlined function, |
| 470 | // 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] | 471 | // name of the topmost function in it. |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 472 | SmallVector<DWARFDie, 4> InlinedChain; |
| 473 | CU->getInlinedChainForAddress(Address, InlinedChain); |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 474 | if (InlinedChain.empty()) |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 475 | return false; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 476 | |
| 477 | const DWARFDie &DIE = InlinedChain[0]; |
| 478 | bool FoundResult = false; |
| 479 | const char *Name = nullptr; |
| 480 | if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 481 | FunctionName = Name; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 482 | FoundResult = true; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 483 | } |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 484 | if (auto DeclLineResult = DIE.getDeclLine()) { |
| 485 | StartLine = DeclLineResult; |
| 486 | FoundResult = true; |
| 487 | } |
| 488 | |
| 489 | return FoundResult; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 492 | DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address, |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 493 | DILineInfoSpecifier Spec) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 494 | DILineInfo Result; |
| 495 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 496 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 497 | if (!CU) |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 498 | return Result; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 499 | getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, |
| 500 | Result.FunctionName, |
| 501 | Result.StartLine); |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 502 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 503 | if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) |
| 504 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 505 | Spec.FLIKind, Result); |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 506 | } |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 507 | return Result; |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 508 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 509 | |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 510 | DILineInfoTable |
| 511 | DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size, |
| 512 | DILineInfoSpecifier Spec) { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 513 | DILineInfoTable Lines; |
| 514 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 515 | if (!CU) |
| 516 | return Lines; |
| 517 | |
| 518 | std::string FunctionName = "<invalid>"; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 519 | uint32_t StartLine = 0; |
| 520 | getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName, |
| 521 | StartLine); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 522 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 523 | // If the Specifier says we don't need FileLineInfo, just |
| 524 | // return the top-most function at the starting address. |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 525 | if (Spec.FLIKind == FileLineInfoKind::None) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 526 | DILineInfo Result; |
| 527 | Result.FunctionName = FunctionName; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 528 | Result.StartLine = StartLine; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 529 | Lines.push_back(std::make_pair(Address, Result)); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 530 | return Lines; |
| 531 | } |
| 532 | |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 533 | const DWARFLineTable *LineTable = getLineTableForUnit(CU); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 534 | |
| 535 | // Get the index of row we're looking for in the line table. |
| 536 | std::vector<uint32_t> RowVector; |
| 537 | if (!LineTable->lookupAddressRange(Address, Size, RowVector)) |
| 538 | return Lines; |
| 539 | |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 540 | for (uint32_t RowIndex : RowVector) { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 541 | // Take file number and line/column from the row. |
| 542 | const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 543 | DILineInfo Result; |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 544 | LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(), |
| 545 | Spec.FLIKind, Result.FileName); |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 546 | Result.FunctionName = FunctionName; |
| 547 | Result.Line = Row.Line; |
| 548 | Result.Column = Row.Column; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 549 | Result.StartLine = StartLine; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 550 | Lines.push_back(std::make_pair(Row.Address, Result)); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | return Lines; |
| 554 | } |
| 555 | |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 556 | DIInliningInfo |
| 557 | DWARFContext::getInliningInfoForAddress(uint64_t Address, |
| 558 | DILineInfoSpecifier Spec) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 559 | DIInliningInfo InliningInfo; |
| 560 | |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 561 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 562 | if (!CU) |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 563 | return InliningInfo; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 564 | |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 565 | const DWARFLineTable *LineTable = nullptr; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 566 | SmallVector<DWARFDie, 4> InlinedChain; |
| 567 | CU->getInlinedChainForAddress(Address, InlinedChain); |
| 568 | if (InlinedChain.size() == 0) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 569 | // If there is no DIE for address (e.g. it is in unavailable .dwo file), |
| 570 | // try to at least get file/line info from symbol table. |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 571 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 572 | DILineInfo Frame; |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 573 | LineTable = getLineTableForUnit(CU); |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 574 | if (LineTable && |
| 575 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 576 | Spec.FLIKind, Frame)) |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 577 | InliningInfo.addFrame(Frame); |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 578 | } |
| 579 | return InliningInfo; |
| 580 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 581 | |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 582 | uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 583 | for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) { |
| 584 | DWARFDie &FunctionDIE = InlinedChain[i]; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 585 | DILineInfo Frame; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 586 | // Get function name if necessary. |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 587 | if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind)) |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 588 | Frame.FunctionName = Name; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 589 | if (auto DeclLineResult = FunctionDIE.getDeclLine()) |
| 590 | Frame.StartLine = DeclLineResult; |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 591 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 592 | if (i == 0) { |
| 593 | // For the topmost frame, initialize the line table of this |
| 594 | // compile unit and fetch file/line info from it. |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 595 | LineTable = getLineTableForUnit(CU); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 596 | // For the topmost routine, get file/line info from line table. |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 597 | if (LineTable) |
| 598 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 599 | Spec.FLIKind, Frame); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 600 | } else { |
| 601 | // Otherwise, use call file, call line and call column from |
| 602 | // previous DIE in inlined chain. |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 603 | if (LineTable) |
| 604 | LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(), |
| 605 | Spec.FLIKind, Frame.FileName); |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 606 | Frame.Line = CallLine; |
| 607 | Frame.Column = CallColumn; |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 608 | Frame.Discriminator = CallDiscriminator; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 609 | } |
| 610 | // Get call file/line/column of a current DIE. |
| 611 | if (i + 1 < n) { |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 612 | FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn, |
| 613 | CallDiscriminator); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 616 | InliningInfo.addFrame(Frame); |
| 617 | } |
| 618 | return InliningInfo; |
| 619 | } |
| 620 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 621 | static Error createError(const Twine &Reason, llvm::Error E) { |
| 622 | return make_error<StringError>(Reason + toString(std::move(E)), |
| 623 | inconvertibleErrorCode()); |
| 624 | } |
| 625 | |
| 626 | /// Returns the address of symbol relocation used against. Used for futher |
| 627 | /// relocations computation. Symbol's section load address is taken in account if |
| 628 | /// LoadedObjectInfo interface is provided. |
| 629 | static Expected<uint64_t> getSymbolAddress(const object::ObjectFile &Obj, |
| 630 | const RelocationRef &Reloc, |
| 631 | const LoadedObjectInfo *L) { |
| 632 | uint64_t Ret = 0; |
| 633 | object::section_iterator RSec = Obj.section_end(); |
| 634 | object::symbol_iterator Sym = Reloc.getSymbol(); |
| 635 | |
| 636 | // First calculate the address of the symbol or section as it appears |
| 637 | // in the object file |
| 638 | if (Sym != Obj.symbol_end()) { |
| 639 | Expected<uint64_t> SymAddrOrErr = Sym->getAddress(); |
| 640 | if (!SymAddrOrErr) |
| 641 | return createError("error: failed to compute symbol address: ", |
| 642 | SymAddrOrErr.takeError()); |
| 643 | |
| 644 | // Also remember what section this symbol is in for later |
| 645 | auto SectOrErr = Sym->getSection(); |
| 646 | if (!SectOrErr) |
| 647 | return createError("error: failed to get symbol section: ", |
| 648 | SectOrErr.takeError()); |
| 649 | |
| 650 | RSec = *SectOrErr; |
| 651 | Ret = *SymAddrOrErr; |
| 652 | } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) { |
| 653 | RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl()); |
| 654 | Ret = RSec->getAddress(); |
| 655 | } |
| 656 | |
| 657 | // If we are given load addresses for the sections, we need to adjust: |
| 658 | // SymAddr = (Address of Symbol Or Section in File) - |
| 659 | // (Address of Section in File) + |
| 660 | // (Load Address of Section) |
| 661 | // RSec is now either the section being targeted or the section |
| 662 | // containing the symbol being targeted. In either case, |
| 663 | // we need to perform the same computation. |
| 664 | if (L && RSec != Obj.section_end()) |
| 665 | if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec)) |
| 666 | Ret += SectionLoadAddress - RSec->getAddress(); |
| 667 | return Ret; |
| 668 | } |
| 669 | |
| 670 | static bool isRelocScattered(const object::ObjectFile &Obj, |
| 671 | const RelocationRef &Reloc) { |
George Rimar | d4998b0 | 2017-04-13 09:52:50 +0000 | [diff] [blame] | 672 | const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj); |
| 673 | if (!MachObj) |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 674 | return false; |
| 675 | // MachO also has relocations that point to sections and |
| 676 | // scattered relocations. |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 677 | auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl()); |
| 678 | return MachObj->isRelocationScattered(RelocInfo); |
| 679 | } |
| 680 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 681 | DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, |
| 682 | const LoadedObjectInfo *L) |
Rafael Espindola | a04bb5b | 2014-07-31 20:19:36 +0000 | [diff] [blame] | 683 | : IsLittleEndian(Obj.isLittleEndian()), |
| 684 | AddressSize(Obj.getBytesInAddress()) { |
| 685 | for (const SectionRef &Section : Obj.sections()) { |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 686 | StringRef name; |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 687 | Section.getName(name); |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 688 | // Skip BSS and Virtual sections, they aren't interesting. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 689 | bool IsBSS = Section.isBSS(); |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 690 | if (IsBSS) |
| 691 | continue; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 692 | bool IsVirtual = Section.isVirtual(); |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 693 | if (IsVirtual) |
| 694 | continue; |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 695 | StringRef data; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 696 | |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 697 | section_iterator RelocatedSection = Section.getRelocatedSection(); |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 698 | // Try to obtain an already relocated version of this section. |
| 699 | // Else use the unrelocated section from the object file. We'll have to |
| 700 | // apply relocations ourselves later. |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 701 | if (!L || !L->getLoadedSectionContents(*RelocatedSection,data)) |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 702 | Section.getContents(data); |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 703 | |
George Rimar | 4bf3083 | 2017-01-11 15:26:41 +0000 | [diff] [blame] | 704 | if (Decompressor::isCompressed(Section)) { |
| 705 | Expected<Decompressor> Decompressor = |
| 706 | Decompressor::create(name, data, IsLittleEndian, AddressSize == 8); |
| 707 | if (!Decompressor) |
| 708 | continue; |
George Rimar | 401e4e5 | 2016-05-24 12:48:46 +0000 | [diff] [blame] | 709 | SmallString<32> Out; |
George Rimar | 4bf3083 | 2017-01-11 15:26:41 +0000 | [diff] [blame] | 710 | if (auto Err = Decompressor->decompress(Out)) |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 711 | continue; |
George Rimar | 401e4e5 | 2016-05-24 12:48:46 +0000 | [diff] [blame] | 712 | UncompressedSections.emplace_back(std::move(Out)); |
David Blaikie | a505f24 | 2014-04-05 21:26:44 +0000 | [diff] [blame] | 713 | data = UncompressedSections.back(); |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 714 | } |
| 715 | |
George Rimar | 4bf3083 | 2017-01-11 15:26:41 +0000 | [diff] [blame] | 716 | // Compressed sections names in GNU style starts from ".z", |
| 717 | // at this point section is decompressed and we drop compression prefix. |
| 718 | name = name.substr( |
| 719 | name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes. |
| 720 | |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 721 | if (StringRef *SectionData = MapSectionToMember(name)) { |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 722 | *SectionData = data; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 723 | if (name == "debug_ranges") { |
| 724 | // FIXME: Use the other dwo range section when we emit it. |
| 725 | RangeDWOSection = data; |
| 726 | } |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 727 | } else if (name == "debug_types") { |
David Blaikie | 427e435 | 2013-09-23 23:39:55 +0000 | [diff] [blame] | 728 | // Find debug_types data by section rather than name as there are |
| 729 | // multiple, comdat grouped, debug_types sections. |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 730 | TypesSections[Section].Data = data; |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 731 | } else if (name == "debug_types.dwo") { |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 732 | TypesDWOSections[Section].Data = data; |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 733 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 734 | |
Rafael Espindola | a04bb5b | 2014-07-31 20:19:36 +0000 | [diff] [blame] | 735 | if (RelocatedSection == Obj.section_end()) |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 736 | continue; |
| 737 | |
| 738 | StringRef RelSecName; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 739 | StringRef RelSecData; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 740 | RelocatedSection->getName(RelSecName); |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 741 | |
| 742 | // If the section we're relocating was relocated already by the JIT, |
| 743 | // then we used the relocated version above, so we do not need to process |
| 744 | // relocations for it now. |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 745 | if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData)) |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 746 | continue; |
| 747 | |
Frederic Riss | 7bb1226 | 2015-08-23 04:44:21 +0000 | [diff] [blame] | 748 | // In Mach-o files, the relocations do not need to be applied if |
| 749 | // there is no load offset to apply. The value read at the |
| 750 | // relocation point already factors in the section address |
| 751 | // (actually applying the relocations will produce wrong results |
| 752 | // as the section address will be added twice). |
Craig Topper | 66059c9 | 2015-11-18 07:07:59 +0000 | [diff] [blame] | 753 | if (!L && isa<MachOObjectFile>(&Obj)) |
Frederic Riss | 7bb1226 | 2015-08-23 04:44:21 +0000 | [diff] [blame] | 754 | continue; |
| 755 | |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 756 | RelSecName = RelSecName.substr( |
| 757 | RelSecName.find_first_not_of("._")); // Skip . and _ prefixes. |
| 758 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 759 | // TODO: Add support for relocations in other sections as needed. |
| 760 | // Record relocations for the debug_info and debug_line sections. |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 761 | RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName) |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 762 | .Case("debug_info", &InfoSection.Relocs) |
| 763 | .Case("debug_loc", &LocSection.Relocs) |
| 764 | .Case("debug_info.dwo", &InfoDWOSection.Relocs) |
| 765 | .Case("debug_line", &LineSection.Relocs) |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 766 | .Case("apple_names", &AppleNamesSection.Relocs) |
| 767 | .Case("apple_types", &AppleTypesSection.Relocs) |
| 768 | .Case("apple_namespaces", &AppleNamespacesSection.Relocs) |
| 769 | .Case("apple_namespac", &AppleNamespacesSection.Relocs) |
| 770 | .Case("apple_objc", &AppleObjCSection.Relocs) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 771 | .Default(nullptr); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 772 | if (!Map) { |
David Blaikie | 427e435 | 2013-09-23 23:39:55 +0000 | [diff] [blame] | 773 | // Find debug_types relocs by section rather than name as there are |
| 774 | // multiple, comdat grouped, debug_types sections. |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 775 | if (RelSecName == "debug_types") |
| 776 | Map = &TypesSections[*RelocatedSection].Relocs; |
| 777 | else if (RelSecName == "debug_types.dwo") |
| 778 | Map = &TypesDWOSections[*RelocatedSection].Relocs; |
| 779 | else |
| 780 | continue; |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 781 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 782 | |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 783 | if (Section.relocation_begin() != Section.relocation_end()) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 784 | uint64_t SectionSize = RelocatedSection->getSize(); |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 785 | for (const RelocationRef &Reloc : Section.relocations()) { |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 786 | // FIXME: it's not clear how to correctly handle scattered |
| 787 | // relocations. |
| 788 | if (isRelocScattered(Obj, Reloc)) |
| 789 | continue; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 790 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 791 | Expected<uint64_t> SymAddrOrErr = getSymbolAddress(Obj, Reloc, L); |
| 792 | if (!SymAddrOrErr) { |
Krasimir Georgiev | 4ed589d | 2017-04-12 11:33:26 +0000 | [diff] [blame] | 793 | errs() << toString(SymAddrOrErr.takeError()) << '\n'; |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 794 | continue; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 795 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 796 | |
Eric Christopher | 47e079d | 2014-10-06 06:55:55 +0000 | [diff] [blame] | 797 | object::RelocVisitor V(Obj); |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 798 | object::RelocToApply R(V.visit(Reloc.getType(), Reloc, *SymAddrOrErr)); |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 799 | if (V.error()) { |
| 800 | SmallString<32> Name; |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 801 | Reloc.getTypeName(Name); |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 802 | errs() << "error: failed to compute relocation: " |
| 803 | << Name << "\n"; |
| 804 | continue; |
| 805 | } |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 806 | uint64_t Address = Reloc.getOffset(); |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 807 | if (Address + R.Width > SectionSize) { |
| 808 | errs() << "error: " << R.Width << "-byte relocation starting " |
| 809 | << Address << " bytes into section " << name << " which is " |
| 810 | << SectionSize << " bytes long.\n"; |
| 811 | continue; |
| 812 | } |
| 813 | if (R.Width > 8) { |
| 814 | errs() << "error: can't handle a relocation of more than 8 bytes at " |
| 815 | "a time.\n"; |
| 816 | continue; |
| 817 | } |
| 818 | DEBUG(dbgs() << "Writing " << format("%p", R.Value) |
| 819 | << " at " << format("%p", Address) |
| 820 | << " with width " << format("%d", R.Width) |
| 821 | << "\n"); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 822 | Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value))); |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 828 | DWARFContextInMemory::DWARFContextInMemory( |
| 829 | const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize, |
| 830 | bool isLittleEndian) |
| 831 | : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) { |
| 832 | for (const auto &SecIt : Sections) { |
| 833 | if (StringRef *SectionData = MapSectionToMember(SecIt.first())) |
| 834 | *SectionData = SecIt.second->getBuffer(); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) { |
| 839 | return StringSwitch<StringRef *>(Name) |
| 840 | .Case("debug_info", &InfoSection.Data) |
| 841 | .Case("debug_abbrev", &AbbrevSection) |
| 842 | .Case("debug_loc", &LocSection.Data) |
| 843 | .Case("debug_line", &LineSection.Data) |
| 844 | .Case("debug_aranges", &ARangeSection) |
| 845 | .Case("debug_frame", &DebugFrameSection) |
| 846 | .Case("eh_frame", &EHFrameSection) |
| 847 | .Case("debug_str", &StringSection) |
| 848 | .Case("debug_ranges", &RangeSection) |
| 849 | .Case("debug_macinfo", &MacinfoSection) |
| 850 | .Case("debug_pubnames", &PubNamesSection) |
| 851 | .Case("debug_pubtypes", &PubTypesSection) |
| 852 | .Case("debug_gnu_pubnames", &GnuPubNamesSection) |
| 853 | .Case("debug_gnu_pubtypes", &GnuPubTypesSection) |
| 854 | .Case("debug_info.dwo", &InfoDWOSection.Data) |
| 855 | .Case("debug_abbrev.dwo", &AbbrevDWOSection) |
| 856 | .Case("debug_loc.dwo", &LocDWOSection.Data) |
| 857 | .Case("debug_line.dwo", &LineDWOSection.Data) |
| 858 | .Case("debug_str.dwo", &StringDWOSection) |
| 859 | .Case("debug_str_offsets.dwo", &StringOffsetDWOSection) |
| 860 | .Case("debug_addr", &AddrSection) |
| 861 | .Case("apple_names", &AppleNamesSection.Data) |
| 862 | .Case("apple_types", &AppleTypesSection.Data) |
| 863 | .Case("apple_namespaces", &AppleNamespacesSection.Data) |
| 864 | .Case("apple_namespac", &AppleNamespacesSection.Data) |
| 865 | .Case("apple_objc", &AppleObjCSection.Data) |
| 866 | .Case("debug_cu_index", &CUIndexSection) |
| 867 | .Case("debug_tu_index", &TUIndexSection) |
| 868 | .Case("gdb_index", &GdbIndexSection) |
| 869 | // Any more debug info sections go here. |
| 870 | .Default(nullptr); |
| 871 | } |
| 872 | |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 873 | void DWARFContextInMemory::anchor() {} |