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