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/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 {