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

Chirag Patel Chirag at raincode.com
Tue Oct 29 04:40:12 PDT 2019


Hello,

Thank you all for prompt reply.

@Robinson, Paul<mailto:paul.robinson at sony.com>,
Thanks for the shorter list of operators, but as Michael has suggested, i have added a vendor specific operator for that in llvm/lldb(fork of our branch anyways).
the operator, pops the stack entry and byte reverses the value and pushes it back on the stack.

@Ron Brender<mailto:ron.brender at gmail.com>
The operator that i was suggesting, pops the top of the stack, reverses the bytes and pushes it on the stack(like neg operator does for negation).(on second thought the byte_rev would be a better name)
About the other cases you suggested, i sure hope others can help to zero in for perfect
design to cover wide range.

@Michael Eager<mailto:eager at eagercon.com>
Thanks, i have tried the same for our fork of llvm/lldb branch and it seems to be working well(for our case :). https://gitlab.phidani.be/Chirag.Patel

Thank you all for the helpful comments. i will open a dwarf enhancement request soon.

Best Regards.





Chirag Patel

Software Engineer | Raincode Labs India

Tel: (+91) 080 41159811

Mob: (+91) 9049336744

<http://www.raincodelabs.com/>
<https://www.linkedin.com/in/chirag-patel-/>

[cid:68b118f1-419e-4f8e-a7ba-1d94b0379f85]


________________________________
From: Michael Eager <eager at eagercon.com>
Sent: 28 October 2019 20:37
To: Robinson, Paul <paul.robinson at sony.com>; Chirag Patel <Chirag at raincode.com>
Cc: dwarf-discuss at lists.dwarfstd.org <dwarf-discuss at lists.dwarfstd.org>
Subject: Re: [Dwarf-Discuss] dwarf stack operator for byte swap.

Hi Chirag --

A byte-swap operator would be a reasonable addition to the DWARF
interpreter.

In addition to writing a proposal as Paul suggests, one option I would
recommend you consider is to create a user-defined operation to perform
this byte-swap operation and give it a value in the range between
DW_OP_lo_user and DW_OP_hi_user.  Of course, this user-defined would
need to be implemented not only in the producer (i.e., compiler) but in
any consumers (i.e., debugger).

When a new version of the DWARF Standard is released, this operation
would be assigned a new value.

On 10/28/19 6:53 AM, Robinson, Paul via Dwarf-Discuss wrote:
> 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> <http://www.raincodelabs.com/>__
>
> linkedin-button <https://in.linkedin.com/in/chirag-patel->
>
>
> _______________________________________________
> Dwarf-Discuss mailing list
> Dwarf-Discuss at lists.dwarfstd.org
> http://lists.dwarfstd.org/listinfo.cgi/dwarf-discuss-dwarfstd.org
>

--
Michael Eager    eager at eagercon.com
1960 Park Blvd., Palo Alto, CA 94306
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dwarfstd.org/pipermail/dwarf-discuss-dwarfstd.org/attachments/20191029/86db0cfe/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Outlook-lrost1ey.png
Type: image/png
Size: 22419 bytes
Desc: Outlook-lrost1ey.png
URL: <http://lists.dwarfstd.org/pipermail/dwarf-discuss-dwarfstd.org/attachments/20191029/86db0cfe/attachment-0001.png>


More information about the Dwarf-Discuss mailing list