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