Add dex file intermediate representation

Add an intermediate representation, constructed from the DexFile
utilities, that can be used to optimize the layout and re-emit dex
files.

BUG: b/29921113
Change-Id: I62c7a2bf98320eb73e6c7f8ffa0fff0f392bf32d
TEST: TODO: Add dex files to compare output with dexdump
diff --git a/Android.mk b/Android.mk
index 4dc84c4..c4c765b 100644
--- a/Android.mk
+++ b/Android.mk
@@ -80,6 +80,7 @@
 include $(art_path)/runtime/simulator/Android.mk
 include $(art_path)/compiler/Android.mk
 include $(art_path)/dexdump/Android.mk
+include $(art_path)/dexlayout/Android.mk
 include $(art_path)/dexlist/Android.mk
 include $(art_path)/dex2oat/Android.mk
 include $(art_path)/disassembler/Android.mk
diff --git a/dexlayout/Android.mk b/dexlayout/Android.mk
new file mode 100755
index 0000000..3095866
--- /dev/null
+++ b/dexlayout/Android.mk
@@ -0,0 +1,50 @@
+# Copyright (C) 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# TODO(sehr): Art-i-fy this makefile
+
+LOCAL_PATH:= $(call my-dir)
+
+dexlayout_src_files := dexlayout_main.cc dexlayout.cc dex_ir.cc
+dexlayout_c_includes := art/runtime
+dexlayout_libraries := libart
+
+##
+## Build the device command line tool dexlayout.
+##
+
+ifneq ($(SDK_ONLY),true)  # SDK_only doesn't need device version
+include $(CLEAR_VARS)
+LOCAL_CPP_EXTENSION := cc
+LOCAL_SRC_FILES := $(dexlayout_src_files)
+LOCAL_C_INCLUDES := $(dexlayout_c_includes)
+LOCAL_CFLAGS += -Wall
+LOCAL_SHARED_LIBRARIES += $(dexlayout_libraries)
+LOCAL_MODULE := dexlayout
+include $(BUILD_EXECUTABLE)
+endif # !SDK_ONLY
+
+##
+## Build the host command line tool dexlayout.
+##
+
+include $(CLEAR_VARS)
+LOCAL_CPP_EXTENSION := cc
+LOCAL_SRC_FILES := $(dexlayout_src_files)
+LOCAL_C_INCLUDES := $(dexlayout_c_includes)
+LOCAL_CFLAGS += -Wall
+LOCAL_SHARED_LIBRARIES += $(dexlayout_libraries)
+LOCAL_MODULE := dexlayout
+LOCAL_MULTILIB := $(ART_MULTILIB_OVERRIDE_host)
+include $(BUILD_HOST_EXECUTABLE)
diff --git a/dexlayout/dex_ir.cc b/dexlayout/dex_ir.cc
new file mode 100644
index 0000000..40e60f7
--- /dev/null
+++ b/dexlayout/dex_ir.cc
@@ -0,0 +1,407 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Implementation file of the dex file intermediate representation.
+ *
+ * Utilities for reading dex files into an internal representation,
+ * manipulating them, and writing them out.
+ */
+
+#include "dex_ir.h"
+
+#include <map>
+#include <vector>
+
+#include "dex_file.h"
+#include "dex_file-inl.h"
+#include "utils.h"
+
+namespace art {
+namespace dex_ir {
+
+namespace {
+static uint64_t ReadVarWidth(const uint8_t** data, uint8_t length, bool sign_extend) {
+  uint64_t value = 0;
+  for (uint32_t i = 0; i <= length; i++) {
+    value |= static_cast<uint64_t>(*(*data)++) << (i * 8);
+  }
+  if (sign_extend) {
+    int shift = (7 - length) * 8;
+    return (static_cast<int64_t>(value) << shift) >> shift;
+  }
+  return value;
+}
+
+static bool GetPositionsCb(void* context, const DexFile::PositionInfo& entry) {
+  DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context);
+  std::vector<std::unique_ptr<PositionInfo>>& positions = debug_info->GetPositionInfo();
+  positions.push_back(std::unique_ptr<PositionInfo>(new PositionInfo(entry.address_, entry.line_)));
+  return false;
+}
+
+static void GetLocalsCb(void* context, const DexFile::LocalInfo& entry) {
+  DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context);
+  std::vector<std::unique_ptr<LocalInfo>>& locals = debug_info->GetLocalInfo();
+  const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
+  locals.push_back(std::unique_ptr<LocalInfo>(
+      new LocalInfo(entry.name_, entry.descriptor_, signature, entry.start_address_,
+                    entry.end_address_, entry.reg_)));
+}
+}  // namespace
+
+Header::Header(const DexFile& dex_file) : dex_file_(dex_file) {
+  const DexFile::Header& disk_header = dex_file.GetHeader();
+  memcpy(magic_, disk_header.magic_, sizeof(magic_));
+  checksum_ = disk_header.checksum_;
+  // TODO(sehr): clearly the signature will need to be recomputed before dumping.
+  memcpy(signature_, disk_header.signature_, sizeof(signature_));
+  endian_tag_ = disk_header.endian_tag_;
+  file_size_ = disk_header.file_size_;
+  header_size_ = disk_header.header_size_;
+  link_size_ = disk_header.link_size_;
+  link_offset_ = disk_header.link_off_;
+  data_size_ = disk_header.data_size_;
+  data_offset_ = disk_header.data_off_;
+  // Walk the rest of the header fields.
+  string_ids_.SetOffset(disk_header.string_ids_off_);
+  for (uint32_t i = 0; i < dex_file_.NumStringIds(); ++i) {
+    string_ids_.AddWithPosition(i, new StringId(dex_file_.GetStringId(i), *this));
+  }
+  type_ids_.SetOffset(disk_header.type_ids_off_);
+  for (uint32_t i = 0; i < dex_file_.NumTypeIds(); ++i) {
+    type_ids_.AddWithPosition(i, new TypeId(dex_file_.GetTypeId(i), *this));
+  }
+  proto_ids_.SetOffset(disk_header.proto_ids_off_);
+  for (uint32_t i = 0; i < dex_file_.NumProtoIds(); ++i) {
+    proto_ids_.AddWithPosition(i, new ProtoId(dex_file_.GetProtoId(i), *this));
+  }
+  field_ids_.SetOffset(disk_header.field_ids_off_);
+  for (uint32_t i = 0; i < dex_file_.NumFieldIds(); ++i) {
+    field_ids_.AddWithPosition(i, new FieldId(dex_file_.GetFieldId(i), *this));
+  }
+  method_ids_.SetOffset(disk_header.method_ids_off_);
+  for (uint32_t i = 0; i < dex_file_.NumMethodIds(); ++i) {
+    method_ids_.AddWithPosition(i, new MethodId(dex_file_.GetMethodId(i), *this));
+  }
+  class_defs_.SetOffset(disk_header.class_defs_off_);
+  for (uint32_t i = 0; i < dex_file_.NumClassDefs(); ++i) {
+    class_defs_.AddWithPosition(i, new ClassDef(dex_file_.GetClassDef(i), *this));
+  }
+}
+
+ArrayItem::ArrayItem(Header& header, const uint8_t** data, uint8_t type, uint8_t length) {
+  Read(header, data, type, length);
+}
+
+ArrayItem::ArrayItem(Header& header, const uint8_t** data) {
+  const uint8_t encoded_value = *(*data)++;
+  Read(header, data, encoded_value & 0x1f, encoded_value >> 5);
+}
+
+void ArrayItem::Read(Header& header, const uint8_t** data, uint8_t type, uint8_t length) {
+  type_ = type;
+  switch (type_) {
+    case DexFile::kDexAnnotationByte:
+      item_.byte_val_ = static_cast<int8_t>(ReadVarWidth(data, length, false));
+      break;
+    case DexFile::kDexAnnotationShort:
+      item_.short_val_ = static_cast<int16_t>(ReadVarWidth(data, length, true));
+      break;
+    case DexFile::kDexAnnotationChar:
+      item_.char_val_ = static_cast<uint16_t>(ReadVarWidth(data, length, false));
+      break;
+    case DexFile::kDexAnnotationInt:
+      item_.int_val_ = static_cast<int32_t>(ReadVarWidth(data, length, true));
+      break;
+    case DexFile::kDexAnnotationLong:
+      item_.long_val_ = static_cast<int64_t>(ReadVarWidth(data, length, true));
+      break;
+    case DexFile::kDexAnnotationFloat: {
+      // Fill on right.
+      union {
+        float f;
+        uint32_t data;
+      } conv;
+      conv.data = static_cast<uint32_t>(ReadVarWidth(data, length, false)) << (3 - length) * 8;
+      item_.float_val_ = conv.f;
+      break;
+    }
+    case DexFile::kDexAnnotationDouble: {
+      // Fill on right.
+      union {
+        double d;
+        uint64_t data;
+      } conv;
+      conv.data = ReadVarWidth(data, length, false) << (7 - length) * 8;
+      item_.double_val_ = conv.d;
+      break;
+    }
+    case DexFile::kDexAnnotationString: {
+      const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
+      item_.string_val_ = header.StringIds()[string_index].get();
+      break;
+    }
+    case DexFile::kDexAnnotationType: {
+      const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
+      item_.string_val_ = header.TypeIds()[string_index]->GetStringId();
+      break;
+    }
+    case DexFile::kDexAnnotationField:
+    case DexFile::kDexAnnotationEnum: {
+      const uint32_t field_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
+      item_.field_val_ = header.FieldIds()[field_index].get();
+      break;
+    }
+    case DexFile::kDexAnnotationMethod: {
+      const uint32_t method_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
+      item_.method_val_ = header.MethodIds()[method_index].get();
+      break;
+    }
+    case DexFile::kDexAnnotationArray: {
+      item_.annotation_array_val_ = new std::vector<std::unique_ptr<ArrayItem>>();
+      // Decode all elements.
+      const uint32_t size = DecodeUnsignedLeb128(data);
+      for (uint32_t i = 0; i < size; i++) {
+        item_.annotation_array_val_->push_back(
+            std::unique_ptr<ArrayItem>(new ArrayItem(header, data)));
+      }
+      break;
+    }
+    case DexFile::kDexAnnotationAnnotation: {
+      const uint32_t type_idx = DecodeUnsignedLeb128(data);
+      item_.annotation_annotation_val_.string_ = header.TypeIds()[type_idx]->GetStringId();
+      item_.annotation_annotation_val_.array_ = new std::vector<std::unique_ptr<NameValuePair>>();
+      // Decode all name=value pairs.
+      const uint32_t size = DecodeUnsignedLeb128(data);
+      for (uint32_t i = 0; i < size; i++) {
+        const uint32_t name_index = DecodeUnsignedLeb128(data);
+        item_.annotation_annotation_val_.array_->push_back(std::unique_ptr<NameValuePair>(
+            new NameValuePair(header.StringIds()[name_index].get(), new ArrayItem(header, data))));
+      }
+      break;
+    }
+    case DexFile::kDexAnnotationNull:
+      break;
+    case DexFile::kDexAnnotationBoolean:
+      item_.bool_val_ = (length != 0);
+      break;
+    default:
+      break;
+  }
+}
+
+ClassDef::ClassDef(const DexFile::ClassDef& disk_class_def, Header& header) {
+  class_type_ = header.TypeIds()[disk_class_def.class_idx_].get();
+  access_flags_ = disk_class_def.access_flags_;
+  superclass_ = header.TypeIds()[disk_class_def.superclass_idx_].get();
+
+  const DexFile::TypeList* type_list = header.GetDexFile().GetInterfacesList(disk_class_def);
+  interfaces_offset_ = disk_class_def.interfaces_off_;
+  if (type_list != nullptr) {
+    for (uint32_t index = 0; index < type_list->Size(); ++index) {
+      interfaces_.push_back(header.TypeIds()[type_list->GetTypeItem(index).type_idx_].get());
+    }
+  }
+  if (disk_class_def.source_file_idx_ == DexFile::kDexNoIndex) {
+    source_file_ = nullptr;
+  } else {
+    source_file_ = header.StringIds()[disk_class_def.source_file_idx_].get();
+  }
+  // Annotations.
+  const DexFile::AnnotationsDirectoryItem* disk_annotations_directory_item =
+      header.GetDexFile().GetAnnotationsDirectory(disk_class_def);
+  if (disk_annotations_directory_item == nullptr) {
+    annotations_.reset(nullptr);
+  } else {
+    annotations_.reset(new AnnotationsDirectoryItem(disk_annotations_directory_item, header));
+    annotations_->SetOffset(disk_class_def.annotations_off_);
+  }
+  // Static field initializers.
+  static_values_ = nullptr;
+  const uint8_t* static_data = header.GetDexFile().GetEncodedStaticFieldValuesArray(disk_class_def);
+  if (static_data != nullptr) {
+    uint32_t static_value_count = static_data == nullptr ? 0 : DecodeUnsignedLeb128(&static_data);
+    if (static_value_count > 0) {
+      static_values_ = new std::vector<std::unique_ptr<ArrayItem>>();
+      for (uint32_t i = 0; i < static_value_count; ++i) {
+        static_values_->push_back(std::unique_ptr<ArrayItem>(new ArrayItem(header, &static_data)));
+      }
+    }
+  }
+  // Read the fields and methods defined by the class, resolving the circular reference from those
+  // to classes by setting class at the same time.
+  const uint8_t* encoded_data = header.GetDexFile().GetClassData(disk_class_def);
+  class_data_.SetOffset(disk_class_def.class_data_off_);
+  if (encoded_data != nullptr) {
+    ClassDataItemIterator cdii(header.GetDexFile(), encoded_data);
+    // Static fields.
+    for (uint32_t i = 0; cdii.HasNextStaticField(); i++, cdii.Next()) {
+      FieldId* field_item = header.FieldIds()[cdii.GetMemberIndex()].get();
+      uint32_t access_flags = cdii.GetRawMemberAccessFlags();
+      class_data_.StaticFields().push_back(
+          std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
+    }
+    // Instance fields.
+    for (uint32_t i = 0; cdii.HasNextInstanceField(); i++, cdii.Next()) {
+      FieldId* field_item = header.FieldIds()[cdii.GetMemberIndex()].get();
+      uint32_t access_flags = cdii.GetRawMemberAccessFlags();
+      class_data_.InstanceFields().push_back(
+          std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
+    }
+    // Direct methods.
+    for (uint32_t i = 0; cdii.HasNextDirectMethod(); i++, cdii.Next()) {
+      MethodId* method_item = header.MethodIds()[cdii.GetMemberIndex()].get();
+      uint32_t access_flags = cdii.GetRawMemberAccessFlags();
+      const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem();
+      CodeItem* code_item = nullptr;
+      DebugInfoItem* debug_info = nullptr;
+      if (disk_code_item != nullptr) {
+        code_item = new CodeItem(*disk_code_item, header);
+        code_item->SetOffset(cdii.GetMethodCodeItemOffset());
+        debug_info = code_item->DebugInfo();
+      }
+      if (debug_info != nullptr) {
+        // TODO: Fix debug local info.
+        // bool is_static = (access_flags & kAccStatic) != 0;
+        // header.GetDexFile().DecodeDebugLocalInfo(
+        //       disk_code_item, is_static, cdii.GetMemberIndex(), GetLocalsCb, debug_info);
+        header.GetDexFile().DecodeDebugPositionInfo(disk_code_item, GetPositionsCb, debug_info);
+      }
+      class_data_.DirectMethods().push_back(
+          std::unique_ptr<MethodItem>(new MethodItem(access_flags, method_item, code_item)));
+    }
+    // Virtual methods.
+    for (uint32_t i = 0; cdii.HasNextVirtualMethod(); i++, cdii.Next()) {
+      MethodId* method_item = header.MethodIds()[cdii.GetMemberIndex()].get();
+      uint32_t access_flags = cdii.GetRawMemberAccessFlags();
+      const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem();
+      CodeItem* code_item = nullptr;
+      DebugInfoItem* debug_info = nullptr;
+      if (disk_code_item != nullptr) {
+        code_item = new CodeItem(*disk_code_item, header);
+        code_item->SetOffset(cdii.GetMethodCodeItemOffset());
+        debug_info = code_item->DebugInfo();
+      }
+      if (debug_info != nullptr) {
+        // TODO: Fix debug local info.
+        // bool is_static = (access_flags & kAccStatic) != 0;
+        // header.GetDexFile().DecodeDebugLocalInfo(
+        //       disk_code_item, is_static, cdii.GetMemberIndex(), GetLocalsCb, debug_info);
+        header.GetDexFile().DecodeDebugPositionInfo(disk_code_item, GetPositionsCb, debug_info);
+      }
+      class_data_.VirtualMethods().push_back(
+          std::unique_ptr<MethodItem>(new MethodItem(access_flags, method_item, code_item)));
+    }
+  }
+}
+
+CodeItem::CodeItem(const DexFile::CodeItem& disk_code_item, Header& header) {
+  registers_size_ = disk_code_item.registers_size_;
+  ins_size_ = disk_code_item.ins_size_;
+  outs_size_ = disk_code_item.outs_size_;
+  tries_size_ = disk_code_item.tries_size_;
+
+  const uint8_t* debug_info_stream = header.GetDexFile().GetDebugInfoStream(&disk_code_item);
+  if (debug_info_stream != nullptr) {
+    debug_info_.reset(new DebugInfoItem());
+  } else {
+    debug_info_.reset(nullptr);
+  }
+
+  insns_size_ = disk_code_item.insns_size_in_code_units_;
+  insns_.reset(new uint16_t[insns_size_]);
+  memcpy(insns_.get(), disk_code_item.insns_, insns_size_ * sizeof(uint16_t));
+
+  if (tries_size_ > 0) {
+    tries_ = new std::vector<std::unique_ptr<const TryItem>>();
+    for (uint32_t i = 0; i < tries_size_; ++i) {
+      const DexFile::TryItem* disk_try_item = header.GetDexFile().GetTryItems(disk_code_item, i);
+      tries_->push_back(std::unique_ptr<const TryItem>(
+          new TryItem(*disk_try_item, disk_code_item, header)));
+    }
+  } else {
+    tries_ = nullptr;
+  }
+}
+
+AnnotationSetItem::AnnotationSetItem(const DexFile::AnnotationSetItem& disk_annotations_item,
+                                     Header& header) {
+  if (disk_annotations_item.size_ == 0) {
+    return;
+  }
+  for (uint32_t i = 0; i < disk_annotations_item.size_; ++i) {
+    const DexFile::AnnotationItem* annotation =
+        header.GetDexFile().GetAnnotationItem(&disk_annotations_item, i);
+    if (annotation == nullptr) {
+      continue;
+    }
+    uint8_t visibility = annotation->visibility_;
+    const uint8_t* annotation_data = annotation->annotation_;
+    ArrayItem* array_item =
+        new ArrayItem(header, &annotation_data, DexFile::kDexAnnotationAnnotation, 0);
+    items_.push_back(std::unique_ptr<AnnotationItem>(new AnnotationItem(visibility, array_item)));
+  }
+}
+
+AnnotationsDirectoryItem::AnnotationsDirectoryItem(
+    const DexFile::AnnotationsDirectoryItem* disk_annotations_item, Header& header) {
+  const DexFile::AnnotationSetItem* class_set_item =
+      header.GetDexFile().GetClassAnnotationSet(disk_annotations_item);
+  if (class_set_item == nullptr) {
+    class_annotation_.reset(nullptr);
+  } else {
+    class_annotation_.reset(new AnnotationSetItem(*class_set_item, header));
+  }
+  const DexFile::FieldAnnotationsItem* fields =
+      header.GetDexFile().GetFieldAnnotations(disk_annotations_item);
+  if (fields != nullptr) {
+    for (uint32_t i = 0; i < disk_annotations_item->fields_size_; ++i) {
+      FieldId* field_id = header.FieldIds()[fields[i].field_idx_].get();
+      const DexFile::AnnotationSetItem* field_set_item =
+          header.GetDexFile().GetFieldAnnotationSetItem(fields[i]);
+      dex_ir::AnnotationSetItem* annotation_set_item =
+          new AnnotationSetItem(*field_set_item, header);
+      field_annotations_.push_back(std::unique_ptr<FieldAnnotation>(
+          new FieldAnnotation(field_id, annotation_set_item)));
+    }
+  }
+  const DexFile::MethodAnnotationsItem* methods =
+      header.GetDexFile().GetMethodAnnotations(disk_annotations_item);
+  if (methods != nullptr) {
+    for (uint32_t i = 0; i < disk_annotations_item->methods_size_; ++i) {
+      MethodId* method_id = header.MethodIds()[methods[i].method_idx_].get();
+      const DexFile::AnnotationSetItem* method_set_item =
+          header.GetDexFile().GetMethodAnnotationSetItem(methods[i]);
+      dex_ir::AnnotationSetItem* annotation_set_item =
+          new AnnotationSetItem(*method_set_item, header);
+      method_annotations_.push_back(std::unique_ptr<MethodAnnotation>(
+          new MethodAnnotation(method_id, annotation_set_item)));
+    }
+  }
+  const DexFile::ParameterAnnotationsItem* parameters =
+      header.GetDexFile().GetParameterAnnotations(disk_annotations_item);
+  if (parameters != nullptr) {
+    for (uint32_t i = 0; i < disk_annotations_item->parameters_size_; ++i) {
+      MethodId* method_id = header.MethodIds()[parameters[i].method_idx_].get();
+      const DexFile::AnnotationSetRefList* list =
+          header.GetDexFile().GetParameterAnnotationSetRefList(&parameters[i]);
+      parameter_annotations_.push_back(std::unique_ptr<ParameterAnnotation>(
+          new ParameterAnnotation(method_id, list, header)));
+    }
+  }
+}
+
+}  // namespace dex_ir
+}  // namespace art
diff --git a/dexlayout/dex_ir.h b/dexlayout/dex_ir.h
new file mode 100644
index 0000000..bef5c0d
--- /dev/null
+++ b/dexlayout/dex_ir.h
@@ -0,0 +1,684 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Header file of an in-memory representation of DEX files.
+ */
+
+#ifndef ART_DEXLAYOUT_DEX_IR_H_
+#define ART_DEXLAYOUT_DEX_IR_H_
+
+#include <iostream>
+#include <map>
+#include <vector>
+#include <stdint.h>
+
+#include "dex_file.h"
+
+namespace art {
+namespace dex_ir {
+
+// Forward declarations for classes used in containers or pointed to.
+class AnnotationsDirectoryItem;
+class AnnotationSetItem;
+class ArrayItem;
+class ClassData;
+class ClassDef;
+class CodeItem;
+class DebugInfoItem;
+class FieldId;
+class FieldItem;
+class Header;
+class MapList;
+class MapItem;
+class MethodId;
+class MethodItem;
+class ProtoId;
+class StringId;
+class TryItem;
+class TypeId;
+
+// Visitor support
+class AbstractDispatcher {
+ public:
+  AbstractDispatcher() = default;
+  virtual ~AbstractDispatcher() { }
+
+  virtual void Dispatch(Header* header) = 0;
+  virtual void Dispatch(const StringId* string_id) = 0;
+  virtual void Dispatch(const TypeId* type_id) = 0;
+  virtual void Dispatch(const ProtoId* proto_id) = 0;
+  virtual void Dispatch(const FieldId* field_id) = 0;
+  virtual void Dispatch(const MethodId* method_id) = 0;
+  virtual void Dispatch(ClassData* class_data) = 0;
+  virtual void Dispatch(ClassDef* class_def) = 0;
+  virtual void Dispatch(FieldItem* field_item) = 0;
+  virtual void Dispatch(MethodItem* method_item) = 0;
+  virtual void Dispatch(ArrayItem* array_item) = 0;
+  virtual void Dispatch(CodeItem* code_item) = 0;
+  virtual void Dispatch(TryItem* try_item) = 0;
+  virtual void Dispatch(DebugInfoItem* debug_info_item) = 0;
+  virtual void Dispatch(AnnotationSetItem* annotation_set_item) = 0;
+  virtual void Dispatch(AnnotationsDirectoryItem* annotations_directory_item) = 0;
+  virtual void Dispatch(MapList* map_list) = 0;
+  virtual void Dispatch(MapItem* map_item) = 0;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(AbstractDispatcher);
+};
+
+// Collections become owners of the objects added by moving them into unique pointers.
+template<class T> class CollectionWithOffset {
+ public:
+  CollectionWithOffset() = default;
+  std::vector<std::unique_ptr<T>>& Collection() { return collection_; }
+  // Read-time support methods
+  void AddWithPosition(uint32_t position, T* object) {
+    collection_.push_back(std::unique_ptr<T>(object));
+    collection_.back()->SetOffset(position);
+  }
+  // Ordinary object insertion into collection.
+  void Insert(T object ATTRIBUTE_UNUSED) {
+    // TODO(sehr): add ordered insertion support.
+    UNIMPLEMENTED(FATAL) << "Insertion not ready";
+  }
+  uint32_t GetOffset() const { return offset_; }
+  void SetOffset(uint32_t new_offset) { offset_ = new_offset; }
+  uint32_t Size() const { return collection_.size(); }
+
+ private:
+  std::vector<std::unique_ptr<T>> collection_;
+  uint32_t offset_ = 0;
+  DISALLOW_COPY_AND_ASSIGN(CollectionWithOffset);
+};
+
+class Item {
+ public:
+  virtual ~Item() { }
+  uint32_t GetOffset() const { return offset_; }
+  void SetOffset(uint32_t offset) { offset_ = offset; }
+ protected:
+  uint32_t offset_ = 0;
+};
+
+class Header : public Item {
+ public:
+  explicit Header(const DexFile& dex_file);
+  ~Header() OVERRIDE { }
+
+  const DexFile& GetDexFile() const { return dex_file_; }
+
+  const uint8_t* Magic() const { return magic_; }
+  uint32_t Checksum() const { return checksum_; }
+  const uint8_t* Signature() const { return signature_; }
+  uint32_t EndianTag() const { return endian_tag_; }
+  uint32_t FileSize() const { return file_size_; }
+  uint32_t HeaderSize() const { return header_size_; }
+  uint32_t LinkSize() const { return link_size_; }
+  uint32_t LinkOffset() const { return link_offset_; }
+  uint32_t DataSize() const { return data_size_; }
+  uint32_t DataOffset() const { return data_offset_; }
+
+  void SetChecksum(uint32_t new_checksum) { checksum_ = new_checksum; }
+  void SetSignature(const uint8_t* new_signature) {
+    memcpy(signature_, new_signature, sizeof(signature_));
+  }
+  void SetFileSize(uint32_t new_file_size) { file_size_ = new_file_size; }
+  void SetHeaderSize(uint32_t new_header_size) { header_size_ = new_header_size; }
+  void SetLinkSize(uint32_t new_link_size) { link_size_ = new_link_size; }
+  void SetLinkOffset(uint32_t new_link_offset) { link_offset_ = new_link_offset; }
+  void SetDataSize(uint32_t new_data_size) { data_size_ = new_data_size; }
+  void SetDataOffset(uint32_t new_data_offset) { data_offset_ = new_data_offset; }
+
+  // Collections.
+  std::vector<std::unique_ptr<StringId>>& StringIds() { return string_ids_.Collection(); }
+  std::vector<std::unique_ptr<TypeId>>& TypeIds() { return type_ids_.Collection(); }
+  std::vector<std::unique_ptr<ProtoId>>& ProtoIds() { return proto_ids_.Collection(); }
+  std::vector<std::unique_ptr<FieldId>>& FieldIds() { return field_ids_.Collection(); }
+  std::vector<std::unique_ptr<MethodId>>& MethodIds() { return method_ids_.Collection(); }
+  std::vector<std::unique_ptr<ClassDef>>& ClassDefs() { return class_defs_.Collection(); }
+  uint32_t StringIdsOffset() const { return string_ids_.GetOffset(); }
+  uint32_t TypeIdsOffset() const { return type_ids_.GetOffset(); }
+  uint32_t ProtoIdsOffset() const { return proto_ids_.GetOffset(); }
+  uint32_t FieldIdsOffset() const { return field_ids_.GetOffset(); }
+  uint32_t MethodIdsOffset() const { return method_ids_.GetOffset(); }
+  uint32_t ClassDefsOffset() const { return class_defs_.GetOffset(); }
+  void SetStringIdsOffset(uint32_t new_offset) { string_ids_.SetOffset(new_offset); }
+  void SetTypeIdsOffset(uint32_t new_offset) { type_ids_.SetOffset(new_offset); }
+  void SetProtoIdsOffset(uint32_t new_offset) { proto_ids_.SetOffset(new_offset); }
+  void SetFieldIdsOffset(uint32_t new_offset) { field_ids_.SetOffset(new_offset); }
+  void SetMethodIdsOffset(uint32_t new_offset) { method_ids_.SetOffset(new_offset); }
+  void SetClassDefsOffset(uint32_t new_offset) { class_defs_.SetOffset(new_offset); }
+  uint32_t StringIdsSize() const { return string_ids_.Size(); }
+  uint32_t TypeIdsSize() const { return type_ids_.Size(); }
+  uint32_t ProtoIdsSize() const { return proto_ids_.Size(); }
+  uint32_t FieldIdsSize() const { return field_ids_.Size(); }
+  uint32_t MethodIdsSize() const { return method_ids_.Size(); }
+  uint32_t ClassDefsSize() const { return class_defs_.Size(); }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  const DexFile& dex_file_;
+  uint8_t magic_[8];
+  uint32_t checksum_;
+  uint8_t signature_[DexFile::kSha1DigestSize];
+  uint32_t endian_tag_;
+  uint32_t file_size_;
+  uint32_t header_size_;
+  uint32_t link_size_;
+  uint32_t link_offset_;
+  uint32_t data_size_;
+  uint32_t data_offset_;
+
+  CollectionWithOffset<StringId> string_ids_;
+  CollectionWithOffset<TypeId> type_ids_;
+  CollectionWithOffset<ProtoId> proto_ids_;
+  CollectionWithOffset<FieldId> field_ids_;
+  CollectionWithOffset<MethodId> method_ids_;
+  CollectionWithOffset<ClassDef> class_defs_;
+  DISALLOW_COPY_AND_ASSIGN(Header);
+};
+
+class StringId : public Item {
+ public:
+  StringId(const DexFile::StringId& disk_string_id, Header& header) :
+    data_(strdup(header.GetDexFile().GetStringData(disk_string_id))) {
+  }
+  ~StringId() OVERRIDE { }
+
+  const char* Data() const { return data_.get(); }
+
+  void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
+
+ private:
+  std::unique_ptr<const char> data_;
+  DISALLOW_COPY_AND_ASSIGN(StringId);
+};
+
+class TypeId : public Item {
+ public:
+  TypeId(const DexFile::TypeId& disk_type_id, Header& header) :
+    string_id_(header.StringIds()[disk_type_id.descriptor_idx_].get()) {
+  }
+  ~TypeId() OVERRIDE { }
+
+  StringId* GetStringId() const { return string_id_; }
+
+  void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
+
+ private:
+  StringId* string_id_;
+  DISALLOW_COPY_AND_ASSIGN(TypeId);
+};
+
+class ProtoId : public Item {
+ public:
+  ProtoId(const DexFile::ProtoId& disk_proto_id, Header& header) {
+    shorty_ = header.StringIds()[disk_proto_id.shorty_idx_].get();
+    return_type_ = header.TypeIds()[disk_proto_id.return_type_idx_].get();
+    DexFileParameterIterator dfpi(header.GetDexFile(), disk_proto_id);
+    while (dfpi.HasNext()) {
+      parameters_.push_back(header.TypeIds()[dfpi.GetTypeIdx()].get());
+      dfpi.Next();
+    }
+  }
+  ~ProtoId() OVERRIDE { }
+
+  const StringId* Shorty() const { return shorty_; }
+  const TypeId* ReturnType() const { return return_type_; }
+  const std::vector<const TypeId*>& Parameters() const { return parameters_; }
+
+  void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
+
+ private:
+  const StringId* shorty_;
+  const TypeId* return_type_;
+  std::vector<const TypeId*> parameters_;
+  DISALLOW_COPY_AND_ASSIGN(ProtoId);
+};
+
+class FieldId : public Item {
+ public:
+  FieldId(const DexFile::FieldId& disk_field_id, Header& header) {
+    class_ = header.TypeIds()[disk_field_id.class_idx_].get();
+    type_ = header.TypeIds()[disk_field_id.type_idx_].get();
+    name_ = header.StringIds()[disk_field_id.name_idx_].get();
+  }
+  ~FieldId() OVERRIDE { }
+
+  const TypeId* Class() const { return class_; }
+  const TypeId* Type() const { return type_; }
+  const StringId* Name() const { return name_; }
+
+  void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
+
+ private:
+  const TypeId* class_;
+  const TypeId* type_;
+  const StringId* name_;
+  DISALLOW_COPY_AND_ASSIGN(FieldId);
+};
+
+class MethodId : public Item {
+ public:
+  MethodId(const DexFile::MethodId& disk_method_id, Header& header) {
+    class_ = header.TypeIds()[disk_method_id.class_idx_].get();
+    proto_ = header.ProtoIds()[disk_method_id.proto_idx_].get();
+    name_ = header.StringIds()[disk_method_id.name_idx_].get();
+  }
+  ~MethodId() OVERRIDE { }
+
+  const TypeId* Class() const { return class_; }
+  const ProtoId* Proto() const { return proto_; }
+  const StringId* Name() const { return name_; }
+
+  void Accept(AbstractDispatcher* dispatch) const { dispatch->Dispatch(this); }
+
+ private:
+  const TypeId* class_;
+  const ProtoId* proto_;
+  const StringId* name_;
+  DISALLOW_COPY_AND_ASSIGN(MethodId);
+};
+
+class FieldItem : public Item {
+ public:
+  FieldItem(uint32_t access_flags, const FieldId* field_id) :
+    access_flags_(access_flags), field_id_(field_id) { }
+  ~FieldItem() OVERRIDE { }
+
+  uint32_t GetAccessFlags() const { return access_flags_; }
+  const FieldId* GetFieldId() const { return field_id_; }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  uint32_t access_flags_;
+  const FieldId* field_id_;
+  DISALLOW_COPY_AND_ASSIGN(FieldItem);
+};
+
+class MethodItem : public Item {
+ public:
+  MethodItem(uint32_t access_flags, const MethodId* method_id, const CodeItem* code) :
+    access_flags_(access_flags), method_id_(method_id), code_(code) { }
+  ~MethodItem() OVERRIDE { }
+
+  uint32_t GetAccessFlags() const { return access_flags_; }
+  const MethodId* GetMethodId() const { return method_id_; }
+  const CodeItem* GetCodeItem() const { return code_.get(); }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  uint32_t access_flags_;
+  const MethodId* method_id_;
+  std::unique_ptr<const CodeItem> code_;
+  DISALLOW_COPY_AND_ASSIGN(MethodItem);
+};
+
+class ArrayItem : public Item {
+ public:
+  class NameValuePair {
+   public:
+    NameValuePair(StringId* name, ArrayItem* value) :
+      name_(name), value_(value) { }
+
+    StringId* Name() const { return name_; }
+    ArrayItem* Value() const { return value_.get(); }
+
+   private:
+    StringId* name_;
+    std::unique_ptr<ArrayItem> value_;
+    DISALLOW_COPY_AND_ASSIGN(NameValuePair);
+  };
+
+  ArrayItem(Header& header, const uint8_t** data, uint8_t type, uint8_t length);
+  ArrayItem(Header& header, const uint8_t** data);
+  ~ArrayItem() OVERRIDE { }
+
+  int8_t Type() const { return type_; }
+  bool GetBoolean() const { return item_.bool_val_; }
+  int8_t GetByte() const { return item_.byte_val_; }
+  int16_t GetShort() const { return item_.short_val_; }
+  uint16_t GetChar() const { return item_.char_val_; }
+  int32_t GetInt() const { return item_.int_val_; }
+  int64_t GetLong() const { return item_.long_val_; }
+  float GetFloat() const { return item_.float_val_; }
+  double GetDouble() const { return item_.double_val_; }
+  StringId* GetStringId() const { return item_.string_val_; }
+  FieldId* GetFieldId() const { return item_.field_val_; }
+  MethodId* GetMethodId() const { return item_.method_val_; }
+  std::vector<std::unique_ptr<ArrayItem>>* GetAnnotationArray() const {
+    return item_.annotation_array_val_;
+  }
+  StringId* GetAnnotationAnnotationString() const {
+    return item_.annotation_annotation_val_.string_;
+  }
+  std::vector<std::unique_ptr<NameValuePair>>* GetAnnotationAnnotationNameValuePairArray() const {
+    return item_.annotation_annotation_val_.array_;
+  }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  void Read(Header& header, const uint8_t** data, uint8_t type, uint8_t length);
+  uint8_t type_;
+  union {
+    bool bool_val_;
+    int8_t byte_val_;
+    int16_t short_val_;
+    uint16_t char_val_;
+    int32_t int_val_;
+    int64_t long_val_;
+    float float_val_;
+    double double_val_;
+    StringId* string_val_;
+    FieldId* field_val_;
+    MethodId* method_val_;
+    std::vector<std::unique_ptr<ArrayItem>>* annotation_array_val_;
+    struct {
+      StringId* string_;
+      std::vector<std::unique_ptr<NameValuePair>>* array_;
+    } annotation_annotation_val_;
+  } item_;
+  DISALLOW_COPY_AND_ASSIGN(ArrayItem);
+};
+
+class ClassData : public Item {
+ public:
+  ClassData() = default;
+  ~ClassData() OVERRIDE = default;
+  std::vector<std::unique_ptr<FieldItem>>& StaticFields() { return static_fields_; }
+  std::vector<std::unique_ptr<FieldItem>>& InstanceFields() { return instance_fields_; }
+  std::vector<std::unique_ptr<MethodItem>>& DirectMethods() { return direct_methods_; }
+  std::vector<std::unique_ptr<MethodItem>>& VirtualMethods() { return virtual_methods_; }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  std::vector<std::unique_ptr<FieldItem>> static_fields_;
+  std::vector<std::unique_ptr<FieldItem>> instance_fields_;
+  std::vector<std::unique_ptr<MethodItem>> direct_methods_;
+  std::vector<std::unique_ptr<MethodItem>> virtual_methods_;
+  DISALLOW_COPY_AND_ASSIGN(ClassData);
+};
+
+class ClassDef : public Item {
+ public:
+  ClassDef(const DexFile::ClassDef& disk_class_def, Header& header);
+  ~ClassDef() OVERRIDE { }
+
+  const TypeId* ClassType() const { return class_type_; }
+  uint32_t GetAccessFlags() const { return access_flags_; }
+  const TypeId* Superclass() const { return superclass_; }
+  std::vector<TypeId*>* Interfaces() { return &interfaces_; }
+  uint32_t InterfacesOffset() const { return interfaces_offset_; }
+  void SetInterfacesOffset(uint32_t new_offset) { interfaces_offset_ = new_offset; }
+  const StringId* SourceFile() const { return source_file_; }
+  AnnotationsDirectoryItem* Annotations() const { return annotations_.get(); }
+  std::vector<std::unique_ptr<ArrayItem>>* StaticValues() { return static_values_; }
+  ClassData* GetClassData() { return &class_data_; }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  const TypeId* class_type_;
+  uint32_t access_flags_;
+  const TypeId* superclass_;
+  std::vector<TypeId*> interfaces_;
+  uint32_t interfaces_offset_;
+  const StringId* source_file_;
+  std::unique_ptr<AnnotationsDirectoryItem> annotations_;
+  std::vector<std::unique_ptr<ArrayItem>>* static_values_;
+  ClassData class_data_;
+  DISALLOW_COPY_AND_ASSIGN(ClassDef);
+};
+
+class CodeItem : public Item {
+ public:
+  CodeItem(const DexFile::CodeItem& disk_code_item, Header& header);
+  ~CodeItem() OVERRIDE { }
+
+  uint16_t RegistersSize() const { return registers_size_; }
+  uint16_t InsSize() const { return ins_size_; }
+  uint16_t OutsSize() const { return outs_size_; }
+  uint16_t TriesSize() const { return tries_size_; }
+  DebugInfoItem* DebugInfo() const { return debug_info_.get(); }
+  uint32_t InsnsSize() const { return insns_size_; }
+  uint16_t* Insns() const { return insns_.get(); }
+  std::vector<std::unique_ptr<const TryItem>>* Tries() const { return tries_; }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  uint16_t registers_size_;
+  uint16_t ins_size_;
+  uint16_t outs_size_;
+  uint16_t tries_size_;
+  std::unique_ptr<DebugInfoItem> debug_info_;
+  uint32_t insns_size_;
+  std::unique_ptr<uint16_t[]> insns_;
+  std::vector<std::unique_ptr<const TryItem>>* tries_;
+  DISALLOW_COPY_AND_ASSIGN(CodeItem);
+};
+
+class TryItem : public Item {
+ public:
+  class CatchHandler {
+   public:
+    CatchHandler(const TypeId* type_id, uint32_t address) : type_id_(type_id), address_(address) { }
+
+    const TypeId* GetTypeId() const { return type_id_; }
+    uint32_t GetAddress() const { return address_; }
+
+   private:
+    const TypeId* type_id_;
+    uint32_t address_;
+    DISALLOW_COPY_AND_ASSIGN(CatchHandler);
+  };
+
+  TryItem(const DexFile::TryItem& disk_try_item,
+          const DexFile::CodeItem& disk_code_item,
+          Header& header) {
+    start_addr_ = disk_try_item.start_addr_;
+    insn_count_ = disk_try_item.insn_count_;
+    for (CatchHandlerIterator it(disk_code_item, disk_try_item); it.HasNext(); it.Next()) {
+      const uint16_t type_index = it.GetHandlerTypeIndex();
+      const TypeId* type_id = type_index != DexFile::kDexNoIndex16 ?
+          header.TypeIds()[type_index].get() : nullptr;
+      handlers_.push_back(std::unique_ptr<const CatchHandler>(
+          new CatchHandler(type_id, it.GetHandlerAddress())));
+    }
+  }
+  ~TryItem() OVERRIDE { }
+
+  uint32_t StartAddr() const { return start_addr_; }
+  uint16_t InsnCount() const { return insn_count_; }
+  const std::vector<std::unique_ptr<const CatchHandler>>& GetHandlers() const { return handlers_; }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  uint32_t start_addr_;
+  uint16_t insn_count_;
+  std::vector<std::unique_ptr<const CatchHandler>> handlers_;
+  DISALLOW_COPY_AND_ASSIGN(TryItem);
+};
+
+
+struct PositionInfo {
+  PositionInfo(uint32_t address, uint32_t line) : address_(address), line_(line) { }
+
+  uint32_t address_;
+  uint32_t line_;
+};
+
+struct LocalInfo {
+  LocalInfo(const char* name, const char* descriptor, const char* signature, uint32_t start_address,
+            uint32_t end_address, uint16_t reg) :
+    name_(name), descriptor_(descriptor), signature_(signature), start_address_(start_address),
+    end_address_(end_address), reg_(reg) { }
+
+  std::string name_;
+  std::string descriptor_;
+  std::string signature_;
+  uint32_t start_address_;
+  uint32_t end_address_;
+  uint16_t reg_;
+};
+
+class DebugInfoItem : public Item {
+ public:
+  DebugInfoItem() = default;
+
+  std::vector<std::unique_ptr<PositionInfo>>& GetPositionInfo() { return positions_; }
+  std::vector<std::unique_ptr<LocalInfo>>& GetLocalInfo() { return locals_; }
+
+ private:
+  std::vector<std::unique_ptr<PositionInfo>> positions_;
+  std::vector<std::unique_ptr<LocalInfo>> locals_;
+  DISALLOW_COPY_AND_ASSIGN(DebugInfoItem);
+};
+
+class AnnotationSetItem : public Item {
+ public:
+  class AnnotationItem {
+   public:
+    AnnotationItem(uint8_t visibility, ArrayItem* item) :
+      visibility_(visibility), item_(item) { }
+
+    uint8_t GetVisibility() const { return visibility_; }
+    ArrayItem* GetItem() const { return item_.get(); }
+
+   private:
+    uint8_t visibility_;
+    std::unique_ptr<ArrayItem> item_;
+    DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
+  };
+
+  AnnotationSetItem(const DexFile::AnnotationSetItem& disk_annotations_item, Header& header);
+  ~AnnotationSetItem() OVERRIDE { }
+
+  std::vector<std::unique_ptr<AnnotationItem>>& GetItems() { return items_; }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  std::vector<std::unique_ptr<AnnotationItem>> items_;
+  DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
+};
+
+class AnnotationsDirectoryItem : public Item {
+ public:
+  class FieldAnnotation {
+   public:
+    FieldAnnotation(FieldId* field_id, AnnotationSetItem* annotation_set_item) :
+      field_id_(field_id), annotation_set_item_(annotation_set_item) { }
+
+    FieldId* GetFieldId() const { return field_id_; }
+    AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_.get(); }
+
+   private:
+    FieldId* field_id_;
+    std::unique_ptr<AnnotationSetItem> annotation_set_item_;
+    DISALLOW_COPY_AND_ASSIGN(FieldAnnotation);
+  };
+
+  class MethodAnnotation {
+   public:
+    MethodAnnotation(MethodId* method_id, AnnotationSetItem* annotation_set_item) :
+      method_id_(method_id), annotation_set_item_(annotation_set_item) { }
+
+    MethodId* GetMethodId() const { return method_id_; }
+    AnnotationSetItem* GetAnnotationSetItem() const { return annotation_set_item_.get(); }
+
+   private:
+    MethodId* method_id_;
+    std::unique_ptr<AnnotationSetItem> annotation_set_item_;
+    DISALLOW_COPY_AND_ASSIGN(MethodAnnotation);
+  };
+
+  class ParameterAnnotation {
+   public:
+    ParameterAnnotation(MethodId* method_id,
+                        const DexFile::AnnotationSetRefList* annotation_set_ref_list,
+                        Header& header) :
+      method_id_(method_id) {
+      for (uint32_t i = 0; i < annotation_set_ref_list->size_; ++i) {
+        const DexFile::AnnotationSetItem* annotation_set_item =
+            header.GetDexFile().GetSetRefItemItem(&annotation_set_ref_list->list_[i]);
+        annotations_.push_back(std::unique_ptr<AnnotationSetItem>(
+            new AnnotationSetItem(*annotation_set_item, header)));
+      }
+    }
+
+    MethodId* GetMethodId() const { return method_id_; }
+    std::vector<std::unique_ptr<AnnotationSetItem>>& GetAnnotations() { return annotations_; }
+
+   private:
+    MethodId* method_id_;
+    std::vector<std::unique_ptr<AnnotationSetItem>> annotations_;
+    DISALLOW_COPY_AND_ASSIGN(ParameterAnnotation);
+  };
+
+  AnnotationsDirectoryItem(const DexFile::AnnotationsDirectoryItem* disk_annotations_item,
+                           Header& header);
+
+  AnnotationSetItem* GetClassAnnotation() const { return class_annotation_.get(); }
+
+  std::vector<std::unique_ptr<FieldAnnotation>>& GetFieldAnnotations() {
+    return field_annotations_;
+  }
+
+  std::vector<std::unique_ptr<MethodAnnotation>>& GetMethodAnnotations() {
+    return method_annotations_;
+  }
+
+  std::vector<std::unique_ptr<ParameterAnnotation>>& GetParameterAnnotations() {
+    return parameter_annotations_;
+  }
+
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  std::unique_ptr<AnnotationSetItem> class_annotation_;
+  std::vector<std::unique_ptr<FieldAnnotation>> field_annotations_;
+  std::vector<std::unique_ptr<MethodAnnotation>> method_annotations_;
+  std::vector<std::unique_ptr<ParameterAnnotation>> parameter_annotations_;
+  DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
+};
+
+// TODO(sehr): implement MapList.
+class MapList : public Item {
+ public:
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(MapList);
+};
+
+class MapItem : public Item {
+ public:
+  void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(MapItem);
+};
+
+}  // namespace dex_ir
+}  // namespace art
+
+#endif  // ART_DEXLAYOUT_DEX_IR_H_
diff --git a/dexlayout/dexlayout.cc b/dexlayout/dexlayout.cc
new file mode 100644
index 0000000..a774921
--- /dev/null
+++ b/dexlayout/dexlayout.cc
@@ -0,0 +1,1503 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Implementation file of the dexlayout utility.
+ *
+ * This is a tool to read dex files into an internal representation,
+ * reorganize the representation, and emit dex files with a better
+ * file layout.
+ */
+
+#include "dexlayout.h"
+
+#include <inttypes.h>
+#include <stdio.h>
+
+#include <iostream>
+#include <memory>
+#include <sstream>
+#include <vector>
+
+#include "dex_ir.h"
+#include "dex_file-inl.h"
+#include "dex_instruction-inl.h"
+#include "utils.h"
+
+namespace art {
+
+/*
+ * Options parsed in main driver.
+ */
+struct Options options_;
+
+/*
+ * Output file. Defaults to stdout.
+ */
+FILE* out_file_ = stdout;
+
+/*
+ * Flags for use with createAccessFlagStr().
+ */
+enum AccessFor {
+  kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
+};
+const int kNumFlags = 18;
+
+/*
+ * Gets 2 little-endian bytes.
+ */
+static inline uint16_t Get2LE(unsigned char const* src) {
+  return src[0] | (src[1] << 8);
+}
+
+/*
+ * Converts the class name portion of a type descriptor to human-readable
+ * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
+ */
+static std::string DescriptorClassToDot(const char* str) {
+  std::string descriptor(str);
+  // Reduce to just the class name prefix.
+  size_t last_slash = descriptor.rfind('/');
+  if (last_slash == std::string::npos) {
+    last_slash = 0;
+  }
+  // Start past the '/' or 'L'.
+  last_slash++;
+
+  // Copy class name over, trimming trailing ';'.
+  size_t size = descriptor.size() - 1 - last_slash;
+  std::string result(descriptor.substr(last_slash, size));
+
+  // Replace '$' with '.'.
+  size_t dollar_sign = result.find('$');
+  while (dollar_sign != std::string::npos) {
+    result[dollar_sign] = '.';
+    dollar_sign = result.find('$', dollar_sign);
+  }
+
+  return result;
+}
+
+/*
+ * Returns string representing the boolean value.
+ */
+static const char* StrBool(bool val) {
+  return val ? "true" : "false";
+}
+
+/*
+ * Returns a quoted string representing the boolean value.
+ */
+static const char* QuotedBool(bool val) {
+  return val ? "\"true\"" : "\"false\"";
+}
+
+/*
+ * Returns a quoted string representing the access flags.
+ */
+static const char* QuotedVisibility(uint32_t access_flags) {
+  if (access_flags & kAccPublic) {
+    return "\"public\"";
+  } else if (access_flags & kAccProtected) {
+    return "\"protected\"";
+  } else if (access_flags & kAccPrivate) {
+    return "\"private\"";
+  } else {
+    return "\"package\"";
+  }
+}
+
+/*
+ * Counts the number of '1' bits in a word.
+ */
+static int CountOnes(uint32_t val) {
+  val = val - ((val >> 1) & 0x55555555);
+  val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
+  return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
+}
+
+/*
+ * Creates a new string with human-readable access flags.
+ *
+ * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
+ */
+static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
+  static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
+    {
+      "PUBLIC",                /* 0x00001 */
+      "PRIVATE",               /* 0x00002 */
+      "PROTECTED",             /* 0x00004 */
+      "STATIC",                /* 0x00008 */
+      "FINAL",                 /* 0x00010 */
+      "?",                     /* 0x00020 */
+      "?",                     /* 0x00040 */
+      "?",                     /* 0x00080 */
+      "?",                     /* 0x00100 */
+      "INTERFACE",             /* 0x00200 */
+      "ABSTRACT",              /* 0x00400 */
+      "?",                     /* 0x00800 */
+      "SYNTHETIC",             /* 0x01000 */
+      "ANNOTATION",            /* 0x02000 */
+      "ENUM",                  /* 0x04000 */
+      "?",                     /* 0x08000 */
+      "VERIFIED",              /* 0x10000 */
+      "OPTIMIZED",             /* 0x20000 */
+    }, {
+      "PUBLIC",                /* 0x00001 */
+      "PRIVATE",               /* 0x00002 */
+      "PROTECTED",             /* 0x00004 */
+      "STATIC",                /* 0x00008 */
+      "FINAL",                 /* 0x00010 */
+      "SYNCHRONIZED",          /* 0x00020 */
+      "BRIDGE",                /* 0x00040 */
+      "VARARGS",               /* 0x00080 */
+      "NATIVE",                /* 0x00100 */
+      "?",                     /* 0x00200 */
+      "ABSTRACT",              /* 0x00400 */
+      "STRICT",                /* 0x00800 */
+      "SYNTHETIC",             /* 0x01000 */
+      "?",                     /* 0x02000 */
+      "?",                     /* 0x04000 */
+      "MIRANDA",               /* 0x08000 */
+      "CONSTRUCTOR",           /* 0x10000 */
+      "DECLARED_SYNCHRONIZED", /* 0x20000 */
+    }, {
+      "PUBLIC",                /* 0x00001 */
+      "PRIVATE",               /* 0x00002 */
+      "PROTECTED",             /* 0x00004 */
+      "STATIC",                /* 0x00008 */
+      "FINAL",                 /* 0x00010 */
+      "?",                     /* 0x00020 */
+      "VOLATILE",              /* 0x00040 */
+      "TRANSIENT",             /* 0x00080 */
+      "?",                     /* 0x00100 */
+      "?",                     /* 0x00200 */
+      "?",                     /* 0x00400 */
+      "?",                     /* 0x00800 */
+      "SYNTHETIC",             /* 0x01000 */
+      "?",                     /* 0x02000 */
+      "ENUM",                  /* 0x04000 */
+      "?",                     /* 0x08000 */
+      "?",                     /* 0x10000 */
+      "?",                     /* 0x20000 */
+    },
+  };
+
+  // Allocate enough storage to hold the expected number of strings,
+  // plus a space between each.  We over-allocate, using the longest
+  // string above as the base metric.
+  const int kLongest = 21;  // The strlen of longest string above.
+  const int count = CountOnes(flags);
+  char* str;
+  char* cp;
+  cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
+
+  for (int i = 0; i < kNumFlags; i++) {
+    if (flags & 0x01) {
+      const char* accessStr = kAccessStrings[for_what][i];
+      const int len = strlen(accessStr);
+      if (cp != str) {
+        *cp++ = ' ';
+      }
+      memcpy(cp, accessStr, len);
+      cp += len;
+    }
+    flags >>= 1;
+  }  // for
+
+  *cp = '\0';
+  return str;
+}
+
+static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
+  if (proto == nullptr) {
+    return "<no signature>";
+  }
+
+  const std::vector<const dex_ir::TypeId*>& params = proto->Parameters();
+  std::string result("(");
+  for (uint32_t i = 0; i < params.size(); ++i) {
+    result += params[i]->GetStringId()->Data();
+  }
+  result += ")";
+  result += proto->ReturnType()->GetStringId()->Data();
+  return result;
+}
+
+/*
+ * Copies character data from "data" to "out", converting non-ASCII values
+ * to fprintf format chars or an ASCII filler ('.' or '?').
+ *
+ * The output buffer must be able to hold (2*len)+1 bytes.  The result is
+ * NULL-terminated.
+ */
+static void Asciify(char* out, const unsigned char* data, size_t len) {
+  while (len--) {
+    if (*data < 0x20) {
+      // Could do more here, but we don't need them yet.
+      switch (*data) {
+        case '\0':
+          *out++ = '\\';
+          *out++ = '0';
+          break;
+        case '\n':
+          *out++ = '\\';
+          *out++ = 'n';
+          break;
+        default:
+          *out++ = '.';
+          break;
+      }  // switch
+    } else if (*data >= 0x80) {
+      *out++ = '?';
+    } else {
+      *out++ = *data;
+    }
+    data++;
+  }  // while
+  *out = '\0';
+}
+
+/*
+ * Dumps a string value with some escape characters.
+ */
+static void DumpEscapedString(const char* p) {
+  fputs("\"", out_file_);
+  for (; *p; p++) {
+    switch (*p) {
+      case '\\':
+        fputs("\\\\", out_file_);
+        break;
+      case '\"':
+        fputs("\\\"", out_file_);
+        break;
+      case '\t':
+        fputs("\\t", out_file_);
+        break;
+      case '\n':
+        fputs("\\n", out_file_);
+        break;
+      case '\r':
+        fputs("\\r", out_file_);
+        break;
+      default:
+        putc(*p, out_file_);
+    }  // switch
+  }  // for
+  fputs("\"", out_file_);
+}
+
+/*
+ * Dumps a string as an XML attribute value.
+ */
+static void DumpXmlAttribute(const char* p) {
+  for (; *p; p++) {
+    switch (*p) {
+      case '&':
+        fputs("&amp;", out_file_);
+        break;
+      case '<':
+        fputs("&lt;", out_file_);
+        break;
+      case '>':
+        fputs("&gt;", out_file_);
+        break;
+      case '"':
+        fputs("&quot;", out_file_);
+        break;
+      case '\t':
+        fputs("&#x9;", out_file_);
+        break;
+      case '\n':
+        fputs("&#xA;", out_file_);
+        break;
+      case '\r':
+        fputs("&#xD;", out_file_);
+        break;
+      default:
+        putc(*p, out_file_);
+    }  // switch
+  }  // for
+}
+
+/*
+ * Dumps encoded value.
+ */
+static void DumpEncodedValue(const dex_ir::ArrayItem* data) {
+  switch (data->Type()) {
+    case DexFile::kDexAnnotationByte:
+      fprintf(out_file_, "%" PRId8, data->GetByte());
+      break;
+    case DexFile::kDexAnnotationShort:
+      fprintf(out_file_, "%" PRId16, data->GetShort());
+      break;
+    case DexFile::kDexAnnotationChar:
+      fprintf(out_file_, "%" PRIu16, data->GetChar());
+      break;
+    case DexFile::kDexAnnotationInt:
+      fprintf(out_file_, "%" PRId32, data->GetInt());
+      break;
+    case DexFile::kDexAnnotationLong:
+      fprintf(out_file_, "%" PRId64, data->GetLong());
+      break;
+    case DexFile::kDexAnnotationFloat: {
+      fprintf(out_file_, "%g", data->GetFloat());
+      break;
+    }
+    case DexFile::kDexAnnotationDouble: {
+      fprintf(out_file_, "%g", data->GetDouble());
+      break;
+    }
+    case DexFile::kDexAnnotationString: {
+      dex_ir::StringId* string_id = data->GetStringId();
+      if (options_.output_format_ == kOutputPlain) {
+        DumpEscapedString(string_id->Data());
+      } else {
+        DumpXmlAttribute(string_id->Data());
+      }
+      break;
+    }
+    case DexFile::kDexAnnotationType: {
+      dex_ir::StringId* string_id = data->GetStringId();
+      fputs(string_id->Data(), out_file_);
+      break;
+    }
+    case DexFile::kDexAnnotationField:
+    case DexFile::kDexAnnotationEnum: {
+      dex_ir::FieldId* field_id = data->GetFieldId();
+      fputs(field_id->Name()->Data(), out_file_);
+      break;
+    }
+    case DexFile::kDexAnnotationMethod: {
+      dex_ir::MethodId* method_id = data->GetMethodId();
+      fputs(method_id->Name()->Data(), out_file_);
+      break;
+    }
+    case DexFile::kDexAnnotationArray: {
+      fputc('{', out_file_);
+      // Display all elements.
+      for (auto& array : *data->GetAnnotationArray()) {
+        fputc(' ', out_file_);
+        DumpEncodedValue(array.get());
+      }
+      fputs(" }", out_file_);
+      break;
+    }
+    case DexFile::kDexAnnotationAnnotation: {
+      fputs(data->GetAnnotationAnnotationString()->Data(), out_file_);
+      // Display all name=value pairs.
+      for (auto& subannotation : *data->GetAnnotationAnnotationNameValuePairArray()) {
+        fputc(' ', out_file_);
+        fputs(subannotation->Name()->Data(), out_file_);
+        fputc('=', out_file_);
+        DumpEncodedValue(subannotation->Value());
+      }
+      break;
+    }
+    case DexFile::kDexAnnotationNull:
+      fputs("null", out_file_);
+      break;
+    case DexFile::kDexAnnotationBoolean:
+      fputs(StrBool(data->GetBoolean()), out_file_);
+      break;
+    default:
+      fputs("????", out_file_);
+      break;
+  }  // switch
+}
+
+/*
+ * Dumps the file header.
+ */
+static void DumpFileHeader(const dex_ir::Header* header) {
+  char sanitized[8 * 2 + 1];
+  fprintf(out_file_, "DEX file header:\n");
+  Asciify(sanitized, header->Magic(), 8);
+  fprintf(out_file_, "magic               : '%s'\n", sanitized);
+  fprintf(out_file_, "checksum            : %08x\n", header->Checksum());
+  fprintf(out_file_, "signature           : %02x%02x...%02x%02x\n",
+          header->Signature()[0], header->Signature()[1],
+          header->Signature()[DexFile::kSha1DigestSize - 2],
+          header->Signature()[DexFile::kSha1DigestSize - 1]);
+  fprintf(out_file_, "file_size           : %d\n", header->FileSize());
+  fprintf(out_file_, "header_size         : %d\n", header->HeaderSize());
+  fprintf(out_file_, "link_size           : %d\n", header->LinkSize());
+  fprintf(out_file_, "link_off            : %d (0x%06x)\n",
+          header->LinkOffset(), header->LinkOffset());
+  fprintf(out_file_, "string_ids_size     : %d\n", header->StringIdsSize());
+  fprintf(out_file_, "string_ids_off      : %d (0x%06x)\n",
+          header->StringIdsOffset(), header->StringIdsOffset());
+  fprintf(out_file_, "type_ids_size       : %d\n", header->TypeIdsSize());
+  fprintf(out_file_, "type_ids_off        : %d (0x%06x)\n",
+          header->TypeIdsOffset(), header->TypeIdsOffset());
+  fprintf(out_file_, "proto_ids_size      : %d\n", header->ProtoIdsSize());
+  fprintf(out_file_, "proto_ids_off       : %d (0x%06x)\n",
+          header->ProtoIdsOffset(), header->ProtoIdsOffset());
+  fprintf(out_file_, "field_ids_size      : %d\n", header->FieldIdsSize());
+  fprintf(out_file_, "field_ids_off       : %d (0x%06x)\n",
+          header->FieldIdsOffset(), header->FieldIdsOffset());
+  fprintf(out_file_, "method_ids_size     : %d\n", header->MethodIdsSize());
+  fprintf(out_file_, "method_ids_off      : %d (0x%06x)\n",
+          header->MethodIdsOffset(), header->MethodIdsOffset());
+  fprintf(out_file_, "class_defs_size     : %d\n", header->ClassDefsSize());
+  fprintf(out_file_, "class_defs_off      : %d (0x%06x)\n",
+          header->ClassDefsOffset(), header->ClassDefsOffset());
+  fprintf(out_file_, "data_size           : %d\n", header->DataSize());
+  fprintf(out_file_, "data_off            : %d (0x%06x)\n\n",
+          header->DataOffset(), header->DataOffset());
+}
+
+/*
+ * Dumps a class_def_item.
+ */
+static void DumpClassDef(dex_ir::Header* header, int idx) {
+  // General class information.
+  dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
+  fprintf(out_file_, "Class #%d header:\n", idx);
+  fprintf(out_file_, "class_idx           : %d\n", class_def->ClassType()->GetOffset());
+  fprintf(out_file_, "access_flags        : %d (0x%04x)\n",
+          class_def->GetAccessFlags(), class_def->GetAccessFlags());
+  fprintf(out_file_, "superclass_idx      : %d\n", class_def->Superclass()->GetOffset());
+  fprintf(out_file_, "interfaces_off      : %d (0x%06x)\n",
+          class_def->InterfacesOffset(), class_def->InterfacesOffset());
+  uint32_t source_file_offset = 0xffffffffU;
+  if (class_def->SourceFile() != nullptr) {
+    source_file_offset = class_def->SourceFile()->GetOffset();
+  }
+  fprintf(out_file_, "source_file_idx     : %d\n", source_file_offset);
+  uint32_t annotations_offset = 0;
+  if (class_def->Annotations() != nullptr) {
+    annotations_offset = class_def->Annotations()->GetOffset();
+  }
+  fprintf(out_file_, "annotations_off     : %d (0x%06x)\n",
+          annotations_offset, annotations_offset);
+  fprintf(out_file_, "class_data_off      : %d (0x%06x)\n",
+          class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
+
+  // Fields and methods.
+  dex_ir::ClassData* class_data = class_def->GetClassData();
+  if (class_data != nullptr) {
+    fprintf(out_file_, "static_fields_size  : %zu\n", class_data->StaticFields().size());
+    fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields().size());
+    fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods().size());
+    fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods().size());
+  } else {
+    fprintf(out_file_, "static_fields_size  : 0\n");
+    fprintf(out_file_, "instance_fields_size: 0\n");
+    fprintf(out_file_, "direct_methods_size : 0\n");
+    fprintf(out_file_, "virtual_methods_size: 0\n");
+  }
+  fprintf(out_file_, "\n");
+}
+
+/**
+ * Dumps an annotation set item.
+ */
+static void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
+  if (set_item == nullptr || set_item->GetItems().size() == 0) {
+    fputs("  empty-annotation-set\n", out_file_);
+    return;
+  }
+  for (std::unique_ptr<dex_ir::AnnotationSetItem::AnnotationItem>& annotation :
+       set_item->GetItems()) {
+    if (annotation == nullptr) {
+      continue;
+    }
+    fputs("  ", out_file_);
+    switch (annotation->GetVisibility()) {
+      case DexFile::kDexVisibilityBuild:   fputs("VISIBILITY_BUILD ",   out_file_); break;
+      case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
+      case DexFile::kDexVisibilitySystem:  fputs("VISIBILITY_SYSTEM ",  out_file_); break;
+      default:                             fputs("VISIBILITY_UNKNOWN ", out_file_); break;
+    }  // switch
+    // Decode raw bytes in annotation.
+    // const uint8_t* rData = annotation->annotation_;
+    dex_ir::ArrayItem* data = annotation->GetItem();
+    DumpEncodedValue(data);
+    fputc('\n', out_file_);
+  }
+}
+
+/*
+ * Dumps class annotations.
+ */
+static void DumpClassAnnotations(dex_ir::Header* header, int idx) {
+  dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
+  dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
+  if (annotations_directory == nullptr) {
+    return;  // none
+  }
+
+  fprintf(out_file_, "Class #%d annotations:\n", idx);
+
+  dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
+  std::vector<std::unique_ptr<dex_ir::AnnotationsDirectoryItem::FieldAnnotation>>& fields =
+      annotations_directory->GetFieldAnnotations();
+  std::vector<std::unique_ptr<dex_ir::AnnotationsDirectoryItem::MethodAnnotation>>& methods =
+      annotations_directory->GetMethodAnnotations();
+  std::vector<std::unique_ptr<dex_ir::AnnotationsDirectoryItem::ParameterAnnotation>>& parameters =
+      annotations_directory->GetParameterAnnotations();
+
+  // Annotations on the class itself.
+  if (class_set_item != nullptr) {
+    fprintf(out_file_, "Annotations on class\n");
+    DumpAnnotationSetItem(class_set_item);
+  }
+
+  // Annotations on fields.
+  for (auto& field : fields) {
+    const dex_ir::FieldId* field_id = field->GetFieldId();
+    const uint32_t field_idx = field_id->GetOffset();
+    const char* field_name = field_id->Name()->Data();
+    fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
+    DumpAnnotationSetItem(field->GetAnnotationSetItem());
+  }
+
+  // Annotations on methods.
+  for (auto& method : methods) {
+    const dex_ir::MethodId* method_id = method->GetMethodId();
+    const uint32_t method_idx = method_id->GetOffset();
+    const char* method_name = method_id->Name()->Data();
+    fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
+    DumpAnnotationSetItem(method->GetAnnotationSetItem());
+  }
+
+  // Annotations on method parameters.
+  for (auto& parameter : parameters) {
+    const dex_ir::MethodId* method_id = parameter->GetMethodId();
+    const uint32_t method_idx = method_id->GetOffset();
+    const char* method_name = method_id->Name()->Data();
+    fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
+    uint32_t j = 0;
+    for (auto& annotation : parameter->GetAnnotations()) {
+      fprintf(out_file_, "#%u\n", j);
+      DumpAnnotationSetItem(annotation.get());
+      ++j;
+    }
+  }
+
+  fputc('\n', out_file_);
+}
+
+/*
+ * Dumps an interface that a class declares to implement.
+ */
+static void DumpInterface(dex_ir::TypeId* type_item, int i) {
+  const char* interface_name = type_item->GetStringId()->Data();
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "    #%d              : '%s'\n", i, interface_name);
+  } else {
+    std::string dot(DescriptorToDot(interface_name));
+    fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
+  }
+}
+
+/*
+ * Dumps the catches table associated with the code.
+ */
+static void DumpCatches(const dex_ir::CodeItem* code) {
+  const uint16_t tries_size = code->TriesSize();
+
+  // No catch table.
+  if (tries_size == 0) {
+    fprintf(out_file_, "      catches       : (none)\n");
+    return;
+  }
+
+  // Dump all table entries.
+  fprintf(out_file_, "      catches       : %d\n", tries_size);
+  std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
+  for (uint32_t i = 0; i < tries_size; i++) {
+    const dex_ir::TryItem* try_item = (*tries)[i].get();
+    const uint32_t start = try_item->StartAddr();
+    const uint32_t end = start + try_item->InsnCount();
+    fprintf(out_file_, "        0x%04x - 0x%04x\n", start, end);
+    for (auto& handler : try_item->GetHandlers()) {
+      const dex_ir::TypeId* type_id = handler->GetTypeId();
+      const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
+      fprintf(out_file_, "          %s -> 0x%04x\n", descriptor, handler->GetAddress());
+    }  // for
+  }  // for
+}
+
+/*
+ * Dumps all positions table entries associated with the code.
+ */
+static void DumpPositionInfo(const dex_ir::CodeItem* code) {
+  dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
+  if (debug_info == nullptr) {
+    return;
+  }
+  std::vector<std::unique_ptr<dex_ir::PositionInfo>>& positions = debug_info->GetPositionInfo();
+  for (size_t i = 0; i < positions.size(); ++i) {
+    fprintf(out_file_, "        0x%04x line=%d\n", positions[i]->address_, positions[i]->line_);
+  }
+}
+
+/*
+ * Dumps all locals table entries associated with the code.
+ */
+static void DumpLocalInfo(const dex_ir::CodeItem* code) {
+  dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
+  if (debug_info == nullptr) {
+    return;
+  }
+  std::vector<std::unique_ptr<dex_ir::LocalInfo>>& locals = debug_info->GetLocalInfo();
+  for (size_t i = 0; i < locals.size(); ++i) {
+    dex_ir::LocalInfo* entry = locals[i].get();
+    fprintf(out_file_, "        0x%04x - 0x%04x reg=%d %s %s %s\n",
+            entry->start_address_, entry->end_address_, entry->reg_,
+            entry->name_.c_str(), entry->descriptor_.c_str(), entry->signature_.c_str());
+  }
+}
+
+/*
+ * Helper for dumpInstruction(), which builds the string
+ * representation for the index in the given instruction.
+ * Returns a pointer to a buffer of sufficient size.
+ */
+static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
+                                           const Instruction* dec_insn,
+                                           size_t buf_size) {
+  std::unique_ptr<char[]> buf(new char[buf_size]);
+  // Determine index and width of the string.
+  uint32_t index = 0;
+  uint32_t width = 4;
+  switch (Instruction::FormatOf(dec_insn->Opcode())) {
+    // SOME NOT SUPPORTED:
+    // case Instruction::k20bc:
+    case Instruction::k21c:
+    case Instruction::k35c:
+    // case Instruction::k35ms:
+    case Instruction::k3rc:
+    // case Instruction::k3rms:
+    // case Instruction::k35mi:
+    // case Instruction::k3rmi:
+      index = dec_insn->VRegB();
+      width = 4;
+      break;
+    case Instruction::k31c:
+      index = dec_insn->VRegB();
+      width = 8;
+      break;
+    case Instruction::k22c:
+    // case Instruction::k22cs:
+      index = dec_insn->VRegC();
+      width = 4;
+      break;
+    default:
+      break;
+  }  // switch
+
+  // Determine index type.
+  size_t outSize = 0;
+  switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
+    case Instruction::kIndexUnknown:
+      // This function should never get called for this type, but do
+      // something sensible here, just to help with debugging.
+      outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
+      break;
+    case Instruction::kIndexNone:
+      // This function should never get called for this type, but do
+      // something sensible here, just to help with debugging.
+      outSize = snprintf(buf.get(), buf_size, "<no-index>");
+      break;
+    case Instruction::kIndexTypeRef:
+      if (index < header->TypeIdsSize()) {
+        const char* tp = header->TypeIds()[index]->GetStringId()->Data();
+        outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
+      } else {
+        outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
+      }
+      break;
+    case Instruction::kIndexStringRef:
+      if (index < header->StringIdsSize()) {
+        const char* st = header->StringIds()[index]->Data();
+        outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
+      } else {
+        outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
+      }
+      break;
+    case Instruction::kIndexMethodRef:
+      if (index < header->MethodIdsSize()) {
+        dex_ir::MethodId* method_id = header->MethodIds()[index].get();
+        const char* name = method_id->Name()->Data();
+        char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
+        const char* back_descriptor = method_id->Class()->GetStringId()->Data();
+        outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
+                           back_descriptor, name, type_descriptor, width, index);
+      } else {
+        outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
+      }
+      break;
+    case Instruction::kIndexFieldRef:
+      if (index < header->FieldIdsSize()) {
+        dex_ir::FieldId* field_id = header->FieldIds()[index].get();
+        const char* name = field_id->Name()->Data();
+        const char* type_descriptor = field_id->Type()->GetStringId()->Data();
+        const char* back_descriptor = field_id->Class()->GetStringId()->Data();
+        outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
+                           back_descriptor, name, type_descriptor, width, index);
+      } else {
+        outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
+      }
+      break;
+    case Instruction::kIndexVtableOffset:
+      outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
+                         width, index, width, index);
+      break;
+    case Instruction::kIndexFieldOffset:
+      outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
+      break;
+    // SOME NOT SUPPORTED:
+    // case Instruction::kIndexVaries:
+    // case Instruction::kIndexInlineMethod:
+    default:
+      outSize = snprintf(buf.get(), buf_size, "<?>");
+      break;
+  }  // switch
+
+  // Determine success of string construction.
+  if (outSize >= buf_size) {
+    // The buffer wasn't big enough; retry with computed size. Note: snprintf()
+    // doesn't count/ the '\0' as part of its returned size, so we add explicit
+    // space for it here.
+    return IndexString(header, dec_insn, outSize + 1);
+  }
+  return buf;
+}
+
+/*
+ * Dumps a single instruction.
+ */
+static void DumpInstruction(dex_ir::Header* header, const dex_ir::CodeItem* code,
+                            uint32_t code_offset, uint32_t insn_idx, uint32_t insn_width,
+                            const Instruction* dec_insn) {
+  // Address of instruction (expressed as byte offset).
+  fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
+
+  // Dump (part of) raw bytes.
+  const uint16_t* insns = code->Insns();
+  for (uint32_t i = 0; i < 8; i++) {
+    if (i < insn_width) {
+      if (i == 7) {
+        fprintf(out_file_, " ... ");
+      } else {
+        // Print 16-bit value in little-endian order.
+        const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
+        fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
+      }
+    } else {
+      fputs("     ", out_file_);
+    }
+  }  // for
+
+  // Dump pseudo-instruction or opcode.
+  if (dec_insn->Opcode() == Instruction::NOP) {
+    const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
+    if (instr == Instruction::kPackedSwitchSignature) {
+      fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
+    } else if (instr == Instruction::kSparseSwitchSignature) {
+      fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
+    } else if (instr == Instruction::kArrayDataSignature) {
+      fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
+    } else {
+      fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
+    }
+  } else {
+    fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
+  }
+
+  // Set up additional argument.
+  std::unique_ptr<char[]> index_buf;
+  if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
+    index_buf = IndexString(header, dec_insn, 200);
+  }
+
+  // Dump the instruction.
+  //
+  // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
+  //
+  switch (Instruction::FormatOf(dec_insn->Opcode())) {
+    case Instruction::k10x:        // op
+      break;
+    case Instruction::k12x:        // op vA, vB
+      fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
+      break;
+    case Instruction::k11n:        // op vA, #+B
+      fprintf(out_file_, " v%d, #int %d // #%x",
+              dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
+      break;
+    case Instruction::k11x:        // op vAA
+      fprintf(out_file_, " v%d", dec_insn->VRegA());
+      break;
+    case Instruction::k10t:        // op +AA
+    case Instruction::k20t: {      // op +AAAA
+      const int32_t targ = (int32_t) dec_insn->VRegA();
+      fprintf(out_file_, " %04x // %c%04x",
+              insn_idx + targ,
+              (targ < 0) ? '-' : '+',
+              (targ < 0) ? -targ : targ);
+      break;
+    }
+    case Instruction::k22x:        // op vAA, vBBBB
+      fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
+      break;
+    case Instruction::k21t: {     // op vAA, +BBBB
+      const int32_t targ = (int32_t) dec_insn->VRegB();
+      fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
+              insn_idx + targ,
+              (targ < 0) ? '-' : '+',
+              (targ < 0) ? -targ : targ);
+      break;
+    }
+    case Instruction::k21s:        // op vAA, #+BBBB
+      fprintf(out_file_, " v%d, #int %d // #%x",
+              dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
+      break;
+    case Instruction::k21h:        // op vAA, #+BBBB0000[00000000]
+      // The printed format varies a bit based on the actual opcode.
+      if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
+        const int32_t value = dec_insn->VRegB() << 16;
+        fprintf(out_file_, " v%d, #int %d // #%x",
+                dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
+      } else {
+        const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
+        fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
+                dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
+      }
+      break;
+    case Instruction::k21c:        // op vAA, thing@BBBB
+    case Instruction::k31c:        // op vAA, thing@BBBBBBBB
+      fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
+      break;
+    case Instruction::k23x:        // op vAA, vBB, vCC
+      fprintf(out_file_, " v%d, v%d, v%d",
+              dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
+      break;
+    case Instruction::k22b:        // op vAA, vBB, #+CC
+      fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
+              dec_insn->VRegA(), dec_insn->VRegB(),
+              (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
+      break;
+    case Instruction::k22t: {      // op vA, vB, +CCCC
+      const int32_t targ = (int32_t) dec_insn->VRegC();
+      fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
+              dec_insn->VRegA(), dec_insn->VRegB(),
+              insn_idx + targ,
+              (targ < 0) ? '-' : '+',
+              (targ < 0) ? -targ : targ);
+      break;
+    }
+    case Instruction::k22s:        // op vA, vB, #+CCCC
+      fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
+              dec_insn->VRegA(), dec_insn->VRegB(),
+              (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
+      break;
+    case Instruction::k22c:        // op vA, vB, thing@CCCC
+    // NOT SUPPORTED:
+    // case Instruction::k22cs:    // [opt] op vA, vB, field offset CCCC
+      fprintf(out_file_, " v%d, v%d, %s",
+              dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
+      break;
+    case Instruction::k30t:
+      fprintf(out_file_, " #%08x", dec_insn->VRegA());
+      break;
+    case Instruction::k31i: {     // op vAA, #+BBBBBBBB
+      // This is often, but not always, a float.
+      union {
+        float f;
+        uint32_t i;
+      } conv;
+      conv.i = dec_insn->VRegB();
+      fprintf(out_file_, " v%d, #float %g // #%08x",
+              dec_insn->VRegA(), conv.f, dec_insn->VRegB());
+      break;
+    }
+    case Instruction::k31t:       // op vAA, offset +BBBBBBBB
+      fprintf(out_file_, " v%d, %08x // +%08x",
+              dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
+      break;
+    case Instruction::k32x:        // op vAAAA, vBBBB
+      fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
+      break;
+    case Instruction::k35c: {      // op {vC, vD, vE, vF, vG}, thing@BBBB
+    // NOT SUPPORTED:
+    // case Instruction::k35ms:       // [opt] invoke-virtual+super
+    // case Instruction::k35mi:       // [opt] inline invoke
+      uint32_t arg[Instruction::kMaxVarArgRegs];
+      dec_insn->GetVarArgs(arg);
+      fputs(" {", out_file_);
+      for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
+        if (i == 0) {
+          fprintf(out_file_, "v%d", arg[i]);
+        } else {
+          fprintf(out_file_, ", v%d", arg[i]);
+        }
+      }  // for
+      fprintf(out_file_, "}, %s", index_buf.get());
+      break;
+    }
+    case Instruction::k3rc:        // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
+    // NOT SUPPORTED:
+    // case Instruction::k3rms:       // [opt] invoke-virtual+super/range
+    // case Instruction::k3rmi:       // [opt] execute-inline/range
+      {
+        // This doesn't match the "dx" output when some of the args are
+        // 64-bit values -- dx only shows the first register.
+        fputs(" {", out_file_);
+        for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
+          if (i == 0) {
+            fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
+          } else {
+            fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
+          }
+        }  // for
+        fprintf(out_file_, "}, %s", index_buf.get());
+      }
+      break;
+    case Instruction::k51l: {      // op vAA, #+BBBBBBBBBBBBBBBB
+      // This is often, but not always, a double.
+      union {
+        double d;
+        uint64_t j;
+      } conv;
+      conv.j = dec_insn->WideVRegB();
+      fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
+              dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
+      break;
+    }
+    // NOT SUPPORTED:
+    // case Instruction::k00x:        // unknown op or breakpoint
+    //    break;
+    default:
+      fprintf(out_file_, " ???");
+      break;
+  }  // switch
+
+  fputc('\n', out_file_);
+}
+
+/*
+ * Dumps a bytecode disassembly.
+ */
+static void DumpBytecodes(dex_ir::Header* header, uint32_t idx,
+                          const dex_ir::CodeItem* code, uint32_t code_offset) {
+  dex_ir::MethodId* method_id = header->MethodIds()[idx].get();
+  const char* name = method_id->Name()->Data();
+  const char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
+  const char* back_descriptor = method_id->Class()->GetStringId()->Data();
+
+  // Generate header.
+  std::string dot(DescriptorToDot(back_descriptor));
+  fprintf(out_file_, "%06x:                                        |[%06x] %s.%s:%s\n",
+          code_offset, code_offset, dot.c_str(), name, type_descriptor);
+
+  // Iterate over all instructions.
+  const uint16_t* insns = code->Insns();
+  for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) {
+    const Instruction* instruction = Instruction::At(&insns[insn_idx]);
+    const uint32_t insn_width = instruction->SizeInCodeUnits();
+    if (insn_width == 0) {
+      fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insn_idx);
+      break;
+    }
+    DumpInstruction(header, code, code_offset, insn_idx, insn_width, instruction);
+    insn_idx += insn_width;
+  }  // for
+}
+
+/*
+ * Dumps code of a method.
+ */
+static void DumpCode(dex_ir::Header* header, uint32_t idx, const dex_ir::CodeItem* code,
+                     uint32_t code_offset) {
+  fprintf(out_file_, "      registers     : %d\n", code->RegistersSize());
+  fprintf(out_file_, "      ins           : %d\n", code->InsSize());
+  fprintf(out_file_, "      outs          : %d\n", code->OutsSize());
+  fprintf(out_file_, "      insns size    : %d 16-bit code units\n",
+          code->InsnsSize());
+
+  // Bytecode disassembly, if requested.
+  if (options_.disassemble_) {
+    DumpBytecodes(header, idx, code, code_offset);
+  }
+
+  // Try-catch blocks.
+  DumpCatches(code);
+
+  // Positions and locals table in the debug info.
+  fprintf(out_file_, "      positions     : \n");
+  DumpPositionInfo(code);
+  fprintf(out_file_, "      locals        : \n");
+  DumpLocalInfo(code);
+}
+
+/*
+ * Dumps a method.
+ */
+static void DumpMethod(dex_ir::Header* header, uint32_t idx, uint32_t flags,
+                       const dex_ir::CodeItem* code, int i) {
+  // Bail for anything private if export only requested.
+  if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
+    return;
+  }
+
+  dex_ir::MethodId* method_id = header->MethodIds()[idx].get();
+  const char* name = method_id->Name()->Data();
+  char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
+  const char* back_descriptor = method_id->Class()->GetStringId()->Data();
+  char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
+
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "    #%d              : (in %s)\n", i, back_descriptor);
+    fprintf(out_file_, "      name          : '%s'\n", name);
+    fprintf(out_file_, "      type          : '%s'\n", type_descriptor);
+    fprintf(out_file_, "      access        : 0x%04x (%s)\n", flags, access_str);
+    if (code == nullptr) {
+      fprintf(out_file_, "      code          : (none)\n");
+    } else {
+      fprintf(out_file_, "      code          -\n");
+      DumpCode(header, idx, code, code->GetOffset());
+    }
+    if (options_.disassemble_) {
+      fputc('\n', out_file_);
+    }
+  } else if (options_.output_format_ == kOutputXml) {
+    const bool constructor = (name[0] == '<');
+
+    // Method name and prototype.
+    if (constructor) {
+      std::string dot(DescriptorClassToDot(back_descriptor));
+      fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
+      dot = DescriptorToDot(back_descriptor);
+      fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
+    } else {
+      fprintf(out_file_, "<method name=\"%s\"\n", name);
+      const char* return_type = strrchr(type_descriptor, ')');
+      if (return_type == nullptr) {
+        fprintf(stderr, "bad method type descriptor '%s'\n", type_descriptor);
+        goto bail;
+      }
+      std::string dot(DescriptorToDot(return_type + 1));
+      fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
+      fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
+      fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
+      fprintf(out_file_, " synchronized=%s\n", QuotedBool(
+          (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
+    }
+
+    // Additional method flags.
+    fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
+    fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
+    // The "deprecated=" not knowable w/o parsing annotations.
+    fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
+
+    // Parameters.
+    if (type_descriptor[0] != '(') {
+      fprintf(stderr, "ERROR: bad descriptor '%s'\n", type_descriptor);
+      goto bail;
+    }
+    char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
+    const char* base = type_descriptor + 1;
+    int arg_num = 0;
+    while (*base != ')') {
+      char* cp = tmp_buf;
+      while (*base == '[') {
+        *cp++ = *base++;
+      }
+      if (*base == 'L') {
+        // Copy through ';'.
+        do {
+          *cp = *base++;
+        } while (*cp++ != ';');
+      } else {
+        // Primitive char, copy it.
+        if (strchr("ZBCSIFJD", *base) == nullptr) {
+          fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
+          break;  // while
+        }
+        *cp++ = *base++;
+      }
+      // Null terminate and display.
+      *cp++ = '\0';
+      std::string dot(DescriptorToDot(tmp_buf));
+      fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
+                        "</parameter>\n", arg_num++, dot.c_str());
+    }  // while
+    free(tmp_buf);
+    if (constructor) {
+      fprintf(out_file_, "</constructor>\n");
+    } else {
+      fprintf(out_file_, "</method>\n");
+    }
+  }
+
+ bail:
+  free(type_descriptor);
+  free(access_str);
+}
+
+/*
+ * Dumps a static (class) field.
+ */
+static void DumpSField(dex_ir::Header* header, uint32_t idx, uint32_t flags,
+                       int i, dex_ir::ArrayItem* init) {
+  // Bail for anything private if export only requested.
+  if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
+    return;
+  }
+
+  dex_ir::FieldId* field_id = header->FieldIds()[idx].get();
+  const char* name = field_id->Name()->Data();
+  const char* type_descriptor = field_id->Type()->GetStringId()->Data();
+  const char* back_descriptor = field_id->Class()->GetStringId()->Data();
+  char* access_str = CreateAccessFlagStr(flags, kAccessForField);
+
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "    #%d              : (in %s)\n", i, back_descriptor);
+    fprintf(out_file_, "      name          : '%s'\n", name);
+    fprintf(out_file_, "      type          : '%s'\n", type_descriptor);
+    fprintf(out_file_, "      access        : 0x%04x (%s)\n", flags, access_str);
+    if (init != nullptr) {
+      fputs("      value         : ", out_file_);
+      DumpEncodedValue(init);
+      fputs("\n", out_file_);
+    }
+  } else if (options_.output_format_ == kOutputXml) {
+    fprintf(out_file_, "<field name=\"%s\"\n", name);
+    std::string dot(DescriptorToDot(type_descriptor));
+    fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
+    fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
+    fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
+    // The "value=" is not knowable w/o parsing annotations.
+    fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
+    fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
+    // The "deprecated=" is not knowable w/o parsing annotations.
+    fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
+    if (init != nullptr) {
+      fputs(" value=\"", out_file_);
+      DumpEncodedValue(init);
+      fputs("\"\n", out_file_);
+    }
+    fputs(">\n</field>\n", out_file_);
+  }
+
+  free(access_str);
+}
+
+/*
+ * Dumps an instance field.
+ */
+static void DumpIField(dex_ir::Header* header, uint32_t idx, uint32_t flags, int i) {
+  DumpSField(header, idx, flags, i, nullptr);
+}
+
+/*
+ * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
+ * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
+ * tool, so this is not performance-critical.
+ */
+
+static void DumpCFG(const DexFile* dex_file,
+                    uint32_t dex_method_idx,
+                    const DexFile::CodeItem* code) {
+  if (code != nullptr) {
+    std::ostringstream oss;
+    DumpMethodCFG(dex_file, dex_method_idx, oss);
+    fprintf(out_file_, "%s", oss.str().c_str());
+  }
+}
+
+static void DumpCFG(const DexFile* dex_file, int idx) {
+  const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
+  const uint8_t* class_data = dex_file->GetClassData(class_def);
+  if (class_data == nullptr) {  // empty class such as a marker interface?
+    return;
+  }
+  ClassDataItemIterator it(*dex_file, class_data);
+  while (it.HasNextStaticField()) {
+    it.Next();
+  }
+  while (it.HasNextInstanceField()) {
+    it.Next();
+  }
+  while (it.HasNextDirectMethod()) {
+    DumpCFG(dex_file,
+            it.GetMemberIndex(),
+            it.GetMethodCodeItem());
+    it.Next();
+  }
+  while (it.HasNextVirtualMethod()) {
+    DumpCFG(dex_file,
+                it.GetMemberIndex(),
+                it.GetMethodCodeItem());
+    it.Next();
+  }
+}
+
+/*
+ * Dumps the class.
+ *
+ * Note "idx" is a DexClassDef index, not a DexTypeId index.
+ *
+ * If "*last_package" is nullptr or does not match the current class' package,
+ * the value will be replaced with a newly-allocated string.
+ */
+static void DumpClass(dex_ir::Header* header, int idx, char** last_package) {
+  dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
+  // Omitting non-public class.
+  if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
+    return;
+  }
+
+  if (options_.show_section_headers_) {
+    DumpClassDef(header, idx);
+  }
+
+  if (options_.show_annotations_) {
+    DumpClassAnnotations(header, idx);
+  }
+
+  if (options_.show_cfg_) {
+    DumpCFG(&header->GetDexFile(), idx);
+    return;
+  }
+
+  // For the XML output, show the package name.  Ideally we'd gather
+  // up the classes, sort them, and dump them alphabetically so the
+  // package name wouldn't jump around, but that's not a great plan
+  // for something that needs to run on the device.
+  const char* class_descriptor = header->ClassDefs()[idx]->ClassType()->GetStringId()->Data();
+  if (!(class_descriptor[0] == 'L' &&
+        class_descriptor[strlen(class_descriptor)-1] == ';')) {
+    // Arrays and primitives should not be defined explicitly. Keep going?
+    fprintf(stderr, "Malformed class name '%s'\n", class_descriptor);
+  } else if (options_.output_format_ == kOutputXml) {
+    char* mangle = strdup(class_descriptor + 1);
+    mangle[strlen(mangle)-1] = '\0';
+
+    // Reduce to just the package name.
+    char* last_slash = strrchr(mangle, '/');
+    if (last_slash != nullptr) {
+      *last_slash = '\0';
+    } else {
+      *mangle = '\0';
+    }
+
+    for (char* cp = mangle; *cp != '\0'; cp++) {
+      if (*cp == '/') {
+        *cp = '.';
+      }
+    }  // for
+
+    if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
+      // Start of a new package.
+      if (*last_package != nullptr) {
+        fprintf(out_file_, "</package>\n");
+      }
+      fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
+      free(*last_package);
+      *last_package = mangle;
+    } else {
+      free(mangle);
+    }
+  }
+
+  // General class information.
+  char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
+  const char* superclass_descriptor = nullptr;
+  if (class_def->Superclass() != nullptr) {
+    superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
+  }
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "Class #%d            -\n", idx);
+    fprintf(out_file_, "  Class descriptor  : '%s'\n", class_descriptor);
+    fprintf(out_file_, "  Access flags      : 0x%04x (%s)\n",
+            class_def->GetAccessFlags(), access_str);
+    if (superclass_descriptor != nullptr) {
+      fprintf(out_file_, "  Superclass        : '%s'\n", superclass_descriptor);
+    }
+    fprintf(out_file_, "  Interfaces        -\n");
+  } else {
+    std::string dot(DescriptorClassToDot(class_descriptor));
+    fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
+    if (superclass_descriptor != nullptr) {
+      dot = DescriptorToDot(superclass_descriptor);
+      fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
+    }
+    fprintf(out_file_, " interface=%s\n",
+            QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
+    fprintf(out_file_, " abstract=%s\n",
+            QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
+    fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
+    fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
+    // The "deprecated=" not knowable w/o parsing annotations.
+    fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
+    fprintf(out_file_, ">\n");
+  }
+
+  // Interfaces.
+  std::vector<dex_ir::TypeId*>* interfaces = class_def->Interfaces();
+  for (uint32_t i = 0; i < interfaces->size(); i++) {
+    DumpInterface((*interfaces)[i], i);
+  }  // for
+
+  // Fields and methods.
+  dex_ir::ClassData* class_data = class_def->GetClassData();
+  // Prepare data for static fields.
+  std::vector<std::unique_ptr<dex_ir::ArrayItem>>* static_values = class_def->StaticValues();
+  const uint32_t static_values_size = (static_values == nullptr) ? 0 : static_values->size();
+
+  // Static fields.
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "  Static fields     -\n");
+  }
+  std::vector<std::unique_ptr<dex_ir::FieldItem>>& static_fields = class_data->StaticFields();
+  for (uint32_t i = 0; i < static_fields.size(); i++) {
+    DumpSField(header,
+               static_fields[i]->GetFieldId()->GetOffset(),
+               static_fields[i]->GetAccessFlags(),
+               i,
+               i < static_values_size ? (*static_values)[i].get() : nullptr);
+  }  // for
+
+  // Instance fields.
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "  Instance fields   -\n");
+  }
+  std::vector<std::unique_ptr<dex_ir::FieldItem>>& instance_fields = class_data->InstanceFields();
+  for (uint32_t i = 0; i < instance_fields.size(); i++) {
+    DumpIField(header,
+               instance_fields[i]->GetFieldId()->GetOffset(),
+               instance_fields[i]->GetAccessFlags(),
+               i);
+  }  // for
+
+  // Direct methods.
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "  Direct methods    -\n");
+  }
+  std::vector<std::unique_ptr<dex_ir::MethodItem>>& direct_methods = class_data->DirectMethods();
+  for (uint32_t i = 0; i < direct_methods.size(); i++) {
+    DumpMethod(header,
+               direct_methods[i]->GetMethodId()->GetOffset(),
+               direct_methods[i]->GetAccessFlags(),
+               direct_methods[i]->GetCodeItem(),
+               i);
+  }  // for
+
+  // Virtual methods.
+  if (options_.output_format_ == kOutputPlain) {
+    fprintf(out_file_, "  Virtual methods   -\n");
+  }
+  std::vector<std::unique_ptr<dex_ir::MethodItem>>& virtual_methods = class_data->VirtualMethods();
+  for (uint32_t i = 0; i < virtual_methods.size(); i++) {
+    DumpMethod(header,
+               virtual_methods[i]->GetMethodId()->GetOffset(),
+               virtual_methods[i]->GetAccessFlags(),
+               virtual_methods[i]->GetCodeItem(),
+               i);
+  }  // for
+
+  // End of class.
+  if (options_.output_format_ == kOutputPlain) {
+    const char* file_name = "unknown";
+    if (class_def->SourceFile() != nullptr) {
+      file_name = class_def->SourceFile()->Data();
+    }
+    const dex_ir::StringId* source_file = class_def->SourceFile();
+    fprintf(out_file_, "  source_file_idx   : %d (%s)\n\n",
+            source_file == nullptr ? 0xffffffffU : source_file->GetOffset(), file_name);
+  } else if (options_.output_format_ == kOutputXml) {
+    fprintf(out_file_, "</class>\n");
+  }
+
+  free(access_str);
+}
+
+/*
+ * Dumps the requested sections of the file.
+ */
+static void ProcessDexFile(const char* file_name, const DexFile* dex_file) {
+  if (options_.verbose_) {
+    fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
+            file_name, dex_file->GetHeader().magic_ + 4);
+  }
+  dex_ir::Header header(*dex_file);
+
+  // Headers.
+  if (options_.show_file_headers_) {
+    DumpFileHeader(&header);
+  }
+
+  // Open XML context.
+  if (options_.output_format_ == kOutputXml) {
+    fprintf(out_file_, "<api>\n");
+  }
+
+  // Iterate over all classes.
+  char* package = nullptr;
+  const uint32_t class_defs_size = header.ClassDefsSize();
+  for (uint32_t i = 0; i < class_defs_size; i++) {
+    DumpClass(&header, i, &package);
+  }  // for
+
+  // Free the last package allocated.
+  if (package != nullptr) {
+    fprintf(out_file_, "</package>\n");
+    free(package);
+  }
+
+  // Close XML context.
+  if (options_.output_format_ == kOutputXml) {
+    fprintf(out_file_, "</api>\n");
+  }
+}
+
+/*
+ * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
+ */
+int ProcessFile(const char* file_name) {
+  if (options_.verbose_) {
+    fprintf(out_file_, "Processing '%s'...\n", file_name);
+  }
+
+  // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
+  // all of which are Zip archives with "classes.dex" inside.
+  const bool verify_checksum = !options_.ignore_bad_checksum_;
+  std::string error_msg;
+  std::vector<std::unique_ptr<const DexFile>> dex_files;
+  if (!DexFile::Open(file_name, file_name, verify_checksum, &error_msg, &dex_files)) {
+    // Display returned error message to user. Note that this error behavior
+    // differs from the error messages shown by the original Dalvik dexdump.
+    fputs(error_msg.c_str(), stderr);
+    fputc('\n', stderr);
+    return -1;
+  }
+
+  // Success. Either report checksum verification or process
+  // all dex files found in given file.
+  if (options_.checksum_only_) {
+    fprintf(out_file_, "Checksum verified\n");
+  } else {
+    for (size_t i = 0; i < dex_files.size(); i++) {
+      ProcessDexFile(file_name, dex_files[i].get());
+    }
+  }
+  return 0;
+}
+
+}  // namespace art
diff --git a/dexlayout/dexlayout.h b/dexlayout/dexlayout.h
new file mode 100644
index 0000000..bae587d
--- /dev/null
+++ b/dexlayout/dexlayout.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Header file of the dexlayout utility.
+ *
+ * This is a tool to read dex files into an internal representation,
+ * reorganize the representation, and emit dex files with a better
+ * file layout.
+ */
+
+#ifndef ART_DEXLAYOUT_DEXLAYOUT_H_
+#define ART_DEXLAYOUT_DEXLAYOUT_H_
+
+#include <stdint.h>
+#include <stdio.h>
+
+namespace art {
+
+/* Supported output formats. */
+enum OutputFormat {
+  kOutputPlain = 0,  // default
+  kOutputXml,        // XML-style
+};
+
+/* Command-line options. */
+struct Options {
+  bool build_dex_ir_;
+  bool checksum_only_;
+  bool disassemble_;
+  bool exports_only_;
+  bool ignore_bad_checksum_;
+  bool show_annotations_;
+  bool show_cfg_;
+  bool show_file_headers_;
+  bool show_section_headers_;
+  bool verbose_;
+  OutputFormat output_format_;
+  const char* output_file_name_;
+};
+
+/* Prototypes. */
+extern struct Options options_;
+extern FILE* out_file_;
+int ProcessFile(const char* file_name);
+
+}  // namespace art
+
+#endif  // ART_DEXLAYOUT_DEXLAYOUT_H_
diff --git a/dexlayout/dexlayout_main.cc b/dexlayout/dexlayout_main.cc
new file mode 100644
index 0000000..286a0c6
--- /dev/null
+++ b/dexlayout/dexlayout_main.cc
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Main driver of the dexlayout utility.
+ *
+ * This is a tool to read dex files into an internal representation,
+ * reorganize the representation, and emit dex files with a better
+ * file layout.
+ */
+
+#include "dexlayout.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "mem_map.h"
+#include "runtime.h"
+
+namespace art {
+
+static const char* kProgramName = "dexlayout";
+
+/*
+ * Shows usage.
+ */
+static void Usage(void) {
+  fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
+  fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile]"
+                  " dexfile...\n\n", kProgramName);
+  fprintf(stderr, " -a : display annotations\n");
+  fprintf(stderr, " -b : build dex_ir\n");
+  fprintf(stderr, " -c : verify checksum and exit\n");
+  fprintf(stderr, " -d : disassemble code sections\n");
+  fprintf(stderr, " -e : display exported items only\n");
+  fprintf(stderr, " -f : display summary information from file header\n");
+  fprintf(stderr, " -g : display CFG for dex\n");
+  fprintf(stderr, " -h : display file header details\n");
+  fprintf(stderr, " -i : ignore checksum failures\n");
+  fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
+  fprintf(stderr, " -o : output file name (defaults to stdout)\n");
+}
+
+/*
+ * Main driver of the dexlayout utility.
+ */
+int DexlayoutDriver(int argc, char** argv) {
+  // Art specific set up.
+  InitLogging(argv);
+  MemMap::Init();
+
+  // Reset options.
+  bool want_usage = false;
+  memset(&options_, 0, sizeof(options_));
+  options_.verbose_ = true;
+
+  // Parse all arguments.
+  while (1) {
+    const int ic = getopt(argc, argv, "abcdefghil:o:");
+    if (ic < 0) {
+      break;  // done
+    }
+    switch (ic) {
+      case 'a':  // display annotations
+        options_.show_annotations_ = true;
+        break;
+      case 'b':  // build dex_ir
+        options_.build_dex_ir_ = true;
+        break;
+      case 'c':  // verify the checksum then exit
+        options_.checksum_only_ = true;
+        break;
+      case 'd':  // disassemble Dalvik instructions
+        options_.disassemble_ = true;
+        break;
+      case 'e':  // exported items only
+        options_.exports_only_ = true;
+        break;
+      case 'f':  // display outer file header
+        options_.show_file_headers_ = true;
+        break;
+      case 'g':  // display cfg
+        options_.show_cfg_ = true;
+        break;
+      case 'h':  // display section headers, i.e. all meta-data
+        options_.show_section_headers_ = true;
+        break;
+      case 'i':  // continue even if checksum is bad
+        options_.ignore_bad_checksum_ = true;
+        break;
+      case 'l':  // layout
+        if (strcmp(optarg, "plain") == 0) {
+          options_.output_format_ = kOutputPlain;
+        } else if (strcmp(optarg, "xml") == 0) {
+          options_.output_format_ = kOutputXml;
+          options_.verbose_ = false;
+        } else {
+          want_usage = true;
+        }
+        break;
+      case 'o':  // output file
+        options_.output_file_name_ = optarg;
+        break;
+      default:
+        want_usage = true;
+        break;
+    }  // switch
+  }  // while
+
+  // Detect early problems.
+  if (optind == argc) {
+    fprintf(stderr, "%s: no file specified\n", kProgramName);
+    want_usage = true;
+  }
+  if (options_.checksum_only_ && options_.ignore_bad_checksum_) {
+    fprintf(stderr, "Can't specify both -c and -i\n");
+    want_usage = true;
+  }
+  if (want_usage) {
+    Usage();
+    return 2;
+  }
+
+  // Open alternative output file.
+  if (options_.output_file_name_) {
+    out_file_ = fopen(options_.output_file_name_, "w");
+    if (!out_file_) {
+      fprintf(stderr, "Can't open %s\n", options_.output_file_name_);
+      return 1;
+    }
+  }
+
+  // Process all files supplied on command line.
+  int result = 0;
+  while (optind < argc) {
+    result |= ProcessFile(argv[optind++]);
+  }  // while
+  return result != 0;
+}
+
+}  // namespace art
+
+int main(int argc, char** argv) {
+  return art::DexlayoutDriver(argc, argv);
+}