blob: b3aec1a4cb761a33a7337d2aa868ac0fba5d8900 [file] [log] [blame]
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4#include <stdlib.h>
5
Brian Carlstromae826982011-11-09 01:33:42 -08006#include <iostream>
7#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07008#include <string>
9#include <vector>
10
11#include "class_linker.h"
12#include "class_loader.h"
13#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070014#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070015#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080016#include "leb128.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070017#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080018#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070019#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070020#include "runtime.h"
21#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080022#include "timing_logger.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080023#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070024
25namespace art {
26
27static void usage() {
28 fprintf(stderr,
29 "Usage: dex2oat [options]...\n"
30 "\n");
31 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080032 " --dex-file=<dex-file>: specifies a .dex file to compile.\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070033 " Example: --dex-file=/system/framework/core.jar\n"
34 "\n");
35 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080036 " --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file\n"
37 " containing a classes.dex file to compile.\n"
38 " Example: --zip-fd=5\n"
39 "\n");
40 fprintf(stderr,
41 " --zip-name=<zip-name>: specifies a symbolic name for the file corresponding\n"
42 " to the file descriptor specified by --zip-fd.\n"
43 " Example: --zip-name=/system/app/Calculator.apk\n"
44 "\n");
45 fprintf(stderr,
46 " --oat-file=<file.oat>: specifies the required oat filename.\n"
47 " Example: --oat-file=/system/framework/boot.oat\n"
48 "\n");
49 fprintf(stderr,
50 " --oat-name=<oat-name>: specifies a symbolic name for the file corresponding\n"
51 " to the file descriptor specified by --oat-fd.\n"
52 " Example: --oat-name=/data/art-cache/system@app@Calculator.apk.oat\n"
Brian Carlstromae826982011-11-09 01:33:42 -080053 "\n");
54 fprintf(stderr,
55 " --image=<file.art>: specifies the output image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080056 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070058 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080059 " --image-classes=<classname-file>: specifies classes to include in an image.\n"
60 " Example: --image=frameworks/base/preloaded-classes\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070061 "\n");
62 fprintf(stderr,
63 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
64 " Example: --base=0x50000000\n"
65 "\n");
66 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070067 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080068 " Example: --boot-image=/system/framework/boot.art\n"
69 " Default: <host-prefix>/system/framework/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070070 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070071 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070072 " --host-prefix may be used to translate host paths to target paths during\n"
73 " cross compilation.\n"
74 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromb0011262011-12-09 12:17:24 -080075 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070076 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070077 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070078 " --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
79 " such as initial heap size, maximum heap size, and verbose output.\n"
80 " Use a separate --runtime-arg switch for each argument.\n"
81 " Example: --runtime-arg -Xms256m\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070082 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070083 exit(EXIT_FAILURE);
84}
85
Brian Carlstromae826982011-11-09 01:33:42 -080086class Dex2Oat {
87 public:
88
89 static Dex2Oat* Create(Runtime::Options& options) {
90 UniquePtr<Runtime> runtime(CreateRuntime(options));
91 if (runtime.get() == NULL) {
92 return NULL;
93 }
94 return new Dex2Oat(runtime.release());
95 }
96
97 ~Dex2Oat() {
98 delete runtime_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -080099 LOG(INFO) << "dex2oat took " << NsToMs(NanoTime() - start_ns_) << "ms";
Brian Carlstromae826982011-11-09 01:33:42 -0800100 }
101
102 // Make a list of descriptors for classes to include in the image
103 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
104 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
105 if (image_classes_file.get() == NULL) {
106 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
107 return NULL;
108 }
109
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800110 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800111 ClassLinker* class_linker = runtime_->GetClassLinker();
112 while (image_classes_file->good()) {
113 std::string dot;
114 std::getline(*image_classes_file.get(), dot);
115 if (StringPiece(dot).starts_with("#") || dot.empty()) {
116 continue;
117 }
Elliott Hughes95572412011-12-13 18:14:20 -0800118 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800119 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800120 if (klass.get() == NULL) {
121 LOG(WARNING) << "Failed to find class " << descriptor;
122 Thread::Current()->ClearException();
123 }
124 }
125 image_classes_file->close();
126
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800127 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
128 // exceptions are resolved by the verifier when there is a catch block in an interested method.
129 // Do this here so that exception classes appear to have been specified image classes.
130 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
131 do {
132 unresolved_exception_types.clear();
133 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
134 &unresolved_exception_types);
135 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
136 for (It it = unresolved_exception_types.begin(),
137 end = unresolved_exception_types.end();
138 it != end; ++it) {
139 uint16_t exception_type_idx = it->first;
140 const DexFile* dex_file = it->second;
141 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
142 ClassLoader* class_loader = NULL;
143 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
144 class_loader));
145 if (klass.get() == NULL) {
146 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
147 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
148 LOG(FATAL) << "Failed to resolve class " << descriptor;
149 }
150 DCHECK(klass->IsThrowableClass());
151 }
152 // Resolving exceptions may load classes that reference more exceptions, iterate until no
153 // more are found
154 } while (!unresolved_exception_types.empty());
155
Brian Carlstromae826982011-11-09 01:33:42 -0800156 // We walk the roots looking for classes so that we'll pick up the
157 // above classes plus any classes them depend on such super
158 // classes, interfaces, and the required ClassLinker roots.
159 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800160 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800161 CHECK_NE(image_classes->size(), 0U);
162 return image_classes.release();
163 }
164
165 bool CreateOatFile(const std::string& boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800166 const std::vector<const DexFile*>& dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800167 File* oat_file,
168 bool image,
169 const std::set<std::string>* image_classes) {
170 // SirtRef and ClassLoader creation needs to come after Runtime::Create
171 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
172 if (class_loader.get() == NULL) {
173 LOG(ERROR) << "Failed to create SirtRef for class loader";
174 return false;
175 }
176
Brian Carlstromae826982011-11-09 01:33:42 -0800177 if (!boot_image_option.empty()) {
178 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800179 std::vector<const DexFile*> class_path_files(dex_files);
180 OpenClassPathFiles(runtime_->GetClassPath(), class_path_files);
181 for (size_t i = 0; i < class_path_files.size(); i++) {
182 class_linker->RegisterDexFile(*class_path_files[i]);
183 }
184 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800185 }
186
187 Compiler compiler(instruction_set_, image, image_classes);
188 compiler.CompileAll(class_loader->get(), dex_files);
189
jeffhao10037c82012-01-23 15:06:23 -0800190 if (!OatWriter::Create(oat_file, class_loader->get(), dex_files, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800191 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
192 return false;
193 }
194 return true;
195 }
196
197 bool CreateImageFile(const char* image_filename,
198 uintptr_t image_base,
199 const std::set<std::string>* image_classes,
200 const std::string& oat_filename,
201 const std::string& host_prefix) {
202 // If we have an existing boot image, position new space after its oat file
203 if (Heap::GetSpaces().size() > 1) {
204 Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
205 CHECK(last_image_space != NULL);
206 CHECK(last_image_space->IsImageSpace());
207 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
208 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr();
209 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
210 }
211
212 ImageWriter image_writer(image_classes);
213 if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) {
214 LOG(ERROR) << "Failed to create image file " << image_filename;
215 return false;
216 }
217 return true;
218 }
219
220 private:
221
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800222 explicit Dex2Oat(Runtime* runtime) : runtime_(runtime), start_ns_(NanoTime()) {
223 }
Brian Carlstromae826982011-11-09 01:33:42 -0800224
225 static Runtime* CreateRuntime(Runtime::Options& options) {
226 Runtime* runtime = Runtime::Create(options, false);
227 if (runtime == NULL) {
228 LOG(ERROR) << "Failed to create runtime";
229 return NULL;
230 }
231
232 // if we loaded an existing image, we will reuse values from the image roots.
233 if (!runtime->HasJniDlsymLookupStub()) {
Elliott Hughes8add92d2012-01-18 18:18:43 -0800234 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set_));
Brian Carlstromae826982011-11-09 01:33:42 -0800235 }
236 if (!runtime->HasAbstractMethodErrorStubArray()) {
237 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_));
238 }
239 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
240 Runtime::TrampolineType type = Runtime::TrampolineType(i);
241 if (!runtime->HasResolutionStubArray(type)) {
242 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type);
243 }
244 }
245 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
246 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
247 if (!runtime->HasCalleeSaveMethod(type)) {
248 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type);
249 }
250 }
251 return runtime;
252 }
253
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800254 static void ResolveExceptionsForMethod(MethodHelper* mh,
255 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
256 const DexFile::CodeItem* code_item = mh->GetCodeItem();
257 if (code_item == NULL) {
258 return; // native or abstract method
259 }
260 if (code_item->tries_size_ == 0) {
261 return; // nothing to process
262 }
263 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
264 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
265 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
266 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
267 bool has_catch_all = false;
268 if (encoded_catch_handler_size <= 0) {
269 encoded_catch_handler_size = -encoded_catch_handler_size;
270 has_catch_all = true;
271 }
272 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
273 uint16_t encoded_catch_handler_handlers_type_idx =
274 DecodeUnsignedLeb128(&encoded_catch_handler_list);
275 // Add to set of types to resolve if not already in the dex cache resolved types
276 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
277 exceptions_to_resolve.insert(
278 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
279 &mh->GetDexFile()));
280 }
281 // ignore address associated with catch handler
282 DecodeUnsignedLeb128(&encoded_catch_handler_list);
283 }
284 if (has_catch_all) {
285 // ignore catch all address
286 DecodeUnsignedLeb128(&encoded_catch_handler_list);
287 }
288 }
289 }
290 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
291 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
292 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
293 MethodHelper mh;
294 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
295 Method* m = c->GetVirtualMethod(i);
296 mh.ChangeMethod(m);
297 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
298 }
299 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
300 Method* m = c->GetDirectMethod(i);
301 mh.ChangeMethod(m);
302 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
303 }
304 return true;
305 }
306 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800307 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
308 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500309 return true;
310 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800311 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800312 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500313 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500314
Brian Carlstromae826982011-11-09 01:33:42 -0800315 // Appends to dex_files any elements of class_path that it doesn't already
316 // contain. This will open those dex files as necessary.
317 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
318 std::vector<std::string> parsed;
319 Split(class_path, ':', parsed);
320 for (size_t i = 0; i < parsed.size(); ++i) {
321 if (DexFilesContains(dex_files, parsed[i])) {
322 continue;
323 }
324 const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix());
325 if (dex_file == NULL) {
326 LOG(WARNING) << "Failed to open dex file " << parsed[i];
327 } else {
328 dex_files.push_back(dex_file);
329 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500330 }
331 }
Brian Carlstromae826982011-11-09 01:33:42 -0800332
333 // Returns true if dex_files has a dex with the named location.
334 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
335 for (size_t i = 0; i < dex_files.size(); ++i) {
336 if (dex_files[i]->GetLocation() == location) {
337 return true;
338 }
339 }
340 return false;
341 }
342
Brian Carlstromae826982011-11-09 01:33:42 -0800343 static const InstructionSet instruction_set_ = kThumb2;
344
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800345 Runtime* runtime_;
346 uint64_t start_ns_;
347
Brian Carlstromae826982011-11-09 01:33:42 -0800348 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
349};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500350
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800351bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800352 char* end;
353 int result = strtol(in, &end, 10);
354 if (in == end || *end != '\0') {
355 return false;
356 }
357 *out = result;
358 return true;
359}
360
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700361int dex2oat(int argc, char** argv) {
362 // Skip over argv[0].
363 argv++;
364 argc--;
365
366 if (argc == 0) {
367 fprintf(stderr, "no arguments specified\n");
368 usage();
369 }
370
371 std::vector<const char*> dex_filenames;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800372 int zip_fd = -1;
373 std::string zip_name;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374 std::string oat_filename;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800375 int oat_fd = -1;
376 std::string oat_name;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700377 const char* image_filename = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800378 const char* image_classes_filename = NULL;
Brian Carlstromb0011262011-12-09 12:17:24 -0800379 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700380 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700381 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700382 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700383
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700384 for (int i = 0; i < argc; i++) {
385 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800386 bool log_options = false;
387 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700388 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
389 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700390 if (option.starts_with("--dex-file=")) {
391 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800392 } else if (option.starts_with("--zip-fd=")) {
393 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800394 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstrom866c8622012-01-06 16:35:13 -0800395 fprintf(stderr, "could not parse --zip-fd argument '%s' as an integer\n", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800396 usage();
397 }
398 } else if (option.starts_with("--zip-name=")) {
399 zip_name = option.substr(strlen("--zip-name=")).data();
400 } else if (option.starts_with("--oat-file=")) {
401 oat_filename = option.substr(strlen("--oat-file=")).data();
402 } else if (option.starts_with("--oat-fd=")) {
403 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800404 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstrom866c8622012-01-06 16:35:13 -0800405 fprintf(stderr, "could not parse --oat-fd argument '%s' as an integer\n", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800406 usage();
407 }
408 } else if (option.starts_with("--oat-name=")) {
409 oat_name = option.substr(strlen("--oat-name=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700410 } else if (option.starts_with("--image=")) {
411 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800412 } else if (option.starts_with("--image-classes=")) {
413 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700414 } else if (option.starts_with("--base=")) {
415 const char* image_base_str = option.substr(strlen("--base=")).data();
416 char* end;
417 image_base = strtoul(image_base_str, &end, 16);
418 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700419 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700420 usage();
421 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800423 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700424 } else if (option.starts_with("--host-prefix=")) {
425 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700426 } else if (option == "--runtime-arg") {
427 if (++i >= argc) {
428 fprintf(stderr, "Missing required argument for --runtime-arg\n");
429 usage();
430 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800431 if (log_options) {
432 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
433 }
jeffhao5d840402011-10-24 17:09:45 -0700434 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700435 } else {
436 fprintf(stderr, "unknown argument %s\n", option.data());
437 usage();
438 }
439 }
440
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800441 if (oat_filename.empty() && oat_fd == -1) {
442 fprintf(stderr, "Output must be supplied with either --oat-file or --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_fd != -1) {
452 fprintf(stderr, "--oat-file should not be used with --oat-fd\n");
453 return EXIT_FAILURE;
454 }
455
456 if (!oat_filename.empty() && !oat_name.empty()) {
457 fprintf(stderr, "--oat-file should not be used with --oat-name\n");
458 return EXIT_FAILURE;
459 }
460
461 if (oat_fd != -1 && oat_name.empty()) {
462 fprintf(stderr, "--oat-name should be supplied with --oat-fd\n");
463 return EXIT_FAILURE;
464 }
465
466 if (oat_fd != -1 && image_filename != NULL) {
467 fprintf(stderr, "--oat-fd should not be used with --image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700468 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700469 }
470
Brian Carlstromb0011262011-12-09 12:17:24 -0800471 if (host_prefix.empty()) {
472 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
473 if (android_product_out != NULL) {
474 host_prefix = android_product_out;
475 }
476 }
477
Brian Carlstromae826982011-11-09 01:33:42 -0800478 bool image = (image_filename != NULL);
Brian Carlstromb0011262011-12-09 12:17:24 -0800479 if (!image && boot_image_filename.empty()) {
480 boot_image_filename += host_prefix;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800481 boot_image_filename += "/system/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800482 }
483 std::string boot_image_option;
484 if (boot_image_filename != NULL) {
485 boot_image_option += "-Ximage:";
486 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700487 }
488
Brian Carlstromae826982011-11-09 01:33:42 -0800489 if (image_classes_filename != NULL && !image) {
490 fprintf(stderr, "--image-classes should only be used with --image\n");
491 return EXIT_FAILURE;
492 }
493
494 if (image_classes_filename != NULL && !boot_image_option.empty()) {
495 fprintf(stderr, "--image-classes should not be used with --boot-image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700496 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700497 }
498
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800499 if (dex_filenames.empty() && zip_fd == -1) {
500 fprintf(stderr, "Input must be supplied with either --dex-file or --zip-fd\n");
501 return EXIT_FAILURE;
502 }
503
504 if (!dex_filenames.empty() && zip_fd != -1) {
505 fprintf(stderr, "--dex-file should not be used with --zip-fd\n");
506 return EXIT_FAILURE;
507 }
508
509 if (!dex_filenames.empty() && !zip_name.empty()) {
510 fprintf(stderr, "--dex-file should not be used with --zip-name\n");
511 return EXIT_FAILURE;
512 }
513
514 if (zip_fd != -1 && zip_name.empty()) {
515 fprintf(stderr, "--zip-name should be supplied with --zip-fd\n");
516 return EXIT_FAILURE;
517 }
518
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700519 if (boot_image_option.empty()) {
520 if (image_base == 0) {
521 fprintf(stderr, "non-zero --base not specified\n");
522 return EXIT_FAILURE;
523 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700524 }
525
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800526 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800527 UniquePtr<File> oat_file;
528 if (!oat_filename.empty()) {
529 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
530 oat_name = oat_filename;
531 } else {
532 oat_file.reset(OS::FileFromFd(oat_name.c_str(), oat_fd));
533 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800534 if (oat_file.get() == NULL) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800535 PLOG(ERROR) << "Unable to create oat file: " << oat_name;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800536 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700537 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800538
539 LOG(INFO) << "dex2oat: " << oat_name;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700540
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700541 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700542 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700543 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700544 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700545 boot_class_path_string += "-Xbootclasspath:";
546 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
547 boot_class_path_string += dex_filenames[i];
548 boot_class_path_string += ":";
549 }
550 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
551 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700552 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700553 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
554 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700555 if (!host_prefix.empty()) {
556 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
557 }
jeffhao5d840402011-10-24 17:09:45 -0700558 for (size_t i = 0; i < runtime_args.size(); i++) {
559 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
560 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700561
Brian Carlstromae826982011-11-09 01:33:42 -0800562 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700563
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800564 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800565 UniquePtr<const std::set<std::string> > image_classes(NULL);
566 if (image_classes_filename != NULL) {
567 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
568 if (image_classes.get() == NULL) {
569 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
570 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700571 }
572 }
573
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800574 std::vector<const DexFile*> dex_files;
575 if (boot_image_option.empty()) {
576 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
577 } else {
578 if (dex_filenames.empty()) {
579 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
580 if (zip_archive.get() == NULL) {
581 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_name;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800582 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800583 }
584 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_name);
585 if (dex_file == NULL) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800586 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_name;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800587 return EXIT_FAILURE;
588 }
589 dex_files.push_back(dex_file);
590 } else {
591 DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix);
592 }
593 }
594
Brian Carlstromae826982011-11-09 01:33:42 -0800595 if (!dex2oat->CreateOatFile(boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800596 dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800597 oat_file.get(),
598 image,
599 image_classes.get())) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800600 LOG(ERROR) << "Failed to create oat file: " << oat_name;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700601 return EXIT_FAILURE;
602 }
603
Brian Carlstromae826982011-11-09 01:33:42 -0800604 if (!image) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800605 LOG(INFO) << "Oat file written successfully: " << oat_name;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700606 return EXIT_SUCCESS;
607 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700608
Brian Carlstromae826982011-11-09 01:33:42 -0800609 if (!dex2oat->CreateImageFile(image_filename,
610 image_base,
611 image_classes.get(),
612 oat_filename,
613 host_prefix)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700614 return EXIT_FAILURE;
615 }
616
Brian Carlstromae826982011-11-09 01:33:42 -0800617 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800618 LOG(INFO) << "Oat file written successfully: " << oat_filename;
619 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700620 return EXIT_SUCCESS;
621}
622
623} // namespace art
624
625int main(int argc, char** argv) {
626 return art::dex2oat(argc, argv);
627}