dex2oat.cc revision 6f28d91aab952e3244fbb4e707fa38f85538f374
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/* 22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project 32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License. 62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at 72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software 112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and 142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License. 152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */ 1669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 1769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <stdio.h> 1869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <stdlib.h> 196cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom#include <sys/stat.h> 2069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 21ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom#include <fstream> 224922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom#include <iostream> 234922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom#include <sstream> 2469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <string> 2569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include <vector> 2669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 271aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h" 28e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/stringpiece.h" 29a84395489098e4531619b1cffd1afc282b14510eSameer Abu Asal#include "base/timing_logger.h" 30761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h" 3169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "class_linker.h" 324f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h" 337940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "driver/compiler_driver.h" 347940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "elf_fixup.h" 357940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "elf_stripper.h" 361d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h" 371d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h" 387940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "image_writer.h" 396f1dfe415019de95f0305de66b3afb40005fe382Ian Rogers#include "leb128.h" 402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/abstract_method-inl.h" 412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h" 422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h" 432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h" 442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h" 457940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "oat_writer.h" 466d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 475e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers#include "os.h" 4869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom#include "runtime.h" 4900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "ScopedLocalRef.h" 5000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 511f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h" 52700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "vector_output_stream.h" 5300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "well_known_classes.h" 54a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom#include "zip_archive.h" 5569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 5669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromnamespace art { 5769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 58cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageErrorV(const char* fmt, va_list ap) { 59cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom std::string error; 60cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom StringAppendV(&error, fmt, ap); 61cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom LOG(ERROR) << error; 62cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom} 63cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 64cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void UsageError(const char* fmt, ...) { 65cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_list ap; 66cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_start(ap, fmt); 67cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageErrorV(fmt, ap); 68cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_end(ap); 69cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom} 70cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 71cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstromstatic void Usage(const char* fmt, ...) { 72cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_list ap; 73cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_start(ap, fmt); 74cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageErrorV(fmt, ap); 75cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom va_end(ap); 76cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom 77cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError("Usage: dex2oat [options]..."); 78cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 79cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); 80cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --dex-file=/system/framework/core.jar"); 81cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 82cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); 83cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" containing a classes.dex file to compile."); 84cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --zip-fd=5"); 85cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 864560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file"); 874560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom UsageError(" corresponding to the file descriptor specified by --zip-fd."); 88cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --zip-location=/system/app/Calculator.apk"); 89cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 90265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); 91265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" Example: --oat-file=/system/framework/boot.oat"); 92265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 93265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); 94cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --oat-file=/system/framework/boot.oat"); 95cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 96cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); 97cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" to the file descriptor specified by --oat-fd."); 987675e16bcae06c0fe258aad89b3d511037dec399Brian Carlstrom UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat"); 99cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 100265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); 101265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); 102265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 1038b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); 1048b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(" Example: --bitcode=/system/framework/boot.bc"); 1058b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien UsageError(""); 106cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --image=<file.art>: specifies the output image filename."); 107cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --image=/system/framework/boot.art"); 108cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 109cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); 110cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --image=frameworks/base/preloaded-classes"); 111cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 112cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); 113cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --base=0x50000000"); 114cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 115cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); 116cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --boot-image=/system/framework/boot.art"); 117cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Default: <host-prefix>/system/framework/boot.art"); 118cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 119265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --host-prefix=<path>: used to translate host paths to target paths during"); 120cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" cross compilation."); 121cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --host-prefix=out/target/product/crespo"); 122cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Default: $ANDROID_PRODUCT_OUT"); 123cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 1243f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" --android-root=<path>: used to locate libraries for portable linking."); 1253f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" Example: --android-root=out/host/linux-x86"); 1263f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(" Default: $ANDROID_ROOT"); 1273f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom UsageError(""); 1281f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction"); 12949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers UsageError(" set."); 1301f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" Example: --instruction-set=x86"); 1311f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao UsageError(" Default: arm"); 13249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers UsageError(""); 133c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend"); 134c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" set."); 135c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" Example: --instruction-set=Portable"); 136c531cefbfb5394413122e9f57d211ba436cff012buzbee UsageError(" Default: Quick"); 137265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 138265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(" --host: used with Portable backend to link against host runtime libraries"); 139265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UsageError(""); 140cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); 141cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" such as initial heap size, maximum heap size, and verbose output."); 142cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Use a separate --runtime-arg switch for each argument."); 143cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(" Example: --runtime-arg -Xms256m"); 144cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom UsageError(""); 145cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom std::cerr << "See log for usage error information\n"; 14669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom exit(EXIT_FAILURE); 14769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 14869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 149ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstromclass Dex2Oat { 150ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom public: 1514560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom static bool Create(Dex2Oat** p_dex2oat, 1524560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom Runtime::Options& options, 1534560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom CompilerBackend compiler_backend, 1544560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom InstructionSet instruction_set, 1554560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom size_t thread_count) 156b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { 15700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!CreateRuntime(options, instruction_set)) { 15800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers *p_dex2oat = NULL; 15900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return false; 160254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 1610177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count); 16200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return true; 163ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 164ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 165ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ~Dex2Oat() { 166ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom delete runtime_; 1674560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) 1684560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom << " (threads: " << thread_count_ << ")"; 169ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 170ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 171c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum 1724560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) 1734922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom CompilerDriver::DescriptorSet* ReadImageClassesFromFile(const char* image_classes_filename) { 1744560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, 1754560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom std::ifstream::in)); 176ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_file.get() == NULL) { 177ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; 178ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return NULL; 179ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 1804922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom UniquePtr<CompilerDriver::DescriptorSet> result(ReadImageClasses(*image_classes_file.get())); 1814922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom image_classes_file->close(); 1824922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom return result.release(); 1834922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } 184ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 1854922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom CompilerDriver::DescriptorSet* ReadImageClasses(std::istream& image_classes_stream) { 18696391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom UniquePtr<CompilerDriver::DescriptorSet> image_classes(new CompilerDriver::DescriptorSet); 1874922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom while (image_classes_stream.good()) { 188ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::string dot; 1894922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom std::getline(image_classes_stream, dot); 190f1a5adc87760f938b01df26d906295063546b259Elliott Hughes if (StartsWith(dot, "#") || dot.empty()) { 191ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom continue; 192ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 193955724179c6c739524f610023287f56b24dc31deElliott Hughes std::string descriptor(DotToDescriptor(dot.c_str())); 19496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom image_classes->insert(descriptor); 195ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 196ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return image_classes.release(); 197254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 198254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson 1994560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) 2004560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom CompilerDriver::DescriptorSet* ReadImageClassesFromZip(const std::string& zip_filename, 2014560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom const char* image_classes_filename) { 2024922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename)); 2034922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom if (zip_archive.get() == NULL) { 2044922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom LOG(ERROR) << "Failed to open zip file " << zip_filename; 2054922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom return NULL; 2064922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } 2074922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom UniquePtr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename)); 2084922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom if (zip_entry.get() == NULL) { 2094922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom LOG(ERROR) << "Failed to find " << image_classes_filename << " within " << zip_filename; 2104922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom return NULL; 2114922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } 2124922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom UniquePtr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(image_classes_filename)); 2134922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom if (image_classes_file.get() == NULL) { 2144922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom LOG(ERROR) << "Failed to extract " << image_classes_filename << " from " << zip_filename; 2154922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom return NULL; 2164922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } 2174922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()), 2184922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom image_classes_file->Size()); 2194922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom std::istringstream image_classes_stream(image_classes_string); 2204922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom return ReadImageClasses(image_classes_stream); 2214922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } 2224922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom 2231212a022fa5f8ef9585d765b1809521812af882cIan Rogers const CompilerDriver* CreateOatFile(const std::string& boot_image_option, 2243f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string* host_prefix, 2253f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string& android_root, 2263f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool is_host, 2273f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::vector<const DexFile*>& dex_files, 2283f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom File* oat_file, 2293f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const std::string& bitcode_filename, 2303f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool image, 23196391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom UniquePtr<CompilerDriver::DescriptorSet>& image_classes, 2323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom bool dump_stats, 2336f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum base::TimingLogger& timings) 234b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 235ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // SirtRef and ClassLoader creation needs to come after Runtime::Create 23600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers jobject class_loader = NULL; 237ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!boot_image_option.empty()) { 238ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 239ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::vector<const DexFile*> class_path_files(dex_files); 240a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); 241ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < class_path_files.size(); i++) { 242ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom class_linker->RegisterDexFile(*class_path_files[i]); 243ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 24400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccessUnchecked soa(Thread::Current()); 24500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); 24600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedLocalRef<jobject> class_loader_local(soa.Env(), 24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); 24800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); 24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); 250254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 251ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 2521212a022fa5f8ef9585d765b1809521812af882cIan Rogers UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_, 2531212a022fa5f8ef9585d765b1809521812af882cIan Rogers instruction_set_, 2541212a022fa5f8ef9585d765b1809521812af882cIan Rogers image, 25596391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom image_classes.release(), 2561212a022fa5f8ef9585d765b1809521812af882cIan Rogers thread_count_, 2574560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom dump_stats)); 2588b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien 259c928de90ad22ecdf83c18a07008409595f13d3b1Ian Rogers if (compiler_backend_ == kPortable) { 2601212a022fa5f8ef9585d765b1809521812af882cIan Rogers driver->SetBitcodeFileName(bitcode_filename); 261c531cefbfb5394413122e9f57d211ba436cff012buzbee } 2628b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien 2638447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum 26400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromRunnableToSuspended(kNative); 26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 2664560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom driver->CompileAll(class_loader, dex_files, timings); 26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 26800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromSuspendedToRunnable(); 269ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 2706f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum timings.NewSplit("dex2oat OatWriter"); 27181f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom std::string image_file_location; 27228db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom uint32_t image_file_location_oat_checksum = 0; 273700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom uint32_t image_file_location_oat_data_begin = 0; 2740aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao if (!driver->IsImage()) { 2751d54e73444e017d3a65234e0f193846f3e27472bIan Rogers gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace(); 27628db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); 27739ebcb800aabedd0ffa6aa4aeac8aa4194c66e61Ian Rogers image_file_location_oat_data_begin = 27839ebcb800aabedd0ffa6aa4aeac8aa4194c66e61Ian Rogers reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin()); 27981f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom image_file_location = image_space->GetImageFilename(); 28034f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { 28134f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom image_file_location = image_file_location.substr(host_prefix->size()); 28281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom } 28381f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom } 28481f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom 285c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom OatWriter oat_writer(dex_files, 286c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom image_file_location_oat_checksum, 287c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom image_file_location_oat_data_begin, 288c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom image_file_location, 289c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom driver.get()); 290700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 291c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom if (!driver->WriteElf(android_root, is_host, dex_files, oat_writer, oat_file)) { 292700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); 293700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return NULL; 294700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 295700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 2961212a022fa5f8ef9585d765b1809521812af882cIan Rogers return driver.release(); 297254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson } 298ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 299a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom bool CreateImageFile(const std::string& image_filename, 300ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom uintptr_t image_base, 301ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const std::string& oat_filename, 302f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom const std::string& oat_location, 3031212a022fa5f8ef9585d765b1809521812af882cIan Rogers const CompilerDriver& compiler) 304b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers LOCKS_EXCLUDED(Locks::mutator_lock_) { 305700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom uintptr_t oat_data_begin; 306700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom { 307700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // ImageWriter is scoped so it can free memory before doing FixupElf 30896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom ImageWriter image_writer(compiler); 30996391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) { 310700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to create image file " << image_filename; 311700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return false; 312700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 313700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom oat_data_begin = image_writer.GetOatDataBegin(); 314700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 315700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom 316700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false)); 317700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom if (oat_file.get() == NULL) { 318700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom PLOG(ERROR) << "Failed to open ELF file: " << oat_filename; 319700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom return false; 320700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom } 32151c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom if (!ElfFixup::Fixup(oat_file.get(), oat_data_begin)) { 322700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); 323ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return false; 324ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 325ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 326ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 327ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 328ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom private: 3294560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom explicit Dex2Oat(Runtime* runtime, 3304560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom CompilerBackend compiler_backend, 3314560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom InstructionSet instruction_set, 3320177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom size_t thread_count) 333c531cefbfb5394413122e9f57d211ba436cff012buzbee : compiler_backend_(compiler_backend), 334c531cefbfb5394413122e9f57d211ba436cff012buzbee instruction_set_(instruction_set), 33549c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers runtime_(runtime), 336de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes thread_count_(thread_count), 337de6e4cf1b63acd7032a52826d9df21ff649d7128Elliott Hughes start_ns_(NanoTime()) { 338bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes } 339ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 34000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) 341b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { 34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!Runtime::Create(options, false)) { 343ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to create runtime"; 34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return false; 345ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime* runtime = Runtime::Current(); 347ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // if we loaded an existing image, we will reuse values from the image roots. 3481984651929744dd603fd082e23eacd877b9bc177Ian Rogers if (!runtime->HasResolutionMethod()) { 3491984651929744dd603fd082e23eacd877b9bc177Ian Rogers runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); 3501984651929744dd603fd082e23eacd877b9bc177Ian Rogers } 351ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 352ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 353ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (!runtime->HasCalleeSaveMethod(type)) { 35449c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); 355ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 356ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 3571984651929744dd603fd082e23eacd877b9bc177Ian Rogers runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); 35800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return true; 359ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 360ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 361ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Appends to dex_files any elements of class_path that it doesn't already 362ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // contain. This will open those dex files as necessary. 3634560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom static void OpenClassPathFiles(const std::string& class_path, 3644560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom std::vector<const DexFile*>& dex_files) { 365ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom std::vector<std::string> parsed; 366ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom Split(class_path, ':', parsed); 36733e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained. 36833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers ScopedObjectAccess soa(Thread::Current()); 369ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < parsed.size(); ++i) { 370ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (DexFilesContains(dex_files, parsed[i])) { 371ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom continue; 372ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 373a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]); 374ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (dex_file == NULL) { 375ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(WARNING) << "Failed to open dex file " << parsed[i]; 376ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } else { 377ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom dex_files.push_back(dex_file); 378ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 379ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 380ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 381ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 382ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // Returns true if dex_files has a dex with the named location. 3834560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, 3844560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom const std::string& location) { 385ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom for (size_t i = 0; i < dex_files.size(); ++i) { 386ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (dex_files[i]->GetLocation() == location) { 387ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return true; 388ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 389ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 390ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return false; 391ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 392ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 393c531cefbfb5394413122e9f57d211ba436cff012buzbee const CompilerBackend compiler_backend_; 394c531cefbfb5394413122e9f57d211ba436cff012buzbee 39549c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers const InstructionSet instruction_set_; 396ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 397bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes Runtime* runtime_; 3985523ee070b005576c6f889415205d49ea77cf243Elliott Hughes size_t thread_count_; 399bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes uint64_t start_ns_; 400bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes 401ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); 402ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom}; 403254db0ff7ea6509a1c2914b1d9532e2041a0c4c4Jesse Wilson 40472395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic bool ParseInt(const char* in, int* out) { 405a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom char* end; 406a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int result = strtol(in, &end, 10); 407a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (in == end || *end != '\0') { 408a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return false; 409a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 410a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom *out = result; 411a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return true; 412a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom} 413a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 414bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughesstatic size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, 415bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes const std::vector<const char*>& dex_locations, 416bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes std::vector<const DexFile*>& dex_files) { 417bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = 0; 4185b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom for (size_t i = 0; i < dex_filenames.size(); i++) { 4195b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom const char* dex_filename = dex_filenames[i]; 420a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const char* dex_location = dex_locations[i]; 421a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(dex_filename, dex_location); 4225b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom if (dex_file == NULL) { 42333e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers LOG(WARNING) << "Could not open .dex from file '" << dex_filename << "'\n"; 424bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes ++failure_count; 42560f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao } else { 42660f83e3274739317d8c3a1b069cebc6bf1e40f29jeffhao dex_files.push_back(dex_file); 4275b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 4285b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom } 429bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return failure_count; 4305b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom} 4315b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom 432bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// The primary goal of the watchdog is to prevent stuck build servers 433bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// during development when fatal aborts lead to a cascade of failures 434bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// that result in a deadlock. 435bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromclass WatchDog { 436bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks 437bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#undef CHECK_PTHREAD_CALL 438bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ 439bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom do { \ 440bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom int rc = call args; \ 441bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom if (rc != 0) { \ 442bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom errno = rc; \ 443bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom std::string message(# call); \ 444bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom message += " failed for "; \ 445bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom message += reason; \ 446ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Fatal(message); \ 447bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } \ 448bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } while (false) 449bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 450bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom public: 45193ba893c20532990a430741e0a97212900094e8cBrian Carlstrom explicit WatchDog(bool is_watch_dog_enabled) { 452994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom is_watch_dog_enabled_ = is_watch_dog_enabled; 453994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom if (!is_watch_dog_enabled_) { 454bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return; 455bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 456bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom shutting_down_ = false; 457bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread startup"; 458bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason); 459bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason); 460bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); 461bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); 462bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); 463bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 464bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom ~WatchDog() { 465994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom if (!is_watch_dog_enabled_) { 466bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return; 467bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 468bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread shutdown"; 469bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); 470bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom shutting_down_ = true; 471bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); 472bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 473bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 474bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason); 475bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 476bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); 477bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); 478bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 479bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 480bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom private: 481bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom static void* CallBack(void* arg) { 482bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom WatchDog* self = reinterpret_cast<WatchDog*>(arg); 4831d54e73444e017d3a65234e0f193846f3e27472bIan Rogers ::art::SetThreadName("dex2oat watch dog"); 484bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom self->Wait(); 485bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom return NULL; 486bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 487bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 488ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static void Message(char severity, const std::string& message) { 4891d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error 4901d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // cases. 491ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom fprintf(stderr, "dex2oat%s %c %d %d %s\n", 492ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom kIsDebugBuild ? "d" : "", 493ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom severity, 494ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom getpid(), 495ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom GetTid(), 496ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom message.c_str()); 497ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom } 498ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom 499eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static void Warn(const std::string& message) { 500ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Message('W', message); 501eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } 502eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers 503ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static void Fatal(const std::string& message) { 504ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Message('F', message); 505bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom exit(1); 506bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 507bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 508bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom void Wait() { 509eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers bool warning = true; 510eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds); 5111d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: tune the multiplier for GC verification, the following is just to make the timeout 5121d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // large. 5131d54e73444e017d3a65234e0f193846f3e27472bIan Rogers int64_t multiplier = gc::kDesiredHeapVerification > gc::kVerifyAllFast ? 100 : 1; 514ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom timespec warning_ts; 5151d54e73444e017d3a65234e0f193846f3e27472bIan Rogers InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts); 516ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom timespec timeout_ts; 5171d54e73444e017d3a65234e0f193846f3e27472bIan Rogers InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); 518bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom const char* reason = "dex2oat watch dog thread waiting"; 519bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); 520bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom while (!shutting_down_) { 521eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, 522eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers warning ? &warning_ts 523eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers : &timeout_ts)); 524bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom if (rc == ETIMEDOUT) { 525bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom std::string message(StringPrintf("dex2oat did not finish after %d seconds", 526eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers warning ? kWatchDogWarningSeconds 527eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers : kWatchDogTimeoutSeconds)); 528eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers if (warning) { 529eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers Warn(message.c_str()); 530eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers warning = false; 531eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } else { 532ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Fatal(message.c_str()); 533eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } 534eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers } else if (rc != 0) { 535bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom std::string message(StringPrintf("pthread_cond_timedwait failed: %s", 536bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom strerror(errno))); 537ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom Fatal(message.c_str()); 538bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 539bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 540bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 541bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom } 542bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 543ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom // When setting timeouts, keep in mind that the build server may not be as fast as your desktop. 544ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom#if ART_USE_PORTABLE_COMPILER 545ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static const unsigned int kWatchDogWarningSeconds = 2 * 60; // 2 minutes. 546eb5cb608fcb0d636961653ad561b2c7ee40de1adIan Rogers static const unsigned int kWatchDogTimeoutSeconds = 30 * 60; // 25 minutes + buffer. 547bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#else 548ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static const unsigned int kWatchDogWarningSeconds = 1 * 60; // 1 minute. 549ed115647f6ae7cfb6c2c22a7aea33b5802b57a86Brian Carlstrom static const unsigned int kWatchDogTimeoutSeconds = 6 * 60; // 5 minutes + buffer. 550bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif 551bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 552994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom bool is_watch_dog_enabled_; 553bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom bool shutting_down_; 5541d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases. 555bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_mutex_t mutex_; 556bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_cond_t cond_; 557bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_attr_t attr_; 558bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom pthread_t pthread_; 559bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom}; 5601c653d545058a1a850fb6d07d74e607fa1ddf48eIan Rogersconst unsigned int WatchDog::kWatchDogWarningSeconds; 5611c653d545058a1a850fb6d07d74e607fa1ddf48eIan Rogersconst unsigned int WatchDog::kWatchDogTimeoutSeconds; 562bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom 56372395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughesstatic int dex2oat(int argc, char** argv) { 5646f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum base::TimingLogger timings("compiler", false, false); 5654560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom 5660d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes InitLogging(argv); 56772395bf298b7707ad9d93c3e51b57e1b8e010311Elliott Hughes 56869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom // Skip over argv[0]. 56969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom argv++; 57069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom argc--; 57169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 57269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (argc == 0) { 573cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("no arguments specified"); 57469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 57569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 57669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom std::vector<const char*> dex_filenames; 577a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::vector<const char*> dex_locations; 578a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int zip_fd = -1; 579a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string zip_location; 580e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom std::string oat_filename; 581265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_symbols; 582a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string oat_location; 583a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom int oat_fd = -1; 5848b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien std::string bitcode_filename; 5854922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom const char* image_classes_zip_filename = NULL; 586ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom const char* image_classes_filename = NULL; 587a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::string image_filename; 588b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom std::string boot_image_filename; 58969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom uintptr_t image_base = 0; 59034f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom UniquePtr<std::string> host_prefix; 5913f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom std::string android_root; 5925d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao std::vector<const char*> runtime_args; 5935c599943f8c347acd84c4d9fda56a9df70649b78Elliott Hughes int thread_count = sysconf(_SC_NPROCESSORS_CONF); 594c531cefbfb5394413122e9f57d211ba436cff012buzbee#if defined(ART_USE_PORTABLE_COMPILER) 595c531cefbfb5394413122e9f57d211ba436cff012buzbee CompilerBackend compiler_backend = kPortable; 596c531cefbfb5394413122e9f57d211ba436cff012buzbee#else 597c531cefbfb5394413122e9f57d211ba436cff012buzbee CompilerBackend compiler_backend = kQuick; 598c531cefbfb5394413122e9f57d211ba436cff012buzbee#endif 5999ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#if defined(__arm__) 60049c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers InstructionSet instruction_set = kThumb2; 6019ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__i386__) 6029ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao InstructionSet instruction_set = kX86; 6039ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#elif defined(__mips__) 6049ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao InstructionSet instruction_set = kMips; 6059ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#else 6069ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#error "Unsupported architecture" 6079ad4f22cd9a94a3bd3439e1bc9d67fdc3e9bc67fjeffhao#endif 608265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool is_host = false; 60967d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes bool dump_stats = kIsDebugBuild; 61067d920071fe4a0aa8b8bc339e93b18276238c320Elliott Hughes bool dump_timings = kIsDebugBuild; 611994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom bool watch_dog_enabled = !kIsTargetBuild; 612161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom 613c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum 61469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom for (int i = 0; i < argc; i++) { 61569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom const StringPiece option(argv[i]); 616a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom bool log_options = false; 617a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (log_options) { 618b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 619b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom } 62069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (option.starts_with("--dex-file=")) { 62169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); 622a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--dex-location=")) { 623a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); 624a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--zip-fd=")) { 625a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); 626bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes if (!ParseInt(zip_fd_str, &zip_fd)) { 627cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str); 628a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 629a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--zip-location=")) { 630a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom zip_location = option.substr(strlen("--zip-location=")).data(); 631a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--oat-file=")) { 632a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom oat_filename = option.substr(strlen("--oat-file=")).data(); 633265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else if (option.starts_with("--oat-symbols=")) { 634265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_symbols = option.substr(strlen("--oat-symbols=")).data(); 635a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else if (option.starts_with("--oat-fd=")) { 636a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); 637bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes if (!ParseInt(oat_fd_str, &oat_fd)) { 638cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str); 639a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 640994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "--watch-dog") { 641994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom watch_dog_enabled = true; 642994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom } else if (option == "--no-watch-dog") { 643994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom watch_dog_enabled = false; 6445523ee070b005576c6f889415205d49ea77cf243Elliott Hughes } else if (option.starts_with("-j")) { 645b12552a95d68b9e4a567103190074ae47b6a61dcBrian Carlstrom const char* thread_count_str = option.substr(strlen("-j")).data(); 6465523ee070b005576c6f889415205d49ea77cf243Elliott Hughes if (!ParseInt(thread_count_str, &thread_count)) { 647cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("could not parse -j argument '%s' as an integer", thread_count_str); 6485523ee070b005576c6f889415205d49ea77cf243Elliott Hughes } 649a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (option.starts_with("--oat-location=")) { 650a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom oat_location = option.substr(strlen("--oat-location=")).data(); 6518b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien } else if (option.starts_with("--bitcode=")) { 6528b977d38483aaa08abcbdaa5fa888076c1142169Logan Chien bitcode_filename = option.substr(strlen("--bitcode=")).data(); 65369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else if (option.starts_with("--image=")) { 65469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom image_filename = option.substr(strlen("--image=")).data(); 655ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } else if (option.starts_with("--image-classes=")) { 656ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom image_classes_filename = option.substr(strlen("--image-classes=")).data(); 6574922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } else if (option.starts_with("--image-classes-zip=")) { 6584922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom image_classes_zip_filename = option.substr(strlen("--image-classes-zip=")).data(); 65969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else if (option.starts_with("--base=")) { 66069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom const char* image_base_str = option.substr(strlen("--base=")).data(); 66169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom char* end; 66269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom image_base = strtoul(image_base_str, &end, 16); 66369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (end == image_base_str || *end != '\0') { 664cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Failed to parse hexadecimal value for option %s", option.data()); 66569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 666e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } else if (option.starts_with("--boot-image=")) { 667b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_filename = option.substr(strlen("--boot-image=")).data(); 66858ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom } else if (option.starts_with("--host-prefix=")) { 66934f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); 6703f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } else if (option.starts_with("--android-root=")) { 6713f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root = option.substr(strlen("--android-root=")).data(); 67249c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers } else if (option.starts_with("--instruction-set=")) { 67349c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); 6741f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao if (instruction_set_str == "arm") { 67549c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kThumb2; 6761f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao } else if (instruction_set_str == "mips") { 67749c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kMips; 6781f71ae819e506c40ad5adccec4b2e57699e0b5c4jeffhao } else if (instruction_set_str == "x86") { 67949c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers instruction_set = kX86; 68049c4894f76f6a7aec4d6a1ec2c901700c9108944Ian Rogers } 681c531cefbfb5394413122e9f57d211ba436cff012buzbee } else if (option.starts_with("--compiler-backend=")) { 682c531cefbfb5394413122e9f57d211ba436cff012buzbee StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); 683c531cefbfb5394413122e9f57d211ba436cff012buzbee if (backend_str == "Quick") { 684c531cefbfb5394413122e9f57d211ba436cff012buzbee compiler_backend = kQuick; 685c531cefbfb5394413122e9f57d211ba436cff012buzbee } else if (backend_str == "Portable") { 686c531cefbfb5394413122e9f57d211ba436cff012buzbee compiler_backend = kPortable; 687c531cefbfb5394413122e9f57d211ba436cff012buzbee } 688265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else if (option == "--host") { 689265091e581c9f643b37e7966890911f09e223269Brian Carlstrom is_host = true; 6905d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } else if (option == "--runtime-arg") { 6915d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao if (++i >= argc) { 692cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Missing required argument for --runtime-arg"); 6935d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } 694a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (log_options) { 695a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 696a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 6975d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao runtime_args.push_back(argv[i]); 69869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else { 699cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("unknown argument %s", option.data()); 70069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 70169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 70269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 703a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (oat_filename.empty() && oat_fd == -1) { 704cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Output must be supplied with either --oat-file or --oat-fd"); 705a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 706a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 707a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (!oat_filename.empty() && oat_fd != -1) { 708cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--oat-file should not be used with --oat-fd"); 709a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 710a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 711265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty() && oat_fd != -1) { 712265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Usage("--oat-symbols should not be used with --oat-fd"); 713265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 714265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 715265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty() && is_host) { 716265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Usage("--oat-symbols should not be used with --host"); 717a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 718a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 719a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (oat_fd != -1 && !image_filename.empty()) { 720cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--oat-fd should not be used with --image"); 721e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } 722e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom 72334f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() == NULL) { 724b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); 725b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom if (android_product_out != NULL) { 72634f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom host_prefix.reset(new std::string(android_product_out)); 727b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 728b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 729b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom 7303f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (android_root.empty()) { 7313f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom const char* android_root_env_var = getenv("ANDROID_ROOT"); 7323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom if (android_root_env_var == NULL) { 73354d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom Usage("--android-root unspecified and ANDROID_ROOT not set"); 7343f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } 7353f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root += android_root_env_var; 7363f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom } 7373f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom 738a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom bool image = (!image_filename.empty()); 739b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom if (!image && boot_image_filename.empty()) { 74034f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() == NULL) { 741a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += GetAndroidRoot(); 742a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom } else { 74334f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom boot_image_filename += *host_prefix.get(); 744a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += "/system"; 745a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom } 746a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom boot_image_filename += "/framework/boot.art"; 747b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom } 748b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom std::string boot_image_option; 749a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (!boot_image_filename.empty()) { 750b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_option += "-Ximage:"; 751b001126eb9e47d0088b3672652454c53f4e17e9fBrian Carlstrom boot_image_option += boot_image_filename; 75269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 75369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 754ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL && !image) { 755cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--image-classes should only be used with --image"); 756ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom } 757ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom 758ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL && !boot_image_option.empty()) { 759cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--image-classes should not be used with --boot-image"); 76078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom } 76178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom 7624922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom if (image_classes_zip_filename != NULL && image_classes_filename == NULL) { 7634922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom Usage("--image-classes-zip should be used with --image-classes"); 7644922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } 7654922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom 766a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_filenames.empty() && zip_fd == -1) { 767cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("Input must be supplied with either --dex-file or --zip-fd"); 768a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 769a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 770a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (!dex_filenames.empty() && zip_fd != -1) { 771cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-file should not be used with --zip-fd"); 772a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 773a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 774a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (!dex_filenames.empty() && !zip_location.empty()) { 775cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-file should not be used with --zip-location"); 776a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 777a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 778a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (dex_locations.empty()) { 779a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom for (size_t i = 0; i < dex_filenames.size(); i++) { 780a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom dex_locations.push_back(dex_filenames[i]); 781a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 782a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } else if (dex_locations.size() != dex_filenames.size()) { 783cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--dex-location arguments do not match --dex-file arguments"); 784a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 785a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom 786a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (zip_fd != -1 && zip_location.empty()) { 787cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("--zip-location should be supplied with --zip-fd"); 788a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 789a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 79069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (boot_image_option.empty()) { 79169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (image_base == 0) { 792cbfe6fea382328cd5a0a9906b61da5ed4ae3eaabBrian Carlstrom Usage("non-zero --base not specified"); 79369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 79469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 79569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 796265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_stripped(oat_filename); 797265091e581c9f643b37e7966890911f09e223269Brian Carlstrom std::string oat_unstripped; 798265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!oat_symbols.empty()) { 799265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped += oat_symbols; 800265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } else { 801265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped += oat_filename; 802265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 803265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 804994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom // Done with usage checks, enable watchdog if requested 805994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom WatchDog watch_dog(watch_dog_enabled); 806994d62a359188354de6ad01699b2ae6698db16f9Brian Carlstrom 8076ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom // Check early that the result of compilation can be written 808a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom UniquePtr<File> oat_file; 809265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor 8106cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom if (create_file) { 811265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(OS::OpenFile(oat_unstripped.c_str(), true)); 812a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom if (oat_location.empty()) { 813a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom oat_location = oat_filename; 814a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom } 815a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 816761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes oat_file.reset(new File(oat_fd, oat_location)); 817761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes oat_file->DisableAutoClose(); 818a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 8196ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom if (oat_file.get() == NULL) { 8206cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom PLOG(ERROR) << "Failed to create oat file: " << oat_location; 8216cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom return EXIT_FAILURE; 8226cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom } 8236cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { 8246cd40e5cc59259a0b7636eb5532c76de9b8a7286Brian Carlstrom PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; 8256ef827a33b04fd5413d2ad88fd4599ca1920c824Brian Carlstrom return EXIT_FAILURE; 826234da578a2d91ed7f2ef47b2ec23fb0033e2746bElliott Hughes } 827a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 828a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(INFO) << "dex2oat: " << oat_location; 8295e863ddd72a70d33525f7403a695f7bc1c218938Ian Rogers 83069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom Runtime::Options options; 8315de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); 832a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom std::vector<const DexFile*> boot_class_path; 83369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom if (boot_image_option.empty()) { 834bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); 835bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes if (failure_count > 0) { 836bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes LOG(ERROR) << "Failed to open some dex files: " << failure_count; 837bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return EXIT_FAILURE; 838bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes } 839a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom options.push_back(std::make_pair("bootclasspath", &boot_class_path)); 84069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } else { 84169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); 84269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 84334f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom if (host_prefix.get() != NULL) { 84434f1fa453357af94377f83b21485a60429d1cd7fBrian Carlstrom options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); 84558ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom } 8465d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao for (size_t i = 0; i < runtime_args.size(); i++) { 8475d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); 8485d84040e2231de1d48e6f30cab2cc8d4beb8effejeffhao } 84969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 8508447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum#if ART_SMALL_MODE 8518447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum options.push_back(std::make_pair("-small", reinterpret_cast<void*>(NULL))); 8528447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum#endif // ART_SMALL_MODE 8538447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum 8547467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea 8557467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea#ifdef ART_SEA_IR_MODE 8567467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea options.push_back(std::make_pair("-sea_ir", reinterpret_cast<void*>(NULL))); 8577467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea#endif 8587467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea 8597467ee05012e1fd9834df74663c1ebda46f5636bDragos Sbirlea 86000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Dex2Oat* p_dex2oat; 8610177fe200efc1bf4d433955ee7920c683fdf5901Brian Carlstrom if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count)) { 86200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(ERROR) << "Failed to create dex2oat"; 86300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return EXIT_FAILURE; 86400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 86500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers UniquePtr<Dex2Oat> dex2oat(p_dex2oat); 86600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, 86700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // give it away now and then switch to a more managable ScopedObjectAccess. 86800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread::Current()->TransitionFromRunnableToSuspended(kNative); 86900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Whilst we're in native take the opportunity to initialize well known classes. 87000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv()); 87100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 87269b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 873bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes // If --image-classes was specified, calculate the full list of classes to include in the image 87496391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom UniquePtr<CompilerDriver::DescriptorSet> image_classes(NULL); 875ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes_filename != NULL) { 8764922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom if (image_classes_zip_filename != NULL) { 8774922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom image_classes.reset(dex2oat->ReadImageClassesFromZip(image_classes_zip_filename, 8784922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom image_classes_filename)); 8794922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } else { 8804922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom image_classes.reset(dex2oat->ReadImageClassesFromFile(image_classes_filename)); 8814922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom } 882ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom if (image_classes.get() == NULL) { 883ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; 884ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom return EXIT_FAILURE; 88569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 88669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 88769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 888a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom std::vector<const DexFile*> dex_files; 889a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (boot_image_option.empty()) { 890a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); 891a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 892a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_filenames.empty()) { 893a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd)); 894a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (zip_archive.get() == NULL) { 8954922e9d4e5f86e40ca89fb097cec40e191dae0a1Brian Carlstrom LOG(ERROR) << "Failed to open zip from file descriptor for " << zip_location; 8962e3d1b262af0839380e1d60e86d8b281943ef944Brian Carlstrom return EXIT_FAILURE; 897a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 898a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location); 899a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom if (dex_file == NULL) { 900a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location; 901a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom return EXIT_FAILURE; 902a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 903a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom dex_files.push_back(dex_file); 904a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } else { 905bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); 906bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes if (failure_count > 0) { 907bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes LOG(ERROR) << "Failed to open some dex files: " << failure_count; 908bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes return EXIT_FAILURE; 909bf1b4574dc681d49696571c59033e8c63583a029Elliott Hughes } 910a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 911a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom } 912a6cc893c4b142cd410fc956963b6f5a014e983adBrian Carlstrom 9138447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum // If we're in small mode, but the program is small, turn off small mode. 9148447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum // It doesn't make a difference for the boot image, so let's skip the check 9158447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum // altogether. 9168447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum if (Runtime::Current()->IsSmallMode() && !image) { 9178447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum size_t num_methods = 0; 9188447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum for (size_t i = 0; i != dex_files.size(); ++i) { 9198447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum const DexFile* dex_file = dex_files[i]; 9208447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum CHECK(dex_file != NULL); 9218447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum num_methods += dex_file->NumMethodIds(); 9228447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum } 9238447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum if (num_methods <= Runtime::Current()->GetSmallModeMethodThreshold()) { 9248447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum Runtime::Current()->SetSmallMode(false); 9258447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum LOG(INFO) << "Below method threshold, compiling anyways"; 9268447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum } 9278447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum } 928c4f105d75cd0cbc5145620068bbb8a819148e535Anwar Ghuloum 9296f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum timings.StartSplit("dex2oat Setup"); 9301212a022fa5f8ef9585d765b1809521812af882cIan Rogers UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option, 9311212a022fa5f8ef9585d765b1809521812af882cIan Rogers host_prefix.get(), 9323f47c12487250f61f3be95e9f275e3b08e2c49fbBrian Carlstrom android_root, 933265091e581c9f643b37e7966890911f09e223269Brian Carlstrom is_host, 9341212a022fa5f8ef9585d765b1809521812af882cIan Rogers dex_files, 9351212a022fa5f8ef9585d765b1809521812af882cIan Rogers oat_file.get(), 9361212a022fa5f8ef9585d765b1809521812af882cIan Rogers bitcode_filename, 9371212a022fa5f8ef9585d765b1809521812af882cIan Rogers image, 93896391606d8adfc661e1c21703ded1e7a39377a76Brian Carlstrom image_classes, 9391212a022fa5f8ef9585d765b1809521812af882cIan Rogers dump_stats, 9404560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom timings)); 941f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom 942f582258f0e296223a091fd64231a203ad71e9649Brian Carlstrom if (compiler.get() == NULL) { 943a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom LOG(ERROR) << "Failed to create oat file: " << oat_location; 944e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom return EXIT_FAILURE; 945e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom } 946e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom 947265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Oat file written successfully (unstripped): " << oat_location; 948aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom 949700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Notes on the interleaving of creating the image and oat file to 950700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // ensure the references between the two are correct. 951700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 952700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Currently we have a memory layout that looks something like this: 953700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 954700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 955700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | image | 956700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 957700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | boot oat | 958700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 959700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // | alloc spaces | 960700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // +--------------+ 961700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 9624560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom // There are several constraints on the loading of the image and boot.oat. 963700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 964700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1. The image is expected to be loaded at an absolute address and 965700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // contains Objects with absolute pointers within the image. 966700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 967700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 2. There are absolute pointers from Methods in the image to their 968700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // code in the oat. 969700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 970700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 3. There are absolute pointers from the code in the oat to Methods 971700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // in the image. 972700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 973700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 4. There are absolute pointers from code in the oat to other code 974700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // in the oat. 975700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 976700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // To get this all correct, we go through several steps. 977700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 978700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 1. We have already created that oat file above with 979700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // CreateOatFile. Originally this was just our own proprietary file 9804560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom // but now it is contained within an ELF dynamic object (aka an .so 981700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // file). The Compiler returned by CreateOatFile provides 982700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // PatchInformation for references to oat code and Methods that need 983700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // to be update once we know where the oat file will be located 984700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // after the image. 985700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 986700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 2. We create the image file. It needs to know where the oat file 987700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // will be loaded after itself. Originally when oat file was simply 988700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // memory mapped so we could predict where its contents were based 989700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // on the file size. Now that it is an ELF file, we need to inspect 990700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // the ELF file to understand the in memory segment layout including 991700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // where the oat header is located within. ImageWriter's 992700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // PatchOatCodeAndMethods uses the PatchInformation from the 993700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Compiler to touch up absolute references in the oat file. 994700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 995700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 3. We fixup the ELF program headers so that dlopen will try to 996700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // load the .so at the desired location at runtime by offsetting the 997700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // Elf32_Phdr.p_vaddr values by the desired base address. 998700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom // 999265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (image) { 10006f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum timings.NewSplit("dex2oat ImageWriter"); 1001265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Thread::Current()->TransitionFromRunnableToSuspended(kNative); 1002265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool image_creation_success = dex2oat->CreateImageFile(image_filename, 1003265091e581c9f643b37e7966890911f09e223269Brian Carlstrom image_base, 1004265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_unstripped, 1005265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_location, 1006265091e581c9f643b37e7966890911f09e223269Brian Carlstrom *compiler.get()); 1007265091e581c9f643b37e7966890911f09e223269Brian Carlstrom Thread::Current()->TransitionFromSuspendedToRunnable(); 1008265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (!image_creation_success) { 1009265091e581c9f643b37e7966890911f09e223269Brian Carlstrom return EXIT_FAILURE; 1010265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1011c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom LOG(INFO) << "Image written successfully: " << image_filename; 1012265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1013265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1014265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (is_host) { 10154560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom if (dump_timings && timings.GetTotalNs() > MsToNs(1000)) { 10166f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum LOG(INFO) << Dumpable<base::TimingLogger>(timings); 10174560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom } 1018265091e581c9f643b37e7966890911f09e223269Brian Carlstrom return EXIT_SUCCESS; 101969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom } 102069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 1021265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // If we don't want to strip in place, copy from unstripped location to stripped location. 1022265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // We need to strip after image creation because FixupElf needs to use .strtab. 1023265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (oat_unstripped != oat_stripped) { 10246f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum timings.NewSplit("dex2oat OatFile copy"); 1025265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(); 1026265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<File> in(OS::OpenFile(oat_unstripped.c_str(), false)); 1027265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<File> out(OS::OpenFile(oat_stripped.c_str(), true)); 1028265091e581c9f643b37e7966890911f09e223269Brian Carlstrom size_t buffer_size = 8192; 1029265091e581c9f643b37e7966890911f09e223269Brian Carlstrom UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]); 1030265091e581c9f643b37e7966890911f09e223269Brian Carlstrom while (true) { 1031265091e581c9f643b37e7966890911f09e223269Brian Carlstrom int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); 1032265091e581c9f643b37e7966890911f09e223269Brian Carlstrom if (bytes_read <= 0) { 1033265091e581c9f643b37e7966890911f09e223269Brian Carlstrom break; 1034265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1035265091e581c9f643b37e7966890911f09e223269Brian Carlstrom bool write_ok = out->WriteFully(buffer.get(), bytes_read); 1036265091e581c9f643b37e7966890911f09e223269Brian Carlstrom CHECK(write_ok); 1037265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1038265091e581c9f643b37e7966890911f09e223269Brian Carlstrom oat_file.reset(out.release()); 1039265091e581c9f643b37e7966890911f09e223269Brian Carlstrom LOG(INFO) << "Oat file copied successfully (stripped): " << oat_stripped; 1040265091e581c9f643b37e7966890911f09e223269Brian Carlstrom } 1041265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 10427fcba11846d3320911aefdba6a20c1192e36fc2eBrian Carlstrom#if ART_USE_PORTABLE_COMPILER // We currently only generate symbols on Portable 10436f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum timings.NewSplit("dex2oat ElfStripper"); 1044265091e581c9f643b37e7966890911f09e223269Brian Carlstrom // Strip unneeded sections for target 1045265091e581c9f643b37e7966890911f09e223269Brian Carlstrom off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET); 1046265091e581c9f643b37e7966890911f09e223269Brian Carlstrom CHECK_EQ(0, seek_actual); 104751c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom ElfStripper::Strip(oat_file.get()); 10486f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum 1049265091e581c9f643b37e7966890911f09e223269Brian Carlstrom 1050ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom // We wrote the oat file successfully, and want to keep it. 105154d22c2bb47da44ef586b9de94749d5648178a26Brian Carlstrom LOG(INFO) << "Oat file written successfully (stripped): " << oat_location; 10524560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom#endif // ART_USE_PORTABLE_COMPILER 10534560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom 10546f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum timings.EndSplit(); 10556f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum 10564560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom if (dump_timings && timings.GetTotalNs() > MsToNs(1000)) { 10576f28d91aab952e3244fbb4e707fa38f85538f374Anwar Ghuloum LOG(INFO) << Dumpable<base::TimingLogger>(timings); 10584560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom } 105969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom return EXIT_SUCCESS; 106069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 106169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 10624560248d4c85cade7f4fc7b30c3fb41b95a04a7fBrian Carlstrom 106369b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} // namespace art 106469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom 106569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromint main(int argc, char** argv) { 106669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom return art::dex2oat(argc, argv); 106769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom} 1068