Remove ExtractCodeAndPrelink and switch Portable to MCLinker

Change-Id: Ia2459c7da6b79e0a1c0f1148c6e28ad9cbbe27a2
diff --git a/src/compiler/driver/compiler_driver.cc b/src/compiler/driver/compiler_driver.cc
index a28ba18..8856a00 100644
--- a/src/compiler/driver/compiler_driver.cc
+++ b/src/compiler/driver/compiler_driver.cc
@@ -1678,7 +1678,7 @@
   const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
   bool is_static = (access_flags & kAccStatic) != 0;
   std::string key(MakeInvokeStubKey(is_static, shorty));
-  const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(key);
+  CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(key);
   if (compiled_invoke_stub == NULL) {
     compiled_invoke_stub = (*create_invoke_stub_)(*this, is_static, shorty, shorty_len);
     CHECK(compiled_invoke_stub != NULL);
@@ -1686,7 +1686,7 @@
   }
 
   if ((compiler_backend_ == kPortable) && !is_static) {
-    const CompiledInvokeStub* compiled_proxy_stub = FindProxyStub(shorty);
+    CompiledInvokeStub* compiled_proxy_stub = FindProxyStub(shorty);
     if (compiled_proxy_stub == NULL) {
       compiled_proxy_stub = (*create_proxy_stub_)(*this, shorty, shorty_len);
       CHECK(compiled_proxy_stub != NULL);
@@ -1701,12 +1701,12 @@
   }
 }
 
-const CompiledInvokeStub* CompilerDriver::FindInvokeStub(bool is_static, const char* shorty) const {
+CompiledInvokeStub* CompilerDriver::FindInvokeStub(bool is_static, const char* shorty) const {
   const std::string key(MakeInvokeStubKey(is_static, shorty));
   return FindInvokeStub(key);
 }
 
-const CompiledInvokeStub* CompilerDriver::FindInvokeStub(const std::string& key) const {
+CompiledInvokeStub* CompilerDriver::FindInvokeStub(const std::string& key) const {
   MutexLock mu(Thread::Current(), compiled_invoke_stubs_lock_);
   InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
   if (it == compiled_invoke_stubs_.end()) {
@@ -1717,8 +1717,7 @@
   }
 }
 
-void CompilerDriver::InsertInvokeStub(const std::string& key,
-                                      const CompiledInvokeStub* compiled_invoke_stub) {
+void CompilerDriver::InsertInvokeStub(const std::string& key, CompiledInvokeStub* compiled_invoke_stub) {
   MutexLock mu(Thread::Current(), compiled_invoke_stubs_lock_);
   InvokeStubTable::iterator it = compiled_invoke_stubs_.find(key);
   if (it != compiled_invoke_stubs_.end()) {
@@ -1729,7 +1728,7 @@
   }
 }
 
-const CompiledInvokeStub* CompilerDriver::FindProxyStub(const char* shorty) const {
+CompiledInvokeStub* CompilerDriver::FindProxyStub(const char* shorty) const {
   MutexLock mu(Thread::Current(), compiled_proxy_stubs_lock_);
   ProxyStubTable::const_iterator it = compiled_proxy_stubs_.find(shorty);
   if (it == compiled_proxy_stubs_.end()) {
@@ -1740,8 +1739,7 @@
   }
 }
 
-void CompilerDriver::InsertProxyStub(const char* shorty,
-                                     const CompiledInvokeStub* compiled_proxy_stub) {
+void CompilerDriver::InsertProxyStub(const char* shorty, CompiledInvokeStub* compiled_proxy_stub) {
   MutexLock mu(Thread::Current(), compiled_proxy_stubs_lock_);
   InvokeStubTable::iterator it = compiled_proxy_stubs_.find(shorty);
   if (it != compiled_proxy_stubs_.end()) {
@@ -1795,11 +1793,21 @@
   return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0;
 }
 
-bool CompilerDriver::WriteElf(std::vector<uint8_t>& oat_contents, File* file) {
-  typedef bool (*WriteElfFn)(CompilerDriver&, std::vector<uint8_t>&, File*);
+bool CompilerDriver::WriteElf(const std::string* host_prefix,
+                              bool is_host,
+                              const std::vector<const DexFile*>& dex_files,
+                              std::vector<uint8_t>& oat_contents,
+                              File* file) {
+  typedef bool (*WriteElfFn)(CompilerDriver&,
+                             const std::string* host_prefix,
+                             bool is_host,
+                             const std::vector<const DexFile*>& dex_files,
+                             std::vector<uint8_t>&,
+                             File*);
   WriteElfFn WriteElf =
     FindFunction<WriteElfFn>(MakeCompilerSoName(compiler_backend_), compiler_library_, "WriteElf");
-  return WriteElf(*this, oat_contents, file);
+  Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
+  return WriteElf(*this, host_prefix, is_host, dex_files, oat_contents, file);
 }
 
 bool CompilerDriver::FixupElf(File* file, uintptr_t oat_data_begin) const {
@@ -1819,11 +1827,18 @@
   GetOatElfInformation(file, oat_loaded_size, oat_data_offset);
 }
 
+bool CompilerDriver::StripElf(File* file) const {
+  typedef bool (*StripElfFn)(File*);
+  StripElfFn StripElf =
+    FindFunction<StripElfFn>(MakeCompilerSoName(compiler_backend_), compiler_library_, "StripElf");
+  return StripElf(file);
+}
+
 void CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set,
                                                 std::string& target_triple,
                                                 std::string& target_cpu,
                                                 std::string& target_attr) {
-    switch (instruction_set) {
+  switch (instruction_set) {
     case kThumb2:
       target_triple = "thumb-none-linux-gnueabi";
       target_cpu = "cortex-a9";
diff --git a/src/compiler/driver/compiler_driver.h b/src/compiler/driver/compiler_driver.h
index 49bc473..7f67c21 100644
--- a/src/compiler/driver/compiler_driver.h
+++ b/src/compiler/driver/compiler_driver.h
@@ -123,11 +123,11 @@
   CompiledMethod* GetCompiledMethod(MethodReference ref) const
       LOCKS_EXCLUDED(compiled_methods_lock_);
 
-  const CompiledInvokeStub* FindInvokeStub(bool is_static, const char* shorty) const;
-  const CompiledInvokeStub* FindInvokeStub(const std::string& key) const
+  CompiledInvokeStub* FindInvokeStub(bool is_static, const char* shorty) const;
+  CompiledInvokeStub* FindInvokeStub(const std::string& key) const
       LOCKS_EXCLUDED(compiled_invoke_stubs_lock_);
 
-  const CompiledInvokeStub* FindProxyStub(const char* shorty) const;
+  CompiledInvokeStub* FindProxyStub(const char* shorty) const;
 
   void AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file, size_t class_def_index);
   bool RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, size_t class_def_index);
@@ -186,10 +186,15 @@
 
   void SetBitcodeFileName(std::string const& filename);
 
-  // TODO: remove when libart links against LLVM (when separate compiler library is gone)
-  bool WriteElf(std::vector<uint8_t>& oat_contents, File* file);
+  // TODO: remove these Elf wrappers when libart links against LLVM (when separate compiler library is gone)
+  bool WriteElf(const std::string* host_prefix,
+                bool is_host,
+                const std::vector<const DexFile*>& dex_files,
+                std::vector<uint8_t>& oat_contents,
+                File* file);
   bool FixupElf(File* file, uintptr_t oat_data_begin) const;
   void GetOatElfInformation(File* file, size_t& oat_loaded_size, size_t& oat_data_offset) const;
+  bool StripElf(File* file) const;
 
   // TODO: move to a common home for llvm helpers once quick/portable are merged
   static void InstructionSetToLLVMTarget(InstructionSet instruction_set,
@@ -316,10 +321,10 @@
   static void CompileClass(const ParallelCompilationManager* context, size_t class_def_index)
       LOCKS_EXCLUDED(Locks::mutator_lock_);
 
-  void InsertInvokeStub(const std::string& key, const CompiledInvokeStub* compiled_invoke_stub)
+  void InsertInvokeStub(const std::string& key, CompiledInvokeStub* compiled_invoke_stub)
       LOCKS_EXCLUDED(compiled_invoke_stubs_lock_);
 
-  void InsertProxyStub(const char* shorty, const CompiledInvokeStub* compiled_proxy_stub);
+  void InsertProxyStub(const char* shorty, CompiledInvokeStub* compiled_proxy_stub);
 
   std::vector<const PatchInformation*> code_to_patch_;
   std::vector<const PatchInformation*> methods_to_patch_;
@@ -342,12 +347,12 @@
   mutable Mutex compiled_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
   MethodTable compiled_methods_ GUARDED_BY(compiled_methods_lock_);
 
-  typedef SafeMap<std::string, const CompiledInvokeStub*> InvokeStubTable;
+  typedef SafeMap<std::string, CompiledInvokeStub*> InvokeStubTable;
   // Invocation stubs created to allow invocation of the compiled methods.
   mutable Mutex compiled_invoke_stubs_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
   InvokeStubTable compiled_invoke_stubs_ GUARDED_BY(compiled_invoke_stubs_lock_);
 
-  typedef SafeMap<std::string, const CompiledInvokeStub*> ProxyStubTable;
+  typedef SafeMap<std::string, CompiledInvokeStub*> ProxyStubTable;
   // Proxy stubs created for proxy invocation delegation
   mutable Mutex compiled_proxy_stubs_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
   ProxyStubTable compiled_proxy_stubs_ GUARDED_BY(compiled_proxy_stubs_lock_);
diff --git a/src/compiler/driver/compiler_driver_test.cc b/src/compiler/driver/compiler_driver_test.cc
index 19ccb35..dee448d 100644
--- a/src/compiler/driver/compiler_driver_test.cc
+++ b/src/compiler/driver/compiler_driver_test.cc
@@ -132,6 +132,7 @@
 }
 
 TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
+  TEST_DISABLED_FOR_PORTABLE();
   jobject class_loader;
   {
     ScopedObjectAccess soa(Thread::Current());
diff --git a/src/compiler/driver/dex_compilation_unit.cc b/src/compiler/driver/dex_compilation_unit.cc
new file mode 100644
index 0000000..67987fa
--- /dev/null
+++ b/src/compiler/driver/dex_compilation_unit.cc
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#include "dex_compilation_unit.h"
+
+#include "base/stringprintf.h"
+#include "compiler/dex/compiler_ir.h"
+#include "utils.h"
+
+namespace art {
+
+DexCompilationUnit::DexCompilationUnit(CompilationUnit* cu)
+    : cu_(cu),
+      class_loader_(cu->class_loader),
+      class_linker_(cu->class_linker),
+      dex_file_(cu->dex_file),
+      code_item_(cu->code_item),
+      class_def_idx_(cu->class_def_idx),
+      dex_method_idx_(cu->method_idx),
+      access_flags_(cu->access_flags),
+      symbol_(StringPrintf("dex_%s", MangleForJni(PrettyMethod(dex_method_idx_, *dex_file_)).c_str())) {
+}
+
+DexCompilationUnit:: DexCompilationUnit(CompilationUnit* cu,
+                                        jobject class_loader,
+                                        ClassLinker* class_linker,
+                                        const DexFile& dex_file,
+                                        const DexFile::CodeItem* code_item,
+                                        uint32_t class_def_idx,
+                                        uint32_t method_idx,
+                                        uint32_t access_flags)
+    : cu_(cu),
+      class_loader_(class_loader),
+      class_linker_(class_linker),
+      dex_file_(&dex_file),
+      code_item_(code_item),
+      class_def_idx_(class_def_idx),
+      dex_method_idx_(method_idx),
+      access_flags_(access_flags),
+      symbol_(StringPrintf("dex_%s", MangleForJni(PrettyMethod(dex_method_idx_, *dex_file_)).c_str())) {
+}
+
+} // namespace art
diff --git a/src/compiler/driver/dex_compilation_unit.h b/src/compiler/driver/dex_compilation_unit.h
index 6a0218d..0fc1123 100644
--- a/src/compiler/driver/dex_compilation_unit.h
+++ b/src/compiler/driver/dex_compilation_unit.h
@@ -17,26 +17,29 @@
 #ifndef ART_SRC_COMPILER_DEX_DEX_COMPILATION_UNIT_H_
 #define ART_SRC_COMPILER_DEX_DEX_COMPILATION_UNIT_H_
 
-#include "dex_file.h"
-
 #include <stdint.h>
 
+#include "dex_file.h"
+#include "jni.h"
+
 namespace art {
 namespace mirror {
 class ClassLoader;
 class DexCache;
 }  // namespace mirror
 class ClassLinker;
-class DexFile;
+class CompilationUnit;
 
 class DexCompilationUnit {
  public:
-  DexCompilationUnit(jobject class_loader, ClassLinker* class_linker, const DexFile& dex_file,
-                     const DexFile::CodeItem* code_item, uint32_t class_def_idx,
-                     uint32_t method_idx, uint32_t access_flags)
-      : class_loader_(class_loader), class_linker_(class_linker), dex_file_(&dex_file),
-        code_item_(code_item), class_def_idx_(class_def_idx), dex_method_idx_(method_idx),
-        access_flags_(access_flags) {
+  DexCompilationUnit(CompilationUnit* cu);
+
+  DexCompilationUnit(CompilationUnit* cu, jobject class_loader, ClassLinker* class_linker,
+                     const DexFile& dex_file, const DexFile::CodeItem* code_item,
+                     uint32_t class_def_idx, uint32_t method_idx, uint32_t access_flags);
+
+  CompilationUnit* GetCompilationUnit() const {
+    return cu_;
   }
 
   jobject GetClassLoader() const {
@@ -89,8 +92,15 @@
     return ((access_flags_ & kAccSynchronized) != 0);
   }
 
+  const std::string& GetSymbol() const {
+    return symbol_;
+  }
+
  private:
+  CompilationUnit* cu_;
+
   const jobject class_loader_;
+
   ClassLinker* const class_linker_;
 
   const DexFile* const dex_file_;
@@ -99,6 +109,8 @@
   const uint32_t class_def_idx_;
   const uint32_t dex_method_idx_;
   const uint32_t access_flags_;
+
+  const std::string symbol_;
 };
 
 } // namespace art