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