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 | |
| 26 | #include "class_linker.h" |
| 27 | #include "class_loader.h" |
| 28 | #include "compiler.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 29 | #include "file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 30 | #include "image_writer.h" |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 31 | #include "leb128.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | #include "oat_writer.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 33 | #include "object_utils.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 34 | #include "os.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 35 | #include "runtime.h" |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 36 | #include "stl_util.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 37 | #include "stringpiece.h" |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 38 | #include "timing_logger.h" |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 39 | #include "zip_archive.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | b08e8a3 | 2012-04-02 10:51:41 -0700 | [diff] [blame] | 41 | #if defined(__APPLE__) |
| 42 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
| 43 | #endif |
| 44 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 45 | namespace art { |
| 46 | |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 47 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 48 | std::string error; |
| 49 | StringAppendV(&error, fmt, ap); |
| 50 | LOG(ERROR) << error; |
| 51 | } |
| 52 | |
| 53 | static void UsageError(const char* fmt, ...) { |
| 54 | va_list ap; |
| 55 | va_start(ap, fmt); |
| 56 | UsageErrorV(fmt, ap); |
| 57 | va_end(ap); |
| 58 | } |
| 59 | |
| 60 | static void Usage(const char* fmt, ...) { |
| 61 | va_list ap; |
| 62 | va_start(ap, fmt); |
| 63 | UsageErrorV(fmt, ap); |
| 64 | va_end(ap); |
| 65 | |
| 66 | UsageError("Usage: dex2oat [options]..."); |
| 67 | UsageError(""); |
| 68 | UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); |
| 69 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 70 | UsageError(""); |
| 71 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 72 | UsageError(" containing a classes.dex file to compile."); |
| 73 | UsageError(" Example: --zip-fd=5"); |
| 74 | UsageError(""); |
| 75 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding"); |
| 76 | UsageError(" to the file descriptor specified by --zip-fd."); |
| 77 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 78 | UsageError(""); |
| 79 | UsageError(" --oat-file=<file.oat>: specifies the required oat filename."); |
| 80 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 81 | UsageError(""); |
| 82 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 83 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 84 | UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat"); |
| 85 | UsageError(""); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 86 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 87 | UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); |
| 88 | UsageError(" Example: --bitcode=/system/framework/boot.bc"); |
| 89 | UsageError(""); |
| 90 | #endif |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 91 | UsageError(" --image=<file.art>: specifies the output image filename."); |
| 92 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 93 | UsageError(""); |
| 94 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 95 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 96 | UsageError(""); |
| 97 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 98 | UsageError(" Example: --base=0x50000000"); |
| 99 | UsageError(""); |
| 100 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
| 101 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
| 102 | UsageError(" Default: <host-prefix>/system/framework/boot.art"); |
| 103 | UsageError(""); |
| 104 | UsageError(" --host-prefix may be used to translate host paths to target paths during"); |
| 105 | UsageError(" cross compilation."); |
| 106 | UsageError(" Example: --host-prefix=out/target/product/crespo"); |
| 107 | UsageError(" Default: $ANDROID_PRODUCT_OUT"); |
| 108 | UsageError(""); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 109 | UsageError(" --instruction-set=(ARM|Thumb2|MIPS|X86): compile for a particular instruction"); |
| 110 | UsageError(" set."); |
| 111 | UsageError(" Example: --instruction-set=X86"); |
| 112 | UsageError(" Default: Thumb2"); |
| 113 | UsageError(""); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 114 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 115 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 116 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 117 | UsageError(" Example: --runtime-arg -Xms256m"); |
| 118 | UsageError(""); |
| 119 | std::cerr << "See log for usage error information\n"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 120 | exit(EXIT_FAILURE); |
| 121 | } |
| 122 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 123 | class Dex2Oat { |
| 124 | public: |
| 125 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 126 | static Dex2Oat* Create(Runtime::Options& options, InstructionSet instruction_set, |
| 127 | size_t thread_count, bool support_debugging) { |
| 128 | UniquePtr<Runtime> runtime(CreateRuntime(options, instruction_set)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 129 | if (runtime.get() == NULL) { |
| 130 | return NULL; |
| 131 | } |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 132 | return new Dex2Oat(runtime.release(), instruction_set, thread_count, support_debugging); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | ~Dex2Oat() { |
| 136 | delete runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 137 | LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Make a list of descriptors for classes to include in the image |
| 141 | const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) { |
| 142 | UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); |
| 143 | if (image_classes_file.get() == NULL) { |
| 144 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 148 | // Load all the classes specified in the file |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 149 | ClassLinker* class_linker = runtime_->GetClassLinker(); |
| 150 | while (image_classes_file->good()) { |
| 151 | std::string dot; |
| 152 | std::getline(*image_classes_file.get(), dot); |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 153 | if (StartsWith(dot, "#") || dot.empty()) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 154 | continue; |
| 155 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 156 | std::string descriptor(DotToDescriptor(dot.c_str())); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 157 | SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str())); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 158 | if (klass.get() == NULL) { |
| 159 | LOG(WARNING) << "Failed to find class " << descriptor; |
| 160 | Thread::Current()->ClearException(); |
| 161 | } |
| 162 | } |
| 163 | image_classes_file->close(); |
| 164 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 165 | // Resolve exception classes referenced by the loaded classes. The catch logic assumes |
| 166 | // exceptions are resolved by the verifier when there is a catch block in an interested method. |
| 167 | // Do this here so that exception classes appear to have been specified image classes. |
| 168 | std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; |
| 169 | do { |
| 170 | unresolved_exception_types.clear(); |
| 171 | class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, |
| 172 | &unresolved_exception_types); |
| 173 | typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto |
| 174 | for (It it = unresolved_exception_types.begin(), |
| 175 | end = unresolved_exception_types.end(); |
| 176 | it != end; ++it) { |
| 177 | uint16_t exception_type_idx = it->first; |
| 178 | const DexFile* dex_file = it->second; |
| 179 | DexCache* dex_cache = class_linker->FindDexCache(*dex_file); |
| 180 | ClassLoader* class_loader = NULL; |
| 181 | SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache, |
| 182 | class_loader)); |
| 183 | if (klass.get() == NULL) { |
| 184 | const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); |
| 185 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 186 | LOG(FATAL) << "Failed to resolve class " << descriptor; |
| 187 | } |
| 188 | DCHECK(klass->IsThrowableClass()); |
| 189 | } |
| 190 | // Resolving exceptions may load classes that reference more exceptions, iterate until no |
| 191 | // more are found |
| 192 | } while (!unresolved_exception_types.empty()); |
| 193 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 194 | // We walk the roots looking for classes so that we'll pick up the |
| 195 | // above classes plus any classes them depend on such super |
| 196 | // classes, interfaces, and the required ClassLinker roots. |
| 197 | UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>()); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 198 | class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 199 | CHECK_NE(image_classes->size(), 0U); |
| 200 | return image_classes.release(); |
| 201 | } |
| 202 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 203 | const Compiler* CreateOatFile(const std::string& boot_image_option, |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 204 | const std::string* host_prefix, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 205 | const std::vector<const DexFile*>& dex_files, |
| 206 | File* oat_file, |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 207 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | de08e84 | 2012-03-21 00:34:12 +0800 | [diff] [blame] | 208 | const std::string& bitcode_filename, |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 209 | #endif |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 210 | bool image, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 211 | const std::set<std::string>* image_classes, |
| 212 | bool dump_stats, |
| 213 | bool dump_timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 214 | // SirtRef and ClassLoader creation needs to come after Runtime::Create |
| 215 | UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL)); |
| 216 | if (class_loader.get() == NULL) { |
| 217 | LOG(ERROR) << "Failed to create SirtRef for class loader"; |
| 218 | return false; |
| 219 | } |
| 220 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 221 | if (!boot_image_option.empty()) { |
| 222 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 223 | std::vector<const DexFile*> class_path_files(dex_files); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 224 | OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 225 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 226 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 227 | } |
| 228 | class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 231 | UniquePtr<Compiler> compiler(new Compiler(instruction_set_, |
| 232 | image, |
| 233 | thread_count_, |
| 234 | support_debugging_, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 235 | image_classes, |
| 236 | dump_stats, |
| 237 | dump_timings)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 238 | |
| 239 | #if defined(ART_USE_LLVM_COMPILER) |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 240 | compiler->SetBitcodeFileName(bitcode_filename); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 241 | #endif |
| 242 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 243 | compiler->CompileAll(class_loader->get(), dex_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 244 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 245 | std::string image_file_location; |
| 246 | uint32_t image_file_location_checksum = 0; |
| 247 | Heap* heap = Runtime::Current()->GetHeap(); |
| 248 | if (heap->GetSpaces().size() > 1) { |
| 249 | ImageSpace* image_space = heap->GetImageSpace(); |
| 250 | image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum(); |
| 251 | image_file_location = image_space->GetImageFilename(); |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 252 | if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { |
| 253 | image_file_location = image_file_location.substr(host_prefix->size()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | if (!OatWriter::Create(oat_file, |
| 258 | class_loader->get(), |
| 259 | dex_files, |
| 260 | image_file_location_checksum, |
| 261 | image_file_location, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 262 | *compiler.get())) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 263 | LOG(ERROR) << "Failed to create oat file " << oat_file->name(); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 264 | return NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 265 | } |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 266 | return compiler.release(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 269 | bool CreateImageFile(const std::string& image_filename, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 270 | uintptr_t image_base, |
| 271 | const std::set<std::string>* image_classes, |
| 272 | const std::string& oat_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 273 | const std::string& oat_location, |
| 274 | const Compiler& compiler) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 275 | ImageWriter image_writer(image_classes); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 276 | if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 277 | LOG(ERROR) << "Failed to create image file " << image_filename; |
| 278 | return false; |
| 279 | } |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | private: |
| 284 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 285 | explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count, |
| 286 | bool support_debugging) |
| 287 | : instruction_set_(instruction_set), |
| 288 | runtime_(runtime), |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 289 | thread_count_(thread_count), |
| 290 | support_debugging_(support_debugging), |
| 291 | start_ns_(NanoTime()) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 292 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 293 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 294 | static Runtime* CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 295 | Runtime* runtime = Runtime::Create(options, false); |
| 296 | if (runtime == NULL) { |
| 297 | LOG(ERROR) << "Failed to create runtime"; |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | // if we loaded an existing image, we will reuse values from the image roots. |
| 302 | if (!runtime->HasJniDlsymLookupStub()) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 303 | runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 304 | } |
| 305 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 306 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 307 | } |
| 308 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 309 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 310 | if (!runtime->HasResolutionStubArray(type)) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 311 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 314 | if (!runtime->HasResolutionMethod()) { |
| 315 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 316 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 317 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 318 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 319 | if (!runtime->HasCalleeSaveMethod(type)) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 320 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 323 | runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 324 | return runtime; |
| 325 | } |
| 326 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 327 | static void ResolveExceptionsForMethod(MethodHelper* mh, |
| 328 | std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) { |
| 329 | const DexFile::CodeItem* code_item = mh->GetCodeItem(); |
| 330 | if (code_item == NULL) { |
| 331 | return; // native or abstract method |
| 332 | } |
| 333 | if (code_item->tries_size_ == 0) { |
| 334 | return; // nothing to process |
| 335 | } |
| 336 | const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); |
| 337 | size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 338 | for (size_t i = 0; i < num_encoded_catch_handlers; i++) { |
| 339 | int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); |
| 340 | bool has_catch_all = false; |
| 341 | if (encoded_catch_handler_size <= 0) { |
| 342 | encoded_catch_handler_size = -encoded_catch_handler_size; |
| 343 | has_catch_all = true; |
| 344 | } |
| 345 | for (int32_t j = 0; j < encoded_catch_handler_size; j++) { |
| 346 | uint16_t encoded_catch_handler_handlers_type_idx = |
| 347 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 348 | // Add to set of types to resolve if not already in the dex cache resolved types |
| 349 | if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { |
| 350 | exceptions_to_resolve.insert( |
| 351 | std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, |
| 352 | &mh->GetDexFile())); |
| 353 | } |
| 354 | // ignore address associated with catch handler |
| 355 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 356 | } |
| 357 | if (has_catch_all) { |
| 358 | // ignore catch all address |
| 359 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) { |
| 364 | std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = |
| 365 | reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); |
| 366 | MethodHelper mh; |
| 367 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 368 | Method* m = c->GetVirtualMethod(i); |
| 369 | mh.ChangeMethod(m); |
| 370 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 371 | } |
| 372 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 373 | Method* m = c->GetDirectMethod(i); |
| 374 | mh.ChangeMethod(m); |
| 375 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | static bool RecordImageClassesVisitor(Class* klass, void* arg) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 380 | std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); |
| 381 | if (klass->IsArrayClass() || klass->IsPrimitive()) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 382 | return true; |
| 383 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 384 | image_classes->insert(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 385 | return true; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 386 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 387 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 388 | // Appends to dex_files any elements of class_path that it doesn't already |
| 389 | // contain. This will open those dex files as necessary. |
| 390 | static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { |
| 391 | std::vector<std::string> parsed; |
| 392 | Split(class_path, ':', parsed); |
| 393 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 394 | if (DexFilesContains(dex_files, parsed[i])) { |
| 395 | continue; |
| 396 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 397 | const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 398 | if (dex_file == NULL) { |
| 399 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 400 | } else { |
| 401 | dex_files.push_back(dex_file); |
| 402 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 403 | } |
| 404 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 405 | |
| 406 | // Returns true if dex_files has a dex with the named location. |
| 407 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { |
| 408 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 409 | if (dex_files[i]->GetLocation() == location) { |
| 410 | return true; |
| 411 | } |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 416 | const InstructionSet instruction_set_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 417 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 418 | Runtime* runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 419 | size_t thread_count_; |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 420 | bool support_debugging_; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 421 | uint64_t start_ns_; |
| 422 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 423 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 424 | }; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 425 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 426 | static bool ParseInt(const char* in, int* out) { |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 427 | char* end; |
| 428 | int result = strtol(in, &end, 10); |
| 429 | if (in == end || *end != '\0') { |
| 430 | return false; |
| 431 | } |
| 432 | *out = result; |
| 433 | return true; |
| 434 | } |
| 435 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 436 | static void OpenDexFiles(const std::vector<const char*>& dex_filenames, |
| 437 | const std::vector<const char*>& dex_locations, |
| 438 | std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 439 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 440 | const char* dex_filename = dex_filenames[i]; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 441 | const char* dex_location = dex_locations[i]; |
| 442 | const DexFile* dex_file = DexFile::Open(dex_filename, dex_location); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 443 | if (dex_file == NULL) { |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 444 | LOG(WARNING) << "could not open .dex from file " << dex_filename; |
| 445 | } else { |
| 446 | dex_files.push_back(dex_file); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 447 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 451 | static int dex2oat(int argc, char** argv) { |
| 452 | InitLogging(); |
| 453 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 454 | // Skip over argv[0]. |
| 455 | argv++; |
| 456 | argc--; |
| 457 | |
| 458 | if (argc == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 459 | Usage("no arguments specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | std::vector<const char*> dex_filenames; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 463 | std::vector<const char*> dex_locations; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 464 | int zip_fd = -1; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 465 | std::string zip_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 466 | std::string oat_filename; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 467 | std::string oat_location; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 468 | int oat_fd = -1; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 469 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 470 | std::string bitcode_filename; |
| 471 | #endif |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 472 | const char* image_classes_filename = NULL; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 473 | std::string image_filename; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 474 | std::string boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 475 | uintptr_t image_base = 0; |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 476 | UniquePtr<std::string> host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 477 | std::vector<const char*> runtime_args; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 478 | int thread_count = 2; |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 479 | bool support_debugging = false; |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 480 | InstructionSet instruction_set = kThumb2; |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 481 | bool dump_stats = kIsDebugBuild; |
| 482 | bool dump_timings = kIsDebugBuild; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 483 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 484 | for (int i = 0; i < argc; i++) { |
| 485 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 486 | bool log_options = false; |
| 487 | if (log_options) { |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 488 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 489 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 490 | if (option.starts_with("--dex-file=")) { |
| 491 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 492 | } else if (option.starts_with("--dex-location=")) { |
| 493 | dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 494 | } else if (option.starts_with("--zip-fd=")) { |
| 495 | const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 496 | if (!ParseInt(zip_fd_str, &zip_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 497 | 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] | 498 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 499 | } else if (option.starts_with("--zip-location=")) { |
| 500 | zip_location = option.substr(strlen("--zip-location=")).data(); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 501 | } else if (option.starts_with("--oat-file=")) { |
| 502 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
| 503 | } else if (option.starts_with("--oat-fd=")) { |
| 504 | const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 505 | if (!ParseInt(oat_fd_str, &oat_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 506 | 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] | 507 | } |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 508 | } else if (option.starts_with("-g")) { |
| 509 | support_debugging = true; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 510 | } else if (option.starts_with("-j")) { |
Brian Carlstrom | b12552a | 2012-02-04 17:17:31 -0800 | [diff] [blame] | 511 | const char* thread_count_str = option.substr(strlen("-j")).data(); |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 512 | if (!ParseInt(thread_count_str, &thread_count)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 513 | 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] | 514 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 515 | } else if (option.starts_with("--oat-location=")) { |
| 516 | oat_location = option.substr(strlen("--oat-location=")).data(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 517 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 518 | } else if (option.starts_with("--bitcode=")) { |
| 519 | bitcode_filename = option.substr(strlen("--bitcode=")).data(); |
| 520 | #endif |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 521 | } else if (option.starts_with("--image=")) { |
| 522 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 523 | } else if (option.starts_with("--image-classes=")) { |
| 524 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 525 | } else if (option.starts_with("--base=")) { |
| 526 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 527 | char* end; |
| 528 | image_base = strtoul(image_base_str, &end, 16); |
| 529 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 530 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 531 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 532 | } else if (option.starts_with("--boot-image=")) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 533 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 534 | } else if (option.starts_with("--host-prefix=")) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 535 | host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 536 | } else if (option.starts_with("--instruction-set=")) { |
| 537 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
| 538 | if (instruction_set_str == "Thumb2" || instruction_set_str == "ARM") { |
| 539 | instruction_set = kThumb2; |
| 540 | } else if (instruction_set_str == "MIPS") { |
| 541 | instruction_set = kMips; |
| 542 | } else if (instruction_set_str == "X86") { |
| 543 | instruction_set = kX86; |
| 544 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 545 | } else if (option == "--runtime-arg") { |
| 546 | if (++i >= argc) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 547 | Usage("Missing required argument for --runtime-arg"); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 548 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 549 | if (log_options) { |
| 550 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 551 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 552 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 553 | } else { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 554 | Usage("unknown argument %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Elliott Hughes | b907a3f | 2012-04-02 16:11:36 -0700 | [diff] [blame] | 558 | #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1060 |
| 559 | // Something is broken on Mac OS 10.5, and we don't have direct access to any machines running it. |
| 560 | // Restricting dex2oat to a single thread on Mac OS 10.5 appears to be a workaround. |
| 561 | thread_count = 1; |
| 562 | #endif |
| 563 | |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame^] | 564 | #if defined(ART_USE_LLVM_COMPILER) && defined(ART_TARGET) |
| 565 | // To avoid high memory usage, always run dex2oat in single thread mode when |
| 566 | // we are using LLVM-based compiler. |
| 567 | thread_count = 1; |
| 568 | #endif |
| 569 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 570 | if (oat_filename.empty() && oat_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 571 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (!oat_filename.empty() && oat_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 575 | Usage("--oat-file should not be used with --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | if (!oat_filename.empty() && oat_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 579 | Usage("--oat-file should not be used with --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 582 | if (oat_fd != -1 && !image_filename.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 583 | Usage("--oat-fd should not be used with --image"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 586 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 587 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 588 | if (android_product_out != NULL) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 589 | host_prefix.reset(new std::string(android_product_out)); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 593 | bool image = (!image_filename.empty()); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 594 | if (!image && boot_image_filename.empty()) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 595 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 596 | boot_image_filename += GetAndroidRoot(); |
| 597 | } else { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 598 | boot_image_filename += *host_prefix.get(); |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 599 | boot_image_filename += "/system"; |
| 600 | } |
| 601 | boot_image_filename += "/framework/boot.art"; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 602 | } |
| 603 | std::string boot_image_option; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 604 | if (!boot_image_filename.empty()) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 605 | boot_image_option += "-Ximage:"; |
| 606 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 609 | if (image_classes_filename != NULL && !image) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 610 | Usage("--image-classes should only be used with --image"); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (image_classes_filename != NULL && !boot_image_option.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 614 | Usage("--image-classes should not be used with --boot-image"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 617 | if (dex_filenames.empty() && zip_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 618 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | if (!dex_filenames.empty() && zip_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 622 | Usage("--dex-file should not be used with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 625 | if (!dex_filenames.empty() && !zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 626 | Usage("--dex-file should not be used with --zip-location"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 629 | if (dex_locations.empty()) { |
| 630 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 631 | dex_locations.push_back(dex_filenames[i]); |
| 632 | } |
| 633 | } else if (dex_locations.size() != dex_filenames.size()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 634 | Usage("--dex-location arguments do not match --dex-file arguments"); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (zip_fd != -1 && zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 638 | Usage("--zip-location should be supplied with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 641 | if (boot_image_option.empty()) { |
| 642 | if (image_base == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 643 | Usage("non-zero --base not specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 644 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 647 | // Check early that the result of compilation can be written |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 648 | UniquePtr<File> oat_file; |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 649 | bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor |
| 650 | if (create_file) { |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 651 | oat_file.reset(OS::OpenFile(oat_filename.c_str(), true)); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 652 | if (oat_location.empty()) { |
| 653 | oat_location = oat_filename; |
| 654 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 655 | } else { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 656 | oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd)); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 657 | } |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 658 | if (oat_file.get() == NULL) { |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 659 | PLOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 660 | return EXIT_FAILURE; |
| 661 | } |
| 662 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 663 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 664 | return EXIT_FAILURE; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 665 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 666 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 667 | LOG(INFO) << "dex2oat: " << oat_location; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 668 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 669 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 670 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 671 | std::vector<const DexFile*> boot_class_path; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 672 | if (boot_image_option.empty()) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 673 | OpenDexFiles(dex_filenames, dex_locations, boot_class_path); |
| 674 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 675 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 676 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 677 | } |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 678 | if (host_prefix.get() != NULL) { |
| 679 | options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 680 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 681 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 682 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 683 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 684 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 685 | UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, instruction_set, thread_count, |
| 686 | support_debugging)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 687 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 688 | // 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] | 689 | UniquePtr<const std::set<std::string> > image_classes(NULL); |
| 690 | if (image_classes_filename != NULL) { |
| 691 | image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); |
| 692 | if (image_classes.get() == NULL) { |
| 693 | LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; |
| 694 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 698 | std::vector<const DexFile*> dex_files; |
| 699 | if (boot_image_option.empty()) { |
| 700 | dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 701 | } else { |
| 702 | if (dex_filenames.empty()) { |
| 703 | UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd)); |
| 704 | if (zip_archive.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 705 | LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location; |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 706 | return EXIT_FAILURE; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 707 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 708 | const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 709 | if (dex_file == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 710 | 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] | 711 | return EXIT_FAILURE; |
| 712 | } |
| 713 | dex_files.push_back(dex_file); |
| 714 | } else { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 715 | OpenDexFiles(dex_filenames, dex_locations, dex_files); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 719 | UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option, |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 720 | host_prefix.get(), |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 721 | dex_files, |
| 722 | oat_file.get(), |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 723 | #if defined(ART_USE_LLVM_COMPILER) |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 724 | bitcode_filename, |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 725 | #endif |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 726 | image, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 727 | image_classes.get(), |
| 728 | dump_stats, |
| 729 | dump_timings)); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 730 | |
| 731 | if (compiler.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 732 | LOG(ERROR) << "Failed to create oat file: " << oat_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 733 | return EXIT_FAILURE; |
| 734 | } |
| 735 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 736 | if (!image) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 737 | LOG(INFO) << "Oat file written successfully: " << oat_location; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 738 | return EXIT_SUCCESS; |
| 739 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 740 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 741 | if (!dex2oat->CreateImageFile(image_filename, |
| 742 | image_base, |
| 743 | image_classes.get(), |
| 744 | oat_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 745 | oat_location, |
| 746 | *compiler.get())) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 747 | return EXIT_FAILURE; |
| 748 | } |
| 749 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 750 | // We wrote the oat file successfully, and want to keep it. |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 751 | LOG(INFO) << "Oat file written successfully: " << oat_filename; |
| 752 | LOG(INFO) << "Image written successfully: " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 753 | return EXIT_SUCCESS; |
| 754 | } |
| 755 | |
| 756 | } // namespace art |
| 757 | |
| 758 | int main(int argc, char** argv) { |
| 759 | return art::dex2oat(argc, argv); |
| 760 | } |