diff --git a/llvm/include/llvm/ObjectYAML/DWARFEmitter.h b/llvm/include/llvm/ObjectYAML/DWARFEmitter.h --- a/llvm/include/llvm/ObjectYAML/DWARFEmitter.h +++ b/llvm/include/llvm/ObjectYAML/DWARFEmitter.h @@ -37,6 +37,7 @@ bool IsLittleEndian); Error emitDebugInfo(raw_ostream &OS, const Data &DI); Error emitDebugLine(raw_ostream &OS, const Data &DI); +Error emitDebugAddr(raw_ostream &OS, const Data &DI); Expected>> emitDebugSections(StringRef YAMLString, bool ApplyFixups = false, diff --git a/llvm/include/llvm/ObjectYAML/DWARFYAML.h b/llvm/include/llvm/ObjectYAML/DWARFYAML.h --- a/llvm/include/llvm/ObjectYAML/DWARFYAML.h +++ b/llvm/include/llvm/ObjectYAML/DWARFYAML.h @@ -159,12 +159,27 @@ std::vector Opcodes; }; +struct SegAddrPair { + yaml::Hex64 Segment; + yaml::Hex64 Address; +}; + +struct AddrTableEntry { + dwarf::DwarfFormat Format; + Optional Length; + yaml::Hex16 Version; + yaml::Hex8 AddrSize; + yaml::Hex8 SegSelectorSize; + std::vector SegAddrPairs; +}; + struct Data { bool IsLittleEndian; std::vector AbbrevDecls; std::vector DebugStrings; std::vector ARanges; std::vector DebugRanges; + std::vector DebugAddr; Optional PubNames; Optional PubTypes; @@ -196,6 +211,8 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::File) LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::LineTable) LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::LineTableOpcode) +LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::SegAddrPair) +LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::AddrTableEntry) namespace llvm { namespace yaml { @@ -260,6 +277,14 @@ static void mapping(IO &IO, DWARFYAML::LineTable &LineTable); }; +template <> struct MappingTraits { + static void mapping(IO &IO, DWARFYAML::SegAddrPair &SegAddrPair); +}; + +template <> struct MappingTraits { + static void mapping(IO &IO, DWARFYAML::AddrTableEntry &AddrTable); +}; + template <> struct MappingTraits { static void mapping(IO &IO, DWARFYAML::InitialLength &DWARF); }; diff --git a/llvm/lib/ObjectYAML/DWARFEmitter.cpp b/llvm/lib/ObjectYAML/DWARFEmitter.cpp --- a/llvm/lib/ObjectYAML/DWARFEmitter.cpp +++ b/llvm/lib/ObjectYAML/DWARFEmitter.cpp @@ -27,6 +27,7 @@ #include "llvm/Support/YAMLTraits.h" #include "llvm/Support/raw_ostream.h" #include +#include #include #include #include @@ -70,6 +71,16 @@ writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian); } +static void writeInitialLength(const dwarf::DwarfFormat Format, + const uint64_t Length, raw_ostream &OS, + bool IsLittleEndian) { + if (Format == dwarf::DWARF64) { + writeInteger((uint32_t)UINT32_MAX, OS, IsLittleEndian); + writeInteger((uint64_t)Length, OS, IsLittleEndian); + } else + writeInteger((uint32_t)Length, OS, IsLittleEndian); +} + Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) { for (auto Str : DI.DebugStrings) { OS.write(Str.data(), Str.size()); @@ -337,6 +348,33 @@ return Error::success(); } +Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) { + for (auto AddrTableEntry : DI.DebugAddr) { + uint64_t Length = + AddrTableEntry.Length ? (uint64_t)*AddrTableEntry.Length : + /*2-byte (version) + 1-byte (address_size) + + 1-byte (segment_selector_size) = 4-byte*/ + 4 + (AddrTableEntry.AddrSize + AddrTableEntry.SegSelectorSize) * + AddrTableEntry.SegAddrPairs.size(); + writeInitialLength(AddrTableEntry.Format, Length, OS, DI.IsLittleEndian); + writeInteger((uint16_t)AddrTableEntry.Version, OS, DI.IsLittleEndian); + writeInteger((uint8_t)AddrTableEntry.AddrSize, OS, DI.IsLittleEndian); + writeInteger((uint8_t)AddrTableEntry.SegSelectorSize, OS, + DI.IsLittleEndian); + + for (auto Pair : AddrTableEntry.SegAddrPairs) { + if (AddrTableEntry.SegSelectorSize != 0) + writeVariableSizedInteger(Pair.Segment, AddrTableEntry.SegSelectorSize, + OS, DI.IsLittleEndian); + if (AddrTableEntry.AddrSize != 0) + writeVariableSizedInteger(Pair.Address, AddrTableEntry.AddrSize, OS, + DI.IsLittleEndian); + } + } + + return Error::success(); +} + using EmitFuncType = Error (*)(raw_ostream &, const DWARFYAML::Data &); static Error diff --git a/llvm/lib/ObjectYAML/DWARFYAML.cpp b/llvm/lib/ObjectYAML/DWARFYAML.cpp --- a/llvm/lib/ObjectYAML/DWARFYAML.cpp +++ b/llvm/lib/ObjectYAML/DWARFYAML.cpp @@ -13,6 +13,7 @@ #include "llvm/ObjectYAML/DWARFYAML.h" #include "llvm/BinaryFormat/Dwarf.h" +#include "llvm/Support/YAMLTraits.h" namespace llvm { @@ -32,6 +33,8 @@ SecNames.insert("debug_ranges"); if (!DebugLines.empty()) SecNames.insert("debug_line"); + if (!DebugAddr.empty()) + SecNames.insert("debug_addr"); return SecNames; } @@ -52,6 +55,7 @@ IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes); IO.mapOptional("debug_info", DWARF.CompileUnits); IO.mapOptional("debug_line", DWARF.DebugLines); + IO.mapOptional("debug_addr", DWARF.DebugAddr); IO.setContext(&oldContext); } @@ -191,6 +195,22 @@ IO.mapRequired("Opcodes", LineTable.Opcodes); } +void MappingTraits::mapping( + IO &IO, DWARFYAML::SegAddrPair &SegAddrPair) { + IO.mapOptional("Segment", SegAddrPair.Segment, 0); + IO.mapOptional("Address", SegAddrPair.Address, 0); +} + +void MappingTraits::mapping( + IO &IO, DWARFYAML::AddrTableEntry &AddrTable) { + IO.mapOptional("Format", AddrTable.Format, dwarf::DWARF32); + IO.mapOptional("Length", AddrTable.Length); + IO.mapOptional("Version", AddrTable.Version, 5); + IO.mapRequired("AddressSize", AddrTable.AddrSize); + IO.mapRequired("SegmentSelectorSize", AddrTable.SegSelectorSize); + IO.mapOptional("Entries", AddrTable.SegAddrPairs); +} + void MappingTraits::mapping( IO &IO, DWARFYAML::InitialLength &InitialLength) { IO.mapRequired("TotalLength", InitialLength.TotalLength); diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -861,6 +861,8 @@ Err = DWARFYAML::emitDebugRanges(OS, DWARF); else if (Name == ".debug_line") Err = DWARFYAML::emitDebugLine(OS, DWARF); + else if (Name == ".debug_addr") + Err = DWARFYAML::emitDebugAddr(OS, DWARF); else llvm_unreachable("unexpected emitDWARF() call"); diff --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-addr.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-addr.yaml new file mode 100644 --- /dev/null +++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-addr.yaml @@ -0,0 +1,370 @@ +## Test that yaml2obj emits .debug_addr section. + +## a) Generate the .debug_addr section from the "DWARF" entry. + +## Generate and verify a little endian DWARF32 .debug_addr section. + +# RUN: yaml2obj --docnum=1 -DENDIAN=ELFDATA2LSB -DFORMAT=DWARF32 %s -o %t1.dwarf32.le.o +# RUN: llvm-readobj --sections --section-data %t1.dwarf32.le.o | \ +# RUN: FileCheck %s -DSIZE=272 -DADDRALIGN=1 --check-prefixes=SHDR,DWARF32-LE + +# SHDR: Index: 1 +# SHDR-NEXT: Name: .debug_addr (1) +# SHDR-NEXT: Type: SHT_PROGBITS (0x1) +# SHDR-NEXT: Flags [ (0x0) +# SHDR-NEXT: ] +# SHDR-NEXT: Address: 0x0 +# SHDR-NEXT: Offset: 0x40 +# SHDR-NEXT: Size: [[SIZE]] +# SHDR-NEXT: Link: 0 +# SHDR-NEXT: Info: 0 +# SHDR-NEXT: AddressAlignment: [[ADDRALIGN]] +# SHDR-NEXT: EntrySize: 0 +# DWARF32-LE-NEXT: SectionData ( +# DWARF32-LE-NEXT: 0000: 0C000000 05000400 34120000 78560000 +## | | | | | | +## | | | | | +------- address (4-byte) +## | | | | +------- address (4-byte) +## | | | +- segment_selector_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a1) unit_length (4-byte) +## +# DWARF32-LE-NEXT: 0010: 14000000 05000800 21430000 00000000 +## | | | | | +## | | | | +---------------- address (8-byte) +## | | | +- segment_selector_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a2) unit_length (4-byte) +## +# DWARF32-LE-NEXT: 0020: 65870000 00000000 0C000000 05000004 +## | | | | | +## | | | | +- segment_selector_size (1-byte) +## | | | +- address_size (1-byte) +## | | +--- version (2-byte) +## | +------- a3) unit_length (4-byte) +## +---------------- address (8-byte) +## +# DWARF32-LE-NEXT: 0030: 34120000 78560000 14000000 05000008 +## | | | | | | +## | | | | | +- segment_selector_size (1-byte) +## | | | | +- address_size (1-byte) +## | | | +--- version (2-byte) +## | | +------- a4) unit_length (4-byte) +## | +------- segment (4-byte) +## +------- segment (4-byte) +## +# DWARF32-LE-NEXT: 0040: 21430000 00000000 65870000 00000000 +## | | +## | +---------------- segment (8-byte) +## +---------------- segment (8-byte) +## +# DWARF32-LE-NEXT: 0050: 1C000000 05000404 34120000 78560000 +## | | | | | | +## | | | | | +------- address (4-byte) +## | | | | +------- segment (4-byte) +## | | | +- segment_selector_size (4-byte) +## | | +- address_size (4-byte) +## | +--- version (2-byte) +## +------- a5) unit_length (4-byte) +## +# DWARF32-LE-NEXT: 0060: 21430000 00000000 00000000 21430000 +## | | | | +## | | | +------- address (4-byte) +## | | +------- segment (4-byte) +## | +------- address (4-byte) +## +------- segment (4-byte) +## +# DWARF32-LE-NEXT: 0070: 34000000 05000808 34120000 00000000 +## | | | | | +## | | | | +---------------- segment (8-byte) +## | | | +- segment_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a6) unit_length (4-byte) +## +# DWARF32-LE-NEXT: 0080: 78560000 00000000 21430000 00000000 +## | | +## | +---------------- segment (8-byte) +## +---------------- address (8-byte) +## +# DWARF32-LE-NEXT: 0090: 00000000 00000000 00000000 00000000 +## | +---------------- segment (8-byte) +## +---------------- address (8-byte) +## +# DWARF32-LE-NEXT: 00A0: 21430000 00000000 28000000 05000408 +## | | | | | +## | | | | +- segment_size (1-byte) +## | | | +- address_size (1-byte) +## | | +--- version (2-byte) +## | +------- a7) unit_length (4-byte) +## +---------------- address (8-byte) +## +# DWARF32-LE-NEXT: 00B0: 34120000 00000000 78560000 21430000 +## | | | +## | | +------- segment (8-byte) +## | +------- address (4-byte) +## +---------------- segment (8-byte) +## +# DWARF32-LE-NEXT: 00C0: 00000000 00000000 00000000 00000000 +## | | +---------------- segment (8-byte) +## | +------- address (4-btye) +## +------- higher 4-byte of segment +## +# DWARF32-LE-NEXT: 00D0: 21430000 28000000 05000804 34120000 +## | | | | | +------- segment (4-byte) +## | | | | +- segment_size (1-byte) +## | | | +- address_size (1-byte) +## | | +--- version (2-byte) +## | +------- a8) unit_length (4-byte) +## +------- address (4-byte) +## +# DWARF32-LE-NEXT: 00E0: 78560000 00000000 21430000 00000000 +## | | | +## | | +------- address (8-byte) +## | +------- segment (4-byte) +## +---------------- address (8-byte) +## +# DWARF32-LE-NEXT: 00F0: 00000000 00000000 21430000 00000000 +## | | | +## | | +---------------- address (8-byte) +## | +------- segment (4-byte) +## +------- higher 4-byte of address +## +# DWARF32-LE-NEXT: 0100: 04000000 05000400 34120000 02000400 +## | | | | | | | | +## | | | | | | | +- segment_size (1-byte) +## | | | | | | +- address_size (1-byte) +## | | | | | +--- version (2-byte) +## | | | | +------- a10) unit_length (4-byte) +## | | | +- segment_selector_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a9) unit_length (4-byte) +# DWARF32-LE-NEXT: ) + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: [[ENDIAN]] + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: + debug_addr: + ## a1) Only address (8-byte) exists. + - Format: [[FORMAT]] + AddressSize: 0x04 + SegmentSelectorSize: 0x00 + Entries: + - Address: 0x1234 + - Address: 0x5678 + ## a2) Only address (8-byte) exists. + - Format: [[FORMAT]] + AddressSize: 0x08 + SegmentSelectorSize: 0x00 + Entries: + - Address: 0x4321 + - Address: 0x8765 + ## a3) Only segment (4-byte) exists. + - Format: [[FORMAT]] + AddressSize: 0x00 + SegmentSelectorSize: 0x04 + Entries: + - Segment: 0x1234 + - Segment: 0x5678 + ## a4) Only segment (8-byte) exists. + - Format: [[FORMAT]] + AddressSize: 0x00 + SegmentSelectorSize: 0x08 + Entries: + - Segment: 0x4321 + - Segment: 0x8765 + ## a5) Both address (4-byte) and segment (4-byte) exist. + - Format: [[FORMAT]] + AddressSize: 0x04 + SegmentSelectorSize: 0x04 + Entries: + - Segment: 0x1234 + Address: 0x5678 + - Segment: 0x4321 + ## Segment exists, while address takes the default value 0x00. + - Address: 0x4321 + ## Address exists, while segment takes the default value 0x00. + ## a6) Both address (8-byte) and segment (8-byte) exist. + - Format: [[FORMAT]] + AddressSize: 0x08 + SegmentSelectorSize: 0x08 + Entries: + - Segment: 0x1234 + Address: 0x5678 + - Segment: 0x4321 + ## Segment exists, while address takes the default value 0x00. + - Address: 0x4321 + ## Address exists, while segment takes the default value 0x00. + ## a7) Both address (4-byte) and segment (8-byte) exist. + - Format: [[FORMAT]] + AddressSize: 0x04 + SegmentSelectorSize: 0x08 + Entries: + - Segment: 0x1234 + Address: 0x5678 + - Segment: 0x4321 + ## Segment exists, while address takes the default value 0x00. + - Address: 0x4321 + ## Address exists, while segment takes the default value 0x00. + ## a8) Both address (8-byte) and segment (4-byte) exist. + - Format: [[FORMAT]] + AddressSize: 0x08 + SegmentSelectorSize: 0x04 + Entries: + - Segment: 0x1234 + Address: 0x5678 + - Segment: 0x4321 + ## Segment exists, while address takes the default value 0x00. + - Address: 0x4321 + ## Address exists, while segment takes the default value 0x00. + ## a9) Omit the 'Format' field, DWARF32 by default. + - AddressSize: 0x04 + SegmentSelectorSize: 0x00 + Entries: [] + ## a10) Assign custom values to 'Version' and 'Length'. + - Format: [[FORMAT]] + Length: 0x1234 + Version: 0x02 + AddressSize: 0x04 + SegmentSelectorSize: 0x00 + Entries: [] + +## Generate and verify a big endian DWARF32 .debug_addr section. + +# RUN: yaml2obj --docnum=1 -DENDIAN=ELFDATA2MSB -DFORMAT=DWARF32 %s -o %t1.dwarf32.be.o +# RUN: llvm-readobj --sections --section-data %t1.dwarf32.be.o | \ +# RUN: FileCheck %s -DSIZE=272 -DADDRALIGN=1 --check-prefixes=SHDR,DWARF32-BE + +# DWARF32-BE-NEXT: SectionData ( +# DWARF32-BE-NEXT: 0000: 0000000C 00050400 00001234 00005678 +## | | | | | | +## | | | | | +------- address (4-byte) +## | | | | +------- address (4-byte) +## | | | +- segment_selector_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a1) unit_length (4-byte) +## +# DWARF32-BE-NEXT: 0010: 00000014 00050800 00000000 00004321 +## | | | | | +## | | | | +---------------- address (8-byte) +## | | | +- segment_selector_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a2) unit_length (4-byte) +## +# DWARF32-BE-NEXT: 0020: 00000000 00008765 0000000C 00050004 +## | | | | | +## | | | | +- segment_selector_size (1-byte) +## | | | +- address_size (1-byte) +## | | +--- version (2-byte) +## | +------- a3) unit_length (4-byte) +## +---------------- address (8-byte) +## +# DWARF32-BE-NEXT: 0030: 00001234 00005678 00000014 00050008 +## | | | | | | +## | | | | | +- segment_selector_size (1-byte) +## | | | | +- address_size (1-byte) +## | | | +--- version (2-byte) +## | | +------- a4) unit_length (4-byte) +## | +------- segment (4-byte) +## +------- segment (4-byte) +## +# DWARF32-BE-NEXT: 0040: 00000000 00004321 00000000 00008765 +## | | +## | +---------------- segment (8-byte) +## +---------------- segment (8-byte) +## +# DWARF32-BE-NEXT: 0050: 0000001C 00050404 00001234 00005678 +## | | | | | | +## | | | | | +------- address (4-byte) +## | | | | +------- segment (4-byte) +## | | | +- segment_selector_size (4-byte) +## | | +- address_size (4-byte) +## | +--- version (2-byte) +## +------- a5) unit_length (4-byte) +## +# DWARF32-BE-NEXT: 0060: 00004321 00000000 00000000 00004321 +## | | | | +## | | | +------- address (4-byte) +## | | +------- segment (4-byte) +## | +------- address (4-byte) +## +------- segment (4-byte) +## +# DWARF32-BE-NEXT: 0070: 00000034 00050808 00000000 00001234 +## | | | | | +## | | | | +---------------- segment (8-byte) +## | | | +- segment_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a6) unit_length (4-byte) +## +# DWARF32-BE-NEXT: 0080: 00000000 00005678 00000000 00004321 +## | | +## | +---------------- segment (8-byte) +## +---------------- address (8-byte) +## +# DWARF32-BE-NEXT: 0090: 00000000 00000000 00000000 00000000 +## | | +## | +---------------- segment (8-byte) +## +---------------- address (8-byte) +## +# DWARF32-BE-NEXT: 00A0: 00000000 00004321 00000028 00050408 +## | | | | | +## | | | | +- segment_size (1-byte) +## | | | +- address_size (1-byte) +## | | +--- version (2-byte) +## | +------- a7) unit_length (4-byte) +## +---------------- address (8-byte) +## +# DWARF32-BE-NEXT: 00B0: 00000000 00001234 00005678 00000000 +## | | | +## | | +------- segment (8-byte) +## | +------- address (4-byte) +## +---------------- segment (8-byte) +## +# DWARF32-BE-NEXT: 00C0: 00004321 00000000 00000000 00000000 +## | | | +## | | +---------------- segment (8-byte) +## | +------- address (4-btye) +## +------- lower 4-byte of segment +## +# DWARF32-BE-NEXT: 00D0: 00004321 00000028 00050804 00001234 +## | | | | | | +## | | | | | +------- segment (4-byte) +## | | | | +- segment_size (1-byte) +## | | | +- address_size (1-byte) +## | | +--- version (2-byte) +## | +------- a8) unit_length (4-byte) +## +------- address (4-byte) +## +# DWARF32-BE-NEXT: 00E0: 00000000 00005678 00004321 00000000 +## | | | +## | | +------- address (8-byte) +## | +------- segment (4-byte) +## +---------------- address (8-byte) +## +# DWARF32-BE-NEXT: 00F0: 00000000 00000000 00000000 00004321 +## | | | +## | | +---------------- address (8-byte) +## | +------- segment (4-byte) +## +------- lower 4-byte of address +## +# DWARF32-BE-NEXT: 0100: 00000004 00050400 00001234 00020400 +## | | | | | | | | +## | | | | | | | +- segment_size (1-byte) +## | | | | | | +- address_size (1-byte) +## | | | | | +--- version (2-byte) +## | | | | +------- a10) unit_length (4-byte) +## | | | +- segment_selector_size (1-byte) +## | | +- address_size (1-byte) +## | +--- version (2-byte) +## +------- a9) unit_length (4-byte) +# DWARF32-BE-NEXT: )