blob: 24058ecca9995ec6e89003411d7b2208199cbf13 [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"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080022#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070023
24namespace art {
25
26static void usage() {
27 fprintf(stderr,
28 "Usage: dex2oat [options]...\n"
29 "\n");
30 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080031 " --dex-file=<dex-file>: specifies a .dex file to compile.\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070032 " Example: --dex-file=/system/framework/core.jar\n"
33 "\n");
34 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080035 " --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 Carlstromae826982011-11-09 01:33:42 -080052 "\n");
53 fprintf(stderr,
54 " --image=<file.art>: specifies the output image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080055 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080058 " --image-classes=<classname-file>: specifies classes to include in an image.\n"
59 " Example: --image=frameworks/base/preloaded-classes\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070060 "\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 Carlstrome24fa612011-09-29 00:53:55 -070066 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080067 " Example: --boot-image=/system/framework/boot.art\n"
68 " Default: <host-prefix>/system/framework/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070069 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070070 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070071 " --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 Carlstromb0011262011-12-09 12:17:24 -080074 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070075 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070076 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070077 " --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 Carlstrom27ec9612011-09-19 20:20:38 -070081 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070082 exit(EXIT_FAILURE);
83}
84
Brian Carlstromae826982011-11-09 01:33:42 -080085class 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 Rogers6f1dfe42011-12-08 17:28:34 -0800108 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800109 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 Hughes95572412011-12-13 18:14:20 -0800116 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800117 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800118 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 Rogers6f1dfe42011-12-08 17:28:34 -0800125 // 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 Carlstromae826982011-11-09 01:33:42 -0800154 // 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 Rogers6f1dfe42011-12-08 17:28:34 -0800158 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800159 CHECK_NE(image_classes->size(), 0U);
160 return image_classes.release();
161 }
162
163 bool CreateOatFile(const std::string& boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800164 const std::vector<const DexFile*>& dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800165 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 Carlstromae826982011-11-09 01:33:42 -0800175 if (!boot_image_option.empty()) {
176 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800177 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 Carlstromae826982011-11-09 01:33:42 -0800183 }
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()) {
231 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlysmLookupStub(instruction_set_));
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 Rogers6f1dfe42011-12-08 17:28:34 -0800251 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 Carlstromae826982011-11-09 01:33:42 -0800304 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
305 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500306 return true;
307 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800308 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800309 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500310 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500311
Brian Carlstromae826982011-11-09 01:33:42 -0800312 // 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 Wilson254db0f2011-11-16 16:44:11 -0500327 }
328 }
Brian Carlstromae826982011-11-09 01:33:42 -0800329
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 Wilson254db0f2011-11-16 16:44:11 -0500345
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800346bool 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700356int 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 Carlstroma6cc8932012-01-04 14:44:07 -0800367 int zip_fd = -1;
368 std::string zip_name;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700369 std::string oat_filename;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800370 int oat_fd = -1;
371 std::string oat_name;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700372 const char* image_filename = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800373 const char* image_classes_filename = NULL;
Brian Carlstromb0011262011-12-09 12:17:24 -0800374 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700375 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700376 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700377 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700378
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700379 for (int i = 0; i < argc; i++) {
380 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800381 bool log_options = false;
382 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700383 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
384 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700385 if (option.starts_with("--dex-file=")) {
386 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800387 } 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 Carlstrom866c8622012-01-06 16:35:13 -0800390 fprintf(stderr, "could not parse --zip-fd argument '%s' as an integer\n", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800391 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 Carlstrom866c8622012-01-06 16:35:13 -0800400 fprintf(stderr, "could not parse --oat-fd argument '%s' as an integer\n", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800401 usage();
402 }
403 } else if (option.starts_with("--oat-name=")) {
404 oat_name = option.substr(strlen("--oat-name=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700405 } else if (option.starts_with("--image=")) {
406 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800407 } else if (option.starts_with("--image-classes=")) {
408 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700409 } 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 Carlstrom27ec9612011-09-19 20:20:38 -0700414 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700415 usage();
416 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800418 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700419 } else if (option.starts_with("--host-prefix=")) {
420 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700421 } else if (option == "--runtime-arg") {
422 if (++i >= argc) {
423 fprintf(stderr, "Missing required argument for --runtime-arg\n");
424 usage();
425 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800426 if (log_options) {
427 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
428 }
jeffhao5d840402011-10-24 17:09:45 -0700429 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700430 } else {
431 fprintf(stderr, "unknown argument %s\n", option.data());
432 usage();
433 }
434 }
435
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800436 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 Hughes362f9bc2011-10-17 18:56:41 -0700463 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700464 }
465
Brian Carlstromb0011262011-12-09 12:17:24 -0800466 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 Carlstromae826982011-11-09 01:33:42 -0800473 bool image = (image_filename != NULL);
Brian Carlstromb0011262011-12-09 12:17:24 -0800474 if (!image && boot_image_filename.empty()) {
475 boot_image_filename += host_prefix;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800476 boot_image_filename += "/system/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800477 }
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 Carlstrom69b15fb2011-09-03 12:25:21 -0700482 }
483
Brian Carlstromae826982011-11-09 01:33:42 -0800484 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 Hughes362f9bc2011-10-17 18:56:41 -0700491 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700492 }
493
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800494 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700514 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700519 }
520
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800521 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800522 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 Carlstrom6ef827a2011-12-11 14:57:47 -0800529 if (oat_file.get() == NULL) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800530 PLOG(ERROR) << "Unable to create oat file: " << oat_name;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800531 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700532 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800533
534 LOG(INFO) << "dex2oat: " << oat_name;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700535
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700536 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700537 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700538 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700539 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700540 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700547 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700548 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
549 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700550 if (!host_prefix.empty()) {
551 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
552 }
jeffhao5d840402011-10-24 17:09:45 -0700553 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700556
Brian Carlstromae826982011-11-09 01:33:42 -0800557 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700558
Brian Carlstromae826982011-11-09 01:33:42 -0800559 // 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700566 }
567 }
568
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800569 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 Carlstrom2e3d1b22012-01-09 18:01:56 -0800577 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800578 }
579 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_name);
580 if (dex_file == NULL) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800581 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_name;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800582 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 Carlstromae826982011-11-09 01:33:42 -0800590 if (!dex2oat->CreateOatFile(boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800591 dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800592 oat_file.get(),
593 image,
594 image_classes.get())) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800595 LOG(ERROR) << "Failed to create oat file: " << oat_name;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700596 return EXIT_FAILURE;
597 }
598
Brian Carlstromae826982011-11-09 01:33:42 -0800599 if (!image) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800600 LOG(INFO) << "Oat file written successfully: " << oat_name;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700601 return EXIT_SUCCESS;
602 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700603
Brian Carlstromae826982011-11-09 01:33:42 -0800604 if (!dex2oat->CreateImageFile(image_filename,
605 image_base,
606 image_classes.get(),
607 oat_filename,
608 host_prefix)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700609 return EXIT_FAILURE;
610 }
611
Brian Carlstromae826982011-11-09 01:33:42 -0800612 // We wrote the oat file successfully, and want to keep it.
Brian Carlstromae826982011-11-09 01:33:42 -0800613 LOG(INFO) << "Oat file written successfully " << oat_filename;
Elliott Hughes234da572011-11-03 22:13:06 -0700614 LOG(INFO) << "Image written successfully " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700615 return EXIT_SUCCESS;
616}
617
618} // namespace art
619
620int main(int argc, char** argv) {
621 return art::dex2oat(argc, argv);
622}