[lld] r328696 - [ELF] - Linkerscript: support MIN and MAX.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 28 04:33:00 PDT 2018


Author: grimar
Date: Wed Mar 28 04:33:00 2018
New Revision: 328696

URL: http://llvm.org/viewvc/llvm-project?rev=328696&view=rev
Log:
[ELF] - Linkerscript: support MIN and MAX.

Sample for the OVERLAY command from the spec 
(https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/sections.html)
uses MAX command that we do not support currently:

. = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));

This patch implements support for MIN and MAX.

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

Modified:
    lld/trunk/ELF/ScriptParser.cpp
    lld/trunk/test/ELF/linkerscript/operators.test

Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=328696&r1=328695&r2=328696&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Wed Mar 28 04:33:00 2018
@@ -1097,6 +1097,16 @@ Expr ScriptParser::readPrimary() {
       return Cmd->getLMA();
     };
   }
+  if (Tok == "MAX" || Tok == "MIN") {
+    expect("(");
+    Expr A = readExpr();
+    expect(",");
+    Expr B = readExpr();
+    expect(")");
+    if (Tok == "MIN")
+      return [=] { return std::min(A().getValue(), B().getValue()); };
+    return [=] { return std::max(A().getValue(), B().getValue()); };
+  }
   if (Tok == "ORIGIN") {
     StringRef Name = readParenLiteral();
     if (Script->MemoryRegions.count(Name) == 0) {

Modified: lld/trunk/test/ELF/linkerscript/operators.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/operators.test?rev=328696&r1=328695&r2=328696&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/operators.test (original)
+++ lld/trunk/test/ELF/linkerscript/operators.test Wed Mar 28 04:33:00 2018
@@ -36,6 +36,8 @@ SECTIONS {
   _end = .;
   minus_rel = _end - 0x10;
   minus_abs = _end - _start;
+  max = MAX(11, 22);
+  min = MIN(11, 22);
 }
 
 # CHECK: 00000000000006 *ABS* 00000000 plus
@@ -66,6 +68,8 @@ SECTIONS {
 # CHECK: 0000000000fff0 *ABS* 00000000 datasegmentalign2
 # CHECK: 0000000000ffe0 .text 00000000 minus_rel
 # CHECK: 0000000000fff0 *ABS* 00000000 minus_abs
+# CHECK: 00000000000016 *ABS* 00000000 max
+# CHECK: 0000000000000b *ABS* 00000000 min
 
 ## Mailformed number error.
 # RUN: echo "SECTIONS { . = 0x12Q41; }" > %t.script




More information about the llvm-commits mailing list