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