* int_encoding.cc (read_unsigned_LEB_128): Replaced with inline
authorCary Coutant <ccoutant@google.com>
Wed, 25 Jan 2012 07:34:55 +0000 (07:34 +0000)
committerCary Coutant <ccoutant@google.com>
Wed, 25 Jan 2012 07:34:55 +0000 (07:34 +0000)
definition and ...
(read_unsigned_LEB_128_x): ... this new function.
(read_signed_LEB_128): Replaced with inline definition and ...
(read_signed_LEB_128_x): ... this new function.
* int_encoding.h  (read_unsigned_LEB_128_x): New function.
(read_unsigned_LEB_128): Add inline definition.
(read_signed_LEB_128_x): New function.
(read_signed_LEB_128): Add inline definition.
* testsuite/Makefile.am (leb128_unittest): New unit test.
* testsuite/Makefile.in: Regenerate.
* testsuite/leb128_unittest.cc: New unit test.

gold/ChangeLog
gold/int_encoding.cc
gold/int_encoding.h
gold/testsuite/Makefile.am
gold/testsuite/Makefile.in
gold/testsuite/leb128_unittest.cc [new file with mode: 0644]

index 813248b05e496adb082159005622d1c70de21f9a..8ff80480f764db60f576bdb70e82b8e532c7ada4 100644 (file)
@@ -1,3 +1,18 @@
+2012-01-24  Cary Coutant  <ccoutant@google.com>
+
+       * int_encoding.cc (read_unsigned_LEB_128): Replaced with inline
+       definition and ...
+       (read_unsigned_LEB_128_x): ... this new function.
+       (read_signed_LEB_128): Replaced with inline definition and ...
+       (read_signed_LEB_128_x): ... this new function.
+       * int_encoding.h  (read_unsigned_LEB_128_x): New function.
+       (read_unsigned_LEB_128): Add inline definition.
+       (read_signed_LEB_128_x): New function.
+       (read_signed_LEB_128): Add inline definition.
+       * testsuite/Makefile.am (leb128_unittest): New unit test.
+       * testsuite/Makefile.in: Regenerate.
+       * testsuite/leb128_unittest.cc: New unit test.
+
 2012-01-23  Ian Lance Taylor  <iant@google.com>
 
        PR gold/13617
index 6d635ac267954b095d54426354891fc0848b501f..78874779b2ffaf93a03b319d819bd0938a891b6b 100644 (file)
@@ -32,19 +32,20 @@ namespace gold {
 
 // Read an unsigned LEB128 number.  Each byte contains 7 bits of
 // information, plus one bit saying whether the number continues or
-// not.
+// not.  BYTE contains the first byte of the number, and is guaranteed
+// to have the continuation bit set.
 
 uint64_t
-read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
+read_unsigned_LEB_128_x(const unsigned char* buffer, size_t* len,
+                       unsigned char byte)
 {
-  uint64_t result = 0;
-  size_t num_read = 0;
-  unsigned int shift = 0;
-  unsigned char byte;
+  uint64_t result = static_cast<uint64_t>(byte & 0x7f);
+  size_t num_read = 1;
+  unsigned int shift = 7;
 
   do
     {
-      if (num_read >= 64 / 7) 
+      if (num_read > 64 / 7 + 1)
         {
           gold_warning(_("Unusually large LEB128 decoded, "
                         "debug information may be corrupted"));
@@ -64,18 +65,20 @@ read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
 
 // Read a signed LEB128 number.  These are like regular LEB128
 // numbers, except the last byte may have a sign bit set.
+// BYTE contains the first byte of the number, and is guaranteed
+// to have the continuation bit set.
 
 int64_t
-read_signed_LEB_128(const unsigned char* buffer, size_t* len)
+read_signed_LEB_128_x(const unsigned char* buffer, size_t* len,
+                     unsigned char byte)
 {
-  int64_t result = 0;
-  int shift = 0;
-  size_t num_read = 0;
-  unsigned char byte;
+  int64_t result = static_cast<uint64_t>(byte & 0x7f);
+  int shift = 7;
+  size_t num_read = 1;
 
   do
     {
-      if (num_read >= 64 / 7) 
+      if (num_read > 64 / 7 + 1)
         {
           gold_warning(_("Unusually large LEB128 decoded, "
                         "debug information may be corrupted"));
index 6485a931071058d8f7b6a29a382328e3b719fc3c..467d2244ce72c33ba1867df8883a49d3cf81f9b5 100644 (file)
@@ -38,16 +38,48 @@ namespace gold
 //
 
 // Read a ULEB 128 encoded integer from BUFFER.  Return the length of the
-// encoded integer at the location PLEN. 
+// encoded integer at the location PLEN.  The common case of a single-byte
+// value is handled inline, and multi-byte values are processed by the _x
+// routine, where BYTE is the first byte of the value.
 
 uint64_t
-read_unsigned_LEB_128(const unsigned char* buffer, size_t* plen);
+read_unsigned_LEB_128_x(const unsigned char* buffer, size_t* plen,
+                       unsigned char byte);
+
+inline uint64_t
+read_unsigned_LEB_128(const unsigned char* buffer, size_t* plen)
+{
+  unsigned char byte = *buffer++;
+
+  if ((byte & 0x80) != 0)
+    return read_unsigned_LEB_128_x(buffer, plen, byte);
+
+  *plen = 1;
+  return static_cast<uint64_t>(byte);
+}
 
 // Read an SLEB 128 encoded integer from BUFFER.  Return the length of the
-// encoded integer at the location PLEN. 
+// encoded integer at the location PLEN.  The common case of a single-byte
+// value is handled inline, and multi-byte values are processed by the _x
+// routine, where BYTE is the first byte of the value.
 
 int64_t
-read_signed_LEB_128(const unsigned char* buffer, size_t* plen);
+read_signed_LEB_128_x(const unsigned char* buffer, size_t* plen,
+                     unsigned char byte);
+
+inline int64_t
+read_signed_LEB_128(const unsigned char* buffer, size_t* plen)
+{
+  unsigned char byte = *buffer++;
+
+  if ((byte & 0x80) != 0)
+    return read_signed_LEB_128_x(buffer, plen, byte);
+
+  *plen = 1;
+  if (byte & 0x40)
+    return -(static_cast<int64_t>(1) << 7) | static_cast<int64_t>(byte);
+  return static_cast<int64_t>(byte);
+}
 
 // Write a ULEB 128 encoded VALUE to BUFFER.
 
index 794dac8fc28a9789199fe5b68befdb87c7b23c48..c7209f31a9ec7160bc7140f6403a3c19db470c3a 100644 (file)
@@ -94,6 +94,9 @@ object_unittest_SOURCES = object_unittest.cc
 check_PROGRAMS += binary_unittest
 binary_unittest_SOURCES = binary_unittest.cc
 
+check_PROGRAMS += leb128_unittest
+leb128_unittest_SOURCES = leb128_unittest.cc
+
 endif NATIVE_OR_CROSS_LINKER
 
 # ---------------------------------------------------------------------
index af62c8d1dd8e8715531d15652d6f92e006124565..c5c1ae6cb3e83243cfbc8511176453f9531ea119 100644 (file)
@@ -54,7 +54,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
        $(am__EXEEXT_34) $(am__EXEEXT_35) $(am__EXEEXT_36) \
        $(am__EXEEXT_37) $(am__EXEEXT_38)
 @NATIVE_OR_CROSS_LINKER_TRUE@am__append_1 = object_unittest \
-@NATIVE_OR_CROSS_LINKER_TRUE@  binary_unittest
+@NATIVE_OR_CROSS_LINKER_TRUE@  binary_unittest leb128_unittest
 
 # Test --detect-odr-violations
 
@@ -646,7 +646,8 @@ am_libgoldtest_a_OBJECTS = test.$(OBJEXT) testmain.$(OBJEXT) \
        testfile.$(OBJEXT)
 libgoldtest_a_OBJECTS = $(am_libgoldtest_a_OBJECTS)
 @NATIVE_OR_CROSS_LINKER_TRUE@am__EXEEXT_1 = object_unittest$(EXEEXT) \
-@NATIVE_OR_CROSS_LINKER_TRUE@  binary_unittest$(EXEEXT)
+@NATIVE_OR_CROSS_LINKER_TRUE@  binary_unittest$(EXEEXT) \
+@NATIVE_OR_CROSS_LINKER_TRUE@  leb128_unittest$(EXEEXT)
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_2 = icf_virtual_function_folding_test$(EXEEXT) \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@ basic_test$(EXEEXT) \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@ basic_pic_test$(EXEEXT)
@@ -1221,6 +1222,13 @@ justsyms_exec_LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
 large_OBJECTS = $(am_large_OBJECTS)
 large_LINK = $(CCLD) $(large_CFLAGS) $(CFLAGS) $(large_LDFLAGS) \
        $(LDFLAGS) -o $@
+@NATIVE_OR_CROSS_LINKER_TRUE@am_leb128_unittest_OBJECTS =  \
+@NATIVE_OR_CROSS_LINKER_TRUE@  leb128_unittest.$(OBJEXT)
+leb128_unittest_OBJECTS = $(am_leb128_unittest_OBJECTS)
+leb128_unittest_LDADD = $(LDADD)
+leb128_unittest_DEPENDENCIES = libgoldtest.a ../libgold.a \
+       ../../libiberty/libiberty.a $(am__DEPENDENCIES_1) \
+       $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
 local_labels_test_SOURCES = local_labels_test.c
 local_labels_test_OBJECTS = local_labels_test.$(OBJEXT)
 local_labels_test_LDADD = $(LDADD)
@@ -1701,7 +1709,8 @@ SOURCES = $(libgoldtest_a_SOURCES) basic_pic_test.c basic_pie_test.c \
        incremental_test_3.c incremental_test_4.c incremental_test_5.c \
        incremental_test_6.c $(initpri1_SOURCES) $(initpri2_SOURCES) \
        $(initpri3a_SOURCES) $(initpri3b_SOURCES) $(justsyms_SOURCES) \
-       $(justsyms_exec_SOURCES) $(large_SOURCES) local_labels_test.c \
+       $(justsyms_exec_SOURCES) $(large_SOURCES) \
+       $(leb128_unittest_SOURCES) local_labels_test.c \
        many_sections_r_test.c $(many_sections_test_SOURCES) \
        $(object_unittest_SOURCES) permission_test.c plugin_test_1.c \
        plugin_test_2.c plugin_test_3.c plugin_test_4.c \
@@ -2014,6 +2023,7 @@ LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \
 
 @NATIVE_OR_CROSS_LINKER_TRUE@object_unittest_SOURCES = object_unittest.cc
 @NATIVE_OR_CROSS_LINKER_TRUE@binary_unittest_SOURCES = binary_unittest.cc
+@NATIVE_OR_CROSS_LINKER_TRUE@leb128_unittest_SOURCES = leb128_unittest.cc
 @GCC_TRUE@@NATIVE_LINKER_TRUE@constructor_test_SOURCES = constructor_test.cc
 @GCC_TRUE@@NATIVE_LINKER_TRUE@constructor_test_DEPENDENCIES = gcctestdir/ld
 @GCC_TRUE@@NATIVE_LINKER_TRUE@constructor_test_LDFLAGS = -Bgcctestdir/
@@ -2942,6 +2952,9 @@ justsyms_exec$(EXEEXT): $(justsyms_exec_OBJECTS) $(justsyms_exec_DEPENDENCIES)
 large$(EXEEXT): $(large_OBJECTS) $(large_DEPENDENCIES) 
        @rm -f large$(EXEEXT)
        $(large_LINK) $(large_OBJECTS) $(large_LDADD) $(LIBS)
+leb128_unittest$(EXEEXT): $(leb128_unittest_OBJECTS) $(leb128_unittest_DEPENDENCIES) 
+       @rm -f leb128_unittest$(EXEEXT)
+       $(CXXLINK) $(leb128_unittest_OBJECTS) $(leb128_unittest_LDADD) $(LIBS)
 @GCC_FALSE@local_labels_test$(EXEEXT): $(local_labels_test_OBJECTS) $(local_labels_test_DEPENDENCIES) 
 @GCC_FALSE@    @rm -f local_labels_test$(EXEEXT)
 @GCC_FALSE@    $(LINK) $(local_labels_test_OBJECTS) $(local_labels_test_LDADD) $(LIBS)
@@ -3322,6 +3335,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/justsyms_1.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/justsyms_exec.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/large-large.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/leb128_unittest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/local_labels_test.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/many_sections_r_test.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/many_sections_test.Po@am__quote@
@@ -3749,6 +3763,8 @@ object_unittest.log: object_unittest$(EXEEXT)
        @p='object_unittest$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 binary_unittest.log: binary_unittest$(EXEEXT)
        @p='binary_unittest$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
+leb128_unittest.log: leb128_unittest$(EXEEXT)
+       @p='leb128_unittest$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 icf_virtual_function_folding_test.log: icf_virtual_function_folding_test$(EXEEXT)
        @p='icf_virtual_function_folding_test$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 basic_test.log: basic_test$(EXEEXT)
diff --git a/gold/testsuite/leb128_unittest.cc b/gold/testsuite/leb128_unittest.cc
new file mode 100644 (file)
index 0000000..05c7093
--- /dev/null
@@ -0,0 +1,88 @@
+// leb_unittest.cc -- test read_signed_LEB_128 and read_unsigned_LEB_128 
+
+// Copyright 2012 Free Software Foundation, Inc.
+// Written by Cary Coutant <ccoutant@google.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+#include "gold.h"
+
+#include <sys/types.h>
+
+#include "int_encoding.h"
+
+#include "test.h"
+
+namespace gold_testsuite
+{
+
+using namespace gold;
+
+bool
+Leb128_test(Test_report*)
+{
+  size_t len;
+
+  // Unsigned tests.
+  static unsigned char u1[] = { 0 };           // 0
+  static unsigned char u2[] = { 1 };           // 1
+  static unsigned char u3[] = { 126 };         // 126
+  static unsigned char u4[] = { 127 };         // 127
+  static unsigned char u5[] = { 0x80+0, 1 };   // 128
+  static unsigned char u6[] = { 0x80+1, 1 };   // 129
+  static unsigned char u7[] = { 0x80+57, 100 };        // 12857
+  static unsigned char u8[] = { 0x80, 0x80, 0x80, 0x80,
+                               0x80, 0x80, 0x80, 0x80,
+                               0x80, 1};       // 1ULL << 63
+
+  // Signed tests.
+  static unsigned char s1[] = { 0 };           // 0
+  static unsigned char s2[] = { 1 };           // 1
+  static unsigned char s3[] = { 0x7e };                // -2
+  static unsigned char s4[] = { 0x80+127, 0 }; // 127
+  static unsigned char s5[] = { 0x80+1, 0x7f };        // -127
+  static unsigned char s6[] = { 0x80+0, 1 };   // 128
+  static unsigned char s7[] = { 0x80+0, 0x7f };        // -128
+  static unsigned char s8[] = { 0x80+1, 1 };   // 129
+  static unsigned char s9[] = { 0xff, 0x7e };  // -129
+
+  CHECK(read_unsigned_LEB_128(u1, &len) == 0 && len == sizeof(u1));
+  CHECK(read_unsigned_LEB_128(u2, &len) == 1 && len == sizeof(u2));
+  CHECK(read_unsigned_LEB_128(u3, &len) == 126 && len == sizeof(u3));
+  CHECK(read_unsigned_LEB_128(u4, &len) == 127 && len == sizeof(u4));
+  CHECK(read_unsigned_LEB_128(u5, &len) == 128 && len == sizeof(u5));
+  CHECK(read_unsigned_LEB_128(u6, &len) == 129 && len == sizeof(u6));
+  CHECK(read_unsigned_LEB_128(u7, &len) == 12857 && len == sizeof(u7));
+  CHECK(read_unsigned_LEB_128(u8, &len) == (1ULL << 63) && len == sizeof(u8));
+
+  CHECK(read_signed_LEB_128(s1, &len) == 0 && len == sizeof(s1));
+  CHECK(read_signed_LEB_128(s2, &len) == 1 && len == sizeof(s2));
+  CHECK(read_signed_LEB_128(s3, &len) == -2 && len == sizeof(s3));
+  CHECK(read_signed_LEB_128(s4, &len) == 127 && len == sizeof(s4));
+  CHECK(read_signed_LEB_128(s5, &len) == -127 && len == sizeof(s5));
+  CHECK(read_signed_LEB_128(s6, &len) == 128 && len == sizeof(s6));
+  CHECK(read_signed_LEB_128(s7, &len) == -128 && len == sizeof(s7));
+  CHECK(read_signed_LEB_128(s8, &len) == 129 && len == sizeof(s8));
+  CHECK(read_signed_LEB_128(s9, &len) == -129 && len == sizeof(s9));
+
+  return true;
+}
+
+Register_test leb128_register("LEB128", Leb128_test);
+
+} // End namespace gold_testsuite.