From 1d98d6292f38d1ca6692db8039777a241beeab22 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 15 Sep 2016 19:36:01 +0000 Subject: [PATCH] Make sure we create all necessary PT_LOADs. We were some times stopping early when using linker scripts. llvm-svn: 281647 --- lld/ELF/Writer.cpp | 9 ++++++++- lld/test/ELF/linkerscript/non-alloc.s | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lld/test/ELF/linkerscript/non-alloc.s diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 2ebdfae..a97418e 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -969,8 +969,15 @@ std::vector> Writer::createPhdrs() { Phdr RelRo(PT_GNU_RELRO, PF_R); Phdr Note(PT_NOTE, PF_R); for (OutputSectionBase *Sec : OutputSections) { + // Skip non alloc section. + // The reason we skip instead of just breaking out of the loop is the way + // we implement linker scripts. We always put the linker script sections + // first, which means that a non alloc section can be in the middle of the + // file. Continuing in here means it will be included in a PT_LOAD anyway. + // We should probably sort sections based of SHF_ALLOC even if they are + // on linker scripts. if (!(Sec->getFlags() & SHF_ALLOC)) - break; + continue; // If we meet TLS section then we create TLS header // and put all TLS sections inside for futher use when diff --git a/lld/test/ELF/linkerscript/non-alloc.s b/lld/test/ELF/linkerscript/non-alloc.s new file mode 100644 index 0000000..b830c74 --- /dev/null +++ b/lld/test/ELF/linkerscript/non-alloc.s @@ -0,0 +1,28 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t + +# RUN: echo "SECTIONS { .foo : {*(foo)} }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t -shared +# RUN: llvm-readobj -elf-output-style=GNU -s -l %t1 | FileCheck %s + +# Test that we create all necessary PT_LOAD. It is a harmless oddity that +# foo ends in a PT_LOAD. We use to stop at the first non-alloc, causing +# us to not create PT_LOAD for linker generated sections. + +# CHECK: Program Headers: +# CHECK-NEXT: Type +# CHECK-NEXT: PHDR +# CHECK-NEXT: LOAD {{.*}} R +# CHECK-NEXT: LOAD {{.*}} R E +# CHECK-NEXT: LOAD {{.*}} RW + +# CHECK: Section to Segment mapping: +# CHECK-NEXT: Segment Sections... +# CHECK-NEXT: 00 +# CHECK-NEXT: 01 .foo .dynsym .hash .dynstr +# CHECK-NEXT: 02 .text +# CHECK-NEXT: 03 .dynamic + +nop +.section foo +.quad 0 -- 2.7.4