DWARFContext.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. //===- DWARFContext.cpp ---------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  9. #include "llvm/ADT/MapVector.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallString.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/ADT/StringSwitch.h"
  15. #include "llvm/BinaryFormat/Dwarf.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
  17. #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
  18. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFDebugAddr.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
  22. #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
  23. #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
  24. #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
  25. #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
  26. #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
  27. #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
  28. #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
  29. #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
  30. #include "llvm/DebugInfo/DWARF/DWARFDie.h"
  31. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  32. #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
  33. #include "llvm/DebugInfo/DWARF/DWARFListTable.h"
  34. #include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
  35. #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
  36. #include "llvm/DebugInfo/DWARF/DWARFSection.h"
  37. #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
  38. #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
  39. #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
  40. #include "llvm/MC/TargetRegistry.h"
  41. #include "llvm/Object/Decompressor.h"
  42. #include "llvm/Object/MachO.h"
  43. #include "llvm/Object/ObjectFile.h"
  44. #include "llvm/Object/RelocationResolver.h"
  45. #include "llvm/Support/Casting.h"
  46. #include "llvm/Support/DataExtractor.h"
  47. #include "llvm/Support/Error.h"
  48. #include "llvm/Support/Format.h"
  49. #include "llvm/Support/LEB128.h"
  50. #include "llvm/Support/MemoryBuffer.h"
  51. #include "llvm/Support/Path.h"
  52. #include "llvm/Support/raw_ostream.h"
  53. #include <algorithm>
  54. #include <cstdint>
  55. #include <deque>
  56. #include <map>
  57. #include <string>
  58. #include <utility>
  59. #include <vector>
  60. using namespace llvm;
  61. using namespace dwarf;
  62. using namespace object;
  63. #define DEBUG_TYPE "dwarf"
  64. using DWARFLineTable = DWARFDebugLine::LineTable;
  65. using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
  66. using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
  67. DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
  68. std::string DWPName,
  69. std::function<void(Error)> RecoverableErrorHandler,
  70. std::function<void(Error)> WarningHandler)
  71. : DIContext(CK_DWARF), DWPName(std::move(DWPName)),
  72. RecoverableErrorHandler(RecoverableErrorHandler),
  73. WarningHandler(WarningHandler), DObj(std::move(DObj)) {}
  74. DWARFContext::~DWARFContext() = default;
  75. /// Dump the UUID load command.
  76. static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
  77. auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
  78. if (!MachO)
  79. return;
  80. for (auto LC : MachO->load_commands()) {
  81. raw_ostream::uuid_t UUID;
  82. if (LC.C.cmd == MachO::LC_UUID) {
  83. if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
  84. OS << "error: UUID load command is too short.\n";
  85. return;
  86. }
  87. OS << "UUID: ";
  88. memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
  89. OS.write_uuid(UUID);
  90. Triple T = MachO->getArchTriple();
  91. OS << " (" << T.getArchName() << ')';
  92. OS << ' ' << MachO->getFileName() << '\n';
  93. }
  94. }
  95. }
  96. using ContributionCollection =
  97. std::vector<std::optional<StrOffsetsContributionDescriptor>>;
  98. // Collect all the contributions to the string offsets table from all units,
  99. // sort them by their starting offsets and remove duplicates.
  100. static ContributionCollection
  101. collectContributionData(DWARFContext::unit_iterator_range Units) {
  102. ContributionCollection Contributions;
  103. for (const auto &U : Units)
  104. if (const auto &C = U->getStringOffsetsTableContribution())
  105. Contributions.push_back(C);
  106. // Sort the contributions so that any invalid ones are placed at
  107. // the start of the contributions vector. This way they are reported
  108. // first.
  109. llvm::sort(Contributions,
  110. [](const std::optional<StrOffsetsContributionDescriptor> &L,
  111. const std::optional<StrOffsetsContributionDescriptor> &R) {
  112. if (L && R)
  113. return L->Base < R->Base;
  114. return R.has_value();
  115. });
  116. // Uniquify contributions, as it is possible that units (specifically
  117. // type units in dwo or dwp files) share contributions. We don't want
  118. // to report them more than once.
  119. Contributions.erase(
  120. std::unique(Contributions.begin(), Contributions.end(),
  121. [](const std::optional<StrOffsetsContributionDescriptor> &L,
  122. const std::optional<StrOffsetsContributionDescriptor> &R) {
  123. if (L && R)
  124. return L->Base == R->Base && L->Size == R->Size;
  125. return false;
  126. }),
  127. Contributions.end());
  128. return Contributions;
  129. }
  130. // Dump a DWARF string offsets section. This may be a DWARF v5 formatted
  131. // string offsets section, where each compile or type unit contributes a
  132. // number of entries (string offsets), with each contribution preceded by
  133. // a header containing size and version number. Alternatively, it may be a
  134. // monolithic series of string offsets, as generated by the pre-DWARF v5
  135. // implementation of split DWARF; however, in that case we still need to
  136. // collect contributions of units because the size of the offsets (4 or 8
  137. // bytes) depends on the format of the referencing unit (DWARF32 or DWARF64).
  138. static void dumpStringOffsetsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
  139. StringRef SectionName,
  140. const DWARFObject &Obj,
  141. const DWARFSection &StringOffsetsSection,
  142. StringRef StringSection,
  143. DWARFContext::unit_iterator_range Units,
  144. bool LittleEndian) {
  145. auto Contributions = collectContributionData(Units);
  146. DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
  147. DataExtractor StrData(StringSection, LittleEndian, 0);
  148. uint64_t SectionSize = StringOffsetsSection.Data.size();
  149. uint64_t Offset = 0;
  150. for (auto &Contribution : Contributions) {
  151. // Report an ill-formed contribution.
  152. if (!Contribution) {
  153. OS << "error: invalid contribution to string offsets table in section ."
  154. << SectionName << ".\n";
  155. return;
  156. }
  157. dwarf::DwarfFormat Format = Contribution->getFormat();
  158. int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
  159. uint16_t Version = Contribution->getVersion();
  160. uint64_t ContributionHeader = Contribution->Base;
  161. // In DWARF v5 there is a contribution header that immediately precedes
  162. // the string offsets base (the location we have previously retrieved from
  163. // the CU DIE's DW_AT_str_offsets attribute). The header is located either
  164. // 8 or 16 bytes before the base, depending on the contribution's format.
  165. if (Version >= 5)
  166. ContributionHeader -= Format == DWARF32 ? 8 : 16;
  167. // Detect overlapping contributions.
  168. if (Offset > ContributionHeader) {
  169. DumpOpts.RecoverableErrorHandler(createStringError(
  170. errc::invalid_argument,
  171. "overlapping contributions to string offsets table in section .%s.",
  172. SectionName.data()));
  173. }
  174. // Report a gap in the table.
  175. if (Offset < ContributionHeader) {
  176. OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset);
  177. OS << (ContributionHeader - Offset) << "\n";
  178. }
  179. OS << format("0x%8.8" PRIx64 ": ", ContributionHeader);
  180. // In DWARF v5 the contribution size in the descriptor does not equal
  181. // the originally encoded length (it does not contain the length of the
  182. // version field and the padding, a total of 4 bytes). Add them back in
  183. // for reporting.
  184. OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4))
  185. << ", Format = " << dwarf::FormatString(Format)
  186. << ", Version = " << Version << "\n";
  187. Offset = Contribution->Base;
  188. unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
  189. while (Offset - Contribution->Base < Contribution->Size) {
  190. OS << format("0x%8.8" PRIx64 ": ", Offset);
  191. uint64_t StringOffset =
  192. StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
  193. OS << format("%0*" PRIx64 " ", OffsetDumpWidth, StringOffset);
  194. const char *S = StrData.getCStr(&StringOffset);
  195. if (S)
  196. OS << format("\"%s\"", S);
  197. OS << "\n";
  198. }
  199. }
  200. // Report a gap at the end of the table.
  201. if (Offset < SectionSize) {
  202. OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset);
  203. OS << (SectionSize - Offset) << "\n";
  204. }
  205. }
  206. // Dump the .debug_addr section.
  207. static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData,
  208. DIDumpOptions DumpOpts, uint16_t Version,
  209. uint8_t AddrSize) {
  210. uint64_t Offset = 0;
  211. while (AddrData.isValidOffset(Offset)) {
  212. DWARFDebugAddrTable AddrTable;
  213. uint64_t TableOffset = Offset;
  214. if (Error Err = AddrTable.extract(AddrData, &Offset, Version, AddrSize,
  215. DumpOpts.WarningHandler)) {
  216. DumpOpts.RecoverableErrorHandler(std::move(Err));
  217. // Keep going after an error, if we can, assuming that the length field
  218. // could be read. If it couldn't, stop reading the section.
  219. if (auto TableLength = AddrTable.getFullLength()) {
  220. Offset = TableOffset + *TableLength;
  221. continue;
  222. }
  223. break;
  224. }
  225. AddrTable.dump(OS, DumpOpts);
  226. }
  227. }
  228. // Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5).
  229. static void dumpRnglistsSection(
  230. raw_ostream &OS, DWARFDataExtractor &rnglistData,
  231. llvm::function_ref<std::optional<object::SectionedAddress>(uint32_t)>
  232. LookupPooledAddress,
  233. DIDumpOptions DumpOpts) {
  234. uint64_t Offset = 0;
  235. while (rnglistData.isValidOffset(Offset)) {
  236. llvm::DWARFDebugRnglistTable Rnglists;
  237. uint64_t TableOffset = Offset;
  238. if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
  239. DumpOpts.RecoverableErrorHandler(std::move(Err));
  240. uint64_t Length = Rnglists.length();
  241. // Keep going after an error, if we can, assuming that the length field
  242. // could be read. If it couldn't, stop reading the section.
  243. if (Length == 0)
  244. break;
  245. Offset = TableOffset + Length;
  246. } else {
  247. Rnglists.dump(rnglistData, OS, LookupPooledAddress, DumpOpts);
  248. }
  249. }
  250. }
  251. std::unique_ptr<DWARFDebugMacro>
  252. DWARFContext::parseMacroOrMacinfo(MacroSecType SectionType) {
  253. auto Macro = std::make_unique<DWARFDebugMacro>();
  254. auto ParseAndDump = [&](DWARFDataExtractor &Data, bool IsMacro) {
  255. if (Error Err = IsMacro ? Macro->parseMacro(SectionType == MacroSection
  256. ? compile_units()
  257. : dwo_compile_units(),
  258. SectionType == MacroSection
  259. ? getStringExtractor()
  260. : getStringDWOExtractor(),
  261. Data)
  262. : Macro->parseMacinfo(Data)) {
  263. RecoverableErrorHandler(std::move(Err));
  264. Macro = nullptr;
  265. }
  266. };
  267. switch (SectionType) {
  268. case MacinfoSection: {
  269. DWARFDataExtractor Data(DObj->getMacinfoSection(), isLittleEndian(), 0);
  270. ParseAndDump(Data, /*IsMacro=*/false);
  271. break;
  272. }
  273. case MacinfoDwoSection: {
  274. DWARFDataExtractor Data(DObj->getMacinfoDWOSection(), isLittleEndian(), 0);
  275. ParseAndDump(Data, /*IsMacro=*/false);
  276. break;
  277. }
  278. case MacroSection: {
  279. DWARFDataExtractor Data(*DObj, DObj->getMacroSection(), isLittleEndian(),
  280. 0);
  281. ParseAndDump(Data, /*IsMacro=*/true);
  282. break;
  283. }
  284. case MacroDwoSection: {
  285. DWARFDataExtractor Data(DObj->getMacroDWOSection(), isLittleEndian(), 0);
  286. ParseAndDump(Data, /*IsMacro=*/true);
  287. break;
  288. }
  289. }
  290. return Macro;
  291. }
  292. static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
  293. DWARFDataExtractor Data, const DWARFObject &Obj,
  294. std::optional<uint64_t> DumpOffset) {
  295. uint64_t Offset = 0;
  296. while (Data.isValidOffset(Offset)) {
  297. DWARFListTableHeader Header(".debug_loclists", "locations");
  298. if (Error E = Header.extract(Data, &Offset)) {
  299. DumpOpts.RecoverableErrorHandler(std::move(E));
  300. return;
  301. }
  302. Header.dump(Data, OS, DumpOpts);
  303. uint64_t EndOffset = Header.length() + Header.getHeaderOffset();
  304. Data.setAddressSize(Header.getAddrSize());
  305. DWARFDebugLoclists Loc(Data, Header.getVersion());
  306. if (DumpOffset) {
  307. if (DumpOffset >= Offset && DumpOffset < EndOffset) {
  308. Offset = *DumpOffset;
  309. Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, Obj,
  310. nullptr, DumpOpts, /*Indent=*/0);
  311. OS << "\n";
  312. return;
  313. }
  314. } else {
  315. Loc.dumpRange(Offset, EndOffset - Offset, OS, Obj, DumpOpts);
  316. }
  317. Offset = EndOffset;
  318. }
  319. }
  320. static void dumpPubTableSection(raw_ostream &OS, DIDumpOptions DumpOpts,
  321. DWARFDataExtractor Data, bool GnuStyle) {
  322. DWARFDebugPubTable Table;
  323. Table.extract(Data, GnuStyle, DumpOpts.RecoverableErrorHandler);
  324. Table.dump(OS);
  325. }
  326. void DWARFContext::dump(
  327. raw_ostream &OS, DIDumpOptions DumpOpts,
  328. std::array<std::optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
  329. uint64_t DumpType = DumpOpts.DumpType;
  330. StringRef Extension = sys::path::extension(DObj->getFileName());
  331. bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
  332. // Print UUID header.
  333. const auto *ObjFile = DObj->getFile();
  334. if (DumpType & DIDT_UUID)
  335. dumpUUID(OS, *ObjFile);
  336. // Print a header for each explicitly-requested section.
  337. // Otherwise just print one for non-empty sections.
  338. // Only print empty .dwo section headers when dumping a .dwo file.
  339. bool Explicit = DumpType != DIDT_All && !IsDWO;
  340. bool ExplicitDWO = Explicit && IsDWO;
  341. auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
  342. StringRef Section) -> std::optional<uint64_t> * {
  343. unsigned Mask = 1U << ID;
  344. bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
  345. if (!Should)
  346. return nullptr;
  347. OS << "\n" << Name << " contents:\n";
  348. return &DumpOffsets[ID];
  349. };
  350. // Dump individual sections.
  351. if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
  352. DObj->getAbbrevSection()))
  353. getDebugAbbrev()->dump(OS);
  354. if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
  355. DObj->getAbbrevDWOSection()))
  356. getDebugAbbrevDWO()->dump(OS);
  357. auto dumpDebugInfo = [&](const char *Name, unit_iterator_range Units) {
  358. OS << '\n' << Name << " contents:\n";
  359. if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugInfo])
  360. for (const auto &U : Units)
  361. U->getDIEForOffset(*DumpOffset)
  362. .dump(OS, 0, DumpOpts.noImplicitRecursion());
  363. else
  364. for (const auto &U : Units)
  365. U->dump(OS, DumpOpts);
  366. };
  367. if ((DumpType & DIDT_DebugInfo)) {
  368. if (Explicit || getNumCompileUnits())
  369. dumpDebugInfo(".debug_info", info_section_units());
  370. if (ExplicitDWO || getNumDWOCompileUnits())
  371. dumpDebugInfo(".debug_info.dwo", dwo_info_section_units());
  372. }
  373. auto dumpDebugType = [&](const char *Name, unit_iterator_range Units) {
  374. OS << '\n' << Name << " contents:\n";
  375. for (const auto &U : Units)
  376. if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugTypes])
  377. U->getDIEForOffset(*DumpOffset)
  378. .dump(OS, 0, DumpOpts.noImplicitRecursion());
  379. else
  380. U->dump(OS, DumpOpts);
  381. };
  382. if ((DumpType & DIDT_DebugTypes)) {
  383. if (Explicit || getNumTypeUnits())
  384. dumpDebugType(".debug_types", types_section_units());
  385. if (ExplicitDWO || getNumDWOTypeUnits())
  386. dumpDebugType(".debug_types.dwo", dwo_types_section_units());
  387. }
  388. DIDumpOptions LLDumpOpts = DumpOpts;
  389. if (LLDumpOpts.Verbose)
  390. LLDumpOpts.DisplayRawContents = true;
  391. if (const auto *Off = shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
  392. DObj->getLocSection().Data)) {
  393. getDebugLoc()->dump(OS, *DObj, LLDumpOpts, *Off);
  394. }
  395. if (const auto *Off =
  396. shouldDump(Explicit, ".debug_loclists", DIDT_ID_DebugLoclists,
  397. DObj->getLoclistsSection().Data)) {
  398. DWARFDataExtractor Data(*DObj, DObj->getLoclistsSection(), isLittleEndian(),
  399. 0);
  400. dumpLoclistsSection(OS, LLDumpOpts, Data, *DObj, *Off);
  401. }
  402. if (const auto *Off =
  403. shouldDump(ExplicitDWO, ".debug_loclists.dwo", DIDT_ID_DebugLoclists,
  404. DObj->getLoclistsDWOSection().Data)) {
  405. DWARFDataExtractor Data(*DObj, DObj->getLoclistsDWOSection(),
  406. isLittleEndian(), 0);
  407. dumpLoclistsSection(OS, LLDumpOpts, Data, *DObj, *Off);
  408. }
  409. if (const auto *Off =
  410. shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
  411. DObj->getLocDWOSection().Data)) {
  412. DWARFDataExtractor Data(*DObj, DObj->getLocDWOSection(), isLittleEndian(),
  413. 4);
  414. DWARFDebugLoclists Loc(Data, /*Version=*/4);
  415. if (*Off) {
  416. uint64_t Offset = **Off;
  417. Loc.dumpLocationList(&Offset, OS,
  418. /*BaseAddr=*/std::nullopt, *DObj, nullptr,
  419. LLDumpOpts,
  420. /*Indent=*/0);
  421. OS << "\n";
  422. } else {
  423. Loc.dumpRange(0, Data.getData().size(), OS, *DObj, LLDumpOpts);
  424. }
  425. }
  426. if (const std::optional<uint64_t> *Off =
  427. shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
  428. DObj->getFrameSection().Data)) {
  429. if (Expected<const DWARFDebugFrame *> DF = getDebugFrame())
  430. (*DF)->dump(OS, DumpOpts, *Off);
  431. else
  432. RecoverableErrorHandler(DF.takeError());
  433. }
  434. if (const std::optional<uint64_t> *Off =
  435. shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
  436. DObj->getEHFrameSection().Data)) {
  437. if (Expected<const DWARFDebugFrame *> DF = getEHFrame())
  438. (*DF)->dump(OS, DumpOpts, *Off);
  439. else
  440. RecoverableErrorHandler(DF.takeError());
  441. }
  442. if (shouldDump(Explicit, ".debug_macro", DIDT_ID_DebugMacro,
  443. DObj->getMacroSection().Data)) {
  444. if (auto Macro = getDebugMacro())
  445. Macro->dump(OS);
  446. }
  447. if (shouldDump(Explicit, ".debug_macro.dwo", DIDT_ID_DebugMacro,
  448. DObj->getMacroDWOSection())) {
  449. if (auto MacroDWO = getDebugMacroDWO())
  450. MacroDWO->dump(OS);
  451. }
  452. if (shouldDump(Explicit, ".debug_macinfo", DIDT_ID_DebugMacro,
  453. DObj->getMacinfoSection())) {
  454. if (auto Macinfo = getDebugMacinfo())
  455. Macinfo->dump(OS);
  456. }
  457. if (shouldDump(Explicit, ".debug_macinfo.dwo", DIDT_ID_DebugMacro,
  458. DObj->getMacinfoDWOSection())) {
  459. if (auto MacinfoDWO = getDebugMacinfoDWO())
  460. MacinfoDWO->dump(OS);
  461. }
  462. if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
  463. DObj->getArangesSection())) {
  464. uint64_t offset = 0;
  465. DWARFDataExtractor arangesData(DObj->getArangesSection(), isLittleEndian(),
  466. 0);
  467. DWARFDebugArangeSet set;
  468. while (arangesData.isValidOffset(offset)) {
  469. if (Error E =
  470. set.extract(arangesData, &offset, DumpOpts.WarningHandler)) {
  471. RecoverableErrorHandler(std::move(E));
  472. break;
  473. }
  474. set.dump(OS);
  475. }
  476. }
  477. auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
  478. DIDumpOptions DumpOpts,
  479. std::optional<uint64_t> DumpOffset) {
  480. while (!Parser.done()) {
  481. if (DumpOffset && Parser.getOffset() != *DumpOffset) {
  482. Parser.skip(DumpOpts.WarningHandler, DumpOpts.WarningHandler);
  483. continue;
  484. }
  485. OS << "debug_line[" << format("0x%8.8" PRIx64, Parser.getOffset())
  486. << "]\n";
  487. Parser.parseNext(DumpOpts.WarningHandler, DumpOpts.WarningHandler, &OS,
  488. DumpOpts.Verbose);
  489. }
  490. };
  491. auto DumpStrSection = [&](StringRef Section) {
  492. DataExtractor StrData(Section, isLittleEndian(), 0);
  493. uint64_t Offset = 0;
  494. uint64_t StrOffset = 0;
  495. while (StrData.isValidOffset(Offset)) {
  496. Error Err = Error::success();
  497. const char *CStr = StrData.getCStr(&Offset, &Err);
  498. if (Err) {
  499. DumpOpts.WarningHandler(std::move(Err));
  500. return;
  501. }
  502. OS << format("0x%8.8" PRIx64 ": \"", StrOffset);
  503. OS.write_escaped(CStr);
  504. OS << "\"\n";
  505. StrOffset = Offset;
  506. }
  507. };
  508. if (const auto *Off = shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
  509. DObj->getLineSection().Data)) {
  510. DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
  511. 0);
  512. DWARFDebugLine::SectionParser Parser(LineData, *this, normal_units());
  513. DumpLineSection(Parser, DumpOpts, *Off);
  514. }
  515. if (const auto *Off =
  516. shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
  517. DObj->getLineDWOSection().Data)) {
  518. DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
  519. isLittleEndian(), 0);
  520. DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_units());
  521. DumpLineSection(Parser, DumpOpts, *Off);
  522. }
  523. if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
  524. DObj->getCUIndexSection())) {
  525. getCUIndex().dump(OS);
  526. }
  527. if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
  528. DObj->getTUIndexSection())) {
  529. getTUIndex().dump(OS);
  530. }
  531. if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
  532. DObj->getStrSection()))
  533. DumpStrSection(DObj->getStrSection());
  534. if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
  535. DObj->getStrDWOSection()))
  536. DumpStrSection(DObj->getStrDWOSection());
  537. if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
  538. DObj->getLineStrSection()))
  539. DumpStrSection(DObj->getLineStrSection());
  540. if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr,
  541. DObj->getAddrSection().Data)) {
  542. DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(),
  543. isLittleEndian(), 0);
  544. dumpAddrSection(OS, AddrData, DumpOpts, getMaxVersion(), getCUAddrSize());
  545. }
  546. if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
  547. DObj->getRangesSection().Data)) {
  548. uint8_t savedAddressByteSize = getCUAddrSize();
  549. DWARFDataExtractor rangesData(*DObj, DObj->getRangesSection(),
  550. isLittleEndian(), savedAddressByteSize);
  551. uint64_t offset = 0;
  552. DWARFDebugRangeList rangeList;
  553. while (rangesData.isValidOffset(offset)) {
  554. if (Error E = rangeList.extract(rangesData, &offset)) {
  555. DumpOpts.RecoverableErrorHandler(std::move(E));
  556. break;
  557. }
  558. rangeList.dump(OS);
  559. }
  560. }
  561. auto LookupPooledAddress =
  562. [&](uint32_t Index) -> std::optional<SectionedAddress> {
  563. const auto &CUs = compile_units();
  564. auto I = CUs.begin();
  565. if (I == CUs.end())
  566. return std::nullopt;
  567. return (*I)->getAddrOffsetSectionItem(Index);
  568. };
  569. if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
  570. DObj->getRnglistsSection().Data)) {
  571. DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(),
  572. isLittleEndian(), 0);
  573. dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts);
  574. }
  575. if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists,
  576. DObj->getRnglistsDWOSection().Data)) {
  577. DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(),
  578. isLittleEndian(), 0);
  579. dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts);
  580. }
  581. if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
  582. DObj->getPubnamesSection().Data)) {
  583. DWARFDataExtractor PubTableData(*DObj, DObj->getPubnamesSection(),
  584. isLittleEndian(), 0);
  585. dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/false);
  586. }
  587. if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
  588. DObj->getPubtypesSection().Data)) {
  589. DWARFDataExtractor PubTableData(*DObj, DObj->getPubtypesSection(),
  590. isLittleEndian(), 0);
  591. dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/false);
  592. }
  593. if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
  594. DObj->getGnuPubnamesSection().Data)) {
  595. DWARFDataExtractor PubTableData(*DObj, DObj->getGnuPubnamesSection(),
  596. isLittleEndian(), 0);
  597. dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/true);
  598. }
  599. if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
  600. DObj->getGnuPubtypesSection().Data)) {
  601. DWARFDataExtractor PubTableData(*DObj, DObj->getGnuPubtypesSection(),
  602. isLittleEndian(), 0);
  603. dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/true);
  604. }
  605. if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
  606. DObj->getStrOffsetsSection().Data))
  607. dumpStringOffsetsSection(
  608. OS, DumpOpts, "debug_str_offsets", *DObj, DObj->getStrOffsetsSection(),
  609. DObj->getStrSection(), normal_units(), isLittleEndian());
  610. if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
  611. DObj->getStrOffsetsDWOSection().Data))
  612. dumpStringOffsetsSection(OS, DumpOpts, "debug_str_offsets.dwo", *DObj,
  613. DObj->getStrOffsetsDWOSection(),
  614. DObj->getStrDWOSection(), dwo_units(),
  615. isLittleEndian());
  616. if (shouldDump(Explicit, ".gdb_index", DIDT_ID_GdbIndex,
  617. DObj->getGdbIndexSection())) {
  618. getGdbIndex().dump(OS);
  619. }
  620. if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
  621. DObj->getAppleNamesSection().Data))
  622. getAppleNames().dump(OS);
  623. if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
  624. DObj->getAppleTypesSection().Data))
  625. getAppleTypes().dump(OS);
  626. if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
  627. DObj->getAppleNamespacesSection().Data))
  628. getAppleNamespaces().dump(OS);
  629. if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
  630. DObj->getAppleObjCSection().Data))
  631. getAppleObjC().dump(OS);
  632. if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
  633. DObj->getNamesSection().Data))
  634. getDebugNames().dump(OS);
  635. }
  636. DWARFTypeUnit *DWARFContext::getTypeUnitForHash(uint16_t Version, uint64_t Hash,
  637. bool IsDWO) {
  638. parseDWOUnits(LazyParse);
  639. if (const auto &TUI = getTUIndex()) {
  640. if (const auto *R = TUI.getFromHash(Hash))
  641. return dyn_cast_or_null<DWARFTypeUnit>(
  642. DWOUnits.getUnitForIndexEntry(*R));
  643. return nullptr;
  644. }
  645. struct UnitContainers {
  646. const DWARFUnitVector &Units;
  647. std::optional<DenseMap<uint64_t, DWARFTypeUnit *>> &Map;
  648. };
  649. UnitContainers Units = IsDWO ? UnitContainers{DWOUnits, DWOTypeUnits}
  650. : UnitContainers{NormalUnits, NormalTypeUnits};
  651. if (!Units.Map) {
  652. Units.Map.emplace();
  653. for (const auto &U : IsDWO ? dwo_units() : normal_units()) {
  654. if (DWARFTypeUnit *TU = dyn_cast<DWARFTypeUnit>(U.get()))
  655. (*Units.Map)[TU->getTypeHash()] = TU;
  656. }
  657. }
  658. return (*Units.Map)[Hash];
  659. }
  660. DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
  661. parseDWOUnits(LazyParse);
  662. if (const auto &CUI = getCUIndex()) {
  663. if (const auto *R = CUI.getFromHash(Hash))
  664. return dyn_cast_or_null<DWARFCompileUnit>(
  665. DWOUnits.getUnitForIndexEntry(*R));
  666. return nullptr;
  667. }
  668. // If there's no index, just search through the CUs in the DWO - there's
  669. // probably only one unless this is something like LTO - though an in-process
  670. // built/cached lookup table could be used in that case to improve repeated
  671. // lookups of different CUs in the DWO.
  672. for (const auto &DWOCU : dwo_compile_units()) {
  673. // Might not have parsed DWO ID yet.
  674. if (!DWOCU->getDWOId()) {
  675. if (std::optional<uint64_t> DWOId =
  676. toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id)))
  677. DWOCU->setDWOId(*DWOId);
  678. else
  679. // No DWO ID?
  680. continue;
  681. }
  682. if (DWOCU->getDWOId() == Hash)
  683. return dyn_cast<DWARFCompileUnit>(DWOCU.get());
  684. }
  685. return nullptr;
  686. }
  687. DWARFDie DWARFContext::getDIEForOffset(uint64_t Offset) {
  688. parseNormalUnits();
  689. if (auto *CU = NormalUnits.getUnitForOffset(Offset))
  690. return CU->getDIEForOffset(Offset);
  691. return DWARFDie();
  692. }
  693. bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
  694. bool Success = true;
  695. DWARFVerifier verifier(OS, *this, DumpOpts);
  696. Success &= verifier.handleDebugAbbrev();
  697. if (DumpOpts.DumpType & DIDT_DebugCUIndex)
  698. Success &= verifier.handleDebugCUIndex();
  699. if (DumpOpts.DumpType & DIDT_DebugTUIndex)
  700. Success &= verifier.handleDebugTUIndex();
  701. if (DumpOpts.DumpType & DIDT_DebugInfo)
  702. Success &= verifier.handleDebugInfo();
  703. if (DumpOpts.DumpType & DIDT_DebugLine)
  704. Success &= verifier.handleDebugLine();
  705. Success &= verifier.handleAccelTables();
  706. return Success;
  707. }
  708. void fixupIndex(const DWARFObject &DObj, DWARFContext &C,
  709. DWARFUnitIndex &Index) {
  710. using EntryType = DWARFUnitIndex::Entry::SectionContribution;
  711. using EntryMap = DenseMap<uint32_t, EntryType>;
  712. EntryMap Map;
  713. if (DObj.getCUIndexSection().empty())
  714. return;
  715. uint64_t Offset = 0;
  716. uint32_t TruncOffset = 0;
  717. DObj.forEachInfoDWOSections([&](const DWARFSection &S) {
  718. if (!(C.getParseCUTUIndexManually() ||
  719. S.Data.size() >= std::numeric_limits<uint32_t>::max()))
  720. return;
  721. DWARFDataExtractor Data(DObj, S, C.isLittleEndian(), 0);
  722. while (Data.isValidOffset(Offset)) {
  723. DWARFUnitHeader Header;
  724. if (!Header.extract(C, Data, &Offset, DWARFSectionKind::DW_SECT_INFO)) {
  725. logAllUnhandledErrors(
  726. createError("Failed to parse CU header in DWP file"), errs());
  727. Map.clear();
  728. break;
  729. }
  730. auto Iter = Map.insert({TruncOffset,
  731. {Header.getOffset(), Header.getNextUnitOffset() -
  732. Header.getOffset()}});
  733. if (!Iter.second) {
  734. logAllUnhandledErrors(
  735. createError("Collision occured between for truncated offset 0x" +
  736. Twine::utohexstr(TruncOffset)),
  737. errs());
  738. Map.clear();
  739. return;
  740. }
  741. Offset = Header.getNextUnitOffset();
  742. TruncOffset = Offset;
  743. }
  744. });
  745. if (Map.empty())
  746. return;
  747. for (DWARFUnitIndex::Entry &E : Index.getMutableRows()) {
  748. if (!E.isValid())
  749. continue;
  750. DWARFUnitIndex::Entry::SectionContribution &CUOff = E.getContribution();
  751. auto Iter = Map.find(CUOff.getOffset());
  752. if (Iter == Map.end()) {
  753. logAllUnhandledErrors(createError("Could not find CU offset 0x" +
  754. Twine::utohexstr(CUOff.getOffset()) +
  755. " in the Map"),
  756. errs());
  757. break;
  758. }
  759. CUOff.setOffset(Iter->second.getOffset());
  760. if (CUOff.getOffset() != Iter->second.getOffset())
  761. logAllUnhandledErrors(createError("Length of CU in CU index doesn't "
  762. "match calculated length at offset 0x" +
  763. Twine::utohexstr(CUOff.getOffset())),
  764. errs());
  765. }
  766. return;
  767. }
  768. const DWARFUnitIndex &DWARFContext::getCUIndex() {
  769. if (CUIndex)
  770. return *CUIndex;
  771. DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
  772. CUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
  773. CUIndex->parse(CUIndexData);
  774. fixupIndex(*DObj, *this, *CUIndex.get());
  775. return *CUIndex;
  776. }
  777. const DWARFUnitIndex &DWARFContext::getTUIndex() {
  778. if (TUIndex)
  779. return *TUIndex;
  780. DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
  781. TUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_EXT_TYPES);
  782. bool isParseSuccessful = TUIndex->parse(TUIndexData);
  783. // If we are parsing TU-index and for .debug_types section we don't need
  784. // to do anything.
  785. if (isParseSuccessful && TUIndex->getVersion() != 2)
  786. fixupIndex(*DObj, *this, *TUIndex.get());
  787. return *TUIndex;
  788. }
  789. DWARFGdbIndex &DWARFContext::getGdbIndex() {
  790. if (GdbIndex)
  791. return *GdbIndex;
  792. DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
  793. GdbIndex = std::make_unique<DWARFGdbIndex>();
  794. GdbIndex->parse(GdbIndexData);
  795. return *GdbIndex;
  796. }
  797. const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
  798. if (Abbrev)
  799. return Abbrev.get();
  800. DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
  801. Abbrev.reset(new DWARFDebugAbbrev());
  802. Abbrev->extract(abbrData);
  803. return Abbrev.get();
  804. }
  805. const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
  806. if (AbbrevDWO)
  807. return AbbrevDWO.get();
  808. DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
  809. AbbrevDWO.reset(new DWARFDebugAbbrev());
  810. AbbrevDWO->extract(abbrData);
  811. return AbbrevDWO.get();
  812. }
  813. const DWARFDebugLoc *DWARFContext::getDebugLoc() {
  814. if (Loc)
  815. return Loc.get();
  816. // Assume all units have the same address byte size.
  817. auto LocData =
  818. getNumCompileUnits()
  819. ? DWARFDataExtractor(*DObj, DObj->getLocSection(), isLittleEndian(),
  820. getUnitAtIndex(0)->getAddressByteSize())
  821. : DWARFDataExtractor("", isLittleEndian(), 0);
  822. Loc.reset(new DWARFDebugLoc(std::move(LocData)));
  823. return Loc.get();
  824. }
  825. const DWARFDebugAranges *DWARFContext::getDebugAranges() {
  826. if (Aranges)
  827. return Aranges.get();
  828. Aranges.reset(new DWARFDebugAranges());
  829. Aranges->generate(this);
  830. return Aranges.get();
  831. }
  832. Expected<const DWARFDebugFrame *> DWARFContext::getDebugFrame() {
  833. if (DebugFrame)
  834. return DebugFrame.get();
  835. const DWARFSection &DS = DObj->getFrameSection();
  836. // There's a "bug" in the DWARFv3 standard with respect to the target address
  837. // size within debug frame sections. While DWARF is supposed to be independent
  838. // of its container, FDEs have fields with size being "target address size",
  839. // which isn't specified in DWARF in general. It's only specified for CUs, but
  840. // .eh_frame can appear without a .debug_info section. Follow the example of
  841. // other tools (libdwarf) and extract this from the container (ObjectFile
  842. // provides this information). This problem is fixed in DWARFv4
  843. // See this dwarf-discuss discussion for more details:
  844. // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
  845. DWARFDataExtractor DebugFrameData(*DObj, DS, isLittleEndian(),
  846. DObj->getAddressSize());
  847. auto DF =
  848. std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/false, DS.Address);
  849. if (Error E = DF->parse(DebugFrameData))
  850. return std::move(E);
  851. DebugFrame.swap(DF);
  852. return DebugFrame.get();
  853. }
  854. Expected<const DWARFDebugFrame *> DWARFContext::getEHFrame() {
  855. if (EHFrame)
  856. return EHFrame.get();
  857. const DWARFSection &DS = DObj->getEHFrameSection();
  858. DWARFDataExtractor DebugFrameData(*DObj, DS, isLittleEndian(),
  859. DObj->getAddressSize());
  860. auto DF =
  861. std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/true, DS.Address);
  862. if (Error E = DF->parse(DebugFrameData))
  863. return std::move(E);
  864. DebugFrame.swap(DF);
  865. return DebugFrame.get();
  866. }
  867. const DWARFDebugMacro *DWARFContext::getDebugMacro() {
  868. if (!Macro)
  869. Macro = parseMacroOrMacinfo(MacroSection);
  870. return Macro.get();
  871. }
  872. const DWARFDebugMacro *DWARFContext::getDebugMacroDWO() {
  873. if (!MacroDWO)
  874. MacroDWO = parseMacroOrMacinfo(MacroDwoSection);
  875. return MacroDWO.get();
  876. }
  877. const DWARFDebugMacro *DWARFContext::getDebugMacinfo() {
  878. if (!Macinfo)
  879. Macinfo = parseMacroOrMacinfo(MacinfoSection);
  880. return Macinfo.get();
  881. }
  882. const DWARFDebugMacro *DWARFContext::getDebugMacinfoDWO() {
  883. if (!MacinfoDWO)
  884. MacinfoDWO = parseMacroOrMacinfo(MacinfoDwoSection);
  885. return MacinfoDWO.get();
  886. }
  887. template <typename T>
  888. static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
  889. const DWARFSection &Section, StringRef StringSection,
  890. bool IsLittleEndian) {
  891. if (Cache)
  892. return *Cache;
  893. DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
  894. DataExtractor StrData(StringSection, IsLittleEndian, 0);
  895. Cache.reset(new T(AccelSection, StrData));
  896. if (Error E = Cache->extract())
  897. llvm::consumeError(std::move(E));
  898. return *Cache;
  899. }
  900. const DWARFDebugNames &DWARFContext::getDebugNames() {
  901. return getAccelTable(Names, *DObj, DObj->getNamesSection(),
  902. DObj->getStrSection(), isLittleEndian());
  903. }
  904. const AppleAcceleratorTable &DWARFContext::getAppleNames() {
  905. return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
  906. DObj->getStrSection(), isLittleEndian());
  907. }
  908. const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
  909. return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
  910. DObj->getStrSection(), isLittleEndian());
  911. }
  912. const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
  913. return getAccelTable(AppleNamespaces, *DObj,
  914. DObj->getAppleNamespacesSection(),
  915. DObj->getStrSection(), isLittleEndian());
  916. }
  917. const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
  918. return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
  919. DObj->getStrSection(), isLittleEndian());
  920. }
  921. const DWARFDebugLine::LineTable *
  922. DWARFContext::getLineTableForUnit(DWARFUnit *U) {
  923. Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
  924. getLineTableForUnit(U, WarningHandler);
  925. if (!ExpectedLineTable) {
  926. WarningHandler(ExpectedLineTable.takeError());
  927. return nullptr;
  928. }
  929. return *ExpectedLineTable;
  930. }
  931. Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit(
  932. DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) {
  933. if (!Line)
  934. Line.reset(new DWARFDebugLine);
  935. auto UnitDIE = U->getUnitDIE();
  936. if (!UnitDIE)
  937. return nullptr;
  938. auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
  939. if (!Offset)
  940. return nullptr; // No line table for this compile unit.
  941. uint64_t stmtOffset = *Offset + U->getLineTableOffset();
  942. // See if the line table is cached.
  943. if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
  944. return lt;
  945. // Make sure the offset is good before we try to parse.
  946. if (stmtOffset >= U->getLineSection().Data.size())
  947. return nullptr;
  948. // We have to parse it first.
  949. DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
  950. U->getAddressByteSize());
  951. return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
  952. RecoverableErrorHandler);
  953. }
  954. void DWARFContext::clearLineTableForUnit(DWARFUnit *U) {
  955. if (!Line)
  956. return;
  957. auto UnitDIE = U->getUnitDIE();
  958. if (!UnitDIE)
  959. return;
  960. auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
  961. if (!Offset)
  962. return;
  963. uint64_t stmtOffset = *Offset + U->getLineTableOffset();
  964. Line->clearLineTable(stmtOffset);
  965. }
  966. void DWARFContext::parseNormalUnits() {
  967. if (!NormalUnits.empty())
  968. return;
  969. DObj->forEachInfoSections([&](const DWARFSection &S) {
  970. NormalUnits.addUnitsForSection(*this, S, DW_SECT_INFO);
  971. });
  972. NormalUnits.finishedInfoUnits();
  973. DObj->forEachTypesSections([&](const DWARFSection &S) {
  974. NormalUnits.addUnitsForSection(*this, S, DW_SECT_EXT_TYPES);
  975. });
  976. }
  977. void DWARFContext::parseDWOUnits(bool Lazy) {
  978. if (!DWOUnits.empty())
  979. return;
  980. DObj->forEachInfoDWOSections([&](const DWARFSection &S) {
  981. DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_INFO, Lazy);
  982. });
  983. DWOUnits.finishedInfoUnits();
  984. DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
  985. DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_EXT_TYPES, Lazy);
  986. });
  987. }
  988. DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint64_t Offset) {
  989. parseNormalUnits();
  990. return dyn_cast_or_null<DWARFCompileUnit>(
  991. NormalUnits.getUnitForOffset(Offset));
  992. }
  993. DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
  994. // First, get the offset of the compile unit.
  995. uint64_t CUOffset = getDebugAranges()->findAddress(Address);
  996. // Retrieve the compile unit.
  997. if (DWARFCompileUnit *OffsetCU = getCompileUnitForOffset(CUOffset))
  998. return OffsetCU;
  999. // Global variables are often not found by the above search, for one of two
  1000. // reasons:
  1001. // 1. .debug_aranges may not include global variables. On clang, it seems we
  1002. // put the globals in the aranges, but this isn't true for gcc.
  1003. // 2. Even if the global variable is in a .debug_arange, global variables
  1004. // may not be captured in the [start, end) addresses described by the
  1005. // parent compile unit.
  1006. //
  1007. // So, we walk the CU's and their child DI's manually, looking for the
  1008. // specific global variable.
  1009. for (std::unique_ptr<DWARFUnit> &CU : compile_units()) {
  1010. if (DWARFDie Die = CU->getVariableForAddress(Address)) {
  1011. return static_cast<DWARFCompileUnit *>(CU.get());
  1012. }
  1013. }
  1014. return nullptr;
  1015. }
  1016. DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
  1017. DIEsForAddress Result;
  1018. DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
  1019. if (!CU)
  1020. return Result;
  1021. Result.CompileUnit = CU;
  1022. Result.FunctionDIE = CU->getSubroutineForAddress(Address);
  1023. std::vector<DWARFDie> Worklist;
  1024. Worklist.push_back(Result.FunctionDIE);
  1025. while (!Worklist.empty()) {
  1026. DWARFDie DIE = Worklist.back();
  1027. Worklist.pop_back();
  1028. if (!DIE.isValid())
  1029. continue;
  1030. if (DIE.getTag() == DW_TAG_lexical_block &&
  1031. DIE.addressRangeContainsAddress(Address)) {
  1032. Result.BlockDIE = DIE;
  1033. break;
  1034. }
  1035. append_range(Worklist, DIE);
  1036. }
  1037. return Result;
  1038. }
  1039. /// TODO: change input parameter from "uint64_t Address"
  1040. /// into "SectionedAddress Address"
  1041. static bool getFunctionNameAndStartLineForAddress(
  1042. DWARFCompileUnit *CU, uint64_t Address, FunctionNameKind Kind,
  1043. DILineInfoSpecifier::FileLineInfoKind FileNameKind,
  1044. std::string &FunctionName, std::string &StartFile, uint32_t &StartLine,
  1045. std::optional<uint64_t> &StartAddress) {
  1046. // The address may correspond to instruction in some inlined function,
  1047. // so we have to build the chain of inlined functions and take the
  1048. // name of the topmost function in it.
  1049. SmallVector<DWARFDie, 4> InlinedChain;
  1050. CU->getInlinedChainForAddress(Address, InlinedChain);
  1051. if (InlinedChain.empty())
  1052. return false;
  1053. const DWARFDie &DIE = InlinedChain[0];
  1054. bool FoundResult = false;
  1055. const char *Name = nullptr;
  1056. if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
  1057. FunctionName = Name;
  1058. FoundResult = true;
  1059. }
  1060. std::string DeclFile = DIE.getDeclFile(FileNameKind);
  1061. if (!DeclFile.empty()) {
  1062. StartFile = DeclFile;
  1063. FoundResult = true;
  1064. }
  1065. if (auto DeclLineResult = DIE.getDeclLine()) {
  1066. StartLine = DeclLineResult;
  1067. FoundResult = true;
  1068. }
  1069. if (auto LowPcAddr = toSectionedAddress(DIE.find(DW_AT_low_pc)))
  1070. StartAddress = LowPcAddr->Address;
  1071. return FoundResult;
  1072. }
  1073. static std::optional<int64_t>
  1074. getExpressionFrameOffset(ArrayRef<uint8_t> Expr,
  1075. std::optional<unsigned> FrameBaseReg) {
  1076. if (!Expr.empty() &&
  1077. (Expr[0] == DW_OP_fbreg ||
  1078. (FrameBaseReg && Expr[0] == DW_OP_breg0 + *FrameBaseReg))) {
  1079. unsigned Count;
  1080. int64_t Offset = decodeSLEB128(Expr.data() + 1, &Count, Expr.end());
  1081. // A single DW_OP_fbreg or DW_OP_breg.
  1082. if (Expr.size() == Count + 1)
  1083. return Offset;
  1084. // Same + DW_OP_deref (Fortran arrays look like this).
  1085. if (Expr.size() == Count + 2 && Expr[Count + 1] == DW_OP_deref)
  1086. return Offset;
  1087. // Fallthrough. Do not accept ex. (DW_OP_breg W29, DW_OP_stack_value)
  1088. }
  1089. return std::nullopt;
  1090. }
  1091. void DWARFContext::addLocalsForDie(DWARFCompileUnit *CU, DWARFDie Subprogram,
  1092. DWARFDie Die, std::vector<DILocal> &Result) {
  1093. if (Die.getTag() == DW_TAG_variable ||
  1094. Die.getTag() == DW_TAG_formal_parameter) {
  1095. DILocal Local;
  1096. if (const char *Name = Subprogram.getSubroutineName(DINameKind::ShortName))
  1097. Local.FunctionName = Name;
  1098. std::optional<unsigned> FrameBaseReg;
  1099. if (auto FrameBase = Subprogram.find(DW_AT_frame_base))
  1100. if (std::optional<ArrayRef<uint8_t>> Expr = FrameBase->getAsBlock())
  1101. if (!Expr->empty() && (*Expr)[0] >= DW_OP_reg0 &&
  1102. (*Expr)[0] <= DW_OP_reg31) {
  1103. FrameBaseReg = (*Expr)[0] - DW_OP_reg0;
  1104. }
  1105. if (Expected<std::vector<DWARFLocationExpression>> Loc =
  1106. Die.getLocations(DW_AT_location)) {
  1107. for (const auto &Entry : *Loc) {
  1108. if (std::optional<int64_t> FrameOffset =
  1109. getExpressionFrameOffset(Entry.Expr, FrameBaseReg)) {
  1110. Local.FrameOffset = *FrameOffset;
  1111. break;
  1112. }
  1113. }
  1114. } else {
  1115. // FIXME: missing DW_AT_location is OK here, but other errors should be
  1116. // reported to the user.
  1117. consumeError(Loc.takeError());
  1118. }
  1119. if (auto TagOffsetAttr = Die.find(DW_AT_LLVM_tag_offset))
  1120. Local.TagOffset = TagOffsetAttr->getAsUnsignedConstant();
  1121. if (auto Origin =
  1122. Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
  1123. Die = Origin;
  1124. if (auto NameAttr = Die.find(DW_AT_name))
  1125. if (std::optional<const char *> Name = dwarf::toString(*NameAttr))
  1126. Local.Name = *Name;
  1127. if (auto Type = Die.getAttributeValueAsReferencedDie(DW_AT_type))
  1128. Local.Size = Type.getTypeSize(getCUAddrSize());
  1129. if (auto DeclFileAttr = Die.find(DW_AT_decl_file)) {
  1130. if (const auto *LT = CU->getContext().getLineTableForUnit(CU))
  1131. LT->getFileNameByIndex(
  1132. *DeclFileAttr->getAsUnsignedConstant(), CU->getCompilationDir(),
  1133. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
  1134. Local.DeclFile);
  1135. }
  1136. if (auto DeclLineAttr = Die.find(DW_AT_decl_line))
  1137. Local.DeclLine = *DeclLineAttr->getAsUnsignedConstant();
  1138. Result.push_back(Local);
  1139. return;
  1140. }
  1141. if (Die.getTag() == DW_TAG_inlined_subroutine)
  1142. if (auto Origin =
  1143. Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
  1144. Subprogram = Origin;
  1145. for (auto Child : Die)
  1146. addLocalsForDie(CU, Subprogram, Child, Result);
  1147. }
  1148. std::vector<DILocal>
  1149. DWARFContext::getLocalsForAddress(object::SectionedAddress Address) {
  1150. std::vector<DILocal> Result;
  1151. DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
  1152. if (!CU)
  1153. return Result;
  1154. DWARFDie Subprogram = CU->getSubroutineForAddress(Address.Address);
  1155. if (Subprogram.isValid())
  1156. addLocalsForDie(CU, Subprogram, Subprogram, Result);
  1157. return Result;
  1158. }
  1159. DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
  1160. DILineInfoSpecifier Spec) {
  1161. DILineInfo Result;
  1162. DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
  1163. if (!CU)
  1164. return Result;
  1165. getFunctionNameAndStartLineForAddress(
  1166. CU, Address.Address, Spec.FNKind, Spec.FLIKind, Result.FunctionName,
  1167. Result.StartFileName, Result.StartLine, Result.StartAddress);
  1168. if (Spec.FLIKind != FileLineInfoKind::None) {
  1169. if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) {
  1170. LineTable->getFileLineInfoForAddress(
  1171. {Address.Address, Address.SectionIndex}, CU->getCompilationDir(),
  1172. Spec.FLIKind, Result);
  1173. }
  1174. }
  1175. return Result;
  1176. }
  1177. DILineInfo
  1178. DWARFContext::getLineInfoForDataAddress(object::SectionedAddress Address) {
  1179. DILineInfo Result;
  1180. DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
  1181. if (!CU)
  1182. return Result;
  1183. if (DWARFDie Die = CU->getVariableForAddress(Address.Address)) {
  1184. Result.FileName = Die.getDeclFile(FileLineInfoKind::AbsoluteFilePath);
  1185. Result.Line = Die.getDeclLine();
  1186. }
  1187. return Result;
  1188. }
  1189. DILineInfoTable DWARFContext::getLineInfoForAddressRange(
  1190. object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Spec) {
  1191. DILineInfoTable Lines;
  1192. DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
  1193. if (!CU)
  1194. return Lines;
  1195. uint32_t StartLine = 0;
  1196. std::string StartFileName;
  1197. std::string FunctionName(DILineInfo::BadString);
  1198. std::optional<uint64_t> StartAddress;
  1199. getFunctionNameAndStartLineForAddress(CU, Address.Address, Spec.FNKind,
  1200. Spec.FLIKind, FunctionName,
  1201. StartFileName, StartLine, StartAddress);
  1202. // If the Specifier says we don't need FileLineInfo, just
  1203. // return the top-most function at the starting address.
  1204. if (Spec.FLIKind == FileLineInfoKind::None) {
  1205. DILineInfo Result;
  1206. Result.FunctionName = FunctionName;
  1207. Result.StartFileName = StartFileName;
  1208. Result.StartLine = StartLine;
  1209. Result.StartAddress = StartAddress;
  1210. Lines.push_back(std::make_pair(Address.Address, Result));
  1211. return Lines;
  1212. }
  1213. const DWARFLineTable *LineTable = getLineTableForUnit(CU);
  1214. // Get the index of row we're looking for in the line table.
  1215. std::vector<uint32_t> RowVector;
  1216. if (!LineTable->lookupAddressRange({Address.Address, Address.SectionIndex},
  1217. Size, RowVector)) {
  1218. return Lines;
  1219. }
  1220. for (uint32_t RowIndex : RowVector) {
  1221. // Take file number and line/column from the row.
  1222. const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
  1223. DILineInfo Result;
  1224. LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
  1225. Spec.FLIKind, Result.FileName);
  1226. Result.FunctionName = FunctionName;
  1227. Result.Line = Row.Line;
  1228. Result.Column = Row.Column;
  1229. Result.StartFileName = StartFileName;
  1230. Result.StartLine = StartLine;
  1231. Result.StartAddress = StartAddress;
  1232. Lines.push_back(std::make_pair(Row.Address.Address, Result));
  1233. }
  1234. return Lines;
  1235. }
  1236. DIInliningInfo
  1237. DWARFContext::getInliningInfoForAddress(object::SectionedAddress Address,
  1238. DILineInfoSpecifier Spec) {
  1239. DIInliningInfo InliningInfo;
  1240. DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
  1241. if (!CU)
  1242. return InliningInfo;
  1243. const DWARFLineTable *LineTable = nullptr;
  1244. SmallVector<DWARFDie, 4> InlinedChain;
  1245. CU->getInlinedChainForAddress(Address.Address, InlinedChain);
  1246. if (InlinedChain.size() == 0) {
  1247. // If there is no DIE for address (e.g. it is in unavailable .dwo file),
  1248. // try to at least get file/line info from symbol table.
  1249. if (Spec.FLIKind != FileLineInfoKind::None) {
  1250. DILineInfo Frame;
  1251. LineTable = getLineTableForUnit(CU);
  1252. if (LineTable && LineTable->getFileLineInfoForAddress(
  1253. {Address.Address, Address.SectionIndex},
  1254. CU->getCompilationDir(), Spec.FLIKind, Frame))
  1255. InliningInfo.addFrame(Frame);
  1256. }
  1257. return InliningInfo;
  1258. }
  1259. uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
  1260. for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
  1261. DWARFDie &FunctionDIE = InlinedChain[i];
  1262. DILineInfo Frame;
  1263. // Get function name if necessary.
  1264. if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
  1265. Frame.FunctionName = Name;
  1266. if (auto DeclLineResult = FunctionDIE.getDeclLine())
  1267. Frame.StartLine = DeclLineResult;
  1268. Frame.StartFileName = FunctionDIE.getDeclFile(Spec.FLIKind);
  1269. if (auto LowPcAddr = toSectionedAddress(FunctionDIE.find(DW_AT_low_pc)))
  1270. Frame.StartAddress = LowPcAddr->Address;
  1271. if (Spec.FLIKind != FileLineInfoKind::None) {
  1272. if (i == 0) {
  1273. // For the topmost frame, initialize the line table of this
  1274. // compile unit and fetch file/line info from it.
  1275. LineTable = getLineTableForUnit(CU);
  1276. // For the topmost routine, get file/line info from line table.
  1277. if (LineTable)
  1278. LineTable->getFileLineInfoForAddress(
  1279. {Address.Address, Address.SectionIndex}, CU->getCompilationDir(),
  1280. Spec.FLIKind, Frame);
  1281. } else {
  1282. // Otherwise, use call file, call line and call column from
  1283. // previous DIE in inlined chain.
  1284. if (LineTable)
  1285. LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
  1286. Spec.FLIKind, Frame.FileName);
  1287. Frame.Line = CallLine;
  1288. Frame.Column = CallColumn;
  1289. Frame.Discriminator = CallDiscriminator;
  1290. }
  1291. // Get call file/line/column of a current DIE.
  1292. if (i + 1 < n) {
  1293. FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
  1294. CallDiscriminator);
  1295. }
  1296. }
  1297. InliningInfo.addFrame(Frame);
  1298. }
  1299. return InliningInfo;
  1300. }
  1301. std::shared_ptr<DWARFContext>
  1302. DWARFContext::getDWOContext(StringRef AbsolutePath) {
  1303. if (auto S = DWP.lock()) {
  1304. DWARFContext *Ctxt = S->Context.get();
  1305. return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
  1306. }
  1307. std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
  1308. if (auto S = Entry->lock()) {
  1309. DWARFContext *Ctxt = S->Context.get();
  1310. return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
  1311. }
  1312. Expected<OwningBinary<ObjectFile>> Obj = [&] {
  1313. if (!CheckedForDWP) {
  1314. SmallString<128> DWPName;
  1315. auto Obj = object::ObjectFile::createObjectFile(
  1316. this->DWPName.empty()
  1317. ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
  1318. : StringRef(this->DWPName));
  1319. if (Obj) {
  1320. Entry = &DWP;
  1321. return Obj;
  1322. } else {
  1323. CheckedForDWP = true;
  1324. // TODO: Should this error be handled (maybe in a high verbosity mode)
  1325. // before falling back to .dwo files?
  1326. consumeError(Obj.takeError());
  1327. }
  1328. }
  1329. return object::ObjectFile::createObjectFile(AbsolutePath);
  1330. }();
  1331. if (!Obj) {
  1332. // TODO: Actually report errors helpfully.
  1333. consumeError(Obj.takeError());
  1334. return nullptr;
  1335. }
  1336. auto S = std::make_shared<DWOFile>();
  1337. S->File = std::move(Obj.get());
  1338. S->Context = DWARFContext::create(*S->File.getBinary(),
  1339. ProcessDebugRelocations::Ignore);
  1340. *Entry = S;
  1341. auto *Ctxt = S->Context.get();
  1342. return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
  1343. }
  1344. static Error createError(const Twine &Reason, llvm::Error E) {
  1345. return make_error<StringError>(Reason + toString(std::move(E)),
  1346. inconvertibleErrorCode());
  1347. }
  1348. /// SymInfo contains information about symbol: it's address
  1349. /// and section index which is -1LL for absolute symbols.
  1350. struct SymInfo {
  1351. uint64_t Address;
  1352. uint64_t SectionIndex;
  1353. };
  1354. /// Returns the address of symbol relocation used against and a section index.
  1355. /// Used for futher relocations computation. Symbol's section load address is
  1356. static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
  1357. const RelocationRef &Reloc,
  1358. const LoadedObjectInfo *L,
  1359. std::map<SymbolRef, SymInfo> &Cache) {
  1360. SymInfo Ret = {0, (uint64_t)-1LL};
  1361. object::section_iterator RSec = Obj.section_end();
  1362. object::symbol_iterator Sym = Reloc.getSymbol();
  1363. std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
  1364. // First calculate the address of the symbol or section as it appears
  1365. // in the object file
  1366. if (Sym != Obj.symbol_end()) {
  1367. bool New;
  1368. std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
  1369. if (!New)
  1370. return CacheIt->second;
  1371. Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
  1372. if (!SymAddrOrErr)
  1373. return createError("failed to compute symbol address: ",
  1374. SymAddrOrErr.takeError());
  1375. // Also remember what section this symbol is in for later
  1376. auto SectOrErr = Sym->getSection();
  1377. if (!SectOrErr)
  1378. return createError("failed to get symbol section: ",
  1379. SectOrErr.takeError());
  1380. RSec = *SectOrErr;
  1381. Ret.Address = *SymAddrOrErr;
  1382. } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
  1383. RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
  1384. Ret.Address = RSec->getAddress();
  1385. }
  1386. if (RSec != Obj.section_end())
  1387. Ret.SectionIndex = RSec->getIndex();
  1388. // If we are given load addresses for the sections, we need to adjust:
  1389. // SymAddr = (Address of Symbol Or Section in File) -
  1390. // (Address of Section in File) +
  1391. // (Load Address of Section)
  1392. // RSec is now either the section being targeted or the section
  1393. // containing the symbol being targeted. In either case,
  1394. // we need to perform the same computation.
  1395. if (L && RSec != Obj.section_end())
  1396. if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
  1397. Ret.Address += SectionLoadAddress - RSec->getAddress();
  1398. if (CacheIt != Cache.end())
  1399. CacheIt->second = Ret;
  1400. return Ret;
  1401. }
  1402. static bool isRelocScattered(const object::ObjectFile &Obj,
  1403. const RelocationRef &Reloc) {
  1404. const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
  1405. if (!MachObj)
  1406. return false;
  1407. // MachO also has relocations that point to sections and
  1408. // scattered relocations.
  1409. auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
  1410. return MachObj->isRelocationScattered(RelocInfo);
  1411. }
  1412. namespace {
  1413. struct DWARFSectionMap final : public DWARFSection {
  1414. RelocAddrMap Relocs;
  1415. };
  1416. class DWARFObjInMemory final : public DWARFObject {
  1417. bool IsLittleEndian;
  1418. uint8_t AddressSize;
  1419. StringRef FileName;
  1420. const object::ObjectFile *Obj = nullptr;
  1421. std::vector<SectionName> SectionNames;
  1422. using InfoSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
  1423. std::map<object::SectionRef, unsigned>>;
  1424. InfoSectionMap InfoSections;
  1425. InfoSectionMap TypesSections;
  1426. InfoSectionMap InfoDWOSections;
  1427. InfoSectionMap TypesDWOSections;
  1428. DWARFSectionMap LocSection;
  1429. DWARFSectionMap LoclistsSection;
  1430. DWARFSectionMap LoclistsDWOSection;
  1431. DWARFSectionMap LineSection;
  1432. DWARFSectionMap RangesSection;
  1433. DWARFSectionMap RnglistsSection;
  1434. DWARFSectionMap StrOffsetsSection;
  1435. DWARFSectionMap LineDWOSection;
  1436. DWARFSectionMap FrameSection;
  1437. DWARFSectionMap EHFrameSection;
  1438. DWARFSectionMap LocDWOSection;
  1439. DWARFSectionMap StrOffsetsDWOSection;
  1440. DWARFSectionMap RangesDWOSection;
  1441. DWARFSectionMap RnglistsDWOSection;
  1442. DWARFSectionMap AddrSection;
  1443. DWARFSectionMap AppleNamesSection;
  1444. DWARFSectionMap AppleTypesSection;
  1445. DWARFSectionMap AppleNamespacesSection;
  1446. DWARFSectionMap AppleObjCSection;
  1447. DWARFSectionMap NamesSection;
  1448. DWARFSectionMap PubnamesSection;
  1449. DWARFSectionMap PubtypesSection;
  1450. DWARFSectionMap GnuPubnamesSection;
  1451. DWARFSectionMap GnuPubtypesSection;
  1452. DWARFSectionMap MacroSection;
  1453. DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
  1454. return StringSwitch<DWARFSectionMap *>(Name)
  1455. .Case("debug_loc", &LocSection)
  1456. .Case("debug_loclists", &LoclistsSection)
  1457. .Case("debug_loclists.dwo", &LoclistsDWOSection)
  1458. .Case("debug_line", &LineSection)
  1459. .Case("debug_frame", &FrameSection)
  1460. .Case("eh_frame", &EHFrameSection)
  1461. .Case("debug_str_offsets", &StrOffsetsSection)
  1462. .Case("debug_ranges", &RangesSection)
  1463. .Case("debug_rnglists", &RnglistsSection)
  1464. .Case("debug_loc.dwo", &LocDWOSection)
  1465. .Case("debug_line.dwo", &LineDWOSection)
  1466. .Case("debug_names", &NamesSection)
  1467. .Case("debug_rnglists.dwo", &RnglistsDWOSection)
  1468. .Case("debug_str_offsets.dwo", &StrOffsetsDWOSection)
  1469. .Case("debug_addr", &AddrSection)
  1470. .Case("apple_names", &AppleNamesSection)
  1471. .Case("debug_pubnames", &PubnamesSection)
  1472. .Case("debug_pubtypes", &PubtypesSection)
  1473. .Case("debug_gnu_pubnames", &GnuPubnamesSection)
  1474. .Case("debug_gnu_pubtypes", &GnuPubtypesSection)
  1475. .Case("apple_types", &AppleTypesSection)
  1476. .Case("apple_namespaces", &AppleNamespacesSection)
  1477. .Case("apple_namespac", &AppleNamespacesSection)
  1478. .Case("apple_objc", &AppleObjCSection)
  1479. .Case("debug_macro", &MacroSection)
  1480. .Default(nullptr);
  1481. }
  1482. StringRef AbbrevSection;
  1483. StringRef ArangesSection;
  1484. StringRef StrSection;
  1485. StringRef MacinfoSection;
  1486. StringRef MacinfoDWOSection;
  1487. StringRef MacroDWOSection;
  1488. StringRef AbbrevDWOSection;
  1489. StringRef StrDWOSection;
  1490. StringRef CUIndexSection;
  1491. StringRef GdbIndexSection;
  1492. StringRef TUIndexSection;
  1493. StringRef LineStrSection;
  1494. // A deque holding section data whose iterators are not invalidated when
  1495. // new decompressed sections are inserted at the end.
  1496. std::deque<SmallString<0>> UncompressedSections;
  1497. StringRef *mapSectionToMember(StringRef Name) {
  1498. if (DWARFSection *Sec = mapNameToDWARFSection(Name))
  1499. return &Sec->Data;
  1500. return StringSwitch<StringRef *>(Name)
  1501. .Case("debug_abbrev", &AbbrevSection)
  1502. .Case("debug_aranges", &ArangesSection)
  1503. .Case("debug_str", &StrSection)
  1504. .Case("debug_macinfo", &MacinfoSection)
  1505. .Case("debug_macinfo.dwo", &MacinfoDWOSection)
  1506. .Case("debug_macro.dwo", &MacroDWOSection)
  1507. .Case("debug_abbrev.dwo", &AbbrevDWOSection)
  1508. .Case("debug_str.dwo", &StrDWOSection)
  1509. .Case("debug_cu_index", &CUIndexSection)
  1510. .Case("debug_tu_index", &TUIndexSection)
  1511. .Case("gdb_index", &GdbIndexSection)
  1512. .Case("debug_line_str", &LineStrSection)
  1513. // Any more debug info sections go here.
  1514. .Default(nullptr);
  1515. }
  1516. /// If Sec is compressed section, decompresses and updates its contents
  1517. /// provided by Data. Otherwise leaves it unchanged.
  1518. Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
  1519. StringRef &Data) {
  1520. if (!Sec.isCompressed())
  1521. return Error::success();
  1522. Expected<Decompressor> Decompressor =
  1523. Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
  1524. if (!Decompressor)
  1525. return Decompressor.takeError();
  1526. SmallString<0> Out;
  1527. if (auto Err = Decompressor->resizeAndDecompress(Out))
  1528. return Err;
  1529. UncompressedSections.push_back(std::move(Out));
  1530. Data = UncompressedSections.back();
  1531. return Error::success();
  1532. }
  1533. public:
  1534. DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
  1535. uint8_t AddrSize, bool IsLittleEndian)
  1536. : IsLittleEndian(IsLittleEndian) {
  1537. for (const auto &SecIt : Sections) {
  1538. if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
  1539. *SectionData = SecIt.second->getBuffer();
  1540. else if (SecIt.first() == "debug_info")
  1541. // Find debug_info and debug_types data by section rather than name as
  1542. // there are multiple, comdat grouped, of these sections.
  1543. InfoSections[SectionRef()].Data = SecIt.second->getBuffer();
  1544. else if (SecIt.first() == "debug_info.dwo")
  1545. InfoDWOSections[SectionRef()].Data = SecIt.second->getBuffer();
  1546. else if (SecIt.first() == "debug_types")
  1547. TypesSections[SectionRef()].Data = SecIt.second->getBuffer();
  1548. else if (SecIt.first() == "debug_types.dwo")
  1549. TypesDWOSections[SectionRef()].Data = SecIt.second->getBuffer();
  1550. }
  1551. }
  1552. DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
  1553. function_ref<void(Error)> HandleError,
  1554. function_ref<void(Error)> HandleWarning,
  1555. DWARFContext::ProcessDebugRelocations RelocAction)
  1556. : IsLittleEndian(Obj.isLittleEndian()),
  1557. AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
  1558. Obj(&Obj) {
  1559. StringMap<unsigned> SectionAmountMap;
  1560. for (const SectionRef &Section : Obj.sections()) {
  1561. StringRef Name;
  1562. if (auto NameOrErr = Section.getName())
  1563. Name = *NameOrErr;
  1564. else
  1565. consumeError(NameOrErr.takeError());
  1566. ++SectionAmountMap[Name];
  1567. SectionNames.push_back({ Name, true });
  1568. // Skip BSS and Virtual sections, they aren't interesting.
  1569. if (Section.isBSS() || Section.isVirtual())
  1570. continue;
  1571. // Skip sections stripped by dsymutil.
  1572. if (Section.isStripped())
  1573. continue;
  1574. StringRef Data;
  1575. Expected<section_iterator> SecOrErr = Section.getRelocatedSection();
  1576. if (!SecOrErr) {
  1577. HandleError(createError("failed to get relocated section: ",
  1578. SecOrErr.takeError()));
  1579. continue;
  1580. }
  1581. // Try to obtain an already relocated version of this section.
  1582. // Else use the unrelocated section from the object file. We'll have to
  1583. // apply relocations ourselves later.
  1584. section_iterator RelocatedSection =
  1585. Obj.isRelocatableObject() ? *SecOrErr : Obj.section_end();
  1586. if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) {
  1587. Expected<StringRef> E = Section.getContents();
  1588. if (E)
  1589. Data = *E;
  1590. else
  1591. // maybeDecompress below will error.
  1592. consumeError(E.takeError());
  1593. }
  1594. if (auto Err = maybeDecompress(Section, Name, Data)) {
  1595. HandleError(createError("failed to decompress '" + Name + "', ",
  1596. std::move(Err)));
  1597. continue;
  1598. }
  1599. // Compressed sections names in GNU style starts from ".z",
  1600. // at this point section is decompressed and we drop compression prefix.
  1601. Name = Name.substr(
  1602. Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
  1603. // Map platform specific debug section names to DWARF standard section
  1604. // names.
  1605. Name = Obj.mapDebugSectionName(Name);
  1606. if (StringRef *SectionData = mapSectionToMember(Name)) {
  1607. *SectionData = Data;
  1608. if (Name == "debug_ranges") {
  1609. // FIXME: Use the other dwo range section when we emit it.
  1610. RangesDWOSection.Data = Data;
  1611. } else if (Name == "debug_frame" || Name == "eh_frame") {
  1612. if (DWARFSection *S = mapNameToDWARFSection(Name))
  1613. S->Address = Section.getAddress();
  1614. }
  1615. } else if (InfoSectionMap *Sections =
  1616. StringSwitch<InfoSectionMap *>(Name)
  1617. .Case("debug_info", &InfoSections)
  1618. .Case("debug_info.dwo", &InfoDWOSections)
  1619. .Case("debug_types", &TypesSections)
  1620. .Case("debug_types.dwo", &TypesDWOSections)
  1621. .Default(nullptr)) {
  1622. // Find debug_info and debug_types data by section rather than name as
  1623. // there are multiple, comdat grouped, of these sections.
  1624. DWARFSectionMap &S = (*Sections)[Section];
  1625. S.Data = Data;
  1626. }
  1627. if (RelocatedSection != Obj.section_end() && Name.contains(".dwo"))
  1628. HandleWarning(
  1629. createError("Unexpected relocations for dwo section " + Name));
  1630. if (RelocatedSection == Obj.section_end() ||
  1631. (RelocAction == DWARFContext::ProcessDebugRelocations::Ignore))
  1632. continue;
  1633. StringRef RelSecName;
  1634. if (auto NameOrErr = RelocatedSection->getName())
  1635. RelSecName = *NameOrErr;
  1636. else
  1637. consumeError(NameOrErr.takeError());
  1638. // If the section we're relocating was relocated already by the JIT,
  1639. // then we used the relocated version above, so we do not need to process
  1640. // relocations for it now.
  1641. StringRef RelSecData;
  1642. if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
  1643. continue;
  1644. // In Mach-o files, the relocations do not need to be applied if
  1645. // there is no load offset to apply. The value read at the
  1646. // relocation point already factors in the section address
  1647. // (actually applying the relocations will produce wrong results
  1648. // as the section address will be added twice).
  1649. if (!L && isa<MachOObjectFile>(&Obj))
  1650. continue;
  1651. RelSecName = RelSecName.substr(
  1652. RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
  1653. // TODO: Add support for relocations in other sections as needed.
  1654. // Record relocations for the debug_info and debug_line sections.
  1655. DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
  1656. RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
  1657. if (!Map) {
  1658. // Find debug_info and debug_types relocs by section rather than name
  1659. // as there are multiple, comdat grouped, of these sections.
  1660. if (RelSecName == "debug_info")
  1661. Map = &static_cast<DWARFSectionMap &>(InfoSections[*RelocatedSection])
  1662. .Relocs;
  1663. else if (RelSecName == "debug_types")
  1664. Map =
  1665. &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
  1666. .Relocs;
  1667. else
  1668. continue;
  1669. }
  1670. if (Section.relocation_begin() == Section.relocation_end())
  1671. continue;
  1672. // Symbol to [address, section index] cache mapping.
  1673. std::map<SymbolRef, SymInfo> AddrCache;
  1674. SupportsRelocation Supports;
  1675. RelocationResolver Resolver;
  1676. std::tie(Supports, Resolver) = getRelocationResolver(Obj);
  1677. for (const RelocationRef &Reloc : Section.relocations()) {
  1678. // FIXME: it's not clear how to correctly handle scattered
  1679. // relocations.
  1680. if (isRelocScattered(Obj, Reloc))
  1681. continue;
  1682. Expected<SymInfo> SymInfoOrErr =
  1683. getSymbolInfo(Obj, Reloc, L, AddrCache);
  1684. if (!SymInfoOrErr) {
  1685. HandleError(SymInfoOrErr.takeError());
  1686. continue;
  1687. }
  1688. // Check if Resolver can handle this relocation type early so as not to
  1689. // handle invalid cases in DWARFDataExtractor.
  1690. //
  1691. // TODO Don't store Resolver in every RelocAddrEntry.
  1692. if (Supports && Supports(Reloc.getType())) {
  1693. auto I = Map->try_emplace(
  1694. Reloc.getOffset(),
  1695. RelocAddrEntry{
  1696. SymInfoOrErr->SectionIndex, Reloc, SymInfoOrErr->Address,
  1697. std::optional<object::RelocationRef>(), 0, Resolver});
  1698. // If we didn't successfully insert that's because we already had a
  1699. // relocation for that offset. Store it as a second relocation in the
  1700. // same RelocAddrEntry instead.
  1701. if (!I.second) {
  1702. RelocAddrEntry &entry = I.first->getSecond();
  1703. if (entry.Reloc2) {
  1704. HandleError(createError(
  1705. "At most two relocations per offset are supported"));
  1706. }
  1707. entry.Reloc2 = Reloc;
  1708. entry.SymbolValue2 = SymInfoOrErr->Address;
  1709. }
  1710. } else {
  1711. SmallString<32> Type;
  1712. Reloc.getTypeName(Type);
  1713. // FIXME: Support more relocations & change this to an error
  1714. HandleWarning(
  1715. createError("failed to compute relocation: " + Type + ", ",
  1716. errorCodeToError(object_error::parse_failed)));
  1717. }
  1718. }
  1719. }
  1720. for (SectionName &S : SectionNames)
  1721. if (SectionAmountMap[S.Name] > 1)
  1722. S.IsNameUnique = false;
  1723. }
  1724. std::optional<RelocAddrEntry> find(const DWARFSection &S,
  1725. uint64_t Pos) const override {
  1726. auto &Sec = static_cast<const DWARFSectionMap &>(S);
  1727. RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
  1728. if (AI == Sec.Relocs.end())
  1729. return std::nullopt;
  1730. return AI->second;
  1731. }
  1732. const object::ObjectFile *getFile() const override { return Obj; }
  1733. ArrayRef<SectionName> getSectionNames() const override {
  1734. return SectionNames;
  1735. }
  1736. bool isLittleEndian() const override { return IsLittleEndian; }
  1737. StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
  1738. const DWARFSection &getLineDWOSection() const override {
  1739. return LineDWOSection;
  1740. }
  1741. const DWARFSection &getLocDWOSection() const override {
  1742. return LocDWOSection;
  1743. }
  1744. StringRef getStrDWOSection() const override { return StrDWOSection; }
  1745. const DWARFSection &getStrOffsetsDWOSection() const override {
  1746. return StrOffsetsDWOSection;
  1747. }
  1748. const DWARFSection &getRangesDWOSection() const override {
  1749. return RangesDWOSection;
  1750. }
  1751. const DWARFSection &getRnglistsDWOSection() const override {
  1752. return RnglistsDWOSection;
  1753. }
  1754. const DWARFSection &getLoclistsDWOSection() const override {
  1755. return LoclistsDWOSection;
  1756. }
  1757. const DWARFSection &getAddrSection() const override { return AddrSection; }
  1758. StringRef getCUIndexSection() const override { return CUIndexSection; }
  1759. StringRef getGdbIndexSection() const override { return GdbIndexSection; }
  1760. StringRef getTUIndexSection() const override { return TUIndexSection; }
  1761. // DWARF v5
  1762. const DWARFSection &getStrOffsetsSection() const override {
  1763. return StrOffsetsSection;
  1764. }
  1765. StringRef getLineStrSection() const override { return LineStrSection; }
  1766. // Sections for DWARF5 split dwarf proposal.
  1767. void forEachInfoDWOSections(
  1768. function_ref<void(const DWARFSection &)> F) const override {
  1769. for (auto &P : InfoDWOSections)
  1770. F(P.second);
  1771. }
  1772. void forEachTypesDWOSections(
  1773. function_ref<void(const DWARFSection &)> F) const override {
  1774. for (auto &P : TypesDWOSections)
  1775. F(P.second);
  1776. }
  1777. StringRef getAbbrevSection() const override { return AbbrevSection; }
  1778. const DWARFSection &getLocSection() const override { return LocSection; }
  1779. const DWARFSection &getLoclistsSection() const override { return LoclistsSection; }
  1780. StringRef getArangesSection() const override { return ArangesSection; }
  1781. const DWARFSection &getFrameSection() const override {
  1782. return FrameSection;
  1783. }
  1784. const DWARFSection &getEHFrameSection() const override {
  1785. return EHFrameSection;
  1786. }
  1787. const DWARFSection &getLineSection() const override { return LineSection; }
  1788. StringRef getStrSection() const override { return StrSection; }
  1789. const DWARFSection &getRangesSection() const override { return RangesSection; }
  1790. const DWARFSection &getRnglistsSection() const override {
  1791. return RnglistsSection;
  1792. }
  1793. const DWARFSection &getMacroSection() const override { return MacroSection; }
  1794. StringRef getMacroDWOSection() const override { return MacroDWOSection; }
  1795. StringRef getMacinfoSection() const override { return MacinfoSection; }
  1796. StringRef getMacinfoDWOSection() const override { return MacinfoDWOSection; }
  1797. const DWARFSection &getPubnamesSection() const override { return PubnamesSection; }
  1798. const DWARFSection &getPubtypesSection() const override { return PubtypesSection; }
  1799. const DWARFSection &getGnuPubnamesSection() const override {
  1800. return GnuPubnamesSection;
  1801. }
  1802. const DWARFSection &getGnuPubtypesSection() const override {
  1803. return GnuPubtypesSection;
  1804. }
  1805. const DWARFSection &getAppleNamesSection() const override {
  1806. return AppleNamesSection;
  1807. }
  1808. const DWARFSection &getAppleTypesSection() const override {
  1809. return AppleTypesSection;
  1810. }
  1811. const DWARFSection &getAppleNamespacesSection() const override {
  1812. return AppleNamespacesSection;
  1813. }
  1814. const DWARFSection &getAppleObjCSection() const override {
  1815. return AppleObjCSection;
  1816. }
  1817. const DWARFSection &getNamesSection() const override {
  1818. return NamesSection;
  1819. }
  1820. StringRef getFileName() const override { return FileName; }
  1821. uint8_t getAddressSize() const override { return AddressSize; }
  1822. void forEachInfoSections(
  1823. function_ref<void(const DWARFSection &)> F) const override {
  1824. for (auto &P : InfoSections)
  1825. F(P.second);
  1826. }
  1827. void forEachTypesSections(
  1828. function_ref<void(const DWARFSection &)> F) const override {
  1829. for (auto &P : TypesSections)
  1830. F(P.second);
  1831. }
  1832. };
  1833. } // namespace
  1834. std::unique_ptr<DWARFContext>
  1835. DWARFContext::create(const object::ObjectFile &Obj,
  1836. ProcessDebugRelocations RelocAction,
  1837. const LoadedObjectInfo *L, std::string DWPName,
  1838. std::function<void(Error)> RecoverableErrorHandler,
  1839. std::function<void(Error)> WarningHandler) {
  1840. auto DObj = std::make_unique<DWARFObjInMemory>(
  1841. Obj, L, RecoverableErrorHandler, WarningHandler, RelocAction);
  1842. return std::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName),
  1843. RecoverableErrorHandler,
  1844. WarningHandler);
  1845. }
  1846. std::unique_ptr<DWARFContext>
  1847. DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
  1848. uint8_t AddrSize, bool isLittleEndian,
  1849. std::function<void(Error)> RecoverableErrorHandler,
  1850. std::function<void(Error)> WarningHandler) {
  1851. auto DObj =
  1852. std::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
  1853. return std::make_unique<DWARFContext>(
  1854. std::move(DObj), "", RecoverableErrorHandler, WarningHandler);
  1855. }
  1856. uint8_t DWARFContext::getCUAddrSize() {
  1857. // In theory, different compile units may have different address byte
  1858. // sizes, but for simplicity we just use the address byte size of the
  1859. // first compile unit. In practice the address size field is repeated across
  1860. // various DWARF headers (at least in version 5) to make it easier to dump
  1861. // them independently, not to enable varying the address size.
  1862. auto CUs = compile_units();
  1863. return CUs.empty() ? 0 : (*CUs.begin())->getAddressByteSize();
  1864. }