Fold arithmetic integer expressions
authorAlan Modra <amodra@gmail.com>
Tue, 4 Oct 2016 00:11:26 +0000 (10:41 +1030)
committerAlan Modra <amodra@gmail.com>
Tue, 4 Oct 2016 00:11:26 +0000 (10:41 +1030)
Commit b751e639 regressed arm linux kernel builds, that have an
ASSERT (((__hyp_idmap_text_end - (__hyp_idmap_text_start
                  & ~ (((0x1 << 0xc) - 0x1))))
         <= (0x1 << 0xc)), HYP init code too big or misaligned)

Due to some insanity in ld expression evaluation, the integer values
0x1 and 0xc above are treated as absolute addresses (ie. they have an
associated section, *ABS*, see exp_fold_tree_1 case etree_value) while
the expression (0x1 << 0xc) has a plain number result.  The left hand
side of the inequality happens to evaluate to a "negative" .text
section relative value.  Comparing a section relative value against an
absolute value works since the section relative value is first
converted to absolute.  Comparing a section relative value against a
number just compares the offsets, which fails since the "negative"
offset is really a very large positive number.

This patch works around the problem by folding integer expressions, so
the assert again becomes
ASSERT (((__hyp_idmap_text_end - (__hyp_idmap_text_start
                  & 0xfffffffffffff000))
         <= 0x1000), HYP init code too big or misaligned)

* ldexp.c (exp_value_fold): New function.
(exp_unop, exp_binop, exp_trinop): Use it.

ld/ChangeLog
ld/ldexp.c

index 3ad2a7a..8b22c50 100644 (file)
@@ -1,3 +1,8 @@
+2016-10-04  Alan Modra  <amodra@gmail.com>
+
+       * ldexp.c (exp_value_fold): New function.
+       (exp_unop, exp_binop, exp_trinop): Use it.
+
 2016-09-30  Alan Modra  <amodra@gmail.com>
 
        * scripttempl/v850.sc: Don't reference __ctbp, __ep, __gp when
index a560643..9f88144 100644 (file)
@@ -1252,6 +1252,19 @@ exp_fold_tree_no_dot (etree_type *tree)
   exp_fold_tree_1 (tree);
 }
 
+static void
+exp_value_fold (etree_type *tree)
+{
+  exp_fold_tree_no_dot (tree);
+  if (expld.result.valid_p)
+    {
+      tree->type.node_code = INT;
+      tree->value.value = expld.result.value;
+      tree->value.str = NULL;
+      tree->type.node_class = etree_value;
+    }
+}
+
 etree_type *
 exp_binop (int code, etree_type *lhs, etree_type *rhs)
 {
@@ -1263,6 +1276,12 @@ exp_binop (int code, etree_type *lhs, etree_type *rhs)
   new_e->binary.lhs = lhs;
   new_e->binary.rhs = rhs;
   new_e->type.node_class = etree_binary;
+  if (lhs->type.node_class == etree_value
+      && rhs->type.node_class == etree_value
+      && code != ALIGN_K
+      && code != DATA_SEGMENT_ALIGN
+      && code != DATA_SEGMENT_RELRO_END)
+    exp_value_fold (new_e);
   return new_e;
 }
 
@@ -1278,6 +1297,10 @@ exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
   new_e->trinary.cond = cond;
   new_e->trinary.rhs = rhs;
   new_e->type.node_class = etree_trinary;
+  if (cond->type.node_class == etree_value
+      && lhs->type.node_class == etree_value
+      && rhs->type.node_class == etree_value)
+    exp_value_fold (new_e);
   return new_e;
 }
 
@@ -1291,6 +1314,12 @@ exp_unop (int code, etree_type *child)
   new_e->unary.type.lineno = child->type.lineno;
   new_e->unary.child = child;
   new_e->unary.type.node_class = etree_unary;
+  if (child->type.node_class == etree_value
+      && code != ALIGN_K
+      && code != ABSOLUTE
+      && code != NEXT
+      && code != DATA_SEGMENT_END)
+    exp_value_fold (new_e);
   return new_e;
 }