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 @@ -211,7 +211,7 @@ std::vector AbbrevDecls; std::vector DebugStrings; Optional> DebugStrOffsets; - std::vector ARanges; + Optional> DebugAranges; std::vector DebugRanges; std::vector DebugAddr; Optional PubNames; 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 @@ -126,7 +126,8 @@ } Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) { - for (auto Range : DI.ARanges) { + assert(DI.DebugAranges && "unexpected emitDebugAranges() call"); + for (auto Range : *DI.DebugAranges) { uint8_t AddrSize; if (Range.AddrSize) AddrSize = *Range.AddrSize; 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 @@ -17,7 +17,7 @@ namespace llvm { bool DWARFYAML::Data::isEmpty() const { - return DebugStrings.empty() && AbbrevDecls.empty() && ARanges.empty() && + return DebugStrings.empty() && AbbrevDecls.empty() && DebugAranges && DebugRanges.empty() && !PubNames && !PubTypes && !GNUPubNames && !GNUPubTypes && CompileUnits.empty() && DebugLines.empty(); } @@ -26,7 +26,7 @@ SetVector SecNames; if (!DebugStrings.empty()) SecNames.insert("debug_str"); - if (!ARanges.empty()) + if (DebugAranges) SecNames.insert("debug_aranges"); if (!DebugRanges.empty()) SecNames.insert("debug_ranges"); @@ -61,8 +61,7 @@ IO.setContext(&DWARFCtx); IO.mapOptional("debug_str", DWARF.DebugStrings); IO.mapOptional("debug_abbrev", DWARF.AbbrevDecls); - if (!DWARF.ARanges.empty() || !IO.outputting()) - IO.mapOptional("debug_aranges", DWARF.ARanges); + IO.mapOptional("debug_aranges", DWARF.DebugAranges); if (!DWARF.DebugRanges.empty() || !IO.outputting()) IO.mapOptional("debug_ranges", DWARF.DebugRanges); IO.mapOptional("debug_pubnames", DWARF.PubNames); diff --git a/llvm/lib/ObjectYAML/MachOEmitter.cpp b/llvm/lib/ObjectYAML/MachOEmitter.cpp --- a/llvm/lib/ObjectYAML/MachOEmitter.cpp +++ b/llvm/lib/ObjectYAML/MachOEmitter.cpp @@ -293,9 +293,10 @@ Err = DWARFYAML::emitDebugStr(OS, Obj.DWARF); else if (0 == strncmp(&Sec.sectname[0], "__debug_abbrev", 16)) Err = DWARFYAML::emitDebugAbbrev(OS, Obj.DWARF); - else if (0 == strncmp(&Sec.sectname[0], "__debug_aranges", 16)) - Err = DWARFYAML::emitDebugAranges(OS, Obj.DWARF); - else if (0 == strncmp(&Sec.sectname[0], "__debug_ranges", 16)) + else if (0 == strncmp(&Sec.sectname[0], "__debug_aranges", 16)) { + if (Obj.DWARF.DebugAranges) + Err = DWARFYAML::emitDebugAranges(OS, Obj.DWARF); + } else if (0 == strncmp(&Sec.sectname[0], "__debug_ranges", 16)) Err = DWARFYAML::emitDebugRanges(OS, Obj.DWARF); else if (0 == strncmp(&Sec.sectname[0], "__debug_pubnames", 16)) { if (Obj.DWARF.PubNames) diff --git a/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml b/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml --- a/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml +++ b/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml @@ -1,4 +1,7 @@ -# RUN: yaml2obj %s | obj2yaml | FileCheck %s +## a) Test that yaml2macho is able to emit the .debug_aranges section and obj2yaml +## is able to convert it back. + +# RUN: yaml2obj --docnum=1 %s | obj2yaml | FileCheck %s --- !mach-o FileHeader: @@ -331,3 +334,65 @@ # CHECK-NEXT: Descriptors: # CHECK-NEXT: - Address: 0x0000000100000F50 # CHECK-NEXT: Length: 52 + +## b) Test that if the "debug_aranges" entry is empty, yaml2macho will only emit the +## section header. + +# RUN: yaml2obj --docnum=2 %s -o %t2.o +# RUN: llvm-readobj --sections --section-data %t2.o | FileCheck %s --check-prefix=EMPTY-SECTION + +# EMPTY-SECTION: Index: 0 +# EMPTY-SECTION-NEXT: Name: __debug_aranges (5F 5F 64 65 62 75 67 5F 61 72 61 6E 67 65 73 00) +# EMPTY-SECTION-NEXT: Segment: __DWARF (5F 5F 44 57 41 52 46 00 00 00 00 00 00 00 00 00) +# EMPTY-SECTION-NEXT: Address: 0x0 +# EMPTY-SECTION-NEXT: Size: 0xC +# EMPTY-SECTION-NEXT: Offset: 528 +# EMPTY-SECTION-NEXT: Alignment: 0 +# EMPTY-SECTION-NEXT: RelocationOffset: 0x0 +# EMPTY-SECTION-NEXT: RelocationCount: 0 +# EMPTY-SECTION-NEXT: Type: Regular (0x0) +# EMPTY-SECTION-NEXT: Attributes [ (0x0) +# EMPTY-SECTION-NEXT: ] +# EMPTY-SECTION-NEXT: Reserved1: 0x0 +# EMPTY-SECTION-NEXT: Reserved2: 0x0 +# EMPTY-SECTION-NEXT: Reserved3: 0x0 +# EMPTY-SECTION-NEXT: SectionData ( +# EMPTY-SECTION-NEXT: ) + +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x01000007 + cpusubtype: 0x00000003 + filetype: 0x0000000A + ncmds: 1 + sizeofcmds: 232 + flags: 0x00000000 + reserved: 0x00000000 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 152 + segname: __DWARF + vmaddr: 0x00 + vmsize: 0x00 + fileoff: 0x00 + filesize: 0x00 + maxprot: 0 + initprot: 0 + nsects: 1 + flags: 0 + Sections: + - sectname: __debug_aranges + segname: __DWARF + addr: 0x00 + size: 12 + offset: 528 + align: 0 + reloff: 0x00000000 + nreloc: 0 + flags: 0x00000000 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 +DWARF: + debug_aranges: [] diff --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml --- a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml +++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml @@ -595,3 +595,22 @@ Descriptors: - Address: 0x1234 Length: 0x4321 + +## l) Test that the .debug_aranges section header is emitted if the "debug_aranges" +## entry is empty. + +# RUN: yaml2obj --docnum=12 %s -o %t12.o +# RUN: llvm-readobj --sections --section-data %t12.o | \ +# RUN: FileCheck -DSIZE=0 -DADDRALIGN=1 %s --check-prefixes=DWARF-HEADER,EMPTY-CONTENT + +# EMPTY-CONTENT-NEXT: SectionData ( +# EMPTY-CONTENT-NEXT: ) + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: + debug_aranges: [] diff --git a/llvm/tools/obj2yaml/dwarf2yaml.cpp b/llvm/tools/obj2yaml/dwarf2yaml.cpp --- a/llvm/tools/obj2yaml/dwarf2yaml.cpp +++ b/llvm/tools/obj2yaml/dwarf2yaml.cpp @@ -64,7 +64,7 @@ DCtx.isLittleEndian(), 0); uint64_t Offset = 0; DWARFDebugArangeSet Set; - + std::vector DebugAranges; while (ArangesData.isValidOffset(Offset)) { if (Error E = Set.extract(ArangesData, &Offset)) return E; @@ -81,8 +81,10 @@ Desc.Length = Descriptor.Length; Range.Descriptors.push_back(Desc); } - Y.ARanges.push_back(Range); + DebugAranges.push_back(Range); } + + Y.DebugAranges = DebugAranges; return ErrorSuccess(); }