Index: lld/trunk/ELF/LinkerScript.h =================================================================== --- lld/trunk/ELF/LinkerScript.h +++ lld/trunk/ELF/LinkerScript.h @@ -289,9 +289,10 @@ // A list of symbols referenced by the script. std::vector ReferencedSymbols; - // Used to implement INSERT AFTER. Contains commands that need + // Used to implement INSERT [AFTER|BEFORE]. Contains commands that need // to be inserted into SECTIONS commands list. llvm::DenseMap> InsertAfterCommands; + llvm::DenseMap> InsertBeforeCommands; }; extern LinkerScript *Script; Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -207,18 +207,26 @@ // the list of script commands to mix sections inserted into. void LinkerScript::processInsertCommands() { std::vector V; + auto Insert = [&](std::vector &From) { + V.insert(V.end(), From.begin(), From.end()); + From.clear(); + }; + for (BaseCommand *Base : SectionCommands) { - V.push_back(Base); - if (auto *Cmd = dyn_cast(Base)) { - std::vector &W = InsertAfterCommands[Cmd->Name]; - V.insert(V.end(), W.begin(), W.end()); - W.clear(); + if (auto *OS = dyn_cast(Base)) { + Insert(InsertBeforeCommands[OS->Name]); + V.push_back(Base); + Insert(InsertAfterCommands[OS->Name]); + continue; } + V.push_back(Base); } - for (std::pair> &P : - InsertAfterCommands) - if (!P.second.empty()) - error("unable to INSERT AFTER " + P.first + ": section not defined"); + + for (auto &Cmds : {InsertBeforeCommands, InsertAfterCommands}) + for (const std::pair> &P : Cmds) + if (!P.second.empty()) + error("unable to INSERT AFTER/BEFORE " + P.first + + ": section not defined"); SectionCommands = std::move(V); } Index: lld/trunk/ELF/ScriptParser.cpp =================================================================== --- lld/trunk/ELF/ScriptParser.cpp +++ lld/trunk/ELF/ScriptParser.cpp @@ -450,9 +450,15 @@ } if (!atEOF() && consume("INSERT")) { - expect("AFTER"); - std::vector &Dest = Script->InsertAfterCommands[next()]; - Dest.insert(Dest.end(), V.begin(), V.end()); + std::vector *Dest = nullptr; + if (consume("AFTER")) + Dest = &Script->InsertAfterCommands[next()]; + else if (consume("BEFORE")) + Dest = &Script->InsertBeforeCommands[next()]; + else + setError("expected AFTER/BEFORE, but got '" + next() + "'"); + if (Dest) + Dest->insert(Dest->end(), V.begin(), V.end()); return; } Index: lld/trunk/test/ELF/linkerscript/insert-after.test =================================================================== --- lld/trunk/test/ELF/linkerscript/insert-after.test +++ lld/trunk/test/ELF/linkerscript/insert-after.test @@ -25,5 +25,5 @@ # RUN: not ld.lld %t1.o -o %t1 --script %s 2>&1 \ # RUN: | FileCheck %s --check-prefix=ERR -# ERR-DAG: error: unable to INSERT AFTER .text: section not defined -# ERR-DAG: error: unable to INSERT AFTER .data: section not defined +# ERR-DAG: error: unable to INSERT AFTER/BEFORE .text: section not defined +# ERR-DAG: error: unable to INSERT AFTER/BEFORE .data: section not defined Index: lld/trunk/test/ELF/linkerscript/insert-before.test =================================================================== --- lld/trunk/test/ELF/linkerscript/insert-before.test +++ lld/trunk/test/ELF/linkerscript/insert-before.test @@ -0,0 +1,29 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/insert-after.s -o %t1.o + +## Main linker script contains .text and .data sections. Here +## we check that can use INSERT BEFORE to insert sections .foo.data +## and .foo.text at the right places. + +SECTIONS { + .foo.data : { *(.foo.data) } +} INSERT BEFORE .data; + +SECTIONS { + .foo.text : { *(.foo.text) } +} INSERT BEFORE .text; + +# RUN: ld.lld %t1.o -o %t1 --script %p/Inputs/insert-after.script --script %s +# RUN: llvm-objdump -section-headers %t1 | FileCheck %s +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address Type +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 .foo.text 00000008 0000000000000000 TEXT DATA +# CHECK-NEXT: 2 .text 00000008 0000000000000008 TEXT DATA +# CHECK-NEXT: 3 .foo.data 00000008 0000000000000010 DATA +# CHECK-NEXT: 4 .data 00000008 0000000000000018 DATA + +# RUN: not ld.lld %t1.o -o %t1 --script %s 2>&1 \ +# RUN: | FileCheck %s --check-prefix=ERR +# ERR-DAG: error: unable to INSERT AFTER/BEFORE .text: section not defined +# ERR-DAG: error: unable to INSERT AFTER/BEFORE .data: section not defined Index: lld/trunk/test/ELF/linkerscript/insert-broken.test =================================================================== --- lld/trunk/test/ELF/linkerscript/insert-broken.test +++ lld/trunk/test/ELF/linkerscript/insert-broken.test @@ -3,4 +3,4 @@ } INSERT .data; # RUN: not ld.lld -o %t1 --script %s 2>&1 | FileCheck %s -# CHECK: {{.*}}:3: AFTER expected, but got .data +# CHECK: {{.*}}:3: expected AFTER/BEFORE, but got '.data'