[PATCH] D25330: [ELF] Remove empty PT_LOAD being created for program headers.

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 6 08:41:10 PDT 2016


evgeny777 created this revision.
evgeny777 added reviewers: ruiu, rafael.
evgeny777 added subscribers: grimar, ikudrin, llvm-commits.
evgeny777 set the repository for this revision to rL LLVM.
evgeny777 added a project: lld.

Sometimes the very first PT_LOAD segment, created by lld, can be empty. This happens when (all conditions met):

- Linker script is used
- First section in ELF image is not RO
- Not enough space for program headers.

In such case lld emits empty PT_LOAD to result ELF image. This patch fixes it


Repository:
  rL LLVM

https://reviews.llvm.org/D25330

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/empty-load.s


Index: test/ELF/linkerscript/empty-load.s
===================================================================
--- test/ELF/linkerscript/empty-load.s
+++ test/ELF/linkerscript/empty-load.s
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo "SECTIONS { .rw : { *(.rw) } .text : { *(.text) }  }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -private-headers %t1 | FileCheck %s
+
+## We expect 2 PT_LOAD segments
+# CHECK:       PHDR
+# CHECK-NEXT:     filesz
+# CHECK-NEXT:  LOAD
+# CHECK-NEXT:     filesz
+# CHECK-NEXT:  LOAD
+# CHECK-NEXT:     filesz
+# CHECK-NEXT:  STACK
+# CHECK-NEXT:     filesz
+
+.globl _start
+_start:
+  jmp _start
+
+.section .rw, "aw"
+ .quad 0
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -655,6 +655,11 @@
     FirstPTLoad->First = Out<ELFT>::ElfHeader;
     if (!FirstPTLoad->Last)
       FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
+  } else if (!FirstPTLoad->First) {
+    // Remove empty PT_LOAD and update size of ProgramHeaders.
+    Phdrs.erase(FirstPTLoad);
+    Out<ELFT>::ProgramHeaders->setSize(sizeof(typename ELFT::Phdr) *
+                                       Phdrs.size());
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25330.73801.patch
Type: text/x-patch
Size: 1331 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161006/1fdca408/attachment.bin>


More information about the llvm-commits mailing list