This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Relative expressions and ASSERT
- From: Alan Modra <amodra at gmail dot com>
- To: "H.J. Lu" <hjl dot tools at gmail dot com>
- Cc: "Maciej W. Rozycki" <macro at codesourcery dot com>, Daniel Jacobowitz <dan at codesourcery dot com>, binutils at sourceware dot org, Kazu Hirata <kazu at codesourcery dot com>
- Date: Thu, 20 Jan 2011 22:55:40 +1030
- Subject: Re: Relative expressions and ASSERT
- References: <AANLkTin_6FXTQwGjbbgk9CSc_ceahsYQyS5zVcX11ySJ@mail.gmail.com> <20101216225820.GE15607@bubble.grove.modra.org> <AANLkTikYeeUOs41zH2K7Ef8VzXwXan11OLhn+m7iBk=G@mail.gmail.com> <20101217000136.GF15607@bubble.grove.modra.org> <AANLkTi=5BPNSOw+SFR0baqYq-peS47whyc0P9dhyprMh@mail.gmail.com> <AANLkTi=XGnHQ7Gn_oi+1PwBn34gvFEgr_W11b3Cg=hWh@mail.gmail.com> <20101220125909.GL15607@bubble.grove.modra.org> <alpine.DEB.1.10.1101190615310.5891@tp.orcam.me.uk> <20110119082505.GA11694@bubble.grove.modra.org> <AANLkTimCSXPDpB-S4cWZTfL1ssDrC+W+_BGfuJhnYC98@mail.gmail.com>
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