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