[Dwarf-Discuss] dwarf stack operator for byte swap.

Robinson, Paul paul.robinson at sony.com
Mon Oct 28 06:53:26 PDT 2019


Hello Chirag,

Regarding a byte-swap operation, it seems that you have a reasonable use-case on a bi-endian machine.  Feel free to request a new operator on the "public comments" page at http://dwarfstd.org/Comment.php

Note that a byte-swap operator would swap all bytes in the top-of-stack value, which on your 64-bit machine would of course be a 64-bit value.  As you want a 32-bit swapped value, you would still need to do a shift afterward, but even so, "DW_OP_byte_swap DW_OP_const1u 32 DW_OP_shr" would be considerably shorter than what you have to do now.

Of course a new operator would be introduced in a new DWARF revision, which is likely to be years away.  In the meantime let me suggest a shorter expression for doing the byte-swap operation.  The book "Hacker's Delight" shows a straightforward 32-bit byte swap with masks no wider than 16 bits, as follows:
               x = (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24);
Your 64-bit machine will of course use 64-bit values on the expression stack, so to keep the result "32-bit clean" we want to do one additional mask:
               x = ((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24);
Translating this into a DWARF expression, I get the following:
               DW_OP_dup, DW_OP_const1u 0xff, DW_OP_and, DW_OP_lit24, DW_OP_shl, DW_OP_swap, DW_OP_dup, DW_OP_const2u 0xff00, DW_OP_and, DW_OP_lit8, DW_OP_shl, DW_OP_swap, DW_OP_dup, DW_OP_lit8, DW_OP_shr, DW_OP_const2u 0xff00, DW_OP_and, DW_OP_swap, DW_OP_lit24, DW_OP_shr, DW_OP_or, DW_OP_or, DW_OP_or

I hope this is helpful to you.
--paulr

From: Dwarf-Discuss <dwarf-discuss-bounces at lists.dwarfstd.org> On Behalf Of Chirag Patel via Dwarf-Discuss
Sent: Monday, October 28, 2019 12:47 AM
To: dwarf-discuss at lists.dwarfstd.org
Subject: [Dwarf-Discuss] dwarf stack operator for byte swap.

Hello Dwarf experts.

I am currently working trying to encode dwarf of binaries with having bi-endian variables marked with DW_AT_endianity attribute.
The location calculation for some  variable depends on other variable with different endianity, also the value of this other variable is known at runtime.

At the moment I am using location list to calculate the correct location of first variable and list of dwarf operators to reverse the endianity of variable "__gbloffset__" in below case (I only needed lower 32 bits on 64 bit machine).

0x000001e5: DW_TAG_base_type
 DW_AT_byte_size  (0x04)
  DW_AT_encoding  (DW_ATE_signed)
                DW_AT_name      ("int")
                DW_AT_endianity (DW_END_big)
...
0x00000057:   DW_TAG_variable
                DW_AT_name      ("__gbloffset__")
                DW_AT_type      (0x000001e5 "int")
                DW_AT_external  (true)
                DW_AT_decl_file ("...")
                DW_AT_decl_line (8)
                DW_AT_location  (DW_OP_addr 0) // pre linkage
                DW_AT_linkage_name      ("_gblsection__")

0x00000170:   DW_TAG_variable
                DW_AT_name      ("VAR1")
                DW_AT_type      (0x0000010b "fixed.dec.display.72")
                DW_AT_decl_file ("...")
                DW_AT_decl_line (10)
                DW_AT_location  (DW_OP_addr 0x0, DW_OP_call4 0x57, DW_OP_deref_size, 4,
DW_OP_dup, DW_OP_constu 0xff, DW_OP_lit0, DW_OP_shl, DW_OP_and, DW_OP_lit24, DW_OP_shl, DW_OP_swap, DW_OP_dup, DW_OP_constu 0xff, DW_OP_lit8, DW_OP_shl, DW_OP_and, DW_OP_lit8, DW_OP_shl, DW_OP_swap, DW_OP_dup, DW_OP_constu 0xff, DW_OP_lit16, DW_OP_shl, DW_OP_and, DW_OP_lit8, DW_OP_shr, DW_OP_swap, DW_OP_constu 0xff, DW_OP_lit24, DW_OP_shl, DW_OP_and, DW_OP_lit24, DW_OP_shr, DW_OP_swap, DW_OP_or, DW_OP_or, DW_OP_or, DW_OP_plus)
                DW_AT_linkage_name      ("VAR1")


In above snippet of dwarf dump, I am using yellow highlighted list of operators to swap the bytes.
I think there should be a support for DW_OP_byte_swap simple operator to accomplice this simple task. Does this idea looks like it can be useful? Is there any specific reason why dwarf spec does not have it or I am missing something subtle.

I hope I conveyed the idea properly, apologies in advanced as English is not my first language.

Thanks and regards,

Chirag Patel
Software Engineer | Raincode Labs India
Tel: (+91) 080 41159811
Mob: (+91) 9049336744
www.raincodelabs.com<http://www.raincodelabs.com/>
[linkedin-button]<https://in.linkedin.com/in/chirag-patel->

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dwarfstd.org/pipermail/dwarf-discuss-dwarfstd.org/attachments/20191028/f696de51/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 7398 bytes
Desc: image002.png
URL: <http://lists.dwarfstd.org/pipermail/dwarf-discuss-dwarfstd.org/attachments/20191028/f696de51/attachment-0001.png>


More information about the Dwarf-Discuss mailing list