blob: 896837c85473818e81f78e791dc5294773c80e82 [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
David Blaikie15d85fc2017-05-23 06:48:53 +0000290DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
291 // FIXME: Improve this for the case where this DWO file is really a DWP file
292 // with an index - use the index for lookup instead of a linear search.
293 for (const auto &DWOCU : dwo_compile_units())
294 if (DWOCU->getDWOId() == Hash)
295 return DWOCU.get();
296 return nullptr;
297}
298
Greg Claytonc7695a82017-05-02 20:28:33 +0000299DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
300 parseCompileUnits();
301 if (auto *CU = CUs.getUnitForOffset(Offset))
302 return CU->getDIEForOffset(Offset);
303 return DWARFDie();
304}
305
306namespace {
307
308class Verifier {
309 raw_ostream &OS;
310 DWARFContext &DCtx;
311public:
312 Verifier(raw_ostream &S, DWARFContext &D) : OS(S), DCtx(D) {}
313
314 bool HandleDebugInfo() {
315 bool Success = true;
316 // A map that tracks all references (converted absolute references) so we
317 // can verify each reference points to a valid DIE and not an offset that
318 // lies between to valid DIEs.
319 std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
320
Greg Clayton48432cf2017-05-01 22:07:02 +0000321 OS << "Verifying .debug_info...\n";
Greg Claytonc7695a82017-05-02 20:28:33 +0000322 for (const auto &CU : DCtx.compile_units()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000323 unsigned NumDies = CU->getNumDIEs();
324 for (unsigned I = 0; I < NumDies; ++I) {
325 auto Die = CU->getDIEAtIndex(I);
326 const auto Tag = Die.getTag();
327 if (Tag == DW_TAG_null)
328 continue;
329 for (auto AttrValue : Die.attributes()) {
330 const auto Attr = AttrValue.Attr;
331 const auto Form = AttrValue.Value.getForm();
332 switch (Attr) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000333 case DW_AT_ranges:
334 // Make sure the offset in the DW_AT_ranges attribute is valid.
335 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
336 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
337 Success = false;
338 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
339 "bounds:\n";
340 Die.dump(OS, 0);
341 OS << "\n";
342 }
343 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000344 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000345 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000346 Die.dump(OS, 0);
347 OS << "\n";
348 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000349 break;
350 case DW_AT_stmt_list:
351 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
352 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
353 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
354 Success = false;
355 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
356 "bounds: "
357 << format("0x%08" PRIx32, *SectionOffset) << "\n";
358 CU->getUnitDIE().dump(OS, 0);
359 OS << "\n";
360 }
361 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000362 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000363 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
364 Die.dump(OS, 0);
Greg Clayton48432cf2017-05-01 22:07:02 +0000365 OS << "\n";
366 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000367 break;
368
369 default:
370 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000371 }
372 switch (Form) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000373 case DW_FORM_ref1:
374 case DW_FORM_ref2:
375 case DW_FORM_ref4:
376 case DW_FORM_ref8:
377 case DW_FORM_ref_udata: {
378 // Verify all CU relative references are valid CU offsets.
379 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
380 assert(RefVal);
381 if (RefVal) {
382 auto DieCU = Die.getDwarfUnit();
383 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
384 auto CUOffset = AttrValue.Value.getRawUValue();
385 if (CUOffset >= CUSize) {
386 Success = false;
387 OS << "error: " << FormEncodingString(Form) << " CU offset "
388 << format("0x%08" PRIx32, CUOffset)
389 << " is invalid (must be less than CU size of "
390 << format("0x%08" PRIx32, CUSize) << "):\n";
391 Die.dump(OS, 0);
392 OS << "\n";
393 } else {
394 // Valid reference, but we will verify it points to an actual
395 // DIE later.
396 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
397 }
398 }
399 break;
400 }
401 case DW_FORM_ref_addr: {
402 // Verify all absolute DIE references have valid offsets in the
403 // .debug_info section.
404 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
405 assert(RefVal);
406 if (RefVal) {
407 if(*RefVal >= DCtx.getInfoSection().Data.size()) {
408 Success = false;
409 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
410 "bounds:\n";
411 Die.dump(OS, 0);
412 OS << "\n";
413 } else {
414 // Valid reference, but we will verify it points to an actual
415 // DIE later.
416 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
417 }
418 }
419 break;
420 }
421 case DW_FORM_strp: {
422 auto SecOffset = AttrValue.Value.getAsSectionOffset();
423 assert(SecOffset); // DW_FORM_strp is a section offset.
424 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000425 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000426 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000427 Die.dump(OS, 0);
428 OS << "\n";
429 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000430 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000431 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000432 default:
433 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000434 }
435 }
436 }
437 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000438
439 // Take all references and make sure they point to an actual DIE by
440 // getting the DIE by offset and emitting an error
441 OS << "Verifying .debug_info references...\n";
442 for (auto Pair: ReferenceToDIEOffsets) {
443 auto Die = DCtx.getDIEForOffset(Pair.first);
444 if (Die)
445 continue;
446 Success = false;
447 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
448 << ". Offset is in between DIEs:\n";
449 for (auto Offset: Pair.second) {
450 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
451 ReferencingDie.dump(OS, 0);
452 OS << "\n";
453 }
454 OS << "\n";
455 }
456 return Success;
457 }
Greg Clayton67070462017-05-02 22:48:52 +0000458
459 bool HandleDebugLine() {
Greg Clayton8df55b42017-05-03 15:45:31 +0000460 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Clayton67070462017-05-02 22:48:52 +0000461 bool Success = true;
462 OS << "Verifying .debug_line...\n";
463 for (const auto &CU : DCtx.compile_units()) {
464 uint32_t LineTableOffset = 0;
Greg Clayton8df55b42017-05-03 15:45:31 +0000465 auto CUDie = CU->getUnitDIE();
466 auto StmtFormValue = CUDie.find(DW_AT_stmt_list);
Greg Clayton67070462017-05-02 22:48:52 +0000467 if (!StmtFormValue) {
468 // No line table for this compile unit.
469 continue;
470 }
471 // Get the attribute value as a section offset. No need to produce an
472 // error here if the encoding isn't correct because we validate this in
473 // the .debug_info verifier.
474 if (auto StmtSectionOffset = toSectionOffset(StmtFormValue)) {
475 LineTableOffset = *StmtSectionOffset;
476 if (LineTableOffset >= DCtx.getLineSection().Data.size()) {
477 // Make sure we don't get a valid line table back if the offset
478 // is wrong.
479 assert(DCtx.getLineTableForUnit(CU.get()) == nullptr);
480 // Skip this line table as it isn't valid. No need to create an error
481 // here because we validate this in the .debug_info verifier.
482 continue;
Greg Clayton8df55b42017-05-03 15:45:31 +0000483 } else {
484 auto Iter = StmtListToDie.find(LineTableOffset);
485 if (Iter != StmtListToDie.end()) {
486 Success = false;
487 OS << "error: two compile unit DIEs, "
488 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
489 << format("0x%08" PRIx32, CUDie.getOffset())
490 << ", have the same DW_AT_stmt_list section offset:\n";
491 Iter->second.dump(OS, 0);
492 CUDie.dump(OS, 0);
493 OS << '\n';
494 // Already verified this line table before, no need to do it again.
495 continue;
496 }
497 StmtListToDie[LineTableOffset] = CUDie;
Greg Clayton67070462017-05-02 22:48:52 +0000498 }
499 }
500 auto LineTable = DCtx.getLineTableForUnit(CU.get());
501 if (!LineTable) {
502 Success = false;
503 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
504 << "] was not able to be parsed for CU:\n";
Greg Clayton8df55b42017-05-03 15:45:31 +0000505 CUDie.dump(OS, 0);
Greg Clayton67070462017-05-02 22:48:52 +0000506 OS << '\n';
507 continue;
508 }
509 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
510 uint64_t PrevAddress = 0;
511 uint32_t RowIndex = 0;
512 for (const auto &Row : LineTable->Rows) {
513 if (Row.Address < PrevAddress) {
514 Success = false;
515 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
516 << "] row[" << RowIndex
517 << "] decreases in address from previous row:\n";
518
519 DWARFDebugLine::Row::dumpTableHeader(OS);
520 if (RowIndex > 0)
521 LineTable->Rows[RowIndex - 1].dump(OS);
522 Row.dump(OS);
523 OS << '\n';
524 }
525
526 if (Row.File > MaxFileIndex) {
527 Success = false;
528 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
529 << "][" << RowIndex << "] has invalid file index " << Row.File
530 << " (valid values are [1," << MaxFileIndex << "]):\n";
531 DWARFDebugLine::Row::dumpTableHeader(OS);
532 Row.dump(OS);
533 OS << '\n';
534 }
535 if (Row.EndSequence)
536 PrevAddress = 0;
537 else
538 PrevAddress = Row.Address;
539 ++RowIndex;
540 }
541 }
542 return Success;
543 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000544};
545
546} // anonymous namespace
547
548bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
549 bool Success = true;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000550 DWARFVerifier verifier(OS, *this);
Greg Claytonc7695a82017-05-02 20:28:33 +0000551 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000552 if (!verifier.handleDebugInfo())
Greg Claytonc7695a82017-05-02 20:28:33 +0000553 Success = false;
Greg Clayton48432cf2017-05-01 22:07:02 +0000554 }
Greg Clayton67070462017-05-02 22:48:52 +0000555 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000556 if (!verifier.handleDebugLine())
Greg Clayton67070462017-05-02 22:48:52 +0000557 Success = false;
558 }
Greg Clayton48432cf2017-05-01 22:07:02 +0000559 return Success;
560}
David Blaikie82641be2015-11-17 00:39:55 +0000561const DWARFUnitIndex &DWARFContext::getCUIndex() {
562 if (CUIndex)
563 return *CUIndex;
564
565 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
566
David Blaikieb073cb92015-12-02 06:21:34 +0000567 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000568 CUIndex->parse(CUIndexData);
569 return *CUIndex;
570}
571
572const DWARFUnitIndex &DWARFContext::getTUIndex() {
573 if (TUIndex)
574 return *TUIndex;
575
576 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
577
David Blaikieb073cb92015-12-02 06:21:34 +0000578 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000579 TUIndex->parse(TUIndexData);
580 return *TUIndex;
581}
582
George Rimar4f82df52016-09-23 11:01:53 +0000583DWARFGdbIndex &DWARFContext::getGdbIndex() {
584 if (GdbIndex)
585 return *GdbIndex;
586
587 DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0);
588 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
589 GdbIndex->parse(GdbIndexData);
590 return *GdbIndex;
591}
592
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000593const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
594 if (Abbrev)
595 return Abbrev.get();
596
597 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
598
599 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000600 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000601 return Abbrev.get();
602}
603
Eric Christopherda4b2192013-01-02 23:52:13 +0000604const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
605 if (AbbrevDWO)
606 return AbbrevDWO.get();
607
608 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
609 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000610 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000611 return AbbrevDWO.get();
612}
613
David Blaikie18e73502013-06-19 21:37:13 +0000614const DWARFDebugLoc *DWARFContext::getDebugLoc() {
615 if (Loc)
616 return Loc.get();
617
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000618 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
619 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000620 // assume all compile units have the same address byte size
621 if (getNumCompileUnits())
622 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
623 return Loc.get();
624}
625
David Blaikie9c550ac2014-03-25 01:44:02 +0000626const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
627 if (LocDWO)
628 return LocDWO.get();
629
630 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
631 LocDWO.reset(new DWARFDebugLocDWO());
632 LocDWO->parse(LocData);
633 return LocDWO.get();
634}
635
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000636const DWARFDebugAranges *DWARFContext::getDebugAranges() {
637 if (Aranges)
638 return Aranges.get();
639
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000640 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000641 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000642 return Aranges.get();
643}
644
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000645const DWARFDebugFrame *DWARFContext::getDebugFrame() {
646 if (DebugFrame)
647 return DebugFrame.get();
648
649 // There's a "bug" in the DWARFv3 standard with respect to the target address
650 // size within debug frame sections. While DWARF is supposed to be independent
651 // of its container, FDEs have fields with size being "target address size",
652 // which isn't specified in DWARF in general. It's only specified for CUs, but
653 // .eh_frame can appear without a .debug_info section. Follow the example of
654 // other tools (libdwarf) and extract this from the container (ObjectFile
655 // provides this information). This problem is fixed in DWARFv4
656 // See this dwarf-discuss discussion for more details:
657 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
658 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
659 getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000660 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
661 DebugFrame->parse(debugFrameData);
662 return DebugFrame.get();
663}
664
665const DWARFDebugFrame *DWARFContext::getEHFrame() {
666 if (EHFrame)
667 return EHFrame.get();
668
669 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
670 getAddressSize());
671 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000672 DebugFrame->parse(debugFrameData);
673 return DebugFrame.get();
674}
675
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000676const DWARFDebugMacro *DWARFContext::getDebugMacro() {
677 if (Macro)
678 return Macro.get();
679
680 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
681 Macro.reset(new DWARFDebugMacro());
682 Macro->parse(MacinfoData);
683 return Macro.get();
684}
685
Eric Christopher494109b2012-10-16 23:46:25 +0000686const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000687DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000688 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000689 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
David Blaikiec4e2bed2015-11-17 21:08:05 +0000690
Greg Claytonc8c10322016-12-13 18:25:19 +0000691 auto UnitDIE = U->getUnitDIE();
692 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000693 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000694
Greg Clayton97d22182017-01-13 21:08:18 +0000695 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000696 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000697 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000698
Greg Clayton52fe1f62016-12-14 22:38:08 +0000699 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000700 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000701 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000702 return lt;
703
Greg Clayton48ff66a2017-05-04 18:29:44 +0000704 // Make sure the offset is good before we try to parse.
705 if (stmtOffset >= U->getLineSection().size())
706 return nullptr;
707
Benjamin Kramer679e1752011-09-15 20:43:18 +0000708 // We have to parse it first.
David Blaikiec4e2bed2015-11-17 21:08:05 +0000709 DataExtractor lineData(U->getLineSection(), isLittleEndian(),
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000710 U->getAddressByteSize());
Benjamin Kramer679e1752011-09-15 20:43:18 +0000711 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000712}
713
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000714void DWARFContext::parseCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000715 CUs.parse(*this, getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000716}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000717
David Blaikie03c089c2013-09-23 22:44:47 +0000718void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000719 if (!TUs.empty())
720 return;
721 for (const auto &I : getTypesSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000722 TUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000723 TUs.back().parse(*this, I.second);
David Blaikie03c089c2013-09-23 22:44:47 +0000724 }
725}
726
Eric Christopherda4b2192013-01-02 23:52:13 +0000727void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000728 DWOCUs.parseDWO(*this, getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000729}
730
David Blaikie92d9d622014-01-09 05:08:24 +0000731void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000732 if (!DWOTUs.empty())
733 return;
734 for (const auto &I : getTypesDWOSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000735 DWOTUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000736 DWOTUs.back().parseDWO(*this, I.second);
David Blaikie92d9d622014-01-09 05:08:24 +0000737 }
738}
739
Alexey Samsonov45be7932012-08-30 07:49:50 +0000740DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000741 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000742 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000743}
744
Alexey Samsonov45be7932012-08-30 07:49:50 +0000745DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000746 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000747 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000748 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000749 return getCompileUnitForOffset(CUOffset);
750}
751
David Blaikieefc4eba2017-02-06 20:19:02 +0000752static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
753 uint64_t Address,
754 FunctionNameKind Kind,
755 std::string &FunctionName,
756 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000757 // The address may correspond to instruction in some inlined function,
758 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000759 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000760 SmallVector<DWARFDie, 4> InlinedChain;
761 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000762 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000763 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000764
765 const DWARFDie &DIE = InlinedChain[0];
766 bool FoundResult = false;
767 const char *Name = nullptr;
768 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000769 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000770 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000771 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000772 if (auto DeclLineResult = DIE.getDeclLine()) {
773 StartLine = DeclLineResult;
774 FoundResult = true;
775 }
776
777 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000778}
779
Alexey Samsonov45be7932012-08-30 07:49:50 +0000780DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000781 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000782 DILineInfo Result;
783
Alexey Samsonov45be7932012-08-30 07:49:50 +0000784 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
785 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000786 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000787 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
788 Result.FunctionName,
789 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000790 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000791 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
792 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
793 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000794 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000795 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000796}
David Blaikiea379b1812011-12-20 02:50:00 +0000797
Alexey Samsonovdce67342014-05-15 21:24:32 +0000798DILineInfoTable
799DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
800 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000801 DILineInfoTable Lines;
802 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
803 if (!CU)
804 return Lines;
805
806 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000807 uint32_t StartLine = 0;
808 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
809 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000810
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000811 // If the Specifier says we don't need FileLineInfo, just
812 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000813 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000814 DILineInfo Result;
815 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000816 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000817 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000818 return Lines;
819 }
820
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000821 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000822
823 // Get the index of row we're looking for in the line table.
824 std::vector<uint32_t> RowVector;
825 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
826 return Lines;
827
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000828 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000829 // Take file number and line/column from the row.
830 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000831 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000832 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
833 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000834 Result.FunctionName = FunctionName;
835 Result.Line = Row.Line;
836 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000837 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000838 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000839 }
840
841 return Lines;
842}
843
Alexey Samsonovdce67342014-05-15 21:24:32 +0000844DIInliningInfo
845DWARFContext::getInliningInfoForAddress(uint64_t Address,
846 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000847 DIInliningInfo InliningInfo;
848
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000849 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
850 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000851 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000852
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000853 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000854 SmallVector<DWARFDie, 4> InlinedChain;
855 CU->getInlinedChainForAddress(Address, InlinedChain);
856 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000857 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
858 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000859 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000860 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000861 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000862 if (LineTable &&
863 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
864 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000865 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000866 }
867 return InliningInfo;
868 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000869
Dehao Chenef700d52017-04-17 20:10:39 +0000870 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000871 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
872 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000873 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000874 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000875 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000876 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000877 if (auto DeclLineResult = FunctionDIE.getDeclLine())
878 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000879 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000880 if (i == 0) {
881 // For the topmost frame, initialize the line table of this
882 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000883 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000884 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000885 if (LineTable)
886 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
887 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000888 } else {
889 // Otherwise, use call file, call line and call column from
890 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000891 if (LineTable)
892 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
893 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000894 Frame.Line = CallLine;
895 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000896 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000897 }
898 // Get call file/line/column of a current DIE.
899 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +0000900 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
901 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000902 }
903 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000904 InliningInfo.addFrame(Frame);
905 }
906 return InliningInfo;
907}
908
David Blaikief9803fb2017-05-23 00:30:42 +0000909std::shared_ptr<DWARFContext>
910DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +0000911 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +0000912 DWARFContext *Ctxt = S->Context.get();
913 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
914 }
915
David Blaikie15d85fc2017-05-23 06:48:53 +0000916 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
917
918 if (auto S = Entry->lock()) {
919 DWARFContext *Ctxt = S->Context.get();
920 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
921 }
922
923 SmallString<128> DWPName;
924 Expected<OwningBinary<ObjectFile>> Obj = [&] {
925 if (!CheckedForDWP) {
926 (getFileName() + ".dwp").toVector(DWPName);
927 auto Obj = object::ObjectFile::createObjectFile(DWPName);
928 if (Obj) {
929 Entry = &DWP;
930 return Obj;
931 } else {
932 CheckedForDWP = true;
933 // TODO: Should this error be handled (maybe in a high verbosity mode)
934 // before falling back to .dwo files?
935 consumeError(Obj.takeError());
936 }
937 }
938
939 return object::ObjectFile::createObjectFile(AbsolutePath);
940 }();
941
David Blaikief9803fb2017-05-23 00:30:42 +0000942 if (!Obj) {
943 // TODO: Actually report errors helpfully.
944 consumeError(Obj.takeError());
945 return nullptr;
946 }
David Blaikie15d85fc2017-05-23 06:48:53 +0000947
948 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +0000949 S->File = std::move(Obj.get());
950 S->Context = llvm::make_unique<DWARFContextInMemory>(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +0000951 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +0000952 auto *Ctxt = S->Context.get();
953 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
954}
955
George Rimar702dac62017-04-12 08:59:15 +0000956static Error createError(const Twine &Reason, llvm::Error E) {
957 return make_error<StringError>(Reason + toString(std::move(E)),
958 inconvertibleErrorCode());
959}
960
961/// Returns the address of symbol relocation used against. Used for futher
962/// relocations computation. Symbol's section load address is taken in account if
963/// LoadedObjectInfo interface is provided.
George Rimar958b01a2017-05-15 11:45:28 +0000964static Expected<uint64_t>
965getSymbolAddress(const object::ObjectFile &Obj, const RelocationRef &Reloc,
966 const LoadedObjectInfo *L,
967 std::map<SymbolRef, uint64_t> &Cache) {
George Rimar702dac62017-04-12 08:59:15 +0000968 uint64_t Ret = 0;
969 object::section_iterator RSec = Obj.section_end();
970 object::symbol_iterator Sym = Reloc.getSymbol();
971
George Rimar958b01a2017-05-15 11:45:28 +0000972 std::map<SymbolRef, uint64_t>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +0000973 // First calculate the address of the symbol or section as it appears
974 // in the object file
975 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +0000976 bool New;
977 std::tie(CacheIt, New) = Cache.insert({*Sym, 0});
978 if (!New)
979 return CacheIt->second;
980
George Rimar702dac62017-04-12 08:59:15 +0000981 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
982 if (!SymAddrOrErr)
983 return createError("error: failed to compute symbol address: ",
984 SymAddrOrErr.takeError());
985
986 // Also remember what section this symbol is in for later
987 auto SectOrErr = Sym->getSection();
988 if (!SectOrErr)
989 return createError("error: failed to get symbol section: ",
990 SectOrErr.takeError());
991
992 RSec = *SectOrErr;
993 Ret = *SymAddrOrErr;
994 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
995 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
996 Ret = RSec->getAddress();
997 }
998
999 // If we are given load addresses for the sections, we need to adjust:
1000 // SymAddr = (Address of Symbol Or Section in File) -
1001 // (Address of Section in File) +
1002 // (Load Address of Section)
1003 // RSec is now either the section being targeted or the section
1004 // containing the symbol being targeted. In either case,
1005 // we need to perform the same computation.
1006 if (L && RSec != Obj.section_end())
1007 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
1008 Ret += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001009
1010 if (CacheIt != Cache.end())
1011 CacheIt->second = Ret;
1012
George Rimar702dac62017-04-12 08:59:15 +00001013 return Ret;
1014}
1015
1016static bool isRelocScattered(const object::ObjectFile &Obj,
1017 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001018 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1019 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001020 return false;
1021 // MachO also has relocations that point to sections and
1022 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001023 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1024 return MachObj->isRelocationScattered(RelocInfo);
1025}
1026
George Rimar2122ff642017-05-05 10:52:39 +00001027Error DWARFContextInMemory::maybeDecompress(const SectionRef &Sec,
1028 StringRef Name, StringRef &Data) {
1029 if (!Decompressor::isCompressed(Sec))
1030 return Error::success();
1031
1032 Expected<Decompressor> Decompressor =
1033 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1034 if (!Decompressor)
1035 return Decompressor.takeError();
1036
1037 SmallString<32> Out;
George Rimarf98b9ac2017-05-18 08:00:01 +00001038 if (auto Err = Decompressor->resizeAndDecompress(Out))
George Rimar2122ff642017-05-05 10:52:39 +00001039 return Err;
1040
1041 UncompressedSections.emplace_back(std::move(Out));
1042 Data = UncompressedSections.back();
1043
1044 return Error::success();
1045}
1046
Keno Fischerc780e8e2015-05-21 21:24:32 +00001047DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
David Blaikie15d85fc2017-05-23 06:48:53 +00001048 const LoadedObjectInfo *L)
1049 : FileName(Obj.getFileName()), IsLittleEndian(Obj.isLittleEndian()),
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +00001050 AddressSize(Obj.getBytesInAddress()) {
1051 for (const SectionRef &Section : Obj.sections()) {
Eric Christopher7370b552012-11-12 21:40:38 +00001052 StringRef name;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001053 Section.getName(name);
David Majnemerdac39852014-09-26 22:32:16 +00001054 // Skip BSS and Virtual sections, they aren't interesting.
Rafael Espindola80291272014-10-08 15:28:58 +00001055 bool IsBSS = Section.isBSS();
David Majnemerdac39852014-09-26 22:32:16 +00001056 if (IsBSS)
1057 continue;
Rafael Espindola80291272014-10-08 15:28:58 +00001058 bool IsVirtual = Section.isVirtual();
David Majnemerdac39852014-09-26 22:32:16 +00001059 if (IsVirtual)
1060 continue;
Eric Christopher7370b552012-11-12 21:40:38 +00001061 StringRef data;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001062
Lang Hames2e88f4f2015-07-28 17:52:11 +00001063 section_iterator RelocatedSection = Section.getRelocatedSection();
Keno Fischerc780e8e2015-05-21 21:24:32 +00001064 // Try to obtain an already relocated version of this section.
1065 // Else use the unrelocated section from the object file. We'll have to
1066 // apply relocations ourselves later.
Lang Hames2e88f4f2015-07-28 17:52:11 +00001067 if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
Keno Fischerc780e8e2015-05-21 21:24:32 +00001068 Section.getContents(data);
Eric Christopher7370b552012-11-12 21:40:38 +00001069
George Rimar2122ff642017-05-05 10:52:39 +00001070 if (auto Err = maybeDecompress(Section, name, data)) {
1071 errs() << "error: failed to decompress '" + name + "', " +
1072 toString(std::move(Err))
1073 << '\n';
1074 continue;
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001075 }
1076
George Rimar4bf30832017-01-11 15:26:41 +00001077 // Compressed sections names in GNU style starts from ".z",
1078 // at this point section is decompressed and we drop compression prefix.
1079 name = name.substr(
1080 name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1081
Chris Bieneman2e752db2017-01-20 19:03:14 +00001082 if (StringRef *SectionData = MapSectionToMember(name)) {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001083 *SectionData = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001084 if (name == "debug_ranges") {
1085 // FIXME: Use the other dwo range section when we emit it.
George Rimarca532112017-04-24 10:19:45 +00001086 RangeDWOSection.Data = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001087 }
David Blaikie03c089c2013-09-23 22:44:47 +00001088 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +00001089 // Find debug_types data by section rather than name as there are
1090 // multiple, comdat grouped, debug_types sections.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001091 TypesSections[Section].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +00001092 } else if (name == "debug_types.dwo") {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001093 TypesDWOSections[Section].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +00001094 }
Eric Christopher7370b552012-11-12 21:40:38 +00001095
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +00001096 if (RelocatedSection == Obj.section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +00001097 continue;
1098
1099 StringRef RelSecName;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001100 StringRef RelSecData;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001101 RelocatedSection->getName(RelSecName);
Keno Fischerc780e8e2015-05-21 21:24:32 +00001102
1103 // If the section we're relocating was relocated already by the JIT,
1104 // then we used the relocated version above, so we do not need to process
1105 // relocations for it now.
Lang Hames2e88f4f2015-07-28 17:52:11 +00001106 if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
Keno Fischerc780e8e2015-05-21 21:24:32 +00001107 continue;
1108
Frederic Riss7bb12262015-08-23 04:44:21 +00001109 // In Mach-o files, the relocations do not need to be applied if
1110 // there is no load offset to apply. The value read at the
1111 // relocation point already factors in the section address
1112 // (actually applying the relocations will produce wrong results
1113 // as the section address will be added twice).
Craig Topper66059c92015-11-18 07:07:59 +00001114 if (!L && isa<MachOObjectFile>(&Obj))
Frederic Riss7bb12262015-08-23 04:44:21 +00001115 continue;
1116
Rafael Espindola4f60a382013-05-30 03:05:14 +00001117 RelSecName = RelSecName.substr(
1118 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
1119
Andrew Kaylord55d7012013-01-25 22:50:58 +00001120 // TODO: Add support for relocations in other sections as needed.
1121 // Record relocations for the debug_info and debug_line sections.
David Blaikied2f3a942017-05-22 07:02:47 +00001122 RelocAddrMap *Map =
1123 StringSwitch<RelocAddrMap *>(RelSecName)
1124 .Case("debug_info", &InfoSection.Relocs)
1125 .Case("debug_loc", &LocSection.Relocs)
1126 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
1127 .Case("debug_line", &LineSection.Relocs)
1128 .Case("debug_ranges", &RangeSection.Relocs)
1129 .Case("debug_addr", &AddrSection.Relocs)
1130 .Case("apple_names", &AppleNamesSection.Relocs)
1131 .Case("apple_types", &AppleTypesSection.Relocs)
1132 .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
1133 .Case("apple_namespac", &AppleNamespacesSection.Relocs)
1134 .Case("apple_objc", &AppleObjCSection.Relocs)
1135 .Default(nullptr);
David Blaikie03c089c2013-09-23 22:44:47 +00001136 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +00001137 // Find debug_types relocs by section rather than name as there are
1138 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +00001139 if (RelSecName == "debug_types")
1140 Map = &TypesSections[*RelocatedSection].Relocs;
1141 else if (RelSecName == "debug_types.dwo")
1142 Map = &TypesDWOSections[*RelocatedSection].Relocs;
1143 else
1144 continue;
David Blaikie03c089c2013-09-23 22:44:47 +00001145 }
Eric Christopher7370b552012-11-12 21:40:38 +00001146
George Rimarfed9f092017-05-17 12:10:51 +00001147 if (Section.relocation_begin() == Section.relocation_end())
1148 continue;
1149
George Rimar958b01a2017-05-15 11:45:28 +00001150 std::map<SymbolRef, uint64_t> AddrCache;
George Rimarfed9f092017-05-17 12:10:51 +00001151 for (const RelocationRef &Reloc : Section.relocations()) {
1152 // FIXME: it's not clear how to correctly handle scattered
1153 // relocations.
1154 if (isRelocScattered(Obj, Reloc))
1155 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001156
George Rimarfed9f092017-05-17 12:10:51 +00001157 Expected<uint64_t> SymAddrOrErr =
1158 getSymbolAddress(Obj, Reloc, L, AddrCache);
1159 if (!SymAddrOrErr) {
1160 errs() << toString(SymAddrOrErr.takeError()) << '\n';
1161 continue;
Eric Christopher7370b552012-11-12 21:40:38 +00001162 }
George Rimarfed9f092017-05-17 12:10:51 +00001163
1164 object::RelocVisitor V(Obj);
George Rimar47f84b12017-05-18 08:25:11 +00001165 uint64_t Val = V.visit(Reloc.getType(), Reloc, *SymAddrOrErr);
George Rimarfed9f092017-05-17 12:10:51 +00001166 if (V.error()) {
1167 SmallString<32> Name;
1168 Reloc.getTypeName(Name);
1169 errs() << "error: failed to compute relocation: " << Name << "\n";
1170 continue;
1171 }
George Rimar47f84b12017-05-18 08:25:11 +00001172 Map->insert({Reloc.getOffset(), {Val}});
Eric Christopher7370b552012-11-12 21:40:38 +00001173 }
1174 }
1175}
1176
Chris Bieneman2e752db2017-01-20 19:03:14 +00001177DWARFContextInMemory::DWARFContextInMemory(
1178 const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize,
1179 bool isLittleEndian)
1180 : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) {
1181 for (const auto &SecIt : Sections) {
1182 if (StringRef *SectionData = MapSectionToMember(SecIt.first()))
1183 *SectionData = SecIt.second->getBuffer();
1184 }
1185}
1186
1187StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) {
1188 return StringSwitch<StringRef *>(Name)
1189 .Case("debug_info", &InfoSection.Data)
1190 .Case("debug_abbrev", &AbbrevSection)
1191 .Case("debug_loc", &LocSection.Data)
1192 .Case("debug_line", &LineSection.Data)
1193 .Case("debug_aranges", &ARangeSection)
1194 .Case("debug_frame", &DebugFrameSection)
1195 .Case("eh_frame", &EHFrameSection)
1196 .Case("debug_str", &StringSection)
George Rimarca532112017-04-24 10:19:45 +00001197 .Case("debug_ranges", &RangeSection.Data)
Chris Bieneman2e752db2017-01-20 19:03:14 +00001198 .Case("debug_macinfo", &MacinfoSection)
1199 .Case("debug_pubnames", &PubNamesSection)
1200 .Case("debug_pubtypes", &PubTypesSection)
1201 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1202 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1203 .Case("debug_info.dwo", &InfoDWOSection.Data)
1204 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1205 .Case("debug_loc.dwo", &LocDWOSection.Data)
1206 .Case("debug_line.dwo", &LineDWOSection.Data)
1207 .Case("debug_str.dwo", &StringDWOSection)
1208 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
David Blaikied2f3a942017-05-22 07:02:47 +00001209 .Case("debug_addr", &AddrSection.Data)
Chris Bieneman2e752db2017-01-20 19:03:14 +00001210 .Case("apple_names", &AppleNamesSection.Data)
1211 .Case("apple_types", &AppleTypesSection.Data)
1212 .Case("apple_namespaces", &AppleNamespacesSection.Data)
1213 .Case("apple_namespac", &AppleNamespacesSection.Data)
1214 .Case("apple_objc", &AppleObjCSection.Data)
1215 .Case("debug_cu_index", &CUIndexSection)
1216 .Case("debug_tu_index", &TUIndexSection)
1217 .Case("gdb_index", &GdbIndexSection)
1218 // Any more debug info sections go here.
1219 .Default(nullptr);
1220}
1221
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001222void DWARFContextInMemory::anchor() {}