[lld] r284709 - Don't include PHDRs if linker script doesn't want them

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 20 02:39:09 PDT 2016


Author: evgeny777
Date: Thu Oct 20 04:39:09 2016
New Revision: 284709

URL: http://llvm.org/viewvc/llvm-project?rev=284709&view=rev
Log:
Don't include PHDRs if linker script doesn't want them

This script below shouldn't include file and program headers
to PT_LOAD segment, because it doesn't have PHDRS and FILEHDR
attributes:

PHDRS { all PT_LOAD; }
SECTIONS { /* list of sections here */ }

Differential revision: https://reviews.llvm.org/D25774

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/test/ELF/linkerscript/phdrs.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=284709&r1=284708&r2=284709&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Oct 20 04:39:09 2016
@@ -652,7 +652,16 @@ void LinkerScript<ELFT>::assignAddresses
       std::find_if(Phdrs.begin(), Phdrs.end(), [](const PhdrEntry<ELFT> &E) {
         return E.H.p_type == PT_LOAD;
       });
+
   if (HeaderSize <= MinVA && FirstPTLoad != Phdrs.end()) {
+    // If linker script specifies program headers and first PT_LOAD doesn't 
+    // have both PHDRS and FILEHDR attributes then do nothing
+    if (!Opt.PhdrsCommands.empty()) {
+      size_t SegNum = std::distance(Phdrs.begin(), FirstPTLoad);
+      if (!Opt.PhdrsCommands[SegNum].HasPhdrs ||
+          !Opt.PhdrsCommands[SegNum].HasFilehdr)
+        return;
+    }
     // ELF and Program headers need to be right before the first section in
     // memory. Set their addresses accordingly.
     MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);

Modified: lld/trunk/test/ELF/linkerscript/phdrs.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/phdrs.s?rev=284709&r1=284708&r2=284709&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/phdrs.s (original)
+++ lld/trunk/test/ELF/linkerscript/phdrs.s Thu Oct 20 04:39:09 2016
@@ -9,6 +9,17 @@
 # RUN: ld.lld -o %t1 --script %t.script %t
 # RUN: llvm-readobj -program-headers %t1 | FileCheck %s
 
+## Check that program headers are not written, unless we explicitly tell
+## lld to do this.
+# RUN: echo "PHDRS {all PT_LOAD;} \
+# RUN:       SECTIONS { \
+# RUN:           . = 0x10000200; \
+# RUN:           /DISCARD/ : {*(.text*)}  \
+# RUN:           .foo : {*(.foo.*)} :all \
+# RUN:       }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-readobj -program-headers %t1 | FileCheck --check-prefix=NOPHDR %s
+
 ## Check the AT(expr)
 # RUN: echo "PHDRS {all PT_LOAD FILEHDR PHDRS AT(0x500 + 0x500) ;} \
 # RUN:       SECTIONS { \
@@ -42,6 +53,22 @@
 # CHECK-NEXT:      PF_X (0x1)
 # CHECK-NEXT:    ]
 
+# NOPHDR:     ProgramHeaders [
+# NOPHDR-NEXT:  ProgramHeader {
+# NOPHDR-NEXT:    Type: PT_LOAD (0x1)
+# NOPHDR-NEXT:    Offset: 0x200
+# NOPHDR-NEXT:    VirtualAddress: 0x10000200
+# NOPHDR-NEXT:    PhysicalAddress: 0x10000200
+# NOPHDR-NEXT:    FileSize: 8
+# NOPHDR-NEXT:    MemSize: 8
+# NOPHDR-NEXT:    Flags [ (0x6)
+# NOPHDR-NEXT:      PF_R (0x4)
+# NOPHDR-NEXT:      PF_W (0x2)
+# NOPHDR-NEXT:    ]
+# NOPHDR-NEXT:    Alignment: 4096
+# NOPHDR-NEXT:  }
+# NOPHDR-NEXT: ]
+
 # AT:       ProgramHeaders [
 # AT-NEXT:    ProgramHeader {
 # AT-NEXT:      Type: PT_LOAD (0x1)




More information about the llvm-commits mailing list