dex2oat.cc revision 8447d84d847d4562d7a7bce62768c27e7d20a9aa
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/* 22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project 32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License. 62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at 72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software 112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and 142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License. 152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */ 1669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 1769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <stdio.h> 1869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <stdlib.h> 196cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom#include <sys/stat.h> 2069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 21ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom#include <iostream> 22ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom#include <fstream> 2369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <string> 2469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <vector> 2569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 261aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h" 27e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/stringpiece.h" 28a84395489098e4531619b1cffd1afc282b14510eSameer Abu Asal#include "base/timing_logger.h" 29761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h" 3069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "class_linker.h" 311212a022fa5f8ef9585d765b1809521812af882cIan Rogers#include "compiler/driver/compiler_driver.h" 324f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h" 3369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "image_writer.h" 346f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers#include "leb128.h" 352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/abstract_method-inl.h" 362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h" 372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h" 382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h" 392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h" 40e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "oat_writer.h" 416d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 425e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers#include "os.h" 4369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "runtime.h" 4400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "ScopedLocalRef.h" 4500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 461f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h" 47700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "vector_output_stream.h" 4800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "well_known_classes.h" 49a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom#include "zip_archive.h" 5069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 5169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromnamespace art { 5269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 53cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageErrorV(const char* fmt, va_list ap) { 54cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom std::string error; 55cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom StringAppendV(&error, fmt, ap); 56cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom LOG(ERROR) << error; 57cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom} 58cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 59cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageError(const char* fmt, ...) { 60cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_list ap; 61cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_start(ap, fmt); 62cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageErrorV(fmt, ap); 63cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_end(ap); 64cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom} 65cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 66cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void Usage(const char* fmt, ...) { 67cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_list ap; 68cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_start(ap, fmt); 69cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageErrorV(fmt, ap); 70cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_end(ap); 71cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 72cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError("Usage: dex2oat [options]..."); 73cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 74cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); 75cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --dex-file=/system/framework/core.jar"); 76cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 77cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); 78cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" containing a classes.dex file to compile."); 79cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --zip-fd=5"); 80cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 81cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding"); 82cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" to the file descriptor specified by --zip-fd."); 83cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --zip-location=/system/app/Calculator.apk"); 84cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 85265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); 86265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" Example: --oat-file=/system/framework/boot.oat"); 87265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 88265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); 89cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --oat-file=/system/framework/boot.oat"); 90cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 91cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); 92cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" to the file descriptor specified by --oat-fd."); 93cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat"); 94cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 95265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); 96265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); 97265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 988b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); 998b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(" Example: --bitcode=/system/framework/boot.bc"); 1008b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(""); 101cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --image=<file.art>: specifies the output image filename."); 102cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --image=/system/framework/boot.art"); 103cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 104cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); 105cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --image=frameworks/base/preloaded-classes"); 106cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 107cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); 108cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --base=0x50000000"); 109cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 110cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); 111cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --boot-image=/system/framework/boot.art"); 112cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Default: <host-prefix>/system/framework/boot.art"); 113cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 114265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --host-prefix=<path>: used to translate host paths to target paths during"); 115cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" cross compilation."); 116cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --host-prefix=out/target/product/crespo"); 117cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Default: $ANDROID_PRODUCT_OUT"); 118cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 1193f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" --android-root=<path>: used to locate libraries for portable linking."); 1203f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" Example: --android-root=out/host/linux-x86"); 1213f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" Default: $ANDROID_ROOT"); 1223f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(""); 1231f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction"); 12449c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers UsageError(" set."); 1251f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" Example: --instruction-set=x86"); 1261f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" Default: arm"); 12749c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers UsageError(""); 128c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend"); 129c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" set."); 130c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" Example: --instruction-set=Portable"); 131c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" Default: Quick"); 132265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 133265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --host: used with Portable backend to link against host runtime libraries"); 134265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 135cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); 136cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" such as initial heap size, maximum heap size, and verbose output."); 137cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Use a separate --runtime-arg switch for each argument."); 138cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --runtime-arg -Xms256m"); 139cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 140cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom std::cerr << "See log for usage error information\n"; 14169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom exit(EXIT_FAILURE); 14269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 14369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 144ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstromclass Dex2Oat { 145ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom public: 146c531cefbfb5394413122e9f57d211ba436cff012buzbee static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, CompilerBackend compiler_backend, 1478447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum InstructionSet instruction_set, size_t thread_count, bool support_debugging) 148b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { 14900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!CreateRuntime(options, instruction_set)) { 15000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers *p_dex2oat = NULL; 15100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return false; 152254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 153c531cefbfb5394413122e9f57d211ba436cff012buzbee *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count, 1548447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum support_debugging); 15500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return true; 156ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 157ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 158ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ~Dex2Oat() { 159ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom delete runtime_; 1605523ee070b005576c6f889415205d49ea77cf243Elliott Hughes LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")"; 161ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 162ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 163c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum 164ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Make a list of descriptors for classes to include in the image 16500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) 166b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 167ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); 168ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_file.get() == NULL) { 169ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; 170ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return NULL; 171ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 172ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 1736f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Load all the classes specified in the file 174ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ClassLinker* class_linker = runtime_->GetClassLinker(); 1751f5393447b9f45be7918042d9ee7b521376de866Ian Rogers Thread* self = Thread::Current(); 176ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom while (image_classes_file->good()) { 177ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::string dot; 178ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::getline(*image_classes_file.get(), dot); 179f1a5adc87760f938b01df26d906295063546b259Elliott Hughes if (StartsWith(dot, "#") || dot.empty()) { 180ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom continue; 181ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 182955724179c6c739524f610023287f56b24dc31deElliott Hughes std::string descriptor(DotToDescriptor(dot.c_str())); 1832dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers SirtRef<mirror::Class> klass(self, class_linker->FindSystemClass(descriptor.c_str())); 184ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (klass.get() == NULL) { 185ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(WARNING) << "Failed to find class " << descriptor; 186ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Thread::Current()->ClearException(); 187ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 188ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 189ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom image_classes_file->close(); 190ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 1916f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Resolve exception classes referenced by the loaded classes. The catch logic assumes 1926f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // exceptions are resolved by the verifier when there is a catch block in an interested method. 1936f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Do this here so that exception classes appear to have been specified image classes. 1946f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; 1952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers SirtRef<mirror::Class> java_lang_Throwable(self, 1961f5393447b9f45be7918042d9ee7b521376de866Ian Rogers class_linker->FindSystemClass("Ljava/lang/Throwable;")); 1976f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers do { 1986f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers unresolved_exception_types.clear(); 1996f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, 2006f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers &unresolved_exception_types); 2016f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto 2026f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (It it = unresolved_exception_types.begin(), 2036f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers end = unresolved_exception_types.end(); 2046f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers it != end; ++it) { 2056f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers uint16_t exception_type_idx = it->first; 2066f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const DexFile* dex_file = it->second; 2072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::DexCache* dex_cache = class_linker->FindDexCache(*dex_file); 2082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror:: ClassLoader* class_loader = NULL; 2092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers SirtRef<mirror::Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx, 2102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers dex_cache, class_loader)); 2116f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (klass.get() == NULL) { 2126f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); 2136f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const char* descriptor = dex_file->GetTypeDescriptor(type_id); 2146f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers LOG(FATAL) << "Failed to resolve class " << descriptor; 2156f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 21635be9b1f7d5b51652adda85d79e368597df68d64Elliott Hughes DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get())); 2176f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 2186f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Resolving exceptions may load classes that reference more exceptions, iterate until no 2196f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // more are found 2206f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } while (!unresolved_exception_types.empty()); 2216f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers 222ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // We walk the roots looking for classes so that we'll pick up the 223ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // above classes plus any classes them depend on such super 224ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // classes, interfaces, and the required ClassLinker roots. 225ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>()); 2266f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get()); 227ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom CHECK_NE(image_classes->size(), 0U); 228ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return image_classes.release(); 229254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 230254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson 2311212a022fa5f8ef9585d765b1809521812af882cIan Rogers const CompilerDriver* CreateOatFile(const std::string& boot_image_option, 2323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string* host_prefix, 2333f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string& android_root, 2343f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool is_host, 2353f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::vector<const DexFile*>& dex_files, 2363f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom File* oat_file, 2373f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string& bitcode_filename, 2383f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool image, 2393f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::set<std::string>* image_classes, 2403f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool dump_stats, 2413f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool dump_timings) 242b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 243ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // SirtRef and ClassLoader creation needs to come after Runtime::Create 24400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers jobject class_loader = NULL; 245ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!boot_image_option.empty()) { 246ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 247ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::vector<const DexFile*> class_path_files(dex_files); 248a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); 249ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < class_path_files.size(); i++) { 250ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom class_linker->RegisterDexFile(*class_path_files[i]); 251ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 25200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccessUnchecked soa(Thread::Current()); 25300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); 25400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedLocalRef<jobject> class_loader_local(soa.Env(), 25500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); 25600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); 25700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); 258254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 259ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 2601212a022fa5f8ef9585d765b1809521812af882cIan Rogers UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_, 2611212a022fa5f8ef9585d765b1809521812af882cIan Rogers instruction_set_, 2621212a022fa5f8ef9585d765b1809521812af882cIan Rogers image, 2631212a022fa5f8ef9585d765b1809521812af882cIan Rogers thread_count_, 2641212a022fa5f8ef9585d765b1809521812af882cIan Rogers support_debugging_, 2651212a022fa5f8ef9585d765b1809521812af882cIan Rogers image_classes, 2661212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_stats, 2671212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_timings)); 2688b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien 269c928de90ad22ecdf83c18a07008409595f13d3b1Ian Rogers if (compiler_backend_ == kPortable) { 2701212a022fa5f8ef9585d765b1809521812af882cIan Rogers driver->SetBitcodeFileName(bitcode_filename); 271c531cefbfb5394413122e9f57d211ba436cff012buzbee } 2728b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien 2738447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum 27400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromRunnableToSuspended(kNative); 27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 2761212a022fa5f8ef9585d765b1809521812af882cIan Rogers driver->CompileAll(class_loader, dex_files); 27700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 27800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromSuspendedToRunnable(); 279ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 28081f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom std::string image_file_location; 28128db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom uint32_t image_file_location_oat_checksum = 0; 282700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom uint32_t image_file_location_oat_data_begin = 0; 28381f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom Heap* heap = Runtime::Current()->GetHeap(); 28481f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom if (heap->GetSpaces().size() > 1) { 28581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom ImageSpace* image_space = heap->GetImageSpace(); 28628db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); 287700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom image_file_location_oat_data_begin = reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin()); 28881f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom image_file_location = image_space->GetImageFilename(); 28934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { 29034f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom image_file_location = image_file_location.substr(host_prefix->size()); 29181f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom } 29281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom } 29381f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom 294700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom std::vector<uint8_t> oat_contents; 295700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom VectorOutputStream vector_output_stream(oat_file->GetPath(), oat_contents); 296700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (!OatWriter::Create(vector_output_stream, 29781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom dex_files, 29828db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom image_file_location_oat_checksum, 299700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom image_file_location_oat_data_begin, 30081f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom image_file_location, 3011212a022fa5f8ef9585d765b1809521812af882cIan Rogers *driver.get())) { 302761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes LOG(ERROR) << "Failed to create oat file " << oat_file->GetPath(); 303f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom return NULL; 304ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 305700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 3063f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (!driver->WriteElf(android_root, is_host, dex_files, oat_contents, oat_file)) { 307700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); 308700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return NULL; 309700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 310700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 3111212a022fa5f8ef9585d765b1809521812af882cIan Rogers return driver.release(); 312254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 313ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 314a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom bool CreateImageFile(const std::string& image_filename, 315ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom uintptr_t image_base, 316ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const std::set<std::string>* image_classes, 317ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const std::string& oat_filename, 318f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom const std::string& oat_location, 3191212a022fa5f8ef9585d765b1809521812af882cIan Rogers const CompilerDriver& compiler) 320b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers LOCKS_EXCLUDED(Locks::mutator_lock_) { 321700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom uintptr_t oat_data_begin; 322700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom { 323700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // ImageWriter is scoped so it can free memory before doing FixupElf 324700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom ImageWriter image_writer(image_classes); 325700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) { 326700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to create image file " << image_filename; 327700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return false; 328700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 329700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom oat_data_begin = image_writer.GetOatDataBegin(); 330700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 331700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 332700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false)); 333700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (oat_file.get() == NULL) { 334700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom PLOG(ERROR) << "Failed to open ELF file: " << oat_filename; 335700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return false; 336700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 337700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (!compiler.FixupElf(oat_file.get(), oat_data_begin)) { 338700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); 339ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return false; 340ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 341ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 342ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 343ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 344ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom private: 345c531cefbfb5394413122e9f57d211ba436cff012buzbee explicit Dex2Oat(Runtime* runtime, CompilerBackend compiler_backend, InstructionSet instruction_set, 3468447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum size_t thread_count, bool support_debugging) 347c531cefbfb5394413122e9f57d211ba436cff012buzbee : compiler_backend_(compiler_backend), 348c531cefbfb5394413122e9f57d211ba436cff012buzbee instruction_set_(instruction_set), 34949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers runtime_(runtime), 350de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes thread_count_(thread_count), 351de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes support_debugging_(support_debugging), 352de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes start_ns_(NanoTime()) { 353bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes } 354ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 35500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) 356b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { 35700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!Runtime::Create(options, false)) { 358ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to create runtime"; 35900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return false; 360ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 36100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime* runtime = Runtime::Current(); 362ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // if we loaded an existing image, we will reuse values from the image roots. 3631984651929744dd603fd082e23eacd877b9bc177Ian Rogers if (!runtime->HasResolutionMethod()) { 3641984651929744dd603fd082e23eacd877b9bc177Ian Rogers runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); 3651984651929744dd603fd082e23eacd877b9bc177Ian Rogers } 366ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 367ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 368ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!runtime->HasCalleeSaveMethod(type)) { 36949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); 370ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 371ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 3721984651929744dd603fd082e23eacd877b9bc177Ian Rogers runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); 37300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return true; 374ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 375ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 3766f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers static void ResolveExceptionsForMethod(MethodHelper* mh, 37700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) 378b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 3796f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const DexFile::CodeItem* code_item = mh->GetCodeItem(); 3806f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (code_item == NULL) { 3816f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers return; // native or abstract method 3826f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 3836f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (code_item->tries_size_ == 0) { 3846f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers return; // nothing to process 3856f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 3866f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); 3876f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); 3886f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (size_t i = 0; i < num_encoded_catch_handlers; i++) { 3896f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); 3906f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers bool has_catch_all = false; 3916f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (encoded_catch_handler_size <= 0) { 3926f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers encoded_catch_handler_size = -encoded_catch_handler_size; 3936f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers has_catch_all = true; 3946f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 3956f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (int32_t j = 0; j < encoded_catch_handler_size; j++) { 3966f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers uint16_t encoded_catch_handler_handlers_type_idx = 3976f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers DecodeUnsignedLeb128(&encoded_catch_handler_list); 3986f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Add to set of types to resolve if not already in the dex cache resolved types 3996f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { 4006f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers exceptions_to_resolve.insert( 4016f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, 4026f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers &mh->GetDexFile())); 4036f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4046f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // ignore address associated with catch handler 4056f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers DecodeUnsignedLeb128(&encoded_catch_handler_list); 4066f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4076f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (has_catch_all) { 4086f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // ignore catch all address 4096f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers DecodeUnsignedLeb128(&encoded_catch_handler_list); 4106f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4116f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4126f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 41300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 4142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg) 415b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 4166f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = 4176f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); 4186f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers MethodHelper mh; 4196f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { 4202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* m = c->GetVirtualMethod(i); 4216f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers mh.ChangeMethod(m); 4226f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); 4236f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4246f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (size_t i = 0; i < c->NumDirectMethods(); ++i) { 4252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* m = c->GetDirectMethod(i); 4266f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers mh.ChangeMethod(m); 4276f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); 4286f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4296f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers return true; 4306f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 43100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 4322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg) 433b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 434ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); 435ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (klass->IsArrayClass() || klass->IsPrimitive()) { 436ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 437ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 4386d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers image_classes->insert(ClassHelper(klass).GetDescriptor()); 439ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 440ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 441ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 442ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Appends to dex_files any elements of class_path that it doesn't already 443ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // contain. This will open those dex files as necessary. 444ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { 445ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::vector<std::string> parsed; 446ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Split(class_path, ':', parsed); 447ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < parsed.size(); ++i) { 448ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (DexFilesContains(dex_files, parsed[i])) { 449ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom continue; 450ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 451a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]); 452ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (dex_file == NULL) { 453ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(WARNING) << "Failed to open dex file " << parsed[i]; 454ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } else { 455ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom dex_files.push_back(dex_file); 456ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 457ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 458ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 459ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 460ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Returns true if dex_files has a dex with the named location. 461ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { 462ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < dex_files.size(); ++i) { 463ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (dex_files[i]->GetLocation() == location) { 464ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 465ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 466ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 467ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return false; 468ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 469ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 470c531cefbfb5394413122e9f57d211ba436cff012buzbee const CompilerBackend compiler_backend_; 471c531cefbfb5394413122e9f57d211ba436cff012buzbee 47249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers const InstructionSet instruction_set_; 473ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 474bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes Runtime* runtime_; 4755523ee070b005576c6f889415205d49ea77cf243Elliott Hughes size_t thread_count_; 476de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes bool support_debugging_; 477bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes uint64_t start_ns_; 478bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes 479ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); 480ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom}; 481254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson 48272395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic bool ParseInt(const char* in, int* out) { 483a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom char* end; 484a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int result = strtol(in, &end, 10); 485a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (in == end || *end != '\0') { 486a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return false; 487a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 488a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom *out = result; 489a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return true; 490a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom} 491a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 492bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughesstatic size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, 493bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes const std::vector<const char*>& dex_locations, 494bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes std::vector<const DexFile*>& dex_files) { 495bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = 0; 4965b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom for (size_t i = 0; i < dex_filenames.size(); i++) { 4975b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom const char* dex_filename = dex_filenames[i]; 498a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const char* dex_location = dex_locations[i]; 499a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(dex_filename, dex_location); 5005b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom if (dex_file == NULL) { 50160f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao LOG(WARNING) << "could not open .dex from file " << dex_filename; 502bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes ++failure_count; 50360f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao } else { 50460f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao dex_files.push_back(dex_file); 5055b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 5065b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 507bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return failure_count; 5085b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom} 5095b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom 510bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// The primary goal of the watchdog is to prevent stuck build servers 511bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// during development when fatal aborts lead to a cascade of failures 512bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// that result in a deadlock. 513bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromclass WatchDog { 514bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 515bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks 516bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#undef CHECK_PTHREAD_CALL 517bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ 518bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom do { \ 519bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom int rc = call args; \ 520bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom if (rc != 0) { \ 521bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom errno = rc; \ 522bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom std::string message(# call); \ 523bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom message += " failed for "; \ 524bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom message += reason; \ 525ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Fatal(message); \ 526bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } \ 527bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } while (false) 528bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 529bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom public: 530994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom WatchDog(bool is_watch_dog_enabled) { 531994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom is_watch_dog_enabled_ = is_watch_dog_enabled; 532994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom if (!is_watch_dog_enabled_) { 533bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return; 534bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 535bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom shutting_down_ = false; 536bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread startup"; 537bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason); 538bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason); 539bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); 540bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); 541bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); 542bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 543bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom ~WatchDog() { 544994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom if (!is_watch_dog_enabled_) { 545bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return; 546bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 547bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread shutdown"; 548bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); 549bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom shutting_down_ = true; 550bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); 551bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 552bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 553bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason); 554bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 555bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); 556bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); 557bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 558bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 559bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom private: 560bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom static void* CallBack(void* arg) { 561bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom WatchDog* self = reinterpret_cast<WatchDog*>(arg); 562bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom self->Wait(); 563bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return NULL; 564bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 565bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 566ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static void Message(char severity, const std::string& message) { 567ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error cases. 568ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom fprintf(stderr, "dex2oat%s %c %d %d %s\n", 569ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom kIsDebugBuild ? "d" : "", 570ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom severity, 571ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom getpid(), 572ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom GetTid(), 573ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom message.c_str()); 574ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom } 575ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom 576eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static void Warn(const std::string& message) { 577ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Message('W', message); 578eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } 579eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers 580ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static void Fatal(const std::string& message) { 581ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Message('F', message); 582bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom exit(1); 583bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 584bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 585bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom void Wait() { 586eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers bool warning = true; 587eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds); 588ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom timespec warning_ts; 589eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers InitTimeSpec(true, CLOCK_REALTIME, kWatchDogWarningSeconds * 1000, 0, &warning_ts); 590ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom timespec timeout_ts; 591eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers InitTimeSpec(true, CLOCK_REALTIME, kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); 592bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread waiting"; 593bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); 594bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom while (!shutting_down_) { 595eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, 596eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers warning ? &warning_ts 597eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers : &timeout_ts)); 598bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom if (rc == ETIMEDOUT) { 599bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom std::string message(StringPrintf("dex2oat did not finish after %d seconds", 600eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers warning ? kWatchDogWarningSeconds 601eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers : kWatchDogTimeoutSeconds)); 602eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers if (warning) { 603eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers Warn(message.c_str()); 604eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers warning = false; 605eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } else { 606ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Fatal(message.c_str()); 607eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } 608eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } else if (rc != 0) { 609bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom std::string message(StringPrintf("pthread_cond_timedwait failed: %s", 610bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom strerror(errno))); 611ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Fatal(message.c_str()); 612bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 613bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 614bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 615bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 616bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 617ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom // When setting timeouts, keep in mind that the build server may not be as fast as your desktop. 618ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom#if ART_USE_PORTABLE_COMPILER 619ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static const unsigned int kWatchDogWarningSeconds = 2 * 60; // 2 minutes. 620eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static const unsigned int kWatchDogTimeoutSeconds = 30 * 60; // 25 minutes + buffer. 621bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#else 622ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static const unsigned int kWatchDogWarningSeconds = 1 * 60; // 1 minute. 623ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static const unsigned int kWatchDogTimeoutSeconds = 6 * 60; // 5 minutes + buffer. 624bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif 625bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 626994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom bool is_watch_dog_enabled_; 627bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom bool shutting_down_; 628bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases 629bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_mutex_t mutex_; 630bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_cond_t cond_; 631bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_attr_t attr_; 632bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_t pthread_; 633bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom}; 634bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 63572395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic int dex2oat(int argc, char** argv) { 6360d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes InitLogging(argv); 63772395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughes 63869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom // Skip over argv[0]. 63969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom argv++; 64069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom argc--; 64169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 64269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (argc == 0) { 643cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("no arguments specified"); 64469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 64569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 64669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom std::vector<const char*> dex_filenames; 647a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::vector<const char*> dex_locations; 648a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int zip_fd = -1; 649a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string zip_location; 650e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom std::string oat_filename; 651265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_symbols; 652a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string oat_location; 653a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int oat_fd = -1; 6548b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien std::string bitcode_filename; 655ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const char* image_classes_filename = NULL; 656a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string image_filename; 657b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom std::string boot_image_filename; 65869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom uintptr_t image_base = 0; 65934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom UniquePtr<std::string> host_prefix; 6603f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom std::string android_root; 6615d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao std::vector<const char*> runtime_args; 6625c599943f8c347acd84c4d9fda56a9df70649b78Elliott Hughes int thread_count = sysconf(_SC_NPROCESSORS_CONF); 663de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes bool support_debugging = false; 664c531cefbfb5394413122e9f57d211ba436cff012buzbee#if defined(ART_USE_PORTABLE_COMPILER) 665c531cefbfb5394413122e9f57d211ba436cff012buzbee CompilerBackend compiler_backend = kPortable; 666c531cefbfb5394413122e9f57d211ba436cff012buzbee#else 667c531cefbfb5394413122e9f57d211ba436cff012buzbee CompilerBackend compiler_backend = kQuick; 668c531cefbfb5394413122e9f57d211ba436cff012buzbee#endif 6699ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#if defined(__arm__) 67049c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers InstructionSet instruction_set = kThumb2; 6719ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__i386__) 6729ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao InstructionSet instruction_set = kX86; 6739ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__mips__) 6749ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao InstructionSet instruction_set = kMips; 6759ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#else 6769ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#error "Unsupported architecture" 6779ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#endif 678265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool is_host = false; 67967d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes bool dump_stats = kIsDebugBuild; 68067d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes bool dump_timings = kIsDebugBuild; 681994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom bool watch_dog_enabled = !kIsTargetBuild; 682161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom 683c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum 68469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom for (int i = 0; i < argc; i++) { 68569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom const StringPiece option(argv[i]); 686a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom bool log_options = false; 687a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (log_options) { 688b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 689b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom } 69069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (option.starts_with("--dex-file=")) { 69169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); 692a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--dex-location=")) { 693a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); 694a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--zip-fd=")) { 695a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); 696bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes if (!ParseInt(zip_fd_str, &zip_fd)) { 697cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str); 698a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 699a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--zip-location=")) { 700a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom zip_location = option.substr(strlen("--zip-location=")).data(); 701a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--oat-file=")) { 702a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom oat_filename = option.substr(strlen("--oat-file=")).data(); 703265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else if (option.starts_with("--oat-symbols=")) { 704265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_symbols = option.substr(strlen("--oat-symbols=")).data(); 705a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--oat-fd=")) { 706a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); 707bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes if (!ParseInt(oat_fd_str, &oat_fd)) { 708cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str); 709a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 710994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "-g") { 711de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes support_debugging = true; 712994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "--watch-dog") { 713994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom watch_dog_enabled = true; 714994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "--no-watch-dog") { 715994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom watch_dog_enabled = false; 7165523ee070b005576c6f889415205d49ea77cf243Elliott Hughes } else if (option.starts_with("-j")) { 717b12552a95d68b9e4a567103190074ae47b6a61dcBrian Carlstrom const char* thread_count_str = option.substr(strlen("-j")).data(); 7185523ee070b005576c6f889415205d49ea77cf243Elliott Hughes if (!ParseInt(thread_count_str, &thread_count)) { 719cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse -j argument '%s' as an integer", thread_count_str); 7205523ee070b005576c6f889415205d49ea77cf243Elliott Hughes } 721a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--oat-location=")) { 722a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom oat_location = option.substr(strlen("--oat-location=")).data(); 7238b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien } else if (option.starts_with("--bitcode=")) { 7248b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien bitcode_filename = option.substr(strlen("--bitcode=")).data(); 72569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else if (option.starts_with("--image=")) { 72669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom image_filename = option.substr(strlen("--image=")).data(); 727ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } else if (option.starts_with("--image-classes=")) { 728ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom image_classes_filename = option.substr(strlen("--image-classes=")).data(); 72969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else if (option.starts_with("--base=")) { 73069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom const char* image_base_str = option.substr(strlen("--base=")).data(); 73169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom char* end; 73269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom image_base = strtoul(image_base_str, &end, 16); 73369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (end == image_base_str || *end != '\0') { 734cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Failed to parse hexadecimal value for option %s", option.data()); 73569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 736e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } else if (option.starts_with("--boot-image=")) { 737b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_filename = option.substr(strlen("--boot-image=")).data(); 73858ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom } else if (option.starts_with("--host-prefix=")) { 73934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); 7403f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } else if (option.starts_with("--android-root=")) { 7413f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root = option.substr(strlen("--android-root=")).data(); 74249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers } else if (option.starts_with("--instruction-set=")) { 74349c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); 7441f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao if (instruction_set_str == "arm") { 74549c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kThumb2; 7461f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao } else if (instruction_set_str == "mips") { 74749c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kMips; 7481f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao } else if (instruction_set_str == "x86") { 74949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kX86; 75049c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers } 751c531cefbfb5394413122e9f57d211ba436cff012buzbee } else if (option.starts_with("--compiler-backend=")) { 752c531cefbfb5394413122e9f57d211ba436cff012buzbee StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); 753c531cefbfb5394413122e9f57d211ba436cff012buzbee if (backend_str == "Quick") { 754c531cefbfb5394413122e9f57d211ba436cff012buzbee compiler_backend = kQuick; 755c531cefbfb5394413122e9f57d211ba436cff012buzbee } else if (backend_str == "Portable") { 756c531cefbfb5394413122e9f57d211ba436cff012buzbee compiler_backend = kPortable; 757c531cefbfb5394413122e9f57d211ba436cff012buzbee } 758265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else if (option == "--host") { 759265091e581c9f643b37e7966890911f09e223269Brian Carlstrom is_host = true; 7605d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } else if (option == "--runtime-arg") { 7615d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao if (++i >= argc) { 762cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Missing required argument for --runtime-arg"); 7635d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } 764a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (log_options) { 765a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 766a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 7675d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao runtime_args.push_back(argv[i]); 76869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else { 769cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("unknown argument %s", option.data()); 77069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 77169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 77269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 773a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (oat_filename.empty() && oat_fd == -1) { 774cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Output must be supplied with either --oat-file or --oat-fd"); 775a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 776a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 777a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (!oat_filename.empty() && oat_fd != -1) { 778cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--oat-file should not be used with --oat-fd"); 779a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 780a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 781265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty() && oat_fd != -1) { 782265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Usage("--oat-symbols should not be used with --oat-fd"); 783265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 784265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 785265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty() && is_host) { 786265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Usage("--oat-symbols should not be used with --host"); 787a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 788a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 789a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (oat_fd != -1 && !image_filename.empty()) { 790cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--oat-fd should not be used with --image"); 791e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } 792e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom 79334f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() == NULL) { 794b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); 795b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom if (android_product_out != NULL) { 79634f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom host_prefix.reset(new std::string(android_product_out)); 797b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 798b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 799b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom 8003f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (android_root.empty()) { 8013f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const char* android_root_env_var = getenv("ANDROID_ROOT"); 8023f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (android_root_env_var == NULL) { 80354d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom Usage("--android-root unspecified and ANDROID_ROOT not set"); 8043f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } 8053f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root += android_root_env_var; 8063f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } 8073f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom 808a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom bool image = (!image_filename.empty()); 809b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom if (!image && boot_image_filename.empty()) { 81034f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() == NULL) { 811a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += GetAndroidRoot(); 812a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom } else { 81334f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom boot_image_filename += *host_prefix.get(); 814a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += "/system"; 815a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom } 816a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += "/framework/boot.art"; 817b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 818b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom std::string boot_image_option; 819a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (!boot_image_filename.empty()) { 820b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_option += "-Ximage:"; 821b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_option += boot_image_filename; 82269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 82369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 824ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL && !image) { 825cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--image-classes should only be used with --image"); 826ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 827ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 828ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL && !boot_image_option.empty()) { 829cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--image-classes should not be used with --boot-image"); 83078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom } 83178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom 832a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_filenames.empty() && zip_fd == -1) { 833cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Input must be supplied with either --dex-file or --zip-fd"); 834a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 835a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 836a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (!dex_filenames.empty() && zip_fd != -1) { 837cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-file should not be used with --zip-fd"); 838a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 839a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 840a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (!dex_filenames.empty() && !zip_location.empty()) { 841cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-file should not be used with --zip-location"); 842a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 843a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 844a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (dex_locations.empty()) { 845a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom for (size_t i = 0; i < dex_filenames.size(); i++) { 846a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom dex_locations.push_back(dex_filenames[i]); 847a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 848a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (dex_locations.size() != dex_filenames.size()) { 849cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-location arguments do not match --dex-file arguments"); 850a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 851a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom 852a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (zip_fd != -1 && zip_location.empty()) { 853cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--zip-location should be supplied with --zip-fd"); 854a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 855a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 85669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (boot_image_option.empty()) { 85769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (image_base == 0) { 858cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("non-zero --base not specified"); 85969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 86069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 86169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 862265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_stripped(oat_filename); 863265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_unstripped; 864265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty()) { 865265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped += oat_symbols; 866265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else { 867265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped += oat_filename; 868265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 869265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 870994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom // Done with usage checks, enable watchdog if requested 871994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom WatchDog watch_dog(watch_dog_enabled); 872994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom 8736ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom // Check early that the result of compilation can be written 874a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom UniquePtr<File> oat_file; 875265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor 8766cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom if (create_file) { 877265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(OS::OpenFile(oat_unstripped.c_str(), true)); 878a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (oat_location.empty()) { 879a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom oat_location = oat_filename; 880a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 881a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 882761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes oat_file.reset(new File(oat_fd, oat_location)); 883761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes oat_file->DisableAutoClose(); 884a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 8856ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom if (oat_file.get() == NULL) { 8866cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom PLOG(ERROR) << "Failed to create oat file: " << oat_location; 8876cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom return EXIT_FAILURE; 8886cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom } 8896cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { 8906cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; 8916ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom return EXIT_FAILURE; 892234da578a2d91ed7f2ef47b2ec23fb0033e2746bElliott Hughes } 893a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 894a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(INFO) << "dex2oat: " << oat_location; 8955e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers 89669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom Runtime::Options options; 8975de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); 898a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::vector<const DexFile*> boot_class_path; 89969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (boot_image_option.empty()) { 900bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); 901bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes if (failure_count > 0) { 902bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes LOG(ERROR) << "Failed to open some dex files: " << failure_count; 903bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return EXIT_FAILURE; 904bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes } 905a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom options.push_back(std::make_pair("bootclasspath", &boot_class_path)); 90669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else { 90769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); 90869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 90934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() != NULL) { 91034f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); 91158ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom } 9125d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao for (size_t i = 0; i < runtime_args.size(); i++) { 9135d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); 9145d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } 91569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 9168447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum#if ART_SMALL_MODE 9178447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum options.push_back(std::make_pair("-small", reinterpret_cast<void*>(NULL))); 9188447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum#endif // ART_SMALL_MODE 9198447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum 92000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Dex2Oat* p_dex2oat; 9218447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count, 9228447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum support_debugging)) { 92300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(ERROR) << "Failed to create dex2oat"; 92400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return EXIT_FAILURE; 92500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 92600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers UniquePtr<Dex2Oat> dex2oat(p_dex2oat); 92700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, 92800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // give it away now and then switch to a more managable ScopedObjectAccess. 92900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromRunnableToSuspended(kNative); 93000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Whilst we're in native take the opportunity to initialize well known classes. 93100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv()); 93200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 93369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 934bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes // If --image-classes was specified, calculate the full list of classes to include in the image 935ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom UniquePtr<const std::set<std::string> > image_classes(NULL); 936ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL) { 937ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); 938ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes.get() == NULL) { 939ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; 940ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return EXIT_FAILURE; 94169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 94269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 94369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 944a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom std::vector<const DexFile*> dex_files; 945a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (boot_image_option.empty()) { 946a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); 947a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 948a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_filenames.empty()) { 949a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd)); 950a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (zip_archive.get() == NULL) { 951a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location; 9522e3d1b262af0839380e1d60e86d8b281943ef944Brian Carlstrom return EXIT_FAILURE; 953a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 954a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location); 955a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_file == NULL) { 956a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location; 957a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return EXIT_FAILURE; 958a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 959a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom dex_files.push_back(dex_file); 960a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 961bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); 962bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes if (failure_count > 0) { 963bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes LOG(ERROR) << "Failed to open some dex files: " << failure_count; 964bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return EXIT_FAILURE; 965bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes } 966a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 967a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 968a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 9698447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum // If we're in small mode, but the program is small, turn off small mode. 9708447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum // It doesn't make a difference for the boot image, so let's skip the check 9718447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum // altogether. 9728447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum if (Runtime::Current()->IsSmallMode() && !image) { 9738447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum size_t num_methods = 0; 9748447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum for (size_t i = 0; i != dex_files.size(); ++i) { 9758447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum const DexFile* dex_file = dex_files[i]; 9768447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum CHECK(dex_file != NULL); 9778447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum num_methods += dex_file->NumMethodIds(); 9788447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum } 9798447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum if (num_methods <= Runtime::Current()->GetSmallModeMethodThreshold()) { 9808447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum Runtime::Current()->SetSmallMode(false); 9818447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum LOG(INFO) << "Below method threshold, compiling anyways"; 9828447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum } 9838447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum } 984c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum 9851212a022fa5f8ef9585d765b1809521812af882cIan Rogers UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option, 9861212a022fa5f8ef9585d765b1809521812af882cIan Rogers host_prefix.get(), 9873f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root, 988265091e581c9f643b37e7966890911f09e223269Brian Carlstrom is_host, 9891212a022fa5f8ef9585d765b1809521812af882cIan Rogers dex_files, 9901212a022fa5f8ef9585d765b1809521812af882cIan Rogers oat_file.get(), 9911212a022fa5f8ef9585d765b1809521812af882cIan Rogers bitcode_filename, 9921212a022fa5f8ef9585d765b1809521812af882cIan Rogers image, 9931212a022fa5f8ef9585d765b1809521812af882cIan Rogers image_classes.get(), 9941212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_stats, 9951212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_timings)); 996f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom 997f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom if (compiler.get() == NULL) { 998a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to create oat file: " << oat_location; 999e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom return EXIT_FAILURE; 1000e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } 1001e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom 1002265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Oat file written successfully (unstripped): " << oat_location; 1003aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom 1004700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Notes on the interleaving of creating the image and oat file to 1005700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // ensure the references between the two are correct. 1006700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1007700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Currently we have a memory layout that looks something like this: 1008700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1009700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 1010700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | image | 1011700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 1012700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | boot oat | 1013700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 1014700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | alloc spaces | 1015700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 1016700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1017700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // There are several constraints on the loading of the imag and boot.oat. 1018700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1019700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1. The image is expected to be loaded at an absolute address and 1020700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // contains Objects with absolute pointers within the image. 1021700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1022700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 2. There are absolute pointers from Methods in the image to their 1023700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // code in the oat. 1024700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1025700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 3. There are absolute pointers from the code in the oat to Methods 1026700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // in the image. 1027700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1028700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 4. There are absolute pointers from code in the oat to other code 1029700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // in the oat. 1030700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1031700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // To get this all correct, we go through several steps. 1032700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1033700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1. We have already created that oat file above with 1034700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // CreateOatFile. Originally this was just our own proprietary file 1035700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // but now it is contained within an ELF dynamic object (aka .so 1036700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // file). The Compiler returned by CreateOatFile provides 1037700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // PatchInformation for references to oat code and Methods that need 1038700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // to be update once we know where the oat file will be located 1039700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // after the image. 1040700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1041700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 2. We create the image file. It needs to know where the oat file 1042700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // will be loaded after itself. Originally when oat file was simply 1043700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // memory mapped so we could predict where its contents were based 1044700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // on the file size. Now that it is an ELF file, we need to inspect 1045700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // the ELF file to understand the in memory segment layout including 1046700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // where the oat header is located within. ImageWriter's 1047700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // PatchOatCodeAndMethods uses the PatchInformation from the 1048700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Compiler to touch up absolute references in the oat file. 1049700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1050700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 3. We fixup the ELF program headers so that dlopen will try to 1051700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // load the .so at the desired location at runtime by offsetting the 1052700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Elf32_Phdr.p_vaddr values by the desired base address. 1053700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1054265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (image) { 1055265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Thread::Current()->TransitionFromRunnableToSuspended(kNative); 1056265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool image_creation_success = dex2oat->CreateImageFile(image_filename, 1057265091e581c9f643b37e7966890911f09e223269Brian Carlstrom image_base, 1058265091e581c9f643b37e7966890911f09e223269Brian Carlstrom image_classes.get(), 1059265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped, 1060265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_location, 1061265091e581c9f643b37e7966890911f09e223269Brian Carlstrom *compiler.get()); 1062265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Thread::Current()->TransitionFromSuspendedToRunnable(); 1063265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Image written successfully: " << image_filename; 1064265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!image_creation_success) { 1065265091e581c9f643b37e7966890911f09e223269Brian Carlstrom return EXIT_FAILURE; 1066265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1067265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1068265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1069265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (is_host) { 1070265091e581c9f643b37e7966890911f09e223269Brian Carlstrom return EXIT_SUCCESS; 107169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 107269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 1073265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // If we don't want to strip in place, copy from unstripped location to stripped location. 1074265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // We need to strip after image creation because FixupElf needs to use .strtab. 1075265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (oat_unstripped != oat_stripped) { 1076265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(); 1077265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<File> in(OS::OpenFile(oat_unstripped.c_str(), false)); 1078265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<File> out(OS::OpenFile(oat_stripped.c_str(), true)); 1079265091e581c9f643b37e7966890911f09e223269Brian Carlstrom size_t buffer_size = 8192; 1080265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]); 1081265091e581c9f643b37e7966890911f09e223269Brian Carlstrom while (true) { 1082265091e581c9f643b37e7966890911f09e223269Brian Carlstrom int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); 1083265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (bytes_read <= 0) { 1084265091e581c9f643b37e7966890911f09e223269Brian Carlstrom break; 1085265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1086265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool write_ok = out->WriteFully(buffer.get(), bytes_read); 1087265091e581c9f643b37e7966890911f09e223269Brian Carlstrom CHECK(write_ok); 1088265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1089265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(out.release()); 1090265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Oat file copied successfully (stripped): " << oat_stripped; 1091265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1092265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1093265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // Strip unneeded sections for target 1094265091e581c9f643b37e7966890911f09e223269Brian Carlstrom off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET); 1095265091e581c9f643b37e7966890911f09e223269Brian Carlstrom CHECK_EQ(0, seek_actual); 1096265091e581c9f643b37e7966890911f09e223269Brian Carlstrom compiler->StripElf(oat_file.get()); 1097265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1098ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // We wrote the oat file successfully, and want to keep it. 109954d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom LOG(INFO) << "Oat file written successfully (stripped): " << oat_location; 110069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom return EXIT_SUCCESS; 110169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 110269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 110369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} // namespace art 110469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 110569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromint main(int argc, char** argv) { 110669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom return art::dex2oat(argc, argv); 110769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 1108