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 | |
| 10 | #include "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" |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Compression.h" |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Dwarf.h" |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Format.h" |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Path.h" |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 20 | using namespace dwarf; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 21 | using namespace object; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 22 | |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 23 | typedef DWARFDebugLine::LineTable DWARFLineTable; |
| 24 | |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 25 | static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data, |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 26 | bool LittleEndian, bool GnuStyle) { |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 27 | OS << "\n." << Name << " contents:\n"; |
| 28 | DataExtractor pubNames(Data, LittleEndian, 0); |
| 29 | uint32_t offset = 0; |
David Blaikie | 8a263cb | 2013-11-26 00:22:37 +0000 | [diff] [blame] | 30 | while (pubNames.isValidOffset(offset)) { |
| 31 | OS << "length = " << format("0x%08x", pubNames.getU32(&offset)); |
| 32 | OS << " version = " << format("0x%04x", pubNames.getU16(&offset)); |
| 33 | OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset)); |
| 34 | OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n'; |
| 35 | if (GnuStyle) |
| 36 | OS << "Offset Linkage Kind Name\n"; |
| 37 | else |
| 38 | OS << "Offset Name\n"; |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 39 | |
David Blaikie | 8a263cb | 2013-11-26 00:22:37 +0000 | [diff] [blame] | 40 | while (offset < Data.size()) { |
| 41 | uint32_t dieRef = pubNames.getU32(&offset); |
| 42 | if (dieRef == 0) |
| 43 | break; |
| 44 | OS << format("0x%8.8x ", dieRef); |
| 45 | if (GnuStyle) { |
| 46 | PubIndexEntryDescriptor desc(pubNames.getU8(&offset)); |
| 47 | OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage)) |
| 48 | << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind)) |
| 49 | << ' '; |
| 50 | } |
| 51 | OS << '\"' << pubNames.getCStr(&offset) << "\"\n"; |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 52 | } |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 56 | void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) { |
| 57 | if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) { |
| 58 | OS << ".debug_abbrev contents:\n"; |
| 59 | getDebugAbbrev()->dump(OS); |
| 60 | } |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 61 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 62 | if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) |
| 63 | if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) { |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 64 | OS << "\n.debug_abbrev.dwo contents:\n"; |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 65 | D->dump(OS); |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 66 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 67 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 68 | if (DumpType == DIDT_All || DumpType == DIDT_Info) { |
| 69 | OS << "\n.debug_info contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 70 | for (const auto &CU : compile_units()) |
| 71 | CU->dump(OS); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 72 | } |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 73 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 74 | if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) && |
| 75 | getNumDWOCompileUnits()) { |
| 76 | OS << "\n.debug_info.dwo contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 77 | for (const auto &DWOCU : dwo_compile_units()) |
| 78 | DWOCU->dump(OS); |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 79 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 80 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 81 | if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) { |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 82 | OS << "\n.debug_types contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 83 | for (const auto &TU : type_units()) |
| 84 | TU->dump(OS); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 87 | if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) && |
| 88 | getNumDWOTypeUnits()) { |
| 89 | OS << "\n.debug_types.dwo contents:\n"; |
| 90 | for (const auto &DWOTU : dwo_type_units()) |
| 91 | DWOTU->dump(OS); |
| 92 | } |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 93 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 94 | if (DumpType == DIDT_All || DumpType == DIDT_Loc) { |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 95 | OS << "\n.debug_loc contents:\n"; |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 96 | getDebugLoc()->dump(OS); |
| 97 | } |
| 98 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 99 | if (DumpType == DIDT_All || DumpType == DIDT_Frames) { |
| 100 | OS << "\n.debug_frame contents:\n"; |
| 101 | getDebugFrame()->dump(OS); |
| 102 | } |
| 103 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 104 | uint32_t offset = 0; |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 105 | if (DumpType == DIDT_All || DumpType == DIDT_Aranges) { |
| 106 | OS << "\n.debug_aranges contents:\n"; |
| 107 | DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0); |
| 108 | DWARFDebugArangeSet set; |
| 109 | while (set.extract(arangesData, &offset)) |
| 110 | set.dump(OS); |
| 111 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 112 | |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 113 | uint8_t savedAddressByteSize = 0; |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 114 | if (DumpType == DIDT_All || DumpType == DIDT_Line) { |
| 115 | OS << "\n.debug_line contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 116 | for (const auto &CU : compile_units()) { |
| 117 | savedAddressByteSize = CU->getAddressByteSize(); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 118 | unsigned stmtOffset = |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 119 | CU->getCompileUnitDIE()->getAttributeValueAsSectionOffset( |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 120 | CU.get(), DW_AT_stmt_list, -1U); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 121 | if (stmtOffset != -1U) { |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 122 | DataExtractor lineData(getLineSection().Data, isLittleEndian(), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 123 | savedAddressByteSize); |
| 124 | DWARFDebugLine::DumpingState state(OS); |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 125 | DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 126 | } |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 129 | |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 130 | if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) { |
| 131 | OS << "\n.debug_line.dwo contents:\n"; |
| 132 | unsigned stmtOffset = 0; |
| 133 | DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(), |
| 134 | savedAddressByteSize); |
| 135 | DWARFDebugLine::DumpingState state(OS); |
| 136 | while (DWARFDebugLine::parsePrologue(lineData, &stmtOffset, &state.Prologue)) |
| 137 | state.finalize(); |
| 138 | } |
| 139 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 140 | if (DumpType == DIDT_All || DumpType == DIDT_Str) { |
| 141 | OS << "\n.debug_str contents:\n"; |
| 142 | DataExtractor strData(getStringSection(), isLittleEndian(), 0); |
| 143 | offset = 0; |
| 144 | uint32_t strOffset = 0; |
| 145 | while (const char *s = strData.getCStr(&offset)) { |
| 146 | OS << format("0x%8.8x: \"%s\"\n", strOffset, s); |
| 147 | strOffset = offset; |
| 148 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 149 | } |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 150 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 151 | if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) && |
| 152 | !getStringDWOSection().empty()) { |
| 153 | OS << "\n.debug_str.dwo contents:\n"; |
| 154 | DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0); |
| 155 | offset = 0; |
| 156 | uint32_t strDWOOffset = 0; |
| 157 | while (const char *s = strDWOData.getCStr(&offset)) { |
| 158 | OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s); |
| 159 | strDWOOffset = offset; |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 160 | } |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 161 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 162 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 163 | if (DumpType == DIDT_All || DumpType == DIDT_Ranges) { |
| 164 | OS << "\n.debug_ranges contents:\n"; |
| 165 | // In fact, different compile units may have different address byte |
| 166 | // sizes, but for simplicity we just use the address byte size of the last |
| 167 | // compile unit (there is no easy and fast way to associate address range |
| 168 | // list and the compile unit it describes). |
| 169 | DataExtractor rangesData(getRangeSection(), isLittleEndian(), |
| 170 | savedAddressByteSize); |
| 171 | offset = 0; |
| 172 | DWARFDebugRangeList rangeList; |
| 173 | while (rangeList.extract(rangesData, &offset)) |
| 174 | rangeList.dump(OS); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 175 | } |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 176 | |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 177 | if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) |
| 178 | dumpPubSection(OS, "debug_pubnames", getPubNamesSection(), |
| 179 | isLittleEndian(), false); |
Krzysztof Parzyszek | 97438dc | 2013-02-12 16:20:28 +0000 | [diff] [blame] | 180 | |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 181 | if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes) |
| 182 | dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(), |
| 183 | isLittleEndian(), false); |
| 184 | |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 185 | if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames) |
| 186 | dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(), |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 187 | isLittleEndian(), true /* GnuStyle */); |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 188 | |
| 189 | if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes) |
| 190 | dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(), |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 191 | isLittleEndian(), true /* GnuStyle */); |
David Blaikie | 404d304 | 2013-09-19 23:01:29 +0000 | [diff] [blame] | 192 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 193 | if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) && |
| 194 | !getStringOffsetDWOSection().empty()) { |
| 195 | OS << "\n.debug_str_offsets.dwo contents:\n"; |
| 196 | DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), |
| 197 | 0); |
| 198 | offset = 0; |
| 199 | uint64_t size = getStringOffsetDWOSection().size(); |
| 200 | while (offset < size) { |
| 201 | OS << format("0x%8.8x: ", offset); |
| 202 | OS << format("%8.8x\n", strOffsetExt.getU32(&offset)); |
Eric Christopher | 92f3c0b | 2013-05-06 17:50:42 +0000 | [diff] [blame] | 203 | } |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 204 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { |
| 208 | if (Abbrev) |
| 209 | return Abbrev.get(); |
| 210 | |
| 211 | DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0); |
| 212 | |
| 213 | Abbrev.reset(new DWARFDebugAbbrev()); |
| 214 | Abbrev->parse(abbrData); |
| 215 | return Abbrev.get(); |
| 216 | } |
| 217 | |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 218 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { |
| 219 | if (AbbrevDWO) |
| 220 | return AbbrevDWO.get(); |
| 221 | |
| 222 | DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0); |
| 223 | AbbrevDWO.reset(new DWARFDebugAbbrev()); |
| 224 | AbbrevDWO->parse(abbrData); |
| 225 | return AbbrevDWO.get(); |
| 226 | } |
| 227 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 228 | const DWARFDebugLoc *DWARFContext::getDebugLoc() { |
| 229 | if (Loc) |
| 230 | return Loc.get(); |
| 231 | |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 232 | DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0); |
| 233 | Loc.reset(new DWARFDebugLoc(getLocSection().Relocs)); |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 234 | // assume all compile units have the same address byte size |
| 235 | if (getNumCompileUnits()) |
| 236 | Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize()); |
| 237 | return Loc.get(); |
| 238 | } |
| 239 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 240 | const DWARFDebugAranges *DWARFContext::getDebugAranges() { |
| 241 | if (Aranges) |
| 242 | return Aranges.get(); |
| 243 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 244 | Aranges.reset(new DWARFDebugAranges()); |
Alexey Samsonov | a1694c1 | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 245 | Aranges->generate(this); |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 246 | return Aranges.get(); |
| 247 | } |
| 248 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 249 | const DWARFDebugFrame *DWARFContext::getDebugFrame() { |
| 250 | if (DebugFrame) |
| 251 | return DebugFrame.get(); |
| 252 | |
| 253 | // There's a "bug" in the DWARFv3 standard with respect to the target address |
| 254 | // size within debug frame sections. While DWARF is supposed to be independent |
| 255 | // of its container, FDEs have fields with size being "target address size", |
| 256 | // which isn't specified in DWARF in general. It's only specified for CUs, but |
| 257 | // .eh_frame can appear without a .debug_info section. Follow the example of |
| 258 | // other tools (libdwarf) and extract this from the container (ObjectFile |
| 259 | // provides this information). This problem is fixed in DWARFv4 |
| 260 | // See this dwarf-discuss discussion for more details: |
| 261 | // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html |
| 262 | DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(), |
| 263 | getAddressSize()); |
| 264 | DebugFrame.reset(new DWARFDebugFrame()); |
| 265 | DebugFrame->parse(debugFrameData); |
| 266 | return DebugFrame.get(); |
| 267 | } |
| 268 | |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 269 | const DWARFLineTable * |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 270 | DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) { |
| 271 | if (!Line) |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 272 | Line.reset(new DWARFDebugLine(&getLineSection().Relocs)); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 273 | |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 274 | unsigned stmtOffset = |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 275 | cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset( |
| 276 | cu, DW_AT_stmt_list, -1U); |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 277 | if (stmtOffset == -1U) |
| 278 | return 0; // No line table for this compile unit. |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 279 | |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 280 | // See if the line table is cached. |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 281 | if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 282 | return lt; |
| 283 | |
| 284 | // We have to parse it first. |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 285 | DataExtractor lineData(getLineSection().Data, isLittleEndian(), |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 286 | cu->getAddressByteSize()); |
| 287 | return Line->getOrParseLineTable(lineData, stmtOffset); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 290 | void DWARFContext::parseCompileUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 291 | if (!CUs.empty()) |
| 292 | return; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 293 | uint32_t offset = 0; |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 294 | const DataExtractor &DIData = DataExtractor(getInfoSection().Data, |
Eric Christopher | 0250948 | 2012-10-16 23:46:23 +0000 | [diff] [blame] | 295 | isLittleEndian(), 0); |
| 296 | while (DIData.isValidOffset(offset)) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 297 | std::unique_ptr<DWARFCompileUnit> CU(new DWARFCompileUnit( |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 298 | getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(), |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 299 | getRangeSection(), getStringSection(), StringRef(), getAddrSection(), |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 300 | &getInfoSection().Relocs, isLittleEndian())); |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 301 | if (!CU->extract(DIData, &offset)) { |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 302 | break; |
| 303 | } |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 304 | CUs.push_back(std::move(CU)); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 305 | offset = CUs.back()->getNextUnitOffset(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 308 | |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 309 | void DWARFContext::parseTypeUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 310 | if (!TUs.empty()) |
| 311 | return; |
| 312 | for (const auto &I : getTypesSections()) { |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 313 | uint32_t offset = 0; |
| 314 | const DataExtractor &DIData = |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 315 | DataExtractor(I.second.Data, isLittleEndian(), 0); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 316 | while (DIData.isValidOffset(offset)) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 317 | std::unique_ptr<DWARFTypeUnit> TU(new DWARFTypeUnit( |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 318 | getDebugAbbrev(), I.second.Data, getAbbrevSection(), |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 319 | getRangeSection(), getStringSection(), StringRef(), getAddrSection(), |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 320 | &I.second.Relocs, isLittleEndian())); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 321 | if (!TU->extract(DIData, &offset)) |
| 322 | break; |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 323 | TUs.push_back(std::move(TU)); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 324 | offset = TUs.back()->getNextUnitOffset(); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 329 | void DWARFContext::parseDWOCompileUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 330 | if (!DWOCUs.empty()) |
| 331 | return; |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 332 | uint32_t offset = 0; |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 333 | const DataExtractor &DIData = |
| 334 | DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 335 | while (DIData.isValidOffset(offset)) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 336 | std::unique_ptr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit( |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 337 | getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(), |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 338 | getRangeDWOSection(), getStringDWOSection(), |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 339 | getStringOffsetDWOSection(), getAddrSection(), |
| 340 | &getInfoDWOSection().Relocs, isLittleEndian())); |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 341 | if (!DWOCU->extract(DIData, &offset)) { |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 342 | break; |
| 343 | } |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 344 | DWOCUs.push_back(std::move(DWOCU)); |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 345 | offset = DWOCUs.back()->getNextUnitOffset(); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 349 | void DWARFContext::parseDWOTypeUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 350 | if (!DWOTUs.empty()) |
| 351 | return; |
| 352 | for (const auto &I : getTypesDWOSections()) { |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 353 | uint32_t offset = 0; |
| 354 | const DataExtractor &DIData = |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 355 | DataExtractor(I.second.Data, isLittleEndian(), 0); |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 356 | while (DIData.isValidOffset(offset)) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 357 | std::unique_ptr<DWARFTypeUnit> TU(new DWARFTypeUnit( |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 358 | getDebugAbbrevDWO(), I.second.Data, getAbbrevDWOSection(), |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 359 | getRangeDWOSection(), getStringDWOSection(), |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 360 | getStringOffsetDWOSection(), getAddrSection(), &I.second.Relocs, |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 361 | isLittleEndian())); |
| 362 | if (!TU->extract(DIData, &offset)) |
| 363 | break; |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 364 | DWOTUs.push_back(std::move(TU)); |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 365 | offset = DWOTUs.back()->getNextUnitOffset(); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 370 | namespace { |
| 371 | struct OffsetComparator { |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 372 | |
| 373 | bool operator()(const std::unique_ptr<DWARFCompileUnit> &LHS, |
| 374 | const std::unique_ptr<DWARFCompileUnit> &RHS) const { |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 375 | return LHS->getOffset() < RHS->getOffset(); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 376 | } |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 377 | bool operator()(const std::unique_ptr<DWARFCompileUnit> &LHS, |
| 378 | uint32_t RHS) const { |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 379 | return LHS->getOffset() < RHS; |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 380 | } |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 381 | bool operator()(uint32_t LHS, |
| 382 | const std::unique_ptr<DWARFCompileUnit> &RHS) const { |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 383 | return LHS < RHS->getOffset(); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 384 | } |
| 385 | }; |
| 386 | } |
| 387 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 388 | DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 389 | parseCompileUnits(); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 390 | |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 391 | std::unique_ptr<DWARFCompileUnit> *CU = |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 392 | std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator()); |
| 393 | if (CU != CUs.end()) { |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 394 | return CU->get(); |
Alexey Samsonov | a9debbf | 2013-08-23 06:56:01 +0000 | [diff] [blame] | 395 | } |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 399 | DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { |
Benjamin Kramer | 112ec17 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 400 | // First, get the offset of the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 401 | uint32_t CUOffset = getDebugAranges()->findAddress(Address); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 402 | // Retrieve the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 403 | return getCompileUnitForOffset(CUOffset); |
| 404 | } |
| 405 | |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 406 | static bool getFileNameForCompileUnit(DWARFCompileUnit *CU, |
| 407 | const DWARFLineTable *LineTable, |
| 408 | uint64_t FileIndex, |
| 409 | bool NeedsAbsoluteFilePath, |
| 410 | std::string &FileName) { |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 411 | if (CU == 0 || |
| 412 | LineTable == 0 || |
| 413 | !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath, |
| 414 | FileName)) |
| 415 | return false; |
| 416 | if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) { |
| 417 | // We may still need to append compilation directory of compile unit. |
| 418 | SmallString<16> AbsolutePath; |
| 419 | if (const char *CompilationDir = CU->getCompilationDir()) { |
| 420 | sys::path::append(AbsolutePath, CompilationDir); |
| 421 | } |
| 422 | sys::path::append(AbsolutePath, FileName); |
| 423 | FileName = AbsolutePath.str(); |
| 424 | } |
| 425 | return true; |
| 426 | } |
| 427 | |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 428 | static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU, |
| 429 | const DWARFLineTable *LineTable, |
| 430 | uint64_t Address, |
| 431 | bool NeedsAbsoluteFilePath, |
| 432 | std::string &FileName, |
| 433 | uint32_t &Line, uint32_t &Column) { |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 434 | if (CU == 0 || LineTable == 0) |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 435 | return false; |
| 436 | // Get the index of row we're looking for in the line table. |
| 437 | uint32_t RowIndex = LineTable->lookupAddress(Address); |
| 438 | if (RowIndex == -1U) |
| 439 | return false; |
| 440 | // Take file number and line/column from the row. |
| 441 | const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; |
| 442 | if (!getFileNameForCompileUnit(CU, LineTable, Row.File, |
| 443 | NeedsAbsoluteFilePath, FileName)) |
| 444 | return false; |
| 445 | Line = Row.Line; |
| 446 | Column = Row.Column; |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address, |
| 451 | DILineInfoSpecifier Specifier) { |
| 452 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 453 | if (!CU) |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 454 | return DILineInfo(); |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 455 | std::string FileName = "<invalid>"; |
| 456 | std::string FunctionName = "<invalid>"; |
| 457 | uint32_t Line = 0; |
| 458 | uint32_t Column = 0; |
| 459 | if (Specifier.needs(DILineInfoSpecifier::FunctionName)) { |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 460 | // The address may correspond to instruction in some inlined function, |
| 461 | // so we have to build the chain of inlined functions and take the |
| 462 | // name of the topmost function in it. |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 463 | const DWARFDebugInfoEntryInlinedChain &InlinedChain = |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 464 | CU->getInlinedChainForAddress(Address); |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 465 | if (InlinedChain.DIEs.size() > 0) { |
| 466 | const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0]; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 467 | if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U)) |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 468 | FunctionName = Name; |
Alexey Samsonov | b604ff2 | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 469 | } |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 470 | } |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 471 | if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) { |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 472 | const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU); |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 473 | const bool NeedsAbsoluteFilePath = |
| 474 | Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 475 | getFileLineInfoForCompileUnit(CU, LineTable, Address, |
| 476 | NeedsAbsoluteFilePath, |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 477 | FileName, Line, Column); |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 478 | } |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 479 | return DILineInfo(StringRef(FileName), StringRef(FunctionName), |
| 480 | Line, Column); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 481 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 482 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 483 | DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address, |
| 484 | uint64_t Size, |
| 485 | DILineInfoSpecifier Specifier) { |
| 486 | DILineInfoTable Lines; |
| 487 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 488 | if (!CU) |
| 489 | return Lines; |
| 490 | |
| 491 | std::string FunctionName = "<invalid>"; |
| 492 | if (Specifier.needs(DILineInfoSpecifier::FunctionName)) { |
| 493 | // The address may correspond to instruction in some inlined function, |
| 494 | // so we have to build the chain of inlined functions and take the |
| 495 | // name of the topmost function in it. |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 496 | const DWARFDebugInfoEntryInlinedChain &InlinedChain = |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 497 | CU->getInlinedChainForAddress(Address); |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 498 | if (InlinedChain.DIEs.size() > 0) { |
| 499 | const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0]; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 500 | if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U)) |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 501 | FunctionName = Name; |
| 502 | } |
| 503 | } |
| 504 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 505 | // If the Specifier says we don't need FileLineInfo, just |
| 506 | // return the top-most function at the starting address. |
| 507 | if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) { |
David Blaikie | ba860d7 | 2013-09-22 17:01:50 +0000 | [diff] [blame] | 508 | Lines.push_back( |
| 509 | std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0))); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 510 | return Lines; |
| 511 | } |
| 512 | |
| 513 | const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU); |
| 514 | const bool NeedsAbsoluteFilePath = |
| 515 | Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath); |
| 516 | |
| 517 | // Get the index of row we're looking for in the line table. |
| 518 | std::vector<uint32_t> RowVector; |
| 519 | if (!LineTable->lookupAddressRange(Address, Size, RowVector)) |
| 520 | return Lines; |
| 521 | |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 522 | for (uint32_t RowIndex : RowVector) { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 523 | // Take file number and line/column from the row. |
| 524 | const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; |
| 525 | std::string FileName = "<invalid>"; |
| 526 | getFileNameForCompileUnit(CU, LineTable, Row.File, |
| 527 | NeedsAbsoluteFilePath, FileName); |
David Blaikie | ba860d7 | 2013-09-22 17:01:50 +0000 | [diff] [blame] | 528 | Lines.push_back(std::make_pair( |
| 529 | Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column))); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | return Lines; |
| 533 | } |
| 534 | |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 535 | DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address, |
| 536 | DILineInfoSpecifier Specifier) { |
| 537 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 538 | if (!CU) |
| 539 | return DIInliningInfo(); |
| 540 | |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 541 | const DWARFDebugInfoEntryInlinedChain &InlinedChain = |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 542 | CU->getInlinedChainForAddress(Address); |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 543 | if (InlinedChain.DIEs.size() == 0) |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 544 | return DIInliningInfo(); |
| 545 | |
| 546 | DIInliningInfo InliningInfo; |
| 547 | uint32_t CallFile = 0, CallLine = 0, CallColumn = 0; |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 548 | const DWARFLineTable *LineTable = 0; |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 549 | for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) { |
| 550 | const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i]; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 551 | std::string FileName = "<invalid>"; |
| 552 | std::string FunctionName = "<invalid>"; |
| 553 | uint32_t Line = 0; |
| 554 | uint32_t Column = 0; |
| 555 | // Get function name if necessary. |
| 556 | if (Specifier.needs(DILineInfoSpecifier::FunctionName)) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 557 | if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U)) |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 558 | FunctionName = Name; |
| 559 | } |
| 560 | if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) { |
| 561 | const bool NeedsAbsoluteFilePath = |
| 562 | Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath); |
| 563 | if (i == 0) { |
| 564 | // For the topmost frame, initialize the line table of this |
| 565 | // compile unit and fetch file/line info from it. |
| 566 | LineTable = getLineTableForCompileUnit(CU); |
| 567 | // For the topmost routine, get file/line info from line table. |
| 568 | getFileLineInfoForCompileUnit(CU, LineTable, Address, |
| 569 | NeedsAbsoluteFilePath, |
| 570 | FileName, Line, Column); |
| 571 | } else { |
| 572 | // Otherwise, use call file, call line and call column from |
| 573 | // previous DIE in inlined chain. |
| 574 | getFileNameForCompileUnit(CU, LineTable, CallFile, |
| 575 | NeedsAbsoluteFilePath, FileName); |
| 576 | Line = CallLine; |
| 577 | Column = CallColumn; |
| 578 | } |
| 579 | // Get call file/line/column of a current DIE. |
| 580 | if (i + 1 < n) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 581 | FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine, |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 582 | CallColumn); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | DILineInfo Frame(StringRef(FileName), StringRef(FunctionName), |
| 586 | Line, Column); |
| 587 | InliningInfo.addFrame(Frame); |
| 588 | } |
| 589 | return InliningInfo; |
| 590 | } |
| 591 | |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 592 | static bool consumeCompressedDebugSectionHeader(StringRef &data, |
| 593 | uint64_t &OriginalSize) { |
| 594 | // Consume "ZLIB" prefix. |
| 595 | if (!data.startswith("ZLIB")) |
| 596 | return false; |
| 597 | data = data.substr(4); |
| 598 | // Consume uncompressed section size (big-endian 8 bytes). |
| 599 | DataExtractor extractor(data, false, 8); |
| 600 | uint32_t Offset = 0; |
| 601 | OriginalSize = extractor.getU64(&Offset); |
| 602 | if (Offset == 0) |
| 603 | return false; |
| 604 | data = data.substr(Offset); |
| 605 | return true; |
| 606 | } |
| 607 | |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 608 | DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) : |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 609 | IsLittleEndian(Obj->isLittleEndian()), |
| 610 | AddressSize(Obj->getBytesInAddress()) { |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 611 | for (object::section_iterator i = Obj->section_begin(), |
| 612 | e = Obj->section_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 613 | i != e; ++i) { |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 614 | StringRef name; |
| 615 | i->getName(name); |
| 616 | StringRef data; |
| 617 | i->getContents(data); |
| 618 | |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 619 | name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes. |
Alexey Samsonov | 3d0e3ed | 2013-04-17 14:27:04 +0000 | [diff] [blame] | 620 | |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 621 | // Check if debug info section is compressed with zlib. |
| 622 | if (name.startswith("zdebug_")) { |
| 623 | uint64_t OriginalSize; |
| 624 | if (!zlib::isAvailable() || |
| 625 | !consumeCompressedDebugSectionHeader(data, OriginalSize)) |
| 626 | continue; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 627 | std::unique_ptr<MemoryBuffer> UncompressedSection; |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 628 | if (zlib::uncompress(data, UncompressedSection, OriginalSize) != |
| 629 | zlib::StatusOK) |
| 630 | continue; |
| 631 | // Make data point to uncompressed section contents and save its contents. |
| 632 | name = name.substr(1); |
| 633 | data = UncompressedSection->getBuffer(); |
Alexey Samsonov | 96dc29c | 2014-03-13 08:19:59 +0000 | [diff] [blame^] | 634 | UncompressedSections.push_back(std::move(UncompressedSection)); |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 637 | StringRef *Section = |
| 638 | StringSwitch<StringRef *>(name) |
| 639 | .Case("debug_info", &InfoSection.Data) |
| 640 | .Case("debug_abbrev", &AbbrevSection) |
| 641 | .Case("debug_loc", &LocSection.Data) |
| 642 | .Case("debug_line", &LineSection.Data) |
| 643 | .Case("debug_aranges", &ARangeSection) |
| 644 | .Case("debug_frame", &DebugFrameSection) |
| 645 | .Case("debug_str", &StringSection) |
| 646 | .Case("debug_ranges", &RangeSection) |
| 647 | .Case("debug_pubnames", &PubNamesSection) |
| 648 | .Case("debug_pubtypes", &PubTypesSection) |
| 649 | .Case("debug_gnu_pubnames", &GnuPubNamesSection) |
| 650 | .Case("debug_gnu_pubtypes", &GnuPubTypesSection) |
| 651 | .Case("debug_info.dwo", &InfoDWOSection.Data) |
| 652 | .Case("debug_abbrev.dwo", &AbbrevDWOSection) |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 653 | .Case("debug_line.dwo", &LineDWOSection.Data) |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 654 | .Case("debug_str.dwo", &StringDWOSection) |
| 655 | .Case("debug_str_offsets.dwo", &StringOffsetDWOSection) |
| 656 | .Case("debug_addr", &AddrSection) |
| 657 | // Any more debug info sections go here. |
| 658 | .Default(0); |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 659 | if (Section) { |
| 660 | *Section = data; |
| 661 | if (name == "debug_ranges") { |
| 662 | // FIXME: Use the other dwo range section when we emit it. |
| 663 | RangeDWOSection = data; |
| 664 | } |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 665 | } else if (name == "debug_types") { |
David Blaikie | 427e435 | 2013-09-23 23:39:55 +0000 | [diff] [blame] | 666 | // Find debug_types data by section rather than name as there are |
| 667 | // multiple, comdat grouped, debug_types sections. |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 668 | TypesSections[*i].Data = data; |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 669 | } else if (name == "debug_types.dwo") { |
| 670 | TypesDWOSections[*i].Data = data; |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 671 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 672 | |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 673 | section_iterator RelocatedSection = i->getRelocatedSection(); |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 674 | if (RelocatedSection == Obj->section_end()) |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 675 | continue; |
| 676 | |
| 677 | StringRef RelSecName; |
| 678 | RelocatedSection->getName(RelSecName); |
| 679 | RelSecName = RelSecName.substr( |
| 680 | RelSecName.find_first_not_of("._")); // Skip . and _ prefixes. |
| 681 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 682 | // TODO: Add support for relocations in other sections as needed. |
| 683 | // Record relocations for the debug_info and debug_line sections. |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 684 | RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName) |
David Blaikie | 1b5ee5d | 2013-09-23 17:42:01 +0000 | [diff] [blame] | 685 | .Case("debug_info", &InfoSection.Relocs) |
| 686 | .Case("debug_loc", &LocSection.Relocs) |
| 687 | .Case("debug_info.dwo", &InfoDWOSection.Relocs) |
| 688 | .Case("debug_line", &LineSection.Relocs) |
Alexey Samsonov | 3d0e3ed | 2013-04-17 14:27:04 +0000 | [diff] [blame] | 689 | .Default(0); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 690 | if (!Map) { |
David Blaikie | 427e435 | 2013-09-23 23:39:55 +0000 | [diff] [blame] | 691 | // Find debug_types relocs by section rather than name as there are |
| 692 | // multiple, comdat grouped, debug_types sections. |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 693 | if (RelSecName == "debug_types") |
| 694 | Map = &TypesSections[*RelocatedSection].Relocs; |
| 695 | else if (RelSecName == "debug_types.dwo") |
| 696 | Map = &TypesDWOSections[*RelocatedSection].Relocs; |
| 697 | else |
| 698 | continue; |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 699 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 700 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 701 | if (i->relocation_begin() != i->relocation_end()) { |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 702 | uint64_t SectionSize; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 703 | RelocatedSection->getSize(SectionSize); |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 704 | for (object::relocation_iterator reloc_i = i->relocation_begin(), |
| 705 | reloc_e = i->relocation_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 706 | reloc_i != reloc_e; ++reloc_i) { |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 707 | uint64_t Address; |
Rafael Espindola | 1e48387 | 2013-04-25 12:28:45 +0000 | [diff] [blame] | 708 | reloc_i->getOffset(Address); |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 709 | uint64_t Type; |
| 710 | reloc_i->getType(Type); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 711 | uint64_t SymAddr = 0; |
| 712 | // ELF relocations may need the symbol address |
| 713 | if (Obj->isELF()) { |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 714 | object::symbol_iterator Sym = reloc_i->getSymbol(); |
| 715 | Sym->getAddress(SymAddr); |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 716 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 717 | |
| 718 | object::RelocVisitor V(Obj->getFileFormatName()); |
| 719 | // The section address is always 0 for debug sections. |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 720 | object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr)); |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 721 | if (V.error()) { |
| 722 | SmallString<32> Name; |
| 723 | error_code ec(reloc_i->getTypeName(Name)); |
| 724 | if (ec) { |
| 725 | errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n"; |
| 726 | } |
| 727 | errs() << "error: failed to compute relocation: " |
| 728 | << Name << "\n"; |
| 729 | continue; |
| 730 | } |
| 731 | |
| 732 | if (Address + R.Width > SectionSize) { |
| 733 | errs() << "error: " << R.Width << "-byte relocation starting " |
| 734 | << Address << " bytes into section " << name << " which is " |
| 735 | << SectionSize << " bytes long.\n"; |
| 736 | continue; |
| 737 | } |
| 738 | if (R.Width > 8) { |
| 739 | errs() << "error: can't handle a relocation of more than 8 bytes at " |
| 740 | "a time.\n"; |
| 741 | continue; |
| 742 | } |
| 743 | DEBUG(dbgs() << "Writing " << format("%p", R.Value) |
| 744 | << " at " << format("%p", Address) |
| 745 | << " with width " << format("%d", R.Width) |
| 746 | << "\n"); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 747 | 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] | 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 753 | void DWARFContextInMemory::anchor() { } |