Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -897,6 +897,18 @@ expect(")"); return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; } + if (Tok == "ASSERT") { + expect("("); + Expr E = readExpr(); + expect(","); + StringRef Msg = next(); + expect(")"); + return [=](uint64_t Dot) { + if (!E(Dot)) + error(Msg); + return Dot; + }; + } // 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,58 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o + +# RUN: echo "SECTIONS { \ +# RUN: .aaa : { *(.aaa) } \ +# RUN: _end_aaa = .; \ +# RUN: . = ASSERT(_end_aaa == 0x128, \"Assert fail!\"); \ +# RUN: .bbb : { *(.bbb) } \ +# RUN: _end_bbb = .; \ +# RUN: . = ASSERT(_end_bbb <= 0x128 * 2, \"Assert fail!\"); \ +# RUN: }" > %t1.script +# RUN: ld.lld -o %t1 --script %t1.script %t1.o +# RUN: llvm-readobj -s %t1 | FileCheck %s -check-prefix=PASS +# PASS: Section { +# PASS: Index: 2 +# PASS-NEXT: Name: .bbb +# PASS-NEXT: Type: SHT_PROGBITS +# PASS-NEXT: Flags [ +# PASS-NEXT: SHF_ALLOC +# PASS-NEXT: ] +# PASS-NEXT: Address: 0x128 +# PASS-NEXT: Offset: 0x128 +# PASS-NEXT: Size: 8 +# PASS-NEXT: Link: +# PASS-NEXT: Info: +# PASS-NEXT: AddressAlignment: +# PASS-NEXT: EntrySize: +# PASS-NEXT: } + +# RUN: echo "SECTIONS { \ +# RUN: .aaa : { *(.aaa) } \ +# RUN: _end_aaa = .; \ +# RUN: . = ASSERT(_end_aaa != 0x128, \"Assert fail!\"); \ +# RUN: }" > %t2.script +# RUN: not ld.lld -o %t2 --script %t2.script %t1.o > %t.log 2>&1 +# RUN: FileCheck %s -check-prefix=FAIL < %t.log +# FAIL: Assert fail! + +# RUN: echo "SECTIONS { \ +# RUN: .aaa : { *(.aaa) } \ +# RUN: _end_aaa = .; \ +# RUN: . = ASSERT(_end_aaa == 0x128, \"Assert fail!\"); \ +# RUN: .bbb : { *(.bbb) } \ +# RUN: _end_bbb = .; \ +# RUN: . = ASSERT(_end_bbb > 0x128 * 2, \"Assert fail!\"); \ +# RUN: }" > %t3.script +# RUN: not ld.lld -o %t3 --script %t3.script %t1.o > %t.log 2>&1 +# RUN: FileCheck %s -check-prefix=FAIL < %t.log + +.global _start +_start: + nop + +.section .aaa,"a" + .quad 0 + +.section .bbb,"a" + .quad 0