diff --git a/lld/ELF/DWARF.cpp b/lld/ELF/DWARF.cpp --- a/lld/ELF/DWARF.cpp +++ b/lld/ELF/DWARF.cpp @@ -25,17 +25,22 @@ using namespace lld; using namespace lld::elf; -template LLDDwarfObj::LLDDwarfObj(ObjFile *obj) { - for (InputSectionBase *sec : obj->getSections()) { +template +LLDDwarfObj::LLDDwarfObj(ObjFile *obj) { + using Elf_Shdr = typename ELFT::Shdr; + ArrayRef objSections = CHECK(obj->getObj().sections(), obj); + for (auto it : llvm::enumerate(obj->getSections())) { + InputSectionBase *sec = it.value(); if (!sec) continue; + // In DWARF v5, a .debug_info in a COMDAT group may represent a type unit. + // Don't bother calling data() (which may uncompress the content) on it. if (LLDDWARFSection *m = StringSwitch(sec->name) .Case(".debug_addr", &addrSection) .Case(".debug_gnu_pubnames", &gnuPubnamesSection) .Case(".debug_gnu_pubtypes", &gnuPubtypesSection) - .Case(".debug_info", &infoSection) .Case(".debug_loclists", &loclistsSection) .Case(".debug_ranges", &rangesSection) .Case(".debug_rnglists", &rnglistsSection) @@ -53,6 +58,15 @@ strSection = toStringRef(sec->data()); else if (sec->name == ".debug_line_str") lineStrSection = toStringRef(sec->data()); + else if (sec->name == ".debug_info" && + !(objSections[it.index()].sh_flags & ELF::SHF_GROUP)) { + // In DWARF v5, -fdebug-types-section places type units in .debug_info + // sections in COMDAT groups. They are not compile units and thus should + // be ignored. If we place compile units in COMDAT groups in the future, + // we will need to perform a lightweight parsing. + infoSection.Data = toStringRef(sec->data()); + infoSection.sec = sec; + } } } diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -2700,9 +2700,12 @@ static std::vector getDebugInfoSections() { std::vector ret; + // See the comment in LLDDwarfObj::LLDDwarfObj. NOTE: isec->data() may + // uncompress the full content, which should be parallelized. + // FIXME This does not actually work because we drop SHF_FLAG very early. for (InputSectionBase *s : inputSections) if (InputSection *isec = dyn_cast(s)) - if (isec->name == ".debug_info") + if (isec->name == ".debug_info" && !(isec->flags & SHF_GROUP)) ret.push_back(isec); return ret; } diff --git a/lld/test/ELF/gdb-index-dwarf5-type-unit.s b/lld/test/ELF/gdb-index-dwarf5-type-unit.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/gdb-index-dwarf5-type-unit.s @@ -0,0 +1,77 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o +# RUN: ld.lld --gdb-index -Ttext=0x1000 %t.o -o %t +# RUN: llvm-dwarfdump --gdb-index %t | FileCheck %s + +# CHECK: CU list offset = 0x18, has 1 entries: +# CHECK-NEXT: 0: Offset = 0x0, Length = 0x19 + +# CHECK: Address area offset = 0x28, has 1 entries: +# CHECK-NEXT: Low/High address = [0x1000, 0x1001) (Size: 0x1), CU id = 0 + +.globl main +main: +.Lfunc_begin0: + retq +.Lfunc_end0: + +.section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 65 # DW_TAG_type_unit + .byte 0 # DW_CHILDREN_no + .byte 0 # EOM(1) + .byte 0 # EOM(2) + + .byte 2 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 0 # DW_CHILDREN_no + .byte 17 # DW_AT_low_pc + .byte 1 # DW_FORM_addr + .byte 18 # DW_AT_high_pc + .byte 6 # DW_FORM_data4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + + .byte 0 # EOM(3) + +.macro TYPE_UNIT id signature +.section .debug_info,"G",@progbits,\signature + .long .Ldebug_info_end\id-.Ldebug_info_start\id # Length of Unit +.Ldebug_info_start\id: + .short 5 # DWARF version number + .byte 2 # DWARF Unit Type + .byte 8 # Address Size + .long .debug_abbrev # Offset Into Abbrev. Section + .quad \signature # Type Signature + .long .Ldebug_info_end\id # Type DIE Offset + .byte 1 # Abbrev [1] DW_TAG_type_unit +.Ldebug_info_end\id: +.endm + +TYPE_UNIT 0, 123 + +.section .debug_info,"",@progbits +.Lcu_begin0: + .long .Lcu_end0-.Lcu_begin0-4 # Length of Unit + .short 5 # DWARF version number + .byte 1 # DWARF Unit Type + .byte 8 # Address Size + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 2 # Abbrev [2] DW_TAG_compile_unit + .quad .Lfunc_begin0 # DW_AT_low_pc + .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc +.Lcu_end0: + +#.section .debug_info,"G",@progbits,1234 +# .long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit +#.Ldebug_info_start0: +# .short 5 # DWARF version number +# .byte 2 # DWARF Unit Type +# .byte 8 # Address Size +# .long .debug_abbrev # Offset Into Abbrev. Section +# .quad 1234 # Type Signature +# .long .Ldebug_info_end0 # Type DIE Offset +# .byte 1 # Abbrev [1] DW_TAG_type_unit +#.Ldebug_info_end0: + +TYPE_UNIT 1, 456