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