From cd8eaf891a576464d26f52d53f38ffbdde9ebfe4 Mon Sep 17 00:00:00 2001 From: Eugene Leviant Date: Mon, 10 Oct 2016 15:09:44 +0000 Subject: [PATCH] [ELF] Don't emit empty PT_LOAD segment Sometimes the very first PT_LOAD segment, created by lld, can be empty. This happens when (all conditions met): - Linker script is used - First section in ELF image is not RO - Not enough space for program headers. Differential revision: https://reviews.llvm.org/D25330 llvm-svn: 283760 --- lld/ELF/LinkerScript.cpp | 11 +++++++++++ lld/test/ELF/linkerscript/empty-load.s | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 lld/test/ELF/linkerscript/empty-load.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index cb8a2b6..d7b0dc8 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -655,6 +655,17 @@ void LinkerScript::assignAddresses(std::vector> &Phdrs) { FirstPTLoad->First = Out::ElfHeader; if (!FirstPTLoad->Last) FirstPTLoad->Last = Out::ProgramHeaders; + } else if (!FirstPTLoad->First) { + // Sometimes the very first PT_LOAD segment can be empty. + // This happens if (all conditions met): + // - Linker script is used + // - First section in ELF image is not RO + // - Not enough space for program headers. + // The code below removes empty PT_LOAD segment and updates + // program headers size. + Phdrs.erase(FirstPTLoad); + Out::ProgramHeaders->setSize(sizeof(typename ELFT::Phdr) * + Phdrs.size()); } } diff --git a/lld/test/ELF/linkerscript/empty-load.s b/lld/test/ELF/linkerscript/empty-load.s new file mode 100644 index 0000000..0a87d57 --- /dev/null +++ b/lld/test/ELF/linkerscript/empty-load.s @@ -0,0 +1,22 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo "SECTIONS { .rw : { *(.rw) } .text : { *(.text) } }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -private-headers %t1 | FileCheck %s + +## We expect 2 PT_LOAD segments +# CHECK: PHDR +# CHECK-NEXT: filesz {{0x[0-9a-f]+}} memsz {{0x[0-9a-f]+}} flags r-- +# CHECK-NEXT: LOAD +# CHECK-NEXT: filesz {{0x[0-9a-f]+}} memsz {{0x[0-9a-f]+}} flags rw- +# CHECK-NEXT: LOAD +# CHECK-NEXT: filesz {{0x[0-9a-f]+}} memsz {{0x[0-9a-f]+}} flags r-x +# CHECK-NEXT: STACK +# CHECK-NEXT: filesz + +.globl _start +_start: + jmp _start + +.section .rw, "aw" + .quad 0 -- 2.7.4