DWARFDebugMacro.cpp 8.8 KB

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