Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -406,15 +406,8 @@ } // Add input sections to an output section. - unsigned Pos = 0; - for (InputSectionBase *S : V) { - // The actual offset will be computed during - // assignAddresses. For now, use the index as a very crude - // approximation so that it is at least easy for other code to - // know the section order. - cast(S)->OutSecOff = Pos++; + for (InputSectionBase *S : V) Factory.addInputSec(S, Cmd->Name, Cmd->Sec); - } } } CurOutSec = nullptr; @@ -642,6 +635,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 @@ -139,12 +139,24 @@ this->Info = S->OutSec->SectionIndex; } +static uint64_t updateOffset(uint64_t Off, InputSection *S) { + Off = alignTo(Off, S->Alignment); + S->OutSecOff = Off; + return Off + S->getSize(); +} + void OutputSection::addSection(InputSection *S) { assert(S->Live); Sections.push_back(S); S->OutSec = this; this->updateAlignment(S->Alignment); + // The actual offsets will be computed during assignAddresses. For now, use + // crude approximation so that it is at least easy for other code to know the + // section order. It is also to for calculate the output section size early + // for compressed debug sections. + this->Size = updateOffset(Size, S); + // If this section contains a table of fixed-size entries, sh_entsize // holds the element size. Consequently, if this contains two or more // input sections, all of them must have the same sh_entsize. However, @@ -159,11 +171,8 @@ // and scan relocations to setup sections' offsets. void OutputSection::assignOffsets() { uint64_t Off = 0; - for (InputSection *S : Sections) { - Off = alignTo(Off, S->Alignment); - S->OutSecOff = Off; - Off += S->getSize(); - } + for (InputSection *S : Sections) + Off = updateOffset(Off, S); this->Size = Off; } 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,49 @@ +# REQUIRES: x86, zlib + +# 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 + +# RUN: echo "SECTIONS { .debug_str 0 : { *(.debug_str) } \ +# RUN: .debug_gnu_pubtypes 0 : { *(.debug_gnu_pubtypes) } }" > %t2.script +# RUN: ld.lld %t1.o %t2.o %t2.script -o %t2 --compress-debug-sections=zlib +# RUN: llvm-dwarfdump %t2 | 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: