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