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