DWARFContext.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. //===-- DWARFContext.cpp --------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringSwitch.h"
  12. #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
  13. #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
  14. #include "llvm/Support/Compression.h"
  15. #include "llvm/Support/Dwarf.h"
  16. #include "llvm/Support/Format.h"
  17. #include "llvm/Support/Path.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <algorithm>
  20. using namespace llvm;
  21. using namespace dwarf;
  22. using namespace object;
  23. #define DEBUG_TYPE "dwarf"
  24. typedef DWARFDebugLine::LineTable DWARFLineTable;
  25. typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
  26. typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
  27. static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
  28. bool LittleEndian, bool GnuStyle) {
  29. OS << "\n." << Name << " contents:\n";
  30. DataExtractor pubNames(Data, LittleEndian, 0);
  31. uint32_t offset = 0;
  32. while (pubNames.isValidOffset(offset)) {
  33. OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
  34. OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
  35. OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
  36. OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
  37. if (GnuStyle)
  38. OS << "Offset Linkage Kind Name\n";
  39. else
  40. OS << "Offset Name\n";
  41. while (offset < Data.size()) {
  42. uint32_t dieRef = pubNames.getU32(&offset);
  43. if (dieRef == 0)
  44. break;
  45. OS << format("0x%8.8x ", dieRef);
  46. if (GnuStyle) {
  47. PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
  48. OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
  49. << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
  50. << ' ';
  51. }
  52. OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
  53. }
  54. }
  55. }
  56. static void dumpAccelSection(raw_ostream &OS, StringRef Name,
  57. const DWARFSection& Section, StringRef StringSection,
  58. bool LittleEndian) {
  59. DataExtractor AccelSection(Section.Data, LittleEndian, 0);
  60. DataExtractor StrData(StringSection, LittleEndian, 0);
  61. OS << "\n." << Name << " contents:\n";
  62. DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
  63. if (!Accel.extract())
  64. return;
  65. Accel.dump(OS);
  66. }
  67. void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
  68. if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
  69. OS << ".debug_abbrev contents:\n";
  70. getDebugAbbrev()->dump(OS);
  71. }
  72. if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
  73. if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
  74. OS << "\n.debug_abbrev.dwo contents:\n";
  75. D->dump(OS);
  76. }
  77. if (DumpType == DIDT_All || DumpType == DIDT_Info) {
  78. OS << "\n.debug_info contents:\n";
  79. for (const auto &CU : compile_units())
  80. CU->dump(OS);
  81. }
  82. if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
  83. getNumDWOCompileUnits()) {
  84. OS << "\n.debug_info.dwo contents:\n";
  85. for (const auto &DWOCU : dwo_compile_units())
  86. DWOCU->dump(OS);
  87. }
  88. if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
  89. OS << "\n.debug_types contents:\n";
  90. for (const auto &TUS : type_unit_sections())
  91. for (const auto &TU : TUS)
  92. TU->dump(OS);
  93. }
  94. if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
  95. getNumDWOTypeUnits()) {
  96. OS << "\n.debug_types.dwo contents:\n";
  97. for (const auto &DWOTUS : dwo_type_unit_sections())
  98. for (const auto &DWOTU : DWOTUS)
  99. DWOTU->dump(OS);
  100. }
  101. if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
  102. OS << "\n.debug_loc contents:\n";
  103. getDebugLoc()->dump(OS);
  104. }
  105. if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
  106. OS << "\n.debug_loc.dwo contents:\n";
  107. getDebugLocDWO()->dump(OS);
  108. }
  109. if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
  110. OS << "\n.debug_frame contents:\n";
  111. getDebugFrame()->dump(OS);
  112. }
  113. uint32_t offset = 0;
  114. if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
  115. OS << "\n.debug_aranges contents:\n";
  116. DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
  117. DWARFDebugArangeSet set;
  118. while (set.extract(arangesData, &offset))
  119. set.dump(OS);
  120. }
  121. uint8_t savedAddressByteSize = 0;
  122. if (DumpType == DIDT_All || DumpType == DIDT_Line) {
  123. OS << "\n.debug_line contents:\n";
  124. for (const auto &CU : compile_units()) {
  125. savedAddressByteSize = CU->getAddressByteSize();
  126. const auto *CUDIE = CU->getUnitDIE();
  127. if (CUDIE == nullptr)
  128. continue;
  129. unsigned stmtOffset = CUDIE->getAttributeValueAsSectionOffset(
  130. CU.get(), DW_AT_stmt_list, -1U);
  131. if (stmtOffset != -1U) {
  132. DataExtractor lineData(getLineSection().Data, isLittleEndian(),
  133. savedAddressByteSize);
  134. DWARFDebugLine::LineTable LineTable;
  135. LineTable.parse(lineData, &getLineSection().Relocs, &stmtOffset);
  136. LineTable.dump(OS);
  137. }
  138. }
  139. }
  140. if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
  141. OS << "\n.debug_line.dwo contents:\n";
  142. unsigned stmtOffset = 0;
  143. DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
  144. savedAddressByteSize);
  145. DWARFDebugLine::LineTable LineTable;
  146. while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
  147. LineTable.dump(OS);
  148. LineTable.clear();
  149. }
  150. }
  151. if (DumpType == DIDT_All || DumpType == DIDT_Str) {
  152. OS << "\n.debug_str contents:\n";
  153. DataExtractor strData(getStringSection(), isLittleEndian(), 0);
  154. offset = 0;
  155. uint32_t strOffset = 0;
  156. while (const char *s = strData.getCStr(&offset)) {
  157. OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
  158. strOffset = offset;
  159. }
  160. }
  161. if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
  162. !getStringDWOSection().empty()) {
  163. OS << "\n.debug_str.dwo contents:\n";
  164. DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
  165. offset = 0;
  166. uint32_t strDWOOffset = 0;
  167. while (const char *s = strDWOData.getCStr(&offset)) {
  168. OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
  169. strDWOOffset = offset;
  170. }
  171. }
  172. if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
  173. OS << "\n.debug_ranges contents:\n";
  174. // In fact, different compile units may have different address byte
  175. // sizes, but for simplicity we just use the address byte size of the last
  176. // compile unit (there is no easy and fast way to associate address range
  177. // list and the compile unit it describes).
  178. DataExtractor rangesData(getRangeSection(), isLittleEndian(),
  179. savedAddressByteSize);
  180. offset = 0;
  181. DWARFDebugRangeList rangeList;
  182. while (rangeList.extract(rangesData, &offset))
  183. rangeList.dump(OS);
  184. }
  185. if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
  186. dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
  187. isLittleEndian(), false);
  188. if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
  189. dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
  190. isLittleEndian(), false);
  191. if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
  192. dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
  193. isLittleEndian(), true /* GnuStyle */);
  194. if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
  195. dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
  196. isLittleEndian(), true /* GnuStyle */);
  197. if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
  198. !getStringOffsetDWOSection().empty()) {
  199. OS << "\n.debug_str_offsets.dwo contents:\n";
  200. DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
  201. 0);
  202. offset = 0;
  203. uint64_t size = getStringOffsetDWOSection().size();
  204. while (offset < size) {
  205. OS << format("0x%8.8x: ", offset);
  206. OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
  207. }
  208. }
  209. if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
  210. dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
  211. getStringSection(), isLittleEndian());
  212. if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
  213. dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
  214. getStringSection(), isLittleEndian());
  215. if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
  216. dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
  217. getStringSection(), isLittleEndian());
  218. if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
  219. dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
  220. getStringSection(), isLittleEndian());
  221. }
  222. const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
  223. if (Abbrev)
  224. return Abbrev.get();
  225. DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
  226. Abbrev.reset(new DWARFDebugAbbrev());
  227. Abbrev->extract(abbrData);
  228. return Abbrev.get();
  229. }
  230. const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
  231. if (AbbrevDWO)
  232. return AbbrevDWO.get();
  233. DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
  234. AbbrevDWO.reset(new DWARFDebugAbbrev());
  235. AbbrevDWO->extract(abbrData);
  236. return AbbrevDWO.get();
  237. }
  238. const DWARFDebugLoc *DWARFContext::getDebugLoc() {
  239. if (Loc)
  240. return Loc.get();
  241. DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
  242. Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
  243. // assume all compile units have the same address byte size
  244. if (getNumCompileUnits())
  245. Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
  246. return Loc.get();
  247. }
  248. const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
  249. if (LocDWO)
  250. return LocDWO.get();
  251. DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
  252. LocDWO.reset(new DWARFDebugLocDWO());
  253. LocDWO->parse(LocData);
  254. return LocDWO.get();
  255. }
  256. const DWARFDebugAranges *DWARFContext::getDebugAranges() {
  257. if (Aranges)
  258. return Aranges.get();
  259. Aranges.reset(new DWARFDebugAranges());
  260. Aranges->generate(this);
  261. return Aranges.get();
  262. }
  263. const DWARFDebugFrame *DWARFContext::getDebugFrame() {
  264. if (DebugFrame)
  265. return DebugFrame.get();
  266. // There's a "bug" in the DWARFv3 standard with respect to the target address
  267. // size within debug frame sections. While DWARF is supposed to be independent
  268. // of its container, FDEs have fields with size being "target address size",
  269. // which isn't specified in DWARF in general. It's only specified for CUs, but
  270. // .eh_frame can appear without a .debug_info section. Follow the example of
  271. // other tools (libdwarf) and extract this from the container (ObjectFile
  272. // provides this information). This problem is fixed in DWARFv4
  273. // See this dwarf-discuss discussion for more details:
  274. // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
  275. DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
  276. getAddressSize());
  277. DebugFrame.reset(new DWARFDebugFrame());
  278. DebugFrame->parse(debugFrameData);
  279. return DebugFrame.get();
  280. }
  281. const DWARFLineTable *
  282. DWARFContext::getLineTableForUnit(DWARFUnit *U) {
  283. if (!Line)
  284. Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
  285. const auto *UnitDIE = U->getUnitDIE();
  286. if (UnitDIE == nullptr)
  287. return nullptr;
  288. unsigned stmtOffset =
  289. UnitDIE->getAttributeValueAsSectionOffset(U, DW_AT_stmt_list, -1U);
  290. if (stmtOffset == -1U)
  291. return nullptr; // No line table for this compile unit.
  292. // See if the line table is cached.
  293. if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
  294. return lt;
  295. // We have to parse it first.
  296. DataExtractor lineData(getLineSection().Data, isLittleEndian(),
  297. U->getAddressByteSize());
  298. return Line->getOrParseLineTable(lineData, stmtOffset);
  299. }
  300. void DWARFContext::parseCompileUnits() {
  301. CUs.parse(*this, getInfoSection());
  302. }
  303. void DWARFContext::parseTypeUnits() {
  304. if (!TUs.empty())
  305. return;
  306. for (const auto &I : getTypesSections()) {
  307. TUs.emplace_back();
  308. TUs.back().parse(*this, I.second);
  309. }
  310. }
  311. void DWARFContext::parseDWOCompileUnits() {
  312. DWOCUs.parseDWO(*this, getInfoDWOSection());
  313. }
  314. void DWARFContext::parseDWOTypeUnits() {
  315. if (!DWOTUs.empty())
  316. return;
  317. for (const auto &I : getTypesDWOSections()) {
  318. DWOTUs.emplace_back();
  319. DWOTUs.back().parseDWO(*this, I.second);
  320. }
  321. }
  322. DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
  323. parseCompileUnits();
  324. return CUs.getUnitForOffset(Offset);
  325. }
  326. DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
  327. // First, get the offset of the compile unit.
  328. uint32_t CUOffset = getDebugAranges()->findAddress(Address);
  329. // Retrieve the compile unit.
  330. return getCompileUnitForOffset(CUOffset);
  331. }
  332. static bool getFunctionNameForAddress(DWARFCompileUnit *CU, uint64_t Address,
  333. FunctionNameKind Kind,
  334. std::string &FunctionName) {
  335. if (Kind == FunctionNameKind::None)
  336. return false;
  337. // The address may correspond to instruction in some inlined function,
  338. // so we have to build the chain of inlined functions and take the
  339. // name of the topmost function in it.
  340. const DWARFDebugInfoEntryInlinedChain &InlinedChain =
  341. CU->getInlinedChainForAddress(Address);
  342. if (InlinedChain.DIEs.size() == 0)
  343. return false;
  344. const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
  345. if (const char *Name =
  346. TopFunctionDIE.getSubroutineName(InlinedChain.U, Kind)) {
  347. FunctionName = Name;
  348. return true;
  349. }
  350. return false;
  351. }
  352. DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
  353. DILineInfoSpecifier Spec) {
  354. DILineInfo Result;
  355. DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
  356. if (!CU)
  357. return Result;
  358. getFunctionNameForAddress(CU, Address, Spec.FNKind, Result.FunctionName);
  359. if (Spec.FLIKind != FileLineInfoKind::None) {
  360. if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
  361. LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
  362. Spec.FLIKind, Result);
  363. }
  364. return Result;
  365. }
  366. DILineInfoTable
  367. DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
  368. DILineInfoSpecifier Spec) {
  369. DILineInfoTable Lines;
  370. DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
  371. if (!CU)
  372. return Lines;
  373. std::string FunctionName = "<invalid>";
  374. getFunctionNameForAddress(CU, Address, Spec.FNKind, FunctionName);
  375. // If the Specifier says we don't need FileLineInfo, just
  376. // return the top-most function at the starting address.
  377. if (Spec.FLIKind == FileLineInfoKind::None) {
  378. DILineInfo Result;
  379. Result.FunctionName = FunctionName;
  380. Lines.push_back(std::make_pair(Address, Result));
  381. return Lines;
  382. }
  383. const DWARFLineTable *LineTable = getLineTableForUnit(CU);
  384. // Get the index of row we're looking for in the line table.
  385. std::vector<uint32_t> RowVector;
  386. if (!LineTable->lookupAddressRange(Address, Size, RowVector))
  387. return Lines;
  388. for (uint32_t RowIndex : RowVector) {
  389. // Take file number and line/column from the row.
  390. const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
  391. DILineInfo Result;
  392. LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
  393. Spec.FLIKind, Result.FileName);
  394. Result.FunctionName = FunctionName;
  395. Result.Line = Row.Line;
  396. Result.Column = Row.Column;
  397. Lines.push_back(std::make_pair(Row.Address, Result));
  398. }
  399. return Lines;
  400. }
  401. DIInliningInfo
  402. DWARFContext::getInliningInfoForAddress(uint64_t Address,
  403. DILineInfoSpecifier Spec) {
  404. DIInliningInfo InliningInfo;
  405. DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
  406. if (!CU)
  407. return InliningInfo;
  408. const DWARFLineTable *LineTable = nullptr;
  409. const DWARFDebugInfoEntryInlinedChain &InlinedChain =
  410. CU->getInlinedChainForAddress(Address);
  411. if (InlinedChain.DIEs.size() == 0) {
  412. // If there is no DIE for address (e.g. it is in unavailable .dwo file),
  413. // try to at least get file/line info from symbol table.
  414. if (Spec.FLIKind != FileLineInfoKind::None) {
  415. DILineInfo Frame;
  416. LineTable = getLineTableForUnit(CU);
  417. if (LineTable &&
  418. LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
  419. Spec.FLIKind, Frame))
  420. InliningInfo.addFrame(Frame);
  421. }
  422. return InliningInfo;
  423. }
  424. uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
  425. for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
  426. const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
  427. DILineInfo Frame;
  428. // Get function name if necessary.
  429. if (const char *Name =
  430. FunctionDIE.getSubroutineName(InlinedChain.U, Spec.FNKind))
  431. Frame.FunctionName = Name;
  432. if (Spec.FLIKind != FileLineInfoKind::None) {
  433. if (i == 0) {
  434. // For the topmost frame, initialize the line table of this
  435. // compile unit and fetch file/line info from it.
  436. LineTable = getLineTableForUnit(CU);
  437. // For the topmost routine, get file/line info from line table.
  438. if (LineTable)
  439. LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
  440. Spec.FLIKind, Frame);
  441. } else {
  442. // Otherwise, use call file, call line and call column from
  443. // previous DIE in inlined chain.
  444. if (LineTable)
  445. LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
  446. Spec.FLIKind, Frame.FileName);
  447. Frame.Line = CallLine;
  448. Frame.Column = CallColumn;
  449. }
  450. // Get call file/line/column of a current DIE.
  451. if (i + 1 < n) {
  452. FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
  453. CallColumn);
  454. }
  455. }
  456. InliningInfo.addFrame(Frame);
  457. }
  458. return InliningInfo;
  459. }
  460. static bool consumeCompressedDebugSectionHeader(StringRef &data,
  461. uint64_t &OriginalSize) {
  462. // Consume "ZLIB" prefix.
  463. if (!data.startswith("ZLIB"))
  464. return false;
  465. data = data.substr(4);
  466. // Consume uncompressed section size (big-endian 8 bytes).
  467. DataExtractor extractor(data, false, 8);
  468. uint32_t Offset = 0;
  469. OriginalSize = extractor.getU64(&Offset);
  470. if (Offset == 0)
  471. return false;
  472. data = data.substr(Offset);
  473. return true;
  474. }
  475. DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
  476. const LoadedObjectInfo *L)
  477. : IsLittleEndian(Obj.isLittleEndian()),
  478. AddressSize(Obj.getBytesInAddress()) {
  479. for (const SectionRef &Section : Obj.sections()) {
  480. StringRef name;
  481. Section.getName(name);
  482. // Skip BSS and Virtual sections, they aren't interesting.
  483. bool IsBSS = Section.isBSS();
  484. if (IsBSS)
  485. continue;
  486. bool IsVirtual = Section.isVirtual();
  487. if (IsVirtual)
  488. continue;
  489. StringRef data;
  490. // Try to obtain an already relocated version of this section.
  491. // Else use the unrelocated section from the object file. We'll have to
  492. // apply relocations ourselves later.
  493. if (!L || !L->getLoadedSectionContents(name,data))
  494. Section.getContents(data);
  495. name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
  496. // Check if debug info section is compressed with zlib.
  497. if (name.startswith("zdebug_")) {
  498. uint64_t OriginalSize;
  499. if (!zlib::isAvailable() ||
  500. !consumeCompressedDebugSectionHeader(data, OriginalSize))
  501. continue;
  502. UncompressedSections.resize(UncompressedSections.size() + 1);
  503. if (zlib::uncompress(data, UncompressedSections.back(), OriginalSize) !=
  504. zlib::StatusOK) {
  505. UncompressedSections.pop_back();
  506. continue;
  507. }
  508. // Make data point to uncompressed section contents and save its contents.
  509. name = name.substr(1);
  510. data = UncompressedSections.back();
  511. }
  512. StringRef *SectionData =
  513. StringSwitch<StringRef *>(name)
  514. .Case("debug_info", &InfoSection.Data)
  515. .Case("debug_abbrev", &AbbrevSection)
  516. .Case("debug_loc", &LocSection.Data)
  517. .Case("debug_line", &LineSection.Data)
  518. .Case("debug_aranges", &ARangeSection)
  519. .Case("debug_frame", &DebugFrameSection)
  520. .Case("debug_str", &StringSection)
  521. .Case("debug_ranges", &RangeSection)
  522. .Case("debug_pubnames", &PubNamesSection)
  523. .Case("debug_pubtypes", &PubTypesSection)
  524. .Case("debug_gnu_pubnames", &GnuPubNamesSection)
  525. .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
  526. .Case("debug_info.dwo", &InfoDWOSection.Data)
  527. .Case("debug_abbrev.dwo", &AbbrevDWOSection)
  528. .Case("debug_loc.dwo", &LocDWOSection.Data)
  529. .Case("debug_line.dwo", &LineDWOSection.Data)
  530. .Case("debug_str.dwo", &StringDWOSection)
  531. .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
  532. .Case("debug_addr", &AddrSection)
  533. .Case("apple_names", &AppleNamesSection.Data)
  534. .Case("apple_types", &AppleTypesSection.Data)
  535. .Case("apple_namespaces", &AppleNamespacesSection.Data)
  536. .Case("apple_namespac", &AppleNamespacesSection.Data)
  537. .Case("apple_objc", &AppleObjCSection.Data)
  538. // Any more debug info sections go here.
  539. .Default(nullptr);
  540. if (SectionData) {
  541. *SectionData = data;
  542. if (name == "debug_ranges") {
  543. // FIXME: Use the other dwo range section when we emit it.
  544. RangeDWOSection = data;
  545. }
  546. } else if (name == "debug_types") {
  547. // Find debug_types data by section rather than name as there are
  548. // multiple, comdat grouped, debug_types sections.
  549. TypesSections[Section].Data = data;
  550. } else if (name == "debug_types.dwo") {
  551. TypesDWOSections[Section].Data = data;
  552. }
  553. section_iterator RelocatedSection = Section.getRelocatedSection();
  554. if (RelocatedSection == Obj.section_end())
  555. continue;
  556. StringRef RelSecName;
  557. StringRef RelSecData;
  558. RelocatedSection->getName(RelSecName);
  559. // If the section we're relocating was relocated already by the JIT,
  560. // then we used the relocated version above, so we do not need to process
  561. // relocations for it now.
  562. if (L && L->getLoadedSectionContents(RelSecName,RelSecData))
  563. continue;
  564. RelSecName = RelSecName.substr(
  565. RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
  566. // TODO: Add support for relocations in other sections as needed.
  567. // Record relocations for the debug_info and debug_line sections.
  568. RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
  569. .Case("debug_info", &InfoSection.Relocs)
  570. .Case("debug_loc", &LocSection.Relocs)
  571. .Case("debug_info.dwo", &InfoDWOSection.Relocs)
  572. .Case("debug_line", &LineSection.Relocs)
  573. .Case("apple_names", &AppleNamesSection.Relocs)
  574. .Case("apple_types", &AppleTypesSection.Relocs)
  575. .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
  576. .Case("apple_namespac", &AppleNamespacesSection.Relocs)
  577. .Case("apple_objc", &AppleObjCSection.Relocs)
  578. .Default(nullptr);
  579. if (!Map) {
  580. // Find debug_types relocs by section rather than name as there are
  581. // multiple, comdat grouped, debug_types sections.
  582. if (RelSecName == "debug_types")
  583. Map = &TypesSections[*RelocatedSection].Relocs;
  584. else if (RelSecName == "debug_types.dwo")
  585. Map = &TypesDWOSections[*RelocatedSection].Relocs;
  586. else
  587. continue;
  588. }
  589. if (Section.relocation_begin() != Section.relocation_end()) {
  590. uint64_t SectionSize = RelocatedSection->getSize();
  591. for (const RelocationRef &Reloc : Section.relocations()) {
  592. uint64_t Address = Reloc.getOffset();
  593. uint64_t Type = Reloc.getType();
  594. uint64_t SymAddr = 0;
  595. uint64_t SectionLoadAddress = 0;
  596. object::symbol_iterator Sym = Reloc.getSymbol();
  597. object::section_iterator RSec = Obj.section_end();
  598. // First calculate the address of the symbol or section as it appears
  599. // in the objct file
  600. if (Sym != Obj.symbol_end()) {
  601. ErrorOr<uint64_t> SymAddrOrErr = Sym->getAddress();
  602. if (std::error_code EC = SymAddrOrErr.getError()) {
  603. errs() << "error: failed to compute symbol address: "
  604. << EC.message() << '\n';
  605. continue;
  606. }
  607. SymAddr = *SymAddrOrErr;
  608. // Also remember what section this symbol is in for later
  609. Sym->getSection(RSec);
  610. } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
  611. // MachO also has relocations that point to sections and
  612. // scattered relocations.
  613. // FIXME: We are not handling scattered relocations, do we have to?
  614. RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
  615. SymAddr = RSec->getAddress();
  616. }
  617. // If we are given load addresses for the sections, we need to adjust:
  618. // SymAddr = (Address of Symbol Or Section in File) -
  619. // (Address of Section in File) +
  620. // (Load Address of Section)
  621. if (L != nullptr && RSec != Obj.section_end()) {
  622. // RSec is now either the section being targetted or the section
  623. // containing the symbol being targetted. In either case,
  624. // we need to perform the same computation.
  625. StringRef SecName;
  626. RSec->getName(SecName);
  627. SectionLoadAddress = L->getSectionLoadAddress(SecName);
  628. if (SectionLoadAddress != 0)
  629. SymAddr += SectionLoadAddress - RSec->getAddress();
  630. }
  631. object::RelocVisitor V(Obj);
  632. object::RelocToApply R(V.visit(Type, Reloc, SymAddr));
  633. if (V.error()) {
  634. SmallString<32> Name;
  635. Reloc.getTypeName(Name);
  636. errs() << "error: failed to compute relocation: "
  637. << Name << "\n";
  638. continue;
  639. }
  640. if (Address + R.Width > SectionSize) {
  641. errs() << "error: " << R.Width << "-byte relocation starting "
  642. << Address << " bytes into section " << name << " which is "
  643. << SectionSize << " bytes long.\n";
  644. continue;
  645. }
  646. if (R.Width > 8) {
  647. errs() << "error: can't handle a relocation of more than 8 bytes at "
  648. "a time.\n";
  649. continue;
  650. }
  651. DEBUG(dbgs() << "Writing " << format("%p", R.Value)
  652. << " at " << format("%p", Address)
  653. << " with width " << format("%d", R.Width)
  654. << "\n");
  655. Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
  656. }
  657. }
  658. }
  659. }
  660. void DWARFContextInMemory::anchor() { }