Refactor linker files from compiler/ to dex2oat/.

This shifts some code from the libart-compiler.so to dex2oat
and reduces memory needed for JIT. We also avoid loading the
libart-dexlayout.so for JIT but the memory savings are
minimal (one shared clean page, two shared dirty pages and
some per-app kernel mmap data) as the code has never been
needed in memory by JIT.

aosp_angler-userdebug file sizes (stripped):
  lib64/libart-compiler.so: 2989112 -> 2671888 (-310KiB)
  lib/libart-compiler.so: 2160816 -> 1939276 (-216KiB)
  bin/dex2oat: 141868 -> 368808 (+222KiB)
LOAD/executable elf mapping sizes:
  lib64/libart-compiler.so: 2866308 -> 2555500 (-304KiB)
  lib/libart-compiler.so: 2050960 -> 1834836 (-211KiB)
  bin/dex2oat: 129316 -> 345916 (+212KiB)

Test: m test-art-host-gtest
Test: testrunner.py --host
Test: cd art/; mma; cd -
Change-Id: If62f02847a6cbb208eaf7e1f3e91af4663fa4a5f
diff --git a/build/art.go b/build/art.go
index 19b39cd..1bcaf51 100644
--- a/build/art.go
+++ b/build/art.go
@@ -261,6 +261,7 @@
 
 func init() {
 	android.RegisterModuleType("art_cc_library", artLibrary)
+	android.RegisterModuleType("art_cc_static_library", artStaticLibrary)
 	android.RegisterModuleType("art_cc_binary", artBinary)
 	android.RegisterModuleType("art_cc_test", artTest)
 	android.RegisterModuleType("art_cc_test_library", artTestLibrary)
@@ -292,8 +293,18 @@
 }
 
 func artLibrary() android.Module {
-	library, _ := cc.NewLibrary(android.HostAndDeviceSupported)
-	module := library.Init()
+	m, _ := cc.NewLibrary(android.HostAndDeviceSupported)
+	module := m.Init()
+
+	installCodegenCustomizer(module, true)
+
+	return module
+}
+
+func artStaticLibrary() android.Module {
+	m, library := cc.NewLibrary(android.HostAndDeviceSupported)
+	library.BuildOnlyStatic()
+	module := m.Init()
 
 	installCodegenCustomizer(module, true)
 
diff --git a/compiler/Android.bp b/compiler/Android.bp
index c798d97..1475679 100644
--- a/compiler/Android.bp
+++ b/compiler/Android.bp
@@ -38,7 +38,6 @@
         "driver/dex_compilation_unit.cc",
         "linker/buffered_output_stream.cc",
         "linker/file_output_stream.cc",
-        "linker/multi_oat_relative_patcher.cc",
         "linker/output_stream.cc",
         "linker/vector_output_stream.cc",
         "linker/relative_patcher.cc",
@@ -95,10 +94,6 @@
         "utils/jni_macro_assembler.cc",
         "utils/swap_space.cc",
         "compiler.cc",
-        "elf_writer.cc",
-        "elf_writer_quick.cc",
-        "image_writer.cc",
-        "oat_writer.cc",
     ],
 
     codegen: {
@@ -198,19 +193,10 @@
     generated_sources: ["art_compiler_operator_srcs"],
     shared_libs: [
         "libbase",
-        "liblz4",
         "liblzma",
     ],
     include_dirs: ["art/disassembler"],
     export_include_dirs: ["."],
-
-    // For SHA-1 checksumming of build ID
-    static: {
-        whole_static_libs: ["libcrypto"],
-    },
-    shared: {
-        shared_libs: ["libcrypto"],
-    },
 }
 
 gensrcs {
@@ -222,7 +208,6 @@
         "dex/dex_to_dex_compiler.h",
         "driver/compiler_driver.h",
         "driver/compiler_options.h",
-        "image_writer.h",
         "optimizing/locations.h",
 
         "utils/arm/constants_arm.h",
@@ -265,7 +250,6 @@
     },
     shared_libs: [
         "libart",
-        "libart-dexlayout",
     ],
 }
 
@@ -305,7 +289,6 @@
     },
     shared_libs: [
         "libartd",
-        "libartd-dexlayout"
     ],
 }
 
@@ -332,15 +315,10 @@
         "dex/dex_to_dex_decompiler_test.cc",
         "driver/compiled_method_storage_test.cc",
         "driver/compiler_driver_test.cc",
-        "elf_writer_test.cc",
         "exception_test.cc",
-        "image_test.cc",
-        "image_write_read_test.cc",
         "jni/jni_compiler_test.cc",
         "linker/method_bss_mapping_encoder_test.cc",
-        "linker/multi_oat_relative_patcher_test.cc",
         "linker/output_stream_test.cc",
-        "oat_test.cc",
         "optimizing/bounds_check_elimination_test.cc",
         "optimizing/dominator_test.cc",
         "optimizing/find_loops_test.cc",
diff --git a/compiler/debug/dwarf/dwarf_test.h b/compiler/debug/dwarf/dwarf_test.h
index e1f538d..b30ff14 100644
--- a/compiler/debug/dwarf/dwarf_test.h
+++ b/compiler/debug/dwarf/dwarf_test.h
@@ -28,8 +28,8 @@
 
 #include "base/unix_file/fd_file.h"
 #include "common_runtime_test.h"
-#include "elf_builder.h"
 #include "gtest/gtest.h"
+#include "linker/elf_builder.h"
 #include "linker/file_output_stream.h"
 #include "os.h"
 
@@ -62,8 +62,8 @@
     // Write simple elf file with just the DWARF sections.
     InstructionSet isa = (sizeof(typename ElfTypes::Addr) == 8) ? kX86_64 : kX86;
     ScratchFile file;
-    FileOutputStream output_stream(file.GetFile());
-    ElfBuilder<ElfTypes> builder(isa, nullptr, &output_stream);
+    linker::FileOutputStream output_stream(file.GetFile());
+    linker::ElfBuilder<ElfTypes> builder(isa, nullptr, &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 f9d33c1..6dacdfa 100644
--- a/compiler/debug/elf_debug_frame_writer.h
+++ b/compiler/debug/elf_debug_frame_writer.h
@@ -24,7 +24,7 @@
 #include "debug/dwarf/dwarf_constants.h"
 #include "debug/dwarf/headers.h"
 #include "debug/method_debug_info.h"
-#include "elf_builder.h"
+#include "linker/elf_builder.h"
 
 namespace art {
 namespace debug {
@@ -168,7 +168,7 @@
 }
 
 template<typename ElfTypes>
-void WriteCFISection(ElfBuilder<ElfTypes>* builder,
+void WriteCFISection(linker::ElfBuilder<ElfTypes>* builder,
                      const ArrayRef<const MethodDebugInfo>& method_infos,
                      dwarf::CFIFormat format,
                      bool write_oat_patches) {
diff --git a/compiler/debug/elf_debug_info_writer.h b/compiler/debug/elf_debug_info_writer.h
index 2801240..2b61727 100644
--- a/compiler/debug/elf_debug_info_writer.h
+++ b/compiler/debug/elf_debug_info_writer.h
@@ -29,9 +29,9 @@
 #include "debug/method_debug_info.h"
 #include "dex_file-inl.h"
 #include "dex_file.h"
-#include "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"
@@ -68,7 +68,7 @@
   using Elf_Addr = typename ElfTypes::Addr;
 
  public:
-  explicit ElfDebugInfoWriter(ElfBuilder<ElfTypes>* builder)
+  explicit ElfDebugInfoWriter(linker::ElfBuilder<ElfTypes>* builder)
       : builder_(builder),
         debug_abbrev_(&debug_abbrev_buffer_) {
   }
@@ -93,7 +93,7 @@
   }
 
  private:
-  ElfBuilder<ElfTypes>* builder_;
+  linker::ElfBuilder<ElfTypes>* builder_;
   std::vector<uintptr_t> debug_info_patches_;
   std::vector<uint8_t> debug_abbrev_buffer_;
   dwarf::DebugAbbrevWriter<> debug_abbrev_;
diff --git a/compiler/debug/elf_debug_line_writer.h b/compiler/debug/elf_debug_line_writer.h
index cdd1e53..cf5d65e 100644
--- a/compiler/debug/elf_debug_line_writer.h
+++ b/compiler/debug/elf_debug_line_writer.h
@@ -25,7 +25,7 @@
 #include "debug/dwarf/headers.h"
 #include "debug/elf_compilation_unit.h"
 #include "dex_file-inl.h"
-#include "elf_builder.h"
+#include "linker/elf_builder.h"
 #include "stack_map.h"
 
 namespace art {
@@ -43,7 +43,7 @@
   using Elf_Addr = typename ElfTypes::Addr;
 
  public:
-  explicit ElfDebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
+  explicit ElfDebugLineWriter(linker::ElfBuilder<ElfTypes>* builder) : builder_(builder) {
   }
 
   void Start() {
@@ -280,7 +280,7 @@
   }
 
  private:
-  ElfBuilder<ElfTypes>* builder_;
+  linker::ElfBuilder<ElfTypes>* builder_;
   std::vector<uintptr_t> debug_line_patches_;
 };
 
diff --git a/compiler/debug/elf_debug_writer.cc b/compiler/debug/elf_debug_writer.cc
index c5ff858..33c46d7 100644
--- a/compiler/debug/elf_debug_writer.cc
+++ b/compiler/debug/elf_debug_writer.cc
@@ -29,7 +29,7 @@
 #include "debug/elf_gnu_debugdata_writer.h"
 #include "debug/elf_symtab_writer.h"
 #include "debug/method_debug_info.h"
-#include "elf_builder.h"
+#include "linker/elf_builder.h"
 #include "linker/vector_output_stream.h"
 #include "oat.h"
 
@@ -37,7 +37,7 @@
 namespace debug {
 
 template <typename ElfTypes>
-void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
+void WriteDebugInfo(linker::ElfBuilder<ElfTypes>* builder,
                     const ArrayRef<const MethodDebugInfo>& method_infos,
                     dwarf::CFIFormat cfi_format,
                     bool write_oat_patches) {
@@ -133,8 +133,9 @@
     const ArrayRef<const MethodDebugInfo>& method_infos) {
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
-  VectorOutputStream out("Debug ELF file", &buffer);
-  std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
+  linker::VectorOutputStream out("Debug ELF file", &buffer);
+  std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
+      new linker::ElfBuilder<ElfTypes>(isa, features, &out));
   // No program headers since the ELF file is not linked and has no allocated sections.
   builder->Start(false /* write_program_headers */);
   WriteDebugInfo(builder.get(),
@@ -165,8 +166,9 @@
     REQUIRES_SHARED(Locks::mutator_lock_) {
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
-  VectorOutputStream out("Debug ELF file", &buffer);
-  std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
+  linker::VectorOutputStream out("Debug ELF file", &buffer);
+  std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
+      new linker::ElfBuilder<ElfTypes>(isa, features, &out));
   // No program headers since the ELF file is not linked and has no allocated sections.
   builder->Start(false /* write_program_headers */);
   ElfDebugInfoWriter<ElfTypes> info_writer(builder.get());
@@ -192,12 +194,12 @@
 
 // Explicit instantiations
 template void WriteDebugInfo<ElfTypes32>(
-    ElfBuilder<ElfTypes32>* builder,
+    linker::ElfBuilder<ElfTypes32>* builder,
     const ArrayRef<const MethodDebugInfo>& method_infos,
     dwarf::CFIFormat cfi_format,
     bool write_oat_patches);
 template void WriteDebugInfo<ElfTypes64>(
-    ElfBuilder<ElfTypes64>* builder,
+    linker::ElfBuilder<ElfTypes64>* builder,
     const ArrayRef<const MethodDebugInfo>& method_infos,
     dwarf::CFIFormat cfi_format,
     bool write_oat_patches);
diff --git a/compiler/debug/elf_debug_writer.h b/compiler/debug/elf_debug_writer.h
index 6e26ba3..d24ca9b 100644
--- a/compiler/debug/elf_debug_writer.h
+++ b/compiler/debug/elf_debug_writer.h
@@ -23,7 +23,7 @@
 #include "base/macros.h"
 #include "base/mutex.h"
 #include "debug/dwarf/dwarf_constants.h"
-#include "elf_builder.h"
+#include "linker/elf_builder.h"
 
 namespace art {
 class OatHeader;
@@ -35,7 +35,7 @@
 
 template <typename ElfTypes>
 void WriteDebugInfo(
-    ElfBuilder<ElfTypes>* builder,
+    linker::ElfBuilder<ElfTypes>* builder,
     const ArrayRef<const MethodDebugInfo>& method_infos,
     dwarf::CFIFormat cfi_format,
     bool write_oat_patches);
diff --git a/compiler/debug/elf_gnu_debugdata_writer.h b/compiler/debug/elf_gnu_debugdata_writer.h
index fb63d62..1cdf6b0 100644
--- a/compiler/debug/elf_gnu_debugdata_writer.h
+++ b/compiler/debug/elf_gnu_debugdata_writer.h
@@ -20,7 +20,7 @@
 #include <vector>
 
 #include "arch/instruction_set.h"
-#include "elf_builder.h"
+#include "linker/elf_builder.h"
 #include "linker/vector_output_stream.h"
 
 // liblzma.
@@ -85,8 +85,9 @@
     const ArrayRef<const MethodDebugInfo>& method_infos) {
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
-  VectorOutputStream out("Mini-debug-info ELF file", &buffer);
-  std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
+  linker::VectorOutputStream out("Mini-debug-info ELF file", &buffer);
+  std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
+      new linker::ElfBuilder<ElfTypes>(isa, features, &out));
   builder->Start();
   // Mirror .rodata and .text as NOBITS sections.
   // It is needed to detected relocations after compression.
diff --git a/compiler/debug/elf_symtab_writer.h b/compiler/debug/elf_symtab_writer.h
index abd2699..b37f984 100644
--- a/compiler/debug/elf_symtab_writer.h
+++ b/compiler/debug/elf_symtab_writer.h
@@ -20,7 +20,7 @@
 #include <unordered_set>
 
 #include "debug/method_debug_info.h"
-#include "elf_builder.h"
+#include "linker/elf_builder.h"
 #include "utils.h"
 
 namespace art {
@@ -36,7 +36,7 @@
 constexpr bool kGenerateSingleArmMappingSymbol = true;
 
 template <typename ElfTypes>
-static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
+static void WriteDebugSymbols(linker::ElfBuilder<ElfTypes>* builder,
                               const ArrayRef<const MethodDebugInfo>& method_infos,
                               bool with_signature) {
   uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max();
diff --git a/compiler/linker/buffered_output_stream.cc b/compiler/linker/buffered_output_stream.cc
index 4c66c76..07066b7 100644
--- a/compiler/linker/buffered_output_stream.cc
+++ b/compiler/linker/buffered_output_stream.cc
@@ -19,6 +19,7 @@
 #include <string.h>
 
 namespace art {
+namespace linker {
 
 BufferedOutputStream::BufferedOutputStream(std::unique_ptr<OutputStream> out)
     : OutputStream(out->GetLocation()),  // Before out is moved to out_.
@@ -67,4 +68,5 @@
   return out_->Seek(offset, whence);
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/buffered_output_stream.h b/compiler/linker/buffered_output_stream.h
index a2eefbb..66994e8 100644
--- a/compiler/linker/buffered_output_stream.h
+++ b/compiler/linker/buffered_output_stream.h
@@ -24,6 +24,7 @@
 #include "globals.h"
 
 namespace art {
+namespace linker {
 
 class BufferedOutputStream FINAL : public OutputStream {
  public:
@@ -49,6 +50,7 @@
   DISALLOW_COPY_AND_ASSIGN(BufferedOutputStream);
 };
 
+}  // namespace linker
 }  // namespace art
 
 #endif  // ART_COMPILER_LINKER_BUFFERED_OUTPUT_STREAM_H_
diff --git a/compiler/elf_builder.h b/compiler/linker/elf_builder.h
similarity index 99%
rename from compiler/elf_builder.h
rename to compiler/linker/elf_builder.h
index 2ef9fa1..7941237 100644
--- a/compiler/elf_builder.h
+++ b/compiler/linker/elf_builder.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_ELF_BUILDER_H_
-#define ART_COMPILER_ELF_BUILDER_H_
+#ifndef ART_COMPILER_LINKER_ELF_BUILDER_H_
+#define ART_COMPILER_LINKER_ELF_BUILDER_H_
 
 #include <vector>
 
@@ -30,6 +30,7 @@
 #include "linker/error_delaying_output_stream.h"
 
 namespace art {
+namespace linker {
 
 // Writes ELF file.
 //
@@ -1021,6 +1022,7 @@
   DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
 };
 
+}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_ELF_BUILDER_H_
+#endif  // ART_COMPILER_LINKER_ELF_BUILDER_H_
diff --git a/compiler/linker/error_delaying_output_stream.h b/compiler/linker/error_delaying_output_stream.h
index 99410e4..33e6b5a 100644
--- a/compiler/linker/error_delaying_output_stream.h
+++ b/compiler/linker/error_delaying_output_stream.h
@@ -22,6 +22,7 @@
 #include "base/logging.h"
 
 namespace art {
+namespace linker {
 
 // OutputStream wrapper that delays reporting an error until Flush().
 class ErrorDelayingOutputStream FINAL : public OutputStream {
@@ -96,6 +97,7 @@
   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_
diff --git a/compiler/linker/file_output_stream.cc b/compiler/linker/file_output_stream.cc
index bbfbdfd..477846e 100644
--- a/compiler/linker/file_output_stream.cc
+++ b/compiler/linker/file_output_stream.cc
@@ -22,6 +22,7 @@
 #include "base/unix_file/fd_file.h"
 
 namespace art {
+namespace linker {
 
 FileOutputStream::FileOutputStream(File* file) : OutputStream(file->GetPath()), file_(file) {}
 
@@ -37,4 +38,5 @@
   return file_->Flush() == 0;
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/file_output_stream.h b/compiler/linker/file_output_stream.h
index f2d8453..28296a4 100644
--- a/compiler/linker/file_output_stream.h
+++ b/compiler/linker/file_output_stream.h
@@ -22,6 +22,7 @@
 #include "os.h"
 
 namespace art {
+namespace linker {
 
 class FileOutputStream FINAL : public OutputStream {
  public:
@@ -41,6 +42,7 @@
   DISALLOW_COPY_AND_ASSIGN(FileOutputStream);
 };
 
+}  // namespace linker
 }  // namespace art
 
 #endif  // ART_COMPILER_LINKER_FILE_OUTPUT_STREAM_H_
diff --git a/compiler/linker/output_stream.cc b/compiler/linker/output_stream.cc
index a8b64ca..f5a1913 100644
--- a/compiler/linker/output_stream.cc
+++ b/compiler/linker/output_stream.cc
@@ -17,6 +17,7 @@
 #include "output_stream.h"
 
 namespace art {
+namespace linker {
 
 std::ostream& operator<<(std::ostream& os, const Whence& rhs) {
   switch (rhs) {
@@ -28,4 +29,5 @@
   return os;
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/output_stream.h b/compiler/linker/output_stream.h
index 96a5f48..5310e2f 100644
--- a/compiler/linker/output_stream.h
+++ b/compiler/linker/output_stream.h
@@ -23,6 +23,7 @@
 #include "base/macros.h"
 
 namespace art {
+namespace linker {
 
 enum Whence {
   kSeekSet = SEEK_SET,
@@ -59,6 +60,7 @@
   DISALLOW_COPY_AND_ASSIGN(OutputStream);
 };
 
+}  // namespace linker
 }  // namespace art
 
 #endif  // ART_COMPILER_LINKER_OUTPUT_STREAM_H_
diff --git a/compiler/linker/output_stream_test.cc b/compiler/linker/output_stream_test.cc
index 87cb100..ad29840 100644
--- a/compiler/linker/output_stream_test.cc
+++ b/compiler/linker/output_stream_test.cc
@@ -23,6 +23,7 @@
 #include "common_runtime_test.h"
 
 namespace art {
+namespace linker {
 
 class OutputStreamTest : public CommonRuntimeTest {
  protected:
@@ -133,4 +134,5 @@
   ASSERT_TRUE(checking_output_stream->flush_called);
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/relative_patcher.h b/compiler/linker/relative_patcher.h
index 53a0966..e079946 100644
--- a/compiler/linker/relative_patcher.h
+++ b/compiler/linker/relative_patcher.h
@@ -29,7 +29,6 @@
 
 class CompiledMethod;
 class LinkerPatch;
-class OutputStream;
 
 namespace debug {
 struct MethodDebugInfo;
@@ -37,6 +36,8 @@
 
 namespace linker {
 
+class OutputStream;
+
 /**
  * @class RelativePatcherTargetProvider
  * @brief Interface for providing method offsets for relative call targets.
diff --git a/compiler/linker/relative_patcher_test.h b/compiler/linker/relative_patcher_test.h
index ca8743a..f7dbc1e 100644
--- a/compiler/linker/relative_patcher_test.h
+++ b/compiler/linker/relative_patcher_test.h
@@ -252,8 +252,8 @@
   }
 
   // Map method reference to assinged offset.
-  // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
-  class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
+  // Wrap the map in a class implementing RelativePatcherTargetProvider.
+  class MethodOffsetMap FINAL : public RelativePatcherTargetProvider {
    public:
     std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE {
       auto it = map.find(ref);
diff --git a/compiler/linker/vector_output_stream.cc b/compiler/linker/vector_output_stream.cc
index f758005..75f90e5 100644
--- a/compiler/linker/vector_output_stream.cc
+++ b/compiler/linker/vector_output_stream.cc
@@ -19,6 +19,7 @@
 #include "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) {}
@@ -45,4 +46,5 @@
   return offset_;
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/linker/vector_output_stream.h b/compiler/linker/vector_output_stream.h
index a9b93e7..92caf59 100644
--- a/compiler/linker/vector_output_stream.h
+++ b/compiler/linker/vector_output_stream.h
@@ -24,6 +24,7 @@
 #include <vector>
 
 namespace art {
+namespace linker {
 
 class VectorOutputStream FINAL : public OutputStream {
  public:
@@ -64,6 +65,7 @@
   DISALLOW_COPY_AND_ASSIGN(VectorOutputStream);
 };
 
+}  // namespace linker
 }  // namespace art
 
 #endif  // ART_COMPILER_LINKER_VECTOR_OUTPUT_STREAM_H_
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index e128a15..8dd2762 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -63,7 +63,6 @@
 #include "driver/compiler_driver-inl.h"
 #include "driver/compiler_options.h"
 #include "driver/dex_compilation_unit.h"
-#include "elf_writer_quick.h"
 #include "graph_checker.h"
 #include "graph_visualizer.h"
 #include "gvn.h"
diff --git a/dex2oat/Android.bp b/dex2oat/Android.bp
index f13f01c..bdb8ff1 100644
--- a/dex2oat/Android.bp
+++ b/dex2oat/Android.bp
@@ -14,6 +14,78 @@
 // limitations under the License.
 //
 
+art_cc_defaults {
+    name: "libart-dex2oat-defaults",
+    defaults: ["art_defaults"],
+    host_supported: true,
+    clang: true,
+    srcs: [
+        "linker/elf_writer.cc",
+        "linker/elf_writer_quick.cc",
+        "linker/image_writer.cc",
+        "linker/multi_oat_relative_patcher.cc",
+        "linker/oat_writer.cc",
+    ],
+    target: {
+        host: {
+            // For compiler driver TLS.
+            host_ldlibs: ["-lpthread"],
+        },
+        android: {
+            // For atrace.
+            shared_libs: ["libcutils"],
+        },
+    },
+    generated_sources: ["art_dex2oat_operator_srcs"],
+    shared_libs: [
+        "libart-compiler",
+        "libart-dexlayout",
+        "libbase",
+        "liblz4",
+        "liblzma",
+    ],
+    export_include_dirs: ["."],
+
+    // For SHA-1 checksumming of build ID
+    static: {
+        whole_static_libs: ["libcrypto"],
+    },
+    shared: {
+        shared_libs: ["libcrypto"],
+    },
+}
+
+gensrcs {
+    name: "art_dex2oat_operator_srcs",
+    cmd: "$(location generate-operator-out.py) art/dex2oat $(in) > $(out)",
+    tool_files: ["generate-operator-out.py"],
+    srcs: [
+        "linker/image_writer.h",
+    ],
+    output_extension: "operator_out.cc",
+}
+
+art_cc_static_library {
+    name: "libart-dex2oat",
+    defaults: ["libart-dex2oat-defaults"],
+    shared_libs: [
+        "libart-compiler",
+        "libart"
+    ],
+}
+
+art_cc_static_library {
+    name: "libartd-dex2oat",
+    defaults: [
+        "art_debug_defaults",
+        "libart-dex2oat-defaults",
+    ],
+    shared_libs: [
+        "libartd-compiler",
+        "libartd"
+    ],
+}
+
 cc_library_headers {
     name: "dex2oat_headers",
     host_supported: true,
@@ -45,11 +117,16 @@
         "dex2oat-defaults",
     ],
     shared_libs: [
-        "libart",
         "libart-compiler",
+        "libart-dexlayout",
+        "libart",
         "libbase",
+        "liblz4",
         "libsigchain",
     ],
+    static_libs: [
+        "libart-dex2oat",
+    ]
 }
 
 art_cc_binary {
@@ -59,11 +136,16 @@
         "dex2oat-defaults",
     ],
     shared_libs: [
-        "libartd",
         "libartd-compiler",
+        "libartd-dexlayout",
+        "libartd",
         "libbase",
+        "liblz4",
         "libsigchain",
     ],
+    static_libs: [
+        "libartd-dex2oat",
+    ]
 }
 
 art_cc_binary {
@@ -85,6 +167,7 @@
         "-z muldefs",
     ],
     static_libs: [
+        "libart-dex2oat",
         "libart-compiler",
         "libart-dexlayout",
         "libart",
@@ -115,6 +198,7 @@
         "-z muldefs",
     ],
     static_libs: [
+        "libartd-dex2oat",
         "libartd-compiler",
         "libartd-dexlayout",
         "libartd",
@@ -131,6 +215,21 @@
     srcs: [
         "dex2oat_test.cc",
         "dex2oat_image_test.cc",
+        "linker/elf_writer_test.cc",
+        "linker/image_test.cc",
+        "linker/image_write_read_test.cc",
+        "linker/multi_oat_relative_patcher_test.cc",
+        "linker/oat_writer_test.cc",
     ],
     header_libs: ["dex2oat_headers"],
+    shared_libs: [
+        "libartd-compiler",
+        "libartd-dexlayout",
+        "libbase",
+        "liblz4",
+        "libsigchain",
+    ],
+    static_libs: [
+        "libartd-dex2oat",
+    ]
 }
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index d8caf42..21d3895 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -61,19 +61,20 @@
 #include "driver/compiler_driver.h"
 #include "driver/compiler_options.h"
 #include "elf_file.h"
-#include "elf_writer.h"
-#include "elf_writer_quick.h"
 #include "gc/space/image_space.h"
 #include "gc/space/space-inl.h"
 #include "gc/verification.h"
-#include "image_writer.h"
 #include "interpreter/unstarted_runtime.h"
 #include "java_vm_ext.h"
 #include "jit/profile_compilation_info.h"
 #include "leb128.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"
 #include "mirror/class-inl.h"
 #include "mirror/class_loader.h"
 #include "mirror/object-inl.h"
@@ -81,7 +82,6 @@
 #include "nativehelper/ScopedLocalRef.h"
 #include "oat_file.h"
 #include "oat_file_assistant.h"
-#include "oat_writer.h"
 #include "os.h"
 #include "runtime.h"
 #include "runtime_options.h"
@@ -1455,8 +1455,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<BufferedOutputStream> vdex_out = std::make_unique<BufferedOutputStream>(
-          std::make_unique<FileOutputStream>(vdex_files_.back().get()));
+      std::unique_ptr<linker::BufferedOutputStream> vdex_out =
+          std::make_unique<linker::BufferedOutputStream>(
+              std::make_unique<linker::FileOutputStream>(vdex_files_.back().get()));
       if (!vdex_out->WriteFully(&VdexFile::Header::kVdexInvalidMagic,
                                 arraysize(VdexFile::Header::kVdexInvalidMagic))) {
         PLOG(ERROR) << "Failed to invalidate vdex header. File: " << vdex_out->GetLocation();
@@ -2026,14 +2027,14 @@
         VLOG(compiler) << "App image base=" << reinterpret_cast<void*>(image_base_);
       }
 
-      image_writer_.reset(new ImageWriter(*driver_,
-                                          image_base_,
-                                          compiler_options_->GetCompilePic(),
-                                          IsAppImage(),
-                                          image_storage_mode_,
-                                          oat_filenames_,
-                                          dex_file_oat_index_map_,
-                                          dirty_image_objects_.get()));
+      image_writer_.reset(new linker::ImageWriter(*driver_,
+                                                  image_base_,
+                                                  compiler_options_->GetCompilePic(),
+                                                  IsAppImage(),
+                                                  image_storage_mode_,
+                                                  oat_filenames_,
+                                                  dex_file_oat_index_map_,
+                                                  dirty_image_objects_.get()));
 
       // We need to prepare method offsets in the image address space for direct method patching.
       TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_);
@@ -2046,7 +2047,7 @@
     // Initialize the writers with the compiler driver, image writer, and their
     // dex files. The writers were created without those being there yet.
     for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
-      std::unique_ptr<OatWriter>& oat_writer = oat_writers_[i];
+      std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
       std::vector<const DexFile*>& dex_files = dex_files_per_oat_file_[i];
       oat_writer->Initialize(driver_.get(), image_writer_.get(), dex_files);
     }
@@ -2057,8 +2058,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<BufferedOutputStream> vdex_out =
-            std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(vdex_file));
+        std::unique_ptr<linker::BufferedOutputStream> vdex_out =
+            std::make_unique<linker::BufferedOutputStream>(
+                std::make_unique<linker::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();
@@ -2082,8 +2084,8 @@
       TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_);
       linker::MultiOatRelativePatcher patcher(instruction_set_, instruction_set_features_.get());
       for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
-        std::unique_ptr<ElfWriter>& elf_writer = elf_writers_[i];
-        std::unique_ptr<OatWriter>& oat_writer = oat_writers_[i];
+        std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i];
+        std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
 
         oat_writer->PrepareLayout(&patcher);
 
@@ -2116,14 +2118,14 @@
 
       for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
         std::unique_ptr<File>& oat_file = oat_files_[i];
-        std::unique_ptr<ElfWriter>& elf_writer = elf_writers_[i];
-        std::unique_ptr<OatWriter>& oat_writer = oat_writers_[i];
+        std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i];
+        std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
 
         // We need to mirror the layout of the ELF file in the compressed debug-info.
         // Therefore PrepareDebugInfo() relies on the SetLoadedSectionSizes() call further above.
         elf_writer->PrepareDebugInfo(oat_writer->GetMethodDebugInfo());
 
-        OutputStream*& rodata = rodata_[i];
+        linker::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();
@@ -2132,7 +2134,7 @@
         elf_writer->EndRoData(rodata);
         rodata = nullptr;
 
-        OutputStream* text = elf_writer->StartText();
+        linker::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;
@@ -2510,13 +2512,13 @@
     elf_writers_.reserve(oat_files_.size());
     oat_writers_.reserve(oat_files_.size());
     for (const std::unique_ptr<File>& oat_file : oat_files_) {
-      elf_writers_.emplace_back(CreateElfWriterQuick(instruction_set_,
-                                                     instruction_set_features_.get(),
-                                                     compiler_options_.get(),
-                                                     oat_file.get()));
+      elf_writers_.emplace_back(linker::CreateElfWriterQuick(instruction_set_,
+                                                             instruction_set_features_.get(),
+                                                             compiler_options_.get(),
+                                                             oat_file.get()));
       elf_writers_.back()->Start();
       const bool do_dexlayout = DoDexLayoutOptimizations();
-      oat_writers_.emplace_back(new OatWriter(
+      oat_writers_.emplace_back(new linker::OatWriter(
           IsBootImage(), timings_, do_dexlayout ? profile_compilation_info_.get() : nullptr));
     }
   }
@@ -2679,7 +2681,7 @@
           return false;
         }
 
-        if (!ElfWriter::Fixup(oat_file.get(), oat_data_begins[i])) {
+        if (!linker::ElfWriter::Fixup(oat_file.get(), oat_data_begins[i])) {
           oat_file->Erase();
           LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath();
           return false;
@@ -2882,11 +2884,11 @@
   std::vector<const DexFile*> dex_files_;
   std::string no_inline_from_string_;
 
-  std::vector<std::unique_ptr<ElfWriter>> elf_writers_;
-  std::vector<std::unique_ptr<OatWriter>> oat_writers_;
-  std::vector<OutputStream*> rodata_;
-  std::vector<std::unique_ptr<OutputStream>> vdex_out_;
-  std::unique_ptr<ImageWriter> image_writer_;
+  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::unique_ptr<linker::ImageWriter> image_writer_;
   std::unique_ptr<CompilerDriver> driver_;
 
   std::vector<std::unique_ptr<MemMap>> opened_dex_files_maps_;
diff --git a/dex2oat/generate-operator-out.py b/dex2oat/generate-operator-out.py
new file mode 120000
index 0000000..cc291d2
--- /dev/null
+++ b/dex2oat/generate-operator-out.py
@@ -0,0 +1 @@
+../tools/generate-operator-out.py
\ No newline at end of file
diff --git a/compiler/elf_writer.cc b/dex2oat/linker/elf_writer.cc
similarity index 97%
rename from compiler/elf_writer.cc
rename to dex2oat/linker/elf_writer.cc
index 37e4f11..ca34864 100644
--- a/compiler/elf_writer.cc
+++ b/dex2oat/linker/elf_writer.cc
@@ -20,6 +20,7 @@
 #include "elf_file.h"
 
 namespace art {
+namespace linker {
 
 uintptr_t ElfWriter::GetOatDataAddress(ElfFile* elf_file) {
   uintptr_t oatdata_address = elf_file->FindSymbolAddress(SHT_DYNSYM,
@@ -59,4 +60,5 @@
   return elf_file->Fixup(base_address);
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/elf_writer.h b/dex2oat/linker/elf_writer.h
similarity index 93%
rename from compiler/elf_writer.h
rename to dex2oat/linker/elf_writer.h
index a8a5bc3..0eb36ed 100644
--- a/compiler/elf_writer.h
+++ b/dex2oat/linker/elf_writer.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_ELF_WRITER_H_
-#define ART_COMPILER_ELF_WRITER_H_
+#ifndef ART_DEX2OAT_LINKER_ELF_WRITER_H_
+#define ART_DEX2OAT_LINKER_ELF_WRITER_H_
 
 #include <stdint.h>
 #include <cstddef>
@@ -30,12 +30,15 @@
 namespace art {
 
 class ElfFile;
-class OutputStream;
 
 namespace debug {
 struct MethodDebugInfo;
 }  // namespace debug
 
+namespace linker {
+
+class OutputStream;
+
 class ElfWriter {
  public:
   // Looks up information about location of oat file in elf file container.
@@ -78,6 +81,7 @@
   ElfWriter() = default;
 };
 
+}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_ELF_WRITER_H_
+#endif  // ART_DEX2OAT_LINKER_ELF_WRITER_H_
diff --git a/compiler/elf_writer_quick.cc b/dex2oat/linker/elf_writer_quick.cc
similarity index 98%
rename from compiler/elf_writer_quick.cc
rename to dex2oat/linker/elf_writer_quick.cc
index 5d6dd2e..93f5a1d 100644
--- a/compiler/elf_writer_quick.cc
+++ b/dex2oat/linker/elf_writer_quick.cc
@@ -27,17 +27,18 @@
 #include "debug/method_debug_info.h"
 #include "driver/compiler_options.h"
 #include "elf.h"
-#include "elf_builder.h"
 #include "elf_utils.h"
 #include "globals.h"
 #include "leb128.h"
 #include "linker/buffered_output_stream.h"
+#include "linker/elf_builder.h"
 #include "linker/file_output_stream.h"
 #include "thread-current-inl.h"
 #include "thread_pool.h"
 #include "utils.h"
 
 namespace art {
+namespace linker {
 
 // .eh_frame and .debug_frame are almost identical.
 // Except for some minor formatting differences, the main difference
@@ -315,4 +316,5 @@
 template class ElfWriterQuick<ElfTypes32>;
 template class ElfWriterQuick<ElfTypes64>;
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/elf_writer_quick.h b/dex2oat/linker/elf_writer_quick.h
similarity index 85%
rename from compiler/elf_writer_quick.h
rename to dex2oat/linker/elf_writer_quick.h
index 3d5dd39..e20957c 100644
--- a/compiler/elf_writer_quick.h
+++ b/dex2oat/linker/elf_writer_quick.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_ELF_WRITER_QUICK_H_
-#define ART_COMPILER_ELF_WRITER_QUICK_H_
+#ifndef ART_DEX2OAT_LINKER_ELF_WRITER_QUICK_H_
+#define ART_DEX2OAT_LINKER_ELF_WRITER_QUICK_H_
 
 #include <memory>
 
@@ -28,11 +28,14 @@
 class CompilerOptions;
 class InstructionSetFeatures;
 
+namespace linker {
+
 std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
                                                 const InstructionSetFeatures* features,
                                                 const CompilerOptions* compiler_options,
                                                 File* elf_file);
 
+}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_ELF_WRITER_QUICK_H_
+#endif  // ART_DEX2OAT_LINKER_ELF_WRITER_QUICK_H_
diff --git a/compiler/elf_writer_test.cc b/dex2oat/linker/elf_writer_test.cc
similarity index 98%
rename from compiler/elf_writer_test.cc
rename to dex2oat/linker/elf_writer_test.cc
index 984e9ee..9f8ed77 100644
--- a/compiler/elf_writer_test.cc
+++ b/dex2oat/linker/elf_writer_test.cc
@@ -18,14 +18,15 @@
 
 #include "base/unix_file/fd_file.h"
 #include "common_compiler_test.h"
-#include "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"
 #include "utils.h"
 
 namespace art {
+namespace linker {
 
 class ElfWriterTest : public CommonCompilerTest {
  protected:
@@ -161,4 +162,5 @@
   }
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/image_test.cc b/dex2oat/linker/image_test.cc
similarity index 98%
rename from compiler/image_test.cc
rename to dex2oat/linker/image_test.cc
index 7b623dd..ab6e7a8 100644
--- a/compiler/image_test.cc
+++ b/dex2oat/linker/image_test.cc
@@ -24,6 +24,7 @@
 #include "thread.h"
 
 namespace art {
+namespace linker {
 
 TEST_F(ImageTest, TestImageLayout) {
   std::vector<size_t> image_sizes;
@@ -153,4 +154,5 @@
   ASSERT_TRUE(class_linker_->IsQuickToInterpreterBridge(code));
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/image_test.h b/dex2oat/linker/image_test.h
similarity index 98%
rename from compiler/image_test.h
rename to dex2oat/linker/image_test.h
index f1adedd..71f1fa6 100644
--- a/compiler/image_test.h
+++ b/dex2oat/linker/image_test.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_IMAGE_TEST_H_
-#define ART_COMPILER_IMAGE_TEST_H_
+#ifndef ART_DEX2OAT_LINKER_IMAGE_TEST_H_
+#define ART_DEX2OAT_LINKER_IMAGE_TEST_H_
 
 #include "image.h"
 
@@ -33,11 +33,11 @@
 #include "debug/method_debug_info.h"
 #include "dex/quick_compiler_callbacks.h"
 #include "driver/compiler_options.h"
-#include "elf_writer.h"
-#include "elf_writer_quick.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"
@@ -48,6 +48,7 @@
 #include "utils.h"
 
 namespace art {
+namespace linker {
 
 static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
 
@@ -494,7 +495,7 @@
   }
 }
 
-
+}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_IMAGE_TEST_H_
+#endif  // ART_DEX2OAT_LINKER_IMAGE_TEST_H_
diff --git a/compiler/image_write_read_test.cc b/dex2oat/linker/image_write_read_test.cc
similarity index 95%
rename from compiler/image_write_read_test.cc
rename to dex2oat/linker/image_write_read_test.cc
index 32c0b06..30996b5 100644
--- a/compiler/image_write_read_test.cc
+++ b/dex2oat/linker/image_write_read_test.cc
@@ -17,6 +17,7 @@
 #include "image_test.h"
 
 namespace art {
+namespace linker {
 
 TEST_F(ImageTest, WriteReadUncompressed) {
   TestWriteRead(ImageHeader::kStorageModeUncompressed);
@@ -30,4 +31,5 @@
   TestWriteRead(ImageHeader::kStorageModeLZ4HC);
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/image_writer.cc b/dex2oat/linker/image_writer.cc
similarity index 99%
rename from compiler/image_writer.cc
rename to dex2oat/linker/image_writer.cc
index 4ffe238..ee568e8 100644
--- a/compiler/image_writer.cc
+++ b/dex2oat/linker/image_writer.cc
@@ -38,7 +38,6 @@
 #include "driver/compiler_driver.h"
 #include "elf_file.h"
 #include "elf_utils.h"
-#include "elf_writer.h"
 #include "gc/accounting/card_table-inl.h"
 #include "gc/accounting/heap_bitmap.h"
 #include "gc/accounting/space_bitmap-inl.h"
@@ -82,6 +81,7 @@
 using ::art::mirror::String;
 
 namespace art {
+namespace linker {
 
 // Separate objects into multiple bins to optimize dirty memory use.
 static constexpr bool kBinObjects = true;
@@ -2835,5 +2835,5 @@
   }
 }
 
-
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/image_writer.h b/dex2oat/linker/image_writer.h
similarity index 98%
rename from compiler/image_writer.h
rename to dex2oat/linker/image_writer.h
index 2fc394e..bdea000 100644
--- a/compiler/image_writer.h
+++ b/dex2oat/linker/image_writer.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_IMAGE_WRITER_H_
-#define ART_COMPILER_IMAGE_WRITER_H_
+#ifndef ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
+#define ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
 
 #include <stdint.h>
 #include "base/memory_tool.h"
@@ -66,6 +66,8 @@
 
 static constexpr int kInvalidFd = -1;
 
+namespace linker {
+
 // Write a Space built during compilation for use during execution.
 class ImageWriter FINAL {
  public:
@@ -620,6 +622,7 @@
   DISALLOW_COPY_AND_ASSIGN(ImageWriter);
 };
 
+}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_IMAGE_WRITER_H_
+#endif  // ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
diff --git a/compiler/linker/multi_oat_relative_patcher.cc b/dex2oat/linker/multi_oat_relative_patcher.cc
similarity index 95%
rename from compiler/linker/multi_oat_relative_patcher.cc
rename to dex2oat/linker/multi_oat_relative_patcher.cc
index 4ae75d6..178a78f 100644
--- a/compiler/linker/multi_oat_relative_patcher.cc
+++ b/dex2oat/linker/multi_oat_relative_patcher.cc
@@ -26,8 +26,7 @@
 MultiOatRelativePatcher::MultiOatRelativePatcher(InstructionSet instruction_set,
                                                  const InstructionSetFeatures* features)
     : method_offset_map_(),
-      relative_patcher_(
-          linker::RelativePatcher::Create(instruction_set, features, &method_offset_map_)),
+      relative_patcher_(RelativePatcher::Create(instruction_set, features, &method_offset_map_)),
       adjustment_(0u),
       instruction_set_(instruction_set),
       start_size_code_alignment_(0u),
diff --git a/compiler/linker/multi_oat_relative_patcher.h b/dex2oat/linker/multi_oat_relative_patcher.h
similarity index 93%
rename from compiler/linker/multi_oat_relative_patcher.h
rename to dex2oat/linker/multi_oat_relative_patcher.h
index 02cd4b0..6683366 100644
--- a/compiler/linker/multi_oat_relative_patcher.h
+++ b/dex2oat/linker/multi_oat_relative_patcher.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_
-#define ART_COMPILER_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_
+#ifndef ART_DEX2OAT_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_
+#define ART_DEX2OAT_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_
 
 #include "arch/instruction_set.h"
 #include "debug/method_debug_info.h"
 #include "method_reference.h"
-#include "relative_patcher.h"
+#include "linker/relative_patcher.h"
 #include "safe_map.h"
 
 namespace art {
@@ -131,8 +131,8 @@
 
  private:
   // Map method reference to assigned offset.
-  // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
-  class MethodOffsetMap : public linker::RelativePatcherTargetProvider {
+  // Wrap the map in a class implementing RelativePatcherTargetProvider.
+  class MethodOffsetMap : public RelativePatcherTargetProvider {
    public:
     std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE;
     SafeMap<MethodReference, uint32_t> map;
@@ -155,4 +155,4 @@
 }  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_
+#endif  // ART_DEX2OAT_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_
diff --git a/compiler/linker/multi_oat_relative_patcher_test.cc b/dex2oat/linker/multi_oat_relative_patcher_test.cc
similarity index 99%
rename from compiler/linker/multi_oat_relative_patcher_test.cc
rename to dex2oat/linker/multi_oat_relative_patcher_test.cc
index 5c359dc..1b2d43e 100644
--- a/compiler/linker/multi_oat_relative_patcher_test.cc
+++ b/dex2oat/linker/multi_oat_relative_patcher_test.cc
@@ -19,7 +19,7 @@
 #include "compiled_method.h"
 #include "debug/method_debug_info.h"
 #include "gtest/gtest.h"
-#include "vector_output_stream.h"
+#include "linker/vector_output_stream.h"
 
 namespace art {
 namespace linker {
diff --git a/compiler/oat_writer.cc b/dex2oat/linker/oat_writer.cc
similarity index 99%
rename from compiler/oat_writer.cc
rename to dex2oat/linker/oat_writer.cc
index ce1b755..51c2a03 100644
--- a/compiler/oat_writer.cc
+++ b/dex2oat/linker/oat_writer.cc
@@ -61,6 +61,7 @@
 #include "zip_archive.h"
 
 namespace art {
+namespace linker {
 
 namespace {  // anonymous namespace
 
@@ -600,7 +601,7 @@
   return true;
 }
 
-void OatWriter::PrepareLayout(linker::MultiOatRelativePatcher* relative_patcher) {
+void OatWriter::PrepareLayout(MultiOatRelativePatcher* relative_patcher) {
   CHECK(write_state_ == WriteState::kPrepareLayout);
 
   relative_patcher_ = relative_patcher;
@@ -3629,4 +3630,5 @@
   UNREACHABLE();
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/compiler/oat_writer.h b/dex2oat/linker/oat_writer.h
similarity index 97%
rename from compiler/oat_writer.h
rename to dex2oat/linker/oat_writer.h
index ef0ce52..a93dd23 100644
--- a/compiler/oat_writer.h
+++ b/dex2oat/linker/oat_writer.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_OAT_WRITER_H_
-#define ART_COMPILER_OAT_WRITER_H_
+#ifndef ART_DEX2OAT_LINKER_OAT_WRITER_H_
+#define ART_DEX2OAT_LINKER_OAT_WRITER_H_
 
 #include <stdint.h>
 #include <cstddef>
@@ -23,7 +23,7 @@
 
 #include "base/array_ref.h"
 #include "base/dchecked_vector.h"
-#include "linker/relative_patcher.h"  // For linker::RelativePatcherTargetProvider.
+#include "linker/relative_patcher.h"  // For RelativePatcherTargetProvider.
 #include "mem_map.h"
 #include "method_reference.h"
 #include "mirror/class.h"
@@ -38,9 +38,7 @@
 class BitVector;
 class CompiledMethod;
 class CompilerDriver;
-class ImageWriter;
 class ProfileCompilationInfo;
-class OutputStream;
 class TimingLogger;
 class TypeLookupTable;
 class VdexFile;
@@ -50,14 +48,16 @@
 struct MethodDebugInfo;
 }  // namespace debug
 
-namespace linker {
-class MultiOatRelativePatcher;
-}  // namespace linker
-
 namespace verifier {
-  class VerifierDeps;
+class VerifierDeps;
 }  // namespace verifier
 
+namespace linker {
+
+class ImageWriter;
+class MultiOatRelativePatcher;
+class OutputStream;
+
 // OatHeader         variable length with count of D OatDexFiles
 //
 // TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
@@ -183,7 +183,7 @@
   }
 
   // Prepare layout of remaining data.
-  void PrepareLayout(linker::MultiOatRelativePatcher* relative_patcher);
+  void PrepareLayout(MultiOatRelativePatcher* relative_patcher);
   // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
   bool WriteRodata(OutputStream* out);
   // Write the code to the .text section.
@@ -478,7 +478,7 @@
   uint32_t size_method_bss_mappings_;
 
   // The helper for processing relative patches is external so that we can patch across oat files.
-  linker::MultiOatRelativePatcher* relative_patcher_;
+  MultiOatRelativePatcher* relative_patcher_;
 
   // The locations of absolute patches relative to the start of the executable section.
   dchecked_vector<uintptr_t> absolute_patch_locations_;
@@ -489,6 +489,7 @@
   DISALLOW_COPY_AND_ASSIGN(OatWriter);
 };
 
+}  // namespace linker
 }  // namespace art
 
-#endif  // ART_COMPILER_OAT_WRITER_H_
+#endif  // ART_DEX2OAT_LINKER_OAT_WRITER_H_
diff --git a/compiler/oat_test.cc b/dex2oat/linker/oat_writer_test.cc
similarity index 98%
rename from compiler/oat_test.cc
rename to dex2oat/linker/oat_writer_test.cc
index 6f89049..0600ceb 100644
--- a/compiler/oat_test.cc
+++ b/dex2oat/linker/oat_writer_test.cc
@@ -30,10 +30,10 @@
 #include "dex/verification_results.h"
 #include "driver/compiler_driver.h"
 #include "driver/compiler_options.h"
-#include "elf_writer.h"
-#include "elf_writer_quick.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"
@@ -46,6 +46,7 @@
 #include "utils/test_dex_file_builder.h"
 
 namespace art {
+namespace linker {
 
 NO_RETURN static void Usage(const char* fmt, ...) {
   va_list ap;
@@ -212,8 +213,8 @@
       ScopedObjectAccess soa(Thread::Current());
       class_linker->RegisterDexFile(*dex_file, nullptr);
     }
-    linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
-                                            instruction_set_features_.get());
+    MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(),
+                                    instruction_set_features_.get());
     oat_writer.Initialize(compiler_driver_.get(), nullptr, dex_files);
     oat_writer.PrepareLayout(&patcher);
     size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset();
@@ -867,4 +868,5 @@
   EXPECT_EQ(216138397U, oat_header->GetChecksum());
 }
 
+}  // namespace linker
 }  // namespace art
diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc
index 7b11258..36bd4bc 100644
--- a/oatdump/oatdump.cc
+++ b/oatdump/oatdump.cc
@@ -41,7 +41,6 @@
 #include "dex_file-inl.h"
 #include "dex_instruction-inl.h"
 #include "disassembler.h"
-#include "elf_builder.h"
 #include "gc/accounting/space_bitmap-inl.h"
 #include "gc/space/image_space.h"
 #include "gc/space/large_object_space.h"
@@ -51,6 +50,7 @@
 #include "indenter.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"
@@ -133,9 +133,10 @@
     if (elf_file == nullptr) {
       return false;
     }
-    std::unique_ptr<BufferedOutputStream> output_stream =
-        std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file.get()));
-    builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
+    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()));
 
     builder_->Start();
 
@@ -326,7 +327,7 @@
 
  private:
   const OatFile* oat_file_;
-  std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
+  std::unique_ptr<linker::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/class_linker.h b/runtime/class_linker.h
index 2f92da3..f97433c 100644
--- a/runtime/class_linker.h
+++ b/runtime/class_linker.h
@@ -43,6 +43,13 @@
   class ImageSpace;
 }  // namespace space
 }  // namespace gc
+
+namespace linker {
+  struct CompilationHelper;
+  class ImageWriter;
+  class OatWriter;
+}  // namespace linker
+
 namespace mirror {
   class ClassLoader;
   class DexCache;
@@ -1276,12 +1283,12 @@
   class FindVirtualMethodHolderVisitor;
 
   friend class AppImageClassLoadersAndDexCachesHelper;
-  friend struct CompilationHelper;  // For Compile in ImageTest.
   friend class ImageDumper;  // for DexLock
-  friend class ImageWriter;  // for GetClassRoots
+  friend struct linker::CompilationHelper;  // For Compile in ImageTest.
+  friend class linker::ImageWriter;  // for GetClassRoots
+  friend class linker::OatWriter;  // for boot image string/class table slot address lookup.
   friend class JniCompilerTest;  // for GetRuntimeQuickGenericJniStub
   friend class JniInternalTest;  // for GetRuntimeQuickGenericJniStub
-  friend class OatWriter;  // for boot image string/class table slot address lookup.
   friend class VMClassLoader;  // for LookupClass and FindClassInBaseDexClassLoader.
   ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName);  // for DexLock, and RegisterDexFileLocked
   ART_FRIEND_TEST(mirror::DexCacheMethodHandlesTest, Open);  // for AllocDexCache
diff --git a/runtime/class_table.h b/runtime/class_table.h
index 0ffe93e..744c010 100644
--- a/runtime/class_table.h
+++ b/runtime/class_table.h
@@ -32,6 +32,14 @@
 
 class OatFile;
 
+namespace linker {
+class ImageWriter;
+}  // namespace linker
+
+namespace linker {
+class OatWriter;
+}  // namespace linker
+
 namespace mirror {
   class Class;
   class ClassLoader;
@@ -286,8 +294,8 @@
   // Keep track of oat files with GC roots associated with dex caches in `strong_roots_`.
   std::vector<const OatFile*> oat_files_ GUARDED_BY(lock_);
 
-  friend class ImageWriter;  // for InsertWithoutLocks.
-  friend class OatWriter;  // for boot class TableSlot address lookup.
+  friend class linker::ImageWriter;  // for InsertWithoutLocks.
+  friend class linker::OatWriter;  // for boot class TableSlot address lookup.
 };
 
 }  // namespace art
diff --git a/runtime/image.h b/runtime/image.h
index da04333..3844186 100644
--- a/runtime/image.h
+++ b/runtime/image.h
@@ -29,6 +29,10 @@
 class ArtField;
 class ArtMethod;
 
+namespace linker {
+class ImageWriter;
+}  // namespace linker
+
 class ObjectVisitor {
  public:
   virtual ~ObjectVisitor() {}
@@ -442,7 +446,7 @@
   // is the compressed size in the file.
   uint32_t data_size_;
 
-  friend class ImageWriter;
+  friend class linker::ImageWriter;
 };
 
 std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
diff --git a/runtime/intern_table.h b/runtime/intern_table.h
index 3e3abbd..05f2794 100644
--- a/runtime/intern_table.h
+++ b/runtime/intern_table.h
@@ -38,6 +38,10 @@
 
 enum VisitRootFlags : uint8_t;
 
+namespace linker {
+class OatWriter;
+}  // namespace linker
+
 namespace mirror {
 class String;
 }  // namespace mirror
@@ -223,7 +227,7 @@
     // modifying the zygote intern table. The back of table is modified when strings are interned.
     std::vector<UnorderedSet> tables_;
 
-    friend class OatWriter;  // for boot image string table slot address lookup.
+    friend class linker::OatWriter;  // for boot image string table slot address lookup.
     ART_FRIEND_TEST(InternTableTest, CrossHash);
   };
 
@@ -283,7 +287,7 @@
   // Weak root state, used for concurrent system weak processing and more.
   gc::WeakRootState weak_root_state_ GUARDED_BY(Locks::intern_table_lock_);
 
-  friend class OatWriter;  // for boot image string table slot address lookup.
+  friend class linker::OatWriter;  // for boot image string table slot address lookup.
   friend class Transaction;
   ART_FRIEND_TEST(InternTableTest, CrossHash);
   DISALLOW_COPY_AND_ASSIGN(InternTable);