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