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