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