public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: dwz@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] Add write_sleb129 and size_of_sleb128 for writing DW_FORM_implicit_const
Date: Mon, 28 Sep 2020 09:36:47 +0200	[thread overview]
Message-ID: <20200928073647.4629-1-mark@klomp.org> (raw)

We were writing out the DW_FORM_implicit_const value as uleb128,
but it should be written out as sleb128 (as we already read it).
I'll merge this into my experimental DW_FORM_implicit_const patch.


The DW_FORM_implicit_const value is a signed 64bit sleb128 number.

	* dwz.c (write_sleb128): New macro.
	(size_of_sleb128): New function.
---
 dwz.c | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)

diff --git a/dwz.c b/dwz.c
index d730f98..e772954 100644
--- a/dwz.c
+++ b/dwz.c
@@ -399,6 +399,25 @@ typedef struct
     }					\
   while (0)
 
+#define write_sleb128(ptr, val)					\
+  do								\
+    {								\
+      int64_t valv = (val);					\
+      bool more;						\
+      do							\
+	{							\
+	  unsigned char c = (valv & 0x7f);			\
+	  valv >>= 7;						\
+	  more = !((((valv == 0) && ((c & 0x40) == 0))		\
+		    || ((valv == -1) && ((c & 0x40) != 0))));	\
+	  if (more)						\
+	    c |= 0x80;						\
+	  *ptr++ = c;						\
+	}							\
+      while (more);						\
+    }								\
+  while (0)
+
 /* Macro to skip a uleb128 or sleb128 number and update ptr to the end of the
    number.  */
 #define skip_leb128(ptr) \
@@ -8991,6 +9010,24 @@ size_of_uleb128 (uint64_t val)
   return size;
 }
 
+/* Return number of bytes needed to encode VAL using
+   sleb128.  */
+static unsigned int
+size_of_sleb128 (int64_t val)
+{
+  unsigned int size = 0;
+  unsigned char b;
+  do
+    {
+      b = (val & 0x7f);
+      val >>= 7;
+      size++;
+    }
+  while (!(((val == 0) && ((b & 0x40) == 0))
+	   || ((val == -1) && ((b & 0x40) != 0))));
+  return size;
+}
+
 /* Hash table mapping original file IDs to new ids.  */
 static htab_t line_htab;
 /* Current new maximum file ID.  */
@@ -11011,7 +11048,7 @@ compute_abbrevs (DSO *dso)
 		      uint64_t value = arr[i]->attr[j].value;
 		      arr[i]->attr[j].value = line_htab_lookup (cu, value);
 		    }
-		  abbrev_size += size_of_uleb128 (arr[i]->attr[j].value);
+		  abbrev_size += size_of_sleb128 (arr[i]->attr[j].value);
 		}
 	    }
 	  abbrev_size += 2;
@@ -11104,7 +11141,7 @@ write_abbrev (void)
 	      write_uleb128 (ptr, arr[i]->attr[j].attr);
 	      write_uleb128 (ptr, arr[i]->attr[j].form);
 	      if (arr[i]->attr[j].form == DW_FORM_implicit_const)
-		write_uleb128 (ptr, arr[i]->attr[j].value);
+		write_sleb128 (ptr, arr[i]->attr[j].value);
 	    }
 	  *ptr++ = 0;
 	  *ptr++ = 0;
-- 
2.18.4


             reply	other threads:[~2020-09-28  7:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-28  7:36 Mark Wielaard [this message]
2020-09-29  8:24 ` Jakub Jelinek
2020-09-29 18:19   ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200928073647.4629-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=dwz@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).