blob: 1be156d6ea9b7f126f07c28d2ff140531ca91ad6 [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,
George Rimara25d3292017-05-27 18:10:23 +000063 uint32_t *Off, const RelocAddrMap *Relocs,
64 uint64_t *SectionIndex) {
George Rimarf8a96422017-04-21 09:12:18 +000065 if (!Relocs)
66 return Data.getUnsigned(Off, Size);
67 RelocAddrMap::const_iterator AI = Relocs->find(*Off);
68 if (AI == Relocs->end())
69 return Data.getUnsigned(Off, Size);
George Rimara25d3292017-05-27 18:10:23 +000070 if (SectionIndex)
71 *SectionIndex = AI->second.SectionIndex;
George Rimar41e65672017-05-16 14:05:45 +000072 return Data.getUnsigned(Off, Size) + AI->second.Value;
George Rimarf8a96422017-04-21 09:12:18 +000073}
74
Frederic Riss7c500472014-11-14 19:30:08 +000075static void dumpAccelSection(raw_ostream &OS, StringRef Name,
76 const DWARFSection& Section, StringRef StringSection,
77 bool LittleEndian) {
78 DataExtractor AccelSection(Section.Data, LittleEndian, 0);
Frederic Risse837ec22014-11-14 16:15:53 +000079 DataExtractor StrData(StringSection, LittleEndian, 0);
80 OS << "\n." << Name << " contents:\n";
Frederic Riss7c500472014-11-14 19:30:08 +000081 DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
Frederic Risse837ec22014-11-14 16:15:53 +000082 if (!Accel.extract())
83 return;
84 Accel.dump(OS);
85}
86
Adrian Prantlf4bc1f72017-06-01 18:18:23 +000087void DWARFContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts){
88
89 DIDumpType DumpType = DumpOpts.DumpType;
90 bool DumpEH = DumpOpts.DumpEH;
91 bool SummarizeTypes = DumpOpts.SummarizeTypes;
92
Eli Bendersky7a94daa2013-01-25 20:26:43 +000093 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
94 OS << ".debug_abbrev contents:\n";
95 getDebugAbbrev()->dump(OS);
96 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000097
David Blaikie66865d62014-01-09 00:13:35 +000098 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
99 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +0000100 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +0000101 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +0000102 }
David Blaikie622dce42014-01-08 23:29:59 +0000103
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000104 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
105 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000106 for (const auto &CU : compile_units())
107 CU->dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000108 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000109
David Blaikie66865d62014-01-09 00:13:35 +0000110 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
111 getNumDWOCompileUnits()) {
112 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000113 for (const auto &DWOCU : dwo_compile_units())
114 DWOCU->dump(OS);
David Blaikie66865d62014-01-09 00:13:35 +0000115 }
David Blaikie622dce42014-01-08 23:29:59 +0000116
David Blaikie66865d62014-01-09 00:13:35 +0000117 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000118 OS << "\n.debug_types contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000119 for (const auto &TUS : type_unit_sections())
120 for (const auto &TU : TUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000121 TU->dump(OS, SummarizeTypes);
David Blaikie03c089c2013-09-23 22:44:47 +0000122 }
123
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000124 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
125 getNumDWOTypeUnits()) {
126 OS << "\n.debug_types.dwo contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000127 for (const auto &DWOTUS : dwo_type_unit_sections())
128 for (const auto &DWOTU : DWOTUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000129 DWOTU->dump(OS, SummarizeTypes);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000130 }
David Blaikie92d9d622014-01-09 05:08:24 +0000131
David Blaikie18e73502013-06-19 21:37:13 +0000132 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +0000133 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +0000134 getDebugLoc()->dump(OS);
135 }
136
David Blaikie9c550ac2014-03-25 01:44:02 +0000137 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
138 OS << "\n.debug_loc.dwo contents:\n";
139 getDebugLocDWO()->dump(OS);
140 }
141
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000142 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
143 OS << "\n.debug_frame contents:\n";
144 getDebugFrame()->dump(OS);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000145 if (DumpEH) {
146 OS << "\n.eh_frame contents:\n";
147 getEHFrame()->dump(OS);
148 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000149 }
150
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000151 if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
152 OS << "\n.debug_macinfo contents:\n";
153 getDebugMacro()->dump(OS);
154 }
155
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000156 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000157 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
158 OS << "\n.debug_aranges contents:\n";
159 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
160 DWARFDebugArangeSet set;
161 while (set.extract(arangesData, &offset))
162 set.dump(OS);
163 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000164
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000165 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000166 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
167 OS << "\n.debug_line contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000168 for (const auto &CU : compile_units()) {
169 savedAddressByteSize = CU->getAddressByteSize();
Greg Claytonc8c10322016-12-13 18:25:19 +0000170 auto CUDIE = CU->getUnitDIE();
171 if (!CUDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000172 continue;
Greg Clayton97d22182017-01-13 21:08:18 +0000173 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000174 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000175 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000176 DWARFDebugLine::LineTable LineTable;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000177 uint32_t Offset = *StmtOffset;
178 LineTable.parse(lineData, &getLineSection().Relocs, &Offset);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000179 LineTable.dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000180 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000181 }
182 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000183
David Blaikie65a8efe2015-11-11 19:28:21 +0000184 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
185 OS << "\n.debug_cu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000186 getCUIndex().dump(OS);
David Blaikie65a8efe2015-11-11 19:28:21 +0000187 }
188
David Blaikie51c40282015-11-11 19:40:49 +0000189 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
190 OS << "\n.debug_tu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000191 getTUIndex().dump(OS);
David Blaikie51c40282015-11-11 19:40:49 +0000192 }
193
David Blaikie1d4736e2014-02-24 23:58:54 +0000194 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
195 OS << "\n.debug_line.dwo contents:\n";
196 unsigned stmtOffset = 0;
197 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
198 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000199 DWARFDebugLine::LineTable LineTable;
200 while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
201 LineTable.dump(OS);
202 LineTable.clear();
203 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000204 }
205
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000206 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
207 OS << "\n.debug_str contents:\n";
208 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
209 offset = 0;
210 uint32_t strOffset = 0;
211 while (const char *s = strData.getCStr(&offset)) {
212 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
213 strOffset = offset;
214 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000215 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000216
David Blaikie66865d62014-01-09 00:13:35 +0000217 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
218 !getStringDWOSection().empty()) {
219 OS << "\n.debug_str.dwo contents:\n";
220 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
221 offset = 0;
222 uint32_t strDWOOffset = 0;
223 while (const char *s = strDWOData.getCStr(&offset)) {
224 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
225 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000226 }
David Blaikie66865d62014-01-09 00:13:35 +0000227 }
David Blaikie622dce42014-01-08 23:29:59 +0000228
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000229 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
230 OS << "\n.debug_ranges contents:\n";
231 // In fact, different compile units may have different address byte
232 // sizes, but for simplicity we just use the address byte size of the last
233 // compile unit (there is no easy and fast way to associate address range
234 // list and the compile unit it describes).
George Rimarca532112017-04-24 10:19:45 +0000235 DataExtractor rangesData(getRangeSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000236 savedAddressByteSize);
237 offset = 0;
238 DWARFDebugRangeList rangeList;
George Rimarca532112017-04-24 10:19:45 +0000239 while (rangeList.extract(rangesData, &offset, getRangeSection().Relocs))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000240 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000241 }
Eric Christopher962c9082013-01-15 23:56:56 +0000242
Eric Christopher0de53592013-09-25 23:02:36 +0000243 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000244 DWARFDebugPubTable(getPubNamesSection(), isLittleEndian(), false)
245 .dump("debug_pubnames", OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000246
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000247 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000248 DWARFDebugPubTable(getPubTypesSection(), isLittleEndian(), false)
249 .dump("debug_pubtypes", OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000250
David Blaikieecd21ff2013-09-24 19:50:00 +0000251 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000252 DWARFDebugPubTable(getGnuPubNamesSection(), isLittleEndian(),
253 true /* GnuStyle */)
254 .dump("debug_gnu_pubnames", OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000255
256 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000257 DWARFDebugPubTable(getGnuPubTypesSection(), isLittleEndian(),
258 true /* GnuStyle */)
259 .dump("debug_gnu_pubtypes", OS);
David Blaikie404d3042013-09-19 23:01:29 +0000260
David Blaikie66865d62014-01-09 00:13:35 +0000261 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
262 !getStringOffsetDWOSection().empty()) {
263 OS << "\n.debug_str_offsets.dwo contents:\n";
264 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
265 0);
266 offset = 0;
267 uint64_t size = getStringOffsetDWOSection().size();
268 while (offset < size) {
269 OS << format("0x%8.8x: ", offset);
270 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000271 }
David Blaikie66865d62014-01-09 00:13:35 +0000272 }
Frederic Risse837ec22014-11-14 16:15:53 +0000273
George Rimar4f82df52016-09-23 11:01:53 +0000274 if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) &&
275 !getGdbIndexSection().empty()) {
276 OS << "\n.gnu_index contents:\n";
277 getGdbIndex().dump(OS);
278 }
279
Frederic Risse837ec22014-11-14 16:15:53 +0000280 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
281 dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
282 getStringSection(), isLittleEndian());
283
284 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
285 dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
286 getStringSection(), isLittleEndian());
287
288 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
289 dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
290 getStringSection(), isLittleEndian());
291
292 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
293 dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
294 getStringSection(), isLittleEndian());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000295}
296
David Blaikie15d85fc2017-05-23 06:48:53 +0000297DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
298 // FIXME: Improve this for the case where this DWO file is really a DWP file
299 // with an index - use the index for lookup instead of a linear search.
300 for (const auto &DWOCU : dwo_compile_units())
301 if (DWOCU->getDWOId() == Hash)
302 return DWOCU.get();
303 return nullptr;
304}
305
Greg Claytonc7695a82017-05-02 20:28:33 +0000306DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
307 parseCompileUnits();
308 if (auto *CU = CUs.getUnitForOffset(Offset))
309 return CU->getDIEForOffset(Offset);
310 return DWARFDie();
311}
312
313namespace {
314
315class Verifier {
316 raw_ostream &OS;
317 DWARFContext &DCtx;
318public:
319 Verifier(raw_ostream &S, DWARFContext &D) : OS(S), DCtx(D) {}
320
321 bool HandleDebugInfo() {
322 bool Success = true;
323 // A map that tracks all references (converted absolute references) so we
324 // can verify each reference points to a valid DIE and not an offset that
325 // lies between to valid DIEs.
326 std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
327
Greg Clayton48432cf2017-05-01 22:07:02 +0000328 OS << "Verifying .debug_info...\n";
Greg Claytonc7695a82017-05-02 20:28:33 +0000329 for (const auto &CU : DCtx.compile_units()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000330 unsigned NumDies = CU->getNumDIEs();
331 for (unsigned I = 0; I < NumDies; ++I) {
332 auto Die = CU->getDIEAtIndex(I);
333 const auto Tag = Die.getTag();
334 if (Tag == DW_TAG_null)
335 continue;
336 for (auto AttrValue : Die.attributes()) {
337 const auto Attr = AttrValue.Attr;
338 const auto Form = AttrValue.Value.getForm();
339 switch (Attr) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000340 case DW_AT_ranges:
341 // Make sure the offset in the DW_AT_ranges attribute is valid.
342 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
343 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
344 Success = false;
345 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
346 "bounds:\n";
347 Die.dump(OS, 0);
348 OS << "\n";
349 }
350 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000351 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000352 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000353 Die.dump(OS, 0);
354 OS << "\n";
355 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000356 break;
357 case DW_AT_stmt_list:
358 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
359 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
360 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
361 Success = false;
362 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
363 "bounds: "
364 << format("0x%08" PRIx32, *SectionOffset) << "\n";
365 CU->getUnitDIE().dump(OS, 0);
366 OS << "\n";
367 }
368 } else {
Greg Clayton48432cf2017-05-01 22:07:02 +0000369 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000370 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
371 Die.dump(OS, 0);
Greg Clayton48432cf2017-05-01 22:07:02 +0000372 OS << "\n";
373 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000374 break;
375
376 default:
377 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000378 }
379 switch (Form) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000380 case DW_FORM_ref1:
381 case DW_FORM_ref2:
382 case DW_FORM_ref4:
383 case DW_FORM_ref8:
384 case DW_FORM_ref_udata: {
385 // Verify all CU relative references are valid CU offsets.
386 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
387 assert(RefVal);
388 if (RefVal) {
389 auto DieCU = Die.getDwarfUnit();
390 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
391 auto CUOffset = AttrValue.Value.getRawUValue();
392 if (CUOffset >= CUSize) {
393 Success = false;
394 OS << "error: " << FormEncodingString(Form) << " CU offset "
395 << format("0x%08" PRIx32, CUOffset)
396 << " is invalid (must be less than CU size of "
397 << format("0x%08" PRIx32, CUSize) << "):\n";
398 Die.dump(OS, 0);
399 OS << "\n";
400 } else {
401 // Valid reference, but we will verify it points to an actual
402 // DIE later.
403 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
404 }
405 }
406 break;
407 }
408 case DW_FORM_ref_addr: {
409 // Verify all absolute DIE references have valid offsets in the
410 // .debug_info section.
411 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
412 assert(RefVal);
413 if (RefVal) {
414 if(*RefVal >= DCtx.getInfoSection().Data.size()) {
415 Success = false;
416 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
417 "bounds:\n";
418 Die.dump(OS, 0);
419 OS << "\n";
420 } else {
421 // Valid reference, but we will verify it points to an actual
422 // DIE later.
423 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
424 }
425 }
426 break;
427 }
428 case DW_FORM_strp: {
429 auto SecOffset = AttrValue.Value.getAsSectionOffset();
430 assert(SecOffset); // DW_FORM_strp is a section offset.
431 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000432 Success = false;
Greg Claytonc7695a82017-05-02 20:28:33 +0000433 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
Greg Clayton48432cf2017-05-01 22:07:02 +0000434 Die.dump(OS, 0);
435 OS << "\n";
436 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000437 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000438 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000439 default:
440 break;
Greg Clayton48432cf2017-05-01 22:07:02 +0000441 }
442 }
443 }
444 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000445
446 // Take all references and make sure they point to an actual DIE by
447 // getting the DIE by offset and emitting an error
448 OS << "Verifying .debug_info references...\n";
449 for (auto Pair: ReferenceToDIEOffsets) {
450 auto Die = DCtx.getDIEForOffset(Pair.first);
451 if (Die)
452 continue;
453 Success = false;
454 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
455 << ". Offset is in between DIEs:\n";
456 for (auto Offset: Pair.second) {
457 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
458 ReferencingDie.dump(OS, 0);
459 OS << "\n";
460 }
461 OS << "\n";
462 }
463 return Success;
464 }
Greg Clayton67070462017-05-02 22:48:52 +0000465
466 bool HandleDebugLine() {
Greg Clayton8df55b42017-05-03 15:45:31 +0000467 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Clayton67070462017-05-02 22:48:52 +0000468 bool Success = true;
469 OS << "Verifying .debug_line...\n";
470 for (const auto &CU : DCtx.compile_units()) {
471 uint32_t LineTableOffset = 0;
Greg Clayton8df55b42017-05-03 15:45:31 +0000472 auto CUDie = CU->getUnitDIE();
473 auto StmtFormValue = CUDie.find(DW_AT_stmt_list);
Greg Clayton67070462017-05-02 22:48:52 +0000474 if (!StmtFormValue) {
475 // No line table for this compile unit.
476 continue;
477 }
478 // Get the attribute value as a section offset. No need to produce an
479 // error here if the encoding isn't correct because we validate this in
480 // the .debug_info verifier.
481 if (auto StmtSectionOffset = toSectionOffset(StmtFormValue)) {
482 LineTableOffset = *StmtSectionOffset;
483 if (LineTableOffset >= DCtx.getLineSection().Data.size()) {
484 // Make sure we don't get a valid line table back if the offset
485 // is wrong.
486 assert(DCtx.getLineTableForUnit(CU.get()) == nullptr);
487 // Skip this line table as it isn't valid. No need to create an error
488 // here because we validate this in the .debug_info verifier.
489 continue;
Greg Clayton8df55b42017-05-03 15:45:31 +0000490 } else {
491 auto Iter = StmtListToDie.find(LineTableOffset);
492 if (Iter != StmtListToDie.end()) {
493 Success = false;
494 OS << "error: two compile unit DIEs, "
495 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
496 << format("0x%08" PRIx32, CUDie.getOffset())
497 << ", have the same DW_AT_stmt_list section offset:\n";
498 Iter->second.dump(OS, 0);
499 CUDie.dump(OS, 0);
500 OS << '\n';
501 // Already verified this line table before, no need to do it again.
502 continue;
503 }
504 StmtListToDie[LineTableOffset] = CUDie;
Greg Clayton67070462017-05-02 22:48:52 +0000505 }
506 }
507 auto LineTable = DCtx.getLineTableForUnit(CU.get());
508 if (!LineTable) {
509 Success = false;
510 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
511 << "] was not able to be parsed for CU:\n";
Greg Clayton8df55b42017-05-03 15:45:31 +0000512 CUDie.dump(OS, 0);
Greg Clayton67070462017-05-02 22:48:52 +0000513 OS << '\n';
514 continue;
515 }
516 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
517 uint64_t PrevAddress = 0;
518 uint32_t RowIndex = 0;
519 for (const auto &Row : LineTable->Rows) {
520 if (Row.Address < PrevAddress) {
521 Success = false;
522 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
523 << "] row[" << RowIndex
524 << "] decreases in address from previous row:\n";
525
526 DWARFDebugLine::Row::dumpTableHeader(OS);
527 if (RowIndex > 0)
528 LineTable->Rows[RowIndex - 1].dump(OS);
529 Row.dump(OS);
530 OS << '\n';
531 }
532
533 if (Row.File > MaxFileIndex) {
534 Success = false;
535 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
536 << "][" << RowIndex << "] has invalid file index " << Row.File
537 << " (valid values are [1," << MaxFileIndex << "]):\n";
538 DWARFDebugLine::Row::dumpTableHeader(OS);
539 Row.dump(OS);
540 OS << '\n';
541 }
542 if (Row.EndSequence)
543 PrevAddress = 0;
544 else
545 PrevAddress = Row.Address;
546 ++RowIndex;
547 }
548 }
549 return Success;
550 }
Greg Claytonc7695a82017-05-02 20:28:33 +0000551};
552
553} // anonymous namespace
554
555bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
556 bool Success = true;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000557 DWARFVerifier verifier(OS, *this);
Greg Claytonc7695a82017-05-02 20:28:33 +0000558 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000559 if (!verifier.handleDebugInfo())
Greg Claytonc7695a82017-05-02 20:28:33 +0000560 Success = false;
Greg Clayton48432cf2017-05-01 22:07:02 +0000561 }
Greg Clayton67070462017-05-02 22:48:52 +0000562 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000563 if (!verifier.handleDebugLine())
Greg Clayton67070462017-05-02 22:48:52 +0000564 Success = false;
565 }
Greg Clayton48432cf2017-05-01 22:07:02 +0000566 return Success;
567}
David Blaikie82641be2015-11-17 00:39:55 +0000568const DWARFUnitIndex &DWARFContext::getCUIndex() {
569 if (CUIndex)
570 return *CUIndex;
571
572 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
573
David Blaikieb073cb92015-12-02 06:21:34 +0000574 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000575 CUIndex->parse(CUIndexData);
576 return *CUIndex;
577}
578
579const DWARFUnitIndex &DWARFContext::getTUIndex() {
580 if (TUIndex)
581 return *TUIndex;
582
583 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
584
David Blaikieb073cb92015-12-02 06:21:34 +0000585 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000586 TUIndex->parse(TUIndexData);
587 return *TUIndex;
588}
589
George Rimar4f82df52016-09-23 11:01:53 +0000590DWARFGdbIndex &DWARFContext::getGdbIndex() {
591 if (GdbIndex)
592 return *GdbIndex;
593
594 DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0);
595 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
596 GdbIndex->parse(GdbIndexData);
597 return *GdbIndex;
598}
599
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000600const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
601 if (Abbrev)
602 return Abbrev.get();
603
604 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
605
606 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000607 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000608 return Abbrev.get();
609}
610
Eric Christopherda4b2192013-01-02 23:52:13 +0000611const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
612 if (AbbrevDWO)
613 return AbbrevDWO.get();
614
615 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
616 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000617 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000618 return AbbrevDWO.get();
619}
620
David Blaikie18e73502013-06-19 21:37:13 +0000621const DWARFDebugLoc *DWARFContext::getDebugLoc() {
622 if (Loc)
623 return Loc.get();
624
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000625 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
626 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000627 // assume all compile units have the same address byte size
628 if (getNumCompileUnits())
629 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
630 return Loc.get();
631}
632
David Blaikie9c550ac2014-03-25 01:44:02 +0000633const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
634 if (LocDWO)
635 return LocDWO.get();
636
637 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
638 LocDWO.reset(new DWARFDebugLocDWO());
639 LocDWO->parse(LocData);
640 return LocDWO.get();
641}
642
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000643const DWARFDebugAranges *DWARFContext::getDebugAranges() {
644 if (Aranges)
645 return Aranges.get();
646
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000647 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000648 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000649 return Aranges.get();
650}
651
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000652const DWARFDebugFrame *DWARFContext::getDebugFrame() {
653 if (DebugFrame)
654 return DebugFrame.get();
655
656 // There's a "bug" in the DWARFv3 standard with respect to the target address
657 // size within debug frame sections. While DWARF is supposed to be independent
658 // of its container, FDEs have fields with size being "target address size",
659 // which isn't specified in DWARF in general. It's only specified for CUs, but
660 // .eh_frame can appear without a .debug_info section. Follow the example of
661 // other tools (libdwarf) and extract this from the container (ObjectFile
662 // provides this information). This problem is fixed in DWARFv4
663 // See this dwarf-discuss discussion for more details:
664 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
665 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
666 getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000667 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
668 DebugFrame->parse(debugFrameData);
669 return DebugFrame.get();
670}
671
672const DWARFDebugFrame *DWARFContext::getEHFrame() {
673 if (EHFrame)
674 return EHFrame.get();
675
676 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
677 getAddressSize());
678 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000679 DebugFrame->parse(debugFrameData);
680 return DebugFrame.get();
681}
682
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000683const DWARFDebugMacro *DWARFContext::getDebugMacro() {
684 if (Macro)
685 return Macro.get();
686
687 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
688 Macro.reset(new DWARFDebugMacro());
689 Macro->parse(MacinfoData);
690 return Macro.get();
691}
692
Eric Christopher494109b2012-10-16 23:46:25 +0000693const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000694DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000695 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000696 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
David Blaikiec4e2bed2015-11-17 21:08:05 +0000697
Greg Claytonc8c10322016-12-13 18:25:19 +0000698 auto UnitDIE = U->getUnitDIE();
699 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000700 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000701
Greg Clayton97d22182017-01-13 21:08:18 +0000702 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000703 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000704 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000705
Greg Clayton52fe1f62016-12-14 22:38:08 +0000706 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000707 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000708 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000709 return lt;
710
Greg Clayton48ff66a2017-05-04 18:29:44 +0000711 // Make sure the offset is good before we try to parse.
712 if (stmtOffset >= U->getLineSection().size())
713 return nullptr;
714
Benjamin Kramer679e1752011-09-15 20:43:18 +0000715 // We have to parse it first.
David Blaikiec4e2bed2015-11-17 21:08:05 +0000716 DataExtractor lineData(U->getLineSection(), isLittleEndian(),
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000717 U->getAddressByteSize());
Benjamin Kramer679e1752011-09-15 20:43:18 +0000718 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000719}
720
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000721void DWARFContext::parseCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000722 CUs.parse(*this, getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000723}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000724
David Blaikie03c089c2013-09-23 22:44:47 +0000725void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000726 if (!TUs.empty())
727 return;
728 for (const auto &I : getTypesSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000729 TUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000730 TUs.back().parse(*this, I.second);
David Blaikie03c089c2013-09-23 22:44:47 +0000731 }
732}
733
Eric Christopherda4b2192013-01-02 23:52:13 +0000734void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000735 DWOCUs.parseDWO(*this, getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000736}
737
David Blaikie92d9d622014-01-09 05:08:24 +0000738void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000739 if (!DWOTUs.empty())
740 return;
741 for (const auto &I : getTypesDWOSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000742 DWOTUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000743 DWOTUs.back().parseDWO(*this, I.second);
David Blaikie92d9d622014-01-09 05:08:24 +0000744 }
745}
746
Alexey Samsonov45be7932012-08-30 07:49:50 +0000747DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000748 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000749 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000750}
751
Alexey Samsonov45be7932012-08-30 07:49:50 +0000752DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000753 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000754 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000755 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000756 return getCompileUnitForOffset(CUOffset);
757}
758
David Blaikieefc4eba2017-02-06 20:19:02 +0000759static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
760 uint64_t Address,
761 FunctionNameKind Kind,
762 std::string &FunctionName,
763 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000764 // The address may correspond to instruction in some inlined function,
765 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000766 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000767 SmallVector<DWARFDie, 4> InlinedChain;
768 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000769 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000770 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000771
772 const DWARFDie &DIE = InlinedChain[0];
773 bool FoundResult = false;
774 const char *Name = nullptr;
775 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000776 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000777 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000778 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000779 if (auto DeclLineResult = DIE.getDeclLine()) {
780 StartLine = DeclLineResult;
781 FoundResult = true;
782 }
783
784 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000785}
786
Alexey Samsonov45be7932012-08-30 07:49:50 +0000787DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000788 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000789 DILineInfo Result;
790
Alexey Samsonov45be7932012-08-30 07:49:50 +0000791 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
792 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000793 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000794 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
795 Result.FunctionName,
796 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000797 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000798 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
799 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
800 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000801 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000802 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000803}
David Blaikiea379b1812011-12-20 02:50:00 +0000804
Alexey Samsonovdce67342014-05-15 21:24:32 +0000805DILineInfoTable
806DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
807 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000808 DILineInfoTable Lines;
809 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
810 if (!CU)
811 return Lines;
812
813 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000814 uint32_t StartLine = 0;
815 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
816 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000817
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000818 // If the Specifier says we don't need FileLineInfo, just
819 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000820 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000821 DILineInfo Result;
822 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000823 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000824 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000825 return Lines;
826 }
827
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000828 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000829
830 // Get the index of row we're looking for in the line table.
831 std::vector<uint32_t> RowVector;
832 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
833 return Lines;
834
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000835 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000836 // Take file number and line/column from the row.
837 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000838 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000839 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
840 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000841 Result.FunctionName = FunctionName;
842 Result.Line = Row.Line;
843 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000844 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000845 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000846 }
847
848 return Lines;
849}
850
Alexey Samsonovdce67342014-05-15 21:24:32 +0000851DIInliningInfo
852DWARFContext::getInliningInfoForAddress(uint64_t Address,
853 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000854 DIInliningInfo InliningInfo;
855
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000856 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
857 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000858 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000859
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000860 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000861 SmallVector<DWARFDie, 4> InlinedChain;
862 CU->getInlinedChainForAddress(Address, InlinedChain);
863 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000864 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
865 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000866 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000867 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000868 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000869 if (LineTable &&
870 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
871 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000872 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000873 }
874 return InliningInfo;
875 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000876
Dehao Chenef700d52017-04-17 20:10:39 +0000877 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000878 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
879 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000880 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000881 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000882 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000883 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000884 if (auto DeclLineResult = FunctionDIE.getDeclLine())
885 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000886 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000887 if (i == 0) {
888 // For the topmost frame, initialize the line table of this
889 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000890 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000891 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000892 if (LineTable)
893 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
894 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000895 } else {
896 // Otherwise, use call file, call line and call column from
897 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000898 if (LineTable)
899 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
900 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000901 Frame.Line = CallLine;
902 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000903 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000904 }
905 // Get call file/line/column of a current DIE.
906 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +0000907 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
908 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000909 }
910 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000911 InliningInfo.addFrame(Frame);
912 }
913 return InliningInfo;
914}
915
David Blaikief9803fb2017-05-23 00:30:42 +0000916std::shared_ptr<DWARFContext>
917DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +0000918 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +0000919 DWARFContext *Ctxt = S->Context.get();
920 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
921 }
922
David Blaikie15d85fc2017-05-23 06:48:53 +0000923 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
924
925 if (auto S = Entry->lock()) {
926 DWARFContext *Ctxt = S->Context.get();
927 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
928 }
929
930 SmallString<128> DWPName;
931 Expected<OwningBinary<ObjectFile>> Obj = [&] {
932 if (!CheckedForDWP) {
933 (getFileName() + ".dwp").toVector(DWPName);
934 auto Obj = object::ObjectFile::createObjectFile(DWPName);
935 if (Obj) {
936 Entry = &DWP;
937 return Obj;
938 } else {
939 CheckedForDWP = true;
940 // TODO: Should this error be handled (maybe in a high verbosity mode)
941 // before falling back to .dwo files?
942 consumeError(Obj.takeError());
943 }
944 }
945
946 return object::ObjectFile::createObjectFile(AbsolutePath);
947 }();
948
David Blaikief9803fb2017-05-23 00:30:42 +0000949 if (!Obj) {
950 // TODO: Actually report errors helpfully.
951 consumeError(Obj.takeError());
952 return nullptr;
953 }
David Blaikie15d85fc2017-05-23 06:48:53 +0000954
955 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +0000956 S->File = std::move(Obj.get());
957 S->Context = llvm::make_unique<DWARFContextInMemory>(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +0000958 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +0000959 auto *Ctxt = S->Context.get();
960 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
961}
962
George Rimar702dac62017-04-12 08:59:15 +0000963static Error createError(const Twine &Reason, llvm::Error E) {
964 return make_error<StringError>(Reason + toString(std::move(E)),
965 inconvertibleErrorCode());
966}
967
George Rimara25d3292017-05-27 18:10:23 +0000968/// SymInfo contains information about symbol: it's address
969/// and section index which is -1LL for absolute symbols.
970struct SymInfo {
971 uint64_t Address;
972 uint64_t SectionIndex;
973};
974
975/// Returns the address of symbol relocation used against and a section index.
976/// Used for futher relocations computation. Symbol's section load address is
977static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
978 const RelocationRef &Reloc,
979 const LoadedObjectInfo *L,
980 std::map<SymbolRef, SymInfo> &Cache) {
981 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +0000982 object::section_iterator RSec = Obj.section_end();
983 object::symbol_iterator Sym = Reloc.getSymbol();
984
George Rimara25d3292017-05-27 18:10:23 +0000985 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +0000986 // First calculate the address of the symbol or section as it appears
987 // in the object file
988 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +0000989 bool New;
George Rimara25d3292017-05-27 18:10:23 +0000990 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +0000991 if (!New)
992 return CacheIt->second;
993
George Rimar702dac62017-04-12 08:59:15 +0000994 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
995 if (!SymAddrOrErr)
996 return createError("error: failed to compute symbol address: ",
997 SymAddrOrErr.takeError());
998
999 // Also remember what section this symbol is in for later
1000 auto SectOrErr = Sym->getSection();
1001 if (!SectOrErr)
1002 return createError("error: failed to get symbol section: ",
1003 SectOrErr.takeError());
1004
1005 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001006 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001007 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1008 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001009 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001010 }
1011
George Rimara25d3292017-05-27 18:10:23 +00001012 if (RSec != Obj.section_end())
1013 Ret.SectionIndex = RSec->getIndex();
1014
George Rimar702dac62017-04-12 08:59:15 +00001015 // If we are given load addresses for the sections, we need to adjust:
1016 // SymAddr = (Address of Symbol Or Section in File) -
1017 // (Address of Section in File) +
1018 // (Load Address of Section)
1019 // RSec is now either the section being targeted or the section
1020 // containing the symbol being targeted. In either case,
1021 // we need to perform the same computation.
1022 if (L && RSec != Obj.section_end())
1023 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001024 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001025
1026 if (CacheIt != Cache.end())
1027 CacheIt->second = Ret;
1028
George Rimar702dac62017-04-12 08:59:15 +00001029 return Ret;
1030}
1031
1032static bool isRelocScattered(const object::ObjectFile &Obj,
1033 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001034 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1035 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001036 return false;
1037 // MachO also has relocations that point to sections and
1038 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001039 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1040 return MachObj->isRelocationScattered(RelocInfo);
1041}
1042
George Rimar2122ff642017-05-05 10:52:39 +00001043Error DWARFContextInMemory::maybeDecompress(const SectionRef &Sec,
1044 StringRef Name, StringRef &Data) {
1045 if (!Decompressor::isCompressed(Sec))
1046 return Error::success();
1047
1048 Expected<Decompressor> Decompressor =
1049 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1050 if (!Decompressor)
1051 return Decompressor.takeError();
1052
1053 SmallString<32> Out;
George Rimarf98b9ac2017-05-18 08:00:01 +00001054 if (auto Err = Decompressor->resizeAndDecompress(Out))
George Rimar2122ff642017-05-05 10:52:39 +00001055 return Err;
1056
1057 UncompressedSections.emplace_back(std::move(Out));
1058 Data = UncompressedSections.back();
1059
1060 return Error::success();
1061}
1062
Keno Fischerc780e8e2015-05-21 21:24:32 +00001063DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
David Blaikie15d85fc2017-05-23 06:48:53 +00001064 const LoadedObjectInfo *L)
1065 : FileName(Obj.getFileName()), IsLittleEndian(Obj.isLittleEndian()),
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +00001066 AddressSize(Obj.getBytesInAddress()) {
1067 for (const SectionRef &Section : Obj.sections()) {
Eric Christopher7370b552012-11-12 21:40:38 +00001068 StringRef name;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001069 Section.getName(name);
David Majnemerdac39852014-09-26 22:32:16 +00001070 // Skip BSS and Virtual sections, they aren't interesting.
Rafael Espindola80291272014-10-08 15:28:58 +00001071 bool IsBSS = Section.isBSS();
David Majnemerdac39852014-09-26 22:32:16 +00001072 if (IsBSS)
1073 continue;
Rafael Espindola80291272014-10-08 15:28:58 +00001074 bool IsVirtual = Section.isVirtual();
David Majnemerdac39852014-09-26 22:32:16 +00001075 if (IsVirtual)
1076 continue;
Eric Christopher7370b552012-11-12 21:40:38 +00001077 StringRef data;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001078
Lang Hames2e88f4f2015-07-28 17:52:11 +00001079 section_iterator RelocatedSection = Section.getRelocatedSection();
Keno Fischerc780e8e2015-05-21 21:24:32 +00001080 // Try to obtain an already relocated version of this section.
1081 // Else use the unrelocated section from the object file. We'll have to
1082 // apply relocations ourselves later.
George Rimara25d3292017-05-27 18:10:23 +00001083 if (!L || !L->getLoadedSectionContents(*RelocatedSection, data))
Keno Fischerc780e8e2015-05-21 21:24:32 +00001084 Section.getContents(data);
Eric Christopher7370b552012-11-12 21:40:38 +00001085
George Rimar2122ff642017-05-05 10:52:39 +00001086 if (auto Err = maybeDecompress(Section, name, data)) {
1087 errs() << "error: failed to decompress '" + name + "', " +
1088 toString(std::move(Err))
1089 << '\n';
1090 continue;
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001091 }
1092
George Rimar4bf30832017-01-11 15:26:41 +00001093 // Compressed sections names in GNU style starts from ".z",
1094 // at this point section is decompressed and we drop compression prefix.
1095 name = name.substr(
1096 name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1097
Chris Bieneman2e752db2017-01-20 19:03:14 +00001098 if (StringRef *SectionData = MapSectionToMember(name)) {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001099 *SectionData = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001100 if (name == "debug_ranges") {
1101 // FIXME: Use the other dwo range section when we emit it.
George Rimarca532112017-04-24 10:19:45 +00001102 RangeDWOSection.Data = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001103 }
David Blaikie03c089c2013-09-23 22:44:47 +00001104 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +00001105 // Find debug_types data by section rather than name as there are
1106 // multiple, comdat grouped, debug_types sections.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001107 TypesSections[Section].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +00001108 } else if (name == "debug_types.dwo") {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +00001109 TypesDWOSections[Section].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +00001110 }
Eric Christopher7370b552012-11-12 21:40:38 +00001111
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +00001112 if (RelocatedSection == Obj.section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +00001113 continue;
1114
1115 StringRef RelSecName;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001116 StringRef RelSecData;
Rafael Espindola4f60a382013-05-30 03:05:14 +00001117 RelocatedSection->getName(RelSecName);
Keno Fischerc780e8e2015-05-21 21:24:32 +00001118
1119 // If the section we're relocating was relocated already by the JIT,
1120 // then we used the relocated version above, so we do not need to process
1121 // relocations for it now.
George Rimara25d3292017-05-27 18:10:23 +00001122 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
Keno Fischerc780e8e2015-05-21 21:24:32 +00001123 continue;
1124
Frederic Riss7bb12262015-08-23 04:44:21 +00001125 // In Mach-o files, the relocations do not need to be applied if
1126 // there is no load offset to apply. The value read at the
1127 // relocation point already factors in the section address
1128 // (actually applying the relocations will produce wrong results
1129 // as the section address will be added twice).
Craig Topper66059c92015-11-18 07:07:59 +00001130 if (!L && isa<MachOObjectFile>(&Obj))
Frederic Riss7bb12262015-08-23 04:44:21 +00001131 continue;
1132
Rafael Espindola4f60a382013-05-30 03:05:14 +00001133 RelSecName = RelSecName.substr(
1134 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
1135
Andrew Kaylord55d7012013-01-25 22:50:58 +00001136 // TODO: Add support for relocations in other sections as needed.
1137 // Record relocations for the debug_info and debug_line sections.
David Blaikied2f3a942017-05-22 07:02:47 +00001138 RelocAddrMap *Map =
1139 StringSwitch<RelocAddrMap *>(RelSecName)
1140 .Case("debug_info", &InfoSection.Relocs)
1141 .Case("debug_loc", &LocSection.Relocs)
1142 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
1143 .Case("debug_line", &LineSection.Relocs)
1144 .Case("debug_ranges", &RangeSection.Relocs)
1145 .Case("debug_addr", &AddrSection.Relocs)
1146 .Case("apple_names", &AppleNamesSection.Relocs)
1147 .Case("apple_types", &AppleTypesSection.Relocs)
1148 .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
1149 .Case("apple_namespac", &AppleNamespacesSection.Relocs)
1150 .Case("apple_objc", &AppleObjCSection.Relocs)
1151 .Default(nullptr);
David Blaikie03c089c2013-09-23 22:44:47 +00001152 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +00001153 // Find debug_types relocs by section rather than name as there are
1154 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +00001155 if (RelSecName == "debug_types")
1156 Map = &TypesSections[*RelocatedSection].Relocs;
1157 else if (RelSecName == "debug_types.dwo")
1158 Map = &TypesDWOSections[*RelocatedSection].Relocs;
1159 else
1160 continue;
David Blaikie03c089c2013-09-23 22:44:47 +00001161 }
Eric Christopher7370b552012-11-12 21:40:38 +00001162
George Rimarfed9f092017-05-17 12:10:51 +00001163 if (Section.relocation_begin() == Section.relocation_end())
1164 continue;
1165
George Rimara25d3292017-05-27 18:10:23 +00001166 // Symbol to [address, section index] cache mapping.
1167 std::map<SymbolRef, SymInfo> AddrCache;
George Rimarfed9f092017-05-17 12:10:51 +00001168 for (const RelocationRef &Reloc : Section.relocations()) {
1169 // FIXME: it's not clear how to correctly handle scattered
1170 // relocations.
1171 if (isRelocScattered(Obj, Reloc))
1172 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001173
George Rimara25d3292017-05-27 18:10:23 +00001174 Expected<SymInfo> SymInfoOrErr = getSymbolInfo(Obj, Reloc, L, AddrCache);
1175 if (!SymInfoOrErr) {
1176 errs() << toString(SymInfoOrErr.takeError()) << '\n';
George Rimarfed9f092017-05-17 12:10:51 +00001177 continue;
Eric Christopher7370b552012-11-12 21:40:38 +00001178 }
George Rimarfed9f092017-05-17 12:10:51 +00001179
1180 object::RelocVisitor V(Obj);
George Rimara25d3292017-05-27 18:10:23 +00001181 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
George Rimarfed9f092017-05-17 12:10:51 +00001182 if (V.error()) {
1183 SmallString<32> Name;
1184 Reloc.getTypeName(Name);
1185 errs() << "error: failed to compute relocation: " << Name << "\n";
1186 continue;
1187 }
George Rimara25d3292017-05-27 18:10:23 +00001188 llvm::RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1189 Map->insert({Reloc.getOffset(), Rel});
Eric Christopher7370b552012-11-12 21:40:38 +00001190 }
1191 }
1192}
1193
Chris Bieneman2e752db2017-01-20 19:03:14 +00001194DWARFContextInMemory::DWARFContextInMemory(
1195 const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize,
1196 bool isLittleEndian)
1197 : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) {
1198 for (const auto &SecIt : Sections) {
1199 if (StringRef *SectionData = MapSectionToMember(SecIt.first()))
1200 *SectionData = SecIt.second->getBuffer();
1201 }
1202}
1203
1204StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) {
1205 return StringSwitch<StringRef *>(Name)
1206 .Case("debug_info", &InfoSection.Data)
1207 .Case("debug_abbrev", &AbbrevSection)
1208 .Case("debug_loc", &LocSection.Data)
1209 .Case("debug_line", &LineSection.Data)
1210 .Case("debug_aranges", &ARangeSection)
1211 .Case("debug_frame", &DebugFrameSection)
1212 .Case("eh_frame", &EHFrameSection)
1213 .Case("debug_str", &StringSection)
George Rimarca532112017-04-24 10:19:45 +00001214 .Case("debug_ranges", &RangeSection.Data)
Chris Bieneman2e752db2017-01-20 19:03:14 +00001215 .Case("debug_macinfo", &MacinfoSection)
1216 .Case("debug_pubnames", &PubNamesSection)
1217 .Case("debug_pubtypes", &PubTypesSection)
1218 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1219 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1220 .Case("debug_info.dwo", &InfoDWOSection.Data)
1221 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1222 .Case("debug_loc.dwo", &LocDWOSection.Data)
1223 .Case("debug_line.dwo", &LineDWOSection.Data)
1224 .Case("debug_str.dwo", &StringDWOSection)
1225 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
David Blaikied2f3a942017-05-22 07:02:47 +00001226 .Case("debug_addr", &AddrSection.Data)
Chris Bieneman2e752db2017-01-20 19:03:14 +00001227 .Case("apple_names", &AppleNamesSection.Data)
1228 .Case("apple_types", &AppleTypesSection.Data)
1229 .Case("apple_namespaces", &AppleNamespacesSection.Data)
1230 .Case("apple_namespac", &AppleNamespacesSection.Data)
1231 .Case("apple_objc", &AppleObjCSection.Data)
1232 .Case("debug_cu_index", &CUIndexSection)
1233 .Case("debug_tu_index", &TUIndexSection)
1234 .Case("gdb_index", &GdbIndexSection)
1235 // Any more debug info sections go here.
1236 .Default(nullptr);
1237}
1238
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001239void DWARFContextInMemory::anchor() {}