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