Index: test/tools/llvm-objcopy/sectionless-segment.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/sectionless-segment.test @@ -0,0 +1,4 @@ +# RUN: llvm-objcopy -O binary %p/Inputs/pt-phdr.elf %t +# RUN: wc -c < %t | FileCheck %s + +# CHECK: 4110 Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -353,7 +353,10 @@ template void BinaryObject::write(FileOutputBuffer &Out) const { for (auto &Segment : this->Segments) { - if (Segment->Type == llvm::ELF::PT_LOAD) { + // GNU objcopy does not output segments that do not cover a section. Such + // segments can sometimes be produced by LLD due to how LLD handles PT_PHDR. + if (Segment->Type == llvm::ELF::PT_LOAD && + Segment->firstSection() != nullptr) { Segment->writeSegment(Out); } } @@ -372,7 +375,8 @@ uint64_t Offset = 0; for (auto &Segment : this->Segments) { - if (Segment->Type == llvm::ELF::PT_LOAD) { + if (Segment->Type == llvm::ELF::PT_LOAD && + Segment->firstSection() != nullptr) { Offset = alignTo(Offset, Segment->Align); Segment->Offset = Offset; Offset += Segment->FileSize;