From: Petr Hosek Date: Fri, 4 Aug 2017 23:18:18 +0000 (+0000) Subject: [llvm][llvm-objcopy] When outputting to binary don't output segments that cover no... X-Git-Tag: llvmorg-6.0.0-rc1~10815 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d53951d2ef7e3883a51680958b95d35a360c6562;p=platform%2Fupstream%2Fllvm.git [llvm][llvm-objcopy] When outputting to binary don't output segments that cover no sections Sometimes LLD will produce a PT_LOAD segment that only covers the headers (and covers no sections). GNU objcopy does not output the segment contents for these sections. In particular this is an issue in building magenta because the final link step for the kernel would produce just such a PT_LOAD segment. This change is to support this case and to match what GNU objcopy does in this case. Patch by Jake Ehrlich Differential Revision: https://reviews.llvm.org/D36196 llvm-svn: 310149 --- diff --git a/llvm/test/tools/llvm-objcopy/Inputs/pt-phdr.elf b/llvm/test/tools/llvm-objcopy/Inputs/pt-phdr.elf new file mode 100644 index 000000000000..cede0e7e404b Binary files /dev/null and b/llvm/test/tools/llvm-objcopy/Inputs/pt-phdr.elf differ 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 000000000000..054e84f60663 --- /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 576f660bd989..adb25435a5b4 100644 --- a/llvm/tools/llvm-objcopy/Object.cpp +++ b/llvm/tools/llvm-objcopy/Object.cpp @@ -354,7 +354,10 @@ template size_t BinaryObject::totalSize() const { 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); } } @@ -373,7 +376,8 @@ template void BinaryObject::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;