172c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer//===-- DWARFContext.cpp --------------------------------------------------===//
272c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer//
372c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer//                     The LLVM Compiler Infrastructure
472c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer//
572c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer// This file is distributed under the University of Illinois Open Source
672c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer// License. See LICENSE.TXT for details.
772c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer//
872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer//===----------------------------------------------------------------------===//
972c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
10ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/DebugInfo/DWARF/DWARFContext.h"
1171d94f805514f28730bf39143ee227648d521d09Alexey Samsonov#include "llvm/ADT/SmallString.h"
12784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov#include "llvm/ADT/StringSwitch.h"
13ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
14ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
15f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
16de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include "llvm/Object/MachO.h"
17de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include "llvm/Object/RelocVisitor.h"
18005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov#include "llvm/Support/Compression.h"
19fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramer#include "llvm/Support/Dwarf.h"
20de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include "llvm/Support/ELF.h"
2134f864fd382156331c61fbb6b7ae4828108b9d69Benjamin Kramer#include "llvm/Support/Format.h"
2271d94f805514f28730bf39143ee227648d521d09Alexey Samsonov#include "llvm/Support/Path.h"
23358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer#include "llvm/Support/raw_ostream.h"
24101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer#include <algorithm>
2572c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramerusing namespace llvm;
26fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramerusing namespace dwarf;
277486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindolausing namespace object;
2872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
29dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "dwarf"
30dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
31e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christophertypedef DWARFDebugLine::LineTable DWARFLineTable;
32dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestypedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
33dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestypedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
34e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher
359ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikiestatic void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
36c839df0e4cbc3329ae9f03ca4db5893ea9b2c7bdEric Christopher                           bool LittleEndian, bool GnuStyle) {
379ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie  OS << "\n." << Name << " contents:\n";
389ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie  DataExtractor pubNames(Data, LittleEndian, 0);
399ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie  uint32_t offset = 0;
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  while (pubNames.isValidOffset(offset)) {
4136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
4236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
4336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
4436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (GnuStyle)
4636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << "Offset     Linkage  Kind     Name\n";
4736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    else
4836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << "Offset     Name\n";
4936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
5036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    while (offset < Data.size()) {
5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      uint32_t dieRef = pubNames.getU32(&offset);
5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (dieRef == 0)
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        break;
5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << format("0x%8.8x ", dieRef);
5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (GnuStyle) {
5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines           << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines           << ' ';
6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      }
6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
62c839df0e4cbc3329ae9f03ca4db5893ea9b2c7bdEric Christopher    }
639ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie  }
649ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie}
659ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie
6637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic void dumpAccelSection(raw_ostream &OS, StringRef Name,
6737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                             const DWARFSection& Section, StringRef StringSection,
6837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                             bool LittleEndian) {
6937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  DataExtractor AccelSection(Section.Data, LittleEndian, 0);
7037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  DataExtractor StrData(StringSection, LittleEndian, 0);
7137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  OS << "\n." << Name << " contents:\n";
7237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!Accel.extract())
7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return;
7537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Accel.dump(OS);
7637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
7737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
78de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarvoid DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH) {
79939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
80939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << ".debug_abbrev contents:\n";
81939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    getDebugAbbrev()->dump(OS);
82939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
83358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
8436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
8536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
8636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << "\n.debug_abbrev.dwo contents:\n";
8736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      D->dump(OS);
8836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
8936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
90939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Info) {
91939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_info contents:\n";
9236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    for (const auto &CU : compile_units())
9336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      CU->dump(OS);
9436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
9536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
9636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
9736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      getNumDWOCompileUnits()) {
9836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << "\n.debug_info.dwo contents:\n";
9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    for (const auto &DWOCU : dwo_compile_units())
10036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      DWOCU->dump(OS);
101939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
102358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
10336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
104438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie    OS << "\n.debug_types contents:\n";
10537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    for (const auto &TUS : type_unit_sections())
10637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      for (const auto &TU : TUS)
10737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        TU->dump(OS);
10836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
10936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      getNumDWOTypeUnits()) {
11236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << "\n.debug_types.dwo contents:\n";
11337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    for (const auto &DWOTUS : dwo_type_unit_sections())
11437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      for (const auto &DWOTU : DWOTUS)
11537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        DWOTU->dump(OS);
116438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie  }
117438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie
1183df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
119438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie    OS << "\n.debug_loc contents:\n";
1203df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie    getDebugLoc()->dump(OS);
1213df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  }
1223df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie
12336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
12436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << "\n.debug_loc.dwo contents:\n";
12536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    getDebugLocDWO()->dump(OS);
12636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
12736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
12860bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
12960bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky    OS << "\n.debug_frame contents:\n";
13060bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky    getDebugFrame()->dump(OS);
131de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    if (DumpEH) {
132de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      OS << "\n.eh_frame contents:\n";
133de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      getEHFrame()->dump(OS);
134de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    }
13560bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  }
13660bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky
137f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
138f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    OS << "\n.debug_macinfo contents:\n";
139f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    getDebugMacro()->dump(OS);
140f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
141f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
142358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  uint32_t offset = 0;
143939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
144939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_aranges contents:\n";
145939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
146939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DWARFDebugArangeSet set;
147939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    while (set.extract(arangesData, &offset))
148939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      set.dump(OS);
149939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
150b848e976110a2c4f0a6a9e252115ba291c844fbeBenjamin Kramer
151eceb5b99777ba944a0ae3748a0371e9a3aa94d56Alexey Samsonov  uint8_t savedAddressByteSize = 0;
152939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Line) {
153939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_line contents:\n";
15436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    for (const auto &CU : compile_units()) {
15536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      savedAddressByteSize = CU->getAddressByteSize();
1566948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      const auto *CUDIE = CU->getUnitDIE();
1576948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      if (CUDIE == nullptr)
1586948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        continue;
1596948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      unsigned stmtOffset = CUDIE->getAttributeValueAsSectionOffset(
1606948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar          CU.get(), DW_AT_stmt_list, -1U);
161939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      if (stmtOffset != -1U) {
1629528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie        DataExtractor lineData(getLineSection().Data, isLittleEndian(),
163939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky                               savedAddressByteSize);
164dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        DWARFDebugLine::LineTable LineTable;
165dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        LineTable.parse(lineData, &getLineSection().Relocs, &stmtOffset);
166dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        LineTable.dump(OS);
167939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      }
168fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramer    }
169fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramer  }
17034f864fd382156331c61fbb6b7ae4828108b9d69Benjamin Kramer
171f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
172f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    OS << "\n.debug_cu_index contents:\n";
173f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    getCUIndex().dump(OS);
174f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
175f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
176f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
177f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    OS << "\n.debug_tu_index contents:\n";
178f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    getTUIndex().dump(OS);
179f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
180f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
18136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
18236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << "\n.debug_line.dwo contents:\n";
18336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    unsigned stmtOffset = 0;
18436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
18536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                           savedAddressByteSize);
186dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DWARFDebugLine::LineTable LineTable;
187dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
188dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      LineTable.dump(OS);
189dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      LineTable.clear();
190dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    }
19136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
19236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
193939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Str) {
194939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_str contents:\n";
195939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DataExtractor strData(getStringSection(), isLittleEndian(), 0);
196939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    offset = 0;
197939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    uint32_t strOffset = 0;
198939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    while (const char *s = strData.getCStr(&offset)) {
199939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
200939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      strOffset = offset;
201939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    }
20234f864fd382156331c61fbb6b7ae4828108b9d69Benjamin Kramer  }
203eceb5b99777ba944a0ae3748a0371e9a3aa94d56Alexey Samsonov
20436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
20536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      !getStringDWOSection().empty()) {
20636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << "\n.debug_str.dwo contents:\n";
20736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
20836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    offset = 0;
20936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    uint32_t strDWOOffset = 0;
21036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    while (const char *s = strDWOData.getCStr(&offset)) {
21136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
21236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      strDWOOffset = offset;
21336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
21436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
21536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
216939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
217939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_ranges contents:\n";
218939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // In fact, different compile units may have different address byte
219939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // sizes, but for simplicity we just use the address byte size of the last
220939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // compile unit (there is no easy and fast way to associate address range
221939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // list and the compile unit it describes).
222939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DataExtractor rangesData(getRangeSection(), isLittleEndian(),
223939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky                             savedAddressByteSize);
224939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    offset = 0;
225939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DWARFDebugRangeList rangeList;
226939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    while (rangeList.extract(rangesData, &offset))
227939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      rangeList.dump(OS);
22882de10a34c9432029040ced17129079a7d80904eEric Christopher  }
22972f7bfbf0e02bb11d3e7cca1f9598c5f9d9fa2caEric Christopher
230c839df0e4cbc3329ae9f03ca4db5893ea9b2c7bdEric Christopher  if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
231c839df0e4cbc3329ae9f03ca4db5893ea9b2c7bdEric Christopher    dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
232c839df0e4cbc3329ae9f03ca4db5893ea9b2c7bdEric Christopher                   isLittleEndian(), false);
233e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek
2347357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher  if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
2357357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher    dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
2367357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher                   isLittleEndian(), false);
2377357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher
2389ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie  if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
2399ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie    dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
240c839df0e4cbc3329ae9f03ca4db5893ea9b2c7bdEric Christopher                   isLittleEndian(), true /* GnuStyle */);
2419ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie
2429ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie  if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
2439ddf28d501144276fb47ce2e5f48f2497d9898d5David Blaikie    dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
244c839df0e4cbc3329ae9f03ca4db5893ea9b2c7bdEric Christopher                   isLittleEndian(), true /* GnuStyle */);
245994c37fcb001bc5a53bf2c676009b327b882d765David Blaikie
24636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
24736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      !getStringOffsetDWOSection().empty()) {
24836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    OS << "\n.debug_str_offsets.dwo contents:\n";
24936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
25036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                               0);
25136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    offset = 0;
25236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    uint64_t size = getStringOffsetDWOSection().size();
25336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    while (offset < size) {
25436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << format("0x%8.8x: ", offset);
25536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
25693f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    }
257939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
25837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
25937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
26037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
26137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                     getStringSection(), isLittleEndian());
26237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
26337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
26437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
26537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                     getStringSection(), isLittleEndian());
26637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
26737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
26837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
26937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                     getStringSection(), isLittleEndian());
27037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
27137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
27237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
27337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                     getStringSection(), isLittleEndian());
27472c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer}
27572c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
276f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarconst DWARFUnitIndex &DWARFContext::getCUIndex() {
277f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (CUIndex)
278f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return *CUIndex;
279f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
280f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
281f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
282f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
283f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  CUIndex->parse(CUIndexData);
284f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  return *CUIndex;
285f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
286f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
287f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarconst DWARFUnitIndex &DWARFContext::getTUIndex() {
288f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (TUIndex)
289f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return *TUIndex;
290f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
291f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
292f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
293f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
294f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TUIndex->parse(TUIndexData);
295f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  return *TUIndex;
296f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
297f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
29872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramerconst DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
29972c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  if (Abbrev)
30072c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer    return Abbrev.get();
30172c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
30272c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
30372c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
30472c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  Abbrev.reset(new DWARFDebugAbbrev());
305dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Abbrev->extract(abbrData);
30672c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  return Abbrev.get();
30772c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer}
30872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
30982de10a34c9432029040ced17129079a7d80904eEric Christopherconst DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
31082de10a34c9432029040ced17129079a7d80904eEric Christopher  if (AbbrevDWO)
31182de10a34c9432029040ced17129079a7d80904eEric Christopher    return AbbrevDWO.get();
31282de10a34c9432029040ced17129079a7d80904eEric Christopher
31382de10a34c9432029040ced17129079a7d80904eEric Christopher  DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
31482de10a34c9432029040ced17129079a7d80904eEric Christopher  AbbrevDWO.reset(new DWARFDebugAbbrev());
315dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  AbbrevDWO->extract(abbrData);
31682de10a34c9432029040ced17129079a7d80904eEric Christopher  return AbbrevDWO.get();
31782de10a34c9432029040ced17129079a7d80904eEric Christopher}
31882de10a34c9432029040ced17129079a7d80904eEric Christopher
3193df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikieconst DWARFDebugLoc *DWARFContext::getDebugLoc() {
3203df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  if (Loc)
3213df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie    return Loc.get();
3223df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie
3239528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie  DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
3249528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie  Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
3253df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  // assume all compile units have the same address byte size
3263df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  if (getNumCompileUnits())
3273df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie    Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
3283df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  return Loc.get();
3293df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie}
3303df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie
33136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesconst DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
33236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (LocDWO)
33336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return LocDWO.get();
33436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
33536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
33636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LocDWO.reset(new DWARFDebugLocDWO());
33736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LocDWO->parse(LocData);
33836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  return LocDWO.get();
33936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
34036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
341358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramerconst DWARFDebugAranges *DWARFContext::getDebugAranges() {
342358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  if (Aranges)
343358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer    return Aranges.get();
344358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
345358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  Aranges.reset(new DWARFDebugAranges());
34663a450a313a9b0a08622e97b53f5dd83f9266143Alexey Samsonov  Aranges->generate(this);
347358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  return Aranges.get();
348358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer}
349358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
35060bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Benderskyconst DWARFDebugFrame *DWARFContext::getDebugFrame() {
35160bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  if (DebugFrame)
35260bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky    return DebugFrame.get();
35360bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky
35460bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // There's a "bug" in the DWARFv3 standard with respect to the target address
35560bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // size within debug frame sections. While DWARF is supposed to be independent
35660bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // of its container, FDEs have fields with size being "target address size",
35760bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // which isn't specified in DWARF in general. It's only specified for CUs, but
35860bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // .eh_frame can appear without a .debug_info section. Follow the example of
35960bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // other tools (libdwarf) and extract this from the container (ObjectFile
36060bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // provides this information). This problem is fixed in DWARFv4
36160bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // See this dwarf-discuss discussion for more details:
36260bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
36360bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
36460bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky                               getAddressSize());
365de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
366de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  DebugFrame->parse(debugFrameData);
367de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return DebugFrame.get();
368de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
369de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
370de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarconst DWARFDebugFrame *DWARFContext::getEHFrame() {
371de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (EHFrame)
372de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return EHFrame.get();
373de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
374de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
375de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                               getAddressSize());
376de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
37760bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  DebugFrame->parse(debugFrameData);
37860bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  return DebugFrame.get();
37960bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky}
38060bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky
381f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarconst DWARFDebugMacro *DWARFContext::getDebugMacro() {
382f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (Macro)
383f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return Macro.get();
384f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
385f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
386f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Macro.reset(new DWARFDebugMacro());
387f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Macro->parse(MacinfoData);
388f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  return Macro.get();
389f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
390f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
391e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopherconst DWARFLineTable *
3926948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga NainarDWARFContext::getLineTableForUnit(DWARFUnit *U) {
393c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  if (!Line)
3949528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie    Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
395f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
3966948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  const auto *UnitDIE = U->getUnitDIE();
3976948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  if (UnitDIE == nullptr)
3986948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    return nullptr;
399f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
400c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  unsigned stmtOffset =
4016948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      UnitDIE->getAttributeValueAsSectionOffset(U, DW_AT_stmt_list, -1U);
402c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  if (stmtOffset == -1U)
403dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr; // No line table for this compile unit.
404c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer
405f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  stmtOffset += U->getLineTableOffset();
406c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  // See if the line table is cached.
407e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher  if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
408c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer    return lt;
409c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer
410c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  // We have to parse it first.
411f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  DataExtractor lineData(U->getLineSection(), isLittleEndian(),
4126948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar                         U->getAddressByteSize());
413c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  return Line->getOrParseLineTable(lineData, stmtOffset);
414b848e976110a2c4f0a6a9e252115ba291c844fbeBenjamin Kramer}
415b848e976110a2c4f0a6a9e252115ba291c844fbeBenjamin Kramer
41672c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramervoid DWARFContext::parseCompileUnits() {
41737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  CUs.parse(*this, getInfoSection());
41872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer}
419101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer
420438f5391b2d502a132a4a20469e9e56305b221a4David Blaikievoid DWARFContext::parseTypeUnits() {
42136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (!TUs.empty())
42236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return;
42336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (const auto &I : getTypesSections()) {
4246948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    TUs.emplace_back();
42537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    TUs.back().parse(*this, I.second);
426438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie  }
427438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie}
428438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie
42982de10a34c9432029040ced17129079a7d80904eEric Christophervoid DWARFContext::parseDWOCompileUnits() {
43037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  DWOCUs.parseDWO(*this, getInfoDWOSection());
43182de10a34c9432029040ced17129079a7d80904eEric Christopher}
43282de10a34c9432029040ced17129079a7d80904eEric Christopher
43336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid DWARFContext::parseDWOTypeUnits() {
43436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (!DWOTUs.empty())
43536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return;
43636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (const auto &I : getTypesDWOSections()) {
4376948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    DWOTUs.emplace_back();
43837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    DWOTUs.back().parseDWO(*this, I.second);
43936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
44036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
44136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
44238a6381c0a58e013577b1957199128af9573fc20Alexey SamsonovDWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
44336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  parseCompileUnits();
44437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return CUs.getUnitForOffset(Offset);
445101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer}
446101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer
44738a6381c0a58e013577b1957199128af9573fc20Alexey SamsonovDWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
4489013db3399d2847899ba32daf58844bba1aef6ddBenjamin Kramer  // First, get the offset of the compile unit.
44938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  uint32_t CUOffset = getDebugAranges()->findAddress(Address);
450101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer  // Retrieve the compile unit.
45138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  return getCompileUnitForOffset(CUOffset);
45238a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov}
45338a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov
454dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic bool getFunctionNameForAddress(DWARFCompileUnit *CU, uint64_t Address,
455dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                      FunctionNameKind Kind,
456dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                      std::string &FunctionName) {
457dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (Kind == FunctionNameKind::None)
458dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return false;
459dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // The address may correspond to instruction in some inlined function,
460dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // so we have to build the chain of inlined functions and take the
461dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // name of the topmost function in it.
462dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const DWARFDebugInfoEntryInlinedChain &InlinedChain =
463dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      CU->getInlinedChainForAddress(Address);
464dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (InlinedChain.DIEs.size() == 0)
465dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return false;
466dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
467dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (const char *Name =
468dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines          TopFunctionDIE.getSubroutineName(InlinedChain.U, Kind)) {
469dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    FunctionName = Name;
470dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return true;
471dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
472dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return false;
473dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
474dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
47538a6381c0a58e013577b1957199128af9573fc20Alexey SamsonovDILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
476dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                               DILineInfoSpecifier Spec) {
477dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  DILineInfo Result;
478dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
47938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
48038a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (!CU)
481dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return Result;
482dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  getFunctionNameForAddress(CU, Address, Spec.FNKind, Result.FunctionName);
483dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (Spec.FLIKind != FileLineInfoKind::None) {
48437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
48537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
48637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                           Spec.FLIKind, Result);
48738a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  }
488dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return Result;
489101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer}
4902d24e2a396a1d211baaeedf32148a3b657240170David Blaikie
491dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesDILineInfoTable
492dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesDWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
493dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                         DILineInfoSpecifier Spec) {
494e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  DILineInfoTable  Lines;
495e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
496e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  if (!CU)
497e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    return Lines;
498e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
499e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  std::string FunctionName = "<invalid>";
500dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  getFunctionNameForAddress(CU, Address, Spec.FNKind, FunctionName);
501e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
502e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  // If the Specifier says we don't need FileLineInfo, just
503e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  // return the top-most function at the starting address.
504dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (Spec.FLIKind == FileLineInfoKind::None) {
505dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DILineInfo Result;
506dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Result.FunctionName = FunctionName;
507dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Lines.push_back(std::make_pair(Address, Result));
508e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    return Lines;
509e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  }
510e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
51137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const DWARFLineTable *LineTable = getLineTableForUnit(CU);
512e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
513e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  // Get the index of row we're looking for in the line table.
514e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  std::vector<uint32_t> RowVector;
515e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  if (!LineTable->lookupAddressRange(Address, Size, RowVector))
516e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    return Lines;
517e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
51836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (uint32_t RowIndex : RowVector) {
519e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    // Take file number and line/column from the row.
520e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
521dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DILineInfo Result;
52237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
52337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                  Spec.FLIKind, Result.FileName);
524dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Result.FunctionName = FunctionName;
525dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Result.Line = Row.Line;
526dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Result.Column = Row.Column;
527dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Lines.push_back(std::make_pair(Row.Address, Result));
528e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  }
529e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
530e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  return Lines;
531e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor}
532e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
533dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesDIInliningInfo
534dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesDWARFContext::getInliningInfoForAddress(uint64_t Address,
535dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                        DILineInfoSpecifier Spec) {
536dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  DIInliningInfo InliningInfo;
537dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
5385eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
5395eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  if (!CU)
540dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return InliningInfo;
5415eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov
542dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const DWARFLineTable *LineTable = nullptr;
543e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov  const DWARFDebugInfoEntryInlinedChain &InlinedChain =
5445eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      CU->getInlinedChainForAddress(Address);
545dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (InlinedChain.DIEs.size() == 0) {
546dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // If there is no DIE for address (e.g. it is in unavailable .dwo file),
547dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // try to at least get file/line info from symbol table.
548dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Spec.FLIKind != FileLineInfoKind::None) {
549dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      DILineInfo Frame;
55037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      LineTable = getLineTableForUnit(CU);
55137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      if (LineTable &&
55237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines          LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
55337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                               Spec.FLIKind, Frame))
554dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        InliningInfo.addFrame(Frame);
555dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    }
556dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return InliningInfo;
557dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
5585eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov
5595eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
560e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov  for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
561e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
562dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DILineInfo Frame;
5635eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    // Get function name if necessary.
564dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (const char *Name =
565dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            FunctionDIE.getSubroutineName(InlinedChain.U, Spec.FNKind))
566dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Frame.FunctionName = Name;
567dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Spec.FLIKind != FileLineInfoKind::None) {
5685eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      if (i == 0) {
5695eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // For the topmost frame, initialize the line table of this
5705eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // compile unit and fetch file/line info from it.
57137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        LineTable = getLineTableForUnit(CU);
5725eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // For the topmost routine, get file/line info from line table.
57337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        if (LineTable)
57437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines          LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
57537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                               Spec.FLIKind, Frame);
5765eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      } else {
5775eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // Otherwise, use call file, call line and call column from
5785eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // previous DIE in inlined chain.
57937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        if (LineTable)
58037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines          LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
58137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                        Spec.FLIKind, Frame.FileName);
582dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        Frame.Line = CallLine;
583dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        Frame.Column = CallColumn;
5845eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      }
5855eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      // Get call file/line/column of a current DIE.
5865eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      if (i + 1 < n) {
587cd7c4980d4ee2d22d92a4907f2d029e67b52d732David Blaikie        FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
588e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov                                   CallColumn);
5895eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      }
5905eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    }
5915eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    InliningInfo.addFrame(Frame);
5925eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  }
5935eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  return InliningInfo;
5945eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov}
5955eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov
596de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarstatic bool consumeCompressedGnuHeader(StringRef &data,
597de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                       uint64_t &OriginalSize) {
598005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  // Consume "ZLIB" prefix.
599005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  if (!data.startswith("ZLIB"))
600005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    return false;
601005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  data = data.substr(4);
602005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  // Consume uncompressed section size (big-endian 8 bytes).
603005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  DataExtractor extractor(data, false, 8);
604005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  uint32_t Offset = 0;
605005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  OriginalSize = extractor.getU64(&Offset);
606005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  if (Offset == 0)
607005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    return false;
608005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  data = data.substr(Offset);
609005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  return true;
610005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov}
611005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov
612de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarstatic bool consumeCompressedZLibHeader(StringRef &Data, uint64_t &OriginalSize,
613de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                        bool IsLE, bool Is64Bit) {
614de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  using namespace ELF;
615de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
616de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (Data.size() < HdrSize)
617de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return false;
618de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
619de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  DataExtractor Extractor(Data, IsLE, 0);
620de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  uint32_t Offset = 0;
621de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
622de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                             : sizeof(Elf32_Word)) !=
623de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      ELFCOMPRESS_ZLIB)
624de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return false;
625de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
626de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  // Skip Elf64_Chdr::ch_reserved field.
627de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (Is64Bit)
628de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    Offset += sizeof(Elf64_Word);
629de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
630de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  OriginalSize = Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Xword)
631de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                                        : sizeof(Elf32_Word));
632de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Data = Data.substr(HdrSize);
633de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return true;
634de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
635de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
636de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarstatic bool tryDecompress(StringRef &Name, StringRef &Data,
637de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                          SmallString<32> &Out, bool ZLibStyle, bool IsLE,
638de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                          bool Is64Bit) {
639de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (!zlib::isAvailable())
640de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return false;
641de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
642de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  uint64_t OriginalSize;
643de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool Result =
644de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      ZLibStyle ? consumeCompressedZLibHeader(Data, OriginalSize, IsLE, Is64Bit)
645de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                : consumeCompressedGnuHeader(Data, OriginalSize);
646de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
647de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (!Result || zlib::uncompress(Data, Out, OriginalSize) != zlib::StatusOK)
648de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return false;
649de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
650de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  // gnu-style names are started from "z", consume that.
651de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (!ZLibStyle)
652de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    Name = Name.substr(1);
653de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return true;
654de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
655de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
6566948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga NainarDWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
6576948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    const LoadedObjectInfo *L)
65837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    : IsLittleEndian(Obj.isLittleEndian()),
65937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      AddressSize(Obj.getBytesInAddress()) {
66037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  for (const SectionRef &Section : Obj.sections()) {
661d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    StringRef name;
66236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    Section.getName(name);
66337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // Skip BSS and Virtual sections, they aren't interesting.
66437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    bool IsBSS = Section.isBSS();
66537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (IsBSS)
66637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      continue;
66737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    bool IsVirtual = Section.isVirtual();
66837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (IsVirtual)
66937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      continue;
670d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    StringRef data;
6716948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
672f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    section_iterator RelocatedSection = Section.getRelocatedSection();
6736948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    // Try to obtain an already relocated version of this section.
6746948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    // Else use the unrelocated section from the object file. We'll have to
6756948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    // apply relocations ourselves later.
676f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
6776948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      Section.getContents(data);
678d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
679d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
680784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov
681de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    bool ZLibStyleCompressed = Section.isCompressed();
682de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    if (ZLibStyleCompressed || name.startswith("zdebug_")) {
683de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      SmallString<32> Out;
684de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      if (!tryDecompress(name, data, Out, ZLibStyleCompressed, IsLittleEndian,
685de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                         AddressSize == 8))
686005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov        continue;
687de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      UncompressedSections.emplace_back(std::move(Out));
688dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      data = UncompressedSections.back();
689005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    }
690005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov
69136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    StringRef *SectionData =
6927357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher        StringSwitch<StringRef *>(name)
6937357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_info", &InfoSection.Data)
6947357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_abbrev", &AbbrevSection)
6957357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_loc", &LocSection.Data)
6967357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_line", &LineSection.Data)
6977357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_aranges", &ARangeSection)
6987357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_frame", &DebugFrameSection)
699de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            .Case("eh_frame", &EHFrameSection)
7007357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_str", &StringSection)
7017357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_ranges", &RangeSection)
702f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            .Case("debug_macinfo", &MacinfoSection)
7037357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_pubnames", &PubNamesSection)
7047357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_pubtypes", &PubTypesSection)
7057357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_gnu_pubnames", &GnuPubNamesSection)
7067357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
7077357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_info.dwo", &InfoDWOSection.Data)
7087357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_abbrev.dwo", &AbbrevDWOSection)
70936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines            .Case("debug_loc.dwo", &LocDWOSection.Data)
71036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines            .Case("debug_line.dwo", &LineDWOSection.Data)
7117357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_str.dwo", &StringDWOSection)
7127357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
7137357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            .Case("debug_addr", &AddrSection)
71437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines            .Case("apple_names", &AppleNamesSection.Data)
71537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines            .Case("apple_types", &AppleTypesSection.Data)
71637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines            .Case("apple_namespaces", &AppleNamespacesSection.Data)
71737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines            .Case("apple_namespac", &AppleNamespacesSection.Data)
71837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines            .Case("apple_objc", &AppleObjCSection.Data)
719f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            .Case("debug_cu_index", &CUIndexSection)
720f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            .Case("debug_tu_index", &TUIndexSection)
7217357f03e888e7d95066ca1bbb26994c278eb465cEric Christopher            // Any more debug info sections go here.
722dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            .Default(nullptr);
72336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (SectionData) {
72436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      *SectionData = data;
7257486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      if (name == "debug_ranges") {
7267486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola        // FIXME: Use the other dwo range section when we emit it.
7277486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola        RangeDWOSection = data;
7287486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      }
729438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie    } else if (name == "debug_types") {
73075331656124ddfdc7829b4dd430083e140bd8549David Blaikie      // Find debug_types data by section rather than name as there are
73175331656124ddfdc7829b4dd430083e140bd8549David Blaikie      // multiple, comdat grouped, debug_types sections.
73236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      TypesSections[Section].Data = data;
73336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    } else if (name == "debug_types.dwo") {
73436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      TypesDWOSections[Section].Data = data;
73582de10a34c9432029040ced17129079a7d80904eEric Christopher    }
736d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
73737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (RelocatedSection == Obj.section_end())
7387486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      continue;
7397486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola
7407486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    StringRef RelSecName;
7416948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    StringRef RelSecData;
7427486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    RelocatedSection->getName(RelSecName);
7436948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
7446948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    // If the section we're relocating was relocated already by the JIT,
7456948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    // then we used the relocated version above, so we do not need to process
7466948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    // relocations for it now.
747f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
748f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      continue;
749f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
750f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // In Mach-o files, the relocations do not need to be applied if
751f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // there is no load offset to apply. The value read at the
752f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // relocation point already factors in the section address
753f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // (actually applying the relocations will produce wrong results
754f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // as the section address will be added twice).
755f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (!L && isa<MachOObjectFile>(&Obj))
7566948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      continue;
7576948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
7587486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    RelSecName = RelSecName.substr(
7597486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola        RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
7607486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola
761ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor    // TODO: Add support for relocations in other sections as needed.
762ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor    // Record relocations for the debug_info and debug_line sections.
7637486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
7649528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie        .Case("debug_info", &InfoSection.Relocs)
7659528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie        .Case("debug_loc", &LocSection.Relocs)
7669528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie        .Case("debug_info.dwo", &InfoDWOSection.Relocs)
7679528b0e466ace36268abe9d011fffc67d831088cDavid Blaikie        .Case("debug_line", &LineSection.Relocs)
76837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        .Case("apple_names", &AppleNamesSection.Relocs)
76937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        .Case("apple_types", &AppleTypesSection.Relocs)
77037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
77137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        .Case("apple_namespac", &AppleNamespacesSection.Relocs)
77237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        .Case("apple_objc", &AppleObjCSection.Relocs)
773dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        .Default(nullptr);
774438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie    if (!Map) {
77575331656124ddfdc7829b4dd430083e140bd8549David Blaikie      // Find debug_types relocs by section rather than name as there are
77675331656124ddfdc7829b4dd430083e140bd8549David Blaikie      // multiple, comdat grouped, debug_types sections.
77736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (RelSecName == "debug_types")
77836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        Map = &TypesSections[*RelocatedSection].Relocs;
77936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      else if (RelSecName == "debug_types.dwo")
78036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        Map = &TypesDWOSections[*RelocatedSection].Relocs;
78136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      else
78236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        continue;
783438f5391b2d502a132a4a20469e9e56305b221a4David Blaikie    }
784d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
78536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (Section.relocation_begin() != Section.relocation_end()) {
78637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      uint64_t SectionSize = RelocatedSection->getSize();
78736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      for (const RelocationRef &Reloc : Section.relocations()) {
788f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar        uint64_t Address = Reloc.getOffset();
789f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar        uint64_t Type = Reloc.getType();
790ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor        uint64_t SymAddr = 0;
7916948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        uint64_t SectionLoadAddress = 0;
79237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        object::symbol_iterator Sym = Reloc.getSymbol();
793f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar        object::section_iterator RSec = Obj.section_end();
7946948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
7956948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        // First calculate the address of the symbol or section as it appears
7966948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        // in the objct file
7976948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        if (Sym != Obj.symbol_end()) {
798de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar          Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
799de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar          if (!SymAddrOrErr) {
800de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            std::string Buf;
801de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            raw_string_ostream OS(Buf);
802de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            logAllUnhandledErrors(SymAddrOrErr.takeError(), OS, "");
803de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            OS.flush();
804f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            errs() << "error: failed to compute symbol address: "
805de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                   << Buf << '\n';
806f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            continue;
807f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          }
808f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          SymAddr = *SymAddrOrErr;
8096948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar          // Also remember what section this symbol is in for later
810de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar          auto SectOrErr = Sym->getSection();
811de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar          if (!SectOrErr) {
812de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            std::string Buf;
813de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            raw_string_ostream OS(Buf);
814de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            logAllUnhandledErrors(SectOrErr.takeError(), OS, "");
815de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            OS.flush();
816de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            errs() << "error: failed to get symbol section: "
817de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                   << Buf << '\n';
818de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar            continue;
819de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar          }
820de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar          RSec = *SectOrErr;
821f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar        } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
822f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          // MachO also has relocations that point to sections and
823f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          // scattered relocations.
824f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          auto RelocInfo = MObj->getRelocation(Reloc.getRawDataRefImpl());
825f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          if (MObj->isRelocationScattered(RelocInfo)) {
826f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            // FIXME: it's not clear how to correctly handle scattered
827f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            // relocations.
828f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            continue;
829f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          } else {
830f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
831f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            SymAddr = RSec->getAddress();
832f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          }
833f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar        }
8346948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
8356948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        // If we are given load addresses for the sections, we need to adjust:
8366948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        // SymAddr = (Address of Symbol Or Section in File) -
8376948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        //           (Address of Section in File) +
8386948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        //           (Load Address of Section)
8396948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        if (L != nullptr && RSec != Obj.section_end()) {
840f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          // RSec is now either the section being targeted or the section
841f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          // containing the symbol being targeted. In either case,
8426948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar          // we need to perform the same computation.
8436948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar          StringRef SecName;
8446948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar          RSec->getName(SecName);
845f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//           llvm::dbgs() << "Name: '" << SecName
846f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//                        << "', RSec: " << RSec->getRawDataRefImpl()
847f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//                        << ", Section: " << Section.getRawDataRefImpl() << "\n";
848f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          SectionLoadAddress = L->getSectionLoadAddress(*RSec);
8496948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar          if (SectionLoadAddress != 0)
8506948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar            SymAddr += SectionLoadAddress - RSec->getAddress();
8516948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar        }
852d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
85337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        object::RelocVisitor V(Obj);
85437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        object::RelocToApply R(V.visit(Type, Reloc, SymAddr));
855d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        if (V.error()) {
856d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          SmallString<32> Name;
857f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar          Reloc.getTypeName(Name);
858d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          errs() << "error: failed to compute relocation: "
859d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                 << Name << "\n";
860d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          continue;
861d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        }
862d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
863d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        if (Address + R.Width > SectionSize) {
864d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          errs() << "error: " << R.Width << "-byte relocation starting "
865d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                 << Address << " bytes into section " << name << " which is "
866d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                 << SectionSize << " bytes long.\n";
867d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          continue;
868d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        }
869d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        if (R.Width > 8) {
870d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          errs() << "error: can't handle a relocation of more than 8 bytes at "
871d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                    "a time.\n";
872d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          continue;
873d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        }
874d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        DEBUG(dbgs() << "Writing " << format("%p", R.Value)
875d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                     << " at " << format("%p", Address)
876d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                     << " with width " << format("%d", R.Width)
877d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                     << "\n");
87882de10a34c9432029040ced17129079a7d80904eEric Christopher        Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
879d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher      }
880d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    }
881d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher  }
882d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher}
883d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
8842d24e2a396a1d211baaeedf32148a3b657240170David Blaikievoid DWARFContextInMemory::anchor() { }
885