return C->Kind == InputSectionKind;
}
+bool AssertCommand::classof(const BaseCommand *C) {
+ return C->Kind == AssertKind;
+}
+
template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
return !S || !S->Live;
}
continue;
}
+ if (auto *Cmd = dyn_cast<AssertCommand>(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.
SymbolAssignment *readProvide(bool Hidden);
Expr readAlign();
void readSort();
+ Expr readAssert();
Expr readExpr();
Expr readExpr1(Expr Lhs, int MinPrec);
Cmd = readProvide(false);
} else if (Tok == "PROVIDE_HIDDEN") {
Cmd = readProvide(true);
+ } else if (Tok == "ASSERT") {
+ Cmd = new AssertCommand(readAssert());
} else {
Cmd = readOutputSectionDescription(Tok);
}
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;
+ };
+}
+
OutputSectionCommand *
ScriptParser::readOutputSectionDescription(StringRef OutSec) {
OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
// Built-in functions are parsed here.
// https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
+ if (Tok == "ASSERT")
+ return readAssert();
if (Tok == "ALIGN") {
expect("(");
Expr E = readExpr();
enum SectionsCommandKind {
AssignmentKind,
OutputSectionKind,
- InputSectionKind
+ InputSectionKind,
+ AssertKind
};
struct BaseCommand {
std::vector<StringRef> 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;
--- /dev/null
+# 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