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: macro expansion and operator &&


>>> On 16.08.10 at 07:01, Alan Modra <amodra@gmail.com> wrote:
> On Sat, Aug 14, 2010 at 02:24:35PM +0300, Alex Druinsky wrote:
>> Hi,
>> 
>> I am writing code in GCC's inline assembly, and I stumbled upon what
>> looks like a bug in GAS. It appears that when GAS expands macros, it
>> will sometimes replace expressions with operator && by expressions
>> with operator &, changing the original meaning of the code.
>> 
>> I made a tiny program to illustrate the problem. To reproduce this,
>> run the attached program and verify that the return code is 10. The
>> return code should actually be 0.
>> 
>> Is this indeed a bug?
> 
> Yes, I think it is, but I'm not at all comfortable making changes to
> macro.c.  There are a lot or things happening in this file that are
> not documented in either the code or the gas info, so it's hard to
> know what should be done about bugs like this one.  To get some idea,
> it helps to go back to gas-2.3 and look at the gasp code and info.  If
> you do that, you discover that a single quote could be used to
> separate a gasp preprocessor variable from following text.  So the
> calls to sub_actual that pass kind='\'' are arranging to have
> get_apost_token remove a single quote from the input stream.  The
> sub_actual call causing a problem is the one that passes kind='&'.
> This seems to allow macros like the following:
> 
>   .macro x arg
>   .long &arg&,&arg,0
>   .endm
> 
> ie. deliniating macro parameter substitutions by an ampersand prefix,
> or both an ampersand prefix and suffix like masm.  We don't document
> this feature as far as I can see.
> 
> Ian, Nick, does this look reasonable?
> 
> 	* macro.c (sub_actual): Add back ampersand suffix when no
> 	substitution.
> 
> Index: gas/macro.c
> ===================================================================
> RCS file: /cvs/src/src/gas/macro.c,v
> retrieving revision 1.53
> diff -u -p -r1.53 macro.c
> --- gas/macro.c	23 Jul 2010 06:44:25 -0000	1.53
> +++ gas/macro.c	16 Aug 2010 04:53:52 -0000
> @@ -748,6 +748,8 @@ sub_actual (int start, sb *in, sb *t, st
>        /* Doing this permits people to use & in macro bodies.  */
>        sb_add_char (out, '&');
>        sb_add_sb (out, t);
> +      if (src != start && in->ptr[src - 1] == '&')
> +	sb_add_char (out, '&');
>      }
>    else if (copyifnotthere)
>      {

While I didn't verify it, I would think that in alternate macro mode
you shouldn't put back the ampersand. Actually, according to the
comment around the respective invocation of sub_actual(), I have
always been suspecting that the function should only be called
there in alternate mode anyway.

Jan

Jan


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