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