Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -68,8 +68,12 @@ Sym->Binding = STB_GLOBAL; ExprValue Value = Cmd->Expression(); SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec; + // We want to set symbol values early if we can. This allows us to use symbols + // as variables in linker scripts. Doing so allows us to write expressions + // like this: `alignment = 16; . = ALIGN(., alignment)` + uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0; replaceBody(Sym, Cmd->Name, /*IsLocal=*/false, Visibility, - STT_NOTYPE, 0, 0, Sec, nullptr); + STT_NOTYPE, SymValue, 0, Sec, nullptr); return Sym->body(); } Index: test/ELF/linkerscript/align.s =================================================================== --- test/ELF/linkerscript/align.s +++ test/ELF/linkerscript/align.s @@ -67,7 +67,40 @@ # RUN: } \ # RUN: }" > %t2.script # RUN: ld.lld -o %t2 --script %t2.script %t -# RUN: llvm-objdump -section-headers %t1 | FileCheck %s +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s + +## Check use of variables in align expressions: +# RUN: echo " VAR = 0x1000; \ +# RUN: __code_base__ = 0x10000; \ +# RUN: SECTIONS { \ +# RUN: . = __code_base__; \ +# RUN: .aaa : \ +# RUN: { \ +# RUN: *(.aaa) \ +# RUN: } \ +# RUN: .bbb : ALIGN(VAR) \ +# RUN: { \ +# RUN: *(.bbb) \ +# RUN: } \ +# RUN: . = ALIGN(., VAR * 4); \ +# RUN: .ccc : \ +# RUN: { \ +# RUN: *(.ccc) \ +# RUN: } \ +# RUN: __start_bbb = ADDR(.bbb); \ +# RUN: __end_bbb = ALIGN(__start_bbb + SIZEOF(.bbb), VAR); \ +# RUN: }" > %t3.script +# RUN: ld.lld -o %t3 --script %t3.script %t +# RUN: llvm-objdump -section-headers %t3 | FileCheck %s +# RUN: llvm-objdump -t %t3 | FileCheck -check-prefix SYMBOLS %s + +# SYMBOLS-LABEL: SYMBOL TABLE: +# SYMBOLS-NEXT: 0000000000000000 *UND* 00000000 +# SYMBOLS-NEXT: 0000000000014008 .text 00000000 _start +# SYMBOLS-NEXT: 0000000000010000 *ABS* 00000000 __code_base__ +# SYMBOLS-NEXT: 0000000000001000 *ABS* 00000000 VAR +# SYMBOLS-NEXT: 0000000000011000 .bbb 00000000 __start_bbb +# SYMBOLS-NEXT: 0000000000012000 *ABS* 00000000 __end_bbb .global _start _start: