[lld] r281660 - Error out instead of producing a corrupt PT_LOAD.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 15 14:22:12 PDT 2016


Author: rafael
Date: Thu Sep 15 16:22:11 2016
New Revision: 281660

URL: http://llvm.org/viewvc/llvm-project?rev=281660&view=rev
Log:
Error out instead of producing a corrupt PT_LOAD.

What bfd and gold do is give up in putting the headers in the PT_LOAD
and just start the PT_LOAD in the second page.

Added:
    lld/trunk/test/ELF/linkerscript/no-space.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=281660&r1=281659&r2=281660&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Sep 15 16:22:11 2016
@@ -467,11 +467,14 @@ template <class ELFT> void LinkerScript<
     }
   }
 
+  uintX_t HeaderSize =
+      Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
+  if (HeaderSize > MinVA)
+    fatal("Not enough space for ELF and program headers");
+
   // ELF and Program headers need to be right before the first section in
   // memory. Set their addresses accordingly.
-  MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
-                        Out<ELFT>::ProgramHeaders->getSize(),
-                    Target->PageSize);
+  MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
   Out<ELFT>::ElfHeader->setVA(MinVA);
   Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
 }

Added: lld/trunk/test/ELF/linkerscript/no-space.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/no-space.s?rev=281660&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/no-space.s (added)
+++ lld/trunk/test/ELF/linkerscript/no-space.s Thu Sep 15 16:22:11 2016
@@ -0,0 +1,10 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+
+# RUN: echo "SECTIONS {foo 0 : {*(foo*)} }" > %t.script
+# RUN: not ld.lld -o %t --script %t.script %t.o -shared 2>&1 | FileCheck %s
+
+# CHECK: Not enough space for ELF and program headers
+
+.section foo, "a"
+.quad 0




More information about the llvm-commits mailing list