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
1072c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer#include "DWARFContext.h"
1171d94f805514f28730bf39143ee227648d521d09Alexey Samsonov#include "llvm/ADT/SmallString.h"
12784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov#include "llvm/ADT/StringSwitch.h"
13005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov#include "llvm/ADT/STLExtras.h"
14005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov#include "llvm/Support/Compression.h"
15fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramer#include "llvm/Support/Dwarf.h"
1634f864fd382156331c61fbb6b7ae4828108b9d69Benjamin Kramer#include "llvm/Support/Format.h"
1771d94f805514f28730bf39143ee227648d521d09Alexey Samsonov#include "llvm/Support/Path.h"
18358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer#include "llvm/Support/raw_ostream.h"
19101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer#include <algorithm>
2072c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramerusing namespace llvm;
21fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramerusing namespace dwarf;
227486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindolausing namespace object;
2372c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
24e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christophertypedef DWARFDebugLine::LineTable DWARFLineTable;
25e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher
26939a4e8b693820d161f362317f7dba9057e66cc7Eli Benderskyvoid DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
27939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
28939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << ".debug_abbrev contents:\n";
29939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    getDebugAbbrev()->dump(OS);
30939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
31358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
32939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Info) {
33939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_info contents:\n";
34939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
35939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      getCompileUnitAtIndex(i)->dump(OS);
36939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
37358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
383df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
393df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie    OS << ".debug_loc contents:\n";
403df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie    getDebugLoc()->dump(OS);
413df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  }
423df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie
4360bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
4460bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky    OS << "\n.debug_frame contents:\n";
4560bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky    getDebugFrame()->dump(OS);
4660bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  }
4760bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky
48358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  uint32_t offset = 0;
49939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
50939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_aranges contents:\n";
51939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
52939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DWARFDebugArangeSet set;
53939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    while (set.extract(arangesData, &offset))
54939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      set.dump(OS);
55939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
56b848e976110a2c4f0a6a9e252115ba291c844fbeBenjamin Kramer
57eceb5b99777ba944a0ae3748a0371e9a3aa94d56Alexey Samsonov  uint8_t savedAddressByteSize = 0;
58939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Line) {
59939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_line contents:\n";
60939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
61939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
62939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      savedAddressByteSize = cu->getAddressByteSize();
63939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      unsigned stmtOffset =
64939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky        cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
65939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky                                                             -1U);
66939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      if (stmtOffset != -1U) {
67939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky        DataExtractor lineData(getLineSection(), isLittleEndian(),
68939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky                               savedAddressByteSize);
69939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky        DWARFDebugLine::DumpingState state(OS);
70ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor        DWARFDebugLine::parseStatementTable(lineData, &lineRelocMap(), &stmtOffset, state);
71939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      }
72fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramer    }
73fe80f1da404d25f93e4a2492b127554a882bd5bbBenjamin Kramer  }
7434f864fd382156331c61fbb6b7ae4828108b9d69Benjamin Kramer
75939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Str) {
76939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_str contents:\n";
77939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DataExtractor strData(getStringSection(), isLittleEndian(), 0);
78939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    offset = 0;
79939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    uint32_t strOffset = 0;
80939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    while (const char *s = strData.getCStr(&offset)) {
81939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
82939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      strOffset = offset;
83939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    }
8434f864fd382156331c61fbb6b7ae4828108b9d69Benjamin Kramer  }
85eceb5b99777ba944a0ae3748a0371e9a3aa94d56Alexey Samsonov
86939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
87939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    OS << "\n.debug_ranges contents:\n";
88939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // In fact, different compile units may have different address byte
89939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // sizes, but for simplicity we just use the address byte size of the last
90939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // compile unit (there is no easy and fast way to associate address range
91939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    // list and the compile unit it describes).
92939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DataExtractor rangesData(getRangeSection(), isLittleEndian(),
93939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky                             savedAddressByteSize);
94939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    offset = 0;
95939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    DWARFDebugRangeList rangeList;
96939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    while (rangeList.extract(rangesData, &offset))
97939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky      rangeList.dump(OS);
9882de10a34c9432029040ced17129079a7d80904eEric Christopher  }
9972f7bfbf0e02bb11d3e7cca1f9598c5f9d9fa2caEric Christopher
100e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek  if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
101e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    OS << "\n.debug_pubnames contents:\n";
102e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
103e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    offset = 0;
104e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    OS << "Length:                " << pubNames.getU32(&offset) << "\n";
105e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    OS << "Version:               " << pubNames.getU16(&offset) << "\n";
106e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
107e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    OS << "Size:                  " << pubNames.getU32(&offset) << "\n";
108e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    OS << "\n  Offset    Name\n";
109e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    while (offset < getPubNamesSection().size()) {
110e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek      uint32_t n = pubNames.getU32(&offset);
111e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek      if (n == 0)
112e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek        break;
113e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek      OS << format("%8x    ", n);
114e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek      OS << pubNames.getCStr(&offset) << "\n";
115e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek    }
116e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek  }
117e38825f490b898644089d5cd9cb90cec681bded8Krzysztof Parzyszek
118939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
11993f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
12093f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    if (D) {
12193f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      OS << "\n.debug_abbrev.dwo contents:\n";
12293f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      getDebugAbbrevDWO()->dump(OS);
12393f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    }
124939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky  }
125939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky
12693f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher  if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
12793f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    if (getNumDWOCompileUnits()) {
12893f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      OS << "\n.debug_info.dwo contents:\n";
12993f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
1305a0c366b1f0112de7d55346ab75e4aa7073a6a19Eric Christopher        getDWOCompileUnitAtIndex(i)->dump(OS);
13193f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    }
132939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky
13393f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher  if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
13493f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    if (!getStringDWOSection().empty()) {
13593f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      OS << "\n.debug_str.dwo contents:\n";
13693f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
13793f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      offset = 0;
13893f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      uint32_t strDWOOffset = 0;
13993f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      while (const char *s = strDWOData.getCStr(&offset)) {
1405a0c366b1f0112de7d55346ab75e4aa7073a6a19Eric Christopher        OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
1415a0c366b1f0112de7d55346ab75e4aa7073a6a19Eric Christopher        strDWOOffset = offset;
14293f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      }
143939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    }
144939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky
14593f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher  if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
14693f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher    if (!getStringOffsetDWOSection().empty()) {
14793f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      OS << "\n.debug_str_offsets.dwo contents:\n";
14893f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
14993f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      offset = 0;
150e305e03d6f71f70a7102f15ac5ff6fd93cf9e818Eric Christopher      uint64_t size = getStringOffsetDWOSection().size();
151e305e03d6f71f70a7102f15ac5ff6fd93cf9e818Eric Christopher      while (offset < size) {
1525a0c366b1f0112de7d55346ab75e4aa7073a6a19Eric Christopher        OS << format("0x%8.8x: ", offset);
1535a0c366b1f0112de7d55346ab75e4aa7073a6a19Eric Christopher        OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
15493f3fed999e039988deca64d19cbf122d46cdd59Eric Christopher      }
155939a4e8b693820d161f362317f7dba9057e66cc7Eli Bendersky    }
15672c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer}
15772c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
15872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramerconst DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
15972c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  if (Abbrev)
16072c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer    return Abbrev.get();
16172c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
16272c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
16372c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
16472c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  Abbrev.reset(new DWARFDebugAbbrev());
16572c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  Abbrev->parse(abbrData);
16672c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  return Abbrev.get();
16772c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer}
16872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
16982de10a34c9432029040ced17129079a7d80904eEric Christopherconst DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
17082de10a34c9432029040ced17129079a7d80904eEric Christopher  if (AbbrevDWO)
17182de10a34c9432029040ced17129079a7d80904eEric Christopher    return AbbrevDWO.get();
17282de10a34c9432029040ced17129079a7d80904eEric Christopher
17382de10a34c9432029040ced17129079a7d80904eEric Christopher  DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
17482de10a34c9432029040ced17129079a7d80904eEric Christopher  AbbrevDWO.reset(new DWARFDebugAbbrev());
17582de10a34c9432029040ced17129079a7d80904eEric Christopher  AbbrevDWO->parse(abbrData);
17682de10a34c9432029040ced17129079a7d80904eEric Christopher  return AbbrevDWO.get();
17782de10a34c9432029040ced17129079a7d80904eEric Christopher}
17882de10a34c9432029040ced17129079a7d80904eEric Christopher
1793df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikieconst DWARFDebugLoc *DWARFContext::getDebugLoc() {
1803df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  if (Loc)
1813df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie    return Loc.get();
1823df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie
1833df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  DataExtractor LocData(getLocSection(), isLittleEndian(), 0);
1843df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  Loc.reset(new DWARFDebugLoc(locRelocMap()));
1853df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  // assume all compile units have the same address byte size
1863df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  if (getNumCompileUnits())
1873df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie    Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
1883df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie  return Loc.get();
1893df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie}
1903df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie
191358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramerconst DWARFDebugAranges *DWARFContext::getDebugAranges() {
192358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  if (Aranges)
193358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer    return Aranges.get();
194358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
195358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
196358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
197358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  Aranges.reset(new DWARFDebugAranges());
198358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  Aranges->extract(arangesData);
19963a450a313a9b0a08622e97b53f5dd83f9266143Alexey Samsonov  // Generate aranges from DIEs: even if .debug_aranges section is present,
20063a450a313a9b0a08622e97b53f5dd83f9266143Alexey Samsonov  // it may describe only a small subset of compilation units, so we need to
20163a450a313a9b0a08622e97b53f5dd83f9266143Alexey Samsonov  // manually build aranges for the rest of them.
20263a450a313a9b0a08622e97b53f5dd83f9266143Alexey Samsonov  Aranges->generate(this);
203358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer  return Aranges.get();
204358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer}
205358f4fd9ee078b3c79597fc688855fb48bc1f356Benjamin Kramer
20660bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Benderskyconst DWARFDebugFrame *DWARFContext::getDebugFrame() {
20760bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  if (DebugFrame)
20860bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky    return DebugFrame.get();
20960bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky
21060bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // There's a "bug" in the DWARFv3 standard with respect to the target address
21160bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // size within debug frame sections. While DWARF is supposed to be independent
21260bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // of its container, FDEs have fields with size being "target address size",
21360bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // which isn't specified in DWARF in general. It's only specified for CUs, but
21460bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // .eh_frame can appear without a .debug_info section. Follow the example of
21560bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // other tools (libdwarf) and extract this from the container (ObjectFile
21660bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // provides this information). This problem is fixed in DWARFv4
21760bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // See this dwarf-discuss discussion for more details:
21860bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
21960bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
22060bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky                               getAddressSize());
22160bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  DebugFrame.reset(new DWARFDebugFrame());
22260bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  DebugFrame->parse(debugFrameData);
22360bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  return DebugFrame.get();
22460bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky}
22560bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky
226e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopherconst DWARFLineTable *
227c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin KramerDWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
228c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  if (!Line)
229ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor    Line.reset(new DWARFDebugLine(&lineRelocMap()));
230c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer
231c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  unsigned stmtOffset =
232c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer    cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
233c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer                                                         -1U);
234c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  if (stmtOffset == -1U)
235c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer    return 0; // No line table for this compile unit.
236c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer
237c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  // See if the line table is cached.
238e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher  if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
239c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer    return lt;
240c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer
241c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  // We have to parse it first.
242c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  DataExtractor lineData(getLineSection(), isLittleEndian(),
243c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer                         cu->getAddressByteSize());
244c26ed9b47ff77ca6244feda9e3837b49624605dbBenjamin Kramer  return Line->getOrParseLineTable(lineData, stmtOffset);
245b848e976110a2c4f0a6a9e252115ba291c844fbeBenjamin Kramer}
246b848e976110a2c4f0a6a9e252115ba291c844fbeBenjamin Kramer
24772c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramervoid DWARFContext::parseCompileUnits() {
24872c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  uint32_t offset = 0;
249b69b55f54a66d4df9549927a3cce307855d6de49Eric Christopher  const DataExtractor &DIData = DataExtractor(getInfoSection(),
250b69b55f54a66d4df9549927a3cce307855d6de49Eric Christopher                                              isLittleEndian(), 0);
251b69b55f54a66d4df9549927a3cce307855d6de49Eric Christopher  while (DIData.isValidOffset(offset)) {
25282de10a34c9432029040ced17129079a7d80904eEric Christopher    CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
25382de10a34c9432029040ced17129079a7d80904eEric Christopher                                   getAbbrevSection(), getRangeSection(),
25472f7bfbf0e02bb11d3e7cca1f9598c5f9d9fa2caEric Christopher                                   getStringSection(), StringRef(),
25572f7bfbf0e02bb11d3e7cca1f9598c5f9d9fa2caEric Christopher                                   getAddrSection(),
256dd8e9f395e881972b320d947de88102a0be04b70Eric Christopher                                   &infoRelocMap(),
25782de10a34c9432029040ced17129079a7d80904eEric Christopher                                   isLittleEndian()));
258b69b55f54a66d4df9549927a3cce307855d6de49Eric Christopher    if (!CUs.back().extract(DIData, &offset)) {
25972c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer      CUs.pop_back();
26072c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer      break;
26172c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer    }
26272c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer
26372c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer    offset = CUs.back().getNextCompileUnitOffset();
26472c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer  }
26572c0d7fdd3d0930c7507060e96aec7d7429a8190Benjamin Kramer}
266101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer
26782de10a34c9432029040ced17129079a7d80904eEric Christophervoid DWARFContext::parseDWOCompileUnits() {
26882de10a34c9432029040ced17129079a7d80904eEric Christopher  uint32_t offset = 0;
26982de10a34c9432029040ced17129079a7d80904eEric Christopher  const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
27082de10a34c9432029040ced17129079a7d80904eEric Christopher                                              isLittleEndian(), 0);
27182de10a34c9432029040ced17129079a7d80904eEric Christopher  while (DIData.isValidOffset(offset)) {
27282de10a34c9432029040ced17129079a7d80904eEric Christopher    DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
27382de10a34c9432029040ced17129079a7d80904eEric Christopher                                      getAbbrevDWOSection(),
27482de10a34c9432029040ced17129079a7d80904eEric Christopher                                      getRangeDWOSection(),
27582de10a34c9432029040ced17129079a7d80904eEric Christopher                                      getStringDWOSection(),
276dd8e9f395e881972b320d947de88102a0be04b70Eric Christopher                                      getStringOffsetDWOSection(),
27772f7bfbf0e02bb11d3e7cca1f9598c5f9d9fa2caEric Christopher                                      getAddrSection(),
27882de10a34c9432029040ced17129079a7d80904eEric Christopher                                      &infoDWORelocMap(),
27982de10a34c9432029040ced17129079a7d80904eEric Christopher                                      isLittleEndian()));
28082de10a34c9432029040ced17129079a7d80904eEric Christopher    if (!DWOCUs.back().extract(DIData, &offset)) {
28182de10a34c9432029040ced17129079a7d80904eEric Christopher      DWOCUs.pop_back();
28282de10a34c9432029040ced17129079a7d80904eEric Christopher      break;
28382de10a34c9432029040ced17129079a7d80904eEric Christopher    }
28482de10a34c9432029040ced17129079a7d80904eEric Christopher
28582de10a34c9432029040ced17129079a7d80904eEric Christopher    offset = DWOCUs.back().getNextCompileUnitOffset();
28682de10a34c9432029040ced17129079a7d80904eEric Christopher  }
28782de10a34c9432029040ced17129079a7d80904eEric Christopher}
28882de10a34c9432029040ced17129079a7d80904eEric Christopher
289101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramernamespace {
290101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer  struct OffsetComparator {
291101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer    bool operator()(const DWARFCompileUnit &LHS,
292101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer                    const DWARFCompileUnit &RHS) const {
293101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer      return LHS.getOffset() < RHS.getOffset();
294101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer    }
295101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer    bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
296101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer      return LHS.getOffset() < RHS;
297101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer    }
298101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer    bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
299101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer      return LHS < RHS.getOffset();
300101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer    }
301101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer  };
302101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer}
303101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer
30438a6381c0a58e013577b1957199128af9573fc20Alexey SamsonovDWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
305101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer  if (CUs.empty())
306101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer    parseCompileUnits();
307101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer
30838a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
30938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov                                          OffsetComparator());
31038a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (CU != CUs.end())
31138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    return &*CU;
312101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer  return 0;
313101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer}
314101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer
31538a6381c0a58e013577b1957199128af9573fc20Alexey SamsonovDWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
3169013db3399d2847899ba32daf58844bba1aef6ddBenjamin Kramer  // First, get the offset of the compile unit.
31738a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  uint32_t CUOffset = getDebugAranges()->findAddress(Address);
318101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer  // Retrieve the compile unit.
31938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  return getCompileUnitForOffset(CUOffset);
32038a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov}
32138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov
322e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopherstatic bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
323e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                      const DWARFLineTable *LineTable,
324e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                      uint64_t FileIndex,
325e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                      bool NeedsAbsoluteFilePath,
326e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                      std::string &FileName) {
32738a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (CU == 0 ||
32838a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov      LineTable == 0 ||
32938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov      !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
33038a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov                                     FileName))
33138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    return false;
33238a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
33338a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    // We may still need to append compilation directory of compile unit.
33438a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    SmallString<16> AbsolutePath;
33538a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    if (const char *CompilationDir = CU->getCompilationDir()) {
33638a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov      sys::path::append(AbsolutePath, CompilationDir);
3379d26b0ba06479d9debadebce19344169f72407ddAlexey Samsonov    }
33838a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    sys::path::append(AbsolutePath, FileName);
33938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    FileName = AbsolutePath.str();
3403e25c4a1e3e58bc1d00d894854a29dd2e4e7e88aAlexey Samsonov  }
34138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  return true;
34238a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov}
34338a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov
344e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopherstatic bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
345e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                          const DWARFLineTable *LineTable,
346e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                          uint64_t Address,
347e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                          bool NeedsAbsoluteFilePath,
348e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                          std::string &FileName,
349e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher                                          uint32_t &Line, uint32_t &Column) {
3505eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  if (CU == 0 || LineTable == 0)
35138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    return false;
35238a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  // Get the index of row we're looking for in the line table.
35338a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  uint32_t RowIndex = LineTable->lookupAddress(Address);
35438a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (RowIndex == -1U)
35538a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    return false;
35638a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  // Take file number and line/column from the row.
35738a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
35838a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
35938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov                                 NeedsAbsoluteFilePath, FileName))
36038a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    return false;
36138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  Line = Row.Line;
36238a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  Column = Row.Column;
36338a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  return true;
36438a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov}
36538a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov
36638a6381c0a58e013577b1957199128af9573fc20Alexey SamsonovDILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
36738a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    DILineInfoSpecifier Specifier) {
36838a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
36938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (!CU)
37038a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    return DILineInfo();
37138a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  std::string FileName = "<invalid>";
37238a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  std::string FunctionName = "<invalid>";
37338a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  uint32_t Line = 0;
37438a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  uint32_t Column = 0;
37538a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
3765eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    // The address may correspond to instruction in some inlined function,
3775eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    // so we have to build the chain of inlined functions and take the
3785eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    // name of the topmost function in it.
379e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    const DWARFDebugInfoEntryInlinedChain &InlinedChain =
3805eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        CU->getInlinedChainForAddress(Address);
381e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    if (InlinedChain.DIEs.size() > 0) {
382e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov      const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
383e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov      if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.CU))
38438a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov        FunctionName = Name;
3853e25c4a1e3e58bc1d00d894854a29dd2e4e7e88aAlexey Samsonov    }
3863e25c4a1e3e58bc1d00d894854a29dd2e4e7e88aAlexey Samsonov  }
38738a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
388e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher    const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
38938a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov    const bool NeedsAbsoluteFilePath =
39038a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov        Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
3915eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    getFileLineInfoForCompileUnit(CU, LineTable, Address,
3925eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov                                  NeedsAbsoluteFilePath,
39338a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov                                  FileName, Line, Column);
39438a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  }
39538a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov  return DILineInfo(StringRef(FileName), StringRef(FunctionName),
39638a6381c0a58e013577b1957199128af9573fc20Alexey Samsonov                    Line, Column);
397101b1c5ff16dffd45d03746d92c024740f72ecc6Benjamin Kramer}
3982d24e2a396a1d211baaeedf32148a3b657240170David Blaikie
399e27a787760ea7c2899da3287002fe8ba316df0d0Andrew KaylorDILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
400e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    uint64_t Size,
401e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    DILineInfoSpecifier Specifier) {
402e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  DILineInfoTable  Lines;
403e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
404e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  if (!CU)
405e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    return Lines;
406e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
407e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  std::string FunctionName = "<invalid>";
408e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
409e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    // The address may correspond to instruction in some inlined function,
410e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    // so we have to build the chain of inlined functions and take the
411e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    // name of the topmost function in it.
412e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    const DWARFDebugInfoEntryInlinedChain &InlinedChain =
413e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor        CU->getInlinedChainForAddress(Address);
414e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    if (InlinedChain.DIEs.size() > 0) {
415e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov      const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
416e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov      if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.CU))
417e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor        FunctionName = Name;
418e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    }
419e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  }
420e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
421e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  StringRef  FuncNameRef = StringRef(FunctionName);
422e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
423e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  // If the Specifier says we don't need FileLineInfo, just
424e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  // return the top-most function at the starting address.
425e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
426e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    Lines.push_back(std::make_pair(Address,
427e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov                                   DILineInfo(StringRef("<invalid>"),
428e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor                                              FuncNameRef, 0, 0)));
429e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    return Lines;
430e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  }
431e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
432e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
433e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  const bool NeedsAbsoluteFilePath =
434e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor      Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
435e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
436e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  // Get the index of row we're looking for in the line table.
437e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  std::vector<uint32_t> RowVector;
438e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  if (!LineTable->lookupAddressRange(Address, Size, RowVector))
439e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    return Lines;
440e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
441e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  uint32_t NumRows = RowVector.size();
442e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  for (uint32_t i = 0; i < NumRows; ++i) {
443e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    uint32_t RowIndex = RowVector[i];
444e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    // Take file number and line/column from the row.
445e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
446e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    std::string FileName = "<invalid>";
447e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor    getFileNameForCompileUnit(CU, LineTable, Row.File,
448e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor                              NeedsAbsoluteFilePath, FileName);
449e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    Lines.push_back(std::make_pair(Row.Address,
450e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor                                   DILineInfo(StringRef(FileName),
451e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor                                         FuncNameRef, Row.Line, Row.Column)));
452e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  }
453e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
454e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor  return Lines;
455e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor}
456e27a787760ea7c2899da3287002fe8ba316df0d0Andrew Kaylor
4575eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey SamsonovDIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
4585eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    DILineInfoSpecifier Specifier) {
4595eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
4605eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  if (!CU)
4615eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    return DIInliningInfo();
4625eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov
463e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov  const DWARFDebugInfoEntryInlinedChain &InlinedChain =
4645eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      CU->getInlinedChainForAddress(Address);
465e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov  if (InlinedChain.DIEs.size() == 0)
4665eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    return DIInliningInfo();
4675eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov
4685eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  DIInliningInfo InliningInfo;
4695eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
470e9403c1fd6647eda9f6c9d46a75ab9370b2354b6Eric Christopher  const DWARFLineTable *LineTable = 0;
471e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov  for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
472e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov    const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
4735eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    std::string FileName = "<invalid>";
4745eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    std::string FunctionName = "<invalid>";
4755eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    uint32_t Line = 0;
4765eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    uint32_t Column = 0;
4775eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    // Get function name if necessary.
4785eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
479e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov      if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.CU))
4805eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        FunctionName = Name;
4815eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    }
4825eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
4835eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      const bool NeedsAbsoluteFilePath =
4845eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov          Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
4855eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      if (i == 0) {
4865eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // For the topmost frame, initialize the line table of this
4875eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // compile unit and fetch file/line info from it.
4885eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        LineTable = getLineTableForCompileUnit(CU);
4895eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // For the topmost routine, get file/line info from line table.
4905eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        getFileLineInfoForCompileUnit(CU, LineTable, Address,
4915eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov                                      NeedsAbsoluteFilePath,
4925eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov                                      FileName, Line, Column);
4935eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      } else {
4945eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // Otherwise, use call file, call line and call column from
4955eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        // previous DIE in inlined chain.
4965eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        getFileNameForCompileUnit(CU, LineTable, CallFile,
4975eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov                                  NeedsAbsoluteFilePath, FileName);
4985eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        Line = CallLine;
4995eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov        Column = CallColumn;
5005eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      }
5015eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      // Get call file/line/column of a current DIE.
5025eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      if (i + 1 < n) {
503e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov        FunctionDIE.getCallerFrame(InlinedChain.CU, CallFile, CallLine,
504e664290ad6d988e0ae40f2461084f6adbababa47Alexey Samsonov                                   CallColumn);
5055eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov      }
5065eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    }
5075eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
5085eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov                     Line, Column);
5095eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov    InliningInfo.addFrame(Frame);
5105eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  }
5115eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov  return InliningInfo;
5125eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov}
5135eae90d727c64ca5b4b43b110521b38dcd9f0de6Alexey Samsonov
514005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonovstatic bool consumeCompressedDebugSectionHeader(StringRef &data,
515005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov                                                uint64_t &OriginalSize) {
516005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  // Consume "ZLIB" prefix.
517005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  if (!data.startswith("ZLIB"))
518005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    return false;
519005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  data = data.substr(4);
520005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  // Consume uncompressed section size (big-endian 8 bytes).
521005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  DataExtractor extractor(data, false, 8);
522005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  uint32_t Offset = 0;
523005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  OriginalSize = extractor.getU64(&Offset);
524005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  if (Offset == 0)
525005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    return false;
526005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  data = data.substr(Offset);
527005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  return true;
528005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov}
529005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov
530d1726a4580f3dc42e2debbfea41acb9e815c06beEric ChristopherDWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
53160bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  IsLittleEndian(Obj->isLittleEndian()),
53260bdc5b16e2fc17be184b515a00c2e2a2eb40b89Eli Bendersky  AddressSize(Obj->getBytesInAddress()) {
533d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher  error_code ec;
534d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher  for (object::section_iterator i = Obj->begin_sections(),
535d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher         e = Obj->end_sections();
536d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher       i != e; i.increment(ec)) {
537d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    StringRef name;
538d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    i->getName(name);
539d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    StringRef data;
540d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    i->getContents(data);
541d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
542d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
543784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov
544005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    // Check if debug info section is compressed with zlib.
545005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    if (name.startswith("zdebug_")) {
546005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      uint64_t OriginalSize;
547005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      if (!zlib::isAvailable() ||
548005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov          !consumeCompressedDebugSectionHeader(data, OriginalSize))
549005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov        continue;
550005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      OwningPtr<MemoryBuffer> UncompressedSection;
551005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
552005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov          zlib::StatusOK)
553005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov        continue;
554005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      // Make data point to uncompressed section contents and save its contents.
555005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      name = name.substr(1);
556005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      data = UncompressedSection->getBuffer();
557005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov      UncompressedSections.push_back(UncompressedSection.take());
558005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov    }
559005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov
560784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov    StringRef *Section = StringSwitch<StringRef*>(name)
561784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_info", &InfoSection)
562784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_abbrev", &AbbrevSection)
5633df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie        .Case("debug_loc", &LocSection)
564784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_line", &LineSection)
565784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_aranges", &ARangeSection)
566784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_frame", &DebugFrameSection)
567784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_str", &StringSection)
568784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_ranges", &RangeSection)
569784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_pubnames", &PubNamesSection)
570784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_info.dwo", &InfoDWOSection)
571784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_abbrev.dwo", &AbbrevDWOSection)
572784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_str.dwo", &StringDWOSection)
573784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
574784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_addr", &AddrSection)
575784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        // Any more debug info sections go here.
576784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Default(0);
5777486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    if (Section) {
5787486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      *Section = data;
5797486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      if (name == "debug_ranges") {
5807486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola        // FIXME: Use the other dwo range section when we emit it.
5817486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola        RangeDWOSection = data;
5827486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      }
58382de10a34c9432029040ced17129079a7d80904eEric Christopher    }
584d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
5857486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    section_iterator RelocatedSection = i->getRelocatedSection();
5867486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    if (RelocatedSection == Obj->end_sections())
5877486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      continue;
5887486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola
5897486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    StringRef RelSecName;
5907486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    RelocatedSection->getName(RelSecName);
5917486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    RelSecName = RelSecName.substr(
5927486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola        RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
5937486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola
594ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor    // TODO: Add support for relocations in other sections as needed.
595ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor    // Record relocations for the debug_info and debug_line sections.
5967486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola    RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
597784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_info", &InfoRelocMap)
5983df7d2f70bb316ebeec8a8c862b3da5386fbb145David Blaikie        .Case("debug_loc", &LocRelocMap)
599784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_info.dwo", &InfoDWORelocMap)
600784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Case("debug_line", &LineRelocMap)
601784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov        .Default(0);
602784baa6f4440beac54881673ecc7baa88f8b94ddAlexey Samsonov    if (!Map)
603d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher      continue;
604d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
605d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    if (i->begin_relocations() != i->end_relocations()) {
606d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher      uint64_t SectionSize;
6077486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola      RelocatedSection->getSize(SectionSize);
608d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher      for (object::relocation_iterator reloc_i = i->begin_relocations(),
609d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher             reloc_e = i->end_relocations();
610d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher           reloc_i != reloc_e; reloc_i.increment(ec)) {
611d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        uint64_t Address;
612956ca7265c697107708468b7e1b2fd21f4185baeRafael Espindola        reloc_i->getOffset(Address);
613d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        uint64_t Type;
614d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        reloc_i->getType(Type);
615ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor        uint64_t SymAddr = 0;
616ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor        // ELF relocations may need the symbol address
617ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor        if (Obj->isELF()) {
6186c1202c459ffa6d693ad92fa84e43902bc780bcaRafael Espindola          object::symbol_iterator Sym = reloc_i->getSymbol();
6196c1202c459ffa6d693ad92fa84e43902bc780bcaRafael Espindola          Sym->getAddress(SymAddr);
620ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor        }
621d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
622d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        object::RelocVisitor V(Obj->getFileFormatName());
623d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        // The section address is always 0 for debug sections.
624ee7c0d2f931590ccdc53a14b1839e11bb29fc96eAndrew Kaylor        object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
625d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        if (V.error()) {
626d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          SmallString<32> Name;
627d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          error_code ec(reloc_i->getTypeName(Name));
628d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          if (ec) {
629d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher            errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
630d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          }
631d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          errs() << "error: failed to compute relocation: "
632d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                 << Name << "\n";
633d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          continue;
634d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        }
635d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
636d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        if (Address + R.Width > SectionSize) {
637d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          errs() << "error: " << R.Width << "-byte relocation starting "
638d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                 << Address << " bytes into section " << name << " which is "
639d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                 << SectionSize << " bytes long.\n";
640d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          continue;
641d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        }
642d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        if (R.Width > 8) {
643d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          errs() << "error: can't handle a relocation of more than 8 bytes at "
644d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                    "a time.\n";
645d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher          continue;
646d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        }
647d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher        DEBUG(dbgs() << "Writing " << format("%p", R.Value)
648d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                     << " at " << format("%p", Address)
649d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                     << " with width " << format("%d", R.Width)
650d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher                     << "\n");
65182de10a34c9432029040ced17129079a7d80904eEric Christopher        Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
652d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher      }
653d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher    }
654d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher  }
655d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher}
656d1726a4580f3dc42e2debbfea41acb9e815c06beEric Christopher
657005159e92420a102516ee6e29ef2178c818da5d0Alexey SamsonovDWARFContextInMemory::~DWARFContextInMemory() {
658005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov  DeleteContainerPointers(UncompressedSections);
659005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov}
660005159e92420a102516ee6e29ef2178c818da5d0Alexey Samsonov
6612d24e2a396a1d211baaeedf32148a3b657240170David Blaikievoid DWARFContextInMemory::anchor() { }
662