Index: llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -61,6 +61,7 @@ std::unique_ptr Line; std::unique_ptr DebugFrame; std::unique_ptr EHFrame; + std::unique_ptr Macro; std::unique_ptr Macinfo; std::unique_ptr Names; std::unique_ptr AppleNames; @@ -272,11 +273,14 @@ /// Get a pointer to the parsed eh frame information object. const DWARFDebugFrame *getEHFrame(); - /// Get a pointer to the parsed DebugMacro object. - const DWARFDebugMacro *getDebugMacinfo(); + /// Get a pointer to the parsed Macro information object. + Expected getDebugMacro(); + + /// Get a pointer to the parsed Macinfo inforamtion object. + Expected getDebugMacinfo(); /// Get a pointer to the parsed dwo DebugMacro object. - const DWARFDebugMacro *getDebugMacinfoDWO(); + Expected getDebugMacinfoDWO(); /// Get a reference to the parsed accelerator table object. const DWARFDebugNames &getDebugNames(); Index: llvm/include/llvm/DebugInfo/DWARF/DWARFDebugMacro.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFDebugMacro.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFDebugMacro.h @@ -10,14 +10,58 @@ #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H #include "llvm/ADT/SmallVector.h" -#include "llvm/Support/DataExtractor.h" +#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/WithColor.h" #include namespace llvm { class raw_ostream; - +class DWARFContext; class DWARFDebugMacro { + /// DWARFv5 section 6.3.1 Macro Information Header + enum HeaderFlagMask { +#define HANDLE_MACRO_FLAG(ID, NAME) MACRO_##NAME = ID, +#include "llvm/BinaryFormat/Dwarf.def" + }; + struct MacroHeader { + /// Macro information number. + uint16_t Version = 0; + /// flags + /// The bits of the flags field are interpreted as a set of flags, some of + /// which may indicate that additional fields follow. The following flags, + /// beginning with the least significant bit, are defined: offset_size_flag: + /// If the offset_size_flag is zero, the header is for a 32-bit DWARF + /// format macro section and all offsets are 4 bytes long; if it is one, + /// the header is for a 64-bit DWARF format macro section and all offsets + /// are 8 bytes long. + /// debug_line_offset_flag: + /// If the debug_line_offset_flag is one, the debug_line_offset field (see + /// below) is present. If zero, that field is omitted. + /// opcode_operands_table_flag: + /// If the opcode_operands_table_flag is one, the opcode_operands_table + /// field (see below) is present. If zero, that field is omitted. + + uint8_t Flags; + /// debug_line_offset + /// An offset in the .debug_line section of the beginning of the line + /// number information in the containing compilation, encoded as a 4-byte + /// offset for a 32-bit DWARF format macro section and an 8-byte offset + /// for a 64-bit DWARF format macro section + uint64_t DebugLineOffset; + + uint16_t getVersion() const { return Version; } + uint8_t getFlags() const { return Flags; } + uint64_t getDebugLineOffset() const { return DebugLineOffset; } + + /// Print the macro header from the debug_macro section. + void dumpMacroHeader(raw_ostream &OS) const; + + /// Parse the debug_macro header. + Error parseMacroHeader(DWARFDataExtractor data, uint64_t *Offset); + }; /// A single macro entry within a macro list. struct Entry { /// The type of the macro entry. @@ -40,6 +84,7 @@ }; struct MacroList { + MacroHeader Header; SmallVector Macros; uint64_t Offset; }; @@ -50,11 +95,13 @@ public: DWARFDebugMacro() = default; - /// Print the macro list found within the debug_macinfo section. + /// Print the macro list found within the debug_macinfo/debug_macro section. void dump(raw_ostream &OS) const; - /// Parse the debug_macinfo section accessible via the 'data' parameter. - void parse(DataExtractor data); + /// Parse the debug_macinfo/debug_macro section accessible via the 'data' + /// parameter. + Error parse(DataExtractor StringExtractor, DWARFDataExtractor data, + bool IsMacro); /// Return whether the section has any entries. bool empty() const { return MacroLists.empty(); } Index: llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h @@ -47,6 +47,7 @@ virtual StringRef getStrSection() const { return ""; } virtual const DWARFSection &getRangesSection() const { return Dummy; } virtual const DWARFSection &getRnglistsSection() const { return Dummy; } + virtual const DWARFSection &getMacroSection() const { return Dummy; } virtual StringRef getMacinfoSection() const { return ""; } virtual StringRef getMacinfoDWOSection() const { return ""; } virtual const DWARFSection &getPubnamesSection() const { return Dummy; } Index: llvm/lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -444,14 +444,31 @@ DObj->getEHFrameSection().Data)) getEHFrame()->dump(OS, getRegisterInfo(), *Off); + if (shouldDump(Explicit, ".debug_macro", DIDT_ID_DebugMacro, + DObj->getMacroSection().Data)) { + Expected ExpectedMacro = getDebugMacro(); + if (!ExpectedMacro) + DumpOpts.RecoverableErrorHandler(ExpectedMacro.takeError()); + else + ExpectedMacro.get()->dump(OS); + } + if (shouldDump(Explicit, ".debug_macinfo", DIDT_ID_DebugMacro, DObj->getMacinfoSection())) { - getDebugMacinfo()->dump(OS); + Expected ExpectedMacinfo = getDebugMacinfo(); + if (!ExpectedMacinfo) + DumpOpts.RecoverableErrorHandler(ExpectedMacinfo.takeError()); + else + ExpectedMacinfo.get()->dump(OS); } if (shouldDump(Explicit, ".debug_macinfo.dwo", DIDT_ID_DebugMacro, DObj->getMacinfoDWOSection())) { - getDebugMacinfoDWO()->dump(OS); + Expected ExpectedMacinfoDWO = getDebugMacinfoDWO(); + if (!ExpectedMacinfoDWO) + DumpOpts.RecoverableErrorHandler(ExpectedMacinfoDWO.takeError()); + else + ExpectedMacinfoDWO.get()->dump(OS); } if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges, @@ -815,24 +832,39 @@ return DebugFrame.get(); } -const DWARFDebugMacro *DWARFContext::getDebugMacinfoDWO() { +Expected DWARFContext::getDebugMacinfoDWO() { if (MacinfoDWO) return MacinfoDWO.get(); - - DataExtractor MacinfoDWOData(DObj->getMacinfoDWOSection(), isLittleEndian(), - 0); + DWARFDataExtractor MacinfoDWOData(DObj->getMacinfoDWOSection(), + isLittleEndian(), 0); MacinfoDWO.reset(new DWARFDebugMacro()); - MacinfoDWO->parse(MacinfoDWOData); + if (auto Err = MacinfoDWO->parse(this->getStringExtractor(), MacinfoDWOData, + /*IsMacro=*/false)) + return std::move(Err); return MacinfoDWO.get(); } -const DWARFDebugMacro *DWARFContext::getDebugMacinfo() { +Expected DWARFContext::getDebugMacro() { + if (Macro) + return Macro.get(); + DWARFDataExtractor MacroData(*DObj, DObj->getMacroSection(), isLittleEndian(), + 0); + Macro.reset(new DWARFDebugMacro()); + if (auto Err = Macro->parse(this->getStringExtractor(), MacroData, + /*IsMacro=*/true)) + return std::move(Err); + return Macro.get(); +} + +Expected DWARFContext::getDebugMacinfo() { if (Macinfo) return Macinfo.get(); - - DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0); + DWARFDataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), + 0); Macinfo.reset(new DWARFDebugMacro()); - Macinfo->parse(MacinfoData); + if (auto Err = Macinfo->parse(this->getStringExtractor(), MacinfoData, + /*IsMacro=*/false)) + return std::move(Err); return Macinfo.get(); } @@ -1476,6 +1508,7 @@ DWARFSectionMap PubtypesSection; DWARFSectionMap GnuPubnamesSection; DWARFSectionMap GnuPubtypesSection; + DWARFSectionMap MacroSection; DWARFSectionMap *mapNameToDWARFSection(StringRef Name) { return StringSwitch(Name) @@ -1503,6 +1536,7 @@ .Case("apple_namespaces", &AppleNamespacesSection) .Case("apple_namespac", &AppleNamespacesSection) .Case("apple_objc", &AppleObjCSection) + .Case("debug_macro", &MacroSection) .Default(nullptr); } @@ -1847,6 +1881,7 @@ const DWARFSection &getRnglistsSection() const override { return RnglistsSection; } + const DWARFSection &getMacroSection() const override { return MacroSection; } StringRef getMacinfoSection() const override { return MacinfoSection; } StringRef getMacinfoDWOSection() const override { return MacinfoDWOSection; } const DWARFSection &getPubnamesSection() const override { return PubnamesSection; } Index: llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp =================================================================== --- llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp +++ llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp @@ -8,6 +8,8 @@ #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" #include "llvm/BinaryFormat/Dwarf.h" +#include "llvm/DebugInfo/DWARF/DWARFContext.h" +#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" #include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" #include @@ -15,10 +17,23 @@ using namespace llvm; using namespace dwarf; +void DWARFDebugMacro::MacroHeader::dumpMacroHeader(raw_ostream &OS) const { + // FIXME: Add support for dumping opcode_operands_table + OS << format("macro header: version = 0x%04" PRIx16 ", flags = 0x%02" PRIx8, + getVersion(), getFlags()); + if (getFlags() & MACRO_DEBUG_LINE_OFFSET) + OS << format(", debug_line_offset = 0x%04" PRIx64 "\n", + getDebugLineOffset()); + else + OS << "\n"; +} + void DWARFDebugMacro::dump(raw_ostream &OS) const { unsigned IndLevel = 0; for (const auto &Macros : MacroLists) { OS << format("0x%08" PRIx64 ":\n", Macros.Offset); + if (Macros.Header.getVersion() >= 5) + Macros.Header.dumpMacroHeader(OS); for (const Entry &E : Macros.Macros) { // There should not be DW_MACINFO_end_file when IndLevel is Zero. However, // this check handles the case of corrupted ".debug_macinfo" section. @@ -28,22 +43,34 @@ for (unsigned I = 0; I < IndLevel; I++) OS << " "; IndLevel += (E.Type == DW_MACINFO_start_file); - - WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type); + // Based on which version we are handling choose appropriate macro forms. + if (Macros.Header.getVersion() >= 5) + WithColor(OS, HighlightColor::Macro).get() << MacroString(E.Type); + else + WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type); switch (E.Type) { default: - // Got a corrupted ".debug_macinfo" section (invalid macinfo type). + // Got a corrupted ".debug_macinfo/.debug_macro" section (invalid + // macinfo type). break; - case DW_MACINFO_define: - case DW_MACINFO_undef: + // debug_macro/debug_macinfo shares couple of common encodings. + // DW_MACRO_define == DW_MACINFO_define + // DW_MACRO_undef == DW_MACINFO_undef + // DW_MACRO_start_file == DW_MACINFO_start_file + // DW_MACRO_end_file == DW_MACINFO_end_file + // For readibility/uniformity we are using DW_MACRO_*. + case DW_MACRO_define: + case DW_MACRO_undef: + case DW_MACRO_define_strp: + case DW_MACRO_undef_strp: OS << " - lineno: " << E.Line; OS << " macro: " << E.MacroStr; break; - case DW_MACINFO_start_file: + case DW_MACRO_start_file: OS << " - lineno: " << E.Line; OS << " filenum: " << E.File; break; - case DW_MACINFO_end_file: + case DW_MACRO_end_file: break; case DW_MACINFO_vendor_ext: OS << " - constant: " << E.ExtConstant; @@ -55,7 +82,8 @@ } } -void DWARFDebugMacro::parse(DataExtractor data) { +Error DWARFDebugMacro::parse(DataExtractor StringExtractor, + DWARFDataExtractor data, bool IsMacro) { uint64_t Offset = 0; MacroList *M = nullptr; while (data.isValidOffset(Offset)) { @@ -63,6 +91,11 @@ MacroLists.emplace_back(); M = &MacroLists.back(); M->Offset = Offset; + if (IsMacro) { + auto Err = M->Header.parseMacroHeader(data, &Offset); + if (Err) + return std::move(Err); + } } // A macro list entry consists of: M->Macros.emplace_back(); @@ -71,31 +104,47 @@ E.Type = data.getULEB128(&Offset); if (E.Type == 0) { - // Reached end of a ".debug_macinfo" section contribution. + // Reached end of a ".debug_macinfo/debug_macro" section contribution. M = nullptr; continue; } + uint64_t strOffset = 0; switch (E.Type) { default: // Got a corrupted ".debug_macinfo" section (invalid macinfo type). // Push the corrupted entry to the list and halt parsing. E.Type = DW_MACINFO_invalid; - return; - case DW_MACINFO_define: - case DW_MACINFO_undef: + return Error::success(); + // debug_macro/debug_macinfo shares couple of common encodings. + // DW_MACRO_define == DW_MACINFO_define + // DW_MACRO_undef == DW_MACINFO_undef + // DW_MACRO_start_file == DW_MACINFO_start_file + // DW_MACRO_end_file == DW_MACINFO_end_file + // For readibility/uniformity we are using DW_MACRO_*. + case DW_MACRO_define: + case DW_MACRO_undef: // 2. Source line E.Line = data.getULEB128(&Offset); // 3. Macro string E.MacroStr = data.getCStr(&Offset); break; - case DW_MACINFO_start_file: + case DW_MACRO_define_strp: + case DW_MACRO_undef_strp: + // 2. Source line + E.Line = data.getULEB128(&Offset); + // 3. Macro string + // FIXME:Add support for DWARF64 + strOffset = data.getRelocatedValue(4 /*Offset Size*/, &Offset); + E.MacroStr = StringExtractor.getCStr(&strOffset); + break; + case DW_MACRO_start_file: // 2. Source line E.Line = data.getULEB128(&Offset); // 3. Source file id E.File = data.getULEB128(&Offset); break; - case DW_MACINFO_end_file: + case DW_MACRO_end_file: break; case DW_MACINFO_vendor_ext: // 2. Vendor extension constant @@ -105,4 +154,23 @@ break; } } + return Error::success(); +} + +Error DWARFDebugMacro::MacroHeader::parseMacroHeader(DWARFDataExtractor data, + uint64_t *Offset) { + Version = data.getU16(Offset); + uint8_t Flagdata = data.getU8(Offset); + // FIXME: Add support for DWARF64 + if (Flagdata & MACRO_OFFSET_SIZE) + return createStringError(errc::not_supported, "DWARF64 is not supported"); + + // FIXME: Add support for parsing opcode_operands_table + if (Flagdata & MACRO_OPCODE_OPERANDS_TABLE) + return createStringError(errc::not_supported, + "opcode_operands_table is not supported"); + Flags = Flagdata; + if (Flags & MACRO_DEBUG_LINE_OFFSET) + DebugLineOffset = data.getU32(Offset); + return Error::success(); } Index: llvm/test/DebugInfo/X86/debug-macro-macinfo.s =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/debug-macro-macinfo.s @@ -0,0 +1,63 @@ +# RUN: llvm-mc -triple x86_64-unknown-linux --filetype=obj -dwarf-version=5 %s -o -|\ +# RUN: llvm-dwarfdump -v - | FileCheck %s + +# CHECK: debug_macro contents: +# CHECK-NEXT: 0x00000000: +# CHECK-NEXT: macro header: version = 0x0005, flags = 0x02, debug_line_offset = 0x0000 +# CHECK-NEXT: DW_MACRO_start_file - lineno: 0 filenum: 0 +# CHECK-NEXT: DW_MACRO_define_strp - lineno: 1 macro: DWARF_VERSION 5 +# CHECK-NEXT: DW_MACRO_end_file + +# CHECK: .debug_macinfo contents: +# CHECK-NEXT: 0x00000000: +# CHECK-NEXT: DW_MACINFO_start_file - lineno: 0 filenum: 1 +# CHECK-NEXT: DW_MACINFO_define - lineno: 1 macro: DWARF_VERSION 4 +# CHECK-NEXT: DW_MACINFO_end_file + + .section .debug_macro,"",@progbits +.Lcu_macro_begin0: + .short 5 # Macro information version + .byte 2 # Flags: 32 bit, debug_line_offset present + .long .Lline_table_start0 # lineptr + .byte 3 # DW_MACRO_start_file + .byte 0 # Line Number + .byte 0 # File Number + .byte 5 # DW_MACRO_define_strp + .byte 1 # Line Number + .long .Linfo_string3 # Macro String + .byte 4 # DW_MACRO_end_file + .byte 0 # End Of Macro List Mark + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "clang version 11.0.0" # string offset=0 +.Linfo_string1: + .asciz "a-v5.c" # string offset=105 +.Linfo_string2: + .asciz "/macro" # string offset=112 +.Linfo_string3: + .asciz "DWARF_VERSION 5" # string offset=149 + .section .debug_line,"",@progbits +.Lline_table_start0: + .section .debug_macinfo,"",@progbits +.Lcu_macro_begin1: + .byte 3 + .byte 0 + .file 1 "/macro" "a-v4.c" + .byte 1 + .byte 1 + .byte 1 + .ascii "DWARF_VERSION" + .byte 32 + .byte 52 + .byte 0 + .byte 4 + .byte 0 # End Of Macro List Mark + .section .debug_str,"MS",@progbits,1 +.Linfo_string4: + .asciz "clang version 11.0.0" # string offset=0 +.Linfo_string5: + .asciz "a-v4.c" # string offset=105 +.Linfo_string6: + .asciz "/macro" # string offset=112 + .section .debug_line,"",@progbits +.Lline_table_start1: Index: llvm/test/DebugInfo/X86/debug-macro-v5.s =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/debug-macro-v5.s @@ -0,0 +1,59 @@ +# RUN: llvm-mc -triple x86_64-unknown-linux --filetype=obj -dwarf-version=5 %s -o -|\ +# RUN: llvm-dwarfdump --debug-macro - | FileCheck %s + +# CHECK: .debug_macro +# CHECK-NEXT: 0x00000000: +# CHECK-NEXT: macro header: version = 0x0005, flags = 0x02, debug_line_offset = 0x0000 +# CHECK-NEXT: DW_MACRO_start_file - lineno: 0 filenum: 0 +# CHECK-NEXT: DW_MACRO_start_file - lineno: 1 filenum: 1 +# CHECK-NEXT: DW_MACRO_define_strp - lineno: 1 macro: FOO 5 +# CHECK-NEXT: DW_MACRO_end_file +# CHECK-NEXT: DW_MACRO_undef_strp - lineno: 8 macro: WORLD1 +# CHECK-NEXT: DW_MACRO_end_file + +# CHECK: 0x{{.*}}: +# CHECK-NEXT: macro header: version = 0x0005, flags = 0x00 +# CHECK-NEXT: DW_MACRO_define_strp - lineno: 0 macro: WORLD2 + + .section .debug_macro,"",@progbits +.Lcu_macro_begin0: + .short 5 # Macro information version + .byte 2 # Flags: 32 bit, debug_line_offset present + .long .Lline_table_start0 # lineptr + .byte 3 # DW_MACRO_start_file + .byte 0 # Line Number + .byte 0 # File Number + .byte 3 # DW_MACRO_start_file + .byte 1 # Line Number + .file 1 "." "foo.h" md5 0x0f0cd0e22b44f49d3944992c8dc28661 # File Number + .byte 1 + .byte 5 # DW_MACRO_define_strp + .byte 1 # Line Number + .long .Linfo_string3 # Macro String + .byte 4 # DW_MACRO_end_file + .byte 6 # DW_MACRO_undef_strp + .byte 8 # Line Number + .long .Linfo_string4 # Macro String + .byte 4 # DW_MACRO_end_file + .byte 0 # End Of Macro List Mark + .short 5 # Macro information version + .byte 0 # Flags: 32 bit + .byte 5 # DW_MACRO_define_strp + .byte 0 # Line Number + .long .Linfo_string5 # Macro String + .byte 0 # End Of Macro List Mark + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "clang version 11.0.0" +.Linfo_string1: + .asciz "test.c" +.Linfo_string2: + .asciz "/" +.Linfo_string3: + .asciz "FOO 5" +.Linfo_string4: + .asciz "WORLD1 " +.Linfo_string5: + .asciz "WORLD2 " + .section .debug_line,"",@progbits +.Lline_table_start0: Index: llvm/test/DebugInfo/X86/unsupported-dwarf64-debug-macro-v5.s =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/unsupported-dwarf64-debug-macro-v5.s @@ -0,0 +1,12 @@ +# RUN: llvm-mc -triple x86_64-unknown-linux --filetype=obj -dwarf-version=5 %s -o -|\ +# RUN: not llvm-dwarfdump --debug-macro - /dev/null 2>&1 | FileCheck %s + +# CHECK: error: DWARF64 is not supported + + .section .debug_macro,"",@progbits +.Lcu_macro_begin0: + .short 5 # Macro information version + .byte 3 # Flags: 64 bit, debug_line_offset present + .long .Lline_table_start0 # lineptr + .section .debug_line,"",@progbits +.Lline_table_start0: Index: llvm/test/DebugInfo/X86/unsupported-opcode_operands_table-debug-macro-v5.s =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/unsupported-opcode_operands_table-debug-macro-v5.s @@ -0,0 +1,9 @@ +# RUN: llvm-mc -triple x86_64-unknown-linux --filetype=obj -dwarf-version=5 %s -o -|\ +# RUN: not llvm-dwarfdump --debug-macro - /dev/null 2>&1 | FileCheck %s + +# CHECK: error: opcode_operands_table is not supported + + .section .debug_macro,"",@progbits +.Lcu_macro_begin0: + .short 5 # Macro information version + .byte 4 # Flags: 32 bit, debug_line_offset absent, opcode_operands_table present