Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1306,6 +1306,18 @@ return false; }); + // In a linker script, we might have alloc and non-alloc output sections + // mixed, but our logic for creation of program headers requires the + // non-alloc sections to come after the alloc sections. + auto isAllocSection = [](const BaseCommand *ACmd, const BaseCommand *BCmd) { + if (auto *AS = dyn_cast(ACmd)) + if (auto *BS = dyn_cast(BCmd)) + return (AS->Flags & SHF_ALLOC) && !(BS->Flags & SHF_ALLOC); + return false; + }; + std::stable_sort(I, NonScriptI, isAllocSection); + + // Sort the orphan sections. std::stable_sort(NonScriptI, E, compareSections); Index: test/ELF/linkerscript/Inputs/nonalloc.s =================================================================== --- /dev/null +++ test/ELF/linkerscript/Inputs/nonalloc.s @@ -0,0 +1,10 @@ +.globl _start +_start: + mov $60, %rax + mov $42, %rdi + +.section .data,"aw" +.quad 10, 10, 20, 20 +.section .other,"" +.short 10 +.byte 20 Index: test/ELF/linkerscript/nonalloc.test =================================================================== --- /dev/null +++ test/ELF/linkerscript/nonalloc.test @@ -0,0 +1,29 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/nonalloc.s -o %t.o +# RUN: ld.lld -o %t --script %s %t.o +# RUN: llvm-objdump -all-headers %t | FileCheck %s + +SECTIONS { + .comment : { *(.comment) } + .data : { *(.data) } + .other : { *(.other) } + .text : { *(.text*) } +} + +# .comment and .other are both non-alloc sections and therefore should come after alloc +# sections. Make sure their relative order is preserved + +# Idx Name Size +# CHECK: 1 .data 00000020 {{[0-9a-f]*}} DATA +# CHECK: 2 .text 0000000e {{[0-9a-f]*}} TEXT +# CHECK: 3 .comment 00000008 {{[0-9a-f]*}} +# CHECK: 4 .other 00000003 {{[0-9a-f]*}} + +# The creation of program headers depends on the strict ordering of alloc/non-alloc sections +# Make sure both .data and .text are covered by a PT_LOAD header + +# Program Header: +# CHECK: LOAD off 0x0000000000001000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**12 +# CHECK: filesz 0x0000000000000020 memsz 0x0000000000000020 flags rw- +# CHECK LOAD off 0x0000000000001020 vaddr 0x0000000000000020 paddr 0x0000000000000020 align 2**12 +# CHECK: filesz 0x000000000000000e memsz 0x000000000000000e flags r-x \ No newline at end of file Index: test/ELF/linkerscript/sections.s =================================================================== --- test/ELF/linkerscript/sections.s +++ test/ELF/linkerscript/sections.s @@ -42,12 +42,12 @@ # Idx Name Size # SEC-ORDER: 1 .bss 00000002 {{[0-9a-f]*}} BSS # SEC-ORDER: 2 other 00000003 {{[0-9a-f]*}} DATA -# SEC-ORDER: 3 .shstrtab 0000003b {{[0-9a-f]*}} -# SEC-ORDER: 4 .symtab 00000030 {{[0-9a-f]*}} -# SEC-ORDER: 5 .strtab 00000008 {{[0-9a-f]*}} -# SEC-ORDER: 6 .comment 00000008 {{[0-9a-f]*}} -# SEC-ORDER: 7 .data 00000020 {{[0-9a-f]*}} DATA -# SEC-ORDER: 8 .text 0000000e {{[0-9a-f]*}} TEXT +# SEC-ORDER: 3 .data 00000020 {{[0-9a-f]*}} DATA +# SEC-ORDER: 4 .text 0000000e {{[0-9a-f]*}} TEXT +# SEC-ORDER: 5 .shstrtab 0000003b {{[0-9a-f]*}} +# SEC-ORDER: 6 .symtab 00000030 {{[0-9a-f]*}} +# SEC-ORDER: 7 .strtab 00000008 {{[0-9a-f]*}} +# SEC-ORDER: 8 .comment 00000008 {{[0-9a-f]*}} # .text and .data have swapped names but proper sizes and types. # RUN: echo "SECTIONS { \