Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -642,6 +642,11 @@ Dot = CurMemRegion->Offset; switchTo(Sec); + // We do not support custom layout for compressed debug sectons. + // At this point we already know their size and have compressed content. + if (CurOutSec->Flags & SHF_COMPRESSED) + return; + for (BaseCommand *C : Cmd->Commands) process(*C); } Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -93,6 +93,11 @@ !Name.startswith(".debug_")) return; + // We call maybeCompress() early and for linkerscript case need to assign offsets, + // because that was not done yet. That finalizes mergeable synthetic sections and + // also finds output section total size for use in compressing code below. + assignOffsets(); + // Create a section header. ZDebugHeader.resize(sizeof(Elf_Chdr)); auto *Hdr = reinterpret_cast(ZDebugHeader.data()); Index: test/ELF/linkerscript/Inputs/compress-debug-sections.s =================================================================== --- test/ELF/linkerscript/Inputs/compress-debug-sections.s +++ test/ELF/linkerscript/Inputs/compress-debug-sections.s @@ -0,0 +1,20 @@ +.section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "CCC" +.Linfo_string1: + .asciz "DDD" + +.section .debug_gnu_pubtypes,"",@progbits +.long .LpubTypes_end0-.LpubTypes_begin0 # Length of Public Types Info +.LpubTypes_begin0: + .short 2 # DWARF Version + .long 0 # Offset of Compilation Unit Info + .long 36 # Compilation Unit Length + .long 36 # DIE offset + .byte 144 # Kind: TYPE, STATIC + .asciz "ccc" # External Name + .long 36 # DIE offset + .byte 144 # Kind: TYPE, STATIC + .asciz "ddd" # External Name + .long 0 # End Mark +.LpubTypes_end0: Index: test/ELF/linkerscript/compress-debug-sections.s =================================================================== --- test/ELF/linkerscript/compress-debug-sections.s +++ test/ELF/linkerscript/compress-debug-sections.s @@ -0,0 +1,44 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \ +# RUN: %S/Inputs/compress-debug-sections.s -o %t1.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t2.o +# RUN: echo "SECTIONS { }" > %t.script +# RUN: ld.lld %t1.o %t2.o %t.script -o %t --compress-debug-sections=zlib +# RUN: llvm-dwarfdump %t | FileCheck %s + +# CHECK: .debug_str contents: +# CHECK-NEXT: CCC +# CHECK-NEXT: DDD +# CHECK-NEXT: AAA +# CHECK-NEXT: BBB +# CHECK: .debug_gnu_pubtypes contents: +# CHECK-NEXT: length = 0x00000020 version = 0x0002 unit_offset = 0x00000000 unit_size = 0x00000024 +# CHECK-NEXT: Offset Linkage Kind Name +# CHECK-NEXT: 0x00000024 STATIC TYPE "ccc" +# CHECK-NEXT: 0x00000024 STATIC TYPE "ddd" +# CHECK-NEXT: length = 0x00000020 version = 0x0002 unit_offset = 0x00000000 unit_size = 0x00000024 +# CHECK-NEXT: Offset Linkage Kind Name +# CHECK-NEXT: 0x00000024 STATIC TYPE "aaa" +# CHECK-NEXT: 0x00000024 STATIC TYPE "bbb" + +.section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "AAA" +.Linfo_string1: + .asciz "BBB" + +.section .debug_gnu_pubtypes,"",@progbits +.long .LpubTypes_end0-.LpubTypes_begin0 # Length of Public Types Info +.LpubTypes_begin0: + .short 2 # DWARF Version + .long 0 # Offset of Compilation Unit Info + .long 36 # Compilation Unit Length + .long 36 # DIE offset + .byte 144 # Kind: TYPE, STATIC + .asciz "aaa" # External Name + .long 36 # DIE offset + .byte 144 # Kind: TYPE, STATIC + .asciz "bbb" # External Name + .long 0 # End Mark +.LpubTypes_end0: