[lld] r280915 - Linker script: implement ALIGNOF

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 8 02:08:31 PDT 2016


Author: evgeny777
Date: Thu Sep  8 04:08:30 2016
New Revision: 280915

URL: http://llvm.org/viewvc/llvm-project?rev=280915&view=rev
Log:
Linker script: implement ALIGNOF

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

Added:
    lld/trunk/test/ELF/linkerscript/alignof.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=280915&r1=280914&r2=280915&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Sep  8 04:08:30 2016
@@ -572,6 +572,15 @@ uint64_t LinkerScript<ELFT>::getOutputSe
   return 0;
 }
 
+template <class ELFT>
+uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
+  for (OutputSectionBase<ELFT> *Sec : *OutputSections)
+    if (Sec->getName() == Name)
+      return Sec->getAlignment();
+  error("undefined section " + Name);
+  return 0;
+}
+
 template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
   return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
 }
@@ -1306,6 +1315,13 @@ Expr ScriptParser::readPrimary() {
     expect(")");
     return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
   }
+  if (Tok == "ALIGNOF") {
+    expect("(");
+    StringRef Name = next();
+    expect(")");
+    return
+        [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
+  }
   if (Tok == "SIZEOF_HEADERS")
     return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
 

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=280915&r1=280914&r2=280915&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Thu Sep  8 04:08:30 2016
@@ -127,6 +127,7 @@ class LinkerScriptBase {
 public:
   virtual uint64_t getOutputSectionAddress(StringRef Name) = 0;
   virtual uint64_t getOutputSectionSize(StringRef Name) = 0;
+  virtual uint64_t getOutputSectionAlign(StringRef Name) = 0;
   virtual uint64_t getHeaderSize() = 0;
   virtual uint64_t getSymbolValue(StringRef S) = 0;
 };
@@ -173,6 +174,7 @@ public:
   bool hasPhdrsCommands();
   uint64_t getOutputSectionAddress(StringRef Name) override;
   uint64_t getOutputSectionSize(StringRef Name) override;
+  uint64_t getOutputSectionAlign(StringRef Name) override;
   uint64_t getHeaderSize() override;
   uint64_t getSymbolValue(StringRef S) override;
 

Added: lld/trunk/test/ELF/linkerscript/alignof.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/alignof.s?rev=280915&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/alignof.s (added)
+++ lld/trunk/test/ELF/linkerscript/alignof.s Thu Sep  8 04:08:30 2016
@@ -0,0 +1,41 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { \
+# RUN:   .aaa         : { *(.aaa) } \
+# RUN:   .bbb         : { *(.bbb) } \
+# RUN:   .ccc         : { *(.ccc) } \
+# RUN:   _aaa = ALIGNOF(.aaa); \
+# RUN:   _bbb = ALIGNOF(.bbb); \
+# RUN:   _ccc = ALIGNOF(.ccc); \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -t %t1 | FileCheck %s
+# CHECK:      SYMBOL TABLE:
+# CHECK:      0000000000000008         *ABS*     00000000 _aaa
+# CHECK-NEXT: 0000000000000010         *ABS*     00000000 _bbb
+# CHECK-NEXT: 0000000000000020         *ABS*     00000000 _ccc
+
+## Check that we error out if trying to get alignment of
+## section that does not exist.
+# RUN: echo "SECTIONS { \
+# RUN:   _aaa = ALIGNOF(.foo); \
+# RUN: }" > %t.script
+# RUN: not ld.lld -o %t1 --script %t.script %t 2>&1 \
+# RUN:  | FileCheck -check-prefix=ERR %s
+# ERR: undefined section .foo
+.global _start
+_start:
+ nop
+
+.section .aaa,"a"
+ .align 8
+ .quad 0
+
+.section .bbb,"a"
+ .align 16
+ .quad 0
+
+.section .ccc,"a"
+ .align 32
+ .quad 0




More information about the llvm-commits mailing list