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