Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 19 | #include <sys/stat.h> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 20 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <fstream> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 26 | #include "base/stl_util.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 27 | #include "base/stringpiece.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 28 | #include "base/timing_logger.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 29 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 30 | #include "class_linker.h" |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 31 | #include "compiler/driver/compiler_driver.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 32 | #include "dex_file-inl.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 33 | #include "image_writer.h" |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 34 | #include "leb128.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/abstract_method-inl.h" |
| 36 | #include "mirror/class-inl.h" |
| 37 | #include "mirror/class_loader.h" |
| 38 | #include "mirror/object-inl.h" |
| 39 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 40 | #include "oat_writer.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 41 | #include "object_utils.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 42 | #include "os.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 43 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 44 | #include "ScopedLocalRef.h" |
| 45 | #include "scoped_thread_state_change.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 46 | #include "sirt_ref.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 47 | #include "vector_output_stream.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 48 | #include "well_known_classes.h" |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 49 | #include "zip_archive.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 50 | |
| 51 | namespace art { |
| 52 | |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 53 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 54 | std::string error; |
| 55 | StringAppendV(&error, fmt, ap); |
| 56 | LOG(ERROR) << error; |
| 57 | } |
| 58 | |
| 59 | static void UsageError(const char* fmt, ...) { |
| 60 | va_list ap; |
| 61 | va_start(ap, fmt); |
| 62 | UsageErrorV(fmt, ap); |
| 63 | va_end(ap); |
| 64 | } |
| 65 | |
| 66 | static void Usage(const char* fmt, ...) { |
| 67 | va_list ap; |
| 68 | va_start(ap, fmt); |
| 69 | UsageErrorV(fmt, ap); |
| 70 | va_end(ap); |
| 71 | |
| 72 | UsageError("Usage: dex2oat [options]..."); |
| 73 | UsageError(""); |
| 74 | UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); |
| 75 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 76 | UsageError(""); |
| 77 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 78 | UsageError(" containing a classes.dex file to compile."); |
| 79 | UsageError(" Example: --zip-fd=5"); |
| 80 | UsageError(""); |
| 81 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding"); |
| 82 | UsageError(" to the file descriptor specified by --zip-fd."); |
| 83 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 84 | UsageError(""); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 85 | UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); |
| 86 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 87 | UsageError(""); |
| 88 | UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 89 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 90 | UsageError(""); |
| 91 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 92 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 93 | UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat"); |
| 94 | UsageError(""); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 95 | UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); |
| 96 | UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); |
| 97 | UsageError(""); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 98 | UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); |
| 99 | UsageError(" Example: --bitcode=/system/framework/boot.bc"); |
| 100 | UsageError(""); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 101 | UsageError(" --image=<file.art>: specifies the output image filename."); |
| 102 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 103 | UsageError(""); |
| 104 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 105 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 106 | UsageError(""); |
| 107 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 108 | UsageError(" Example: --base=0x50000000"); |
| 109 | UsageError(""); |
| 110 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
| 111 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
| 112 | UsageError(" Default: <host-prefix>/system/framework/boot.art"); |
| 113 | UsageError(""); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 114 | UsageError(" --host-prefix=<path>: used to translate host paths to target paths during"); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 115 | UsageError(" cross compilation."); |
| 116 | UsageError(" Example: --host-prefix=out/target/product/crespo"); |
| 117 | UsageError(" Default: $ANDROID_PRODUCT_OUT"); |
| 118 | UsageError(""); |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 119 | UsageError(" --android-root=<path>: used to locate libraries for portable linking."); |
| 120 | UsageError(" Example: --android-root=out/host/linux-x86"); |
| 121 | UsageError(" Default: $ANDROID_ROOT"); |
| 122 | UsageError(""); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 123 | UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction"); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 124 | UsageError(" set."); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 125 | UsageError(" Example: --instruction-set=x86"); |
| 126 | UsageError(" Default: arm"); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 127 | UsageError(""); |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 128 | UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend"); |
| 129 | UsageError(" set."); |
| 130 | UsageError(" Example: --instruction-set=Portable"); |
| 131 | UsageError(" Default: Quick"); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 132 | UsageError(""); |
| 133 | UsageError(" --host: used with Portable backend to link against host runtime libraries"); |
| 134 | UsageError(""); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 135 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 136 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 137 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 138 | UsageError(" Example: --runtime-arg -Xms256m"); |
| 139 | UsageError(""); |
| 140 | std::cerr << "See log for usage error information\n"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 141 | exit(EXIT_FAILURE); |
| 142 | } |
| 143 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 144 | class Dex2Oat { |
| 145 | public: |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 146 | static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, CompilerBackend compiler_backend, |
Anwar Ghuloum | 8447d84 | 2013-04-30 17:27:40 -0700 | [diff] [blame] | 147 | InstructionSet instruction_set, size_t thread_count, bool support_debugging) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 148 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 149 | if (!CreateRuntime(options, instruction_set)) { |
| 150 | *p_dex2oat = NULL; |
| 151 | return false; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 152 | } |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 153 | *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count, |
Anwar Ghuloum | 8447d84 | 2013-04-30 17:27:40 -0700 | [diff] [blame] | 154 | support_debugging); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | ~Dex2Oat() { |
| 159 | delete runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 160 | LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 163 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 164 | // Make a list of descriptors for classes to include in the image |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 166 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 167 | UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); |
| 168 | if (image_classes_file.get() == NULL) { |
| 169 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 173 | // Load all the classes specified in the file |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 174 | ClassLinker* class_linker = runtime_->GetClassLinker(); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 175 | Thread* self = Thread::Current(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 176 | while (image_classes_file->good()) { |
| 177 | std::string dot; |
| 178 | std::getline(*image_classes_file.get(), dot); |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 179 | if (StartsWith(dot, "#") || dot.empty()) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 180 | continue; |
| 181 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 182 | std::string descriptor(DotToDescriptor(dot.c_str())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 183 | SirtRef<mirror::Class> klass(self, class_linker->FindSystemClass(descriptor.c_str())); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 184 | if (klass.get() == NULL) { |
| 185 | LOG(WARNING) << "Failed to find class " << descriptor; |
| 186 | Thread::Current()->ClearException(); |
| 187 | } |
| 188 | } |
| 189 | image_classes_file->close(); |
| 190 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 191 | // Resolve exception classes referenced by the loaded classes. The catch logic assumes |
| 192 | // exceptions are resolved by the verifier when there is a catch block in an interested method. |
| 193 | // Do this here so that exception classes appear to have been specified image classes. |
| 194 | std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 195 | SirtRef<mirror::Class> java_lang_Throwable(self, |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 196 | class_linker->FindSystemClass("Ljava/lang/Throwable;")); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 197 | do { |
| 198 | unresolved_exception_types.clear(); |
| 199 | class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, |
| 200 | &unresolved_exception_types); |
| 201 | typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto |
| 202 | for (It it = unresolved_exception_types.begin(), |
| 203 | end = unresolved_exception_types.end(); |
| 204 | it != end; ++it) { |
| 205 | uint16_t exception_type_idx = it->first; |
| 206 | const DexFile* dex_file = it->second; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 207 | mirror::DexCache* dex_cache = class_linker->FindDexCache(*dex_file); |
| 208 | mirror:: ClassLoader* class_loader = NULL; |
| 209 | SirtRef<mirror::Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx, |
| 210 | dex_cache, class_loader)); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 211 | if (klass.get() == NULL) { |
| 212 | const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); |
| 213 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 214 | LOG(FATAL) << "Failed to resolve class " << descriptor; |
| 215 | } |
Elliott Hughes | 35be9b1 | 2012-05-29 17:59:26 -0700 | [diff] [blame] | 216 | DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get())); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 217 | } |
| 218 | // Resolving exceptions may load classes that reference more exceptions, iterate until no |
| 219 | // more are found |
| 220 | } while (!unresolved_exception_types.empty()); |
| 221 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 222 | // We walk the roots looking for classes so that we'll pick up the |
| 223 | // above classes plus any classes them depend on such super |
| 224 | // classes, interfaces, and the required ClassLinker roots. |
| 225 | UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>()); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 226 | class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 227 | CHECK_NE(image_classes->size(), 0U); |
| 228 | return image_classes.release(); |
| 229 | } |
| 230 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 231 | const CompilerDriver* CreateOatFile(const std::string& boot_image_option, |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 232 | const std::string* host_prefix, |
| 233 | const std::string& android_root, |
| 234 | bool is_host, |
| 235 | const std::vector<const DexFile*>& dex_files, |
| 236 | File* oat_file, |
| 237 | const std::string& bitcode_filename, |
| 238 | bool image, |
| 239 | const std::set<std::string>* image_classes, |
| 240 | bool dump_stats, |
| 241 | bool dump_timings) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 242 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 243 | // SirtRef and ClassLoader creation needs to come after Runtime::Create |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 244 | jobject class_loader = NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 245 | if (!boot_image_option.empty()) { |
| 246 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 247 | std::vector<const DexFile*> class_path_files(dex_files); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 248 | OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 249 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 250 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 251 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 252 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 253 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); |
| 254 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 255 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 256 | class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 257 | Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 260 | UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_, |
| 261 | instruction_set_, |
| 262 | image, |
| 263 | thread_count_, |
| 264 | support_debugging_, |
| 265 | image_classes, |
| 266 | dump_stats, |
| 267 | dump_timings)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 268 | |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 269 | if (compiler_backend_ == kPortable) { |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 270 | driver->SetBitcodeFileName(bitcode_filename); |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 271 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 272 | |
Anwar Ghuloum | 8447d84 | 2013-04-30 17:27:40 -0700 | [diff] [blame] | 273 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 274 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 275 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 276 | driver->CompileAll(class_loader, dex_files); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 277 | |
| 278 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 279 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 280 | std::string image_file_location; |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 281 | uint32_t image_file_location_oat_checksum = 0; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 282 | uint32_t image_file_location_oat_data_begin = 0; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 283 | Heap* heap = Runtime::Current()->GetHeap(); |
| 284 | if (heap->GetSpaces().size() > 1) { |
| 285 | ImageSpace* image_space = heap->GetImageSpace(); |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 286 | image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 287 | image_file_location_oat_data_begin = reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 288 | image_file_location = image_space->GetImageFilename(); |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 289 | if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { |
| 290 | image_file_location = image_file_location.substr(host_prefix->size()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 294 | std::vector<uint8_t> oat_contents; |
| 295 | VectorOutputStream vector_output_stream(oat_file->GetPath(), oat_contents); |
| 296 | if (!OatWriter::Create(vector_output_stream, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 297 | dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 298 | image_file_location_oat_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 299 | image_file_location_oat_data_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 300 | image_file_location, |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 301 | *driver.get())) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 302 | LOG(ERROR) << "Failed to create oat file " << oat_file->GetPath(); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 303 | return NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 304 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 305 | |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 306 | if (!driver->WriteElf(android_root, is_host, dex_files, oat_contents, oat_file)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 307 | LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 311 | return driver.release(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 314 | bool CreateImageFile(const std::string& image_filename, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 315 | uintptr_t image_base, |
| 316 | const std::set<std::string>* image_classes, |
| 317 | const std::string& oat_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 318 | const std::string& oat_location, |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 319 | const CompilerDriver& compiler) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 320 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 321 | uintptr_t oat_data_begin; |
| 322 | { |
| 323 | // ImageWriter is scoped so it can free memory before doing FixupElf |
| 324 | ImageWriter image_writer(image_classes); |
| 325 | if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) { |
| 326 | LOG(ERROR) << "Failed to create image file " << image_filename; |
| 327 | return false; |
| 328 | } |
| 329 | oat_data_begin = image_writer.GetOatDataBegin(); |
| 330 | } |
| 331 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 332 | UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 333 | if (oat_file.get() == NULL) { |
| 334 | PLOG(ERROR) << "Failed to open ELF file: " << oat_filename; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 335 | return false; |
| 336 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 337 | if (!compiler.FixupElf(oat_file.get(), oat_data_begin)) { |
| 338 | LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); |
| 339 | return false; |
| 340 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
| 344 | private: |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 345 | explicit Dex2Oat(Runtime* runtime, CompilerBackend compiler_backend, InstructionSet instruction_set, |
Anwar Ghuloum | 8447d84 | 2013-04-30 17:27:40 -0700 | [diff] [blame] | 346 | size_t thread_count, bool support_debugging) |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 347 | : compiler_backend_(compiler_backend), |
| 348 | instruction_set_(instruction_set), |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 349 | runtime_(runtime), |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 350 | thread_count_(thread_count), |
| 351 | support_debugging_(support_debugging), |
| 352 | start_ns_(NanoTime()) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 353 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 354 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 355 | static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 356 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 357 | if (!Runtime::Create(options, false)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 358 | LOG(ERROR) << "Failed to create runtime"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 359 | return false; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 360 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 361 | Runtime* runtime = Runtime::Current(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 362 | // if we loaded an existing image, we will reuse values from the image roots. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 363 | if (!runtime->HasResolutionMethod()) { |
| 364 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 365 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 366 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 367 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 368 | if (!runtime->HasCalleeSaveMethod(type)) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 369 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 372 | runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 373 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 376 | static void ResolveExceptionsForMethod(MethodHelper* mh, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 377 | std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 378 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 379 | const DexFile::CodeItem* code_item = mh->GetCodeItem(); |
| 380 | if (code_item == NULL) { |
| 381 | return; // native or abstract method |
| 382 | } |
| 383 | if (code_item->tries_size_ == 0) { |
| 384 | return; // nothing to process |
| 385 | } |
| 386 | const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); |
| 387 | size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 388 | for (size_t i = 0; i < num_encoded_catch_handlers; i++) { |
| 389 | int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); |
| 390 | bool has_catch_all = false; |
| 391 | if (encoded_catch_handler_size <= 0) { |
| 392 | encoded_catch_handler_size = -encoded_catch_handler_size; |
| 393 | has_catch_all = true; |
| 394 | } |
| 395 | for (int32_t j = 0; j < encoded_catch_handler_size; j++) { |
| 396 | uint16_t encoded_catch_handler_handlers_type_idx = |
| 397 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 398 | // Add to set of types to resolve if not already in the dex cache resolved types |
| 399 | if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { |
| 400 | exceptions_to_resolve.insert( |
| 401 | std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, |
| 402 | &mh->GetDexFile())); |
| 403 | } |
| 404 | // ignore address associated with catch handler |
| 405 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 406 | } |
| 407 | if (has_catch_all) { |
| 408 | // ignore catch all address |
| 409 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 410 | } |
| 411 | } |
| 412 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 413 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 414 | static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 415 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 416 | std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = |
| 417 | reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); |
| 418 | MethodHelper mh; |
| 419 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 420 | mirror::AbstractMethod* m = c->GetVirtualMethod(i); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 421 | mh.ChangeMethod(m); |
| 422 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 423 | } |
| 424 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 425 | mirror::AbstractMethod* m = c->GetDirectMethod(i); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 426 | mh.ChangeMethod(m); |
| 427 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 428 | } |
| 429 | return true; |
| 430 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 431 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 432 | static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 433 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 434 | std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); |
| 435 | if (klass->IsArrayClass() || klass->IsPrimitive()) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 436 | return true; |
| 437 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 438 | image_classes->insert(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 439 | return true; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 440 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 441 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 442 | // Appends to dex_files any elements of class_path that it doesn't already |
| 443 | // contain. This will open those dex files as necessary. |
| 444 | static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { |
| 445 | std::vector<std::string> parsed; |
| 446 | Split(class_path, ':', parsed); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 447 | // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained. |
| 448 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 449 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 450 | if (DexFilesContains(dex_files, parsed[i])) { |
| 451 | continue; |
| 452 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 453 | const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 454 | if (dex_file == NULL) { |
| 455 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 456 | } else { |
| 457 | dex_files.push_back(dex_file); |
| 458 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 459 | } |
| 460 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 461 | |
| 462 | // Returns true if dex_files has a dex with the named location. |
| 463 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { |
| 464 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 465 | if (dex_files[i]->GetLocation() == location) { |
| 466 | return true; |
| 467 | } |
| 468 | } |
| 469 | return false; |
| 470 | } |
| 471 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 472 | const CompilerBackend compiler_backend_; |
| 473 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 474 | const InstructionSet instruction_set_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 475 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 476 | Runtime* runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 477 | size_t thread_count_; |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 478 | bool support_debugging_; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 479 | uint64_t start_ns_; |
| 480 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 481 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 482 | }; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 483 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 484 | static bool ParseInt(const char* in, int* out) { |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 485 | char* end; |
| 486 | int result = strtol(in, &end, 10); |
| 487 | if (in == end || *end != '\0') { |
| 488 | return false; |
| 489 | } |
| 490 | *out = result; |
| 491 | return true; |
| 492 | } |
| 493 | |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 494 | static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, |
| 495 | const std::vector<const char*>& dex_locations, |
| 496 | std::vector<const DexFile*>& dex_files) { |
| 497 | size_t failure_count = 0; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 498 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 499 | const char* dex_filename = dex_filenames[i]; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 500 | const char* dex_location = dex_locations[i]; |
| 501 | const DexFile* dex_file = DexFile::Open(dex_filename, dex_location); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 502 | if (dex_file == NULL) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 503 | LOG(WARNING) << "Could not open .dex from file '" << dex_filename << "'\n"; |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 504 | ++failure_count; |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 505 | } else { |
| 506 | dex_files.push_back(dex_file); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 507 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 508 | } |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 509 | return failure_count; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 512 | // The primary goal of the watchdog is to prevent stuck build servers |
| 513 | // during development when fatal aborts lead to a cascade of failures |
| 514 | // that result in a deadlock. |
| 515 | class WatchDog { |
| 516 | |
| 517 | // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks |
| 518 | #undef CHECK_PTHREAD_CALL |
| 519 | #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ |
| 520 | do { \ |
| 521 | int rc = call args; \ |
| 522 | if (rc != 0) { \ |
| 523 | errno = rc; \ |
| 524 | std::string message(# call); \ |
| 525 | message += " failed for "; \ |
| 526 | message += reason; \ |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 527 | Fatal(message); \ |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 528 | } \ |
| 529 | } while (false) |
| 530 | |
| 531 | public: |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 532 | WatchDog(bool is_watch_dog_enabled) { |
| 533 | is_watch_dog_enabled_ = is_watch_dog_enabled; |
| 534 | if (!is_watch_dog_enabled_) { |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | shutting_down_ = false; |
| 538 | const char* reason = "dex2oat watch dog thread startup"; |
| 539 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason); |
| 540 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason); |
| 541 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); |
| 542 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); |
| 543 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); |
| 544 | } |
| 545 | ~WatchDog() { |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 546 | if (!is_watch_dog_enabled_) { |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | const char* reason = "dex2oat watch dog thread shutdown"; |
| 550 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 551 | shutting_down_ = true; |
| 552 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); |
| 553 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 554 | |
| 555 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason); |
| 556 | |
| 557 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); |
| 558 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); |
| 559 | } |
| 560 | |
| 561 | private: |
| 562 | static void* CallBack(void* arg) { |
| 563 | WatchDog* self = reinterpret_cast<WatchDog*>(arg); |
| 564 | self->Wait(); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 568 | static void Message(char severity, const std::string& message) { |
| 569 | // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error cases. |
| 570 | fprintf(stderr, "dex2oat%s %c %d %d %s\n", |
| 571 | kIsDebugBuild ? "d" : "", |
| 572 | severity, |
| 573 | getpid(), |
| 574 | GetTid(), |
| 575 | message.c_str()); |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 578 | static void Warn(const std::string& message) { |
| 579 | Message('W', message); |
| 580 | } |
| 581 | |
| 582 | static void Fatal(const std::string& message) { |
| 583 | Message('F', message); |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 584 | exit(1); |
| 585 | } |
| 586 | |
| 587 | void Wait() { |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 588 | bool warning = true; |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 589 | CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds); |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 590 | timespec warning_ts; |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 591 | InitTimeSpec(true, CLOCK_REALTIME, kWatchDogWarningSeconds * 1000, 0, &warning_ts); |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 592 | timespec timeout_ts; |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 593 | InitTimeSpec(true, CLOCK_REALTIME, kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 594 | const char* reason = "dex2oat watch dog thread waiting"; |
| 595 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 596 | while (!shutting_down_) { |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 597 | int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, |
| 598 | warning ? &warning_ts |
| 599 | : &timeout_ts)); |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 600 | if (rc == ETIMEDOUT) { |
| 601 | std::string message(StringPrintf("dex2oat did not finish after %d seconds", |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 602 | warning ? kWatchDogWarningSeconds |
| 603 | : kWatchDogTimeoutSeconds)); |
| 604 | if (warning) { |
| 605 | Warn(message.c_str()); |
| 606 | warning = false; |
| 607 | } else { |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 608 | Fatal(message.c_str()); |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 609 | } |
| 610 | } else if (rc != 0) { |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 611 | std::string message(StringPrintf("pthread_cond_timedwait failed: %s", |
| 612 | strerror(errno))); |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 613 | Fatal(message.c_str()); |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 617 | } |
| 618 | |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 619 | // When setting timeouts, keep in mind that the build server may not be as fast as your desktop. |
| 620 | #if ART_USE_PORTABLE_COMPILER |
| 621 | static const unsigned int kWatchDogWarningSeconds = 2 * 60; // 2 minutes. |
Ian Rogers | eb5cb60 | 2013-02-25 15:06:01 -0800 | [diff] [blame] | 622 | static const unsigned int kWatchDogTimeoutSeconds = 30 * 60; // 25 minutes + buffer. |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 623 | #else |
Brian Carlstrom | ed11564 | 2013-03-15 16:04:40 -0700 | [diff] [blame] | 624 | static const unsigned int kWatchDogWarningSeconds = 1 * 60; // 1 minute. |
| 625 | static const unsigned int kWatchDogTimeoutSeconds = 6 * 60; // 5 minutes + buffer. |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 626 | #endif |
| 627 | |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 628 | bool is_watch_dog_enabled_; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 629 | bool shutting_down_; |
| 630 | // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases |
| 631 | pthread_mutex_t mutex_; |
| 632 | pthread_cond_t cond_; |
| 633 | pthread_attr_t attr_; |
| 634 | pthread_t pthread_; |
| 635 | }; |
| 636 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 637 | static int dex2oat(int argc, char** argv) { |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 638 | InitLogging(argv); |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 639 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 640 | // Skip over argv[0]. |
| 641 | argv++; |
| 642 | argc--; |
| 643 | |
| 644 | if (argc == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 645 | Usage("no arguments specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | std::vector<const char*> dex_filenames; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 649 | std::vector<const char*> dex_locations; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 650 | int zip_fd = -1; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 651 | std::string zip_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 652 | std::string oat_filename; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 653 | std::string oat_symbols; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 654 | std::string oat_location; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 655 | int oat_fd = -1; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 656 | std::string bitcode_filename; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 657 | const char* image_classes_filename = NULL; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 658 | std::string image_filename; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 659 | std::string boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 660 | uintptr_t image_base = 0; |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 661 | UniquePtr<std::string> host_prefix; |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 662 | std::string android_root; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 663 | std::vector<const char*> runtime_args; |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 664 | int thread_count = sysconf(_SC_NPROCESSORS_CONF); |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 665 | bool support_debugging = false; |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 666 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 667 | CompilerBackend compiler_backend = kPortable; |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 668 | #else |
| 669 | CompilerBackend compiler_backend = kQuick; |
| 670 | #endif |
jeffhao | 9ad4f22 | 2012-05-30 18:51:19 -0700 | [diff] [blame] | 671 | #if defined(__arm__) |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 672 | InstructionSet instruction_set = kThumb2; |
jeffhao | 9ad4f22 | 2012-05-30 18:51:19 -0700 | [diff] [blame] | 673 | #elif defined(__i386__) |
| 674 | InstructionSet instruction_set = kX86; |
| 675 | #elif defined(__mips__) |
| 676 | InstructionSet instruction_set = kMips; |
| 677 | #else |
| 678 | #error "Unsupported architecture" |
| 679 | #endif |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 680 | bool is_host = false; |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 681 | bool dump_stats = kIsDebugBuild; |
| 682 | bool dump_timings = kIsDebugBuild; |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 683 | bool watch_dog_enabled = !kIsTargetBuild; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 684 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 685 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 686 | for (int i = 0; i < argc; i++) { |
| 687 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 688 | bool log_options = false; |
| 689 | if (log_options) { |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 690 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 691 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 692 | if (option.starts_with("--dex-file=")) { |
| 693 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 694 | } else if (option.starts_with("--dex-location=")) { |
| 695 | dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 696 | } else if (option.starts_with("--zip-fd=")) { |
| 697 | const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 698 | if (!ParseInt(zip_fd_str, &zip_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 699 | Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 700 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 701 | } else if (option.starts_with("--zip-location=")) { |
| 702 | zip_location = option.substr(strlen("--zip-location=")).data(); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 703 | } else if (option.starts_with("--oat-file=")) { |
| 704 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 705 | } else if (option.starts_with("--oat-symbols=")) { |
| 706 | oat_symbols = option.substr(strlen("--oat-symbols=")).data(); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 707 | } else if (option.starts_with("--oat-fd=")) { |
| 708 | const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 709 | if (!ParseInt(oat_fd_str, &oat_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 710 | Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 711 | } |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 712 | } else if (option == "-g") { |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 713 | support_debugging = true; |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 714 | } else if (option == "--watch-dog") { |
| 715 | watch_dog_enabled = true; |
| 716 | } else if (option == "--no-watch-dog") { |
| 717 | watch_dog_enabled = false; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 718 | } else if (option.starts_with("-j")) { |
Brian Carlstrom | b12552a | 2012-02-04 17:17:31 -0800 | [diff] [blame] | 719 | const char* thread_count_str = option.substr(strlen("-j")).data(); |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 720 | if (!ParseInt(thread_count_str, &thread_count)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 721 | Usage("could not parse -j argument '%s' as an integer", thread_count_str); |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 722 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 723 | } else if (option.starts_with("--oat-location=")) { |
| 724 | oat_location = option.substr(strlen("--oat-location=")).data(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 725 | } else if (option.starts_with("--bitcode=")) { |
| 726 | bitcode_filename = option.substr(strlen("--bitcode=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 727 | } else if (option.starts_with("--image=")) { |
| 728 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 729 | } else if (option.starts_with("--image-classes=")) { |
| 730 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 731 | } else if (option.starts_with("--base=")) { |
| 732 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 733 | char* end; |
| 734 | image_base = strtoul(image_base_str, &end, 16); |
| 735 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 736 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 737 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 738 | } else if (option.starts_with("--boot-image=")) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 739 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 740 | } else if (option.starts_with("--host-prefix=")) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 741 | host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 742 | } else if (option.starts_with("--android-root=")) { |
| 743 | android_root = option.substr(strlen("--android-root=")).data(); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 744 | } else if (option.starts_with("--instruction-set=")) { |
| 745 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 746 | if (instruction_set_str == "arm") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 747 | instruction_set = kThumb2; |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 748 | } else if (instruction_set_str == "mips") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 749 | instruction_set = kMips; |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 750 | } else if (instruction_set_str == "x86") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 751 | instruction_set = kX86; |
| 752 | } |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 753 | } else if (option.starts_with("--compiler-backend=")) { |
| 754 | StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); |
| 755 | if (backend_str == "Quick") { |
| 756 | compiler_backend = kQuick; |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 757 | } else if (backend_str == "Portable") { |
| 758 | compiler_backend = kPortable; |
| 759 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 760 | } else if (option == "--host") { |
| 761 | is_host = true; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 762 | } else if (option == "--runtime-arg") { |
| 763 | if (++i >= argc) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 764 | Usage("Missing required argument for --runtime-arg"); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 765 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 766 | if (log_options) { |
| 767 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 768 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 769 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 770 | } else { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 771 | Usage("unknown argument %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 775 | if (oat_filename.empty() && oat_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 776 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | if (!oat_filename.empty() && oat_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 780 | Usage("--oat-file should not be used with --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 783 | if (!oat_symbols.empty() && oat_fd != -1) { |
| 784 | Usage("--oat-symbols should not be used with --oat-fd"); |
| 785 | } |
| 786 | |
| 787 | if (!oat_symbols.empty() && is_host) { |
| 788 | Usage("--oat-symbols should not be used with --host"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 789 | } |
| 790 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 791 | if (oat_fd != -1 && !image_filename.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 792 | Usage("--oat-fd should not be used with --image"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 795 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 796 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 797 | if (android_product_out != NULL) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 798 | host_prefix.reset(new std::string(android_product_out)); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 802 | if (android_root.empty()) { |
| 803 | const char* android_root_env_var = getenv("ANDROID_ROOT"); |
| 804 | if (android_root_env_var == NULL) { |
Brian Carlstrom | 54d22c2 | 2013-03-13 15:14:57 -0700 | [diff] [blame] | 805 | Usage("--android-root unspecified and ANDROID_ROOT not set"); |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 806 | } |
| 807 | android_root += android_root_env_var; |
| 808 | } |
| 809 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 810 | bool image = (!image_filename.empty()); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 811 | if (!image && boot_image_filename.empty()) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 812 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 813 | boot_image_filename += GetAndroidRoot(); |
| 814 | } else { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 815 | boot_image_filename += *host_prefix.get(); |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 816 | boot_image_filename += "/system"; |
| 817 | } |
| 818 | boot_image_filename += "/framework/boot.art"; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 819 | } |
| 820 | std::string boot_image_option; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 821 | if (!boot_image_filename.empty()) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 822 | boot_image_option += "-Ximage:"; |
| 823 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 826 | if (image_classes_filename != NULL && !image) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 827 | Usage("--image-classes should only be used with --image"); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (image_classes_filename != NULL && !boot_image_option.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 831 | Usage("--image-classes should not be used with --boot-image"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 834 | if (dex_filenames.empty() && zip_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 835 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | if (!dex_filenames.empty() && zip_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 839 | Usage("--dex-file should not be used with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 842 | if (!dex_filenames.empty() && !zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 843 | Usage("--dex-file should not be used with --zip-location"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 846 | if (dex_locations.empty()) { |
| 847 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 848 | dex_locations.push_back(dex_filenames[i]); |
| 849 | } |
| 850 | } else if (dex_locations.size() != dex_filenames.size()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 851 | Usage("--dex-location arguments do not match --dex-file arguments"); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if (zip_fd != -1 && zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 855 | Usage("--zip-location should be supplied with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 858 | if (boot_image_option.empty()) { |
| 859 | if (image_base == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 860 | Usage("non-zero --base not specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 861 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 864 | std::string oat_stripped(oat_filename); |
| 865 | std::string oat_unstripped; |
| 866 | if (!oat_symbols.empty()) { |
| 867 | oat_unstripped += oat_symbols; |
| 868 | } else { |
| 869 | oat_unstripped += oat_filename; |
| 870 | } |
| 871 | |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 872 | // Done with usage checks, enable watchdog if requested |
| 873 | WatchDog watch_dog(watch_dog_enabled); |
| 874 | |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 875 | // Check early that the result of compilation can be written |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 876 | UniquePtr<File> oat_file; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 877 | bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 878 | if (create_file) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 879 | oat_file.reset(OS::OpenFile(oat_unstripped.c_str(), true)); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 880 | if (oat_location.empty()) { |
| 881 | oat_location = oat_filename; |
| 882 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 883 | } else { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 884 | oat_file.reset(new File(oat_fd, oat_location)); |
| 885 | oat_file->DisableAutoClose(); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 886 | } |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 887 | if (oat_file.get() == NULL) { |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 888 | PLOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 889 | return EXIT_FAILURE; |
| 890 | } |
| 891 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 892 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 893 | return EXIT_FAILURE; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 894 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 895 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 896 | LOG(INFO) << "dex2oat: " << oat_location; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 897 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 898 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 899 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 900 | std::vector<const DexFile*> boot_class_path; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 901 | if (boot_image_option.empty()) { |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 902 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); |
| 903 | if (failure_count > 0) { |
| 904 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 905 | return EXIT_FAILURE; |
| 906 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 907 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 908 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 909 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 910 | } |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 911 | if (host_prefix.get() != NULL) { |
| 912 | options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 913 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 914 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 915 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 916 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 917 | |
Anwar Ghuloum | 8447d84 | 2013-04-30 17:27:40 -0700 | [diff] [blame] | 918 | #if ART_SMALL_MODE |
| 919 | options.push_back(std::make_pair("-small", reinterpret_cast<void*>(NULL))); |
| 920 | #endif // ART_SMALL_MODE |
| 921 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 922 | Dex2Oat* p_dex2oat; |
Anwar Ghuloum | 8447d84 | 2013-04-30 17:27:40 -0700 | [diff] [blame] | 923 | if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count, |
| 924 | support_debugging)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 925 | LOG(ERROR) << "Failed to create dex2oat"; |
| 926 | return EXIT_FAILURE; |
| 927 | } |
| 928 | UniquePtr<Dex2Oat> dex2oat(p_dex2oat); |
| 929 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 930 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 931 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 932 | // Whilst we're in native take the opportunity to initialize well known classes. |
| 933 | WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv()); |
| 934 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 935 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 936 | // If --image-classes was specified, calculate the full list of classes to include in the image |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 937 | UniquePtr<const std::set<std::string> > image_classes(NULL); |
| 938 | if (image_classes_filename != NULL) { |
| 939 | image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); |
| 940 | if (image_classes.get() == NULL) { |
| 941 | LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; |
| 942 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 946 | std::vector<const DexFile*> dex_files; |
| 947 | if (boot_image_option.empty()) { |
| 948 | dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 949 | } else { |
| 950 | if (dex_filenames.empty()) { |
| 951 | UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd)); |
| 952 | if (zip_archive.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 953 | LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location; |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 954 | return EXIT_FAILURE; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 955 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 956 | const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 957 | if (dex_file == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 958 | LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 959 | return EXIT_FAILURE; |
| 960 | } |
| 961 | dex_files.push_back(dex_file); |
| 962 | } else { |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 963 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); |
| 964 | if (failure_count > 0) { |
| 965 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 966 | return EXIT_FAILURE; |
| 967 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
Anwar Ghuloum | 8447d84 | 2013-04-30 17:27:40 -0700 | [diff] [blame] | 971 | // If we're in small mode, but the program is small, turn off small mode. |
| 972 | // It doesn't make a difference for the boot image, so let's skip the check |
| 973 | // altogether. |
| 974 | if (Runtime::Current()->IsSmallMode() && !image) { |
| 975 | size_t num_methods = 0; |
| 976 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 977 | const DexFile* dex_file = dex_files[i]; |
| 978 | CHECK(dex_file != NULL); |
| 979 | num_methods += dex_file->NumMethodIds(); |
| 980 | } |
| 981 | if (num_methods <= Runtime::Current()->GetSmallModeMethodThreshold()) { |
| 982 | Runtime::Current()->SetSmallMode(false); |
| 983 | LOG(INFO) << "Below method threshold, compiling anyways"; |
| 984 | } |
| 985 | } |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 986 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 987 | UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option, |
| 988 | host_prefix.get(), |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 989 | android_root, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 990 | is_host, |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 991 | dex_files, |
| 992 | oat_file.get(), |
| 993 | bitcode_filename, |
| 994 | image, |
| 995 | image_classes.get(), |
| 996 | dump_stats, |
| 997 | dump_timings)); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 998 | |
| 999 | if (compiler.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1000 | LOG(ERROR) << "Failed to create oat file: " << oat_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1001 | return EXIT_FAILURE; |
| 1002 | } |
| 1003 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1004 | LOG(INFO) << "Oat file written successfully (unstripped): " << oat_location; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1005 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1006 | // Notes on the interleaving of creating the image and oat file to |
| 1007 | // ensure the references between the two are correct. |
| 1008 | // |
| 1009 | // Currently we have a memory layout that looks something like this: |
| 1010 | // |
| 1011 | // +--------------+ |
| 1012 | // | image | |
| 1013 | // +--------------+ |
| 1014 | // | boot oat | |
| 1015 | // +--------------+ |
| 1016 | // | alloc spaces | |
| 1017 | // +--------------+ |
| 1018 | // |
| 1019 | // There are several constraints on the loading of the imag and boot.oat. |
| 1020 | // |
| 1021 | // 1. The image is expected to be loaded at an absolute address and |
| 1022 | // contains Objects with absolute pointers within the image. |
| 1023 | // |
| 1024 | // 2. There are absolute pointers from Methods in the image to their |
| 1025 | // code in the oat. |
| 1026 | // |
| 1027 | // 3. There are absolute pointers from the code in the oat to Methods |
| 1028 | // in the image. |
| 1029 | // |
| 1030 | // 4. There are absolute pointers from code in the oat to other code |
| 1031 | // in the oat. |
| 1032 | // |
| 1033 | // To get this all correct, we go through several steps. |
| 1034 | // |
| 1035 | // 1. We have already created that oat file above with |
| 1036 | // CreateOatFile. Originally this was just our own proprietary file |
| 1037 | // but now it is contained within an ELF dynamic object (aka .so |
| 1038 | // file). The Compiler returned by CreateOatFile provides |
| 1039 | // PatchInformation for references to oat code and Methods that need |
| 1040 | // to be update once we know where the oat file will be located |
| 1041 | // after the image. |
| 1042 | // |
| 1043 | // 2. We create the image file. It needs to know where the oat file |
| 1044 | // will be loaded after itself. Originally when oat file was simply |
| 1045 | // memory mapped so we could predict where its contents were based |
| 1046 | // on the file size. Now that it is an ELF file, we need to inspect |
| 1047 | // the ELF file to understand the in memory segment layout including |
| 1048 | // where the oat header is located within. ImageWriter's |
| 1049 | // PatchOatCodeAndMethods uses the PatchInformation from the |
| 1050 | // Compiler to touch up absolute references in the oat file. |
| 1051 | // |
| 1052 | // 3. We fixup the ELF program headers so that dlopen will try to |
| 1053 | // load the .so at the desired location at runtime by offsetting the |
| 1054 | // Elf32_Phdr.p_vaddr values by the desired base address. |
| 1055 | // |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1056 | if (image) { |
| 1057 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 1058 | bool image_creation_success = dex2oat->CreateImageFile(image_filename, |
| 1059 | image_base, |
| 1060 | image_classes.get(), |
| 1061 | oat_unstripped, |
| 1062 | oat_location, |
| 1063 | *compiler.get()); |
| 1064 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1065 | LOG(INFO) << "Image written successfully: " << image_filename; |
| 1066 | if (!image_creation_success) { |
| 1067 | return EXIT_FAILURE; |
| 1068 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1071 | if (is_host) { |
| 1072 | return EXIT_SUCCESS; |
| 1073 | } |
| 1074 | |
| 1075 | // If we don't want to strip in place, copy from unstripped location to stripped location. |
| 1076 | // We need to strip after image creation because FixupElf needs to use .strtab. |
| 1077 | if (oat_unstripped != oat_stripped) { |
| 1078 | oat_file.reset(); |
| 1079 | UniquePtr<File> in(OS::OpenFile(oat_unstripped.c_str(), false)); |
| 1080 | UniquePtr<File> out(OS::OpenFile(oat_stripped.c_str(), true)); |
| 1081 | size_t buffer_size = 8192; |
| 1082 | UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]); |
| 1083 | while (true) { |
| 1084 | int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); |
| 1085 | if (bytes_read <= 0) { |
| 1086 | break; |
| 1087 | } |
| 1088 | bool write_ok = out->WriteFully(buffer.get(), bytes_read); |
| 1089 | CHECK(write_ok); |
| 1090 | } |
| 1091 | oat_file.reset(out.release()); |
| 1092 | LOG(INFO) << "Oat file copied successfully (stripped): " << oat_stripped; |
| 1093 | } |
| 1094 | |
| 1095 | // Strip unneeded sections for target |
| 1096 | off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET); |
| 1097 | CHECK_EQ(0, seek_actual); |
| 1098 | compiler->StripElf(oat_file.get()); |
| 1099 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1100 | // We wrote the oat file successfully, and want to keep it. |
Brian Carlstrom | 54d22c2 | 2013-03-13 15:14:57 -0700 | [diff] [blame] | 1101 | LOG(INFO) << "Oat file written successfully (stripped): " << oat_location; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1102 | return EXIT_SUCCESS; |
| 1103 | } |
| 1104 | |
| 1105 | } // namespace art |
| 1106 | |
| 1107 | int main(int argc, char** argv) { |
| 1108 | return art::dex2oat(argc, argv); |
| 1109 | } |