blob: f01050c83ba5b8ebbdf33c8d03f1f1e30f8ef146 [file] [log] [blame]
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001//===- DWARFContext.cpp ---------------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Alexey Samsonove16e16a2012-07-19 07:03:58 +000010#include "llvm/ADT/SmallString.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/STLExtras.h"
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +000013#include "llvm/ADT/StringSwitch.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000014#include "llvm/ADT/StringRef.h"
Zachary Turner82af9432015-01-30 18:07:45 +000015#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000016#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
17#include "llvm/DebugInfo/DWARF/DWARFContext.h"
18#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
19#include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
Zachary Turner82af9432015-01-30 18:07:45 +000020#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000021#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
22#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
23#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
24#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
George Rimare71e33f2016-12-17 09:10:32 +000025#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000026#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
27#include "llvm/DebugInfo/DWARF/DWARFDie.h"
28#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
29#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
30#include "llvm/DebugInfo/DWARF/DWARFSection.h"
David Blaikie65a8efe2015-11-11 19:28:21 +000031#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
George Rimar4bf30832017-01-11 15:26:41 +000032#include "llvm/Object/Decompressor.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000033#include "llvm/Object/MachO.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000034#include "llvm/Object/ObjectFile.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000035#include "llvm/Object/RelocVisitor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000036#include "llvm/Support/Casting.h"
37#include "llvm/Support/DataExtractor.h"
38#include "llvm/Support/Debug.h"
39#include "llvm/Support/Error.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000040#include "llvm/Support/Format.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000041#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000042#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000043#include <algorithm>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000044#include <cstdint>
45#include <string>
46#include <utility>
47#include <vector>
48
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000049using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000050using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000051using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000052
Chandler Carruthe96dd892014-04-21 22:55:11 +000053#define DEBUG_TYPE "dwarf"
54
Eric Christopher494109b2012-10-16 23:46:25 +000055typedef DWARFDebugLine::LineTable DWARFLineTable;
Alexey Samsonovdce67342014-05-15 21:24:32 +000056typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
57typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000058
George Rimarf8a96422017-04-21 09:12:18 +000059uint64_t llvm::getRelocatedValue(const DataExtractor &Data, uint32_t Size,
60 uint32_t *Off, const RelocAddrMap *Relocs) {
61 if (!Relocs)
62 return Data.getUnsigned(Off, Size);
63 RelocAddrMap::const_iterator AI = Relocs->find(*Off);
64 if (AI == Relocs->end())
65 return Data.getUnsigned(Off, Size);
66 return Data.getUnsigned(Off, Size) + AI->second.second;
67}
68
Frederic Riss7c500472014-11-14 19:30:08 +000069static void dumpAccelSection(raw_ostream &OS, StringRef Name,
70 const DWARFSection& Section, StringRef StringSection,
71 bool LittleEndian) {
72 DataExtractor AccelSection(Section.Data, LittleEndian, 0);
Frederic Risse837ec22014-11-14 16:15:53 +000073 DataExtractor StrData(StringSection, LittleEndian, 0);
74 OS << "\n." << Name << " contents:\n";
Frederic Riss7c500472014-11-14 19:30:08 +000075 DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
Frederic Risse837ec22014-11-14 16:15:53 +000076 if (!Accel.extract())
77 return;
78 Accel.dump(OS);
79}
80
David Blaikie50cc27e2016-10-18 21:09:48 +000081void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH,
82 bool SummarizeTypes) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +000083 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
84 OS << ".debug_abbrev contents:\n";
85 getDebugAbbrev()->dump(OS);
86 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000087
David Blaikie66865d62014-01-09 00:13:35 +000088 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
89 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +000090 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +000091 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +000092 }
David Blaikie622dce42014-01-08 23:29:59 +000093
Eli Bendersky7a94daa2013-01-25 20:26:43 +000094 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
95 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000096 for (const auto &CU : compile_units())
97 CU->dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +000098 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000099
David Blaikie66865d62014-01-09 00:13:35 +0000100 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
101 getNumDWOCompileUnits()) {
102 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000103 for (const auto &DWOCU : dwo_compile_units())
104 DWOCU->dump(OS);
David Blaikie66865d62014-01-09 00:13:35 +0000105 }
David Blaikie622dce42014-01-08 23:29:59 +0000106
David Blaikie66865d62014-01-09 00:13:35 +0000107 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000108 OS << "\n.debug_types contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000109 for (const auto &TUS : type_unit_sections())
110 for (const auto &TU : TUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000111 TU->dump(OS, SummarizeTypes);
David Blaikie03c089c2013-09-23 22:44:47 +0000112 }
113
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000114 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
115 getNumDWOTypeUnits()) {
116 OS << "\n.debug_types.dwo contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000117 for (const auto &DWOTUS : dwo_type_unit_sections())
118 for (const auto &DWOTU : DWOTUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000119 DWOTU->dump(OS, SummarizeTypes);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000120 }
David Blaikie92d9d622014-01-09 05:08:24 +0000121
David Blaikie18e73502013-06-19 21:37:13 +0000122 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +0000123 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +0000124 getDebugLoc()->dump(OS);
125 }
126
David Blaikie9c550ac2014-03-25 01:44:02 +0000127 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
128 OS << "\n.debug_loc.dwo contents:\n";
129 getDebugLocDWO()->dump(OS);
130 }
131
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000132 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
133 OS << "\n.debug_frame contents:\n";
134 getDebugFrame()->dump(OS);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000135 if (DumpEH) {
136 OS << "\n.eh_frame contents:\n";
137 getEHFrame()->dump(OS);
138 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000139 }
140
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000141 if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
142 OS << "\n.debug_macinfo contents:\n";
143 getDebugMacro()->dump(OS);
144 }
145
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000146 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000147 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
148 OS << "\n.debug_aranges contents:\n";
149 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
150 DWARFDebugArangeSet set;
151 while (set.extract(arangesData, &offset))
152 set.dump(OS);
153 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000154
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000155 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000156 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
157 OS << "\n.debug_line contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000158 for (const auto &CU : compile_units()) {
159 savedAddressByteSize = CU->getAddressByteSize();
Greg Claytonc8c10322016-12-13 18:25:19 +0000160 auto CUDIE = CU->getUnitDIE();
161 if (!CUDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000162 continue;
Greg Clayton97d22182017-01-13 21:08:18 +0000163 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000164 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000165 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000166 DWARFDebugLine::LineTable LineTable;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000167 uint32_t Offset = *StmtOffset;
168 LineTable.parse(lineData, &getLineSection().Relocs, &Offset);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000169 LineTable.dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000170 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000171 }
172 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000173
David Blaikie65a8efe2015-11-11 19:28:21 +0000174 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
175 OS << "\n.debug_cu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000176 getCUIndex().dump(OS);
David Blaikie65a8efe2015-11-11 19:28:21 +0000177 }
178
David Blaikie51c40282015-11-11 19:40:49 +0000179 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
180 OS << "\n.debug_tu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000181 getTUIndex().dump(OS);
David Blaikie51c40282015-11-11 19:40:49 +0000182 }
183
David Blaikie1d4736e2014-02-24 23:58:54 +0000184 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
185 OS << "\n.debug_line.dwo contents:\n";
186 unsigned stmtOffset = 0;
187 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
188 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000189 DWARFDebugLine::LineTable LineTable;
190 while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
191 LineTable.dump(OS);
192 LineTable.clear();
193 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000194 }
195
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000196 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
197 OS << "\n.debug_str contents:\n";
198 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
199 offset = 0;
200 uint32_t strOffset = 0;
201 while (const char *s = strData.getCStr(&offset)) {
202 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
203 strOffset = offset;
204 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000205 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000206
David Blaikie66865d62014-01-09 00:13:35 +0000207 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
208 !getStringDWOSection().empty()) {
209 OS << "\n.debug_str.dwo contents:\n";
210 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
211 offset = 0;
212 uint32_t strDWOOffset = 0;
213 while (const char *s = strDWOData.getCStr(&offset)) {
214 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
215 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000216 }
David Blaikie66865d62014-01-09 00:13:35 +0000217 }
David Blaikie622dce42014-01-08 23:29:59 +0000218
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000219 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
220 OS << "\n.debug_ranges contents:\n";
221 // In fact, different compile units may have different address byte
222 // sizes, but for simplicity we just use the address byte size of the last
223 // compile unit (there is no easy and fast way to associate address range
224 // list and the compile unit it describes).
225 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
226 savedAddressByteSize);
227 offset = 0;
228 DWARFDebugRangeList rangeList;
229 while (rangeList.extract(rangesData, &offset))
230 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000231 }
Eric Christopher962c9082013-01-15 23:56:56 +0000232
Eric Christopher0de53592013-09-25 23:02:36 +0000233 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000234 DWARFDebugPubTable(getPubNamesSection(), isLittleEndian(), false)
235 .dump("debug_pubnames", OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000236
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000237 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000238 DWARFDebugPubTable(getPubTypesSection(), isLittleEndian(), false)
239 .dump("debug_pubtypes", OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000240
David Blaikieecd21ff2013-09-24 19:50:00 +0000241 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
George Rimare71e33f2016-12-17 09:10:32 +0000242 DWARFDebugPubTable(getGnuPubNamesSection(), isLittleEndian(),
243 true /* GnuStyle */)
244 .dump("debug_gnu_pubnames", OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000245
246 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
George Rimare71e33f2016-12-17 09:10:32 +0000247 DWARFDebugPubTable(getGnuPubTypesSection(), isLittleEndian(),
248 true /* GnuStyle */)
249 .dump("debug_gnu_pubtypes", OS);
David Blaikie404d3042013-09-19 23:01:29 +0000250
David Blaikie66865d62014-01-09 00:13:35 +0000251 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
252 !getStringOffsetDWOSection().empty()) {
253 OS << "\n.debug_str_offsets.dwo contents:\n";
254 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
255 0);
256 offset = 0;
257 uint64_t size = getStringOffsetDWOSection().size();
258 while (offset < size) {
259 OS << format("0x%8.8x: ", offset);
260 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000261 }
David Blaikie66865d62014-01-09 00:13:35 +0000262 }
Frederic Risse837ec22014-11-14 16:15:53 +0000263
George Rimar4f82df52016-09-23 11:01:53 +0000264 if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) &&
265 !getGdbIndexSection().empty()) {
266 OS << "\n.gnu_index contents:\n";
267 getGdbIndex().dump(OS);
268 }
269
Frederic Risse837ec22014-11-14 16:15:53 +0000270 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
271 dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
272 getStringSection(), isLittleEndian());
273
274 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
275 dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
276 getStringSection(), isLittleEndian());
277
278 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
279 dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
280 getStringSection(), isLittleEndian());
281
282 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
283 dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
284 getStringSection(), isLittleEndian());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000285}
286
David Blaikie82641be2015-11-17 00:39:55 +0000287const DWARFUnitIndex &DWARFContext::getCUIndex() {
288 if (CUIndex)
289 return *CUIndex;
290
291 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
292
David Blaikieb073cb92015-12-02 06:21:34 +0000293 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000294 CUIndex->parse(CUIndexData);
295 return *CUIndex;
296}
297
298const DWARFUnitIndex &DWARFContext::getTUIndex() {
299 if (TUIndex)
300 return *TUIndex;
301
302 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
303
David Blaikieb073cb92015-12-02 06:21:34 +0000304 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000305 TUIndex->parse(TUIndexData);
306 return *TUIndex;
307}
308
George Rimar4f82df52016-09-23 11:01:53 +0000309DWARFGdbIndex &DWARFContext::getGdbIndex() {
310 if (GdbIndex)
311 return *GdbIndex;
312
313 DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0);
314 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
315 GdbIndex->parse(GdbIndexData);
316 return *GdbIndex;
317}
318
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000319const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
320 if (Abbrev)
321 return Abbrev.get();
322
323 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
324
325 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000326 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000327 return Abbrev.get();
328}
329
Eric Christopherda4b2192013-01-02 23:52:13 +0000330const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
331 if (AbbrevDWO)
332 return AbbrevDWO.get();
333
334 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
335 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000336 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000337 return AbbrevDWO.get();
338}
339
David Blaikie18e73502013-06-19 21:37:13 +0000340const DWARFDebugLoc *DWARFContext::getDebugLoc() {
341 if (Loc)
342 return Loc.get();
343
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000344 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
345 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000346 // assume all compile units have the same address byte size
347 if (getNumCompileUnits())
348 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
349 return Loc.get();
350}
351
David Blaikie9c550ac2014-03-25 01:44:02 +0000352const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
353 if (LocDWO)
354 return LocDWO.get();
355
356 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
357 LocDWO.reset(new DWARFDebugLocDWO());
358 LocDWO->parse(LocData);
359 return LocDWO.get();
360}
361
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000362const DWARFDebugAranges *DWARFContext::getDebugAranges() {
363 if (Aranges)
364 return Aranges.get();
365
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000366 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000367 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000368 return Aranges.get();
369}
370
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000371const DWARFDebugFrame *DWARFContext::getDebugFrame() {
372 if (DebugFrame)
373 return DebugFrame.get();
374
375 // There's a "bug" in the DWARFv3 standard with respect to the target address
376 // size within debug frame sections. While DWARF is supposed to be independent
377 // of its container, FDEs have fields with size being "target address size",
378 // which isn't specified in DWARF in general. It's only specified for CUs, but
379 // .eh_frame can appear without a .debug_info section. Follow the example of
380 // other tools (libdwarf) and extract this from the container (ObjectFile
381 // provides this information). This problem is fixed in DWARFv4
382 // See this dwarf-discuss discussion for more details:
383 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
384 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
385 getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000386 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
387 DebugFrame->parse(debugFrameData);
388 return DebugFrame.get();
389}
390
391const DWARFDebugFrame *DWARFContext::getEHFrame() {
392 if (EHFrame)
393 return EHFrame.get();
394
395 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
396 getAddressSize());
397 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000398 DebugFrame->parse(debugFrameData);
399 return DebugFrame.get();
400}
401
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000402const DWARFDebugMacro *DWARFContext::getDebugMacro() {
403 if (Macro)
404 return Macro.get();
405
406 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
407 Macro.reset(new DWARFDebugMacro());
408 Macro->parse(MacinfoData);
409 return Macro.get();
410}
411
Eric Christopher494109b2012-10-16 23:46:25 +0000412const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000413DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000414 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000415 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
David Blaikiec4e2bed2015-11-17 21:08:05 +0000416
Greg Claytonc8c10322016-12-13 18:25:19 +0000417 auto UnitDIE = U->getUnitDIE();
418 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000419 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000420
Greg Clayton97d22182017-01-13 21:08:18 +0000421 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000422 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000423 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000424
Greg Clayton52fe1f62016-12-14 22:38:08 +0000425 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000426 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000427 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000428 return lt;
429
430 // We have to parse it first.
David Blaikiec4e2bed2015-11-17 21:08:05 +0000431 DataExtractor lineData(U->getLineSection(), isLittleEndian(),
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000432 U->getAddressByteSize());
Benjamin Kramer679e1752011-09-15 20:43:18 +0000433 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000434}
435
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000436void DWARFContext::parseCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000437 CUs.parse(*this, getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000438}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000439
David Blaikie03c089c2013-09-23 22:44:47 +0000440void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000441 if (!TUs.empty())
442 return;
443 for (const auto &I : getTypesSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000444 TUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000445 TUs.back().parse(*this, I.second);
David Blaikie03c089c2013-09-23 22:44:47 +0000446 }
447}
448
Eric Christopherda4b2192013-01-02 23:52:13 +0000449void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000450 DWOCUs.parseDWO(*this, getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000451}
452
David Blaikie92d9d622014-01-09 05:08:24 +0000453void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000454 if (!DWOTUs.empty())
455 return;
456 for (const auto &I : getTypesDWOSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000457 DWOTUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000458 DWOTUs.back().parseDWO(*this, I.second);
David Blaikie92d9d622014-01-09 05:08:24 +0000459 }
460}
461
Alexey Samsonov45be7932012-08-30 07:49:50 +0000462DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000463 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000464 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000465}
466
Alexey Samsonov45be7932012-08-30 07:49:50 +0000467DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000468 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000469 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000470 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000471 return getCompileUnitForOffset(CUOffset);
472}
473
David Blaikieefc4eba2017-02-06 20:19:02 +0000474static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
475 uint64_t Address,
476 FunctionNameKind Kind,
477 std::string &FunctionName,
478 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000479 // The address may correspond to instruction in some inlined function,
480 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000481 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000482 SmallVector<DWARFDie, 4> InlinedChain;
483 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000484 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000485 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000486
487 const DWARFDie &DIE = InlinedChain[0];
488 bool FoundResult = false;
489 const char *Name = nullptr;
490 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000491 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000492 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000493 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000494 if (auto DeclLineResult = DIE.getDeclLine()) {
495 StartLine = DeclLineResult;
496 FoundResult = true;
497 }
498
499 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000500}
501
Alexey Samsonov45be7932012-08-30 07:49:50 +0000502DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000503 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000504 DILineInfo Result;
505
Alexey Samsonov45be7932012-08-30 07:49:50 +0000506 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
507 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000508 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000509 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
510 Result.FunctionName,
511 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000512 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000513 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
514 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
515 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000516 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000517 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000518}
David Blaikiea379b1812011-12-20 02:50:00 +0000519
Alexey Samsonovdce67342014-05-15 21:24:32 +0000520DILineInfoTable
521DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
522 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000523 DILineInfoTable Lines;
524 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
525 if (!CU)
526 return Lines;
527
528 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000529 uint32_t StartLine = 0;
530 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
531 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000532
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000533 // If the Specifier says we don't need FileLineInfo, just
534 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000535 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000536 DILineInfo Result;
537 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000538 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000539 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000540 return Lines;
541 }
542
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000543 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000544
545 // Get the index of row we're looking for in the line table.
546 std::vector<uint32_t> RowVector;
547 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
548 return Lines;
549
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000550 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000551 // Take file number and line/column from the row.
552 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000553 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000554 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
555 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000556 Result.FunctionName = FunctionName;
557 Result.Line = Row.Line;
558 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000559 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000560 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000561 }
562
563 return Lines;
564}
565
Alexey Samsonovdce67342014-05-15 21:24:32 +0000566DIInliningInfo
567DWARFContext::getInliningInfoForAddress(uint64_t Address,
568 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000569 DIInliningInfo InliningInfo;
570
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000571 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
572 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000573 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000574
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000575 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000576 SmallVector<DWARFDie, 4> InlinedChain;
577 CU->getInlinedChainForAddress(Address, InlinedChain);
578 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000579 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
580 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000581 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000582 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000583 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000584 if (LineTable &&
585 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
586 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000587 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000588 }
589 return InliningInfo;
590 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000591
Dehao Chenef700d52017-04-17 20:10:39 +0000592 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000593 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
594 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000595 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000596 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000597 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000598 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000599 if (auto DeclLineResult = FunctionDIE.getDeclLine())
600 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000601 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000602 if (i == 0) {
603 // For the topmost frame, initialize the line table of this
604 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000605 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000606 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000607 if (LineTable)
608 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
609 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000610 } else {
611 // Otherwise, use call file, call line and call column from
612 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000613 if (LineTable)
614 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
615 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000616 Frame.Line = CallLine;
617 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000618 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000619 }
620 // Get call file/line/column of a current DIE.
621 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +0000622 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
623 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000624 }
625 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000626 InliningInfo.addFrame(Frame);
627 }
628 return InliningInfo;
629}
630
George Rimar702dac62017-04-12 08:59:15 +0000631static Error createError(const Twine &Reason, llvm::Error E) {
632 return make_error<StringError>(Reason + toString(std::move(E)),
633 inconvertibleErrorCode());
634}
635
636/// Returns the address of symbol relocation used against. Used for futher
637/// relocations computation. Symbol's section load address is taken in account if
638/// LoadedObjectInfo interface is provided.
639static Expected<uint64_t> getSymbolAddress(const object::ObjectFile &Obj,
640 const RelocationRef &Reloc,
641 const LoadedObjectInfo *L) {
642 uint64_t Ret = 0;
643 object::section_iterator RSec = Obj.section_end();
644 object::symbol_iterator Sym = Reloc.getSymbol();
645
646 // First calculate the address of the symbol or section as it appears
647 // in the object file
648 if (Sym != Obj.symbol_end()) {
649 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
650 if (!SymAddrOrErr)
651 return createError("error: failed to compute symbol address: ",
652 SymAddrOrErr.takeError());
653
654 // Also remember what section this symbol is in for later
655 auto SectOrErr = Sym->getSection();
656 if (!SectOrErr)
657 return createError("error: failed to get symbol section: ",
658 SectOrErr.takeError());
659
660 RSec = *SectOrErr;
661 Ret = *SymAddrOrErr;
662 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
663 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
664 Ret = RSec->getAddress();
665 }
666
667 // If we are given load addresses for the sections, we need to adjust:
668 // SymAddr = (Address of Symbol Or Section in File) -
669 // (Address of Section in File) +
670 // (Load Address of Section)
671 // RSec is now either the section being targeted or the section
672 // containing the symbol being targeted. In either case,
673 // we need to perform the same computation.
674 if (L && RSec != Obj.section_end())
675 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
676 Ret += SectionLoadAddress - RSec->getAddress();
677 return Ret;
678}
679
680static bool isRelocScattered(const object::ObjectFile &Obj,
681 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +0000682 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
683 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +0000684 return false;
685 // MachO also has relocations that point to sections and
686 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +0000687 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
688 return MachObj->isRelocationScattered(RelocInfo);
689}
690
Keno Fischerc780e8e2015-05-21 21:24:32 +0000691DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
692 const LoadedObjectInfo *L)
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +0000693 : IsLittleEndian(Obj.isLittleEndian()),
694 AddressSize(Obj.getBytesInAddress()) {
695 for (const SectionRef &Section : Obj.sections()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000696 StringRef name;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000697 Section.getName(name);
David Majnemerdac39852014-09-26 22:32:16 +0000698 // Skip BSS and Virtual sections, they aren't interesting.
Rafael Espindola80291272014-10-08 15:28:58 +0000699 bool IsBSS = Section.isBSS();
David Majnemerdac39852014-09-26 22:32:16 +0000700 if (IsBSS)
701 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000702 bool IsVirtual = Section.isVirtual();
David Majnemerdac39852014-09-26 22:32:16 +0000703 if (IsVirtual)
704 continue;
Eric Christopher7370b552012-11-12 21:40:38 +0000705 StringRef data;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000706
Lang Hames2e88f4f2015-07-28 17:52:11 +0000707 section_iterator RelocatedSection = Section.getRelocatedSection();
Keno Fischerc780e8e2015-05-21 21:24:32 +0000708 // Try to obtain an already relocated version of this section.
709 // Else use the unrelocated section from the object file. We'll have to
710 // apply relocations ourselves later.
Lang Hames2e88f4f2015-07-28 17:52:11 +0000711 if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
Keno Fischerc780e8e2015-05-21 21:24:32 +0000712 Section.getContents(data);
Eric Christopher7370b552012-11-12 21:40:38 +0000713
George Rimar4bf30832017-01-11 15:26:41 +0000714 if (Decompressor::isCompressed(Section)) {
715 Expected<Decompressor> Decompressor =
716 Decompressor::create(name, data, IsLittleEndian, AddressSize == 8);
717 if (!Decompressor)
718 continue;
George Rimar401e4e52016-05-24 12:48:46 +0000719 SmallString<32> Out;
George Rimar4bf30832017-01-11 15:26:41 +0000720 if (auto Err = Decompressor->decompress(Out))
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000721 continue;
George Rimar401e4e52016-05-24 12:48:46 +0000722 UncompressedSections.emplace_back(std::move(Out));
David Blaikiea505f242014-04-05 21:26:44 +0000723 data = UncompressedSections.back();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000724 }
725
George Rimar4bf30832017-01-11 15:26:41 +0000726 // Compressed sections names in GNU style starts from ".z",
727 // at this point section is decompressed and we drop compression prefix.
728 name = name.substr(
729 name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
730
Chris Bieneman2e752db2017-01-20 19:03:14 +0000731 if (StringRef *SectionData = MapSectionToMember(name)) {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000732 *SectionData = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000733 if (name == "debug_ranges") {
734 // FIXME: Use the other dwo range section when we emit it.
735 RangeDWOSection = data;
736 }
David Blaikie03c089c2013-09-23 22:44:47 +0000737 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +0000738 // Find debug_types data by section rather than name as there are
739 // multiple, comdat grouped, debug_types sections.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000740 TypesSections[Section].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +0000741 } else if (name == "debug_types.dwo") {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000742 TypesDWOSections[Section].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +0000743 }
Eric Christopher7370b552012-11-12 21:40:38 +0000744
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +0000745 if (RelocatedSection == Obj.section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +0000746 continue;
747
748 StringRef RelSecName;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000749 StringRef RelSecData;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000750 RelocatedSection->getName(RelSecName);
Keno Fischerc780e8e2015-05-21 21:24:32 +0000751
752 // If the section we're relocating was relocated already by the JIT,
753 // then we used the relocated version above, so we do not need to process
754 // relocations for it now.
Lang Hames2e88f4f2015-07-28 17:52:11 +0000755 if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
Keno Fischerc780e8e2015-05-21 21:24:32 +0000756 continue;
757
Frederic Riss7bb12262015-08-23 04:44:21 +0000758 // In Mach-o files, the relocations do not need to be applied if
759 // there is no load offset to apply. The value read at the
760 // relocation point already factors in the section address
761 // (actually applying the relocations will produce wrong results
762 // as the section address will be added twice).
Craig Topper66059c92015-11-18 07:07:59 +0000763 if (!L && isa<MachOObjectFile>(&Obj))
Frederic Riss7bb12262015-08-23 04:44:21 +0000764 continue;
765
Rafael Espindola4f60a382013-05-30 03:05:14 +0000766 RelSecName = RelSecName.substr(
767 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
768
Andrew Kaylord55d7012013-01-25 22:50:58 +0000769 // TODO: Add support for relocations in other sections as needed.
770 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola4f60a382013-05-30 03:05:14 +0000771 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000772 .Case("debug_info", &InfoSection.Relocs)
773 .Case("debug_loc", &LocSection.Relocs)
774 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
775 .Case("debug_line", &LineSection.Relocs)
Frederic Riss7c500472014-11-14 19:30:08 +0000776 .Case("apple_names", &AppleNamesSection.Relocs)
777 .Case("apple_types", &AppleTypesSection.Relocs)
778 .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
779 .Case("apple_namespac", &AppleNamespacesSection.Relocs)
780 .Case("apple_objc", &AppleObjCSection.Relocs)
Craig Topper2617dcc2014-04-15 06:32:26 +0000781 .Default(nullptr);
David Blaikie03c089c2013-09-23 22:44:47 +0000782 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +0000783 // Find debug_types relocs by section rather than name as there are
784 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +0000785 if (RelSecName == "debug_types")
786 Map = &TypesSections[*RelocatedSection].Relocs;
787 else if (RelSecName == "debug_types.dwo")
788 Map = &TypesDWOSections[*RelocatedSection].Relocs;
789 else
790 continue;
David Blaikie03c089c2013-09-23 22:44:47 +0000791 }
Eric Christopher7370b552012-11-12 21:40:38 +0000792
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000793 if (Section.relocation_begin() != Section.relocation_end()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000794 uint64_t SectionSize = RelocatedSection->getSize();
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000795 for (const RelocationRef &Reloc : Section.relocations()) {
George Rimar702dac62017-04-12 08:59:15 +0000796 // FIXME: it's not clear how to correctly handle scattered
797 // relocations.
798 if (isRelocScattered(Obj, Reloc))
799 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000800
George Rimar702dac62017-04-12 08:59:15 +0000801 Expected<uint64_t> SymAddrOrErr = getSymbolAddress(Obj, Reloc, L);
802 if (!SymAddrOrErr) {
Krasimir Georgiev4ed589d2017-04-12 11:33:26 +0000803 errs() << toString(SymAddrOrErr.takeError()) << '\n';
George Rimar702dac62017-04-12 08:59:15 +0000804 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000805 }
Eric Christopher7370b552012-11-12 21:40:38 +0000806
Eric Christopher47e079d2014-10-06 06:55:55 +0000807 object::RelocVisitor V(Obj);
George Rimar702dac62017-04-12 08:59:15 +0000808 object::RelocToApply R(V.visit(Reloc.getType(), Reloc, *SymAddrOrErr));
Eric Christopher7370b552012-11-12 21:40:38 +0000809 if (V.error()) {
810 SmallString<32> Name;
Rafael Espindola41bb4322015-06-30 04:08:37 +0000811 Reloc.getTypeName(Name);
Eric Christopher7370b552012-11-12 21:40:38 +0000812 errs() << "error: failed to compute relocation: "
813 << Name << "\n";
814 continue;
815 }
George Rimar702dac62017-04-12 08:59:15 +0000816 uint64_t Address = Reloc.getOffset();
Eric Christopher7370b552012-11-12 21:40:38 +0000817 if (Address + R.Width > SectionSize) {
818 errs() << "error: " << R.Width << "-byte relocation starting "
819 << Address << " bytes into section " << name << " which is "
820 << SectionSize << " bytes long.\n";
821 continue;
822 }
823 if (R.Width > 8) {
824 errs() << "error: can't handle a relocation of more than 8 bytes at "
825 "a time.\n";
826 continue;
827 }
828 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
829 << " at " << format("%p", Address)
830 << " with width " << format("%d", R.Width)
831 << "\n");
Eric Christopherda4b2192013-01-02 23:52:13 +0000832 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopher7370b552012-11-12 21:40:38 +0000833 }
834 }
835 }
836}
837
Chris Bieneman2e752db2017-01-20 19:03:14 +0000838DWARFContextInMemory::DWARFContextInMemory(
839 const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize,
840 bool isLittleEndian)
841 : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) {
842 for (const auto &SecIt : Sections) {
843 if (StringRef *SectionData = MapSectionToMember(SecIt.first()))
844 *SectionData = SecIt.second->getBuffer();
845 }
846}
847
848StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) {
849 return StringSwitch<StringRef *>(Name)
850 .Case("debug_info", &InfoSection.Data)
851 .Case("debug_abbrev", &AbbrevSection)
852 .Case("debug_loc", &LocSection.Data)
853 .Case("debug_line", &LineSection.Data)
854 .Case("debug_aranges", &ARangeSection)
855 .Case("debug_frame", &DebugFrameSection)
856 .Case("eh_frame", &EHFrameSection)
857 .Case("debug_str", &StringSection)
858 .Case("debug_ranges", &RangeSection)
859 .Case("debug_macinfo", &MacinfoSection)
860 .Case("debug_pubnames", &PubNamesSection)
861 .Case("debug_pubtypes", &PubTypesSection)
862 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
863 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
864 .Case("debug_info.dwo", &InfoDWOSection.Data)
865 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
866 .Case("debug_loc.dwo", &LocDWOSection.Data)
867 .Case("debug_line.dwo", &LineDWOSection.Data)
868 .Case("debug_str.dwo", &StringDWOSection)
869 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
870 .Case("debug_addr", &AddrSection)
871 .Case("apple_names", &AppleNamesSection.Data)
872 .Case("apple_types", &AppleTypesSection.Data)
873 .Case("apple_namespaces", &AppleNamespacesSection.Data)
874 .Case("apple_namespac", &AppleNamespacesSection.Data)
875 .Case("apple_objc", &AppleObjCSection.Data)
876 .Case("debug_cu_index", &CUIndexSection)
877 .Case("debug_tu_index", &TUIndexSection)
878 .Case("gdb_index", &GdbIndexSection)
879 // Any more debug info sections go here.
880 .Default(nullptr);
881}
882
Eugene Zelenko28db7e62017-03-01 01:14:23 +0000883void DWARFContextInMemory::anchor() {}