blob: 59a060d143ff25fc9ebb6a1aa9068022e63c4417 [file] [log] [blame]
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001//===- DWARFContext.cpp ---------------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
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
Greg Claytonb8c162b2017-05-03 16:02:29 +000010#include "llvm/DebugInfo/DWARF/DWARFContext.h"
11#include "llvm/ADT/STLExtras.h"
Alexey Samsonove16e16a2012-07-19 07:03:58 +000012#include "llvm/ADT/SmallString.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000013#include "llvm/ADT/SmallVector.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000014#include "llvm/ADT/StringRef.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000015#include "llvm/ADT/StringSwitch.h"
Zachary Turner82af9432015-01-30 18:07:45 +000016#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000017#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000018#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
Zachary Turner82af9432015-01-30 18:07:45 +000019#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000020#include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000021#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
22#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
23#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
24#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
George Rimare71e33f2016-12-17 09:10:32 +000025#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000026#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
27#include "llvm/DebugInfo/DWARF/DWARFDie.h"
28#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
29#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
30#include "llvm/DebugInfo/DWARF/DWARFSection.h"
David Blaikie65a8efe2015-11-11 19:28:21 +000031#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000032#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
George Rimar4bf30832017-01-11 15:26:41 +000033#include "llvm/Object/Decompressor.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000034#include "llvm/Object/MachO.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000035#include "llvm/Object/ObjectFile.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000036#include "llvm/Object/RelocVisitor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000037#include "llvm/Support/Casting.h"
38#include "llvm/Support/DataExtractor.h"
39#include "llvm/Support/Debug.h"
40#include "llvm/Support/Error.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000041#include "llvm/Support/Format.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000042#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000043#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000044#include <algorithm>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000045#include <cstdint>
Greg Claytonc7695a82017-05-02 20:28:33 +000046#include <map>
47#include <set>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000048#include <string>
49#include <utility>
50#include <vector>
51
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000052using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000053using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000054using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000055
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "dwarf"
57
Eric Christopher494109b2012-10-16 23:46:25 +000058typedef DWARFDebugLine::LineTable DWARFLineTable;
Alexey Samsonovdce67342014-05-15 21:24:32 +000059typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
60typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000061
George Rimarf8a96422017-04-21 09:12:18 +000062uint64_t llvm::getRelocatedValue(const DataExtractor &Data, uint32_t Size,
63 uint32_t *Off, const RelocAddrMap *Relocs) {
64 if (!Relocs)
65 return Data.getUnsigned(Off, Size);
66 RelocAddrMap::const_iterator AI = Relocs->find(*Off);
67 if (AI == Relocs->end())
68 return Data.getUnsigned(Off, Size);
George Rimar41e65672017-05-16 14:05:45 +000069 return Data.getUnsigned(Off, Size) + AI->second.Value;
George Rimarf8a96422017-04-21 09:12:18 +000070}
71
Frederic Riss7c500472014-11-14 19:30:08 +000072static void dumpAccelSection(raw_ostream &OS, StringRef Name,
73 const DWARFSection& Section, StringRef StringSection,
74 bool LittleEndian) {
75 DataExtractor AccelSection(Section.Data, LittleEndian, 0);
Frederic Risse837ec22014-11-14 16:15:53 +000076 DataExtractor StrData(StringSection, LittleEndian, 0);
77 OS << "\n." << Name << " contents:\n";
Frederic Riss7c500472014-11-14 19:30:08 +000078 DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
Frederic Risse837ec22014-11-14 16:15:53 +000079 if (!Accel.extract())
80 return;
81 Accel.dump(OS);
82}
83
David Blaikie50cc27e2016-10-18 21:09:48 +000084void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH,
85 bool SummarizeTypes) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +000086 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
87 OS << ".debug_abbrev contents:\n";
88 getDebugAbbrev()->dump(OS);
89 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000090
David Blaikie66865d62014-01-09 00:13:35 +000091 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
92 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +000093 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +000094 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +000095 }
David Blaikie622dce42014-01-08 23:29:59 +000096
Eli Bendersky7a94daa2013-01-25 20:26:43 +000097 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
98 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000099 for (const auto &CU : compile_units())
100 CU->dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000101 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000102
David Blaikie66865d62014-01-09 00:13:35 +0000103 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
104 getNumDWOCompileUnits()) {
105 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000106 for (const auto &DWOCU : dwo_compile_units())
107 DWOCU->dump(OS);
David Blaikie66865d62014-01-09 00:13:35 +0000108 }
David Blaikie622dce42014-01-08 23:29:59 +0000109
David Blaikie66865d62014-01-09 00:13:35 +0000110 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000111 OS << "\n.debug_types contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000112 for (const auto &TUS : type_unit_sections())
113 for (const auto &TU : TUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000114 TU->dump(OS, SummarizeTypes);
David Blaikie03c089c2013-09-23 22:44:47 +0000115 }
116
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000117 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
118 getNumDWOTypeUnits()) {
119 OS << "\n.debug_types.dwo contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000120 for (const auto &DWOTUS : dwo_type_unit_sections())
121 for (const auto &DWOTU : DWOTUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000122 DWOTU->dump(OS, SummarizeTypes);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000123 }
David Blaikie92d9d622014-01-09 05:08:24 +0000124
David Blaikie18e73502013-06-19 21:37:13 +0000125 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +0000126 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +0000127 getDebugLoc()->dump(OS);
128 }
129
David Blaikie9c550ac2014-03-25 01:44:02 +0000130 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
131 OS << "\n.debug_loc.dwo contents:\n";
132 getDebugLocDWO()->dump(OS);
133 }
134
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000135 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
136 OS << "\n.debug_frame contents:\n";
137 getDebugFrame()->dump(OS);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000138 if (DumpEH) {
139 OS << "\n.eh_frame contents:\n";
140 getEHFrame()->dump(OS);
141 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000142 }
143
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000144 if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
145 OS << "\n.debug_macinfo contents:\n";
146 getDebugMacro()->dump(OS);
147 }
148
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000149 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000150 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
151 OS << "\n.debug_aranges contents:\n";
152 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
153 DWARFDebugArangeSet set;
154 while (set.extract(arangesData, &offset))
155 set.dump(OS);
156 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000157
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000158 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000159 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
160 OS << "\n.debug_line contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000161 for (const auto &CU : compile_units()) {
162 savedAddressByteSize = CU->getAddressByteSize();
Greg Claytonc8c10322016-12-13 18:25:19 +0000163 auto CUDIE = CU->getUnitDIE();
164 if (!CUDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000165 continue;
Greg Clayton97d22182017-01-13 21:08:18 +0000166 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000167 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000168 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000169 DWARFDebugLine::LineTable LineTable;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000170 uint32_t Offset = *StmtOffset;
171 LineTable.parse(lineData, &getLineSection().Relocs, &Offset);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000172 LineTable.dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000173 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000174 }
175 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000176
David Blaikie65a8efe2015-11-11 19:28:21 +0000177 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
178 OS << "\n.debug_cu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000179 getCUIndex().dump(OS);
David Blaikie65a8efe2015-11-11 19:28:21 +0000180 }
181
David Blaikie51c40282015-11-11 19:40:49 +0000182 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
183 OS << "\n.debug_tu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000184 getTUIndex().dump(OS);
David Blaikie51c40282015-11-11 19:40:49 +0000185 }
186
David Blaikie1d4736e2014-02-24 23:58:54 +0000187 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
188 OS << "\n.debug_line.dwo contents:\n";
189 unsigned stmtOffset = 0;
190 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
191 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000192 DWARFDebugLine::LineTable LineTable;
193 while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
194 LineTable.dump(OS);
195 LineTable.clear();
196 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000197 }
198
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000199 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
200 OS << "\n.debug_str contents:\n";
201 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
202 offset = 0;
203 uint32_t strOffset = 0;
204 while (const char *s = strData.getCStr(&offset)) {
205 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
206 strOffset = offset;
207 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000208 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000209
David Blaikie66865d62014-01-09 00:13:35 +0000210 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
211 !getStringDWOSection().empty()) {
212 OS << "\n.debug_str.dwo contents:\n";
213 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
214 offset = 0;
215 uint32_t strDWOOffset = 0;
216 while (const char *s = strDWOData.getCStr(&offset)) {
217 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
218 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000219 }
David Blaikie66865d62014-01-09 00:13:35 +0000220 }
David Blaikie622dce42014-01-08 23:29:59 +0000221
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000222 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
223 OS << "\n.debug_ranges contents:\n";
224 // In fact, different compile units may have different address byte
225 // sizes, but for simplicity we just use the address byte size of the last
226 // compile unit (there is no easy and fast way to associate address range
227 // list and the compile unit it describes).
George Rimarca532112017-04-24 10:19:45 +0000228 DataExtractor rangesData(getRangeSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000229 savedAddressByteSize);
230 offset = 0;
231 DWARFDebugRangeList rangeList;
George Rimarca532112017-04-24 10:19:45 +0000232 while (rangeList.extract(rangesData, &offset, getRangeSection().Relocs))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000233 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000234 }
Eric Christopher962c9082013-01-15 23:56:56 +0000235
Eric Christopher0de53592013-09-25 23:02:36 +0000236 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000237 DWARFDebugPubTable(getPubNamesSection(), isLittleEndian(), false)
238 .dump("debug_pubnames", OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000239
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000240 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000241 DWARFDebugPubTable(getPubTypesSection(), isLittleEndian(), false)
242 .dump("debug_pubtypes", OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000243
David Blaikieecd21ff2013-09-24 19:50:00 +0000244 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000245 DWARFDebugPubTable(getGnuPubNamesSection(), isLittleEndian(),
246 true /* GnuStyle */)
247 .dump("debug_gnu_pubnames", OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000248
249 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000250 DWARFDebugPubTable(getGnuPubTypesSection(), isLittleEndian(),
251 true /* GnuStyle */)
252 .dump("debug_gnu_pubtypes", OS);
David Blaikie404d3042013-09-19 23:01:29 +0000253
David Blaikie66865d62014-01-09 00:13:35 +0000254 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
255 !getStringOffsetDWOSection().empty()) {
256 OS << "\n.debug_str_offsets.dwo contents:\n";
257 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
258 0);
259 offset = 0;
260 uint64_t size = getStringOffsetDWOSection().size();
261 while (offset < size) {
262 OS << format("0x%8.8x: ", offset);
263 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000264 }
David Blaikie66865d62014-01-09 00:13:35 +0000265 }
Frederic Risse837ec22014-11-14 16:15:53 +0000266
George Rimar4f82df52016-09-23 11:01:53 +0000267 if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) &&
268 !getGdbIndexSection().empty()) {
269 OS << "\n.gnu_index contents:\n";
270 getGdbIndex().dump(OS);
271 }
272
Frederic Risse837ec22014-11-14 16:15:53 +0000273 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
274 dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
275 getStringSection(), isLittleEndian());
276
277 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
278 dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
279 getStringSection(), isLittleEndian());
280
281 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
282 dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
283 getStringSection(), isLittleEndian());
284
285 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
286 dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
287 getStringSection(), isLittleEndian());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000288}
289
Greg Claytonc7695a82017-05-02 20:28:33 +0000290DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
291 parseCompileUnits();
292 if (auto *CU = CUs.getUnitForOffset(Offset))
293 return CU->getDIEForOffset(Offset);
294 return DWARFDie();
295}
296
297namespace {
298
299class Verifier {
300 raw_ostream &OS;
301 DWARFContext &DCtx;
302public:
303 Verifier(raw_ostream &S, DWARFContext &D) : OS(S), DCtx(D) {}
304
305 bool HandleDebugInfo() {
306 bool Success = true;
307 // A map that tracks all references (converted absolute references) so we
308 // can verify each reference points to a valid DIE and not an offset that
309 // lies between to valid DIEs.
310 std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
311
Greg Clayton48432cf2017-05-01 22:07:02 +0000312 OS << "Verifying .debug_info...\n";
Greg Claytonc7695a82017-05-02 20:28:33 +0000313 for (const auto &CU : DCtx.compile_units()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000314 unsigned NumDies = CU->getNumDIEs();
315 for (unsigned I = 0; I < NumDies; ++I) {
316 auto Die = CU->getDIEAtIndex(I);
317 const auto Tag = Die.getTag();
318 if (Tag == DW_TAG_null)
319 continue;
320 for (auto AttrValue : Die.attributes()) {
321 const auto Attr = AttrValue.Attr;
322 const auto Form = AttrValue.Value.getForm();
323 switch (Attr) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000324 case DW_AT_ranges:
325 // Make sure the offset in the DW_AT_ranges attribute is valid.
326 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
327 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
328 Success = false;
329 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
330 "bounds:\n";
331 Die.dump(OS, 0);
332 OS << "\n";
333 }
334 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000335 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000336 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000337 Die.dump(OS, 0);
338 OS << "\n";
339 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000340 break;
341 case DW_AT_stmt_list:
342 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
343 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
344 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
345 Success = false;
346 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
347 "bounds: "
348 << format("0x%08" PRIx32, *SectionOffset) << "\n";
349 CU->getUnitDIE().dump(OS, 0);
350 OS << "\n";
351 }
352 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000353 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000354 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
355 Die.dump(OS, 0);
Greg Clayton48432cf2017-05-01 22:07:02 +0000356 OS << "\n";
357 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000358 break;
359
360 default:
361 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000362 }
363 switch (Form) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000364 case DW_FORM_ref1:
365 case DW_FORM_ref2:
366 case DW_FORM_ref4:
367 case DW_FORM_ref8:
368 case DW_FORM_ref_udata: {
369 // Verify all CU relative references are valid CU offsets.
370 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
371 assert(RefVal);
372 if (RefVal) {
373 auto DieCU = Die.getDwarfUnit();
374 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
375 auto CUOffset = AttrValue.Value.getRawUValue();
376 if (CUOffset >= CUSize) {
377 Success = false;
378 OS << "error: " << FormEncodingString(Form) << " CU offset "
379 << format("0x%08" PRIx32, CUOffset)
380 << " is invalid (must be less than CU size of "
381 << format("0x%08" PRIx32, CUSize) << "):\n";
382 Die.dump(OS, 0);
383 OS << "\n";
384 } else {
385 // Valid reference, but we will verify it points to an actual
386 // DIE later.
387 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
388 }
389 }
390 break;
391 }
392 case DW_FORM_ref_addr: {
393 // Verify all absolute DIE references have valid offsets in the
394 // .debug_info section.
395 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
396 assert(RefVal);
397 if (RefVal) {
398 if(*RefVal >= DCtx.getInfoSection().Data.size()) {
399 Success = false;
400 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
401 "bounds:\n";
402 Die.dump(OS, 0);
403 OS << "\n";
404 } else {
405 // Valid reference, but we will verify it points to an actual
406 // DIE later.
407 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
408 }
409 }
410 break;
411 }
412 case DW_FORM_strp: {
413 auto SecOffset = AttrValue.Value.getAsSectionOffset();
414 assert(SecOffset); // DW_FORM_strp is a section offset.
415 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000416 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000417 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000418 Die.dump(OS, 0);
419 OS << "\n";
420 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000421 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000422 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000423 default:
424 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000425 }
426 }
427 }
428 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000429
430 // Take all references and make sure they point to an actual DIE by
431 // getting the DIE by offset and emitting an error
432 OS << "Verifying .debug_info references...\n";
433 for (auto Pair: ReferenceToDIEOffsets) {
434 auto Die = DCtx.getDIEForOffset(Pair.first);
435 if (Die)
436 continue;
437 Success = false;
438 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
439 << ". Offset is in between DIEs:\n";
440 for (auto Offset: Pair.second) {
441 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
442 ReferencingDie.dump(OS, 0);
443 OS << "\n";
444 }
445 OS << "\n";
446 }
447 return Success;
448 }
Greg Clayton67070462017-05-02 22:48:52 +0000449
450 bool HandleDebugLine() {
Greg Clayton8df55b42017-05-03 15:45:31 +0000451 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Clayton67070462017-05-02 22:48:52 +0000452 bool Success = true;
453 OS << "Verifying .debug_line...\n";
454 for (const auto &CU : DCtx.compile_units()) {
455 uint32_t LineTableOffset = 0;
Greg Clayton8df55b42017-05-03 15:45:31 +0000456 auto CUDie = CU->getUnitDIE();
457 auto StmtFormValue = CUDie.find(DW_AT_stmt_list);
Greg Clayton67070462017-05-02 22:48:52 +0000458 if (!StmtFormValue) {
459 // No line table for this compile unit.
460 continue;
461 }
462 // Get the attribute value as a section offset. No need to produce an
463 // error here if the encoding isn't correct because we validate this in
464 // the .debug_info verifier.
465 if (auto StmtSectionOffset = toSectionOffset(StmtFormValue)) {
466 LineTableOffset = *StmtSectionOffset;
467 if (LineTableOffset >= DCtx.getLineSection().Data.size()) {
468 // Make sure we don't get a valid line table back if the offset
469 // is wrong.
470 assert(DCtx.getLineTableForUnit(CU.get()) == nullptr);
471 // Skip this line table as it isn't valid. No need to create an error
472 // here because we validate this in the .debug_info verifier.
473 continue;
Greg Clayton8df55b42017-05-03 15:45:31 +0000474 } else {
475 auto Iter = StmtListToDie.find(LineTableOffset);
476 if (Iter != StmtListToDie.end()) {
477 Success = false;
478 OS << "error: two compile unit DIEs, "
479 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
480 << format("0x%08" PRIx32, CUDie.getOffset())
481 << ", have the same DW_AT_stmt_list section offset:\n";
482 Iter->second.dump(OS, 0);
483 CUDie.dump(OS, 0);
484 OS << '\n';
485 // Already verified this line table before, no need to do it again.
486 continue;
487 }
488 StmtListToDie[LineTableOffset] = CUDie;
Greg Clayton67070462017-05-02 22:48:52 +0000489 }
490 }
491 auto LineTable = DCtx.getLineTableForUnit(CU.get());
492 if (!LineTable) {
493 Success = false;
494 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
495 << "] was not able to be parsed for CU:\n";
Greg Clayton8df55b42017-05-03 15:45:31 +0000496 CUDie.dump(OS, 0);
Greg Clayton67070462017-05-02 22:48:52 +0000497 OS << '\n';
498 continue;
499 }
500 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
501 uint64_t PrevAddress = 0;
502 uint32_t RowIndex = 0;
503 for (const auto &Row : LineTable->Rows) {
504 if (Row.Address < PrevAddress) {
505 Success = false;
506 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
507 << "] row[" << RowIndex
508 << "] decreases in address from previous row:\n";
509
510 DWARFDebugLine::Row::dumpTableHeader(OS);
511 if (RowIndex > 0)
512 LineTable->Rows[RowIndex - 1].dump(OS);
513 Row.dump(OS);
514 OS << '\n';
515 }
516
517 if (Row.File > MaxFileIndex) {
518 Success = false;
519 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
520 << "][" << RowIndex << "] has invalid file index " << Row.File
521 << " (valid values are [1," << MaxFileIndex << "]):\n";
522 DWARFDebugLine::Row::dumpTableHeader(OS);
523 Row.dump(OS);
524 OS << '\n';
525 }
526 if (Row.EndSequence)
527 PrevAddress = 0;
528 else
529 PrevAddress = Row.Address;
530 ++RowIndex;
531 }
532 }
533 return Success;
534 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000535};
536
537} // anonymous namespace
538
539bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
540 bool Success = true;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000541 DWARFVerifier verifier(OS, *this);
Greg Claytonc7695a82017-05-02 20:28:33 +0000542 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000543 if (!verifier.handleDebugInfo())
Greg Claytonc7695a82017-05-02 20:28:33 +0000544 Success = false;
Greg Clayton48432cf2017-05-01 22:07:02 +0000545 }
Greg Clayton67070462017-05-02 22:48:52 +0000546 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000547 if (!verifier.handleDebugLine())
Greg Clayton67070462017-05-02 22:48:52 +0000548 Success = false;
549 }
Greg Clayton48432cf2017-05-01 22:07:02 +0000550 return Success;
551}
David Blaikie82641be2015-11-17 00:39:55 +0000552const DWARFUnitIndex &DWARFContext::getCUIndex() {
553 if (CUIndex)
554 return *CUIndex;
555
556 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
557
David Blaikieb073cb92015-12-02 06:21:34 +0000558 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000559 CUIndex->parse(CUIndexData);
560 return *CUIndex;
561}
562
563const DWARFUnitIndex &DWARFContext::getTUIndex() {
564 if (TUIndex)
565 return *TUIndex;
566
567 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
568
David Blaikieb073cb92015-12-02 06:21:34 +0000569 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000570 TUIndex->parse(TUIndexData);
571 return *TUIndex;
572}
573
George Rimar4f82df52016-09-23 11:01:53 +0000574DWARFGdbIndex &DWARFContext::getGdbIndex() {
575 if (GdbIndex)
576 return *GdbIndex;
577
578 DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0);
579 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
580 GdbIndex->parse(GdbIndexData);
581 return *GdbIndex;
582}
583
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000584const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
585 if (Abbrev)
586 return Abbrev.get();
587
588 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
589
590 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000591 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000592 return Abbrev.get();
593}
594
Eric Christopherda4b2192013-01-02 23:52:13 +0000595const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
596 if (AbbrevDWO)
597 return AbbrevDWO.get();
598
599 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
600 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000601 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000602 return AbbrevDWO.get();
603}
604
David Blaikie18e73502013-06-19 21:37:13 +0000605const DWARFDebugLoc *DWARFContext::getDebugLoc() {
606 if (Loc)
607 return Loc.get();
608
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000609 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
610 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000611 // assume all compile units have the same address byte size
612 if (getNumCompileUnits())
613 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
614 return Loc.get();
615}
616
David Blaikie9c550ac2014-03-25 01:44:02 +0000617const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
618 if (LocDWO)
619 return LocDWO.get();
620
621 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
622 LocDWO.reset(new DWARFDebugLocDWO());
623 LocDWO->parse(LocData);
624 return LocDWO.get();
625}
626
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000627const DWARFDebugAranges *DWARFContext::getDebugAranges() {
628 if (Aranges)
629 return Aranges.get();
630
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000631 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000632 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000633 return Aranges.get();
634}
635
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000636const DWARFDebugFrame *DWARFContext::getDebugFrame() {
637 if (DebugFrame)
638 return DebugFrame.get();
639
640 // There's a "bug" in the DWARFv3 standard with respect to the target address
641 // size within debug frame sections. While DWARF is supposed to be independent
642 // of its container, FDEs have fields with size being "target address size",
643 // which isn't specified in DWARF in general. It's only specified for CUs, but
644 // .eh_frame can appear without a .debug_info section. Follow the example of
645 // other tools (libdwarf) and extract this from the container (ObjectFile
646 // provides this information). This problem is fixed in DWARFv4
647 // See this dwarf-discuss discussion for more details:
648 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
649 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
650 getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000651 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
652 DebugFrame->parse(debugFrameData);
653 return DebugFrame.get();
654}
655
656const DWARFDebugFrame *DWARFContext::getEHFrame() {
657 if (EHFrame)
658 return EHFrame.get();
659
660 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
661 getAddressSize());
662 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000663 DebugFrame->parse(debugFrameData);
664 return DebugFrame.get();
665}
666
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000667const DWARFDebugMacro *DWARFContext::getDebugMacro() {
668 if (Macro)
669 return Macro.get();
670
671 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
672 Macro.reset(new DWARFDebugMacro());
673 Macro->parse(MacinfoData);
674 return Macro.get();
675}
676
Eric Christopher494109b2012-10-16 23:46:25 +0000677const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000678DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000679 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000680 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
David Blaikiec4e2bed2015-11-17 21:08:05 +0000681
Greg Claytonc8c10322016-12-13 18:25:19 +0000682 auto UnitDIE = U->getUnitDIE();
683 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000684 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000685
Greg Clayton97d22182017-01-13 21:08:18 +0000686 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000687 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000688 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000689
Greg Clayton52fe1f62016-12-14 22:38:08 +0000690 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000691 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000692 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000693 return lt;
694
Greg Clayton48ff66a2017-05-04 18:29:44 +0000695 // Make sure the offset is good before we try to parse.
696 if (stmtOffset >= U->getLineSection().size())
697 return nullptr;
698
Benjamin Kramer679e1752011-09-15 20:43:18 +0000699 // We have to parse it first.
David Blaikiec4e2bed2015-11-17 21:08:05 +0000700 DataExtractor lineData(U->getLineSection(), isLittleEndian(),
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000701 U->getAddressByteSize());
Benjamin Kramer679e1752011-09-15 20:43:18 +0000702 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000703}
704
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000705void DWARFContext::parseCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000706 CUs.parse(*this, getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000707}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000708
David Blaikie03c089c2013-09-23 22:44:47 +0000709void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000710 if (!TUs.empty())
711 return;
712 for (const auto &I : getTypesSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000713 TUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000714 TUs.back().parse(*this, I.second);
David Blaikie03c089c2013-09-23 22:44:47 +0000715 }
716}
717
Eric Christopherda4b2192013-01-02 23:52:13 +0000718void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000719 DWOCUs.parseDWO(*this, getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000720}
721
David Blaikie92d9d622014-01-09 05:08:24 +0000722void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000723 if (!DWOTUs.empty())
724 return;
725 for (const auto &I : getTypesDWOSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000726 DWOTUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000727 DWOTUs.back().parseDWO(*this, I.second);
David Blaikie92d9d622014-01-09 05:08:24 +0000728 }
729}
730
Alexey Samsonov45be7932012-08-30 07:49:50 +0000731DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000732 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000733 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000734}
735
Alexey Samsonov45be7932012-08-30 07:49:50 +0000736DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000737 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000738 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000739 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000740 return getCompileUnitForOffset(CUOffset);
741}
742
David Blaikieefc4eba2017-02-06 20:19:02 +0000743static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
744 uint64_t Address,
745 FunctionNameKind Kind,
746 std::string &FunctionName,
747 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000748 // The address may correspond to instruction in some inlined function,
749 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000750 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000751 SmallVector<DWARFDie, 4> InlinedChain;
752 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000753 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000754 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000755
756 const DWARFDie &DIE = InlinedChain[0];
757 bool FoundResult = false;
758 const char *Name = nullptr;
759 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000760 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000761 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000762 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000763 if (auto DeclLineResult = DIE.getDeclLine()) {
764 StartLine = DeclLineResult;
765 FoundResult = true;
766 }
767
768 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000769}
770
Alexey Samsonov45be7932012-08-30 07:49:50 +0000771DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000772 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000773 DILineInfo Result;
774
Alexey Samsonov45be7932012-08-30 07:49:50 +0000775 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
776 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000777 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000778 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
779 Result.FunctionName,
780 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000781 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000782 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
783 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
784 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000785 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000786 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000787}
David Blaikiea379b1812011-12-20 02:50:00 +0000788
Alexey Samsonovdce67342014-05-15 21:24:32 +0000789DILineInfoTable
790DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
791 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000792 DILineInfoTable Lines;
793 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
794 if (!CU)
795 return Lines;
796
797 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000798 uint32_t StartLine = 0;
799 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
800 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000801
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000802 // If the Specifier says we don't need FileLineInfo, just
803 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000804 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000805 DILineInfo Result;
806 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000807 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000808 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000809 return Lines;
810 }
811
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000812 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000813
814 // Get the index of row we're looking for in the line table.
815 std::vector<uint32_t> RowVector;
816 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
817 return Lines;
818
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000819 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000820 // Take file number and line/column from the row.
821 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000822 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000823 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
824 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000825 Result.FunctionName = FunctionName;
826 Result.Line = Row.Line;
827 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000828 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000829 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000830 }
831
832 return Lines;
833}
834
Alexey Samsonovdce67342014-05-15 21:24:32 +0000835DIInliningInfo
836DWARFContext::getInliningInfoForAddress(uint64_t Address,
837 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000838 DIInliningInfo InliningInfo;
839
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000840 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
841 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000842 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000843
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000844 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000845 SmallVector<DWARFDie, 4> InlinedChain;
846 CU->getInlinedChainForAddress(Address, InlinedChain);
847 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000848 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
849 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000850 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000851 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000852 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000853 if (LineTable &&
854 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
855 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000856 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000857 }
858 return InliningInfo;
859 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000860
Dehao Chenef700d52017-04-17 20:10:39 +0000861 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000862 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
863 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000864 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000865 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000866 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000867 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000868 if (auto DeclLineResult = FunctionDIE.getDeclLine())
869 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000870 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000871 if (i == 0) {
872 // For the topmost frame, initialize the line table of this
873 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000874 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000875 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000876 if (LineTable)
877 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
878 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000879 } else {
880 // Otherwise, use call file, call line and call column from
881 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000882 if (LineTable)
883 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
884 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000885 Frame.Line = CallLine;
886 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000887 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000888 }
889 // Get call file/line/column of a current DIE.
890 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +0000891 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
892 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000893 }
894 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000895 InliningInfo.addFrame(Frame);
896 }
897 return InliningInfo;
898}
899
George Rimar702dac62017-04-12 08:59:15 +0000900static Error createError(const Twine &Reason, llvm::Error E) {
901 return make_error<StringError>(Reason + toString(std::move(E)),
902 inconvertibleErrorCode());
903}
904
905/// Returns the address of symbol relocation used against. Used for futher
906/// relocations computation. Symbol's section load address is taken in account if
907/// LoadedObjectInfo interface is provided.
George Rimar958b01a2017-05-15 11:45:28 +0000908static Expected<uint64_t>
909getSymbolAddress(const object::ObjectFile &Obj, const RelocationRef &Reloc,
910 const LoadedObjectInfo *L,
911 std::map<SymbolRef, uint64_t> &Cache) {
George Rimar702dac62017-04-12 08:59:15 +0000912 uint64_t Ret = 0;
913 object::section_iterator RSec = Obj.section_end();
914 object::symbol_iterator Sym = Reloc.getSymbol();
915
George Rimar958b01a2017-05-15 11:45:28 +0000916 std::map<SymbolRef, uint64_t>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +0000917 // First calculate the address of the symbol or section as it appears
918 // in the object file
919 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +0000920 bool New;
921 std::tie(CacheIt, New) = Cache.insert({*Sym, 0});
922 if (!New)
923 return CacheIt->second;
924
George Rimar702dac62017-04-12 08:59:15 +0000925 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
926 if (!SymAddrOrErr)
927 return createError("error: failed to compute symbol address: ",
928 SymAddrOrErr.takeError());
929
930 // Also remember what section this symbol is in for later
931 auto SectOrErr = Sym->getSection();
932 if (!SectOrErr)
933 return createError("error: failed to get symbol section: ",
934 SectOrErr.takeError());
935
936 RSec = *SectOrErr;
937 Ret = *SymAddrOrErr;
938 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
939 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
940 Ret = RSec->getAddress();
941 }
942
943 // If we are given load addresses for the sections, we need to adjust:
944 // SymAddr = (Address of Symbol Or Section in File) -
945 // (Address of Section in File) +
946 // (Load Address of Section)
947 // RSec is now either the section being targeted or the section
948 // containing the symbol being targeted. In either case,
949 // we need to perform the same computation.
950 if (L && RSec != Obj.section_end())
951 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
952 Ret += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +0000953
954 if (CacheIt != Cache.end())
955 CacheIt->second = Ret;
956
George Rimar702dac62017-04-12 08:59:15 +0000957 return Ret;
958}
959
960static bool isRelocScattered(const object::ObjectFile &Obj,
961 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +0000962 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
963 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +0000964 return false;
965 // MachO also has relocations that point to sections and
966 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +0000967 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
968 return MachObj->isRelocationScattered(RelocInfo);
969}
970
George Rimar2122ff642017-05-05 10:52:39 +0000971Error DWARFContextInMemory::maybeDecompress(const SectionRef &Sec,
972 StringRef Name, StringRef &Data) {
973 if (!Decompressor::isCompressed(Sec))
974 return Error::success();
975
976 Expected<Decompressor> Decompressor =
977 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
978 if (!Decompressor)
979 return Decompressor.takeError();
980
981 SmallString<32> Out;
982 if (auto Err = Decompressor->decompress(Out))
983 return Err;
984
985 UncompressedSections.emplace_back(std::move(Out));
986 Data = UncompressedSections.back();
987
988 return Error::success();
989}
990
Keno Fischerc780e8e2015-05-21 21:24:32 +0000991DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
992 const LoadedObjectInfo *L)
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +0000993 : IsLittleEndian(Obj.isLittleEndian()),
994 AddressSize(Obj.getBytesInAddress()) {
995 for (const SectionRef &Section : Obj.sections()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000996 StringRef name;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000997 Section.getName(name);
David Majnemerdac39852014-09-26 22:32:16 +0000998 // Skip BSS and Virtual sections, they aren't interesting.
Rafael Espindola80291272014-10-08 15:28:58 +0000999 bool IsBSS = Section.isBSS();
David Majnemerdac39852014-09-26 22:32:16 +00001000 if (IsBSS)
1001 continue;
Rafael Espindola80291272014-10-08 15:28:58 +00001002 bool IsVirtual = Section.isVirtual();
David Majnemerdac39852014-09-26 22:32:16 +00001003 if (IsVirtual)
1004 continue;
Eric Christopher7370b552012-11-12 21:40:38 +00001005 StringRef data;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001006
Lang Hames2e88f4f2015-07-28 17:52:11 +00001007 section_iterator RelocatedSection = Section.getRelocatedSection();
Keno Fischerc780e8e2015-05-21 21:24:32 +00001008 // Try to obtain an already relocated version of this section.
1009 // Else use the unrelocated section from the object file. We'll have to
1010 // apply relocations ourselves later.
Lang Hames2e88f4f2015-07-28 17:52:11 +00001011 if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
Keno Fischerc780e8e2015-05-21 21:24:32 +00001012 Section.getContents(data);
Eric Christopher7370b552012-11-12 21:40:38 +00001013
George Rimar2122ff642017-05-05 10:52:39 +00001014 if (auto Err = maybeDecompress(Section, name, data)) {
1015 errs() << "error: failed to decompress '" + name + "', " +
1016 toString(std::move(Err))
1017 << '\n';
1018 continue;
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001019 }
1020
George Rimar4bf30832017-01-11 15:26:41 +00001021 // Compressed sections names in GNU style starts from ".z",
1022 // at this point section is decompressed and we drop compression prefix.
1023 name = name.substr(
1024 name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1025
Chris Bieneman2e752db2017-01-20 19:03:14 +00001026 if (StringRef *SectionData = MapSectionToMember(name)) {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001027 *SectionData = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001028 if (name == "debug_ranges") {
1029 // FIXME: Use the other dwo range section when we emit it.
George Rimarca532112017-04-24 10:19:45 +00001030 RangeDWOSection.Data = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001031 }
David Blaikie03c089c2013-09-23 22:44:47 +00001032 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +00001033 // Find debug_types data by section rather than name as there are
1034 // multiple, comdat grouped, debug_types sections.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001035 TypesSections[Section].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +00001036 } else if (name == "debug_types.dwo") {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001037 TypesDWOSections[Section].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +00001038 }
Eric Christopher7370b552012-11-12 21:40:38 +00001039
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +00001040 if (RelocatedSection == Obj.section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +00001041 continue;
1042
1043 StringRef RelSecName;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001044 StringRef RelSecData;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001045 RelocatedSection->getName(RelSecName);
Keno Fischerc780e8e2015-05-21 21:24:32 +00001046
1047 // If the section we're relocating was relocated already by the JIT,
1048 // then we used the relocated version above, so we do not need to process
1049 // relocations for it now.
Lang Hames2e88f4f2015-07-28 17:52:11 +00001050 if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
Keno Fischerc780e8e2015-05-21 21:24:32 +00001051 continue;
1052
Frederic Riss7bb12262015-08-23 04:44:21 +00001053 // In Mach-o files, the relocations do not need to be applied if
1054 // there is no load offset to apply. The value read at the
1055 // relocation point already factors in the section address
1056 // (actually applying the relocations will produce wrong results
1057 // as the section address will be added twice).
Craig Topper66059c92015-11-18 07:07:59 +00001058 if (!L && isa<MachOObjectFile>(&Obj))
Frederic Riss7bb12262015-08-23 04:44:21 +00001059 continue;
1060
Rafael Espindola4f60a382013-05-30 03:05:14 +00001061 RelSecName = RelSecName.substr(
1062 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
1063
Andrew Kaylord55d7012013-01-25 22:50:58 +00001064 // TODO: Add support for relocations in other sections as needed.
1065 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola4f60a382013-05-30 03:05:14 +00001066 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie1b5ee5d2013-09-23 17:42:01 +00001067 .Case("debug_info", &InfoSection.Relocs)
1068 .Case("debug_loc", &LocSection.Relocs)
1069 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
1070 .Case("debug_line", &LineSection.Relocs)
George Rimarca532112017-04-24 10:19:45 +00001071 .Case("debug_ranges", &RangeSection.Relocs)
Frederic Riss7c500472014-11-14 19:30:08 +00001072 .Case("apple_names", &AppleNamesSection.Relocs)
1073 .Case("apple_types", &AppleTypesSection.Relocs)
1074 .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
1075 .Case("apple_namespac", &AppleNamespacesSection.Relocs)
1076 .Case("apple_objc", &AppleObjCSection.Relocs)
Craig Topper2617dcc2014-04-15 06:32:26 +00001077 .Default(nullptr);
David Blaikie03c089c2013-09-23 22:44:47 +00001078 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +00001079 // Find debug_types relocs by section rather than name as there are
1080 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +00001081 if (RelSecName == "debug_types")
1082 Map = &TypesSections[*RelocatedSection].Relocs;
1083 else if (RelSecName == "debug_types.dwo")
1084 Map = &TypesDWOSections[*RelocatedSection].Relocs;
1085 else
1086 continue;
David Blaikie03c089c2013-09-23 22:44:47 +00001087 }
Eric Christopher7370b552012-11-12 21:40:38 +00001088
George Rimar958b01a2017-05-15 11:45:28 +00001089 std::map<SymbolRef, uint64_t> AddrCache;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001090 if (Section.relocation_begin() != Section.relocation_end()) {
Rafael Espindola80291272014-10-08 15:28:58 +00001091 uint64_t SectionSize = RelocatedSection->getSize();
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001092 for (const RelocationRef &Reloc : Section.relocations()) {
George Rimar702dac62017-04-12 08:59:15 +00001093 // FIXME: it's not clear how to correctly handle scattered
1094 // relocations.
1095 if (isRelocScattered(Obj, Reloc))
1096 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001097
George Rimar958b01a2017-05-15 11:45:28 +00001098 Expected<uint64_t> SymAddrOrErr =
1099 getSymbolAddress(Obj, Reloc, L, AddrCache);
George Rimar702dac62017-04-12 08:59:15 +00001100 if (!SymAddrOrErr) {
Krasimir Georgiev4ed589d2017-04-12 11:33:26 +00001101 errs() << toString(SymAddrOrErr.takeError()) << '\n';
George Rimar702dac62017-04-12 08:59:15 +00001102 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001103 }
Eric Christopher7370b552012-11-12 21:40:38 +00001104
Eric Christopher47e079d2014-10-06 06:55:55 +00001105 object::RelocVisitor V(Obj);
George Rimar702dac62017-04-12 08:59:15 +00001106 object::RelocToApply R(V.visit(Reloc.getType(), Reloc, *SymAddrOrErr));
Eric Christopher7370b552012-11-12 21:40:38 +00001107 if (V.error()) {
1108 SmallString<32> Name;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001109 Reloc.getTypeName(Name);
Eric Christopher7370b552012-11-12 21:40:38 +00001110 errs() << "error: failed to compute relocation: "
1111 << Name << "\n";
1112 continue;
1113 }
George Rimar702dac62017-04-12 08:59:15 +00001114 uint64_t Address = Reloc.getOffset();
Eric Christopher7370b552012-11-12 21:40:38 +00001115 if (Address + R.Width > SectionSize) {
1116 errs() << "error: " << R.Width << "-byte relocation starting "
1117 << Address << " bytes into section " << name << " which is "
1118 << SectionSize << " bytes long.\n";
1119 continue;
1120 }
1121 if (R.Width > 8) {
1122 errs() << "error: can't handle a relocation of more than 8 bytes at "
1123 "a time.\n";
1124 continue;
1125 }
1126 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
1127 << " at " << format("%p", Address)
1128 << " with width " << format("%d", R.Width)
1129 << "\n");
George Rimar41e65672017-05-16 14:05:45 +00001130 Map->insert({Address, {(uint8_t)R.Width, R.Value}});
Eric Christopher7370b552012-11-12 21:40:38 +00001131 }
1132 }
1133 }
1134}
1135
Chris Bieneman2e752db2017-01-20 19:03:14 +00001136DWARFContextInMemory::DWARFContextInMemory(
1137 const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize,
1138 bool isLittleEndian)
1139 : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) {
1140 for (const auto &SecIt : Sections) {
1141 if (StringRef *SectionData = MapSectionToMember(SecIt.first()))
1142 *SectionData = SecIt.second->getBuffer();
1143 }
1144}
1145
1146StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) {
1147 return StringSwitch<StringRef *>(Name)
1148 .Case("debug_info", &InfoSection.Data)
1149 .Case("debug_abbrev", &AbbrevSection)
1150 .Case("debug_loc", &LocSection.Data)
1151 .Case("debug_line", &LineSection.Data)
1152 .Case("debug_aranges", &ARangeSection)
1153 .Case("debug_frame", &DebugFrameSection)
1154 .Case("eh_frame", &EHFrameSection)
1155 .Case("debug_str", &StringSection)
George Rimarca532112017-04-24 10:19:45 +00001156 .Case("debug_ranges", &RangeSection.Data)
Chris Bieneman2e752db2017-01-20 19:03:14 +00001157 .Case("debug_macinfo", &MacinfoSection)
1158 .Case("debug_pubnames", &PubNamesSection)
1159 .Case("debug_pubtypes", &PubTypesSection)
1160 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1161 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1162 .Case("debug_info.dwo", &InfoDWOSection.Data)
1163 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1164 .Case("debug_loc.dwo", &LocDWOSection.Data)
1165 .Case("debug_line.dwo", &LineDWOSection.Data)
1166 .Case("debug_str.dwo", &StringDWOSection)
1167 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1168 .Case("debug_addr", &AddrSection)
1169 .Case("apple_names", &AppleNamesSection.Data)
1170 .Case("apple_types", &AppleTypesSection.Data)
1171 .Case("apple_namespaces", &AppleNamespacesSection.Data)
1172 .Case("apple_namespac", &AppleNamespacesSection.Data)
1173 .Case("apple_objc", &AppleObjCSection.Data)
1174 .Case("debug_cu_index", &CUIndexSection)
1175 .Case("debug_tu_index", &TUIndexSection)
1176 .Case("gdb_index", &GdbIndexSection)
1177 // Any more debug info sections go here.
1178 .Default(nullptr);
1179}
1180
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001181void DWARFContextInMemory::anchor() {}