dex2oat.cc revision 46398608eaa47afe5a4cfb91e3f43211bf937d68
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
1669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
1769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <stdio.h>
1869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <stdlib.h>
196cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom#include <sys/stat.h>
2069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
21ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom#include <fstream>
224922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom#include <iostream>
234922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom#include <sstream>
2469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <string>
2569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <vector>
2669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
271aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h"
28e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/stringpiece.h"
29a84395489098e4531619b1cffd1afc282b14510eSameer Abu Asal#include "base/timing_logger.h"
30761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
3169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "class_linker.h"
324f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
337940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "driver/compiler_driver.h"
347940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "elf_fixup.h"
357940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "elf_stripper.h"
361d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
371d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
387940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "image_writer.h"
396f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers#include "leb128.h"
40ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h"
432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
457940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "oat_writer.h"
466d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
475e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers#include "os.h"
4869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "runtime.h"
4900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "ScopedLocalRef.h"
5000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
511f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h"
52700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "vector_output_stream.h"
5300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "well_known_classes.h"
54a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom#include "zip_archive.h"
5569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
5669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromnamespace art {
5769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
58cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageErrorV(const char* fmt, va_list ap) {
59cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  std::string error;
60cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  StringAppendV(&error, fmt, ap);
61cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  LOG(ERROR) << error;
62cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom}
63cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom
64cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageError(const char* fmt, ...) {
65cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_list ap;
66cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_start(ap, fmt);
67cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageErrorV(fmt, ap);
68cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_end(ap);
69cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom}
70cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom
71cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void Usage(const char* fmt, ...) {
72cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_list ap;
73cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_start(ap, fmt);
74cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageErrorV(fmt, ap);
75cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_end(ap);
76cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom
77cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("Usage: dex2oat [options]...");
78cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
79cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --dex-file=<dex-file>: specifies a .dex file to compile.");
80cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --dex-file=/system/framework/core.jar");
81cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
82cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
83cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      containing a classes.dex file to compile.");
84cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --zip-fd=5");
85cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
864560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  UsageError("  --zip-location=<zip-location>: specifies a symbolic name for the file");
874560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  UsageError("      corresponding to the file descriptor specified by --zip-fd.");
88cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --zip-location=/system/app/Calculator.apk");
89cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
90265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --oat-file=<file.oat>: specifies the oat output destination via a filename.");
91265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("      Example: --oat-file=/system/framework/boot.oat");
92265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
93265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --oat-fd=<number>: specifies the oat output destination via a file descriptor.");
94cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --oat-file=/system/framework/boot.oat");
95cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
96cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
97cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      to the file descriptor specified by --oat-fd.");
987675e16bcae06c0fe258aad89b3d511037dec399Brian Carlstrom  UsageError("      Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat");
99cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
100265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --oat-symbols=<file.oat>: specifies the oat output destination with full symbols.");
101265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("      Example: --oat-symbols=/symbols/system/framework/boot.oat");
102265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
1038b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  UsageError("  --bitcode=<file.bc>: specifies the optional bitcode filename.");
1048b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  UsageError("      Example: --bitcode=/system/framework/boot.bc");
1058b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  UsageError("");
106cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --image=<file.art>: specifies the output image filename.");
107cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --image=/system/framework/boot.art");
108cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
109cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --image-classes=<classname-file>: specifies classes to include in an image.");
110cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --image=frameworks/base/preloaded-classes");
111cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
112cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --base=<hex-address>: specifies the base address when creating a boot image.");
113cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --base=0x50000000");
114cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
115cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --boot-image=<file.art>: provide the image file for the boot class path.");
116cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --boot-image=/system/framework/boot.art");
117cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Default: <host-prefix>/system/framework/boot.art");
118cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
119265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --host-prefix=<path>: used to translate host paths to target paths during");
120cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      cross compilation.");
121cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --host-prefix=out/target/product/crespo");
122cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Default: $ANDROID_PRODUCT_OUT");
123cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
1243f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("  --android-root=<path>: used to locate libraries for portable linking.");
1253f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("      Example: --android-root=out/host/linux-x86");
1263f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("      Default: $ANDROID_ROOT");
1273f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("");
1281f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao  UsageError("  --instruction-set=(arm|mips|x86): compile for a particular instruction");
12949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  UsageError("      set.");
1301f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao  UsageError("      Example: --instruction-set=x86");
1311f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao  UsageError("      Default: arm");
13249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  UsageError("");
133c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("  --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend");
134c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("      set.");
135c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("      Example: --instruction-set=Portable");
136c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("      Default: Quick");
137265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
138265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --host: used with Portable backend to link against host runtime libraries");
139265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
14046398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  UsageError("  --dump-timing: display a breakdown of where time was spent");
14146398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  UsageError("");
142cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --runtime-arg <argument>: used to specify various arguments for the runtime,");
143cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      such as initial heap size, maximum heap size, and verbose output.");
144cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Use a separate --runtime-arg switch for each argument.");
145cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --runtime-arg -Xms256m");
146cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
147cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  std::cerr << "See log for usage error information\n";
14869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  exit(EXIT_FAILURE);
14969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom}
15069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
151ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstromclass Dex2Oat {
152ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom public:
1534560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  static bool Create(Dex2Oat** p_dex2oat,
1544560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     Runtime::Options& options,
1554560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     CompilerBackend compiler_backend,
1564560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     InstructionSet instruction_set,
1574560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     size_t thread_count)
158b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
15900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (!CreateRuntime(options, instruction_set)) {
16000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      *p_dex2oat = NULL;
16100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return false;
162254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson    }
1630177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom    *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count);
16400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return true;
165ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
166ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
167ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  ~Dex2Oat() {
168ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    delete runtime_;
16975a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum    VLOG(compiler) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_)
1704560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom              << " (threads: " << thread_count_ << ")";
171ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
172ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
173c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum
1744560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
1754922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  CompilerDriver::DescriptorSet* ReadImageClassesFromFile(const char* image_classes_filename) {
1764560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom    UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename,
1774560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                                  std::ifstream::in));
178ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    if (image_classes_file.get() == NULL) {
179ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
180ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return NULL;
181ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
1824922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<CompilerDriver::DescriptorSet> result(ReadImageClasses(*image_classes_file.get()));
1834922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    image_classes_file->close();
1844922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    return result.release();
1854922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  }
186ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
1874922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  CompilerDriver::DescriptorSet* ReadImageClasses(std::istream& image_classes_stream) {
18896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    UniquePtr<CompilerDriver::DescriptorSet> image_classes(new CompilerDriver::DescriptorSet);
1894922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    while (image_classes_stream.good()) {
190ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      std::string dot;
1914922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      std::getline(image_classes_stream, dot);
192f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      if (StartsWith(dot, "#") || dot.empty()) {
193ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        continue;
194ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
195955724179c6c739524f610023287f56b24dc31deElliott Hughes      std::string descriptor(DotToDescriptor(dot.c_str()));
19696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      image_classes->insert(descriptor);
197ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
198ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    return image_classes.release();
199254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson  }
200254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson
2014560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
2024560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  CompilerDriver::DescriptorSet* ReadImageClassesFromZip(const std::string& zip_filename,
2034560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                         const char* image_classes_filename) {
2044922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename));
2054922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (zip_archive.get() == NULL) {
2064922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      LOG(ERROR) << "Failed to open zip file " << zip_filename;
2074922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      return NULL;
2084922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
2094922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename));
2104922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (zip_entry.get() == NULL) {
2114922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      LOG(ERROR) << "Failed to find " << image_classes_filename << " within " << zip_filename;
2124922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      return NULL;
2134922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
2144922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(image_classes_filename));
2154922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (image_classes_file.get() == NULL) {
2164922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      LOG(ERROR) << "Failed to extract " << image_classes_filename << " from " << zip_filename;
2174922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      return NULL;
2184922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
2194922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()),
2204922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom                                           image_classes_file->Size());
2214922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    std::istringstream image_classes_stream(image_classes_string);
2224922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    return ReadImageClasses(image_classes_stream);
2234922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  }
2244922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom
2251212a022fa5f8ef9585d765b1809521812af882cIan Rogers  const CompilerDriver* CreateOatFile(const std::string& boot_image_option,
2263f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::string* host_prefix,
2273f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::string& android_root,
2283f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      bool is_host,
2293f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::vector<const DexFile*>& dex_files,
2303f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      File* oat_file,
2313f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::string& bitcode_filename,
2323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      bool image,
23396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom                                      UniquePtr<CompilerDriver::DescriptorSet>& image_classes,
2343f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      bool dump_stats,
2356f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum                                      base::TimingLogger& timings)
236b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
237ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    // SirtRef and ClassLoader creation needs to come after Runtime::Create
23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    jobject class_loader = NULL;
239ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    if (!boot_image_option.empty()) {
240ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
241ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      std::vector<const DexFile*> class_path_files(dex_files);
242a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
243ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      for (size_t i = 0; i < class_path_files.size(); i++) {
244ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        class_linker->RegisterDexFile(*class_path_files[i]);
245ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
24600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      ScopedObjectAccessUnchecked soa(Thread::Current());
24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
24800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      ScopedLocalRef<jobject> class_loader_local(soa.Env(),
24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers          soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
25100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
252254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson    }
253ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
2541212a022fa5f8ef9585d765b1809521812af882cIan Rogers    UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_,
2551212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                        instruction_set_,
2561212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                        image,
25796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom                                                        image_classes.release(),
2581212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                        thread_count_,
2594560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                        dump_stats));
2608b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien
261c928de90ad22ecdf83c18a07008409595f13d3b1Ian Rogers    if (compiler_backend_ == kPortable) {
2621212a022fa5f8ef9585d765b1809521812af882cIan Rogers      driver->SetBitcodeFileName(bitcode_filename);
263c531cefbfb5394413122e9f57d211ba436cff012buzbee    }
2648b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien
2658447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum
26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    Thread::Current()->TransitionFromRunnableToSuspended(kNative);
26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2684560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom    driver->CompileAll(class_loader, dex_files, timings);
26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    Thread::Current()->TransitionFromSuspendedToRunnable();
271ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
2726f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    timings.NewSplit("dex2oat OatWriter");
27381f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom    std::string image_file_location;
27428db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    uint32_t image_file_location_oat_checksum = 0;
275700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    uint32_t image_file_location_oat_data_begin = 0;
2760aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao    if (!driver->IsImage()) {
2771d54e73444e017d3a65234e0f193846f3e27472bIan Rogers      gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
27828db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom      image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
27939ebcb800aabedd0ffa6aa4aeac8aa4194c66e61Ian Rogers      image_file_location_oat_data_begin =
28039ebcb800aabedd0ffa6aa4aeac8aa4194c66e61Ian Rogers          reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin());
28181f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom      image_file_location = image_space->GetImageFilename();
28234f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom      if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
28334f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom        image_file_location = image_file_location.substr(host_prefix->size());
28481f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom      }
28581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom    }
28681f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
287c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom    OatWriter oat_writer(dex_files,
288c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         image_file_location_oat_checksum,
289c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         image_file_location_oat_data_begin,
290c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         image_file_location,
291c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         driver.get());
292700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
293c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom    if (!driver->WriteElf(android_root, is_host, dex_files, oat_writer, oat_file)) {
294700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
295700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      return NULL;
296700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
297700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2981212a022fa5f8ef9585d765b1809521812af882cIan Rogers    return driver.release();
299254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson  }
300ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
301a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  bool CreateImageFile(const std::string& image_filename,
302ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom                       uintptr_t image_base,
303ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom                       const std::string& oat_filename,
304f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom                       const std::string& oat_location,
3051212a022fa5f8ef9585d765b1809521812af882cIan Rogers                       const CompilerDriver& compiler)
306b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::mutator_lock_) {
307700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    uintptr_t oat_data_begin;
308700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    {
309700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      // ImageWriter is scoped so it can free memory before doing FixupElf
31096391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      ImageWriter image_writer(compiler);
31196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
312700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom        LOG(ERROR) << "Failed to create image file " << image_filename;
313700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom        return false;
314700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      }
315700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      oat_data_begin = image_writer.GetOatDataBegin();
316700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
317700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
3187571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom    UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
319700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    if (oat_file.get() == NULL) {
320700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      PLOG(ERROR) << "Failed to open ELF file: " << oat_filename;
321700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      return false;
322700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
32351c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom    if (!ElfFixup::Fixup(oat_file.get(), oat_data_begin)) {
324700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath();
325ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return false;
326ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
327ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    return true;
328ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
329ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
330ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom private:
3314560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  explicit Dex2Oat(Runtime* runtime,
3324560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                   CompilerBackend compiler_backend,
3334560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                   InstructionSet instruction_set,
3340177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom                   size_t thread_count)
335c531cefbfb5394413122e9f57d211ba436cff012buzbee      : compiler_backend_(compiler_backend),
336c531cefbfb5394413122e9f57d211ba436cff012buzbee        instruction_set_(instruction_set),
33749c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        runtime_(runtime),
338de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes        thread_count_(thread_count),
339de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes        start_ns_(NanoTime()) {
340bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  }
341ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
343b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (!Runtime::Create(options, false)) {
345ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      LOG(ERROR) << "Failed to create runtime";
34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return false;
347ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
34800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    Runtime* runtime = Runtime::Current();
349ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    // if we loaded an existing image, we will reuse values from the image roots.
3501984651929744dd603fd082e23eacd877b9bc177Ian Rogers    if (!runtime->HasResolutionMethod()) {
3511984651929744dd603fd082e23eacd877b9bc177Ian Rogers      runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
3521984651929744dd603fd082e23eacd877b9bc177Ian Rogers    }
353ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
354ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
355ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (!runtime->HasCalleeSaveMethod(type)) {
35649c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
357ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
358ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
3591984651929744dd603fd082e23eacd877b9bc177Ian Rogers    runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
36000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return true;
361ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
362ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
363ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // Appends to dex_files any elements of class_path that it doesn't already
364ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // contain. This will open those dex files as necessary.
3654560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  static void OpenClassPathFiles(const std::string& class_path,
3664560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                 std::vector<const DexFile*>& dex_files) {
367ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    std::vector<std::string> parsed;
368ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    Split(class_path, ':', parsed);
36933e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers    // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained.
37033e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers    ScopedObjectAccess soa(Thread::Current());
371ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    for (size_t i = 0; i < parsed.size(); ++i) {
372ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (DexFilesContains(dex_files, parsed[i])) {
373ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        continue;
374ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
375a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
376ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (dex_file == NULL) {
377ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        LOG(WARNING) << "Failed to open dex file " << parsed[i];
378ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      } else {
379ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        dex_files.push_back(dex_file);
380ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
381ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
382ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
383ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
384ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // Returns true if dex_files has a dex with the named location.
3854560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  static bool DexFilesContains(const std::vector<const DexFile*>& dex_files,
3864560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                               const std::string& location) {
387ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    for (size_t i = 0; i < dex_files.size(); ++i) {
388ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (dex_files[i]->GetLocation() == location) {
389ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        return true;
390ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
391ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
392ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    return false;
393ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
394ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
395c531cefbfb5394413122e9f57d211ba436cff012buzbee  const CompilerBackend compiler_backend_;
396c531cefbfb5394413122e9f57d211ba436cff012buzbee
39749c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  const InstructionSet instruction_set_;
398ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
399bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  Runtime* runtime_;
4005523ee070b005576c6f889415205d49ea77cf243Elliott Hughes  size_t thread_count_;
401bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  uint64_t start_ns_;
402bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes
403ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
404ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom};
405254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson
40672395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic bool ParseInt(const char* in, int* out) {
407a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  char* end;
408a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  int result = strtol(in, &end, 10);
409a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (in == end || *end != '\0') {
410a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    return false;
411a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
412a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  *out = result;
413a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  return true;
414a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom}
415a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
416bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughesstatic size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
417bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes                           const std::vector<const char*>& dex_locations,
418bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes                           std::vector<const DexFile*>& dex_files) {
419bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes  size_t failure_count = 0;
4205b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  for (size_t i = 0; i < dex_filenames.size(); i++) {
4215b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    const char* dex_filename = dex_filenames[i];
422a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    const char* dex_location = dex_locations[i];
423a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
4245b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    if (dex_file == NULL) {
42533e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      LOG(WARNING) << "Could not open .dex from file '" << dex_filename << "'\n";
426bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      ++failure_count;
42760f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao    } else {
42860f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao      dex_files.push_back(dex_file);
4295b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    }
4305b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  }
431bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes  return failure_count;
4325b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom}
4335b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
434bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// The primary goal of the watchdog is to prevent stuck build servers
435bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// during development when fatal aborts lead to a cascade of failures
436bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// that result in a deadlock.
437bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromclass WatchDog {
438bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks
439bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#undef CHECK_PTHREAD_CALL
440bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \
441bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  do { \
442bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    int rc = call args; \
443bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    if (rc != 0) { \
444bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      errno = rc; \
445bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      std::string message(# call); \
446bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      message += " failed for "; \
447bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      message += reason; \
448ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom      Fatal(message); \
449bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    } \
450bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  } while (false)
451bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
452bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom public:
45393ba893c20532990a430741e0a97212900094e8cBrian Carlstrom  explicit WatchDog(bool is_watch_dog_enabled) {
454994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    is_watch_dog_enabled_ = is_watch_dog_enabled;
455994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    if (!is_watch_dog_enabled_) {
456bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      return;
457bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    }
458bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    shutting_down_ = false;
459bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    const char* reason = "dex2oat watch dog thread startup";
460bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason);
461bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason);
462bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason);
463bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason);
464bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
465bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
466bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  ~WatchDog() {
467994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    if (!is_watch_dog_enabled_) {
468bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      return;
469bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    }
470bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    const char* reason = "dex2oat watch dog thread shutdown";
471bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
472bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    shutting_down_ = true;
473bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason);
474bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
475bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
476bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason);
477bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
478bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason);
479bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason);
480bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
481bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
482bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom private:
483bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  static void* CallBack(void* arg) {
484bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    WatchDog* self = reinterpret_cast<WatchDog*>(arg);
4851d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    ::art::SetThreadName("dex2oat watch dog");
486bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    self->Wait();
487bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    return NULL;
488bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
489bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
490ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static void Message(char severity, const std::string& message) {
4911d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error
4921d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    //       cases.
493ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    fprintf(stderr, "dex2oat%s %c %d %d %s\n",
494ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            kIsDebugBuild ? "d" : "",
495ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            severity,
496ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            getpid(),
497ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            GetTid(),
498ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            message.c_str());
499ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  }
500ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom
501eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers  static void Warn(const std::string& message) {
502ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    Message('W', message);
503eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers  }
504eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers
505ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static void Fatal(const std::string& message) {
506ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    Message('F', message);
507bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    exit(1);
508bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
509bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
510bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  void Wait() {
511eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers    bool warning = true;
512eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers    CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds);
5131d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    // TODO: tune the multiplier for GC verification, the following is just to make the timeout
5141d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    //       large.
5151d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    int64_t multiplier = gc::kDesiredHeapVerification > gc::kVerifyAllFast ? 100 : 1;
516ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    timespec warning_ts;
5171d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts);
518ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    timespec timeout_ts;
5191d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts);
520bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    const char* reason = "dex2oat watch dog thread waiting";
521bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
522bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    while (!shutting_down_) {
523eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers      int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_,
524eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                                         warning ? &warning_ts
525eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                                                 : &timeout_ts));
526bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      if (rc == ETIMEDOUT) {
527bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom        std::string message(StringPrintf("dex2oat did not finish after %d seconds",
528eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                         warning ? kWatchDogWarningSeconds
529eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                                 : kWatchDogTimeoutSeconds));
530eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers        if (warning) {
531eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers          Warn(message.c_str());
532eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers          warning = false;
533eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers        } else {
534ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom          Fatal(message.c_str());
535eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers        }
536eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers      } else if (rc != 0) {
537bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom        std::string message(StringPrintf("pthread_cond_timedwait failed: %s",
538bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom                                         strerror(errno)));
539ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom        Fatal(message.c_str());
540bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      }
541bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    }
542bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
543bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
544bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
545ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  // When setting timeouts, keep in mind that the build server may not be as fast as your desktop.
546ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom#if ART_USE_PORTABLE_COMPILER
547ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static const unsigned int kWatchDogWarningSeconds =  2 * 60;  // 2 minutes.
548eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers  static const unsigned int kWatchDogTimeoutSeconds = 30 * 60;  // 25 minutes + buffer.
549bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#else
550ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static const unsigned int kWatchDogWarningSeconds =  1 * 60;  // 1 minute.
551ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static const unsigned int kWatchDogTimeoutSeconds =  6 * 60;  // 5 minutes + buffer.
552bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif
553bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
554994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  bool is_watch_dog_enabled_;
555bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  bool shutting_down_;
5561d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases.
557bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_mutex_t mutex_;
558bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_cond_t cond_;
559bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_attr_t attr_;
560bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_t pthread_;
561bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom};
5621c653d545058a1a850fb6d07d74e607fa1ddf48eIan Rogersconst unsigned int WatchDog::kWatchDogWarningSeconds;
5631c653d545058a1a850fb6d07d74e607fa1ddf48eIan Rogersconst unsigned int WatchDog::kWatchDogTimeoutSeconds;
564bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
56572395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic int dex2oat(int argc, char** argv) {
5666f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum  base::TimingLogger timings("compiler", false, false);
5674560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom
5680d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes  InitLogging(argv);
56972395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughes
57069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  // Skip over argv[0].
57169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  argv++;
57269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  argc--;
57369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
57469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  if (argc == 0) {
575cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("no arguments specified");
57669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
57769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
57869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  std::vector<const char*> dex_filenames;
579a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::vector<const char*> dex_locations;
580a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  int zip_fd = -1;
581a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string zip_location;
582e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  std::string oat_filename;
583265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  std::string oat_symbols;
584a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string oat_location;
585a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  int oat_fd = -1;
5868b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  std::string bitcode_filename;
5874922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  const char* image_classes_zip_filename = NULL;
588ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  const char* image_classes_filename = NULL;
589a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string image_filename;
590b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  std::string boot_image_filename;
59169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  uintptr_t image_base = 0;
59234f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom  UniquePtr<std::string> host_prefix;
5933f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  std::string android_root;
5945d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao  std::vector<const char*> runtime_args;
5955c599943f8c347acd84c4d9fda56a9df70649b78Elliott Hughes  int thread_count = sysconf(_SC_NPROCESSORS_CONF);
596c531cefbfb5394413122e9f57d211ba436cff012buzbee#if defined(ART_USE_PORTABLE_COMPILER)
597c531cefbfb5394413122e9f57d211ba436cff012buzbee  CompilerBackend compiler_backend = kPortable;
598c531cefbfb5394413122e9f57d211ba436cff012buzbee#else
599c531cefbfb5394413122e9f57d211ba436cff012buzbee  CompilerBackend compiler_backend = kQuick;
600c531cefbfb5394413122e9f57d211ba436cff012buzbee#endif
6019ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#if defined(__arm__)
60249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  InstructionSet instruction_set = kThumb2;
6039ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__i386__)
6049ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao  InstructionSet instruction_set = kX86;
6059ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__mips__)
6069ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao  InstructionSet instruction_set = kMips;
6079ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#else
6089ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#error "Unsupported architecture"
6099ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#endif
610265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  bool is_host = false;
61167d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes  bool dump_stats = kIsDebugBuild;
61246398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  bool dump_timing = false;
61346398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  bool dump_slow_timing = kIsDebugBuild;
614994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  bool watch_dog_enabled = !kIsTargetBuild;
615161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom
616c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum
61769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  for (int i = 0; i < argc; i++) {
61869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    const StringPiece option(argv[i]);
619a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    bool log_options = false;
620a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    if (log_options) {
621b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom      LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
622b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom    }
62369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    if (option.starts_with("--dex-file=")) {
62469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
625a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    } else if (option.starts_with("--dex-location=")) {
626a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
627a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else if (option.starts_with("--zip-fd=")) {
628a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
629bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes      if (!ParseInt(zip_fd_str, &zip_fd)) {
630cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom        Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
631a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
632a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    } else if (option.starts_with("--zip-location=")) {
633a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      zip_location = option.substr(strlen("--zip-location=")).data();
634a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else if (option.starts_with("--oat-file=")) {
635a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      oat_filename = option.substr(strlen("--oat-file=")).data();
636265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    } else if (option.starts_with("--oat-symbols=")) {
637265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      oat_symbols = option.substr(strlen("--oat-symbols=")).data();
638a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else if (option.starts_with("--oat-fd=")) {
639a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
640bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes      if (!ParseInt(oat_fd_str, &oat_fd)) {
641cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom        Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
642a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
643994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    } else if (option == "--watch-dog") {
644994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom      watch_dog_enabled = true;
645994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    } else if (option == "--no-watch-dog") {
646994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom      watch_dog_enabled = false;
6475523ee070b005576c6f889415205d49ea77cf243Elliott Hughes    } else if (option.starts_with("-j")) {
648b12552a95d68b9e4a567103190074ae47b6a61dcBrian Carlstrom      const char* thread_count_str = option.substr(strlen("-j")).data();
6495523ee070b005576c6f889415205d49ea77cf243Elliott Hughes      if (!ParseInt(thread_count_str, &thread_count)) {
650cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom        Usage("could not parse -j argument '%s' as an integer", thread_count_str);
6515523ee070b005576c6f889415205d49ea77cf243Elliott Hughes      }
652a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    } else if (option.starts_with("--oat-location=")) {
653a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      oat_location = option.substr(strlen("--oat-location=")).data();
6548b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien    } else if (option.starts_with("--bitcode=")) {
6558b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien      bitcode_filename = option.substr(strlen("--bitcode=")).data();
65669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    } else if (option.starts_with("--image=")) {
65769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      image_filename = option.substr(strlen("--image=")).data();
658ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    } else if (option.starts_with("--image-classes=")) {
659ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      image_classes_filename = option.substr(strlen("--image-classes=")).data();
6604922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    } else if (option.starts_with("--image-classes-zip=")) {
6614922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      image_classes_zip_filename = option.substr(strlen("--image-classes-zip=")).data();
66269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    } else if (option.starts_with("--base=")) {
66369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      const char* image_base_str = option.substr(strlen("--base=")).data();
66469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      char* end;
66569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      image_base = strtoul(image_base_str, &end, 16);
66669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      if (end == image_base_str || *end != '\0') {
667cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom        Usage("Failed to parse hexadecimal value for option %s", option.data());
66869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      }
669e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    } else if (option.starts_with("--boot-image=")) {
670b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom      boot_image_filename = option.substr(strlen("--boot-image=")).data();
67158ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom    } else if (option.starts_with("--host-prefix=")) {
67234f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom      host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
6733f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    } else if (option.starts_with("--android-root=")) {
6743f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom      android_root = option.substr(strlen("--android-root=")).data();
67549c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers    } else if (option.starts_with("--instruction-set=")) {
67649c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers      StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
6771f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao      if (instruction_set_str == "arm") {
67849c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        instruction_set = kThumb2;
6791f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao      } else if (instruction_set_str == "mips") {
68049c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        instruction_set = kMips;
6811f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao      } else if (instruction_set_str == "x86") {
68249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        instruction_set = kX86;
68349c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers      }
684c531cefbfb5394413122e9f57d211ba436cff012buzbee    } else if (option.starts_with("--compiler-backend=")) {
685c531cefbfb5394413122e9f57d211ba436cff012buzbee      StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data();
686c531cefbfb5394413122e9f57d211ba436cff012buzbee      if (backend_str == "Quick") {
687c531cefbfb5394413122e9f57d211ba436cff012buzbee        compiler_backend = kQuick;
688c531cefbfb5394413122e9f57d211ba436cff012buzbee      } else if (backend_str == "Portable") {
689c531cefbfb5394413122e9f57d211ba436cff012buzbee        compiler_backend = kPortable;
690c531cefbfb5394413122e9f57d211ba436cff012buzbee      }
691265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    } else if (option == "--host") {
692265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      is_host = true;
6935d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao    } else if (option == "--runtime-arg") {
6945d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao      if (++i >= argc) {
695cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom        Usage("Missing required argument for --runtime-arg");
6965d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao      }
697a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      if (log_options) {
698a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom        LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
699a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
7005d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao      runtime_args.push_back(argv[i]);
70146398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers    } else if (option == "--dump-timing") {
70246398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers      dump_timing = true;
70369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    } else {
704cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom      Usage("unknown argument %s", option.data());
70569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    }
70669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
70769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
708a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (oat_filename.empty() && oat_fd == -1) {
709cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("Output must be supplied with either --oat-file or --oat-fd");
710a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
711a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
712a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (!oat_filename.empty() && oat_fd != -1) {
713cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--oat-file should not be used with --oat-fd");
714a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
715a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
716265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (!oat_symbols.empty() && oat_fd != -1) {
717265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    Usage("--oat-symbols should not be used with --oat-fd");
718265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
719265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
720265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (!oat_symbols.empty() && is_host) {
721265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    Usage("--oat-symbols should not be used with --host");
722a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
723a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
724a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (oat_fd != -1 && !image_filename.empty()) {
725cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--oat-fd should not be used with --image");
726e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  }
727e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
72834f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom  if (host_prefix.get() == NULL) {
729b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
730b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    if (android_product_out != NULL) {
73134f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom        host_prefix.reset(new std::string(android_product_out));
732b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    }
733b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  }
734b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom
7353f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  if (android_root.empty()) {
7363f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    const char* android_root_env_var = getenv("ANDROID_ROOT");
7373f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    if (android_root_env_var == NULL) {
73854d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom      Usage("--android-root unspecified and ANDROID_ROOT not set");
7393f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    }
7403f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    android_root += android_root_env_var;
7413f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  }
7423f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom
743a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  bool image = (!image_filename.empty());
744b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  if (!image && boot_image_filename.empty()) {
74534f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom    if (host_prefix.get() == NULL) {
746a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom      boot_image_filename += GetAndroidRoot();
747a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom    } else {
74834f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom      boot_image_filename += *host_prefix.get();
749a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom      boot_image_filename += "/system";
750a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom    }
751a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom    boot_image_filename += "/framework/boot.art";
752b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  }
753b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  std::string boot_image_option;
754a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (!boot_image_filename.empty()) {
755b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    boot_image_option += "-Ximage:";
756b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    boot_image_option += boot_image_filename;
75769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
75869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
759ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  if (image_classes_filename != NULL && !image) {
760cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--image-classes should only be used with --image");
761ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
762ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
763ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  if (image_classes_filename != NULL && !boot_image_option.empty()) {
764cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--image-classes should not be used with --boot-image");
76578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
76678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
7674922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  if (image_classes_zip_filename != NULL && image_classes_filename == NULL) {
7684922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    Usage("--image-classes-zip should be used with --image-classes");
7694922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  }
7704922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom
771a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (dex_filenames.empty() && zip_fd == -1) {
772cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("Input must be supplied with either --dex-file or --zip-fd");
773a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
774a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
775a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (!dex_filenames.empty() && zip_fd != -1) {
776cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--dex-file should not be used with --zip-fd");
777a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
778a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
779a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (!dex_filenames.empty() && !zip_location.empty()) {
780cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--dex-file should not be used with --zip-location");
781a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
782a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
783a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (dex_locations.empty()) {
784a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    for (size_t i = 0; i < dex_filenames.size(); i++) {
785a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      dex_locations.push_back(dex_filenames[i]);
786a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    }
787a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  } else if (dex_locations.size() != dex_filenames.size()) {
788cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--dex-location arguments do not match --dex-file arguments");
789a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  }
790a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom
791a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (zip_fd != -1 && zip_location.empty()) {
792cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--zip-location should be supplied with --zip-fd");
793a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
794a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
79569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  if (boot_image_option.empty()) {
79669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    if (image_base == 0) {
797cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom      Usage("non-zero --base not specified");
79869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    }
79969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
80069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
801265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  std::string oat_stripped(oat_filename);
802265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  std::string oat_unstripped;
803265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (!oat_symbols.empty()) {
804265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_unstripped += oat_symbols;
805265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  } else {
806265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_unstripped += oat_filename;
807265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
808265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
809994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  // Done with usage checks, enable watchdog if requested
810994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  WatchDog watch_dog(watch_dog_enabled);
811994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom
8126ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom  // Check early that the result of compilation can be written
813a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  UniquePtr<File> oat_file;
814265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  bool create_file = !oat_unstripped.empty();  // as opposed to using open file descriptor
8156cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom  if (create_file) {
8167571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom    oat_file.reset(OS::CreateEmptyFile(oat_unstripped.c_str()));
817a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    if (oat_location.empty()) {
818a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      oat_location = oat_filename;
819a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    }
820a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  } else {
821761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes    oat_file.reset(new File(oat_fd, oat_location));
822761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes    oat_file->DisableAutoClose();
823a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
8246ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom  if (oat_file.get() == NULL) {
8256cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom    PLOG(ERROR) << "Failed to create oat file: " << oat_location;
8266cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom    return EXIT_FAILURE;
8276cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom  }
8286cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom  if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
8296cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom    PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
8306ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom    return EXIT_FAILURE;
831234da578a2d91ed7f2ef47b2ec23fb0033e2746bElliott Hughes  }
832a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
833e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  timings.StartSplit("dex2oat Setup");
834a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  LOG(INFO) << "dex2oat: " << oat_location;
8355e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers
83669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  Runtime::Options options;
8375de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom  options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
838a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::vector<const DexFile*> boot_class_path;
83969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  if (boot_image_option.empty()) {
840bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes    size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
841bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes    if (failure_count > 0) {
842bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      LOG(ERROR) << "Failed to open some dex files: " << failure_count;
843bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      return EXIT_FAILURE;
844bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes    }
845a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    options.push_back(std::make_pair("bootclasspath", &boot_class_path));
84669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  } else {
84769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
84869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
84934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom  if (host_prefix.get() != NULL) {
85034f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom    options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
85158ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  }
8525d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao  for (size_t i = 0; i < runtime_args.size(); i++) {
8535d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao    options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
8545d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao  }
85569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
8567467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea#ifdef ART_SEA_IR_MODE
8577467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea  options.push_back(std::make_pair("-sea_ir", reinterpret_cast<void*>(NULL)));
8587467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea#endif
8597467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea
86000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Dex2Oat* p_dex2oat;
8610177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom  if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count)) {
86200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    LOG(ERROR) << "Failed to create dex2oat";
86300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return EXIT_FAILURE;
86400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
86500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  UniquePtr<Dex2Oat> dex2oat(p_dex2oat);
86600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
86700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // give it away now and then switch to a more managable ScopedObjectAccess.
86800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
8690f40ac31134d9ae0f059d4c448165599dc8459c1Ian Rogers  // If we're doing the image, override the compiler filter to force full compilation. Must be
8700f40ac31134d9ae0f059d4c448165599dc8459c1Ian Rogers  // done ahead of WellKnownClasses::Init that causes verification.
8710f40ac31134d9ae0f059d4c448165599dc8459c1Ian Rogers  if (image && Runtime::Current()->GetCompilerFilter() == Runtime::kInterpretOnly) {
8720f40ac31134d9ae0f059d4c448165599dc8459c1Ian Rogers    Runtime::Current()->SetCompilerFilter(Runtime::kSpeed);
8730f40ac31134d9ae0f059d4c448165599dc8459c1Ian Rogers  }
87400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Whilst we're in native take the opportunity to initialize well known classes.
875ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  WellKnownClasses::Init(Thread::Current()->GetJniEnv());
87600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
87769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
878bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  // If --image-classes was specified, calculate the full list of classes to include in the image
87996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  UniquePtr<CompilerDriver::DescriptorSet> image_classes(NULL);
880ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  if (image_classes_filename != NULL) {
8814922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (image_classes_zip_filename != NULL) {
8824922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      image_classes.reset(dex2oat->ReadImageClassesFromZip(image_classes_zip_filename,
8834922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom                                                           image_classes_filename));
8844922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    } else {
8854922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      image_classes.reset(dex2oat->ReadImageClassesFromFile(image_classes_filename));
8864922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
887ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    if (image_classes.get() == NULL) {
888ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
889ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return EXIT_FAILURE;
89069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    }
89169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
89269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
893a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  std::vector<const DexFile*> dex_files;
894a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (boot_image_option.empty()) {
895a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
896a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  } else {
897a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    if (dex_filenames.empty()) {
898a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
899a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      if (zip_archive.get() == NULL) {
9004922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom        LOG(ERROR) << "Failed to open zip from file descriptor for " << zip_location;
9012e3d1b262af0839380e1d60e86d8b281943ef944Brian Carlstrom        return EXIT_FAILURE;
902a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
903a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
904a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      if (dex_file == NULL) {
905a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom        LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
906a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom        return EXIT_FAILURE;
907a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
908a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      dex_files.push_back(dex_file);
909a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else {
910bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
911bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      if (failure_count > 0) {
912bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes        LOG(ERROR) << "Failed to open some dex files: " << failure_count;
913bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes        return EXIT_FAILURE;
914bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      }
915a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    }
916a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
917a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
918a024a0686c3b0fea13f362bff70d65981e5febc5buzbee  /*
919a024a0686c3b0fea13f362bff70d65981e5febc5buzbee   * If we're not in interpret-only mode, go ahead and compile small applications. Don't
920a024a0686c3b0fea13f362bff70d65981e5febc5buzbee   * bother to check if we're doing the image.
921a024a0686c3b0fea13f362bff70d65981e5febc5buzbee   */
922a024a0686c3b0fea13f362bff70d65981e5febc5buzbee  if (!image && (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly)) {
9238447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    size_t num_methods = 0;
9248447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    for (size_t i = 0; i != dex_files.size(); ++i) {
9258447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum      const DexFile* dex_file = dex_files[i];
9268447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum      CHECK(dex_file != NULL);
9278447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum      num_methods += dex_file->NumMethodIds();
9288447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    }
929a024a0686c3b0fea13f362bff70d65981e5febc5buzbee    if (num_methods <= Runtime::Current()->GetNumDexMethodsThreshold()) {
930a024a0686c3b0fea13f362bff70d65981e5febc5buzbee      Runtime::Current()->SetCompilerFilter(Runtime::kSpeed);
93175a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum      VLOG(compiler) << "Below method threshold, compiling anyways";
9328447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    }
9338447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum  }
934c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum
9351212a022fa5f8ef9585d765b1809521812af882cIan Rogers  UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option,
9361212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  host_prefix.get(),
9373f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                                                  android_root,
938265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                                  is_host,
9391212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  dex_files,
9401212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  oat_file.get(),
9411212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  bitcode_filename,
9421212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  image,
94396391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom                                                                  image_classes,
9441212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  dump_stats,
9454560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                                  timings));
946f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom
947f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom  if (compiler.get() == NULL) {
948a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    LOG(ERROR) << "Failed to create oat file: " << oat_location;
949e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    return EXIT_FAILURE;
950e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  }
951e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
95275a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum  VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location;
953aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
954700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Notes on the interleaving of creating the image and oat file to
955700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // ensure the references between the two are correct.
956700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
957700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Currently we have a memory layout that looks something like this:
958700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
959700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
960700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // | image        |
961700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
962700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // | boot oat     |
963700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
964700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // | alloc spaces |
965700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
966700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
9674560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // There are several constraints on the loading of the image and boot.oat.
968700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
969700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 1. The image is expected to be loaded at an absolute address and
970700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // contains Objects with absolute pointers within the image.
971700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
972700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 2. There are absolute pointers from Methods in the image to their
973700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // code in the oat.
974700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
975700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 3. There are absolute pointers from the code in the oat to Methods
976700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // in the image.
977700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
978700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 4. There are absolute pointers from code in the oat to other code
979700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // in the oat.
980700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
981700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // To get this all correct, we go through several steps.
982700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
983700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 1. We have already created that oat file above with
984700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // CreateOatFile. Originally this was just our own proprietary file
9854560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // but now it is contained within an ELF dynamic object (aka an .so
986700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // file). The Compiler returned by CreateOatFile provides
987700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // PatchInformation for references to oat code and Methods that need
988700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // to be update once we know where the oat file will be located
989700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // after the image.
990700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
991700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 2. We create the image file. It needs to know where the oat file
992700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // will be loaded after itself. Originally when oat file was simply
993700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // memory mapped so we could predict where its contents were based
994700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // on the file size. Now that it is an ELF file, we need to inspect
995700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // the ELF file to understand the in memory segment layout including
996700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // where the oat header is located within. ImageWriter's
997700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // PatchOatCodeAndMethods uses the PatchInformation from the
998700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Compiler to touch up absolute references in the oat file.
999700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
1000700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 3. We fixup the ELF program headers so that dlopen will try to
1001700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // load the .so at the desired location at runtime by offsetting the
1002700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Elf32_Phdr.p_vaddr values by the desired base address.
1003700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
1004265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (image) {
10056f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    timings.NewSplit("dex2oat ImageWriter");
1006265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1007265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    bool image_creation_success = dex2oat->CreateImageFile(image_filename,
1008265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           image_base,
1009265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           oat_unstripped,
1010265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           oat_location,
1011265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           *compiler.get());
1012265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    Thread::Current()->TransitionFromSuspendedToRunnable();
1013265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    if (!image_creation_success) {
1014265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      return EXIT_FAILURE;
1015265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    }
101675a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum    VLOG(compiler) << "Image written successfully: " << image_filename;
1017265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
1018265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
1019265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (is_host) {
102046398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers    if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
10216f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum      LOG(INFO) << Dumpable<base::TimingLogger>(timings);
10224560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom    }
1023265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    return EXIT_SUCCESS;
102469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
102569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
1026265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // If we don't want to strip in place, copy from unstripped location to stripped location.
1027265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // We need to strip after image creation because FixupElf needs to use .strtab.
1028265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (oat_unstripped != oat_stripped) {
10296f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    timings.NewSplit("dex2oat OatFile copy");
1030265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_file.reset();
10317571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom     UniquePtr<File> in(OS::OpenFileForReading(oat_unstripped.c_str()));
10327571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom    UniquePtr<File> out(OS::CreateEmptyFile(oat_stripped.c_str()));
1033265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    size_t buffer_size = 8192;
1034265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]);
1035265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    while (true) {
1036265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size));
1037265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      if (bytes_read <= 0) {
1038265091e581c9f643b37e7966890911f09e223269Brian Carlstrom        break;
1039265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      }
1040265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      bool write_ok = out->WriteFully(buffer.get(), bytes_read);
1041265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      CHECK(write_ok);
1042265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    }
1043265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_file.reset(out.release());
104475a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum    VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped;
1045265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
1046265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
10477fcba11846d3320911aefdba6a20c1192e36fc2eBrian Carlstrom#if ART_USE_PORTABLE_COMPILER  // We currently only generate symbols on Portable
10486f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum  timings.NewSplit("dex2oat ElfStripper");
1049265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // Strip unneeded sections for target
1050265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET);
1051265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  CHECK_EQ(0, seek_actual);
105251c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom  ElfStripper::Strip(oat_file.get());
10536f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum
1054265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
1055ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // We wrote the oat file successfully, and want to keep it.
105675a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum  VLOG(compiler) << "Oat file written successfully (stripped): " << oat_location;
10577934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom#endif  // ART_USE_PORTABLE_COMPILER
10584560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom
10596f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum  timings.EndSplit();
10606f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum
106146398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  if (dump_timing && timings.GetTotalNs() > MsToNs(1000)) {
10626f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    LOG(INFO) << Dumpable<base::TimingLogger>(timings);
10634560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  }
106469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  return EXIT_SUCCESS;
106569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom}
106669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
10674560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom
10687934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
106969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
107069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromint main(int argc, char** argv) {
107169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  return art::dex2oat(argc, argv);
107269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom}
1073