diff options
-rw-r--r-- | llvm/test/tools/llvm-objcopy/Inputs/pt-phdr.elf | bin | 0 -> 8808 bytes | |||
-rw-r--r-- | llvm/test/tools/llvm-objcopy/sectionless-segment.test | 4 | ||||
-rw-r--r-- | llvm/tools/llvm-objcopy/Object.cpp | 8 |
3 files changed, 10 insertions, 2 deletions
diff --git a/llvm/test/tools/llvm-objcopy/Inputs/pt-phdr.elf b/llvm/test/tools/llvm-objcopy/Inputs/pt-phdr.elf Binary files differnew file mode 100644 index 00000000000..cede0e7e404 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/Inputs/pt-phdr.elf diff --git a/llvm/test/tools/llvm-objcopy/sectionless-segment.test b/llvm/test/tools/llvm-objcopy/sectionless-segment.test new file mode 100644 index 00000000000..054e84f6066 --- /dev/null +++ b/llvm/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 diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp index 576f660bd98..adb25435a5b 100644 --- a/llvm/tools/llvm-objcopy/Object.cpp +++ b/llvm/tools/llvm-objcopy/Object.cpp @@ -354,7 +354,10 @@ template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const { template <class ELFT> void BinaryObject<ELFT>::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); } } @@ -373,7 +376,8 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() { 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; |