dex2oat.cc revision 54d22c2bb47da44ef586b9de94749d5648178a26
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" 3269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "image_writer.h" 336f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers#include "leb128.h" 342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/abstract_method-inl.h" 352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h" 362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h" 372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h" 382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h" 39e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "oat_writer.h" 406d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 415e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers#include "os.h" 4269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "runtime.h" 4300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "ScopedLocalRef.h" 4400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 451f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h" 46700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "vector_output_stream.h" 4700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "well_known_classes.h" 48a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom#include "zip_archive.h" 4969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 5069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromnamespace art { 5169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 52cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageErrorV(const char* fmt, va_list ap) { 53cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom std::string error; 54cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom StringAppendV(&error, fmt, ap); 55cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom LOG(ERROR) << error; 56cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom} 57cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 58cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageError(const char* fmt, ...) { 59cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_list ap; 60cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_start(ap, fmt); 61cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageErrorV(fmt, ap); 62cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_end(ap); 63cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom} 64cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 65cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void Usage(const char* fmt, ...) { 66cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_list ap; 67cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_start(ap, fmt); 68cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageErrorV(fmt, ap); 69cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_end(ap); 70cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 71cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError("Usage: dex2oat [options]..."); 72cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 73cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); 74cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --dex-file=/system/framework/core.jar"); 75cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 76cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); 77cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" containing a classes.dex file to compile."); 78cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --zip-fd=5"); 79cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 80cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding"); 81cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" to the file descriptor specified by --zip-fd."); 82cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --zip-location=/system/app/Calculator.apk"); 83cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 84265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); 85265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" Example: --oat-file=/system/framework/boot.oat"); 86265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 87265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); 88cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --oat-file=/system/framework/boot.oat"); 89cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 90cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); 91cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" to the file descriptor specified by --oat-fd."); 92cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat"); 93cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 94265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); 95265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); 96265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 978b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); 988b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(" Example: --bitcode=/system/framework/boot.bc"); 998b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(""); 100cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --image=<file.art>: specifies the output image filename."); 101cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --image=/system/framework/boot.art"); 102cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 103cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); 104cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --image=frameworks/base/preloaded-classes"); 105cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 106cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); 107cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --base=0x50000000"); 108cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 109cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); 110cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --boot-image=/system/framework/boot.art"); 111cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Default: <host-prefix>/system/framework/boot.art"); 112cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 113265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --host-prefix=<path>: used to translate host paths to target paths during"); 114cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" cross compilation."); 115cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --host-prefix=out/target/product/crespo"); 116cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Default: $ANDROID_PRODUCT_OUT"); 117cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 1183f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" --android-root=<path>: used to locate libraries for portable linking."); 1193f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" Example: --android-root=out/host/linux-x86"); 1203f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" Default: $ANDROID_ROOT"); 1213f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(""); 1221f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction"); 12349c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers UsageError(" set."); 1241f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" Example: --instruction-set=x86"); 1251f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" Default: arm"); 12649c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers UsageError(""); 127c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend"); 128c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" set."); 129c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" Example: --instruction-set=Portable"); 130c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" Default: Quick"); 131265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 132265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --host: used with Portable backend to link against host runtime libraries"); 133265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 134cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); 135cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" such as initial heap size, maximum heap size, and verbose output."); 136cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Use a separate --runtime-arg switch for each argument."); 137cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --runtime-arg -Xms256m"); 138cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 139cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom std::cerr << "See log for usage error information\n"; 14069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom exit(EXIT_FAILURE); 14169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 14269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 143ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstromclass Dex2Oat { 144ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom public: 145c531cefbfb5394413122e9f57d211ba436cff012buzbee static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, CompilerBackend compiler_backend, 146c531cefbfb5394413122e9f57d211ba436cff012buzbee InstructionSet instruction_set, size_t thread_count, bool support_debugging) 147b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { 14800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!CreateRuntime(options, instruction_set)) { 14900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers *p_dex2oat = NULL; 15000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return false; 151254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 152c531cefbfb5394413122e9f57d211ba436cff012buzbee *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count, 153c531cefbfb5394413122e9f57d211ba436cff012buzbee support_debugging); 15400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return true; 155ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 156ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 157ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ~Dex2Oat() { 158ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom delete runtime_; 1595523ee070b005576c6f889415205d49ea77cf243Elliott Hughes LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")"; 160ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 161ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 162ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Make a list of descriptors for classes to include in the image 16300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) 164b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 165ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); 166ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_file.get() == NULL) { 167ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; 168ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return NULL; 169ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 170ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 1716f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Load all the classes specified in the file 172ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ClassLinker* class_linker = runtime_->GetClassLinker(); 1731f5393447b9f45be7918042d9ee7b521376de866Ian Rogers Thread* self = Thread::Current(); 174ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom while (image_classes_file->good()) { 175ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::string dot; 176ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::getline(*image_classes_file.get(), dot); 177f1a5adc87760f938b01df26d906295063546b259Elliott Hughes if (StartsWith(dot, "#") || dot.empty()) { 178ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom continue; 179ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 180955724179c6c739524f610023287f56b24dc31deElliott Hughes std::string descriptor(DotToDescriptor(dot.c_str())); 1812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers SirtRef<mirror::Class> klass(self, class_linker->FindSystemClass(descriptor.c_str())); 182ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (klass.get() == NULL) { 183ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(WARNING) << "Failed to find class " << descriptor; 184ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Thread::Current()->ClearException(); 185ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 186ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 187ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom image_classes_file->close(); 188ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 1896f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Resolve exception classes referenced by the loaded classes. The catch logic assumes 1906f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // exceptions are resolved by the verifier when there is a catch block in an interested method. 1916f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Do this here so that exception classes appear to have been specified image classes. 1926f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; 1932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers SirtRef<mirror::Class> java_lang_Throwable(self, 1941f5393447b9f45be7918042d9ee7b521376de866Ian Rogers class_linker->FindSystemClass("Ljava/lang/Throwable;")); 1956f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers do { 1966f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers unresolved_exception_types.clear(); 1976f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, 1986f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers &unresolved_exception_types); 1996f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto 2006f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (It it = unresolved_exception_types.begin(), 2016f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers end = unresolved_exception_types.end(); 2026f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers it != end; ++it) { 2036f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers uint16_t exception_type_idx = it->first; 2046f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const DexFile* dex_file = it->second; 2052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::DexCache* dex_cache = class_linker->FindDexCache(*dex_file); 2062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror:: ClassLoader* class_loader = NULL; 2072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers SirtRef<mirror::Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx, 2082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers dex_cache, class_loader)); 2096f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (klass.get() == NULL) { 2106f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); 2116f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const char* descriptor = dex_file->GetTypeDescriptor(type_id); 2126f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers LOG(FATAL) << "Failed to resolve class " << descriptor; 2136f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 21435be9b1f7d5b51652adda85d79e368597df68d64Elliott Hughes DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get())); 2156f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 2166f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Resolving exceptions may load classes that reference more exceptions, iterate until no 2176f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // more are found 2186f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } while (!unresolved_exception_types.empty()); 2196f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers 220ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // We walk the roots looking for classes so that we'll pick up the 221ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // above classes plus any classes them depend on such super 222ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // classes, interfaces, and the required ClassLinker roots. 223ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>()); 2246f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get()); 225ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom CHECK_NE(image_classes->size(), 0U); 226ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return image_classes.release(); 227254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 228254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson 2291212a022fa5f8ef9585d765b1809521812af882cIan Rogers const CompilerDriver* CreateOatFile(const std::string& boot_image_option, 2303f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string* host_prefix, 2313f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string& android_root, 2323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool is_host, 2333f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::vector<const DexFile*>& dex_files, 2343f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom File* oat_file, 2353f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string& bitcode_filename, 2363f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool image, 2373f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::set<std::string>* image_classes, 2383f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool dump_stats, 2393f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool dump_timings) 240b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 241ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // SirtRef and ClassLoader creation needs to come after Runtime::Create 24200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers jobject class_loader = NULL; 243ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!boot_image_option.empty()) { 244ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 245ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::vector<const DexFile*> class_path_files(dex_files); 246a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); 247ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < class_path_files.size(); i++) { 248ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom class_linker->RegisterDexFile(*class_path_files[i]); 249ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccessUnchecked soa(Thread::Current()); 25100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); 25200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedLocalRef<jobject> class_loader_local(soa.Env(), 25300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); 25400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); 25500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); 256254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 257ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 2581212a022fa5f8ef9585d765b1809521812af882cIan Rogers UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_, 2591212a022fa5f8ef9585d765b1809521812af882cIan Rogers instruction_set_, 2601212a022fa5f8ef9585d765b1809521812af882cIan Rogers image, 2611212a022fa5f8ef9585d765b1809521812af882cIan Rogers thread_count_, 2621212a022fa5f8ef9585d765b1809521812af882cIan Rogers support_debugging_, 2631212a022fa5f8ef9585d765b1809521812af882cIan Rogers image_classes, 2641212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_stats, 2651212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_timings)); 2668b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien 267c928de90ad22ecdf83c18a07008409595f13d3b1Ian Rogers if (compiler_backend_ == kPortable) { 2681212a022fa5f8ef9585d765b1809521812af882cIan Rogers driver->SetBitcodeFileName(bitcode_filename); 269c531cefbfb5394413122e9f57d211ba436cff012buzbee } 2708b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien 27100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromRunnableToSuspended(kNative); 27200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 2731212a022fa5f8ef9585d765b1809521812af882cIan Rogers driver->CompileAll(class_loader, dex_files); 27400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromSuspendedToRunnable(); 276ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 27781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom std::string image_file_location; 27828db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom uint32_t image_file_location_oat_checksum = 0; 279700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom uint32_t image_file_location_oat_data_begin = 0; 28081f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom Heap* heap = Runtime::Current()->GetHeap(); 28181f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom if (heap->GetSpaces().size() > 1) { 28281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom ImageSpace* image_space = heap->GetImageSpace(); 28328db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); 284700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom image_file_location_oat_data_begin = reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin()); 28581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom image_file_location = image_space->GetImageFilename(); 28634f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { 28734f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom image_file_location = image_file_location.substr(host_prefix->size()); 28881f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom } 28981f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom } 29081f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom 291700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom std::vector<uint8_t> oat_contents; 292700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom VectorOutputStream vector_output_stream(oat_file->GetPath(), oat_contents); 293700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (!OatWriter::Create(vector_output_stream, 29481f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom dex_files, 29528db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom image_file_location_oat_checksum, 296700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom image_file_location_oat_data_begin, 29781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom image_file_location, 2981212a022fa5f8ef9585d765b1809521812af882cIan Rogers *driver.get())) { 299761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes LOG(ERROR) << "Failed to create oat file " << oat_file->GetPath(); 300f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom return NULL; 301ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 302700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 3033f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (!driver->WriteElf(android_root, is_host, dex_files, oat_contents, oat_file)) { 304700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); 305700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return NULL; 306700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 307700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 3081212a022fa5f8ef9585d765b1809521812af882cIan Rogers return driver.release(); 309254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 310ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 311a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom bool CreateImageFile(const std::string& image_filename, 312ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom uintptr_t image_base, 313ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const std::set<std::string>* image_classes, 314ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const std::string& oat_filename, 315f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom const std::string& oat_location, 3161212a022fa5f8ef9585d765b1809521812af882cIan Rogers const CompilerDriver& compiler) 317b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers LOCKS_EXCLUDED(Locks::mutator_lock_) { 318700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom uintptr_t oat_data_begin; 319700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom { 320700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // ImageWriter is scoped so it can free memory before doing FixupElf 321700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom ImageWriter image_writer(image_classes); 322700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) { 323700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to create image file " << image_filename; 324700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return false; 325700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 326700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom oat_data_begin = image_writer.GetOatDataBegin(); 327700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 328700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 329700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false)); 330700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (oat_file.get() == NULL) { 331700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom PLOG(ERROR) << "Failed to open ELF file: " << oat_filename; 332700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return false; 333700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 334700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (!compiler.FixupElf(oat_file.get(), oat_data_begin)) { 335700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); 336ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return false; 337ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 338ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 339ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 340ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 341ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom private: 342c531cefbfb5394413122e9f57d211ba436cff012buzbee explicit Dex2Oat(Runtime* runtime, CompilerBackend compiler_backend, InstructionSet instruction_set, 343c531cefbfb5394413122e9f57d211ba436cff012buzbee size_t thread_count, bool support_debugging) 344c531cefbfb5394413122e9f57d211ba436cff012buzbee : compiler_backend_(compiler_backend), 345c531cefbfb5394413122e9f57d211ba436cff012buzbee instruction_set_(instruction_set), 34649c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers runtime_(runtime), 347de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes thread_count_(thread_count), 348de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes support_debugging_(support_debugging), 349de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes start_ns_(NanoTime()) { 350bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes } 351ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 35200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) 353b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { 35400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!Runtime::Create(options, false)) { 355ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to create runtime"; 35600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return false; 357ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 35800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime* runtime = Runtime::Current(); 359ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // if we loaded an existing image, we will reuse values from the image roots. 360ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!runtime->HasJniDlsymLookupStub()) { 3611212a022fa5f8ef9585d765b1809521812af882cIan Rogers runtime->SetJniDlsymLookupStub(CompilerDriver::CreateJniDlsymLookupStub(instruction_set)); 362ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 363ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!runtime->HasAbstractMethodErrorStubArray()) { 3641212a022fa5f8ef9585d765b1809521812af882cIan Rogers runtime->SetAbstractMethodErrorStubArray(CompilerDriver::CreateAbstractMethodErrorStub(instruction_set)); 365ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 366ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { 367ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Runtime::TrampolineType type = Runtime::TrampolineType(i); 368ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!runtime->HasResolutionStubArray(type)) { 3691212a022fa5f8ef9585d765b1809521812af882cIan Rogers runtime->SetResolutionStubArray(CompilerDriver::CreateResolutionStub(instruction_set, type), type); 370ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 371ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 3721984651929744dd603fd082e23eacd877b9bc177Ian Rogers if (!runtime->HasResolutionMethod()) { 3731984651929744dd603fd082e23eacd877b9bc177Ian Rogers runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); 3741984651929744dd603fd082e23eacd877b9bc177Ian Rogers } 375ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 376ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 377ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!runtime->HasCalleeSaveMethod(type)) { 37849c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); 379ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 380ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 3811984651929744dd603fd082e23eacd877b9bc177Ian Rogers runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); 38200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return true; 383ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 384ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 3856f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers static void ResolveExceptionsForMethod(MethodHelper* mh, 38600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) 387b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 3886f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const DexFile::CodeItem* code_item = mh->GetCodeItem(); 3896f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (code_item == NULL) { 3906f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers return; // native or abstract method 3916f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 3926f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (code_item->tries_size_ == 0) { 3936f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers return; // nothing to process 3946f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 3956f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); 3966f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); 3976f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (size_t i = 0; i < num_encoded_catch_handlers; i++) { 3986f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); 3996f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers bool has_catch_all = false; 4006f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (encoded_catch_handler_size <= 0) { 4016f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers encoded_catch_handler_size = -encoded_catch_handler_size; 4026f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers has_catch_all = true; 4036f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4046f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (int32_t j = 0; j < encoded_catch_handler_size; j++) { 4056f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers uint16_t encoded_catch_handler_handlers_type_idx = 4066f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers DecodeUnsignedLeb128(&encoded_catch_handler_list); 4076f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // Add to set of types to resolve if not already in the dex cache resolved types 4086f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { 4096f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers exceptions_to_resolve.insert( 4106f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, 4116f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers &mh->GetDexFile())); 4126f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4136f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // ignore address associated with catch handler 4146f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers DecodeUnsignedLeb128(&encoded_catch_handler_list); 4156f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4166f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers if (has_catch_all) { 4176f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers // ignore catch all address 4186f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers DecodeUnsignedLeb128(&encoded_catch_handler_list); 4196f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4206f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4216f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 4232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg) 424b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 4256f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = 4266f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); 4276f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers MethodHelper mh; 4286f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { 4292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* m = c->GetVirtualMethod(i); 4306f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers mh.ChangeMethod(m); 4316f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); 4326f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4336f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers for (size_t i = 0; i < c->NumDirectMethods(); ++i) { 4342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* m = c->GetDirectMethod(i); 4356f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers mh.ChangeMethod(m); 4366f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); 4376f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 4386f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers return true; 4396f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers } 44000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 4412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg) 442b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 443ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); 444ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (klass->IsArrayClass() || klass->IsPrimitive()) { 445ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 446ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 4476d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers image_classes->insert(ClassHelper(klass).GetDescriptor()); 448ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 449ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 450ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 451ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Appends to dex_files any elements of class_path that it doesn't already 452ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // contain. This will open those dex files as necessary. 453ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { 454ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::vector<std::string> parsed; 455ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Split(class_path, ':', parsed); 456ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < parsed.size(); ++i) { 457ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (DexFilesContains(dex_files, parsed[i])) { 458ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom continue; 459ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 460a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]); 461ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (dex_file == NULL) { 462ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(WARNING) << "Failed to open dex file " << parsed[i]; 463ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } else { 464ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom dex_files.push_back(dex_file); 465ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 466ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 467ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 468ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 469ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Returns true if dex_files has a dex with the named location. 470ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { 471ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < dex_files.size(); ++i) { 472ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (dex_files[i]->GetLocation() == location) { 473ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 474ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 475ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 476ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return false; 477ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 478ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 479c531cefbfb5394413122e9f57d211ba436cff012buzbee const CompilerBackend compiler_backend_; 480c531cefbfb5394413122e9f57d211ba436cff012buzbee 48149c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers const InstructionSet instruction_set_; 482ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 483bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes Runtime* runtime_; 4845523ee070b005576c6f889415205d49ea77cf243Elliott Hughes size_t thread_count_; 485de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes bool support_debugging_; 486bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes uint64_t start_ns_; 487bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes 488ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); 489ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom}; 490254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson 49172395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic bool ParseInt(const char* in, int* out) { 492a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom char* end; 493a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int result = strtol(in, &end, 10); 494a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (in == end || *end != '\0') { 495a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return false; 496a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 497a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom *out = result; 498a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return true; 499a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom} 500a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 501bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughesstatic size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, 502bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes const std::vector<const char*>& dex_locations, 503bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes std::vector<const DexFile*>& dex_files) { 504bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = 0; 5055b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom for (size_t i = 0; i < dex_filenames.size(); i++) { 5065b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom const char* dex_filename = dex_filenames[i]; 507a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const char* dex_location = dex_locations[i]; 508a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(dex_filename, dex_location); 5095b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom if (dex_file == NULL) { 51060f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao LOG(WARNING) << "could not open .dex from file " << dex_filename; 511bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes ++failure_count; 51260f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao } else { 51360f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao dex_files.push_back(dex_file); 5145b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 5155b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 516bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return failure_count; 5175b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom} 5185b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom 519bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// The primary goal of the watchdog is to prevent stuck build servers 520bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// during development when fatal aborts lead to a cascade of failures 521bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// that result in a deadlock. 522bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromclass WatchDog { 523bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 524bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks 525bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#undef CHECK_PTHREAD_CALL 526bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ 527bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom do { \ 528bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom int rc = call args; \ 529bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom if (rc != 0) { \ 530bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom errno = rc; \ 531bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom std::string message(# call); \ 532bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom message += " failed for "; \ 533bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom message += reason; \ 534bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom Die(message); \ 535bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } \ 536bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } while (false) 537bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 538bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom public: 539994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom WatchDog(bool is_watch_dog_enabled) { 540994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom is_watch_dog_enabled_ = is_watch_dog_enabled; 541994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom if (!is_watch_dog_enabled_) { 542bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return; 543bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 544bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom shutting_down_ = false; 545bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread startup"; 546bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason); 547bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason); 548bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); 549bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); 550bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); 551bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 552bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom ~WatchDog() { 553994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom if (!is_watch_dog_enabled_) { 554bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return; 555bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 556bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread shutdown"; 557bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); 558bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom shutting_down_ = true; 559bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); 560bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 561bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 562bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason); 563bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 564bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); 565bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); 566bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 567bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 568bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom private: 569bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom static void* CallBack(void* arg) { 570bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom WatchDog* self = reinterpret_cast<WatchDog*>(arg); 571bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom self->Wait(); 572bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return NULL; 573bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 574bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 575eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static void Warn(const std::string& message) { 576eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers // TODO: Switch to LOG(WARNING) when we can guarantee it won't prevent shutdown in error cases. 577eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers fprintf(stderr, "%s\n", message.c_str()); 578eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } 579eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers 580bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom static void Die(const std::string& message) { 581eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers // TODO: Switch to LOG(FATAL) when we can guarantee it won't prevent shutdown in error cases. 582bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom fprintf(stderr, "%s\n", message.c_str()); 583bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom exit(1); 584bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 585bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 586bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom void Wait() { 587eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers bool warning = true; 588eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers timespec warning_ts, timeout_ts; 589eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds); 590eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers InitTimeSpec(true, CLOCK_REALTIME, kWatchDogWarningSeconds * 1000, 0, &warning_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 { 606eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers Die(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))); 611bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom Die(message.c_str()); 612bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 613bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 614bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 615bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 616bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 617eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static const unsigned int kWatchDogWarningSeconds = 1 * 60; // 1 minute. 618c928de90ad22ecdf83c18a07008409595f13d3b1Ian Rogers#ifdef ART_USE_PORTABLE_COMPILER 619eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static const unsigned int kWatchDogTimeoutSeconds = 30 * 60; // 25 minutes + buffer. 620bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#else 621eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static const unsigned int kWatchDogTimeoutSeconds = 3 * 60; // 2 minutes + buffer. 622bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif 623bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 624994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom bool is_watch_dog_enabled_; 625bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom bool shutting_down_; 626bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases 627bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_mutex_t mutex_; 628bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_cond_t cond_; 629bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_attr_t attr_; 630bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_t pthread_; 631bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom}; 632bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 63372395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic int dex2oat(int argc, char** argv) { 6340d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes InitLogging(argv); 63572395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughes 63669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom // Skip over argv[0]. 63769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom argv++; 63869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom argc--; 63969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 64069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (argc == 0) { 641cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("no arguments specified"); 64269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 64369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 64469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom std::vector<const char*> dex_filenames; 645a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::vector<const char*> dex_locations; 646a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int zip_fd = -1; 647a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string zip_location; 648e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom std::string oat_filename; 649265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_symbols; 650a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string oat_location; 651a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int oat_fd = -1; 6528b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien std::string bitcode_filename; 653ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const char* image_classes_filename = NULL; 654a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string image_filename; 655b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom std::string boot_image_filename; 65669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom uintptr_t image_base = 0; 65734f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom UniquePtr<std::string> host_prefix; 6583f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom std::string android_root; 6595d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao std::vector<const char*> runtime_args; 6605c599943f8c347acd84c4d9fda56a9df70649b78Elliott Hughes int thread_count = sysconf(_SC_NPROCESSORS_CONF); 661de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes bool support_debugging = false; 662c531cefbfb5394413122e9f57d211ba436cff012buzbee#if defined(ART_USE_PORTABLE_COMPILER) 663c531cefbfb5394413122e9f57d211ba436cff012buzbee CompilerBackend compiler_backend = kPortable; 664c531cefbfb5394413122e9f57d211ba436cff012buzbee#else 665c531cefbfb5394413122e9f57d211ba436cff012buzbee CompilerBackend compiler_backend = kQuick; 666c531cefbfb5394413122e9f57d211ba436cff012buzbee#endif 6679ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#if defined(__arm__) 66849c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers InstructionSet instruction_set = kThumb2; 6699ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__i386__) 6709ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao InstructionSet instruction_set = kX86; 6719ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__mips__) 6729ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao InstructionSet instruction_set = kMips; 6739ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#else 6749ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#error "Unsupported architecture" 6759ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#endif 676265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool is_host = false; 67767d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes bool dump_stats = kIsDebugBuild; 67867d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes bool dump_timings = kIsDebugBuild; 679994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom bool watch_dog_enabled = !kIsTargetBuild; 680161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom 68169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom for (int i = 0; i < argc; i++) { 68269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom const StringPiece option(argv[i]); 683a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom bool log_options = false; 684a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (log_options) { 685b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 686b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom } 68769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (option.starts_with("--dex-file=")) { 68869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); 689a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--dex-location=")) { 690a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); 691a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--zip-fd=")) { 692a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); 693bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes if (!ParseInt(zip_fd_str, &zip_fd)) { 694cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str); 695a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 696a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--zip-location=")) { 697a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom zip_location = option.substr(strlen("--zip-location=")).data(); 698a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--oat-file=")) { 699a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom oat_filename = option.substr(strlen("--oat-file=")).data(); 700265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else if (option.starts_with("--oat-symbols=")) { 701265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_symbols = option.substr(strlen("--oat-symbols=")).data(); 702a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--oat-fd=")) { 703a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); 704bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes if (!ParseInt(oat_fd_str, &oat_fd)) { 705cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str); 706a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 707994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "-g") { 708de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes support_debugging = true; 709994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "--watch-dog") { 710994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom watch_dog_enabled = true; 711994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "--no-watch-dog") { 712994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom watch_dog_enabled = false; 7135523ee070b005576c6f889415205d49ea77cf243Elliott Hughes } else if (option.starts_with("-j")) { 714b12552a95d68b9e4a567103190074ae47b6a61dcBrian Carlstrom const char* thread_count_str = option.substr(strlen("-j")).data(); 7155523ee070b005576c6f889415205d49ea77cf243Elliott Hughes if (!ParseInt(thread_count_str, &thread_count)) { 716cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse -j argument '%s' as an integer", thread_count_str); 7175523ee070b005576c6f889415205d49ea77cf243Elliott Hughes } 718a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--oat-location=")) { 719a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom oat_location = option.substr(strlen("--oat-location=")).data(); 7208b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien } else if (option.starts_with("--bitcode=")) { 7218b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien bitcode_filename = option.substr(strlen("--bitcode=")).data(); 72269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else if (option.starts_with("--image=")) { 72369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom image_filename = option.substr(strlen("--image=")).data(); 724ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } else if (option.starts_with("--image-classes=")) { 725ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom image_classes_filename = option.substr(strlen("--image-classes=")).data(); 72669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else if (option.starts_with("--base=")) { 72769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom const char* image_base_str = option.substr(strlen("--base=")).data(); 72869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom char* end; 72969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom image_base = strtoul(image_base_str, &end, 16); 73069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (end == image_base_str || *end != '\0') { 731cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Failed to parse hexadecimal value for option %s", option.data()); 73269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 733e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } else if (option.starts_with("--boot-image=")) { 734b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_filename = option.substr(strlen("--boot-image=")).data(); 73558ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom } else if (option.starts_with("--host-prefix=")) { 73634f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); 7373f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } else if (option.starts_with("--android-root=")) { 7383f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root = option.substr(strlen("--android-root=")).data(); 73949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers } else if (option.starts_with("--instruction-set=")) { 74049c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); 7411f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao if (instruction_set_str == "arm") { 74249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kThumb2; 7431f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao } else if (instruction_set_str == "mips") { 74449c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kMips; 7451f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao } else if (instruction_set_str == "x86") { 74649c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kX86; 74749c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers } 748c531cefbfb5394413122e9f57d211ba436cff012buzbee } else if (option.starts_with("--compiler-backend=")) { 749c531cefbfb5394413122e9f57d211ba436cff012buzbee StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); 750c531cefbfb5394413122e9f57d211ba436cff012buzbee if (backend_str == "Quick") { 751c531cefbfb5394413122e9f57d211ba436cff012buzbee compiler_backend = kQuick; 752c531cefbfb5394413122e9f57d211ba436cff012buzbee } else if (backend_str == "QuickGBC") { 753c531cefbfb5394413122e9f57d211ba436cff012buzbee compiler_backend = kQuickGBC; 754c531cefbfb5394413122e9f57d211ba436cff012buzbee } else if (backend_str == "Portable") { 755c531cefbfb5394413122e9f57d211ba436cff012buzbee compiler_backend = kPortable; 756c531cefbfb5394413122e9f57d211ba436cff012buzbee } 757265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else if (option == "--host") { 758265091e581c9f643b37e7966890911f09e223269Brian Carlstrom is_host = true; 7595d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } else if (option == "--runtime-arg") { 7605d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao if (++i >= argc) { 761cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Missing required argument for --runtime-arg"); 7625d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } 763a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (log_options) { 764a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 765a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 7665d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao runtime_args.push_back(argv[i]); 76769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else { 768cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("unknown argument %s", option.data()); 76969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 77069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 77169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 772a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (oat_filename.empty() && oat_fd == -1) { 773cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Output must be supplied with either --oat-file or --oat-fd"); 774a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 775a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 776a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (!oat_filename.empty() && oat_fd != -1) { 777cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--oat-file should not be used with --oat-fd"); 778a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 779a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 780265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty() && oat_fd != -1) { 781265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Usage("--oat-symbols should not be used with --oat-fd"); 782265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 783265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 784265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty() && is_host) { 785265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Usage("--oat-symbols should not be used with --host"); 786a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 787a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 788a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (oat_fd != -1 && !image_filename.empty()) { 789cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--oat-fd should not be used with --image"); 790e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } 791e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom 79234f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() == NULL) { 793b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); 794b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom if (android_product_out != NULL) { 79534f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom host_prefix.reset(new std::string(android_product_out)); 796b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 797b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 798b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom 7993f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (android_root.empty()) { 8003f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const char* android_root_env_var = getenv("ANDROID_ROOT"); 8013f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (android_root_env_var == NULL) { 80254d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom Usage("--android-root unspecified and ANDROID_ROOT not set"); 8033f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } 8043f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root += android_root_env_var; 8053f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } 8063f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom 807a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom bool image = (!image_filename.empty()); 808b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom if (!image && boot_image_filename.empty()) { 80934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() == NULL) { 810a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += GetAndroidRoot(); 811a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom } else { 81234f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom boot_image_filename += *host_prefix.get(); 813a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += "/system"; 814a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom } 815a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += "/framework/boot.art"; 816b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 817b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom std::string boot_image_option; 818a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (!boot_image_filename.empty()) { 819b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_option += "-Ximage:"; 820b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_option += boot_image_filename; 82169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 82269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 823ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL && !image) { 824cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--image-classes should only be used with --image"); 825ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 826ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 827ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL && !boot_image_option.empty()) { 828cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--image-classes should not be used with --boot-image"); 82978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom } 83078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom 831a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_filenames.empty() && zip_fd == -1) { 832cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Input must be supplied with either --dex-file or --zip-fd"); 833a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 834a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 835a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (!dex_filenames.empty() && zip_fd != -1) { 836cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-file should not be used with --zip-fd"); 837a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 838a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 839a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (!dex_filenames.empty() && !zip_location.empty()) { 840cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-file should not be used with --zip-location"); 841a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 842a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 843a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (dex_locations.empty()) { 844a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom for (size_t i = 0; i < dex_filenames.size(); i++) { 845a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom dex_locations.push_back(dex_filenames[i]); 846a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 847a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (dex_locations.size() != dex_filenames.size()) { 848cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-location arguments do not match --dex-file arguments"); 849a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 850a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom 851a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (zip_fd != -1 && zip_location.empty()) { 852cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--zip-location should be supplied with --zip-fd"); 853a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 854a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 85569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (boot_image_option.empty()) { 85669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (image_base == 0) { 857cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("non-zero --base not specified"); 85869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 85969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 86069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 861265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_stripped(oat_filename); 862265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_unstripped; 863265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty()) { 864265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped += oat_symbols; 865265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else { 866265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped += oat_filename; 867265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 868265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 869994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom // Done with usage checks, enable watchdog if requested 870994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom WatchDog watch_dog(watch_dog_enabled); 871994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom 8726ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom // Check early that the result of compilation can be written 873a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom UniquePtr<File> oat_file; 874265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor 8756cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom if (create_file) { 876265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(OS::OpenFile(oat_unstripped.c_str(), true)); 877a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (oat_location.empty()) { 878a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom oat_location = oat_filename; 879a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 880a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 881761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes oat_file.reset(new File(oat_fd, oat_location)); 882761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes oat_file->DisableAutoClose(); 883a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 8846ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom if (oat_file.get() == NULL) { 8856cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom PLOG(ERROR) << "Failed to create oat file: " << oat_location; 8866cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom return EXIT_FAILURE; 8876cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom } 8886cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { 8896cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; 8906ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom return EXIT_FAILURE; 891234da578a2d91ed7f2ef47b2ec23fb0033e2746bElliott Hughes } 892a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 893a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(INFO) << "dex2oat: " << oat_location; 8945e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers 89569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom Runtime::Options options; 8965de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); 897a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::vector<const DexFile*> boot_class_path; 89869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (boot_image_option.empty()) { 899bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); 900bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes if (failure_count > 0) { 901bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes LOG(ERROR) << "Failed to open some dex files: " << failure_count; 902bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return EXIT_FAILURE; 903bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes } 904a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom options.push_back(std::make_pair("bootclasspath", &boot_class_path)); 90569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else { 90669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); 90769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 90834f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() != NULL) { 90934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); 91058ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom } 9115d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao for (size_t i = 0; i < runtime_args.size(); i++) { 9125d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); 9135d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } 91469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 91500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Dex2Oat* p_dex2oat; 916c531cefbfb5394413122e9f57d211ba436cff012buzbee if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count, support_debugging)) { 91700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(ERROR) << "Failed to create dex2oat"; 91800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return EXIT_FAILURE; 91900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 92000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers UniquePtr<Dex2Oat> dex2oat(p_dex2oat); 92100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, 92200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // give it away now and then switch to a more managable ScopedObjectAccess. 92300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromRunnableToSuspended(kNative); 92400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Whilst we're in native take the opportunity to initialize well known classes. 92500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv()); 92600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 92769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 928bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes // If --image-classes was specified, calculate the full list of classes to include in the image 929ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom UniquePtr<const std::set<std::string> > image_classes(NULL); 930ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL) { 931ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); 932ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes.get() == NULL) { 933ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; 934ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return EXIT_FAILURE; 93569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 93669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 93769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 938a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom std::vector<const DexFile*> dex_files; 939a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (boot_image_option.empty()) { 940a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); 941a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 942a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_filenames.empty()) { 943a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd)); 944a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (zip_archive.get() == NULL) { 945a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location; 9462e3d1b262af0839380e1d60e86d8b281943ef944Brian Carlstrom return EXIT_FAILURE; 947a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 948a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location); 949a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_file == NULL) { 950a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location; 951a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return EXIT_FAILURE; 952a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 953a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom dex_files.push_back(dex_file); 954a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 955bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); 956bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes if (failure_count > 0) { 957bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes LOG(ERROR) << "Failed to open some dex files: " << failure_count; 958bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return EXIT_FAILURE; 959bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes } 960a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 961a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 962a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 9631212a022fa5f8ef9585d765b1809521812af882cIan Rogers UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option, 9641212a022fa5f8ef9585d765b1809521812af882cIan Rogers host_prefix.get(), 9653f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root, 966265091e581c9f643b37e7966890911f09e223269Brian Carlstrom is_host, 9671212a022fa5f8ef9585d765b1809521812af882cIan Rogers dex_files, 9681212a022fa5f8ef9585d765b1809521812af882cIan Rogers oat_file.get(), 9691212a022fa5f8ef9585d765b1809521812af882cIan Rogers bitcode_filename, 9701212a022fa5f8ef9585d765b1809521812af882cIan Rogers image, 9711212a022fa5f8ef9585d765b1809521812af882cIan Rogers image_classes.get(), 9721212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_stats, 9731212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_timings)); 974f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom 975f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom if (compiler.get() == NULL) { 976a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to create oat file: " << oat_location; 977e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom return EXIT_FAILURE; 978e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } 979e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom 980265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Oat file written successfully (unstripped): " << oat_location; 981aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom 982700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Notes on the interleaving of creating the image and oat file to 983700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // ensure the references between the two are correct. 984700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 985700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Currently we have a memory layout that looks something like this: 986700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 987700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 988700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | image | 989700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 990700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | boot oat | 991700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 992700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | alloc spaces | 993700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 994700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 995700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // There are several constraints on the loading of the imag and boot.oat. 996700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 997700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1. The image is expected to be loaded at an absolute address and 998700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // contains Objects with absolute pointers within the image. 999700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1000700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 2. There are absolute pointers from Methods in the image to their 1001700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // code in the oat. 1002700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1003700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 3. There are absolute pointers from the code in the oat to Methods 1004700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // in the image. 1005700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1006700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 4. There are absolute pointers from code in the oat to other code 1007700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // in the oat. 1008700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1009700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // To get this all correct, we go through several steps. 1010700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1011700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1. We have already created that oat file above with 1012700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // CreateOatFile. Originally this was just our own proprietary file 1013700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // but now it is contained within an ELF dynamic object (aka .so 1014700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // file). The Compiler returned by CreateOatFile provides 1015700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // PatchInformation for references to oat code and Methods that need 1016700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // to be update once we know where the oat file will be located 1017700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // after the image. 1018700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1019700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 2. We create the image file. It needs to know where the oat file 1020700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // will be loaded after itself. Originally when oat file was simply 1021700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // memory mapped so we could predict where its contents were based 1022700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // on the file size. Now that it is an ELF file, we need to inspect 1023700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // the ELF file to understand the in memory segment layout including 1024700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // where the oat header is located within. ImageWriter's 1025700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // PatchOatCodeAndMethods uses the PatchInformation from the 1026700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Compiler to touch up absolute references in the oat file. 1027700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1028700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 3. We fixup the ELF program headers so that dlopen will try to 1029700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // load the .so at the desired location at runtime by offsetting the 1030700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Elf32_Phdr.p_vaddr values by the desired base address. 1031700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1032265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (image) { 1033265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Thread::Current()->TransitionFromRunnableToSuspended(kNative); 1034265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool image_creation_success = dex2oat->CreateImageFile(image_filename, 1035265091e581c9f643b37e7966890911f09e223269Brian Carlstrom image_base, 1036265091e581c9f643b37e7966890911f09e223269Brian Carlstrom image_classes.get(), 1037265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped, 1038265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_location, 1039265091e581c9f643b37e7966890911f09e223269Brian Carlstrom *compiler.get()); 1040265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Thread::Current()->TransitionFromSuspendedToRunnable(); 1041265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Image written successfully: " << image_filename; 1042265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!image_creation_success) { 1043265091e581c9f643b37e7966890911f09e223269Brian Carlstrom return EXIT_FAILURE; 1044265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1045265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1046265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1047265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (is_host) { 1048265091e581c9f643b37e7966890911f09e223269Brian Carlstrom return EXIT_SUCCESS; 104969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 105069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 1051265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // If we don't want to strip in place, copy from unstripped location to stripped location. 1052265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // We need to strip after image creation because FixupElf needs to use .strtab. 1053265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (oat_unstripped != oat_stripped) { 1054265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(); 1055265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<File> in(OS::OpenFile(oat_unstripped.c_str(), false)); 1056265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<File> out(OS::OpenFile(oat_stripped.c_str(), true)); 1057265091e581c9f643b37e7966890911f09e223269Brian Carlstrom size_t buffer_size = 8192; 1058265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]); 1059265091e581c9f643b37e7966890911f09e223269Brian Carlstrom while (true) { 1060265091e581c9f643b37e7966890911f09e223269Brian Carlstrom int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); 1061265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (bytes_read <= 0) { 1062265091e581c9f643b37e7966890911f09e223269Brian Carlstrom break; 1063265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1064265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool write_ok = out->WriteFully(buffer.get(), bytes_read); 1065265091e581c9f643b37e7966890911f09e223269Brian Carlstrom CHECK(write_ok); 1066265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1067265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(out.release()); 1068265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Oat file copied successfully (stripped): " << oat_stripped; 1069265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1070265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1071265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // Strip unneeded sections for target 1072265091e581c9f643b37e7966890911f09e223269Brian Carlstrom off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET); 1073265091e581c9f643b37e7966890911f09e223269Brian Carlstrom CHECK_EQ(0, seek_actual); 1074265091e581c9f643b37e7966890911f09e223269Brian Carlstrom compiler->StripElf(oat_file.get()); 1075265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1076ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // We wrote the oat file successfully, and want to keep it. 107754d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom LOG(INFO) << "Oat file written successfully (stripped): " << oat_location; 107869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom return EXIT_SUCCESS; 107969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 108069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 108169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} // namespace art 108269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 108369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromint main(int argc, char** argv) { 108469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom return art::dex2oat(argc, argv); 108569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 1086