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