This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Relative expressions and ASSERT


On Wed, Jan 19, 2011 at 04:27:53AM -0800, H.J. Lu wrote:
> On Wed, Jan 19, 2011 at 12:25 AM, Alan Modra <amodra@gmail.com> wrote:
> > On Wed, Jan 19, 2011 at 06:48:51AM +0000, Maciej W. Rozycki wrote:
> >> ?Is that intended behaviour? ?From your update to the manual I infer
> >> otherwise and it looks to me like a mishandled case that slipped through,
> >> but perhaps I'm missing something. ?Either way I find it plain wrong that
> >> a difference of two symbols (both coming from/relative to the same
> >> section) yields a plain number in GAS, but not in an LD script. ?Hmm...
> >
> > ld actually does follow the current ld.texinfo description. ?The two
> > symbols are in the same section, so the operation (subtraction in this
> > case) is performed on their offsets, yielding a result in the same
> > section.
> >
> > Admittedly, this does lead to an odd result. ?Should we special case
> > subtraction? ?Exclusive or too?
> >
> 
> I got a linker regression report on symbol subtraction on x86. I am waiting
> for a testcase.

I'd like to see the testcase too.

I have two possible patches.  The first only changes subtraction and
xor of two symbols so that the result is simply a number, even if the
symbols are not in the same section.  That I think is the correct
result and uncontroversial.  However, this patch leaves (x + -y) with
a section, a result that differs now from (x - y).

The second patch changes all binary arithmetic and logical operations
on two symbols to always return a plain number, fixing the case above.
Unfortunately it does make some other subexpressions inconsistent.
For example, (2 * a) has section while (a + a) does not.  It's a toss
up which patch to choose.  Here's the second one.

	* ldexp.c (fold_binary): Set result section for arithmetic and
	logical operations to NULL when both operands are in same section.
	* ld.texinfo (Expression Section): Describe this.

Index: ld/ldexp.c
===================================================================
RCS file: /cvs/src/src/ld/ldexp.c,v
retrieving revision 1.92
diff -u -p -r1.92 ldexp.c
--- ld/ldexp.c	13 Jan 2011 13:29:55 -0000	1.92
+++ ld/ldexp.c	20 Jan 2011 03:42:26 -0000
@@ -335,11 +335,15 @@ fold_binary (etree_type *tree)
 	    {
 	      make_abs ();
 	      lhs.value += lhs.section->vma;
+	      lhs.section = bfd_abs_section_ptr;
 	    }
 
 	  /* If the rhs is just a number, keep the lhs section.  */
 	  else if (expld.result.section == NULL)
-	    expld.result.section = lhs.section;
+	    {
+	      expld.result.section = lhs.section;
+	      lhs.section = NULL;
+	    }
 	}
 
       switch (tree->type.node_code)
@@ -363,6 +367,8 @@ fold_binary (etree_type *tree)
 #define BOP(x, y) \
 	case x:							\
 	  expld.result.value = lhs.value y expld.result.value;	\
+	  if (expld.result.section == lhs.section)		\
+	    expld.result.section = NULL;			\
 	  break;
 
 #define BOPN(x, y) \
Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.271
diff -u -p -r1.271 ld.texinfo
--- ld/ld.texinfo	13 Jan 2011 13:34:53 -0000	1.271
+++ ld/ld.texinfo	20 Jan 2011 03:42:31 -0000
@@ -5567,8 +5567,13 @@ An operation involving only numbers resu
 @item
 The result of comparisons, @samp{&&} and @samp{||} is also a number.
 @item
-The result of other operations on relative addresses (after above
-conversions) is a relative address in the same section as the operand(s).
+The result of other binary arithmetic and logical operations on two
+relative addresses in the same section or two absolute addresess
+(after above conversions) is also a number.
+@item
+The result of other operations on relative addresses or one
+relative address and a number, is a relative address in the same
+section as the relative operand(s).
 @item
 The result of other operations on absolute addresses (after above
 conversions) is an absolute address.

-- 
Alan Modra
Australia Development Lab, IBM


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]