Create libelffile library for ELF file manipulation.

Move some of our tooling to library to make it reusable.

Remove MIPS support from the ELF builder.  This is slightly
easier than making it independent of the runtime.

Bug: 110133331
Test: test.py -b --host
Change-Id: I93343808d0e27ee8e1117e713a2503e8179fc245
diff --git a/compiler/Android.bp b/compiler/Android.bp
index 341727d..4ad59e1 100644
--- a/compiler/Android.bp
+++ b/compiler/Android.bp
@@ -26,17 +26,12 @@
     srcs: [
         "compiled_method.cc",
         "debug/elf_debug_writer.cc",
-        "debug/xz_utils.cc",
         "dex/inline_method_analyser.cc",
         "dex/verified_method.cc",
         "dex/verification_results.cc",
         "driver/compiled_method_storage.cc",
         "driver/compiler_options.cc",
         "driver/dex_compilation_unit.cc",
-        "linker/buffered_output_stream.cc",
-        "linker/file_output_stream.cc",
-        "linker/output_stream.cc",
-        "linker/vector_output_stream.cc",
         "jit/jit_compiler.cc",
         "jit/jit_logger.cc",
         "jni/quick/calling_convention.cc",
@@ -254,6 +249,7 @@
         "libprofile",
         "libdexfile",
     ],
+    whole_static_libs: ["libelffile"],
 
     target: {
         android: {
@@ -317,6 +313,7 @@
         "libprofiled",
         "libdexfiled",
     ],
+    whole_static_libs: ["libelffiled"],
 }
 
 cc_defaults {
diff --git a/compiler/cfi_test.h b/compiler/cfi_test.h
index 2fc81c9..9755ef1 100644
--- a/compiler/cfi_test.h
+++ b/compiler/cfi_test.h
@@ -23,10 +23,10 @@
 
 #include "arch/instruction_set.h"
 #include "base/enums.h"
-#include "debug/dwarf/dwarf_constants.h"
 #include "debug/dwarf/dwarf_test.h"
-#include "debug/dwarf/headers.h"
 #include "disassembler.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/headers.h"
 #include "gtest/gtest.h"
 #include "thread.h"
 
diff --git a/compiler/debug/dwarf/dwarf_test.cc b/compiler/debug/dwarf/dwarf_test.cc
index 5491596..5946af8 100644
--- a/compiler/debug/dwarf/dwarf_test.cc
+++ b/compiler/debug/dwarf/dwarf_test.cc
@@ -16,11 +16,11 @@
 
 #include "dwarf_test.h"
 
-#include "debug/dwarf/debug_frame_opcode_writer.h"
-#include "debug/dwarf/debug_info_entry_writer.h"
-#include "debug/dwarf/debug_line_opcode_writer.h"
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/headers.h"
+#include "dwarf/debug_frame_opcode_writer.h"
+#include "dwarf/debug_info_entry_writer.h"
+#include "dwarf/debug_line_opcode_writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/headers.h"
 #include "gtest/gtest.h"
 
 namespace art {
diff --git a/compiler/debug/dwarf/dwarf_test.h b/compiler/debug/dwarf/dwarf_test.h
index 6b039a7..e51f807 100644
--- a/compiler/debug/dwarf/dwarf_test.h
+++ b/compiler/debug/dwarf/dwarf_test.h
@@ -29,9 +29,9 @@
 #include "base/os.h"
 #include "base/unix_file/fd_file.h"
 #include "common_compiler_test.h"
+#include "elf/elf_builder.h"
 #include "gtest/gtest.h"
-#include "linker/elf_builder.h"
-#include "linker/file_output_stream.h"
+#include "stream/file_output_stream.h"
 
 namespace art {
 namespace dwarf {
@@ -63,8 +63,8 @@
     InstructionSet isa =
         (sizeof(typename ElfTypes::Addr) == 8) ? InstructionSet::kX86_64 : InstructionSet::kX86;
     ScratchFile file;
-    linker::FileOutputStream output_stream(file.GetFile());
-    linker::ElfBuilder<ElfTypes> builder(isa, nullptr, &output_stream);
+    FileOutputStream output_stream(file.GetFile());
+    ElfBuilder<ElfTypes> builder(isa, &output_stream);
     builder.Start();
     if (!debug_info_data_.empty()) {
       builder.WriteSection(".debug_info", &debug_info_data_);
diff --git a/compiler/debug/elf_debug_frame_writer.h b/compiler/debug/elf_debug_frame_writer.h
index 1f827ea..44b70ff 100644
--- a/compiler/debug/elf_debug_frame_writer.h
+++ b/compiler/debug/elf_debug_frame_writer.h
@@ -20,11 +20,11 @@
 #include <vector>
 
 #include "arch/instruction_set.h"
-#include "debug/dwarf/debug_frame_opcode_writer.h"
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/headers.h"
 #include "debug/method_debug_info.h"
-#include "linker/elf_builder.h"
+#include "dwarf/debug_frame_opcode_writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/headers.h"
+#include "elf/elf_builder.h"
 
 namespace art {
 namespace debug {
@@ -166,7 +166,7 @@
 }
 
 template<typename ElfTypes>
-void WriteCFISection(linker::ElfBuilder<ElfTypes>* builder,
+void WriteCFISection(ElfBuilder<ElfTypes>* builder,
                      const ArrayRef<const MethodDebugInfo>& method_infos) {
   typedef typename ElfTypes::Addr Elf_Addr;
 
diff --git a/compiler/debug/elf_debug_info_writer.h b/compiler/debug/elf_debug_info_writer.h
index 05a4a3e..8eead4e 100644
--- a/compiler/debug/elf_debug_info_writer.h
+++ b/compiler/debug/elf_debug_info_writer.h
@@ -22,17 +22,17 @@
 #include <vector>
 
 #include "art_field-inl.h"
-#include "debug/dwarf/debug_abbrev_writer.h"
-#include "debug/dwarf/debug_info_entry_writer.h"
 #include "debug/elf_compilation_unit.h"
 #include "debug/elf_debug_loc_writer.h"
 #include "debug/method_debug_info.h"
 #include "dex/code_item_accessors-inl.h"
 #include "dex/dex_file-inl.h"
 #include "dex/dex_file.h"
+#include "dwarf/debug_abbrev_writer.h"
+#include "dwarf/debug_info_entry_writer.h"
+#include "elf/elf_builder.h"
 #include "heap_poisoning.h"
 #include "linear_alloc.h"
-#include "linker/elf_builder.h"
 #include "mirror/array.h"
 #include "mirror/class-inl.h"
 #include "mirror/class.h"
@@ -59,7 +59,7 @@
   using Elf_Addr = typename ElfTypes::Addr;
 
  public:
-  explicit ElfDebugInfoWriter(linker::ElfBuilder<ElfTypes>* builder)
+  explicit ElfDebugInfoWriter(ElfBuilder<ElfTypes>* builder)
       : builder_(builder),
         debug_abbrev_(&debug_abbrev_buffer_) {
   }
@@ -80,7 +80,7 @@
   }
 
  private:
-  linker::ElfBuilder<ElfTypes>* builder_;
+  ElfBuilder<ElfTypes>* builder_;
   std::vector<uint8_t> debug_abbrev_buffer_;
   dwarf::DebugAbbrevWriter<> debug_abbrev_;
   std::vector<uint8_t> debug_loc_;
diff --git a/compiler/debug/elf_debug_line_writer.h b/compiler/debug/elf_debug_line_writer.h
index f95912a..479725b 100644
--- a/compiler/debug/elf_debug_line_writer.h
+++ b/compiler/debug/elf_debug_line_writer.h
@@ -20,12 +20,12 @@
 #include <unordered_set>
 #include <vector>
 
-#include "debug/dwarf/debug_line_opcode_writer.h"
-#include "debug/dwarf/headers.h"
 #include "debug/elf_compilation_unit.h"
 #include "debug/src_map_elem.h"
 #include "dex/dex_file-inl.h"
-#include "linker/elf_builder.h"
+#include "dwarf/debug_line_opcode_writer.h"
+#include "dwarf/headers.h"
+#include "elf/elf_builder.h"
 #include "oat_file.h"
 #include "stack_map.h"
 
@@ -39,7 +39,7 @@
   using Elf_Addr = typename ElfTypes::Addr;
 
  public:
-  explicit ElfDebugLineWriter(linker::ElfBuilder<ElfTypes>* builder) : builder_(builder) {
+  explicit ElfDebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
   }
 
   void Start() {
@@ -273,7 +273,7 @@
   }
 
  private:
-  linker::ElfBuilder<ElfTypes>* builder_;
+  ElfBuilder<ElfTypes>* builder_;
 };
 
 }  // namespace debug
diff --git a/compiler/debug/elf_debug_loc_writer.h b/compiler/debug/elf_debug_loc_writer.h
index b663291..a5a84bb 100644
--- a/compiler/debug/elf_debug_loc_writer.h
+++ b/compiler/debug/elf_debug_loc_writer.h
@@ -22,9 +22,9 @@
 
 #include "arch/instruction_set.h"
 #include "compiled_method.h"
-#include "debug/dwarf/debug_info_entry_writer.h"
-#include "debug/dwarf/register.h"
 #include "debug/method_debug_info.h"
+#include "dwarf/debug_info_entry_writer.h"
+#include "dwarf/register.h"
 #include "stack_map.h"
 
 namespace art {
diff --git a/compiler/debug/elf_debug_writer.cc b/compiler/debug/elf_debug_writer.cc
index 3b7363b..cc74717 100644
--- a/compiler/debug/elf_debug_writer.cc
+++ b/compiler/debug/elf_debug_writer.cc
@@ -22,20 +22,20 @@
 
 #include "base/array_ref.h"
 #include "base/stl_util.h"
-#include "debug/dwarf/dwarf_constants.h"
 #include "debug/elf_compilation_unit.h"
 #include "debug/elf_debug_frame_writer.h"
 #include "debug/elf_debug_info_writer.h"
 #include "debug/elf_debug_line_writer.h"
 #include "debug/elf_debug_loc_writer.h"
-#include "debug/elf_debug_reader.h"
 #include "debug/elf_symtab_writer.h"
 #include "debug/method_debug_info.h"
-#include "debug/xz_utils.h"
-#include "elf.h"
-#include "linker/elf_builder.h"
-#include "linker/vector_output_stream.h"
+#include "dwarf/dwarf_constants.h"
+#include "elf/elf.h"
+#include "elf/elf_builder.h"
+#include "elf/elf_debug_reader.h"
+#include "elf/xz_utils.h"
 #include "oat.h"
+#include "stream/vector_output_stream.h"
 
 namespace art {
 namespace debug {
@@ -43,7 +43,7 @@
 using ElfRuntimeTypes = std::conditional<sizeof(void*) == 4, ElfTypes32, ElfTypes64>::type;
 
 template <typename ElfTypes>
-void WriteDebugInfo(linker::ElfBuilder<ElfTypes>* builder,
+void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
                     const DebugInfo& debug_info) {
   // Write .strtab and .symtab.
   WriteDebugSymbols(builder, /* mini-debug-info= */ false, debug_info);
@@ -112,7 +112,7 @@
 template <typename ElfTypes>
 static std::vector<uint8_t> MakeMiniDebugInfoInternal(
     InstructionSet isa,
-    const InstructionSetFeatures* features,
+    const InstructionSetFeatures* features ATTRIBUTE_UNUSED,
     typename ElfTypes::Addr text_section_address,
     size_t text_section_size,
     typename ElfTypes::Addr dex_section_address,
@@ -120,9 +120,8 @@
     const DebugInfo& debug_info) {
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
-  linker::VectorOutputStream out("Mini-debug-info ELF file", &buffer);
-  std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
-      new linker::ElfBuilder<ElfTypes>(isa, features, &out));
+  VectorOutputStream out("Mini-debug-info ELF file", &buffer);
+  std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
   builder->Start(/* write_program_headers= */ false);
   // Mirror ELF sections as NOBITS since the added symbols will reference them.
   if (text_section_size != 0) {
@@ -174,7 +173,7 @@
 
 std::vector<uint8_t> MakeElfFileForJIT(
     InstructionSet isa,
-    const InstructionSetFeatures* features,
+    const InstructionSetFeatures* features ATTRIBUTE_UNUSED,
     bool mini_debug_info,
     const MethodDebugInfo& method_info) {
   using ElfTypes = ElfRuntimeTypes;
@@ -184,9 +183,8 @@
   debug_info.compiled_methods = ArrayRef<const MethodDebugInfo>(&method_info, 1);
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
-  linker::VectorOutputStream out("Debug ELF file", &buffer);
-  std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
-      new linker::ElfBuilder<ElfTypes>(isa, features, &out));
+  VectorOutputStream out("Debug ELF file", &buffer);
+  std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
   // No program headers since the ELF file is not linked and has no allocated sections.
   builder->Start(/* write_program_headers= */ false);
   builder->GetText()->AllocateVirtualMemory(method_info.code_address, method_info.code_size);
@@ -230,7 +228,7 @@
 // Combine several mini-debug-info ELF files into one, while filtering some symbols.
 std::vector<uint8_t> PackElfFileForJIT(
     InstructionSet isa,
-    const InstructionSetFeatures* features,
+    const InstructionSetFeatures* features ATTRIBUTE_UNUSED,
     std::vector<ArrayRef<const uint8_t>>& added_elf_files,
     std::vector<const void*>& removed_symbols,
     /*out*/ size_t* num_symbols) {
@@ -250,9 +248,8 @@
   std::vector<uint8_t> inner_elf_file;
   {
     inner_elf_file.reserve(1 * KB);  // Approximate size of ELF file with a single symbol.
-    linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &inner_elf_file);
-    std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
-        new linker::ElfBuilder<ElfTypes>(isa, features, &out));
+    VectorOutputStream out("Mini-debug-info ELF file for JIT", &inner_elf_file);
+    std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
     builder->Start(/*write_program_headers=*/ false);
     auto* text = builder->GetText();
     auto* strtab = builder->GetStrTab();
@@ -328,9 +325,8 @@
     XzCompress(ArrayRef<const uint8_t>(inner_elf_file), &gnu_debugdata);
 
     outer_elf_file.reserve(KB + gnu_debugdata.size());
-    linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &outer_elf_file);
-    std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
-        new linker::ElfBuilder<ElfTypes>(isa, features, &out));
+    VectorOutputStream out("Mini-debug-info ELF file for JIT", &outer_elf_file);
+    std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
     builder->Start(/*write_program_headers=*/ false);
     if (max_address > min_address) {
       builder->GetText()->AllocateVirtualMemory(min_address, max_address - min_address);
@@ -345,16 +341,15 @@
 
 std::vector<uint8_t> WriteDebugElfFileForClasses(
     InstructionSet isa,
-    const InstructionSetFeatures* features,
+    const InstructionSetFeatures* features ATTRIBUTE_UNUSED,
     const ArrayRef<mirror::Class*>& types)
     REQUIRES_SHARED(Locks::mutator_lock_) {
   using ElfTypes = ElfRuntimeTypes;
   CHECK_EQ(sizeof(ElfTypes::Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa)));
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
-  linker::VectorOutputStream out("Debug ELF file", &buffer);
-  std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
-      new linker::ElfBuilder<ElfTypes>(isa, features, &out));
+  VectorOutputStream out("Debug ELF file", &buffer);
+  std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
   // No program headers since the ELF file is not linked and has no allocated sections.
   builder->Start(/* write_program_headers= */ false);
   ElfDebugInfoWriter<ElfTypes> info_writer(builder.get());
@@ -370,10 +365,10 @@
 
 // Explicit instantiations
 template void WriteDebugInfo<ElfTypes32>(
-    linker::ElfBuilder<ElfTypes32>* builder,
+    ElfBuilder<ElfTypes32>* builder,
     const DebugInfo& debug_info);
 template void WriteDebugInfo<ElfTypes64>(
-    linker::ElfBuilder<ElfTypes64>* builder,
+    ElfBuilder<ElfTypes64>* builder,
     const DebugInfo& debug_info);
 
 }  // namespace debug
diff --git a/compiler/debug/elf_debug_writer.h b/compiler/debug/elf_debug_writer.h
index 90580b4..14a5edb 100644
--- a/compiler/debug/elf_debug_writer.h
+++ b/compiler/debug/elf_debug_writer.h
@@ -19,12 +19,13 @@
 
 #include <vector>
 
+#include "arch/instruction_set_features.h"
 #include "base/array_ref.h"
 #include "base/macros.h"
 #include "base/mutex.h"
-#include "debug/dwarf/dwarf_constants.h"
 #include "debug/debug_info.h"
-#include "linker/elf_builder.h"
+#include "dwarf/dwarf_constants.h"
+#include "elf/elf_builder.h"
 
 namespace art {
 class OatHeader;
@@ -36,7 +37,7 @@
 
 template <typename ElfTypes>
 void WriteDebugInfo(
-    linker::ElfBuilder<ElfTypes>* builder,
+    ElfBuilder<ElfTypes>* builder,
     const DebugInfo& debug_info);
 
 std::vector<uint8_t> MakeMiniDebugInfo(
diff --git a/compiler/debug/elf_symtab_writer.h b/compiler/debug/elf_symtab_writer.h
index 7a8e291..2ed3a4b 100644
--- a/compiler/debug/elf_symtab_writer.h
+++ b/compiler/debug/elf_symtab_writer.h
@@ -25,7 +25,7 @@
 #include "debug/method_debug_info.h"
 #include "dex/dex_file-inl.h"
 #include "dex/code_item_accessors.h"
-#include "linker/elf_builder.h"
+#include "elf/elf_builder.h"
 
 namespace art {
 namespace debug {
@@ -45,7 +45,7 @@
 constexpr const char* kDexFileSymbolName = "$dexfile";
 
 template <typename ElfTypes>
-static void WriteDebugSymbols(linker::ElfBuilder<ElfTypes>* builder,
+static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
                               bool mini_debug_info,
                               const DebugInfo& debug_info) {
   uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max();
diff --git a/compiler/jni/quick/jni_compiler.cc b/compiler/jni/quick/jni_compiler.cc
index bdbf429..7054078 100644
--- a/compiler/jni/quick/jni_compiler.cc
+++ b/compiler/jni/quick/jni_compiler.cc
@@ -32,7 +32,7 @@
 #include "base/utils.h"
 #include "calling_convention.h"
 #include "class_linker.h"
-#include "debug/dwarf/debug_frame_opcode_writer.h"
+#include "dwarf/debug_frame_opcode_writer.h"
 #include "dex/dex_file-inl.h"
 #include "driver/compiler_options.h"
 #include "entrypoints/quick/quick_entrypoints.h"
diff --git a/compiler/linker/output_stream_test.cc b/compiler/linker/output_stream_test.cc
index bcb129c..00231b1 100644
--- a/compiler/linker/output_stream_test.cc
+++ b/compiler/linker/output_stream_test.cc
@@ -14,15 +14,14 @@
  * limitations under the License.
  */
 
-#include "file_output_stream.h"
-#include "vector_output_stream.h"
-
 #include <android-base/logging.h>
 
 #include "base/macros.h"
 #include "base/unix_file/fd_file.h"
-#include "buffered_output_stream.h"
 #include "common_runtime_test.h"
+#include "stream/buffered_output_stream.h"
+#include "stream/file_output_stream.h"
+#include "stream/vector_output_stream.h"
 
 namespace art {
 namespace linker {
diff --git a/compiler/optimizing/common_arm.h b/compiler/optimizing/common_arm.h
index 356ff9f..7d3af95 100644
--- a/compiler/optimizing/common_arm.h
+++ b/compiler/optimizing/common_arm.h
@@ -17,7 +17,7 @@
 #ifndef ART_COMPILER_OPTIMIZING_COMMON_ARM_H_
 #define ART_COMPILER_OPTIMIZING_COMMON_ARM_H_
 
-#include "debug/dwarf/register.h"
+#include "dwarf/register.h"
 #include "instruction_simplifier_shared.h"
 #include "locations.h"
 #include "nodes.h"
diff --git a/compiler/utils/assembler.h b/compiler/utils/assembler.h
index 0537225..aa21f86 100644
--- a/compiler/utils/assembler.h
+++ b/compiler/utils/assembler.h
@@ -30,7 +30,7 @@
 #include "base/enums.h"
 #include "base/macros.h"
 #include "base/memory_region.h"
-#include "debug/dwarf/debug_frame_opcode_writer.h"
+#include "dwarf/debug_frame_opcode_writer.h"
 #include "label.h"
 #include "managed_register.h"
 #include "mips/constants_mips.h"
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 71f71d1..d04fa4e 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -61,13 +61,13 @@
 #include "compiler_callbacks.h"
 #include "debug/elf_debug_writer.h"
 #include "debug/method_debug_info.h"
-#include "dexlayout.h"
 #include "dex/descriptors_names.h"
 #include "dex/dex_file-inl.h"
 #include "dex/quick_compiler_callbacks.h"
 #include "dex/verification_results.h"
 #include "dex2oat_options.h"
 #include "dex2oat_return_codes.h"
+#include "dexlayout.h"
 #include "driver/compiler_driver.h"
 #include "driver/compiler_options.h"
 #include "driver/compiler_options_map-inl.h"
@@ -77,10 +77,8 @@
 #include "gc/verification.h"
 #include "interpreter/unstarted_runtime.h"
 #include "jni/java_vm_ext.h"
-#include "linker/buffered_output_stream.h"
 #include "linker/elf_writer.h"
 #include "linker/elf_writer_quick.h"
-#include "linker/file_output_stream.h"
 #include "linker/image_writer.h"
 #include "linker/multi_oat_relative_patcher.h"
 #include "linker/oat_writer.h"
@@ -94,6 +92,8 @@
 #include "runtime.h"
 #include "runtime_options.h"
 #include "scoped_thread_state_change-inl.h"
+#include "stream/buffered_output_stream.h"
+#include "stream/file_output_stream.h"
 #include "vdex_file.h"
 #include "verifier/verifier_deps.h"
 #include "well_known_classes.h"
@@ -1323,9 +1323,9 @@
     // Note: we're only invalidating the magic data in the file, as dex2oat needs the rest of
     // the information to remain valid.
     if (update_input_vdex_) {
-      std::unique_ptr<linker::BufferedOutputStream> vdex_out =
-          std::make_unique<linker::BufferedOutputStream>(
-              std::make_unique<linker::FileOutputStream>(vdex_files_.back().get()));
+      std::unique_ptr<BufferedOutputStream> vdex_out =
+          std::make_unique<BufferedOutputStream>(
+              std::make_unique<FileOutputStream>(vdex_files_.back().get()));
       if (!vdex_out->WriteFully(&VdexFile::VerifierDepsHeader::kVdexInvalidMagic,
                                 arraysize(VdexFile::VerifierDepsHeader::kVdexInvalidMagic))) {
         PLOG(ERROR) << "Failed to invalidate vdex header. File: " << vdex_out->GetLocation();
@@ -1962,9 +1962,9 @@
       verifier::VerifierDeps* verifier_deps = callbacks_->GetVerifierDeps();
       for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
         File* vdex_file = vdex_files_[i].get();
-        std::unique_ptr<linker::BufferedOutputStream> vdex_out =
-            std::make_unique<linker::BufferedOutputStream>(
-                std::make_unique<linker::FileOutputStream>(vdex_file));
+        std::unique_ptr<BufferedOutputStream> vdex_out =
+            std::make_unique<BufferedOutputStream>(
+                std::make_unique<FileOutputStream>(vdex_file));
 
         if (!oat_writers_[i]->WriteVerifierDeps(vdex_out.get(), verifier_deps)) {
           LOG(ERROR) << "Failed to write verifier dependencies into VDEX " << vdex_file->GetPath();
@@ -2022,7 +2022,7 @@
         debug::DebugInfo debug_info = oat_writer->GetDebugInfo();  // Keep the variable alive.
         elf_writer->PrepareDebugInfo(debug_info);  // Processes the data on background thread.
 
-        linker::OutputStream*& rodata = rodata_[i];
+        OutputStream*& rodata = rodata_[i];
         DCHECK(rodata != nullptr);
         if (!oat_writer->WriteRodata(rodata)) {
           LOG(ERROR) << "Failed to write .rodata section to the ELF file " << oat_file->GetPath();
@@ -2031,7 +2031,7 @@
         elf_writer->EndRoData(rodata);
         rodata = nullptr;
 
-        linker::OutputStream* text = elf_writer->StartText();
+        OutputStream* text = elf_writer->StartText();
         if (!oat_writer->WriteCode(text)) {
           LOG(ERROR) << "Failed to write .text section to the ELF file " << oat_file->GetPath();
           return false;
@@ -2039,7 +2039,7 @@
         elf_writer->EndText(text);
 
         if (oat_writer->GetDataBimgRelRoSize() != 0u) {
-          linker::OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
+          OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
           if (!oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro)) {
             LOG(ERROR) << "Failed to write .data.bimg.rel.ro section to the ELF file "
                 << oat_file->GetPath();
@@ -2734,8 +2734,8 @@
 
   std::vector<std::unique_ptr<linker::ElfWriter>> elf_writers_;
   std::vector<std::unique_ptr<linker::OatWriter>> oat_writers_;
-  std::vector<linker::OutputStream*> rodata_;
-  std::vector<std::unique_ptr<linker::OutputStream>> vdex_out_;
+  std::vector<OutputStream*> rodata_;
+  std::vector<std::unique_ptr<OutputStream>> vdex_out_;
   std::unique_ptr<linker::ImageWriter> image_writer_;
   std::unique_ptr<CompilerDriver> driver_;
 
diff --git a/dex2oat/linker/arm/relative_patcher_arm_base.cc b/dex2oat/linker/arm/relative_patcher_arm_base.cc
index a2ba339..828dc5d 100644
--- a/dex2oat/linker/arm/relative_patcher_arm_base.cc
+++ b/dex2oat/linker/arm/relative_patcher_arm_base.cc
@@ -21,9 +21,9 @@
 #include "debug/method_debug_info.h"
 #include "dex/dex_file_types.h"
 #include "linker/linker_patch.h"
-#include "linker/output_stream.h"
 #include "oat.h"
 #include "oat_quick_method_header.h"
+#include "stream/output_stream.h"
 
 namespace art {
 namespace linker {
diff --git a/dex2oat/linker/arm64/relative_patcher_arm64.cc b/dex2oat/linker/arm64/relative_patcher_arm64.cc
index 0497d4f..ee8d4d1 100644
--- a/dex2oat/linker/arm64/relative_patcher_arm64.cc
+++ b/dex2oat/linker/arm64/relative_patcher_arm64.cc
@@ -26,13 +26,13 @@
 #include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "heap_poisoning.h"
 #include "linker/linker_patch.h"
-#include "linker/output_stream.h"
 #include "lock_word.h"
 #include "mirror/array-inl.h"
 #include "mirror/object.h"
 #include "oat.h"
 #include "oat_quick_method_header.h"
 #include "read_barrier.h"
+#include "stream/output_stream.h"
 #include "utils/arm64/assembler_arm64.h"
 
 namespace art {
diff --git a/dex2oat/linker/elf_writer.h b/dex2oat/linker/elf_writer.h
index 637330c..a60c708 100644
--- a/dex2oat/linker/elf_writer.h
+++ b/dex2oat/linker/elf_writer.h
@@ -31,6 +31,7 @@
 namespace art {
 
 class ElfFile;
+class OutputStream;
 
 namespace debug {
 struct MethodDebugInfo;
@@ -38,8 +39,6 @@
 
 namespace linker {
 
-class OutputStream;
-
 class ElfWriter {
  public:
   // Looks up information about location of oat file in elf file container.
diff --git a/dex2oat/linker/elf_writer_quick.cc b/dex2oat/linker/elf_writer_quick.cc
index 7e0f29d..65aa5a9 100644
--- a/dex2oat/linker/elf_writer_quick.cc
+++ b/dex2oat/linker/elf_writer_quick.cc
@@ -31,11 +31,11 @@
 #include "debug/elf_debug_writer.h"
 #include "debug/method_debug_info.h"
 #include "driver/compiler_options.h"
-#include "elf.h"
+#include "elf/elf.h"
+#include "elf/elf_builder.h"
 #include "elf_utils.h"
-#include "linker/buffered_output_stream.h"
-#include "linker/elf_builder.h"
-#include "linker/file_output_stream.h"
+#include "stream/buffered_output_stream.h"
+#include "stream/file_output_stream.h"
 #include "thread-current-inl.h"
 #include "thread_pool.h"
 
@@ -159,7 +159,6 @@
       output_stream_(
           std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file))),
       builder_(new ElfBuilder<ElfTypes>(compiler_options_.GetInstructionSet(),
-                                        compiler_options_.GetInstructionSetFeatures(),
                                         output_stream_.get())) {}
 
 template <typename ElfTypes>
@@ -243,10 +242,6 @@
 
 template <typename ElfTypes>
 void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
-  if (builder_->GetIsa() == InstructionSet::kMips ||
-      builder_->GetIsa() == InstructionSet::kMips64) {
-    builder_->WriteMIPSabiflagsSection();
-  }
   builder_->WriteDynamicSection();
 }
 
diff --git a/dex2oat/linker/elf_writer_test.cc b/dex2oat/linker/elf_writer_test.cc
index d8a360a..c1ff8f2 100644
--- a/dex2oat/linker/elf_writer_test.cc
+++ b/dex2oat/linker/elf_writer_test.cc
@@ -23,10 +23,10 @@
 #include "base/unix_file/fd_file.h"
 #include "base/utils.h"
 #include "common_compiler_driver_test.h"
+#include "elf/elf_builder.h"
 #include "elf_file.h"
 #include "elf_file_impl.h"
 #include "elf_writer_quick.h"
-#include "linker/elf_builder.h"
 #include "oat.h"
 
 namespace art {
diff --git a/dex2oat/linker/image_test.h b/dex2oat/linker/image_test.h
index aa4fb70..7209fbf 100644
--- a/dex2oat/linker/image_test.h
+++ b/dex2oat/linker/image_test.h
@@ -43,16 +43,16 @@
 #include "driver/compiler_options.h"
 #include "gc/space/image_space.h"
 #include "image_writer.h"
-#include "linker/buffered_output_stream.h"
 #include "linker/elf_writer.h"
 #include "linker/elf_writer_quick.h"
-#include "linker/file_output_stream.h"
 #include "linker/multi_oat_relative_patcher.h"
 #include "lock_word.h"
 #include "mirror/object-inl.h"
 #include "oat_writer.h"
 #include "scoped_thread_state_change-inl.h"
 #include "signal_catcher.h"
+#include "stream/buffered_output_stream.h"
+#include "stream/file_output_stream.h"
 
 namespace art {
 namespace linker {
diff --git a/dex2oat/linker/multi_oat_relative_patcher_test.cc b/dex2oat/linker/multi_oat_relative_patcher_test.cc
index 2610561..274084f 100644
--- a/dex2oat/linker/multi_oat_relative_patcher_test.cc
+++ b/dex2oat/linker/multi_oat_relative_patcher_test.cc
@@ -20,7 +20,7 @@
 #include "debug/method_debug_info.h"
 #include "gtest/gtest.h"
 #include "linker/linker_patch.h"
-#include "linker/vector_output_stream.h"
+#include "stream/vector_output_stream.h"
 
 namespace art {
 namespace linker {
diff --git a/dex2oat/linker/oat_writer.cc b/dex2oat/linker/oat_writer.cc
index a6fb2de..897278f 100644
--- a/dex2oat/linker/oat_writer.cc
+++ b/dex2oat/linker/oat_writer.cc
@@ -54,12 +54,9 @@
 #include "gc/space/space.h"
 #include "handle_scope-inl.h"
 #include "image_writer.h"
-#include "linker/buffered_output_stream.h"
-#include "linker/file_output_stream.h"
 #include "linker/index_bss_mapping_encoder.h"
 #include "linker/linker_patch.h"
 #include "linker/multi_oat_relative_patcher.h"
-#include "linker/output_stream.h"
 #include "mirror/array.h"
 #include "mirror/class_loader.h"
 #include "mirror/dex_cache-inl.h"
@@ -69,6 +66,9 @@
 #include "quicken_info.h"
 #include "scoped_thread_state_change-inl.h"
 #include "stack_map.h"
+#include "stream/buffered_output_stream.h"
+#include "stream/file_output_stream.h"
+#include "stream/output_stream.h"
 #include "utils/dex_cache_arrays_layout-inl.h"
 #include "vdex_file.h"
 #include "verifier/verifier_deps.h"
diff --git a/dex2oat/linker/oat_writer.h b/dex2oat/linker/oat_writer.h
index 37785f2..84d13a8 100644
--- a/dex2oat/linker/oat_writer.h
+++ b/dex2oat/linker/oat_writer.h
@@ -44,6 +44,7 @@
 class CompilerDriver;
 class CompilerOptions;
 class DexContainer;
+class OutputStream;
 class ProfileCompilationInfo;
 class TimingLogger;
 class TypeLookupTable;
@@ -62,7 +63,6 @@
 
 class ImageWriter;
 class MultiOatRelativePatcher;
-class OutputStream;
 
 enum class CopyOption {
   kNever,
diff --git a/dex2oat/linker/oat_writer_test.cc b/dex2oat/linker/oat_writer_test.cc
index ae22477..c46aa18 100644
--- a/dex2oat/linker/oat_writer_test.cc
+++ b/dex2oat/linker/oat_writer_test.cc
@@ -35,12 +35,9 @@
 #include "driver/compiler_driver.h"
 #include "driver/compiler_options.h"
 #include "entrypoints/quick/quick_entrypoints.h"
-#include "linker/buffered_output_stream.h"
 #include "linker/elf_writer.h"
 #include "linker/elf_writer_quick.h"
-#include "linker/file_output_stream.h"
 #include "linker/multi_oat_relative_patcher.h"
-#include "linker/vector_output_stream.h"
 #include "mirror/class-inl.h"
 #include "mirror/object-inl.h"
 #include "mirror/object_array-inl.h"
@@ -48,6 +45,9 @@
 #include "oat_writer.h"
 #include "profile/profile_compilation_info.h"
 #include "scoped_thread_state_change-inl.h"
+#include "stream/buffered_output_stream.h"
+#include "stream/file_output_stream.h"
+#include "stream/vector_output_stream.h"
 #include "vdex_file.h"
 
 namespace art {
diff --git a/dex2oat/linker/relative_patcher.cc b/dex2oat/linker/relative_patcher.cc
index 45a4a22..4db0e8a 100644
--- a/dex2oat/linker/relative_patcher.cc
+++ b/dex2oat/linker/relative_patcher.cc
@@ -35,7 +35,7 @@
 #ifdef ART_ENABLE_CODEGEN_x86_64
 #include "linker/x86_64/relative_patcher_x86_64.h"
 #endif
-#include "linker/output_stream.h"
+#include "stream/output_stream.h"
 
 namespace art {
 namespace linker {
diff --git a/dex2oat/linker/relative_patcher.h b/dex2oat/linker/relative_patcher.h
index d80eaf9..e8e15c9 100644
--- a/dex2oat/linker/relative_patcher.h
+++ b/dex2oat/linker/relative_patcher.h
@@ -28,6 +28,7 @@
 namespace art {
 
 class CompiledMethod;
+class OutputStream;
 
 namespace debug {
 struct MethodDebugInfo;
@@ -36,7 +37,6 @@
 namespace linker {
 
 class LinkerPatch;
-class OutputStream;
 
 /**
  * @class RelativePatcherThunkProvider
diff --git a/dex2oat/linker/relative_patcher_test.h b/dex2oat/linker/relative_patcher_test.h
index 4329ee1..dead38d 100644
--- a/dex2oat/linker/relative_patcher_test.h
+++ b/dex2oat/linker/relative_patcher_test.h
@@ -29,9 +29,9 @@
 #include "dex/string_reference.h"
 #include "driver/compiled_method_storage.h"
 #include "linker/relative_patcher.h"
-#include "linker/vector_output_stream.h"
 #include "oat.h"
 #include "oat_quick_method_header.h"
+#include "stream/vector_output_stream.h"
 
 namespace art {
 namespace linker {
diff --git a/libelffile/Android.bp b/libelffile/Android.bp
new file mode 100644
index 0000000..60499dc
--- /dev/null
+++ b/libelffile/Android.bp
@@ -0,0 +1,55 @@
+//
+// Copyright (C) 2011 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.
+//
+
+// Needed to import elf.h in the runtime.
+cc_library_headers {
+    name: "libelffile-headers",
+    host_supported: true,
+    export_include_dirs: ["."],
+}
+
+art_cc_defaults {
+    name: "libelffile-defaults",
+    host_supported: true,
+    export_include_dirs: ["."],
+    srcs: [
+        "elf/xz_utils.cc",
+        "stream/buffered_output_stream.cc",
+        "stream/file_output_stream.cc",
+        "stream/output_stream.cc",
+        "stream/vector_output_stream.cc",
+    ],
+    shared_libs: [
+        "libartbase",
+        "libbase",
+    ],
+}
+
+art_cc_library_static {
+    name: "libelffile",
+    defaults: [
+        "art_defaults",
+        "libelffile-defaults",
+    ],
+}
+
+art_cc_library_static {
+    name: "libelffiled",
+    defaults: [
+        "art_debug_defaults",
+        "libelffile-defaults",
+    ],
+}
diff --git a/compiler/debug/dwarf/debug_abbrev_writer.h b/libelffile/dwarf/debug_abbrev_writer.h
similarity index 92%
rename from compiler/debug/dwarf/debug_abbrev_writer.h
rename to libelffile/dwarf/debug_abbrev_writer.h
index 63a049b..eecaa05 100644
--- a/compiler/debug/dwarf/debug_abbrev_writer.h
+++ b/libelffile/dwarf/debug_abbrev_writer.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_DEBUG_ABBREV_WRITER_H_
-#define ART_COMPILER_DEBUG_DWARF_DEBUG_ABBREV_WRITER_H_
+#ifndef ART_LIBELFFILE_DWARF_DEBUG_ABBREV_WRITER_H_
+#define ART_LIBELFFILE_DWARF_DEBUG_ABBREV_WRITER_H_
 
 #include <cstdint>
 #include <type_traits>
@@ -24,8 +24,8 @@
 #include "base/casts.h"
 #include "base/leb128.h"
 #include "base/stl_util.h"
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/writer.h"
 
 namespace art {
 namespace dwarf {
@@ -95,4 +95,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_DEBUG_ABBREV_WRITER_H_
+#endif  // ART_LIBELFFILE_DWARF_DEBUG_ABBREV_WRITER_H_
diff --git a/compiler/debug/dwarf/debug_frame_opcode_writer.h b/libelffile/dwarf/debug_frame_opcode_writer.h
similarity index 96%
rename from compiler/debug/dwarf/debug_frame_opcode_writer.h
rename to libelffile/dwarf/debug_frame_opcode_writer.h
index 7c75c9b..b255f9c 100644
--- a/compiler/debug/dwarf/debug_frame_opcode_writer.h
+++ b/libelffile/dwarf/debug_frame_opcode_writer.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
-#define ART_COMPILER_DEBUG_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
+#ifndef ART_LIBELFFILE_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
+#define ART_LIBELFFILE_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
 
 #include "base/bit_utils.h"
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/register.h"
-#include "debug/dwarf/writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/register.h"
+#include "dwarf/writer.h"
 
 namespace art {
 namespace dwarf {
@@ -338,4 +338,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
+#endif  // ART_LIBELFFILE_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
diff --git a/compiler/debug/dwarf/debug_info_entry_writer.h b/libelffile/dwarf/debug_info_entry_writer.h
similarity index 94%
rename from compiler/debug/dwarf/debug_info_entry_writer.h
rename to libelffile/dwarf/debug_info_entry_writer.h
index b198178..48654e9 100644
--- a/compiler/debug/dwarf/debug_info_entry_writer.h
+++ b/libelffile/dwarf/debug_info_entry_writer.h
@@ -14,18 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
-#define ART_COMPILER_DEBUG_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
+#ifndef ART_LIBELFFILE_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
+#define ART_LIBELFFILE_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
 
 #include <cstdint>
 #include <unordered_map>
 
 #include "base/casts.h"
 #include "base/leb128.h"
-#include "debug/dwarf/debug_abbrev_writer.h"
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/expression.h"
-#include "debug/dwarf/writer.h"
+#include "dwarf/debug_abbrev_writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/expression.h"
+#include "dwarf/writer.h"
 
 namespace art {
 namespace dwarf {
@@ -225,4 +225,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
+#endif  // ART_LIBELFFILE_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
diff --git a/compiler/debug/dwarf/debug_line_opcode_writer.h b/libelffile/dwarf/debug_line_opcode_writer.h
similarity index 96%
rename from compiler/debug/dwarf/debug_line_opcode_writer.h
rename to libelffile/dwarf/debug_line_opcode_writer.h
index bb4e87f..95b626e 100644
--- a/compiler/debug/dwarf/debug_line_opcode_writer.h
+++ b/libelffile/dwarf/debug_line_opcode_writer.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_DEBUG_LINE_OPCODE_WRITER_H_
-#define ART_COMPILER_DEBUG_DWARF_DEBUG_LINE_OPCODE_WRITER_H_
+#ifndef ART_LIBELFFILE_DWARF_DEBUG_LINE_OPCODE_WRITER_H_
+#define ART_LIBELFFILE_DWARF_DEBUG_LINE_OPCODE_WRITER_H_
 
 #include <cstdint>
 
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/writer.h"
 
 namespace art {
 namespace dwarf {
@@ -258,4 +258,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_DEBUG_LINE_OPCODE_WRITER_H_
+#endif  // ART_LIBELFFILE_DWARF_DEBUG_LINE_OPCODE_WRITER_H_
diff --git a/compiler/debug/dwarf/dwarf_constants.h b/libelffile/dwarf/dwarf_constants.h
similarity index 98%
rename from compiler/debug/dwarf/dwarf_constants.h
rename to libelffile/dwarf/dwarf_constants.h
index 7beb1fa..d52db3a 100644
--- a/compiler/debug/dwarf/dwarf_constants.h
+++ b/libelffile/dwarf/dwarf_constants.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_DWARF_CONSTANTS_H_
-#define ART_COMPILER_DEBUG_DWARF_DWARF_CONSTANTS_H_
+#ifndef ART_LIBELFFILE_DWARF_DWARF_CONSTANTS_H_
+#define ART_LIBELFFILE_DWARF_DWARF_CONSTANTS_H_
 
 namespace art {
 namespace dwarf {
@@ -683,4 +683,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_DWARF_CONSTANTS_H_
+#endif  // ART_LIBELFFILE_DWARF_DWARF_CONSTANTS_H_
diff --git a/compiler/debug/dwarf/expression.h b/libelffile/dwarf/expression.h
similarity index 93%
rename from compiler/debug/dwarf/expression.h
rename to libelffile/dwarf/expression.h
index fafc046..613da88 100644
--- a/compiler/debug/dwarf/expression.h
+++ b/libelffile/dwarf/expression.h
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_EXPRESSION_H_
-#define ART_COMPILER_DEBUG_DWARF_EXPRESSION_H_
+#ifndef ART_LIBELFFILE_DWARF_EXPRESSION_H_
+#define ART_LIBELFFILE_DWARF_EXPRESSION_H_
 
 #include <cstddef>
 #include <cstdint>
 
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/writer.h"
 
 namespace art {
 namespace dwarf {
@@ -118,4 +118,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_EXPRESSION_H_
+#endif  // ART_LIBELFFILE_DWARF_EXPRESSION_H_
diff --git a/compiler/debug/dwarf/headers.h b/libelffile/dwarf/headers.h
similarity index 93%
rename from compiler/debug/dwarf/headers.h
rename to libelffile/dwarf/headers.h
index 3cc8ad8..d77012f 100644
--- a/compiler/debug/dwarf/headers.h
+++ b/libelffile/dwarf/headers.h
@@ -14,18 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_HEADERS_H_
-#define ART_COMPILER_DEBUG_DWARF_HEADERS_H_
+#ifndef ART_LIBELFFILE_DWARF_HEADERS_H_
+#define ART_LIBELFFILE_DWARF_HEADERS_H_
 
 #include <cstdint>
 
 #include "base/array_ref.h"
-#include "debug/dwarf/debug_frame_opcode_writer.h"
-#include "debug/dwarf/debug_info_entry_writer.h"
-#include "debug/dwarf/debug_line_opcode_writer.h"
-#include "debug/dwarf/dwarf_constants.h"
-#include "debug/dwarf/register.h"
-#include "debug/dwarf/writer.h"
+#include "dwarf/debug_frame_opcode_writer.h"
+#include "dwarf/debug_info_entry_writer.h"
+#include "dwarf/debug_line_opcode_writer.h"
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/register.h"
+#include "dwarf/writer.h"
 
 namespace art {
 namespace dwarf {
@@ -159,4 +159,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_HEADERS_H_
+#endif  // ART_LIBELFFILE_DWARF_HEADERS_H_
diff --git a/compiler/debug/dwarf/register.h b/libelffile/dwarf/register.h
similarity index 93%
rename from compiler/debug/dwarf/register.h
rename to libelffile/dwarf/register.h
index 24bacac..eadb441 100644
--- a/compiler/debug/dwarf/register.h
+++ b/libelffile/dwarf/register.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_REGISTER_H_
-#define ART_COMPILER_DEBUG_DWARF_REGISTER_H_
+#ifndef ART_LIBELFFILE_DWARF_REGISTER_H_
+#define ART_LIBELFFILE_DWARF_REGISTER_H_
 
 namespace art {
 namespace dwarf {
@@ -59,4 +59,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_REGISTER_H_
+#endif  // ART_LIBELFFILE_DWARF_REGISTER_H_
diff --git a/compiler/debug/dwarf/writer.h b/libelffile/dwarf/writer.h
similarity index 96%
rename from compiler/debug/dwarf/writer.h
rename to libelffile/dwarf/writer.h
index c09d97a..a9764f6 100644
--- a/compiler/debug/dwarf/writer.h
+++ b/libelffile/dwarf/writer.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_DWARF_WRITER_H_
-#define ART_COMPILER_DEBUG_DWARF_WRITER_H_
+#ifndef ART_LIBELFFILE_DWARF_WRITER_H_
+#define ART_LIBELFFILE_DWARF_WRITER_H_
 
 #include <type_traits>
 #include <vector>
@@ -181,4 +181,4 @@
 }  // namespace dwarf
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_DWARF_WRITER_H_
+#endif  // ART_LIBELFFILE_DWARF_WRITER_H_
diff --git a/runtime/CPPLINT.cfg b/libelffile/elf/CPPLINT.cfg
similarity index 100%
rename from runtime/CPPLINT.cfg
rename to libelffile/elf/CPPLINT.cfg
diff --git a/runtime/elf.h b/libelffile/elf/elf.h
similarity index 99%
rename from runtime/elf.h
rename to libelffile/elf/elf.h
index 521d4a2..8eb9ae2 100644
--- a/runtime/elf.h
+++ b/libelffile/elf/elf.h
@@ -18,8 +18,11 @@
 //===----------------------------------------------------------------------===//
 
 // BEGIN android-changed
-#ifndef ART_RUNTIME_ELF_H_
-#define ART_RUNTIME_ELF_H_
+#ifndef ART_LIBELFFILE_ELF_ELF_H_
+#define ART_LIBELFFILE_ELF_ELF_H_
+// HACK: This file replaces the system elf.h header and defines everything needed.
+//   Avoid including the system header as that would lead to re-definition errors.
+#define _ELF_H
 // END android-changed
 
 // BEGIN android-changed
@@ -1892,5 +1895,5 @@
 };
 
 // BEGIN android-changed
-#endif  // ART_RUNTIME_ELF_H_
+#endif  // ART_LIBELFFILE_ELF_ELF_H_
 // END android-changed
diff --git a/compiler/linker/elf_builder.h b/libelffile/elf/elf_builder.h
similarity index 87%
rename from compiler/linker/elf_builder.h
rename to libelffile/elf/elf_builder.h
index 33e1866..1f6781a 100644
--- a/compiler/linker/elf_builder.h
+++ b/libelffile/elf/elf_builder.h
@@ -14,24 +14,22 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_LINKER_ELF_BUILDER_H_
-#define ART_COMPILER_LINKER_ELF_BUILDER_H_
+#ifndef ART_LIBELFFILE_ELF_ELF_BUILDER_H_
+#define ART_LIBELFFILE_ELF_ELF_BUILDER_H_
 
 #include <vector>
 #include <deque>
 
 #include "arch/instruction_set.h"
-#include "arch/mips/instruction_set_features_mips.h"
 #include "base/array_ref.h"
 #include "base/bit_utils.h"
 #include "base/casts.h"
 #include "base/leb128.h"
 #include "base/unix_file/fd_file.h"
-#include "elf_utils.h"
-#include "linker/error_delaying_output_stream.h"
+#include "elf/elf.h"
+#include "stream/error_delaying_output_stream.h"
 
 namespace art {
-namespace linker {
 
 // Writes ELF file.
 //
@@ -43,7 +41,6 @@
 //   .text                       - Compiled code.
 //   .bss                        - Zero-initialized writeable section.
 //   .dex                        - Reserved NOBITS space for dex-related data.
-//   .MIPS.abiflags              - MIPS specific section.
 //   .dynstr                     - Names for .dynsym.
 //   .dynsym                     - A few oat-specific dynamic symbols.
 //   .hash                       - Hash-table for .dynsym.
@@ -397,76 +394,6 @@
     std::deque<Elf_Sym> syms_;  // Buffered/cached content of the whole section.
   };
 
-  class AbiflagsSection final : public Section {
-   public:
-    // Section with Mips abiflag info.
-    static constexpr uint8_t MIPS_AFL_REG_NONE =         0;  // no registers
-    static constexpr uint8_t MIPS_AFL_REG_32 =           1;  // 32-bit registers
-    static constexpr uint8_t MIPS_AFL_REG_64 =           2;  // 64-bit registers
-    static constexpr uint32_t MIPS_AFL_FLAGS1_ODDSPREG = 1;  // Uses odd single-prec fp regs
-    static constexpr uint8_t MIPS_ABI_FP_DOUBLE =        1;  // -mdouble-float
-    static constexpr uint8_t MIPS_ABI_FP_XX =            5;  // -mfpxx
-    static constexpr uint8_t MIPS_ABI_FP_64A =           7;  // -mips32r* -mfp64 -mno-odd-spreg
-
-    AbiflagsSection(ElfBuilder<ElfTypes>* owner,
-                    const std::string& name,
-                    Elf_Word type,
-                    Elf_Word flags,
-                    const Section* link,
-                    Elf_Word info,
-                    Elf_Word align,
-                    Elf_Word entsize,
-                    InstructionSet isa,
-                    const InstructionSetFeatures* features)
-        : Section(owner, name, type, flags, link, info, align, entsize) {
-      if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
-        bool fpu32 = false;    // assume mips64 values
-        uint8_t isa_rev = 6;   // assume mips64 values
-        if (isa == InstructionSet::kMips) {
-          // adjust for mips32 values
-          fpu32 = features->AsMipsInstructionSetFeatures()->Is32BitFloatingPoint();
-          isa_rev = features->AsMipsInstructionSetFeatures()->IsR6()
-              ? 6
-              : features->AsMipsInstructionSetFeatures()->IsMipsIsaRevGreaterThanEqual2()
-                  ? (fpu32 ? 2 : 5)
-                  : 1;
-        }
-        abiflags_.version = 0;  // version of flags structure
-        abiflags_.isa_level = (isa == InstructionSet::kMips) ? 32 : 64;
-        abiflags_.isa_rev = isa_rev;
-        abiflags_.gpr_size = (isa == InstructionSet::kMips) ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64;
-        abiflags_.cpr1_size = fpu32 ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64;
-        abiflags_.cpr2_size = MIPS_AFL_REG_NONE;
-        // Set the fp_abi to MIPS_ABI_FP_64A for mips32 with 64-bit FPUs (ie: mips32 R5 and R6).
-        // Otherwise set to MIPS_ABI_FP_DOUBLE.
-        abiflags_.fp_abi =
-            (isa == InstructionSet::kMips && !fpu32) ? MIPS_ABI_FP_64A : MIPS_ABI_FP_DOUBLE;
-        abiflags_.isa_ext = 0;
-        abiflags_.ases = 0;
-        // To keep the code simple, we are not using odd FP reg for single floats for both
-        // mips32 and mips64 ART. Therefore we are not setting the MIPS_AFL_FLAGS1_ODDSPREG bit.
-        abiflags_.flags1 = 0;
-        abiflags_.flags2 = 0;
-      }
-    }
-
-    Elf_Word GetSize() const {
-      return sizeof(abiflags_);
-    }
-
-    void Write() {
-      this->WriteFully(&abiflags_, sizeof(abiflags_));
-    }
-
-   private:
-    struct {
-      uint16_t version;  // version of this structure
-      uint8_t  isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size;
-      uint8_t  fp_abi;
-      uint32_t isa_ext, ases, flags1, flags2;
-    } abiflags_;
-  };
-
   class BuildIdSection final : public Section {
    public:
     BuildIdSection(ElfBuilder<ElfTypes>* owner,
@@ -515,9 +442,8 @@
     off_t digest_start_;
   };
 
-  ElfBuilder(InstructionSet isa, const InstructionSetFeatures* features, OutputStream* output)
+  ElfBuilder(InstructionSet isa, OutputStream* output)
       : isa_(isa),
-        features_(features),
         stream_(output),
         rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
         text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0),
@@ -537,8 +463,6 @@
         debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
         debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
         shstrtab_(this, ".shstrtab", 0, 1),
-        abiflags_(this, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
-                  isa, features),
         build_id_(this, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, nullptr, 0, 4, 0),
         current_section_(nullptr),
         started_(false),
@@ -552,7 +476,6 @@
     dex_.phdr_flags_ = PF_R;
     dynamic_.phdr_flags_ = PF_R | PF_W;
     dynamic_.phdr_type_ = PT_DYNAMIC;
-    abiflags_.phdr_type_ = PT_MIPS_ABIFLAGS;
     build_id_.phdr_type_ = PT_NOTE;
   }
   ~ElfBuilder() {}
@@ -637,7 +560,7 @@
     stream_.Flush();
 
     // The main ELF header.
-    Elf_Ehdr elf_header = MakeElfHeader(isa_, features_);
+    Elf_Ehdr elf_header = MakeElfHeader(isa_);
     elf_header.e_shoff = section_headers_offset;
     elf_header.e_shnum = shdrs.size();
     elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
@@ -722,9 +645,6 @@
     if (dex_size != 0) {
       dex_.AllocateVirtualMemory(dex_size);
     }
-    if (isa_ == InstructionSet::kMips || isa_ == InstructionSet::kMips64) {
-      abiflags_.AllocateVirtualMemory(abiflags_.GetSize());
-    }
 
     // Cache .dynstr, .dynsym and .hash data.
     dynstr_.Add("");  // dynstr should start with empty string.
@@ -847,12 +767,6 @@
     return loaded_size_;
   }
 
-  void WriteMIPSabiflagsSection() {
-    abiflags_.Start();
-    abiflags_.Write();
-    abiflags_.End();
-  }
-
   void WriteBuildIdSection() {
     build_id_.Start();
     build_id_.Write();
@@ -880,7 +794,7 @@
   }
 
  private:
-  static Elf_Ehdr MakeElfHeader(InstructionSet isa, const InstructionSetFeatures* features) {
+  static Elf_Ehdr MakeElfHeader(InstructionSet isa) {
     Elf_Ehdr elf_header = Elf_Ehdr();
     switch (isa) {
       case InstructionSet::kArm:
@@ -905,25 +819,6 @@
         elf_header.e_flags = 0;
         break;
       }
-      case InstructionSet::kMips: {
-        elf_header.e_machine = EM_MIPS;
-        elf_header.e_flags = (EF_MIPS_NOREORDER |
-                              EF_MIPS_PIC       |
-                              EF_MIPS_CPIC      |
-                              EF_MIPS_ABI_O32   |
-                              (features->AsMipsInstructionSetFeatures()->IsR6()
-                                   ? EF_MIPS_ARCH_32R6
-                                   : EF_MIPS_ARCH_32R2));
-        break;
-      }
-      case InstructionSet::kMips64: {
-        elf_header.e_machine = EM_MIPS;
-        elf_header.e_flags = (EF_MIPS_NOREORDER |
-                              EF_MIPS_PIC       |
-                              EF_MIPS_CPIC      |
-                              EF_MIPS_ARCH_64R6);
-        break;
-      }
       case InstructionSet::kNone: {
         LOG(FATAL) << "No instruction set";
         break;
@@ -1032,7 +927,6 @@
   }
 
   InstructionSet isa_;
-  const InstructionSetFeatures* features_;
 
   ErrorDelayingOutputStream stream_;
 
@@ -1052,7 +946,6 @@
   Section debug_info_;
   Section debug_line_;
   StringSection shstrtab_;
-  AbiflagsSection abiflags_;
   BuildIdSection build_id_;
   std::vector<std::unique_ptr<Section>> other_sections_;
 
@@ -1073,7 +966,6 @@
   DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
 };
 
-}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_LINKER_ELF_BUILDER_H_
+#endif  // ART_LIBELFFILE_ELF_ELF_BUILDER_H_
diff --git a/compiler/debug/elf_debug_reader.h b/libelffile/elf/elf_debug_reader.h
similarity index 95%
rename from compiler/debug/elf_debug_reader.h
rename to libelffile/elf/elf_debug_reader.h
index 1820e7c..3b3c050 100644
--- a/compiler/debug/elf_debug_reader.h
+++ b/libelffile/elf/elf_debug_reader.h
@@ -14,19 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_ELF_DEBUG_READER_H_
-#define ART_COMPILER_DEBUG_ELF_DEBUG_READER_H_
+#ifndef ART_LIBELFFILE_ELF_ELF_DEBUG_READER_H_
+#define ART_LIBELFFILE_ELF_ELF_DEBUG_READER_H_
 
 #include "base/array_ref.h"
-#include "debug/dwarf/headers.h"
-#include "elf.h"
+#include "dwarf/headers.h"
+#include "elf/elf.h"
 #include "xz_utils.h"
 
 #include <map>
 #include <string_view>
 
 namespace art {
-namespace debug {
 
 // Trivial ELF file reader.
 //
@@ -168,6 +167,5 @@
   DISALLOW_COPY_AND_ASSIGN(ElfDebugReader);
 };
 
-}  // namespace debug
 }  // namespace art
-#endif  // ART_COMPILER_DEBUG_ELF_DEBUG_READER_H_
+#endif  // ART_LIBELFFILE_ELF_ELF_DEBUG_READER_H_
diff --git a/compiler/debug/xz_utils.cc b/libelffile/elf/xz_utils.cc
similarity index 98%
rename from compiler/debug/xz_utils.cc
rename to libelffile/elf/xz_utils.cc
index 5b36209..0d47171 100644
--- a/compiler/debug/xz_utils.cc
+++ b/libelffile/elf/xz_utils.cc
@@ -31,7 +31,6 @@
 #include "XzEnc.h"
 
 namespace art {
-namespace debug {
 
 constexpr size_t kChunkSize = kPageSize;
 
@@ -130,5 +129,4 @@
   dst->resize(dst_offset);
 }
 
-}  // namespace debug
 }  // namespace art
diff --git a/compiler/debug/xz_utils.h b/libelffile/elf/xz_utils.h
similarity index 83%
rename from compiler/debug/xz_utils.h
rename to libelffile/elf/xz_utils.h
index 731b03c..2b11584 100644
--- a/compiler/debug/xz_utils.h
+++ b/libelffile/elf/xz_utils.h
@@ -14,20 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DEBUG_XZ_UTILS_H_
-#define ART_COMPILER_DEBUG_XZ_UTILS_H_
+#ifndef ART_LIBELFFILE_ELF_XZ_UTILS_H_
+#define ART_LIBELFFILE_ELF_XZ_UTILS_H_
 
 #include <vector>
 
 #include "base/array_ref.h"
 
 namespace art {
-namespace debug {
 
 void XzCompress(ArrayRef<const uint8_t> src, std::vector<uint8_t>* dst);
 void XzDecompress(ArrayRef<const uint8_t> src, std::vector<uint8_t>* dst);
 
-}  // namespace debug
 }  // namespace art
 
-#endif  // ART_COMPILER_DEBUG_XZ_UTILS_H_
+#endif  // ART_LIBELFFILE_ELF_XZ_UTILS_H_
diff --git a/compiler/linker/buffered_output_stream.cc b/libelffile/stream/buffered_output_stream.cc
similarity index 97%
rename from compiler/linker/buffered_output_stream.cc
rename to libelffile/stream/buffered_output_stream.cc
index 07066b7..4c66c76 100644
--- a/compiler/linker/buffered_output_stream.cc
+++ b/libelffile/stream/buffered_output_stream.cc
@@ -19,7 +19,6 @@
 #include <string.h>
 
 namespace art {
-namespace linker {
 
 BufferedOutputStream::BufferedOutputStream(std::unique_ptr<OutputStream> out)
     : OutputStream(out->GetLocation()),  // Before out is moved to out_.
@@ -68,5 +67,4 @@
   return out_->Seek(offset, whence);
 }
 
-}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/buffered_output_stream.h b/libelffile/stream/buffered_output_stream.h
similarity index 85%
rename from compiler/linker/buffered_output_stream.h
rename to libelffile/stream/buffered_output_stream.h
index cb1c44b..96abb57 100644
--- a/compiler/linker/buffered_output_stream.h
+++ b/libelffile/stream/buffered_output_stream.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_LINKER_BUFFERED_OUTPUT_STREAM_H_
-#define ART_COMPILER_LINKER_BUFFERED_OUTPUT_STREAM_H_
+#ifndef ART_LIBELFFILE_STREAM_BUFFERED_OUTPUT_STREAM_H_
+#define ART_LIBELFFILE_STREAM_BUFFERED_OUTPUT_STREAM_H_
 
 #include <memory>
 
@@ -24,7 +24,6 @@
 #include "base/globals.h"
 
 namespace art {
-namespace linker {
 
 class BufferedOutputStream final : public OutputStream {
  public:
@@ -50,7 +49,6 @@
   DISALLOW_COPY_AND_ASSIGN(BufferedOutputStream);
 };
 
-}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_LINKER_BUFFERED_OUTPUT_STREAM_H_
+#endif  // ART_LIBELFFILE_STREAM_BUFFERED_OUTPUT_STREAM_H_
diff --git a/compiler/linker/error_delaying_output_stream.h b/libelffile/stream/error_delaying_output_stream.h
similarity index 92%
rename from compiler/linker/error_delaying_output_stream.h
rename to libelffile/stream/error_delaying_output_stream.h
index cadd71c..b37ff4e 100644
--- a/compiler/linker/error_delaying_output_stream.h
+++ b/libelffile/stream/error_delaying_output_stream.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_
-#define ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_
+#ifndef ART_LIBELFFILE_STREAM_ERROR_DELAYING_OUTPUT_STREAM_H_
+#define ART_LIBELFFILE_STREAM_ERROR_DELAYING_OUTPUT_STREAM_H_
 
 #include "output_stream.h"
 
@@ -24,7 +24,6 @@
 #include "base/macros.h"
 
 namespace art {
-namespace linker {
 
 // OutputStream wrapper that delays reporting an error until Flush().
 class ErrorDelayingOutputStream final : public OutputStream {
@@ -99,7 +98,6 @@
   off_t output_offset_;  // Keep track of the current position in the stream.
 };
 
-}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_
+#endif  // ART_LIBELFFILE_STREAM_ERROR_DELAYING_OUTPUT_STREAM_H_
diff --git a/compiler/linker/file_output_stream.cc b/libelffile/stream/file_output_stream.cc
similarity index 96%
rename from compiler/linker/file_output_stream.cc
rename to libelffile/stream/file_output_stream.cc
index 477846e..bbfbdfd 100644
--- a/compiler/linker/file_output_stream.cc
+++ b/libelffile/stream/file_output_stream.cc
@@ -22,7 +22,6 @@
 #include "base/unix_file/fd_file.h"
 
 namespace art {
-namespace linker {
 
 FileOutputStream::FileOutputStream(File* file) : OutputStream(file->GetPath()), file_(file) {}
 
@@ -38,5 +37,4 @@
   return file_->Flush() == 0;
 }
 
-}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/file_output_stream.h b/libelffile/stream/file_output_stream.h
similarity index 84%
rename from compiler/linker/file_output_stream.h
rename to libelffile/stream/file_output_stream.h
index 1417132..e29930f 100644
--- a/compiler/linker/file_output_stream.h
+++ b/libelffile/stream/file_output_stream.h
@@ -14,15 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_LINKER_FILE_OUTPUT_STREAM_H_
-#define ART_COMPILER_LINKER_FILE_OUTPUT_STREAM_H_
+#ifndef ART_LIBELFFILE_STREAM_FILE_OUTPUT_STREAM_H_
+#define ART_LIBELFFILE_STREAM_FILE_OUTPUT_STREAM_H_
 
 #include "base/os.h"
 
 #include "output_stream.h"
 
 namespace art {
-namespace linker {
 
 class FileOutputStream final : public OutputStream {
  public:
@@ -42,7 +41,6 @@
   DISALLOW_COPY_AND_ASSIGN(FileOutputStream);
 };
 
-}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_LINKER_FILE_OUTPUT_STREAM_H_
+#endif  // ART_LIBELFFILE_STREAM_FILE_OUTPUT_STREAM_H_
diff --git a/compiler/linker/output_stream.cc b/libelffile/stream/output_stream.cc
similarity index 95%
rename from compiler/linker/output_stream.cc
rename to libelffile/stream/output_stream.cc
index f5a1913..a8b64ca 100644
--- a/compiler/linker/output_stream.cc
+++ b/libelffile/stream/output_stream.cc
@@ -17,7 +17,6 @@
 #include "output_stream.h"
 
 namespace art {
-namespace linker {
 
 std::ostream& operator<<(std::ostream& os, const Whence& rhs) {
   switch (rhs) {
@@ -29,5 +28,4 @@
   return os;
 }
 
-}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/output_stream.h b/libelffile/stream/output_stream.h
similarity index 89%
rename from compiler/linker/output_stream.h
rename to libelffile/stream/output_stream.h
index 5310e2f..c9fce68 100644
--- a/compiler/linker/output_stream.h
+++ b/libelffile/stream/output_stream.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_LINKER_OUTPUT_STREAM_H_
-#define ART_COMPILER_LINKER_OUTPUT_STREAM_H_
+#ifndef ART_LIBELFFILE_STREAM_OUTPUT_STREAM_H_
+#define ART_LIBELFFILE_STREAM_OUTPUT_STREAM_H_
 
 #include <ostream>
 #include <string>
@@ -23,7 +23,6 @@
 #include "base/macros.h"
 
 namespace art {
-namespace linker {
 
 enum Whence {
   kSeekSet = SEEK_SET,
@@ -60,7 +59,6 @@
   DISALLOW_COPY_AND_ASSIGN(OutputStream);
 };
 
-}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_LINKER_OUTPUT_STREAM_H_
+#endif  // ART_LIBELFFILE_STREAM_OUTPUT_STREAM_H_
diff --git a/compiler/linker/vector_output_stream.cc b/libelffile/stream/vector_output_stream.cc
similarity index 96%
rename from compiler/linker/vector_output_stream.cc
rename to libelffile/stream/vector_output_stream.cc
index f2cae5b..f9724c1 100644
--- a/compiler/linker/vector_output_stream.cc
+++ b/libelffile/stream/vector_output_stream.cc
@@ -19,7 +19,6 @@
 #include <android-base/logging.h>
 
 namespace art {
-namespace linker {
 
 VectorOutputStream::VectorOutputStream(const std::string& location, std::vector<uint8_t>* vector)
     : OutputStream(location), offset_(vector->size()), vector_(vector) {}
@@ -46,5 +45,4 @@
   return offset_;
 }
 
-}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/vector_output_stream.h b/libelffile/stream/vector_output_stream.h
similarity index 89%
rename from compiler/linker/vector_output_stream.h
rename to libelffile/stream/vector_output_stream.h
index 0d34da6..cf7cbb6 100644
--- a/compiler/linker/vector_output_stream.h
+++ b/libelffile/stream/vector_output_stream.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_LINKER_VECTOR_OUTPUT_STREAM_H_
-#define ART_COMPILER_LINKER_VECTOR_OUTPUT_STREAM_H_
+#ifndef ART_LIBELFFILE_STREAM_VECTOR_OUTPUT_STREAM_H_
+#define ART_LIBELFFILE_STREAM_VECTOR_OUTPUT_STREAM_H_
 
 #include "output_stream.h"
 
@@ -24,7 +24,6 @@
 #include <vector>
 
 namespace art {
-namespace linker {
 
 class VectorOutputStream final : public OutputStream {
  public:
@@ -65,7 +64,6 @@
   DISALLOW_COPY_AND_ASSIGN(VectorOutputStream);
 };
 
-}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_LINKER_VECTOR_OUTPUT_STREAM_H_
+#endif  // ART_LIBELFFILE_STREAM_VECTOR_OUTPUT_STREAM_H_
diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc
index 26d0094..e395985 100644
--- a/oatdump/oatdump.cc
+++ b/oatdump/oatdump.cc
@@ -59,18 +59,15 @@
 #include "dex/type_lookup_table.h"
 #include "dexlayout.h"
 #include "disassembler.h"
+#include "elf/elf_builder.h"
 #include "gc/accounting/space_bitmap-inl.h"
 #include "gc/space/image_space.h"
 #include "gc/space/large_object_space.h"
 #include "gc/space/space-inl.h"
 #include "image-inl.h"
 #include "imtable-inl.h"
-#include "subtype_check.h"
 #include "index_bss_mapping.h"
 #include "interpreter/unstarted_runtime.h"
-#include "linker/buffered_output_stream.h"
-#include "linker/elf_builder.h"
-#include "linker/file_output_stream.h"
 #include "mirror/array-inl.h"
 #include "mirror/class-inl.h"
 #include "mirror/dex_cache-inl.h"
@@ -82,6 +79,9 @@
 #include "scoped_thread_state_change-inl.h"
 #include "stack.h"
 #include "stack_map.h"
+#include "stream/buffered_output_stream.h"
+#include "stream/file_output_stream.h"
+#include "subtype_check.h"
 #include "thread_list.h"
 #include "vdex_file.h"
 #include "verifier/method_verifier.h"
@@ -150,10 +150,10 @@
     if (elf_file == nullptr) {
       return false;
     }
-    std::unique_ptr<linker::BufferedOutputStream> output_stream =
-        std::make_unique<linker::BufferedOutputStream>(
-            std::make_unique<linker::FileOutputStream>(elf_file.get()));
-    builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
+    std::unique_ptr<BufferedOutputStream> output_stream =
+        std::make_unique<BufferedOutputStream>(
+            std::make_unique<FileOutputStream>(elf_file.get()));
+    builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
 
     builder_->Start();
 
@@ -176,9 +176,6 @@
       text->End();
     }
 
-    if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
-      builder_->WriteMIPSabiflagsSection();
-    }
     builder_->PrepareDynamicSection(elf_file->GetPath(),
                                     rodata_size,
                                     text_size,
@@ -330,7 +327,7 @@
 
  private:
   const OatFile* oat_file_;
-  std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_;
+  std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
   std::vector<debug::MethodDebugInfo> method_debug_infos_;
   std::unordered_set<uint32_t> seen_offsets_;
   const std::string output_name_;
diff --git a/runtime/Android.bp b/runtime/Android.bp
index ae97907..595c7af 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -391,6 +391,7 @@
         "libicuuc_headers",
         "libnativehelper_header_only",
         "jni_platform_headers",
+        "libelffile-headers",
     ],
     shared_libs: [
         "libartpalette",
@@ -554,7 +555,7 @@
     ],
     shared_libs: [
         "libartd",
-	"libartbase-art-gtest",
+        "libartbase-art-gtest",
         "libbase",
         "libbacktrace",
     ],
diff --git a/runtime/elf_file.h b/runtime/elf_file.h
index 8da7e1a..e2094bc 100644
--- a/runtime/elf_file.h
+++ b/runtime/elf_file.h
@@ -23,7 +23,7 @@
 #include "base/macros.h"
 #include "base/os.h"
 // Explicitly include our own elf.h to avoid Linux and other dependencies.
-#include "./elf.h"
+#include "elf/elf.h"
 
 namespace art {
 
diff --git a/runtime/elf_file_impl.h b/runtime/elf_file_impl.h
index b55b60f..c1899d2 100644
--- a/runtime/elf_file_impl.h
+++ b/runtime/elf_file_impl.h
@@ -23,7 +23,7 @@
 #include <vector>
 
 // Explicitly include our own elf.h to avoid Linux and other dependencies.
-#include "./elf.h"
+#include "elf/elf.h"
 #include "base/mem_map.h"
 
 namespace art {
diff --git a/runtime/elf_utils.h b/runtime/elf_utils.h
index 0cac8e8..deb081d 100644
--- a/runtime/elf_utils.h
+++ b/runtime/elf_utils.h
@@ -22,7 +22,7 @@
 #include <android-base/logging.h>
 
 // Explicitly include our own elf.h to avoid Linux and other dependencies.
-#include "./elf.h"
+#include "elf/elf.h"
 
 namespace art {
 
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 67803b6..6edf448 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -16,6 +16,9 @@
 
 #include "oat_file.h"
 
+// Our elf.h needs to be included before the system one is indirectly included.
+#include "elf/elf.h"
+
 #include <dlfcn.h>
 #ifndef __APPLE__
 #include <link.h>  // for dl_iterate_phdr.