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>
2051db7beb7faaea43d697321520732d1d71288f50Ian Rogers#include <valgrind.h>
2169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
22ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom#include <fstream>
234922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom#include <iostream>
244922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom#include <sstream>
2569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <string>
2669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <vector>
2769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
281aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h"
29e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/stringpiece.h"
30a84395489098e4531619b1cffd1afc282b14510eSameer Abu Asal#include "base/timing_logger.h"
31761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
3269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "class_linker.h"
334f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
347940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "driver/compiler_driver.h"
357940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "elf_fixup.h"
367940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "elf_stripper.h"
371d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
381d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
397940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "image_writer.h"
406f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers#include "leb128.h"
41ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h"
442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
467940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "oat_writer.h"
476d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
485e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers#include "os.h"
4969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "runtime.h"
5000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "ScopedLocalRef.h"
5100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
521f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h"
53700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "vector_output_stream.h"
5400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "well_known_classes.h"
55a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom#include "zip_archive.h"
5669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
5769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromnamespace art {
5869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
59cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageErrorV(const char* fmt, va_list ap) {
60cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  std::string error;
61cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  StringAppendV(&error, fmt, ap);
62cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  LOG(ERROR) << error;
63cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom}
64cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom
65cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageError(const char* fmt, ...) {
66cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_list ap;
67cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_start(ap, fmt);
68cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageErrorV(fmt, ap);
69cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_end(ap);
70cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom}
71cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom
72cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void Usage(const char* fmt, ...) {
73cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_list ap;
74cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_start(ap, fmt);
75cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageErrorV(fmt, ap);
76cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  va_end(ap);
77cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom
78cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("Usage: dex2oat [options]...");
79cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
80cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --dex-file=<dex-file>: specifies a .dex file to compile.");
81cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --dex-file=/system/framework/core.jar");
82cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
83cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
84cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      containing a classes.dex file to compile.");
85cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --zip-fd=5");
86cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
874560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  UsageError("  --zip-location=<zip-location>: specifies a symbolic name for the file");
884560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  UsageError("      corresponding to the file descriptor specified by --zip-fd.");
89cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --zip-location=/system/app/Calculator.apk");
90cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
91265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --oat-file=<file.oat>: specifies the oat output destination via a filename.");
92265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("      Example: --oat-file=/system/framework/boot.oat");
93265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
94265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --oat-fd=<number>: specifies the oat output destination via a file descriptor.");
95cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --oat-file=/system/framework/boot.oat");
96cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
97cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
98cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      to the file descriptor specified by --oat-fd.");
997675e16bcae06c0fe258aad89b3d511037dec399Brian Carlstrom  UsageError("      Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat");
100cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
101265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --oat-symbols=<file.oat>: specifies the oat output destination with full symbols.");
102265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("      Example: --oat-symbols=/symbols/system/framework/boot.oat");
103265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
1048b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  UsageError("  --bitcode=<file.bc>: specifies the optional bitcode filename.");
1058b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  UsageError("      Example: --bitcode=/system/framework/boot.bc");
1068b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  UsageError("");
107cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --image=<file.art>: specifies the output image filename.");
108cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --image=/system/framework/boot.art");
109cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
110cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --image-classes=<classname-file>: specifies classes to include in an image.");
111cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --image=frameworks/base/preloaded-classes");
112cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
113cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --base=<hex-address>: specifies the base address when creating a boot image.");
114cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --base=0x50000000");
115cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
116cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --boot-image=<file.art>: provide the image file for the boot class path.");
117cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --boot-image=/system/framework/boot.art");
118cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Default: <host-prefix>/system/framework/boot.art");
119cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
120265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --host-prefix=<path>: used to translate host paths to target paths during");
121cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      cross compilation.");
122cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --host-prefix=out/target/product/crespo");
123cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Default: $ANDROID_PRODUCT_OUT");
124cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
1253f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("  --android-root=<path>: used to locate libraries for portable linking.");
1263f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("      Example: --android-root=out/host/linux-x86");
1273f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("      Default: $ANDROID_ROOT");
1283f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  UsageError("");
1291f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao  UsageError("  --instruction-set=(arm|mips|x86): compile for a particular instruction");
13049c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  UsageError("      set.");
1311f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao  UsageError("      Example: --instruction-set=x86");
1321f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao  UsageError("      Default: arm");
13349c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  UsageError("");
134c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("  --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend");
135c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("      set.");
136c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("      Example: --instruction-set=Portable");
137c531cefbfb5394413122e9f57d211ba436cff012buzbee  UsageError("      Default: Quick");
138265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
139265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("  --host: used with Portable backend to link against host runtime libraries");
140265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  UsageError("");
14146398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  UsageError("  --dump-timing: display a breakdown of where time was spent");
14246398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  UsageError("");
143cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("  --runtime-arg <argument>: used to specify various arguments for the runtime,");
144cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      such as initial heap size, maximum heap size, and verbose output.");
145cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Use a separate --runtime-arg switch for each argument.");
146cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("      Example: --runtime-arg -Xms256m");
147cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  UsageError("");
148cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom  std::cerr << "See log for usage error information\n";
14969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  exit(EXIT_FAILURE);
15069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom}
15169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
152ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstromclass Dex2Oat {
153ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom public:
1544560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  static bool Create(Dex2Oat** p_dex2oat,
1554560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     Runtime::Options& options,
1564560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     CompilerBackend compiler_backend,
1574560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     InstructionSet instruction_set,
1584560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                     size_t thread_count)
159b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
16000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (!CreateRuntime(options, instruction_set)) {
16100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      *p_dex2oat = NULL;
16200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return false;
163254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson    }
1640177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom    *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count);
16500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return true;
166ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
167ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
168ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  ~Dex2Oat() {
169ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    delete runtime_;
17075a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum    VLOG(compiler) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_)
1714560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom              << " (threads: " << thread_count_ << ")";
172ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
173ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
174c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum
1754560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
1764922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  CompilerDriver::DescriptorSet* ReadImageClassesFromFile(const char* image_classes_filename) {
1774560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom    UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename,
1784560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                                  std::ifstream::in));
179ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    if (image_classes_file.get() == NULL) {
180ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
181ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return NULL;
182ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
1834922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<CompilerDriver::DescriptorSet> result(ReadImageClasses(*image_classes_file.get()));
1844922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    image_classes_file->close();
1854922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    return result.release();
1864922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  }
187ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
1884922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  CompilerDriver::DescriptorSet* ReadImageClasses(std::istream& image_classes_stream) {
18996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom    UniquePtr<CompilerDriver::DescriptorSet> image_classes(new CompilerDriver::DescriptorSet);
1904922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    while (image_classes_stream.good()) {
191ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      std::string dot;
1924922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      std::getline(image_classes_stream, dot);
193f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      if (StartsWith(dot, "#") || dot.empty()) {
194ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        continue;
195ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
196955724179c6c739524f610023287f56b24dc31deElliott Hughes      std::string descriptor(DotToDescriptor(dot.c_str()));
19796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      image_classes->insert(descriptor);
198ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
199ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    return image_classes.release();
200254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson  }
201254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson
2024560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
2034560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  CompilerDriver::DescriptorSet* ReadImageClassesFromZip(const std::string& zip_filename,
2044560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                         const char* image_classes_filename) {
2054922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename));
2064922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (zip_archive.get() == NULL) {
2074922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      LOG(ERROR) << "Failed to open zip file " << zip_filename;
2084922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      return NULL;
2094922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
2104922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename));
2114922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (zip_entry.get() == NULL) {
2124922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      LOG(ERROR) << "Failed to find " << image_classes_filename << " within " << zip_filename;
2134922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      return NULL;
2144922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
2154922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    UniquePtr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(image_classes_filename));
2164922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (image_classes_file.get() == NULL) {
2174922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      LOG(ERROR) << "Failed to extract " << image_classes_filename << " from " << zip_filename;
2184922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      return NULL;
2194922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
2204922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()),
2214922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom                                           image_classes_file->Size());
2224922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    std::istringstream image_classes_stream(image_classes_string);
2234922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    return ReadImageClasses(image_classes_stream);
2244922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  }
2254922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom
2261212a022fa5f8ef9585d765b1809521812af882cIan Rogers  const CompilerDriver* CreateOatFile(const std::string& boot_image_option,
2273f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::string* host_prefix,
2283f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::string& android_root,
2293f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      bool is_host,
2303f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::vector<const DexFile*>& dex_files,
2313f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      File* oat_file,
2323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      const std::string& bitcode_filename,
2333f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      bool image,
23496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom                                      UniquePtr<CompilerDriver::DescriptorSet>& image_classes,
2353f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                      bool dump_stats,
2363f3d22c8fc89d754172858d1770f16916b407d8bIan Rogers                                      base::TimingLogger& timings) {
237ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    // SirtRef and ClassLoader creation needs to come after Runtime::Create
23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    jobject class_loader = NULL;
2393f3d22c8fc89d754172858d1770f16916b407d8bIan Rogers    Thread* self = Thread::Current();
240ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    if (!boot_image_option.empty()) {
241ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
242ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      std::vector<const DexFile*> class_path_files(dex_files);
243a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
2443f3d22c8fc89d754172858d1770f16916b407d8bIan Rogers      ScopedObjectAccess soa(self);
245ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      for (size_t i = 0; i < class_path_files.size(); i++) {
246ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        class_linker->RegisterDexFile(*class_path_files[i]);
247ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
24800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      ScopedLocalRef<jobject> class_loader_local(soa.Env(),
25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers          soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
25100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
25200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
253254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson    }
254ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
2551212a022fa5f8ef9585d765b1809521812af882cIan Rogers    UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_,
2561212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                        instruction_set_,
2571212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                        image,
25896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom                                                        image_classes.release(),
2591212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                        thread_count_,
2604560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                        dump_stats));
2618b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien
262c928de90ad22ecdf83c18a07008409595f13d3b1Ian Rogers    if (compiler_backend_ == kPortable) {
2631212a022fa5f8ef9585d765b1809521812af882cIan Rogers      driver->SetBitcodeFileName(bitcode_filename);
264c531cefbfb5394413122e9f57d211ba436cff012buzbee    }
2658b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien
2664560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom    driver->CompileAll(class_loader, dex_files, timings);
26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2686f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    timings.NewSplit("dex2oat OatWriter");
26981f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom    std::string image_file_location;
27028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    uint32_t image_file_location_oat_checksum = 0;
271700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    uint32_t image_file_location_oat_data_begin = 0;
2720aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao    if (!driver->IsImage()) {
2731d54e73444e017d3a65234e0f193846f3e27472bIan Rogers      gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
27428db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom      image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
27539ebcb800aabedd0ffa6aa4aeac8aa4194c66e61Ian Rogers      image_file_location_oat_data_begin =
27639ebcb800aabedd0ffa6aa4aeac8aa4194c66e61Ian Rogers          reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin());
27781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom      image_file_location = image_space->GetImageFilename();
27834f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom      if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
27934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom        image_file_location = image_file_location.substr(host_prefix->size());
28081f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom      }
28181f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom    }
28281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
283c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom    OatWriter oat_writer(dex_files,
284c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         image_file_location_oat_checksum,
285c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         image_file_location_oat_data_begin,
286c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         image_file_location,
287c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom                         driver.get());
288700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
289c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom    if (!driver->WriteElf(android_root, is_host, dex_files, oat_writer, oat_file)) {
290700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
291700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      return NULL;
292700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
293700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
2941212a022fa5f8ef9585d765b1809521812af882cIan Rogers    return driver.release();
295254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson  }
296ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
297a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  bool CreateImageFile(const std::string& image_filename,
298ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom                       uintptr_t image_base,
299ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom                       const std::string& oat_filename,
300f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom                       const std::string& oat_location,
3011212a022fa5f8ef9585d765b1809521812af882cIan Rogers                       const CompilerDriver& compiler)
302b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      LOCKS_EXCLUDED(Locks::mutator_lock_) {
303700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    uintptr_t oat_data_begin;
304700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    {
305700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      // ImageWriter is scoped so it can free memory before doing FixupElf
30696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      ImageWriter image_writer(compiler);
30796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom      if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
308700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom        LOG(ERROR) << "Failed to create image file " << image_filename;
309700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom        return false;
310700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      }
311700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      oat_data_begin = image_writer.GetOatDataBegin();
312700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
313700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
3147571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom    UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
315700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    if (oat_file.get() == NULL) {
316700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      PLOG(ERROR) << "Failed to open ELF file: " << oat_filename;
317700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      return false;
318700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    }
31951c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom    if (!ElfFixup::Fixup(oat_file.get(), oat_data_begin)) {
320700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom      LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath();
321ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return false;
322ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
323ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    return true;
324ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
325ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
326ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom private:
3274560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  explicit Dex2Oat(Runtime* runtime,
3284560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                   CompilerBackend compiler_backend,
3294560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                   InstructionSet instruction_set,
3300177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom                   size_t thread_count)
331c531cefbfb5394413122e9f57d211ba436cff012buzbee      : compiler_backend_(compiler_backend),
332c531cefbfb5394413122e9f57d211ba436cff012buzbee        instruction_set_(instruction_set),
33349c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        runtime_(runtime),
334de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes        thread_count_(thread_count),
335de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes        start_ns_(NanoTime()) {
336bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  }
337ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
33800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
339b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
34000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (!Runtime::Create(options, false)) {
341ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      LOG(ERROR) << "Failed to create runtime";
34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return false;
343ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    Runtime* runtime = Runtime::Current();
345ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    // if we loaded an existing image, we will reuse values from the image roots.
3461984651929744dd603fd082e23eacd877b9bc177Ian Rogers    if (!runtime->HasResolutionMethod()) {
3471984651929744dd603fd082e23eacd877b9bc177Ian Rogers      runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
3481984651929744dd603fd082e23eacd877b9bc177Ian Rogers    }
349ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
350ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
351ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (!runtime->HasCalleeSaveMethod(type)) {
35249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
353ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
354ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
3551984651929744dd603fd082e23eacd877b9bc177Ian Rogers    runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
35600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return true;
357ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
358ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
359ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // Appends to dex_files any elements of class_path that it doesn't already
360ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // contain. This will open those dex files as necessary.
3614560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  static void OpenClassPathFiles(const std::string& class_path,
3624560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                 std::vector<const DexFile*>& dex_files) {
363ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    std::vector<std::string> parsed;
364ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    Split(class_path, ':', parsed);
36533e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers    // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained.
36633e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers    ScopedObjectAccess soa(Thread::Current());
367ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    for (size_t i = 0; i < parsed.size(); ++i) {
368ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (DexFilesContains(dex_files, parsed[i])) {
369ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        continue;
370ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
371a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
372ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (dex_file == NULL) {
373ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        LOG(WARNING) << "Failed to open dex file " << parsed[i];
374ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      } else {
375ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        dex_files.push_back(dex_file);
376ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
377ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
378ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
379ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
380ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // Returns true if dex_files has a dex with the named location.
3814560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  static bool DexFilesContains(const std::vector<const DexFile*>& dex_files,
3824560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                               const std::string& location) {
383ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    for (size_t i = 0; i < dex_files.size(); ++i) {
384ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (dex_files[i]->GetLocation() == location) {
385ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        return true;
386ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
387ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
388ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    return false;
389ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
390ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
391c531cefbfb5394413122e9f57d211ba436cff012buzbee  const CompilerBackend compiler_backend_;
392c531cefbfb5394413122e9f57d211ba436cff012buzbee
39349c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  const InstructionSet instruction_set_;
394ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
395bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  Runtime* runtime_;
3965523ee070b005576c6f889415205d49ea77cf243Elliott Hughes  size_t thread_count_;
397bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  uint64_t start_ns_;
398bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes
399ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
400ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom};
401254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson
40272395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic bool ParseInt(const char* in, int* out) {
403a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  char* end;
404a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  int result = strtol(in, &end, 10);
405a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (in == end || *end != '\0') {
406a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    return false;
407a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
408a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  *out = result;
409a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  return true;
410a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom}
411a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
412bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughesstatic size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
413bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes                           const std::vector<const char*>& dex_locations,
414bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes                           std::vector<const DexFile*>& dex_files) {
415bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes  size_t failure_count = 0;
4165b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  for (size_t i = 0; i < dex_filenames.size(); i++) {
4175b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    const char* dex_filename = dex_filenames[i];
418a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    const char* dex_location = dex_locations[i];
419a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
4205b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    if (dex_file == NULL) {
421e0948e13d5a4552e6a2728087573c07961e4a4f9Brian Carlstrom      LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "'\n";
422bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      ++failure_count;
42360f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao    } else {
42460f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao      dex_files.push_back(dex_file);
4255b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    }
4265b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom  }
427bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes  return failure_count;
4285b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom}
4295b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
430bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// The primary goal of the watchdog is to prevent stuck build servers
431bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// during development when fatal aborts lead to a cascade of failures
432bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// that result in a deadlock.
433bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromclass WatchDog {
434bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks
435bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#undef CHECK_PTHREAD_CALL
436bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \
437bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  do { \
438bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    int rc = call args; \
439bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    if (rc != 0) { \
440bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      errno = rc; \
441bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      std::string message(# call); \
442bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      message += " failed for "; \
443bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      message += reason; \
444ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom      Fatal(message); \
445bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    } \
446bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  } while (false)
447bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
448bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom public:
44993ba893c20532990a430741e0a97212900094e8cBrian Carlstrom  explicit WatchDog(bool is_watch_dog_enabled) {
450994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    is_watch_dog_enabled_ = is_watch_dog_enabled;
451994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    if (!is_watch_dog_enabled_) {
452bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      return;
453bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    }
454bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    shutting_down_ = false;
455bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    const char* reason = "dex2oat watch dog thread startup";
456bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason);
457bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason);
458bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason);
459bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason);
460bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
461bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
462bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  ~WatchDog() {
463994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    if (!is_watch_dog_enabled_) {
464bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      return;
465bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    }
466bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    const char* reason = "dex2oat watch dog thread shutdown";
467bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
468bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    shutting_down_ = true;
469bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason);
470bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
471bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
472bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason);
473bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
474bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason);
475bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason);
476bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
477bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
478bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom private:
479bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  static void* CallBack(void* arg) {
480bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    WatchDog* self = reinterpret_cast<WatchDog*>(arg);
4811d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    ::art::SetThreadName("dex2oat watch dog");
482bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    self->Wait();
483bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    return NULL;
484bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
485bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
486ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static void Message(char severity, const std::string& message) {
4871d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error
4881d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    //       cases.
489ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    fprintf(stderr, "dex2oat%s %c %d %d %s\n",
490ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            kIsDebugBuild ? "d" : "",
491ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            severity,
492ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            getpid(),
493ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            GetTid(),
494ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom            message.c_str());
495ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  }
496ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom
497eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers  static void Warn(const std::string& message) {
498ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    Message('W', message);
499eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers  }
500eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers
501ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static void Fatal(const std::string& message) {
502ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    Message('F', message);
503bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    exit(1);
504bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
505bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
506bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  void Wait() {
507eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers    bool warning = true;
508eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers    CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds);
5091d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    // TODO: tune the multiplier for GC verification, the following is just to make the timeout
5101d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    //       large.
5111d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    int64_t multiplier = gc::kDesiredHeapVerification > gc::kVerifyAllFast ? 100 : 1;
512ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    timespec warning_ts;
5131d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts);
514ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom    timespec timeout_ts;
5151d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts);
516bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    const char* reason = "dex2oat watch dog thread waiting";
517bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
518bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    while (!shutting_down_) {
519eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers      int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_,
520eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                                         warning ? &warning_ts
521eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                                                 : &timeout_ts));
522bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      if (rc == ETIMEDOUT) {
523bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom        std::string message(StringPrintf("dex2oat did not finish after %d seconds",
524eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                         warning ? kWatchDogWarningSeconds
525eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers                                                 : kWatchDogTimeoutSeconds));
526eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers        if (warning) {
527eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers          Warn(message.c_str());
528eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers          warning = false;
529eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers        } else {
530ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom          Fatal(message.c_str());
531eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers        }
532eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers      } else if (rc != 0) {
533bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom        std::string message(StringPrintf("pthread_cond_timedwait failed: %s",
534bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom                                         strerror(errno)));
535ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom        Fatal(message.c_str());
536bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom      }
537bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    }
538bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
539bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  }
540bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
541ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  // When setting timeouts, keep in mind that the build server may not be as fast as your desktop.
542ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom#if ART_USE_PORTABLE_COMPILER
543ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static const unsigned int kWatchDogWarningSeconds =  2 * 60;  // 2 minutes.
544eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers  static const unsigned int kWatchDogTimeoutSeconds = 30 * 60;  // 25 minutes + buffer.
545bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#else
546ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static const unsigned int kWatchDogWarningSeconds =  1 * 60;  // 1 minute.
547ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom  static const unsigned int kWatchDogTimeoutSeconds =  6 * 60;  // 5 minutes + buffer.
548bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif
549bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
550994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  bool is_watch_dog_enabled_;
551bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  bool shutting_down_;
5521d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases.
553bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_mutex_t mutex_;
554bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_cond_t cond_;
555bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_attr_t attr_;
556bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom  pthread_t pthread_;
557bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom};
5581c653d545058a1a850fb6d07d74e607fa1ddf48eIan Rogersconst unsigned int WatchDog::kWatchDogWarningSeconds;
5591c653d545058a1a850fb6d07d74e607fa1ddf48eIan Rogersconst unsigned int WatchDog::kWatchDogTimeoutSeconds;
560bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
56172395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic int dex2oat(int argc, char** argv) {
5626f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum  base::TimingLogger timings("compiler", false, false);
5634560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom
5640d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes  InitLogging(argv);
56572395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughes
56669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  // Skip over argv[0].
56769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  argv++;
56869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  argc--;
56969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
57069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  if (argc == 0) {
571e0948e13d5a4552e6a2728087573c07961e4a4f9Brian Carlstrom    Usage("No arguments specified");
57269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
57369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
57469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  std::vector<const char*> dex_filenames;
575a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::vector<const char*> dex_locations;
576a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  int zip_fd = -1;
577a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string zip_location;
578e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  std::string oat_filename;
579265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  std::string oat_symbols;
580a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string oat_location;
581a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  int oat_fd = -1;
5828b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien  std::string bitcode_filename;
5834922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  const char* image_classes_zip_filename = NULL;
584ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  const char* image_classes_filename = NULL;
585a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::string image_filename;
586b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  std::string boot_image_filename;
58769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  uintptr_t image_base = 0;
58834f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom  UniquePtr<std::string> host_prefix;
5893f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  std::string android_root;
5905d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao  std::vector<const char*> runtime_args;
5915c599943f8c347acd84c4d9fda56a9df70649b78Elliott Hughes  int thread_count = sysconf(_SC_NPROCESSORS_CONF);
592c531cefbfb5394413122e9f57d211ba436cff012buzbee#if defined(ART_USE_PORTABLE_COMPILER)
593c531cefbfb5394413122e9f57d211ba436cff012buzbee  CompilerBackend compiler_backend = kPortable;
594c531cefbfb5394413122e9f57d211ba436cff012buzbee#else
595c531cefbfb5394413122e9f57d211ba436cff012buzbee  CompilerBackend compiler_backend = kQuick;
596c531cefbfb5394413122e9f57d211ba436cff012buzbee#endif
5979ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#if defined(__arm__)
59849c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers  InstructionSet instruction_set = kThumb2;
5999ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__i386__)
6009ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao  InstructionSet instruction_set = kX86;
6019ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__mips__)
6029ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao  InstructionSet instruction_set = kMips;
6039ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#else
6049ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#error "Unsupported architecture"
6059ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#endif
606265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  bool is_host = false;
60767d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes  bool dump_stats = kIsDebugBuild;
60846398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  bool dump_timing = false;
60946398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers  bool dump_slow_timing = kIsDebugBuild;
610994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  bool watch_dog_enabled = !kIsTargetBuild;
611161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom
612c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum
61369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  for (int i = 0; i < argc; i++) {
61469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    const StringPiece option(argv[i]);
615a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    bool log_options = false;
616a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    if (log_options) {
617b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom      LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
618b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom    }
61969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    if (option.starts_with("--dex-file=")) {
62069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
621a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    } else if (option.starts_with("--dex-location=")) {
622a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
623a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else if (option.starts_with("--zip-fd=")) {
624a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
625bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes      if (!ParseInt(zip_fd_str, &zip_fd)) {
626e0948e13d5a4552e6a2728087573c07961e4a4f9Brian Carlstrom        Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str);
627a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
628a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    } else if (option.starts_with("--zip-location=")) {
629a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      zip_location = option.substr(strlen("--zip-location=")).data();
630a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else if (option.starts_with("--oat-file=")) {
631a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      oat_filename = option.substr(strlen("--oat-file=")).data();
632265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    } else if (option.starts_with("--oat-symbols=")) {
633265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      oat_symbols = option.substr(strlen("--oat-symbols=")).data();
634a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else if (option.starts_with("--oat-fd=")) {
635a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
636bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes      if (!ParseInt(oat_fd_str, &oat_fd)) {
637e0948e13d5a4552e6a2728087573c07961e4a4f9Brian Carlstrom        Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str);
638a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
639994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    } else if (option == "--watch-dog") {
640994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom      watch_dog_enabled = true;
641994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom    } else if (option == "--no-watch-dog") {
642994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom      watch_dog_enabled = false;
6435523ee070b005576c6f889415205d49ea77cf243Elliott Hughes    } else if (option.starts_with("-j")) {
644b12552a95d68b9e4a567103190074ae47b6a61dcBrian Carlstrom      const char* thread_count_str = option.substr(strlen("-j")).data();
6455523ee070b005576c6f889415205d49ea77cf243Elliott Hughes      if (!ParseInt(thread_count_str, &thread_count)) {
646e0948e13d5a4552e6a2728087573c07961e4a4f9Brian Carlstrom        Usage("Failed to parse -j argument '%s' as an integer", thread_count_str);
6475523ee070b005576c6f889415205d49ea77cf243Elliott Hughes      }
648a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    } else if (option.starts_with("--oat-location=")) {
649a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      oat_location = option.substr(strlen("--oat-location=")).data();
6508b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien    } else if (option.starts_with("--bitcode=")) {
6518b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien      bitcode_filename = option.substr(strlen("--bitcode=")).data();
65269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    } else if (option.starts_with("--image=")) {
65369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      image_filename = option.substr(strlen("--image=")).data();
654ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    } else if (option.starts_with("--image-classes=")) {
655ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      image_classes_filename = option.substr(strlen("--image-classes=")).data();
6564922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    } else if (option.starts_with("--image-classes-zip=")) {
6574922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      image_classes_zip_filename = option.substr(strlen("--image-classes-zip=")).data();
65869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    } else if (option.starts_with("--base=")) {
65969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      const char* image_base_str = option.substr(strlen("--base=")).data();
66069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      char* end;
66169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      image_base = strtoul(image_base_str, &end, 16);
66269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      if (end == image_base_str || *end != '\0') {
663cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom        Usage("Failed to parse hexadecimal value for option %s", option.data());
66469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      }
665e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    } else if (option.starts_with("--boot-image=")) {
666b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom      boot_image_filename = option.substr(strlen("--boot-image=")).data();
66758ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom    } else if (option.starts_with("--host-prefix=")) {
66834f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom      host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
6693f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    } else if (option.starts_with("--android-root=")) {
6703f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom      android_root = option.substr(strlen("--android-root=")).data();
67149c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers    } else if (option.starts_with("--instruction-set=")) {
67249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers      StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
6731f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao      if (instruction_set_str == "arm") {
67449c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        instruction_set = kThumb2;
6751f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao      } else if (instruction_set_str == "mips") {
67649c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        instruction_set = kMips;
6771f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao      } else if (instruction_set_str == "x86") {
67849c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers        instruction_set = kX86;
67949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers      }
680c531cefbfb5394413122e9f57d211ba436cff012buzbee    } else if (option.starts_with("--compiler-backend=")) {
681c531cefbfb5394413122e9f57d211ba436cff012buzbee      StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data();
682c531cefbfb5394413122e9f57d211ba436cff012buzbee      if (backend_str == "Quick") {
683c531cefbfb5394413122e9f57d211ba436cff012buzbee        compiler_backend = kQuick;
684c531cefbfb5394413122e9f57d211ba436cff012buzbee      } else if (backend_str == "Portable") {
685c531cefbfb5394413122e9f57d211ba436cff012buzbee        compiler_backend = kPortable;
686c531cefbfb5394413122e9f57d211ba436cff012buzbee      }
687265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    } else if (option == "--host") {
688265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      is_host = true;
6895d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao    } else if (option == "--runtime-arg") {
6905d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao      if (++i >= argc) {
691cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom        Usage("Missing required argument for --runtime-arg");
6925d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao      }
693a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      if (log_options) {
694a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom        LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
695a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
6965d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao      runtime_args.push_back(argv[i]);
69746398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers    } else if (option == "--dump-timing") {
69846398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers      dump_timing = true;
69969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    } else {
700e0948e13d5a4552e6a2728087573c07961e4a4f9Brian Carlstrom      Usage("Unknown argument %s", option.data());
70169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    }
70269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
70369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
704a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (oat_filename.empty() && oat_fd == -1) {
705cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("Output must be supplied with either --oat-file or --oat-fd");
706a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
707a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
708a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (!oat_filename.empty() && oat_fd != -1) {
709cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--oat-file should not be used with --oat-fd");
710a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
711a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
712265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (!oat_symbols.empty() && oat_fd != -1) {
713265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    Usage("--oat-symbols should not be used with --oat-fd");
714265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
715265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
716265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (!oat_symbols.empty() && is_host) {
717265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    Usage("--oat-symbols should not be used with --host");
718a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
719a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
720a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (oat_fd != -1 && !image_filename.empty()) {
721cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--oat-fd should not be used with --image");
722e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  }
723e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
72434f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom  if (host_prefix.get() == NULL) {
725b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
726b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    if (android_product_out != NULL) {
72734f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom        host_prefix.reset(new std::string(android_product_out));
728b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    }
729b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  }
730b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom
7313f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  if (android_root.empty()) {
7323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    const char* android_root_env_var = getenv("ANDROID_ROOT");
7333f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    if (android_root_env_var == NULL) {
73454d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom      Usage("--android-root unspecified and ANDROID_ROOT not set");
7353f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    }
7363f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom    android_root += android_root_env_var;
7373f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom  }
7383f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom
739a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  bool image = (!image_filename.empty());
740b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  if (!image && boot_image_filename.empty()) {
74134f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom    if (host_prefix.get() == NULL) {
742a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom      boot_image_filename += GetAndroidRoot();
743a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom    } else {
74434f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom      boot_image_filename += *host_prefix.get();
745a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom      boot_image_filename += "/system";
746a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom    }
747a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom    boot_image_filename += "/framework/boot.art";
748b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  }
749b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom  std::string boot_image_option;
750a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (!boot_image_filename.empty()) {
751b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    boot_image_option += "-Ximage:";
752b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom    boot_image_option += boot_image_filename;
75369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
75469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
755ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  if (image_classes_filename != NULL && !image) {
756cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--image-classes should only be used with --image");
757ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  }
758ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
759ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  if (image_classes_filename != NULL && !boot_image_option.empty()) {
760cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--image-classes should not be used with --boot-image");
76178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
76278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
7634922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  if (image_classes_zip_filename != NULL && image_classes_filename == NULL) {
7644922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    Usage("--image-classes-zip should be used with --image-classes");
7654922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom  }
7664922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom
767a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (dex_filenames.empty() && zip_fd == -1) {
768cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("Input must be supplied with either --dex-file or --zip-fd");
769a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
770a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
771a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (!dex_filenames.empty() && zip_fd != -1) {
772cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--dex-file should not be used with --zip-fd");
773a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
774a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
775a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (!dex_filenames.empty() && !zip_location.empty()) {
776cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--dex-file should not be used with --zip-location");
777a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
778a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
779a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (dex_locations.empty()) {
780a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    for (size_t i = 0; i < dex_filenames.size(); i++) {
781a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      dex_locations.push_back(dex_filenames[i]);
782a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    }
783a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  } else if (dex_locations.size() != dex_filenames.size()) {
784cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--dex-location arguments do not match --dex-file arguments");
785a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  }
786a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom
787a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (zip_fd != -1 && zip_location.empty()) {
788cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom    Usage("--zip-location should be supplied with --zip-fd");
789a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
790a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
79169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  if (boot_image_option.empty()) {
79269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    if (image_base == 0) {
793e0948e13d5a4552e6a2728087573c07961e4a4f9Brian Carlstrom      Usage("Non-zero --base not specified");
79469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    }
79569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
79669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
797265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  std::string oat_stripped(oat_filename);
798265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  std::string oat_unstripped;
799265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (!oat_symbols.empty()) {
800265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_unstripped += oat_symbols;
801265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  } else {
802265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_unstripped += oat_filename;
803265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
804265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
805994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  // Done with usage checks, enable watchdog if requested
806994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom  WatchDog watch_dog(watch_dog_enabled);
807994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom
8086ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom  // Check early that the result of compilation can be written
809a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  UniquePtr<File> oat_file;
810265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  bool create_file = !oat_unstripped.empty();  // as opposed to using open file descriptor
8116cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom  if (create_file) {
8127571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom    oat_file.reset(OS::CreateEmptyFile(oat_unstripped.c_str()));
813a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    if (oat_location.empty()) {
814a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      oat_location = oat_filename;
815a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    }
816a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  } else {
817761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes    oat_file.reset(new File(oat_fd, oat_location));
818761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes    oat_file->DisableAutoClose();
819a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
8206ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom  if (oat_file.get() == NULL) {
8216cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom    PLOG(ERROR) << "Failed to create oat file: " << oat_location;
8226cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom    return EXIT_FAILURE;
8236cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom  }
8246cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom  if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
8256cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom    PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
8266ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom    return EXIT_FAILURE;
827234da578a2d91ed7f2ef47b2ec23fb0033e2746bElliott Hughes  }
828a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
829e6bb3b2ce5a69c31c2adfc7eb2705633b7f966ebIan Rogers  timings.StartSplit("dex2oat Setup");
830a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  LOG(INFO) << "dex2oat: " << oat_location;
8315e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers
832f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers  if (image) {
833f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers    bool has_compiler_filter = false;
834f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers    for (const char* r : runtime_args) {
835f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers      if (strncmp(r, "-compiler-filter:", 17) == 0) {
836f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers        has_compiler_filter = true;
837f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers        break;
838f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers      }
839f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers    }
840f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers    if (!has_compiler_filter) {
841f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers      runtime_args.push_back("-compiler-filter:everything");
842f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers    }
843f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers  }
844f30f6daf33a9671ff756befb673482d7f3717a18Ian Rogers
84569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  Runtime::Options options;
8465de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom  options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
847a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  std::vector<const DexFile*> boot_class_path;
84869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  if (boot_image_option.empty()) {
849bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes    size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
850bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes    if (failure_count > 0) {
851bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      LOG(ERROR) << "Failed to open some dex files: " << failure_count;
852bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      return EXIT_FAILURE;
853bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes    }
854a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    options.push_back(std::make_pair("bootclasspath", &boot_class_path));
85569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  } else {
85669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
85769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
85834f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom  if (host_prefix.get() != NULL) {
85934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom    options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
86058ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  }
8615d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao  for (size_t i = 0; i < runtime_args.size(); i++) {
8625d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao    options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
8635d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao  }
86469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
8657467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea#ifdef ART_SEA_IR_MODE
8667467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea  options.push_back(std::make_pair("-sea_ir", reinterpret_cast<void*>(NULL)));
8677467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea#endif
8687467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea
86900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Dex2Oat* p_dex2oat;
8700177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom  if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count)) {
87100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    LOG(ERROR) << "Failed to create dex2oat";
87200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return EXIT_FAILURE;
87300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
87400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  UniquePtr<Dex2Oat> dex2oat(p_dex2oat);
87500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
8763f3d22c8fc89d754172858d1770f16916b407d8bIan Rogers  // give it away now so that we don't starve GC.
8773f3d22c8fc89d754172858d1770f16916b407d8bIan Rogers  Thread* self = Thread::Current();
8783f3d22c8fc89d754172858d1770f16916b407d8bIan Rogers  self->TransitionFromRunnableToSuspended(kNative);
8790f40ac31134d9ae0f059d4c448165599dc8459c1Ian Rogers  // If we're doing the image, override the compiler filter to force full compilation. Must be
880fe9ca4028f379688ecba6132ac3738171176b3e4buzbee  // done ahead of WellKnownClasses::Init that causes verification.  Note: doesn't force
881fe9ca4028f379688ecba6132ac3738171176b3e4buzbee  // compilation of class initializers.
88200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Whilst we're in native take the opportunity to initialize well known classes.
8833f3d22c8fc89d754172858d1770f16916b407d8bIan Rogers  WellKnownClasses::Init(self->GetJniEnv());
88469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
885bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes  // If --image-classes was specified, calculate the full list of classes to include in the image
88696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom  UniquePtr<CompilerDriver::DescriptorSet> image_classes(NULL);
887ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  if (image_classes_filename != NULL) {
8884922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    if (image_classes_zip_filename != NULL) {
8894922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      image_classes.reset(dex2oat->ReadImageClassesFromZip(image_classes_zip_filename,
8904922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom                                                           image_classes_filename));
8914922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    } else {
8924922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom      image_classes.reset(dex2oat->ReadImageClassesFromFile(image_classes_filename));
8934922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom    }
894ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    if (image_classes.get() == NULL) {
895ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
896ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return EXIT_FAILURE;
89769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    }
89869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
89969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
900a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  std::vector<const DexFile*> dex_files;
901a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  if (boot_image_option.empty()) {
902a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
903a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  } else {
904a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    if (dex_filenames.empty()) {
905a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
906a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      if (zip_archive.get() == NULL) {
9074922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom        LOG(ERROR) << "Failed to open zip from file descriptor for " << zip_location;
9082e3d1b262af0839380e1d60e86d8b281943ef944Brian Carlstrom        return EXIT_FAILURE;
909a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
910a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
911a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      if (dex_file == NULL) {
912a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom        LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
913a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom        return EXIT_FAILURE;
914a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      }
915a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom      dex_files.push_back(dex_file);
916a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    } else {
917bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
918bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      if (failure_count > 0) {
919bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes        LOG(ERROR) << "Failed to open some dex files: " << failure_count;
920bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes        return EXIT_FAILURE;
921bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes      }
922a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom    }
923d76e08323893c60df77eccca6e882279e7246d8cBrian Carlstrom
924d76e08323893c60df77eccca6e882279e7246d8cBrian Carlstrom    // Ensure opened dex files are writable for dex-to-dex transformations.
925d76e08323893c60df77eccca6e882279e7246d8cBrian Carlstrom    for (const auto& dex_file : dex_files) {
926d76e08323893c60df77eccca6e882279e7246d8cBrian Carlstrom      if (!dex_file->EnableWrite()) {
927d76e08323893c60df77eccca6e882279e7246d8cBrian Carlstrom        PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n";
928d76e08323893c60df77eccca6e882279e7246d8cBrian Carlstrom      }
929d76e08323893c60df77eccca6e882279e7246d8cBrian Carlstrom    }
930a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom  }
931a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom
932a024a0686c3b0fea13f362bff70d65981e5febc5buzbee  /*
933a024a0686c3b0fea13f362bff70d65981e5febc5buzbee   * If we're not in interpret-only mode, go ahead and compile small applications. Don't
934a024a0686c3b0fea13f362bff70d65981e5febc5buzbee   * bother to check if we're doing the image.
935a024a0686c3b0fea13f362bff70d65981e5febc5buzbee   */
936a024a0686c3b0fea13f362bff70d65981e5febc5buzbee  if (!image && (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly)) {
9378447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    size_t num_methods = 0;
9388447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    for (size_t i = 0; i != dex_files.size(); ++i) {
9398447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum      const DexFile* dex_file = dex_files[i];
9408447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum      CHECK(dex_file != NULL);
9418447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum      num_methods += dex_file->NumMethodIds();
9428447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    }
943a024a0686c3b0fea13f362bff70d65981e5febc5buzbee    if (num_methods <= Runtime::Current()->GetNumDexMethodsThreshold()) {
944a024a0686c3b0fea13f362bff70d65981e5febc5buzbee      Runtime::Current()->SetCompilerFilter(Runtime::kSpeed);
94575a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum      VLOG(compiler) << "Below method threshold, compiling anyways";
9468447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum    }
9478447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum  }
948c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum
9491212a022fa5f8ef9585d765b1809521812af882cIan Rogers  UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option,
9501212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  host_prefix.get(),
9513f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom                                                                  android_root,
952265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                                  is_host,
9531212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  dex_files,
9541212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  oat_file.get(),
9551212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  bitcode_filename,
9561212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  image,
95796391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom                                                                  image_classes,
9581212a022fa5f8ef9585d765b1809521812af882cIan Rogers                                                                  dump_stats,
9594560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom                                                                  timings));
960f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom
961f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom  if (compiler.get() == NULL) {
962a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    LOG(ERROR) << "Failed to create oat file: " << oat_location;
963e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    return EXIT_FAILURE;
964e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  }
965e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
96675a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum  VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location;
967aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
968700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Notes on the interleaving of creating the image and oat file to
969700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // ensure the references between the two are correct.
970700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
971700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Currently we have a memory layout that looks something like this:
972700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
973700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
974700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // | image        |
975700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
976700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // | boot oat     |
977700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
978700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // | alloc spaces |
979700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // +--------------+
980700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
9814560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // There are several constraints on the loading of the image and boot.oat.
982700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
983700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 1. The image is expected to be loaded at an absolute address and
984700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // contains Objects with absolute pointers within the image.
985700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
986700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 2. There are absolute pointers from Methods in the image to their
987700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // code in the oat.
988700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
989700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 3. There are absolute pointers from the code in the oat to Methods
990700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // in the image.
991700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
992700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 4. There are absolute pointers from code in the oat to other code
993700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // in the oat.
994700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
995700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // To get this all correct, we go through several steps.
996700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
997700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 1. We have already created that oat file above with
998700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // CreateOatFile. Originally this was just our own proprietary file
9994560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  // but now it is contained within an ELF dynamic object (aka an .so
1000700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // file). The Compiler returned by CreateOatFile provides
1001700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // PatchInformation for references to oat code and Methods that need
1002700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // to be update once we know where the oat file will be located
1003700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // after the image.
1004700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
1005700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 2. We create the image file. It needs to know where the oat file
1006700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // will be loaded after itself. Originally when oat file was simply
1007700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // memory mapped so we could predict where its contents were based
1008700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // on the file size. Now that it is an ELF file, we need to inspect
1009700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // the ELF file to understand the in memory segment layout including
1010700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // where the oat header is located within. ImageWriter's
1011700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // PatchOatCodeAndMethods uses the PatchInformation from the
1012700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Compiler to touch up absolute references in the oat file.
1013700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
1014700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // 3. We fixup the ELF program headers so that dlopen will try to
1015700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // load the .so at the desired location at runtime by offsetting the
1016700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Elf32_Phdr.p_vaddr values by the desired base address.
1017700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  //
1018265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (image) {
10196f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    timings.NewSplit("dex2oat ImageWriter");
1020265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    bool image_creation_success = dex2oat->CreateImageFile(image_filename,
1021265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           image_base,
1022265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           oat_unstripped,
1023265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           oat_location,
1024265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                                                           *compiler.get());
1025265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    if (!image_creation_success) {
1026265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      return EXIT_FAILURE;
1027265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    }
102875a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum    VLOG(compiler) << "Image written successfully: " << image_filename;
1029265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
1030265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
1031265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (is_host) {
103246398608eaa47afe5a4cfb91e3f43211bf937d68Ian Rogers    if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
10336f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum      LOG(INFO) << Dumpable<base::TimingLogger>(timings);
10344560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom    }
1035265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    return EXIT_SUCCESS;
103669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  }
103769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
1038265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // If we don't want to strip in place, copy from unstripped location to stripped location.
1039265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // We need to strip after image creation because FixupElf needs to use .strtab.
1040265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  if (oat_unstripped != oat_stripped) {
10416f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    timings.NewSplit("dex2oat OatFile copy");
1042265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_file.reset();
10437571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom     UniquePtr<File> in(OS::OpenFileForReading(oat_unstripped.c_str()));
10447571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom    UniquePtr<File> out(OS::CreateEmptyFile(oat_stripped.c_str()));
1045265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    size_t buffer_size = 8192;
1046265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]);
1047265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    while (true) {
1048265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size));
1049265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      if (bytes_read <= 0) {
1050265091e581c9f643b37e7966890911f09e223269Brian Carlstrom        break;
1051265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      }
1052265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      bool write_ok = out->WriteFully(buffer.get(), bytes_read);
1053265091e581c9f643b37e7966890911f09e223269Brian Carlstrom      CHECK(write_ok);
1054265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    }
1055265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    oat_file.reset(out.release());
105675a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum    VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped;
1057265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  }
1058265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
10597fcba11846d3320911aefdba6a20c1192e36fc2eBrian Carlstrom#if ART_USE_PORTABLE_COMPILER  // We currently only generate symbols on Portable
10606f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum  timings.NewSplit("dex2oat ElfStripper");
1061265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // Strip unneeded sections for target
1062265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET);
1063265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  CHECK_EQ(0, seek_actual);
106451c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom  ElfStripper::Strip(oat_file.get());
10656f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum
1066265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
1067ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // We wrote the oat file successfully, and want to keep it.
106875a43f10f55e2aa550de51e969cc1e60d583b632Anwar Ghuloum  VLOG(compiler) << "Oat file written successfully (stripped): " << oat_location;
10697934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom#endif  // ART_USE_PORTABLE_COMPILER
10704560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom
10716f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum  timings.EndSplit();
10726f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum
1073c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
10746f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum    LOG(INFO) << Dumpable<base::TimingLogger>(timings);
10754560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom  }
107651db7beb7faaea43d697321520732d1d71288f50Ian Rogers
107751db7beb7faaea43d697321520732d1d71288f50Ian Rogers  // Everything was successfully written, do an explicit exit here to avoid running Runtime
107851db7beb7faaea43d697321520732d1d71288f50Ian Rogers  // destructors that take time (bug 10645725) unless we're a debug build or running on valgrind.
107951db7beb7faaea43d697321520732d1d71288f50Ian Rogers  if (!kIsDebugBuild || (RUNNING_ON_VALGRIND == 0)) {
108051db7beb7faaea43d697321520732d1d71288f50Ian Rogers    exit(EXIT_SUCCESS);
108151db7beb7faaea43d697321520732d1d71288f50Ian Rogers  }
108251db7beb7faaea43d697321520732d1d71288f50Ian Rogers
108369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  return EXIT_SUCCESS;
108469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom}
108569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
10864560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom
10877934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
108869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom
108969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromint main(int argc, char** argv) {
109069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  return art::dex2oat(argc, argv);
109169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom}
1092