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 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 22 | void DWARFContext::dump(raw_ostream &OS) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 23 | OS << ".debug_abbrev contents:\n"; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 24 | getDebugAbbrev()->dump(OS); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 25 | |
| 26 | OS << "\n.debug_info contents:\n"; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 27 | for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) |
| 28 | getCompileUnitAtIndex(i)->dump(OS); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 29 | |
| 30 | OS << "\n.debug_aranges contents:\n"; |
| 31 | DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0); |
| 32 | uint32_t offset = 0; |
| 33 | DWARFDebugArangeSet set; |
| 34 | while (set.extract(arangesData, &offset)) |
| 35 | set.dump(OS); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 36 | |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 37 | uint8_t savedAddressByteSize = 0; |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 38 | OS << "\n.debug_lines contents:\n"; |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 39 | for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) { |
| 40 | DWARFCompileUnit *cu = getCompileUnitAtIndex(i); |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 41 | savedAddressByteSize = cu->getAddressByteSize(); |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 42 | unsigned stmtOffset = |
| 43 | cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list, |
| 44 | -1U); |
| 45 | if (stmtOffset != -1U) { |
| 46 | DataExtractor lineData(getLineSection(), isLittleEndian(), |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 47 | savedAddressByteSize); |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 48 | DWARFDebugLine::DumpingState state(OS); |
| 49 | DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state); |
| 50 | } |
| 51 | } |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 52 | |
| 53 | OS << "\n.debug_str contents:\n"; |
| 54 | DataExtractor strData(getStringSection(), isLittleEndian(), 0); |
| 55 | offset = 0; |
| 56 | uint32_t lastOffset = 0; |
| 57 | while (const char *s = strData.getCStr(&offset)) { |
| 58 | OS << format("0x%8.8x: \"%s\"\n", lastOffset, s); |
| 59 | lastOffset = offset; |
| 60 | } |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 61 | |
| 62 | OS << "\n.debug_ranges contents:\n"; |
| 63 | // In fact, different compile units may have different address byte |
| 64 | // sizes, but for simplicity we just use the address byte size of the last |
| 65 | // compile unit (there is no easy and fast way to associate address range |
| 66 | // list and the compile unit it describes). |
| 67 | DataExtractor rangesData(getRangeSection(), isLittleEndian(), |
| 68 | savedAddressByteSize); |
| 69 | offset = 0; |
| 70 | DWARFDebugRangeList rangeList; |
| 71 | while (rangeList.extract(rangesData, &offset)) |
| 72 | rangeList.dump(OS); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { |
| 76 | if (Abbrev) |
| 77 | return Abbrev.get(); |
| 78 | |
| 79 | DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0); |
| 80 | |
| 81 | Abbrev.reset(new DWARFDebugAbbrev()); |
| 82 | Abbrev->parse(abbrData); |
| 83 | return Abbrev.get(); |
| 84 | } |
| 85 | |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 86 | const DWARFDebugAranges *DWARFContext::getDebugAranges() { |
| 87 | if (Aranges) |
| 88 | return Aranges.get(); |
| 89 | |
| 90 | DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0); |
| 91 | |
| 92 | Aranges.reset(new DWARFDebugAranges()); |
| 93 | Aranges->extract(arangesData); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 94 | if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs. |
| 95 | Aranges->generate(this); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 96 | return Aranges.get(); |
| 97 | } |
| 98 | |
Eric Christopher | e9403c1 | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 99 | const DWARFLineTable * |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 100 | DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) { |
| 101 | if (!Line) |
| 102 | Line.reset(new DWARFDebugLine()); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 103 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 104 | unsigned stmtOffset = |
| 105 | cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list, |
| 106 | -1U); |
| 107 | if (stmtOffset == -1U) |
| 108 | return 0; // No line table for this compile unit. |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 109 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 110 | // See if the line table is cached. |
Eric Christopher | e9403c1 | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 111 | if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 112 | return lt; |
| 113 | |
| 114 | // We have to parse it first. |
| 115 | DataExtractor lineData(getLineSection(), isLittleEndian(), |
| 116 | cu->getAddressByteSize()); |
| 117 | return Line->getOrParseLineTable(lineData, stmtOffset); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 120 | void DWARFContext::parseCompileUnits() { |
| 121 | uint32_t offset = 0; |
Eric Christopher | b69b55f | 2012-10-16 23:46:23 +0000 | [diff] [blame] | 122 | const DataExtractor &DIData = DataExtractor(getInfoSection(), |
| 123 | isLittleEndian(), 0); |
| 124 | while (DIData.isValidOffset(offset)) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 125 | CUs.push_back(DWARFCompileUnit(*this)); |
Eric Christopher | b69b55f | 2012-10-16 23:46:23 +0000 | [diff] [blame] | 126 | if (!CUs.back().extract(DIData, &offset)) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 127 | CUs.pop_back(); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | offset = CUs.back().getNextCompileUnitOffset(); |
| 132 | } |
| 133 | } |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 134 | |
| 135 | namespace { |
| 136 | struct OffsetComparator { |
| 137 | bool operator()(const DWARFCompileUnit &LHS, |
| 138 | const DWARFCompileUnit &RHS) const { |
| 139 | return LHS.getOffset() < RHS.getOffset(); |
| 140 | } |
| 141 | bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const { |
| 142 | return LHS.getOffset() < RHS; |
| 143 | } |
| 144 | bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const { |
| 145 | return LHS < RHS.getOffset(); |
| 146 | } |
| 147 | }; |
| 148 | } |
| 149 | |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 150 | DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) { |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 151 | if (CUs.empty()) |
| 152 | parseCompileUnits(); |
| 153 | |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 154 | DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset, |
| 155 | OffsetComparator()); |
| 156 | if (CU != CUs.end()) |
| 157 | return &*CU; |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 161 | DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { |
Benjamin Kramer | 9013db3 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 162 | // First, get the offset of the compile unit. |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 163 | uint32_t CUOffset = getDebugAranges()->findAddress(Address); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 164 | // Retrieve the compile unit. |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 165 | return getCompileUnitForOffset(CUOffset); |
| 166 | } |
| 167 | |
Eric Christopher | e9403c1 | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 168 | static bool getFileNameForCompileUnit(DWARFCompileUnit *CU, |
| 169 | const DWARFLineTable *LineTable, |
| 170 | uint64_t FileIndex, |
| 171 | bool NeedsAbsoluteFilePath, |
| 172 | std::string &FileName) { |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 173 | if (CU == 0 || |
| 174 | LineTable == 0 || |
| 175 | !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath, |
| 176 | FileName)) |
| 177 | return false; |
| 178 | if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) { |
| 179 | // We may still need to append compilation directory of compile unit. |
| 180 | SmallString<16> AbsolutePath; |
| 181 | if (const char *CompilationDir = CU->getCompilationDir()) { |
| 182 | sys::path::append(AbsolutePath, CompilationDir); |
| 183 | } |
| 184 | sys::path::append(AbsolutePath, FileName); |
| 185 | FileName = AbsolutePath.str(); |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
Eric Christopher | e9403c1 | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 190 | static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU, |
| 191 | const DWARFLineTable *LineTable, |
| 192 | uint64_t Address, |
| 193 | bool NeedsAbsoluteFilePath, |
| 194 | std::string &FileName, |
| 195 | uint32_t &Line, uint32_t &Column) { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 196 | if (CU == 0 || LineTable == 0) |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 197 | return false; |
| 198 | // Get the index of row we're looking for in the line table. |
| 199 | uint32_t RowIndex = LineTable->lookupAddress(Address); |
| 200 | if (RowIndex == -1U) |
| 201 | return false; |
| 202 | // Take file number and line/column from the row. |
| 203 | const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; |
| 204 | if (!getFileNameForCompileUnit(CU, LineTable, Row.File, |
| 205 | NeedsAbsoluteFilePath, FileName)) |
| 206 | return false; |
| 207 | Line = Row.Line; |
| 208 | Column = Row.Column; |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address, |
| 213 | DILineInfoSpecifier Specifier) { |
| 214 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 215 | if (!CU) |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 216 | return DILineInfo(); |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 217 | std::string FileName = "<invalid>"; |
| 218 | std::string FunctionName = "<invalid>"; |
| 219 | uint32_t Line = 0; |
| 220 | uint32_t Column = 0; |
| 221 | if (Specifier.needs(DILineInfoSpecifier::FunctionName)) { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 222 | // The address may correspond to instruction in some inlined function, |
| 223 | // so we have to build the chain of inlined functions and take the |
| 224 | // name of the topmost function in it. |
| 225 | const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain = |
| 226 | CU->getInlinedChainForAddress(Address); |
| 227 | if (InlinedChain.size() > 0) { |
| 228 | const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0]; |
| 229 | if (const char *Name = TopFunctionDIE.getSubroutineName(CU)) |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 230 | FunctionName = Name; |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 231 | } |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 232 | } |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 233 | if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) { |
Eric Christopher | e9403c1 | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 234 | const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU); |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 235 | const bool NeedsAbsoluteFilePath = |
| 236 | Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath); |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 237 | getFileLineInfoForCompileUnit(CU, LineTable, Address, |
| 238 | NeedsAbsoluteFilePath, |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 239 | FileName, Line, Column); |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 240 | } |
Alexey Samsonov | 38a6381 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 241 | return DILineInfo(StringRef(FileName), StringRef(FunctionName), |
| 242 | Line, Column); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 243 | } |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 244 | |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 245 | DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address, |
| 246 | DILineInfoSpecifier Specifier) { |
| 247 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 248 | if (!CU) |
| 249 | return DIInliningInfo(); |
| 250 | |
| 251 | const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain = |
| 252 | CU->getInlinedChainForAddress(Address); |
| 253 | if (InlinedChain.size() == 0) |
| 254 | return DIInliningInfo(); |
| 255 | |
| 256 | DIInliningInfo InliningInfo; |
| 257 | uint32_t CallFile = 0, CallLine = 0, CallColumn = 0; |
Eric Christopher | e9403c1 | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 258 | const DWARFLineTable *LineTable = 0; |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 259 | for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) { |
| 260 | const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i]; |
| 261 | std::string FileName = "<invalid>"; |
| 262 | std::string FunctionName = "<invalid>"; |
| 263 | uint32_t Line = 0; |
| 264 | uint32_t Column = 0; |
| 265 | // Get function name if necessary. |
| 266 | if (Specifier.needs(DILineInfoSpecifier::FunctionName)) { |
| 267 | if (const char *Name = FunctionDIE.getSubroutineName(CU)) |
| 268 | FunctionName = Name; |
| 269 | } |
| 270 | if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) { |
| 271 | const bool NeedsAbsoluteFilePath = |
| 272 | Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath); |
| 273 | if (i == 0) { |
| 274 | // For the topmost frame, initialize the line table of this |
| 275 | // compile unit and fetch file/line info from it. |
| 276 | LineTable = getLineTableForCompileUnit(CU); |
| 277 | // For the topmost routine, get file/line info from line table. |
| 278 | getFileLineInfoForCompileUnit(CU, LineTable, Address, |
| 279 | NeedsAbsoluteFilePath, |
| 280 | FileName, Line, Column); |
| 281 | } else { |
| 282 | // Otherwise, use call file, call line and call column from |
| 283 | // previous DIE in inlined chain. |
| 284 | getFileNameForCompileUnit(CU, LineTable, CallFile, |
| 285 | NeedsAbsoluteFilePath, FileName); |
| 286 | Line = CallLine; |
| 287 | Column = CallColumn; |
| 288 | } |
| 289 | // Get call file/line/column of a current DIE. |
| 290 | if (i + 1 < n) { |
| 291 | FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn); |
| 292 | } |
| 293 | } |
| 294 | DILineInfo Frame(StringRef(FileName), StringRef(FunctionName), |
| 295 | Line, Column); |
| 296 | InliningInfo.addFrame(Frame); |
| 297 | } |
| 298 | return InliningInfo; |
| 299 | } |
| 300 | |
Eric Christopher | d1726a4 | 2012-11-12 21:40:38 +0000 | [diff] [blame^] | 301 | DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) : |
| 302 | IsLittleEndian(true /* FIXME */) { |
| 303 | error_code ec; |
| 304 | for (object::section_iterator i = Obj->begin_sections(), |
| 305 | e = Obj->end_sections(); |
| 306 | i != e; i.increment(ec)) { |
| 307 | StringRef name; |
| 308 | i->getName(name); |
| 309 | StringRef data; |
| 310 | i->getContents(data); |
| 311 | |
| 312 | if (name.startswith("__DWARF,")) |
| 313 | name = name.substr(8); // Skip "__DWARF," prefix. |
| 314 | name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes. |
| 315 | if (name == "debug_info") |
| 316 | InfoSection = data; |
| 317 | else if (name == "debug_abbrev") |
| 318 | AbbrevSection = data; |
| 319 | else if (name == "debug_line") |
| 320 | LineSection = data; |
| 321 | else if (name == "debug_aranges") |
| 322 | ARangeSection = data; |
| 323 | else if (name == "debug_str") |
| 324 | StringSection = data; |
| 325 | else if (name == "debug_ranges") |
| 326 | RangeSection = data; |
| 327 | // Any more debug info sections go here. |
| 328 | else |
| 329 | continue; |
| 330 | |
| 331 | // TODO: For now only handle relocations for the debug_info section. |
| 332 | if (name != "debug_info") |
| 333 | continue; |
| 334 | |
| 335 | if (i->begin_relocations() != i->end_relocations()) { |
| 336 | uint64_t SectionSize; |
| 337 | i->getSize(SectionSize); |
| 338 | for (object::relocation_iterator reloc_i = i->begin_relocations(), |
| 339 | reloc_e = i->end_relocations(); |
| 340 | reloc_i != reloc_e; reloc_i.increment(ec)) { |
| 341 | uint64_t Address; |
| 342 | reloc_i->getAddress(Address); |
| 343 | uint64_t Type; |
| 344 | reloc_i->getType(Type); |
| 345 | |
| 346 | object::RelocVisitor V(Obj->getFileFormatName()); |
| 347 | // The section address is always 0 for debug sections. |
| 348 | object::RelocToApply R(V.visit(Type, *reloc_i)); |
| 349 | if (V.error()) { |
| 350 | SmallString<32> Name; |
| 351 | error_code ec(reloc_i->getTypeName(Name)); |
| 352 | if (ec) { |
| 353 | errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n"; |
| 354 | } |
| 355 | errs() << "error: failed to compute relocation: " |
| 356 | << Name << "\n"; |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | if (Address + R.Width > SectionSize) { |
| 361 | errs() << "error: " << R.Width << "-byte relocation starting " |
| 362 | << Address << " bytes into section " << name << " which is " |
| 363 | << SectionSize << " bytes long.\n"; |
| 364 | continue; |
| 365 | } |
| 366 | if (R.Width > 8) { |
| 367 | errs() << "error: can't handle a relocation of more than 8 bytes at " |
| 368 | "a time.\n"; |
| 369 | continue; |
| 370 | } |
| 371 | DEBUG(dbgs() << "Writing " << format("%p", R.Value) |
| 372 | << " at " << format("%p", Address) |
| 373 | << " with width " << format("%d", R.Width) |
| 374 | << "\n"); |
| 375 | RelocMap[Address] = std::make_pair(R.Width, R.Value); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 381 | void DWARFContextInMemory::anchor() { } |