diff options
author | George Rimar <grimar@accesssoftek.com> | 2016-07-26 18:18:58 +0000 |
---|---|---|
committer | George Rimar <grimar@accesssoftek.com> | 2016-07-26 18:18:58 +0000 |
commit | a9c5a528469e99dd1f7bb29ed053b98dec45c99d (patch) | |
tree | 4828fce79c022741bc5ba5989b6083962770c1f4 | |
parent | f17d48e58a76cef6cc6bfeb2d7b59c30d80d42de (diff) | |
download | bcm5719-llvm-a9c5a528469e99dd1f7bb29ed053b98dec45c99d.tar.gz bcm5719-llvm-a9c5a528469e99dd1f7bb29ed053b98dec45c99d.zip |
[ELF] Linkerscript: symbol assignments with indentifiers on the right side of expression.
In symbol assignments symbol may appear on the right-hand side of the expression:
(https://svnweb.freebsd.org/base/head/sys/conf/ldscript.amd64?revision=284870&view=markup#l8)
kernphys = CONSTANT (MAXPAGESIZE);
. = kernbase + kernphys + SIZEOF_HEADERS;
Patch implements that.
Differential revision: https://reviews.llvm.org/D22759
llvm-svn: 276784
-rw-r--r-- | lld/ELF/LinkerScript.cpp | 32 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/linkerscript-symbol-assignexpr.s | 32 |
2 files changed, 61 insertions, 3 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 3889f104ecf..1079a2fa99b 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -756,6 +756,29 @@ SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { // script expression. Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } +static uint64_t 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; + } + 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) { @@ -844,10 +867,13 @@ Expr ScriptParser::readPrimary() { return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; } - // Parse a number literal + // Parse a symbol name or 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; }; } diff --git a/lld/test/ELF/linkerscript/linkerscript-symbol-assignexpr.s b/lld/test/ELF/linkerscript/linkerscript-symbol-assignexpr.s new file mode 100644 index 00000000000..353800b6de6 --- /dev/null +++ b/lld/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: |