Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -41,7 +41,8 @@ enum SectionsCommandKind { AssignmentKind, OutputSectionKind, - InputSectionKind + InputSectionKind, + AssertKind }; struct BaseCommand { @@ -98,6 +99,12 @@ std::vector SectionPatterns; }; +struct AssertCommand : BaseCommand { + AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {} + static bool classof(const BaseCommand *C); + Expr Expression; +}; + struct PhdrsCommand { StringRef Name; unsigned Type; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -55,6 +55,10 @@ return C->Kind == InputSectionKind; } +bool AssertCommand::classof(const BaseCommand *C) { + return C->Kind == AssertKind; +} + template static bool isDiscarded(InputSectionBase *S) { return !S || !S->Live; } @@ -247,6 +251,11 @@ continue; } + if (auto *Cmd = dyn_cast(Base.get())) { + Cmd->Expression(Dot); + continue; + } + // Find all the sections with required name. There can be more than // one section with such name, if the alignment, flags or type // attribute differs. @@ -485,6 +494,7 @@ void readProvide(bool Hidden); void readAlign(OutputSectionCommand *Cmd); void readSort(); + Expr readAssert(); Expr readExpr(); Expr readExpr1(Expr Lhs, int MinPrec); @@ -690,6 +700,8 @@ readProvide(false); } else if (Tok == "PROVIDE_HIDDEN") { readProvide(true); + } else if (Tok == "ASSERT") { + Opt.Commands.emplace_back(new AssertCommand(readAssert())); } else { readOutputSectionDescription(Tok); } @@ -794,6 +806,20 @@ expect(")"); } +Expr ScriptParser::readAssert() { + expect("("); + Expr E = readExpr(); + expect(","); + StringRef Msg = next(); + expect(")"); + return [=](uint64_t Dot) { + uint64_t V = E(Dot); + if (!V) + error(Msg); + return V; + }; +} + void ScriptParser::readOutputSectionDescription(StringRef OutSec) { OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); Opt.Commands.emplace_back(Cmd); @@ -1022,6 +1048,8 @@ expect(")"); return [=](uint64_t Dot) { return getSectionSize(Name); }; } + if (Tok == "ASSERT") + return readAssert(); // Parse a symbol name or a number literal. uint64_t V = 0; Index: test/ELF/linkerscript/linkerscript-assert.s =================================================================== --- test/ELF/linkerscript/linkerscript-assert.s +++ test/ELF/linkerscript/linkerscript-assert.s @@ -0,0 +1,27 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o + +# RUN: echo "SECTIONS { \ +# RUN: ASSERT(1, \"true\") \ +# RUN: }" > %t1.script +# RUN: ld.lld -shared -o %t1 --script %t1.script %t1.o +# RUN: llvm-readobj %t1 > /dev/null + +# RUN: echo "SECTIONS { \ +# RUN: ASSERT(ASSERT(42, \"true\") == 42, \"true\") \ +# RUN: }" > %t2.script +# RUN: ld.lld -shared -o %t2 --script %t2.script %t1.o +# RUN: llvm-readobj %t2 > /dev/null + +# RUN: echo "SECTIONS { \ +# RUN: ASSERT(0,\ "fail\") \ +# RUN: }" > %t3.script +# RUN: not ld.lld -shared -o %t3 --script %t3.script %t1.o > %t.log 2>&1 +# RUN: FileCheck %s -check-prefix=FAIL < %t.log +# FAIL: fail + +# RUN: echo "SECTIONS { \ +# RUN: . = ASSERT(0x1000, \"true\"); \ +# RUN: }" > %t4.script +# RUN: ld.lld -shared -o %t4 --script %t4.script %t1.o +# RUN: llvm-readobj %t4 > /dev/null