[PATCH] D22759: [ELF] Linkerscript: symbol assignments with indentifiers on the right side of expression.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 26 04:13:24 PDT 2016


grimar updated this revision to Diff 65492.
grimar added a comment.

- Addressed review comments.
- Improved testcase.


https://reviews.llvm.org/D22759

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/linkerscript-symbol-assignexpr.s

Index: test/ELF/linkerscript/linkerscript-symbol-assignexpr.s
===================================================================
--- test/ELF/linkerscript/linkerscript-symbol-assignexpr.s
+++ test/ELF/linkerscript/linkerscript-symbol-assignexpr.s
@@ -0,0 +1,32 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { \
+# RUN:         symbol = CONSTANT(MAXPAGESIZE); \
+# RUN:         symbol2 = symbol + 0x1234; \
+# RUN:         symbol3 = symbol2; \
+# RUN:       }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -t %t1 | FileCheck %s
+
+# CHECK:      SYMBOL TABLE:
+# CHECK-NEXT: 0000000000000000 *UND* 00000000
+# CHECK-NEXT: 0000000000000120 .text 00000000 _start
+# CHECK-NEXT: 0000000000000121 .text 00000000 foo
+# CHECK-NEXT: 0000000000001000 *ABS* 00000000 symbol
+# CHECK-NEXT: 0000000000002234 *ABS* 00000000 symbol2
+# CHECK-NEXT: 0000000000002234 *ABS* 00000000 symbol3
+
+# RUN: echo "SECTIONS { \
+# RUN:         symbol2 = symbol; \
+# RUN:       }" > %t2.script
+# RUN: not ld.lld -o %t2 --script %t2.script %t 2>&1 \
+# RUN:  | FileCheck -check-prefix=ERR %s
+# ERR: symbol not found: symbol
+
+.global _start
+_start:
+ nop
+
+.global foo
+foo:
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -743,6 +743,31 @@
 // script expression.
 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
 
+uint64_t static getSymbolValue(StringRef S) {
+  switch (Config->EKind) {
+  case ELF32LEKind:
+    if (SymbolBody *B = Symtab<ELF32LE>::X->find(S))
+      return B->getVA<ELF32LE>();
+    break;
+  case ELF32BEKind:
+    if (SymbolBody *B = Symtab<ELF32BE>::X->find(S))
+      return B->getVA<ELF32BE>();
+    break;
+  case ELF64LEKind:
+    if (SymbolBody *B = Symtab<ELF64LE>::X->find(S))
+      return B->getVA<ELF64LE>();
+    break;
+  case ELF64BEKind:
+    if (SymbolBody *B = Symtab<ELF64BE>::X->find(S))
+      return B->getVA<ELF64BE>();
+    break;
+  default:
+    llvm_unreachable("unknown ELF kind");
+  }
+  error("symbol not found: " + S);
+  return 0;
+}
+
 // This is a part of the operator-precedence parser. This function
 // assumes that the remaining token stream starts with an operator.
 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
@@ -825,8 +850,11 @@
 
   // Parse a number literal
   uint64_t V = 0;
-  if (Tok.getAsInteger(0, V))
-    setError("malformed number: " + Tok);
+  if (Tok.getAsInteger(0, V)) {
+    if (!isValidCIdentifier(Tok))
+      setError("malformed number: " + Tok);
+    return [=](uint64_t Dot) { return getSymbolValue(Tok); };
+  }
   return [=](uint64_t Dot) { return V; };
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22759.65492.patch
Type: text/x-patch
Size: 2760 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160726/f1bc2f95/attachment.bin>


More information about the llvm-commits mailing list