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