DWARFDebugMacro.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDebugMacro.h ----------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  17. #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
  18. #include "llvm/Support/Errc.h"
  19. #include "llvm/Support/Error.h"
  20. #include <cstdint>
  21. namespace llvm {
  22. class raw_ostream;
  23. class DWARFDebugMacro {
  24. /// DWARFv5 section 6.3.1 Macro Information Header.
  25. enum HeaderFlagMask {
  26. #define HANDLE_MACRO_FLAG(ID, NAME) MACRO_##NAME = ID,
  27. #include "llvm/BinaryFormat/Dwarf.def"
  28. };
  29. struct MacroHeader {
  30. /// Macro version information number.
  31. uint16_t Version = 0;
  32. /// The bits of the flags field are interpreted as a set of flags, some of
  33. /// which may indicate that additional fields follow. The following flags,
  34. /// beginning with the least significant bit, are defined:
  35. /// offset_size_flag:
  36. /// If the offset_size_flag is zero, the header is for a 32-bit DWARF
  37. /// format macro section and all offsets are 4 bytes long; if it is one,
  38. /// the header is for a 64-bit DWARF format macro section and all offsets
  39. /// are 8 bytes long.
  40. /// debug_line_offset_flag:
  41. /// If the debug_line_offset_flag is one, the debug_line_offset field (see
  42. /// below) is present. If zero, that field is omitted.
  43. /// opcode_operands_table_flag:
  44. /// If the opcode_operands_table_flag is one, the opcode_operands_table
  45. /// field (see below) is present. If zero, that field is omitted.
  46. uint8_t Flags = 0;
  47. /// debug_line_offset
  48. /// An offset in the .debug_line section of the beginning of the line
  49. /// number information in the containing compilation unit, encoded as a
  50. /// 4-byte offset for a 32-bit DWARF format macro section and an 8-byte
  51. /// offset for a 64-bit DWARF format macro section.
  52. uint64_t DebugLineOffset;
  53. /// Print the macro header from the debug_macro section.
  54. void dumpMacroHeader(raw_ostream &OS) const;
  55. /// Parse the debug_macro header.
  56. Error parseMacroHeader(DWARFDataExtractor Data, uint64_t *Offset);
  57. /// Get the DWARF format according to the flags.
  58. dwarf::DwarfFormat getDwarfFormat() const;
  59. /// Get the size of a reference according to the DWARF format.
  60. uint8_t getOffsetByteSize() const;
  61. };
  62. /// A single macro entry within a macro list.
  63. struct Entry {
  64. /// The type of the macro entry.
  65. uint32_t Type;
  66. union {
  67. /// The source line where the macro is defined.
  68. uint64_t Line;
  69. /// Vendor extension constant value.
  70. uint64_t ExtConstant;
  71. /// Macro unit import offset.
  72. uint64_t ImportOffset;
  73. };
  74. union {
  75. /// The string (name, value) of the macro entry.
  76. const char *MacroStr;
  77. // An unsigned integer indicating the identity of the source file.
  78. uint64_t File;
  79. /// Vendor extension string.
  80. const char *ExtStr;
  81. };
  82. };
  83. struct MacroList {
  84. // A value 0 in the `Header.Version` field indicates that we're parsing
  85. // a macinfo[.dwo] section which doesn't have header itself, hence
  86. // for that case other fields in the `Header` are uninitialized.
  87. MacroHeader Header;
  88. SmallVector<Entry, 4> Macros;
  89. uint64_t Offset;
  90. /// Whether or not this is a .debug_macro section.
  91. bool IsDebugMacro;
  92. };
  93. /// A list of all the macro entries in the debug_macinfo section.
  94. std::vector<MacroList> MacroLists;
  95. public:
  96. DWARFDebugMacro() = default;
  97. /// Print the macro list found within the debug_macinfo/debug_macro section.
  98. void dump(raw_ostream &OS) const;
  99. Error parseMacro(DWARFUnitVector::compile_unit_range Units,
  100. DataExtractor StringExtractor,
  101. DWARFDataExtractor MacroData) {
  102. return parseImpl(Units, StringExtractor, MacroData, /*IsMacro=*/true);
  103. }
  104. Error parseMacinfo(DWARFDataExtractor MacroData) {
  105. return parseImpl(None, None, MacroData, /*IsMacro=*/false);
  106. }
  107. /// Return whether the section has any entries.
  108. bool empty() const { return MacroLists.empty(); }
  109. private:
  110. /// Parse the debug_macinfo/debug_macro section accessible via the 'MacroData'
  111. /// parameter.
  112. Error parseImpl(Optional<DWARFUnitVector::compile_unit_range> Units,
  113. Optional<DataExtractor> StringExtractor,
  114. DWARFDataExtractor Data, bool IsMacro);
  115. };
  116. } // end namespace llvm
  117. #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H
  118. #ifdef __GNUC__
  119. #pragma GCC diagnostic pop
  120. #endif