[PATCH] D43799: Error instead of allocating a header bellow address 0

Rafael Avila de Espindola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 08:51:11 PST 2018


espindola updated this revision to Diff 136086.
espindola retitled this revision from "Don't allocate a header bellow address 0" to "Error instead of allocating  a header bellow address 0".
espindola edited the summary of this revision.
espindola added a comment.

This now matches bfd and produces an error.


https://reviews.llvm.org/D43799

Files:
  ELF/LinkerScript.cpp
  ELF/LinkerScript.h
  ELF/ScriptParser.cpp
  test/ELF/linkerscript/header-phdr2.s


Index: test/ELF/linkerscript/header-phdr2.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/header-phdr2.s
@@ -0,0 +1,11 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "PHDRS { foobar PT_LOAD FILEHDR PHDRS; }"  > %t.script
+# RUN: echo "SECTIONS { .text : { *(.text) } : foobar }" >> %t.script
+# RUN: not ld.lld --script %t.script %t.o -o %t 2>&1 | FileCheck %s
+
+# CHECK: Could not allocate headers
+
+        .global _start
+_start:
+        retq
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -410,6 +410,8 @@
       else
         setError("unexpected header attribute: " + next());
     }
+    if (Cmd.HasPhdrs || Cmd.HasFilehdr)
+      Script->AllocateHeaders = true;
 
     Script->PhdrsCommands.push_back(Cmd);
   }
Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -274,6 +274,7 @@
 
   bool HasSectionsCommand = false;
   bool ErrorOnMissingSection = false;
+  bool AllocateHeaders = false;
 
   // List of section patterns specified with KEEP commands. They will
   // be kept even if they are unused and --gc-sections is specified.
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -855,6 +855,15 @@
   return nullptr;
 }
 
+static uint64_t computeBase(uint64_t Min) {
+  // If there is no SECTIONS or if the linkerscript is explicit about program
+  // headers, do our best to allocate them.
+  if (!Script->HasSectionsCommand || Script->AllocateHeaders)
+    return 0;
+  // Otherwise only allocate program headers if that would not add a page.
+  return alignDown(Min, Config->MaxPageSize);
+}
+
 // Try to find an address for the file and program headers output sections,
 // which were unconditionally added to the first PT_LOAD segment earlier.
 //
@@ -879,14 +888,14 @@
   PhdrEntry *FirstPTLoad = *It;
 
   uint64_t HeaderSize = getHeaderSize();
-  // When linker script with SECTIONS is being used, don't output headers
-  // unless there's a space for them.
-  uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0;
-  if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
+  if (HeaderSize <= Min - computeBase(Min)) {
     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
     Out::ElfHeader->Addr = Min;
     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
     return;
+  } else if (Script->AllocateHeaders) {
+    // Error if we were explicitly asked to allocate headers.
+    error("Could not allocate headers");
   }
 
   Out::ElfHeader->PtLoad = nullptr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43799.136086.patch
Type: text/x-patch
Size: 2837 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180227/51b59b89/attachment.bin>


More information about the llvm-commits mailing list