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