From 4ec013ac8312604ebb72159e47814ff185adcbed Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 15 Sep 2016 21:22:11 +0000 Subject: [PATCH] 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. llvm-svn: 281660 --- lld/ELF/LinkerScript.cpp | 9 ++++++--- lld/test/ELF/linkerscript/no-space.s | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 lld/test/ELF/linkerscript/no-space.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 7ae61e9..f58f4d7 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -467,11 +467,14 @@ template void LinkerScript::assignAddresses() { } } + uintX_t HeaderSize = + Out::ElfHeader->getSize() + Out::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::ElfHeader->getSize() - - Out::ProgramHeaders->getSize(), - Target->PageSize); + MinVA = alignDown(MinVA - HeaderSize, Target->PageSize); Out::ElfHeader->setVA(MinVA); Out::ProgramHeaders->setVA(Out::ElfHeader->getSize() + MinVA); } diff --git a/lld/test/ELF/linkerscript/no-space.s b/lld/test/ELF/linkerscript/no-space.s new file mode 100644 index 0000000..f24a72a --- /dev/null +++ b/lld/test/ELF/linkerscript/no-space.s @@ -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 -- 2.7.4