DWARFDebugMacro.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //===- DWARFDebugMacro.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/DWARFDebugMacro.h"
  9. #include "llvm/ADT/DenseMap.h"
  10. #include "llvm/BinaryFormat/Dwarf.h"
  11. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  12. #include "llvm/DebugInfo/DWARF/DWARFDie.h"
  13. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  14. #include "llvm/Support/Errc.h"
  15. #include "llvm/Support/WithColor.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <cstdint>
  18. using namespace llvm;
  19. using namespace dwarf;
  20. DwarfFormat DWARFDebugMacro::MacroHeader::getDwarfFormat() const {
  21. return Flags & MACRO_OFFSET_SIZE ? DWARF64 : DWARF32;
  22. }
  23. uint8_t DWARFDebugMacro::MacroHeader::getOffsetByteSize() const {
  24. return getDwarfOffsetByteSize(getDwarfFormat());
  25. }
  26. void DWARFDebugMacro::MacroHeader::dumpMacroHeader(raw_ostream &OS) const {
  27. // FIXME: Add support for dumping opcode_operands_table
  28. OS << format("macro header: version = 0x%04" PRIx16, Version)
  29. << format(", flags = 0x%02" PRIx8, Flags)
  30. << ", format = " << FormatString(getDwarfFormat());
  31. if (Flags & MACRO_DEBUG_LINE_OFFSET)
  32. OS << format(", debug_line_offset = 0x%0*" PRIx64, 2 * getOffsetByteSize(),
  33. DebugLineOffset);
  34. OS << "\n";
  35. }
  36. void DWARFDebugMacro::dump(raw_ostream &OS) const {
  37. unsigned IndLevel = 0;
  38. for (const auto &Macros : MacroLists) {
  39. OS << format("0x%08" PRIx64 ":\n", Macros.Offset);
  40. if (Macros.IsDebugMacro)
  41. Macros.Header.dumpMacroHeader(OS);
  42. for (const Entry &E : Macros.Macros) {
  43. // There should not be DW_MACINFO_end_file when IndLevel is Zero. However,
  44. // this check handles the case of corrupted ".debug_macinfo" section.
  45. if (IndLevel > 0)
  46. IndLevel -= (E.Type == DW_MACINFO_end_file);
  47. // Print indentation.
  48. for (unsigned I = 0; I < IndLevel; I++)
  49. OS << " ";
  50. IndLevel += (E.Type == DW_MACINFO_start_file);
  51. // Based on which version we are handling choose appropriate macro forms.
  52. if (Macros.IsDebugMacro)
  53. WithColor(OS, HighlightColor::Macro).get()
  54. << (Macros.Header.Version < 5 ? GnuMacroString(E.Type)
  55. : MacroString(E.Type));
  56. else
  57. WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type);
  58. switch (E.Type) {
  59. default:
  60. // Got a corrupted ".debug_macinfo/.debug_macro" section (invalid
  61. // macinfo type).
  62. break;
  63. // debug_macro and debug_macinfo share some common encodings.
  64. // DW_MACRO_define == DW_MACINFO_define
  65. // DW_MACRO_undef == DW_MACINFO_undef
  66. // DW_MACRO_start_file == DW_MACINFO_start_file
  67. // DW_MACRO_end_file == DW_MACINFO_end_file
  68. // For readability/uniformity we are using DW_MACRO_*.
  69. //
  70. // The GNU .debug_macro extension's entries have the same encoding
  71. // as DWARF 5's DW_MACRO_* entries, so we only use the latter here.
  72. case DW_MACRO_define:
  73. case DW_MACRO_undef:
  74. case DW_MACRO_define_strp:
  75. case DW_MACRO_undef_strp:
  76. case DW_MACRO_define_strx:
  77. case DW_MACRO_undef_strx:
  78. OS << " - lineno: " << E.Line;
  79. OS << " macro: " << E.MacroStr;
  80. break;
  81. case DW_MACRO_start_file:
  82. OS << " - lineno: " << E.Line;
  83. OS << " filenum: " << E.File;
  84. break;
  85. case DW_MACRO_import:
  86. OS << format(" - import offset: 0x%0*" PRIx64,
  87. 2 * Macros.Header.getOffsetByteSize(), E.ImportOffset);
  88. break;
  89. case DW_MACRO_end_file:
  90. break;
  91. case DW_MACINFO_vendor_ext:
  92. OS << " - constant: " << E.ExtConstant;
  93. OS << " string: " << E.ExtStr;
  94. break;
  95. }
  96. OS << "\n";
  97. }
  98. }
  99. }
  100. Error DWARFDebugMacro::parseImpl(
  101. std::optional<DWARFUnitVector::compile_unit_range> Units,
  102. std::optional<DataExtractor> StringExtractor, DWARFDataExtractor Data,
  103. bool IsMacro) {
  104. uint64_t Offset = 0;
  105. MacroList *M = nullptr;
  106. using MacroToUnitsMap = DenseMap<uint64_t, DWARFUnit *>;
  107. MacroToUnitsMap MacroToUnits;
  108. if (IsMacro && Data.isValidOffset(Offset)) {
  109. // Keep a mapping from Macro contribution to CUs, this will
  110. // be needed while retrieving macro from DW_MACRO_define_strx form.
  111. for (const auto &U : *Units)
  112. if (auto CUDIE = U->getUnitDIE())
  113. // Skip units which does not contibutes to macro section.
  114. if (auto MacroOffset = toSectionOffset(CUDIE.find(DW_AT_macros)))
  115. MacroToUnits.try_emplace(*MacroOffset, U.get());
  116. }
  117. while (Data.isValidOffset(Offset)) {
  118. if (!M) {
  119. MacroLists.emplace_back();
  120. M = &MacroLists.back();
  121. M->Offset = Offset;
  122. M->IsDebugMacro = IsMacro;
  123. if (IsMacro) {
  124. auto Err = M->Header.parseMacroHeader(Data, &Offset);
  125. if (Err)
  126. return Err;
  127. }
  128. }
  129. // A macro list entry consists of:
  130. M->Macros.emplace_back();
  131. Entry &E = M->Macros.back();
  132. // 1. Macinfo type
  133. E.Type = Data.getULEB128(&Offset);
  134. if (E.Type == 0) {
  135. // Reached end of a ".debug_macinfo/debug_macro" section contribution.
  136. M = nullptr;
  137. continue;
  138. }
  139. switch (E.Type) {
  140. default:
  141. // Got a corrupted ".debug_macinfo" section (invalid macinfo type).
  142. // Push the corrupted entry to the list and halt parsing.
  143. E.Type = DW_MACINFO_invalid;
  144. return Error::success();
  145. // debug_macro and debug_macinfo share some common encodings.
  146. // DW_MACRO_define == DW_MACINFO_define
  147. // DW_MACRO_undef == DW_MACINFO_undef
  148. // DW_MACRO_start_file == DW_MACINFO_start_file
  149. // DW_MACRO_end_file == DW_MACINFO_end_file
  150. // For readibility/uniformity we are using DW_MACRO_*.
  151. case DW_MACRO_define:
  152. case DW_MACRO_undef:
  153. // 2. Source line
  154. E.Line = Data.getULEB128(&Offset);
  155. // 3. Macro string
  156. E.MacroStr = Data.getCStr(&Offset);
  157. break;
  158. case DW_MACRO_define_strp:
  159. case DW_MACRO_undef_strp: {
  160. if (!IsMacro) {
  161. // DW_MACRO_define_strp is a new form introduced in DWARFv5, it is
  162. // not supported in debug_macinfo[.dwo] sections. Assume it as an
  163. // invalid entry, push it and halt parsing.
  164. E.Type = DW_MACINFO_invalid;
  165. return Error::success();
  166. }
  167. uint64_t StrOffset = 0;
  168. // 2. Source line
  169. E.Line = Data.getULEB128(&Offset);
  170. // 3. Macro string
  171. StrOffset =
  172. Data.getRelocatedValue(M->Header.getOffsetByteSize(), &Offset);
  173. assert(StringExtractor && "String Extractor not found");
  174. E.MacroStr = StringExtractor->getCStr(&StrOffset);
  175. break;
  176. }
  177. case DW_MACRO_define_strx:
  178. case DW_MACRO_undef_strx: {
  179. if (!IsMacro) {
  180. // DW_MACRO_define_strx is a new form introduced in DWARFv5, it is
  181. // not supported in debug_macinfo[.dwo] sections. Assume it as an
  182. // invalid entry, push it and halt parsing.
  183. E.Type = DW_MACINFO_invalid;
  184. return Error::success();
  185. }
  186. E.Line = Data.getULEB128(&Offset);
  187. auto MacroContributionOffset = MacroToUnits.find(M->Offset);
  188. if (MacroContributionOffset == MacroToUnits.end())
  189. return createStringError(errc::invalid_argument,
  190. "Macro contribution of the unit not found");
  191. Expected<uint64_t> StrOffset =
  192. MacroContributionOffset->second->getStringOffsetSectionItem(
  193. Data.getULEB128(&Offset));
  194. if (!StrOffset)
  195. return StrOffset.takeError();
  196. E.MacroStr =
  197. MacroContributionOffset->second->getStringExtractor().getCStr(
  198. &*StrOffset);
  199. break;
  200. }
  201. case DW_MACRO_start_file:
  202. // 2. Source line
  203. E.Line = Data.getULEB128(&Offset);
  204. // 3. Source file id
  205. E.File = Data.getULEB128(&Offset);
  206. break;
  207. case DW_MACRO_end_file:
  208. break;
  209. case DW_MACRO_import:
  210. E.ImportOffset =
  211. Data.getRelocatedValue(M->Header.getOffsetByteSize(), &Offset);
  212. break;
  213. case DW_MACINFO_vendor_ext:
  214. // 2. Vendor extension constant
  215. E.ExtConstant = Data.getULEB128(&Offset);
  216. // 3. Vendor extension string
  217. E.ExtStr = Data.getCStr(&Offset);
  218. break;
  219. }
  220. }
  221. return Error::success();
  222. }
  223. Error DWARFDebugMacro::MacroHeader::parseMacroHeader(DWARFDataExtractor Data,
  224. uint64_t *Offset) {
  225. Version = Data.getU16(Offset);
  226. uint8_t FlagData = Data.getU8(Offset);
  227. // FIXME: Add support for parsing opcode_operands_table
  228. if (FlagData & MACRO_OPCODE_OPERANDS_TABLE)
  229. return createStringError(errc::not_supported,
  230. "opcode_operands_table is not supported");
  231. Flags = FlagData;
  232. if (Flags & MACRO_DEBUG_LINE_OFFSET)
  233. DebugLineOffset = Data.getUnsigned(Offset, getOffsetByteSize());
  234. return Error::success();
  235. }