[PATCH] D43799: Don't allocate a header bellow address 0
Rafael Avila de Espindola via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 26 18:39:11 PST 2018
espindola created this revision.
espindola added reviewers: ruiu, grimar, jhenderson.
Herald added subscribers: arichardson, emaste.
With the current code if the script has a PHDRS we always obey and try to allocate a header. This can cause Min - HeaderSize to underflow.
It looks like bfd actually prints an error for this case. With this patch we at least don't try to allocate the header.
Found while looking at pr36515
https://reviews.llvm.org/D43799
Files:
ELF/LinkerScript.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,14 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "PHDRS { foobar PT_LOAD FILEHDR PHDRS; } \
+# RUN: SECTIONS { .text : { *(.text) } : foobar }" > %t.script
+# RUN: ld.lld --script %t.script %t.o -o %t
+# RUN: llvm-readelf -l %t | FileCheck %s
+
+# CHECK-NOT: PT_LOAD
+# CHECK: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000001 0x000001 R E 0x1000
+# CHECK-NOT: PT_LOAD
+
+ .global _start
+_start:
+ retq
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->hasPhdrsCommands())
+ 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,10 +888,7 @@
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43799.136021.patch
Type: text/x-patch
Size: 1965 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180227/ba4bb1c3/attachment.bin>
More information about the llvm-commits
mailing list