[ELF] - Add support for '||' and '&&' in linker scripts.
authorGeorge Rimar <grimar@accesssoftek.com>
Tue, 3 Jul 2018 14:02:52 +0000 (14:02 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Tue, 3 Jul 2018 14:02:52 +0000 (14:02 +0000)
This is https://bugs.llvm.org//show_bug.cgi?id=37976,
we had no support, but seems someone faced it.

llvm-svn: 336197

lld/ELF/ScriptLexer.cpp
lld/ELF/ScriptParser.cpp
lld/test/ELF/linkerscript/operators.test

index ef5a1cf..49e808e 100644 (file)
@@ -117,7 +117,7 @@ void ScriptLexer::tokenize(MemoryBufferRef MB) {
 
     // ">foo" is parsed to ">" and "foo", but ">>" is parsed to ">>".
     if (S.startswith("<<") || S.startswith("<=") || S.startswith(">>") ||
-        S.startswith(">=")) {
+        S.startswith(">=") || S.startswith("||") || S.startswith("&&")) {
       Vec.push_back(S.substr(0, 2));
       S = S.substr(2);
       continue;
index 1d6a217..b0982f5 100644 (file)
@@ -524,12 +524,14 @@ void ScriptParser::readSections() {
 
 static int precedence(StringRef Op) {
   return StringSwitch<int>(Op)
-      .Cases("*", "/", "%", 6)
-      .Cases("+", "-", 5)
-      .Cases("<<", ">>", 4)
-      .Cases("<", "<=", ">", ">=", "==", "!=", 3)
-      .Case("&", 2)
-      .Case("|", 1)
+      .Cases("*", "/", "%", 8)
+      .Cases("+", "-", 7)
+      .Cases("<<", ">>", 6)
+      .Cases("<", "<=", ">", ">=", "==", "!=", 5)
+      .Case("&", 4)
+      .Case("|", 3)
+      .Case("&&", 2)
+      .Case("||", 1)
       .Default(-1);
 }
 
@@ -924,6 +926,10 @@ Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
     return [=] { return L().getValue() == R().getValue(); };
   if (Op == "!=")
     return [=] { return L().getValue() != R().getValue(); };
+  if (Op == "||")
+    return [=] { return L().getValue() || R().getValue(); };
+  if (Op == "&&")
+    return [=] { return L().getValue() && R().getValue(); };
   if (Op == "&")
     return [=] { return bitAnd(L(), R()); };
   if (Op == "|")
index 7996044..2be24df 100644 (file)
@@ -38,6 +38,14 @@ SECTIONS {
   minus_abs = _end - _start;
   max = MAX(11, 22);
   min = MIN(11, 22);
+  logicaland1 = 0 && 0;
+  logicaland2 = 0 && 1;
+  logicaland3 = 1 && 0;
+  logicaland4 = 1 && 1;
+  logicalor1 = 0 || 0;
+  logicalor2 = 0 || 1;
+  logicalor3 = 1 || 0;
+  logicalor4 = 1 || 1;
 }
 
 # CHECK: 00000000000006 *ABS* 00000000 plus
@@ -70,6 +78,14 @@ SECTIONS {
 # CHECK: 0000000000fff0 *ABS* 00000000 minus_abs
 # CHECK: 00000000000016 *ABS* 00000000 max
 # CHECK: 0000000000000b *ABS* 00000000 min
+# CHECK: 00000000000000 *ABS* 00000000 logicaland1
+# CHECK: 00000000000000 *ABS* 00000000 logicaland2
+# CHECK: 00000000000000 *ABS* 00000000 logicaland3
+# CHECK: 00000000000001 *ABS* 00000000 logicaland4
+# CHECK: 00000000000000 *ABS* 00000000 logicalor1
+# CHECK: 00000000000001 *ABS* 00000000 logicalor2
+# CHECK: 00000000000001 *ABS* 00000000 logicalor3
+# CHECK: 00000000000001 *ABS* 00000000 logicalor4
 
 ## Mailformed number error.
 # RUN: echo "SECTIONS { . = 0x12Q41; }" > %t.script