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