[llvm] r310149 - [llvm][llvm-objcopy] When outputting to binary don't output segments that cover no sections

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 4 16:18:18 PDT 2017


Author: phosek
Date: Fri Aug  4 16:18:18 2017
New Revision: 310149

URL: http://llvm.org/viewvc/llvm-project?rev=310149&view=rev
Log:
[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

Added:
    llvm/trunk/test/tools/llvm-objcopy/Inputs/
    llvm/trunk/test/tools/llvm-objcopy/Inputs/pt-phdr.elf
    llvm/trunk/test/tools/llvm-objcopy/sectionless-segment.test
Modified:
    llvm/trunk/tools/llvm-objcopy/Object.cpp

Added: llvm/trunk/test/tools/llvm-objcopy/Inputs/pt-phdr.elf
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/Inputs/pt-phdr.elf?rev=310149&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-objcopy/Inputs/pt-phdr.elf (added) and llvm/trunk/test/tools/llvm-objcopy/Inputs/pt-phdr.elf Fri Aug  4 16:18:18 2017 differ

Added: llvm/trunk/test/tools/llvm-objcopy/sectionless-segment.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/sectionless-segment.test?rev=310149&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/sectionless-segment.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/sectionless-segment.test Fri Aug  4 16:18:18 2017
@@ -0,0 +1,4 @@
+# RUN: llvm-objcopy -O binary %p/Inputs/pt-phdr.elf %t
+# RUN: wc -c < %t | FileCheck %s
+
+# CHECK: 4110

Modified: llvm/trunk/tools/llvm-objcopy/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.cpp?rev=310149&r1=310148&r2=310149&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.cpp Fri Aug  4 16:18:18 2017
@@ -354,7 +354,10 @@ template <class ELFT> size_t BinaryObjec
 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<
 
   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;




More information about the llvm-commits mailing list