public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* How to save and restore a symbol value in Aarch64?
@ 2017-04-20  9:16 Jeffrey Walton
  2017-04-20  9:27 ` Jeffrey Walton
  2017-04-20 10:03 ` Andreas Schwab
  0 siblings, 2 replies; 7+ messages in thread
From: Jeffrey Walton @ 2017-04-20  9:16 UTC (permalink / raw)
  To: Binutils

I'm trying to save (and restore) the original value of a symbol under
Aarch64. The code runs on a variety of machines, and it looks like I
need a solution for Binutils 2.24 and above.

I know .set allows me to set the symbol value:

    asm(".set .cpu, generic+crc \n" ... );

I need the method to get the previous .cpu value and save it for the
restore. About all I have been able to do is cause assembler errors.
For example:

    __inline unsigned int GCC_INLINE_ATTRIB
    CRC32B(unsigned int crc, unsigned char v)
    {
          unsigned int r;
          asm (".set old_cpu, .cpu  \n"
               ".cpu generic+crc  \n"
               "crc32w %w2, %w1, %w0  \n"
               ".set .cpu, old_cpu   \n"
               : "=r"(r) : "r"(crc), "r"((unsigned int)v)
              );
          return r;
      }

It results in:

Adding a leading dot results in:

My question is, how do I save and restore a symbol value for Binutils
2.2.4 and above on Aarch64?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to save and restore a symbol value in Aarch64?
  2017-04-20  9:16 How to save and restore a symbol value in Aarch64? Jeffrey Walton
@ 2017-04-20  9:27 ` Jeffrey Walton
  2017-04-20 10:03 ` Andreas Schwab
  1 sibling, 0 replies; 7+ messages in thread
From: Jeffrey Walton @ 2017-04-20  9:27 UTC (permalink / raw)
  To: Binutils

On Thu, Apr 20, 2017 at 5:16 AM, Jeffrey Walton <noloader@gmail.com> wrote:
> I'm trying to save (and restore) the original value of a symbol under
> Aarch64. The code runs on a variety of machines, and it looks like I
> need a solution for Binutils 2.24 and above.
>
> I know .set allows me to set the symbol value:
>
>     asm(".set .cpu, generic+crc \n" ... );
>
> I need the method to get the previous .cpu value and save it for the
> restore. About all I have been able to do is cause assembler errors.
> For example:
>
>     __inline unsigned int GCC_INLINE_ATTRIB
>     CRC32B(unsigned int crc, unsigned char v)
>     {
>           unsigned int r;
>           asm (".set old_cpu, .cpu  \n"
>                ".cpu generic+crc  \n"
>                "crc32w %w2, %w1, %w0  \n"
>                ".set .cpu, old_cpu   \n"
>                : "=r"(r) : "r"(crc), "r"((unsigned int)v)
>               );
>           return r;
>       }
>
> It results in:
>
> Adding a leading dot results in:
>
> My question is, how do I save and restore a symbol value for Binutils
> 2.2.4 and above on Aarch64?

Sorry about the truncated message. Here are the error messages.

For ".set old_cpu, .cpu  \n" (no leading dot):

/tmp/ccbAvT3u.s: Assembler messages:
/tmp/ccbAvT3u.s:32: Error: unknown mnemonic `old_cpu' -- `old_cpu .equ .cpu'
/tmp/ccbAvT3u.s:35: Error: unknown mnemonic `set' -- `set .cpu,old_cpu'

For ".set .old_cpu, .cpu  \n" (leading dot):

/tmp/cc0r6bvh.s: Assembler messages:
/tmp/cc0r6bvh.s:32: Error: unknown pseudo-op: `.old_cpu'

Here's the relevant AS manual section that discusses symbols:
http://tigcc.ticalc.org/doc/gnuasm.html#SEC45

Jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to save and restore a symbol value in Aarch64?
  2017-04-20  9:16 How to save and restore a symbol value in Aarch64? Jeffrey Walton
  2017-04-20  9:27 ` Jeffrey Walton
@ 2017-04-20 10:03 ` Andreas Schwab
  2017-04-20 10:55   ` Jeffrey Walton
  1 sibling, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2017-04-20 10:03 UTC (permalink / raw)
  To: Jeffrey Walton; +Cc: Binutils

On Apr 20 2017, Jeffrey Walton <noloader@gmail.com> wrote:

> I need the method to get the previous .cpu value

.cpu is not a symbol, it is a directive, so you cannot get its "value".
The effect of the directive is to modify the internal state of the
assembler, which is not directly accessible to the assembler input.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to save and restore a symbol value in Aarch64?
  2017-04-20 10:03 ` Andreas Schwab
@ 2017-04-20 10:55   ` Jeffrey Walton
  2017-04-20 12:35     ` Jiong Wang
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey Walton @ 2017-04-20 10:55 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Binutils

On Thu, Apr 20, 2017 at 6:02 AM, Andreas Schwab <schwab@linux-m68k.org> wrote:
> On Apr 20 2017, Jeffrey Walton <noloader@gmail.com> wrote:
>
>> I need the method to get the previous .cpu value
>
> .cpu is not a symbol, it is a directive, so you cannot get its "value".
> The effect of the directive is to modify the internal state of the
> assembler, which is not directly accessible to the assembler input.

Thanks Andreas.

Does '.set push, .cpu' and '.set pop, .cpu' work as {expected|desired}
for Aarch32 and Aarch64?

Sorry to have to ask. I tested code using push and pop, and its not
producing a warning or an error. I'm not sure if its wrong or a case
of the documentation being a bit stale.

Jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to save and restore a symbol value in Aarch64?
  2017-04-20 10:55   ` Jeffrey Walton
@ 2017-04-20 12:35     ` Jiong Wang
  2017-04-20 13:57       ` Jeffrey Walton
  2017-04-20 19:01       ` Jeffrey Walton
  0 siblings, 2 replies; 7+ messages in thread
From: Jiong Wang @ 2017-04-20 12:35 UTC (permalink / raw)
  To: noloader, Andreas Schwab; +Cc: Binutils



On 20/04/17 11:54, Jeffrey Walton wrote:
> On Thu, Apr 20, 2017 at 6:02 AM, Andreas Schwab <schwab@linux-m68k.org> wrote:
>> On Apr 20 2017, Jeffrey Walton <noloader@gmail.com> wrote:
>>
>>> I need the method to get the previous .cpu value
>> .cpu is not a symbol, it is a directive, so you cannot get its "value".
>> The effect of the directive is to modify the internal state of the
>> assembler, which is not directly accessible to the assembler input.
> Thanks Andreas.
>
> Does '.set push, .cpu' and '.set pop, .cpu' work as {expected|desired}
> for Aarch32 and Aarch64?

It will not work as you expected.
They are simply treating "push/pop" as symbols, and their value will be the string ".cpu".


Below is a purely work around for your reference that you can escape assembler's architecture requirement check.

__inline unsigned int GCC_INLINE_ATTRIB
CRC32B(unsigned int crc, unsigned char v)
{
         unsigned int r;
         asm (
                         "\t.set raw_x0, 0\n"
                         "\t.set raw_x1, 1\n"
                         "\t.set raw_x2, 2\n"
                         "\t.set raw_x3, 3\n"
                         "\t.set raw_x4, 4\n"
                         "\t.set raw_x5, 5\n"
                         "\t.set raw_x6, 6\n"
                         "\t.set raw_x7, 7\n"
                         "\t#crc32w %w2, %w1, %w0\n"
                         "\t.inst\t0x1ac04800 | (raw_%2) | (raw_%1 << 5) | (raw_%0 << 16)\n"
                         : "=r"(r) : "r"(crc), "r"((unsigned int)v)
             );
         return r;
}

Regards,
Jiong

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to save and restore a symbol value in Aarch64?
  2017-04-20 12:35     ` Jiong Wang
@ 2017-04-20 13:57       ` Jeffrey Walton
  2017-04-20 19:01       ` Jeffrey Walton
  1 sibling, 0 replies; 7+ messages in thread
From: Jeffrey Walton @ 2017-04-20 13:57 UTC (permalink / raw)
  To: Jiong Wang; +Cc: Andreas Schwab, Binutils

On Thu, Apr 20, 2017 at 8:35 AM, Jiong Wang <jiong.wang@foss.arm.com> wrote:
>
>
> On 20/04/17 11:54, Jeffrey Walton wrote:
>>
>> On Thu, Apr 20, 2017 at 6:02 AM, Andreas Schwab <schwab@linux-m68k.org>
>> wrote:
>>>
>>> On Apr 20 2017, Jeffrey Walton <noloader@gmail.com> wrote:
>>>
>>>> I need the method to get the previous .cpu value
>>>
>>> .cpu is not a symbol, it is a directive, so you cannot get its "value".
>>> The effect of the directive is to modify the internal state of the
>>> assembler, which is not directly accessible to the assembler input.
>>
>> Thanks Andreas.
>>
>> Does '.set push, .cpu' and '.set pop, .cpu' work as {expected|desired}
>> for Aarch32 and Aarch64?
>
> It will not work as you expected.
> They are simply treating "push/pop" as symbols, and their value will be the
> string ".cpu".

OK, thanks. My test case was bad. It needed to use a feature other
than NEON after the CRC. Trying to use AES caused the error I was
looking for.

> Below is a purely work around for your reference that you can escape
> assembler's architecture requirement check.
>
> __inline unsigned int GCC_INLINE_ATTRIB
> CRC32B(unsigned int crc, unsigned char v)
> {
>         unsigned int r;
>         asm (
>                         "\t.set raw_x0, 0\n"
>                         "\t.set raw_x1, 1\n"
>                         "\t.set raw_x2, 2\n"
>                         "\t.set raw_x3, 3\n"
>                         "\t.set raw_x4, 4\n"
>                         "\t.set raw_x5, 5\n"
>                         "\t.set raw_x6, 6\n"
>                         "\t.set raw_x7, 7\n"
>                         "\t#crc32w %w2, %w1, %w0\n"
>                         "\t.inst\t0x1ac04800 | (raw_%2) | (raw_%1 << 5) |
> (raw_%0 << 16)\n"
>                         : "=r"(r) : "r"(crc), "r"((unsigned int)v)
>             );
>         return r;
> }

Thanks Jiong. Wow, I'm fairly certain I would not have gotten that on my own.

If you get to the Baltimore MD or Washington DC area, then dinner and
cold beers are on me.

Jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to save and restore a symbol value in Aarch64?
  2017-04-20 12:35     ` Jiong Wang
  2017-04-20 13:57       ` Jeffrey Walton
@ 2017-04-20 19:01       ` Jeffrey Walton
  1 sibling, 0 replies; 7+ messages in thread
From: Jeffrey Walton @ 2017-04-20 19:01 UTC (permalink / raw)
  To: Jiong Wang; +Cc: Binutils

On Thu, Apr 20, 2017 at 8:35 AM, Jiong Wang <jiong.wang@foss.arm.com> wrote:
> On 20/04/17 11:54, Jeffrey Walton wrote:
>>
>> ...
>
> It will not work as you expected.
> They are simply treating "push/pop" as symbols, and their value will be the
> string ".cpu".
>
>
> Below is a purely work around for your reference that you can escape
> assembler's architecture requirement check.
>
> __inline unsigned int GCC_INLINE_ATTRIB
> CRC32B(unsigned int crc, unsigned char v)
> {
>         unsigned int r;
>         asm (
>                         "\t.set raw_x0, 0\n"
>                         "\t.set raw_x1, 1\n"
>                         "\t.set raw_x2, 2\n"
>                         "\t.set raw_x3, 3\n"
>                         "\t.set raw_x4, 4\n"
>                         "\t.set raw_x5, 5\n"
>                         "\t.set raw_x6, 6\n"
>                         "\t.set raw_x7, 7\n"
>                         "\t#crc32w %w2, %w1, %w0\n"
>                         "\t.inst\t0x1ac04800 | (raw_%2) | (raw_%1 << 5) |
> (raw_%0 << 16)\n"
>                         : "=r"(r) : "r"(crc), "r"((unsigned int)v)
>             );
>         return r;
> }

Thanks again Jiong. I needed one small change to make things work. I'm
posting it back to the list in case someone copies/pastes. The
operands needed to be reversed:

__inline unsigned int GCC_INLINE_ATTRIB
CRC32B(unsigned int crc, unsigned char v)
{
    volatile unsigned int res;
    asm (
         "\t" ".set reg_x0, 0\n"
         "\t" ".set reg_x1, 1\n"
         "\t" ".set reg_x2, 2\n"
         "\t" ".set reg_x3, 3\n"
         "\t" ".set reg_x4, 4\n"
         "\t" ".set reg_x5, 5\n"
         "\t" ".set reg_x6, 6\n"
         "\t" ".set reg_x7, 7\n"
         "\t" "#crc32w %w0, %w1, %w2\n"
         "\t" ".inst 0x1ac04800 | (reg_%2 << 16) | (reg_%1 << 5) | (reg_%0)\n"
         : "=r"(res) : "r"(crc), "r"(val)
    );
    return res;
}

Thanks again.

Jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-04-20 19:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-20  9:16 How to save and restore a symbol value in Aarch64? Jeffrey Walton
2017-04-20  9:27 ` Jeffrey Walton
2017-04-20 10:03 ` Andreas Schwab
2017-04-20 10:55   ` Jeffrey Walton
2017-04-20 12:35     ` Jiong Wang
2017-04-20 13:57       ` Jeffrey Walton
2017-04-20 19:01       ` Jeffrey Walton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).