]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/DebugInfo/DWARF/DWARFContext.cpp
Vendor import of llvm trunk r302069:
[FreeBSD/FreeBSD.git] / lib / DebugInfo / DWARF / DWARFContext.cpp
1 //===- DWARFContext.cpp ---------------------------------------------------===//
2 //
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
10 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
17 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
21 #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"
25 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
26 #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"
31 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
32 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
33 #include "llvm/Object/Decompressor.h"
34 #include "llvm/Object/MachO.h"
35 #include "llvm/Object/ObjectFile.h"
36 #include "llvm/Object/RelocVisitor.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/DataExtractor.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/Error.h"
41 #include "llvm/Support/Format.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include <algorithm>
45 #include <cstdint>
46 #include <map>
47 #include <set>
48 #include <string>
49 #include <utility>
50 #include <vector>
51
52 using namespace llvm;
53 using namespace dwarf;
54 using namespace object;
55
56 #define DEBUG_TYPE "dwarf"
57
58 typedef DWARFDebugLine::LineTable DWARFLineTable;
59 typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
60 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
61
62 uint64_t llvm::getRelocatedValue(const DataExtractor &Data, uint32_t Size,
63                                  uint32_t *Off, const RelocAddrMap *Relocs) {
64   if (!Relocs)
65     return Data.getUnsigned(Off, Size);
66   RelocAddrMap::const_iterator AI = Relocs->find(*Off);
67   if (AI == Relocs->end())
68     return Data.getUnsigned(Off, Size);
69   return Data.getUnsigned(Off, Size) + AI->second.second;
70 }
71
72 static void dumpAccelSection(raw_ostream &OS, StringRef Name,
73                              const DWARFSection& Section, StringRef StringSection,
74                              bool LittleEndian) {
75   DataExtractor AccelSection(Section.Data, LittleEndian, 0);
76   DataExtractor StrData(StringSection, LittleEndian, 0);
77   OS << "\n." << Name << " contents:\n";
78   DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
79   if (!Accel.extract())
80     return;
81   Accel.dump(OS);
82 }
83
84 void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH,
85                         bool SummarizeTypes) {
86   if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
87     OS << ".debug_abbrev contents:\n";
88     getDebugAbbrev()->dump(OS);
89   }
90
91   if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
92     if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
93       OS << "\n.debug_abbrev.dwo contents:\n";
94       D->dump(OS);
95     }
96
97   if (DumpType == DIDT_All || DumpType == DIDT_Info) {
98     OS << "\n.debug_info contents:\n";
99     for (const auto &CU : compile_units())
100       CU->dump(OS);
101   }
102
103   if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
104       getNumDWOCompileUnits()) {
105     OS << "\n.debug_info.dwo contents:\n";
106     for (const auto &DWOCU : dwo_compile_units())
107       DWOCU->dump(OS);
108   }
109
110   if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
111     OS << "\n.debug_types contents:\n";
112     for (const auto &TUS : type_unit_sections())
113       for (const auto &TU : TUS)
114         TU->dump(OS, SummarizeTypes);
115   }
116
117   if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
118       getNumDWOTypeUnits()) {
119     OS << "\n.debug_types.dwo contents:\n";
120     for (const auto &DWOTUS : dwo_type_unit_sections())
121       for (const auto &DWOTU : DWOTUS)
122         DWOTU->dump(OS, SummarizeTypes);
123   }
124
125   if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
126     OS << "\n.debug_loc contents:\n";
127     getDebugLoc()->dump(OS);
128   }
129
130   if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
131     OS << "\n.debug_loc.dwo contents:\n";
132     getDebugLocDWO()->dump(OS);
133   }
134
135   if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
136     OS << "\n.debug_frame contents:\n";
137     getDebugFrame()->dump(OS);
138     if (DumpEH) {
139       OS << "\n.eh_frame contents:\n";
140       getEHFrame()->dump(OS);
141     }
142   }
143
144   if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
145     OS << "\n.debug_macinfo contents:\n";
146     getDebugMacro()->dump(OS);
147   }
148
149   uint32_t offset = 0;
150   if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
151     OS << "\n.debug_aranges contents:\n";
152     DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
153     DWARFDebugArangeSet set;
154     while (set.extract(arangesData, &offset))
155       set.dump(OS);
156   }
157
158   uint8_t savedAddressByteSize = 0;
159   if (DumpType == DIDT_All || DumpType == DIDT_Line) {
160     OS << "\n.debug_line contents:\n";
161     for (const auto &CU : compile_units()) {
162       savedAddressByteSize = CU->getAddressByteSize();
163       auto CUDIE = CU->getUnitDIE();
164       if (!CUDIE)
165         continue;
166       if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) {
167         DataExtractor lineData(getLineSection().Data, isLittleEndian(),
168                                savedAddressByteSize);
169         DWARFDebugLine::LineTable LineTable;
170         uint32_t Offset = *StmtOffset;
171         LineTable.parse(lineData, &getLineSection().Relocs, &Offset);
172         LineTable.dump(OS);
173       }
174     }
175   }
176
177   if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
178     OS << "\n.debug_cu_index contents:\n";
179     getCUIndex().dump(OS);
180   }
181
182   if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
183     OS << "\n.debug_tu_index contents:\n";
184     getTUIndex().dump(OS);
185   }
186
187   if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
188     OS << "\n.debug_line.dwo contents:\n";
189     unsigned stmtOffset = 0;
190     DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
191                            savedAddressByteSize);
192     DWARFDebugLine::LineTable LineTable;
193     while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
194       LineTable.dump(OS);
195       LineTable.clear();
196     }
197   }
198
199   if (DumpType == DIDT_All || DumpType == DIDT_Str) {
200     OS << "\n.debug_str contents:\n";
201     DataExtractor strData(getStringSection(), isLittleEndian(), 0);
202     offset = 0;
203     uint32_t strOffset = 0;
204     while (const char *s = strData.getCStr(&offset)) {
205       OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
206       strOffset = offset;
207     }
208   }
209
210   if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
211       !getStringDWOSection().empty()) {
212     OS << "\n.debug_str.dwo contents:\n";
213     DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
214     offset = 0;
215     uint32_t strDWOOffset = 0;
216     while (const char *s = strDWOData.getCStr(&offset)) {
217       OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
218       strDWOOffset = offset;
219     }
220   }
221
222   if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
223     OS << "\n.debug_ranges contents:\n";
224     // In fact, different compile units may have different address byte
225     // sizes, but for simplicity we just use the address byte size of the last
226     // compile unit (there is no easy and fast way to associate address range
227     // list and the compile unit it describes).
228     DataExtractor rangesData(getRangeSection().Data, isLittleEndian(),
229                              savedAddressByteSize);
230     offset = 0;
231     DWARFDebugRangeList rangeList;
232     while (rangeList.extract(rangesData, &offset, getRangeSection().Relocs))
233       rangeList.dump(OS);
234   }
235
236   if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
237     DWARFDebugPubTable(getPubNamesSection(), isLittleEndian(), false)
238         .dump("debug_pubnames", OS);
239
240   if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
241     DWARFDebugPubTable(getPubTypesSection(), isLittleEndian(), false)
242         .dump("debug_pubtypes", OS);
243
244   if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
245     DWARFDebugPubTable(getGnuPubNamesSection(), isLittleEndian(),
246                        true /* GnuStyle */)
247         .dump("debug_gnu_pubnames", OS);
248
249   if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
250     DWARFDebugPubTable(getGnuPubTypesSection(), isLittleEndian(),
251                        true /* GnuStyle */)
252         .dump("debug_gnu_pubtypes", OS);
253
254   if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
255       !getStringOffsetDWOSection().empty()) {
256     OS << "\n.debug_str_offsets.dwo contents:\n";
257     DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
258                                0);
259     offset = 0;
260     uint64_t size = getStringOffsetDWOSection().size();
261     while (offset < size) {
262       OS << format("0x%8.8x: ", offset);
263       OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
264     }
265   }
266
267   if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) &&
268       !getGdbIndexSection().empty()) {
269     OS << "\n.gnu_index contents:\n";
270     getGdbIndex().dump(OS);
271   }
272
273   if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
274     dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
275                      getStringSection(), isLittleEndian());
276
277   if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
278     dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
279                      getStringSection(), isLittleEndian());
280
281   if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
282     dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
283                      getStringSection(), isLittleEndian());
284
285   if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
286     dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
287                      getStringSection(), isLittleEndian());
288 }
289
290 DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
291   parseCompileUnits();
292   if (auto *CU = CUs.getUnitForOffset(Offset))
293     return CU->getDIEForOffset(Offset);
294   return DWARFDie();
295 }
296
297 namespace {
298   
299 class Verifier {
300   raw_ostream &OS;
301   DWARFContext &DCtx;
302 public:
303   Verifier(raw_ostream &S, DWARFContext &D) : OS(S), DCtx(D) {}
304   
305   bool HandleDebugInfo() {
306     bool Success = true;
307     // A map that tracks all references (converted absolute references) so we
308     // can verify each reference points to a valid DIE and not an offset that
309     // lies between to valid DIEs.
310     std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
311
312     OS << "Verifying .debug_info...\n";
313     for (const auto &CU : DCtx.compile_units()) {
314       unsigned NumDies = CU->getNumDIEs();
315       for (unsigned I = 0; I < NumDies; ++I) {
316         auto Die = CU->getDIEAtIndex(I);
317         const auto Tag = Die.getTag();
318         if (Tag == DW_TAG_null)
319           continue;
320         for (auto AttrValue : Die.attributes()) {
321           const auto Attr = AttrValue.Attr;
322           const auto Form = AttrValue.Value.getForm();
323           switch (Attr) {
324             case DW_AT_ranges:
325               // Make sure the offset in the DW_AT_ranges attribute is valid.
326               if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
327                 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
328                   Success = false;
329                   OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
330                   "bounds:\n";
331                   Die.dump(OS, 0);
332                   OS << "\n";
333                 }
334               } else {
335                 Success = false;
336                 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
337                 Die.dump(OS, 0);
338                 OS << "\n";
339               }
340               break;
341             case DW_AT_stmt_list:
342               // Make sure the offset in the DW_AT_stmt_list attribute is valid.
343               if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
344                 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
345                   Success = false;
346                   OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
347                   "bounds: "
348                   << format("0x%08" PRIx32, *SectionOffset) << "\n";
349                   CU->getUnitDIE().dump(OS, 0);
350                   OS << "\n";
351                 }
352               } else {
353                 Success = false;
354                 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
355                 Die.dump(OS, 0);
356                 OS << "\n";
357               }
358               break;
359               
360             default:
361               break;
362           }
363           switch (Form) {
364             case DW_FORM_ref1:
365             case DW_FORM_ref2:
366             case DW_FORM_ref4:
367             case DW_FORM_ref8:
368             case DW_FORM_ref_udata: {
369               // Verify all CU relative references are valid CU offsets.
370               Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
371               assert(RefVal);
372               if (RefVal) {
373                 auto DieCU = Die.getDwarfUnit();
374                 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
375                 auto CUOffset = AttrValue.Value.getRawUValue();
376                 if (CUOffset >= CUSize) {
377                   Success = false;
378                   OS << "error: " << FormEncodingString(Form) << " CU offset "
379                   << format("0x%08" PRIx32, CUOffset)
380                   << " is invalid (must be less than CU size of "
381                   << format("0x%08" PRIx32, CUSize) << "):\n";
382                   Die.dump(OS, 0);
383                   OS << "\n";
384                 } else {
385                   // Valid reference, but we will verify it points to an actual
386                   // DIE later.
387                   ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
388                 }
389               }
390               break;
391             }
392             case DW_FORM_ref_addr: {
393               // Verify all absolute DIE references have valid offsets in the
394               // .debug_info section.
395               Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
396               assert(RefVal);
397               if (RefVal) {
398                 if(*RefVal >= DCtx.getInfoSection().Data.size()) {
399                   Success = false;
400                   OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
401                         "bounds:\n";
402                   Die.dump(OS, 0);
403                   OS << "\n";
404                 } else {
405                   // Valid reference, but we will verify it points to an actual
406                   // DIE later.
407                   ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
408                 }
409               }
410               break;
411             }
412             case DW_FORM_strp: {
413               auto SecOffset = AttrValue.Value.getAsSectionOffset();
414               assert(SecOffset); // DW_FORM_strp is a section offset.
415               if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
416                 Success = false;
417                 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
418                 Die.dump(OS, 0);
419                 OS << "\n";
420               }
421               break;
422             }
423             default:
424               break;
425           }
426         }
427       }
428     }
429
430     // Take all references and make sure they point to an actual DIE by
431     // getting the DIE by offset and emitting an error
432     OS << "Verifying .debug_info references...\n";
433     for (auto Pair: ReferenceToDIEOffsets) {
434       auto Die = DCtx.getDIEForOffset(Pair.first);
435       if (Die)
436         continue;
437       Success = false;
438       OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
439          << ". Offset is in between DIEs:\n";
440       for (auto Offset: Pair.second) {
441         auto ReferencingDie = DCtx.getDIEForOffset(Offset);
442         ReferencingDie.dump(OS, 0);
443         OS << "\n";
444       }
445       OS << "\n";
446     }
447     return Success;
448   }
449
450   bool HandleDebugLine() {
451     std::map<uint64_t, DWARFDie> StmtListToDie;
452     bool Success = true;
453     OS << "Verifying .debug_line...\n";
454     for (const auto &CU : DCtx.compile_units()) {
455       uint32_t LineTableOffset = 0;
456       auto CUDie = CU->getUnitDIE();
457       auto StmtFormValue = CUDie.find(DW_AT_stmt_list);
458       if (!StmtFormValue) {
459         // No line table for this compile unit.
460         continue;
461       }
462       // Get the attribute value as a section offset. No need to produce an
463       // error here if the encoding isn't correct because we validate this in
464       // the .debug_info verifier.
465       if (auto StmtSectionOffset = toSectionOffset(StmtFormValue)) {
466         LineTableOffset = *StmtSectionOffset;
467         if (LineTableOffset >= DCtx.getLineSection().Data.size()) {
468           // Make sure we don't get a valid line table back if the offset
469           // is wrong.
470           assert(DCtx.getLineTableForUnit(CU.get()) == nullptr);
471           // Skip this line table as it isn't valid. No need to create an error
472           // here because we validate this in the .debug_info verifier.
473           continue;
474         } else {
475           auto Iter = StmtListToDie.find(LineTableOffset);
476           if (Iter != StmtListToDie.end()) {
477             Success = false;
478             OS << "error: two compile unit DIEs, "
479                << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
480                << format("0x%08" PRIx32, CUDie.getOffset())
481                << ", have the same DW_AT_stmt_list section offset:\n";
482             Iter->second.dump(OS, 0);
483             CUDie.dump(OS, 0);
484             OS << '\n';
485             // Already verified this line table before, no need to do it again.
486             continue;
487           }
488           StmtListToDie[LineTableOffset] = CUDie;
489         }
490       }
491       auto LineTable = DCtx.getLineTableForUnit(CU.get());
492       if (!LineTable) {
493         Success = false;
494         OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
495            << "] was not able to be parsed for CU:\n";
496         CUDie.dump(OS, 0);
497         OS << '\n';
498         continue;
499       }
500       uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
501       uint64_t PrevAddress = 0;
502       uint32_t RowIndex = 0;
503       for (const auto &Row : LineTable->Rows) {
504         if (Row.Address < PrevAddress) {
505           Success = false;
506           OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
507              << "] row[" << RowIndex
508              << "] decreases in address from previous row:\n";
509
510           DWARFDebugLine::Row::dumpTableHeader(OS);
511           if (RowIndex > 0)
512             LineTable->Rows[RowIndex - 1].dump(OS);
513           Row.dump(OS);
514           OS << '\n';
515         }
516
517         if (Row.File > MaxFileIndex) {
518           Success = false;
519           OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
520              << "][" << RowIndex << "] has invalid file index " << Row.File
521              << " (valid values are [1," << MaxFileIndex << "]):\n";
522           DWARFDebugLine::Row::dumpTableHeader(OS);
523           Row.dump(OS);
524           OS << '\n';
525         }
526         if (Row.EndSequence)
527           PrevAddress = 0;
528         else
529           PrevAddress = Row.Address;
530         ++RowIndex;
531       }
532     }
533     return Success;
534   }
535 };
536   
537 } // anonymous namespace
538
539 bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
540   bool Success = true;
541   DWARFVerifier verifier(OS, *this);
542   if (DumpType == DIDT_All || DumpType == DIDT_Info) {
543     if (!verifier.handleDebugInfo())
544       Success = false;
545   }
546   if (DumpType == DIDT_All || DumpType == DIDT_Line) {
547     if (!verifier.handleDebugLine())
548       Success = false;
549   }
550   return Success;
551 }
552 const DWARFUnitIndex &DWARFContext::getCUIndex() {
553   if (CUIndex)
554     return *CUIndex;
555
556   DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
557
558   CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
559   CUIndex->parse(CUIndexData);
560   return *CUIndex;
561 }
562
563 const DWARFUnitIndex &DWARFContext::getTUIndex() {
564   if (TUIndex)
565     return *TUIndex;
566
567   DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
568
569   TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
570   TUIndex->parse(TUIndexData);
571   return *TUIndex;
572 }
573
574 DWARFGdbIndex &DWARFContext::getGdbIndex() {
575   if (GdbIndex)
576     return *GdbIndex;
577
578   DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0);
579   GdbIndex = llvm::make_unique<DWARFGdbIndex>();
580   GdbIndex->parse(GdbIndexData);
581   return *GdbIndex;
582 }
583
584 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
585   if (Abbrev)
586     return Abbrev.get();
587
588   DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
589
590   Abbrev.reset(new DWARFDebugAbbrev());
591   Abbrev->extract(abbrData);
592   return Abbrev.get();
593 }
594
595 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
596   if (AbbrevDWO)
597     return AbbrevDWO.get();
598
599   DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
600   AbbrevDWO.reset(new DWARFDebugAbbrev());
601   AbbrevDWO->extract(abbrData);
602   return AbbrevDWO.get();
603 }
604
605 const DWARFDebugLoc *DWARFContext::getDebugLoc() {
606   if (Loc)
607     return Loc.get();
608
609   DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
610   Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
611   // assume all compile units have the same address byte size
612   if (getNumCompileUnits())
613     Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
614   return Loc.get();
615 }
616
617 const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
618   if (LocDWO)
619     return LocDWO.get();
620
621   DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
622   LocDWO.reset(new DWARFDebugLocDWO());
623   LocDWO->parse(LocData);
624   return LocDWO.get();
625 }
626
627 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
628   if (Aranges)
629     return Aranges.get();
630
631   Aranges.reset(new DWARFDebugAranges());
632   Aranges->generate(this);
633   return Aranges.get();
634 }
635
636 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
637   if (DebugFrame)
638     return DebugFrame.get();
639
640   // There's a "bug" in the DWARFv3 standard with respect to the target address
641   // size within debug frame sections. While DWARF is supposed to be independent
642   // of its container, FDEs have fields with size being "target address size",
643   // which isn't specified in DWARF in general. It's only specified for CUs, but
644   // .eh_frame can appear without a .debug_info section. Follow the example of
645   // other tools (libdwarf) and extract this from the container (ObjectFile
646   // provides this information). This problem is fixed in DWARFv4
647   // See this dwarf-discuss discussion for more details:
648   // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
649   DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
650                                getAddressSize());
651   DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
652   DebugFrame->parse(debugFrameData);
653   return DebugFrame.get();
654 }
655
656 const DWARFDebugFrame *DWARFContext::getEHFrame() {
657   if (EHFrame)
658     return EHFrame.get();
659
660   DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
661                                getAddressSize());
662   DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
663   DebugFrame->parse(debugFrameData);
664   return DebugFrame.get();
665 }
666
667 const DWARFDebugMacro *DWARFContext::getDebugMacro() {
668   if (Macro)
669     return Macro.get();
670
671   DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
672   Macro.reset(new DWARFDebugMacro());
673   Macro->parse(MacinfoData);
674   return Macro.get();
675 }
676
677 const DWARFLineTable *
678 DWARFContext::getLineTableForUnit(DWARFUnit *U) {
679   if (!Line)
680     Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
681
682   auto UnitDIE = U->getUnitDIE();
683   if (!UnitDIE)
684     return nullptr;
685
686   auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
687   if (!Offset)
688     return nullptr; // No line table for this compile unit.
689
690   uint32_t stmtOffset = *Offset + U->getLineTableOffset();
691   // See if the line table is cached.
692   if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
693     return lt;
694
695   // We have to parse it first.
696   DataExtractor lineData(U->getLineSection(), isLittleEndian(),
697                          U->getAddressByteSize());
698   return Line->getOrParseLineTable(lineData, stmtOffset);
699 }
700
701 void DWARFContext::parseCompileUnits() {
702   CUs.parse(*this, getInfoSection());
703 }
704
705 void DWARFContext::parseTypeUnits() {
706   if (!TUs.empty())
707     return;
708   for (const auto &I : getTypesSections()) {
709     TUs.emplace_back();
710     TUs.back().parse(*this, I.second);
711   }
712 }
713
714 void DWARFContext::parseDWOCompileUnits() {
715   DWOCUs.parseDWO(*this, getInfoDWOSection());
716 }
717
718 void DWARFContext::parseDWOTypeUnits() {
719   if (!DWOTUs.empty())
720     return;
721   for (const auto &I : getTypesDWOSections()) {
722     DWOTUs.emplace_back();
723     DWOTUs.back().parseDWO(*this, I.second);
724   }
725 }
726
727 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
728   parseCompileUnits();
729   return CUs.getUnitForOffset(Offset);
730 }
731
732 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
733   // First, get the offset of the compile unit.
734   uint32_t CUOffset = getDebugAranges()->findAddress(Address);
735   // Retrieve the compile unit.
736   return getCompileUnitForOffset(CUOffset);
737 }
738
739 static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
740                                                   uint64_t Address,
741                                                   FunctionNameKind Kind,
742                                                   std::string &FunctionName,
743                                                   uint32_t &StartLine) {
744   // The address may correspond to instruction in some inlined function,
745   // so we have to build the chain of inlined functions and take the
746   // name of the topmost function in it.
747   SmallVector<DWARFDie, 4> InlinedChain;
748   CU->getInlinedChainForAddress(Address, InlinedChain);
749   if (InlinedChain.empty())
750     return false;
751
752   const DWARFDie &DIE = InlinedChain[0];
753   bool FoundResult = false;
754   const char *Name = nullptr;
755   if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
756     FunctionName = Name;
757     FoundResult = true;
758   }
759   if (auto DeclLineResult = DIE.getDeclLine()) {
760     StartLine = DeclLineResult;
761     FoundResult = true;
762   }
763
764   return FoundResult;
765 }
766
767 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
768                                                DILineInfoSpecifier Spec) {
769   DILineInfo Result;
770
771   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
772   if (!CU)
773     return Result;
774   getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
775                                         Result.FunctionName,
776                                         Result.StartLine);
777   if (Spec.FLIKind != FileLineInfoKind::None) {
778     if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
779       LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
780                                            Spec.FLIKind, Result);
781   }
782   return Result;
783 }
784
785 DILineInfoTable
786 DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
787                                          DILineInfoSpecifier Spec) {
788   DILineInfoTable  Lines;
789   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
790   if (!CU)
791     return Lines;
792
793   std::string FunctionName = "<invalid>";
794   uint32_t StartLine = 0;
795   getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
796                                         StartLine);
797
798   // If the Specifier says we don't need FileLineInfo, just
799   // return the top-most function at the starting address.
800   if (Spec.FLIKind == FileLineInfoKind::None) {
801     DILineInfo Result;
802     Result.FunctionName = FunctionName;
803     Result.StartLine = StartLine;
804     Lines.push_back(std::make_pair(Address, Result));
805     return Lines;
806   }
807
808   const DWARFLineTable *LineTable = getLineTableForUnit(CU);
809
810   // Get the index of row we're looking for in the line table.
811   std::vector<uint32_t> RowVector;
812   if (!LineTable->lookupAddressRange(Address, Size, RowVector))
813     return Lines;
814
815   for (uint32_t RowIndex : RowVector) {
816     // Take file number and line/column from the row.
817     const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
818     DILineInfo Result;
819     LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
820                                   Spec.FLIKind, Result.FileName);
821     Result.FunctionName = FunctionName;
822     Result.Line = Row.Line;
823     Result.Column = Row.Column;
824     Result.StartLine = StartLine;
825     Lines.push_back(std::make_pair(Row.Address, Result));
826   }
827
828   return Lines;
829 }
830
831 DIInliningInfo
832 DWARFContext::getInliningInfoForAddress(uint64_t Address,
833                                         DILineInfoSpecifier Spec) {
834   DIInliningInfo InliningInfo;
835
836   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
837   if (!CU)
838     return InliningInfo;
839
840   const DWARFLineTable *LineTable = nullptr;
841   SmallVector<DWARFDie, 4> InlinedChain;
842   CU->getInlinedChainForAddress(Address, InlinedChain);
843   if (InlinedChain.size() == 0) {
844     // If there is no DIE for address (e.g. it is in unavailable .dwo file),
845     // try to at least get file/line info from symbol table.
846     if (Spec.FLIKind != FileLineInfoKind::None) {
847       DILineInfo Frame;
848       LineTable = getLineTableForUnit(CU);
849       if (LineTable &&
850           LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
851                                                Spec.FLIKind, Frame))
852         InliningInfo.addFrame(Frame);
853     }
854     return InliningInfo;
855   }
856
857   uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
858   for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
859     DWARFDie &FunctionDIE = InlinedChain[i];
860     DILineInfo Frame;
861     // Get function name if necessary.
862     if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
863       Frame.FunctionName = Name;
864     if (auto DeclLineResult = FunctionDIE.getDeclLine())
865       Frame.StartLine = DeclLineResult;
866     if (Spec.FLIKind != FileLineInfoKind::None) {
867       if (i == 0) {
868         // For the topmost frame, initialize the line table of this
869         // compile unit and fetch file/line info from it.
870         LineTable = getLineTableForUnit(CU);
871         // For the topmost routine, get file/line info from line table.
872         if (LineTable)
873           LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
874                                                Spec.FLIKind, Frame);
875       } else {
876         // Otherwise, use call file, call line and call column from
877         // previous DIE in inlined chain.
878         if (LineTable)
879           LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
880                                         Spec.FLIKind, Frame.FileName);
881         Frame.Line = CallLine;
882         Frame.Column = CallColumn;
883         Frame.Discriminator = CallDiscriminator;
884       }
885       // Get call file/line/column of a current DIE.
886       if (i + 1 < n) {
887         FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
888                                    CallDiscriminator);
889       }
890     }
891     InliningInfo.addFrame(Frame);
892   }
893   return InliningInfo;
894 }
895
896 static Error createError(const Twine &Reason, llvm::Error E) {
897   return make_error<StringError>(Reason + toString(std::move(E)),
898                                  inconvertibleErrorCode());
899 }
900
901 /// Returns the address of symbol relocation used against. Used for futher
902 /// relocations computation. Symbol's section load address is taken in account if
903 /// LoadedObjectInfo interface is provided.
904 static Expected<uint64_t> getSymbolAddress(const object::ObjectFile &Obj,
905                                            const RelocationRef &Reloc,
906                                            const LoadedObjectInfo *L) {
907   uint64_t Ret = 0;
908   object::section_iterator RSec = Obj.section_end();
909   object::symbol_iterator Sym = Reloc.getSymbol();
910
911   // First calculate the address of the symbol or section as it appears
912   // in the object file
913   if (Sym != Obj.symbol_end()) {
914     Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
915     if (!SymAddrOrErr)
916       return createError("error: failed to compute symbol address: ",
917                          SymAddrOrErr.takeError());
918
919     // Also remember what section this symbol is in for later
920     auto SectOrErr = Sym->getSection();
921     if (!SectOrErr)
922       return createError("error: failed to get symbol section: ",
923                          SectOrErr.takeError());
924
925     RSec = *SectOrErr;
926     Ret = *SymAddrOrErr;
927   } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
928     RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
929     Ret = RSec->getAddress();
930   }
931
932   // If we are given load addresses for the sections, we need to adjust:
933   // SymAddr = (Address of Symbol Or Section in File) -
934   //           (Address of Section in File) +
935   //           (Load Address of Section)
936   // RSec is now either the section being targeted or the section
937   // containing the symbol being targeted. In either case,
938   // we need to perform the same computation.
939   if (L && RSec != Obj.section_end())
940     if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
941       Ret += SectionLoadAddress - RSec->getAddress();
942   return Ret;
943 }
944
945 static bool isRelocScattered(const object::ObjectFile &Obj,
946                              const RelocationRef &Reloc) {
947   const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
948   if (!MachObj)
949     return false;
950   // MachO also has relocations that point to sections and
951   // scattered relocations.
952   auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
953   return MachObj->isRelocationScattered(RelocInfo);
954 }
955
956 DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
957     const LoadedObjectInfo *L)
958     : IsLittleEndian(Obj.isLittleEndian()),
959       AddressSize(Obj.getBytesInAddress()) {
960   for (const SectionRef &Section : Obj.sections()) {
961     StringRef name;
962     Section.getName(name);
963     // Skip BSS and Virtual sections, they aren't interesting.
964     bool IsBSS = Section.isBSS();
965     if (IsBSS)
966       continue;
967     bool IsVirtual = Section.isVirtual();
968     if (IsVirtual)
969       continue;
970     StringRef data;
971
972     section_iterator RelocatedSection = Section.getRelocatedSection();
973     // Try to obtain an already relocated version of this section.
974     // Else use the unrelocated section from the object file. We'll have to
975     // apply relocations ourselves later.
976     if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
977       Section.getContents(data);
978
979     if (Decompressor::isCompressed(Section)) {
980       Expected<Decompressor> Decompressor =
981           Decompressor::create(name, data, IsLittleEndian, AddressSize == 8);
982       if (!Decompressor)
983         continue;
984       SmallString<32> Out;
985       if (auto Err = Decompressor->decompress(Out))
986         continue;
987       UncompressedSections.emplace_back(std::move(Out));
988       data = UncompressedSections.back();
989     }
990
991     // Compressed sections names in GNU style starts from ".z",
992     // at this point section is decompressed and we drop compression prefix.
993     name = name.substr(
994         name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
995
996     if (StringRef *SectionData = MapSectionToMember(name)) {
997       *SectionData = data;
998       if (name == "debug_ranges") {
999         // FIXME: Use the other dwo range section when we emit it.
1000         RangeDWOSection.Data = data;
1001       }
1002     } else if (name == "debug_types") {
1003       // Find debug_types data by section rather than name as there are
1004       // multiple, comdat grouped, debug_types sections.
1005       TypesSections[Section].Data = data;
1006     } else if (name == "debug_types.dwo") {
1007       TypesDWOSections[Section].Data = data;
1008     }
1009
1010     if (RelocatedSection == Obj.section_end())
1011       continue;
1012
1013     StringRef RelSecName;
1014     StringRef RelSecData;
1015     RelocatedSection->getName(RelSecName);
1016
1017     // If the section we're relocating was relocated already by the JIT,
1018     // then we used the relocated version above, so we do not need to process
1019     // relocations for it now.
1020     if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
1021       continue;
1022
1023     // In Mach-o files, the relocations do not need to be applied if
1024     // there is no load offset to apply. The value read at the
1025     // relocation point already factors in the section address
1026     // (actually applying the relocations will produce wrong results
1027     // as the section address will be added twice).
1028     if (!L && isa<MachOObjectFile>(&Obj))
1029       continue;
1030
1031     RelSecName = RelSecName.substr(
1032         RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
1033
1034     // TODO: Add support for relocations in other sections as needed.
1035     // Record relocations for the debug_info and debug_line sections.
1036     RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
1037         .Case("debug_info", &InfoSection.Relocs)
1038         .Case("debug_loc", &LocSection.Relocs)
1039         .Case("debug_info.dwo", &InfoDWOSection.Relocs)
1040         .Case("debug_line", &LineSection.Relocs)
1041         .Case("debug_ranges", &RangeSection.Relocs)
1042         .Case("apple_names", &AppleNamesSection.Relocs)
1043         .Case("apple_types", &AppleTypesSection.Relocs)
1044         .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
1045         .Case("apple_namespac", &AppleNamespacesSection.Relocs)
1046         .Case("apple_objc", &AppleObjCSection.Relocs)
1047         .Default(nullptr);
1048     if (!Map) {
1049       // Find debug_types relocs by section rather than name as there are
1050       // multiple, comdat grouped, debug_types sections.
1051       if (RelSecName == "debug_types")
1052         Map = &TypesSections[*RelocatedSection].Relocs;
1053       else if (RelSecName == "debug_types.dwo")
1054         Map = &TypesDWOSections[*RelocatedSection].Relocs;
1055       else
1056         continue;
1057     }
1058
1059     if (Section.relocation_begin() != Section.relocation_end()) {
1060       uint64_t SectionSize = RelocatedSection->getSize();
1061       for (const RelocationRef &Reloc : Section.relocations()) {
1062         // FIXME: it's not clear how to correctly handle scattered
1063         // relocations.
1064         if (isRelocScattered(Obj, Reloc))
1065           continue;
1066
1067         Expected<uint64_t> SymAddrOrErr = getSymbolAddress(Obj, Reloc, L);
1068         if (!SymAddrOrErr) {
1069           errs() << toString(SymAddrOrErr.takeError()) << '\n';
1070           continue;
1071         }
1072
1073         object::RelocVisitor V(Obj);
1074         object::RelocToApply R(V.visit(Reloc.getType(), Reloc, *SymAddrOrErr));
1075         if (V.error()) {
1076           SmallString<32> Name;
1077           Reloc.getTypeName(Name);
1078           errs() << "error: failed to compute relocation: "
1079                  << Name << "\n";
1080           continue;
1081         }
1082         uint64_t Address = Reloc.getOffset();
1083         if (Address + R.Width > SectionSize) {
1084           errs() << "error: " << R.Width << "-byte relocation starting "
1085                  << Address << " bytes into section " << name << " which is "
1086                  << SectionSize << " bytes long.\n";
1087           continue;
1088         }
1089         if (R.Width > 8) {
1090           errs() << "error: can't handle a relocation of more than 8 bytes at "
1091                     "a time.\n";
1092           continue;
1093         }
1094         DEBUG(dbgs() << "Writing " << format("%p", R.Value)
1095                      << " at " << format("%p", Address)
1096                      << " with width " << format("%d", R.Width)
1097                      << "\n");
1098         Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
1099       }
1100     }
1101   }
1102 }
1103
1104 DWARFContextInMemory::DWARFContextInMemory(
1105     const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize,
1106     bool isLittleEndian)
1107     : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) {
1108   for (const auto &SecIt : Sections) {
1109     if (StringRef *SectionData = MapSectionToMember(SecIt.first()))
1110       *SectionData = SecIt.second->getBuffer();
1111   }
1112 }
1113
1114 StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) {
1115   return StringSwitch<StringRef *>(Name)
1116       .Case("debug_info", &InfoSection.Data)
1117       .Case("debug_abbrev", &AbbrevSection)
1118       .Case("debug_loc", &LocSection.Data)
1119       .Case("debug_line", &LineSection.Data)
1120       .Case("debug_aranges", &ARangeSection)
1121       .Case("debug_frame", &DebugFrameSection)
1122       .Case("eh_frame", &EHFrameSection)
1123       .Case("debug_str", &StringSection)
1124       .Case("debug_ranges", &RangeSection.Data)
1125       .Case("debug_macinfo", &MacinfoSection)
1126       .Case("debug_pubnames", &PubNamesSection)
1127       .Case("debug_pubtypes", &PubTypesSection)
1128       .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1129       .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1130       .Case("debug_info.dwo", &InfoDWOSection.Data)
1131       .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1132       .Case("debug_loc.dwo", &LocDWOSection.Data)
1133       .Case("debug_line.dwo", &LineDWOSection.Data)
1134       .Case("debug_str.dwo", &StringDWOSection)
1135       .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1136       .Case("debug_addr", &AddrSection)
1137       .Case("apple_names", &AppleNamesSection.Data)
1138       .Case("apple_types", &AppleTypesSection.Data)
1139       .Case("apple_namespaces", &AppleNamespacesSection.Data)
1140       .Case("apple_namespac", &AppleNamespacesSection.Data)
1141       .Case("apple_objc", &AppleObjCSection.Data)
1142       .Case("debug_cu_index", &CUIndexSection)
1143       .Case("debug_tu_index", &TUIndexSection)
1144       .Case("gdb_index", &GdbIndexSection)
1145       // Any more debug info sections go here.
1146       .Default(nullptr);
1147 }
1148
1149 void DWARFContextInMemory::anchor() {}