[lld] r285608 - Move IsAbsolute from SymbolAssignment to Expr.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 31 10:43:38 PDT 2016


Author: rafael
Date: Mon Oct 31 12:43:38 2016
New Revision: 285608

URL: http://llvm.org/viewvc/llvm-project?rev=285608&view=rev
Log:
Move IsAbsolute from SymbolAssignment to Expr.

And as a token of the new feature, make ALIGNOF always absolute.

This is a step in making it possible to have non absolute symbols out
of output sections.

Added:
    lld/trunk/test/ELF/linkerscript/absolute-expr.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=285608&r1=285607&r2=285608&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Oct 31 12:43:38 2016
@@ -64,7 +64,7 @@ template <class ELFT> static void addSyn
 }
 
 template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
-  if (Cmd->IsAbsolute)
+  if (Cmd->Expression.IsAbsolute)
     addRegular<ELFT>(Cmd);
   else
     addSynthetic<ELFT>(Cmd);
@@ -1424,7 +1424,7 @@ SymbolAssignment *ScriptParser::readProv
     Cmd = readProvideHidden(true, true);
   }
   if (Cmd && MakeAbsolute)
-    Cmd->IsAbsolute = true;
+    Cmd->Expression.IsAbsolute = true;
   return Cmd;
 }
 
@@ -1436,20 +1436,19 @@ static uint64_t getSymbolValue(StringRef
 
 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
   StringRef Op = next();
-  bool IsAbsolute = false;
   Expr E;
   assert(Op == "=" || Op == "+=");
   if (consume("ABSOLUTE")) {
     // The RHS may be something like "ABSOLUTE(.) & 0xff".
     // Call readExpr1 to read the whole expression.
     E = readExpr1(readParenExpr(), 0);
-    IsAbsolute = true;
+    E.IsAbsolute = true;
   } else {
     E = readExpr();
   }
   if (Op == "+=")
     E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
-  return new SymbolAssignment(Name, E, IsAbsolute);
+  return new SymbolAssignment(Name, E);
 }
 
 // This is an operator-precedence parser to parse a linker
@@ -1671,8 +1670,9 @@ Expr ScriptParser::readPrimary() {
   }
   if (Tok == "ALIGNOF") {
     StringRef Name = readParenLiteral();
-    return
-        [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
+    return {
+        [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); },
+        true};
   }
   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=285608&r1=285607&r2=285608&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Mon Oct 31 12:43:38 2016
@@ -37,7 +37,17 @@ class InputSectionData;
 // ScriptParser::readExpr reads an expression and returns an Expr.
 // Later, we evaluate the expression by calling the function
 // with the value of special context variable ".".
-typedef std::function<uint64_t(uint64_t)> Expr;
+struct Expr {
+  std::function<uint64_t(uint64_t)> Val;
+  bool IsAbsolute;
+  uint64_t operator()(uint64_t Dot) const { return Val(Dot); }
+  operator bool() const { return (bool)Val; }
+
+  template <typename T>
+  Expr(T Val, bool IsAbsolute) : Val(Val), IsAbsolute(IsAbsolute) {}
+  template <typename T> Expr(T V) : Expr(V, false) {}
+  Expr() : Expr(nullptr) {}
+};
 
 // Parses a linker script. Calling this function updates
 // Config and ScriptConfig.
@@ -64,9 +74,8 @@ struct BaseCommand {
 
 // This represents ". = <expr>" or "<symbol> = <expr>".
 struct SymbolAssignment : BaseCommand {
-  SymbolAssignment(StringRef Name, Expr E, bool IsAbsolute)
-      : BaseCommand(AssignmentKind), Name(Name), Expression(E),
-        IsAbsolute(IsAbsolute) {}
+  SymbolAssignment(StringRef Name, Expr E)
+      : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
   static bool classof(const BaseCommand *C);
 
   // The LHS of an expression. Name is either a symbol name or ".".
@@ -79,7 +88,6 @@ struct SymbolAssignment : BaseCommand {
   // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
   bool Provide = false;
   bool Hidden = false;
-  bool IsAbsolute;
 };
 
 // Linker scripts allow additional constraints to be put on ouput sections.

Added: lld/trunk/test/ELF/linkerscript/absolute-expr.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/absolute-expr.s?rev=285608&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/absolute-expr.s (added)
+++ lld/trunk/test/ELF/linkerscript/absolute-expr.s Mon Oct 31 12:43:38 2016
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { \
+# RUN:                  .text : { bar = ALIGNOF(.text); *(.text) } \
+# RUN:                };" > %t.script
+# RUN: ld.lld -o %t.so --script %t.script %t.o -shared
+# RUN: llvm-readobj -t %t.so | FileCheck %s
+
+# CHECK:      Symbol {
+# CHECK:        Name: bar
+# CHECK-NEXT:   Value: 0x4
+# CHECK-NEXT:   Size: 0
+# CHECK-NEXT:   Binding: Global
+# CHECK-NEXT:   Type: None
+# CHECK-NEXT:   Other: 0
+# CHECK-NEXT:   Section: Absolute
+# CHECK-NEXT: }




More information about the llvm-commits mailing list