diff --git a/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test b/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test --- a/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test +++ b/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test @@ -183,3 +183,34 @@ - Name: .data Type: SHT_PROGBITS Flags: [ SHF_ALLOC, SHF_WRITE ] + +## NOBITS sections should not appear in output. +# RUN: yaml2obj --docnum=6 %s -o %t6 +# RUN: llvm-objcopy -O binary %t6 %t6.out +# RUN: od -A x -t x2 %t6.out | FileCheck %s --check-prefix=SKIPNOBITS + +# SKIPNOBITS: 000000 c3c3 c3c3 +# SKIPNOBITS-NEXT: 000004 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .bss + Type: SHT_NOBITS + Flags: [ SHF_ALLOC ] + Address: 0x1000 + AddressAlign: 0x1000 + Size: 0x123 + - Name: gap + Type: Fill + Size: 0xffd + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x10000000 + AddressAlign: 0x1000 + Content: "c3c3c3c3" diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp --- a/llvm/tools/llvm-objcopy/ELF/Object.cpp +++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp @@ -2553,7 +2553,7 @@ if (Sec.ParentSegment != nullptr) Sec.Addr = Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr; - if (Sec.Size > 0) + if (Sec.Type != SHT_NOBITS && Sec.Size > 0) MinAddr = std::min(MinAddr, Sec.Addr); }