Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -501,6 +501,9 @@ for (BaseCommand *Cmd : readOverlay()) V.push_back(Cmd); continue; + } else if (Tok == "INCLUDE") { + readInclude(); + continue; } if (BaseCommand *Cmd = readAssignment(Tok)) @@ -803,6 +806,8 @@ Cmd->Filler = readFill(); } else if (Tok == "SORT") { readSort(); + } else if (Tok == "INCLUDE") { + readInclude(); } else if (peek() == "(") { Cmd->SectionCommands.push_back(readInputSectionDescription(Tok)); } else { @@ -1429,7 +1434,11 @@ void ScriptParser::readMemory() { expect("{"); while (!errorCount() && !consume("}")) { - StringRef Name = next(); + StringRef Tok = next(); + if (Tok == "INCLUDE") { + readInclude(); + continue; + } uint32_t Flags = 0; uint32_t NegFlags = 0; @@ -1445,9 +1454,9 @@ // Add the memory region to the region map. MemoryRegion *MR = - make(Name, Origin, Length, Flags, NegFlags); - if (!Script->MemoryRegions.insert({Name, MR}).second) - setError("region '" + Name + "' already defined"); + make(Tok, Origin, Length, Flags, NegFlags); + if (!Script->MemoryRegions.insert({Tok, MR}).second) + setError("region '" + Tok + "' already defined"); } } Index: test/ELF/linkerscript/memory-include.s =================================================================== --- /dev/null +++ test/ELF/linkerscript/memory-include.s @@ -0,0 +1,33 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo "MEMORY { \ +# RUN: ROM (rwx): ORIGIN = 0x1000, LENGTH = 0x100 \ +# RUN: RAM (rwx): ORIGIN = 0x2000, LENGTH = 0x100 \ +# RUN: INCLUDE \"%t.script.inc\" \ +# RUN: } \ +# RUN: SECTIONS { \ +# RUN: .text : { *(.text*) } > ROM \ +# RUN: .data : { *(.data*) } > RAM \ +# RUN: }" > %t.script + +## Empty include file. +# RUN: echo "" > %t.script.inc +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=CHECK1 +# CHECK1: .data 00000008 0000000000002000 DATA + +## Non-empty include file. +# RUN: echo "SECTIONS { .data2 : { QUAD(0) } > RAM2 }" >> %t.script +# RUN: echo "RAM2 (rwx): ORIGIN = 0x3000, LENGTH = 0x100" > %t.script.inc +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=CHECK2 +# CHECK2: .data 00000008 0000000000002000 DATA +# CHECK2: .data2 00000008 0000000000003000 DATA + +.text +.global _start +_start: + nop + +.section .data,"aw" +.quad 0 Index: test/ELF/linkerscript/output-section-include.s =================================================================== --- /dev/null +++ test/ELF/linkerscript/output-section-include.s @@ -0,0 +1,33 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo "MEMORY { \ +# RUN: ROM (rwx): ORIGIN = 0x1000, LENGTH = 0x100 \ +# RUN: RAM (rwx): ORIGIN = 0x2000, LENGTH = 0x100 \ +# RUN: } \ +# RUN: SECTIONS { \ +# RUN: .text : { *(.text*) } > ROM \ +# RUN: .data : { \ +# RUN: *(.data*) \ +# RUN: INCLUDE \"%t.script.inc\" \ +# RUN: } > RAM \ +# RUN: }" > %t.script + +## Empty include file. +# RUN: echo "" > %t.script.inc +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=CHECK1 +# CHECK1: .data 00000008 0000000000002000 DATA + +## Non-empty include file. +# RUN: echo "QUAD(0)" > %t.script.inc +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=CHECK2 +# CHECK2: .data 00000010 0000000000002000 DATA + +.text +.global _start +_start: + nop + +.section .data,"aw" +.quad 0 Index: test/ELF/linkerscript/section-include.s =================================================================== --- /dev/null +++ test/ELF/linkerscript/section-include.s @@ -0,0 +1,35 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo "MEMORY { \ +# RUN: ROM (rwx): ORIGIN = 0x1000, LENGTH = 0x100 \ +# RUN: RAM (rwx): ORIGIN = 0x2000, LENGTH = 0x100 \ +# RUN: } \ +# RUN: SECTIONS { \ +# RUN: .text : { *(.text*) } > ROM \ +# RUN: .data : { *(.data*) } > RAM \ +# RUN: INCLUDE \"%t.script.inc\" \ +# RUN: .data3 : { QUAD(0) } > RAM \ +# RUN: }" > %t.script + +## Empty include file. +# RUN: echo "" > %t.script.inc +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=CHECK1 +# CHECK1: .data 00000008 0000000000002000 DATA +# CHECK1-NEXT: .data3 00000008 0000000000002008 DATA + +## Non-empty include file. +# RUN: echo ".data2 : { QUAD(0) } > RAM" > %t.script.inc +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=CHECK2 +# CHECK2: .data 00000008 0000000000002000 DATA +# CHECK2-NEXT: .data2 00000008 0000000000002008 DATA +# CHECK2-NEXT: .data3 00000008 0000000000002010 DATA + +.text +.global _start +_start: + nop + +.section .data,"aw" +.quad 0