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-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
// 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"));
// 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"));
//
// 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.
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
# ---------------------------------------------------------------------
$(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
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)
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)
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 \
@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/
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)
@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@
@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)
--- /dev/null
+// 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.