Extend the DWARF library to support .debug_info section.

Change-Id: I9916abd8db227e7a73a3311294e675be5222a709
diff --git a/runtime/leb128.h b/runtime/leb128.h
index d36b690..2e27b8e 100644
--- a/runtime/leb128.h
+++ b/runtime/leb128.h
@@ -136,6 +136,19 @@
   dest->push_back(out);
 }
 
+// Overwrite encoded Leb128 with a new value. The new value must be less than
+// or equal to the old value to ensure that it fits the allocated space.
+static inline void UpdateUnsignedLeb128(uint8_t* dest, uint32_t value) {
+  const uint8_t* old_end = dest;
+  uint32_t old_value = DecodeUnsignedLeb128(&old_end);
+  DCHECK_LE(value, old_value);
+  for (uint8_t* end = EncodeUnsignedLeb128(dest, value); end < old_end; end++) {
+    // Use longer encoding than necessary to fill the allocated space.
+    end[-1] |= 0x80;
+    end[0] = 0;
+  }
+}
+
 static inline uint8_t* EncodeSignedLeb128(uint8_t* dest, int32_t value) {
   uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
   uint8_t out = value & 0x7f;
diff --git a/runtime/leb128_test.cc b/runtime/leb128_test.cc
index 5d157dc..87e13ff 100644
--- a/runtime/leb128_test.cc
+++ b/runtime/leb128_test.cc
@@ -252,6 +252,25 @@
   EXPECT_EQ(data_size, static_cast<size_t>(encoded_data_ptr - encoded_data));
 }
 
+TEST(Leb128Test, UnsignedUpdate) {
+  for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
+    for (size_t j = 0; j < arraysize(uleb128_tests); ++j) {
+      uint32_t old_value = uleb128_tests[i].decoded;
+      uint32_t new_value = uleb128_tests[j].decoded;
+      // We can only make the encoded value smaller.
+      if (new_value <= old_value) {
+        uint8_t encoded_data[5];
+        uint8_t* old_end = EncodeUnsignedLeb128(encoded_data, old_value);
+        UpdateUnsignedLeb128(encoded_data, new_value);
+        const uint8_t* new_end = encoded_data;
+        EXPECT_EQ(DecodeUnsignedLeb128(&new_end), new_value);
+        // Even if the new value needs fewer bytes, we should fill the space.
+        EXPECT_EQ(new_end, old_end);
+      }
+    }
+  }
+}
+
 TEST(Leb128Test, Speed) {
   std::unique_ptr<Histogram<uint64_t>> enc_hist(new Histogram<uint64_t>("Leb128EncodeSpeedTest", 5));
   std::unique_ptr<Histogram<uint64_t>> dec_hist(new Histogram<uint64_t>("Leb128DecodeSpeedTest", 5));