blob: 84aa0909b6d053828371759344cd942823189c91 [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
Alexey Samsonove16e16a2012-07-19 07:03:58 +000010#include "llvm/ADT/SmallString.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/STLExtras.h"
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +000013#include "llvm/ADT/StringSwitch.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000014#include "llvm/ADT/StringRef.h"
Zachary Turner82af9432015-01-30 18:07:45 +000015#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000016#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
17#include "llvm/DebugInfo/DWARF/DWARFContext.h"
18#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
19#include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
Zachary Turner82af9432015-01-30 18:07:45 +000020#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.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"
George Rimar4bf30832017-01-11 15:26:41 +000032#include "llvm/Object/Decompressor.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000033#include "llvm/Object/MachO.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000034#include "llvm/Object/ObjectFile.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000035#include "llvm/Object/RelocVisitor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000036#include "llvm/Support/Casting.h"
37#include "llvm/Support/DataExtractor.h"
38#include "llvm/Support/Debug.h"
39#include "llvm/Support/Error.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000040#include "llvm/Support/Format.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000041#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000042#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000043#include <algorithm>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000044#include <cstdint>
Greg Claytonc7695a82017-05-02 20:28:33 +000045#include <map>
46#include <set>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000047#include <string>
48#include <utility>
49#include <vector>
50
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000051using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000052using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000053using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000054
Chandler Carruthe96dd892014-04-21 22:55:11 +000055#define DEBUG_TYPE "dwarf"
56
Eric Christopher494109b2012-10-16 23:46:25 +000057typedef DWARFDebugLine::LineTable DWARFLineTable;
Alexey Samsonovdce67342014-05-15 21:24:32 +000058typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
59typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000060
George Rimarf8a96422017-04-21 09:12:18 +000061uint64_t llvm::getRelocatedValue(const DataExtractor &Data, uint32_t Size,
62 uint32_t *Off, const RelocAddrMap *Relocs) {
63 if (!Relocs)
64 return Data.getUnsigned(Off, Size);
65 RelocAddrMap::const_iterator AI = Relocs->find(*Off);
66 if (AI == Relocs->end())
67 return Data.getUnsigned(Off, Size);
68 return Data.getUnsigned(Off, Size) + AI->second.second;
69}
70
Frederic Riss7c500472014-11-14 19:30:08 +000071static void dumpAccelSection(raw_ostream &OS, StringRef Name,
72 const DWARFSection& Section, StringRef StringSection,
73 bool LittleEndian) {
74 DataExtractor AccelSection(Section.Data, LittleEndian, 0);
Frederic Risse837ec22014-11-14 16:15:53 +000075 DataExtractor StrData(StringSection, LittleEndian, 0);
76 OS << "\n." << Name << " contents:\n";
Frederic Riss7c500472014-11-14 19:30:08 +000077 DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
Frederic Risse837ec22014-11-14 16:15:53 +000078 if (!Accel.extract())
79 return;
80 Accel.dump(OS);
81}
82
David Blaikie50cc27e2016-10-18 21:09:48 +000083void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH,
84 bool SummarizeTypes) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +000085 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
86 OS << ".debug_abbrev contents:\n";
87 getDebugAbbrev()->dump(OS);
88 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000089
David Blaikie66865d62014-01-09 00:13:35 +000090 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
91 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +000092 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +000093 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +000094 }
David Blaikie622dce42014-01-08 23:29:59 +000095
Eli Bendersky7a94daa2013-01-25 20:26:43 +000096 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
97 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000098 for (const auto &CU : compile_units())
99 CU->dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000100 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000101
David Blaikie66865d62014-01-09 00:13:35 +0000102 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
103 getNumDWOCompileUnits()) {
104 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000105 for (const auto &DWOCU : dwo_compile_units())
106 DWOCU->dump(OS);
David Blaikie66865d62014-01-09 00:13:35 +0000107 }
David Blaikie622dce42014-01-08 23:29:59 +0000108
David Blaikie66865d62014-01-09 00:13:35 +0000109 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000110 OS << "\n.debug_types contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000111 for (const auto &TUS : type_unit_sections())
112 for (const auto &TU : TUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000113 TU->dump(OS, SummarizeTypes);
David Blaikie03c089c2013-09-23 22:44:47 +0000114 }
115
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000116 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
117 getNumDWOTypeUnits()) {
118 OS << "\n.debug_types.dwo contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000119 for (const auto &DWOTUS : dwo_type_unit_sections())
120 for (const auto &DWOTU : DWOTUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000121 DWOTU->dump(OS, SummarizeTypes);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000122 }
David Blaikie92d9d622014-01-09 05:08:24 +0000123
David Blaikie18e73502013-06-19 21:37:13 +0000124 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +0000125 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +0000126 getDebugLoc()->dump(OS);
127 }
128
David Blaikie9c550ac2014-03-25 01:44:02 +0000129 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
130 OS << "\n.debug_loc.dwo contents:\n";
131 getDebugLocDWO()->dump(OS);
132 }
133
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000134 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
135 OS << "\n.debug_frame contents:\n";
136 getDebugFrame()->dump(OS);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000137 if (DumpEH) {
138 OS << "\n.eh_frame contents:\n";
139 getEHFrame()->dump(OS);
140 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000141 }
142
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000143 if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
144 OS << "\n.debug_macinfo contents:\n";
145 getDebugMacro()->dump(OS);
146 }
147
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000148 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000149 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
150 OS << "\n.debug_aranges contents:\n";
151 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
152 DWARFDebugArangeSet set;
153 while (set.extract(arangesData, &offset))
154 set.dump(OS);
155 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000156
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000157 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000158 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
159 OS << "\n.debug_line contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000160 for (const auto &CU : compile_units()) {
161 savedAddressByteSize = CU->getAddressByteSize();
Greg Claytonc8c10322016-12-13 18:25:19 +0000162 auto CUDIE = CU->getUnitDIE();
163 if (!CUDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000164 continue;
Greg Clayton97d22182017-01-13 21:08:18 +0000165 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000166 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000167 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000168 DWARFDebugLine::LineTable LineTable;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000169 uint32_t Offset = *StmtOffset;
170 LineTable.parse(lineData, &getLineSection().Relocs, &Offset);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000171 LineTable.dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000172 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000173 }
174 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000175
David Blaikie65a8efe2015-11-11 19:28:21 +0000176 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
177 OS << "\n.debug_cu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000178 getCUIndex().dump(OS);
David Blaikie65a8efe2015-11-11 19:28:21 +0000179 }
180
David Blaikie51c40282015-11-11 19:40:49 +0000181 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
182 OS << "\n.debug_tu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000183 getTUIndex().dump(OS);
David Blaikie51c40282015-11-11 19:40:49 +0000184 }
185
David Blaikie1d4736e2014-02-24 23:58:54 +0000186 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
187 OS << "\n.debug_line.dwo contents:\n";
188 unsigned stmtOffset = 0;
189 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
190 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000191 DWARFDebugLine::LineTable LineTable;
192 while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
193 LineTable.dump(OS);
194 LineTable.clear();
195 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000196 }
197
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000198 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
199 OS << "\n.debug_str contents:\n";
200 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
201 offset = 0;
202 uint32_t strOffset = 0;
203 while (const char *s = strData.getCStr(&offset)) {
204 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
205 strOffset = offset;
206 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000207 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000208
David Blaikie66865d62014-01-09 00:13:35 +0000209 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
210 !getStringDWOSection().empty()) {
211 OS << "\n.debug_str.dwo contents:\n";
212 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
213 offset = 0;
214 uint32_t strDWOOffset = 0;
215 while (const char *s = strDWOData.getCStr(&offset)) {
216 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
217 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000218 }
David Blaikie66865d62014-01-09 00:13:35 +0000219 }
David Blaikie622dce42014-01-08 23:29:59 +0000220
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000221 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
222 OS << "\n.debug_ranges contents:\n";
223 // In fact, different compile units may have different address byte
224 // sizes, but for simplicity we just use the address byte size of the last
225 // compile unit (there is no easy and fast way to associate address range
226 // list and the compile unit it describes).
George Rimarca532112017-04-24 10:19:45 +0000227 DataExtractor rangesData(getRangeSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000228 savedAddressByteSize);
229 offset = 0;
230 DWARFDebugRangeList rangeList;
George Rimarca532112017-04-24 10:19:45 +0000231 while (rangeList.extract(rangesData, &offset, getRangeSection().Relocs))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000232 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000233 }
Eric Christopher962c9082013-01-15 23:56:56 +0000234
Eric Christopher0de53592013-09-25 23:02:36 +0000235 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000236 DWARFDebugPubTable(getPubNamesSection(), isLittleEndian(), false)
237 .dump("debug_pubnames", OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000238
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000239 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000240 DWARFDebugPubTable(getPubTypesSection(), isLittleEndian(), false)
241 .dump("debug_pubtypes", OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000242
David Blaikieecd21ff2013-09-24 19:50:00 +0000243 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000244 DWARFDebugPubTable(getGnuPubNamesSection(), isLittleEndian(),
245 true /* GnuStyle */)
246 .dump("debug_gnu_pubnames", OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000247
248 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000249 DWARFDebugPubTable(getGnuPubTypesSection(), isLittleEndian(),
250 true /* GnuStyle */)
251 .dump("debug_gnu_pubtypes", OS);
David Blaikie404d3042013-09-19 23:01:29 +0000252
David Blaikie66865d62014-01-09 00:13:35 +0000253 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
254 !getStringOffsetDWOSection().empty()) {
255 OS << "\n.debug_str_offsets.dwo contents:\n";
256 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
257 0);
258 offset = 0;
259 uint64_t size = getStringOffsetDWOSection().size();
260 while (offset < size) {
261 OS << format("0x%8.8x: ", offset);
262 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000263 }
David Blaikie66865d62014-01-09 00:13:35 +0000264 }
Frederic Risse837ec22014-11-14 16:15:53 +0000265
George Rimar4f82df52016-09-23 11:01:53 +0000266 if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) &&
267 !getGdbIndexSection().empty()) {
268 OS << "\n.gnu_index contents:\n";
269 getGdbIndex().dump(OS);
270 }
271
Frederic Risse837ec22014-11-14 16:15:53 +0000272 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
273 dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
274 getStringSection(), isLittleEndian());
275
276 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
277 dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
278 getStringSection(), isLittleEndian());
279
280 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
281 dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
282 getStringSection(), isLittleEndian());
283
284 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
285 dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
286 getStringSection(), isLittleEndian());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000287}
288
Greg Claytonc7695a82017-05-02 20:28:33 +0000289DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
290 parseCompileUnits();
291 if (auto *CU = CUs.getUnitForOffset(Offset))
292 return CU->getDIEForOffset(Offset);
293 return DWARFDie();
294}
295
296namespace {
297
298class Verifier {
299 raw_ostream &OS;
300 DWARFContext &DCtx;
301public:
302 Verifier(raw_ostream &S, DWARFContext &D) : OS(S), DCtx(D) {}
303
304 bool HandleDebugInfo() {
305 bool Success = true;
306 // A map that tracks all references (converted absolute references) so we
307 // can verify each reference points to a valid DIE and not an offset that
308 // lies between to valid DIEs.
309 std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
310
Greg Clayton48432cf2017-05-01 22:07:02 +0000311 OS << "Verifying .debug_info...\n";
Greg Claytonc7695a82017-05-02 20:28:33 +0000312 for (const auto &CU : DCtx.compile_units()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000313 unsigned NumDies = CU->getNumDIEs();
314 for (unsigned I = 0; I < NumDies; ++I) {
315 auto Die = CU->getDIEAtIndex(I);
316 const auto Tag = Die.getTag();
317 if (Tag == DW_TAG_null)
318 continue;
319 for (auto AttrValue : Die.attributes()) {
320 const auto Attr = AttrValue.Attr;
321 const auto Form = AttrValue.Value.getForm();
322 switch (Attr) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000323 case DW_AT_ranges:
324 // Make sure the offset in the DW_AT_ranges attribute is valid.
325 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
326 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
327 Success = false;
328 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
329 "bounds:\n";
330 Die.dump(OS, 0);
331 OS << "\n";
332 }
333 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000334 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000335 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000336 Die.dump(OS, 0);
337 OS << "\n";
338 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000339 break;
340 case DW_AT_stmt_list:
341 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
342 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
343 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
344 Success = false;
345 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
346 "bounds: "
347 << format("0x%08" PRIx32, *SectionOffset) << "\n";
348 CU->getUnitDIE().dump(OS, 0);
349 OS << "\n";
350 }
351 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000352 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000353 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
354 Die.dump(OS, 0);
Greg Clayton48432cf2017-05-01 22:07:02 +0000355 OS << "\n";
356 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000357 break;
358
359 default:
360 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000361 }
362 switch (Form) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000363 case DW_FORM_ref1:
364 case DW_FORM_ref2:
365 case DW_FORM_ref4:
366 case DW_FORM_ref8:
367 case DW_FORM_ref_udata: {
368 // Verify all CU relative references are valid CU offsets.
369 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
370 assert(RefVal);
371 if (RefVal) {
372 auto DieCU = Die.getDwarfUnit();
373 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
374 auto CUOffset = AttrValue.Value.getRawUValue();
375 if (CUOffset >= CUSize) {
376 Success = false;
377 OS << "error: " << FormEncodingString(Form) << " CU offset "
378 << format("0x%08" PRIx32, CUOffset)
379 << " is invalid (must be less than CU size of "
380 << format("0x%08" PRIx32, CUSize) << "):\n";
381 Die.dump(OS, 0);
382 OS << "\n";
383 } else {
384 // Valid reference, but we will verify it points to an actual
385 // DIE later.
386 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
387 }
388 }
389 break;
390 }
391 case DW_FORM_ref_addr: {
392 // Verify all absolute DIE references have valid offsets in the
393 // .debug_info section.
394 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
395 assert(RefVal);
396 if (RefVal) {
397 if(*RefVal >= DCtx.getInfoSection().Data.size()) {
398 Success = false;
399 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
400 "bounds:\n";
401 Die.dump(OS, 0);
402 OS << "\n";
403 } else {
404 // Valid reference, but we will verify it points to an actual
405 // DIE later.
406 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
407 }
408 }
409 break;
410 }
411 case DW_FORM_strp: {
412 auto SecOffset = AttrValue.Value.getAsSectionOffset();
413 assert(SecOffset); // DW_FORM_strp is a section offset.
414 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000415 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000416 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000417 Die.dump(OS, 0);
418 OS << "\n";
419 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000420 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000421 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000422 default:
423 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000424 }
425 }
426 }
427 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000428
429 // Take all references and make sure they point to an actual DIE by
430 // getting the DIE by offset and emitting an error
431 OS << "Verifying .debug_info references...\n";
432 for (auto Pair: ReferenceToDIEOffsets) {
433 auto Die = DCtx.getDIEForOffset(Pair.first);
434 if (Die)
435 continue;
436 Success = false;
437 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
438 << ". Offset is in between DIEs:\n";
439 for (auto Offset: Pair.second) {
440 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
441 ReferencingDie.dump(OS, 0);
442 OS << "\n";
443 }
444 OS << "\n";
445 }
446 return Success;
447 }
Greg Clayton67070462017-05-02 22:48:52 +0000448
449 bool HandleDebugLine() {
450 bool Success = true;
451 OS << "Verifying .debug_line...\n";
452 for (const auto &CU : DCtx.compile_units()) {
453 uint32_t LineTableOffset = 0;
454 auto StmtFormValue = CU->getUnitDIE().find(DW_AT_stmt_list);
455 if (!StmtFormValue) {
456 // No line table for this compile unit.
457 continue;
458 }
459 // Get the attribute value as a section offset. No need to produce an
460 // error here if the encoding isn't correct because we validate this in
461 // the .debug_info verifier.
462 if (auto StmtSectionOffset = toSectionOffset(StmtFormValue)) {
463 LineTableOffset = *StmtSectionOffset;
464 if (LineTableOffset >= DCtx.getLineSection().Data.size()) {
465 // Make sure we don't get a valid line table back if the offset
466 // is wrong.
467 assert(DCtx.getLineTableForUnit(CU.get()) == nullptr);
468 // Skip this line table as it isn't valid. No need to create an error
469 // here because we validate this in the .debug_info verifier.
470 continue;
471 }
472 }
473 auto LineTable = DCtx.getLineTableForUnit(CU.get());
474 if (!LineTable) {
475 Success = false;
476 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
477 << "] was not able to be parsed for CU:\n";
478 CU->getUnitDIE().dump(OS, 0);
479 OS << '\n';
480 continue;
481 }
482 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
483 uint64_t PrevAddress = 0;
484 uint32_t RowIndex = 0;
485 for (const auto &Row : LineTable->Rows) {
486 if (Row.Address < PrevAddress) {
487 Success = false;
488 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
489 << "] row[" << RowIndex
490 << "] decreases in address from previous row:\n";
491
492 DWARFDebugLine::Row::dumpTableHeader(OS);
493 if (RowIndex > 0)
494 LineTable->Rows[RowIndex - 1].dump(OS);
495 Row.dump(OS);
496 OS << '\n';
497 }
498
499 if (Row.File > MaxFileIndex) {
500 Success = false;
501 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
502 << "][" << RowIndex << "] has invalid file index " << Row.File
503 << " (valid values are [1," << MaxFileIndex << "]):\n";
504 DWARFDebugLine::Row::dumpTableHeader(OS);
505 Row.dump(OS);
506 OS << '\n';
507 }
508 if (Row.EndSequence)
509 PrevAddress = 0;
510 else
511 PrevAddress = Row.Address;
512 ++RowIndex;
513 }
514 }
515 return Success;
516 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000517};
518
519} // anonymous namespace
520
521bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
522 bool Success = true;
523 Verifier verifier(OS, *this);
524 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
525 if (!verifier.HandleDebugInfo())
526 Success = false;
Greg Clayton48432cf2017-05-01 22:07:02 +0000527 }
Greg Clayton67070462017-05-02 22:48:52 +0000528 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
529 if (!verifier.HandleDebugLine())
530 Success = false;
531 }
Greg Clayton48432cf2017-05-01 22:07:02 +0000532 return Success;
533}
David Blaikie82641be2015-11-17 00:39:55 +0000534const DWARFUnitIndex &DWARFContext::getCUIndex() {
535 if (CUIndex)
536 return *CUIndex;
537
538 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
539
David Blaikieb073cb92015-12-02 06:21:34 +0000540 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000541 CUIndex->parse(CUIndexData);
542 return *CUIndex;
543}
544
545const DWARFUnitIndex &DWARFContext::getTUIndex() {
546 if (TUIndex)
547 return *TUIndex;
548
549 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
550
David Blaikieb073cb92015-12-02 06:21:34 +0000551 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000552 TUIndex->parse(TUIndexData);
553 return *TUIndex;
554}
555
George Rimar4f82df52016-09-23 11:01:53 +0000556DWARFGdbIndex &DWARFContext::getGdbIndex() {
557 if (GdbIndex)
558 return *GdbIndex;
559
560 DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0);
561 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
562 GdbIndex->parse(GdbIndexData);
563 return *GdbIndex;
564}
565
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000566const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
567 if (Abbrev)
568 return Abbrev.get();
569
570 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
571
572 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000573 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000574 return Abbrev.get();
575}
576
Eric Christopherda4b2192013-01-02 23:52:13 +0000577const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
578 if (AbbrevDWO)
579 return AbbrevDWO.get();
580
581 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
582 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000583 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000584 return AbbrevDWO.get();
585}
586
David Blaikie18e73502013-06-19 21:37:13 +0000587const DWARFDebugLoc *DWARFContext::getDebugLoc() {
588 if (Loc)
589 return Loc.get();
590
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000591 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
592 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000593 // assume all compile units have the same address byte size
594 if (getNumCompileUnits())
595 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
596 return Loc.get();
597}
598
David Blaikie9c550ac2014-03-25 01:44:02 +0000599const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
600 if (LocDWO)
601 return LocDWO.get();
602
603 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
604 LocDWO.reset(new DWARFDebugLocDWO());
605 LocDWO->parse(LocData);
606 return LocDWO.get();
607}
608
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000609const DWARFDebugAranges *DWARFContext::getDebugAranges() {
610 if (Aranges)
611 return Aranges.get();
612
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000613 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000614 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000615 return Aranges.get();
616}
617
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000618const DWARFDebugFrame *DWARFContext::getDebugFrame() {
619 if (DebugFrame)
620 return DebugFrame.get();
621
622 // There's a "bug" in the DWARFv3 standard with respect to the target address
623 // size within debug frame sections. While DWARF is supposed to be independent
624 // of its container, FDEs have fields with size being "target address size",
625 // which isn't specified in DWARF in general. It's only specified for CUs, but
626 // .eh_frame can appear without a .debug_info section. Follow the example of
627 // other tools (libdwarf) and extract this from the container (ObjectFile
628 // provides this information). This problem is fixed in DWARFv4
629 // See this dwarf-discuss discussion for more details:
630 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
631 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
632 getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000633 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
634 DebugFrame->parse(debugFrameData);
635 return DebugFrame.get();
636}
637
638const DWARFDebugFrame *DWARFContext::getEHFrame() {
639 if (EHFrame)
640 return EHFrame.get();
641
642 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
643 getAddressSize());
644 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000645 DebugFrame->parse(debugFrameData);
646 return DebugFrame.get();
647}
648
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000649const DWARFDebugMacro *DWARFContext::getDebugMacro() {
650 if (Macro)
651 return Macro.get();
652
653 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
654 Macro.reset(new DWARFDebugMacro());
655 Macro->parse(MacinfoData);
656 return Macro.get();
657}
658
Eric Christopher494109b2012-10-16 23:46:25 +0000659const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000660DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000661 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000662 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
David Blaikiec4e2bed2015-11-17 21:08:05 +0000663
Greg Claytonc8c10322016-12-13 18:25:19 +0000664 auto UnitDIE = U->getUnitDIE();
665 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000666 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000667
Greg Clayton97d22182017-01-13 21:08:18 +0000668 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000669 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000670 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000671
Greg Clayton52fe1f62016-12-14 22:38:08 +0000672 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000673 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000674 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000675 return lt;
676
677 // We have to parse it first.
David Blaikiec4e2bed2015-11-17 21:08:05 +0000678 DataExtractor lineData(U->getLineSection(), isLittleEndian(),
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000679 U->getAddressByteSize());
Benjamin Kramer679e1752011-09-15 20:43:18 +0000680 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000681}
682
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000683void DWARFContext::parseCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000684 CUs.parse(*this, getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000685}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000686
David Blaikie03c089c2013-09-23 22:44:47 +0000687void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000688 if (!TUs.empty())
689 return;
690 for (const auto &I : getTypesSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000691 TUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000692 TUs.back().parse(*this, I.second);
David Blaikie03c089c2013-09-23 22:44:47 +0000693 }
694}
695
Eric Christopherda4b2192013-01-02 23:52:13 +0000696void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000697 DWOCUs.parseDWO(*this, getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000698}
699
David Blaikie92d9d622014-01-09 05:08:24 +0000700void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000701 if (!DWOTUs.empty())
702 return;
703 for (const auto &I : getTypesDWOSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000704 DWOTUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000705 DWOTUs.back().parseDWO(*this, I.second);
David Blaikie92d9d622014-01-09 05:08:24 +0000706 }
707}
708
Alexey Samsonov45be7932012-08-30 07:49:50 +0000709DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000710 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000711 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000712}
713
Alexey Samsonov45be7932012-08-30 07:49:50 +0000714DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000715 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000716 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000717 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000718 return getCompileUnitForOffset(CUOffset);
719}
720
David Blaikieefc4eba2017-02-06 20:19:02 +0000721static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
722 uint64_t Address,
723 FunctionNameKind Kind,
724 std::string &FunctionName,
725 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000726 // The address may correspond to instruction in some inlined function,
727 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000728 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000729 SmallVector<DWARFDie, 4> InlinedChain;
730 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000731 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000732 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000733
734 const DWARFDie &DIE = InlinedChain[0];
735 bool FoundResult = false;
736 const char *Name = nullptr;
737 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000738 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000739 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000740 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000741 if (auto DeclLineResult = DIE.getDeclLine()) {
742 StartLine = DeclLineResult;
743 FoundResult = true;
744 }
745
746 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000747}
748
Alexey Samsonov45be7932012-08-30 07:49:50 +0000749DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000750 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000751 DILineInfo Result;
752
Alexey Samsonov45be7932012-08-30 07:49:50 +0000753 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
754 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000755 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000756 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
757 Result.FunctionName,
758 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000759 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000760 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
761 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
762 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000763 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000764 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000765}
David Blaikiea379b1812011-12-20 02:50:00 +0000766
Alexey Samsonovdce67342014-05-15 21:24:32 +0000767DILineInfoTable
768DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
769 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000770 DILineInfoTable Lines;
771 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
772 if (!CU)
773 return Lines;
774
775 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000776 uint32_t StartLine = 0;
777 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
778 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000779
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000780 // If the Specifier says we don't need FileLineInfo, just
781 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000782 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000783 DILineInfo Result;
784 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000785 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000786 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000787 return Lines;
788 }
789
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000790 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000791
792 // Get the index of row we're looking for in the line table.
793 std::vector<uint32_t> RowVector;
794 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
795 return Lines;
796
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000797 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000798 // Take file number and line/column from the row.
799 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000800 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000801 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
802 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000803 Result.FunctionName = FunctionName;
804 Result.Line = Row.Line;
805 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000806 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000807 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000808 }
809
810 return Lines;
811}
812
Alexey Samsonovdce67342014-05-15 21:24:32 +0000813DIInliningInfo
814DWARFContext::getInliningInfoForAddress(uint64_t Address,
815 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000816 DIInliningInfo InliningInfo;
817
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000818 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
819 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000820 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000821
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000822 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000823 SmallVector<DWARFDie, 4> InlinedChain;
824 CU->getInlinedChainForAddress(Address, InlinedChain);
825 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000826 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
827 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000828 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000829 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000830 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000831 if (LineTable &&
832 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
833 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000834 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000835 }
836 return InliningInfo;
837 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000838
Dehao Chenef700d52017-04-17 20:10:39 +0000839 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000840 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
841 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000842 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000843 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000844 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000845 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000846 if (auto DeclLineResult = FunctionDIE.getDeclLine())
847 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000848 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000849 if (i == 0) {
850 // For the topmost frame, initialize the line table of this
851 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000852 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000853 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000854 if (LineTable)
855 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
856 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000857 } else {
858 // Otherwise, use call file, call line and call column from
859 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000860 if (LineTable)
861 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
862 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000863 Frame.Line = CallLine;
864 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000865 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000866 }
867 // Get call file/line/column of a current DIE.
868 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +0000869 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
870 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000871 }
872 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000873 InliningInfo.addFrame(Frame);
874 }
875 return InliningInfo;
876}
877
George Rimar702dac62017-04-12 08:59:15 +0000878static Error createError(const Twine &Reason, llvm::Error E) {
879 return make_error<StringError>(Reason + toString(std::move(E)),
880 inconvertibleErrorCode());
881}
882
883/// Returns the address of symbol relocation used against. Used for futher
884/// relocations computation. Symbol's section load address is taken in account if
885/// LoadedObjectInfo interface is provided.
886static Expected<uint64_t> getSymbolAddress(const object::ObjectFile &Obj,
887 const RelocationRef &Reloc,
888 const LoadedObjectInfo *L) {
889 uint64_t Ret = 0;
890 object::section_iterator RSec = Obj.section_end();
891 object::symbol_iterator Sym = Reloc.getSymbol();
892
893 // First calculate the address of the symbol or section as it appears
894 // in the object file
895 if (Sym != Obj.symbol_end()) {
896 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
897 if (!SymAddrOrErr)
898 return createError("error: failed to compute symbol address: ",
899 SymAddrOrErr.takeError());
900
901 // Also remember what section this symbol is in for later
902 auto SectOrErr = Sym->getSection();
903 if (!SectOrErr)
904 return createError("error: failed to get symbol section: ",
905 SectOrErr.takeError());
906
907 RSec = *SectOrErr;
908 Ret = *SymAddrOrErr;
909 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
910 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
911 Ret = RSec->getAddress();
912 }
913
914 // If we are given load addresses for the sections, we need to adjust:
915 // SymAddr = (Address of Symbol Or Section in File) -
916 // (Address of Section in File) +
917 // (Load Address of Section)
918 // RSec is now either the section being targeted or the section
919 // containing the symbol being targeted. In either case,
920 // we need to perform the same computation.
921 if (L && RSec != Obj.section_end())
922 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
923 Ret += SectionLoadAddress - RSec->getAddress();
924 return Ret;
925}
926
927static bool isRelocScattered(const object::ObjectFile &Obj,
928 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +0000929 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
930 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +0000931 return false;
932 // MachO also has relocations that point to sections and
933 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +0000934 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
935 return MachObj->isRelocationScattered(RelocInfo);
936}
937
Keno Fischerc780e8e2015-05-21 21:24:32 +0000938DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
939 const LoadedObjectInfo *L)
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +0000940 : IsLittleEndian(Obj.isLittleEndian()),
941 AddressSize(Obj.getBytesInAddress()) {
942 for (const SectionRef &Section : Obj.sections()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000943 StringRef name;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000944 Section.getName(name);
David Majnemerdac39852014-09-26 22:32:16 +0000945 // Skip BSS and Virtual sections, they aren't interesting.
Rafael Espindola80291272014-10-08 15:28:58 +0000946 bool IsBSS = Section.isBSS();
David Majnemerdac39852014-09-26 22:32:16 +0000947 if (IsBSS)
948 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000949 bool IsVirtual = Section.isVirtual();
David Majnemerdac39852014-09-26 22:32:16 +0000950 if (IsVirtual)
951 continue;
Eric Christopher7370b552012-11-12 21:40:38 +0000952 StringRef data;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000953
Lang Hames2e88f4f2015-07-28 17:52:11 +0000954 section_iterator RelocatedSection = Section.getRelocatedSection();
Keno Fischerc780e8e2015-05-21 21:24:32 +0000955 // Try to obtain an already relocated version of this section.
956 // Else use the unrelocated section from the object file. We'll have to
957 // apply relocations ourselves later.
Lang Hames2e88f4f2015-07-28 17:52:11 +0000958 if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
Keno Fischerc780e8e2015-05-21 21:24:32 +0000959 Section.getContents(data);
Eric Christopher7370b552012-11-12 21:40:38 +0000960
George Rimar4bf30832017-01-11 15:26:41 +0000961 if (Decompressor::isCompressed(Section)) {
962 Expected<Decompressor> Decompressor =
963 Decompressor::create(name, data, IsLittleEndian, AddressSize == 8);
964 if (!Decompressor)
965 continue;
George Rimar401e4e52016-05-24 12:48:46 +0000966 SmallString<32> Out;
George Rimar4bf30832017-01-11 15:26:41 +0000967 if (auto Err = Decompressor->decompress(Out))
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000968 continue;
George Rimar401e4e52016-05-24 12:48:46 +0000969 UncompressedSections.emplace_back(std::move(Out));
David Blaikiea505f242014-04-05 21:26:44 +0000970 data = UncompressedSections.back();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000971 }
972
George Rimar4bf30832017-01-11 15:26:41 +0000973 // Compressed sections names in GNU style starts from ".z",
974 // at this point section is decompressed and we drop compression prefix.
975 name = name.substr(
976 name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
977
Chris Bieneman2e752db2017-01-20 19:03:14 +0000978 if (StringRef *SectionData = MapSectionToMember(name)) {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000979 *SectionData = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000980 if (name == "debug_ranges") {
981 // FIXME: Use the other dwo range section when we emit it.
George Rimarca532112017-04-24 10:19:45 +0000982 RangeDWOSection.Data = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000983 }
David Blaikie03c089c2013-09-23 22:44:47 +0000984 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +0000985 // Find debug_types data by section rather than name as there are
986 // multiple, comdat grouped, debug_types sections.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000987 TypesSections[Section].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +0000988 } else if (name == "debug_types.dwo") {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000989 TypesDWOSections[Section].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +0000990 }
Eric Christopher7370b552012-11-12 21:40:38 +0000991
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +0000992 if (RelocatedSection == Obj.section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +0000993 continue;
994
995 StringRef RelSecName;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000996 StringRef RelSecData;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000997 RelocatedSection->getName(RelSecName);
Keno Fischerc780e8e2015-05-21 21:24:32 +0000998
999 // If the section we're relocating was relocated already by the JIT,
1000 // then we used the relocated version above, so we do not need to process
1001 // relocations for it now.
Lang Hames2e88f4f2015-07-28 17:52:11 +00001002 if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
Keno Fischerc780e8e2015-05-21 21:24:32 +00001003 continue;
1004
Frederic Riss7bb12262015-08-23 04:44:21 +00001005 // In Mach-o files, the relocations do not need to be applied if
1006 // there is no load offset to apply. The value read at the
1007 // relocation point already factors in the section address
1008 // (actually applying the relocations will produce wrong results
1009 // as the section address will be added twice).
Craig Topper66059c92015-11-18 07:07:59 +00001010 if (!L && isa<MachOObjectFile>(&Obj))
Frederic Riss7bb12262015-08-23 04:44:21 +00001011 continue;
1012
Rafael Espindola4f60a382013-05-30 03:05:14 +00001013 RelSecName = RelSecName.substr(
1014 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
1015
Andrew Kaylord55d7012013-01-25 22:50:58 +00001016 // TODO: Add support for relocations in other sections as needed.
1017 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola4f60a382013-05-30 03:05:14 +00001018 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie1b5ee5d2013-09-23 17:42:01 +00001019 .Case("debug_info", &InfoSection.Relocs)
1020 .Case("debug_loc", &LocSection.Relocs)
1021 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
1022 .Case("debug_line", &LineSection.Relocs)
George Rimarca532112017-04-24 10:19:45 +00001023 .Case("debug_ranges", &RangeSection.Relocs)
Frederic Riss7c500472014-11-14 19:30:08 +00001024 .Case("apple_names", &AppleNamesSection.Relocs)
1025 .Case("apple_types", &AppleTypesSection.Relocs)
1026 .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
1027 .Case("apple_namespac", &AppleNamespacesSection.Relocs)
1028 .Case("apple_objc", &AppleObjCSection.Relocs)
Craig Topper2617dcc2014-04-15 06:32:26 +00001029 .Default(nullptr);
David Blaikie03c089c2013-09-23 22:44:47 +00001030 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +00001031 // Find debug_types relocs by section rather than name as there are
1032 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +00001033 if (RelSecName == "debug_types")
1034 Map = &TypesSections[*RelocatedSection].Relocs;
1035 else if (RelSecName == "debug_types.dwo")
1036 Map = &TypesDWOSections[*RelocatedSection].Relocs;
1037 else
1038 continue;
David Blaikie03c089c2013-09-23 22:44:47 +00001039 }
Eric Christopher7370b552012-11-12 21:40:38 +00001040
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001041 if (Section.relocation_begin() != Section.relocation_end()) {
Rafael Espindola80291272014-10-08 15:28:58 +00001042 uint64_t SectionSize = RelocatedSection->getSize();
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001043 for (const RelocationRef &Reloc : Section.relocations()) {
George Rimar702dac62017-04-12 08:59:15 +00001044 // FIXME: it's not clear how to correctly handle scattered
1045 // relocations.
1046 if (isRelocScattered(Obj, Reloc))
1047 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001048
George Rimar702dac62017-04-12 08:59:15 +00001049 Expected<uint64_t> SymAddrOrErr = getSymbolAddress(Obj, Reloc, L);
1050 if (!SymAddrOrErr) {
Krasimir Georgiev4ed589d2017-04-12 11:33:26 +00001051 errs() << toString(SymAddrOrErr.takeError()) << '\n';
George Rimar702dac62017-04-12 08:59:15 +00001052 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001053 }
Eric Christopher7370b552012-11-12 21:40:38 +00001054
Eric Christopher47e079d2014-10-06 06:55:55 +00001055 object::RelocVisitor V(Obj);
George Rimar702dac62017-04-12 08:59:15 +00001056 object::RelocToApply R(V.visit(Reloc.getType(), Reloc, *SymAddrOrErr));
Eric Christopher7370b552012-11-12 21:40:38 +00001057 if (V.error()) {
1058 SmallString<32> Name;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001059 Reloc.getTypeName(Name);
Eric Christopher7370b552012-11-12 21:40:38 +00001060 errs() << "error: failed to compute relocation: "
1061 << Name << "\n";
1062 continue;
1063 }
George Rimar702dac62017-04-12 08:59:15 +00001064 uint64_t Address = Reloc.getOffset();
Eric Christopher7370b552012-11-12 21:40:38 +00001065 if (Address + R.Width > SectionSize) {
1066 errs() << "error: " << R.Width << "-byte relocation starting "
1067 << Address << " bytes into section " << name << " which is "
1068 << SectionSize << " bytes long.\n";
1069 continue;
1070 }
1071 if (R.Width > 8) {
1072 errs() << "error: can't handle a relocation of more than 8 bytes at "
1073 "a time.\n";
1074 continue;
1075 }
1076 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
1077 << " at " << format("%p", Address)
1078 << " with width " << format("%d", R.Width)
1079 << "\n");
Eric Christopherda4b2192013-01-02 23:52:13 +00001080 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopher7370b552012-11-12 21:40:38 +00001081 }
1082 }
1083 }
1084}
1085
Chris Bieneman2e752db2017-01-20 19:03:14 +00001086DWARFContextInMemory::DWARFContextInMemory(
1087 const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize,
1088 bool isLittleEndian)
1089 : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) {
1090 for (const auto &SecIt : Sections) {
1091 if (StringRef *SectionData = MapSectionToMember(SecIt.first()))
1092 *SectionData = SecIt.second->getBuffer();
1093 }
1094}
1095
1096StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) {
1097 return StringSwitch<StringRef *>(Name)
1098 .Case("debug_info", &InfoSection.Data)
1099 .Case("debug_abbrev", &AbbrevSection)
1100 .Case("debug_loc", &LocSection.Data)
1101 .Case("debug_line", &LineSection.Data)
1102 .Case("debug_aranges", &ARangeSection)
1103 .Case("debug_frame", &DebugFrameSection)
1104 .Case("eh_frame", &EHFrameSection)
1105 .Case("debug_str", &StringSection)
George Rimarca532112017-04-24 10:19:45 +00001106 .Case("debug_ranges", &RangeSection.Data)
Chris Bieneman2e752db2017-01-20 19:03:14 +00001107 .Case("debug_macinfo", &MacinfoSection)
1108 .Case("debug_pubnames", &PubNamesSection)
1109 .Case("debug_pubtypes", &PubTypesSection)
1110 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1111 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1112 .Case("debug_info.dwo", &InfoDWOSection.Data)
1113 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1114 .Case("debug_loc.dwo", &LocDWOSection.Data)
1115 .Case("debug_line.dwo", &LineDWOSection.Data)
1116 .Case("debug_str.dwo", &StringDWOSection)
1117 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1118 .Case("debug_addr", &AddrSection)
1119 .Case("apple_names", &AppleNamesSection.Data)
1120 .Case("apple_types", &AppleTypesSection.Data)
1121 .Case("apple_namespaces", &AppleNamespacesSection.Data)
1122 .Case("apple_namespac", &AppleNamespacesSection.Data)
1123 .Case("apple_objc", &AppleObjCSection.Data)
1124 .Case("debug_cu_index", &CUIndexSection)
1125 .Case("debug_tu_index", &TUIndexSection)
1126 .Case("gdb_index", &GdbIndexSection)
1127 // Any more debug info sections go here.
1128 .Default(nullptr);
1129}
1130
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001131void DWARFContextInMemory::anchor() {}