am 1d81bd90: Reconcile with jb-release

* commit '1d81bd907f3fe7c642ed45f18049236305ed1e3f':
diff --git a/Android.mk b/Android.mk
index 3285f93..6cf4bc7 100644
--- a/Android.mk
+++ b/Android.mk
@@ -15,45 +15,64 @@
 #
 
 LOCAL_PATH := $(call my-dir)
-include $(LOCAL_PATH)/libbcc-config.mk
-
+LIBBCC_ROOT_PATH := $(LOCAL_PATH)
+include $(LIBBCC_ROOT_PATH)/libbcc.mk
 
 #=====================================================================
 # Whole Static Library to Be Linked In
 #=====================================================================
 
-ifeq ($(libbcc_USE_DISASSEMBLER),1)
-libbcc_WHOLE_STATIC_LIBRARIES += libbccDisassembler
-endif
-
 libbcc_WHOLE_STATIC_LIBRARIES += \
+  libbccAndroidBitcode \
+  libbccRenderScript \
   libbccExecutionEngine \
-  libbccHelper \
-  libbccTransforms
+  libbccCore \
+  libbccSupport
 
+libmcld_STATIC_LIBRARIES += \
+  libmcldCodeGen \
+  libmcldTarget \
+  libmcldLDVariant \
+  libmcldMC \
+  libmcldSupport \
+  libmcldADT \
+  libmcldLD
 
 #=====================================================================
-# Calculate SHA1 checksum for libbcc.so and libRS.so
+# Calculate SHA1 checksum for libbcc.so, libRS.so and libclcore.bc
 #=====================================================================
 
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := libbcc.so.sha1
+LOCAL_MODULE := libbcc.sha1
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
 
-include $(BUILD_SYSTEM)/base_rules.mk
 libbcc_SHA1_SRCS := \
   $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/libbcc.so \
-  $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/libRS.so
+  $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/libRS.so \
+  $(call intermediates-dir-for,SHARED_LIBRARIES,libclcore.bc,,)/libclcore.bc
+
+ifeq ($(ARCH_ARM_HAVE_NEON),true)
+libbcc_SHA1_SRCS += \
+  $(call intermediates-dir-for,SHARED_LIBRARIES,libclcore_neon.bc,,)/libclcore_neon.bc
+endif
 
 libbcc_GEN_SHA1_STAMP := $(LOCAL_PATH)/tools/build/gen-sha1-stamp.py
+intermediates := $(call local-intermediates-dir)
 
-$(LOCAL_BUILT_MODULE): PRIVATE_SHA1_SRCS := $(libbcc_SHA1_SRCS)
-$(LOCAL_BUILT_MODULE): $(libbcc_SHA1_SRCS) $(libbcc_GEN_SHA1_STAMP)
-	$(hide) mkdir -p $(dir $@) && \
-	        $(libbcc_GEN_SHA1_STAMP) $@ $(PRIVATE_SHA1_SRCS)
+libbcc_SHA1_ASM := $(intermediates)/libbcc.sha1.S
+LOCAL_GENERATED_SOURCES += $(libbcc_SHA1_ASM)
+$(libbcc_SHA1_ASM): PRIVATE_SHA1_SRCS := $(libbcc_SHA1_SRCS)
+$(libbcc_SHA1_ASM): $(libbcc_SHA1_SRCS) $(libbcc_GEN_SHA1_STAMP)
+	@echo libbcc.sha1: $@
+	$(hide) mkdir -p $(dir $@)
+	$(hide) $(libbcc_GEN_SHA1_STAMP) $(PRIVATE_SHA1_SRCS) > $@
 
+LOCAL_CFLAGS += -D_REENTRANT -DPIC -fPIC
+LOCAL_CFLAGS += -O3 -nodefaultlibs -nostdlib
+
+include $(BUILD_SHARED_LIBRARY)
 
 #=====================================================================
 # Device Shared Library libbcc
@@ -65,30 +84,25 @@
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
 
-LOCAL_CFLAGS := $(libbcc_CFLAGS)
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-
-LOCAL_SRC_FILES := lib/ExecutionEngine/bcc.cpp
-
 LOCAL_WHOLE_STATIC_LIBRARIES := $(libbcc_WHOLE_STATIC_LIBRARIES)
 
 ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),arm x86))
 LOCAL_WHOLE_STATIC_LIBRARIES += libbccCompilerRT
 endif
 
-LOCAL_STATIC_LIBRARIES += librsloader
+LOCAL_WHOLE_STATIC_LIBRARIES += librsloader
 
 ifeq ($(libbcc_USE_DISASSEMBLER),1)
   ifeq ($(TARGET_ARCH),arm)
-    LOCAL_STATIC_LIBRARIES += \
-      libLLVMARMDisassembler \
-      libLLVMARMAsmPrinter
+    LOCAL_WHOLE_STATIC_LIBRARIES += \
+      libLLVMARMDisassembler
   else
     ifeq ($(TARGET_ARCH),mips)
-	  $(error "Disassembler is not available for MIPS architecture")
+      LOCAL_WHOLE_STATIC_LIBRARIES += \
+        libLLVMMipsDisassembler
     else
       ifeq ($(TARGET_ARCH),x86)
-        LOCAL_STATIC_LIBRARIES += \
+        LOCAL_WHOLE_STATIC_LIBRARIES += \
           libLLVMX86Disassembler
       else
         $(error Unsupported TARGET_ARCH $(TARGET_ARCH))
@@ -98,20 +112,32 @@
 endif
 
 ifeq ($(TARGET_ARCH),arm)
-  LOCAL_STATIC_LIBRARIES += \
+  LOCAL_WHOLE_STATIC_LIBRARIES += \
+    libmcldARMTarget \
+    libmcldARMInfo \
+    $(libmcld_STATIC_LIBRARIES) \
+    libLLVMARMAsmParser \
     libLLVMARMCodeGen \
     libLLVMARMDesc \
     libLLVMARMInfo
 else
   ifeq ($(TARGET_ARCH), mips)
-    LOCAL_STATIC_LIBRARIES += \
+    LOCAL_WHOLE_STATIC_LIBRARIES += \
+      libmcldMipsTarget \
+      libmcldMipsInfo \
+      $(libmcld_STATIC_LIBRARIES) \
+      libLLVMMipsAsmParser \
       libLLVMMipsCodeGen \
       libLLVMMipsAsmPrinter \
       libLLVMMipsDesc \
       libLLVMMipsInfo
   else
     ifeq ($(TARGET_ARCH),x86) # We don't support x86-64 right now
-      LOCAL_STATIC_LIBRARIES += \
+      LOCAL_WHOLE_STATIC_LIBRARIES += \
+        libmcldX86Target \
+        libmcldX86Info \
+        $(libmcld_STATIC_LIBRARIES) \
+        libLLVMX86AsmParser \
         libLLVMX86CodeGen \
         libLLVMX86Desc \
         libLLVMX86Info \
@@ -123,8 +149,10 @@
   endif
 endif
 
-LOCAL_STATIC_LIBRARIES += \
+LOCAL_WHOLE_STATIC_LIBRARIES += \
+  libLLVMObject \
   libLLVMAsmPrinter \
+  libLLVMBitWriter \
   libLLVMBitReader \
   libLLVMSelectionDAG \
   libLLVMCodeGen \
@@ -133,6 +161,8 @@
   libLLVMInstCombine \
   libLLVMipo \
   libLLVMipa \
+  libLLVMVectorize \
+  libLLVMInstrumentation \
   libLLVMTransformUtils \
   libLLVMAnalysis \
   libLLVMTarget \
@@ -145,7 +175,7 @@
 
 # Modules that need get installed if and only if the target libbcc.so is
 # installed.
-LOCAL_REQUIRED_MODULES := libclcore.bc libbcc.so.sha1
+LOCAL_REQUIRED_MODULES := libclcore.bc libbcc.sha1
 
 ifeq ($(ARCH_ARM_HAVE_NEON),true)
 LOCAL_REQUIRED_MODULES += libclcore_neon.bc
@@ -160,13 +190,12 @@
 # Note that libLLVMBitReader:libLLVMCore:libLLVMSupport are used by
 # pixelflinger2.
 
-LOCAL_LDFLAGS += -Wl,--exclude-libs=libLLVMARMDisassembler:libLLVMARMAsmPrinter:libLLVMX86Disassembler:libLLVMX86AsmPrinter:libLLVMMCParser:libLLVMARMCodeGen:libLLVMARMDesc:libLLVMARMInfo:libLLVMSelectionDAG:libLLVMAsmPrinter:libLLVMCodeGen:libLLVMLinker:libLLVMTarget:libLLVMMC:libLLVMScalarOpts:libLLVMInstCombine:libLLVMipo:libLLVMipa:libLLVMTransformUtils:libLLVMAnalysis
+#LOCAL_LDFLAGS += -Wl,--exclude-libs=libmcldARMTarget:libmcldARMInfo:libmcldMipsTarget:libmcldMipsInfo:libmcldX86Target:libmcldX86Info:libmcldCodeGen:libmcldTarget:libmcldLDVariant:libmcldMC:libmcldSupport:libmcldLD:libmcldADT:libLLVMARMDisassembler:libLLVMARMAsmPrinter:libLLVMX86Disassembler:libLLVMX86AsmPrinter:libLLVMMipsDisassembler:libLLVMMipsAsmPrinter:libLLVMMCParser:libLLVMARMCodeGen:libLLVMARMDesc:libLLVMARMInfo:libLLVMX86CodeGen:libLLVMX86Desc:libLLVMX86Info:libLLVMX86Utils:libLLVMMipsCodeGen:libLLVMMipsDesc:libLLVMMipsInfo:libLLVMSelectionDAG:libLLVMAsmPrinter:libLLVMCodeGen:libLLVMLinker:libLLVMTarget:libLLVMMC:libLLVMScalarOpts:libLLVMInstCombine:libLLVMipo:libLLVMipa:libLLVMTransformUtils:libLLVMAnalysis
 
-# Generate build stamp (Build time + Build git revision + Build Semi SHA1)
-include $(LOCAL_PATH)/libbcc-gen-build-stamp.mk
+# Generate build information (Build time + Build git revision + Build Semi SHA1)
+include $(LIBBCC_ROOT_PATH)/libbcc-gen-build-info.mk
 
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
+include $(LIBBCC_DEVICE_BUILD_MK)
 include $(BUILD_SHARED_LIBRARY)
 
 
@@ -181,52 +210,63 @@
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
 LOCAL_IS_HOST_MODULE := true
 
-LOCAL_CFLAGS := $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -D__HOST__
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-
-LOCAL_SRC_FILES := lib/ExecutionEngine/bcc.cpp
-
 LOCAL_WHOLE_STATIC_LIBRARIES += $(libbcc_WHOLE_STATIC_LIBRARIES)
 
-LOCAL_STATIC_LIBRARIES += librsloader
+LOCAL_WHOLE_STATIC_LIBRARIES += librsloader
 
 ifeq ($(libbcc_USE_DISASSEMBLER),1)
-  LOCAL_STATIC_LIBRARIES += \
+  LOCAL_WHOLE_STATIC_LIBRARIES += \
     libLLVMARMDisassembler \
-    libLLVMARMAsmPrinter \
-    libLLVMX86Disassembler \
-    libLLVMMCParser
+    libLLVMMipsDisassembler \
+    libLLVMX86Disassembler
 endif
 
-LOCAL_STATIC_LIBRARIES += \
+LOCAL_WHOLE_STATIC_LIBRARIES += \
+  libmcldARMTarget \
+  libmcldARMInfo \
+  libmcldMipsTarget \
+  libmcldMipsInfo \
+  libmcldX86Target \
+  libmcldX86Info
+
+LOCAL_WHOLE_STATIC_LIBRARIES += $(libmcld_STATIC_LIBRARIES)
+
+LOCAL_WHOLE_STATIC_LIBRARIES += \
+  libLLVMARMAsmParser \
   libLLVMARMCodeGen \
   libLLVMARMDesc \
   libLLVMARMInfo
 
-LOCAL_STATIC_LIBRARIES += \
+LOCAL_WHOLE_STATIC_LIBRARIES += \
+  libLLVMMipsAsmParser \
   libLLVMMipsCodeGen \
   libLLVMMipsAsmPrinter \
   libLLVMMipsDesc \
   libLLVMMipsInfo
 
-LOCAL_STATIC_LIBRARIES += \
+LOCAL_WHOLE_STATIC_LIBRARIES += \
+  libLLVMX86AsmParser \
   libLLVMX86CodeGen \
   libLLVMX86Desc \
   libLLVMX86AsmPrinter \
   libLLVMX86Info \
   libLLVMX86Utils
 
-LOCAL_STATIC_LIBRARIES += \
+LOCAL_WHOLE_STATIC_LIBRARIES += \
+  libLLVMObject \
   libLLVMAsmPrinter \
+  libLLVMBitWriter \
   libLLVMBitReader \
   libLLVMSelectionDAG \
   libLLVMCodeGen \
   libLLVMLinker \
+  libLLVMArchive \
   libLLVMScalarOpts \
   libLLVMInstCombine \
   libLLVMipo \
   libLLVMipa \
+  libLLVMVectorize \
+  libLLVMInstrumentation \
   libLLVMTransformUtils \
   libLLVMAnalysis \
   libLLVMTarget \
@@ -243,11 +283,10 @@
 
 LOCAL_LDLIBS := -ldl -lpthread
 
-# Generate build stamp (Build time + Build git revision + Build Semi SHA1)
-include $(LOCAL_PATH)/libbcc-gen-build-stamp.mk
+# Generate build information (Build time + Build git revision + Build Semi SHA1)
+include $(LIBBCC_ROOT_PATH)/libbcc-gen-build-info.mk
 
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
+include $(LIBBCC_HOST_BUILD_MK)
 include $(BUILD_HOST_SHARED_LIBRARY)
 
 
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 9983497..1f5aa45 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -53,6 +53,8 @@
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libclcore.bc_intermediates)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libclcore.bc_intermediates)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libclcore.bc_intermediates)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libclcore.bc_intermediates)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libclcore_neon.bc_intermediates)
 
 # ************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
diff --git a/helper/Android.mk b/helper/Android.mk
deleted file mode 100644
index ecba95f..0000000
--- a/helper/Android.mk
+++ /dev/null
@@ -1,66 +0,0 @@
-#
-# 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.
-#
-#
-
-LOCAL_PATH := $(call my-dir)
-include $(LOCAL_PATH)/../libbcc-config.mk
-
-
-#=====================================================================
-# Device Static Library: libbccHelper
-#=====================================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libbccHelper
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-
-LOCAL_SRC_FILES := sha1.c
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
-include $(BUILD_STATIC_LIBRARY)
-
-
-#=====================================================================
-# Host Static Library: libbccHelper
-#=====================================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libbccHelper
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-LOCAL_IS_HOST_MODULE := true
-
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -D__HOST__
-
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-
-LOCAL_SRC_FILES := \
-  DebugHelper.c \
-  sha1.c
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
-include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/helper/DebugHelper.c b/helper/DebugHelper.c
deleted file mode 100644
index 5c01b1b..0000000
--- a/helper/DebugHelper.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "DebugHelper.h"
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define LOG_BUF_SIZE 1024
-
-#if USE_LOGGER && !defined(__arm__)
-int __android_log_print(int prio, const char *tag, const char *fmt, ...) {
-  va_list ap;
-  char buf[LOG_BUF_SIZE];
-
-  va_start(ap, fmt);
-  vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
-  va_end(ap);
-
-  return __android_log_write(prio, tag, buf);
-}
-
-int __android_log_write(int prio, const char *tag, const char *msg) {
-  if (!tag) {
-    tag = "";
-  }
-
-  return fprintf(stderr, "[%s] %s\n", tag, msg);
-}
-#endif // USE_LOGGER && !defined(__arm__)
diff --git a/helper/DebugHelper.h b/helper/DebugHelper.h
deleted file mode 100644
index ae68e3a..0000000
--- a/helper/DebugHelper.h
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright 2010, 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.
- */
-
-#ifndef BCC_DEBUGHELPER_H
-#define BCC_DEBUGHELPER_H
-
-#include "Config.h"
-
-#if USE_LOGGER
-
-#define LOG_TAG "bcc"
-#include <cutils/log.h>
-
-#else // !USE_LOGGER
-
-#undef ALOGV
-#undef ALOGI
-#undef ALOGD
-#undef ALOGW
-#undef ALOGE
-#undef LOGA
-
-#define ALOGV(...)
-#define ALOGI(...)
-#define ALOGD(...)
-#define ALOGW(...)
-#define ALOGE(...)
-#define LOGA(...)
-
-#endif
-
-
-#if !USE_FUNC_LOGGER
-
-#define BCC_FUNC_LOGGER()
-
-#else // USE_FUNC_LOGGER
-
-#if defined(__cplusplus)
-namespace bcc {
-  class FuncLogger {
-  private:
-    char const *mFuncName;
-
-  public:
-    FuncLogger(char const *name) : mFuncName(name) {
-      ALOGD("---> BEGIN: libbcc [ %s ]\n", name);
-    }
-
-    ~FuncLogger() {
-      ALOGD("---> END: libbcc [ %s ]\n", mFuncName);
-    }
-  };
-} // namespace bcc
-
-#define BCC_FUNC_LOGGER() bcc::FuncLogger XX__FuncLogger(__func__)
-#endif
-
-#endif
-
-#endif // BCC_DEBUGHELPER_H
diff --git a/include/bcc/AndroidBitcode/ABCCompilerDriver.h b/include/bcc/AndroidBitcode/ABCCompilerDriver.h
new file mode 100644
index 0000000..0fa38fe
--- /dev/null
+++ b/include/bcc/AndroidBitcode/ABCCompilerDriver.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_ABC_COMPILER_DRIVER_H
+#define BCC_ABC_COMPILER_DRIVER_H
+
+#include "bcc/BCCContext.h"
+#include "bcc/Compiler.h"
+#include "bcc/Linker.h"
+
+#include <string>
+
+namespace bcc {
+
+class CompilerConfig;
+class LinkerConfig;
+
+class ABCCompilerDriver {
+private:
+  BCCContext mContext;
+  Compiler mCompiler;
+  Linker mLinker;
+
+  CompilerConfig *mCompilerConfig;
+  LinkerConfig *mLinkerConfig;
+
+  std::string mTriple;
+  std::string mAndroidSysroot;
+
+private:
+  bool configCompiler();
+  bool configLinker();
+
+private:
+  Script *prepareScript(int pInputFd);
+  bool compile(Script &pScript, llvm::raw_ostream &pOutput);
+  bool link(const Script &pScript, const std::string &input_relocatable,
+            int pOutputFd);
+
+public:
+  ABCCompilerDriver(const std::string &pTriple,
+                    const std::string &pAndroidSysroot);
+
+  ~ABCCompilerDriver();
+
+  // Compile the bitcode and link the shared object
+  bool build(int pInputFd, int pOutputFd);
+};
+
+} // end namespace bcc
+
+#endif // BCC_ABC_COMPILER_DRIVER_H
diff --git a/include/bcc/BCCContext.h b/include/bcc/BCCContext.h
new file mode 100644
index 0000000..be24d31
--- /dev/null
+++ b/include/bcc/BCCContext.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_CONTEXT_H
+#define BCC_CONTEXT_H
+
+namespace llvm {
+  class LLVMContext;
+}
+
+namespace bcc {
+
+class BCCContextImpl;
+class Source;
+
+/*
+ * class BCCContext manages the global data across the libbcc infrastructure.
+ */
+class BCCContext {
+public:
+  BCCContextImpl *const mImpl;
+
+  BCCContext();
+  ~BCCContext();
+
+  llvm::LLVMContext &getLLVMContext();
+  const llvm::LLVMContext &getLLVMContext() const;
+
+  void addSource(Source &pSource);
+  void removeSource(Source &pSource);
+
+  // Global BCCContext
+  static BCCContext *GetOrCreateGlobalContext();
+  static void DestroyGlobalContext();
+};
+
+} // namespace bcc
+
+#endif  // BCC_CONTEXT_H
diff --git a/include/bcc/Compiler.h b/include/bcc/Compiler.h
new file mode 100644
index 0000000..1e2676b
--- /dev/null
+++ b/include/bcc/Compiler.h
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2010-2012, 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.
+ */
+
+#ifndef BCC_COMPILER_H
+#define BCC_COMPILER_H
+
+namespace llvm {
+
+class raw_ostream;
+class PassManager;
+class TargetData;
+class TargetMachine;
+
+} // end namespace llvm
+
+namespace bcc {
+
+class CompilerConfig;
+class OutputFile;
+class Script;
+
+//===----------------------------------------------------------------------===//
+// Design of Compiler
+//===----------------------------------------------------------------------===//
+// 1. A compiler instance can be constructed provided an "initial config."
+// 2. A compiler can later be re-configured using config().
+// 3. Once config() is invoked, it'll re-create TargetMachine instance (i.e.,
+//    mTarget) according to the configuration supplied. TargetMachine instance
+//    is *shared* across the different calls to compile() before the next call
+//    to config().
+// 4. Once a compiler instance is created, you can use the compile() service
+//    to compile the file over and over again. Each call uses TargetMachine
+//    instance to construct the compilation passes.
+class Compiler {
+public:
+  enum ErrorCode {
+    kSuccess,
+
+    kInvalidConfigNoTarget,
+    kErrCreateTargetMachine,
+    kErrSwitchTargetMachine,
+    kErrNoTargetMachine,
+    kErrTargetDataNoMemory,
+    kErrMaterialization,
+    kErrInvalidOutputFileState,
+    kErrPrepareOutput,
+    kPrepareCodeGenPass,
+
+    kErrHookBeforeAddLTOPasses,
+    kErrHookAfterAddLTOPasses,
+    kErrHookBeforeExecuteLTOPasses,
+    kErrHookAfterExecuteLTOPasses,
+
+    kErrHookBeforeAddCodeGenPasses,
+    kErrHookAfterAddCodeGenPasses,
+    kErrHookBeforeExecuteCodeGenPasses,
+    kErrHookAfterExecuteCodeGenPasses,
+
+    kMaxErrorCode,
+  };
+
+  static const char *GetErrorString(enum ErrorCode pErrCode);
+
+private:
+  llvm::TargetMachine *mTarget;
+  // LTO is enabled by default.
+  bool mEnableLTO;
+
+  enum ErrorCode runLTO(Script &pScript);
+  enum ErrorCode runCodeGen(Script &pScript, llvm::raw_ostream &pResult);
+
+public:
+  Compiler();
+  Compiler(const CompilerConfig &pConfig);
+
+  enum ErrorCode config(const CompilerConfig &pConfig);
+
+  // Compile a script and output the result to a LLVM stream.
+  enum ErrorCode compile(Script &pScript, llvm::raw_ostream &pResult);
+
+  // Compile a script and output the result to a file.
+  enum ErrorCode compile(Script &pScript, OutputFile &pResult);
+
+  const llvm::TargetMachine& getTargetMachine() const
+  { return *mTarget; }
+
+  void enableLTO(bool pEnable = true)
+  { mEnableLTO = pEnable; }
+
+  virtual ~Compiler();
+
+protected:
+  //===--------------------------------------------------------------------===//
+  // Plugin callbacks for sub-class.
+  //===--------------------------------------------------------------------===//
+  // Called before adding first pass to code-generation passes.
+  virtual bool beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM)
+  { return true; }
+
+  // Called after adding last pass to code-generation passes.
+  virtual bool afterAddLTOPasses(Script &pScript, llvm::PassManager &pPM)
+  { return true; }
+
+  // Called before executing code-generation passes.
+  virtual bool beforeExecuteLTOPasses(Script &pScript,
+                                          llvm::PassManager &pPM)
+  { return true; }
+
+  // Called after executing code-generation passes.
+  virtual bool afterExecuteLTOPasses(Script &pScript)
+  { return true; }
+
+  // Called before adding first pass to code-generation passes.
+  virtual bool beforeAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
+  { return true; }
+
+  // Called after adding last pass to code-generation passes.
+  virtual bool afterAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
+  { return true; }
+
+  // Called before executing code-generation passes.
+  virtual bool beforeExecuteCodeGenPasses(Script &pScript,
+                                          llvm::PassManager &pPM)
+  { return true; }
+
+  // Called after executing code-generation passes.
+  virtual bool afterExecuteCodeGenPasses(Script &pScript)
+  { return true; }
+};
+
+} // end namespace bcc
+
+#endif // BCC_COMPILER_H
diff --git a/include/bcc/Config/BuildInfo.h b/include/bcc/Config/BuildInfo.h
new file mode 100644
index 0000000..b2bbac0
--- /dev/null
+++ b/include/bcc/Config/BuildInfo.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_CONFIG_BUILD_INFO_H
+#define BCC_CONFIG_BUILD_INFO_H
+
+namespace bcc {
+
+class BuildInfo {
+private:
+  // Disable constructor since this is an utility class.
+  BuildInfo();
+
+public:
+  // The implementation of these functions is generated during build. See
+  // libbcc-gen-build-info.mk and tools/build/gen-build-info.py for detail.
+  static const char *GetBuildTime();
+  static const char *GetBuildRev();
+  static const char *GetBuildSourceBlob();
+};
+
+} // end namespace bcc
+
+#endif // BCC_CONFIG_BUILD_INFO_H
diff --git a/Config.h b/include/bcc/Config/Config.h
similarity index 69%
rename from Config.h
rename to include/bcc/Config/Config.h
index b1ea833..9dd2a93 100644
--- a/Config.h
+++ b/include/bcc/Config/Config.h
@@ -1,5 +1,5 @@
-#ifndef BCC_CONFIG_H
-#define BCC_CONFIG_H
+#ifndef BCC_CONFIG_CONFIG_H
+#define BCC_CONFIG_CONFIG_H
 
 #include "ConfigFromMk.h"
 
@@ -14,9 +14,9 @@
 #endif
 
 #if defined(__HOST__)
-#define DEBUG_MC_DISASSEMBLER_FILE "/tmp/mc-dis.s"
+#define DEBUG_DISASSEMBLER_FILE "/tmp/mc-dis.s"
 #else
-#define DEBUG_MC_DISASSEMBLER_FILE "/data/local/tmp/mc-dis.s"
+#define DEBUG_DISASSEMBLER_FILE "/data/local/tmp/mc-dis.s"
 #endif // defined(__HOST__)
 
 //---------------------------------------------------------------------------
@@ -56,14 +56,19 @@
   #endif
 #endif
 
+#define DEFAULT_ARM_TRIPLE_STRING      "armv7-none-linux-gnueabi"
+#define DEFAULT_MIPS_TRIPLE_STRING     "mipsel-none-linux-gnueabi"
+#define DEFAULT_X86_TRIPLE_STRING      "i686-unknown-linux"
+#define DEFAULT_X86_64_TRIPLE_STRING   "x86_64-unknown-linux"
+
 #if defined(DEFAULT_ARM_CODEGEN)
-  #define DEFAULT_TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi"
+  #define DEFAULT_TARGET_TRIPLE_STRING DEFAULT_ARM_TRIPLE_STRING
 #elif defined(DEFAULT_MIPS_CODEGEN)
-  #define DEFAULT_TARGET_TRIPLE_STRING "mipsel-none-linux-gnueabi"
+  #define DEFAULT_TARGET_TRIPLE_STRING DEFAULT_MIPS_TRIPLE_STRING
 #elif defined(DEFAULT_X86_CODEGEN)
-  #define DEFAULT_TARGET_TRIPLE_STRING "i686-unknown-linux"
+  #define DEFAULT_TARGET_TRIPLE_STRING DEFAULT_X86_TRIPLE_STRING
 #elif defined(DEFAULT_X86_64_CODEGEN)
-  #define DEFAULT_TARGET_TRIPLE_STRING "x86_64-unknown-linux"
+  #define DEFAULT_TARGET_TRIPLE_STRING DEFAULT_X86_64_TRIPLE_STRING
 #endif
 
 #if (defined(__VFP_FP__) && !defined(__SOFTFP__))
@@ -72,4 +77,4 @@
 
 //---------------------------------------------------------------------------
 
-#endif // BCC_CONFIG_H
+#endif // BCC_CONFIG_CONFIG_H
diff --git a/include/bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h b/include/bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h
new file mode 100644
index 0000000..4210489
--- /dev/null
+++ b/include/bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_BCC_RUNTIME_SYMBOL_RESOLVERS_H
+#define BCC_EXECUTION_ENGINE_BCC_RUNTIME_SYMBOL_RESOLVERS_H
+
+#include "bcc/ExecutionEngine/SymbolResolvers.h"
+
+namespace bcc {
+
+class BCCRuntimeSymbolResolver :
+  public ArraySymbolResolver<BCCRuntimeSymbolResolver> {
+  typedef ArraySymbolResolver<BCCRuntimeSymbolResolver> super;
+public:
+  // SymbolArray and NumSymbols are required to be define in the
+  // subclass of ArraySymbolResolver.
+  static const SymbolMap SymbolArray[];
+  static const size_t NumSymbols;
+
+  BCCRuntimeSymbolResolver() : super(/* pSorted */true) { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_BCC_RUNTIME_SYMBOL_RESOLVERS_H
diff --git a/lib/ExecutionEngine/GDBJIT.h b/include/bcc/ExecutionEngine/GDBJIT.h
similarity index 96%
rename from lib/ExecutionEngine/GDBJIT.h
rename to include/bcc/ExecutionEngine/GDBJIT.h
index 442b666..4567813 100644
--- a/lib/ExecutionEngine/GDBJIT.h
+++ b/include/bcc/ExecutionEngine/GDBJIT.h
@@ -33,8 +33,8 @@
 #ifndef BCC_EXECUTION_ENGINE_GDB_JIT_H
 #define BCC_EXECUTION_ENGINE_GDB_JIT_H
 
-#include "llvm/Support/DataTypes.h"
-#include "llvm/Support/Compiler.h"
+#include <llvm/Support/DataTypes.h>
+#include <llvm/Support/Compiler.h>
 
 // This must be kept in sync with gdb/gdb/jit.h .
 extern "C" {
diff --git a/lib/ExecutionEngine/GDBJITRegistrar.h b/include/bcc/ExecutionEngine/GDBJITRegistrar.h
similarity index 90%
rename from lib/ExecutionEngine/GDBJITRegistrar.h
rename to include/bcc/ExecutionEngine/GDBJITRegistrar.h
index debf503..868dafa 100644
--- a/lib/ExecutionEngine/GDBJITRegistrar.h
+++ b/include/bcc/ExecutionEngine/GDBJITRegistrar.h
@@ -30,8 +30,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef BCC_GDBJITREGISTRAR_H
-#define BCC_GDBJITREGISTRAR_H
+#ifndef BCC_EXECUTION_ENGINE_GDB_JIT_REGISTRAR_H
+#define BCC_EXECUTION_ENGINE_GDB_JIT_REGISTRAR_H
 
 #include <cstddef>
 
@@ -41,4 +41,4 @@
 void registerObjectWithGDB(const ObjectBuffer* Object, std::size_t Size);
 void deregisterObjectWithGDB(const ObjectBuffer* Object);
 
-#endif // BCC_GDBJITREGISTRAR_H
+#endif // BCC_EXECUTION_ENGINE_GDB_JIT_REGISTRAR_H
diff --git a/include/bcc/ExecutionEngine/ObjectLoader.h b/include/bcc/ExecutionEngine/ObjectLoader.h
new file mode 100644
index 0000000..fb31c33
--- /dev/null
+++ b/include/bcc/ExecutionEngine/ObjectLoader.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_OBJECT_LOADER_H
+#define BCC_EXECUTION_ENGINE_OBJECT_LOADER_H
+
+#include <cstddef>
+
+#include "bcc/Support/Log.h"
+
+#include <utils/Vector.h>
+
+namespace bcc {
+
+class FileBase;
+class ObjectLoaderImpl;
+class SymbolResolverInterface;
+
+class ObjectLoader {
+public:
+  enum SymbolType {
+    // TODO: More types.
+    kFunctionType,
+    kUnknownType,
+  };
+
+private:
+  ObjectLoaderImpl *mImpl;
+
+  void *mDebugImage;
+
+  ObjectLoader() : mImpl(NULL), mDebugImage(0) { }
+
+public:
+  // Load from a in-memory object. pName is a descriptive name of this memory.
+  static ObjectLoader *Load(void *pMemStart, size_t pMemSize, const char *pName,
+                            SymbolResolverInterface &pResolver,
+                            bool pEnableGDBDebug);
+
+  // Load from a file.
+  static ObjectLoader *Load(FileBase &pFile,
+                            SymbolResolverInterface &pResolver,
+                            bool pEnableGDBDebug);
+
+  void *getSymbolAddress(const char *pName) const;
+
+  size_t getSymbolSize(const char *pName) const;
+
+  // Get the symbol name where the symbol is of the type pType. If kUnknownType
+  // is given, it returns all symbols' names in the object.
+  bool getSymbolNameList(android::Vector<const char *>& pNameList,
+                         SymbolType pType = kUnknownType) const;
+
+  ~ObjectLoader();
+};
+
+} // namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_OBJECT_LOADER_H
diff --git a/include/bcc/ExecutionEngine/SymbolResolverInterface.h b/include/bcc/ExecutionEngine/SymbolResolverInterface.h
new file mode 100644
index 0000000..c80073d
--- /dev/null
+++ b/include/bcc/ExecutionEngine/SymbolResolverInterface.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_INTERFACE_H
+#define BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_INTERFACE_H
+
+#include <cstddef>
+
+namespace bcc {
+
+class SymbolResolverInterface {
+public:
+  static void *LookupFunction(void *pResolver, const char *pName) {
+    SymbolResolverInterface *resolver =
+        reinterpret_cast<SymbolResolverInterface*>(pResolver);
+    return ((resolver != NULL) ? resolver->getAddress(pName) : NULL);
+  }
+
+  // Should this be a const method?
+  virtual void *getAddress(const char *pName) = 0;
+
+  virtual ~SymbolResolverInterface() { }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_INTERFACE_H
diff --git a/include/bcc/ExecutionEngine/SymbolResolverProxy.h b/include/bcc/ExecutionEngine/SymbolResolverProxy.h
new file mode 100644
index 0000000..996b829
--- /dev/null
+++ b/include/bcc/ExecutionEngine/SymbolResolverProxy.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_PROXY_H
+#define BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_PROXY_H
+
+#include "bcc/ExecutionEngine/SymbolResolverInterface.h"
+#include "bcc/Support/Log.h"
+
+#include <utils/Vector.h>
+
+namespace bcc {
+
+class SymbolResolverProxy : public SymbolResolverInterface {
+private:
+  android::Vector<SymbolResolverInterface *> mChain;
+
+public:
+  SymbolResolverProxy() { }
+
+  void chainResolver(SymbolResolverInterface &pResolver);
+
+  virtual void *getAddress(const char *pName);
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SYMBOL_RESOLVER_PROXY_H
diff --git a/include/bcc/ExecutionEngine/SymbolResolvers.h b/include/bcc/ExecutionEngine/SymbolResolvers.h
new file mode 100644
index 0000000..909211f
--- /dev/null
+++ b/include/bcc/ExecutionEngine/SymbolResolvers.h
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_SYMBOL_RESOLVERS_H
+#define BCC_EXECUTION_ENGINE_SYMBOL_RESOLVERS_H
+
+#include <cstdlib>
+#include <cstring>
+
+#include "SymbolResolverInterface.h"
+
+namespace bcc {
+
+/*
+ * Symbol lookup via dlopen()/dlsym().
+ */
+class DyldSymbolResolver : public SymbolResolverInterface {
+public:
+  typedef void *HandleTy;
+
+private:
+  HandleTy mHandle;
+  char *mError;
+
+public:
+  // If pFileName is NULL, it will search symbol in the current process image.
+  DyldSymbolResolver(const char *pFileName, bool pLazyBinding = true);
+
+  virtual void *getAddress(const char *pName);
+
+  inline bool hasError() const
+  { return (mError != NULL); }
+  inline const char *getError() const
+  { return mError; }
+
+  ~DyldSymbolResolver();
+};
+
+/*
+ * Symbol lookup by searching through an array of SymbolMap.
+ */
+template<typename Subclass>
+class ArraySymbolResolver : public SymbolResolverInterface {
+public:
+  typedef struct {
+    // Symbol name
+    const char *mName;
+    // Symbol address
+    void *mAddr;
+  } SymbolMap;
+
+private:
+  // True if the symbol name is sorted in the array.
+  bool mSorted;
+
+  static int CompareSymbolName(const void *pA, const void *pB) {
+    return ::strcmp(reinterpret_cast<const SymbolMap *>(pA)->mName,
+                    reinterpret_cast<const SymbolMap *>(pB)->mName);
+  }
+
+public:
+  ArraySymbolResolver(bool pSorted = false) : mSorted(pSorted) { }
+
+  virtual void *getAddress(const char *pName) {
+    const SymbolMap *result = NULL;
+
+    if (mSorted) {
+      // Use binary search.
+      const SymbolMap key = { pName, NULL };
+
+      result = reinterpret_cast<SymbolMap *>(
+                   ::bsearch(&key, Subclass::SymbolArray,
+                                   Subclass::NumSymbols,
+                                   sizeof(SymbolMap),
+                                   CompareSymbolName));
+    } else {
+      // Use linear search.
+      for (size_t i = 0; i < Subclass::NumSymbols; i++) {
+        if (::strcmp(Subclass::SymbolArray[i].mName, pName) == 0) {
+          result = &Subclass::SymbolArray[i];
+          break;
+        }
+      }
+    }
+
+    return ((result != NULL) ? result->mAddr : NULL);
+  }
+};
+
+template<typename ContextTy = void *>
+class LookupFunctionSymbolResolver : public SymbolResolverInterface {
+public:
+  typedef void *(*LookupFunctionTy)(ContextTy pContext, const char *pName);
+
+private:
+  LookupFunctionTy mLookupFunc;
+  ContextTy mContext;
+
+public:
+  LookupFunctionSymbolResolver(LookupFunctionTy pLookupFunc = NULL,
+                               ContextTy pContext = NULL)
+    : mLookupFunc(pLookupFunc), mContext(pContext) { }
+
+  virtual void *getAddress(const char *pName) {
+    return ((mLookupFunc != NULL) ? mLookupFunc(mContext, pName) : NULL);
+  }
+
+  inline LookupFunctionTy getLookupFunction() const
+  { return mLookupFunc; }
+  inline ContextTy getContext() const
+  { return mContext; }
+
+  inline void setLookupFunction(LookupFunctionTy pLookupFunc)
+  { mLookupFunc = pLookupFunc; }
+  inline void setContext(ContextTy pContext)
+  { mContext = pContext; }
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_SYMBOL_RESOLVERS_H
diff --git a/include/bcc/Linker.h b/include/bcc/Linker.h
new file mode 100644
index 0000000..6a3d7d1
--- /dev/null
+++ b/include/bcc/Linker.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2010-2012, 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.
+ */
+
+#ifndef BCC_LINKER_H
+#define BCC_LINKER_H
+
+#include <string>
+
+namespace mcld {
+
+class TargetLDBackend;
+class MCLDDriver;
+class MemoryFactory;
+class MCLDInfo;
+class TreeIteratorBase;
+class Input;
+
+namespace sys { namespace fs {
+
+class Path;
+
+} } // end namespace sys::fs
+
+} // end namespace mcld
+
+namespace bcc {
+
+class MemoryFactory;
+class LinkerConfig;
+
+class Linker {
+public:
+  enum ErrorCode {
+    kSuccess,
+    kDoubleConfig,
+    kCreateBackend,
+    kDelegateLDInfo,
+    kFindNameSpec,
+    kOpenNameSpec,
+    kOpenObjectFile,
+    kNotConfig,
+    kNotSetUpOutput,
+    kOpenOutput,
+    kReadSections,
+    kReadSymbols,
+    kAddAdditionalSymbols,
+    kMaxErrorCode,
+  };
+
+  static const char *GetErrorString(enum ErrorCode pErrCode);
+
+private:
+  mcld::TargetLDBackend *mBackend;
+  mcld::MCLDDriver *mDriver;
+  MemoryFactory *mMemAreaFactory;
+  mcld::MCLDInfo *mLDInfo;
+  mcld::TreeIteratorBase *mRoot;
+  bool mShared;
+  std::string mSOName;
+
+public:
+  Linker();
+
+  Linker(const LinkerConfig& pConfig);
+
+  ~Linker();
+
+  enum ErrorCode config(const LinkerConfig& pConfig);
+
+  enum ErrorCode addNameSpec(const std::string &pNameSpec);
+
+  enum ErrorCode addObject(const std::string &pObjectPath);
+
+  enum ErrorCode addObject(void* pMemory, size_t pSize);
+
+  enum ErrorCode addCode(void* pMemory, size_t pSize);
+
+  enum ErrorCode setOutput(const std::string &pPath);
+
+  enum ErrorCode setOutput(int pFileHandler);
+
+  enum ErrorCode link();
+
+private:
+  enum ErrorCode extractFiles(const LinkerConfig& pConfig);
+
+  enum ErrorCode openFile(const mcld::sys::fs::Path& pPath,
+                          enum ErrorCode pCode,
+                          mcld::Input& pInput);
+
+  void advanceRoot();
+};
+
+} // end namespace bcc
+
+#endif // BCC_LINKER_H
diff --git a/lib/Transforms/BCCTransforms.h b/include/bcc/RenderScript/RSCompiler.h
similarity index 64%
copy from lib/Transforms/BCCTransforms.h
copy to include/bcc/RenderScript/RSCompiler.h
index 1ac8174..537e749 100644
--- a/lib/Transforms/BCCTransforms.h
+++ b/include/bcc/RenderScript/RSCompiler.h
@@ -14,11 +14,19 @@
  * limitations under the License.
  */
 
-#include "llvm/Pass.h"
+#ifndef BCC_RS_COMPILER_H
+#define BCC_RS_COMPILER_H
+
+#include "bcc/Compiler.h"
 
 namespace bcc {
 
-llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                          std::vector<uint32_t>& Signatures);
+class RSCompiler : public Compiler {
+private:
+  virtual bool beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM);
+  virtual bool beforeExecuteLTOPasses(Script &pScript, llvm::PassManager &pPM);
+};
 
-}  // namespace bcc
+} // end namespace bcc
+
+#endif // BCC_RS_COMPILER_H
diff --git a/include/bcc/RenderScript/RSCompilerDriver.h b/include/bcc/RenderScript/RSCompilerDriver.h
new file mode 100644
index 0000000..61bff57
--- /dev/null
+++ b/include/bcc/RenderScript/RSCompilerDriver.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_RS_COMPILER_DRIVER_H
+#define BCC_RS_COMPILER_DRIVER_H
+
+#include "bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h"
+#include "bcc/ExecutionEngine/SymbolResolvers.h"
+#include "bcc/ExecutionEngine/SymbolResolverProxy.h"
+#include "bcc/RenderScript/RSInfo.h"
+#include "bcc/RenderScript/RSCompiler.h"
+
+namespace bcc {
+
+class BCCContext;
+class CompilerConfig;
+class RSExecutable;
+class RSScript;
+
+class RSCompilerDriver {
+private:
+  CompilerConfig *mConfig;
+  RSCompiler mCompiler;
+
+  BCCRuntimeSymbolResolver mBCCRuntime;
+  LookupFunctionSymbolResolver<void*> mRSRuntime;
+  SymbolResolverProxy mResolver;
+
+  RSExecutable *loadScriptCache(const char *pOutputPath,
+                                const RSInfo::DependencyTableTy &pDeps);
+
+  // Setup the compiler config for the given script. Return true if mConfig has
+  // been changed and false if it remains unchanged.
+  bool setupConfig(const RSScript &pScript);
+
+  RSExecutable *compileScript(RSScript &pScript,
+                              const char* pScriptName,
+                              const char *pOutputPath,
+                              const RSInfo::DependencyTableTy &pDeps);
+
+public:
+  RSCompilerDriver();
+  ~RSCompilerDriver();
+
+  inline void setRSRuntimeLookupFunction(
+      LookupFunctionSymbolResolver<>::LookupFunctionTy pLookupFunc)
+  { mRSRuntime.setLookupFunction(pLookupFunc); }
+  inline void setRSRuntimeLookupContext(void *pContext)
+  { mRSRuntime.setContext(pContext); }
+
+  // FIXME: This method accompany with loadScriptCache and compileScript should
+  //        all be const-methods. They're not now because the getAddress() in
+  //        SymbolResolverInterface is not a const-method.
+  RSExecutable *build(BCCContext &pContext,
+                      const char *pCacheDir, const char *pResName,
+                      const char *pBitcode, size_t pBitcodeSize);
+};
+
+} // end namespace bcc
+
+#endif // BCC_RS_COMPILER_DRIVER_H
diff --git a/include/bcc/RenderScript/RSExecutable.h b/include/bcc/RenderScript/RSExecutable.h
new file mode 100644
index 0000000..fd50617
--- /dev/null
+++ b/include/bcc/RenderScript/RSExecutable.h
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_RS_EXECUTABLE_H
+#define BCC_RS_EXECUTABLE_H
+
+#include <cstddef>
+
+
+#include "bcc/ExecutionEngine/ObjectLoader.h"
+#include "bcc/RenderScript/RSInfo.h"
+#include "bcc/Support/Log.h"
+
+#include <utils/Vector.h>
+
+namespace bcc {
+
+class FileBase;
+class OutputFile;
+class SymbolResolverProxy;
+
+/*
+ * RSExecutable holds the build results of a RSScript.
+ */
+class RSExecutable {
+private:
+  RSInfo *mInfo;
+  bool mIsInfoDirty;
+
+  FileBase *mObjFile;
+
+  ObjectLoader *mLoader;
+
+  // Memory address of rs export stuffs
+  android::Vector<void *> mExportVarAddrs;
+  android::Vector<void *> mExportFuncAddrs;
+  android::Vector<void *> mExportForeachFuncAddrs;
+
+  // FIXME: These are designed for RenderScript HAL and is initialized in
+  //        RSExecutable::Create(). Both of them come from RSInfo::getPragmas().
+  //        If possible, read the pragma key/value pairs directly from RSInfo.
+  android::Vector<const char *> mPragmaKeys;
+  android::Vector<const char *> mPragmaValues;
+
+  RSExecutable(RSInfo &pInfo, FileBase &pObjFile, ObjectLoader &pLoader)
+    : mInfo(&pInfo), mIsInfoDirty(false), mObjFile(&pObjFile), mLoader(&pLoader)
+  { }
+
+public:
+  // This is a NULL-terminated string array which specifies "Special" functions
+  // in RenderScript (e.g., root().)
+  static const char *SpecialFunctionNames[];
+
+  // Return NULL on error. If the return object is non-NULL, it claims the
+  // ownership of pInfo and pObjFile.
+  static RSExecutable *Create(RSInfo &pInfo,
+                              FileBase &pObjFile,
+                              SymbolResolverProxy &pResolver);
+
+  inline const RSInfo &getInfo() const
+  { return *mInfo; }
+
+  // Interfaces to RSInfo
+  inline bool isThreadable() const
+  { return mInfo->isThreadable(); }
+
+  inline void setThreadable(bool pThreadable = true) {
+    if (mInfo->isThreadable() != pThreadable) {
+      mInfo->setThreadable(pThreadable);
+      mIsInfoDirty = true;
+    }
+    return;
+  }
+
+  // Interfaces to ObjectLoader
+  inline void *getSymbolAddress(const char *pName) const
+  { return mLoader->getSymbolAddress(pName); }
+
+  bool syncInfo(bool pForce = false);
+
+  // Disassemble and dump the relocated functions to the pOutput.
+  void dumpDisassembly(OutputFile &pOutput) const;
+
+  inline const android::Vector<void *> &getExportVarAddrs() const
+  { return mExportVarAddrs; }
+  inline const android::Vector<void *> &getExportFuncAddrs() const
+  { return mExportFuncAddrs; }
+  inline const android::Vector<void *> &getExportForeachFuncAddrs() const
+  { return mExportForeachFuncAddrs; }
+
+  inline const android::Vector<const char *> &getPragmaKeys() const
+  { return mPragmaKeys; }
+  inline const android::Vector<const char *> &getPragmaValues() const
+  { return mPragmaValues; }
+
+  ~RSExecutable();
+};
+
+} // end namespace bcc
+
+#endif // BCC_RS_EXECUTABLE_H
diff --git a/include/bcc/RenderScript/RSInfo.h b/include/bcc/RenderScript/RSInfo.h
new file mode 100644
index 0000000..a503661
--- /dev/null
+++ b/include/bcc/RenderScript/RSInfo.h
@@ -0,0 +1,260 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_RS_INFO_H
+#define BCC_RS_INFO_H
+
+#include <stdint.h>
+
+#include <utility>
+
+#include "bcc/RenderScript/RSScript.h"
+#include "bcc/Support/Log.h"
+#include "bcc/Support/Sha1Util.h"
+
+#include <utils/String8.h>
+#include <utils/Vector.h>
+
+namespace bcc {
+
+// Forward declarations
+class FileBase;
+class InputFile;
+class OutputFile;
+class Source;
+
+namespace rsinfo {
+
+/* RS info file magic */
+#define RSINFO_MAGIC      "\0rsinfo\n"
+
+/* RS info file version, encoded in 4 bytes of ASCII */
+#define RSINFO_VERSION    "003\0"
+
+struct __attribute__((packed)) ListHeader {
+  // The offset from the beginning of the file of data
+  uint32_t offset;
+  // Number of item in the list
+  uint32_t count;
+  // Size of each item
+  uint8_t itemSize;
+};
+
+/* RS info file header */
+struct __attribute__((packed)) Header {
+  // Magic versus version
+  uint8_t magic[8];
+  uint8_t version[4];
+
+  uint8_t isThreadable;
+  uint8_t hasDebugInformation;
+
+  uint16_t headerSize;
+
+  uint32_t strPoolSize;
+
+  struct ListHeader dependencyTable;
+  struct ListHeader pragmaList;
+  struct ListHeader objectSlotList;
+  struct ListHeader exportVarNameList;
+  struct ListHeader exportFuncNameList;
+  struct ListHeader exportForeachFuncList;
+};
+
+typedef uint32_t StringIndexTy;
+// Use value -1 as an invalid string index marker. No need to declare with
+// 'static' modifier since 'const' variable has internal linkage by default.
+const StringIndexTy gInvalidStringIndex = static_cast<StringIndexTy>(-1);
+
+struct __attribute__((packed)) DependencyTableItem {
+  StringIndexTy id;
+  // SHA-1 checksum is stored as a string in string pool (and has fixed-length
+  // SHA1_DIGEST_LENGTH (=20) bytes)
+  StringIndexTy sha1;
+};
+
+struct __attribute__((packed)) PragmaItem {
+  // Pragma is a key-value pair.
+  StringIndexTy key;
+  StringIndexTy value;
+};
+
+struct __attribute__((packed)) ObjectSlotItem {
+  uint32_t slot;
+};
+
+struct __attribute__((packed)) ExportVarNameItem {
+  StringIndexTy name;
+};
+
+struct __attribute__((packed)) ExportFuncNameItem {
+  StringIndexTy name;
+};
+
+struct __attribute__((packed)) ExportForeachFuncItem {
+  StringIndexTy name;
+  uint32_t signature;
+};
+
+// Return the human-readable name of the given rsinfo::*Item in the template
+// parameter. This is for debugging and error message.
+template<typename Item>
+inline const char *GetItemTypeName();
+
+template<>
+inline const char *GetItemTypeName<DependencyTableItem>()
+{ return "rs dependency info"; }
+
+template<>
+inline const char *GetItemTypeName<PragmaItem>()
+{  return "rs pragma"; }
+
+template<>
+inline const char *GetItemTypeName<ObjectSlotItem>()
+{  return "rs object slot"; }
+
+template<>
+inline const char *GetItemTypeName<ExportVarNameItem>()
+{ return "rs export var"; }
+
+template<>
+inline const char *GetItemTypeName<ExportFuncNameItem>()
+{  return "rs export func"; }
+
+template<>
+inline const char *GetItemTypeName<ExportForeachFuncItem>()
+{ return "rs export foreach"; }
+
+} // end namespace rsinfo
+
+class RSInfo {
+public:
+  typedef android::Vector<std::pair<const char *,
+                                    const uint8_t *> > DependencyTableTy;
+  typedef android::Vector<std::pair<const char*, const char*> > PragmaListTy;
+  typedef android::Vector<uint32_t> ObjectSlotListTy;
+  typedef android::Vector<const char *> ExportVarNameListTy;
+  typedef android::Vector<const char *> ExportFuncNameListTy;
+  typedef android::Vector<std::pair<const char *,
+                                    uint32_t> > ExportForeachFuncListTy;
+
+public:
+  // Calculate or load the SHA-1 information of the built-in dependencies.
+  static void LoadBuiltInSHA1Information();
+
+  // Return the path of the RS info file corresponded to the given output
+  // executable file.
+  static android::String8 GetPath(const FileBase &pFile);
+
+  static const char LibBCCPath[];
+  static const char LibRSPath[];
+  static const char LibCLCorePath[];
+#if defined(ARCH_ARM_HAVE_NEON)
+  static const char LibCLCoreNEONPath[];
+#endif
+
+private:
+  // SHA-1 of the built-in dependencies. Will be initialized in
+  // LoadBuiltInSHA1Information().
+  static const uint8_t *LibBCCSHA1;
+  static const uint8_t *LibRSSHA1;
+  static const uint8_t *LibCLCoreSHA1;
+#if defined(ARCH_ARM_HAVE_NEON)
+  static const uint8_t *LibCLCoreNEONSHA1;
+#endif
+
+  static bool CheckDependency(const RSInfo &pInfo,
+                              const char *pInputFilename,
+                              const DependencyTableTy &pDeps);
+  static bool AddBuiltInDependencies(RSInfo &pInfo);
+
+  rsinfo::Header mHeader;
+
+  char *mStringPool;
+
+  // In most of the time, there're 4 source dependencies stored (libbcc.so,
+  // libRS.so, libclcore and the input bitcode itself.)
+  DependencyTableTy mDependencyTable;
+  PragmaListTy mPragmas;
+  ObjectSlotListTy mObjectSlots;
+  ExportVarNameListTy mExportVarNames;
+  ExportFuncNameListTy mExportFuncNames;
+  ExportForeachFuncListTy mExportForeachFuncs;
+
+  // Initialize an empty RSInfo with its size of string pool is pStringPoolSize.
+  RSInfo(size_t pStringPoolSize);
+
+  // layout() assigns value of offset in each ListHeader (i.e., it decides where
+  // data should go in the file.) It also updates fields other than offset to
+  // reflect the current RSInfo object states to mHeader.
+  bool layout(off_t initial_offset);
+
+public:
+  ~RSInfo();
+
+  // Implemented in RSInfoExtractor.cpp.
+  static RSInfo *ExtractFromSource(const Source &pSource,
+                                   const DependencyTableTy &pDeps);
+
+  // Implemented in RSInfoReader.cpp.
+  static RSInfo *ReadFromFile(InputFile &pInput,
+                              const DependencyTableTy &pDeps);
+
+  // Implemneted in RSInfoWriter.cpp
+  bool write(OutputFile &pOutput);
+
+  void dump() const;
+
+  // const getter
+  inline bool isThreadable() const
+  { return mHeader.isThreadable; }
+  inline bool hasDebugInformation() const
+  { return mHeader.hasDebugInformation; }
+  inline const DependencyTableTy &getDependencyTable() const
+  { return mDependencyTable; }
+  inline const PragmaListTy &getPragmas() const
+  { return mPragmas; }
+  inline const ObjectSlotListTy &getObjectSlots() const
+  { return mObjectSlots; }
+  inline const ExportVarNameListTy &getExportVarNames() const
+  { return mExportVarNames; }
+  inline const ExportFuncNameListTy &getExportFuncNames() const
+  { return mExportFuncNames; }
+  inline const ExportForeachFuncListTy &getExportForeachFuncs() const
+  { return mExportForeachFuncs; }
+
+  const char *getStringFromPool(rsinfo::StringIndexTy pStrIdx) const;
+  rsinfo::StringIndexTy getStringIdxInPool(const char *pStr) const;
+
+  // setter
+  inline void setThreadable(bool pThreadable = true)
+  { mHeader.isThreadable = pThreadable; }
+
+public:
+  enum FloatPrecision {
+    FP_Full,
+    FP_Relaxed,
+    FP_Imprecise,
+  };
+
+  // Return the minimal floating point precision required for the associated
+  // script.
+  FloatPrecision getFloatPrecisionRequirement() const;
+};
+
+} // end namespace bcc
+
+#endif  // BCC_RS_INFO_H
diff --git a/include/bcc/RenderScript/RSScript.h b/include/bcc/RenderScript/RSScript.h
new file mode 100644
index 0000000..4522ada
--- /dev/null
+++ b/include/bcc/RenderScript/RSScript.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_RS_SCRIPT_H
+#define BCC_RS_SCRIPT_H
+
+#include "bcc/Script.h"
+#include "bcc/Support/Sha1Util.h"
+
+namespace bcc {
+
+class RSInfo;
+class Source;
+
+class RSScript : public Script {
+public:
+  // This is one-one mapping with the llvm::CodeGenOpt::Level in
+  // llvm/Support/CodeGen.h. Therefore, value of this type can safely cast
+  // to llvm::CodeGenOpt::Level. This makes RSScript LLVM-free.
+  enum OptimizationLevel {
+    kOptLvl0, // -O0
+    kOptLvl1, // -O1
+    kOptLvl2, // -O2, -Os
+    kOptLvl3  // -O3
+  };
+
+private:
+  const RSInfo *mInfo;
+
+  unsigned mCompilerVersion;
+
+  OptimizationLevel mOptimizationLevel;
+
+private:
+  // This will be invoked when the containing source has been reset.
+  virtual bool doReset();
+
+public:
+  static bool LinkRuntime(RSScript &pScript);
+
+  RSScript(Source &pSource);
+
+  // Set the associated RSInfo of the script.
+  void setInfo(const RSInfo *pInfo)
+  { mInfo = pInfo; }
+
+  const RSInfo *getInfo() const
+  { return mInfo; }
+
+  void setCompilerVersion(unsigned pCompilerVersion)
+  {  mCompilerVersion = pCompilerVersion; }
+
+  unsigned getCompilerVersion() const
+  {  return mCompilerVersion; }
+
+  void setOptimizationLevel(OptimizationLevel pOptimizationLevel)
+  {  mOptimizationLevel = pOptimizationLevel; }
+
+  OptimizationLevel getOptimizationLevel() const
+  {  return mOptimizationLevel; }
+};
+
+} // end namespace bcc
+
+#endif // BCC_RS_SCRIPT_H
diff --git a/lib/Transforms/BCCTransforms.h b/include/bcc/RenderScript/RSTransforms.h
similarity index 68%
copy from lib/Transforms/BCCTransforms.h
copy to include/bcc/RenderScript/RSTransforms.h
index 1ac8174..77c2611 100644
--- a/lib/Transforms/BCCTransforms.h
+++ b/include/bcc/RenderScript/RSTransforms.h
@@ -14,11 +14,20 @@
  * limitations under the License.
  */
 
-#include "llvm/Pass.h"
+#ifndef BCC_RS_TRANSFORMS_H
+#define BCC_RS_TRANSFORMS_H
+
+#include "bcc/RenderScript/RSInfo.h"
+
+namespace llvm {
+  class ModulePass;
+}
 
 namespace bcc {
 
-llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                          std::vector<uint32_t>& Signatures);
+llvm::ModulePass *
+createRSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs);
 
-}  // namespace bcc
+} // end namespace bcc
+
+#endif // BCC_RS_TRANSFORMS_H
diff --git a/include/bcc/Script.h b/include/bcc/Script.h
new file mode 100644
index 0000000..226fcf9
--- /dev/null
+++ b/include/bcc/Script.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2010-2012, 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.
+ */
+
+#ifndef BCC_SCRIPT_H
+#define BCC_SCRIPT_H
+
+namespace bcc {
+
+class Source;
+
+class Script {
+private:
+  // This is the source associated with this object and is going to be
+  // compiled.
+  Source *mSource;
+
+protected:
+  // This hook will be invoked after the script object is successfully reset.
+  virtual bool doReset()
+  { return true; }
+
+public:
+  Script(Source &pSource) : mSource(&pSource) { }
+
+  virtual ~Script() { }
+
+  // Reset this object with the new source supplied. Return false if this
+  // object remains unchanged after the call (e.g., the supplied source is
+  // the same with the one contain in this object.) If pPreserveCurrent is
+  // false, the current containing source will be destroyed after successfully
+  // reset.
+  bool reset(Source &pSource, bool pPreserveCurrent = false);
+
+  // Merge (or link) another source into the current source associated with
+  // this Script object. Return false on error.
+  //
+  // This is equivalent to the call to Script::merge(...) on mSource.
+  bool mergeSource(Source &pSource, bool pPreserveSource = false);
+
+  inline Source &getSource()
+  { return *mSource; }
+  inline const Source &getSource() const
+  { return *mSource; }
+};
+
+} // end namespace bcc
+
+#endif  // BCC_SCRIPT_H
diff --git a/include/bcc/Source.h b/include/bcc/Source.h
new file mode 100644
index 0000000..74d477a
--- /dev/null
+++ b/include/bcc/Source.h
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2010-2012, 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.
+ */
+
+#ifndef BCC_SOURCE_H
+#define BCC_SOURCE_H
+
+#include <string>
+
+namespace llvm {
+  class Module;
+}
+
+namespace bcc {
+
+class BCCContext;
+
+class Source {
+private:
+  BCCContext &mContext;
+  llvm::Module *mModule;
+
+  // If true, destructor won't destroy the mModule.
+  bool mNoDelete;
+
+private:
+  Source(BCCContext &pContext, llvm::Module &pModule, bool pNoDelete = false);
+
+public:
+  static Source *CreateFromBuffer(BCCContext &pContext,
+                                  const char *pName,
+                                  const char *pBitcode,
+                                  size_t pBitcodeSize);
+
+  static Source *CreateFromFile(BCCContext &pContext,
+                                const std::string &pPath);
+
+  static Source *CreateFromFd(BCCContext &pContext, int pFd);
+
+  // Create a Source object from an existing module. If pNoDelete
+  // is true, destructor won't call delete on the given module.
+  static Source *CreateFromModule(BCCContext &pContext,
+                                  llvm::Module &pModule,
+                                  bool pNoDelete = false);
+
+  static Source *CreateEmpty(BCCContext &pContext, const std::string &pName);
+
+  // Merge the current source with pSource. If pPreserveSource is false, pSource
+  // will be destroyed after successfully merged. Return false on error.
+  bool merge(Source &pSource, bool pPreserveSource = false);
+
+  inline BCCContext &getContext()
+  { return mContext; }
+  inline const BCCContext &getContext() const
+  { return mContext; }
+
+  inline llvm::Module &getModule()
+  { return *mModule;  }
+  inline const llvm::Module &getModule() const
+  { return *mModule;  }
+
+  // Get the "identifier" of the bitcode. This will return the value of pName
+  // when it's created using CreateFromBuffer and pPath if CreateFromFile().
+  const std::string &getIdentifier() const;
+
+  ~Source();
+};
+
+} // namespace bcc
+
+#endif // BCC_SOURCE_H
diff --git a/include/bcc/Support/CompilerConfig.h b/include/bcc/Support/CompilerConfig.h
new file mode 100644
index 0000000..fec645b
--- /dev/null
+++ b/include/bcc/Support/CompilerConfig.h
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_COMPILER_CONFIG_H
+#define BCC_SUPPORT_COMPILER_CONFIG_H
+
+#include <string>
+#include <vector>
+
+#include <llvm/ADT/Triple.h>
+#include <llvm/Support/CodeGen.h>
+#include <llvm/Target/TargetOptions.h>
+
+namespace llvm {
+
+class Target;
+
+} // end namespace llvm
+
+namespace bcc {
+
+class CompilerConfig {
+private:
+  //===--------------------------------------------------------------------===//
+  // Available Configurations
+  //===--------------------------------------------------------------------===//
+  std::string mTriple;
+
+  // Optional. If given, the name of the target CPU to generate code for.
+  std::string mCPU;
+
+  llvm::TargetOptions mTargetOpts;
+
+  llvm::CodeModel::Model mCodeModel;
+
+  llvm::CodeGenOpt::Level mOptLevel;
+
+  llvm::Reloc::Model mRelocModel;
+
+  // The list of target specific features to enable or disable -- this should
+  // be a list of strings starting with '+' (enable) or '-' (disable).
+  std::string mFeatureString;
+
+private:
+  //===--------------------------------------------------------------------===//
+  // These are generated by CompilerConfig during initialize().
+  //===--------------------------------------------------------------------===//
+  const llvm::Target *mTarget;
+  bool initializeTarget();
+
+  llvm::Triple::ArchType mArchType;
+  void initializeArch();
+
+public:
+  //===--------------------------------------------------------------------===//
+  // Getters
+  //===--------------------------------------------------------------------===//
+  inline const std::string &getTriple() const
+  { return mTriple; }
+
+  inline const std::string &getCPU() const
+  { return mCPU; }
+  inline void setCPU(const std::string &pCPU)
+  { mCPU = pCPU; }
+
+  inline const llvm::TargetOptions &getTargetOptions() const
+  { return mTargetOpts; }
+  inline llvm::TargetOptions &getTargetOptions()
+  { return mTargetOpts; }
+
+  inline llvm::CodeModel::Model getCodeModel() const
+  { return mCodeModel; }
+  inline void setCodeModel(llvm::CodeModel::Model pCodeMode)
+  { mCodeModel = pCodeMode; }
+
+  inline llvm::CodeGenOpt::Level getOptimizationLevel() const
+  { return mOptLevel; }
+  inline void setOptimizationLevel(llvm::CodeGenOpt::Level pOptLvl)
+  { mOptLevel = pOptLvl; }
+
+  inline llvm::Reloc::Model getRelocationModel() const
+  { return mRelocModel; }
+  inline void setRelocationModel(llvm::Reloc::Model pRelocModel)
+  { mRelocModel = pRelocModel; }
+
+  inline const llvm::Target *getTarget() const
+  { return mTarget; }
+
+  inline llvm::Triple::ArchType getArchType() const
+  { return mArchType; }
+
+  inline const std::string &getFeatureString() const
+  { return mFeatureString; }
+  void setFeatureString(const std::vector<std::string> &pAttrs);
+
+public:
+  CompilerConfig(const std::string &pTriple);
+
+  virtual ~CompilerConfig() { }
+};
+
+} // end namespace bcc
+
+#endif  // BCC_SUPPORT_COMPILER_CONFIG_H
diff --git a/include/bcc/Support/Disassembler.h b/include/bcc/Support/Disassembler.h
new file mode 100644
index 0000000..0ec0eed
--- /dev/null
+++ b/include/bcc/Support/Disassembler.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2011-2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_DISASSEMBLER_H
+#define BCC_SUPPORT_DISASSEMBLER_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+namespace llvm {
+  class raw_ostream;
+} // end namespace llvm
+
+namespace bcc {
+
+class OutputFile;
+
+enum DisassembleResult {
+  kDisassembleSuccess,
+  kDisassemblerNotAvailable,
+  kDisassembleInvalidOutput,
+  kDisassembleFailedPrepareOutput,
+  kDisassembleUnknownTarget,
+  kDisassembleFailedSetup,
+  kDisassembleOutOfMemory,
+  kDisassembleInvalidInstruction,
+};
+
+DisassembleResult Disassemble(llvm::raw_ostream &pOutput, const char *pTriple,
+                              const char *pFuncName, const uint8_t *pFunc,
+                              size_t pFuncSize);
+
+DisassembleResult Disassemble(OutputFile &pOutput, const char *pTriple,
+                              const char *pFuncName, const uint8_t *pFunc,
+                              size_t pFuncSize);
+
+} // end namespace bcc
+
+#endif // BCC_SUPPORT_DISASSEMBLER_H
diff --git a/include/bcc/Support/File.h b/include/bcc/Support/File.h
new file mode 100644
index 0000000..6367359
--- /dev/null
+++ b/include/bcc/Support/File.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_FILE_H
+#define BCC_SUPPORT_FILE_H
+
+#include "bcc/Support/FileBase.h"
+
+namespace bcc {
+
+template<enum FileBase::OpenModeEnum OpenMode>
+struct FileAttribute {
+  // The flags to the 2nd argument in ::open().
+  enum { kOpenFlags };
+
+  // Default value of LockMode.
+  enum { kDefaultLockMode };
+};
+
+// FileAttribute for accessing read-only file
+template<>
+struct FileAttribute<FileBase::kReadMode> {
+  enum { kOpenFlags       = O_RDONLY };
+  enum { kDefaultLockMode = FileBase::kReadLock };
+};
+
+// FileAttribute for accessing writable file
+template<>
+struct FileAttribute<FileBase::kWriteMode> {
+  enum { kOpenFlags       = O_RDWR | O_CREAT };
+  enum { kDefaultLockMode = FileBase::kWriteLock };
+};
+
+template<enum FileBase::OpenModeEnum OpenMode>
+class File : public FileBase {
+public:
+  File(const std::string &pFilename, unsigned pFlags)
+    : FileBase(pFilename, FileAttribute<OpenMode>::kOpenFlags, pFlags) { }
+
+  inline bool lock(enum LockModeEnum pMode = static_cast<enum LockModeEnum>(
+                      FileAttribute<OpenMode>::kDefaultLockMode),
+                   bool pNonblocking = true,
+                   unsigned pMaxRetry = FileBase::kDefaultMaxRetryLock,
+                   useconds_t pRetryInterval =
+                      FileBase::kDefaultRetryLockInterval) {
+    return FileBase::lock(pMode, pNonblocking, pMaxRetry, pRetryInterval);
+  }
+
+  inline android::FileMap *createMap(off_t pOffset, size_t pLength,
+                                     bool pIsReadOnly =
+                                        (OpenMode == FileBase::kReadMode)) {
+    return FileBase::createMap(pOffset, pLength, pIsReadOnly);
+  }
+};
+
+
+} // end namespace bcc
+
+#endif  // BCC_SUPPORT_FILE_H
diff --git a/include/bcc/Support/FileBase.h b/include/bcc/Support/FileBase.h
new file mode 100644
index 0000000..c201f97
--- /dev/null
+++ b/include/bcc/Support/FileBase.h
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_FILE_BASE_H
+#define BCC_SUPPORT_FILE_BASE_H
+
+#include <fcntl.h>
+
+#include <string>
+
+#include <llvm/Support/system_error.h>
+
+namespace android {
+  class FileMap;
+}
+
+namespace bcc {
+
+class FileBase {
+public:
+  enum OpenModeEnum {
+    kReadMode       = 1 << 0,
+    kWriteMode      = 1 << 1,
+    kReadWriteMode  = (kReadMode | kWriteMode),
+  };
+
+  enum FlagEnum {
+    // The openning file is a binary file.
+    kBinary = 1 << 0,
+
+    // The openning file will be truncated to length 0.
+    kTruncate = 1 << 1,
+
+    // The openning file will put its file pointer to the end of the file before
+    // each write.
+    kAppend = 1 << 2,
+  };
+
+  enum LockModeEnum {
+    // The shared resource behind the Stream locked in ReadLock mode can be
+    // locked by other processes at the same time.
+    kReadLock,
+
+    // The shared resource behind the Stream locked in WriteLock mode can only
+    // be locked by one process. It's exclusive. That is, the shared resource
+    // cannot have both ReadLock and WriteLock simultaneously.
+    kWriteLock
+  };
+
+  // Default configuration to the lock().
+  enum {
+    kDefaultMaxRetryLock = 4,
+    kDefaultRetryLockInterval = 200000UL,
+  };
+
+protected:
+  // Grant direct access of the internal file descriptor to the sub-class and
+  // error message such that they can implement their own I/O functionality.
+  int mFD;
+
+  llvm::error_code mError;
+
+private:
+  std::string mName;
+
+  // The 2nd argument to the POSIX open().
+  unsigned mOpenFlags;
+
+  // True true if we should call unlock() in destructor.
+  bool mShouldUnlock;
+
+  // Open mName with flag mOpenFlags (using POSIX open().)
+  bool open();
+
+  // Return true if mFD is the corresponded file descriptor to the file named
+  // mName on the filesystem. This check may returns failed, for example,
+  // someone re-create the file with the same name after we openning the file.
+  bool checkFileIntegrity();
+
+  inline bool reopen() {
+    // It's a private method, and all its callers are the few that can invoke it.
+    // That is, the pre-condition will be checked by the caller. Therefore, we don't
+    // need to check it again in reopen().
+    close();
+    return open();
+  }
+
+private:
+  FileBase(FileBase &); // Do not implement.
+  void operator=(const FileBase &); // Do not implement.
+
+protected:
+  // pOpenFlags is the 2nd argument to the POSIX open(). pFlags are the flags to
+  // FileBase. It's a bit set composed by the value defined in
+  // FileBase::FlagEnum.
+  FileBase(const std::string &pFilename, unsigned pOpenFlags, unsigned pFlags);
+
+  void detectError();
+
+public:
+  // Lock the file descriptor in given pMode. If pNonblocking is true, the lock
+  // request issued will return immediately when the shared resource is locked.
+  // In this case, it retries pMaxRetry times, each wait pRetryInterval (in
+  // usecs) before the previous retry getting done.
+  //
+  // Only file is allowed to use this API.
+  bool lock(enum LockModeEnum pMode, bool pNonblocking = true,
+            unsigned pMaxRetry = kDefaultMaxRetryLock,
+            useconds_t pRetryInterval = kDefaultRetryLockInterval);
+
+  void unlock();
+
+  // Map the file content to the memory.
+  //
+  // One who gets non-null android::FileMap returned from this API is responsible
+  // for destroying it after the use.
+  android::FileMap *createMap(off_t pOffset, size_t pLength, bool pIsReadOnly);
+
+  size_t getSize();
+
+  off_t seek(off_t pOffset);
+  off_t tell();
+
+  inline bool hasError() const
+  { return (mError.value() != llvm::errc::success); }
+
+  inline const llvm::error_code &getError() const
+  { return mError; }
+
+  // The return value of llvm::error_code::message() is obtained upon the call
+  // and is passed by value (that is, it's not a member of llvm::error_code.)
+  inline std::string getErrorMessage() const
+  { return mError.message(); }
+
+  inline const std::string &getName() const
+  { return mName; }
+
+  void close();
+
+  virtual ~FileBase();
+};
+
+} // end namespace bcc
+
+#endif  // BCC_SUPPORT_FILE_BASE_H
diff --git a/include/bcc/Support/FileMutex.h b/include/bcc/Support/FileMutex.h
new file mode 100644
index 0000000..15c9f01
--- /dev/null
+++ b/include/bcc/Support/FileMutex.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_FILE_MUTEX_H
+#define BCC_SUPPORT_FILE_MUTEX_H
+
+#include <string>
+
+#include "bcc/Support/FileBase.h"
+
+namespace bcc {
+
+template<enum FileBase::LockModeEnum LockMode>
+class FileMutex : public FileBase {
+public:
+  FileMutex(const std::string &pFileToLock)
+    : FileBase(pFileToLock + ".lock", O_RDONLY | O_CREAT, 0) { }
+
+  // Provide a lock() interface filled with default configuration.
+  inline bool lock(bool pNonblocking = true,
+                   unsigned pMaxRetry = FileBase::kDefaultMaxRetryLock,
+                   useconds_t pRetryInterval =
+                       FileBase::kDefaultRetryLockInterval) {
+    return FileBase::lock(LockMode, pNonblocking, pMaxRetry, pRetryInterval);
+  }
+};
+
+} // namespace bcc
+
+#endif  // BCC_SUPPORT_FILE_MUTEX_H
diff --git a/lib/Transforms/BCCTransforms.h b/include/bcc/Support/Initialization.h
similarity index 75%
rename from lib/Transforms/BCCTransforms.h
rename to include/bcc/Support/Initialization.h
index 1ac8174..521f082 100644
--- a/lib/Transforms/BCCTransforms.h
+++ b/include/bcc/Support/Initialization.h
@@ -14,11 +14,17 @@
  * limitations under the License.
  */
 
-#include "llvm/Pass.h"
+#ifndef BCC_SUPPORT_INITIALIZATION_H
+#define BCC_SUPPORT_INITIALIZATION_H
 
 namespace bcc {
 
-llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                          std::vector<uint32_t>& Signatures);
+namespace init {
 
-}  // namespace bcc
+void Initialize();
+
+} // end namespace init
+
+} // end namespace bcc
+
+#endif // BCC_SUPPORT_INITIALIZATION_H
diff --git a/lib/Transforms/BCCTransforms.h b/include/bcc/Support/InputFile.h
similarity index 60%
copy from lib/Transforms/BCCTransforms.h
copy to include/bcc/Support/InputFile.h
index 1ac8174..72abfd2 100644
--- a/lib/Transforms/BCCTransforms.h
+++ b/include/bcc/Support/InputFile.h
@@ -14,11 +14,22 @@
  * limitations under the License.
  */
 
-#include "llvm/Pass.h"
+#ifndef BCC_SUPPORT_INPUT_FILE_H
+#define BCC_SUPPORT_INPUT_FILE_H
+
+#include "bcc/Support/File.h"
+#include "bcc/Support/FileBase.h"
 
 namespace bcc {
 
-llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                          std::vector<uint32_t>& Signatures);
+class InputFile : public File<FileBase::kReadMode> {
+  typedef File<FileBase::kReadMode> super;
+public:
+  InputFile(const std::string &pFilename, unsigned pFlags = 0);
 
-}  // namespace bcc
+  ssize_t read(void *pBuf, size_t count);
+};
+
+} // end namespace bcc
+
+#endif  // BCC_SUPPORT_INPUT_FILE_H
diff --git a/include/bcc/Support/LinkerConfig.h b/include/bcc/Support/LinkerConfig.h
new file mode 100644
index 0000000..d1ab0b1
--- /dev/null
+++ b/include/bcc/Support/LinkerConfig.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_LINKER_CONFIG_H
+#define BCC_SUPPORT_LINKER_CONFIG_H
+
+#include <string>
+
+#include <mcld/MC/MCLDInfo.h>
+#include <mcld/Support/TargetRegistry.h>
+#include <mcld/LD/DiagnosticLineInfo.h>
+#include <mcld/LD/DiagnosticPrinter.h>
+
+namespace bcc {
+
+class LinkerConfig {
+private:
+  //===--------------------------------------------------------------------===//
+  // Available Configurations
+  //===--------------------------------------------------------------------===//
+  const std::string mTriple;
+  bool mShared;
+  std::string mSOName;
+
+private:
+  //===--------------------------------------------------------------------===//
+  // These are generated by LinkerConfig during initialize().
+  //===--------------------------------------------------------------------===//
+  const mcld::Target *mTarget;
+  bool initializeTarget();
+
+  mcld::MCLDInfo *mLDInfo;
+  bool initializeLDInfo();
+
+  mcld::DiagnosticLineInfo *mDiagLineInfo;
+  mcld::DiagnosticPrinter *mDiagPrinter;
+  bool initializeDiagnostic();
+
+public:
+  //===--------------------------------------------------------------------===//
+  // Getters
+  //===--------------------------------------------------------------------===//
+  inline const std::string &getTriple() const
+  { return mTriple; }
+
+  inline const mcld::Target *getTarget() const
+  { return mTarget; }
+
+  inline mcld::MCLDInfo* getLDInfo()
+  { return mLDInfo; }
+
+  inline const mcld::MCLDInfo* getLDInfo() const
+  { return mLDInfo; }
+
+  inline bool isShared() const
+  { return mShared; }
+
+  inline std::string getSOName() const
+  { return mSOName; }
+
+  void setShared(bool pEnable = true);
+
+  void setBsymbolic(bool pEnable = true);
+
+  void setSOName(const std::string &pSOName);
+
+  void setDyld(const std::string &pDyld);
+
+  void setSysRoot(const std::string &pSysRoot);
+
+  void addWrap(const std::string &pWrapSymbol);
+
+  void addPortable(const std::string &pPortableSymbol);
+
+  void addSearchDir(const std::string &pDir);
+
+public:
+  LinkerConfig(const std::string& pTriple);
+
+  virtual ~LinkerConfig();
+};
+
+} // end namespace bcc
+
+#endif // BCC_SUPPORT_LINKER_CONFIG_H
diff --git a/lib/Transforms/BCCTransforms.h b/include/bcc/Support/Log.h
similarity index 73%
copy from lib/Transforms/BCCTransforms.h
copy to include/bcc/Support/Log.h
index 1ac8174..f4079ef 100644
--- a/lib/Transforms/BCCTransforms.h
+++ b/include/bcc/Support/Log.h
@@ -14,11 +14,12 @@
  * limitations under the License.
  */
 
-#include "llvm/Pass.h"
+#ifndef BCC_SUPPORT_LOG_H
+#define BCC_SUPPORT_LOG_H
 
-namespace bcc {
+#ifndef LOG_TAG
+#   define LOG_TAG "bcc"
+#   include <cutils/log.h>
+#endif
 
-llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                          std::vector<uint32_t>& Signatures);
-
-}  // namespace bcc
+#endif // BCC_SUPPORT_LOG_H
diff --git a/include/bcc/Support/MemoryFactory.h b/include/bcc/Support/MemoryFactory.h
new file mode 100644
index 0000000..ffbf25e
--- /dev/null
+++ b/include/bcc/Support/MemoryFactory.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_MEMORY_FACTORY_H
+#define BCC_SUPPORT_MEMORY_FACTORY_H
+
+#include <mcld/Support/FileHandle.h>
+#include <mcld/Support/MemoryAreaFactory.h>
+
+namespace mcld {
+class MemoryArea;
+} // end namespace mcld
+
+namespace bcc {
+
+class MemoryFactory : public mcld::MemoryAreaFactory {
+public:
+  MemoryFactory() : mcld::MemoryAreaFactory(32) { }
+
+  ~MemoryFactory() { }
+
+  using mcld::MemoryAreaFactory::produce;
+
+  mcld::MemoryArea* produce(void *pMemBuffer, size_t pSize)
+  { return mcld::MemoryAreaFactory::create(pMemBuffer, pSize); }
+
+  mcld::MemoryArea* produce(int pFD)
+  { return mcld::MemoryAreaFactory::create(pFD, mcld::FileHandle::Unknown); }
+};
+
+} // end namespace bcc
+
+#endif // BCC_SUPPORT_MEMORY_FACTORY_H
diff --git a/include/bcc/Support/OutputFile.h b/include/bcc/Support/OutputFile.h
new file mode 100644
index 0000000..5e5c868
--- /dev/null
+++ b/include/bcc/Support/OutputFile.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_OUTPUT_FILE_H
+#define BCC_SUPPORT_OUTPUT_FILE_H
+
+#include "bcc/Support/File.h"
+#include "bcc/Support/FileBase.h"
+
+namespace llvm {
+  class raw_fd_ostream;
+}
+
+namespace bcc {
+
+class OutputFile : public File<FileBase::kWriteMode> {
+  typedef File<FileBase::kWriteMode> super;
+public:
+  // Generate a unique temporary filename from pFileTemplate and open it in
+  // an OutputFile returned. The filename will be pFileTemplate with
+  // a dot ('.') plus six random characters appended. Return NULL on error.
+  static OutputFile *CreateTemporary(const std::string &pFileTemplate,
+                                     unsigned pFlags);
+
+  OutputFile(const std::string &pFilename, unsigned pFlags = 0);
+
+  ssize_t write(const void *pBuf, size_t count);
+
+  void truncate();
+
+  // This is similar to the system call dup(). It creates a copy of the file
+  // descriptor it contains and wrap it in llvm::raw_fd_ostream object. It
+  // returns a non-NULL object if everything goes well and user should later
+  // use delete operator to destroy it by itself.
+  llvm::raw_fd_ostream *dup();
+};
+
+} // end namespace bcc
+
+#endif  // BCC_SUPPORT_OUTPUT_FILE_H
diff --git a/include/bcc/Support/Sha1Util.h b/include/bcc/Support/Sha1Util.h
new file mode 100644
index 0000000..b7712ed
--- /dev/null
+++ b/include/bcc/Support/Sha1Util.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_SHA1_UTIL_H
+#define BCC_SUPPORT_SHA1_UTIL_H
+
+#include <stdint.h>
+
+#include <cstddef>
+
+// This guard prevents system sha1.h (such as the one in bionic) has been
+// included before this header.
+#ifndef SHA1_DIGEST_LENGTH
+#define SHA1_DIGEST_LENGTH 20
+#endif
+
+namespace bcc {
+
+class Sha1Util {
+private:
+  Sha1Util(); // DISABLED.
+  Sha1Util(Sha1Util &); // DISABLED.
+
+public:
+  // Return true on success.
+  static bool GetSHA1DigestFromFile(uint8_t pResult[SHA1_DIGEST_LENGTH],
+                                    const char *pFilename);
+
+  // Return true on success.
+  static bool GetSHA1DigestFromBuffer(uint8_t pResult[SHA1_DIGEST_LENGTH],
+                                      const uint8_t *pData, size_t pSize);
+
+  // Useful function when passing buffer of type  "const char *."
+  static bool GetSHA1DigestFromBuffer(uint8_t pResult[SHA1_DIGEST_LENGTH],
+                                      const char *pData, size_t pSize) {
+    return GetSHA1DigestFromBuffer(pResult,
+                                   reinterpret_cast<const uint8_t*>(pData),
+                                   pSize);
+  }
+};
+
+} // end namespace bcc
+
+#endif // BCC_SUPPORT_SHA1_UTIL_H
diff --git a/include/bcc/Support/TargetCompilerConfigs.h b/include/bcc/Support/TargetCompilerConfigs.h
new file mode 100644
index 0000000..766765e
--- /dev/null
+++ b/include/bcc/Support/TargetCompilerConfigs.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_TARGET_COMPILER_CONFIGS_H
+#define BCC_SUPPORT_TARGET_COMPILER_CONFIGS_H
+
+#include "bcc/Config/Config.h"
+#include "bcc/Support/CompilerConfig.h"
+
+namespace bcc {
+
+//===----------------------------------------------------------------------===//
+// ARM
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_ARM_CODEGEN)
+class ARMCompilerConfig : public CompilerConfig {
+private:
+  bool mEnableNEON;
+
+  static void GetFeatureVector(std::vector<std::string> &pAttributes,
+                               bool pEnableNEON);
+
+public:
+  ARMCompilerConfig();
+
+  // Return true if config has been changed after returning from this function.
+  bool enableNEON(bool pEnable = true);
+};
+#endif // defined(PROVIDE_ARM_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// MIPS
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_MIPS_CODEGEN)
+class MipsCompilerConfig : public CompilerConfig {
+public:
+  MipsCompilerConfig() : CompilerConfig(DEFAULT_MIPS_TRIPLE_STRING) {}
+};
+#endif // defined(PROVIDE_MIPS_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// X86 and X86_64
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_X86_CODEGEN)
+class X86FamilyCompilerConfigBase : public CompilerConfig {
+protected:
+  X86FamilyCompilerConfigBase(const std::string &pTriple)
+    : CompilerConfig(pTriple) {
+    // Disable frame pointer elimination optimization on x86 family.
+    getTargetOptions().NoFramePointerElim = true;
+    return;
+  }
+};
+
+class X86_32CompilerConfig : public X86FamilyCompilerConfigBase {
+public:
+  X86_32CompilerConfig() :
+      X86FamilyCompilerConfigBase(DEFAULT_X86_TRIPLE_STRING) { }
+};
+
+class X86_64CompilerConfig : public X86FamilyCompilerConfigBase {
+public:
+  X86_64CompilerConfig() :
+      X86FamilyCompilerConfigBase(DEFAULT_X86_64_TRIPLE_STRING) {
+    setCodeModel(llvm::CodeModel::Medium);
+  }
+};
+#endif // defined(PROVIDE_X86_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// Default target
+//===----------------------------------------------------------------------===//
+class DefaultCompilerConfig : public
+#if defined(DEFAULT_ARM_CODEGEN)
+  ARMCompilerConfig
+#elif defined(DEFAULT_MIPS_CODEGEN)
+  MipsCompilerConfig
+#elif defined(DEFAULT_X86_CODEGEN)
+  X86_32CompilerConfig
+#elif defined(DEFAULT_X86_64_CODEGEN)
+  X86_64CompilerConfig
+#else
+#  error "Unsupported Default Target!"
+#endif
+{ };
+
+} // end namespace bcc
+
+#endif // BCC_SUPPORT_TARGET_COMPILER_CONFIGS_H
diff --git a/include/bcc/Support/TargetLinkerConfigs.h b/include/bcc/Support/TargetLinkerConfigs.h
new file mode 100644
index 0000000..c0b488b
--- /dev/null
+++ b/include/bcc/Support/TargetLinkerConfigs.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_SUPPORT_TARGET_LINKER_CONFIGS_H
+#define BCC_SUPPORT_TARGET_LINKER_CONFIGS_H
+
+#include <string>
+
+#include "bcc/Config/Config.h"
+#include "bcc/Support/LinkerConfig.h"
+
+namespace bcc {
+
+//===----------------------------------------------------------------------===//
+// ARM
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_ARM_CODEGEN)
+class ARMLinkerConfig : public LinkerConfig {
+public:
+  ARMLinkerConfig();
+};
+#endif // defined(PROVIDE_ARM_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// MIPS
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_MIPS_CODEGEN)
+class MipsLinkerConfig : public LinkerConfig {
+public:
+  MipsLinkerConfig();
+};
+#endif // defined(PROVIDE_MIPS_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// X86 and X86_64
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_X86_CODEGEN)
+class X86FamilyLinkerConfigBase : public LinkerConfig {
+public:
+  X86FamilyLinkerConfigBase(const std::string& pTriple);
+};
+
+class X86_32LinkerConfig : public X86FamilyLinkerConfigBase {
+public:
+  X86_32LinkerConfig();
+};
+
+class X86_64LinkerConfig : public X86FamilyLinkerConfigBase {
+public:
+  X86_64LinkerConfig();
+};
+#endif // defined(PROVIDE_X86_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// Default target
+//===----------------------------------------------------------------------===//
+class DefaultLinkerConfig : public
+#if defined (DEFAULT_ARM_CODEGEN)
+  ARMLinkerConfig
+#elif defined (DEFAULT_MIPS_CODEGEN)
+  MipsLinkerConfig
+#elif defined (DEFAULT_X86_CODEGEN)
+  X86_32LinkerConfig
+#elif defined (DEFAULT_X86_64_CODEGEN)
+  X86_64LinkerConfig
+#else
+#  error "Unsupported Default Target!"
+#endif
+{ };
+
+} // end namespace bcc
+
+#endif // BCC_SUPPORT_LINKER_CONFIG_H
diff --git a/include/bcc/bcc.h b/include/bcc/bcc.h
deleted file mode 100644
index 6843f03..0000000
--- a/include/bcc/bcc.h
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright (C) 2010-2012 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.
- */
-
-#ifndef ANDROID_BCC_BCC_H
-#define ANDROID_BCC_BCC_H
-
-#include <stddef.h>
-#include <stdint.h>
-
-/*-------------------------------------------------------------------------*/
-
-/* libbcc script opaque type */
-typedef struct BCCOpaqueScript *BCCScriptRef;
-
-
-/* Symbol lookup function type */
-typedef void *(*BCCSymbolLookupFn)(void *context, char const *symbolName);
-
-
-/* llvm::Module (see <llvm>/include/llvm-c/Core.h for details) */
-typedef struct LLVMOpaqueModule *LLVMModuleRef;
-
-
-/*-------------------------------------------------------------------------*/
-
-
-#define BCC_NO_ERROR          0x0000
-#define BCC_INVALID_ENUM      0x0500
-#define BCC_INVALID_OPERATION 0x0502
-#define BCC_INVALID_VALUE     0x0501
-#define BCC_OUT_OF_MEMORY     0x0505
-
-
-/*-------------------------------------------------------------------------*/
-
-
-/* Optional Flags for bccReadBC, bccReadFile, bccLinkBC, bccLinkFile */
-#define BCC_SKIP_DEP_SHA1 (1 << 0)
-
-
-/*-------------------------------------------------------------------------*/
-
-/*
- * Relocation model when prepare object, it provides 1-1 mapping to the enum
- * llvm::Reloc::Model in llvm/Support/CodeGen.h
- */
-typedef enum bccRelocModelEnum {
-  bccRelocDefault,  // Use default target-defined relocation model
-  bccRelocStatic,
-  bccRelocPIC,
-  bccRelocDynamicNoPIC
-} bccRelocModelEnum;
-
-/*-------------------------------------------------------------------------*/
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-BCCScriptRef bccCreateScript();
-
-void bccDisposeScript(BCCScriptRef script);
-
-int bccRegisterSymbolCallback(BCCScriptRef script,
-                              BCCSymbolLookupFn pFn,
-                              void *pContext);
-
-int bccGetError(BCCScriptRef script); /* deprecated */
-
-
-
-int bccReadBC(BCCScriptRef script,
-              char const *resName,
-              char const *bitcode,
-              size_t bitcodeSize,
-              unsigned long flags);
-
-int bccReadModule(BCCScriptRef script,
-                  char const *resName,
-                  LLVMModuleRef module,
-                  unsigned long flags);
-
-int bccReadFile(BCCScriptRef script,
-                char const *path,
-                unsigned long flags);
-
-int bccLinkBC(BCCScriptRef script,
-              char const *resName,
-              char const *bitcode,
-              size_t bitcodeSize,
-              unsigned long flags);
-
-int bccLinkFile(BCCScriptRef script,
-                char const *path,
-                unsigned long flags);
-
-void bccMarkExternalSymbol(BCCScriptRef script, char const *name);
-
-int bccPrepareRelocatable(BCCScriptRef script,
-                          char const *objPath,
-                          bccRelocModelEnum RelocModel,
-                          unsigned long flags);
-
-int bccPrepareSharedObject(BCCScriptRef script,
-                           char const *objPath,
-                           char const *dsoPath,
-                           unsigned long flags);
-
-int bccPrepareExecutable(BCCScriptRef script,
-                         char const *cacheDir,
-                         char const *cacheName,
-                         unsigned long flags);
-
-void *bccGetFuncAddr(BCCScriptRef script, char const *funcname);
-
-void bccGetExportVarList(BCCScriptRef script,
-                         size_t varListSize,
-                         void **varList);
-
-void bccGetExportFuncList(BCCScriptRef script,
-                          size_t funcListSize,
-                          void **funcList);
-
-void bccGetExportForEachList(BCCScriptRef script,
-                             size_t forEachListSize,
-                             void **forEachList);
-
-char const *bccGetBuildTime();
-
-char const *bccGetBuildRev();
-
-char const *bccGetBuildSHA1();
-
-#ifdef __cplusplus
-};
-#endif
-
-/*-------------------------------------------------------------------------*/
-
-#endif
diff --git a/include/bcc/bcc_assert.h b/include/bcc/bcc_assert.h
deleted file mode 100644
index 757961e..0000000
--- a/include/bcc/bcc_assert.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright 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.
- */
-
-#ifndef _FRAMEWORKS_COMPILE_LIBBCC_INCLUDE_BCC_BCC_ASSERT_H_  // NOLINT
-#define _FRAMEWORKS_COMPILE_LIBBCC_INCLUDE_BCC_BCC_ASSERT_H_
-
-#include "DebugHelper.h"
-
-#ifdef __cplusplus
-#include <cstdlib>
-#include <cstdio>
-#else
-#include <stdlib.h>
-#include <stdio.h>
-#endif  // __cplusplus
-
-#ifdef __DISABLE_ASSERTS
-#define bccAssert(v) do {} while (0)
-#else
-#define __ABORT_ON_FAILURES 1
-#define bccAssert(v)                                \
-  do {                                              \
-    if (!(v)) {                                     \
-      ALOGE("bccAssert failed at %s:%d - '%s'\n",   \
-          __FILE__, __LINE__, #v);                  \
-      if (__ABORT_ON_FAILURES) {                    \
-        abort();                                    \
-      }                                             \
-    }                                               \
-  } while (0)
-#endif  // __DISABLE_ASSERTS
-
-
-#endif  // _FRAMEWORKS_COMPILE_LIBBCC_INCLUDE_BCC_BCC_ASSERT_H_  NOLINT
diff --git a/include/bcc/bcc_mccache.h b/include/bcc/bcc_mccache.h
deleted file mode 100644
index a214831..0000000
--- a/include/bcc/bcc_mccache.h
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-#ifndef BCC_MCCACHE_H
-#define BCC_MCCACHE_H
-
-#include <stdint.h>
-#include <sys/types.h>
-
-/* BCC Cache File Magic Word */
-#define MCO_MAGIC "\0bcc"
-
-/* BCC Cache File Version, encoded in 4 bytes of ASCII */
-#define MCO_VERSION "001\0"
-
-/* BCC Cache Header Structure */
-struct MCO_Header {
-  /* magic and version */
-  uint8_t magic[4];
-  uint8_t version[4];
-
-  /* machine-dependent integer type size */
-  uint8_t endianness;
-  uint8_t sizeof_off_t;
-  uint8_t sizeof_size_t;
-  uint8_t sizeof_ptr_t;
-
-  /* string pool section */
-  off_t str_pool_offset;
-  size_t str_pool_size;
-
-  /* dependancy table */
-  off_t depend_tab_offset;
-  size_t depend_tab_size;
-
-  /* relocation table section */
-  off_t reloc_tab_offset;
-  size_t reloc_tab_size;
-
-  /* pragma list section */
-  off_t pragma_list_offset;
-  size_t pragma_list_size;
-
-  /* function table */
-  off_t func_table_offset;
-  size_t func_table_size;
-
-  /* function table */
-  off_t object_slot_list_offset;
-  size_t object_slot_list_size;
-
-  /* export variable name list section */
-  off_t export_var_name_list_offset;
-  size_t export_var_name_list_size;
-
-  /* export function name list section */
-  off_t export_func_name_list_offset;
-  size_t export_func_name_list_size;
-
-  /* dirty hack for libRS */
-  /* TODO: This should be removed in the future */
-  uint32_t libRS_threadable;
-
-  /* export foreach list section */
-  off_t export_foreach_name_list_offset;
-  size_t export_foreach_name_list_size;
-};
-
-struct MCO_String {
-  size_t length; /* String length, without ending '\0' */
-  off_t offset; /* Note: Offset related to string_pool_offset. */
-};
-
-struct MCO_StringPool {
-  size_t count;
-  struct MCO_String list[];
-};
-
-enum MCO_ResourceType {
-  BCC_APK_RESOURCE = 0,
-  BCC_FILE_RESOURCE = 1,
-};
-
-struct MCO_Dependency {
-  size_t res_name_strp_index;
-  uint32_t res_type; /* BCC_APK_RESOURCE or BCC_FILE_RESOURCE */
-  unsigned char sha1[20];
-};
-
-struct MCO_DependencyTable {
-  size_t count;
-  struct MCO_Dependency table[];
-};
-
-struct MCO_ExportVarList {
-  size_t count;
-  void *cached_addr_list[];
-};
-
-struct MCO_ExportFuncList {
-  size_t count;
-  void *cached_addr_list[];
-};
-
-struct MCO_ExportForEachList {
-  size_t count;
-  void *cached_addr_list[];
-};
-
-struct MCO_Pragma {
-  size_t key_strp_index;
-  size_t value_strp_index;
-};
-
-struct MCO_PragmaList {
-  size_t count;
-  struct MCO_Pragma list[];
-};
-
-struct MCO_ObjectSlotList {
-  size_t count;
-  uint32_t object_slot_list[];
-};
-
-struct MCO_FuncInfo {
-  size_t name_strp_index;
-  void *cached_addr;
-  size_t size;
-};
-
-struct MCO_FuncTable {
-  size_t count;
-  struct MCO_FuncInfo table[];
-};
-
-struct MCO_String_Ptr {
-  size_t count;
-  size_t strp_indexs[];
-};
-
-
-#endif /* BCC_MCCACHE_H */
diff --git a/lib/AndroidBitcode/ABCCompilerDriver.cpp b/lib/AndroidBitcode/ABCCompilerDriver.cpp
new file mode 100644
index 0000000..84b6583
--- /dev/null
+++ b/lib/AndroidBitcode/ABCCompilerDriver.cpp
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2012, 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 "bcc/AndroidBitcode/ABCCompilerDriver.h"
+
+#include <llvm/Module.h>
+#include <llvm/Support/MemoryBuffer.h>
+#include <llvm/Support/raw_ostream.h>
+
+#include "bcc/Config/Config.h"
+#include "bcc/Script.h"
+#include "bcc/Source.h"
+#include "bcc/Support/CompilerConfig.h"
+#include "bcc/Support/LinkerConfig.h"
+#include "bcc/Support/Log.h"
+#include "bcc/Support/OutputFile.h"
+#include "bcc/Support/TargetLinkerConfigs.h"
+#include "bcc/Support/TargetCompilerConfigs.h"
+
+#include "mcld/Config/Config.h"
+
+namespace bcc {
+
+ABCCompilerDriver::ABCCompilerDriver(const std::string &pTriple,
+                                     const std::string &pAndroidSysroot)
+  : mContext(), mCompiler(), mLinker(),
+    mCompilerConfig(NULL), mLinkerConfig(NULL),
+    mTriple(pTriple), mAndroidSysroot(pAndroidSysroot) {
+}
+
+ABCCompilerDriver::~ABCCompilerDriver() {
+  delete mCompilerConfig;
+  delete mLinkerConfig;
+}
+
+bool ABCCompilerDriver::configCompiler() {
+  if (mCompilerConfig != NULL) {
+    return true;
+  }
+
+  mCompilerConfig = new (std::nothrow) CompilerConfig(mTriple);
+  if (mCompilerConfig == NULL) {
+    ALOGE("Out of memory when create the compiler configuration!");
+    return false;
+  }
+
+  // Set PIC mode for relocatables.
+  mCompilerConfig->setRelocationModel(llvm::Reloc::PIC_);
+
+  // Set optimization level to -O1.
+  mCompilerConfig->setOptimizationLevel(llvm::CodeGenOpt::Less);
+
+  Compiler::ErrorCode result = mCompiler.config(*mCompilerConfig);
+
+  if (result != Compiler::kSuccess) {
+    ALOGE("Failed to configure the compiler! (detail: %s)",
+          Compiler::GetErrorString(result));
+    return false;
+  }
+
+  return true;
+}
+
+bool ABCCompilerDriver::configLinker() {
+  if (mLinkerConfig != NULL) {
+    return true;
+  }
+
+  mLinkerConfig = new (std::nothrow) LinkerConfig(mTriple);
+  if (mLinkerConfig == NULL) {
+    ALOGE("Out of memory when create the linker configuration!");
+    return false;
+  }
+
+  // FIXME: how can we get the soname if input/output is file descriptor?
+  mLinkerConfig->setSOName("");
+
+  mLinkerConfig->setDyld("/system/bin/linker");
+  mLinkerConfig->setSysRoot(mAndroidSysroot);
+  mLinkerConfig->addSearchDir("=/system/lib");
+
+  // Add non-portable function list. For each function X, linker will rename
+  // it to X_portable. And X_portable" is implemented in libportable to solve
+  // portable issues.
+  mLinkerConfig->addPortable("stat");
+  mLinkerConfig->addPortable("fstat");
+  mLinkerConfig->addPortable("lstat");
+  mLinkerConfig->addPortable("fstatat");
+  mLinkerConfig->addPortable("socket");
+  mLinkerConfig->addPortable("setsockopt");
+  mLinkerConfig->addPortable("getsockopt");
+  mLinkerConfig->addPortable("epoll_event");
+  mLinkerConfig->addPortable("epoll_ctl");
+  mLinkerConfig->addPortable("epoll_wait");
+
+  // -shared
+  mLinkerConfig->setShared(true);
+
+  // -Bsymbolic.
+  mLinkerConfig->setBsymbolic(true);
+
+  // Config the linker.
+  Linker::ErrorCode result = mLinker.config(*mLinkerConfig);
+  if (result != Linker::kSuccess) {
+    ALOGE("Failed to configure the linker! (%s)",
+          Linker::GetErrorString(result));
+    return false;
+  }
+
+  return true;
+}
+
+//------------------------------------------------------------------------------
+
+Script *ABCCompilerDriver::prepareScript(int pInputFd) {
+  Source *source = Source::CreateFromFd(mContext, pInputFd);
+  if (source == NULL) {
+    ALOGE("Failed to load LLVM module from file descriptor `%d'", pInputFd);
+    return NULL;
+  }
+
+  Script *script = new (std::nothrow) Script(*source);
+  if (script == NULL) {
+    ALOGE("Out of memory when create script for file descriptor `%d'!",
+          pInputFd);
+    delete source;
+    return NULL;
+  }
+
+  return script;
+}
+
+bool ABCCompilerDriver::compile(Script &pScript, llvm::raw_ostream &pOutput) {
+  // Config the compiler.
+  if (!configCompiler()) {
+    return false;
+  }
+
+  // Run the compiler.
+  Compiler::ErrorCode result = mCompiler.compile(pScript, pOutput);
+  if (result != Compiler::kSuccess) {
+    ALOGE("Fatal error during compilation (%s)!",
+          Compiler::GetErrorString(result));
+    return false;
+  }
+
+  return true;
+}
+
+bool ABCCompilerDriver::link(const Script &pScript,
+                             const std::string &input_relocatable,
+                             int pOutputFd) {
+  // Config the linker.
+  if (!configLinker()) {
+    return false;
+  }
+
+  // Prepare output file.
+  Linker::ErrorCode result = mLinker.setOutput(pOutputFd);
+
+  if (result != Linker::kSuccess) {
+    ALOGE("Failed to open the output file! (file descriptor `%d': %s)",
+          pOutputFd, Linker::GetErrorString(result));
+    return false;
+  }
+
+  mLinker.addObject(mAndroidSysroot + "/system/lib/crtbegin_so.o");
+
+  // Prepare the relocatables.
+  //
+  // FIXME: Ugly const_cast here.
+  mLinker.addObject(const_cast<char *>(input_relocatable.data()),
+                    input_relocatable.size());
+
+  // Read dependent library list.
+  const Source &source = pScript.getSource();
+  for (llvm::Module::lib_iterator lib_iter = source.getModule().lib_begin(),
+          lib_end = source.getModule().lib_end(); lib_iter != lib_end;
+       ++lib_iter) {
+    mLinker.addNameSpec(*lib_iter);
+  }
+
+  // TODO: Refactor libbcc/runtime/ to libcompilerRT.so and use it.
+  mLinker.addNameSpec("bcc");
+
+  mLinker.addObject(mAndroidSysroot + "/system/lib/crtend_so.o");
+
+  // Perform linking.
+  result = mLinker.link();
+  if (result != Linker::kSuccess) {
+    ALOGE("Failed to link the shared object (detail: %s)",
+          Linker::GetErrorString(result));
+    return false;
+  }
+
+  return true;
+}
+
+//------------------------------------------------------------------------------
+
+bool ABCCompilerDriver::build(int pInputFd, int pOutputFd) {
+  //===--------------------------------------------------------------------===//
+  // Prepare the input.
+  //===--------------------------------------------------------------------===//
+  Script *script = prepareScript(pInputFd);
+  if (script == NULL) {
+    return false;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Prepare the output.
+  //===--------------------------------------------------------------------===//
+  std::string output_relocatable;
+  llvm::raw_ostream *output =
+      new (std::nothrow) llvm::raw_string_ostream(output_relocatable);
+  if (output == NULL) {
+    ALOGE("Failed to prepare the output for compile the input from %d into "
+          "relocatable object!", pInputFd);
+    delete script;
+    return false;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Compile.
+  //===--------------------------------------------------------------------===//
+  if (!compile(*script, *output)) {
+    delete output;
+    delete script;
+    return false;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Close the output.
+  //===--------------------------------------------------------------------===//
+  delete output;
+
+  //===--------------------------------------------------------------------===//
+  // Link.
+  //===--------------------------------------------------------------------===//
+  if (!link(*script, output_relocatable, pOutputFd)) {
+    delete script;
+    return false;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Clean up.
+  //===--------------------------------------------------------------------===//
+  delete script;
+
+  return true;
+}
+
+} // namespace bcc
diff --git a/lib/AndroidBitcode/Android.mk b/lib/AndroidBitcode/Android.mk
new file mode 100644
index 0000000..1abc7b2
--- /dev/null
+++ b/lib/AndroidBitcode/Android.mk
@@ -0,0 +1,59 @@
+#
+# Copyright (C) 2012 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.
+#
+#
+
+LOCAL_PATH := $(call my-dir)
+
+#=====================================================================
+# Common: libbccAndroidBitcode
+#=====================================================================
+
+libbcc_androidbitcode_SRC_FILES := \
+  ABCCompilerDriver.cpp
+
+#=====================================================================
+# Device Static Library: libbccAndroidBitcode
+#=====================================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libbccAndroidBitcode
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+
+LOCAL_SRC_FILES := $(libbcc_androidbitcode_SRC_FILES)
+
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_DEVICE_BUILD_MK)
+include $(BUILD_STATIC_LIBRARY)
+
+#=====================================================================
+# Host Static Library: libbccAndroidBitcode
+#=====================================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libbccAndroidBitcode
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+
+LOCAL_SRC_FILES := $(libbcc_androidbitcode_SRC_FILES)
+
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_HOST_BUILD_MK)
+include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/lib/Core/Android.mk b/lib/Core/Android.mk
new file mode 100644
index 0000000..1d5d5af
--- /dev/null
+++ b/lib/Core/Android.mk
@@ -0,0 +1,67 @@
+#
+# Copyright (C) 2012 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.
+#
+#
+
+LOCAL_PATH := $(call my-dir)
+
+#=====================================================================
+# Common: libbccCore
+#=====================================================================
+
+libbcc_core_SRC_FILES := \
+  BCCContext.cpp \
+  BCCContextImpl.cpp \
+  Compiler.cpp \
+  Linker.cpp \
+  Script.cpp \
+  Source.cpp
+
+#=====================================================================
+# Device Static Library: libbccCore
+#=====================================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libbccCore
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+
+LOCAL_SRC_FILES := $(libbcc_core_SRC_FILES)
+
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_DEVICE_BUILD_MK)
+#include $(LLVM_DEVICE_BUILD_MK)
+include $(BUILD_STATIC_LIBRARY)
+
+
+#=====================================================================
+# Host Static Library: libbccCore
+#=====================================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libbccCore
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+
+LOCAL_SRC_FILES := $(libbcc_core_SRC_FILES)
+
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_HOST_BUILD_MK)
+#include $(LLVM_HOST_BUILD_MK)
+include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/lib/Core/BCCContext.cpp b/lib/Core/BCCContext.cpp
new file mode 100644
index 0000000..4edccaa
--- /dev/null
+++ b/lib/Core/BCCContext.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2012, 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 "bcc/BCCContext.h"
+
+#include <new>
+
+#include "bcc/Source.h"
+#include "bcc/Support/Log.h"
+
+#include "BCCContextImpl.h"
+
+using namespace bcc;
+
+static BCCContext *GlobalContext = NULL;
+
+BCCContext *BCCContext::GetOrCreateGlobalContext() {
+  if (GlobalContext == NULL) {
+    GlobalContext = new (std::nothrow) BCCContext();
+    if (GlobalContext == NULL) {
+      ALOGE("Out of memory when allocating global BCCContext!");
+    }
+  }
+  return GlobalContext;
+}
+
+void BCCContext::DestroyGlobalContext() {
+  delete GlobalContext;
+  GlobalContext = NULL;
+}
+
+BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) { }
+
+BCCContext::~BCCContext() {
+  delete mImpl;
+  if (this == GlobalContext) {
+    // We're deleting the context returned from GetOrCreateGlobalContext().
+    // Reset the GlobalContext.
+    GlobalContext = NULL;
+  }
+}
+
+void BCCContext::addSource(Source &pSource)
+{ mImpl->mOwnSources.insert(&pSource); }
+
+void BCCContext::removeSource(Source &pSource)
+{ mImpl->mOwnSources.erase(&pSource); }
+
+llvm::LLVMContext &BCCContext::getLLVMContext()
+{ return mImpl->mLLVMContext; }
+
+const llvm::LLVMContext &BCCContext::getLLVMContext() const
+{ return mImpl->mLLVMContext; }
diff --git a/lib/Core/BCCContextImpl.cpp b/lib/Core/BCCContextImpl.cpp
new file mode 100644
index 0000000..1278d97
--- /dev/null
+++ b/lib/Core/BCCContextImpl.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2012, 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 "BCCContextImpl.h"
+
+#include <vector>
+
+#include <llvm/ADT/STLExtras.h>
+
+#include "bcc/Source.h"
+
+using namespace bcc;
+
+BCCContextImpl::~BCCContextImpl() {
+  // Another temporary container is needed to store the Source objects that we
+  // are going to destroy. Since the destruction of Source object will call
+  // removeSource() and change the content of OwnSources.
+  std::vector<Source *> Sources(mOwnSources.begin(), mOwnSources.end());
+  llvm::DeleteContainerPointers(Sources);
+}
diff --git a/lib/Core/BCCContextImpl.h b/lib/Core/BCCContextImpl.h
new file mode 100644
index 0000000..a63af49
--- /dev/null
+++ b/lib/Core/BCCContextImpl.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_CORE_CONTEXT_IMPL_H
+#define BCC_CORE_CONTEXT_IMPL_H
+
+#include <llvm/ADT/SmallPtrSet.h>
+#include <llvm/LLVMContext.h>
+
+namespace bcc {
+
+class BCCContext;
+class Source;
+
+/*
+ * class BCCContextImpl contains the implementation of BCCContext.
+ */
+class BCCContextImpl {
+public:
+  llvm::LLVMContext mLLVMContext;
+
+  // The set of sources that initialized in this context. They will be destroyed
+  // automatically when this context is gone.
+  llvm::SmallPtrSet<Source *, 2> mOwnSources;
+
+  BCCContextImpl(BCCContext &pContext) { }
+  ~BCCContextImpl();
+};
+
+} // namespace bcc
+
+#endif  // BCC_CORE_CONTEXT_IMPL_H
diff --git a/lib/Core/Compiler.cpp b/lib/Core/Compiler.cpp
new file mode 100644
index 0000000..c229098
--- /dev/null
+++ b/lib/Core/Compiler.cpp
@@ -0,0 +1,382 @@
+/*
+ * Copyright 2010-2012, 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 "bcc/Compiler.h"
+
+#include <llvm/Analysis/Passes.h>
+#include <llvm/CodeGen/RegAllocRegistry.h>
+#include <llvm/Module.h>
+#include <llvm/PassManager.h>
+#include <llvm/Support/TargetRegistry.h>
+#include <llvm/Support/raw_ostream.h>
+#include <llvm/Target/TargetData.h>
+#include <llvm/Target/TargetMachine.h>
+#include <llvm/Transforms/IPO.h>
+#include <llvm/Transforms/Scalar.h>
+
+#include "bcc/Script.h"
+#include "bcc/Source.h"
+#include "bcc/Support/CompilerConfig.h"
+#include "bcc/Support/Log.h"
+#include "bcc/Support/OutputFile.h"
+
+using namespace bcc;
+
+const char *Compiler::GetErrorString(enum ErrorCode pErrCode) {
+  static const char *ErrorString[] = {
+    /* kSuccess */
+    "Successfully compiled.",
+    /* kInvalidConfigNoTarget */
+    "Invalid compiler config supplied (getTarget() returns NULL.) "
+    "(missing call to CompilerConfig::initialize()?)",
+    /* kErrCreateTargetMachine */
+    "Failed to create llvm::TargetMachine.",
+    /* kErrSwitchTargetMachine */
+    "Failed to switch llvm::TargetMachine.",
+    /* kErrNoTargetMachine */
+    "Failed to compile the script since there's no available TargetMachine."
+    " (missing call to Compiler::config()?)",
+    /* kErrTargetDataNoMemory */
+    "Out of memory when create TargetData during compilation.",
+    /* kErrMaterialization */
+    "Failed to materialize the module.",
+    /* kErrInvalidOutputFileState */
+    "Supplied output file was invalid (in the error state.)",
+    /* kErrPrepareOutput */
+    "Failed to prepare file for output.",
+    /* kPrepareCodeGenPass */
+    "Failed to construct pass list for code-generation.",
+
+    /* kErrHookBeforeAddLTOPasses */
+    "Error occurred during beforeAddLTOPasses() in subclass.",
+    /* kErrHookAfterAddLTOPasses */
+    "Error occurred during afterAddLTOPasses() in subclass.",
+    /* kErrHookBeforeExecuteLTOPasses */
+    "Error occurred during beforeExecuteLTOPasses() in subclass.",
+    /* kErrHookAfterExecuteLTOPasses */
+    "Error occurred during afterExecuteLTOPasses() in subclass.",
+
+    /* kErrHookBeforeAddCodeGenPasses */
+    "Error occurred during beforeAddCodeGenPasses() in subclass.",
+    /* kErrHookAfterAddCodeGenPasses */
+    "Error occurred during afterAddCodeGenPasses() in subclass.",
+    /* kErrHookBeforeExecuteCodeGenPasses */
+    "Error occurred during beforeExecuteCodeGenPasses() in subclass.",
+    /* kErrHookAfterExecuteCodeGenPasses */
+    "Error occurred during afterExecuteCodeGenPasses() in subclass.",
+
+    /* kMaxErrorCode */
+    "(Unknown error code)"
+  };
+
+  if (pErrCode > kMaxErrorCode) {
+    pErrCode = kMaxErrorCode;
+  }
+
+  return ErrorString[ static_cast<size_t>(pErrCode) ];
+}
+
+//===----------------------------------------------------------------------===//
+// Instance Methods
+//===----------------------------------------------------------------------===//
+Compiler::Compiler() : mTarget(NULL), mEnableLTO(true) {
+  return;
+}
+
+Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(NULL),
+                                                    mEnableLTO(true) {
+  const std::string &triple = pConfig.getTriple();
+
+  enum ErrorCode err = config(pConfig);
+  if (err != kSuccess) {
+    ALOGE("%s (%s, features: %s)", GetErrorString(err),
+          triple.c_str(), pConfig.getFeatureString().c_str());
+    return;
+  }
+
+  return;
+}
+
+enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) {
+  if (pConfig.getTarget() == NULL) {
+    return kInvalidConfigNoTarget;
+  }
+
+  llvm::TargetMachine *new_target =
+      (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(),
+                                                 pConfig.getCPU(),
+                                                 pConfig.getFeatureString(),
+                                                 pConfig.getTargetOptions(),
+                                                 pConfig.getRelocationModel(),
+                                                 pConfig.getCodeModel(),
+                                                 pConfig.getOptimizationLevel());
+
+  if (new_target == NULL) {
+    return ((mTarget != NULL) ? kErrSwitchTargetMachine :
+                                kErrCreateTargetMachine);
+  }
+
+  // Replace the old TargetMachine.
+  delete mTarget;
+  mTarget = new_target;
+
+  // Adjust register allocation policy according to the optimization level.
+  //  createFastRegisterAllocator: fast but bad quality
+  //  createLinearScanRegisterAllocator: not so fast but good quality
+  if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) {
+    llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator);
+  } else {
+    llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator);
+  }
+
+  // Relax all machine instructions.
+  mTarget->setMCRelaxAll(true);
+
+  return kSuccess;
+}
+
+Compiler::~Compiler() {
+  delete mTarget;
+}
+
+enum Compiler::ErrorCode Compiler::runLTO(Script &pScript) {
+  llvm::TargetData *target_data = NULL;
+
+  // Pass manager for link-time optimization
+  llvm::PassManager lto_passes;
+
+  // Prepare TargetData target data from Module
+  target_data = new (std::nothrow) llvm::TargetData(*mTarget->getTargetData());
+  if (target_data == NULL) {
+    return kErrTargetDataNoMemory;
+  }
+
+  // Add TargetData to the pass manager.
+  lto_passes.add(target_data);
+
+  // Invokde "beforeAddLTOPasses" before adding the first pass.
+  if (!beforeAddLTOPasses(pScript, lto_passes)) {
+    return kErrHookBeforeAddLTOPasses;
+  }
+
+  // We now create passes list performing LTO. These are copied from
+  // (including comments) llvm::PassManagerBuilder::populateLTOPassManager().
+  // Only a subset of these LTO passes are enabled in optimization level 0 as
+  // they interfere with interactive debugging.
+  //
+  // FIXME: Figure out which passes (if any) makes sense for levels 1 and 2.
+  //if ( != llvm::CodeGenOpt::None) {
+  if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) {
+    lto_passes.add(llvm::createGlobalOptimizerPass());
+    lto_passes.add(llvm::createConstantMergePass());
+  } else {
+    // Propagate constants at call sites into the functions they call. This
+    // opens opportunities for globalopt (and inlining) by substituting
+    // function pointers passed as arguments to direct uses of functions.
+    lto_passes.add(llvm::createIPSCCPPass());
+
+    // Now that we internalized some globals, see if we can hack on them!
+    lto_passes.add(llvm::createGlobalOptimizerPass());
+
+    // Linking modules together can lead to duplicated global constants, only
+    // keep one copy of each constant...
+    lto_passes.add(llvm::createConstantMergePass());
+
+    // Remove unused arguments from functions...
+    lto_passes.add(llvm::createDeadArgEliminationPass());
+
+    // Reduce the code after globalopt and ipsccp. Both can open up
+    // significant simplification opportunities, and both can propagate
+    // functions through function pointers. When this happens, we often have
+    // to resolve varargs calls, etc, so let instcombine do this.
+    lto_passes.add(llvm::createInstructionCombiningPass());
+
+    // Inline small functions
+    lto_passes.add(llvm::createFunctionInliningPass());
+
+    // Remove dead EH info.
+    lto_passes.add(llvm::createPruneEHPass());
+
+    // Internalize the globals again after inlining
+    lto_passes.add(llvm::createGlobalOptimizerPass());
+
+    // Remove dead functions.
+    lto_passes.add(llvm::createGlobalDCEPass());
+
+    // If we didn't decide to inline a function, check to see if we can
+    // transform it to pass arguments by value instead of by reference.
+    lto_passes.add(llvm::createArgumentPromotionPass());
+
+    // The IPO passes may leave cruft around.  Clean up after them.
+    lto_passes.add(llvm::createInstructionCombiningPass());
+    lto_passes.add(llvm::createJumpThreadingPass());
+
+    // Break up allocas
+    lto_passes.add(llvm::createScalarReplAggregatesPass());
+
+    // Run a few AA driven optimizations here and now, to cleanup the code.
+    lto_passes.add(llvm::createFunctionAttrsPass());  // Add nocapture.
+    lto_passes.add(llvm::createGlobalsModRefPass());  // IP alias analysis.
+
+    // Hoist loop invariants.
+    lto_passes.add(llvm::createLICMPass());
+
+    // Remove redundancies.
+    lto_passes.add(llvm::createGVNPass());
+
+    // Remove dead memcpys.
+    lto_passes.add(llvm::createMemCpyOptPass());
+
+    // Nuke dead stores.
+    lto_passes.add(llvm::createDeadStoreEliminationPass());
+
+    // Cleanup and simplify the code after the scalar optimizations.
+    lto_passes.add(llvm::createInstructionCombiningPass());
+
+    lto_passes.add(llvm::createJumpThreadingPass());
+
+    // Delete basic blocks, which optimization passes may have killed.
+    lto_passes.add(llvm::createCFGSimplificationPass());
+
+    // Now that we have optimized the program, discard unreachable functions.
+    lto_passes.add(llvm::createGlobalDCEPass());
+  }
+
+  // Invokde "afterAddLTOPasses" after pass manager finished its
+  // construction.
+  if (!afterAddLTOPasses(pScript, lto_passes)) {
+    return kErrHookAfterAddLTOPasses;
+  }
+
+  // Invokde "beforeExecuteLTOPasses" before executing the passes.
+  if (!beforeExecuteLTOPasses(pScript, lto_passes)) {
+    return kErrHookBeforeExecuteLTOPasses;
+  }
+
+  lto_passes.run(pScript.getSource().getModule());
+
+  // Invokde "afterExecuteLTOPasses" before returning.
+  if (!afterExecuteLTOPasses(pScript)) {
+    return kErrHookAfterExecuteLTOPasses;
+  }
+
+  return kSuccess;
+}
+
+enum Compiler::ErrorCode Compiler::runCodeGen(Script &pScript,
+                                              llvm::raw_ostream &pResult) {
+  llvm::TargetData *target_data;
+  llvm::MCContext *mc_context = NULL;
+
+  // Create pass manager for MC code generation.
+  llvm::PassManager codegen_passes;
+
+  // Prepare TargetData target data from Module
+  target_data = new (std::nothrow) llvm::TargetData(*mTarget->getTargetData());
+  if (target_data == NULL) {
+    return kErrTargetDataNoMemory;
+  }
+
+  // Add TargetData to the pass manager.
+  codegen_passes.add(target_data);
+
+  // Invokde "beforeAddCodeGenPasses" before adding the first pass.
+  if (!beforeAddCodeGenPasses(pScript, codegen_passes)) {
+    return kErrHookBeforeAddCodeGenPasses;
+  }
+
+  // Add passes to the pass manager to emit machine code through MC layer.
+  if (mTarget->addPassesToEmitMC(codegen_passes, mc_context, pResult,
+                                 /* DisableVerify */false)) {
+    return kPrepareCodeGenPass;
+  }
+
+  // Invokde "afterAddCodeGenPasses" after pass manager finished its
+  // construction.
+  if (!afterAddCodeGenPasses(pScript, codegen_passes)) {
+    return kErrHookAfterAddCodeGenPasses;
+  }
+
+  // Invokde "beforeExecuteCodeGenPasses" before executing the passes.
+  if (!beforeExecuteCodeGenPasses(pScript, codegen_passes)) {
+    return kErrHookBeforeExecuteCodeGenPasses;
+  }
+
+  // Execute the pass.
+  codegen_passes.run(pScript.getSource().getModule());
+
+  // Invokde "afterExecuteCodeGenPasses" before returning.
+  if (!afterExecuteCodeGenPasses(pScript)) {
+    return kErrHookAfterExecuteCodeGenPasses;
+  }
+
+  return kSuccess;
+}
+
+enum Compiler::ErrorCode Compiler::compile(Script &pScript,
+                                           llvm::raw_ostream &pResult) {
+  llvm::Module &module = pScript.getSource().getModule();
+  enum ErrorCode err;
+
+  if (mTarget == NULL) {
+    return kErrNoTargetMachine;
+  }
+
+  // Materialize the bitcode module.
+  if (module.getMaterializer() != NULL) {
+    std::string error;
+    // A module with non-null materializer means that it is a lazy-load module.
+    // Materialize it now via invoking MaterializeAllPermanently(). This
+    // function returns false when the materialization is successful.
+    if (module.MaterializeAllPermanently(&error)) {
+      ALOGE("Failed to materialize the module `%s'! (%s)",
+            module.getModuleIdentifier().c_str(), error.c_str());
+      return kErrMaterialization;
+    }
+  }
+
+  if (mEnableLTO && ((err = runLTO(pScript)) != kSuccess)) {
+    return err;
+  }
+
+  if ((err = runCodeGen(pScript, pResult)) != kSuccess) {
+    return err;
+  }
+
+  return kSuccess;
+}
+
+enum Compiler::ErrorCode Compiler::compile(Script &pScript,
+                                           OutputFile &pResult) {
+  // Check the state of the specified output file.
+  if (pResult.hasError()) {
+    return kErrInvalidOutputFileState;
+  }
+
+  // Open the output file decorated in llvm::raw_ostream.
+  llvm::raw_ostream *out = pResult.dup();
+  if (out == NULL) {
+    return kErrPrepareOutput;
+  }
+
+  // Delegate the request.
+  enum Compiler::ErrorCode err = compile(pScript, *out);
+
+  // Close the output before return.
+  delete out;
+
+  return err;
+}
diff --git a/lib/Core/Linker.cpp b/lib/Core/Linker.cpp
new file mode 100644
index 0000000..322e5b2
--- /dev/null
+++ b/lib/Core/Linker.cpp
@@ -0,0 +1,387 @@
+/*
+ * Copyright 2010-2012, 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 "bcc/Linker.h"
+#include "bcc/Support/LinkerConfig.h"
+#include "bcc/Support/MemoryFactory.h"
+#include "bcc/Support/Log.h"
+
+#include <llvm/Support/ELF.h>
+
+#include <mcld/MC/MCLDDriver.h>
+#include <mcld/MC/MCLinker.h>
+#include <mcld/MC/InputTree.h>
+#include <mcld/LD/LDSection.h>
+#include <mcld/LD/LDContext.h>
+#include <mcld/Target/TargetLDBackend.h>
+#include <mcld/Support/Path.h>
+#include <mcld/Support/MemoryArea.h>
+#include <mcld/Support/FileHandle.h>
+#include <mcld/Support/MemoryAreaFactory.h>
+#include <mcld/Support/TargetRegistry.h>
+
+using namespace bcc;
+
+const char* Linker::GetErrorString(enum Linker::ErrorCode pErrCode) {
+  static const char* ErrorString[] = {
+    /* kSuccess */
+    "Successfully compiled.",
+    /* kDoubleConfig */
+    "Configure Linker twice.",
+    /* kCreateBackend */
+    "Cannot create backend.",
+    /* kDelegateLDInfo */
+    "Cannot get linker information",
+    /* kFindNameSpec */
+    "Cannot find -lnamespec",
+    /* kOpenNameSpec */
+    "Cannot open -lnamespec",
+    /* kOpenObjectFile */
+    "Cannot open object file",
+    /* kNotConfig */
+    "Linker::config() is not called",
+    /* kNotSetUpOutput */
+    "Linker::setOutput() is not called before add input files",
+    /* kOpenOutput */
+    "Cannot open output file",
+    /* kReadSections */
+    "Cannot read sections",
+    /* kReadSymbols */
+    "Cannot read symbols",
+    /* kAddAdditionalSymbols */
+    "Cannot add standard and target symbols",
+    /* kMaxErrorCode */
+    "(Unknown error code)"
+  };
+
+  if (pErrCode > kMaxErrorCode) {
+    pErrCode = kMaxErrorCode;
+  }
+
+  return ErrorString[ static_cast<size_t>(pErrCode) ];
+}
+
+//===----------------------------------------------------------------------===//
+// Linker
+//===----------------------------------------------------------------------===//
+Linker::Linker()
+  : mBackend(NULL), mDriver(NULL), mMemAreaFactory(NULL), mLDInfo(NULL),
+    mRoot(NULL), mShared(false) {
+}
+
+Linker::Linker(const LinkerConfig& pConfig)
+  : mBackend(NULL), mDriver(NULL), mMemAreaFactory(NULL), mLDInfo(NULL),
+    mRoot(NULL), mShared(false) {
+
+  const std::string &triple = pConfig.getTriple();
+
+  enum ErrorCode err = config(pConfig);
+  if (kSuccess != err) {
+    ALOGE("%s (%s)", GetErrorString(err), triple.c_str());
+    return;
+  }
+
+  return;
+}
+
+Linker::~Linker() {
+  delete mDriver;
+  delete mBackend;
+  delete mMemAreaFactory;
+  delete mRoot;
+}
+
+enum Linker::ErrorCode Linker::extractFiles(const LinkerConfig& pConfig) {
+  mLDInfo = const_cast<mcld::MCLDInfo*>(pConfig.getLDInfo());
+  if (mLDInfo == NULL) {
+    return kDelegateLDInfo;
+  }
+
+  mRoot = new mcld::InputTree::iterator(mLDInfo->inputs().root());
+  mShared = pConfig.isShared();
+  mSOName = pConfig.getSOName();
+
+  return kSuccess;
+}
+
+enum Linker::ErrorCode Linker::config(const LinkerConfig& pConfig) {
+  if (mLDInfo != NULL) {
+    return kDoubleConfig;
+  }
+
+  extractFiles(pConfig);
+
+  mBackend = pConfig.getTarget()->createLDBackend(pConfig.getTriple());
+  if (mBackend == NULL) {
+    return kCreateBackend;
+  }
+
+  mDriver = new mcld::MCLDDriver(*mLDInfo, *mBackend);
+
+  mMemAreaFactory = new MemoryFactory();
+
+  mDriver->initMCLinker();
+
+  return kSuccess;
+}
+
+void Linker::advanceRoot() {
+  if (mRoot->isRoot()) {
+    mRoot->move<mcld::TreeIteratorBase::Leftward>();
+  } else {
+    mRoot->move<mcld::TreeIteratorBase::Rightward>();
+  }
+  return;
+}
+
+enum Linker::ErrorCode Linker::openFile(const mcld::sys::fs::Path& pPath,
+                                        enum Linker::ErrorCode pCode,
+                                        mcld::Input& pInput) {
+  mcld::MemoryArea *input_memory = mMemAreaFactory->produce(pPath,
+                                                    mcld::FileHandle::ReadOnly);
+
+  if (input_memory->handler()->isGood()) {
+    pInput.setMemArea(input_memory);
+  } else {
+    return pCode;
+  }
+
+  mcld::LDContext *input_context = mLDInfo->contextFactory().produce(pPath);
+  pInput.setContext(input_context);
+  return kSuccess;
+}
+
+enum Linker::ErrorCode Linker::addNameSpec(const std::string &pNameSpec) {
+  mcld::sys::fs::Path* path = NULL;
+  // find out the real path of the namespec.
+  if (mLDInfo->attrFactory().constraint().isSharedSystem()) {
+    // In the system with shared object support, we can find both archive
+    // and shared object.
+
+    if (mLDInfo->attrFactory().last().isStatic()) {
+      // with --static, we must search an archive.
+      path = mLDInfo->options().directories().find(pNameSpec,
+                                                   mcld::Input::Archive);
+    }
+    else {
+      // otherwise, with --Bdynamic, we can find either an archive or a
+      // shared object.
+      path = mLDInfo->options().directories().find(pNameSpec,
+                                                   mcld::Input::DynObj);
+    }
+  }
+  else {
+    // In the system without shared object support, we only look for an
+    // archive.
+    path = mLDInfo->options().directories().find(pNameSpec,
+                                                 mcld::Input::Archive);
+  }
+
+  if (NULL == path)
+    return kFindNameSpec;
+
+  mcld::Input* input = mLDInfo->inputFactory().produce(pNameSpec, *path,
+                                                       mcld::Input::Unknown);
+  mLDInfo->inputs().insert<mcld::InputTree::Positional>(*mRoot, *input);
+
+  advanceRoot();
+
+  return openFile(*path, kOpenNameSpec, *input);
+}
+
+/// addObject - Add a object file by the filename.
+enum Linker::ErrorCode Linker::addObject(const std::string &pObjectPath) {
+  mcld::Input* input = mLDInfo->inputFactory().produce(pObjectPath,
+                                                       pObjectPath,
+                                                       mcld::Input::Unknown);
+
+  mLDInfo->inputs().insert<mcld::InputTree::Positional>(*mRoot, *input);
+
+  advanceRoot();
+
+  return openFile(pObjectPath, kOpenObjectFile, *input);
+}
+
+/// addObject - Add a piece of memory. The memory is of ELF format.
+enum Linker::ErrorCode Linker::addObject(void* pMemory, size_t pSize) {
+
+  mcld::Input* input = mLDInfo->inputFactory().produce("memory object",
+                                                       "NAN",
+                                                       mcld::Input::Unknown);
+
+  mLDInfo->inputs().insert<mcld::InputTree::Positional>(*mRoot, *input);
+
+  advanceRoot();
+
+  mcld::MemoryArea *input_memory = mMemAreaFactory->produce(pMemory, pSize);
+  input->setMemArea(input_memory);
+
+  mcld::LDContext *input_context = mLDInfo->contextFactory().produce();
+  input->setContext(input_context);
+
+  return kSuccess;
+}
+
+enum Linker::ErrorCode Linker::addCode(void* pMemory, size_t pSize) {
+  mcld::Input* input = mLDInfo->inputFactory().produce("code object",
+                                                       "NAN",
+                                                       mcld::Input::External);
+
+  mLDInfo->inputs().insert<mcld::InputTree::Positional>(*mRoot, *input);
+
+  advanceRoot();
+
+  mcld::MemoryArea *input_memory = mMemAreaFactory->produce(pMemory, pSize);
+  input->setMemArea(input_memory);
+
+  mcld::LDContext *input_context = mLDInfo->contextFactory().produce();
+  input->setContext(input_context);
+
+  // FIXME: So far, MCLinker must set up output before add input files.
+  // set up LDContext
+  if (mDriver->hasInitLinker()) {
+    return kNotConfig;
+  }
+
+  if (!mLDInfo->output().hasContext()) {
+    return kNotSetUpOutput;
+  }
+
+  // create NULL section
+  mcld::LDSection& null =
+      mDriver->getLinker()->createSectHdr("",
+                                          mcld::LDFileFormat::Null,
+                                          llvm::ELF::SHT_NULL,
+                                          0);
+
+  null.setSize(0);
+  null.setOffset(0);
+  null.setIndex(0);
+  null.setInfo(0);
+  null.setAlign(0);
+
+  input_context->getSectionTable().push_back(&null);
+
+  // create .text section
+  mcld::LDSection& text = mDriver->getLinker()->createSectHdr(".text",
+                              mcld::LDFileFormat::Regular,
+                              llvm::ELF::SHT_PROGBITS,
+                              llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR);
+
+  text.setSize(pSize);
+  text.setOffset(0x0);
+  text.setIndex(1);
+  text.setInfo(0);
+  text.setAlign(1);
+
+  input_context->getSectionTable().push_back(&text);
+
+  return kSuccess;
+}
+
+enum Linker::ErrorCode Linker::setOutput(const std::string &pPath) {
+  if (mLDInfo->output().hasContext()) {
+    return kDoubleConfig;
+  }
+
+  // -----  initialize output file  ----- //
+
+  mcld::FileHandle::Permission perm = 0755;
+
+  mcld::MemoryArea* out_area = mMemAreaFactory->produce(
+                      pPath,
+                      mcld::FileHandle::ReadWrite |
+                        mcld::FileHandle::Truncate |
+                        mcld::FileHandle::Create,
+                      perm);
+
+  if (!out_area->handler()->isGood()) {
+    return kOpenOutput;
+  }
+
+  if (mShared) {
+    mLDInfo->output().setType(mcld::Output::DynObj);
+  } else {
+    mLDInfo->output().setType(mcld::Output::Exec);
+  }
+
+  mLDInfo->output().setSOName(mSOName);
+  mLDInfo->output().setMemArea(out_area);
+  mLDInfo->output().setContext(mLDInfo->contextFactory().produce(pPath));
+
+  // FIXME: We must initialize MCLinker before setOutput, and initialize
+  // standard sections here. This is because we have to build the section
+  // map before input files using it.
+  if (!mDriver->hasInitLinker()) {
+    return kNotConfig;
+  }
+
+  mDriver->initStdSections();
+
+  return kSuccess;
+}
+
+enum Linker::ErrorCode Linker::setOutput(int pFileHandler) {
+  if (mLDInfo->output().hasContext()) {
+    return kDoubleConfig;
+  }
+
+  // -----  initialize output file  ----- //
+  mcld::MemoryArea* out_area = mMemAreaFactory->produce(pFileHandler);
+
+  mLDInfo->output().setType(mcld::Output::DynObj);
+  mLDInfo->output().setMemArea(out_area);
+  mLDInfo->output().setContext(mLDInfo->contextFactory().produce());
+
+  // FIXME: We must initialize MCLinker before setOutput, and initialize
+  // standard sections here. This is because we have to build the section
+  // map before input files using it.
+  if (!mDriver->hasInitLinker()) {
+    return kNotConfig;
+  }
+
+  mDriver->initStdSections();
+
+  return kSuccess;
+}
+
+enum Linker::ErrorCode Linker::link() {
+  mDriver->normalize();
+
+  if (!mDriver->readSections() || !mDriver->mergeSections()) {
+    return kReadSections;
+  }
+
+  if (!mDriver->readSymbolTables()) {
+    return kReadSymbols;
+  }
+
+  if (!mDriver->addStandardSymbols() || !mDriver->addTargetSymbols()) {
+    return kAddAdditionalSymbols;
+  }
+
+  mDriver->readRelocations();
+  mDriver->prelayout();
+  mDriver->layout();
+  mDriver->postlayout();
+  mDriver->finalizeSymbolValue();
+  mDriver->relocation();
+  mDriver->emitOutput();
+  mDriver->postProcessing();
+
+  return kSuccess;
+}
+
diff --git a/lib/Core/Script.cpp b/lib/Core/Script.cpp
new file mode 100644
index 0000000..8e8a274
--- /dev/null
+++ b/lib/Core/Script.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2010-2012, 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 "bcc/Script.h"
+
+#include "bcc/Source.h"
+
+using namespace bcc;
+
+bool Script::reset(Source &pSource, bool pPreserveCurrent) {
+  if (mSource == &pSource) {
+    return false;
+  }
+
+  if (!pPreserveCurrent) {
+    delete mSource;
+  }
+  mSource = &pSource;
+  return doReset();
+}
+
+bool Script::mergeSource(Source &pSource, bool pPreserveSource) {
+  return mSource->merge(pSource, pPreserveSource);
+}
diff --git a/lib/Core/Source.cpp b/lib/Core/Source.cpp
new file mode 100644
index 0000000..3d712e6
--- /dev/null
+++ b/lib/Core/Source.cpp
@@ -0,0 +1,204 @@
+/*
+ * Copyright 2012, 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 "bcc/Source.h"
+
+#include <new>
+
+#include <llvm/Bitcode/ReaderWriter.h>
+#include <llvm/LLVMContext.h>
+#include <llvm/Linker.h>
+#include <llvm/Module.h>
+#include <llvm/Support/MemoryBuffer.h>
+#include <llvm/Support/system_error.h>
+
+#include "bcc/BCCContext.h"
+#include "bcc/Support/Log.h"
+
+#include "BCCContextImpl.h"
+
+namespace {
+
+// Helper function to load the bitcode. This uses "bitcode lazy load" feature to
+// reduce the startup time. On success, return the LLVM module object created
+// and take the ownership of input memory buffer (i.e., pInput). On error,
+// return NULL and will NOT take the ownership of pInput.
+static inline llvm::Module *helper_load_bitcode(llvm::LLVMContext &pContext,
+                                                llvm::MemoryBuffer *pInput) {
+  std::string error;
+  llvm::Module *module = llvm::getLazyBitcodeModule(pInput, pContext, &error);
+
+  if (module == NULL) {
+    ALOGE("Unable to parse the given bitcode file `%s'! (%s)",
+          pInput->getBufferIdentifier(), error.c_str());
+  }
+
+  return module;
+}
+
+} // end anonymous namespace
+
+namespace bcc {
+
+Source *Source::CreateFromBuffer(BCCContext &pContext,
+                                 const char *pName,
+                                 const char *pBitcode,
+                                 size_t pBitcodeSize) {
+  llvm::StringRef input_data(pBitcode, pBitcodeSize);
+  llvm::MemoryBuffer *input_memory =
+      llvm::MemoryBuffer::getMemBuffer(input_data, "", false);
+
+  if (input_memory == NULL) {
+    ALOGE("Unable to load bitcode `%s' from buffer!", pName);
+    return NULL;
+  }
+
+  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
+                                             input_memory);
+  if (module == NULL) {
+    delete input_memory;
+    return NULL;
+  }
+
+  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
+  if (result == NULL) {
+    delete module;
+  }
+
+  return result;
+}
+
+Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) {
+  llvm::OwningPtr<llvm::MemoryBuffer> input_data;
+
+  llvm::error_code ec = llvm::MemoryBuffer::getFile(pPath, input_data);
+  if (ec != llvm::error_code::success()) {
+    ALOGE("Failed to load bitcode from path %s! (%s)", pPath.c_str(),
+                                                       ec.message().c_str());
+    return NULL;
+  }
+
+  llvm::MemoryBuffer *input_memory = input_data.take();
+  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
+                                             input_memory);
+  if (module == NULL) {
+    delete input_memory;
+    return NULL;
+  }
+
+  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
+  if (result == NULL) {
+    delete module;
+  }
+
+  return result;
+}
+
+Source *Source::CreateFromFd(BCCContext &pContext, int pFd) {
+  llvm::OwningPtr<llvm::MemoryBuffer> input_data;
+
+  llvm::error_code ec =
+      llvm::MemoryBuffer::getOpenFile(pFd, /* Filename */"", input_data);
+
+  if (ec != llvm::error_code::success()) {
+    ALOGE("Failed to load bitcode from file descriptor %d! (%s)",
+          pFd, ec.message().c_str());
+    return NULL;
+  }
+
+  llvm::MemoryBuffer *input_memory = input_data.take();
+  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
+                                             input_memory);
+  if (module == NULL) {
+    delete input_memory;
+    return NULL;
+  }
+
+  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
+  if (result == NULL) {
+    delete module;
+  }
+
+  return result;
+}
+
+Source *Source::CreateFromModule(BCCContext &pContext, llvm::Module &pModule,
+                                 bool pNoDelete) {
+  Source *result = new (std::nothrow) Source(pContext, pModule, pNoDelete);
+  if (result == NULL) {
+    ALOGE("Out of memory during Source object allocation for `%s'!",
+          pModule.getModuleIdentifier().c_str());
+  }
+  return result;
+}
+
+Source::Source(BCCContext &pContext, llvm::Module &pModule, bool pNoDelete)
+  : mContext(pContext), mModule(&pModule), mNoDelete(pNoDelete) {
+    pContext.addSource(*this);
+}
+
+Source::~Source() {
+  mContext.removeSource(*this);
+  if (!mNoDelete)
+    delete mModule;
+}
+
+bool Source::merge(Source &pSource, bool pPreserveSource) {
+  std::string error;
+  llvm::Linker::LinkerMode mode =
+      ((pPreserveSource) ? llvm::Linker::PreserveSource :
+                           llvm::Linker::DestroySource);
+
+  if (llvm::Linker::LinkModules(mModule, &pSource.getModule(),
+                                mode, &error) != 0) {
+    ALOGE("Failed to link source `%s' with `%s' (%s)!",
+          getIdentifier().c_str(),
+          pSource.getIdentifier().c_str(),
+          error.c_str());
+    return false;
+  }
+
+  if (!pPreserveSource) {
+    pSource.mNoDelete = true;
+    delete &pSource;
+  }
+
+  return true;
+}
+
+Source *Source::CreateEmpty(BCCContext &pContext, const std::string &pName) {
+  // Create an empty module
+  llvm::Module *module =
+      new (std::nothrow) llvm::Module(pName, pContext.mImpl->mLLVMContext);
+
+  if (module == NULL) {
+    ALOGE("Out of memory when creating empty LLVM module `%s'!", pName.c_str());
+    return NULL;
+  }
+
+  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
+  if (result == NULL) {
+    delete module;
+  }
+
+  return result;
+}
+
+const std::string &Source::getIdentifier() const {
+  return mModule->getModuleIdentifier();
+}
+
+} // namespace bcc
diff --git a/lib/Disassembler/Android.mk b/lib/Disassembler/Android.mk
deleted file mode 100644
index 1d925ed..0000000
--- a/lib/Disassembler/Android.mk
+++ /dev/null
@@ -1,72 +0,0 @@
-#
-# 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.
-#
-#
-
-LOCAL_PATH := $(call my-dir)
-include $(LOCAL_PATH)/../../libbcc-config.mk
-
-ifeq ($(libbcc_USE_DISASSEMBLER),1)
-
-#=====================================================================
-# Common: libbccDisassembler
-#=====================================================================
-
-libbcc_disassembler_SRC_FILES := \
-  Disassembler.cpp
-
-
-#=====================================================================
-# Device Static Library: libbccDisassembler
-#=====================================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libbccDisassembler
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-LOCAL_SRC_FILES := $(libbcc_disassembler_SRC_FILES)
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
-include $(BUILD_STATIC_LIBRARY)
-
-
-#=====================================================================
-# Host Static Library: libbccDisassembler
-#=====================================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libbccDisassembler
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-LOCAL_IS_HOST_MODULE := true
-
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -D__HOST__
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-
-LOCAL_SRC_FILES := $(libbcc_disassembler_SRC_FILES)
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-endif # $(libbcc_USE_DISASSEMBLER)
diff --git a/lib/Disassembler/Disassembler.cpp b/lib/Disassembler/Disassembler.cpp
deleted file mode 100644
index 7e325ce..0000000
--- a/lib/Disassembler/Disassembler.cpp
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Copyright 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.
- */
-
-#include "Disassembler.h"
-
-#include "Config.h"
-
-#include "DebugHelper.h"
-#include "ExecutionEngine/Compiler.h"
-
-#include "llvm/MC/MCAsmInfo.h"
-#include "llvm/MC/MCDisassembler.h"
-#include "llvm/MC/MCInst.h"
-#include "llvm/MC/MCInstPrinter.h"
-
-#include "llvm/Support/MemoryObject.h"
-#include "llvm/Support/raw_ostream.h"
-
-#include "llvm/Target/TargetData.h"
-#include "llvm/Target/TargetMachine.h"
-#include "llvm/Target/TargetOptions.h"
-#include "llvm/Support/TargetRegistry.h"
-#include "llvm/Support/TargetSelect.h"
-
-#include "llvm/LLVMContext.h"
-
-#if USE_DISASSEMBLER
-
-namespace {
-
-class BufferMemoryObject : public llvm::MemoryObject {
-private:
-  const uint8_t *mBytes;
-  uint64_t mLength;
-
-public:
-  BufferMemoryObject(const uint8_t *Bytes, uint64_t Length)
-    : mBytes(Bytes), mLength(Length) {
-  }
-
-  virtual uint64_t getBase() const { return 0; }
-  virtual uint64_t getExtent() const { return mLength; }
-
-  virtual int readByte(uint64_t Addr, uint8_t *Byte) const {
-    if (Addr > getExtent())
-      return -1;
-    *Byte = mBytes[Addr];
-    return 0;
-  }
-};
-
-} // namespace anonymous
-
-namespace bcc {
-
-void InitializeDisassembler() {
-#if defined(PROVIDE_ARM_CODEGEN)
-  LLVMInitializeARMDisassembler();
-#endif
-
-#if defined(PROVIDE_X86_CODEGEN)
-  LLVMInitializeX86Disassembler();
-#endif
-}
-
-void Disassemble(char const *OutputFileName,
-                 llvm::Target const *Target,
-                 llvm::TargetMachine *TM,
-                 std::string const &Name,
-                 unsigned char const *Func,
-                 size_t FuncSize) {
-
-  std::string ErrorInfo;
-
-  // Open the disassembler output file
-  llvm::raw_fd_ostream OS(OutputFileName, ErrorInfo,
-                          llvm::raw_fd_ostream::F_Append);
-
-  if (!ErrorInfo.empty()) {
-    ALOGE("Unable to open disassembler output file: %s\n", OutputFileName);
-    return;
-  }
-
-  // Disassemble the given function
-  OS << "Disassembled code: " << Name << "\n";
-
-  const llvm::MCAsmInfo *AsmInfo;
-  const llvm::MCSubtargetInfo *SubtargetInfo;
-  const llvm::MCDisassembler *Disassmbler;
-  llvm::MCInstPrinter *IP;
-
-  AsmInfo = Target->createMCAsmInfo(Compiler::getTargetTriple());
-  SubtargetInfo = Target->createMCSubtargetInfo(Compiler::getTargetTriple(), "", "");
-  Disassmbler = Target->createMCDisassembler(*SubtargetInfo);
-  IP = Target->createMCInstPrinter(AsmInfo->getAssemblerDialect(),
-                                   *AsmInfo, *SubtargetInfo);
-
-  const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize);
-
-  uint64_t Size;
-  uint64_t Index;
-
-  for (Index = 0; Index < FuncSize; Index += Size) {
-    llvm::MCInst Inst;
-
-    if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index,
-                           /* REMOVED */ llvm::nulls(), llvm::nulls())) {
-      OS.indent(4);
-      OS.write("0x", 2);
-      OS.write_hex((uint32_t)Func + Index);
-      OS.write(": 0x", 4);
-      OS.write_hex(*(uint32_t *)(Func + Index));
-      IP->printInst(&Inst, OS, "");
-      OS << "\n";
-    } else {
-      if (Size == 0)
-        Size = 1;  // skip illegible bytes
-    }
-  }
-
-  OS << "\n";
-
-  delete BufferMObj;
-
-  delete AsmInfo;
-  delete Disassmbler;
-  delete IP;
-
-  OS.close();
-}
-
-} // namespace bcc
-
-#endif // USE_DISASSEMBLER
diff --git a/lib/Disassembler/Disassembler.h b/lib/Disassembler/Disassembler.h
deleted file mode 100644
index d0d924c..0000000
--- a/lib/Disassembler/Disassembler.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright 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.
- */
-
-#ifndef BCC_DISASSEMBLER_H
-#define BCC_DISASSEMBLER_H
-
-#include "Config.h"
-
-#if USE_DISASSEMBLER
-
-#include <string>
-
-namespace llvm {
-  class Target;
-  class TargetMachine;
-}
-
-namespace bcc {
-
-  void InitializeDisassembler();
-
-  void Disassemble(char const *OutputFileName,
-                   llvm::Target const *Target,
-                   llvm::TargetMachine *TM,
-                   std::string const &Name,
-                   unsigned char const *Func,
-                   size_t FuncSize);
-
-} // namespace bcc
-
-#endif // USE_DISASSEMBLER
-
-#endif // BCC_DISASSEMBLER_H
diff --git a/lib/ExecutionEngine/Android.mk b/lib/ExecutionEngine/Android.mk
index c015bcc..d5a4573 100644
--- a/lib/ExecutionEngine/Android.mk
+++ b/lib/ExecutionEngine/Android.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (C) 2011 The Android Open Source Project
+# Copyright (C) 2011-2012 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.
@@ -16,27 +16,20 @@
 #
 
 LOCAL_PATH := $(call my-dir)
-include $(LOCAL_PATH)/../../libbcc-config.mk
 
 #=====================================================================
 # Common: libbccExecutionEngine
 #=====================================================================
 
 libbcc_executionengine_SRC_FILES := \
-  Compiler.cpp \
-  FileHandle.cpp \
+  BCCRuntimeStub.c \
+  BCCRuntimeSymbolResolver.cpp \
+  ELFObjectLoaderImpl.cpp \
   GDBJIT.cpp \
   GDBJITRegistrar.cpp \
-  MCCacheWriter.cpp \
-  MCCacheReader.cpp \
-  Runtime.c \
-  RuntimeStub.c \
-  Script.cpp \
-  ScriptCached.cpp \
-  ScriptCompiled.cpp \
-  Sha1Helper.cpp \
-  SourceInfo.cpp
-
+  ObjectLoader.cpp \
+  SymbolResolverProxy.cpp \
+  SymbolResolvers.cpp
 
 #=====================================================================
 # Device Static Library: libbccExecutionEngine
@@ -48,16 +41,16 @@
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -DTARGET_BUILD
+LOCAL_C_INCLUDES := \
+  $(LIBBCC_ROOT_PATH) \
+  $(RSLOADER_ROOT_PATH) \
+  $(RSLOADER_ROOT_PATH)/include
 
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
 LOCAL_SRC_FILES := $(libbcc_executionengine_SRC_FILES)
-LOCAL_SHARED_LIBRARIES := libbcinfo
 
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_DEVICE_BUILD_MK)
 include $(BUILD_STATIC_LIBRARY)
 
 
@@ -70,16 +63,15 @@
 LOCAL_MODULE := libbccExecutionEngine
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-LOCAL_IS_HOST_MODULE := true
 
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -D__HOST__
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
+LOCAL_C_INCLUDES := \
+  $(LIBBCC_ROOT_PATH) \
+  $(RSLOADER_ROOT_PATH) \
+  $(RSLOADER_ROOT_PATH)/include
 
 LOCAL_SRC_FILES := $(libbcc_executionengine_SRC_FILES)
-LOCAL_SHARED_LIBRARIES := libbcinfo
 
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_HOST_BUILD_MK)
 include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/lib/ExecutionEngine/Runtime.def b/lib/ExecutionEngine/BCCRuntime.def
similarity index 100%
rename from lib/ExecutionEngine/Runtime.def
rename to lib/ExecutionEngine/BCCRuntime.def
diff --git a/lib/ExecutionEngine/RuntimeStub.c b/lib/ExecutionEngine/BCCRuntimeStub.c
similarity index 98%
rename from lib/ExecutionEngine/RuntimeStub.c
rename to lib/ExecutionEngine/BCCRuntimeStub.c
index d38f68d..fc78c71 100644
--- a/lib/ExecutionEngine/RuntimeStub.c
+++ b/lib/ExecutionEngine/BCCRuntimeStub.c
@@ -1,4 +1,4 @@
-#include "RuntimeStub.h"
+#include "BCCRuntimeStub.h"
 
 #include "runtime/lib/int_lib.h"
 
diff --git a/lib/ExecutionEngine/RuntimeStub.h b/lib/ExecutionEngine/BCCRuntimeStub.h
similarity index 100%
rename from lib/ExecutionEngine/RuntimeStub.h
rename to lib/ExecutionEngine/BCCRuntimeStub.h
diff --git a/lib/ExecutionEngine/BCCRuntimeSymbolResolver.cpp b/lib/ExecutionEngine/BCCRuntimeSymbolResolver.cpp
new file mode 100644
index 0000000..ddc06fc
--- /dev/null
+++ b/lib/ExecutionEngine/BCCRuntimeSymbolResolver.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2012, 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 "bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h"
+
+// int_lib.h included by BCCRuntimeStub.h has the following line:
+//
+//            typedef      int si_int;
+//
+// However, there's already a macro also named "si_int" defined in siginfo.h
+// bionic/libc/kernel/common/asm-generic/. This is #undef is a hack to avoid
+// compiler mistakenly recognize the identifier "si_int" as a macro and abort
+// the compilation.
+//
+// This line of hack should put in the header file since it invalidate a
+// "system" scope macro definition.
+#undef si_int
+#include "BCCRuntimeStub.h"
+
+using namespace bcc;
+
+#if defined(__arm__) || defined(__mips__)
+  #define DEF_GENERIC_RUNTIME(func)   \
+    extern void *func;
+  #define DEF_VFP_RUNTIME(func) \
+    extern void *func ## vfp;
+  #define DEF_LLVM_RUNTIME(func)
+  #define DEF_BCC_RUNTIME(func)
+#include "BCCRuntime.def"
+#endif
+
+const BCCRuntimeSymbolResolver::SymbolMap BCCRuntimeSymbolResolver::SymbolArray[] = {
+#if defined(__arm__) || defined(__mips__)
+  #define DEF_GENERIC_RUNTIME(func)   \
+    { #func, (void*) &func },
+  // TODO: enable only when target support VFP
+  #define DEF_VFP_RUNTIME(func) \
+    { #func, (void*) &func ## vfp },
+#else
+  // host compiler library must contain generic runtime
+  #define DEF_GENERIC_RUNTIME(func)
+  #define DEF_VFP_RUNTIME(func)
+#endif
+#define DEF_LLVM_RUNTIME(func)   \
+  { #func, (void*) &func },
+#define DEF_BCC_RUNTIME(func) \
+  { #func, &func ## _bcc },
+#include "BCCRuntime.def"
+};
+
+const size_t BCCRuntimeSymbolResolver::NumSymbols =
+  sizeof(BCCRuntimeSymbolResolver::SymbolArray) /
+    sizeof(BCCRuntimeSymbolResolver::SymbolArray[0]);
diff --git a/lib/ExecutionEngine/Compiler.cpp b/lib/ExecutionEngine/Compiler.cpp
deleted file mode 100644
index cf18ef6..0000000
--- a/lib/ExecutionEngine/Compiler.cpp
+++ /dev/null
@@ -1,683 +0,0 @@
-/*
- * Copyright 2010-2012, 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 "Compiler.h"
-
-#include "Config.h"
-#include <bcinfo/MetadataExtractor.h>
-
-#if USE_DISASSEMBLER
-#include "Disassembler/Disassembler.h"
-#endif
-
-#include "DebugHelper.h"
-#include "FileHandle.h"
-#include "Runtime.h"
-#include "ScriptCompiled.h"
-#include "Sha1Helper.h"
-#include "CompilerOption.h"
-
-#include "librsloader.h"
-
-#include "Transforms/BCCTransforms.h"
-
-#include "llvm/ADT/StringRef.h"
-
-#include "llvm/Analysis/Passes.h"
-
-#include "llvm/CodeGen/Passes.h"
-#include "llvm/CodeGen/RegAllocRegistry.h"
-#include "llvm/CodeGen/SchedulerRegistry.h"
-
-#include "llvm/MC/MCContext.h"
-#include "llvm/MC/SubtargetFeature.h"
-
-#include "llvm/Transforms/IPO.h"
-#include "llvm/Transforms/Scalar.h"
-
-#include "llvm/Target/TargetData.h"
-#include "llvm/Target/TargetMachine.h"
-
-#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/FormattedStream.h"
-#include "llvm/Support/TargetRegistry.h"
-#include "llvm/Support/TargetSelect.h"
-#include "llvm/Support/raw_ostream.h"
-
-#include "llvm/Constants.h"
-#include "llvm/GlobalValue.h"
-#include "llvm/Linker.h"
-#include "llvm/LLVMContext.h"
-#include "llvm/Module.h"
-#include "llvm/PassManager.h"
-#include "llvm/Type.h"
-#include "llvm/Value.h"
-
-#include <errno.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <string.h>
-
-#include <algorithm>
-#include <iterator>
-#include <string>
-#include <vector>
-
-extern char* gDebugDumpDirectory;
-
-namespace bcc {
-
-//////////////////////////////////////////////////////////////////////////////
-// BCC Compiler Static Variables
-//////////////////////////////////////////////////////////////////////////////
-
-bool Compiler::GlobalInitialized = false;
-
-
-#if !defined(__HOST__)
-  #define TARGET_TRIPLE_STRING  DEFAULT_TARGET_TRIPLE_STRING
-#else
-// In host TARGET_TRIPLE_STRING is a variable to allow cross-compilation.
-  #if defined(__cplusplus)
-    extern "C" {
-  #endif
-      char *TARGET_TRIPLE_STRING = (char*)DEFAULT_TARGET_TRIPLE_STRING;
-  #if defined(__cplusplus)
-    };
-  #endif
-#endif
-
-// Code generation optimization level for the compiler
-llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel;
-
-std::string Compiler::Triple;
-llvm::Triple::ArchType Compiler::ArchType;
-
-std::string Compiler::CPU;
-
-std::vector<std::string> Compiler::Features;
-
-
-//////////////////////////////////////////////////////////////////////////////
-// Compiler
-//////////////////////////////////////////////////////////////////////////////
-
-void Compiler::GlobalInitialization() {
-  if (GlobalInitialized) {
-    return;
-  }
-
-#if defined(PROVIDE_ARM_CODEGEN)
-  LLVMInitializeARMAsmPrinter();
-  LLVMInitializeARMTargetMC();
-  LLVMInitializeARMTargetInfo();
-  LLVMInitializeARMTarget();
-#endif
-
-#if defined(PROVIDE_MIPS_CODEGEN)
-  LLVMInitializeMipsAsmPrinter();
-  LLVMInitializeMipsTargetMC();
-  LLVMInitializeMipsTargetInfo();
-  LLVMInitializeMipsTarget();
-#endif
-
-#if defined(PROVIDE_X86_CODEGEN)
-  LLVMInitializeX86AsmPrinter();
-  LLVMInitializeX86TargetMC();
-  LLVMInitializeX86TargetInfo();
-  LLVMInitializeX86Target();
-#endif
-
-#if USE_DISASSEMBLER
-  InitializeDisassembler();
-#endif
-
-  // if (!llvm::llvm_is_multithreaded())
-  //   llvm::llvm_start_multithreaded();
-
-  // Set Triple, CPU and Features here
-  Triple = TARGET_TRIPLE_STRING;
-
-  // Determine ArchType
-#if defined(__HOST__)
-  {
-    std::string Err;
-    llvm::Target const *Target = llvm::TargetRegistry::lookupTarget(Triple, Err);
-    if (Target != NULL) {
-      ArchType = llvm::Triple::getArchTypeForLLVMName(Target->getName());
-    } else {
-      ArchType = llvm::Triple::UnknownArch;
-      ALOGE("%s", Err.c_str());
-    }
-  }
-#elif defined(DEFAULT_ARM_CODEGEN)
-  ArchType = llvm::Triple::arm;
-#elif defined(DEFAULT_MIPS_CODEGEN)
-  ArchType = llvm::Triple::mipsel;
-#elif defined(DEFAULT_X86_CODEGEN)
-  ArchType = llvm::Triple::x86;
-#elif defined(DEFAULT_X86_64_CODEGEN)
-  ArchType = llvm::Triple::x86_64;
-#else
-  ArchType = llvm::Triple::UnknownArch;
-#endif
-
-  if ((ArchType == llvm::Triple::arm) || (ArchType == llvm::Triple::thumb)) {
-#  if defined(ARCH_ARM_HAVE_VFP)
-    Features.push_back("+vfp3");
-#  if !defined(ARCH_ARM_HAVE_VFP_D32)
-    Features.push_back("+d16");
-#  endif
-#  endif
-
-#  if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
-    Features.push_back("+neon");
-    Features.push_back("+neonfp");
-#  else
-    Features.push_back("-neon");
-    Features.push_back("-neonfp");
-#  endif
-  }
-
-  // Register the scheduler
-  llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
-
-  // Read in SHA1 checksum of libbcc and libRS.
-  readSHA1(sha1LibBCC_SHA1, sizeof(sha1LibBCC_SHA1), pathLibBCC_SHA1);
-
-  calcFileSHA1(sha1LibRS, pathLibRS);
-
-  GlobalInitialized = true;
-}
-
-
-void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) {
-  std::string *Error = static_cast<std::string*>(UserData);
-  Error->assign(Message);
-  ALOGE("%s", Message.c_str());
-  exit(1);
-}
-
-
-Compiler::Compiler(ScriptCompiled *result)
-  : mpResult(result),
-    mRSExecutable(NULL),
-    mpSymbolLookupFn(NULL),
-    mpSymbolLookupContext(NULL),
-    mModule(NULL),
-    mHasLinked(false) /* Turn off linker */ {
-  llvm::remove_fatal_error_handler();
-  llvm::install_fatal_error_handler(LLVMErrorHandler, &mError);
-  return;
-}
-
-
-int Compiler::linkModule(llvm::Module *moduleWith) {
-  if (llvm::Linker::LinkModules(mModule, moduleWith,
-                                llvm::Linker::PreserveSource,
-                                &mError) != 0) {
-    return hasError();
-  }
-
-  // Everything for linking should be settled down here with no error occurs
-  mHasLinked = true;
-  return hasError();
-}
-
-
-int Compiler::compile(const CompilerOption &option) {
-  llvm::Target const *Target = NULL;
-  llvm::TargetData *TD = NULL;
-  llvm::TargetMachine *TM = NULL;
-
-  std::vector<std::string> ExtraFeatures;
-
-  std::string FeaturesStr;
-
-  if (mModule == NULL)  // No module was loaded
-    return 0;
-
-  bcinfo::MetadataExtractor ME(mModule);
-  ME.extract();
-
-  size_t VarCount = ME.getExportVarCount();
-  size_t FuncCount = ME.getExportFuncCount();
-  size_t ForEachSigCount = ME.getExportForEachSignatureCount();
-  size_t ObjectSlotCount = ME.getObjectSlotCount();
-  size_t PragmaCount = ME.getPragmaCount();
-
-  std::vector<std::string> &VarNameList = mpResult->mExportVarsName;
-  std::vector<std::string> &FuncNameList = mpResult->mExportFuncsName;
-  std::vector<std::string> &ForEachExpandList = mpResult->mExportForEachName;
-  std::vector<std::string> ForEachNameList;
-  std::vector<uint32_t> ForEachSigList;
-  std::vector<const char*> ExportSymbols;
-
-  // Defaults to maximum optimization level from MetadataExtractor.
-  uint32_t OptimizationLevel = ME.getOptimizationLevel();
-
-  if (OptimizationLevel == 0) {
-    CodeGenOptLevel = llvm::CodeGenOpt::None;
-  } else if (OptimizationLevel == 1) {
-    CodeGenOptLevel = llvm::CodeGenOpt::Less;
-  } else if (OptimizationLevel == 2) {
-    CodeGenOptLevel = llvm::CodeGenOpt::Default;
-  } else if (OptimizationLevel == 3) {
-    CodeGenOptLevel = llvm::CodeGenOpt::Aggressive;
-  }
-
-  // not the best place for this, but we need to set the register allocation
-  // policy after we read the optimization_level metadata from the bitcode
-
-  // Register allocation policy:
-  //  createFastRegisterAllocator: fast but bad quality
-  //  createLinearScanRegisterAllocator: not so fast but good quality
-  llvm::RegisterRegAlloc::setDefault
-    ((CodeGenOptLevel == llvm::CodeGenOpt::None) ?
-     llvm::createFastRegisterAllocator :
-     llvm::createGreedyRegisterAllocator);
-
-  // Find LLVM Target
-  Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
-  if (hasError())
-    goto on_bcc_compile_error;
-
-#if defined(ARCH_ARM_HAVE_NEON)
-  // Full-precision means we have to disable NEON
-  if (ME.getRSFloatPrecision() == bcinfo::RS_FP_Full) {
-    ExtraFeatures.push_back("-neon");
-    ExtraFeatures.push_back("-neonfp");
-  }
-#endif
-
-  if (!CPU.empty() || !Features.empty() || !ExtraFeatures.empty()) {
-    llvm::SubtargetFeatures F;
-
-    for (std::vector<std::string>::const_iterator
-         I = Features.begin(), E = Features.end(); I != E; I++) {
-      F.AddFeature(*I);
-    }
-
-    for (std::vector<std::string>::const_iterator
-         I = ExtraFeatures.begin(), E = ExtraFeatures.end(); I != E; I++) {
-      F.AddFeature(*I);
-    }
-
-    FeaturesStr = F.getString();
-  }
-
-  // Create LLVM Target Machine
-  TM = Target->createTargetMachine(Triple, CPU, FeaturesStr,
-                                   option.TargetOpt,
-                                   option.RelocModelOpt,
-                                   option.CodeModelOpt);
-
-  if (TM == NULL) {
-    setError("Failed to create target machine implementation for the"
-             " specified triple '" + Triple + "'");
-    goto on_bcc_compile_error;
-  }
-
-  // Get target data from Module
-  TD = new llvm::TargetData(mModule);
-
-  // Read pragma information from MetadataExtractor
-  if (PragmaCount) {
-    ScriptCompiled::PragmaList &PragmaPairs = mpResult->mPragmas;
-    const char **PragmaKeys = ME.getPragmaKeyList();
-    const char **PragmaValues = ME.getPragmaValueList();
-    for (size_t i = 0; i < PragmaCount; i++) {
-      PragmaPairs.push_back(std::make_pair(PragmaKeys[i], PragmaValues[i]));
-    }
-  }
-
-  if (VarCount) {
-    const char **VarNames = ME.getExportVarNameList();
-    for (size_t i = 0; i < VarCount; i++) {
-      VarNameList.push_back(VarNames[i]);
-      ExportSymbols.push_back(VarNames[i]);
-    }
-  }
-
-  if (FuncCount) {
-    const char **FuncNames = ME.getExportFuncNameList();
-    for (size_t i = 0; i < FuncCount; i++) {
-      FuncNameList.push_back(FuncNames[i]);
-      ExportSymbols.push_back(FuncNames[i]);
-    }
-  }
-
-  if (ForEachSigCount) {
-    const char **ForEachNames = ME.getExportForEachNameList();
-    const uint32_t *ForEachSigs = ME.getExportForEachSignatureList();
-    for (size_t i = 0; i < ForEachSigCount; i++) {
-      std::string Name(ForEachNames[i]);
-      ForEachNameList.push_back(Name);
-      ForEachExpandList.push_back(Name + ".expand");
-      ForEachSigList.push_back(ForEachSigs[i]);
-    }
-
-    // Need to wait until ForEachExpandList is fully populated to fill in
-    // exported symbols.
-    for (size_t i = 0; i < ForEachSigCount; i++) {
-      ExportSymbols.push_back(ForEachExpandList[i].c_str());
-    }
-  }
-
-  if (ObjectSlotCount) {
-    ScriptCompiled::ObjectSlotList &objectSlotList = mpResult->mObjectSlots;
-    const uint32_t *ObjectSlots = ME.getObjectSlotList();
-    for (size_t i = 0; i < ObjectSlotCount; i++) {
-      objectSlotList.push_back(ObjectSlots[i]);
-    }
-  }
-
-  runInternalPasses(ForEachNameList, ForEachSigList);
-
-  // Perform link-time optimization if we have multiple modules
-  if (mHasLinked) {
-    runLTO(new llvm::TargetData(*TD), ExportSymbols, CodeGenOptLevel);
-  }
-
-  // Perform code generation
-  if (runMCCodeGen(new llvm::TargetData(*TD), TM) != 0) {
-    goto on_bcc_compile_error;
-  }
-
-  if (!option.LoadAfterCompile)
-    return 0;
-
-  // Load the ELF Object
-  mRSExecutable =
-      rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(),
-                         mEmittedELFExecutable.size(),
-                         &resolveSymbolAdapter, this);
-
-  if (!mRSExecutable) {
-    setError("Fail to load emitted ELF relocatable file");
-    goto on_bcc_compile_error;
-  }
-
-  rsloaderUpdateSectionHeaders(mRSExecutable,
-      (unsigned char*) mEmittedELFExecutable.begin());
-
-  // Once the ELF object has been loaded, populate the various slots for RS
-  // with the appropriate relocated addresses.
-  if (VarCount) {
-    ScriptCompiled::ExportVarList &VarList = mpResult->mExportVars;
-    for (size_t i = 0; i < VarCount; i++) {
-      VarList.push_back(rsloaderGetSymbolAddress(mRSExecutable,
-                                                 VarNameList[i].c_str()));
-    }
-  }
-
-  if (FuncCount) {
-    ScriptCompiled::ExportFuncList &FuncList = mpResult->mExportFuncs;
-    for (size_t i = 0; i < FuncCount; i++) {
-      FuncList.push_back(rsloaderGetSymbolAddress(mRSExecutable,
-                                                  FuncNameList[i].c_str()));
-    }
-  }
-
-  if (ForEachSigCount) {
-    ScriptCompiled::ExportForEachList &ForEachList = mpResult->mExportForEach;
-    for (size_t i = 0; i < ForEachSigCount; i++) {
-      ForEachList.push_back(rsloaderGetSymbolAddress(mRSExecutable,
-          ForEachExpandList[i].c_str()));
-    }
-  }
-
-#if DEBUG_MC_DISASSEMBLER
-  {
-    // Get MC codegen emitted function name list
-    size_t func_list_size = rsloaderGetFuncCount(mRSExecutable);
-    std::vector<char const *> func_list(func_list_size, NULL);
-    rsloaderGetFuncNameList(mRSExecutable, func_list_size, &*func_list.begin());
-
-    // Disassemble each function
-    for (size_t i = 0; i < func_list_size; ++i) {
-      void *func = rsloaderGetSymbolAddress(mRSExecutable, func_list[i]);
-      if (func) {
-        size_t size = rsloaderGetSymbolSize(mRSExecutable, func_list[i]);
-        Disassemble(DEBUG_MC_DISASSEMBLER_FILE,
-                    Target, TM, func_list[i], (unsigned char const *)func, size);
-      }
-    }
-  }
-#endif
-
-on_bcc_compile_error:
-  // ALOGE("on_bcc_compiler_error");
-  if (TD) {
-    delete TD;
-  }
-
-  if (TM) {
-    delete TM;
-  }
-
-  if (mError.empty()) {
-    return 0;
-  }
-
-  // ALOGE(getErrorMessage());
-  return 1;
-}
-
-
-int Compiler::runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM) {
-  // Decorate mEmittedELFExecutable with formatted ostream
-  llvm::raw_svector_ostream OutSVOS(mEmittedELFExecutable);
-
-  // Relax all machine instructions
-  TM->setMCRelaxAll(/* RelaxAll= */ true);
-
-  // Create MC code generation pass manager
-  llvm::PassManager MCCodeGenPasses;
-
-  // Add TargetData to MC code generation pass manager
-  MCCodeGenPasses.add(TD);
-
-  // Add MC code generation passes to pass manager
-  llvm::MCContext *Ctx = NULL;
-  if (TM->addPassesToEmitMC(MCCodeGenPasses, Ctx, OutSVOS, false)) {
-    setError("Fail to add passes to emit file");
-    return 1;
-  }
-
-  MCCodeGenPasses.run(*mModule);
-  OutSVOS.flush();
-  return 0;
-}
-
-int Compiler::runInternalPasses(std::vector<std::string>& Names,
-                                std::vector<uint32_t>& Signatures) {
-  llvm::PassManager BCCPasses;
-
-  // Expand ForEach on CPU path to reduce launch overhead.
-  BCCPasses.add(createForEachExpandPass(Names, Signatures));
-
-  BCCPasses.run(*mModule);
-
-  return 0;
-}
-
-int Compiler::runLTO(llvm::TargetData *TD,
-                     std::vector<const char*>& ExportSymbols,
-                     llvm::CodeGenOpt::Level OptimizationLevel) {
-  // Note: ExportSymbols is a workaround for getting all exported variable,
-  // function, and kernel names.
-  // We should refine it soon.
-
-  // TODO(logan): Remove this after we have finished the
-  // bccMarkExternalSymbol API.
-
-  // root(), init(), and .rs.dtor() are born to be exported
-  ExportSymbols.push_back("root");
-  ExportSymbols.push_back("init");
-  ExportSymbols.push_back(".rs.dtor");
-
-  // User-defined exporting symbols
-  std::vector<char const *> const &UserDefinedExternalSymbols =
-    mpResult->getUserDefinedExternalSymbols();
-
-  std::copy(UserDefinedExternalSymbols.begin(),
-            UserDefinedExternalSymbols.end(),
-            std::back_inserter(ExportSymbols));
-
-  llvm::PassManager LTOPasses;
-
-  // Add TargetData to LTO passes
-  LTOPasses.add(TD);
-
-  // We now create passes list performing LTO. These are copied from
-  // (including comments) llvm::createStandardLTOPasses().
-  // Only a subset of these LTO passes are enabled in optimization level 0
-  // as they interfere with interactive debugging.
-  // FIXME: figure out which passes (if any) makes sense for levels 1 and 2
-
-  if (OptimizationLevel != llvm::CodeGenOpt::None) {
-    // Internalize all other symbols not listed in ExportSymbols
-    LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
-
-    // Propagate constants at call sites into the functions they call. This
-    // opens opportunities for globalopt (and inlining) by substituting
-    // function pointers passed as arguments to direct uses of functions.
-    LTOPasses.add(llvm::createIPSCCPPass());
-
-    // Now that we internalized some globals, see if we can hack on them!
-    LTOPasses.add(llvm::createGlobalOptimizerPass());
-
-    // Linking modules together can lead to duplicated global constants, only
-    // keep one copy of each constant...
-    LTOPasses.add(llvm::createConstantMergePass());
-
-    // Remove unused arguments from functions...
-    LTOPasses.add(llvm::createDeadArgEliminationPass());
-
-    // Reduce the code after globalopt and ipsccp. Both can open up
-    // significant simplification opportunities, and both can propagate
-    // functions through function pointers. When this happens, we often have
-    // to resolve varargs calls, etc, so let instcombine do this.
-    LTOPasses.add(llvm::createInstructionCombiningPass());
-
-    // Inline small functions
-    LTOPasses.add(llvm::createFunctionInliningPass());
-
-    // Remove dead EH info.
-    LTOPasses.add(llvm::createPruneEHPass());
-
-    // Internalize the globals again after inlining
-    LTOPasses.add(llvm::createGlobalOptimizerPass());
-
-    // Remove dead functions.
-    LTOPasses.add(llvm::createGlobalDCEPass());
-
-    // If we didn't decide to inline a function, check to see if we can
-    // transform it to pass arguments by value instead of by reference.
-    LTOPasses.add(llvm::createArgumentPromotionPass());
-
-    // The IPO passes may leave cruft around.  Clean up after them.
-    LTOPasses.add(llvm::createInstructionCombiningPass());
-    LTOPasses.add(llvm::createJumpThreadingPass());
-
-    // Break up allocas
-    LTOPasses.add(llvm::createScalarReplAggregatesPass());
-
-    // Run a few AA driven optimizations here and now, to cleanup the code.
-    LTOPasses.add(llvm::createFunctionAttrsPass());  // Add nocapture.
-    LTOPasses.add(llvm::createGlobalsModRefPass());  // IP alias analysis.
-
-    // Hoist loop invariants.
-    LTOPasses.add(llvm::createLICMPass());
-
-    // Remove redundancies.
-    LTOPasses.add(llvm::createGVNPass());
-
-    // Remove dead memcpys.
-    LTOPasses.add(llvm::createMemCpyOptPass());
-
-    // Nuke dead stores.
-    LTOPasses.add(llvm::createDeadStoreEliminationPass());
-
-    // Cleanup and simplify the code after the scalar optimizations.
-    LTOPasses.add(llvm::createInstructionCombiningPass());
-
-    LTOPasses.add(llvm::createJumpThreadingPass());
-
-    // Delete basic blocks, which optimization passes may have killed.
-    LTOPasses.add(llvm::createCFGSimplificationPass());
-
-    // Now that we have optimized the program, discard unreachable functions.
-    LTOPasses.add(llvm::createGlobalDCEPass());
-
-  } else {
-    LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
-    LTOPasses.add(llvm::createGlobalOptimizerPass());
-    LTOPasses.add(llvm::createConstantMergePass());
-  }
-
-  LTOPasses.run(*mModule);
-
-#if ANDROID_ENGINEERING_BUILD
-  if (0 != gDebugDumpDirectory) {
-    std::string errs;
-    std::string Filename(gDebugDumpDirectory);
-    Filename += "/post-lto-module.ll";
-    llvm::raw_fd_ostream FS(Filename.c_str(), errs);
-    mModule->print(FS, 0);
-    FS.close();
-  }
-#endif
-
-  return 0;
-}
-
-
-void *Compiler::getSymbolAddress(char const *name) {
-  return rsloaderGetSymbolAddress(mRSExecutable, name);
-}
-
-
-void *Compiler::resolveSymbolAdapter(void *context, char const *name) {
-  Compiler *self = reinterpret_cast<Compiler *>(context);
-
-  if (void *Addr = FindRuntimeFunction(name)) {
-    return Addr;
-  }
-
-  if (self->mpSymbolLookupFn) {
-    if (void *Addr = self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
-      return Addr;
-    }
-  }
-
-  ALOGE("Unable to resolve symbol: %s\n", name);
-  return NULL;
-}
-
-
-Compiler::~Compiler() {
-  rsloaderDisposeExec(mRSExecutable);
-
-  // llvm::llvm_shutdown();
-}
-
-
-}  // namespace bcc
diff --git a/lib/ExecutionEngine/Compiler.h b/lib/ExecutionEngine/Compiler.h
deleted file mode 100644
index 863cda6..0000000
--- a/lib/ExecutionEngine/Compiler.h
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-#ifndef BCC_COMPILER_H
-#define BCC_COMPILER_H
-
-#include <bcc/bcc.h>
-
-#include <Config.h>
-
-#include "librsloader.h"
-
-#include "llvm/ADT/OwningPtr.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/Triple.h"
-#include "llvm/Target/TargetMachine.h"
-
-#include <stddef.h>
-
-#include <list>
-#include <string>
-#include <vector>
-#include <utility>
-
-
-namespace llvm {
-  class Module;
-  class NamedMDNode;
-  class TargetData;
-}
-
-
-namespace bcc {
-  class ScriptCompiled;
-  struct CompilerOption;
-
-  class Compiler {
-  private:
-    //////////////////////////////////////////////////////////////////////////
-    // The variable section below (e.g., Triple, CodeGenOptLevel)
-    // is initialized in GlobalInitialization()
-    //
-    static bool GlobalInitialized;
-
-    // If given, this will be the name of the target triple to compile for.
-    // If not given, the initial values defined in this file will be used.
-    static std::string Triple;
-    static llvm::Triple::ArchType ArchType;
-
-    static llvm::CodeGenOpt::Level CodeGenOptLevel;
-
-    // End of section of GlobalInitializing variables
-    /////////////////////////////////////////////////////////////////////////
-    // If given, the name of the target CPU to generate code for.
-    static std::string CPU;
-
-    // The list of target specific features to enable or disable -- this should
-    // be a list of strings starting with '+' (enable) or '-' (disable).
-    static std::vector<std::string> Features;
-
-    static void LLVMErrorHandler(void *UserData, const std::string &Message);
-
-    friend class CodeEmitter;
-    friend class CodeMemoryManager;
-
-  private:
-    ScriptCompiled *mpResult;
-
-    std::string mError;
-
-    // Compilation buffer for MC
-    llvm::SmallVector<char, 1024> mEmittedELFExecutable;
-
-    // Loaded and relocated executable
-    RSExecRef mRSExecutable;
-
-    BCCSymbolLookupFn mpSymbolLookupFn;
-    void *mpSymbolLookupContext;
-
-    llvm::Module *mModule;
-
-    bool mHasLinked;
-
-  public:
-    Compiler(ScriptCompiled *result);
-
-    static void GlobalInitialization();
-
-    static std::string const &getTargetTriple() {
-      return Triple;
-    }
-
-    static llvm::Triple::ArchType getTargetArchType() {
-      return ArchType;
-    }
-
-    void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
-      mpSymbolLookupFn = pFn;
-      mpSymbolLookupContext = pContext;
-    }
-
-    void *getSymbolAddress(char const *name);
-
-    const llvm::SmallVector<char, 1024> &getELF() const {
-      return mEmittedELFExecutable;
-    }
-
-    int readModule(llvm::Module *module) {
-      mModule = module;
-      return hasError();
-    }
-
-    int linkModule(llvm::Module *module);
-
-    int compile(const CompilerOption &option);
-
-    char const *getErrorMessage() {
-      return mError.c_str();
-    }
-
-    const llvm::Module *getModule() const {
-      return mModule;
-    }
-
-    ~Compiler();
-
-  private:
-
-    int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
-                   llvm::NamedMDNode const *ExportVarMetadata,
-                   llvm::NamedMDNode const *ExportFuncMetadata);
-
-    int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
-
-    static void *resolveSymbolAdapter(void *context, char const *name);
-
-    int runInternalPasses(std::vector<std::string>& Names,
-                          std::vector<uint32_t>& Signatures);
-
-    int runLTO(llvm::TargetData *TD,
-               std::vector<const char*>& ExportSymbols,
-               llvm::CodeGenOpt::Level OptimizationLevel);
-
-    bool hasError() const {
-      return !mError.empty();
-    }
-
-    void setError(const char *Error) {
-      mError.assign(Error);  // Copying
-    }
-
-    void setError(const std::string &Error) {
-      mError = Error;
-    }
-
-  };  // End of class Compiler
-
-} // namespace bcc
-
-#endif // BCC_COMPILER_H
diff --git a/lib/ExecutionEngine/CompilerOption.h b/lib/ExecutionEngine/CompilerOption.h
deleted file mode 100644
index 75278c7..0000000
--- a/lib/ExecutionEngine/CompilerOption.h
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright 2012, 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.
- */
-
-#ifndef BCC_COMPILER_OPTION_H
-#define BCC_COMPILER_OPTION_H
-
-#include "Config.h"
-#include "Compiler.h"
-
-#include "llvm/Target/TargetOptions.h"
-#include "llvm/Support/CodeGen.h"
-
-namespace bcc {
-
-class CompilerOption {
- public:
-  // Constructor setup "default configuration". The "default configuration"
-  // here means the configuration for running RenderScript (more specifically,
-  // one can declare a CompilerOption object (call default constructor) and then
-  // pass to the Compiler::compiler() without any modification for RenderScript,
-  // see Script::prepareExecutable(...)).
-  //
-  // Must be invoked after calling Compiler::GlobalInitialization() at least once.
-  //
-  CompilerOption() {
-    //-- Setup options to llvm::TargetMachine --//
-
-    //-- Setup Frame Pointer Elimination Optimization --//
-#if defined(__HOST__)
-    // Disable frame pointer elimination optimization for X86_64 and X86
-    if ((Compiler::getTargetArchType() == llvm::Triple::x86_64) ||
-        (Compiler::getTargetArchType() == llvm::Triple::x86)) {
-      TargetOpt.NoFramePointerElim = true;
-    } else {
-      TargetOpt.NoFramePointerElim = false;
-    }
-#elif defined(DEFAULT_X86_64_CODEGEN)
-    TargetOpt.NoFramePointerElim = true;
-#elif defined(DEFAULT_X86_CODEGEN)
-    TargetOpt.NoFramePointerElim = true;
-#else
-    TargetOpt.NoFramePointerElim = false;
-#endif
-
-    // Use hardfloat ABI
-    //
-    // TODO(all): Need to detect the CPU capability and decide whether to use
-    // softfp. To use softfp, change following 2 lines to
-    //
-    // options.FloatABIType = llvm::FloatABI::Soft;
-    // options.UseSoftFloat = true;
-    TargetOpt.FloatABIType = llvm::FloatABI::Soft;
-    TargetOpt.UseSoftFloat = false;
-
-    //-- Setup relocation model  --//
-    RelocModelOpt = llvm::Reloc::Static;
-
-    //-- Setup code model  --//
-#if defined(__HOST__)
-    // Data address in X86_64 architecture may reside in a far-away place
-    if (Compiler::getTargetArchType() == llvm::Triple::x86_64) {
-      CodeModelOpt = llvm::CodeModel::Medium;
-    } else {
-      CodeModelOpt = llvm::CodeModel::Small;
-    }
-#elif defined(DEFAULT_X86_64_CODEGEN)
-    CodeModelOpt = llvm::CodeModel::Medium;
-#else
-    CodeModelOpt = llvm::CodeModel::Small;
-#endif
-
-    //-- Load the result object after successful compilation  --//
-    LoadAfterCompile = true;
-  }
-
-  llvm::TargetOptions TargetOpt;
-  llvm::CodeModel::Model CodeModelOpt;
-  llvm::Reloc::Model RelocModelOpt;
-  bool LoadAfterCompile;
-
-};
-
-} // namespace bcc
-
-#endif  // BCC_COMPILER_OPTION_H
diff --git a/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp b/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
new file mode 100644
index 0000000..4525daa
--- /dev/null
+++ b/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2012, 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 "ELFObjectLoaderImpl.h"
+
+#include <llvm/Support/ELF.h>
+
+// The following files are included from librsloader.
+#include "ELFObject.h"
+#include "ELFSectionSymTab.h"
+#include "ELFSymbol.h"
+#include "utils/serialize.h"
+
+#include "bcc/ExecutionEngine/SymbolResolverInterface.h"
+#include "bcc/Support/Log.h"
+
+using namespace bcc;
+
+bool ELFObjectLoaderImpl::load(const void *pMem, size_t pMemSize) {
+  ArchiveReaderLE reader(reinterpret_cast<const unsigned char *>(pMem),
+                         pMemSize);
+
+  mObject = ELFObject<32>::read(reader);
+  if (mObject == NULL) {
+    ALOGE("Unable to load the ELF object!");
+    return false;
+  }
+
+  // Retrive the pointer to the symbol table.
+  mSymTab = static_cast<ELFSectionSymTab<32> *>(
+                mObject->getSectionByName(".symtab"));
+  if (mSymTab == NULL) {
+    ALOGW("Object doesn't contain any symbol table.");
+  }
+
+  return true;
+}
+
+bool ELFObjectLoaderImpl::relocate(SymbolResolverInterface &pResolver) {
+  mObject->relocate(SymbolResolverInterface::LookupFunction, &pResolver);
+
+  if (mObject->getMissingSymbols()) {
+    ALOGE("Some symbols are found to be undefined during relocation!");
+    return false;
+  }
+
+  return true;
+}
+
+bool ELFObjectLoaderImpl::prepareDebugImage(void *pDebugImg,
+                                            size_t pDebugImgSize) {
+  // Update the value of sh_addr in pDebugImg to its corresponding section in
+  // the mObject.
+  llvm::ELF::Elf32_Ehdr *elf_header =
+      reinterpret_cast<llvm::ELF::Elf32_Ehdr *>(pDebugImg);
+
+  if (elf_header->e_shoff > pDebugImgSize) {
+    ALOGE("Invalid section header table offset found! (e_shoff = %d)",
+          elf_header->e_shoff);
+    return false;
+  }
+
+  if ((elf_header->e_shoff +
+       sizeof(llvm::ELF::Elf32_Shdr) * elf_header->e_shnum) > pDebugImgSize) {
+    ALOGE("Invalid image supplied (debug image doesn't contain all the section"
+          "header or corrupted image)! (e_shoff = %d, e_shnum = %d)",
+          elf_header->e_shoff, elf_header->e_shnum);
+    return false;
+  }
+
+  llvm::ELF::Elf32_Shdr *section_header_table =
+      reinterpret_cast<llvm::ELF::Elf32_Shdr *>(
+          reinterpret_cast<uint8_t*>(pDebugImg) + elf_header->e_shoff);
+
+  for (unsigned i = 0; i < elf_header->e_shnum; i++) {
+    if (section_header_table[i].sh_flags & llvm::ELF::SHF_ALLOC) {
+      ELFSectionBits<32> *section =
+          static_cast<ELFSectionBits<32> *>(mObject->getSectionByIndex(i));
+      if (section != NULL) {
+        section_header_table[i].sh_addr =
+            reinterpret_cast<llvm::ELF::Elf32_Addr>(section->getBuffer());
+      }
+    }
+  }
+
+  return true;
+}
+
+void *ELFObjectLoaderImpl::getSymbolAddress(const char *pName) const {
+  if (mSymTab == NULL) {
+    return NULL;
+  }
+
+  const ELFSymbol<32> *symbol = mSymTab->getByName(pName);
+  if (symbol == NULL) {
+    ALOGV("Request symbol '%s' is not found in the object!", pName);
+    return NULL;
+  }
+
+  return symbol->getAddress(mObject->getHeader()->getMachine(),
+                            /* autoAlloc */false);
+}
+
+size_t ELFObjectLoaderImpl::getSymbolSize(const char *pName) const {
+  if (mSymTab == NULL) {
+    return 0;
+  }
+
+  const ELFSymbol<32> *symbol = mSymTab->getByName(pName);
+
+  if (symbol == NULL) {
+    ALOGV("Request symbol '%s' is not found in the object!", pName);
+    return 0;
+  }
+
+  return static_cast<size_t>(symbol->getSize());
+
+}
+
+bool
+ELFObjectLoaderImpl::getSymbolNameList(android::Vector<const char *>& pNameList,
+                                       ObjectLoader::SymbolType pType) const {
+  if (mSymTab == NULL) {
+    return false;
+  }
+
+  unsigned elf_type;
+  switch (pType) {
+    case ObjectLoader::kFunctionType: {
+      elf_type = llvm::ELF::STT_FUNC;
+      break;
+    }
+    case ObjectLoader::kUnknownType: {
+      break;
+    }
+    default: {
+      assert(false && "Invalid symbol type given!");
+      return false;
+    }
+  }
+
+  for (size_t i = 0, e = mSymTab->size(); i != e; i++) {
+    ELFSymbol<32> *symbol = (*mSymTab)[i];
+    if (symbol == NULL) {
+      continue;
+    }
+
+    if ((pType == ObjectLoader::kUnknownType) ||
+        (symbol->getType() == elf_type)) {
+      const char *symbol_name = symbol->getName();
+      if (symbol_name != NULL) {
+        pNameList.push_back(symbol_name);
+      }
+    }
+  }
+
+  return true;
+}
+
+ELFObjectLoaderImpl::~ELFObjectLoaderImpl() {
+  delete mObject;
+  return;
+}
diff --git a/lib/ExecutionEngine/ELFObjectLoaderImpl.h b/lib/ExecutionEngine/ELFObjectLoaderImpl.h
new file mode 100644
index 0000000..bc96eb4
--- /dev/null
+++ b/lib/ExecutionEngine/ELFObjectLoaderImpl.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef BCC_EXECUTION_ENGINE_ELF_OBJECT_LOADER_IMPL_H
+#define BCC_EXECUTION_ENGINE_ELF_OBJECT_LOADER_IMPL_H
+
+#include "ObjectLoaderImpl.h"
+
+// ELFObject and ELFSectionSymTab comes from librsloader. They're both
+// defined under global scope without a namespace enclosed.
+template <unsigned Bitwidth>
+class ELFObject;
+
+template <unsigned Bitwidth>
+class ELFSectionSymTab;
+
+namespace bcc {
+
+class ELFObjectLoaderImpl : public ObjectLoaderImpl {
+private:
+  ELFObject<32> *mObject;
+  ELFSectionSymTab<32> *mSymTab;
+
+public:
+  ELFObjectLoaderImpl() : ObjectLoaderImpl(), mObject(NULL), mSymTab(NULL) { }
+
+  virtual bool load(const void *pMem, size_t pMemSize);
+
+  virtual bool relocate(SymbolResolverInterface &pResolver);
+
+  virtual bool prepareDebugImage(void *pDebugImg, size_t pDebugImgSize);
+
+  virtual void *getSymbolAddress(const char *pName) const;
+
+  virtual size_t getSymbolSize(const char *pName) const;
+
+  virtual bool getSymbolNameList(android::Vector<const char *>& pNameList,
+                                 ObjectLoader::SymbolType pType) const;
+  ~ELFObjectLoaderImpl();
+};
+
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_ELF_OBJECT_LOADER_IMPL_H
diff --git a/lib/ExecutionEngine/FileHandle.cpp b/lib/ExecutionEngine/FileHandle.cpp
deleted file mode 100644
index c0a344c..0000000
--- a/lib/ExecutionEngine/FileHandle.cpp
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * Copyright 2010, 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 "FileHandle.h"
-
-#include "DebugHelper.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <string.h>
-
-namespace bcc {
-
-int FileHandle::open(char const *filename, OpenMode::ModeType mode) {
-  static int const open_flags[2] = {
-    O_RDONLY,
-    O_RDWR | O_CREAT | O_TRUNC,
-  };
-
-  static int const lock_flags[2] = { LOCK_SH, LOCK_EX };
-
-#if USE_LOGGER
-  static char const *const open_mode_str[2] = { "read", "write" };
-#endif
-
-  static size_t const RETRY_MAX = 4;
-
-  static useconds_t const RETRY_USEC = 200000UL;
-
-  for (size_t i = 0; i < RETRY_MAX; ++i) {
-    // Try to open the file
-    mFD = ::open(filename, open_flags[mode], 0644);
-
-    if (mFD < 0) {
-      if (errno == EINTR) {
-        // Interrupt occurs while opening the file.  Retry.
-        continue;
-      }
-
-      ALOGW("Unable to open %s in %s mode.  (reason: %s)\n",
-           filename, open_mode_str[mode], strerror(errno));
-
-      return -1;
-    }
-
-    // Try to lock the file
-    if (flock(mFD, lock_flags[mode] | LOCK_NB) < 0) {
-      ALOGW("Unable to acquire the lock immediately, block and wait now ...\n");
-
-      if (flock(mFD, lock_flags[mode]) < 0) {
-        ALOGE("Unable to acquire the lock. Retry ...\n");
-
-        ::close(mFD);
-        mFD = -1;
-
-        usleep(RETRY_USEC);
-        continue;
-      }
-    }
-
-    // Note: From now on, the object is correctly initialized.  We have to
-    // use this->close() to close the file now.
-
-    // Check rather we have locked the correct file or not
-    struct stat sfd, sfname;
-
-    if (fstat(mFD, &sfd) == -1 || stat(filename, &sfname) == -1 ||
-        sfd.st_dev != sfname.st_dev || sfd.st_ino != sfname.st_ino) {
-      // The file we locked is different from the given path.  This may
-      // occur when someone changes the file node before we lock the file.
-      // Just close the file, and retry after sleeping.
-
-      this->close();
-      usleep(RETRY_USEC);
-      continue;
-    }
-
-    // Good, we have open and lock the file correctly.
-    ALOGV("File opened. fd=%d\n", mFD);
-    return mFD;
-  }
-
-  ALOGW("Unable to open %s in %s mode.\n", filename, open_mode_str[mode]);
-  return -1;
-}
-
-
-void FileHandle::close() {
-  if (mFD >= 0) {
-    flock(mFD, LOCK_UN);
-    ::close(mFD);
-    ALOGV("File closed. fd=%d\n", mFD);
-    mFD = -1;
-  }
-}
-
-
-ssize_t FileHandle::read(char *buf, size_t count) {
-  if (mFD < 0) {
-    return -1;
-  }
-
-  while (true) {
-    ssize_t nread = ::read(mFD, static_cast<void *>(buf), count);
-
-    if (nread >= 0) {
-      return nread;
-    }
-
-    if (errno != EAGAIN && errno != EINTR) {
-      // If the errno is EAGAIN or EINTR, then we try to read again.
-      // Otherwise, consider this is a failure.  And returns zero.
-      return -1;
-    }
-  }
-
-  // Unreachable
-  return -1;
-}
-
-
-ssize_t FileHandle::write(char const *buf, size_t count) {
-  if (mFD < 0) {
-    return -1;
-  }
-
-  ssize_t written = 0;
-
-  while (count > 0) {
-    ssize_t nwrite = ::write(mFD, static_cast<void const *>(buf), count);
-
-    if (nwrite < 0) {
-      if (errno != EAGAIN && errno != EINTR) {
-        return written;
-      }
-
-      continue;
-    }
-
-    written += nwrite;
-    count -= (size_t)nwrite;
-    buf += (size_t)nwrite;
-  }
-
-  return written;
-}
-
-
-off_t FileHandle::seek(off_t offset, int whence) {
-  return (mFD < 0) ? -1 : lseek(mFD, offset, whence);
-}
-
-
-void FileHandle::truncate() {
-  if (mFD >= 0) {
-    if (ftruncate(mFD, 0) != 0) {
-      ALOGE("Unable to truncate the file.\n");
-    }
-  }
-}
-
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/FileHandle.h b/lib/ExecutionEngine/FileHandle.h
deleted file mode 100644
index 02dbe31..0000000
--- a/lib/ExecutionEngine/FileHandle.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright 2010, 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.
- */
-
-#ifndef BCC_FILEHANDLE_H
-#define BCC_FILEHANDLE_H
-
-#include <sys/types.h>
-
-#include <stddef.h>
-#include <stdint.h>
-
-namespace bcc {
-  namespace OpenMode {
-    enum ModeType {
-      Read = 0,
-      Write = 1,
-    };
-  }
-
-  class FileHandle {
-  private:
-    int mFD;
-
-  public:
-    FileHandle() : mFD(-1) {
-    }
-
-    ~FileHandle() {
-      if (mFD >= 0) {
-        close();
-      }
-    }
-
-    int open(char const *filename, OpenMode::ModeType mode);
-
-    void close();
-
-    int getFD() {
-      // Note: This function is designed not being qualified by const.
-      // Because once the file descriptor is given, the user can do every
-      // thing on file descriptor.
-
-      return mFD;
-    }
-
-    off_t seek(off_t offset, int whence);
-
-    ssize_t read(char *buf, size_t count);
-
-    ssize_t write(char const *buf, size_t count);
-
-    void truncate();
-
-  };
-
-} // namespace bcc
-
-#endif // BCC_FILEHANDLE_H
diff --git a/lib/ExecutionEngine/GDBJIT.cpp b/lib/ExecutionEngine/GDBJIT.cpp
index 4038098..29944dd 100644
--- a/lib/ExecutionEngine/GDBJIT.cpp
+++ b/lib/ExecutionEngine/GDBJIT.cpp
@@ -29,7 +29,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Support/Compiler.h"
+#include <llvm/Support/Compiler.h>
 
 // This interface must be kept in sync with gdb/gdb/jit.h .
 extern "C" {
diff --git a/lib/ExecutionEngine/GDBJITRegistrar.cpp b/lib/ExecutionEngine/GDBJITRegistrar.cpp
index 5d8428a..68d6833 100644
--- a/lib/ExecutionEngine/GDBJITRegistrar.cpp
+++ b/lib/ExecutionEngine/GDBJITRegistrar.cpp
@@ -30,13 +30,15 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "GDBJITRegistrar.h"
-#include "llvm/ADT/DenseMap.h"
-#include "GDBJIT.h"
-#include "llvm/Support/Memory.h"
-#include "llvm/Support/MutexGuard.h"
-#include "llvm/Support/Mutex.h"
-#include "llvm/Support/ErrorHandling.h"
+#include "bcc/ExecutionEngine/GDBJITRegistrar.h"
+
+#include <llvm/ADT/DenseMap.h>
+#include <llvm/Support/ErrorHandling.h>
+#include <llvm/Support/Memory.h>
+#include <llvm/Support/Mutex.h>
+#include <llvm/Support/MutexGuard.h>
+
+#include "bcc/ExecutionEngine/GDBJIT.h"
 
 #include <fstream>
 
diff --git a/lib/ExecutionEngine/MCCacheReader.cpp b/lib/ExecutionEngine/MCCacheReader.cpp
deleted file mode 100644
index bf4fe47..0000000
--- a/lib/ExecutionEngine/MCCacheReader.cpp
+++ /dev/null
@@ -1,491 +0,0 @@
-/*
- * Copyright 2010-2012, 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 "MCCacheReader.h"
-
-#include "DebugHelper.h"
-#include "FileHandle.h"
-#include "ScriptCached.h"
-#include "Runtime.h"
-
-#include <bcc/bcc_mccache.h>
-
-#include <llvm/ADT/OwningPtr.h>
-
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <utility>
-#include <vector>
-
-#include <new>
-
-#include <stdlib.h>
-#include <string.h>
-
-using namespace std;
-
-namespace bcc {
-
-MCCacheReader::~MCCacheReader() {
-  if (mpHeader) { free(mpHeader); }
-  if (mpCachedDependTable) { free(mpCachedDependTable); }
-  if (mpPragmaList) { free(mpPragmaList); }
-  if (mpVarNameList) { free(mpVarNameList); }
-  if (mpFuncNameList) { free(mpFuncNameList); }
-}
-
-ScriptCached *MCCacheReader::readCacheFile(FileHandle *objFile,
-                                           FileHandle *infoFile,
-                                           Script *S) {
-  bool result = checkCacheFile(objFile, infoFile, S)
-             && readPragmaList()
-             && readObjectSlotList()
-             && readObjFile()
-             && readVarNameList()
-             && readFuncNameList()
-             && readForEachNameList()
-             //&& relocate()
-             ;
-
-  return result ? mpResult.take() : NULL;
-}
-
-bool MCCacheReader::checkCacheFile(FileHandle *objFile,
-                                            FileHandle *infoFile,
-                                            Script *S) {
-  // Check file handle
-  if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) {
-    return false;
-  }
-
-  mObjFile = objFile;
-  mInfoFile = infoFile;
-
-  // Allocate ScriptCached object
-  mpResult.reset(new (nothrow) ScriptCached(S));
-
-  if (!mpResult) {
-    ALOGE("Unable to allocate ScriptCached object.\n");
-    return false;
-  }
-
-  bool result = checkFileSize()
-             && readHeader()
-             && checkHeader()
-             && checkMachineIntType()
-             && checkSectionOffsetAndSize()
-             && readStringPool()
-             && checkStringPool()
-             && readDependencyTable()
-             && checkDependency()
-             ;
-
-  return result;
-}
-
-
-bool MCCacheReader::checkFileSize() {
-  struct stat stfile;
-  if (fstat(mInfoFile->getFD(), &stfile) < 0) {
-    ALOGE("Unable to stat cache file.\n");
-    return false;
-  }
-
-  mInfoFileSize = stfile.st_size;
-
-  if (mInfoFileSize < (off_t)sizeof(MCO_Header)) {
-    ALOGE("Cache file is too small to be correct.\n");
-    return false;
-  }
-
-  return true;
-}
-
-
-bool MCCacheReader::readHeader() {
-  if (mInfoFile->seek(0, SEEK_SET) != 0) {
-    ALOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
-    return false;
-  }
-
-  mpHeader = (MCO_Header *)malloc(sizeof(MCO_Header));
-  if (!mpHeader) {
-    ALOGE("Unable to allocate for cache header.\n");
-    return false;
-  }
-
-  if (mInfoFile->read(reinterpret_cast<char *>(mpHeader), sizeof(MCO_Header)) !=
-      (ssize_t)sizeof(MCO_Header)) {
-    ALOGE("Unable to read cache header.\n");
-    return false;
-  }
-
-  // Dirty hack for libRS.
-  // TODO(all): This should be removed in the future.
-  if (mpHeader->libRS_threadable) {
-    mpResult->mLibRSThreadable = true;
-  }
-
-  return true;
-}
-
-
-bool MCCacheReader::checkHeader() {
-  if (memcmp(mpHeader->magic, MCO_MAGIC, 4) != 0) {
-    ALOGE("Bad magic word\n");
-    return false;
-  }
-
-  if (memcmp(mpHeader->version, MCO_VERSION, 4) != 0) {
-    mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
-    ALOGI("Cache file format version mismatch: now %s cached %s\n",
-         MCO_VERSION, mpHeader->version);
-    return false;
-  }
-  return true;
-}
-
-
-bool MCCacheReader::checkMachineIntType() {
-  uint32_t number = 0x00000001;
-
-  bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
-  if ((isLittleEndian && mpHeader->endianness != 'e') ||
-      (!isLittleEndian && mpHeader->endianness != 'E')) {
-    ALOGE("Machine endianness mismatch.\n");
-    return false;
-  }
-
-  if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
-      (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
-      (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
-    ALOGE("Machine integer size mismatch.\n");
-    return false;
-  }
-
-  return true;
-}
-
-
-bool MCCacheReader::checkSectionOffsetAndSize() {
-#define CHECK_SECTION_OFFSET(NAME)                                          \
-  do {                                                                      \
-    off_t offset = mpHeader-> NAME##_offset;                                \
-    off_t size = (off_t)mpHeader-> NAME##_size;                             \
-                                                                            \
-    if (mInfoFileSize < offset || mInfoFileSize < offset + size) {          \
-      ALOGE(#NAME " section overflow.\n");                                   \
-      return false;                                                         \
-    }                                                                       \
-                                                                            \
-    if (offset % sizeof(int) != 0) {                                        \
-      ALOGE(#NAME " offset must aligned to %d.\n", (int)sizeof(int));        \
-      return false;                                                         \
-    }                                                                       \
-                                                                            \
-    if (size < static_cast<off_t>(sizeof(size_t))) {                        \
-      ALOGE(#NAME " size is too small to be correct.\n");                    \
-      return false;                                                         \
-    }                                                                       \
-  } while (0)
-
-  CHECK_SECTION_OFFSET(str_pool);
-  CHECK_SECTION_OFFSET(depend_tab);
-  //CHECK_SECTION_OFFSET(reloc_tab);
-  CHECK_SECTION_OFFSET(pragma_list);
-
-#undef CHECK_SECTION_OFFSET
-
-  return true;
-}
-
-
-#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME)          \
-  TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size);                 \
-                                                                            \
-  if (!NAME##_raw) {                                                        \
-    ALOGE("Unable to allocate for " #NAME "\n");                             \
-    return false;                                                           \
-  }                                                                         \
-                                                                            \
-  /* We have to ensure that some one will deallocate NAME##_raw */          \
-  AUTO_MANAGED_HOLDER = NAME##_raw;                                         \
-                                                                            \
-  if (mInfoFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) {           \
-    ALOGE("Unable to seek to " #NAME " section\n");                          \
-    return false;                                                           \
-  }                                                                         \
-                                                                            \
-  if (mInfoFile->read(reinterpret_cast<char *>(NAME##_raw),                 \
-                  mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
-  {                                                                         \
-    ALOGE("Unable to read " #NAME ".\n");                                    \
-    return false;                                                           \
-  }
-
-
-bool MCCacheReader::readStringPool() {
-  CACHE_READER_READ_SECTION(MCO_StringPool,
-                            mpResult->mpStringPoolRaw, str_pool);
-
-  char *str_base = reinterpret_cast<char *>(str_pool_raw);
-
-  vector<char const *> &pool = mpResult->mStringPool;
-  for (size_t i = 0; i < str_pool_raw->count; ++i) {
-    char *str = str_base + str_pool_raw->list[i].offset;
-    pool.push_back(str);
-  }
-
-  return true;
-}
-
-
-bool MCCacheReader::checkStringPool() {
-  MCO_StringPool *poolR = mpResult->mpStringPoolRaw;
-  vector<char const *> &pool = mpResult->mStringPool;
-
-  // Ensure that every c-style string is ended with '\0'
-  for (size_t i = 0; i < poolR->count; ++i) {
-    if (pool[i][poolR->list[i].length] != '\0') {
-      ALOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
-      return false;
-    }
-  }
-
-  return true;
-}
-
-
-bool MCCacheReader::readDependencyTable() {
-  CACHE_READER_READ_SECTION(MCO_DependencyTable, mpCachedDependTable,
-                            depend_tab);
-  return true;
-}
-
-
-bool MCCacheReader::checkDependency() {
-  if (mDependencies.size() != mpCachedDependTable->count) {
-    ALOGE("Dependencies count mismatch. (%lu vs %lu)\n",
-         (unsigned long)mDependencies.size(),
-         (unsigned long)mpCachedDependTable->count);
-    return false;
-  }
-
-  vector<char const *> &strPool = mpResult->mStringPool;
-  map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
-
-  dep = mDependencies.begin();
-  for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
-    string const &depName = dep->first;
-    uint32_t depType = dep->second.first;
-    unsigned char const *depSHA1 = dep->second.second;
-
-    MCO_Dependency *depCached =&mpCachedDependTable->table[i];
-    char const *depCachedName = strPool[depCached->res_name_strp_index];
-    uint32_t depCachedType = depCached->res_type;
-    unsigned char const *depCachedSHA1 = depCached->sha1;
-
-    if (depName != depCachedName) {
-      ALOGE("Cache dependency name mismatch:\n");
-      ALOGE("  given:  %s\n", depName.c_str());
-      ALOGE("  cached: %s\n", depCachedName);
-
-      return false;
-    }
-
-    if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
-      ALOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
-
-#define PRINT_SHA1(PREFIX, X, POSTFIX) \
-      ALOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
-                  "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
-           X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
-           X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
-
-      PRINT_SHA1("  given:  ", depSHA1, "\n");
-      PRINT_SHA1("  cached: ", depCachedSHA1, "\n");
-
-#undef PRINT_SHA1
-
-      return false;
-    }
-
-    if (depType != depCachedType) {
-      ALOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
-      return false;
-    }
-  }
-
-  return true;
-}
-
-bool MCCacheReader::readVarNameList() {
-  CACHE_READER_READ_SECTION(MCO_String_Ptr, mpVarNameList, export_var_name_list);
-  vector<char const *> const &strPool = mpResult->mStringPool;
-
-  mpResult->mpExportVars = (MCO_ExportVarList*)
-                            malloc(sizeof(size_t) +
-                                   sizeof(void*) * export_var_name_list_raw->count);
-  if (!mpResult->mpExportVars) {
-    ALOGE("Unable to allocate for mpExportVars\n");
-    return false;
-  }
-  mpResult->mpExportVars->count = export_var_name_list_raw->count;
-
-  for (size_t i = 0; i < export_var_name_list_raw->count; ++i) {
-    mpResult->mpExportVars->cached_addr_list[i] =
-      rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_var_name_list_raw->strp_indexs[i]]);
-#if DEBUG_MC_REFLECT
-    ALOGD("Get symbol address: %s -> %p",
-      strPool[export_var_name_list_raw->strp_indexs[i]], mpResult->mpExportVars->cached_addr_list[i]);
-#endif
-  }
-  return true;
-}
-
-bool MCCacheReader::readFuncNameList() {
-  CACHE_READER_READ_SECTION(MCO_String_Ptr, mpFuncNameList, export_func_name_list);
-  vector<char const *> const &strPool = mpResult->mStringPool;
-
-  mpResult->mpExportFuncs = (MCO_ExportFuncList*)
-                            malloc(sizeof(size_t) +
-                                   sizeof(void*) * export_func_name_list_raw->count);
-  if (!mpResult->mpExportFuncs) {
-    ALOGE("Unable to allocate for mpExportFuncs\n");
-    return false;
-  }
-  mpResult->mpExportFuncs->count = export_func_name_list_raw->count;
-
-  for (size_t i = 0; i < export_func_name_list_raw->count; ++i) {
-    mpResult->mpExportFuncs->cached_addr_list[i] =
-      rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_func_name_list_raw->strp_indexs[i]]);
-#if DEBUG_MC_REFLECT
-    ALOGD("Get function address: %s -> %p",
-      strPool[export_func_name_list_raw->strp_indexs[i]], mpResult->mpExportFuncs->cached_addr_list[i]);
-#endif
-  }
-  return true;
-}
-
-bool MCCacheReader::readForEachNameList() {
-  CACHE_READER_READ_SECTION(MCO_String_Ptr, mpForEachNameList, export_foreach_name_list);
-  vector<char const *> const &strPool = mpResult->mStringPool;
-
-  mpResult->mpExportForEach = (MCO_ExportForEachList*)
-                              malloc(sizeof(size_t) +
-                                     sizeof(void*) * export_foreach_name_list_raw->count);
-  if (!mpResult->mpExportForEach) {
-    ALOGE("Unable to allocate for mpExportForEach\n");
-    return false;
-  }
-  mpResult->mpExportForEach->count = export_foreach_name_list_raw->count;
-
-  for (size_t i = 0; i < export_foreach_name_list_raw->count; ++i) {
-    mpResult->mpExportForEach->cached_addr_list[i] =
-      rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_foreach_name_list_raw->strp_indexs[i]]);
-#if DEBUG_MC_REFLECT
-    ALOGE("Get foreach function address: %s -> %p",
-      strPool[export_foreach_name_list_raw->strp_indexs[i]], mpResult->mpExportForEach->cached_addr_list[i]);
-#endif
-  }
-  return true;
-}
-
-bool MCCacheReader::readPragmaList() {
-  CACHE_READER_READ_SECTION(MCO_PragmaList, mpPragmaList, pragma_list);
-
-  vector<char const *> const &strPool = mpResult->mStringPool;
-  ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
-
-  for (size_t i = 0; i < pragma_list_raw->count; ++i) {
-    MCO_Pragma *pragma = &pragma_list_raw->list[i];
-    pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
-                                strPool[pragma->value_strp_index]));
-  }
-
-  return true;
-}
-
-
-bool MCCacheReader::readObjectSlotList() {
-  CACHE_READER_READ_SECTION(MCO_ObjectSlotList,
-                            mpResult->mpObjectSlotList, object_slot_list);
-  return true;
-}
-
-void *MCCacheReader::resolveSymbolAdapter(void *context, char const *name) {
-  MCCacheReader *self = reinterpret_cast<MCCacheReader *>(context);
-
-  if (void *Addr = FindRuntimeFunction(name)) {
-    return Addr;
-  }
-
-  if (self->mpSymbolLookupFn) {
-    if (void *Addr =
-        self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
-      return Addr;
-    }
-  }
-
-  ALOGE("Unable to resolve symbol: %s\n", name);
-  return NULL;
-}
-
-bool MCCacheReader::readObjFile() {
-  if (mpResult->mCachedELFExecutable.size() != 0) {
-    ALOGE("Attempted to read cached object into a non-empty script");
-    return false;
-  }
-  char readBuffer[1024];
-  int readSize;
-  while ((readSize = mObjFile->read(readBuffer, 1024)) > 0) {
-    mpResult->mCachedELFExecutable.append(readBuffer, readBuffer + readSize);
-  }
-  if (readSize != 0) {
-    ALOGE("Read file Error");
-    return false;
-  }
-  ALOGD("Read object file size %d", (int)mpResult->mCachedELFExecutable.size());
-  mpResult->mRSExecutable =
-  rsloaderCreateExec((unsigned char *)&*(mpResult->mCachedELFExecutable.begin()),
-                     mpResult->mCachedELFExecutable.size(),
-                     &resolveSymbolAdapter, this);
-
-  // Point ELF section headers to location of executable code, otherwise
-  // execution through GDB stops unexpectedly as GDB translates breakpoints
-  // in JITted code incorrectly (and complains about being unable to insert
-  // breakpoint at an invalid address)
-  rsloaderUpdateSectionHeaders(mpResult->mRSExecutable,
-    (unsigned char*) mpResult->mCachedELFExecutable.begin());
-
-  return true;
-}
-
-#undef CACHE_READER_READ_SECTION
-
-bool MCCacheReader::readRelocationTable() {
-  // TODO(logan): Not finished.
-  return true;
-}
-
-
-bool MCCacheReader::relocate() {
-  return true;
-}
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/MCCacheReader.h b/lib/ExecutionEngine/MCCacheReader.h
deleted file mode 100644
index df4aca3..0000000
--- a/lib/ExecutionEngine/MCCacheReader.h
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-#ifndef BCC_MCCACHEREADER_H
-#define BCC_MCCACHEREADER_H
-
-#include "ScriptCached.h"
-
-#include <llvm/ADT/OwningPtr.h>
-
-#include <map>
-#include <string>
-#include <utility>
-
-#include <stddef.h>
-#include <stdint.h>
-
-struct MCO_Header;
-
-namespace bcc {
-  class FileHandle;
-  class Script;
-
-  class MCCacheReader {
-  private:
-    FileHandle *mObjFile, *mInfoFile;
-    off_t mInfoFileSize;
-
-    MCO_Header *mpHeader;
-    MCO_DependencyTable *mpCachedDependTable;
-    MCO_PragmaList *mpPragmaList;
-    MCO_FuncTable *mpFuncTable;
-
-    MCO_String_Ptr *mpVarNameList;
-    MCO_String_Ptr *mpFuncNameList;
-    MCO_String_Ptr *mpForEachNameList;
-
-    llvm::OwningPtr<ScriptCached> mpResult;
-
-    std::map<std::string,
-             std::pair<uint32_t, unsigned char const *> > mDependencies;
-
-    bool mIsContextSlotNotAvail;
-
-    BCCSymbolLookupFn mpSymbolLookupFn;
-    void *mpSymbolLookupContext;
-
-  public:
-    MCCacheReader()
-      : mObjFile(NULL), mInfoFile(NULL), mInfoFileSize(0), mpHeader(NULL),
-        mpCachedDependTable(NULL), mpPragmaList(NULL),
-        mpVarNameList(NULL), mpFuncNameList(NULL), mpForEachNameList(NULL),
-        mIsContextSlotNotAvail(false) {
-    }
-
-    ~MCCacheReader();
-
-    void addDependency(MCO_ResourceType resType,
-                       std::string const &resName,
-                       unsigned char const *sha1) {
-      mDependencies.insert(std::make_pair(resName,
-                           std::make_pair((uint32_t)resType, sha1)));
-    }
-
-    ScriptCached *readCacheFile(FileHandle *objFile, FileHandle *infoFile, Script *s);
-    bool checkCacheFile(FileHandle *objFile, FileHandle *infoFile, Script *S);
-
-    bool isContextSlotNotAvail() const {
-      return mIsContextSlotNotAvail;
-    }
-
-    void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
-      mpSymbolLookupFn = pFn;
-      mpSymbolLookupContext = pContext;
-    }
-
-  private:
-    bool readHeader();
-    bool readStringPool();
-    bool readDependencyTable();
-    bool readPragmaList();
-    bool readObjectSlotList();
-    bool readObjFile();
-    bool readRelocationTable();
-
-    bool readVarNameList();
-    bool readFuncNameList();
-    bool readForEachNameList();
-
-    bool checkFileSize();
-    bool checkHeader();
-    bool checkMachineIntType();
-    bool checkSectionOffsetAndSize();
-    bool checkStringPool();
-    bool checkDependency();
-    bool checkContext();
-
-    bool relocate();
-
-    static void *resolveSymbolAdapter(void *context, char const *name);
-
-  };
-
-} // namespace bcc
-
-#endif // BCC_MCCACHEREADER_H
diff --git a/lib/ExecutionEngine/MCCacheWriter.cpp b/lib/ExecutionEngine/MCCacheWriter.cpp
deleted file mode 100644
index b0bf117..0000000
--- a/lib/ExecutionEngine/MCCacheWriter.cpp
+++ /dev/null
@@ -1,388 +0,0 @@
-/*
- * Copyright 2010-2012, 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 "MCCacheWriter.h"
-
-#include "DebugHelper.h"
-#include "FileHandle.h"
-#include "Script.h"
-
-#include <map>
-#include <string>
-#include <vector>
-#include <utility>
-
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
-using namespace std;
-
-namespace bcc {
-
-MCCacheWriter::~MCCacheWriter() {
-#define CHECK_AND_FREE(VAR) if (VAR) { free(VAR); }
-
-  CHECK_AND_FREE(mpHeaderSection);
-  CHECK_AND_FREE(mpStringPoolSection);
-  CHECK_AND_FREE(mpDependencyTableSection);
-  CHECK_AND_FREE(mpPragmaListSection);
-  CHECK_AND_FREE(mpObjectSlotSection);
-  CHECK_AND_FREE(mpExportVarNameListSection);
-  CHECK_AND_FREE(mpExportFuncNameListSection);
-
-#undef CHECK_AND_FREE
-}
-
-bool MCCacheWriter::writeCacheFile(FileHandle *objFile, FileHandle *infoFile,
-                                 Script *S, uint32_t libRS_threadable) {
-  if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) {
-    return false;
-  }
-
-  mObjFile = objFile;
-  mInfoFile = infoFile;
-  mpOwner = S;
-
-  bool result = prepareHeader(libRS_threadable)
-             && prepareDependencyTable()
-             && preparePragmaList()
-             && prepareExportVarNameList()
-             && prepareExportFuncNameList()
-             && prepareExportForEachNameList()
-             && prepareStringPool()
-             && prepareObjectSlotList()
-             && calcSectionOffset()
-             && writeAll()
-             ;
-
-  return result;
-}
-
-
-bool MCCacheWriter::prepareHeader(uint32_t libRS_threadable) {
-  MCO_Header *header = (MCO_Header *)malloc(sizeof(MCO_Header));
-
-  if (!header) {
-    ALOGE("Unable to allocate for header.\n");
-    return false;
-  }
-
-  mpHeaderSection = header;
-
-  // Initialize
-  memset(header, '\0', sizeof(MCO_Header));
-
-  // Magic word and version
-  memcpy(header->magic, MCO_MAGIC, 4);
-  memcpy(header->version, MCO_VERSION, 4);
-
-  // Machine Integer Type
-  uint32_t number = 0x00000001;
-  header->endianness = (*reinterpret_cast<char *>(&number) == 1) ? 'e' : 'E';
-  header->sizeof_off_t = sizeof(off_t);
-  header->sizeof_size_t = sizeof(size_t);
-  header->sizeof_ptr_t = sizeof(void *);
-
-  // libRS is threadable dirty hack
-  // TODO: This should be removed in the future
-  header->libRS_threadable = libRS_threadable;
-
-  return true;
-}
-
-
-bool MCCacheWriter::prepareDependencyTable() {
-  size_t tableSize = sizeof(MCO_DependencyTable) +
-                     sizeof(MCO_Dependency) * mDependencies.size();
-
-  MCO_DependencyTable *tab = (MCO_DependencyTable *)malloc(tableSize);
-
-  if (!tab) {
-    ALOGE("Unable to allocate for dependency table section.\n");
-    return false;
-  }
-
-  mpDependencyTableSection = tab;
-  mpHeaderSection->depend_tab_size = tableSize;
-
-  tab->count = mDependencies.size();
-
-  size_t i = 0;
-  for (map<string, pair<uint32_t, unsigned char const *> >::iterator
-       I = mDependencies.begin(), E = mDependencies.end(); I != E; ++I, ++i) {
-    MCO_Dependency *dep = &tab->table[i];
-
-    dep->res_name_strp_index = addString(I->first.c_str(), I->first.size());
-    dep->res_type = I->second.first;
-    memcpy(dep->sha1, I->second.second, 20);
-  }
-
-  return true;
-}
-
-bool MCCacheWriter::preparePragmaList() {
-  size_t pragmaCount = mpOwner->getPragmaCount();
-
-  size_t listSize = sizeof(MCO_PragmaList) +
-                    sizeof(MCO_Pragma) * pragmaCount;
-
-  MCO_PragmaList *list = (MCO_PragmaList *)malloc(listSize);
-
-  if (!list) {
-    ALOGE("Unable to allocate for pragma list\n");
-    return false;
-  }
-
-  mpPragmaListSection = list;
-  mpHeaderSection->pragma_list_size = listSize;
-
-  list->count = pragmaCount;
-
-  vector<char const *> keyList(pragmaCount);
-  vector<char const *> valueList(pragmaCount);
-  mpOwner->getPragmaList(pragmaCount, &*keyList.begin(), &*valueList.begin());
-
-  for (size_t i = 0; i < pragmaCount; ++i) {
-    char const *key = keyList[i];
-    char const *value = valueList[i];
-
-    size_t keyLen = strlen(key);
-    size_t valueLen = strlen(value);
-
-    MCO_Pragma *pragma = &list->list[i];
-    pragma->key_strp_index = addString(key, keyLen);
-    pragma->value_strp_index = addString(value, valueLen);
-  }
-
-  return true;
-}
-
-bool MCCacheWriter::prepareStringPool() {
-  // Calculate string pool size
-  size_t size = sizeof(MCO_StringPool) +
-                sizeof(MCO_String) * mStringPool.size();
-
-  off_t strOffset = size;
-
-  for (size_t i = 0; i < mStringPool.size(); ++i) {
-    size += mStringPool[i].second + 1;
-  }
-
-  // Create string pool
-  MCO_StringPool *pool = (MCO_StringPool *)malloc(size);
-
-  if (!pool) {
-    ALOGE("Unable to allocate string pool.\n");
-    return false;
-  }
-
-  mpStringPoolSection = pool;
-  mpHeaderSection->str_pool_size = size;
-
-  pool->count = mStringPool.size();
-
-  char *strPtr = reinterpret_cast<char *>(pool) + strOffset;
-
-  for (size_t i = 0; i < mStringPool.size(); ++i) {
-    MCO_String *str = &pool->list[i];
-
-    str->length = mStringPool[i].second;
-    str->offset = strOffset;
-    memcpy(strPtr, mStringPool[i].first, str->length);
-
-    strPtr += str->length;
-    *strPtr++ = '\0';
-
-    strOffset += str->length + 1;
-  }
-
-  return true;
-}
-
-
-bool MCCacheWriter::prepareExportVarNameList() {
-  size_t varCount = mpOwner->getExportVarCount();
-  size_t listSize = sizeof(MCO_String_Ptr) + sizeof(size_t) * varCount;
-
-  MCO_String_Ptr *list = (MCO_String_Ptr*)malloc(listSize);
-
-  if (!list) {
-    ALOGE("Unable to allocate for export variable name list\n");
-    return false;
-  }
-
-  mpExportVarNameListSection = list;
-  mpHeaderSection->export_var_name_list_size = listSize;
-
-  list->count = static_cast<size_t>(varCount);
-
-  mpOwner->getExportVarNameList(varNameList);
-  for (size_t i = 0; i < varCount; ++i) {
-    list->strp_indexs[i] = addString(varNameList[i].c_str(), varNameList[i].length());
-  }
-  return true;
-}
-
-
-bool MCCacheWriter::prepareExportFuncNameList() {
-  size_t funcCount = mpOwner->getExportFuncCount();
-  size_t listSize = sizeof(MCO_String_Ptr) + sizeof(size_t) * funcCount;
-
-  MCO_String_Ptr *list = (MCO_String_Ptr*)malloc(listSize);
-
-  if (!list) {
-    ALOGE("Unable to allocate for export function name list\n");
-    return false;
-  }
-
-  mpExportFuncNameListSection = list;
-  mpHeaderSection->export_func_name_list_size = listSize;
-
-  list->count = static_cast<size_t>(funcCount);
-
-  mpOwner->getExportFuncNameList(funcNameList);
-  for (size_t i = 0; i < funcCount; ++i) {
-    list->strp_indexs[i] = addString(funcNameList[i].c_str(), funcNameList[i].length());
-  }
-  return true;
-}
-
-
-bool MCCacheWriter::prepareExportForEachNameList() {
-  size_t forEachCount = mpOwner->getExportForEachCount();
-  size_t listSize = sizeof(MCO_String_Ptr) + sizeof(size_t) * forEachCount;
-
-  MCO_String_Ptr *list = (MCO_String_Ptr*)malloc(listSize);
-
-  if (!list) {
-    ALOGE("Unable to allocate for export forEach name list\n");
-    return false;
-  }
-
-  mpExportForEachNameListSection = list;
-  mpHeaderSection->export_foreach_name_list_size = listSize;
-
-  list->count = static_cast<size_t>(forEachCount);
-
-  mpOwner->getExportForEachNameList(forEachNameList);
-  for (size_t i = 0; i < forEachCount; ++i) {
-    list->strp_indexs[i] = addString(forEachNameList[i].c_str(), forEachNameList[i].length());
-  }
-  return true;
-}
-
-
-bool MCCacheWriter::prepareObjectSlotList() {
-  size_t objectSlotCount = mpOwner->getObjectSlotCount();
-
-  size_t listSize = sizeof(MCO_ObjectSlotList) +
-                    sizeof(uint32_t) * objectSlotCount;
-
-  MCO_ObjectSlotList *list = (MCO_ObjectSlotList *)malloc(listSize);
-
-  if (!list) {
-    ALOGE("Unable to allocate for object slot list\n");
-    return false;
-  }
-
-  mpObjectSlotSection = list;
-  mpHeaderSection->object_slot_list_size = listSize;
-
-  list->count = objectSlotCount;
-
-  mpOwner->getObjectSlotList(objectSlotCount, list->object_slot_list);
-  return true;
-}
-
-
-bool MCCacheWriter::calcSectionOffset() {
-  size_t offset = sizeof(MCO_Header);
-
-#define OFFSET_INCREASE(NAME)                                               \
-  do {                                                                      \
-    /* Align to a word */                                                   \
-    size_t rem = offset % sizeof(int);                                      \
-    if (rem > 0) {                                                          \
-      offset += sizeof(int) - rem;                                          \
-    }                                                                       \
-                                                                            \
-    /* Save the offset and increase it */                                   \
-    mpHeaderSection->NAME##_offset = offset;                                \
-    offset += mpHeaderSection->NAME##_size;                                 \
-  } while (0)
-
-  OFFSET_INCREASE(str_pool);
-  OFFSET_INCREASE(depend_tab);
-  OFFSET_INCREASE(pragma_list);
-  OFFSET_INCREASE(func_table);
-  OFFSET_INCREASE(object_slot_list);
-  OFFSET_INCREASE(export_var_name_list);
-  OFFSET_INCREASE(export_func_name_list);
-  OFFSET_INCREASE(export_foreach_name_list);
-
-#undef OFFSET_INCREASE
-
-  return true;
-}
-
-
-bool MCCacheWriter::writeAll() {
-#define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION)                          \
-  do {                                                                      \
-    if (mInfoFile->seek(OFFSET, SEEK_SET) == -1) {                          \
-      ALOGE("Unable to seek to " #NAME " section for writing.\n");           \
-      return false;                                                         \
-    }                                                                       \
-                                                                            \
-    if (mInfoFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) !=      \
-        static_cast<ssize_t>(SIZE)) {                                       \
-      ALOGE("Unable to write " #NAME " section to cache file.\n");           \
-      return false;                                                         \
-    }                                                                       \
-  } while (0)
-
-#define WRITE_SECTION_SIMPLE(NAME, SECTION)                                 \
-  WRITE_SECTION(NAME,                                                       \
-                mpHeaderSection->NAME##_offset,                             \
-                mpHeaderSection->NAME##_size,                               \
-                SECTION)
-
-  WRITE_SECTION(header, 0, sizeof(MCO_Header), mpHeaderSection);
-
-  WRITE_SECTION_SIMPLE(str_pool, mpStringPoolSection);
-  WRITE_SECTION_SIMPLE(depend_tab, mpDependencyTableSection);
-  WRITE_SECTION_SIMPLE(pragma_list, mpPragmaListSection);
-  WRITE_SECTION_SIMPLE(object_slot_list, mpObjectSlotSection);
-
-  WRITE_SECTION_SIMPLE(export_var_name_list, mpExportVarNameListSection);
-  WRITE_SECTION_SIMPLE(export_func_name_list, mpExportFuncNameListSection);
-  WRITE_SECTION_SIMPLE(export_foreach_name_list, mpExportForEachNameListSection);
-
-#undef WRITE_SECTION_SIMPLE
-#undef WRITE_SECTION
-
-  if (static_cast<size_t>(mObjFile->write(mpOwner->getELF(),
-                                          mpOwner->getELFSize()))
-      != mpOwner->getELFSize()) {
-      ALOGE("Unable to write ELF to cache file.\n");
-      return false;
-  }
-
-  return true;
-}
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/MCCacheWriter.h b/lib/ExecutionEngine/MCCacheWriter.h
deleted file mode 100644
index b2bcb12..0000000
--- a/lib/ExecutionEngine/MCCacheWriter.h
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-#ifndef BCC_MCCACHEWRITER_H
-#define BCC_MCCACHEWRITER_H
-
-#include <bcc/bcc_mccache.h>
-
-#include "FileHandle.h"
-
-#include <map>
-#include <string>
-#include <utility>
-#include <vector>
-
-namespace bcc {
-  class Script;
-
-  class MCCacheWriter {
-  private:
-    Script *mpOwner;
-
-    FileHandle *mObjFile, *mInfoFile;
-
-    std::vector<std::pair<char const *, size_t> > mStringPool;
-
-    std::map<std::string,
-             std::pair<uint32_t, unsigned char const *> > mDependencies;
-
-    MCO_Header *mpHeaderSection;
-    MCO_StringPool *mpStringPoolSection;
-    MCO_DependencyTable *mpDependencyTableSection;
-    MCO_PragmaList *mpPragmaListSection;
-    MCO_ObjectSlotList *mpObjectSlotSection;
-
-    MCO_String_Ptr *mpExportVarNameListSection;
-    MCO_String_Ptr *mpExportFuncNameListSection;
-    MCO_String_Ptr *mpExportForEachNameListSection;
-
-    std::vector<std::string> varNameList;
-    std::vector<std::string> funcNameList;
-    std::vector<std::string> forEachNameList;
-
-  public:
-    MCCacheWriter()
-      : mpHeaderSection(NULL), mpStringPoolSection(NULL),
-        mpDependencyTableSection(NULL), mpPragmaListSection(NULL),
-        mpObjectSlotSection(NULL) {
-    }
-
-    ~MCCacheWriter();
-
-    bool writeCacheFile(FileHandle *objFile, FileHandle *infoFile,
-                        Script *S, uint32_t libRS_threadable);
-
-    void addDependency(MCO_ResourceType resType,
-                       std::string const &resName,
-                       unsigned char const *sha1) {
-      mDependencies.insert(std::make_pair(resName,
-                           std::make_pair((uint32_t)resType, sha1)));
-    }
-
-  private:
-    bool prepareHeader(uint32_t libRS_threadable);
-    bool prepareStringPool();
-    bool prepareDependencyTable();
-    bool prepareRelocationTable();
-    bool preparePragmaList();
-    bool prepareObjectSlotList();
-
-    bool prepareExportVarNameList();
-    bool prepareExportFuncNameList();
-    bool prepareExportForEachNameList();
-
-    bool writeAll();
-
-    bool calcSectionOffset();
-
-    size_t addString(char const *str, size_t size) {
-      mStringPool.push_back(std::make_pair(str, size));
-      return mStringPool.size() - 1;
-    }
-
-  };
-
-} // namespace bcc
-
-#endif // BCC_MCCACHEWRITER_H
diff --git a/lib/ExecutionEngine/ObjectLoader.cpp b/lib/ExecutionEngine/ObjectLoader.cpp
new file mode 100644
index 0000000..ade11ab
--- /dev/null
+++ b/lib/ExecutionEngine/ObjectLoader.cpp
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2012, 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 "bcc/ExecutionEngine/ObjectLoader.h"
+
+#include <utils/FileMap.h>
+
+#include "bcc/ExecutionEngine/GDBJITRegistrar.h"
+#include "bcc/Support/FileBase.h"
+#include "bcc/Support/Log.h"
+
+#include "ELFObjectLoaderImpl.h"
+
+using namespace bcc;
+
+ObjectLoader *ObjectLoader::Load(void *pMemStart, size_t pMemSize,
+                                 const char *pName,
+                                 SymbolResolverInterface &pResolver,
+                                 bool pEnableGDBDebug) {
+  ObjectLoader *result = NULL;
+
+  // Check parameters.
+  if ((pMemStart == NULL) || (pMemSize <= 0)) {
+    ALOGE("Invalid memory '%s' was given to load (memory addr: %p, size: %u)",
+          pName, pMemStart, static_cast<unsigned>(pMemSize));
+    goto bail;
+  }
+
+  // Create result object
+  result = new (std::nothrow) ObjectLoader();
+  if (result == NULL) {
+    ALOGE("Out of memory when create object loader for %s!", pName);
+    goto bail;
+  }
+
+  // Currently, only ELF object loader is supported. Therefore, there's no codes
+  // to detect the object file type and to select the one appropriated. Directly
+  // try out the ELF object loader.
+  result->mImpl = new (std::nothrow) ELFObjectLoaderImpl();
+  if (result->mImpl == NULL) {
+    ALOGE("Out of memory when create ELF object loader for %s", pName);
+    goto bail;
+  }
+
+  // Load the object file.
+  if (!result->mImpl->load(pMemStart, pMemSize)) {
+    ALOGE("Failed to load %s!", pName);
+    goto bail;
+  }
+
+  // Perform relocation.
+  if (!result->mImpl->relocate(pResolver)) {
+    ALOGE("Error occurred when performs relocation on %s!", pName);
+    goto bail;
+  }
+
+  // GDB debugging is enabled. Note that error occurrs during the setup of
+  // debugging won't failed the object load. Only a warning is issued to notify
+  // that the debugging is disabled due to the failure.
+  if (pEnableGDBDebug) {
+    // GDB's JIT debugging requires the source object file corresponded to the
+    // process image desired to debug with. And some fields in the object file
+    // must be updated to record the runtime information after it's loaded into
+    // memory. For example, GDB's JIT debugging requires an ELF file with the
+    // value of sh_addr in the section header to be the memory address that the
+    // section lives in the process image. Therefore, a writable memory with its
+    // contents initialized to the contents of pFile is created.
+    result->mDebugImage = new (std::nothrow) uint8_t [ pMemSize ];
+    if (result->mDebugImage != NULL) {
+      ::memcpy(result->mDebugImage, pMemStart, pMemSize);
+      if (!result->mImpl->prepareDebugImage(result->mDebugImage, pMemSize)) {
+        ALOGW("GDB debug for %s is enabled by the user but won't work due to "
+              "failure debug image preparation!", pName);
+      } else {
+        registerObjectWithGDB(
+            reinterpret_cast<const ObjectBuffer *>(result->mDebugImage),
+            pMemSize);
+      }
+    }
+  }
+
+  return result;
+
+bail:
+  delete result;
+  return NULL;
+}
+
+ObjectLoader *ObjectLoader::Load(FileBase &pFile,
+                                 SymbolResolverInterface &pResolver,
+                                 bool pEnableGDBDebug) {
+  size_t file_size;
+  android::FileMap *file_map = NULL;
+  const char *input_filename = pFile.getName().c_str();
+  ObjectLoader *result = NULL;
+
+  // Check the inputs.
+  if (pFile.hasError()) {
+    ALOGE("Input file %s to the object loader is in the invalid state! (%s)",
+          input_filename, pFile.getErrorMessage().c_str());
+    return NULL;
+  }
+
+  // Get the file size.
+  file_size = pFile.getSize();
+  if (pFile.hasError()) {
+    ALOGE("Failed to get size of file %s! (%s)", input_filename,
+          pFile.getErrorMessage().c_str());
+    return NULL;
+  }
+
+  // Abort on empty file.
+  if (file_size <= 0) {
+    ALOGE("Empty file %s to the object loader.", input_filename);
+    return NULL;
+  }
+
+  // Create memory map for the input file.
+  file_map = pFile.createMap(0, file_size, /* pIsReadOnly */true);
+  if ((file_map == NULL) || pFile.hasError())  {
+    ALOGE("Failed to map the file %s to the memory! (%s)", input_filename,
+          pFile.getErrorMessage().c_str());
+    return NULL;
+  }
+
+  // Delegate the load request.
+  result = Load(file_map->getDataPtr(), file_size, input_filename, pResolver,
+                pEnableGDBDebug);
+
+  // No whether the load is successful or not, file_map is no longer needed. On
+  // success, there's a copy of the object corresponded to the pFile in the
+  // memory. Therefore, file_map can be safely released.
+  file_map->release();
+
+  return result;
+}
+
+void *ObjectLoader::getSymbolAddress(const char *pName) const {
+  return mImpl->getSymbolAddress(pName);
+}
+
+size_t ObjectLoader::getSymbolSize(const char *pName) const {
+  return mImpl->getSymbolSize(pName);
+}
+
+bool ObjectLoader::getSymbolNameList(android::Vector<const char *>& pNameList,
+                                     SymbolType pType) const {
+  return mImpl->getSymbolNameList(pNameList, pType);
+}
+
+ObjectLoader::~ObjectLoader() {
+  delete mImpl;
+  delete [] reinterpret_cast<uint8_t *>(mDebugImage);
+}
diff --git a/lib/ExecutionEngine/ObjectLoaderImpl.h b/lib/ExecutionEngine/ObjectLoaderImpl.h
new file mode 100644
index 0000000..a9de913
--- /dev/null
+++ b/lib/ExecutionEngine/ObjectLoaderImpl.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+#ifndef OBJECT_LOADER_IMPL_H
+#define OBJECT_LOADER_IMPL_H
+
+#include <cstring>
+
+#include "bcc/ExecutionEngine/ObjectLoader.h"
+#include "bcc/Support/Log.h"
+
+#include <utils/Vector.h>
+
+namespace bcc {
+
+class SymbolResolverInterface;
+
+class ObjectLoaderImpl {
+public:
+  ObjectLoaderImpl() { }
+
+  virtual bool load(const void *pMem, size_t pMemSize) = 0;
+
+  virtual bool relocate(SymbolResolverInterface &pResolver) = 0;
+
+  virtual bool prepareDebugImage(void *pDebugImg, size_t pDebugImgSize) = 0;
+
+  virtual void *getSymbolAddress(const char *pName) const = 0;
+
+  virtual size_t getSymbolSize(const char *pName) const = 0;
+
+  virtual bool getSymbolNameList(android::Vector<const char *>& pNameList,
+                                 ObjectLoader::SymbolType pType) const = 0;
+
+  virtual ~ObjectLoaderImpl() { }
+};
+
+} // namespace bcc
+
+#endif // OBJECT_LOADER_IMPL_H
diff --git a/lib/ExecutionEngine/Runtime.c b/lib/ExecutionEngine/Runtime.c
deleted file mode 100644
index 6b9e20d..0000000
--- a/lib/ExecutionEngine/Runtime.c
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright 2010, 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 "RuntimeStub.h"
-
-#include <bcc/bcc_assert.h>
-
-#include <stdbool.h>
-#include <string.h>
-#include <stdlib.h>
-
-typedef struct {
-  const char *mName;
-  void *mPtr;
-} RuntimeFunction;
-
-#if defined(__arm__) || defined(__mips__)
-  #define DEF_GENERIC_RUNTIME(func)   \
-    extern void *func;
-  #define DEF_VFP_RUNTIME(func) \
-    extern void *func ## vfp;
-  #define DEF_LLVM_RUNTIME(func)
-  #define DEF_BCC_RUNTIME(func)
-#include "Runtime.def"
-#endif
-
-static const RuntimeFunction gRuntimes[] = {
-#if defined(__arm__) || defined(__mips__)
-  #define DEF_GENERIC_RUNTIME(func)   \
-    { #func, (void*) &func },
-  // TODO: enable only when target support VFP
-  #define DEF_VFP_RUNTIME(func) \
-    { #func, (void*) &func ## vfp },
-#else
-  // host compiler library must contain generic runtime
-  #define DEF_GENERIC_RUNTIME(func)
-  #define DEF_VFP_RUNTIME(func)
-#endif
-#define DEF_LLVM_RUNTIME(func)   \
-  { #func, (void*) &func },
-#define DEF_BCC_RUNTIME(func) \
-  { #func, &func ## _bcc },
-#include "Runtime.def"
-};
-
-static int CompareRuntimeFunction(const void *a, const void *b) {
-  return strcmp(((const RuntimeFunction*) a)->mName,
-               ((const RuntimeFunction*) b)->mName);
-}
-
-void *FindRuntimeFunction(const char *Name) {
-  // binary search
-  const RuntimeFunction Key = { Name, NULL };
-  const RuntimeFunction *R =
-      bsearch(&Key,
-              gRuntimes,
-              sizeof(gRuntimes) / sizeof(RuntimeFunction),
-              sizeof(RuntimeFunction),
-              CompareRuntimeFunction);
-
-  return ((R) ? R->mPtr : NULL);
-}
-
-void VerifyRuntimesTable() {
-  unsigned N = sizeof(gRuntimes) / sizeof(RuntimeFunction), i;
-  for(i = 0; i < N; i++) {
-    const char *Name = gRuntimes[i].mName;
-    int *Ptr = FindRuntimeFunction(Name);
-
-    if (Ptr != (int*) gRuntimes[i].mPtr)
-      bccAssert(false && "Table is corrupted (runtime name should be sorted "
-                         "in Runtime.def).");
-  }
-}
diff --git a/lib/ExecutionEngine/Runtime.h b/lib/ExecutionEngine/Runtime.h
deleted file mode 100644
index c0b7ad8..0000000
--- a/lib/ExecutionEngine/Runtime.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright 2010, 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.
- */
-
-#ifndef BCC_RUNTIME_H
-#define BCC_RUNTIME_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void *FindRuntimeFunction(const char *Name);
-void VerifyRuntimesTable();
-
-#ifdef __cplusplus
-};
-#endif
-
-#endif  /* BCC_RUNTIME_H */
diff --git a/lib/ExecutionEngine/Script.cpp b/lib/ExecutionEngine/Script.cpp
deleted file mode 100644
index 0c3c7a5..0000000
--- a/lib/ExecutionEngine/Script.cpp
+++ /dev/null
@@ -1,817 +0,0 @@
-/*
- * copyright 2010-2012, 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 "Script.h"
-
-#include "Config.h"
-#include "bcinfo/BitcodeWrapper.h"
-
-#include "MCCacheReader.h"
-#include "MCCacheWriter.h"
-#include "CompilerOption.h"
-
-#include "DebugHelper.h"
-#include "FileHandle.h"
-#include "GDBJITRegistrar.h"
-#include "ScriptCompiled.h"
-#include "ScriptCached.h"
-#include "Sha1Helper.h"
-#include "SourceInfo.h"
-
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <new>
-#include <string.h>
-#include <cutils/properties.h>
-
-namespace {
-
-bool getBooleanProp(const char *str) {
-  char buf[PROPERTY_VALUE_MAX];
-  property_get(str, buf, "0");
-  return strcmp(buf, "0") != 0;
-}
-
-bool isSetProp(const char *str) {
-  char buf[PROPERTY_VALUE_MAX];
-  property_get(str, buf, "");
-  return buf[0] != '\0';
-}
-
-} // namespace anonymous
-
-namespace bcc {
-
-Script::~Script() {
-  switch (mStatus) {
-  case ScriptStatus::Compiled:
-    delete mCompiled;
-    break;
-
-  case ScriptStatus::Cached:
-    delete mCached;
-    break;
-
-  default:
-    break;
-  }
-
-  for (size_t i = 0; i < 2; ++i) {
-    delete mSourceList[i];
-  }
-}
-
-
-int Script::addSourceBC(size_t idx,
-                        char const *resName,
-                        const char *bitcode,
-                        size_t bitcodeSize,
-                        unsigned long flags) {
-
-  if (!resName) {
-    mErrorCode = BCC_INVALID_VALUE;
-    ALOGE("Invalid argument: resName = NULL\n");
-    return 1;
-  }
-
-  if (mStatus != ScriptStatus::Unknown) {
-    mErrorCode = BCC_INVALID_OPERATION;
-    ALOGE("Bad operation: Adding source after bccPrepareExecutable\n");
-    return 1;
-  }
-
-  if (!bitcode) {
-    mErrorCode = BCC_INVALID_VALUE;
-    ALOGE("Invalid argument: bitcode = NULL\n");
-    return 1;
-  }
-
-  bcinfo::BitcodeWrapper wrapper(bitcode, bitcodeSize);
-
-  mSourceList[idx] = SourceInfo::createFromBuffer(resName,
-                                                  bitcode, bitcodeSize,
-                                                  flags);
-
-  if (!mSourceList[idx]) {
-    mErrorCode = BCC_OUT_OF_MEMORY;
-    ALOGE("Out of memory while adding source bitcode\n");
-    return 1;
-  }
-
-  return 0;
-}
-
-
-int Script::addSourceModule(size_t idx,
-                            llvm::Module *module,
-                            unsigned long flags) {
-  if (mStatus != ScriptStatus::Unknown) {
-    mErrorCode = BCC_INVALID_OPERATION;
-    ALOGE("Bad operation: Adding source after bccPrepareExecutable\n");
-    return 1;
-  }
-
-  if (!module) {
-    mErrorCode = BCC_INVALID_VALUE;
-    ALOGE("Invalid argument: module = NULL\n");
-    return 1;
-  }
-
-  mSourceList[idx] = SourceInfo::createFromModule(module, flags);
-
-  if (!mSourceList[idx]) {
-    mErrorCode = BCC_OUT_OF_MEMORY;
-    ALOGE("Out of memory when add source module\n");
-    return 1;
-  }
-
-  return 0;
-}
-
-
-int Script::addSourceFile(size_t idx,
-                          char const *path,
-                          unsigned long flags) {
-  if (mStatus != ScriptStatus::Unknown) {
-    mErrorCode = BCC_INVALID_OPERATION;
-    ALOGE("Bad operation: Adding source after bccPrepareExecutable\n");
-    return 1;
-  }
-
-  if (!path) {
-    mErrorCode = BCC_INVALID_VALUE;
-    ALOGE("Invalid argument: path = NULL\n");
-    return 1;
-  }
-
-  struct stat sb;
-  if (stat(path, &sb) != 0) {
-    mErrorCode = BCC_INVALID_VALUE;
-    ALOGE("File not found: %s\n", path);
-    return 1;
-  }
-
-  mSourceList[idx] = SourceInfo::createFromFile(path, flags);
-
-  if (!mSourceList[idx]) {
-    mErrorCode = BCC_OUT_OF_MEMORY;
-    ALOGE("Out of memory while adding source file\n");
-    return 1;
-  }
-
-  return 0;
-}
-
-int Script::prepareRelocatable(char const *objPath,
-                               llvm::Reloc::Model RelocModel,
-                               unsigned long flags) {
-  CompilerOption option;
-  option.RelocModelOpt = RelocModel;
-  option.LoadAfterCompile = false;
-
-  int status = internalCompile(option);
-  if (status != 0) {
-    ALOGE("LLVM error message: %s\n", getCompilerErrorMessage());
-    return status;
-  }
-
-  FileHandle objFile;
-  if (objFile.open(objPath, OpenMode::Write) < 0) {
-    ALOGE("Failed to open %s for write.\n", objPath);
-    return 1;
-  }
-
-  if (static_cast<size_t>(objFile.write(getELF(),
-                                        getELFSize())) != getELFSize()) {
-      objFile.close();
-      ::unlink(objPath);
-      ALOGE("Unable to write ELF to file %s.\n", objPath);
-      return false;
-  }
-
-  mObjectType = ScriptObject::Relocatable;
-
-  return 0;
-}
-
-
-int Script::prepareSharedObject(char const *objPath,
-                                char const *dsoPath,
-                                unsigned long flags) {
-  // TODO: Support cached shared object.
-  return 1;
-}
-
-
-int Script::prepareExecutable(char const *cacheDir,
-                              char const *cacheName,
-                              unsigned long flags) {
-  if (mStatus != ScriptStatus::Unknown) {
-    mErrorCode = BCC_INVALID_OPERATION;
-    ALOGE("Invalid operation: %s\n", __func__);
-    return 1;
-  }
-
-  int status = internalLoadCache(cacheDir, cacheName, /* checkOnly */ false);
-
-  if (status != 0) {
-    CompilerOption option;
-    status = internalCompile(option);
-
-    if (status != 0) {
-      ALOGE("LLVM error message: %s\n", getCompilerErrorMessage());
-      return status;
-    }
-
-    status = writeCache();
-    if (status != 0) {
-      ALOGE("Failed to write the cache for %s\n", cacheName);
-      return status;
-    }
-  }
-
-  // FIXME: Registration can be conditional on the presence of debug metadata
-  registerObjectWithGDB(getELF(), getELFSize()); // thread-safe registration
-
-  mObjectType = ScriptObject::Executable;
-
-  return status;
-}
-
-int Script::internalLoadCache(char const *cacheDir, char const *cacheName,
-                              bool checkOnly) {
-  if ((cacheDir == NULL) || (cacheName == NULL)) {
-    return 1;
-  }
-
-  // Set cache file Name
-  mCacheName = cacheName;
-
-  // Santize mCacheDir. Ensure that mCacheDir ends with '/'.
-  mCacheDir = cacheDir;
-  if (!mCacheDir.empty() && *mCacheDir.rbegin() != '/') {
-    mCacheDir.push_back('/');
-  }
-
-  if (!isCacheable()) {
-    return 1;
-  }
-
-  std::string objPath = getCachedObjectPath();
-  std::string infoPath = getCacheInfoPath();
-
-  FileHandle objFile;
-  if (objFile.open(objPath.c_str(), OpenMode::Read) < 0) {
-    // Unable to open the executable file in read mode.
-    return 1;
-  }
-
-  FileHandle infoFile;
-  if (infoFile.open(infoPath.c_str(), OpenMode::Read) < 0) {
-    // Unable to open the metadata information file in read mode.
-    return 1;
-  }
-
-  MCCacheReader reader;
-
-  // Register symbol lookup function
-  if (mpExtSymbolLookupFn) {
-    reader.registerSymbolCallback(mpExtSymbolLookupFn,
-                                      mpExtSymbolLookupFnContext);
-  }
-
-  // Dependencies
-  reader.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
-  reader.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
-
-  for (size_t i = 0; i < 2; ++i) {
-    if (mSourceList[i]) {
-      mSourceList[i]->introDependency(reader);
-    }
-  }
-
-  if (checkOnly)
-    return !reader.checkCacheFile(&objFile, &infoFile, this);
-
-  // Read cache file
-  ScriptCached *cached = reader.readCacheFile(&objFile, &infoFile, this);
-
-  if (!cached) {
-    mIsContextSlotNotAvail = reader.isContextSlotNotAvail();
-    return 1;
-  }
-
-  mCached = cached;
-  mStatus = ScriptStatus::Cached;
-
-  // Dirty hack for libRS.
-  // TODO(all):  This dirty hack should be removed in the future.
-  if (!cached->isLibRSThreadable() && mpExtSymbolLookupFn) {
-    mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, "__clearThreadable");
-  }
-
-  return 0;
-}
-
-int Script::internalCompile(const CompilerOption &option) {
-  // Create the ScriptCompiled object
-  mCompiled = new (std::nothrow) ScriptCompiled(this);
-
-  if (!mCompiled) {
-    mErrorCode = BCC_OUT_OF_MEMORY;
-    ALOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
-    return 1;
-  }
-
-  mStatus = ScriptStatus::Compiled;
-
-  // Register symbol lookup function
-  if (mpExtSymbolLookupFn) {
-    mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
-                                      mpExtSymbolLookupFnContext);
-  }
-
-  if (!mSourceList[0]) {
-    ALOGE("Source bitcode is not set.\n");
-    return 1;
-  }
-
-  // Parse Source bitcode file (if necessary)
-  if (mSourceList[0]->prepareModule() != 0) {
-    ALOGE("Unable to setup source module\n");
-    return 1;
-  }
-
-  // Parse Library bitcode file (if necessary)
-  if (mSourceList[1]) {
-    if (mSourceList[1]->prepareModule(mSourceList[0]->getContext()) != 0) {
-      ALOGE("Unable to setup library module\n");
-      return 1;
-    }
-  }
-
-  // Set the main source module
-  if (mCompiled->readModule(mSourceList[0]->getModule()) != 0) {
-    ALOGE("Unable to read source module\n");
-    return 1;
-  }
-
-  // Link the source module with the library module
-  if (mSourceList[1]) {
-    if (mCompiled->linkModule(mSourceList[1]->getModule()) != 0) {
-      ALOGE("Unable to link library module\n");
-      return 1;
-    }
-  }
-
-  // Compile and JIT the code
-  if (mCompiled->compile(option) != 0) {
-    ALOGE("Unable to compile.\n");
-    return 1;
-  }
-
-  return 0;
-}
-
-int Script::writeCache() {
-  // Not compiled script or encountered error during the compilation.
-  if ((mStatus != ScriptStatus::Compiled) ||
-      (getCompilerErrorMessage() == NULL))
-    return 1;
-
-  // Note: If we re-compile the script because the cached context slot not
-  // available, then we don't have to write the cache.
-
-  // Note: If the address of the context is not in the context slot, then
-  // we don't have to cache it.
-
-  if (isCacheable()) {
-
-    std::string objPath = getCachedObjectPath();
-    std::string infoPath = getCacheInfoPath();
-
-    // Remove the file if it already exists before writing the new file.
-    // The old file may still be mapped elsewhere in memory and we do not want
-    // to modify its contents.  (The same script may be running concurrently in
-    // the same process or a different process!)
-    ::unlink(objPath.c_str());
-    ::unlink(infoPath.c_str());
-
-    FileHandle objFile;
-    FileHandle infoFile;
-
-    if (objFile.open(objPath.c_str(), OpenMode::Write) >= 0 &&
-        infoFile.open(infoPath.c_str(), OpenMode::Write) >= 0) {
-
-      MCCacheWriter writer;
-
-#ifdef TARGET_BUILD
-      // Dependencies
-      writer.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
-      writer.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
-#endif
-
-      for (size_t i = 0; i < 2; ++i) {
-        if (mSourceList[i]) {
-          mSourceList[i]->introDependency(writer);
-        }
-      }
-
-      // libRS is threadable dirty hack
-      // TODO: This should be removed in the future
-      uint32_t libRS_threadable = 0;
-      if (mpExtSymbolLookupFn) {
-        libRS_threadable =
-          (uint32_t)mpExtSymbolLookupFn(mpExtSymbolLookupFnContext,
-                                        "__isThreadable");
-      }
-
-      if (!writer.writeCacheFile(&objFile, &infoFile, this, libRS_threadable)) {
-        objFile.truncate();
-        objFile.close();
-
-        if (unlink(objPath.c_str()) != 0) {
-          ALOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
-               objPath.c_str(), strerror(errno));
-        }
-
-        infoFile.truncate();
-        infoFile.close();
-
-        if (unlink(infoPath.c_str()) != 0) {
-          ALOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
-               infoPath.c_str(), strerror(errno));
-        }
-      }
-    }
-  }
-
-  return 0;
-}
-
-
-char const *Script::getCompilerErrorMessage() {
-  if (mStatus != ScriptStatus::Compiled) {
-    mErrorCode = BCC_INVALID_OPERATION;
-    return NULL;
-  }
-
-  return mCompiled->getCompilerErrorMessage();
-}
-
-
-void *Script::lookup(const char *name) {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->lookup(name);
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->lookup(name);
-    }
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-      return NULL;
-    }
-  }
-}
-
-
-size_t Script::getExportVarCount() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getExportVarCount();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getExportVarCount();
-    }
-
-    default: {
-      return 0;
-    }
-  }
-}
-
-
-size_t Script::getExportFuncCount() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getExportFuncCount();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getExportFuncCount();
-    }
-
-    default: {
-      return 0;
-    }
-  }
-}
-
-
-size_t Script::getExportForEachCount() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getExportForEachCount();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getExportForEachCount();
-    }
-
-    default: {
-      return 0;
-    }
-  }
-}
-
-
-size_t Script::getPragmaCount() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getPragmaCount();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getPragmaCount();
-    }
-
-    default: {
-      return 0;
-    }
-  }
-}
-
-
-size_t Script::getFuncCount() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getFuncCount();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getFuncCount();
-    }
-
-    default: {
-      return 0;
-    }
-  }
-}
-
-
-size_t Script::getObjectSlotCount() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getObjectSlotCount();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getObjectSlotCount();
-    }
-
-    default: {
-      return 0;
-    }
-  }
-}
-
-
-void Script::getExportVarList(size_t varListSize, void **varList) {
-  switch (mStatus) {
-#define DELEGATE(STATUS) \
-    case ScriptStatus::STATUS:                           \
-      m##STATUS->getExportVarList(varListSize, varList); \
-      break;
-
-    DELEGATE(Cached);
-
-    DELEGATE(Compiled);
-#undef DELEGATE
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-void Script::getExportVarNameList(std::vector<std::string> &varList) {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getExportVarNameList(varList);
-    }
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-
-void Script::getExportFuncList(size_t funcListSize, void **funcList) {
-  switch (mStatus) {
-#define DELEGATE(STATUS) \
-    case ScriptStatus::STATUS:                              \
-      m##STATUS->getExportFuncList(funcListSize, funcList); \
-      break;
-
-    DELEGATE(Cached);
-
-    DELEGATE(Compiled);
-#undef DELEGATE
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-void Script::getExportFuncNameList(std::vector<std::string> &funcList) {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getExportFuncNameList(funcList);
-    }
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-void Script::getExportForEachList(size_t funcListSize, void **funcList) {
-  switch (mStatus) {
-#define DELEGATE(STATUS) \
-    case ScriptStatus::STATUS:                                 \
-      m##STATUS->getExportForEachList(funcListSize, funcList); \
-      break;
-
-    DELEGATE(Cached);
-
-    DELEGATE(Compiled);
-#undef DELEGATE
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-void Script::getExportForEachNameList(std::vector<std::string> &forEachList) {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getExportForEachNameList(forEachList);
-    }
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-void Script::getPragmaList(size_t pragmaListSize,
-                           char const **keyList,
-                           char const **valueList) {
-  switch (mStatus) {
-#define DELEGATE(STATUS) \
-    case ScriptStatus::STATUS:                                      \
-      m##STATUS->getPragmaList(pragmaListSize, keyList, valueList); \
-      break;
-
-    DELEGATE(Cached);
-
-    DELEGATE(Compiled);
-#undef DELEGATE
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-
-void Script::getFuncInfoList(size_t funcInfoListSize,
-                             FuncInfo *funcInfoList) {
-  switch (mStatus) {
-#define DELEGATE(STATUS) \
-    case ScriptStatus::STATUS:                                    \
-      m##STATUS->getFuncInfoList(funcInfoListSize, funcInfoList); \
-      break;
-
-    DELEGATE(Cached);
-
-    DELEGATE(Compiled);
-#undef DELEGATE
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-
-void Script::getObjectSlotList(size_t objectSlotListSize,
-                               uint32_t *objectSlotList) {
-  switch (mStatus) {
-#define DELEGATE(STATUS)     \
-    case ScriptStatus::STATUS:                                          \
-      m##STATUS->getObjectSlotList(objectSlotListSize, objectSlotList); \
-      break;
-
-    DELEGATE(Cached);
-
-    DELEGATE(Compiled);
-#undef DELEGATE
-
-    default: {
-      mErrorCode = BCC_INVALID_OPERATION;
-    }
-  }
-}
-
-
-int Script::registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
-  mpExtSymbolLookupFn = pFn;
-  mpExtSymbolLookupFnContext = pContext;
-
-  if (mStatus != ScriptStatus::Unknown) {
-    mErrorCode = BCC_INVALID_OPERATION;
-    ALOGE("Invalid operation: %s\n", __func__);
-    return 1;
-  }
-  return 0;
-}
-
-bool Script::isCacheable() const {
-  if (getBooleanProp("debug.bcc.nocache")) {
-    // Android system environment property: Disables the cache mechanism by
-    // setting "debug.bcc.nocache".  So we will not load the cache file any
-    // way.
-    return false;
-  }
-
-  if (isSetProp("debug.rs.precision")) {
-    // If we have a floating point precision override, don't use the cache.
-    return false;
-  }
-
-  if (mCacheDir.empty() || mCacheName.empty()) {
-    // The application developer has not specified the cachePath, so
-    // we don't know where to open the cache file.
-    return false;
-  }
-
-  return true;
-}
-
-size_t Script::getELFSize() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getELFSize();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getELFSize();
-    }
-
-    default: {
-      return 0;
-    }
-  }
-}
-
-const char *Script::getELF() const {
-  switch (mStatus) {
-    case ScriptStatus::Compiled: {
-      return mCompiled->getELF();
-    }
-
-    case ScriptStatus::Cached: {
-      return mCached->getELF();
-    }
-
-    default: {
-      return NULL;
-    }
-  }
-}
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/Script.h b/lib/ExecutionEngine/Script.h
deleted file mode 100644
index e95cf8a..0000000
--- a/lib/ExecutionEngine/Script.h
+++ /dev/null
@@ -1,233 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-#ifndef BCC_SCRIPT_H
-#define BCC_SCRIPT_H
-
-#include <bcc/bcc.h>
-#include "bcc_internal.h"
-
-#include "Compiler.h"
-
-#include <llvm/Support/CodeGen.h>
-
-#include <vector>
-#include <string>
-
-#include <stddef.h>
-
-namespace llvm {
-  class Module;
-  class GDBJITRegistrar;
-}
-
-namespace bcc {
-  class ScriptCompiled;
-  class ScriptCached;
-  class SourceInfo;
-  struct CompilerOption;
-
-  namespace ScriptStatus {
-    enum StatusType {
-      Unknown,
-      Compiled,
-      Cached
-    };
-  }
-
-  namespace ScriptObject {
-    enum ObjectType {
-      Unknown,
-      Relocatable,
-      SharedObject,
-      Executable,
-    };
-  }
-
-  class Script {
-  private:
-    int mErrorCode;
-
-    ScriptStatus::StatusType mStatus;
-    // The type of the object behind this script after compilation. For
-    // example, after returning from a successful call to prepareRelocatable(),
-    // the value of mObjectType will be ScriptObject::Relocatable.
-    ScriptObject::ObjectType mObjectType;
-
-    union {
-      ScriptCompiled *mCompiled;
-      ScriptCached *mCached;
-    };
-
-    std::string mCacheDir;
-    std::string mCacheName;
-
-    inline std::string getCachedObjectPath() const {
-      return std::string(mCacheDir + mCacheName + ".o");
-    }
-
-    inline std::string getCacheInfoPath() const {
-      return getCachedObjectPath().append(".info");
-    }
-
-    bool mIsContextSlotNotAvail;
-
-    // Source List
-    SourceInfo *mSourceList[2];
-    // Note: mSourceList[0] (main source)
-    // Note: mSourceList[1] (library source)
-    // TODO(logan): Generalize this, use vector or SmallVector instead!
-
-    // External Function List
-    std::vector<char const *> mUserDefinedExternalSymbols;
-
-    // Register Symbol Lookup Function
-    BCCSymbolLookupFn mpExtSymbolLookupFn;
-    void *mpExtSymbolLookupFnContext;
-
-  public:
-    Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
-               mObjectType(ScriptObject::Unknown),
-               mIsContextSlotNotAvail(false),
-               mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
-      Compiler::GlobalInitialization();
-
-      mSourceList[0] = NULL;
-      mSourceList[1] = NULL;
-    }
-
-    ~Script();
-
-    int addSourceBC(size_t idx,
-                    char const *resName,
-                    const char *bitcode,
-                    size_t bitcodeSize,
-                    unsigned long flags);
-
-    int addSourceModule(size_t idx,
-                        llvm::Module *module,
-                        unsigned long flags);
-
-    int addSourceFile(size_t idx,
-                      char const *path,
-                      unsigned long flags);
-
-    void markExternalSymbol(char const *name) {
-      mUserDefinedExternalSymbols.push_back(name);
-    }
-
-    std::vector<char const *> const &getUserDefinedExternalSymbols() const {
-      return mUserDefinedExternalSymbols;
-    }
-
-    int prepareExecutable(char const *cacheDir,
-                          char const *cacheName,
-                          unsigned long flags);
-    int writeCache();
-
-    /*
-     * Link the given bitcodes in mSourceList to shared object (.so).
-     *
-     * Currently, it requires one to provide the relocatable object files with
-     * given bitcodes to output a shared object.
-     *
-     * The usage of this function is flexible. You can have a relocatable object
-     * compiled before and pass it in objPath to generate shared object. If the
-     * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
-     * you haven't done that yet) and then link the output relocatable object
-     * file to .so in dsoPath.
-     *
-     * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
-     *
-     */
-    int prepareSharedObject(char const *objPath,
-                            char const *dsoPath,
-                            unsigned long flags);
-
-    int prepareRelocatable(char const *objPath,
-                           llvm::Reloc::Model RelocModel,
-                           unsigned long flags);
-
-    char const *getCompilerErrorMessage();
-
-    void *lookup(const char *name);
-
-    size_t getExportVarCount() const;
-
-    size_t getExportFuncCount() const;
-
-    size_t getExportForEachCount() const;
-
-    size_t getPragmaCount() const;
-
-    size_t getFuncCount() const;
-
-    size_t getObjectSlotCount() const;
-
-    void getExportVarList(size_t size, void **list);
-
-    void getExportFuncList(size_t size, void **list);
-
-    void getExportForEachList(size_t size, void **list);
-
-    void getExportVarNameList(std::vector<std::string> &list);
-
-    void getExportFuncNameList(std::vector<std::string> &list);
-
-    void getExportForEachNameList(std::vector<std::string> &list);
-
-    void getPragmaList(size_t size,
-                       char const **keyList,
-                       char const **valueList);
-
-    void getFuncInfoList(size_t size, FuncInfo *list);
-
-    void getObjectSlotList(size_t size, uint32_t *list);
-
-    size_t getELFSize() const;
-
-    const char *getELF() const;
-
-    int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
-
-    bool isCacheable() const;
-
-    void setError(int error) {
-      if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
-        mErrorCode = error;
-      }
-    }
-
-    int getError() {
-      int result = mErrorCode;
-      mErrorCode = BCC_NO_ERROR;
-      return result;
-    }
-
-  private:
-    //
-    // It returns 0 if there's a cache hit.
-    //
-    // Side effect: it will set mCacheDir, mCacheName.
-    int internalLoadCache(char const *cacheDir, char const *cacheName,
-                          bool checkOnly);
-
-    int internalCompile(const CompilerOption&);
-  };
-
-} // namespace bcc
-
-#endif // BCC_SCRIPT_H
diff --git a/lib/ExecutionEngine/ScriptCached.cpp b/lib/ExecutionEngine/ScriptCached.cpp
deleted file mode 100644
index bbfac2b..0000000
--- a/lib/ExecutionEngine/ScriptCached.cpp
+++ /dev/null
@@ -1,141 +0,0 @@
-/*
- * Copyright 2010-2012, 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 "Config.h"
-
-#include "ScriptCached.h"
-
-#include "DebugHelper.h"
-
-#include <stdlib.h>
-
-namespace bcc {
-
-ScriptCached::~ScriptCached() {
-  // Deallocate string pool, exported var list, exported func list
-  if (mpStringPoolRaw) { free(mpStringPoolRaw); }
-  if (mpExportVars) { free(mpExportVars); }
-  if (mpExportFuncs) { free(mpExportFuncs); }
-  if (mpExportForEach) { free(mpExportForEach); }
-  if (mpObjectSlotList) { free(mpObjectSlotList); }
-}
-
-void ScriptCached::getExportVarList(size_t varListSize, void **varList) {
-  if (varList) {
-    size_t varCount = getExportVarCount();
-
-    if (varCount > varListSize) {
-      varCount = varListSize;
-    }
-
-    memcpy(varList, mpExportVars->cached_addr_list, sizeof(void *) * varCount);
-  }
-}
-
-
-void ScriptCached::getExportFuncList(size_t funcListSize, void **funcList) {
-  if (funcList) {
-    size_t funcCount = getExportFuncCount();
-
-    if (funcCount > funcListSize) {
-      funcCount = funcListSize;
-    }
-
-    memcpy(funcList, mpExportFuncs->cached_addr_list,
-           sizeof(void *) * funcCount);
-  }
-}
-
-
-void ScriptCached::getExportForEachList(size_t forEachListSize,
-                                        void **forEachList) {
-  if (forEachList) {
-    size_t forEachCount = getExportForEachCount();
-
-    if (forEachCount > forEachListSize) {
-      forEachCount = forEachListSize;
-    }
-
-    memcpy(forEachList, mpExportForEach->cached_addr_list,
-           sizeof(void *) * forEachCount);
-  }
-}
-
-
-void ScriptCached::getPragmaList(size_t pragmaListSize,
-                                 char const **keyList,
-                                 char const **valueList) {
-  size_t pragmaCount = getPragmaCount();
-
-  if (pragmaCount > pragmaListSize) {
-    pragmaCount = pragmaListSize;
-  }
-
-  if (keyList) {
-    for (size_t i = 0; i < pragmaCount; ++i) {
-      *keyList++ = mPragmas[i].first;
-    }
-  }
-
-  if (valueList) {
-    for (size_t i = 0; i < pragmaCount; ++i) {
-      *valueList++ = mPragmas[i].second;
-    }
-  }
-}
-
-
-void ScriptCached::getObjectSlotList(size_t objectSlotListSize,
-                                     uint32_t *objectSlotList) {
-  if (objectSlotList) {
-    size_t objectSlotCount = getObjectSlotCount();
-
-    if (objectSlotCount > objectSlotListSize) {
-      objectSlotCount = objectSlotListSize;
-    }
-
-    memcpy(objectSlotList, mpObjectSlotList->object_slot_list,
-           sizeof(uint32_t) * objectSlotCount);
-  }
-}
-
-
-void *ScriptCached::lookup(const char *name) {
-  return rsloaderGetSymbolAddress(mRSExecutable, name);
-}
-
-void ScriptCached::getFuncInfoList(size_t funcInfoListSize,
-                                   FuncInfo *funcInfoList) {
-  if (funcInfoList) {
-    size_t funcCount = getFuncCount();
-
-    if (funcCount > funcInfoListSize) {
-      funcCount = funcInfoListSize;
-    }
-
-    FuncInfo *info = funcInfoList;
-    for (FuncTable::const_iterator
-         I = mFunctions.begin(), E = mFunctions.end();
-         I != E && funcCount > 0; ++I, ++info, --funcCount) {
-      info->name = I->first.c_str();
-      info->addr = I->second.first;
-      info->size = I->second.second;
-    }
-  }
-}
-
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/ScriptCached.h b/lib/ExecutionEngine/ScriptCached.h
deleted file mode 100644
index e24a714..0000000
--- a/lib/ExecutionEngine/ScriptCached.h
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-#ifndef BCC_SCRIPTCACHED_H
-#define BCC_SCRIPTCACHED_H
-
-#include "Config.h"
-
-#include <bcc/bcc.h>
-#include <bcc/bcc_mccache.h>
-#include "bcc_internal.h"
-
-#include "librsloader.h"
-
-#include <llvm/ADT/SmallVector.h>
-
-#include <map>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include <stddef.h>
-
-namespace llvm {
-  class Module;
-}
-
-namespace bcc {
-  class Script;
-
-  class ScriptCached {
-    friend class CacheReader;
-    friend class MCCacheReader;
-
-  private:
-    enum { SMALL_VECTOR_QUICKN = 16 };
-
-    typedef llvm::SmallVector<std::pair<char const *, char const *>,
-                              SMALL_VECTOR_QUICKN> PragmaList;
-
-    typedef std::map<std::string, std::pair<void *, size_t> > FuncTable;
-
-  private:
-    Script *mpOwner;
-
-    MCO_ExportVarList *mpExportVars;
-    MCO_ExportFuncList *mpExportFuncs;
-    MCO_ExportForEachList *mpExportForEach;
-    PragmaList mPragmas;
-    MCO_ObjectSlotList *mpObjectSlotList;
-
-    FuncTable mFunctions;
-
-    RSExecRef mRSExecutable;
-    llvm::SmallVector<char, 1024> mCachedELFExecutable;
-
-    MCO_StringPool *mpStringPoolRaw;
-    std::vector<char const *> mStringPool;
-
-    bool mLibRSThreadable;
-
-  public:
-    ScriptCached(Script *owner)
-      : mpOwner(owner),
-        mpExportVars(NULL),
-        mpExportFuncs(NULL),
-        mpExportForEach(NULL),
-        mpObjectSlotList(NULL),
-        mpStringPoolRaw(NULL),
-        mLibRSThreadable(false) {
-    }
-
-    ~ScriptCached();
-
-    void *lookup(const char *name);
-
-
-    size_t getExportVarCount() const {
-      return mpExportVars->count;
-    }
-
-    size_t getExportFuncCount() const {
-      return mpExportFuncs->count;
-    }
-
-    size_t getExportForEachCount() const {
-      return mpExportForEach->count;
-    }
-
-    size_t getPragmaCount() const {
-      return mPragmas.size();
-    }
-
-    size_t getFuncCount() const {
-      return mFunctions.size();
-    }
-
-    size_t getObjectSlotCount() const {
-      return mpObjectSlotList->count;
-    }
-
-    void getExportVarList(size_t varListSize, void **varList);
-
-    void getExportFuncList(size_t funcListSize, void **funcList);
-
-    void getExportForEachList(size_t forEachListSize, void **forEachList);
-
-    void getPragmaList(size_t pragmaListSize,
-                       char const **keyList,
-                       char const **valueList);
-
-    void getFuncInfoList(size_t funcInfoListSize, FuncInfo *funcNameList);
-
-    void getObjectSlotList(size_t objectSlotListSize,
-                           uint32_t *objectSlotList);
-
-    const char *getELF() const {
-      return &*mCachedELFExecutable.begin();
-    }
-
-    size_t getELFSize() const {
-      return mCachedELFExecutable.size();
-    }
-
-    // Dirty hack for libRS.
-    // TODO(all): This should be removed in the future.
-    bool isLibRSThreadable() const {
-      return mLibRSThreadable;
-    }
-
-#if 0
-    void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
-      mCompiler.registerSymbolCallback(pFn, pContext);
-    }
-#endif
-  };
-
-} // namespace bcc
-
-#endif // BCC_SCRIPTCACHED_H
diff --git a/lib/ExecutionEngine/ScriptCompiled.cpp b/lib/ExecutionEngine/ScriptCompiled.cpp
deleted file mode 100644
index 4f2bb1b..0000000
--- a/lib/ExecutionEngine/ScriptCompiled.cpp
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Copyright 2010-2012, 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 "ScriptCompiled.h"
-
-#include "bcc_internal.h"
-#include "DebugHelper.h"
-
-namespace bcc {
-
-ScriptCompiled::~ScriptCompiled() {
-}
-
-void ScriptCompiled::getExportVarList(size_t varListSize, void **varList) {
-  if (varList) {
-    size_t varCount = getExportVarCount();
-
-    if (varCount > varListSize) {
-      varCount = varListSize;
-    }
-
-    for (ExportVarList::const_iterator
-         I = mExportVars.begin(), E = mExportVars.end();
-         I != E && varCount > 0; ++I, --varCount) {
-      *varList++ = *I;
-    }
-  }
-}
-
-void ScriptCompiled::getExportVarNameList(std::vector<std::string> &varList) {
-  varList = mExportVarsName;
-}
-
-
-void ScriptCompiled::getExportFuncNameList(std::vector<std::string> &funcList) {
-  funcList = mExportFuncsName;
-}
-
-
-void ScriptCompiled::getExportForEachNameList(std::vector<std::string> &forEachList) {
-  forEachList = mExportForEachName;
-}
-
-
-void ScriptCompiled::getExportFuncList(size_t funcListSize, void **funcList) {
-  if (funcList) {
-    size_t funcCount = getExportFuncCount();
-
-    if (funcCount > funcListSize) {
-      funcCount = funcListSize;
-    }
-
-    for (ExportFuncList::const_iterator
-         I = mExportFuncs.begin(), E = mExportFuncs.end();
-         I != E && funcCount > 0; ++I, --funcCount) {
-      *funcList++ = *I;
-    }
-  }
-}
-
-
-void ScriptCompiled::getExportForEachList(size_t forEachListSize,
-                                          void **forEachList) {
-  if (forEachList) {
-    size_t forEachCount = getExportForEachCount();
-
-    if (forEachCount > forEachListSize) {
-      forEachCount = forEachListSize;
-    }
-
-    for (ExportForEachList::const_iterator
-         I = mExportForEach.begin(), E = mExportForEach.end();
-         I != E && forEachCount > 0; ++I, --forEachCount) {
-      *forEachList++ = *I;
-    }
-  }
-}
-
-
-void ScriptCompiled::getPragmaList(size_t pragmaListSize,
-                                   char const **keyList,
-                                   char const **valueList) {
-  size_t pragmaCount = getPragmaCount();
-
-  if (pragmaCount > pragmaListSize) {
-    pragmaCount = pragmaListSize;
-  }
-
-  for (PragmaList::const_iterator
-       I = mPragmas.begin(), E = mPragmas.end();
-       I != E && pragmaCount > 0; ++I, --pragmaCount) {
-    if (keyList) { *keyList++ = I->first.c_str(); }
-    if (valueList) { *valueList++ = I->second.c_str(); }
-  }
-}
-
-
-void *ScriptCompiled::lookup(const char *name) {
-  return mCompiler.getSymbolAddress(name);
-}
-
-
-void ScriptCompiled::getFuncInfoList(size_t funcInfoListSize,
-                                     FuncInfo *funcInfoList) {
-  if (funcInfoList) {
-    size_t funcCount = getFuncCount();
-
-    if (funcCount > funcInfoListSize) {
-      funcCount = funcInfoListSize;
-    }
-
-    FuncInfo *info = funcInfoList;
-    for (FuncInfoMap::const_iterator
-         I = mEmittedFunctions.begin(), E = mEmittedFunctions.end();
-         I != E && funcCount > 0; ++I, ++info, --funcCount) {
-      info->name = I->first.c_str();
-      info->addr = I->second->addr;
-      info->size = I->second->size;
-    }
-  }
-}
-
-void ScriptCompiled::getObjectSlotList(size_t objectSlotListSize,
-                                       uint32_t *objectSlotList) {
-  if (objectSlotList) {
-    size_t objectSlotCount = getObjectSlotCount();
-
-    if (objectSlotCount > objectSlotListSize) {
-      objectSlotCount = objectSlotListSize;
-    }
-
-    for (ObjectSlotList::const_iterator
-         I = mObjectSlots.begin(), E = mObjectSlots.end();
-         I != E && objectSlotCount > 0; ++I, --objectSlotCount) {
-      *objectSlotList++ = *I;
-    }
-  }
-
-}
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/ScriptCompiled.h b/lib/ExecutionEngine/ScriptCompiled.h
deleted file mode 100644
index 7b7b62a..0000000
--- a/lib/ExecutionEngine/ScriptCompiled.h
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-#ifndef BCC_SCRIPTCOMPILED_H
-#define BCC_SCRIPTCOMPILED_H
-
-#include "Compiler.h"
-#include "Script.h"
-
-#include <bcc/bcc.h>
-
-#include <list>
-#include <map>
-#include <string>
-#include <utility>
-#include <vector>
-
-namespace llvm {
-  class Module;
-}
-
-namespace bcc {
-  struct CompilerOption;
-
-  class ScriptCompiled {
-    friend class Compiler;
-    friend class CodeEmitter;
-
-  private:
-    typedef std::list<std::pair<std::string, std::string> > PragmaList;
-    typedef std::list<void*> ExportVarList;
-    typedef std::list<void*> ExportFuncList;
-    typedef std::list<void*> ExportForEachList;
-    typedef std::map<std::string, FuncInfo *> FuncInfoMap;
-    typedef std::list<uint32_t> ObjectSlotList;
-
-  private:
-    Script *mpOwner;
-
-    Compiler mCompiler;
-
-    ExportVarList mExportVars;
-
-    std::vector<std::string> mExportVarsName;
-    std::vector<std::string> mExportFuncsName;
-    std::vector<std::string> mExportForEachName;
-
-    ExportFuncList mExportFuncs;
-    ExportForEachList mExportForEach;
-    PragmaList mPragmas;
-    ObjectSlotList mObjectSlots;
-
-    FuncInfoMap mEmittedFunctions;
-
-  public:
-    ScriptCompiled(Script *owner)
-      : mpOwner(owner), mCompiler(this)
-    {
-    }
-
-    ~ScriptCompiled();
-
-    int readModule(llvm::Module *module) {
-      return mCompiler.readModule(module);
-    }
-
-    int linkModule(llvm::Module *module) {
-      return mCompiler.linkModule(module);
-    }
-
-    int compile(const CompilerOption &option) {
-      return mCompiler.compile(option);
-    }
-
-    char const *getCompilerErrorMessage() {
-      return mCompiler.getErrorMessage();
-    }
-
-    void *lookup(const char *name);
-
-    size_t getExportVarCount() const {
-      return mExportVars.size();
-    }
-
-    size_t getExportFuncCount() const {
-      return mExportFuncs.size();
-    }
-
-    size_t getExportForEachCount() const {
-      return mExportForEach.size();
-    }
-
-    size_t getPragmaCount() const {
-      return mPragmas.size();
-    }
-
-    size_t getFuncCount() const {
-      return mEmittedFunctions.size();
-    }
-
-    size_t getObjectSlotCount() const {
-      return mObjectSlots.size();
-    }
-
-    void getExportVarList(size_t varListSize, void **varList);
-
-    void getExportFuncList(size_t funcListSize, void **funcList);
-
-    void getExportForEachList(size_t forEachListSize, void **forEachList);
-
-    void getExportVarNameList(std::vector<std::string> &varList);
-
-    void getExportFuncNameList(std::vector<std::string> &funcList);
-
-    void getExportForEachNameList(std::vector<std::string> &forEachList);
-
-    void getPragmaList(size_t pragmaListSize,
-                       char const **keyList,
-                       char const **valueList);
-
-    void getFuncInfoList(size_t funcInfoListSize,
-                         FuncInfo *funcInfoList);
-
-    void getObjectSlotList(size_t objectSlotListSize,
-                           uint32_t *objectSlotList);
-
-    std::vector<char const *> const & getUserDefinedExternalSymbols() const {
-      return mpOwner->getUserDefinedExternalSymbols();
-    }
-
-    const char *getELF() const {
-      return &*mCompiler.getELF().begin();
-    }
-
-    size_t getELFSize() const {
-      return mCompiler.getELF().size();
-    }
-
-    void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
-      mCompiler.registerSymbolCallback(pFn, pContext);
-    }
-  };
-
-} // namespace bcc
-
-#endif // BCC_SCRIPTCOMPILED_H
diff --git a/lib/ExecutionEngine/Sha1Helper.cpp b/lib/ExecutionEngine/Sha1Helper.cpp
deleted file mode 100644
index 0acd6b8..0000000
--- a/lib/ExecutionEngine/Sha1Helper.cpp
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * copyright 2010, 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 "Sha1Helper.h"
-
-#include "Config.h"
-
-#include "DebugHelper.h"
-#include "FileHandle.h"
-
-#include <string.h>
-
-#include <utils/StopWatch.h>
-
-#include <sha1.h>
-
-namespace bcc {
-
-unsigned char sha1LibBCC_SHA1[20];
-char const *pathLibBCC_SHA1 = "/system/lib/libbcc.so.sha1";
-
-unsigned char sha1LibRS[20];
-char const *pathLibRS = "/system/lib/libRS.so";
-
-void calcSHA1(unsigned char *result, char const *data, size_t size) {
-  SHA1_CTX hashContext;
-
-  SHA1Init(&hashContext);
-  SHA1Update(&hashContext,
-             reinterpret_cast<const unsigned char *>(data),
-             static_cast<unsigned long>(size));
-
-  SHA1Final(result, &hashContext);
-}
-
-
-void calcFileSHA1(unsigned char *result, char const *filename) {
-  android::StopWatch calcFileSHA1Timer("calcFileSHA1 time");
-
-  FileHandle file;
-
-  if (file.open(filename, OpenMode::Read) < 0) {
-    ALOGE("Unable to calculate the sha1 checksum of %s\n", filename);
-    memset(result, '\0', 20);
-    return;
-  }
-
-  SHA1_CTX hashContext;
-  SHA1Init(&hashContext);
-
-  char buf[256];
-  while (true) {
-    ssize_t nread = file.read(buf, sizeof(buf));
-
-    if (nread < 0) {
-      break;
-    }
-
-    SHA1Update(&hashContext,
-               reinterpret_cast<unsigned char *>(buf),
-               static_cast<unsigned long>(nread));
-
-    if ((size_t)nread < sizeof(buf)) {
-      // finished.
-      break;
-    }
-  }
-
-  SHA1Final(result, &hashContext);
-}
-
-void readSHA1(unsigned char *result, int result_size, char const *filename) {
-  FileHandle file;
-  if (file.open(filename, OpenMode::Read) < 0) {
-    ALOGE("Unable to read binary sha1 file %s\n", filename);
-    memset(result, '\0', result_size);
-    return;
-  }
-  file.read((char *)result, result_size);
-}
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/Sha1Helper.h b/lib/ExecutionEngine/Sha1Helper.h
deleted file mode 100644
index fe13b7a..0000000
--- a/lib/ExecutionEngine/Sha1Helper.h
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright 2010, 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.
- */
-
-#ifndef BCC_SHA1HELPER_H
-#define BCC_SHA1HELPER_H
-
-#include "Config.h"
-
-#include <stddef.h>
-
-namespace bcc {
-  extern unsigned char sha1LibBCC_SHA1[20];
-  extern char const *pathLibBCC_SHA1;
-
-  extern unsigned char sha1LibRS[20];
-  extern char const *pathLibRS;
-
-  void calcSHA1(unsigned char *result, char const *data, size_t size);
-
-  void calcFileSHA1(unsigned char *result, char const *filename);
-
-  // Read binary representation of sha1 from filename.
-  void readSHA1(unsigned char *result, int resultsize, char const *filename);
-}
-
-#endif // BCC_SHA1HELPER_H
diff --git a/lib/ExecutionEngine/SourceInfo.cpp b/lib/ExecutionEngine/SourceInfo.cpp
deleted file mode 100644
index 1d0a950..0000000
--- a/lib/ExecutionEngine/SourceInfo.cpp
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Copyright 2010, 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 "SourceInfo.h"
-
-#include "MCCacheWriter.h"
-#include "MCCacheReader.h"
-
-#include "DebugHelper.h"
-#include "ScriptCompiled.h"
-#include "Sha1Helper.h"
-
-#include <bcc/bcc.h>
-
-#include <llvm/Bitcode/ReaderWriter.h>
-#include <llvm/Module.h>
-#include <llvm/LLVMContext.h>
-#include <llvm/ADT/OwningPtr.h>
-#include <llvm/ADT/StringRef.h>
-#include <llvm/Support/MemoryBuffer.h>
-#include <llvm/Support/system_error.h>
-
-#include <stddef.h>
-#include <string.h>
-
-namespace bcc {
-
-
-SourceInfo *SourceInfo::createFromBuffer(char const *resName,
-                                         char const *bitcode,
-                                         size_t bitcodeSize,
-                                         unsigned long flags) {
-  SourceInfo *result = new SourceInfo();
-
-  if (!result) {
-    return NULL;
-  }
-
-  result->type = SourceKind::Buffer;
-  result->buffer.resName = resName;
-  result->buffer.bitcode = bitcode;
-  result->buffer.bitcodeSize = bitcodeSize;
-  result->flags = flags;
-
-  if (!resName && !(flags & BCC_SKIP_DEP_SHA1)) {
-    result->flags |= BCC_SKIP_DEP_SHA1;
-
-    ALOGW("It is required to give resName for sha1 dependency check.\n");
-    ALOGW("Sha1sum dependency check will be skipped.\n");
-    ALOGW("Set BCC_SKIP_DEP_SHA1 for flags to surpress this warning.\n");
-  }
-
-  if (result->flags & BCC_SKIP_DEP_SHA1) {
-    memset(result->sha1, '\0', 20);
-  } else {
-    calcSHA1(result->sha1, bitcode, bitcodeSize);
-  }
-
-  return result;
-}
-
-
-SourceInfo *SourceInfo::createFromFile(char const *path,
-                                       unsigned long flags) {
-  SourceInfo *result = new SourceInfo();
-
-  if (!result) {
-    return NULL;
-  }
-
-  result->type = SourceKind::File;
-  result->file.path = path;
-  result->flags = flags;
-
-  memset(result->sha1, '\0', 20);
-
-  if (!(result->flags & BCC_SKIP_DEP_SHA1)) {
-    calcFileSHA1(result->sha1, path);
-  }
-
-  return result;
-}
-
-
-SourceInfo *SourceInfo::createFromModule(llvm::Module *module,
-                                         unsigned long flags) {
-  SourceInfo *result = new SourceInfo();
-
-  if (!result) {
-    return NULL;
-  }
-
-  result->type = SourceKind::Module;
-  result->module = module;
-  result->flags = flags;
-
-  if (! (flags & BCC_SKIP_DEP_SHA1)) {
-    result->flags |= BCC_SKIP_DEP_SHA1;
-
-    ALOGW("Unable to calculate sha1sum for llvm::Module.\n");
-    ALOGW("Sha1sum dependency check will be skipped.\n");
-    ALOGW("Set BCC_SKIP_DEP_SHA1 for flags to surpress this warning.\n");
-  }
-
-  memset(result->sha1, '\0', 20);
-
-  return result;
-}
-
-
-int SourceInfo::prepareModule(llvm::LLVMContext *context) {
-  if (module)
-    return 0;
-
-  llvm::OwningPtr<llvm::MemoryBuffer> mem;
-  std::string errmsg;
-
-  switch (type) {
-  case SourceKind::Buffer:
-    {
-      mem.reset(llvm::MemoryBuffer::getMemBuffer(
-          llvm::StringRef(buffer.bitcode, buffer.bitcodeSize), "", false));
-
-      if (!mem.get()) {
-        ALOGE("Unable to MemoryBuffer::getMemBuffer(addr=%p, size=%lu)\n",
-              buffer.bitcode, (unsigned long)buffer.bitcodeSize);
-        return 1;
-      }
-    }
-    break;
-
-  case SourceKind::File:
-    {
-      if (llvm::error_code ec = llvm::MemoryBuffer::getFile(file.path, mem)) {
-        ALOGE("Unable to MemoryBuffer::getFile(path=%s, %s)\n",
-              file.path, ec.message().c_str());
-        return 1;
-      }
-    }
-    break;
-
-  default:
-    return 0;
-    break;
-  }
-
-  if (context)
-    shared_context = true;
-  else
-    context = new llvm::LLVMContext();
-
-  module = llvm::ParseBitcodeFile(mem.get(), *context, &errmsg);
-  if (module == NULL) {
-    ALOGE("Unable to ParseBitcodeFile: %s\n", errmsg.c_str());
-    if (!shared_context)
-      delete context;
-  }
-
-  return (module == NULL);
-}
-
-SourceInfo::~SourceInfo() {
-  if (module != NULL) {
-    llvm::LLVMContext *context = &module->getContext();
-    delete module;
-    if (!shared_context)
-      delete context;
-  }
-}
-
-template <typename T> void SourceInfo::introDependency(T &checker) {
-  if (flags & BCC_SKIP_DEP_SHA1) {
-    return;
-  }
-
-  switch (type) {
-  case SourceKind::Buffer:
-    checker.addDependency(BCC_APK_RESOURCE, buffer.resName, sha1);
-    break;
-
-  case SourceKind::File:
-    checker.addDependency(BCC_FILE_RESOURCE, file.path, sha1);
-    break;
-
-  default:
-    break;
-  }
-}
-
-template void SourceInfo::introDependency<MCCacheWriter>(MCCacheWriter &);
-template void SourceInfo::introDependency<MCCacheReader>(MCCacheReader &);
-
-
-} // namespace bcc
diff --git a/lib/ExecutionEngine/SourceInfo.h b/lib/ExecutionEngine/SourceInfo.h
deleted file mode 100644
index 080d3b4..0000000
--- a/lib/ExecutionEngine/SourceInfo.h
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright 2010, 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.
- */
-
-#ifndef BCC_SOURCEINFO_H
-#define BCC_SOURCEINFO_H
-
-#include "Config.h"
-
-#include <llvm/Module.h>
-
-#include <stddef.h>
-
-namespace llvm {
-  class LLVMContext;
-}
-
-namespace bcc {
-  namespace SourceKind {
-    enum SourceType {
-      File,
-      Buffer,
-      Module,
-    };
-  }
-
-  class SourceInfo {
-  private:
-    SourceKind::SourceType type;
-
-    // Note: module should not be a part of union.  Since, we are going to
-    // use module to store the pointer to parsed bitcode.
-    llvm::Module *module;
-    // If true, the LLVM context behind the module is shared with others.
-    // Therefore, don't try to destroy the context it when destroy the module.
-    bool shared_context;
-
-    union {
-      struct {
-        char const *resName;
-        char const *bitcode;
-        size_t bitcodeSize;
-      } buffer;
-
-      struct {
-        char const *path;
-      } file;
-    };
-
-    unsigned long flags;
-
-    unsigned char sha1[20];
-
-  private:
-    SourceInfo() : module(NULL), shared_context(false) { }
-
-  public:
-    static SourceInfo *createFromBuffer(char const *resName,
-                                        char const *bitcode,
-                                        size_t bitcodeSize,
-                                        unsigned long flags);
-
-    static SourceInfo *createFromFile(char const *path,
-                                      unsigned long flags);
-
-    static SourceInfo *createFromModule(llvm::Module *module,
-                                        unsigned long flags);
-
-    inline llvm::Module *getModule() const {
-      return module;
-    }
-
-    inline llvm::LLVMContext *getContext() const {
-      return (module) ? &module->getContext() : NULL;
-    }
-
-    // Share with the given context if it's provided.
-    int prepareModule(llvm::LLVMContext *context = NULL);
-
-    template <typename T> void introDependency(T &checker);
-
-    ~SourceInfo();
-  };
-
-
-} // namespace bcc
-
-#endif // BCC_SOURCEINFO_H
diff --git a/lib/ExecutionEngine/SymbolResolverProxy.cpp b/lib/ExecutionEngine/SymbolResolverProxy.cpp
new file mode 100644
index 0000000..5f3e7dc
--- /dev/null
+++ b/lib/ExecutionEngine/SymbolResolverProxy.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2012, 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 "bcc/ExecutionEngine/SymbolResolverProxy.h"
+
+using namespace bcc;
+
+void *SymbolResolverProxy::getAddress(const char *pName) {
+  // Search the address of the symbol by following the chain of resolvers.
+  for (size_t i = 0; i < mChain.size(); i++) {
+    void *addr = mChain[i]->getAddress(pName);
+    if (addr != NULL) {
+      return addr;
+    }
+  }
+  // Symbol not found or there's no resolver containing in the chain.
+  return NULL;
+}
+
+void SymbolResolverProxy::chainResolver(SymbolResolverInterface &pResolver) {
+  mChain.push_back(&pResolver);
+}
diff --git a/lib/ExecutionEngine/SymbolResolvers.cpp b/lib/ExecutionEngine/SymbolResolvers.cpp
new file mode 100644
index 0000000..62914ac
--- /dev/null
+++ b/lib/ExecutionEngine/SymbolResolvers.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2012, 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 "bcc/ExecutionEngine/SymbolResolvers.h"
+
+#include <dlfcn.h>
+
+#include <cassert>
+#include <cstdio>
+#include <new>
+
+using namespace bcc;
+
+//===----------------------------------------------------------------------===//
+// DyldSymbolResolver
+//===----------------------------------------------------------------------===//
+DyldSymbolResolver::DyldSymbolResolver(const char *pFileName,
+                                       bool pLazyBinding) : mError(NULL) {
+  int flags = (pLazyBinding) ? RTLD_LAZY : RTLD_NOW;
+
+  // Make the symbol within the given library to be local such that it won't
+  // be available for symbol resolution of subsequently loaded libraries.
+  flags |= RTLD_LOCAL;
+
+  mHandle = ::dlopen(pFileName, flags);
+  if (mHandle == NULL) {
+    const char *err = ::dlerror();
+
+#define DYLD_ERROR_MSG_PATTERN  "Failed to load %s! (%s)"
+    size_t error_length = ::strlen(DYLD_ERROR_MSG_PATTERN) +
+                          ::strlen(pFileName) + 1;
+    if (err != NULL) {
+      error_length += ::strlen(err);
+    }
+
+    mError = new (std::nothrow) char [error_length];
+    if (mError != NULL) {
+      ::snprintf(mError, error_length, DYLD_ERROR_MSG_PATTERN, pFileName,
+                 ((err != NULL) ? err : ""));
+    }
+  }
+#undef DYLD_ERROR_MSG_PATTERN
+}
+
+void *DyldSymbolResolver::getAddress(const char *pName) {
+  assert((mHandle != NULL) && "Invalid DyldSymbolResolver!");
+  return ::dlsym(mHandle, pName);
+}
+
+DyldSymbolResolver::~DyldSymbolResolver() {
+  ::dlclose(mHandle);
+  delete [] mError;
+}
diff --git a/lib/ExecutionEngine/bcc.cpp b/lib/ExecutionEngine/bcc.cpp
deleted file mode 100644
index 01ef32f..0000000
--- a/lib/ExecutionEngine/bcc.cpp
+++ /dev/null
@@ -1,265 +0,0 @@
-/*
- * Copyright 2010-2012, 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.
- */
-
-// Bitcode compiler (bcc) for Android:
-//    This is an eager-compilation JIT running on Android.
-
-#include <bcc/bcc.h>
-#include "bcc_internal.h"
-
-#include "Config.h"
-
-#include "Compiler.h"
-#include "DebugHelper.h"
-#include "Script.h"
-
-#include <string>
-
-#include <utils/StopWatch.h>
-
-#include <llvm/Support/CodeGen.h>
-
-using namespace bcc;
-
-namespace llvm {
-  class Module;
-}
-
-static bool bccBuildStampPrinted = false;
-
-static void bccPrintBuildStamp() {
-  if (!bccBuildStampPrinted) {
-    ALOGI("LIBBCC build time: %s", bccGetBuildTime());
-    ALOGI("LIBBCC build revision: %s", bccGetBuildRev());
-    bccBuildStampPrinted = true;
-  }
-}
-
-extern "C" BCCScriptRef bccCreateScript() {
-  BCC_FUNC_LOGGER();
-  bccPrintBuildStamp();
-  return wrap(new bcc::Script());
-}
-
-
-extern "C" void bccDisposeScript(BCCScriptRef script) {
-  BCC_FUNC_LOGGER();
-  delete unwrap(script);
-}
-
-
-extern "C" int bccRegisterSymbolCallback(BCCScriptRef script,
-                                         BCCSymbolLookupFn pFn,
-                                         void *pContext) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->registerSymbolCallback(pFn, pContext);
-}
-
-
-extern "C" int bccGetError(BCCScriptRef script) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->getError();
-}
-
-extern "C" int bccReadBC(BCCScriptRef script,
-                         char const *resName,
-                         char const *bitcode,
-                         size_t bitcodeSize,
-                         unsigned long flags) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->addSourceBC(0, resName, bitcode, bitcodeSize, flags);
-}
-
-
-extern "C" int bccReadModule(BCCScriptRef script,
-                             char const *resName /* deprecated */,
-                             LLVMModuleRef module,
-                             unsigned long flags) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->addSourceModule(0, unwrap(module), flags);
-}
-
-
-extern "C" int bccReadFile(BCCScriptRef script,
-                           char const *path,
-                           unsigned long flags) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->addSourceFile(0, path, flags);
-}
-
-
-extern "C" int bccLinkBC(BCCScriptRef script,
-                         char const *resName,
-                         char const *bitcode,
-                         size_t bitcodeSize,
-                         unsigned long flags) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->addSourceBC(1, resName, bitcode, bitcodeSize, flags);
-}
-
-
-extern "C" int bccLinkFile(BCCScriptRef script,
-                           char const *path,
-                           unsigned long flags) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->addSourceFile(1, path, flags);
-}
-
-
-extern "C" void bccMarkExternalSymbol(BCCScriptRef script, char const *name) {
-  BCC_FUNC_LOGGER();
-  unwrap(script)->markExternalSymbol(name);
-}
-
-
-extern "C" int bccPrepareRelocatable(BCCScriptRef script,
-                                     char const *objPath,
-                                     bccRelocModelEnum RelocModel,
-                                     unsigned long flags) {
-  BCC_FUNC_LOGGER();
-  llvm::Reloc::Model RM;
-
-  switch (RelocModel) {
-    case bccRelocDefault: {
-      RM = llvm::Reloc::Default;
-      break;
-    }
-    case bccRelocStatic: {
-      RM = llvm::Reloc::Static;
-      break;
-    }
-    case bccRelocPIC: {
-      RM = llvm::Reloc::PIC_;
-      break;
-    }
-    case bccRelocDynamicNoPIC: {
-      RM = llvm::Reloc::DynamicNoPIC;
-      break;
-    }
-    default: {
-      ALOGE("Unrecognized relocation model for bccPrepareObject!");
-      return BCC_INVALID_VALUE;
-    }
-  }
-
-  return unwrap(script)->prepareRelocatable(objPath, RM, flags);
-}
-
-
-extern "C" int bccPrepareSharedObject(BCCScriptRef script,
-                                      char const *objPath,
-                                      char const *dsoPath,
-                                      unsigned long flags) {
-  BCC_FUNC_LOGGER();
-  return unwrap(script)->prepareSharedObject(objPath, dsoPath, flags);
-}
-
-
-extern "C" int bccPrepareExecutable(BCCScriptRef script,
-                                    char const *cacheDir,
-                                    char const *cacheName,
-                                    unsigned long flags) {
-  BCC_FUNC_LOGGER();
-
-  android::StopWatch compileTimer("bcc: PrepareExecutable time");
-
-  return unwrap(script)->prepareExecutable(cacheDir, cacheName, flags);
-}
-
-
-extern "C" void *bccGetFuncAddr(BCCScriptRef script, char const *funcname) {
-  BCC_FUNC_LOGGER();
-
-  void *addr = unwrap(script)->lookup(funcname);
-
-#if DEBUG_BCC_REFLECT
-  ALOGD("Function Address: %s --> %p\n", funcname, addr);
-#endif
-
-  return addr;
-}
-
-
-extern "C" void bccGetExportVarList(BCCScriptRef script,
-                                    size_t varListSize,
-                                    void **varList) {
-  BCC_FUNC_LOGGER();
-
-  if (varList) {
-    unwrap(script)->getExportVarList(varListSize, varList);
-
-#if DEBUG_BCC_REFLECT
-    size_t count = unwrap(script)->getExportVarCount();
-    ALOGD("ExportVarCount = %lu\n", (unsigned long)count);
-
-    if (count > varListSize) {
-      count = varListSize;
-    }
-
-    for (size_t i = 0; i < count; ++i) {
-      ALOGD("ExportVarList[%lu] = %p\n", (unsigned long)i, varList[i]);
-    }
-#endif
-  }
-}
-
-
-extern "C" void bccGetExportFuncList(BCCScriptRef script,
-                                     size_t funcListSize,
-                                     void **funcList) {
-  BCC_FUNC_LOGGER();
-
-  if (funcList) {
-    unwrap(script)->getExportFuncList(funcListSize, funcList);
-
-#if DEBUG_BCC_REFLECT
-    size_t count = unwrap(script)->getExportFuncCount();
-    ALOGD("ExportFuncCount = %lu\n", (unsigned long)count);
-
-    if (count > funcListSize) {
-      count = funcListSize;
-    }
-
-    for (size_t i = 0; i < count; ++i) {
-      ALOGD("ExportFuncList[%lu] = %p\n", (unsigned long)i, funcList[i]);
-    }
-#endif
-  }
-}
-
-
-extern "C" void bccGetExportForEachList(BCCScriptRef script,
-                                        size_t forEachListSize,
-                                        void **forEachList) {
-  BCC_FUNC_LOGGER();
-
-  if (forEachList) {
-    unwrap(script)->getExportForEachList(forEachListSize, forEachList);
-
-#if DEBUG_BCC_REFLECT
-    size_t count = unwrap(script)->getExportForEachCount();
-    ALOGD("ExportForEachCount = %lu\n", (unsigned long)count);
-
-    if (count > forEachListSize) {
-      count = forEachListSize;
-    }
-
-    for (size_t i = 0; i < count; ++i) {
-      ALOGD("ExportForEachList[%lu] = %p\n", (unsigned long)i, forEachList[i]);
-    }
-#endif
-  }
-}
diff --git a/lib/ExecutionEngine/bcc_internal.h b/lib/ExecutionEngine/bcc_internal.h
deleted file mode 100644
index d0f5766..0000000
--- a/lib/ExecutionEngine/bcc_internal.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright 2010, 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.
- */
-
-#ifndef BCC_INTERNAL_H
-#define BCC_INTERNAL_H
-
-#include <bcc/bcc.h>
-
-#if defined(__cplusplus)
-
-#define BCC_OPAQUE_TYPE_CONVERSION(TRANSPARENT_TYPE, OPAQUE_TYPE)           \
-  inline OPAQUE_TYPE wrap(TRANSPARENT_TYPE ptr) {                           \
-    return reinterpret_cast<OPAQUE_TYPE>(ptr);                              \
-  }                                                                         \
-                                                                            \
-  inline TRANSPARENT_TYPE unwrap(OPAQUE_TYPE ptr) {                         \
-    return reinterpret_cast<TRANSPARENT_TYPE>(ptr);                         \
-  }
-
-namespace llvm {
-  class Module;
-}
-
-namespace bcc {
-  class Script;
-
-  /* Function information struct */
-  struct FuncInfo {
-    char const *name;
-    void *addr;
-    size_t size;
-  };
-
-  BCC_OPAQUE_TYPE_CONVERSION(bcc::Script *, BCCScriptRef);
-  BCC_OPAQUE_TYPE_CONVERSION(llvm::Module *, LLVMModuleRef);
-
-} // namespace bcc
-
-#undef BCC_OPAQUE_TYPE_CONVERSION
-
-#endif // defined(__cplusplus)
-
-#endif // BCC_INTERNAL_H
diff --git a/lib/Transforms/Android.mk b/lib/RenderScript/Android.mk
similarity index 60%
rename from lib/Transforms/Android.mk
rename to lib/RenderScript/Android.mk
index c30dd29..09c5df0 100644
--- a/lib/Transforms/Android.mk
+++ b/lib/RenderScript/Android.mk
@@ -16,55 +16,57 @@
 #
 
 LOCAL_PATH := $(call my-dir)
-include $(LOCAL_PATH)/../../libbcc-config.mk
 
 #=====================================================================
-# Common: libbccTransforms
+# Common: libbccRenderScript
 #=====================================================================
 
-libbcc_transforms_SRC_FILES := \
-  ForEachExpand.cpp
+libbcc_renderscript_SRC_FILES := \
+  RSCompiler.cpp \
+  RSCompilerDriver.cpp \
+  RSExecutable.cpp \
+  RSForEachExpand.cpp \
+  RSInfo.cpp \
+  RSInfoExtractor.cpp \
+  RSInfoReader.cpp \
+  RSInfoWriter.cpp \
+  RSScript.cpp
 
 #=====================================================================
-# Device Static Library: libbccTransforms
+# Device Static Library: libbccRenderScript
 #=====================================================================
 
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := libbccTransforms
+LOCAL_MODULE := libbccRenderScript
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -DTARGET_BUILD
+LOCAL_SRC_FILES := $(libbcc_renderscript_SRC_FILES)
 
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-LOCAL_SRC_FILES := $(libbcc_transforms_SRC_FILES)
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_DEVICE_BUILD_MK)
 include $(BUILD_STATIC_LIBRARY)
 
 
 #=====================================================================
-# Host Static Library: libbccTransforms
+# Host Static Library: libbccRenderScript
 #=====================================================================
 
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := libbccTransforms
+LOCAL_MODULE := libbccRenderScript
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 LOCAL_IS_HOST_MODULE := true
 
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -D__HOST__
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
+LOCAL_SRC_FILES := $(libbcc_renderscript_SRC_FILES)
 
-LOCAL_SRC_FILES := $(libbcc_transforms_SRC_FILES)
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_HOST_BUILD_MK)
 include $(BUILD_HOST_STATIC_LIBRARY)
+
+# Build RenderScript runtime (libclcore.bc)
+include $(LOCAL_PATH)/runtime/Android.mk
diff --git a/lib/RenderScript/RSCompiler.cpp b/lib/RenderScript/RSCompiler.cpp
new file mode 100644
index 0000000..131fed6
--- /dev/null
+++ b/lib/RenderScript/RSCompiler.cpp
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2012, 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 "bcc/RenderScript/RSCompiler.h"
+
+#include <llvm/Module.h>
+#include <llvm/PassManager.h>
+#include <llvm/Transforms/IPO.h>
+
+#include "bcc/RenderScript/RSExecutable.h"
+#include "bcc/RenderScript/RSInfo.h"
+#include "bcc/RenderScript/RSScript.h"
+#include "bcc/RenderScript/RSTransforms.h"
+#include "bcc/Source.h"
+#include "bcc/Support/Log.h"
+
+using namespace bcc;
+
+bool RSCompiler::beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM) {
+  // Add a pass to internalize the symbols that don't need to have global
+  // visibility.
+  RSScript &script = static_cast<RSScript &>(pScript);
+  const RSInfo *info = script.getInfo();
+
+  // The vector contains the symbols that should not be internalized.
+  std::vector<const char *> export_symbols;
+
+  // Special RS functions should always be global symbols.
+  const char **special_functions = RSExecutable::SpecialFunctionNames;
+  while (*special_functions != NULL) {
+    export_symbols.push_back(*special_functions);
+    special_functions++;
+  }
+
+  // Visibility of symbols appeared in rs_export_var and rs_export_func should
+  // also be preserved.
+  const RSInfo::ExportVarNameListTy &export_vars = info->getExportVarNames();
+  const RSInfo::ExportFuncNameListTy &export_funcs = info->getExportFuncNames();
+
+  for (RSInfo::ExportVarNameListTy::const_iterator
+           export_var_iter = export_vars.begin(),
+           export_var_end = export_vars.end();
+       export_var_iter != export_var_end; export_var_iter++) {
+    export_symbols.push_back(*export_var_iter);
+  }
+
+  for (RSInfo::ExportFuncNameListTy::const_iterator
+           export_func_iter = export_funcs.begin(),
+           export_func_end = export_funcs.end();
+       export_func_iter != export_func_end; export_func_iter++) {
+    export_symbols.push_back(*export_func_iter);
+  }
+
+  // Expanded foreach functions should not be internalized, too.
+  const RSInfo::ExportForeachFuncListTy &export_foreach_func =
+      info->getExportForeachFuncs();
+  std::vector<std::string> expanded_foreach_funcs;
+  for (RSInfo::ExportForeachFuncListTy::const_iterator
+           foreach_func_iter = export_foreach_func.begin(),
+           foreach_func_end = export_foreach_func.end();
+       foreach_func_iter != foreach_func_end; foreach_func_iter++) {
+    std::string name(foreach_func_iter->first);
+    expanded_foreach_funcs.push_back(name.append(".expand"));
+  }
+
+  // Need to wait until ForEachExpandList is fully populated to fill in
+  // exported symbols.
+  for (size_t i = 0; i < expanded_foreach_funcs.size(); i++) {
+    export_symbols.push_back(expanded_foreach_funcs[i].c_str());
+  }
+
+  pPM.add(llvm::createInternalizePass(export_symbols));
+
+  return true;
+}
+
+bool RSCompiler::beforeExecuteLTOPasses(Script &pScript,
+                                        llvm::PassManager &pPM) {
+  // Execute a pass to expand foreach-able functions
+  llvm::PassManager rs_passes;
+
+  // Script passed to RSCompiler must be a RSScript.
+  RSScript &script = static_cast<RSScript &>(pScript);
+  const RSInfo *info = script.getInfo();
+  llvm::Module &module = script.getSource().getModule();
+
+  if (info == NULL) {
+    ALOGE("Missing RSInfo in RSScript to run the pass for foreach expansion on "
+          "%s!", module.getModuleIdentifier().c_str());
+    return false;
+  }
+
+  // Expand ForEach on CPU path to reduce launch overhead.
+  rs_passes.add(createRSForEachExpandPass(info->getExportForeachFuncs()));
+
+  // Execute the pass.
+  rs_passes.run(module);
+
+  return true;
+}
diff --git a/lib/RenderScript/RSCompilerDriver.cpp b/lib/RenderScript/RSCompilerDriver.cpp
new file mode 100644
index 0000000..bd7e63c
--- /dev/null
+++ b/lib/RenderScript/RSCompilerDriver.cpp
@@ -0,0 +1,406 @@
+/*
+ * Copyright 2012, 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 "bcc/RenderScript/RSCompilerDriver.h"
+
+#include <llvm/Support/Path.h>
+
+#include "bcinfo/BitcodeWrapper.h"
+
+#include "bcc/RenderScript/RSExecutable.h"
+#include "bcc/RenderScript/RSScript.h"
+#include "bcc/Support/CompilerConfig.h"
+#include "bcc/Support/TargetCompilerConfigs.h"
+#include "bcc/Source.h"
+#include "bcc/Support/FileMutex.h"
+#include "bcc/Support/Log.h"
+#include "bcc/Support/InputFile.h"
+#include "bcc/Support/Initialization.h"
+#include "bcc/Support/Sha1Util.h"
+#include "bcc/Support/OutputFile.h"
+
+#include <cutils/properties.h>
+#include <utils/String8.h>
+#include <utils/StopWatch.h>
+
+using namespace bcc;
+
+namespace {
+
+bool is_force_recompile() {
+  char buf[PROPERTY_VALUE_MAX];
+
+  // Re-compile if floating point precision has been overridden.
+  property_get("debug.rs.precision", buf, "");
+  if (buf[0] != '\0') {
+    return true;
+  }
+
+  // Re-compile if debug.rs.forcerecompile is set.
+  property_get("debug.rs.forcerecompile", buf, "0");
+  if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
+    return true;
+  } else {
+    return false;
+  }
+}
+
+} // end anonymous namespace
+
+RSCompilerDriver::RSCompilerDriver() : mConfig(NULL), mCompiler() {
+  init::Initialize();
+  // Chain the symbol resolvers for BCC runtimes and RS runtimes.
+  mResolver.chainResolver(mBCCRuntime);
+  mResolver.chainResolver(mRSRuntime);
+}
+
+RSCompilerDriver::~RSCompilerDriver() {
+  delete mConfig;
+}
+
+RSExecutable *
+RSCompilerDriver::loadScriptCache(const char *pOutputPath,
+                                  const RSInfo::DependencyTableTy &pDeps) {
+  android::StopWatch load_time("bcc: RSCompilerDriver::loadScriptCache time");
+  RSExecutable *result = NULL;
+
+  if (is_force_recompile())
+    return NULL;
+
+  //===--------------------------------------------------------------------===//
+  // Acquire the read lock for reading output object file.
+  //===--------------------------------------------------------------------===//
+  FileMutex<FileBase::kReadLock> read_output_mutex(pOutputPath);
+
+  if (read_output_mutex.hasError() || !read_output_mutex.lock()) {
+    ALOGE("Unable to acquire the read lock for %s! (%s)", pOutputPath,
+          read_output_mutex.getErrorMessage().c_str());
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Read the output object file.
+  //===--------------------------------------------------------------------===//
+  InputFile *output_file = new (std::nothrow) InputFile(pOutputPath);
+
+  if ((output_file == NULL) || output_file->hasError()) {
+    ALOGE("Unable to open the %s for read! (%s)", pOutputPath,
+          output_file->getErrorMessage().c_str());
+    delete output_file;
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Acquire the read lock on output_file for reading its RS info file.
+  //===--------------------------------------------------------------------===//
+  android::String8 info_path = RSInfo::GetPath(*output_file);
+
+  if (!output_file->lock()) {
+    ALOGE("Unable to acquire the read lock on %s for reading %s! (%s)",
+          pOutputPath, info_path.string(),
+          output_file->getErrorMessage().c_str());
+    delete output_file;
+    return NULL;
+  }
+
+ //===---------------------------------------------------------------------===//
+  // Open and load the RS info file.
+  //===--------------------------------------------------------------------===//
+  InputFile info_file(info_path.string());
+  RSInfo *info = RSInfo::ReadFromFile(info_file, pDeps);
+
+  // Release the lock on output_file.
+  output_file->unlock();
+
+  if (info == NULL) {
+    delete output_file;
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Create the RSExecutable.
+  //===--------------------------------------------------------------------===//
+  result = RSExecutable::Create(*info, *output_file, mResolver);
+  if (result == NULL) {
+    delete output_file;
+    delete info;
+    return NULL;
+  }
+
+  return result;
+}
+
+bool RSCompilerDriver::setupConfig(const RSScript &pScript) {
+  bool changed = false;
+
+  const llvm::CodeGenOpt::Level script_opt_level =
+      static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel());
+
+  if (mConfig != NULL) {
+    // Renderscript bitcode may have their optimization flag configuration
+    // different than the previous run of RS compilation.
+    if (mConfig->getOptimizationLevel() != script_opt_level) {
+      mConfig->setOptimizationLevel(script_opt_level);
+      changed = true;
+    }
+  } else {
+    // Haven't run the compiler ever.
+    mConfig = new (std::nothrow) DefaultCompilerConfig();
+    if (mConfig == NULL) {
+      // Return false since mConfig remains NULL and out-of-memory.
+      return false;
+    }
+    mConfig->setOptimizationLevel(script_opt_level);
+    changed = true;
+  }
+
+#if defined(DEFAULT_ARM_CODEGEN)
+  // NEON should be disable when full-precision floating point is required.
+  assert((pScript.getInfo() != NULL) && "NULL RS info!");
+  if (pScript.getInfo()->getFloatPrecisionRequirement() == RSInfo::FP_Full) {
+    // Must be ARMCompilerConfig.
+    ARMCompilerConfig *arm_config = static_cast<ARMCompilerConfig *>(mConfig);
+    changed |= arm_config->enableNEON(/* pEnable */false);
+  }
+#endif
+
+  return changed;
+}
+
+RSExecutable *
+RSCompilerDriver::compileScript(RSScript &pScript,
+                                const char* pScriptName,
+                                const char *pOutputPath,
+                                const RSInfo::DependencyTableTy &pDeps) {
+  android::StopWatch compile_time("bcc: RSCompilerDriver::compileScript time");
+  RSExecutable *result = NULL;
+  RSInfo *info = NULL;
+
+  //===--------------------------------------------------------------------===//
+  // Extract RS-specific information from source bitcode.
+  //===--------------------------------------------------------------------===//
+  // RS info may contains configuration (such as #optimization_level) to the
+  // compiler therefore it should be extracted before compilation.
+  info = RSInfo::ExtractFromSource(pScript.getSource(), pDeps);
+  if (info == NULL) {
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Associate script with its info
+  //===--------------------------------------------------------------------===//
+  // This is required since RS compiler may need information in the info file
+  // to do some transformation (e.g., expand foreach-able function.)
+  pScript.setInfo(info);
+
+  //===--------------------------------------------------------------------===//
+  // Link RS script with RenderScript runtime.
+  //===--------------------------------------------------------------------===//
+  if (!RSScript::LinkRuntime(pScript)) {
+    ALOGE("Failed to link script '%s' with RenderScript runtime!", pScriptName);
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Acquire the write lock for writing output object file.
+  //===--------------------------------------------------------------------===//
+  FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath);
+
+  if (write_output_mutex.hasError() || !write_output_mutex.lock()) {
+    ALOGE("Unable to acquire the lock for writing %s! (%s)",
+          pOutputPath, write_output_mutex.getErrorMessage().c_str());
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Open the output file for write.
+  //===--------------------------------------------------------------------===//
+  OutputFile *output_file =
+      new (std::nothrow) OutputFile(pOutputPath, FileBase::kTruncate);
+
+  if ((output_file == NULL) || output_file->hasError()) {
+    ALOGE("Unable to open the %s for write! (%s)", pOutputPath,
+          output_file->getErrorMessage().c_str());
+    delete info;
+    delete output_file;
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Setup the config to the compiler.
+  //===--------------------------------------------------------------------===//
+  bool compiler_need_reconfigure = setupConfig(pScript);
+
+  if (mConfig == NULL) {
+    ALOGE("Failed to setup config for RS compiler to compile %s!", pOutputPath);
+    delete info;
+    delete output_file;
+    return NULL;
+  }
+
+  // Compiler need to re-config if it's haven't run the config() yet or the
+  // configuration it referenced is changed.
+  if (compiler_need_reconfigure) {
+    Compiler::ErrorCode err = mCompiler.config(*mConfig);
+    if (err != Compiler::kSuccess) {
+      ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath,
+            Compiler::GetErrorString(err));
+      delete info;
+      delete output_file;
+      return NULL;
+    }
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Run the compiler.
+  //===--------------------------------------------------------------------===//
+  Compiler::ErrorCode compile_result = mCompiler.compile(pScript, *output_file);
+  if (compile_result != Compiler::kSuccess) {
+    ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
+          Compiler::GetErrorString(compile_result));
+    delete info;
+    delete output_file;
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Create the RSExecutable.
+  //===--------------------------------------------------------------------===//
+  result = RSExecutable::Create(*info, *output_file, mResolver);
+  if (result == NULL) {
+    delete info;
+    delete output_file;
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Dump the disassembly for debug when possible.
+  //===--------------------------------------------------------------------===//
+#if USE_DISASSEMBLER
+  OutputFile *disassembly_output =
+      new (std::nothrow) OutputFile(DEBUG_DISASSEMBLER_FILE,
+                                    FileBase::kAppend);
+
+  if (disassembly_output != NULL) {
+    result->dumpDisassembly(*disassembly_output);
+    delete disassembly_output;
+  }
+#endif
+
+  //===--------------------------------------------------------------------===//
+  // Write out the RS info file.
+  //===--------------------------------------------------------------------===//
+  // Note that write failure only results in a warning since the source is
+  // successfully compiled and loaded.
+  if (!result->syncInfo(/* pForce */true)) {
+    ALOGW("%s was successfully compiled and loaded but its RS info file failed "
+          "to write out!", pOutputPath);
+  }
+
+  return result;
+}
+
+RSExecutable *RSCompilerDriver::build(BCCContext &pContext,
+                                      const char *pCacheDir,
+                                      const char *pResName,
+                                      const char *pBitcode,
+                                      size_t pBitcodeSize) {
+  android::StopWatch build_time("bcc: RSCompilerDriver::build time");
+  //===--------------------------------------------------------------------===//
+  // Check parameters.
+  //===--------------------------------------------------------------------===//
+  if ((pCacheDir == NULL) || (pResName == NULL)) {
+    ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
+          "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
+                                    ((pResName) ? pResName : "(null)"));
+    return NULL;
+  }
+
+  if ((pBitcode == NULL) || (pBitcodeSize <= 0)) {
+    ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
+          pBitcode, static_cast<unsigned>(pBitcodeSize));
+    return NULL;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Prepare dependency information.
+  //===--------------------------------------------------------------------===//
+  RSInfo::DependencyTableTy dep_info;
+  uint8_t bitcode_sha1[20];
+  Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize);
+  dep_info.push(std::make_pair(pResName, bitcode_sha1));
+
+  //===--------------------------------------------------------------------===//
+  // Construct output path.
+  //===--------------------------------------------------------------------===//
+  llvm::sys::Path output_path(pCacheDir);
+
+  // {pCacheDir}/{pResName}
+  if (!output_path.appendComponent(pResName)) {
+    ALOGE("Failed to construct output path %s/%s!", pCacheDir, pResName);
+    return NULL;
+  }
+
+  // {pCacheDir}/{pResName}.o
+  output_path.appendSuffix("o");
+
+  //===--------------------------------------------------------------------===//
+  // Load cache.
+  //===--------------------------------------------------------------------===//
+  RSExecutable *result = loadScriptCache(output_path.c_str(), dep_info);
+
+  if (result != NULL) {
+    // Cache hit
+    return result;
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Load the bitcode and create script.
+  //===--------------------------------------------------------------------===//
+  Source *source = Source::CreateFromBuffer(pContext, pResName,
+                                            pBitcode, pBitcodeSize);
+  if (source == NULL) {
+    return NULL;
+  }
+
+  RSScript *script = new (std::nothrow) RSScript(*source);
+  if (script == NULL) {
+    ALOGE("Out of memory when create Script object for '%s'! (output: %s)",
+          pResName, output_path.c_str());
+    delete source;
+    return NULL;
+  }
+
+  // Read information from bitcode wrapper.
+  bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
+  script->setCompilerVersion(wrapper.getCompilerVersion());
+  script->setOptimizationLevel(static_cast<RSScript::OptimizationLevel>(
+                                   wrapper.getOptimizationLevel()));
+
+  //===--------------------------------------------------------------------===//
+  // Compile the script
+  //===--------------------------------------------------------------------===//
+  result = compileScript(*script, pResName, output_path.c_str(), dep_info);
+
+  // Script is no longer used. Free it to get more memory.
+  delete script;
+
+  if (result == NULL) {
+    return NULL;
+  }
+
+  return result;
+}
diff --git a/lib/RenderScript/RSExecutable.cpp b/lib/RenderScript/RSExecutable.cpp
new file mode 100644
index 0000000..f30600f
--- /dev/null
+++ b/lib/RenderScript/RSExecutable.cpp
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2012, 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 "bcc/RenderScript/RSExecutable.h"
+
+#include "bcc/Config/Config.h"
+#include "bcc/Support/Disassembler.h"
+#include "bcc/Support/FileBase.h"
+#include "bcc/Support/Log.h"
+#include "bcc/Support/OutputFile.h"
+#include "bcc/ExecutionEngine/SymbolResolverProxy.h"
+
+#include <utils/String8.h>
+
+using namespace bcc;
+
+const char *RSExecutable::SpecialFunctionNames[] = {
+  "root",
+  "init",
+  ".rs.dtor",
+  // Must be NULL-terminated.
+  NULL
+};
+
+RSExecutable *RSExecutable::Create(RSInfo &pInfo,
+                                   FileBase &pObjFile,
+                                   SymbolResolverProxy &pResolver) {
+  // Load the object file. Enable the GDB's JIT debugging if the script contains
+  // debug information.
+  ObjectLoader *loader = ObjectLoader::Load(pObjFile,
+                                            pResolver,
+                                            pInfo.hasDebugInformation());
+  if (loader == NULL) {
+    return NULL;
+  }
+
+  // Now, all things required to build a RSExecutable object are ready.
+  RSExecutable *result = new (std::nothrow) RSExecutable(pInfo,
+                                                         pObjFile,
+                                                         *loader);
+  if (result == NULL) {
+    ALOGE("Out of memory when create object to hold RS result file for %s!",
+          pObjFile.getName().c_str());
+    return NULL;
+  }
+
+  unsigned idx;
+  // Resolve addresses of RS export vars.
+  idx = 0;
+  const RSInfo::ExportVarNameListTy &export_var_names =
+      pInfo.getExportVarNames();
+  for (RSInfo::ExportVarNameListTy::const_iterator
+           var_iter = export_var_names.begin(),
+           var_end = export_var_names.end(); var_iter != var_end;
+       var_iter++, idx++) {
+    const char *name = *var_iter;
+    void *addr = result->getSymbolAddress(name);
+    if (addr == NULL) {
+      ALOGW("RS export var at entry #%u named %s cannot be found in the result "
+            "object!", idx, name);
+    }
+    result->mExportVarAddrs.push_back(addr);
+  }
+
+  // Resolve addresses of RS export functions.
+  idx = 0;
+  const RSInfo::ExportFuncNameListTy &export_func_names =
+      pInfo.getExportFuncNames();
+  for (RSInfo::ExportFuncNameListTy::const_iterator
+           func_iter = export_func_names.begin(),
+           func_end = export_func_names.end(); func_iter != func_end;
+       func_iter++, idx++) {
+    const char *name = *func_iter;
+    void *addr = result->getSymbolAddress(name);
+    if (addr == NULL) {
+      ALOGW("RS export func at entry #%u named %s cannot be found in the result"
+            " object!", idx, name);
+    }
+    result->mExportFuncAddrs.push_back(addr);
+  }
+
+  // Resolve addresses of expanded RS foreach function.
+  idx = 0;
+  const RSInfo::ExportForeachFuncListTy &export_foreach_funcs =
+      pInfo.getExportForeachFuncs();
+  for (RSInfo::ExportForeachFuncListTy::const_iterator
+           foreach_iter = export_foreach_funcs.begin(),
+           foreach_end = export_foreach_funcs.end();
+       foreach_iter != foreach_end; foreach_iter++, idx++) {
+    const char *func_name = foreach_iter->first;
+    android::String8 expanded_func_name(func_name);
+    expanded_func_name.append(".expand");
+    void *addr = result->getSymbolAddress(expanded_func_name.string());
+    if (addr == NULL) {
+      ALOGW("Expanded RS foreach at entry #%u named %s cannot be found in the "
+            "result object!", idx, expanded_func_name.string());
+    }
+    result->mExportForeachFuncAddrs.push_back(addr);
+  }
+
+  // Copy pragma key/value pairs from RSInfo::getPragmas() into mPragmaKeys and
+  // mPragmaValues, respectively.
+  const RSInfo::PragmaListTy &pragmas = pInfo.getPragmas();
+  for (RSInfo::PragmaListTy::const_iterator pragma_iter = pragmas.begin(),
+          pragma_end = pragmas.end(); pragma_iter != pragma_end;
+       pragma_iter++){
+    result->mPragmaKeys.push_back(pragma_iter->first);
+    result->mPragmaValues.push_back(pragma_iter->second);
+  }
+
+  return result;
+}
+
+bool RSExecutable::syncInfo(bool pForce) {
+  if (!pForce && !mIsInfoDirty) {
+    return true;
+  }
+
+  android::String8 info_path = RSInfo::GetPath(*mObjFile);
+  OutputFile info_file(info_path.string(), FileBase::kTruncate);
+
+  if (info_file.hasError()) {
+    ALOGE("Failed to open the info file %s for write! (%s)", info_path.string(),
+          info_file.getErrorMessage().c_str());
+    return false;
+  }
+
+  // Operation to the RS info file need to acquire the lock on the output file
+  // first.
+  if (!mObjFile->lock(FileBase::kWriteLock)) {
+    ALOGE("Write to RS info file %s required the acquisition of the write lock "
+          "on %s but got failure! (%s)", info_path.string(),
+          mObjFile->getName().c_str(), info_file.getErrorMessage().c_str());
+    return false;
+  }
+
+  // Perform the write.
+  if (!mInfo->write(info_file)) {
+    ALOGE("Failed to sync the RS info file %s!", info_path.string());
+    mObjFile->unlock();
+    return false;
+  }
+
+  mObjFile->unlock();
+  mIsInfoDirty = false;
+  return true;
+}
+
+void RSExecutable::dumpDisassembly(OutputFile &pOutput) const {
+#if DEBUG_MC_DISASSEMBLER
+  if (pOutput.hasError()) {
+    return;
+  }
+
+  // Get MC codegen emitted function name list.
+  android::Vector<const char *> func_list;
+
+  if (!mLoader->getSymbolNameList(func_list, ObjectLoader::kFunctionType)) {
+    ALOGW("Failed to get the list of function name in %s for disassembly!",
+          mObjFile->getName().c_str());
+  } else {
+    // Disassemble each function
+    for (size_t i = 0, e = func_list.size(); i != e; i++) {
+      const char* func_name = func_list[i];
+      void *func = mLoader->getSymbolAddress(func_name);
+      size_t func_size = mLoader->getSymbolSize(func_name);
+
+      if (func == NULL) {
+        continue;
+      }
+      DisassembleResult result =
+          Disassemble(pOutput, DEFAULT_TARGET_TRIPLE_STRING, func_name,
+                      reinterpret_cast<const uint8_t *>(func), func_size);
+
+      if (result != kDisassembleSuccess) {
+        ALOGW("Failed to disassemble the function %s in %s (error code=%zu)!",
+              func_name, mObjFile->getName().c_str(), static_cast<size_t>(result));
+
+        if (result != kDisassembleInvalidInstruction) {
+          ALOGW("And the error occured in disassembler is fatal. Abort "
+                "disassembler on remaining functions!");
+          break;
+        }
+      }
+    }
+  }
+#endif
+  return;
+}
+
+RSExecutable::~RSExecutable() {
+  syncInfo();
+  delete mInfo;
+  delete mObjFile;
+  delete mLoader;
+}
diff --git a/lib/Transforms/ForEachExpand.cpp b/lib/RenderScript/RSForEachExpand.cpp
similarity index 80%
rename from lib/Transforms/ForEachExpand.cpp
rename to lib/RenderScript/RSForEachExpand.cpp
index 53d6bdc..cf5008f 100644
--- a/lib/Transforms/ForEachExpand.cpp
+++ b/lib/RenderScript/RSForEachExpand.cpp
@@ -14,37 +14,43 @@
  * limitations under the License.
  */
 
-#include "Config.h"
-#include "bcc/bcc_assert.h"
+//#define RS_FOREACH_EXPAND_PASS_NDEBUG 0
+#include "bcc/RenderScript/RSTransforms.h"
 
-#include "DebugHelper.h"
+#include <cstdlib>
 
-#include "llvm/DerivedTypes.h"
-#include "llvm/Function.h"
-#include "llvm/Instructions.h"
-#include "llvm/Module.h"
-#include "llvm/Pass.h"
-#include "llvm/Type.h"
-#include "llvm/Support/IRBuilder.h"
+#include <llvm/DerivedTypes.h>
+#include <llvm/Function.h>
+#include <llvm/Instructions.h>
+#include <llvm/Module.h>
+#include <llvm/Pass.h>
+#include <llvm/Support/IRBuilder.h>
+#include <llvm/Type.h>
+
+#include "bcc/Config/Config.h"
+#include "bcc/RenderScript/RSInfo.h"
+#include "bcc/Support/Log.h"
+
+using namespace bcc;
 
 namespace {
-  /* ForEachExpandPass - This pass operates on functions that are able to be
-   * called via rsForEach() or "foreach_<NAME>". We create an inner loop for
-   * the ForEach-able function to be invoked over the appropriate data cells
-   * of the input/output allocations (adjusting other relevant parameters as
-   * we go). We support doing this for any ForEach-able compute kernels.
-   * The new function name is the original function name followed by
-   * ".expand". Note that we still generate code for the original function.
-   */
-  class ForEachExpandPass : public llvm::ModulePass {
-  private:
+
+/* RSForEachExpandPass - This pass operates on functions that are able to be
+ * called via rsForEach() or "foreach_<NAME>". We create an inner loop for the
+ * ForEach-able function to be invoked over the appropriate data cells of the
+ * input/output allocations (adjusting other relevant parameters as we go). We
+ * support doing this for any ForEach-able compute kernels. The new function
+ * name is the original function name followed by ".expand". Note that we
+ * still generate code for the original function.
+ */
+class RSForEachExpandPass : public llvm::ModulePass {
+private:
   static char ID;
 
   llvm::Module *M;
   llvm::LLVMContext *C;
 
-  std::vector<std::string>& mNames;
-  std::vector<uint32_t>& mSignatures;
+  const RSInfo::ExportForeachFuncListTy &mFuncs;
 
   uint32_t getRootSignature(llvm::Function *F) {
     const llvm::NamedMDNode *ExportForEachMetadata =
@@ -65,7 +71,13 @@
       return (1 << RootArgTys.size()) - 1;
     }
 
-    bccAssert(ExportForEachMetadata->getNumOperands() > 0);
+#if !RS_FOREACH_EXPAND_PASS_NDEBUG
+    if (ExportForEachMetadata->getNumOperands() <= 0) {
+      ALOGE("Assert failed at %s:%d: Invalid #rs_export_foreach metadata in "
+            " '%s'!", __FILE__, __LINE__, M->getModuleIdentifier().c_str());
+      ::abort();
+    }
+#endif
 
     // We only handle the case for legacy root() functions here, so this is
     // hard-coded to look at only the first such function.
@@ -107,11 +119,9 @@
     return Signature & 16;
   }
 
-  public:
-  ForEachExpandPass(std::vector<std::string>& Names,
-                    std::vector<uint32_t>& Signatures)
-      : ModulePass(ID), M(NULL), C(NULL), mNames(Names),
-        mSignatures(Signatures) {
+public:
+  RSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs)
+      : ModulePass(ID), M(NULL), C(NULL), mFuncs(pForeachFuncs) {
   }
 
   /* Performs the actual optimization on a selected function. On success, the
@@ -260,7 +270,13 @@
       Args++;
     }
 
-    bccAssert(Args == F->arg_end());
+#if !RS_FOREACH_EXPAND_PASS_NDEBUG
+    if (Args != F->arg_end()) {
+      ALOGE("Assert failed at %s:%d: Invalid signature to the foreach function "
+            "'%s'!", __FILE__, __LINE__, F->getName().str().c_str());
+      ::abort();
+    }
+#endif
 
     llvm::BasicBlock *Loop = llvm::BasicBlock::Create(*C, "Loop", ExpandedFunc);
     llvm::BasicBlock *Exit = llvm::BasicBlock::Create(*C, "Exit", ExpandedFunc);
@@ -339,11 +355,14 @@
     this->M = &M;
     C = &M.getContext();
 
-    bccAssert(mNames.size() == mSignatures.size());
-    for (int i = 0, e = mNames.size(); i != e; i++) {
-      llvm::Function *kernel = M.getFunction(mNames[i]);
+    for (RSInfo::ExportForeachFuncListTy::const_iterator
+             func_iter = mFuncs.begin(), func_end = mFuncs.end();
+         func_iter != func_end; func_iter++) {
+      const char *name = func_iter->first;
+      uint32_t signature = func_iter->second;
+      llvm::Function *kernel = M.getFunction(name);
       if (kernel && kernel->getReturnType()->isVoidTy()) {
-        Changed |= ExpandFunction(kernel, mSignatures[i]);
+        Changed |= ExpandFunction(kernel, signature);
       }
     }
 
@@ -354,16 +373,17 @@
     return "ForEach-able Function Expansion";
   }
 
-  };
-}  // end anonymous namespace
+}; // end RSForEachExpandPass
 
-char ForEachExpandPass::ID = 0;
+} // end anonymous namespace
+
+char RSForEachExpandPass::ID = 0;
 
 namespace bcc {
 
-  llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                            std::vector<uint32_t>& Signatures) {
-    return new ForEachExpandPass(Names, Signatures);
-  }
+llvm::ModulePass *
+createRSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs){
+  return new RSForEachExpandPass(pForeachFuncs);
+}
 
-}  // namespace bcc
+} // end namespace bcc
diff --git a/lib/RenderScript/RSInfo.cpp b/lib/RenderScript/RSInfo.cpp
new file mode 100644
index 0000000..e06ea62
--- /dev/null
+++ b/lib/RenderScript/RSInfo.cpp
@@ -0,0 +1,368 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+//#define LOG_NDEBUG 0
+#include "bcc/RenderScript/RSInfo.h"
+
+#include <dlfcn.h>
+
+#include <cstring>
+#include <new>
+
+#include "bcc/Support/FileBase.h"
+#include "bcc/Support/Log.h"
+
+#include <cutils/properties.h>
+
+using namespace bcc;
+
+const char RSInfo::LibBCCPath[] = "/system/lib/libbcc.so";
+const char RSInfo::LibRSPath[] = "/system/lib/libRS.so";
+const char RSInfo::LibCLCorePath[] = "/system/lib/libclcore.bc";
+#if defined(ARCH_ARM_HAVE_NEON)
+const char RSInfo::LibCLCoreNEONPath[] = "/system/lib/libclcore_neon.bc";
+#endif
+
+const uint8_t *RSInfo::LibBCCSHA1 = NULL;
+const uint8_t *RSInfo::LibRSSHA1 = NULL;
+const uint8_t *RSInfo::LibCLCoreSHA1 = NULL;
+#if defined(ARCH_ARM_HAVE_NEON)
+const uint8_t *RSInfo::LibCLCoreNEONSHA1 = NULL;
+#endif
+
+void RSInfo::LoadBuiltInSHA1Information() {
+  if (LibBCCSHA1 != NULL) {
+    // Loaded before.
+    return;
+  }
+
+  void *h = ::dlopen("/system/lib/libbcc.sha1.so", RTLD_LAZY | RTLD_NOW);
+  if (h == NULL) {
+    ALOGE("Failed to load SHA-1 information from shared library '"
+          "/system/lib/libbcc.sha1.so'! (%s)", ::dlerror());
+    return;
+  }
+
+  LibBCCSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libbcc_so_SHA1"));
+  LibRSSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libRS_so_SHA1"));
+  LibCLCoreSHA1 =
+      reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_bc_SHA1"));
+#if defined(ARCH_ARM_HAVE_NEON)
+  LibCLCoreNEONSHA1 =
+      reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_neon_bc_SHA1"));
+#endif
+
+  return;
+}
+
+android::String8 RSInfo::GetPath(const FileBase &pFile) {
+  android::String8 result(pFile.getName().c_str());
+  result.append(".info");
+  return result;
+}
+
+#define PRINT_DEPENDENCY(PREFIX, N, X) \
+        ALOGV("\t" PREFIX "Source name: %s, "                                 \
+                          "SHA-1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"   \
+                                 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",  \
+              (N), (X)[ 0], (X)[ 1], (X)[ 2], (X)[ 3], (X)[ 4], (X)[ 5],      \
+                   (X)[ 6], (X)[ 7], (X)[ 8], (X)[ 9], (X)[10], (X)[11],      \
+                   (X)[12], (X)[13], (X)[14], (X)[15], (X)[16], (X)[17],      \
+                   (X)[18], (X)[19]);
+
+bool RSInfo::CheckDependency(const RSInfo &pInfo,
+                             const char *pInputFilename,
+                             const DependencyTableTy &pDeps) {
+  // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc plus
+  // libclcore_neon.bc if NEON is available on the target device.
+#if !defined(ARCH_ARM_HAVE_NEON)
+  static const unsigned NumBuiltInDependencies = 3;
+#else
+  static const unsigned NumBuiltInDependencies = 4;
+#endif
+
+  LoadBuiltInSHA1Information();
+
+  if (pInfo.mDependencyTable.size() != (pDeps.size() + NumBuiltInDependencies)) {
+    ALOGD("Number of dependencies recorded mismatch (%lu v.s. %lu) in %s!",
+          static_cast<unsigned long>(pInfo.mDependencyTable.size()),
+          static_cast<unsigned long>(pDeps.size()), pInputFilename);
+    return false;
+  } else {
+    // Built-in dependencies always go first.
+    const std::pair<const char *, const uint8_t *> &cache_libbcc_dep =
+        pInfo.mDependencyTable[0];
+    const std::pair<const char *, const uint8_t *> &cache_libRS_dep =
+        pInfo.mDependencyTable[1];
+    const std::pair<const char *, const uint8_t *> &cache_libclcore_dep =
+        pInfo.mDependencyTable[2];
+#if defined(ARCH_ARM_HAVE_NEON)
+    const std::pair<const char *, const uint8_t *> &cache_libclcore_neon_dep =
+        pInfo.mDependencyTable[3];
+#endif
+
+    // Check libbcc.so.
+    if (::memcmp(cache_libbcc_dep.second, LibBCCSHA1, SHA1_DIGEST_LENGTH) != 0) {
+        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
+              LibBCCPath);
+        PRINT_DEPENDENCY("current - ", LibBCCPath, LibBCCSHA1);
+        PRINT_DEPENDENCY("cache - ", cache_libbcc_dep.first,
+                                     cache_libbcc_dep.second);
+        return false;
+    }
+
+    // Check libRS.so.
+    if (::memcmp(cache_libRS_dep.second, LibRSSHA1, SHA1_DIGEST_LENGTH) != 0) {
+        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
+              LibRSPath);
+        PRINT_DEPENDENCY("current - ", LibRSPath, LibRSSHA1);
+        PRINT_DEPENDENCY("cache - ", cache_libRS_dep.first,
+                                     cache_libRS_dep.second);
+        return false;
+    }
+
+    // Check libclcore.bc.
+    if (::memcmp(cache_libclcore_dep.second, LibCLCoreSHA1,
+                 SHA1_DIGEST_LENGTH) != 0) {
+        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
+              LibRSPath);
+        PRINT_DEPENDENCY("current - ", LibCLCorePath, LibCLCoreSHA1);
+        PRINT_DEPENDENCY("cache - ", cache_libclcore_dep.first,
+                                     cache_libclcore_dep.second);
+        return false;
+    }
+
+#if defined(ARCH_ARM_HAVE_NEON)
+    // Check libclcore_neon.bc if NEON is available.
+    if (::memcmp(cache_libclcore_neon_dep.second, LibCLCoreNEONSHA1,
+                 SHA1_DIGEST_LENGTH) != 0) {
+        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
+              LibRSPath);
+        PRINT_DEPENDENCY("current - ", LibCLCoreNEONPath, LibCLCoreNEONSHA1);
+        PRINT_DEPENDENCY("cache - ", cache_libclcore_neon_dep.first,
+                                     cache_libclcore_neon_dep.second);
+        return false;
+    }
+#endif
+
+    for (unsigned i = 0; i < pDeps.size(); i++) {
+      const std::pair<const char *, const uint8_t *> &cache_dep =
+          pInfo.mDependencyTable[i + NumBuiltInDependencies];
+
+      if ((::strcmp(pDeps[i].first, cache_dep.first) != 0) ||
+          (::memcmp(pDeps[i].second, cache_dep.second,
+                    SHA1_DIGEST_LENGTH) != 0)) {
+        ALOGD("Cache %s is dirty due to the source it dependends on has been "
+              "changed:", pInputFilename);
+        PRINT_DEPENDENCY("given - ", pDeps[i].first, pDeps[i].second);
+        PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
+        return false;
+      }
+    }
+  }
+
+  return true;
+}
+
+RSInfo::RSInfo(size_t pStringPoolSize) : mStringPool(NULL) {
+  ::memset(&mHeader, 0, sizeof(mHeader));
+
+  ::memcpy(mHeader.magic, RSINFO_MAGIC, sizeof(mHeader.magic));
+  ::memcpy(mHeader.version, RSINFO_VERSION, sizeof(mHeader.version));
+
+  mHeader.headerSize = sizeof(mHeader);
+
+  mHeader.dependencyTable.itemSize = sizeof(rsinfo::DependencyTableItem);
+  mHeader.pragmaList.itemSize = sizeof(rsinfo::PragmaItem);
+  mHeader.objectSlotList.itemSize = sizeof(rsinfo::ObjectSlotItem);
+  mHeader.exportVarNameList.itemSize = sizeof(rsinfo::ExportVarNameItem);
+  mHeader.exportFuncNameList.itemSize = sizeof(rsinfo::ExportFuncNameItem);
+  mHeader.exportForeachFuncList.itemSize = sizeof(rsinfo::ExportForeachFuncItem);
+
+  if (pStringPoolSize > 0) {
+    mHeader.strPoolSize = pStringPoolSize;
+    mStringPool = new (std::nothrow) char [ mHeader.strPoolSize ];
+    if (mStringPool == NULL) {
+      ALOGE("Out of memory when allocate memory for string pool in RSInfo "
+            "constructor (size: %u)!", mHeader.strPoolSize);
+    }
+  }
+}
+
+RSInfo::~RSInfo() {
+  delete [] mStringPool;
+}
+
+bool RSInfo::layout(off_t initial_offset) {
+  mHeader.dependencyTable.offset = initial_offset +
+                                   mHeader.headerSize +
+                                   mHeader.strPoolSize;
+  mHeader.dependencyTable.count = mDependencyTable.size();
+
+#define AFTER(_list) ((_list).offset + (_list).itemSize * (_list).count)
+  mHeader.pragmaList.offset = AFTER(mHeader.dependencyTable);
+  mHeader.pragmaList.count = mPragmas.size();
+
+  mHeader.objectSlotList.offset = AFTER(mHeader.pragmaList);
+  mHeader.objectSlotList.count = mObjectSlots.size();
+
+  mHeader.exportVarNameList.offset = AFTER(mHeader.objectSlotList);
+  mHeader.exportVarNameList.count = mExportVarNames.size();
+
+  mHeader.exportFuncNameList.offset = AFTER(mHeader.exportVarNameList);
+  mHeader.exportFuncNameList.count = mExportFuncNames.size();
+
+  mHeader.exportForeachFuncList.offset = AFTER(mHeader.exportFuncNameList);
+  mHeader.exportForeachFuncList.count = mExportForeachFuncs.size();
+#undef AFTER
+
+  return true;
+}
+
+void RSInfo::dump() const {
+  // Hide the codes to save the code size when debugging is disabled.
+#if !LOG_NDEBUG
+
+  // Dump header
+  ALOGV("RSInfo Header:");
+  ALOGV("\tIs threadable: %s", ((mHeader.isThreadable) ? "true" : "false"));
+  ALOGV("\tHeader size: %u", mHeader.headerSize);
+  ALOGV("\tString pool size: %u", mHeader.strPoolSize);
+
+#define DUMP_LIST_HEADER(_name, _header) do { \
+  ALOGV(_name ":"); \
+  ALOGV("\toffset: %u", (_header).offset);  \
+  ALOGV("\t# of item: %u", (_header).count);  \
+  ALOGV("\tsize of each item: %u", (_header).itemSize); \
+} while (false)
+  DUMP_LIST_HEADER("Dependency table", mHeader.dependencyTable);
+  for (DependencyTableTy::const_iterator dep_iter = mDependencyTable.begin(),
+          dep_end = mDependencyTable.end(); dep_iter != dep_end; dep_iter++) {
+    PRINT_DEPENDENCY("", dep_iter->first, dep_iter->second);
+  }
+
+  DUMP_LIST_HEADER("Pragma list", mHeader.pragmaList);
+  for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
+        pragma_end = mPragmas.end(); pragma_iter != pragma_end; pragma_iter++) {
+    ALOGV("\tkey: %s, value: %s", pragma_iter->first, pragma_iter->second);
+  }
+
+  DUMP_LIST_HEADER("RS object slots", mHeader.objectSlotList);
+  for (ObjectSlotListTy::const_iterator slot_iter = mObjectSlots.begin(),
+          slot_end = mObjectSlots.end(); slot_iter != slot_end; slot_iter++) {
+    ALOGV("slot: %u", *slot_iter);
+  }
+
+  DUMP_LIST_HEADER("RS export variables", mHeader.exportVarNameList);
+  for (ExportVarNameListTy::const_iterator var_iter = mExportVarNames.begin(),
+          var_end = mExportVarNames.end(); var_iter != var_end; var_iter++) {
+    ALOGV("name: %s", *var_iter);
+  }
+
+  DUMP_LIST_HEADER("RS export functions", mHeader.exportFuncNameList);
+  for (ExportFuncNameListTy::const_iterator func_iter = mExportFuncNames.begin(),
+        func_end = mExportFuncNames.end(); func_iter != func_end; func_iter++) {
+    ALOGV("name: %s", *func_iter);
+  }
+
+  DUMP_LIST_HEADER("RS foreach list", mHeader.exportForeachFuncList);
+  for (ExportForeachFuncListTy::const_iterator
+          foreach_iter = mExportForeachFuncs.begin(),
+          foreach_end = mExportForeachFuncs.end(); foreach_iter != foreach_end;
+          foreach_iter++) {
+    ALOGV("name: %s, signature: %05x", foreach_iter->first,
+                                       foreach_iter->second);
+  }
+#undef DUMP_LIST_HEADER
+
+#endif // LOG_NDEBUG
+  return;
+}
+
+const char *RSInfo::getStringFromPool(rsinfo::StringIndexTy pStrIdx) const {
+  // String pool uses direct indexing. Ensure that the pStrIdx is within the
+  // range.
+  if (pStrIdx >= mHeader.strPoolSize) {
+    ALOGE("String index #%u is out of range in string pool (size: %u)!",
+          pStrIdx, mHeader.strPoolSize);
+    return NULL;
+  }
+  return &mStringPool[ pStrIdx ];
+}
+
+rsinfo::StringIndexTy RSInfo::getStringIdxInPool(const char *pStr) const {
+  // Assume we are on the flat memory architecture (i.e., the memory space is
+  // continuous.)
+  if ((mStringPool + mHeader.strPoolSize) < pStr) {
+    ALOGE("String %s does not in the string pool!", pStr);
+    return rsinfo::gInvalidStringIndex;
+  }
+  return (pStr - mStringPool);
+}
+
+RSInfo::FloatPrecision RSInfo::getFloatPrecisionRequirement() const {
+  // Check to see if we have any FP precision-related pragmas.
+  static const char relaxed_pragma[] = "rs_fp_relaxed";
+  static const char imprecise_pragma[] = "rs_fp_imprecise";
+  static const char full_pragma[] = "rs_fp_full";
+  bool relaxed_pragma_seen = false;
+  RSInfo::FloatPrecision result;
+
+  for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
+           pragma_end = mPragmas.end(); pragma_iter != pragma_end;
+       pragma_iter++) {
+    const char *pragma_key = pragma_iter->first;
+    if (::strcmp(pragma_key, relaxed_pragma) == 0) {
+      relaxed_pragma_seen = true;
+    } else if (::strcmp(pragma_key, imprecise_pragma) == 0) {
+      if (relaxed_pragma_seen) {
+        ALOGW("Multiple float precision pragmas specified!");
+      }
+      // Fast return when there's rs_fp_imprecise specified.
+      result = FP_Imprecise;
+    }
+  }
+
+  // Imprecise is selected over Relaxed precision.
+  // In the absence of both, we stick to the default Full precision.
+  if (relaxed_pragma_seen) {
+    result = FP_Relaxed;
+  } else {
+    result = FP_Full;
+  }
+
+  // Provide an override for precsion via adb shell setprop
+  // adb shell setprop debug.rs.precision rs_fp_full
+  // adb shell setprop debug.rs.precision rs_fp_relaxed
+  // adb shell setprop debug.rs.precision rs_fp_imprecise
+  char precision_prop_buf[PROPERTY_VALUE_MAX];
+  property_get("debug.rs.precision", precision_prop_buf, "");
+
+  if (precision_prop_buf[0]) {
+    if (::strcmp(precision_prop_buf, relaxed_pragma) == 0) {
+      ALOGI("Switching to RS FP relaxed mode via setprop");
+      result = FP_Relaxed;
+    } else if (::strcmp(precision_prop_buf, imprecise_pragma) == 0) {
+      ALOGI("Switching to RS FP imprecise mode via setprop");
+      result = FP_Imprecise;
+    } else if (::strcmp(precision_prop_buf, full_pragma) == 0) {
+      ALOGI("Switching to RS FP full mode via setprop");
+      result = FP_Full;
+    }
+  }
+
+  return result;
+}
diff --git a/lib/RenderScript/RSInfoExtractor.cpp b/lib/RenderScript/RSInfoExtractor.cpp
new file mode 100644
index 0000000..f05fa68
--- /dev/null
+++ b/lib/RenderScript/RSInfoExtractor.cpp
@@ -0,0 +1,416 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+//===----------------------------------------------------------------------===//
+// This file implements RSInfo::ExtractFromSource()
+//===----------------------------------------------------------------------===//
+#include "bcc/RenderScript/RSInfo.h"
+
+#include <llvm/Constants.h>
+#include <llvm/Metadata.h>
+#include <llvm/Module.h>
+
+#include "bcc/Source.h"
+#include "bcc/Support/Log.h"
+
+using namespace bcc;
+
+namespace {
+
+// Name of metadata node where pragma info resides (should be synced with
+// slang.cpp)
+const llvm::StringRef pragma_metadata_name("#pragma");
+
+/*
+ * The following names should be synced with the one appeared in
+ * slang_rs_metadata.h.
+ */
+// Name of metadata node where exported variable names reside
+const llvm::StringRef export_var_metadata_name("#rs_export_var");
+
+// Name of metadata node where exported function names reside
+const llvm::StringRef export_func_metadata_name("#rs_export_func");
+
+// Name of metadata node where exported ForEach name information resides
+const llvm::StringRef export_foreach_name_metadata_name("#rs_export_foreach_name");
+
+// Name of metadata node where exported ForEach signature information resides
+const llvm::StringRef export_foreach_metadata_name("#rs_export_foreach");
+
+// Name of metadata node where RS object slot info resides (should be
+const llvm::StringRef object_slot_metadata_name("#rs_object_slots");
+
+inline llvm::StringRef getStringFromOperand(const llvm::Value *pString) {
+  if ((pString != NULL) && (pString->getValueID() == llvm::Value::MDStringVal)) {
+    return static_cast<const llvm::MDString *>(pString)->getString();
+  }
+  return llvm::StringRef();
+}
+
+template<size_t NumOperands>
+inline size_t getMetadataStringLength(const llvm::NamedMDNode *pMetadata) {
+  if (pMetadata == NULL) {
+    return 0;
+  }
+
+  size_t string_size = 0;
+  for (unsigned i = 0, e = pMetadata->getNumOperands(); i < e; i++) {
+    llvm::MDNode *node = pMetadata->getOperand(i);
+    if ((node != NULL) && (node->getNumOperands() >= NumOperands)) {
+      // Compiler try its best to unroll this loop since NumOperands is a
+      // template parameter (therefore the number of iteration can be determined
+      // at compile-time and it's usually small.)
+      for (unsigned j = 0; j < NumOperands; j++) {
+        llvm::StringRef s = getStringFromOperand(node->getOperand(j));
+        if (s.size() > 0) {
+          // +1 is for the null-terminator at the end of string.
+          string_size += (s.size() + 1);
+        }
+      }
+    }
+  }
+
+  return string_size;
+}
+
+// Write a string pString to the string pool pStringPool at offset pWriteStart.
+// Return the pointer the pString resides within the string pool.
+const char *writeString(const llvm::StringRef &pString, char *pStringPool,
+                        off_t *pWriteStart) {
+  if (pString.empty()) {
+    return pStringPool;
+  }
+
+  char *pStringWriteStart = pStringPool + *pWriteStart;
+  // Copy the string.
+  ::memcpy(pStringWriteStart, pString.data(), pString.size());
+  // Write null-terminator at the end of the string.
+  pStringWriteStart[ pString.size() ] = '\0';
+  // Update pWriteStart.
+  *pWriteStart += (pString.size() + 1);
+
+  return pStringWriteStart;
+}
+
+bool writeDependency(const std::string &pSourceName, const uint8_t *pSHA1,
+                     char *pStringPool, off_t *pWriteStart,
+                     RSInfo::DependencyTableTy &pDepTable) {
+  const char *source_name = writeString(pSourceName, pStringPool, pWriteStart);
+
+  uint8_t *sha1 = reinterpret_cast<uint8_t *>(pStringPool + *pWriteStart);
+
+  // SHA-1 is special. It's size of SHA1_DIGEST_LENGTH (=20) bytes long without
+  // null-terminator.
+  ::memcpy(sha1, pSHA1, SHA1_DIGEST_LENGTH);
+  // Record in the result RSInfo object.
+  pDepTable.push(std::make_pair(source_name, sha1));
+  // Update the string pool pointer.
+  *pWriteStart += SHA1_DIGEST_LENGTH;
+
+  return true;
+}
+
+} // end anonymous namespace
+
+RSInfo *RSInfo::ExtractFromSource(const Source &pSource,
+                                  const DependencyTableTy &pDeps)
+{
+  const llvm::Module &module = pSource.getModule();
+  const char *module_name = module.getModuleIdentifier().c_str();
+
+  const llvm::NamedMDNode *pragma =
+      module.getNamedMetadata(pragma_metadata_name);
+  const llvm::NamedMDNode *export_var =
+      module.getNamedMetadata(export_var_metadata_name);
+  const llvm::NamedMDNode *export_func =
+      module.getNamedMetadata(export_func_metadata_name);
+  const llvm::NamedMDNode *export_foreach_name =
+      module.getNamedMetadata(export_foreach_name_metadata_name);
+  const llvm::NamedMDNode *export_foreach_signature =
+      module.getNamedMetadata(export_foreach_metadata_name);
+  const llvm::NamedMDNode *object_slots =
+      module.getNamedMetadata(object_slot_metadata_name);
+
+  // Always write a byte 0x0 at the beginning of the string pool.
+  size_t string_pool_size = 1;
+  off_t cur_string_pool_offset = 0;
+
+  RSInfo *result = NULL;
+
+  // Handle legacy case for pre-ICS bitcode that doesn't contain a metadata
+  // section for ForEach. We generate a full signature for a "root" function.
+  if ((export_foreach_name == NULL) || (export_foreach_signature == NULL)) {
+    export_foreach_name = NULL;
+    export_foreach_signature = NULL;
+    string_pool_size += 5;  // insert "root\0" for #rs_export_foreach_name
+  }
+
+  string_pool_size += getMetadataStringLength<2>(pragma);
+  string_pool_size += getMetadataStringLength<1>(export_var);
+  string_pool_size += getMetadataStringLength<1>(export_func);
+  string_pool_size += getMetadataStringLength<1>(export_foreach_name);
+
+  // Don't forget to reserve the space for the dependency informationin string
+  // pool.
+  string_pool_size += ::strlen(LibBCCPath) + 1 + SHA1_DIGEST_LENGTH;
+  string_pool_size += ::strlen(LibRSPath) + 1 + SHA1_DIGEST_LENGTH;
+  string_pool_size += ::strlen(LibCLCorePath) + 1 + SHA1_DIGEST_LENGTH;
+#if defined(ARCH_ARM_HAVE_NEON)
+  string_pool_size += ::strlen(LibCLCoreNEONPath) + 1 + SHA1_DIGEST_LENGTH;
+#endif
+  for (unsigned i = 0, e = pDeps.size(); i != e; i++) {
+    // +1 for null-terminator
+    string_pool_size += ::strlen(/* name */pDeps[i].first) + 1;
+    // +SHA1_DIGEST_LENGTH for SHA-1 checksum
+    string_pool_size += SHA1_DIGEST_LENGTH;
+  }
+
+  // Allocate result object
+  result = new (std::nothrow) RSInfo(string_pool_size);
+  if (result == NULL) {
+    ALOGE("Out of memory when create RSInfo object for %s!", module_name);
+    goto bail;
+  }
+
+  // Check string pool.
+  if (result->mStringPool == NULL) {
+    ALOGE("Out of memory when allocate string pool in RSInfo object for %s!",
+          module_name);
+    goto bail;
+  }
+
+  // First byte of string pool should be an empty string
+  result->mStringPool[ cur_string_pool_offset++ ] = '\0';
+
+  // Populate all the strings and data.
+#define FOR_EACH_NODE_IN(_metadata, _node)  \
+  for (unsigned i = 0, e = (_metadata)->getNumOperands(); i != e; i++)  \
+    if (((_node) = (_metadata)->getOperand(i)) != NULL)
+  //===--------------------------------------------------------------------===//
+  // #pragma
+  //===--------------------------------------------------------------------===//
+  // Pragma is actually a key-value pair. The value can be an empty string while
+  // the key cannot.
+  if (pragma != NULL) {
+    llvm::MDNode *node;
+    FOR_EACH_NODE_IN(pragma, node) {
+        llvm::StringRef key = getStringFromOperand(node->getOperand(0));
+        llvm::StringRef val = getStringFromOperand(node->getOperand(1));
+        if (key.empty()) {
+          ALOGW("%s contains pragma metadata with empty key (skip)!",
+                module_name);
+        } else {
+          result->mPragmas.push(std::make_pair(
+              writeString(key, result->mStringPool, &cur_string_pool_offset),
+              writeString(val, result->mStringPool, &cur_string_pool_offset)));
+        } // key.empty()
+    } // FOR_EACH_NODE_IN
+  } // pragma != NULL
+
+  //===--------------------------------------------------------------------===//
+  // #rs_export_var
+  //===--------------------------------------------------------------------===//
+  if (export_var != NULL) {
+    llvm::MDNode *node;
+    FOR_EACH_NODE_IN(export_var, node) {
+      llvm::StringRef name = getStringFromOperand(node->getOperand(0));
+      if (name.empty()) {
+        ALOGW("%s contains empty entry in #rs_export_var metadata (skip)!",
+              module_name);
+      } else {
+          result->mExportVarNames.push(
+              writeString(name, result->mStringPool, &cur_string_pool_offset));
+      }
+    }
+  }
+
+  //===--------------------------------------------------------------------===//
+  // #rs_export_func
+  //===--------------------------------------------------------------------===//
+  if (export_func != NULL) {
+    llvm::MDNode *node;
+    FOR_EACH_NODE_IN(export_func, node) {
+      llvm::StringRef name = getStringFromOperand(node->getOperand(0));
+      if (name.empty()) {
+        ALOGW("%s contains empty entry in #rs_export_func metadata (skip)!",
+              module_name);
+      } else {
+        result->mExportFuncNames.push(
+            writeString(name, result->mStringPool, &cur_string_pool_offset));
+      }
+    }
+  }
+
+  //===--------------------------------------------------------------------===//
+  // #rs_export_foreach and #rs_export_foreach_name
+  //===--------------------------------------------------------------------===//
+  // It's a little bit complicated to deal with #rs_export_foreach (the
+  // signature of foreach-able function) and #rs_export_foreach_name (the name
+  // of function which is foreach-able). We have to maintain a legacy case:
+  //
+  //  In pre-ICS bitcode, forEach feature only supports non-graphic root()
+  //  function and only one signature corresponded to that non-graphic root()
+  //  was written to the #rs_export_foreach metadata section. There's no
+  //  #rs_export_foreach_name metadata section.
+  //
+  // Currently, not only non-graphic root() is supported but also other
+  // functions that are exportable. Therefore, a new metadata section
+  // #rs_export_foreach_name is added to specify which functions are
+  // for-eachable. In this case, #rs_export_foreach (the function name) and
+  // #rs_export_foreach metadata (the signature) is one-to-one mapping among
+  // their entries.
+  if ((export_foreach_name != NULL) && (export_foreach_signature != NULL)) {
+    unsigned num_foreach_function;
+
+    // Should be one-to-one mapping.
+    if (export_foreach_name->getNumOperands() !=
+        export_foreach_signature->getNumOperands()) {
+      ALOGE("Mismatch number of foreach-able function names (%u) in "
+            "#rs_export_foreach_name and number of signatures (%u) "
+            "in %s!", export_foreach_name->getNumOperands(),
+            export_foreach_signature->getNumOperands(), module_name);
+      goto bail;
+    }
+
+    num_foreach_function = export_foreach_name->getNumOperands();
+    for (unsigned i = 0; i < num_foreach_function; i++) {
+      llvm::MDNode *name_node = export_foreach_name->getOperand(i);
+      llvm::MDNode *signature_node = export_foreach_signature->getOperand(i);
+
+      llvm::StringRef name, signature_string;
+      if (name_node != NULL) {
+        name = getStringFromOperand(name_node->getOperand(0));
+      }
+      if (signature_node != NULL) {
+        signature_string = getStringFromOperand(signature_node->getOperand(0));
+      }
+
+      if (!name.empty() && !signature_string.empty()) {
+        // Both name_node and signature_node are not NULL nodes.
+        uint32_t signature;
+        if (signature_string.getAsInteger(10, signature)) {
+          ALOGE("Non-integer signature value '%s' for function %s found in %s!",
+                signature_string.str().c_str(), name.str().c_str(), module_name);
+          goto bail;
+        }
+        result->mExportForeachFuncs.push(std::make_pair(
+              writeString(name, result->mStringPool, &cur_string_pool_offset),
+              signature));
+      } else {
+        // One or both of the name and signature value are empty. It's safe only
+        // if both of them are empty.
+        if (name.empty() && signature_string.empty()) {
+          ALOGW("Entries #%u at #rs_export_foreach_name and #rs_export_foreach"
+                " are both NULL in %s! (skip)", i, module_name);
+          continue;
+        } else {
+          ALOGE("Entries #%u at %s is NULL in %s! (skip)", i,
+                (name.empty() ? "#rs_export_foreach_name" :
+                                "#rs_export_foreach"), module_name);
+          goto bail;
+        }
+      }
+    } // end for
+  } else {
+    // To handle the legacy case, we generate a full signature for a "root"
+    // function which means that we need to set the bottom 5 bits (0x1f) in the
+    // mask.
+    result->mExportForeachFuncs.push(std::make_pair(
+          writeString(llvm::StringRef("root"), result->mStringPool,
+                      &cur_string_pool_offset), 0x1f));
+  }
+
+  //===--------------------------------------------------------------------===//
+  // #rs_object_slots
+  //===--------------------------------------------------------------------===//
+  if (object_slots != NULL) {
+    llvm::MDNode *node;
+    FOR_EACH_NODE_IN(object_slots, node) {
+      llvm::StringRef val = getStringFromOperand(node->getOperand(0));
+      if (val.empty()) {
+        ALOGW("%s contains empty entry in #rs_object_slots (skip)!",
+              module.getModuleIdentifier().c_str());
+      } else {
+        uint32_t slot;
+        if (val.getAsInteger(10, slot)) {
+          ALOGE("Non-integer object slot value '%s' in %s!", val.str().c_str(),
+                module.getModuleIdentifier().c_str());
+          goto bail;
+        }
+      }
+    }
+  }
+#undef FOR_EACH_NODE_IN
+
+  //===--------------------------------------------------------------------===//
+  // Record built-in dependency information.
+  //===--------------------------------------------------------------------===//
+  LoadBuiltInSHA1Information();
+
+  if (!writeDependency(LibBCCPath, LibBCCSHA1,
+                       result->mStringPool, &cur_string_pool_offset,
+                       result->mDependencyTable)) {
+    goto bail;
+  }
+
+  if (!writeDependency(LibRSPath, LibRSSHA1,
+                       result->mStringPool, &cur_string_pool_offset,
+                       result->mDependencyTable)) {
+    goto bail;
+  }
+
+  if (!writeDependency(LibCLCorePath, LibCLCoreSHA1,
+                       result->mStringPool, &cur_string_pool_offset,
+                       result->mDependencyTable)) {
+    goto bail;
+  }
+
+#if defined(ARCH_ARM_HAVE_NEON)
+  if (!writeDependency(LibCLCoreNEONPath, LibCLCoreNEONSHA1,
+                       result->mStringPool, &cur_string_pool_offset,
+                       result->mDependencyTable)) {
+    goto bail;
+  }
+#endif
+
+  //===--------------------------------------------------------------------===//
+  // Record dependency information.
+  //===--------------------------------------------------------------------===//
+  for (unsigned i = 0, e = pDeps.size(); i != e; i++) {
+    if (!writeDependency(/* name */pDeps[i].first, /* SHA-1 */pDeps[i].second,
+                         result->mStringPool, &cur_string_pool_offset,
+                         result->mDependencyTable)) {
+      goto bail;
+    }
+  }
+
+  //===--------------------------------------------------------------------===//
+  // Determine whether the bitcode contains debug information
+  //===--------------------------------------------------------------------===//
+  // The root context of the debug information in the bitcode is put under
+  // the metadata named "llvm.dbg.cu".
+  result->mHeader.hasDebugInformation =
+      static_cast<uint8_t>(module.getNamedMetadata("llvm.dbg.cu") != NULL);
+
+  assert((cur_string_pool_offset == string_pool_size) &&
+            "Unexpected string pool size!");
+
+  return result;
+
+bail:
+  delete result;
+  return NULL;
+}
diff --git a/lib/RenderScript/RSInfoReader.cpp b/lib/RenderScript/RSInfoReader.cpp
new file mode 100644
index 0000000..9a25c24
--- /dev/null
+++ b/lib/RenderScript/RSInfoReader.cpp
@@ -0,0 +1,330 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+//===----------------------------------------------------------------------===//
+// This file implements RSInfo::ReadFromFile()
+//===----------------------------------------------------------------------===//
+
+#include "bcc/RenderScript/RSInfo.h"
+
+#include <new>
+
+#include <utils/FileMap.h>
+
+#include "bcc/Support/Log.h"
+#include "bcc/Support/InputFile.h"
+
+using namespace bcc;
+
+namespace {
+
+template<typename ItemType, typename ItemContainer>
+inline bool helper_read_list_item(const ItemType &pItem,
+                                  const RSInfo &pInfo,
+                                  ItemContainer &pResult);
+
+// Process DependencyTableItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::DependencyTableItem, RSInfo::DependencyTableTy>(
+    const rsinfo::DependencyTableItem &pItem,
+    const RSInfo &pInfo,
+    RSInfo::DependencyTableTy &pResult)
+{
+  const char *id = pInfo.getStringFromPool(pItem.id);
+  const uint8_t *sha1 =
+      reinterpret_cast<const uint8_t *>(pInfo.getStringFromPool(pItem.sha1));
+
+  if (id == NULL) {
+    ALOGE("Invalid string index %d for source id in RS dependenct table.",
+          pItem.id);
+    return false;
+  }
+
+  if (sha1 == NULL) {
+    ALOGE("Invalid string index %d for SHA-1 checksum in RS dependenct table.",
+          pItem.id);
+    return false;
+  }
+
+  pResult.push(std::make_pair(id, sha1));
+  return true;
+}
+
+// Process PragmaItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::PragmaItem, RSInfo::PragmaListTy>(
+    const rsinfo::PragmaItem &pItem,
+    const RSInfo &pInfo,
+    RSInfo::PragmaListTy &pResult)
+{
+  const char *key = pInfo.getStringFromPool(pItem.key);
+  const char *value =pInfo.getStringFromPool(pItem.value);
+
+  if (key == NULL) {
+    ALOGE("Invalid string index %d for key in RS pragma list.", pItem.key);
+    return false;
+  }
+
+  if (value == NULL) {
+    ALOGE("Invalid string index %d for value in RS pragma list.", pItem.value);
+    return false;
+  }
+
+  pResult.push(std::make_pair(key, value));
+  return true;
+}
+
+// Procee ObjectSlotItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ObjectSlotItem, RSInfo::ObjectSlotListTy>(
+    const rsinfo::ObjectSlotItem &pItem,
+    const RSInfo &pInfo,
+    RSInfo::ObjectSlotListTy &pResult)
+{
+  pResult.push(pItem.slot);
+  return true;
+}
+
+// Procee ExportVarNameItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ExportVarNameItem, RSInfo::ExportVarNameListTy>(
+    const rsinfo::ExportVarNameItem &pItem,
+    const RSInfo &pInfo,
+    RSInfo::ExportVarNameListTy &pResult)
+{
+  const char *name = pInfo.getStringFromPool(pItem.name);
+
+  if (name == NULL) {
+    ALOGE("Invalid string index %d for name in RS export vars.", pItem.name);
+    return false;
+  }
+
+  pResult.push(name);
+  return true;
+}
+
+// Procee ExportFuncNameItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ExportFuncNameItem, RSInfo::ExportFuncNameListTy>(
+    const rsinfo::ExportFuncNameItem &pItem,
+    const RSInfo &pInfo,
+    RSInfo::ExportFuncNameListTy &pResult)
+{
+  const char *name = pInfo.getStringFromPool(pItem.name);
+
+  if (name == NULL) {
+    ALOGE("Invalid string index %d for name in RS export funcs.", pItem.name);
+    return false;
+  }
+
+  pResult.push(name);
+  return true;
+}
+
+// Procee ExportForeachFuncItem in the file
+template<> inline bool
+helper_read_list_item<rsinfo::ExportForeachFuncItem, RSInfo::ExportForeachFuncListTy>(
+    const rsinfo::ExportForeachFuncItem &pItem,
+    const RSInfo &pInfo,
+    RSInfo::ExportForeachFuncListTy &pResult)
+{
+  const char *name = pInfo.getStringFromPool(pItem.name);
+
+  if (name == NULL) {
+    ALOGE("Invalid string index %d for name in RS export foreachs.", pItem.name);
+    return false;
+  }
+
+  pResult.push(std::make_pair(name, pItem.signature));
+  return true;
+}
+
+template<typename ItemType, typename ItemContainer>
+inline bool helper_read_list(const uint8_t *pData,
+                             const RSInfo &pInfo,
+                             const rsinfo::ListHeader &pHeader,
+                             ItemContainer &pResult) {
+  const ItemType *item;
+
+  // Out-of-range exception has been checked.
+  for (uint32_t i = 0; i < pHeader.count; i++) {
+    item = reinterpret_cast<const ItemType *>(pData +
+                                              pHeader.offset +
+                                              i * pHeader.itemSize);
+    if (!helper_read_list_item<ItemType, ItemContainer>(*item, pInfo, pResult)) {
+      return false;
+    }
+  }
+  return true;
+}
+
+} // end anonymous namespace
+
+RSInfo *RSInfo::ReadFromFile(InputFile &pInput, const DependencyTableTy &pDeps) {
+  android::FileMap *map = NULL;
+  RSInfo *result = NULL;
+  const uint8_t *data;
+  const rsinfo::Header *header;
+  size_t filesize;
+  const char *input_filename = pInput.getName().c_str();
+  const off_t cur_input_offset = pInput.tell();
+
+  if (pInput.hasError()) {
+    ALOGE("Invalid RS info file %s! (%s)", input_filename,
+                                           pInput.getErrorMessage().c_str());
+    goto bail;
+  }
+
+  filesize = pInput.getSize();
+  if (pInput.hasError()) {
+    ALOGE("Failed to get the size of RS info file %s! (%s)",
+          input_filename, pInput.getErrorMessage().c_str());
+    goto bail;
+  }
+
+  // Create memory map for the file.
+  map = pInput.createMap(/* pOffset */cur_input_offset,
+                         /* pLength */filesize - cur_input_offset);
+  if (map == NULL) {
+    ALOGE("Failed to map RS info file %s to the memory! (%s)",
+          input_filename, pInput.getErrorMessage().c_str());
+    goto bail;
+  }
+
+  data = reinterpret_cast<const uint8_t *>(map->getDataPtr());
+
+  // Header starts at the beginning of the file.
+  header = reinterpret_cast<const rsinfo::Header *>(data);
+
+  // Check the magic.
+  if (::memcmp(header->magic, RSINFO_MAGIC, sizeof(header->magic)) != 0) {
+    ALOGV("Wrong magic found in the RS info file %s. Treat it as a dirty "
+          "cache.", input_filename);
+    goto bail;
+  }
+
+  // Check the version.
+  if (::memcmp(header->version,
+               RSINFO_VERSION,
+               sizeof((header->version)) != 0)) {
+    ALOGV("Mismatch the version of RS info file %s: (current) %s v.s. (file) "
+          "%s. Treat it as as a dirty cache.", input_filename, RSINFO_VERSION,
+          header->version);
+    goto bail;
+  }
+
+  // Check the size.
+  if ((header->headerSize != sizeof(rsinfo::Header)) ||
+      (header->dependencyTable.itemSize != sizeof(rsinfo::DependencyTableItem)) ||
+      (header->pragmaList.itemSize != sizeof(rsinfo::PragmaItem)) ||
+      (header->objectSlotList.itemSize != sizeof(rsinfo::ObjectSlotItem)) ||
+      (header->exportVarNameList.itemSize != sizeof(rsinfo::ExportVarNameItem)) ||
+      (header->exportFuncNameList.itemSize != sizeof(rsinfo::ExportFuncNameItem)) ||
+      (header->exportForeachFuncList.itemSize != sizeof(rsinfo::ExportForeachFuncItem))) {
+    ALOGW("Corrupted RS info file %s! (unexpected size found)", input_filename);
+    goto bail;
+  }
+
+  // Check the range.
+#define LIST_DATA_RANGE(_list_header) \
+  ((_list_header).offset + (_list_header).count * (_list_header).itemSize)
+  if (((header->headerSize + header->strPoolSize) > filesize) ||
+      (LIST_DATA_RANGE(header->dependencyTable) > filesize) ||
+      (LIST_DATA_RANGE(header->pragmaList) > filesize) ||
+      (LIST_DATA_RANGE(header->objectSlotList) > filesize) ||
+      (LIST_DATA_RANGE(header->exportVarNameList) > filesize) ||
+      (LIST_DATA_RANGE(header->exportFuncNameList) > filesize) ||
+      (LIST_DATA_RANGE(header->exportForeachFuncList) > filesize)) {
+    ALOGW("Corrupted RS info file %s! (data out of the range)", input_filename);
+    goto bail;
+  }
+#undef LIST_DATA_RANGE
+
+  // File seems ok, create result RSInfo object.
+  result = new (std::nothrow) RSInfo(header->strPoolSize);
+  if (result == NULL) {
+    ALOGE("Out of memory when create RSInfo object for %s!", input_filename);
+    goto bail;
+  }
+
+  // Make advice on our access pattern.
+  map->advise(android::FileMap::SEQUENTIAL);
+
+  // Copy the header.
+  ::memcpy(&result->mHeader, header, sizeof(rsinfo::Header));
+
+  if (header->strPoolSize > 0) {
+    // Copy the string pool. The string pool is immediately after the header at
+    // the offset header->headerSize.
+    if (result->mStringPool == NULL) {
+      ALOGE("Out of memory when allocate string pool for RS info file %s!",
+            input_filename);
+      goto bail;
+    }
+    ::memcpy(result->mStringPool, data + result->mHeader.headerSize,
+             result->mHeader.strPoolSize);
+  }
+
+  // Populate all the data to the result object.
+  if (!helper_read_list<rsinfo::DependencyTableItem, DependencyTableTy>
+        (data, *result, header->dependencyTable, result->mDependencyTable)) {
+    goto bail;
+  }
+
+  // Check dependency to see whether the cache is dirty or not.
+  if (!CheckDependency(*result, pInput.getName().c_str(), pDeps)) {
+    goto bail;
+  }
+
+  if (!helper_read_list<rsinfo::PragmaItem, PragmaListTy>
+        (data, *result, header->pragmaList, result->mPragmas)) {
+    goto bail;
+  }
+
+  if (!helper_read_list<rsinfo::ObjectSlotItem, ObjectSlotListTy>
+        (data, *result, header->objectSlotList, result->mObjectSlots)) {
+    goto bail;
+  }
+
+  if (!helper_read_list<rsinfo::ExportVarNameItem, ExportVarNameListTy>
+        (data, *result, header->exportVarNameList, result->mExportVarNames)) {
+    goto bail;
+  }
+
+  if (!helper_read_list<rsinfo::ExportFuncNameItem, ExportFuncNameListTy>
+        (data, *result, header->exportFuncNameList, result->mExportFuncNames)) {
+    goto bail;
+  }
+
+  if (!helper_read_list<rsinfo::ExportForeachFuncItem, ExportForeachFuncListTy>
+        (data, *result, header->exportForeachFuncList, result->mExportForeachFuncs)) {
+    goto bail;
+  }
+
+  // Clean up.
+  map->release();
+
+  return result;
+
+bail:
+  if (map != NULL) {
+    map->release();
+  }
+
+  delete result;
+
+  return NULL;
+} // RSInfo::ReadFromFile
diff --git a/lib/RenderScript/RSInfoWriter.cpp b/lib/RenderScript/RSInfoWriter.cpp
new file mode 100644
index 0000000..3cfb8da
--- /dev/null
+++ b/lib/RenderScript/RSInfoWriter.cpp
@@ -0,0 +1,234 @@
+/*
+ * Copyright 2012, 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.
+ */
+
+//===----------------------------------------------------------------------===//
+// This file implements RSInfo::write()
+//===----------------------------------------------------------------------===//
+
+#include "bcc/RenderScript/RSInfo.h"
+
+#include "bcc/Support/Log.h"
+#include "bcc/Support/OutputFile.h"
+
+using namespace bcc;
+
+namespace {
+
+template<typename ItemType, typename ItemContainer> inline bool
+helper_adapt_list_item(ItemType &pResult, const RSInfo &pInfo,
+                       const typename ItemContainer::const_iterator &pItem);
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::DependencyTableItem, RSInfo::DependencyTableTy>(
+    rsinfo::DependencyTableItem &pResult,
+    const RSInfo &pInfo,
+    const RSInfo::DependencyTableTy::const_iterator &pItem) {
+  pResult.id = pInfo.getStringIdxInPool(pItem->first);
+  pResult.sha1 =
+      pInfo.getStringIdxInPool(reinterpret_cast<const char *>(pItem->second));
+
+  if (pResult.id == rsinfo::gInvalidStringIndex) {
+    ALOGE("RS dependency table contains invalid source id string '%s'.",
+          pItem->first);
+    return false;
+  }
+
+  if (pResult.sha1 == rsinfo::gInvalidStringIndex) {
+    ALOGE("RS dependency table contains invalid SHA-1 checksum string in '%s'.",
+          pItem->first);
+    return false;
+  }
+
+  return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::PragmaItem, RSInfo::PragmaListTy>(
+    rsinfo::PragmaItem &pResult,
+    const RSInfo &pInfo,
+    const RSInfo::PragmaListTy::const_iterator &pItem) {
+  pResult.key = pInfo.getStringIdxInPool(pItem->first);
+  pResult.value = pInfo.getStringIdxInPool(pItem->second);
+
+  if (pResult.key == rsinfo::gInvalidStringIndex) {
+    ALOGE("RS pragma list contains invalid string '%s' for key.", pItem->first);
+    return false;
+  }
+
+  if (pResult.value == rsinfo::gInvalidStringIndex) {
+    ALOGE("RS pragma list contains invalid string '%s' for value.",
+          pItem->second);
+    return false;
+  }
+
+  return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ObjectSlotItem, RSInfo::ObjectSlotListTy>(
+    rsinfo::ObjectSlotItem &pResult,
+    const RSInfo &pInfo,
+    const RSInfo::ObjectSlotListTy::const_iterator &pItem) {
+  pResult.slot = *pItem;
+  return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ExportVarNameItem, RSInfo::ExportVarNameListTy>(
+    rsinfo::ExportVarNameItem &pResult,
+    const RSInfo &pInfo,
+    const RSInfo::ExportVarNameListTy::const_iterator &pItem) {
+  pResult.name = pInfo.getStringIdxInPool(*pItem);
+
+  if (pResult.name == rsinfo::gInvalidStringIndex) {
+    ALOGE("RS export vars contains invalid string '%s' for name.", *pItem);
+    return false;
+  }
+
+  return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ExportFuncNameItem,
+                       RSInfo::ExportFuncNameListTy>(
+    rsinfo::ExportFuncNameItem &pResult,
+    const RSInfo &pInfo,
+    const RSInfo::ExportFuncNameListTy::const_iterator &pItem) {
+  pResult.name = pInfo.getStringIdxInPool(*pItem);
+
+  if (pResult.name == rsinfo::gInvalidStringIndex) {
+    ALOGE("RS export funcs contains invalid string '%s' for name.", *pItem);
+    return false;
+  }
+
+  return true;
+}
+
+template<> inline bool
+helper_adapt_list_item<rsinfo::ExportForeachFuncItem,
+                       RSInfo::ExportForeachFuncListTy>(
+    rsinfo::ExportForeachFuncItem &pResult,
+    const RSInfo &pInfo,
+    const RSInfo::ExportForeachFuncListTy::const_iterator &pItem) {
+  pResult.name = pInfo.getStringIdxInPool(pItem->first);
+  pResult.signature = pItem->second;
+
+  if (pResult.name == rsinfo::gInvalidStringIndex) {
+    ALOGE("RS export foreach contains invalid string '%s' for name.",
+          pItem->first);
+    return false;
+  }
+
+  return true;
+}
+
+template<typename ItemType, typename ItemContainer>
+inline bool helper_write_list(OutputFile &pOutput,
+                              const RSInfo &pInfo,
+                              const rsinfo::ListHeader &pHeader,
+                              ItemContainer &pList) {
+  ItemType item;
+
+  for (typename ItemContainer::const_iterator item_iter = pList.begin(),
+          item_end = pList.end(); item_iter != item_end; item_iter++) {
+    // Convert each entry in the pList to ItemType.
+    if (!helper_adapt_list_item<ItemType, ItemContainer>(item,
+                                                         pInfo,
+                                                         item_iter)) {
+      return false;
+    }
+    // And write out an item.
+    if (pOutput.write(&item, sizeof(item)) != sizeof(item)) {
+      ALOGE("Cannot write out item of %s for RSInfo file %s! (%s)",
+            rsinfo::GetItemTypeName<ItemType>(), pOutput.getName().c_str(),
+            pOutput.getErrorMessage().c_str());
+      return false;
+    }
+  }
+
+  return true;
+}
+
+} // end anonymous namespace
+
+bool RSInfo::write(OutputFile &pOutput) {
+  off_t initial_offset = pOutput.tell();
+  const char *output_filename = pOutput.getName().c_str();
+
+  if (pOutput.hasError()) {
+    ALOGE("Invalid RS info file %s for output! (%s)",
+          output_filename, pOutput.getErrorMessage().c_str());
+    return false;
+  }
+
+  // Layout.
+  if (!layout(initial_offset)) {
+    return false;
+  }
+
+  // Write header.
+  if (pOutput.write(&mHeader, sizeof(mHeader)) != sizeof(mHeader)) {
+    ALOGE("Cannot write out the header for RSInfo file %s! (%s)",
+          output_filename, pOutput.getErrorMessage().c_str());
+    return false;
+  }
+
+  // Write string pool.
+  if (static_cast<size_t>(pOutput.write(mStringPool, mHeader.strPoolSize))
+          != mHeader.strPoolSize) {
+    ALOGE("Cannot write out the string pool for RSInfo file %s! (%s)",
+          output_filename, pOutput.getErrorMessage().c_str());
+    return false;
+  }
+
+  // Write dependencyTable.
+  if (!helper_write_list<rsinfo::DependencyTableItem, DependencyTableTy>
+        (pOutput, *this, mHeader.dependencyTable, mDependencyTable)) {
+    return false;
+  }
+
+  // Write pragmaList.
+  if (!helper_write_list<rsinfo::PragmaItem, PragmaListTy>
+        (pOutput, *this, mHeader.pragmaList, mPragmas)) {
+    return false;
+  }
+
+  // Write objectSlotList.
+  if (!helper_write_list<rsinfo::ObjectSlotItem, ObjectSlotListTy>
+        (pOutput, *this, mHeader.objectSlotList, mObjectSlots)) {
+    return false;
+  }
+
+  // Write exportVarNameList.
+  if (!helper_write_list<rsinfo::ExportVarNameItem, ExportVarNameListTy>
+        (pOutput, *this, mHeader.exportVarNameList, mExportVarNames)) {
+    return false;
+  }
+
+  // Write exportFuncNameList.
+  if (!helper_write_list<rsinfo::ExportFuncNameItem, ExportFuncNameListTy>
+        (pOutput, *this, mHeader.exportFuncNameList, mExportFuncNames)) {
+    return false;
+  }
+
+  // Write exportForeachFuncList.
+  if (!helper_write_list<rsinfo::ExportForeachFuncItem, ExportForeachFuncListTy>
+        (pOutput, *this, mHeader.exportForeachFuncList, mExportForeachFuncs)) {
+    return false;
+  }
+
+  return true;
+}
diff --git a/lib/RenderScript/RSScript.cpp b/lib/RenderScript/RSScript.cpp
new file mode 100644
index 0000000..50397dc
--- /dev/null
+++ b/lib/RenderScript/RSScript.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2012, 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 "bcc/RenderScript/RSScript.h"
+
+#include "bcc/RenderScript/RSInfo.h"
+#include "bcc/Source.h"
+#include "bcc/Support/Log.h"
+
+using namespace bcc;
+
+bool RSScript::LinkRuntime(RSScript &pScript) {
+  // Using the same context with the source in pScript.
+  BCCContext &context = pScript.getSource().getContext();
+  const char* core_lib = RSInfo::LibCLCorePath;
+
+  // NEON-capable devices can use an accelerated math library for all
+  // reduced precision scripts.
+#if defined(ARCH_ARM_HAVE_NEON)
+  const RSInfo* info = pScript.getInfo();
+  if ((info != NULL) &&
+      (info->getFloatPrecisionRequirement() != RSInfo::FP_Full)) {
+    core_lib = RSInfo::LibCLCoreNEONPath;
+  }
+#endif
+
+  Source *libclcore_source = Source::CreateFromFile(context, core_lib);
+  if (libclcore_source == NULL) {
+    ALOGE("Failed to load Renderscript library '%s' to link!", core_lib);
+    return false;
+  }
+
+  if (!pScript.getSource().merge(*libclcore_source,
+                                 /* pPreserveSource */false)) {
+    ALOGE("Failed to link RenderScript library '%s'!", core_lib);
+    delete libclcore_source;
+    return false;
+  }
+
+  return true;
+}
+
+RSScript::RSScript(Source &pSource)
+  : Script(pSource), mInfo(NULL), mCompilerVersion(0),
+    mOptimizationLevel(kOptLvl3) { }
+
+bool RSScript::doReset() {
+  mInfo = NULL;
+  mCompilerVersion = 0;
+  mOptimizationLevel = kOptLvl3;
+  return true;
+}
diff --git a/lib/ScriptCRT/Android.mk b/lib/RenderScript/runtime/Android.mk
similarity index 100%
rename from lib/ScriptCRT/Android.mk
rename to lib/RenderScript/runtime/Android.mk
diff --git a/lib/ScriptCRT/arch/generic.c b/lib/RenderScript/runtime/arch/generic.c
similarity index 93%
rename from lib/ScriptCRT/arch/generic.c
rename to lib/RenderScript/runtime/arch/generic.c
index 97b7b2f..8f299e4 100644
--- a/lib/ScriptCRT/arch/generic.c
+++ b/lib/RenderScript/runtime/arch/generic.c
@@ -17,6 +17,11 @@
 
 #include "rs_types.rsh"
 
+extern short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
+extern float4 __attribute__((overloadable)) clamp(float4 amount, float4 low, float4 high);
+extern uchar4 __attribute__((overloadable)) convert_uchar4(short4);
+
+
 /*
  * CLAMP
  */
@@ -700,3 +705,39 @@
     return fmin(v1, v2);
 }
 
+
+/*
+ * YUV
+ */
+
+extern uchar4 __attribute__((overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) {
+    short Y = ((short)y) - 16;
+    short U = ((short)u) - 128;
+    short V = ((short)v) - 128;
+
+    short4 p;
+    p.r = (Y * 298 + V * 409 + 128) >> 8;
+    p.g = (Y * 298 - U * 100 - V * 208 + 128) >> 8;
+    p.b = (Y * 298 + U * 516 + 128) >> 8;
+    p.a = 255;
+    p.r = rsClamp(p.r, (short)0, (short)255);
+    p.g = rsClamp(p.g, (short)0, (short)255);
+    p.b = rsClamp(p.b, (short)0, (short)255);
+
+    return convert_uchar4(p);
+}
+
+static float4 yuv_U_values = {0.f, -0.392f * 0.003921569f, +2.02 * 0.003921569f, 0.f};
+static float4 yuv_V_values = {1.603f * 0.003921569f, -0.815f * 0.003921569f, 0.f, 0.f};
+
+extern float4 __attribute__((overloadable)) rsYuvToRGBA_float4(uchar y, uchar u, uchar v) {
+    float4 color = (float)y * 0.003921569f;
+    float4 fU = ((float)u) - 128.f;
+    float4 fV = ((float)v) - 128.f;
+
+    color += fU * yuv_U_values;
+    color += fV * yuv_V_values;
+    color = clamp(color, 0.f, 1.f);
+    return color;
+}
+
diff --git a/lib/ScriptCRT/arch/neon.ll b/lib/RenderScript/runtime/arch/neon.ll
similarity index 90%
rename from lib/ScriptCRT/arch/neon.ll
rename to lib/RenderScript/runtime/arch/neon.ll
index 5135446..5e6c614 100644
--- a/lib/ScriptCRT/arch/neon.ll
+++ b/lib/RenderScript/runtime/arch/neon.ll
@@ -19,6 +19,18 @@
 declare <2 x i32> @llvm.arm.neon.vminu.v2i32(<2 x i32>, <2 x i32>) nounwind readnone
 declare <4 x i32> @llvm.arm.neon.vminu.v4i32(<4 x i32>, <4 x i32>) nounwind readnone
 
+declare <8 x i8>  @llvm.arm.neon.vqshiftns.v8i8(<8 x i16>, <8 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vqshiftns.v4i16(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vqshiftns.v2i32(<2 x i64>, <2 x i64>) nounwind readnone
+
+declare <8 x i8>  @llvm.arm.neon.vqshiftnu.v8i8(<8 x i16>, <8 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vqshiftnu.v4i16(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vqshiftnu.v2i32(<2 x i64>, <2 x i64>) nounwind readnone
+
+declare <8 x i8>  @llvm.arm.neon.vqshiftnsu.v8i8(<8 x i16>, <8 x i16>) nounwind readnone
+declare <4 x i16> @llvm.arm.neon.vqshiftnsu.v4i16(<4 x i32>, <4 x i32>) nounwind readnone
+declare <2 x i32> @llvm.arm.neon.vqshiftnsu.v2i32(<2 x i64>, <2 x i64>) nounwind readnone
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;;;;;                HELPERS                 ;;;;;;;;;;
@@ -38,6 +50,14 @@
   ret <2 x float> %2
 }
 
+define internal <4 x i32> @smear_4i32(i32 %in) nounwind readnone alwaysinline {
+  %1 = insertelement <4 x i32> undef, i32 %in, i32 0
+  %2 = insertelement <4 x i32> %1, i32 %in, i32 1
+  %3 = insertelement <4 x i32> %2, i32 %in, i32 2
+  %4 = insertelement <4 x i32> %3, i32 %in, i32 3
+  ret <4 x i32> %4
+}
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;;;;;                 CLAMP                  ;;;;;;;;;;
@@ -671,3 +691,47 @@
   ret <4 x float> %1
 }
 
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;                  YUV                   ;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+@yuv_U = internal constant <4 x i32> <i32 0, i32 -100, i32 516, i32 0>, align 16
+@yuv_V = internal constant <4 x i32> <i32 409, i32 -208, i32 0, i32 0>, align 16
+@yuv_0 = internal constant <4 x i32> <i32 0, i32 0, i32 0, i32 0>, align 16
+@yuv_255 = internal constant <4 x i32> <i32 65535, i32 65535, i32 65535, i32 65535>, align 16
+
+
+define <4 x i8> @_Z18rsYuvToRGBA_uchar4hhh(i8 %pY, i8 %pU, i8 %pV) nounwind readnone alwaysinline {
+  %_sy = zext i8 %pY to i32
+  %_su = zext i8 %pU to i32
+  %_sv = zext i8 %pV to i32
+
+  %_sy2 = add i32 -16, %_sy
+  %_sy3 = mul i32 298, %_sy2
+  %_su2 = add i32 -128, %_su
+  %_sv2 = add i32 -128, %_sv
+  %_y = tail call <4 x i32> @smear_4i32(i32 %_sy3) nounwind readnone
+  %_u = tail call <4 x i32> @smear_4i32(i32 %_su2) nounwind readnone
+  %_v = tail call <4 x i32> @smear_4i32(i32 %_sv2) nounwind readnone
+
+  %mu = load <4 x i32>* @yuv_U, align 8
+  %mv = load <4 x i32>* @yuv_V, align 8
+  %_u2 = mul <4 x i32> %_u, %mu
+  %_v2 = mul <4 x i32> %_v, %mv
+  %_y2 = add <4 x i32> %_y, %_u2
+  %_y3 = add <4 x i32> %_y2, %_v2
+
+ ; %r1 = tail call <4 x i16> @llvm.arm.neon.vqshiftnsu.v4i16(<4 x i32> %_y3, <4 x i32> <i32 8, i32 8, i32 8, i32 8>) nounwind readnone
+;  %r2 = trunc <4 x i16> %r1 to <4 x i8>
+;  ret <4 x i8> %r2
+
+  %c0 = load <4 x i32>* @yuv_0, align 8
+  %c255 = load <4 x i32>* @yuv_255, align 8
+  %r1 = tail call <4 x i32> @llvm.arm.neon.vmaxs.v4i32(<4 x i32> %_y3, <4 x i32> %c0) nounwind readnone
+  %r2 = tail call <4 x i32> @llvm.arm.neon.vmins.v4i32(<4 x i32> %r1, <4 x i32> %c255) nounwind readnone
+  %r3 = lshr <4 x i32> %r2, <i32 8, i32 8, i32 8, i32 8>
+  %r4 = trunc <4 x i32> %r3 to <4 x i8>
+  ret <4 x i8> %r4
+}
+
diff --git a/lib/ScriptCRT/build_bc_lib.mk b/lib/RenderScript/runtime/build_bc_lib.mk
similarity index 83%
rename from lib/ScriptCRT/build_bc_lib.mk
rename to lib/RenderScript/runtime/build_bc_lib.mk
index fd5bc9c..58f5f6e 100644
--- a/lib/ScriptCRT/build_bc_lib.mk
+++ b/lib/RenderScript/runtime/build_bc_lib.mk
@@ -16,6 +16,12 @@
 
 include $(BUILD_SYSTEM)/base_rules.mk
 
+# We need to pass the +long64 flag to the underlying version of Clang, since
+# we are generating a library for use with Renderscript (64-bit long type,
+# not 32-bit).
+bc_clang_cc1_cflags := -target-feature +long64
+bc_translated_clang_cc1_cflags := $(addprefix -Xclang , $(bc_clang_cc1_cflags))
+
 bc_cflags := -MD \
              -DRS_VERSION=$(RS_VERSION) \
              -std=c99 \
@@ -24,7 +30,8 @@
              -fno-builtin \
              -emit-llvm \
              -ccc-host-triple armv7-none-linux-gnueabi \
-             -fsigned-char
+             -fsigned-char \
+	     $(bc_translated_clang_cc1_cflags)
 
 c_sources := $(filter %.c,$(LOCAL_SRC_FILES))
 ll_sources := $(filter %.ll,$(LOCAL_SRC_FILES))
diff --git a/lib/ScriptCRT/build_clcore.sh b/lib/RenderScript/runtime/build_clcore.sh
similarity index 100%
rename from lib/ScriptCRT/build_clcore.sh
rename to lib/RenderScript/runtime/build_clcore.sh
diff --git a/lib/ScriptCRT/convert.ll b/lib/RenderScript/runtime/convert.ll
similarity index 100%
rename from lib/ScriptCRT/convert.ll
rename to lib/RenderScript/runtime/convert.ll
diff --git a/lib/ScriptCRT/math.ll b/lib/RenderScript/runtime/math.ll
similarity index 100%
rename from lib/ScriptCRT/math.ll
rename to lib/RenderScript/runtime/math.ll
diff --git a/lib/ScriptCRT/matrix.ll b/lib/RenderScript/runtime/matrix.ll
similarity index 100%
rename from lib/ScriptCRT/matrix.ll
rename to lib/RenderScript/runtime/matrix.ll
diff --git a/lib/ScriptCRT/pixel_packing.ll b/lib/RenderScript/runtime/pixel_packing.ll
similarity index 100%
rename from lib/ScriptCRT/pixel_packing.ll
rename to lib/RenderScript/runtime/pixel_packing.ll
diff --git a/lib/ScriptCRT/rs_allocation.c b/lib/RenderScript/runtime/rs_allocation.c
similarity index 100%
rename from lib/ScriptCRT/rs_allocation.c
rename to lib/RenderScript/runtime/rs_allocation.c
diff --git a/lib/ScriptCRT/rs_cl.c b/lib/RenderScript/runtime/rs_cl.c
similarity index 97%
rename from lib/ScriptCRT/rs_cl.c
rename to lib/RenderScript/runtime/rs_cl.c
index b707f44..677a89a 100644
--- a/lib/ScriptCRT/rs_cl.c
+++ b/lib/RenderScript/runtime/rs_cl.c
@@ -704,7 +704,15 @@
 extern float __attribute__((overloadable)) degrees(float radians) {
     return radians * (180.f / M_PI);
 }
-FN_FUNC_FN(degrees)
+extern float2 __attribute__((overloadable)) degrees(float2 radians) {
+    return radians * (180.f / M_PI);
+}
+extern float3 __attribute__((overloadable)) degrees(float3 radians) {
+    return radians * (180.f / M_PI);
+}
+extern float4 __attribute__((overloadable)) degrees(float4 radians) {
+    return radians * (180.f / M_PI);
+}
 
 extern float __attribute__((overloadable)) mix(float start, float stop, float amount) {
     return start + (stop - start) * amount;
@@ -731,7 +739,15 @@
 extern float __attribute__((overloadable)) radians(float degrees) {
     return degrees * (M_PI / 180.f);
 }
-FN_FUNC_FN(radians)
+extern float2 __attribute__((overloadable)) radians(float2 degrees) {
+    return degrees * (M_PI / 180.f);
+}
+extern float3 __attribute__((overloadable)) radians(float3 degrees) {
+    return degrees * (M_PI / 180.f);
+}
+extern float4 __attribute__((overloadable)) radians(float4 degrees) {
+    return degrees * (M_PI / 180.f);
+}
 
 extern float __attribute__((overloadable)) step(float edge, float v) {
     return (v < edge) ? 0.f : 1.f;
diff --git a/lib/ScriptCRT/rs_core.c b/lib/RenderScript/runtime/rs_core.c
similarity index 90%
rename from lib/ScriptCRT/rs_core.c
rename to lib/RenderScript/runtime/rs_core.c
index f655c05..aaf1336 100644
--- a/lib/ScriptCRT/rs_core.c
+++ b/lib/RenderScript/runtime/rs_core.c
@@ -2,29 +2,11 @@
 #include "rs_graphics.rsh"
 #include "rs_structs.h"
 
-/* Declaration of 4 basic functions in libRS */
-extern void __attribute__((overloadable))
-    rsDebug(const char *, float, float);
-extern void __attribute__((overloadable))
-    rsDebug(const char *, float, float, float);
-extern void __attribute__((overloadable))
-    rsDebug(const char *, float, float, float, float);
+/* Function declarations from libRS */
 extern float4 __attribute__((overloadable)) convert_float4(uchar4 c);
 
 /* Implementation of Core Runtime */
 
-extern void __attribute__((overloadable)) rsDebug(const char *s, float2 v) {
-    rsDebug(s, v.x, v.y);
-}
-
-extern void __attribute__((overloadable)) rsDebug(const char *s, float3 v) {
-    rsDebug(s, v.x, v.y, v.z);
-}
-
-extern void __attribute__((overloadable)) rsDebug(const char *s, float4 v) {
-    rsDebug(s, v.x, v.y, v.z, v.w);
-}
-
 /*
 extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b)
 {
diff --git a/lib/ScriptCRT/rs_element.c b/lib/RenderScript/runtime/rs_element.c
similarity index 100%
rename from lib/ScriptCRT/rs_element.c
rename to lib/RenderScript/runtime/rs_element.c
diff --git a/lib/ScriptCRT/rs_mesh.c b/lib/RenderScript/runtime/rs_mesh.c
similarity index 100%
rename from lib/ScriptCRT/rs_mesh.c
rename to lib/RenderScript/runtime/rs_mesh.c
diff --git a/lib/ScriptCRT/rs_program.c b/lib/RenderScript/runtime/rs_program.c
similarity index 100%
rename from lib/ScriptCRT/rs_program.c
rename to lib/RenderScript/runtime/rs_program.c
diff --git a/lib/ScriptCRT/rs_sample.c b/lib/RenderScript/runtime/rs_sample.c
similarity index 100%
rename from lib/ScriptCRT/rs_sample.c
rename to lib/RenderScript/runtime/rs_sample.c
diff --git a/lib/ScriptCRT/rs_sampler.c b/lib/RenderScript/runtime/rs_sampler.c
similarity index 100%
rename from lib/ScriptCRT/rs_sampler.c
rename to lib/RenderScript/runtime/rs_sampler.c
diff --git a/lib/ScriptCRT/rs_structs.h b/lib/RenderScript/runtime/rs_structs.h
similarity index 100%
rename from lib/ScriptCRT/rs_structs.h
rename to lib/RenderScript/runtime/rs_structs.h
diff --git a/lib/Support/Android.mk b/lib/Support/Android.mk
new file mode 100644
index 0000000..ca4aa61
--- /dev/null
+++ b/lib/Support/Android.mk
@@ -0,0 +1,74 @@
+#
+# Copyright (C) 2012 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.
+#
+#
+
+LOCAL_PATH := $(call my-dir)
+
+#=====================================================================
+# Common: libbccSupport
+#=====================================================================
+
+libbcc_support_SRC_FILES := \
+  CompilerConfig.cpp \
+  Disassembler.cpp \
+  FileBase.cpp \
+  Initialization.cpp \
+  InputFile.cpp \
+  LinkerConfig.cpp \
+  OutputFile.cpp \
+  Sha1Util.cpp \
+  TargetCompilerConfigs.cpp \
+  TargetLinkerConfigs.cpp
+
+#=====================================================================
+# Device Static Library: libbccSupport
+#=====================================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libbccSupport
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+
+# Bionic already includes SHA-1 routines.
+LOCAL_SRC_FILES := $(libbcc_support_SRC_FILES)
+
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_DEVICE_BUILD_MK)
+#include $(LLVM_DEVICE_BUILD_MK)
+include $(BUILD_STATIC_LIBRARY)
+
+
+#=====================================================================
+# Host Static Library: libbccSupport
+#=====================================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libbccSupport
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+
+LOCAL_SRC_FILES := \
+  sha1.c \
+  $(libbcc_support_SRC_FILES)
+
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_HOST_BUILD_MK)
+#include $(LLVM_HOST_BUILD_MK)
+include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/lib/Support/CompilerConfig.cpp b/lib/Support/CompilerConfig.cpp
new file mode 100644
index 0000000..8465fec
--- /dev/null
+++ b/lib/Support/CompilerConfig.cpp
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2012, 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 "bcc/Support/CompilerConfig.h"
+
+#include <llvm/CodeGen/SchedulerRegistry.h>
+#include <llvm/MC/SubtargetFeature.h>
+#include <llvm/Support/TargetRegistry.h>
+
+#include "bcc/Support/Log.h"
+#include "bcc/Support/TargetCompilerConfigs.h"
+
+using namespace bcc;
+
+CompilerConfig::CompilerConfig(const std::string &pTriple)
+  : mTriple(pTriple), mTarget(NULL) {
+  //===--------------------------------------------------------------------===//
+  // Default setting of register sheduler
+  //===--------------------------------------------------------------------===//
+  llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
+
+  //===--------------------------------------------------------------------===//
+  // Default setting of target options
+  //===--------------------------------------------------------------------===//
+  // Use hardfloat ABI by default.
+  //
+  // TODO(all): Need to detect the CPU capability and decide whether to use
+  // softfp. To use softfp, change the following 2 lines to
+  //
+  // options.FloatABIType = llvm::FloatABI::Soft;
+  // options.UseSoftFloat = true;
+  mTargetOpts.FloatABIType = llvm::FloatABI::Soft;
+  mTargetOpts.UseSoftFloat = false;
+
+  // Enable frame pointer elimination optimization by default.
+  mTargetOpts.NoFramePointerElim = false;
+
+  //===--------------------------------------------------------------------===//
+  // Default setting for code model
+  //===--------------------------------------------------------------------===//
+  mCodeModel = llvm::CodeModel::Small;
+
+  //===--------------------------------------------------------------------===//
+  // Default setting for relocation model
+  //===--------------------------------------------------------------------===//
+  mRelocModel = llvm::Reloc::Default;
+
+  //===--------------------------------------------------------------------===//
+  // Default setting for optimization level (-O2)
+  //===--------------------------------------------------------------------===//
+  mOptLevel = llvm::CodeGenOpt::Default;
+
+  //===--------------------------------------------------------------------===//
+  // Default setting for architecture type
+  //===--------------------------------------------------------------------===//
+  mArchType = llvm::Triple::UnknownArch;
+
+  initializeTarget();
+  initializeArch();
+
+  return;
+}
+
+bool CompilerConfig::initializeTarget() {
+  std::string error;
+  mTarget = llvm::TargetRegistry::lookupTarget(mTriple, error);
+  if (mTarget != NULL) {
+    return true;
+  } else {
+    ALOGE("Cannot initialize llvm::Target for given triple '%s'! (%s)",
+          mTriple.c_str(), error.c_str());
+    return false;
+  }
+}
+
+void CompilerConfig::initializeArch() {
+  if (mTarget != NULL) {
+    mArchType = llvm::Triple::getArchTypeForLLVMName(mTarget->getName());
+  } else {
+    mArchType = llvm::Triple::UnknownArch;
+  }
+  return;
+}
+
+void CompilerConfig::setFeatureString(const std::vector<std::string> &pAttrs) {
+  llvm::SubtargetFeatures f;
+
+  for (std::vector<std::string>::const_iterator attr_iter = pAttrs.begin(),
+           attr_end = pAttrs.end();
+       attr_iter != attr_end; attr_iter++) {
+    f.AddFeature(*attr_iter);
+  }
+
+  mFeatureString = f.getString();
+  return;
+}
diff --git a/lib/Support/Disassembler.cpp b/lib/Support/Disassembler.cpp
new file mode 100644
index 0000000..5a1d017
--- /dev/null
+++ b/lib/Support/Disassembler.cpp
@@ -0,0 +1,228 @@
+/*
+ * Copyright 2011-2012, 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 "bcc/Support/Disassembler.h"
+
+#include "bcc/Config/Config.h"
+#if USE_DISASSEMBLER
+
+#include <string>
+
+#include <llvm/LLVMContext.h>
+
+#include <llvm/MC/MCAsmInfo.h>
+#include <llvm/MC/MCDisassembler.h>
+#include <llvm/MC/MCInst.h>
+#include <llvm/MC/MCInstPrinter.h>
+#include <llvm/MC/MCInstrInfo.h>
+#include <llvm/MC/MCRegisterInfo.h>
+#include <llvm/MC/MCSubtargetInfo.h>
+
+#include <llvm/Support/MemoryObject.h>
+#include <llvm/Support/TargetRegistry.h>
+#include <llvm/Support/raw_ostream.h>
+
+#include "bcc/Support/OutputFile.h"
+#include "bcc/Support/Log.h"
+
+namespace {
+
+class BufferMemoryObject : public llvm::MemoryObject {
+private:
+  const uint8_t *mBytes;
+  uint64_t mLength;
+
+public:
+  BufferMemoryObject(const uint8_t *pBytes, uint64_t pLength)
+    : mBytes(pBytes), mLength(pLength) {
+  }
+
+  virtual uint64_t getBase() const { return 0; }
+  virtual uint64_t getExtent() const { return mLength; }
+
+  virtual int readByte(uint64_t pAddr, uint8_t *pByte) const {
+    if (pAddr > getExtent())
+      return -1;
+    *pByte = mBytes[pAddr];
+    return 0;
+  }
+};
+
+} // namespace anonymous
+
+namespace bcc {
+
+DisassembleResult Disassemble(llvm::raw_ostream &pOutput, const char *pTriple,
+                              const char *pFuncName, const uint8_t *pFunc,
+                              size_t pFuncSize) {
+  DisassembleResult result = kDisassembleSuccess;
+  uint64_t i = 0;
+
+  const llvm::MCSubtargetInfo *subtarget_info = NULL;
+  const llvm::MCDisassembler *disassembler = NULL;
+  const llvm::MCInstrInfo *mc_inst_info = NULL;
+  const llvm::MCRegisterInfo *mc_reg_info = NULL;
+  const llvm::MCAsmInfo *asm_info = NULL;
+  llvm::MCInstPrinter *inst_printer = NULL;
+
+  BufferMemoryObject *input_function = NULL;
+
+  std::string error;
+  const llvm::Target* target =
+      llvm::TargetRegistry::lookupTarget(pTriple, error);
+
+  if (target == NULL) {
+    ALOGE("Invalid target triple for disassembler: %s (%s)!",
+          pTriple, error.c_str());
+    return kDisassembleUnknownTarget;
+  }
+
+  subtarget_info =
+      target->createMCSubtargetInfo(pTriple, /* CPU */"", /* Features */"");;
+
+  if (subtarget_info == NULL) {
+    result = kDisassembleFailedSetup;
+    goto bail;
+  }
+
+  disassembler = target->createMCDisassembler(*subtarget_info);
+
+  mc_inst_info = target->createMCInstrInfo();
+
+  mc_reg_info = target->createMCRegInfo(pTriple);
+
+  asm_info = target->createMCAsmInfo(pTriple);
+
+  if ((disassembler == NULL) || (mc_inst_info == NULL) ||
+      (mc_reg_info == NULL) || (asm_info == NULL)) {
+    result = kDisassembleFailedSetup;
+    goto bail;
+  }
+
+  inst_printer = target->createMCInstPrinter(asm_info->getAssemblerDialect(),
+                                             *asm_info, *mc_inst_info,
+                                             *mc_reg_info, *subtarget_info);
+
+  if (inst_printer == NULL) {
+    result = kDisassembleFailedSetup;
+    goto bail;
+  }
+
+  input_function = new (std::nothrow) BufferMemoryObject(pFunc, pFuncSize);
+
+  if (input_function == NULL) {
+    result = kDisassembleOutOfMemory;
+    goto bail;
+  }
+
+  // Disassemble the given function
+  pOutput << "Disassembled code: " << pFuncName << "\n";
+
+  while (i < pFuncSize) {
+    llvm::MCInst inst;
+    uint64_t inst_size;
+
+    llvm::MCDisassembler::DecodeStatus decode_result =
+        disassembler->getInstruction(inst, inst_size, *input_function, i,
+                                     llvm::nulls(), llvm::nulls());
+
+    switch (decode_result) {
+      case llvm::MCDisassembler::Fail: {
+        ALOGW("Invalid instruction encoding encountered at %llu of function %s "
+              "under %s.", i, pFuncName, pTriple);
+        i++;
+        break;
+      }
+      case llvm::MCDisassembler::SoftFail: {
+        ALOGW("Potentially undefined instruction encoding encountered at %llu "
+              "of function %s under %s.", i, pFuncName, pTriple);
+        // fall-through
+      }
+      case llvm::MCDisassembler::Success : {
+        const uint8_t *inst_addr = pFunc + i;
+
+        pOutput.indent(4);
+        pOutput << "0x";
+        pOutput.write_hex(reinterpret_cast<uintptr_t>(inst_addr));
+        pOutput << ": 0x";
+        pOutput.write_hex(*reinterpret_cast<const uint32_t *>(inst_addr));
+        inst_printer->printInst(&inst, pOutput, /* Annot */"");
+        pOutput << "\n";
+
+        i += inst_size;
+        break;
+      }
+    }
+  }
+
+  pOutput << "\n";
+
+bail:
+  // Clean up
+  delete input_function;
+  delete inst_printer;
+  delete asm_info;
+  delete mc_reg_info;
+  delete mc_inst_info;
+  delete disassembler;
+  delete subtarget_info;
+
+  return result;
+}
+
+DisassembleResult Disassemble(OutputFile &pOutput, const char *pTriple,
+                              const char *pFuncName, const uint8_t *pFunc,
+                              size_t FuncSize) {
+  // Check the state of the specified output file.
+  if (pOutput.hasError()) {
+    return kDisassembleInvalidOutput;
+  }
+
+  // Open the output file decorated in llvm::raw_ostream.
+  llvm::raw_ostream *output = pOutput.dup();
+  if (output == NULL) {
+    return kDisassembleFailedPrepareOutput;
+  }
+
+  // Delegate the request.
+  DisassembleResult result =
+      Disassemble(*output, pTriple, pFuncName, pFunc, FuncSize);
+
+  // Close the output before return.
+  delete output;
+
+  return result;
+}
+
+} // namespace bcc
+
+#else
+
+bcc::DisassembleResult Disassemble(llvm::raw_ostream &pOutput,
+                                   const char *pTriple, const char *pFuncName,
+                                   const uint8_t *pFunc, size_t pFuncSize) {
+  return bcc::kDisassemblerNotAvailable;
+}
+
+bcc::DisassembleResult bcc::Disassemble(OutputFile &pOutput,
+                                        const char *pTriple,
+                                        const char *pFuncName,
+                                        const uint8_t *pFunc,
+                                        size_t pFuncSize) {
+  return bcc::kDisassemblerNotAvailable;
+}
+
+#endif // USE_DISASSEMBLER
diff --git a/lib/Support/FileBase.cpp b/lib/Support/FileBase.cpp
new file mode 100644
index 0000000..579d78f
--- /dev/null
+++ b/lib/Support/FileBase.cpp
@@ -0,0 +1,286 @@
+/*
+ * Copyright 2012, 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 "bcc/Support/FileBase.h"
+
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <cerrno>
+#include <new>
+
+#include <utils/FileMap.h>
+
+using namespace bcc;
+
+FileBase::FileBase(const std::string &pFilename,
+                   unsigned pOpenFlags,
+                   unsigned pFlags)
+  : mFD(-1),
+    mError(),
+    mName(pFilename), mOpenFlags(pOpenFlags),
+    mShouldUnlock(false) {
+  // Process pFlags
+#ifdef O_BINARY
+  if (pFlags & kBinary) {
+    mOpenFlags |= O_BINARY;
+  }
+#endif
+  if (pFlags & kTruncate) {
+    mOpenFlags |= O_TRUNC;
+  }
+
+  if (pFlags & kAppend) {
+    mOpenFlags |= O_APPEND;
+  }
+
+  // Open the file.
+  open();
+
+  return;
+}
+
+FileBase::~FileBase() {
+  close();
+}
+
+bool FileBase::open() {
+  do {
+    // FIXME: Hard-coded permissions (0644) for newly created file should be
+    //        removed and provide a way to let the user configure the value.
+    mFD = ::open(mName.c_str(), mOpenFlags, 0644);
+    if (mFD > 0) {
+      return true;
+    }
+
+    // Some errors occurred ...
+    if (errno != EINTR) {
+      detectError();
+      return false;
+    }
+  } while (true);
+  // unreachable
+}
+
+
+bool FileBase::checkFileIntegrity() {
+  // Check the file integrity by examining whether the inode referring to the mFD
+  // and to the file mName are the same.
+  struct stat fd_stat, file_stat;
+
+  // Get the file status of file descriptor mFD.
+  do {
+    if (::fstat(mFD, &fd_stat) == 0) {
+      break;
+    } else if (errno != EINTR) {
+      detectError();
+      return false;
+    }
+  } while (true);
+
+  // Get the file status of file mName.
+  do {
+    if (::stat(mName.c_str(), &file_stat) == 0) {
+      break;
+    } else if (errno != EINTR) {
+      detectError();
+      return false;
+    }
+  } while (true);
+
+  return ((fd_stat.st_dev == file_stat.st_dev) &&
+          (fd_stat.st_ino == file_stat.st_ino));
+}
+
+void FileBase::detectError() {
+  // Read error from errno.
+  mError.assign(errno, llvm::posix_category());
+}
+
+bool FileBase::lock(enum LockModeEnum pMode,
+                    bool pNonblocking,
+                    unsigned pMaxRetry,
+                    useconds_t pRetryInterval) {
+  int lock_operation;
+  unsigned retry = 0;
+
+  // Check the state.
+  if ((mFD < 0) || hasError()) {
+    return false;
+  }
+
+  // Return immediately if it's already locked.
+  if (mShouldUnlock) {
+    return true;
+  }
+
+  // Determine the lock operation (2nd argument) to the flock().
+  if (pMode == kReadLock) {
+    lock_operation = LOCK_SH;
+  } else if (pMode == kWriteLock) {
+    lock_operation = LOCK_EX;
+  } else {
+    mError.assign(llvm::errc::invalid_argument, llvm::posix_category());
+    return false;
+  }
+
+  if (pNonblocking) {
+    lock_operation |= LOCK_NB;
+  }
+
+  do {
+    if (::flock(mFD, lock_operation) == 0) {
+      mShouldUnlock = true;
+      // Here we got a lock but we need to check whether the mFD still
+      // "represents" the filename (mName) we opened in the contructor. This
+      // check may failed when another process deleted the original file mFD
+      // mapped when we were trying to obtain the lock on the file.
+      if (!checkFileIntegrity()) {
+        if (hasError() || !reopen()) {
+          // Error occurred when check the file integrity or re-open the file.
+          return false;
+        } else {
+          // Wait a while before the next try.
+          ::usleep(pRetryInterval);
+          retry++;
+          continue;
+        }
+      }
+
+      return true;
+    }
+
+    // flock() was not performed successfully. Check the errno to see whether
+    // it's retry-able.
+    if (errno == EINTR) {
+      // flock() was interrupted by delivery of a signal. Restart without
+      // decrement the retry counter.
+      continue;
+    } else if (errno == EWOULDBLOCK) {
+      // The file descriptor was locked by others, wait for a while before next
+      // retry.
+      retry++;
+      ::usleep(pRetryInterval);
+    } else {
+      // There's a fatal error occurs when perform flock(). Return immediately
+      // without further retry.
+      detectError();
+      return false;
+    }
+  } while (retry <= pMaxRetry);
+
+  return false;
+}
+
+void FileBase::unlock() {
+  if (mFD < 0) {
+    return;
+  }
+
+  do {
+    if (::flock(mFD, LOCK_UN) == 0) {
+      mShouldUnlock = false;
+      return;
+    }
+  } while (errno == EINTR);
+
+  detectError();
+  return;
+}
+
+android::FileMap *FileBase::createMap(off_t pOffset, size_t pLength,
+                                      bool pIsReadOnly) {
+  if (mFD < 0 || hasError()) {
+    return NULL;
+  }
+
+  android::FileMap *map = new (std::nothrow) android::FileMap();
+  if (map == NULL) {
+    mError.assign(llvm::errc::not_enough_memory, llvm::system_category());
+    return NULL;
+  }
+
+  if (!map->create(NULL, mFD, pOffset, pLength, pIsReadOnly)) {
+    detectError();
+    map->release();
+    return NULL;
+  }
+
+  return map;
+}
+
+size_t FileBase::getSize() {
+  if (mFD < 0 || hasError()) {
+    return static_cast<size_t>(-1);
+  }
+
+  struct stat file_stat;
+  do {
+    if (::fstat(mFD, &file_stat) == 0) {
+      break;
+    } else if (errno != EINTR) {
+      detectError();
+      return static_cast<size_t>(-1);
+    }
+  } while (true);
+
+  return file_stat.st_size;
+}
+
+off_t FileBase::seek(off_t pOffset) {
+  if ((mFD < 0) || hasError()) {
+    return static_cast<off_t>(-1);
+  }
+
+  do {
+    off_t result = ::lseek(mFD, pOffset, SEEK_SET);
+    if (result == pOffset) {
+      return result;
+    }
+  } while (errno == EINTR);
+
+  detectError();
+  return static_cast<off_t>(-1);
+}
+
+off_t FileBase::tell() {
+  if ((mFD < 0) || hasError()) {
+    return static_cast<off_t>(-1);
+  }
+
+  do {
+    off_t result = ::lseek(mFD, 0, SEEK_CUR);
+    if (result != static_cast<off_t>(-1)) {
+      return result;
+    }
+  } while (errno == EINTR);
+
+  detectError();
+  return static_cast<off_t>(-1);
+}
+
+void FileBase::close() {
+  if (mShouldUnlock) {
+    unlock();
+    mShouldUnlock = false;
+  }
+  if (mFD > 0) {
+    ::close(mFD);
+    mFD = -1;
+  }
+  return;
+}
diff --git a/lib/Support/Initialization.cpp b/lib/Support/Initialization.cpp
new file mode 100644
index 0000000..6951a05
--- /dev/null
+++ b/lib/Support/Initialization.cpp
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2012, 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 "bcc/Support/Initialization.h"
+
+#include <cstdlib>
+
+#include <llvm/Support/ErrorHandling.h>
+#include <llvm/Support/TargetSelect.h>
+
+#include <mcld/Support/TargetSelect.h>
+#include <mcld/Support/TargetRegistry.h>
+
+#include "bcc/Config/Config.h"
+#include "bcc/Support/Log.h"
+
+namespace {
+
+void llvm_error_handler(void *pUserData, const std::string &pMessage) {
+  ALOGE("%s", pMessage.c_str());
+  ::exit(1);
+}
+
+} // end anonymous namespace
+
+void bcc::init::Initialize() {
+  static bool is_initialized = false;
+
+  if (is_initialized) {
+    return;
+  }
+
+  // Setup error handler for LLVM.
+  llvm::remove_fatal_error_handler();
+  llvm::install_fatal_error_handler(llvm_error_handler, NULL);
+
+#if defined(PROVIDE_ARM_CODEGEN)
+  LLVMInitializeARMAsmPrinter();
+# if USE_DISASSEMBLER
+  LLVMInitializeARMDisassembler();
+# endif
+  LLVMInitializeARMTargetMC();
+  LLVMInitializeARMTargetInfo();
+  LLVMInitializeARMTarget();
+  LLVMInitializeARMLDTargetInfo();
+  LLVMInitializeARMLDTarget();
+  LLVMInitializeARMLDBackend();
+  LLVMInitializeARMDiagnosticLineInfo();
+#endif
+
+#if defined(PROVIDE_MIPS_CODEGEN)
+  LLVMInitializeMipsAsmPrinter();
+# if USE_DISASSEMBLER
+  LLVMInitializeMipsDisassembler();
+# endif
+  LLVMInitializeMipsTargetMC();
+  LLVMInitializeMipsTargetInfo();
+  LLVMInitializeMipsTarget();
+  LLVMInitializeMipsLDTargetInfo();
+  LLVMInitializeMipsLDTarget();
+  LLVMInitializeMipsLDBackend();
+  LLVMInitializeMipsDiagnosticLineInfo();
+#endif
+
+#if defined(PROVIDE_X86_CODEGEN)
+  LLVMInitializeX86AsmPrinter();
+# if USE_DISASSEMBLER
+  LLVMInitializeX86Disassembler();
+# endif
+  LLVMInitializeX86TargetMC();
+  LLVMInitializeX86TargetInfo();
+  LLVMInitializeX86Target();
+  LLVMInitializeX86LDTargetInfo();
+  LLVMInitializeX86LDTarget();
+  LLVMInitializeX86LDBackend();
+  LLVMInitializeX86DiagnosticLineInfo();
+#endif
+
+  is_initialized = true;
+
+  return;
+}
diff --git a/lib/Support/InputFile.cpp b/lib/Support/InputFile.cpp
new file mode 100644
index 0000000..054edc7
--- /dev/null
+++ b/lib/Support/InputFile.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2012, 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 "bcc/Support/InputFile.h"
+
+#include "bcc/Support/Log.h"
+
+using namespace bcc;
+
+InputFile::InputFile(const std::string &pFilename, unsigned pFlags)
+  : super(pFilename, pFlags) { }
+
+ssize_t InputFile::read(void *pBuf, size_t count) {
+  if ((mFD < 0) || hasError()) {
+    return -1;
+  }
+
+  if ((count <= 0) || (pBuf == NULL)) {
+    // Keep safe and issue a warning.
+    ALOGW("InputFile::read: count = %zu, buffer = %p", count, pBuf);
+    return 0;
+  }
+
+  while (count > 0) {
+    ssize_t read_size = ::read(mFD, pBuf, count);
+
+    if (read_size >= 0) {
+      return read_size;
+    } else if ((errno == EAGAIN) || (errno == EINTR)) {
+      // If the errno is EAGAIN or EINTR, then we try to read again.
+      //
+      // Fall-through
+    } else {
+      detectError();
+      return -1;
+    }
+  }
+  // unreachable
+  return 0;
+}
diff --git a/lib/Support/LinkerConfig.cpp b/lib/Support/LinkerConfig.cpp
new file mode 100644
index 0000000..c0285f7
--- /dev/null
+++ b/lib/Support/LinkerConfig.cpp
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2012, 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 "bcc/Support/LinkerConfig.h"
+#include "bcc/Support/Log.h"
+
+#include <llvm/Support/Signals.h>
+
+#include <mcld/MC/MCLDInfo.h>
+#include <mcld/MC/MCLDFile.h>
+#include <mcld/MC/MCLDDirectory.h>
+#include <mcld/LD/TextDiagnosticPrinter.h>
+#include <mcld/Support/Path.h>
+#include <mcld/Support/MsgHandling.h>
+#include <mcld/Support/raw_ostream.h>
+
+using namespace bcc;
+
+LinkerConfig::LinkerConfig(const std::string &pTriple)
+  : mTriple(pTriple), mShared(false), mSOName(), mTarget(NULL), mLDInfo(NULL),
+    mDiagLineInfo(NULL), mDiagPrinter(NULL) {
+
+  initializeTarget();
+  initializeLDInfo();
+  initializeDiagnostic();
+}
+
+LinkerConfig::~LinkerConfig() {
+  delete mLDInfo;
+
+  if (mDiagPrinter->getNumErrors() != 0) {
+    // If here, the program failed ungracefully. Run the interrupt handlers to
+    // ensure any other cleanups (e.g., files that registered by
+    // RemoveFileOnSignal(...)) getting done before exit.
+    llvm::sys::RunInterruptHandlers();
+  }
+  mDiagPrinter->finish();
+
+  delete mDiagLineInfo;
+  delete mDiagPrinter;
+}
+
+bool LinkerConfig::initializeTarget() {
+  std::string error;
+  mTarget = mcld::TargetRegistry::lookupTarget(mTriple, error);
+  if (NULL != mTarget) {
+    return true;
+  } else {
+    ALOGE("Cannot initialize mcld::Target for given triple '%s'! (%s)\n",
+          mTriple.c_str(), error.c_str());
+    return false;
+  }
+}
+
+bool LinkerConfig::initializeLDInfo() {
+  if (NULL != mLDInfo) {
+    ALOGE("Cannot initialize mcld::MCLDInfo for given triple '%s!\n",
+          mTriple.c_str());
+    return false;
+  }
+
+  mLDInfo = new mcld::MCLDInfo(getTriple(), 1, 32);
+  return true;
+}
+
+bool LinkerConfig::initializeDiagnostic() {
+  // Set up MsgHandler.
+  mDiagLineInfo = mTarget->createDiagnosticLineInfo(*mTarget, mTriple);
+
+  mDiagPrinter = new mcld::TextDiagnosticPrinter(mcld::errs(), *mLDInfo);
+
+  mcld::InitializeDiagnosticEngine(*mLDInfo, mDiagLineInfo, mDiagPrinter);
+
+  return true;
+}
+
+void LinkerConfig::setShared(bool pEnable) {
+  mShared = pEnable;
+  return;
+}
+
+void LinkerConfig::setBsymbolic(bool pEnable) {
+  mLDInfo->options().setBsymbolic(pEnable);
+  return;
+}
+
+void LinkerConfig::setSOName(const std::string &pSOName) {
+  mLDInfo->output().setSOName(pSOName);
+  return;
+}
+
+void LinkerConfig::setDyld(const std::string &pDyld) {
+  mLDInfo->options().setDyld(pDyld);
+  return;
+}
+
+void LinkerConfig::setSysRoot(const std::string &pSysRoot) {
+  mLDInfo->options().setSysroot(mcld::sys::fs::Path(pSysRoot));
+  return;
+}
+
+void LinkerConfig::addWrap(const std::string &pWrapSymbol) {
+  bool exist = false;
+
+  // Add wname -> __wrap_wname.
+  mcld::StringEntry<llvm::StringRef>* to_wrap =
+               mLDInfo->scripts().renameMap().insert(pWrapSymbol, exist);
+
+  std::string to_wrap_str = "__wrap_" + pWrapSymbol;
+  to_wrap->setValue(to_wrap_str);
+
+  if (exist) {
+    mcld::warning(mcld::diag::rewrap) << pWrapSymbol << to_wrap_str;
+  }
+
+  // Add __real_wname -> wname.
+  std::string from_real_str = "__real_" + pWrapSymbol;
+  mcld::StringEntry<llvm::StringRef>* from_real =
+             mLDInfo->scripts().renameMap().insert(from_real_str, exist);
+  from_real->setValue(pWrapSymbol);
+
+  if (exist) {
+    mcld::warning(mcld::diag::rewrap) << pWrapSymbol << from_real_str;
+  }
+
+  return;
+}
+
+void LinkerConfig::addPortable(const std::string &pPortableSymbol) {
+  bool exist = false;
+
+  // Add pname -> pname_portable.
+  mcld::StringEntry<llvm::StringRef>* to_port =
+                mLDInfo->scripts().renameMap().insert(pPortableSymbol, exist);
+
+  std::string to_port_str = pPortableSymbol + "_portable";
+  to_port->setValue(to_port_str);
+
+  if (exist) {
+    mcld::warning(mcld::diag::rewrap) << pPortableSymbol << to_port_str;
+}
+
+  // Add __real_pname -> pname.
+  std::string from_real_str = "__real_" + pPortableSymbol;
+  mcld::StringEntry<llvm::StringRef>* from_real =
+           mLDInfo->scripts().renameMap().insert(from_real_str, exist);
+
+  from_real->setValue(pPortableSymbol);
+
+  if (exist) {
+    mcld::warning(mcld::diag::rewrap) << pPortableSymbol << from_real_str;
+  }
+
+  return;
+}
+
+void LinkerConfig::addSearchDir(const std::string &pDirPath) {
+  // SearchDirs will remove the created MCLDDirectory.
+  mcld::MCLDDirectory* sd = new mcld::MCLDDirectory(pDirPath);
+
+  if (sd->isInSysroot()) {
+    sd->setSysroot(mLDInfo->options().sysroot());
+  }
+
+  if (exists(sd->path()) && is_directory(sd->path())) {
+    mLDInfo->options().directories().add(*sd);
+  } else {
+    mcld::warning(mcld::diag::warn_cannot_open_search_dir) << sd->name();
+  }
+
+  return;
+}
diff --git a/lib/Support/OutputFile.cpp b/lib/Support/OutputFile.cpp
new file mode 100644
index 0000000..6d969d0
--- /dev/null
+++ b/lib/Support/OutputFile.cpp
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2012, 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 "bcc/Support/OutputFile.h"
+
+#include <cstdlib>
+
+#include <llvm/Support/raw_ostream.h>
+
+#include "bcc/Support/Log.h"
+
+using namespace bcc;
+
+OutputFile *OutputFile::CreateTemporary(const std::string &pFileTemplate,
+                                        unsigned pFlags) {
+  char *tmp_filename = NULL;
+  int tmp_fd;
+  OutputFile *result = NULL;
+
+  // Allocate memory to hold the generated unique temporary filename.
+  tmp_filename =
+      new (std::nothrow) char [ pFileTemplate.length() + /* .XXXXXX */7 + 1 ];
+  if (tmp_filename == NULL) {
+    ALOGE("Out of memory when allocates memory for filename %s in "
+          "OutputFile::CreateTemporary()!", pFileTemplate.c_str());
+    return NULL;
+  }
+
+  // Construct filename template for mkstemp().
+  if (pFileTemplate.length() > 0)
+    ::memcpy(tmp_filename, pFileTemplate.c_str(), pFileTemplate.length());
+  ::strncpy(tmp_filename + pFileTemplate.length(), ".XXXXXX", 7);
+
+  // POSIX mkstemp() never returns EINTR.
+  tmp_fd = ::mkstemp(tmp_filename);
+  if (tmp_fd < 0) {
+    llvm::error_code err(errno, llvm::posix_category());
+    ALOGE("Failed to create temporary file using mkstemp() for %s! (%s)",
+          tmp_filename, err.message().c_str());
+    delete [] tmp_filename;
+    return NULL;
+  }
+
+  // Create result OutputFile. Temporary file is always truncated.
+  result = new (std::nothrow) OutputFile(tmp_filename,
+                                         pFlags | FileBase::kTruncate);
+  if (result == NULL) {
+    ALOGE("Out of memory when creates OutputFile for %s!", tmp_filename);
+    // Fall through to the clean-up codes.
+  } else {
+    if (result->hasError()) {
+      ALOGE("Failed to open temporary output file %s! (%s)",
+            result->getName().c_str(), result->getErrorMessage().c_str());
+      delete result;
+      result = NULL;
+      // Fall through to the clean-up codes.
+    }
+  }
+
+  // Clean up.
+  delete [] tmp_filename;
+  ::close(tmp_fd);
+
+  return result;
+}
+
+OutputFile::OutputFile(const std::string &pFilename, unsigned pFlags)
+  : super(pFilename, pFlags) { }
+
+ssize_t OutputFile::write(const void *pBuf, size_t count) {
+  if ((mFD < 0) || hasError()) {
+    return -1;
+  }
+
+  if ((count <= 0) || (pBuf == NULL)) {
+    // Keep safe and issue a warning.
+    ALOGW("OutputFile::write: count = %zu, buffer = %p", count, pBuf);
+    return 0;
+  }
+
+  while (count > 0) {
+    ssize_t write_size = ::write(mFD, pBuf, count);
+
+    if (write_size > 0) {
+      return write_size;
+    } else if ((errno == EAGAIN) || (errno == EINTR)) {
+      // If the errno is EAGAIN or EINTR, then we try to write again.
+      //
+      // Fall-through
+    } else {
+      detectError();
+      return -1;
+    }
+  }
+  // unreachable
+  return 0;
+}
+
+void OutputFile::truncate() {
+  if (mFD < 0) {
+    return;
+  }
+
+  do {
+    if (::ftruncate(mFD, 0) == 0) {
+      return;
+    }
+  } while (errno == EINTR);
+  detectError();
+
+  return;
+}
+
+llvm::raw_fd_ostream *OutputFile::dup() {
+  int newfd;
+
+  do {
+    newfd = ::dup(mFD);
+    if (newfd < 0) {
+      if (errno != EINTR) {
+        detectError();
+        return NULL;
+      }
+      // EINTR
+      continue;
+    }
+    // dup() returns ok.
+    break;
+  } while (true);
+
+  llvm::raw_fd_ostream *result =
+      new (std::nothrow) llvm::raw_fd_ostream(newfd, /* shouldClose */true);
+
+  if (result == NULL) {
+    mError.assign(llvm::errc::not_enough_memory, llvm::system_category());
+  }
+
+  return result;
+}
diff --git a/lib/Support/Sha1Util.cpp b/lib/Support/Sha1Util.cpp
new file mode 100644
index 0000000..7cc636c
--- /dev/null
+++ b/lib/Support/Sha1Util.cpp
@@ -0,0 +1,77 @@
+/*
+ * copyright 2012, 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 "bcc/Support/Sha1Util.h"
+
+#include <sha1.h>
+
+#include <cstring>
+
+#include "bcc/Support/Log.h"
+#include "bcc/Support/InputFile.h"
+
+using namespace bcc;
+
+bool Sha1Util::GetSHA1DigestFromFile(uint8_t pResult[SHA1_DIGEST_LENGTH],
+                                     const char *pFilename) {
+  InputFile file(pFilename);
+
+  if (file.hasError()) {
+    ALOGE("Unable to open the file %s before SHA-1 checksum "
+          "calculation! (%s)", pFilename, file.getErrorMessage().c_str());
+    return false;
+  }
+
+  SHA1_CTX sha1_context;
+  SHA1Init(&sha1_context);
+
+  char buf[256];
+  while (true) {
+    ssize_t nread = file.read(buf, sizeof(buf));
+
+    if (nread < 0) {
+      // Some errors occurred during file reading.
+      return false;
+    }
+
+    SHA1Update(&sha1_context,
+               reinterpret_cast<unsigned char *>(buf),
+               static_cast<unsigned long>(nread));
+
+    if (static_cast<size_t>(nread) < sizeof(buf)) {
+      break;
+    }
+  }
+
+  SHA1Final(pResult, &sha1_context);
+
+  return true;
+}
+
+bool Sha1Util::GetSHA1DigestFromBuffer(uint8_t pResult[SHA1_DIGEST_LENGTH],
+                                       const uint8_t *pData, size_t pSize) {
+  SHA1_CTX sha1_context;
+
+  SHA1Init(&sha1_context);
+
+  SHA1Update(&sha1_context,
+             reinterpret_cast<const unsigned char *>(pData),
+             static_cast<unsigned long>(pSize));
+
+  SHA1Final(pResult, &sha1_context);
+
+  return true;
+}
diff --git a/lib/Support/TargetCompilerConfigs.cpp b/lib/Support/TargetCompilerConfigs.cpp
new file mode 100644
index 0000000..df78637
--- /dev/null
+++ b/lib/Support/TargetCompilerConfigs.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2012, 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 "bcc/Support/TargetCompilerConfigs.h"
+
+using namespace bcc;
+
+//===----------------------------------------------------------------------===//
+// ARM
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_ARM_CODEGEN)
+
+void ARMCompilerConfig::GetFeatureVector(std::vector<std::string> &pAttributes,
+                                         bool pEnableNEON) {
+#if defined(ARCH_ARM_HAVE_VFP)
+  pAttributes.push_back("+vfp3");
+#  if !defined(ARCH_ARM_HAVE_VFP_D32)
+  pAttributes.push_back("+d16");
+#  endif
+#endif
+
+#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
+  if (pEnableNEON) {
+    pAttributes.push_back("+neon");
+    pAttributes.push_back("+neonfp");
+  } else {
+    pAttributes.push_back("-neon");
+    pAttributes.push_back("-neonfp");
+  }
+#else
+  pAttributes.push_back("-neon");
+  pAttributes.push_back("-neonfp");
+#endif
+
+  return;
+}
+
+ARMCompilerConfig::ARMCompilerConfig()
+  : CompilerConfig(DEFAULT_ARM_TRIPLE_STRING) {
+
+  // Enable NEON by default.
+  mEnableNEON = true;
+
+  std::vector<std::string> attributes;
+  GetFeatureVector(attributes, /* pEnableNEON */mEnableNEON);
+  setFeatureString(attributes);
+
+  return;
+}
+
+bool ARMCompilerConfig::enableNEON(bool pEnable) {
+#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
+  if (mEnableNEON != pEnable) {
+    std::vector<std::string> attributes;
+    GetFeatureVector(attributes, pEnable);
+    setFeatureString(attributes);
+    mEnableNEON = pEnable;
+    return true;
+  }
+  // Fall-through
+#endif
+  return false;
+}
+#endif // defined(PROVIDE_ARM_CODEGEN)
diff --git a/lib/Support/TargetLinkerConfigs.cpp b/lib/Support/TargetLinkerConfigs.cpp
new file mode 100644
index 0000000..b57e81a
--- /dev/null
+++ b/lib/Support/TargetLinkerConfigs.cpp
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2012, 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 "bcc/Config/Config.h"
+#include "bcc/Support/TargetLinkerConfigs.h"
+
+using namespace bcc;
+
+#ifdef TARGET_BUILD
+static const char* gDefaultDyld = "/system/bin/linker";
+static const char* gDefaultSysroot = "/system";
+#else
+static const char* gDefaultDyld = "/usr/lib/ld.so.1";
+static const char* gDefaultSysroot = "/";
+#endif
+
+//===----------------------------------------------------------------------===//
+// ARM
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_ARM_CODEGEN)
+ARMLinkerConfig::ARMLinkerConfig() : LinkerConfig(DEFAULT_ARM_TRIPLE_STRING) {
+
+  // set up target-dependent constraints of attributes
+  getLDInfo()->attrFactory().constraint().enableWholeArchive();
+  getLDInfo()->attrFactory().constraint().disableAsNeeded();
+  getLDInfo()->attrFactory().constraint().setSharedSystem();
+
+  // set up the predefined attributes
+  getLDInfo()->attrFactory().predefined().setWholeArchive();
+  getLDInfo()->attrFactory().predefined().setDynamic();
+
+  // set up target dependent options
+  if (getLDInfo()->options().sysroot().empty()) {
+    getLDInfo()->options().setSysroot(gDefaultSysroot);
+  }
+
+  if (!getLDInfo()->options().hasDyld()) {
+    getLDInfo()->options().setDyld(gDefaultDyld);
+  }
+}
+#endif // defined(PROVIDE_ARM_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// Mips
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_MIPS_CODEGEN)
+MipsLinkerConfig::MipsLinkerConfig()
+  : LinkerConfig(DEFAULT_MIPS_TRIPLE_STRING) {
+
+  // set up target-dependent constraints of attibutes
+  getLDInfo()->attrFactory().constraint().enableWholeArchive();
+  getLDInfo()->attrFactory().constraint().disableAsNeeded();
+  getLDInfo()->attrFactory().constraint().setSharedSystem();
+
+  // set up the predefined attributes
+  getLDInfo()->attrFactory().predefined().setWholeArchive();
+  getLDInfo()->attrFactory().predefined().setDynamic();
+
+  // set up target dependent options
+  if (getLDInfo()->options().sysroot().empty()) {
+    getLDInfo()->options().setSysroot(gDefaultSysroot);
+  }
+
+  if (!getLDInfo()->options().hasDyld()) {
+    getLDInfo()->options().setDyld(gDefaultDyld);
+  }
+}
+#endif // defined(PROVIDE_MIPS_CODEGEN)
+
+//===----------------------------------------------------------------------===//
+// X86 and X86_64
+//===----------------------------------------------------------------------===//
+#if defined(PROVIDE_X86_CODEGEN)
+X86FamilyLinkerConfigBase::X86FamilyLinkerConfigBase(const std::string& pTriple)
+  : LinkerConfig(pTriple) {
+  // set up target-dependent constraints of attibutes
+  getLDInfo()->attrFactory().constraint().enableWholeArchive();
+  getLDInfo()->attrFactory().constraint().disableAsNeeded();
+  getLDInfo()->attrFactory().constraint().setSharedSystem();
+
+  // set up the predefined attributes
+  getLDInfo()->attrFactory().predefined().setWholeArchive();
+  getLDInfo()->attrFactory().predefined().setDynamic();
+
+  // set up target dependent options
+  if (getLDInfo()->options().sysroot().empty()) {
+    getLDInfo()->options().setSysroot(gDefaultSysroot);
+  }
+
+  if (!getLDInfo()->options().hasDyld()) {
+    getLDInfo()->options().setDyld(gDefaultDyld);
+  }
+}
+
+X86_32LinkerConfig::X86_32LinkerConfig()
+  : X86FamilyLinkerConfigBase(DEFAULT_X86_TRIPLE_STRING) {
+}
+
+X86_64LinkerConfig::X86_64LinkerConfig()
+  : X86FamilyLinkerConfigBase(DEFAULT_X86_64_TRIPLE_STRING) {
+}
+#endif // defined(PROVIDE_X86_CODEGEN)
diff --git a/helper/sha1.c b/lib/Support/sha1.c
similarity index 100%
rename from helper/sha1.c
rename to lib/Support/sha1.c
diff --git a/helper/sha1.h b/lib/Support/sha1.h
similarity index 100%
rename from helper/sha1.h
rename to lib/Support/sha1.h
diff --git a/libbcc-build-rules.mk b/libbcc-build-rules.mk
deleted file mode 100644
index a065ed6..0000000
--- a/libbcc-build-rules.mk
+++ /dev/null
@@ -1,51 +0,0 @@
-#
-# 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.
-#
-
-
-#=====================================================================
-# Architecture Selection
-#=====================================================================
-
-ifeq ($(LOCAL_IS_HOST_MODULE),) # Target Build
-
-  # Note: We should only use -DFORCE_ARCH_CODEGEN on target build.
-  # For the host build, we will include as many architecture as possible,
-  # so that we can test the execution engine easily.
-
-  ifeq ($(TARGET_ARCH),arm)
-    LOCAL_CFLAGS += -DFORCE_ARM_CODEGEN
-    ifeq ($(ARCH_ARM_HAVE_VFP),true)
-      LOCAL_CFLAGS += -DARCH_ARM_HAVE_VFP
-      ifeq ($(ARCH_ARM_HAVE_VFP_D32),true)
-        LOCAL_CFLAGS += -DARCH_ARM_HAVE_VFP_D32
-      endif
-    endif
-    ifeq ($(ARCH_ARM_HAVE_NEON),true)
-      LOCAL_CFLAGS += -DARCH_ARM_HAVE_NEON
-    endif
-  else
-    ifeq ($(TARGET_ARCH),mips)
-      LOCAL_CFLAGS += -DFORCE_MIPS_CODEGEN
-    else
-      ifeq ($(TARGET_ARCH),x86)
-        LOCAL_CFLAGS += -DFORCE_X86_CODEGEN
-      else
-        $(error Unsupported architecture $(TARGET_ARCH))
-      endif
-    endif
-  endif
-
-endif
diff --git a/libbcc-config.mk b/libbcc-config.mk
index 37cc848..bd243ee 100644
--- a/libbcc-config.mk
+++ b/libbcc-config.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (C) 2011 The Android Open Source Project
+# Copyright (C) 2011-2012 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.
@@ -16,22 +16,11 @@
 
 
 #=====================================================================
-# Root Path for Other Projects
-#=====================================================================
-
-LLVM_ROOT_PATH      := external/llvm
-LIBBCC_ROOT_PATH    := frameworks/compile/libbcc
-RSLOADER_ROOT_PATH  := frameworks/compile/linkloader
-
-
-#=====================================================================
 # Configurations
 #=====================================================================
 
 libbcc_DEBUG_MC_DISASSEMBLER        := 0
 
-libbcc_USE_LOGGER                   := 1
-libbcc_USE_FUNC_LOGGER              := 0
 libbcc_DEBUG_BCC_REFLECT            := 0
 libbcc_DEBUG_MC_REFLECT             := 0
 
@@ -45,23 +34,3 @@
 else
 libbcc_USE_DISASSEMBLER := 1
 endif
-
-
-#=====================================================================
-# Common Variables
-#=====================================================================
-
-libbcc_CFLAGS := -Wall -Wno-unused-parameter -Werror
-ifneq ($(TARGET_BUILD_VARIANT),eng)
-libbcc_CFLAGS += -D__DISABLE_ASSERTS
-else
-libbcc_CFLAGS += -DANDROID_ENGINEERING_BUILD
-endif
-
-# Include File Search Path
-libbcc_C_INCLUDES := \
-  $(RSLOADER_ROOT_PATH)/android \
-  $(LIBBCC_ROOT_PATH)/lib \
-  $(LIBBCC_ROOT_PATH)/helper \
-  $(LIBBCC_ROOT_PATH)/include \
-  $(LIBBCC_ROOT_PATH)
diff --git a/libbcc-device-build.mk b/libbcc-device-build.mk
new file mode 100644
index 0000000..e87dc2b
--- /dev/null
+++ b/libbcc-device-build.mk
@@ -0,0 +1,60 @@
+#
+# Copyright (C) 2012 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.
+#
+
+LOCAL_CFLAGS := \
+  -Wall \
+  -Wno-unused-parameter \
+  -Werror \
+  -DTARGET_BUILD \
+  $(LOCAL_CFLAGS)
+
+ifeq ($(TARGET_BUILD_VARIANT),eng)
+LOCAL_CFLAGS += -DANDROID_ENGINEERING_BUILD
+endif
+
+#=====================================================================
+# Architecture Selection
+#=====================================================================
+# Note: We should only use -DFORCE_ARCH_CODEGEN on target build.
+# For the host build, we will include as many architecture as possible,
+# so that we can test the execution engine easily.
+
+ifeq ($(TARGET_ARCH),arm)
+  LOCAL_CFLAGS += -DFORCE_ARM_CODEGEN
+  ifeq ($(ARCH_ARM_HAVE_VFP),true)
+    LOCAL_CFLAGS += -DARCH_ARM_HAVE_VFP
+    ifeq ($(ARCH_ARM_HAVE_VFP_D32),true)
+      LOCAL_CFLAGS += -DARCH_ARM_HAVE_VFP_D32
+    endif
+  endif
+  ifeq ($(ARCH_ARM_HAVE_NEON),true)
+    LOCAL_CFLAGS += -DARCH_ARM_HAVE_NEON
+  endif
+else
+  ifeq ($(TARGET_ARCH),mips)
+    LOCAL_CFLAGS += -DFORCE_MIPS_CODEGEN
+  else
+    ifeq ($(TARGET_ARCH),x86)
+      LOCAL_CFLAGS += -DFORCE_X86_CODEGEN
+    else
+      $(error Unsupported architecture $(TARGET_ARCH))
+    endif
+  endif
+endif
+
+LOCAL_C_INCLUDES := \
+  $(LIBBCC_ROOT_PATH)/include \
+  $(LOCAL_C_INCLUDES)
diff --git a/libbcc-gen-build-stamp.mk b/libbcc-gen-build-info.mk
similarity index 79%
rename from libbcc-gen-build-stamp.mk
rename to libbcc-gen-build-info.mk
index 22fe81d..7112a1a 100644
--- a/libbcc-gen-build-stamp.mk
+++ b/libbcc-gen-build-info.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (C) 2011 The Android Open Source Project
+# Copyright (C) 2012 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.
@@ -40,17 +40,17 @@
       STATIC_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE))/$(lib)$(a_suffix)) \
   $(addprefix $($(my_prefix)OUT_INTERMEDIATE_LIBRARIES)/, \
     $(addsuffix $(so_suffix), $(LOCAL_SHARED_LIBRARIES))) \
-  $(LIBBCC_ROOT_PATH)/lib/ExecutionEngine/bcc.cpp
 
+# Build Rules for Automatically Generated Build Information
+GEN := $(local-intermediates-dir)/BuildInfo.cpp
 
-# Build Rules for Automatically Generated Build Stamp
-GEN := $(local-intermediates-dir)/libbcc-stamp.c
+gen_build_info := $(LOCAL_PATH)/tools/build/gen-build-info.py
 
 $(GEN): PRIVATE_PATH := $(LOCAL_PATH)
 $(GEN): PRIVATE_DEPS := $(LOCAL_LIBBCC_LIB_DEPS)
-$(GEN): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PATH)/tools/build/gen-build-stamp.py \
-                              $(PRIVATE_PATH) $(PRIVATE_DEPS) > $@
-$(GEN): $(LOCAL_PATH)/tools/build/gen-build-stamp.py $(LOCAL_LIBBCC_LIB_DEPS) \
+$(GEN): PRIVATE_CUSTOM_TOOL = $(gen_build_info) $(PRIVATE_PATH) \
+                              $(PRIVATE_DEPS) > $@
+$(GEN): $(gen_build_info) $(LOCAL_LIBBCC_LIB_DEPS) \
         $(wildcard $(LOCAL_PATH)/.git/COMMIT_EDITMSG)
 	$(transform-generated-source)
 
diff --git a/libbcc-gen-config-from-mk.mk b/libbcc-gen-config-from-mk.mk
index cc290ff..f8baa63 100644
--- a/libbcc-gen-config-from-mk.mk
+++ b/libbcc-gen-config-from-mk.mk
@@ -1,13 +1,29 @@
-# Build Rules for Extracting Configuration from Android.mk
-intermediates := $(local-intermediates-dir)
+#
+# Copyright (C) 2011-2012 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.
+#
 
-GEN := $(intermediates)/ConfigFromMk.h
+# Build rules for extracting configuration from Android.mk
+intermediates := $(call local-intermediates-dir)
 
-$(GEN): PRIVATE_PATH := $(LIBBCC_ROOT_PATH)
-$(GEN): PRIVATE_CUSTOM_TOOL = \
+GEN_CONFIG_FROM_MK := $(intermediates)/ConfigFromMk.h
+
+$(GEN_CONFIG_FROM_MK): PRIVATE_PATH := $(LIBBCC_ROOT_PATH)
+$(GEN_CONFIG_FROM_MK): PRIVATE_CUSTOM_TOOL = \
         $(PRIVATE_PATH)/tools/build/gen-config-from-mk.py < $< > $@
-$(GEN): $(LIBBCC_ROOT_PATH)/libbcc-config.mk \
+$(GEN_CONFIG_FROM_MK): $(LIBBCC_ROOT_PATH)/libbcc-config.mk \
         $(LIBBCC_ROOT_PATH)/tools/build/gen-config-from-mk.py
 	$(transform-generated-source)
 
-LOCAL_GENERATED_SOURCES += $(GEN)
+LOCAL_GENERATED_SOURCES += $(GEN_CONFIG_FROM_MK)
diff --git a/libbcc-host-build.mk b/libbcc-host-build.mk
new file mode 100644
index 0000000..85af6f3
--- /dev/null
+++ b/libbcc-host-build.mk
@@ -0,0 +1,32 @@
+#
+# Copyright (C) 2012 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.
+#
+
+LOCAL_CFLAGS := \
+  -Wall \
+  -Wno-unused-parameter \
+  -Werror \
+  -D__HOST__ \
+  $(LOCAL_CFLAGS)
+
+ifeq ($(TARGET_BUILD_VARIANT),eng)
+libbcc_CFLAGS += -DANDROID_ENGINEERING_BUILD
+endif
+
+LOCAL_C_INCLUDES := \
+  $(LIBBCC_ROOT_PATH)/include \
+  $(LOCAL_C_INCLUDES)
+
+LOCAL_IS_HOST_MODULE := true
diff --git a/libbcc.mk b/libbcc.mk
new file mode 100644
index 0000000..71cf856
--- /dev/null
+++ b/libbcc.mk
@@ -0,0 +1,50 @@
+#
+# Copyright (C) 2012 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.
+#
+
+ifeq ($(LIBBCC_ROOT_PATH),)
+$(error Must set variable LIBBCC_ROOT_PATH before including this! $(LOCAL_PATH))
+endif
+
+#=====================================================================
+# Root Path for Other Projects
+#=====================================================================
+
+LLVM_ROOT_PATH          := external/llvm
+MCLD_ROOT_PATH          := frameworks/compile/mclinker
+RSLOADER_ROOT_PATH      := frameworks/compile/linkloader
+
+#=====================================================================
+# Related Makefile Paths of libbcc
+#=====================================================================
+
+LIBBCC_HOST_BUILD_MK    := $(LIBBCC_ROOT_PATH)/libbcc-host-build.mk
+LIBBCC_DEVICE_BUILD_MK  := $(LIBBCC_ROOT_PATH)/libbcc-device-build.mk
+LIBBCC_GEN_CONFIG_MK    := $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
+
+#=====================================================================
+# Configuration of libbcc
+#=====================================================================
+include $(LIBBCC_ROOT_PATH)/libbcc-config.mk
+
+#=====================================================================
+# Related Makefile Paths of LLVM
+#=====================================================================
+include $(LLVM_ROOT_PATH)/llvm.mk
+
+#=====================================================================
+# Related Makefile Paths of MCLinker
+#=====================================================================
+include $(MCLD_ROOT_PATH)/mcld.mk
diff --git a/runtime/Android.mk b/runtime/Android.mk
index c2051ec..64ce061 100644
--- a/runtime/Android.mk
+++ b/runtime/Android.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (C) 2011 The Android Open Source Project
+# Copyright (C) 2011-2012 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.
@@ -16,7 +16,6 @@
 #
 
 LOCAL_PATH := $(call my-dir)
-include $(LOCAL_PATH)/../libbcc-config.mk
 
 #=====================================================================
 # Device Static Library: libbccCompilerRT
@@ -88,7 +87,7 @@
   endif
 endif
 
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
+include $(LIBBCC_DEVICE_BUILD_MK)
 include $(BUILD_STATIC_LIBRARY)
 
 endif # ifneq($(TARGET_ARCH),mips)
diff --git a/tools/abcc/Android.mk b/tools/abcc/Android.mk
new file mode 100644
index 0000000..25c7f2a
--- /dev/null
+++ b/tools/abcc/Android.mk
@@ -0,0 +1,51 @@
+#
+# Copyright (C) 2010-2012 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+# Executable for host
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := abcc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+
+LOCAL_STATIC_LIBRARIES := libcutils
+LOCAL_SHARED_LIBRARIES := libbcc
+LOCAL_LDLIBS = -ldl
+LOCAL_SRC_FILES := Main.cpp
+
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(BUILD_HOST_EXECUTABLE)
+
+# Executable for target
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := abcc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+
+LOCAL_SRC_FILES := Main.cpp
+
+LOCAL_SHARED_LIBRARIES := libdl libstlport libbcinfo libbcc libutils libcutils
+
+include external/stlport/libstlport.mk
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(BUILD_EXECUTABLE)
diff --git a/tools/abcc/Main.cpp b/tools/abcc/Main.cpp
new file mode 100644
index 0000000..7fe99eb
--- /dev/null
+++ b/tools/abcc/Main.cpp
@@ -0,0 +1,212 @@
+/*
+ * Copyright 2012, 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 <cerrno>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+
+#include <fcntl.h>
+
+#include "bcc/Config/Config.h"
+#include "bcc/Support/Initialization.h"
+#include "bcc/Support/Log.h"
+#include "bcc/AndroidBitcode/ABCCompilerDriver.h"
+
+#include <cutils/process_name.h>
+
+using namespace bcc;
+
+static void usage() {
+  fprintf(stderr, "usage: abcc [--fd output_fd|--file output_filename]\n"
+#ifndef TARGET_BUILD
+                  "            [--triple triple]\n"
+                  "            [--android-sysroot sysroot]\n"
+#endif
+                  "            input_filename(s) or input_fd(s)...\n");
+  return;
+}
+
+static inline bool GetIntArg(const char *arg, int &result) {
+  char *endptr;
+
+  result = ::strtol(arg, &endptr, 0);
+  if (*endptr != '\0') {
+    return false;
+  } else {
+    return true;
+  }
+}
+
+enum Mode {
+  kUnknownMode,
+  kFdMode,
+  kFileMode
+};
+
+static inline bool ParseArguments(int argc, const char *const *argv, Mode &mode,
+                                  const char *&input, const char *&output,
+                                  const char *&triple, const char *&sysroot) {
+  if (argc < 4) {
+    return false;
+  }
+
+  // Parse the mode in argv[1].
+  if (::strcmp(argv[1], "--fd") == 0) {
+    mode = kFdMode;
+  } else if (::strcmp(argv[1], "--file") == 0) {
+    mode = kFileMode;
+  } else {
+    ALOGE("Unknown mode '%s'!", argv[1]);
+    return false;
+  }
+
+  // output is always in argv[2].
+  output = argv[2];
+
+  // On-device version cannot configure the triple and sysroot.
+  int arg_idx = 3;
+#ifndef TARGET_BUILD
+  if (::strcmp(argv[arg_idx], "--triple") == 0) {
+    if ((arg_idx + 2 /* --triple [triple] input */) >= argc) {
+      ALOGE("Too few arguments when --triple was given!");
+      return false;
+    }
+
+    triple = argv[arg_idx + 1];
+    arg_idx += 2;
+  }
+
+  if (::strcmp(argv[arg_idx], "--android-sysroot") == 0) {
+    if ((arg_idx + 2 /* --android-sysroot [sysroot] input */) >= argc) {
+      ALOGE("Too few arguments when --android-sysroot was given!");
+      return false;
+    }
+
+    sysroot = argv[arg_idx + 1];
+    arg_idx += 2;
+  }
+#endif
+
+  if (triple == NULL) {
+    triple = DEFAULT_TARGET_TRIPLE_STRING;
+  }
+
+  if (sysroot == NULL) {
+    sysroot = "/";
+  }
+
+  ALOGD("Triple: %s, Android sysroot: %s", triple, sysroot);
+
+  // input is in argv[arg_idx]
+  // TODO: Support multiple input files.
+  input = argv[arg_idx];
+
+  return true;
+}
+
+static bool Build(int input_fd, int output_fd,
+                  const char *triple, const char *sysroot) {
+  ABCCompilerDriver driver(triple, sysroot);
+  return driver.build(input_fd, output_fd);;
+}
+
+static int ProcessFromFd(const char *input, const char *output,
+                         const char *triple, const char *sysroot) {
+  int output_fd, input_fd;
+
+  if (!GetIntArg(output, output_fd)) {
+    ALOGE("Bad output fd '%s'", output);
+    return EXIT_FAILURE;
+  }
+
+  if (!GetIntArg(input, input_fd)) {
+    ALOGE("Bad input fd '%s'", input);
+    return EXIT_FAILURE;
+  }
+
+  if (!Build(input_fd, output_fd, triple, sysroot)) {
+    return EXIT_FAILURE;
+  }
+
+  return EXIT_SUCCESS;
+}
+
+static int ProcessFromFile(const char *input, const char *output,
+                           const char *triple, const char *sysroot) {
+  // TODO: Support multiple input files.
+  int output_fd = -1, input_fd = -1;
+
+  // Open the output file.
+  output_fd = ::open(output, O_RDWR | O_CREAT | O_TRUNC, 0755);
+
+  if (output_fd < 0) {
+    ALOGE("Failed to open %s for output! (%s)", output, strerror(errno));
+    return EXIT_FAILURE;
+  }
+
+  // Open the input file.
+  input_fd = ::open(input, O_RDONLY);
+
+  if (input_fd < 0) {
+    ALOGE("Failed to open %s for input! (%s)", input, strerror(errno));
+    ::close(output_fd);
+    return EXIT_FAILURE;
+  }
+
+  if (!Build(input_fd, output_fd, triple, sysroot)) {
+    ::close(output_fd);
+    ::close(input_fd);
+    return EXIT_FAILURE;
+  }
+
+  ::close(output_fd);
+  ::close(input_fd);
+
+  return EXIT_SUCCESS;
+}
+
+int main(int argc, char **argv) {
+  Mode mode = kUnknownMode;
+  const char *input, *output, *triple = NULL, *sysroot = NULL;
+
+  set_process_name("abcc");
+
+  setvbuf(stdout, NULL, _IONBF, 0);
+
+  init::Initialize();
+
+  if (ParseArguments(argc, argv, mode, input, output, triple, sysroot)) {
+    switch (mode) {
+      case kFdMode: {
+        return ProcessFromFd(input, output, triple, sysroot);
+      }
+      case kFileMode: {
+        return ProcessFromFile(input, output, triple, sysroot);
+      }
+      default: {
+        // Unknown mode encountered. Fall-through to print usage and return
+        // error.
+        break;
+      }
+    }
+    // fall-through
+  }
+
+  usage();
+
+  return EXIT_FAILURE;
+}
diff --git a/tools/bcc/Android.mk b/tools/bcc/Android.mk
index 414b4d2..dd7ba69 100644
--- a/tools/bcc/Android.mk
+++ b/tools/bcc/Android.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (C) 2010 The Android Open Source Project
+# Copyright (C) 2010-2012 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.
@@ -14,9 +14,6 @@
 # limitations under the License.
 #
 
-ifeq (darwin,$(BUILD_OS))
-else
-
 LOCAL_PATH := $(call my-dir)
 
 # Executable for host
@@ -24,22 +21,16 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := bcc
-
-LOCAL_SRC_FILES := \
-  main.cpp
-
-LOCAL_SHARED_LIBRARIES := \
-  libbcc
-
-LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/../../include
-
 LOCAL_MODULE_TAGS := tests eng
+LOCAL_MODULE_CLASS := EXECUTABLES
 
+LOCAL_SHARED_LIBRARIES := libbcc
 LOCAL_LDLIBS = -ldl
+LOCAL_SRC_FILES := Main.cpp
 
-LOCAL_CFLAGS += -D__HOST__ -Wall -Werror
-
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_HOST_BUILD_MK)
 include $(BUILD_HOST_EXECUTABLE)
 
 # Executable for target
@@ -47,18 +38,15 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := bcc
-
-LOCAL_SRC_FILES := \
-  main.cpp
-
-LOCAL_SHARED_LIBRARIES := libdl libstlport libbcinfo libbcc
-
-LOCAL_C_INCLUDES := \
-  $(LOCAL_PATH)/../../include
-
 LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+
+LOCAL_SRC_FILES := Main.cpp
+
+LOCAL_SHARED_LIBRARIES := libdl libstlport libbcinfo libbcc libutils libcutils
 
 include external/stlport/libstlport.mk
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(LLVM_DEVICE_BUILD_MK)
 include $(BUILD_EXECUTABLE)
-
-endif
diff --git a/tools/bcc/Main.cpp b/tools/bcc/Main.cpp
new file mode 100644
index 0000000..585dda0
--- /dev/null
+++ b/tools/bcc/Main.cpp
@@ -0,0 +1,435 @@
+/*
+ * Copyright 2012, 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 <string>
+#include <vector>
+
+#include <stdlib.h>
+
+#include <llvm/ADT/STLExtras.h>
+#include <llvm/ADT/SmallString.h>
+#include <llvm/Config/config.h>
+#include <llvm/Support/CommandLine.h>
+#include <llvm/Support/FileSystem.h>
+#include <llvm/Support/Path.h>
+#include <llvm/Support/raw_ostream.h>
+#include <llvm/Support/system_error.h>
+
+#include <bcc/BCCContext.h>
+#include <bcc/Compiler.h>
+#include <bcc/Config/BuildInfo.h>
+#include <bcc/Config/Config.h>
+#include <bcc/ExecutionEngine/BCCRuntimeSymbolResolver.h>
+#include <bcc/ExecutionEngine/ObjectLoader.h>
+#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
+#include <bcc/ExecutionEngine/SymbolResolvers.h>
+#include <bcc/Script.h>
+#include <bcc/Source.h>
+#include <bcc/Support/CompilerConfig.h>
+#include <bcc/Support/Initialization.h>
+#include <bcc/Support/InputFile.h>
+#include <bcc/Support/OutputFile.h>
+#include <bcc/Support/TargetCompilerConfigs.h>
+
+using namespace bcc;
+
+//===----------------------------------------------------------------------===//
+// General Options
+//===----------------------------------------------------------------------===//
+namespace {
+
+llvm::cl::list<std::string>
+OptInputFilenames(llvm::cl::Positional, llvm::cl::OneOrMore,
+                  llvm::cl::desc("<input bitcode files>"));
+
+llvm::cl::opt<std::string>
+OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
+                  llvm::cl::value_desc("filename"));
+
+#ifdef TARGET_BUILD
+const std::string OptTargetTriple(DEFAULT_TARGET_TRIPLE_STRING);
+#else
+llvm::cl::opt<std::string>
+OptTargetTriple("mtriple",
+                llvm::cl::desc("Specify the target triple (default: "
+                               DEFAULT_TARGET_TRIPLE_STRING ")"),
+                llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
+                llvm::cl::value_desc("triple"));
+
+llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
+                                 llvm::cl::desc("Alias for -mtriple"),
+                                 llvm::cl::aliasopt(OptTargetTriple));
+#endif
+
+//===----------------------------------------------------------------------===//
+// Compiler Options
+//===----------------------------------------------------------------------===//
+llvm::cl::opt<bool>
+OptPIC("fPIC", llvm::cl::desc("Generate fully relocatable, position independent"
+                              " code"));
+
+llvm::cl::opt<char>
+OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
+                                "(default: -O2)"),
+            llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('2'));
+
+llvm::cl::opt<bool>
+OptC("c", llvm::cl::desc("Compile and assemble, but do not link."));
+
+//===----------------------------------------------------------------------===//
+// Linker Options
+//===----------------------------------------------------------------------===//
+// FIXME: this option will be removed in the future when MCLinker is capable
+//        of generating shared library directly from given bitcode. It only
+//        takes effect when -shared is supplied.
+llvm::cl::opt<std::string>
+OptImmObjectOutput("or", llvm::cl::desc("Specify the filename for output the "
+                                        "intermediate relocatable when linking "
+                                        "the input bitcode to the shared "
+                                        "library"), llvm::cl::ValueRequired);
+
+llvm::cl::opt<bool>
+OptShared("shared", llvm::cl::desc("Create a shared library from input bitcode "
+                                   "files"));
+
+
+//===----------------------------------------------------------------------===//
+// Loader Options
+//===----------------------------------------------------------------------===//
+llvm::cl::opt<bool>
+OptRunEntry("R", llvm::cl::desc("Run the entry method after successfully load "
+                                "and compile."));
+
+llvm::cl::opt<std::string>
+OptEntryFunction("entry-function", llvm::cl::desc("Specify the entry function "
+                                                  "for -R (default: main)"),
+                 llvm::cl::value_desc("function"), llvm::cl::init("main"));
+
+llvm::cl::opt<bool>
+OptEnableGDB("enable-gdb", llvm::cl::desc("Enable GDB JIT debugging when "
+                                          "runs the entry method"));
+
+llvm::cl::list<std::string>
+OptRuntimeLibs("load", llvm::cl::desc("Specify the shared libraries for "
+                                      "execution (e.g., -load=c will search "
+                                      "and load libc.so for execution)"),
+               llvm::cl::ZeroOrMore, llvm::cl::value_desc("namespec"));
+
+// Override "bcc -version" since the LLVM version information is not correct on
+// Android build.
+void BCCVersionPrinter() {
+  llvm::raw_ostream &os = llvm::outs();
+  os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
+     << "  Build time: " << BuildInfo::GetBuildTime() << "\n"
+     << "  Build revision: " << BuildInfo::GetBuildRev() << "\n"
+     << "  Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n"
+     << "  Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
+
+  os << "\n";
+
+  os << "LLVM (http://llvm.org/):\n"
+     << "  Version: " << PACKAGE_VERSION << "\n";
+  return;
+}
+
+} // end anonymous namespace
+
+static inline
+Script *PrepareScript(BCCContext &pContext,
+                      const llvm::cl::list<std::string> &pBitcodeFiles) {
+  Script *result = NULL;
+
+  for (unsigned i = 0; i < pBitcodeFiles.size(); i++) {
+    const std::string &input_bitcode = pBitcodeFiles[i];
+    Source *source = Source::CreateFromFile(pContext, input_bitcode);
+    if (source == NULL) {
+      llvm::errs() << "Failed to load llvm module from file `" << input_bitcode
+                   << "'!\n";
+      return NULL;
+    }
+
+    if (result != NULL) {
+      if (!result->mergeSource(*source, /* pPreserveSource */false)) {
+        llvm::errs() << "Failed to merge the llvm module `" << input_bitcode
+                     << "' to compile!\n";
+        delete source;
+        return NULL;
+      }
+    } else {
+      result = new (std::nothrow) Script(*source);
+      if (result == NULL) {
+        llvm::errs() << "Out of memory when create script for file `"
+                     << input_bitcode << "'!\n";
+        delete source;
+        return NULL;
+      }
+    }
+  }
+
+  return result;
+}
+
+static inline
+bool ConfigCompiler(Compiler &pCompiler) {
+  CompilerConfig *config = NULL;
+
+#ifdef TARGET_BUILD
+  config = new (std::nothrow) DefaultCompilerConfig();
+#else
+  config = new (std::nothrow) CompilerConfig(OptTargetTriple);
+#endif
+  if (config == NULL) {
+    llvm::errs() << "Out of memory when create the compiler configuration!\n";
+    return false;
+  }
+
+  // Setup the config according to the valud of command line option.
+  if (OptPIC) {
+    config->setRelocationModel(llvm::Reloc::PIC_);
+  }
+  switch (OptOptLevel) {
+    case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
+    case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
+    case '3': config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); break;
+    case '2':
+    default: {
+      config->setOptimizationLevel(llvm::CodeGenOpt::Default);
+      break;
+    }
+  }
+
+  Compiler::ErrorCode result = pCompiler.config(*config);
+
+  delete config;
+
+  if (result != Compiler::kSuccess) {
+    llvm::errs() << "Failed to configure the compiler! (detail: "
+                 << Compiler::GetErrorString(result) << ")\n";
+    return false;
+  }
+
+  return true;
+}
+
+#define DEFAULT_OUTPUT_PATH   "/sdcard/a.out"
+static inline
+std::string DetermineOutputFilename(const std::string &pOutputPath) {
+  if (!pOutputPath.empty()) {
+    return pOutputPath;
+  }
+
+  // User doesn't specify the value to -o.
+  if (OptInputFilenames.size() > 1) {
+    llvm::errs() << "Use " DEFAULT_OUTPUT_PATH " for output file!\n";
+    return DEFAULT_OUTPUT_PATH;
+  }
+
+  // There's only one input bitcode file.
+  const std::string &input_path = OptInputFilenames[0];
+  llvm::SmallString<200> output_path(input_path);
+
+  llvm::error_code err = llvm::sys::fs::make_absolute(output_path);
+  if (err != llvm::errc::success) {
+    llvm::errs() << "Failed to determine the absolute path of `" << input_path
+                 << "'! (detail: " << err.message() << ")\n";
+    return "";
+  }
+
+  if (OptC) {
+    // -c was specified. Replace the extension to .o.
+    llvm::sys::path::replace_extension(output_path, "o");
+  } else {
+    // Use a.out under current working directory when compile executable or
+    // shared library.
+    llvm::sys::path::remove_filename(output_path);
+    llvm::sys::path::append(output_path, "a.out");
+  }
+
+  return output_path.c_str();
+}
+
+static inline
+bool CompileScript(Compiler &pCompiler, Script &pScript,
+                   const std::string &pOutputPath) {
+  // Open the output file.
+  OutputFile output_file(pOutputPath, FileBase::kTruncate);
+
+  if (output_file.hasError()) {
+    llvm::errs() << "Failed to open the output file `" << pOutputPath
+                 << "'! (detail: " << output_file.getErrorMessage() << ")\n";
+    return false;
+  }
+
+  // Run the compiler.
+  Compiler::ErrorCode result = pCompiler.compile(pScript, output_file);
+  if (result != Compiler::kSuccess) {
+    llvm::errs() << "Fatal error during compilation (detail: "
+                 << Compiler::GetErrorString(result) << ".)\n";
+    return false;
+  }
+
+  return true;
+}
+
+static inline
+bool PrepareRuntimes(std::vector<SymbolResolverInterface *> &pRuntimes) {
+  llvm::SmallVector<const char *, 2> search_paths;
+
+#ifdef TARGET_BUILD
+  search_paths.push_back("/system/lib/");
+#else
+  search_paths.push_back("/lib/");
+  search_paths.push_back("/usr/lib/");
+#endif
+
+  // Most of the following lines comes from llvm/tools/llvm-ld.cpp.
+  for (unsigned i = 0; i < OptRuntimeLibs.size(); i++) {
+    const std::string &lib = OptRuntimeLibs[i];
+    llvm::sys::Path lib_path;
+    for (llvm::SmallVectorImpl<const char *>::const_iterator
+             search_path_iter = search_paths.begin(),
+             search_path_end = search_paths.end();
+         search_path_iter != search_path_end; search_path_iter++) {
+
+      lib_path = *search_path_iter;
+      lib_path.appendComponent("lib" + lib);
+      lib_path.appendSuffix(llvm::sys::Path::GetDLLSuffix());
+
+      if (lib_path.isEmpty()) {
+        if (!lib_path.isDynamicLibrary()) {
+          lib_path = llvm::sys::Path();
+        } else {
+          break;
+        }
+      }
+    } // for each search_paths
+    if (lib_path.isEmpty()) {
+      // FIXME: llvm::sys::Path::FindLibrary(...) is able to consume
+      //        'const std::string &' instead of 'std::string &'.
+      std::string lib_tmp = lib;
+      lib_path = llvm::sys::Path::FindLibrary(lib_tmp);
+    }
+    if (lib_path.isEmpty()) {
+      llvm::errs() << "Unable to find `lib" << lib << "' for execution!\n";
+      llvm::DeleteContainerPointers(pRuntimes);
+      return false;
+    } else {
+      DyldSymbolResolver *dyld_resolver =
+          new (std::nothrow) DyldSymbolResolver(lib_path.str().c_str());
+
+      if (dyld_resolver != NULL) {
+        pRuntimes.push_back(dyld_resolver);
+      } else {
+        llvm::errs() << "Out of memory when load `" << lib_path.str() << "'!\n";
+        llvm::DeleteContainerPointers(pRuntimes);
+        return false;
+      }
+    }
+  } // for each OptRuntimeLibs
+
+  return true;
+}
+
+static inline
+bool LoadAndRun(const std::string &pOutputExecutable) {
+  SymbolResolverProxy runtime_resolver;
+
+  // Include compiler runtime.
+  BCCRuntimeSymbolResolver bcc_runtimes;
+  runtime_resolver.chainResolver(bcc_runtimes);
+
+  // Open the output file for execution.
+  InputFile input_exec(pOutputExecutable);
+  if (input_exec.hasError()) {
+    llvm::errs() << "Failed to open the executable `" << pOutputExecutable
+                 << "'! (detail: " << input_exec.getErrorMessage() << ")\n";
+    return false;
+  }
+
+  // Load the runtime libraries given in command line.
+  std::vector<SymbolResolverInterface *> lib_runtimes;
+  if (!PrepareRuntimes(lib_runtimes)) {
+    return false;
+  }
+
+  for (std::vector<SymbolResolverInterface *>::const_iterator
+           librt_iter = lib_runtimes.begin(), librt_end = lib_runtimes.end();
+       librt_iter != librt_end; librt_iter++) {
+    runtime_resolver.chainResolver(*(*librt_iter));
+  }
+
+  // Load the output file.
+  ObjectLoader *loader = ObjectLoader::Load(input_exec, runtime_resolver,
+                                            OptEnableGDB);
+  if (loader == NULL) {
+    llvm::errs() << "Failed to load `" << pOutputExecutable << "'!\n";
+    llvm::DeleteContainerPointers(lib_runtimes);
+    return false;
+  }
+
+  // Retrieve the address of entry function.
+  void *entry = loader->getSymbolAddress(OptEntryFunction.c_str());
+  if (entry == NULL) {
+    llvm::errs() << "Couldn't find entry method `" << OptEntryFunction
+                 << "' in " << pOutputExecutable << "' for execution!\n";
+    delete loader;
+    llvm::DeleteContainerPointers(lib_runtimes);
+    return false;
+  }
+
+  // Execute the entry function.
+  int run_result = reinterpret_cast<int (*)()>(entry)();
+  llvm::errs() << "result: " << run_result << "\n";
+
+  // Clean up.
+  delete loader;
+  llvm::DeleteContainerPointers(lib_runtimes);
+
+  return true;
+}
+
+int main(int argc, char **argv) {
+  llvm::cl::SetVersionPrinter(BCCVersionPrinter);
+  llvm::cl::ParseCommandLineOptions(argc, argv);
+  init::Initialize();
+
+  BCCContext context;
+  Compiler compiler;
+
+  Script *script = PrepareScript(context, OptInputFilenames);
+  if (script == NULL) {
+    return EXIT_FAILURE;
+  }
+
+  if (!ConfigCompiler(compiler)) {
+    return EXIT_FAILURE;
+  }
+
+  std::string OutputFilename = DetermineOutputFilename(OptOutputFilename);
+  if (OutputFilename.empty()) {
+    return EXIT_FAILURE;
+  }
+
+  if (!CompileScript(compiler, *script, OutputFilename)) {
+    return EXIT_FAILURE;
+  }
+
+  if (OptRunEntry && !LoadAndRun(OutputFilename)) {
+    return EXIT_FAILURE;
+  }
+
+  return EXIT_SUCCESS;
+}
diff --git a/tools/bcc/main.cpp b/tools/bcc/main.cpp
deleted file mode 100644
index 29c3a59..0000000
--- a/tools/bcc/main.cpp
+++ /dev/null
@@ -1,391 +0,0 @@
-/*
- * Copyright 2010-2012, 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 <ctype.h>
-#include <dlfcn.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#if defined(__HOST__)
-  #if defined(__cplusplus)
-    extern "C" {
-  #endif
-      extern char *TARGET_TRIPLE_STRING;
-  #if defined(__cplusplus)
-    };
-  #endif
-#else
-#endif
-
-#include <bcc/bcc.h>
-
-#define DEFAULT_OUTPUT_FILENAME "a.out"
-
-typedef int (*RootPtr)();
-
-// This is a separate function so it can easily be set by breakpoint in gdb.
-static int run(RootPtr rootFunc) {
-  return rootFunc();
-}
-
-enum OutputType {
-  OT_Executable,
-  OT_Relocatable,
-  OT_SharedObject
-};
-
-enum OutputType OutType = OT_Executable;
-enum bccRelocModelEnum OutRelocModel = bccRelocDefault;
-const char *InFile = NULL;
-const char *OutFile = NULL;
-const char *IntermediateOutFile = NULL;
-bool RunRoot = false;
-
-struct OptionInfo {
-  const char *option_name;
-
-  // min_option_argc is the minimum number of arguments this option should
-  // have. This is for sanity check before invoking processing function.
-  unsigned min_option_argc;
-
-  const char *argument_desc;
-  const char *help_message;
-
-  // The function to process this option. Return the number of arguments it
-  // consumed or < 0 if there's an error during the processing.
-  int (*process)(int argc, char **arg);
-};
-
-// forward declaration of option processing functions
-static int optSetTriple(int, char **);
-static int optSetInput(int, char **);
-static int optSetOutput(int, char **);
-static int optSetIntermediateOutput(int, char **);
-static int optOutputReloc(int, char **);
-static int optSetOutputPIC(int, char **);
-static int optSetOutputShared(int, char **);
-static int optRunRoot(int, char **);
-static int optHelp(int, char **);
-
-static const struct OptionInfo Options[] = {
-#if defined(__HOST__)
-  { "C", 1, "triple", "set the triple string.",
-    optSetTriple },
-#endif
-
-  { "c", 0, NULL, "compile and assemble, but do not link.",
-    optOutputReloc },
-
-  { "fPIC", 0, NULL,  "Generate position-independent code, if possible.",
-    optSetOutputPIC },
-
-  { "o", 1, "output", "write the native result to an output file.",
-    optSetOutput },
-
-  // FIXME: this option will be removed in the future when MCLinker is capable
-  //        of generating shared library directly from given bitcode. It only
-  //        takes effect when -shared is supplied.
-  { "or", 1, NULL, "set the output filename for the intermediate relocatable.",
-    optSetIntermediateOutput },
-
-
-  { "shared", 0, NULL, "create a shared library.",
-    optSetOutputShared },
-
-  { "R", 0, NULL, "run root() method after a successful load and compile.",
-    optRunRoot },
-
-  { "h", 0, NULL, "print this help.",
-    optHelp },
-};
-#define NUM_OPTIONS (sizeof(Options) / sizeof(struct OptionInfo))
-
-static int parseOption(int argc, char** argv) {
-  if (argc <= 1) {
-    optHelp(argc, argv);
-    return 0; // unreachable
-  }
-
-  // argv[i] is the current processing argument from command line
-  int i = 1;
-  while (i < argc) {
-    const unsigned left_argc = argc - i - 1;
-
-    if (argv[i][0] == '-') {
-      // Find the corresponding OptionInfo object
-      unsigned opt_idx = 0;
-      while (opt_idx < NUM_OPTIONS) {
-        if (::strcmp(&argv[i][1], Options[opt_idx].option_name) == 0) {
-          const struct OptionInfo *cur_option = &Options[opt_idx];
-          if (left_argc < cur_option->min_option_argc) {
-            fprintf(stderr, "%s: '%s' requires at least %u arguments", argv[0],
-                    cur_option->option_name, cur_option->min_option_argc);
-            return 1;
-          }
-
-          int result = cur_option->process(left_argc, &argv[i]);
-          if (result >= 0) {
-            // consume the used arguments
-            i += result;
-          } else {
-            // error occurs
-            return 1;
-          }
-
-          break;
-        }
-        ++opt_idx;
-      }
-      if (opt_idx >= NUM_OPTIONS) {
-        fprintf(stderr, "%s: unrecognized option '%s'", argv[0], argv[i]);
-        return 1;
-      }
-    } else {
-      if (InFile == NULL) {
-        optSetInput(left_argc, &argv[i]);
-      } else {
-        fprintf(stderr, "%s: only a single input file is allowed currently.",
-                argv[0]);
-        return 1;
-      }
-    }
-    i++;
-  }
-
-  return 0;
-}
-
-static BCCScriptRef loadScript() {
-  if (!InFile) {
-    fprintf(stderr, "input file required.\n");
-    return NULL;
-  }
-
-  BCCScriptRef script = bccCreateScript();
-
-  if (bccReadFile(script, InFile, /* flags */BCC_SKIP_DEP_SHA1) != 0) {
-    fprintf(stderr, "bcc: FAILS to read bitcode.");
-    bccDisposeScript(script);
-    return NULL;
-  }
-
-  char *output = NULL;
-
-  if (OutFile != NULL) {
-    // Copy the outFile since we're going to modify it
-    size_t outFileLen = strlen(OutFile);
-    output = new char [outFileLen + 1];
-    strncpy(output, OutFile, outFileLen);
-  } else {
-    if (OutType == OT_Executable) {
-      output = new char [(sizeof(DEFAULT_OUTPUT_FILENAME) - 1) + 1];
-      strncpy(output, DEFAULT_OUTPUT_FILENAME,
-                  sizeof(DEFAULT_OUTPUT_FILENAME) - 1);
-    } else {
-      size_t inFileLen = strlen(InFile);
-      output = new char [inFileLen + 3 /* ensure there's room for .so */ + 1];
-      strncpy(output, InFile, inFileLen);
-
-      char *fileExtension = strrchr(output, '.');
-      if (fileExtension == NULL) {
-        // append suffix
-        fileExtension = output + inFileLen;
-        *fileExtension = '.';
-      }
-
-      fileExtension++;  // skip '.'
-      if (OutType == OT_Relocatable) {
-        *fileExtension++ = 'o';
-      } else /* must be OT_SharedObject */{
-        *fileExtension++ = 's';
-        *fileExtension++ = 'o';
-      }
-      *fileExtension++ = '\0';
-    }
-  }
-
-  int bccResult = 0;
-  const char *errMsg = NULL;
-  switch (OutType) {
-    case OT_Executable: {
-      bccResult = 1;
-      errMsg = "generation of executable is unsupported currently.";
-      break;
-    }
-    case OT_Relocatable: {
-      bccResult = bccPrepareRelocatable(script, output, OutRelocModel, 0);
-      errMsg = "failed to generate relocatable.";
-      break;
-    }
-    case OT_SharedObject: {
-      if (IntermediateOutFile != NULL) {
-        bccResult =
-            bccPrepareRelocatable(script, IntermediateOutFile, bccRelocPIC, 0);
-        errMsg = "failed to generate intermediate relocatable.";
-      }
-
-      if (bccResult == 0) {
-        bccResult =
-            bccPrepareSharedObject(script, IntermediateOutFile, output, 0);
-        errMsg = "failed to generate shared library.";
-      }
-      break;
-    }
-  }
-
-  delete [] output;
-
-  if (bccResult == 0) {
-    return script;
-  } else {
-    fprintf(stderr, "bcc: %s\n", errMsg);
-    bccDisposeScript(script);
-    return NULL;
-  }
-}
-
-static int runRoot(BCCScriptRef script) {
-  RootPtr rootPointer =
-      reinterpret_cast<RootPtr>(bccGetFuncAddr(script, "main"));
-
-  if (!rootPointer) {
-    rootPointer = reinterpret_cast<RootPtr>(bccGetFuncAddr(script, "root"));
-  }
-  if (!rootPointer) {
-    rootPointer = reinterpret_cast<RootPtr>(bccGetFuncAddr(script, "_Z4rootv"));
-  }
-  if (!rootPointer) {
-    fprintf(stderr, "Could not find root or main or mangled root.\n");
-    return 1;
-  }
-
-  fprintf(stderr, "Executing compiled code:\n");
-
-  int result = run(rootPointer);
-  fprintf(stderr, "result: %d\n", result);
-
-  return 0;
-}
-
-int main(int argc, char** argv) {
-  if(parseOption(argc, argv)) {
-    return 1;
-  }
-
-  BCCScriptRef script;
-
-  if((script = loadScript()) == NULL) {
-    return 2;
-  }
-
-  if(RunRoot && runRoot(script)) {
-    return 6;
-  }
-
-  bccDisposeScript(script);
-
-  return 0;
-}
-
-/*
- * Functions to process the command line option.
- */
-#if defined(__HOST__)
-static int optSetTriple(int, char **arg) {
-  TARGET_TRIPLE_STRING = arg[1];
-  return 1;
-}
-#endif
-
-static int optSetInput(int, char **arg) {
-  // Check the input file path
-  struct stat statInFile;
-  if (stat(arg[0], &statInFile) < 0) {
-    fprintf(stderr, "Unable to stat input file: %s\n", strerror(errno));
-    return -1;
-  }
-
-  if (!S_ISREG(statInFile.st_mode)) {
-    fprintf(stderr, "Input file should be a regular file.\n");
-    return -1;
-  }
-
-  InFile = arg[0];
-  return 0;
-}
-
-static int optSetOutput(int, char **arg) {
-  char *lastSlash = strrchr(arg[1], '/');
-  if ((lastSlash != NULL) && *(lastSlash + 1) == '\0') {
-    fprintf(stderr, "bcc: output file should not be a directory.");
-    return -1;
-  }
-
-  OutFile = arg[1];
-  return 1;
-}
-
-static int optSetIntermediateOutput(int, char **arg) {
-  char *lastSlash = strrchr(arg[1], '/');
-  if ((lastSlash != NULL) && *(lastSlash + 1) == '\0') {
-    fprintf(stderr, "bcc: output intermediate file should not be a directory.");
-    return -1;
-  }
-
-  IntermediateOutFile = arg[1];
-  return 1;
-}
-
-static int optOutputReloc(int, char **) {
-  OutType = OT_Relocatable;
-  return 0;
-}
-
-static int optSetOutputShared(int, char **) {
-  OutType = OT_SharedObject;
-  return 0;
-}
-
-static int optSetOutputPIC(int, char **) {
-  OutRelocModel = bccRelocPIC;
-  return 0;
-}
-
-static int optRunRoot(int, char **) {
-  RunRoot = true;
-  return 0;
-}
-
-static int optHelp(int, char **) {
-  printf("Usage: bcc [OPTION]... [input file]\n\n");
-  for (unsigned i = 0; i < NUM_OPTIONS; i++) {
-    const struct OptionInfo *opt = &Options[i];
-
-    printf("\t-%s", opt->option_name);
-    if (opt->argument_desc)
-      printf(" %s ", opt->argument_desc);
-    else
-      printf(" \t ");
-    printf("\t%s\n", opt->help_message);
-  }
-  exit(0);
-}
diff --git a/tools/build/dataconvert.py b/tools/build/dataconvert.py
deleted file mode 100755
index 9aec157..0000000
--- a/tools/build/dataconvert.py
+++ /dev/null
@@ -1,87 +0,0 @@
-#!/usr/bin/env python
-#
-# 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.
-#
-
-
-"""Convert data files to assembly output."""
-
-import sys
-
-
-def PrintHeader(var_name):
-  """Print out header for assembly file."""
-  sys.stdout.write("""
-#ifdef __APPLE_CC__
-/*\n\
- * The mid-2007 version of gcc that ships with Macs requires a\n\
- * comma on the .section line, but the rest of the world thinks\n\
- * that's a syntax error. It also wants globals to be explicitly\n\
- * prefixed with \"_\" as opposed to modern gccs that do the\n\
- * prefixing for you.\n\
- */\n\
-.globl _%s\n\
-  .section .rodata,\n\
-  .align 8\n\
-_%s:\n\
-#else\n\
-.globl %s\n\
-  .section .rodata\n\
-  .align 8\n\
-%s:\n\
-#endif\n\
-""" % (var_name, var_name, var_name, var_name))
-
-
-def File2Asm(var_name):
-  """Convert file to assembly output."""
-  PrintHeader(var_name)
-
-  input_size = 0
-  col = 0
-  while True:
-    buf = sys.stdin.read(1024)
-    if len(buf) <= 0:
-      break
-    input_size += len(buf)
-    for c in buf:
-      if col == 0:
-        sys.stdout.write(".byte ")
-      sys.stdout.write("0x%02x" % ord(c))
-      col += 1
-      if col == 8:
-        sys.stdout.write("\n")
-        col = 0
-      elif col % 4 == 0:
-        sys.stdout.write(", ")
-      else:
-        sys.stdout.write(",")
-  if col != 0:
-    sys.stdout.write("\n")
-
-  # encode file size
-  PrintHeader(var_name + "_size")
-  sys.stdout.write("  .long %d\n" % input_size)
-
-
-def main(argv):
-  if len(argv) < 2:
-    print "usage: %s <name>" % argv[0]
-    return 1
-
-  File2Asm(argv[1])
-
-if __name__ == "__main__":
-  main(sys.argv)
diff --git a/tools/build/gen-build-stamp.py b/tools/build/gen-build-info.py
similarity index 79%
rename from tools/build/gen-build-stamp.py
rename to tools/build/gen-build-info.py
index 8a43790..df3a0a3 100755
--- a/tools/build/gen-build-stamp.py
+++ b/tools/build/gen-build-info.py
@@ -1,4 +1,19 @@
 #!/usr/bin/env python
+#
+# Copyright (C) 2011-2012 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.
+#
 
 import datetime
 import os
@@ -113,26 +128,22 @@
 /* File list:
 %s*/
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include "bcc/Config/BuildInfo.h"
 
-char const *bccGetBuildTime() {
+using namespace bcc;
+
+const char* BuildInfo::GetBuildTime() {
   return %s;
 }
 
-char const *bccGetBuildRev() {
+const char *BuildInfo::GetBuildRev() {
   return %s;
 }
 
-char const *bccGetBuildSHA1() {
+const char *BuildInfo::GetBuildSourceBlob() {
   return %s;
 }
 
-#ifdef __cplusplus
-}
-#endif
-
 """ % (os.path.abspath(repo_dir),
        lib_list_str,
        quote_str(build_time),
diff --git a/tools/build/gen-config-from-mk.py b/tools/build/gen-config-from-mk.py
index 66b5978..5646ee7 100755
--- a/tools/build/gen-config-from-mk.py
+++ b/tools/build/gen-config-from-mk.py
@@ -1,4 +1,21 @@
 #!/usr/bin/env python
+#
+# Copyright (C) 2011-2012 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.
+#
+
+import datetime
 
 import re
 import sys
@@ -17,6 +34,8 @@
     for line in f:
         if state == STATE_PRE_CONFIG:
             if conf_patt.match(line.strip()):
+                print '#ifndef BCC_CONFIG_FROM_MK_H'
+                print '#define BCC_CONFIG_FROM_MK_H'
                 state = STATE_FOUND_CONFIG
 
         elif state == STATE_FOUND_CONFIG:
@@ -33,6 +52,7 @@
             elif split_patt.match(line.strip()):
                 # Stop reading the configuration
                 print '/* END USER CONFIG */'
+                print '#endif // BCC_CONFIG_FROM_MK_H'
                 break
 
 def main():
diff --git a/tools/build/gen-sha1-stamp.py b/tools/build/gen-sha1-stamp.py
index ab3dfc7..239d040 100755
--- a/tools/build/gen-sha1-stamp.py
+++ b/tools/build/gen-sha1-stamp.py
@@ -1,5 +1,21 @@
 #!/usr/bin/env python
+#
+# Copyright (C) 2011-2012 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.
+#
 
+import os
 import sys
 
 try:
@@ -18,20 +34,88 @@
             break
     f.close()
 
+"""The result is a list of pair of file path and its SHA-1 digest"""
 def compute_sha1_list(path_list):
-    h = sha1()
+    result = []
     for path in path_list:
+        h = sha1()
         compute_sha1(h, path)
-    return h.digest()
+        result.append((path, h.digest()))
+    return result
+
+"""For each path like /xxx/libfoo.so, generate a symbol named libfoo_so_SHA1"""
+def get_symbol_name(path):
+    return os.path.basename(path).replace('.', '_') + '_SHA1';
+
+"""Print out header for assembly file."""
+def print_asm_header(symbols):
+    sys.stdout.write("""
+/*\
+ * The mid-2007 version of gcc that ships with Macs requires a\n\
+ * comma on the .section line, but the rest of the world thinks\n\
+ * that's a syntax error. It also wants globals to be explicitly\n\
+ * prefixed with \"_\" as opposed to modern gccs that do the\n\
+ * prefixing for you.\n\
+ */\n\
+""")
+    for sym in symbols:
+        sys.stdout.write("""
+#ifdef __APPLE_CC__
+.globl _%s\n\
+#else\n\
+.globl %s\n\
+#endif\
+""" % (sym, sym))
+    sys.stdout.write("""
+#ifdef __APPLE_CC__
+  .section .rodata,\n\
+#else\n\
+  .section .rodata\n\
+#endif\
+""" )
+
+def print_asm_data(data, size):
+    col = 0
+    sys.stdout.write(".align 8\n")
+    for i in xrange(size):
+        c = data[i]
+        if col == 0:
+            sys.stdout.write(".byte ")
+        elif col % 4 == 0:
+            sys.stdout.write(", ")
+        else:
+            sys.stdout.write(",")
+        sys.stdout.write("0x%02x" % ord(c))
+        col += 1
+        if col == 8:
+            sys.stdout.write("\n")
+            col = 0
+    if col != 0:
+        sys.stdout.write("\n")
+
+def print_asm_symbol_data(sym, h):
+    sys.stdout.write("""
+#ifdef __APPLE_CC__
+_%s:\n\
+#else\n\
+%s:\n\
+#endif\n\
+""" % (sym, sym))
+    print_asm_data(h, 20)
+
+def print_asm(x):
+    symbols = [get_symbol_name(item[0]) for item in x]
+    print_asm_header(symbols)
+    for (symbol, y) in zip(symbols, x):
+        print_asm_symbol_data(symbol, y[1])
 
 def main():
     if len(sys.argv) < 2:
         print 'USAGE:', sys.argv[0], '[OUTPUT] [INPUTs]'
         sys.exit(1)
 
-    f = open(sys.argv[1], 'wb')
-    f.write(compute_sha1_list(sys.argv[2:]))
-    f.close()
+    result = compute_sha1_list(sys.argv[1:])
+    print_asm(result)
 
 if __name__ == '__main__':
     main()
diff --git a/tools/mcld/Android.mk b/tools/mcld/Android.mk
new file mode 100644
index 0000000..6d6c27b
--- /dev/null
+++ b/tools/mcld/Android.mk
@@ -0,0 +1,55 @@
+#
+# Copyright (C) 2010-2012 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+# Executable for host
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := mcld
+LOCAL_MODULE_TAGS := tests eng
+LOCAL_MODULE_CLASS := EXECUTABLES
+
+LOCAL_SRC_FILES := Main.cpp
+
+LOCAL_SHARED_LIBRARIES := libbcc
+LOCAL_LDLIBS = -ldl
+
+include $(LIBBCC_HOST_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_HOST_BUILD_MK)
+#include $(LLVM_HOST_BUILD_MK)
+include $(BUILD_HOST_EXECUTABLE)
+
+# Executable for target
+# ========================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := mcld
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := EXECUTABLES
+
+LOCAL_SRC_FILES := Main.cpp
+
+LOCAL_SHARED_LIBRARIES := libdl libstlport libbcc
+
+include external/stlport/libstlport.mk
+include $(LIBBCC_DEVICE_BUILD_MK)
+include $(LIBBCC_GEN_CONFIG_MK)
+include $(MCLD_DEVICE_BUILD_MK)
+#include $(LLVM_DEVICE_BUILD_MK)
+include $(BUILD_EXECUTABLE)
diff --git a/tools/mcld/Main.cpp b/tools/mcld/Main.cpp
new file mode 100644
index 0000000..4f1f661
--- /dev/null
+++ b/tools/mcld/Main.cpp
@@ -0,0 +1,353 @@
+/*
+ * Copyright 2012, 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 <stdlib.h>
+#include <string>
+
+#include <llvm/ADT/SmallString.h>
+#include <llvm/Support/CommandLine.h>
+#include <llvm/Support/FileSystem.h>
+#include <llvm/Support/Path.h>
+#include <llvm/Support/raw_ostream.h>
+#include <llvm/Support/system_error.h>
+
+#include <mcld/Config/Config.h>
+
+#include <bcc/Config/Config.h>
+#include <bcc/Support/LinkerConfig.h>
+#include <bcc/Support/Initialization.h>
+#include <bcc/Support/TargetLinkerConfigs.h>
+#include <bcc/Linker.h>
+
+using namespace bcc;
+
+//===----------------------------------------------------------------------===//
+// Compiler Options
+//===----------------------------------------------------------------------===//
+#ifdef TARGET_BUILD
+static const std::string OptTargetTripe(DEFAULT_TARGET_TRIPLE_STRING);
+#else
+static llvm::cl::opt<std::string>
+OptTargetTriple("mtriple",
+                llvm::cl::desc("Specify the target triple (default: "
+                                DEFAULT_TARGET_TRIPLE_STRING ")"),
+                llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
+                llvm::cl::value_desc("triple"));
+
+static llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
+                                        llvm::cl::desc("Alias for -mtriple"),
+                                        llvm::cl::aliasopt(OptTargetTriple));
+#endif
+
+//===----------------------------------------------------------------------===//
+// Command Line Options
+// There are four kinds of command line options:
+//   1. input, (may be a file, such as -m and /tmp/XXXX.o.)
+//   2. scripting options, (represent a subset of link scripting language, such
+//      as --defsym.)
+//   3. and general options. (the rest of options)
+//===----------------------------------------------------------------------===//
+// General Options
+//===----------------------------------------------------------------------===//
+static llvm::cl::opt<std::string>
+OptOutputFilename("o",
+                  llvm::cl::desc("Output filename"),
+                  llvm::cl::value_desc("filename"));
+
+static llvm::cl::opt<std::string>
+OptSysRoot("sysroot", llvm::cl::desc("Use directory as the location of the "
+                                     "sysroot, overriding the configure-time "
+                                     "default."),
+           llvm::cl::value_desc("directory"),
+           llvm::cl::ValueRequired);
+
+static llvm::cl::list<std::string>
+OptSearchDirList("L",
+                 llvm::cl::ZeroOrMore,
+                 llvm::cl::desc("Add path searchdir to the list of paths that "
+                                "mcld will search for archive libraries and "
+                                "mcld control scripts."),
+                 llvm::cl::value_desc("searchdir"),
+                 llvm::cl::Prefix);
+
+static llvm::cl::opt<std::string>
+OptSOName("soname",
+          llvm::cl::desc("Set internal name of shared library"),
+          llvm::cl::value_desc("name"));
+
+
+static llvm::cl::opt<bool>
+OptShared("shared",
+          llvm::cl::desc("Create a shared library."),
+          llvm::cl::init(false));
+
+static llvm::cl::opt<bool>
+OptBsymbolic("Bsymbolic",
+             llvm::cl::desc("Bind references within the shared library."),
+             llvm::cl::init(true));
+
+static llvm::cl::opt<std::string>
+OptDyld("dynamic-linker",
+        llvm::cl::desc("Set the name of the dynamic linker."),
+        llvm::cl::value_desc("Program"));
+
+//===----------------------------------------------------------------------===//
+// Inputs
+//===----------------------------------------------------------------------===//
+static llvm::cl::list<std::string>
+OptInputObjectFiles(llvm::cl::Positional,
+                    llvm::cl::desc("[input object files]"),
+                    llvm::cl::OneOrMore);
+
+static llvm::cl::list<std::string>
+OptNameSpecList("l",
+                llvm::cl::ZeroOrMore,
+                llvm::cl::desc("Add the archive or object file specified by "
+                               "namespec to the list of files to link."),
+                llvm::cl::value_desc("namespec"),
+                llvm::cl::Prefix);
+
+//===----------------------------------------------------------------------===//
+// Scripting Options
+//===----------------------------------------------------------------------===//
+static llvm::cl::list<std::string>
+OptWrapList("wrap",
+            llvm::cl::ZeroOrMore,
+            llvm::cl::desc("Use a wrap function fo symbol."),
+            llvm::cl::value_desc("symbol"));
+
+static llvm::cl::list<std::string>
+OptPortableList("portable",
+                llvm::cl::ZeroOrMore,
+                llvm::cl::desc("Use a portable function to symbol."),
+                llvm::cl::value_desc("symbol"));
+
+//===----------------------------------------------------------------------===//
+// Helper Functions
+//===----------------------------------------------------------------------===//
+// Override "mcld -version"
+static void MCLDVersionPrinter() {
+  llvm::raw_ostream &os = llvm::outs();
+  os << "mcld (The MCLinker Project, http://mclinker.googlecode.com/):\n"
+     << "  version: "MCLD_VERSION"\n"
+     << "  Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n";
+
+  os << "\n";
+
+  os << "LLVM (http://llvm.org/):\n";
+
+  return;
+}
+
+#define DEFAULT_OUTPUT_PATH "a.out"
+static inline
+std::string DetermineOutputFilename(const std::string &pOutputPath) {
+  if (!pOutputPath.empty()) {
+    return pOutputPath;
+  }
+
+  // User does't specify the value to -o
+  if (OptInputObjectFiles.size() > 1) {
+    llvm::errs() << "Use " DEFAULT_OUTPUT_PATH " for output file!\n";
+    return DEFAULT_OUTPUT_PATH;
+  }
+
+  // There's only one input file
+  const std::string &input_path = OptInputObjectFiles[0];
+  llvm::SmallString<200> output_path(input_path);
+
+  llvm::error_code err = llvm::sys::fs::make_absolute(output_path);
+  if (llvm::errc::success != err) {
+    llvm::errs() << "Failed to determine the absolute path of `" << input_path
+                 << "'! (detail: " << err.message() << ")\n";
+    return "";
+  }
+
+  llvm::sys::path::remove_filename(output_path);
+  llvm::sys::path::append(output_path, "a.out");
+
+  return output_path.c_str();
+}
+
+static inline
+bool ConfigLinker(Linker &pLinker, const std::string &pOutputFilename) {
+  LinkerConfig* config = NULL;
+
+#ifdef TARGET_BUILD
+  config = new (std::nothrow) DefaultLinkerConfig();
+#else
+  config = new (std::nothrow) LinkerConfig(OptTargetTriple);
+#endif
+  if (config == NULL) {
+    llvm::errs() << "Out of memory when create the linker configuration!\n";
+    return false;
+  }
+
+  // Setup the configuration accroding to the command line options.
+
+  // 1. Set up soname.
+  if (!OptSOName.empty()) {
+    config->setSOName(OptSOName);
+  } else {
+    config->setSOName(pOutputFilename);
+  }
+
+  // 2. If given, set up sysroot.
+  if (!OptSysRoot.empty()) {
+    config->setSysRoot(OptSysRoot);
+  }
+
+  // 3. If given, set up dynamic linker path.
+  if (!OptDyld.empty()) {
+    config->setDyld(OptDyld);
+  }
+
+  // 4. If given, set up wrapped symbols.
+  llvm::cl::list<std::string>::iterator wrap, wrap_end = OptWrapList.end();
+  for (wrap = OptWrapList.begin(); wrap != wrap_end; ++wrap) {
+    config->addWrap(*wrap);
+  }
+
+  // 5. If given, set up portable symbols.
+  llvm::cl::list<std::string>::iterator portable, portable_end = OptPortableList.end();
+  for (portable = OptPortableList.begin(); portable != portable_end; ++portable) {
+    config->addPortable(*portable);
+  }
+
+  // 6. if given, set up search directories.
+  llvm::cl::list<std::string>::iterator sdir, sdir_end = OptSearchDirList.end();
+  for (sdir = OptSearchDirList.begin(); sdir != sdir_end; ++sdir) {
+    config->addSearchDir(*sdir);
+  }
+
+  // set up default search directories
+  config->addSearchDir("=/lib");
+  config->addSearchDir("=/usr/lib");
+
+  // 7. Set up output's type.
+  config->setShared(OptShared);
+
+  // 8. Set up -Bsymbolic.
+  config->setBsymbolic(OptBsymbolic);
+
+  Linker::ErrorCode result = pLinker.config(*config);
+  if (Linker::kSuccess != result) {
+    llvm::errs() << "Failed to configure the linker! (detail: "
+                 << Linker::GetErrorString(result) << ")\n";
+    return false;
+  }
+
+  return true;
+}
+
+static inline
+bool PrepareInputOutput(Linker &pLinker, const std::string &pOutputPath) {
+  // -----  Set output  ----- //
+
+  // FIXME: Current MCLinker requires one to set up output before inputs. The
+  // constraint will be relaxed in the furture.
+  Linker::ErrorCode result = pLinker.setOutput(pOutputPath);
+
+  if (Linker::kSuccess != result) {
+    llvm::errs() << "Failed to open the output file! (detail: "
+                 << pOutputPath << ": "
+                 << Linker::GetErrorString(result) << ")\n";
+    return false;
+  }
+
+  // -----  Set inputs  ----- //
+  llvm::cl::list<std::string>::iterator file_it = OptInputObjectFiles.begin();
+  llvm::cl::list<std::string>::iterator lib_it  = OptNameSpecList.begin();
+
+  llvm::cl::list<std::string>::iterator file_begin = OptInputObjectFiles.begin();
+  llvm::cl::list<std::string>::iterator lib_begin = OptNameSpecList.begin();
+  llvm::cl::list<std::string>::iterator file_end = OptInputObjectFiles.end();
+  llvm::cl::list<std::string>::iterator lib_end = OptNameSpecList.end();
+
+  unsigned lib_pos = 0, file_pos = 0;
+  while (true) {
+    if (lib_it != lib_end) {
+      lib_pos = OptNameSpecList.getPosition(lib_it - lib_begin);
+    } else {
+      lib_pos = 0;
+    }
+
+    if (file_it != file_end) {
+      file_pos = OptInputObjectFiles.getPosition(file_it - file_begin);
+    } else {
+      file_pos = 0;
+    }
+
+    if ((file_pos != 0) && ((lib_pos == 0) || (file_pos < lib_pos))) {
+      result = pLinker.addObject(*file_it);
+      if (Linker::kSuccess != result) {
+        llvm::errs() << "Failed to open the input file! (detail: " << *file_it
+                     << ": " << Linker::GetErrorString(result) << ")\n";
+        return false;
+      }
+      ++file_it;
+    } else if ((lib_pos != 0) && ((file_pos == 0) || (lib_pos < file_pos))) {
+      result = pLinker.addNameSpec(*lib_it);
+      if (Linker::kSuccess != result) {
+        llvm::errs() << "Failed to open the namespec! (detail: " << *lib_it
+                     << ": " << Linker::GetErrorString(result) << ")\n";
+        return false;
+      }
+      ++lib_it;
+    } else {
+      break; // we're done with the list
+    }
+  }
+
+  return true;
+}
+
+static inline bool LinkFiles(Linker &pLinker) {
+  Linker::ErrorCode result = pLinker.link();
+  if (Linker::kSuccess != result) {
+    llvm::errs() << "Failed to linking! (detail: "
+                 << Linker::GetErrorString(result) << "\n";
+    return false;
+  }
+  return true;
+}
+
+int main(int argc, char** argv) {
+  llvm::cl::SetVersionPrinter(MCLDVersionPrinter);
+  llvm::cl::ParseCommandLineOptions(argc, argv);
+  init::Initialize();
+
+  std::string OutputFilename = DetermineOutputFilename(OptOutputFilename);
+  if (OutputFilename.empty()) {
+    return EXIT_FAILURE;
+  }
+
+  Linker linker;
+  if (!ConfigLinker(linker, OutputFilename)) {
+    return EXIT_FAILURE;
+  }
+
+  if (!PrepareInputOutput(linker, OutputFilename)) {
+    return EXIT_FAILURE;
+  }
+
+  if (!LinkFiles(linker)) {
+    return EXIT_FAILURE;
+  }
+
+  return EXIT_SUCCESS;
+}
+