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