[lld] r300315 - [ELF] LinkerScript: Don't assign zero to all regular symbols

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 14 02:23:26 PDT 2017


Author: grimar
Date: Fri Apr 14 04:23:26 2017
New Revision: 300315

URL: http://llvm.org/viewvc/llvm-project?rev=300315&view=rev
Log:
[ELF] LinkerScript: Don't assign zero to all regular symbols

This fixes an assertion `Align != 0u && "Align can't be 0."'
in llvm::alignTo() when a linker script references a globally
defined variable in an ALIGN() context.

Patch by Alexander Richardson !

Differential revision: https://reviews.llvm.org/D31984

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/test/ELF/linkerscript/align.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=300315&r1=300314&r2=300315&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Fri Apr 14 04:23:26 2017
@@ -68,8 +68,13 @@ template <class ELFT> static SymbolBody
   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<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility,
-                              STT_NOTYPE, 0, 0, Sec, nullptr);
+                              STT_NOTYPE, SymValue, 0, Sec, nullptr);
   return Sym->body();
 }
 

Modified: lld/trunk/test/ELF/linkerscript/align.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/align.s?rev=300315&r1=300314&r2=300315&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/align.s (original)
+++ lld/trunk/test/ELF/linkerscript/align.s Fri Apr 14 04:23:26 2017
@@ -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:




More information about the llvm-commits mailing list