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