blob: 25c1d8272228abcfde772dca70ebe01deac18a13 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstromae826982011-11-09 01:33:42 -080020#include <iostream>
21#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070022#include <string>
23#include <vector>
24
25#include "class_linker.h"
26#include "class_loader.h"
27#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070028#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070029#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080030#include "leb128.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070033#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070034#include "runtime.h"
35#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080036#include "timing_logger.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080037#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070038
39namespace art {
40
41static void usage() {
42 fprintf(stderr,
43 "Usage: dex2oat [options]...\n"
44 "\n");
45 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080046 " --dex-file=<dex-file>: specifies a .dex file to compile.\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070047 " Example: --dex-file=/system/framework/core.jar\n"
48 "\n");
49 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080050 " --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file\n"
51 " containing a classes.dex file to compile.\n"
52 " Example: --zip-fd=5\n"
53 "\n");
54 fprintf(stderr,
55 " --zip-name=<zip-name>: specifies a symbolic name for the file corresponding\n"
56 " to the file descriptor specified by --zip-fd.\n"
57 " Example: --zip-name=/system/app/Calculator.apk\n"
58 "\n");
59 fprintf(stderr,
60 " --oat-file=<file.oat>: specifies the required oat filename.\n"
61 " Example: --oat-file=/system/framework/boot.oat\n"
62 "\n");
63 fprintf(stderr,
64 " --oat-name=<oat-name>: specifies a symbolic name for the file corresponding\n"
65 " to the file descriptor specified by --oat-fd.\n"
66 " Example: --oat-name=/data/art-cache/system@app@Calculator.apk.oat\n"
Brian Carlstromae826982011-11-09 01:33:42 -080067 "\n");
68 fprintf(stderr,
69 " --image=<file.art>: specifies the output image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080070 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080073 " --image-classes=<classname-file>: specifies classes to include in an image.\n"
74 " Example: --image=frameworks/base/preloaded-classes\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070075 "\n");
76 fprintf(stderr,
77 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
78 " Example: --base=0x50000000\n"
79 "\n");
80 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080082 " Example: --boot-image=/system/framework/boot.art\n"
83 " Default: <host-prefix>/system/framework/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070084 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070085 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070086 " --host-prefix may be used to translate host paths to target paths during\n"
87 " cross compilation.\n"
88 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromb0011262011-12-09 12:17:24 -080089 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070090 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070091 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070092 " --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
93 " such as initial heap size, maximum heap size, and verbose output.\n"
94 " Use a separate --runtime-arg switch for each argument.\n"
95 " Example: --runtime-arg -Xms256m\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070096 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070097 exit(EXIT_FAILURE);
98}
99
Brian Carlstromae826982011-11-09 01:33:42 -0800100class Dex2Oat {
101 public:
102
103 static Dex2Oat* Create(Runtime::Options& options) {
104 UniquePtr<Runtime> runtime(CreateRuntime(options));
105 if (runtime.get() == NULL) {
106 return NULL;
107 }
108 return new Dex2Oat(runtime.release());
109 }
110
111 ~Dex2Oat() {
112 delete runtime_;
Elliott Hughes0d0ba692012-02-03 17:28:52 -0800113 const size_t thread_count = static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN));
114 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800115 }
116
117 // Make a list of descriptors for classes to include in the image
118 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
119 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
120 if (image_classes_file.get() == NULL) {
121 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
122 return NULL;
123 }
124
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800125 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800126 ClassLinker* class_linker = runtime_->GetClassLinker();
127 while (image_classes_file->good()) {
128 std::string dot;
129 std::getline(*image_classes_file.get(), dot);
130 if (StringPiece(dot).starts_with("#") || dot.empty()) {
131 continue;
132 }
Elliott Hughes95572412011-12-13 18:14:20 -0800133 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800134 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800135 if (klass.get() == NULL) {
136 LOG(WARNING) << "Failed to find class " << descriptor;
137 Thread::Current()->ClearException();
138 }
139 }
140 image_classes_file->close();
141
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800142 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
143 // exceptions are resolved by the verifier when there is a catch block in an interested method.
144 // Do this here so that exception classes appear to have been specified image classes.
145 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
146 do {
147 unresolved_exception_types.clear();
148 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
149 &unresolved_exception_types);
150 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
151 for (It it = unresolved_exception_types.begin(),
152 end = unresolved_exception_types.end();
153 it != end; ++it) {
154 uint16_t exception_type_idx = it->first;
155 const DexFile* dex_file = it->second;
156 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
157 ClassLoader* class_loader = NULL;
158 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
159 class_loader));
160 if (klass.get() == NULL) {
161 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
162 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
163 LOG(FATAL) << "Failed to resolve class " << descriptor;
164 }
165 DCHECK(klass->IsThrowableClass());
166 }
167 // Resolving exceptions may load classes that reference more exceptions, iterate until no
168 // more are found
169 } while (!unresolved_exception_types.empty());
170
Brian Carlstromae826982011-11-09 01:33:42 -0800171 // We walk the roots looking for classes so that we'll pick up the
172 // above classes plus any classes them depend on such super
173 // classes, interfaces, and the required ClassLinker roots.
174 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800175 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800176 CHECK_NE(image_classes->size(), 0U);
177 return image_classes.release();
178 }
179
180 bool CreateOatFile(const std::string& boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800181 const std::vector<const DexFile*>& dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800182 File* oat_file,
183 bool image,
184 const std::set<std::string>* image_classes) {
185 // SirtRef and ClassLoader creation needs to come after Runtime::Create
186 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
187 if (class_loader.get() == NULL) {
188 LOG(ERROR) << "Failed to create SirtRef for class loader";
189 return false;
190 }
191
Brian Carlstromae826982011-11-09 01:33:42 -0800192 if (!boot_image_option.empty()) {
193 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800194 std::vector<const DexFile*> class_path_files(dex_files);
195 OpenClassPathFiles(runtime_->GetClassPath(), class_path_files);
196 for (size_t i = 0; i < class_path_files.size(); i++) {
197 class_linker->RegisterDexFile(*class_path_files[i]);
198 }
199 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800200 }
201
202 Compiler compiler(instruction_set_, image, image_classes);
203 compiler.CompileAll(class_loader->get(), dex_files);
204
jeffhao10037c82012-01-23 15:06:23 -0800205 if (!OatWriter::Create(oat_file, class_loader->get(), dex_files, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800206 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
207 return false;
208 }
209 return true;
210 }
211
212 bool CreateImageFile(const char* image_filename,
213 uintptr_t image_base,
214 const std::set<std::string>* image_classes,
215 const std::string& oat_filename,
216 const std::string& host_prefix) {
217 // If we have an existing boot image, position new space after its oat file
218 if (Heap::GetSpaces().size() > 1) {
Ian Rogers30fab402012-01-23 15:43:46 -0800219 ImageSpace* last_image_space = NULL;
220 const std::vector<Space*>& spaces = Heap::GetSpaces();
221 for (size_t i=0; i < spaces.size(); i++) {
222 if (spaces[i]->IsImageSpace()) {
223 last_image_space = spaces[i]->AsImageSpace();
224 }
225 }
Brian Carlstromae826982011-11-09 01:33:42 -0800226 CHECK(last_image_space != NULL);
227 CHECK(last_image_space->IsImageSpace());
228 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
Ian Rogers30fab402012-01-23 15:43:46 -0800229 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatEnd();
Brian Carlstromae826982011-11-09 01:33:42 -0800230 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
231 }
232
233 ImageWriter image_writer(image_classes);
234 if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) {
235 LOG(ERROR) << "Failed to create image file " << image_filename;
236 return false;
237 }
238 return true;
239 }
240
241 private:
242
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800243 explicit Dex2Oat(Runtime* runtime) : runtime_(runtime), start_ns_(NanoTime()) {
244 }
Brian Carlstromae826982011-11-09 01:33:42 -0800245
246 static Runtime* CreateRuntime(Runtime::Options& options) {
247 Runtime* runtime = Runtime::Create(options, false);
248 if (runtime == NULL) {
249 LOG(ERROR) << "Failed to create runtime";
250 return NULL;
251 }
252
253 // if we loaded an existing image, we will reuse values from the image roots.
254 if (!runtime->HasJniDlsymLookupStub()) {
Elliott Hughes8add92d2012-01-18 18:18:43 -0800255 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set_));
Brian Carlstromae826982011-11-09 01:33:42 -0800256 }
257 if (!runtime->HasAbstractMethodErrorStubArray()) {
258 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_));
259 }
260 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
261 Runtime::TrampolineType type = Runtime::TrampolineType(i);
262 if (!runtime->HasResolutionStubArray(type)) {
263 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type);
264 }
265 }
266 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
267 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
268 if (!runtime->HasCalleeSaveMethod(type)) {
269 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type);
270 }
271 }
272 return runtime;
273 }
274
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800275 static void ResolveExceptionsForMethod(MethodHelper* mh,
276 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
277 const DexFile::CodeItem* code_item = mh->GetCodeItem();
278 if (code_item == NULL) {
279 return; // native or abstract method
280 }
281 if (code_item->tries_size_ == 0) {
282 return; // nothing to process
283 }
284 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
285 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
286 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
287 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
288 bool has_catch_all = false;
289 if (encoded_catch_handler_size <= 0) {
290 encoded_catch_handler_size = -encoded_catch_handler_size;
291 has_catch_all = true;
292 }
293 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
294 uint16_t encoded_catch_handler_handlers_type_idx =
295 DecodeUnsignedLeb128(&encoded_catch_handler_list);
296 // Add to set of types to resolve if not already in the dex cache resolved types
297 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
298 exceptions_to_resolve.insert(
299 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
300 &mh->GetDexFile()));
301 }
302 // ignore address associated with catch handler
303 DecodeUnsignedLeb128(&encoded_catch_handler_list);
304 }
305 if (has_catch_all) {
306 // ignore catch all address
307 DecodeUnsignedLeb128(&encoded_catch_handler_list);
308 }
309 }
310 }
311 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
312 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
313 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
314 MethodHelper mh;
315 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
316 Method* m = c->GetVirtualMethod(i);
317 mh.ChangeMethod(m);
318 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
319 }
320 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
321 Method* m = c->GetDirectMethod(i);
322 mh.ChangeMethod(m);
323 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
324 }
325 return true;
326 }
327 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800328 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
329 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500330 return true;
331 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800332 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800333 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500334 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500335
Brian Carlstromae826982011-11-09 01:33:42 -0800336 // Appends to dex_files any elements of class_path that it doesn't already
337 // contain. This will open those dex files as necessary.
338 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
339 std::vector<std::string> parsed;
340 Split(class_path, ':', parsed);
341 for (size_t i = 0; i < parsed.size(); ++i) {
342 if (DexFilesContains(dex_files, parsed[i])) {
343 continue;
344 }
345 const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix());
346 if (dex_file == NULL) {
347 LOG(WARNING) << "Failed to open dex file " << parsed[i];
348 } else {
349 dex_files.push_back(dex_file);
350 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500351 }
352 }
Brian Carlstromae826982011-11-09 01:33:42 -0800353
354 // Returns true if dex_files has a dex with the named location.
355 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
356 for (size_t i = 0; i < dex_files.size(); ++i) {
357 if (dex_files[i]->GetLocation() == location) {
358 return true;
359 }
360 }
361 return false;
362 }
363
Brian Carlstromae826982011-11-09 01:33:42 -0800364 static const InstructionSet instruction_set_ = kThumb2;
365
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800366 Runtime* runtime_;
367 uint64_t start_ns_;
368
Brian Carlstromae826982011-11-09 01:33:42 -0800369 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
370};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500371
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800372bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800373 char* end;
374 int result = strtol(in, &end, 10);
375 if (in == end || *end != '\0') {
376 return false;
377 }
378 *out = result;
379 return true;
380}
381
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800382void OpenDexFiles(const std::vector<const char*>& dex_filenames,
383 std::vector<const DexFile*>& dex_files,
384 const std::string& strip_location_prefix) {
385 for (size_t i = 0; i < dex_filenames.size(); i++) {
386 const char* dex_filename = dex_filenames[i];
387 const DexFile* dex_file = DexFile::Open(dex_filename, strip_location_prefix);
388 if (dex_file == NULL) {
389 fprintf(stderr, "could not open .dex from file %s\n", dex_filename);
390 exit(EXIT_FAILURE);
391 }
392 dex_files.push_back(dex_file);
393 }
394}
395
396
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700397int dex2oat(int argc, char** argv) {
398 // Skip over argv[0].
399 argv++;
400 argc--;
401
402 if (argc == 0) {
403 fprintf(stderr, "no arguments specified\n");
404 usage();
405 }
406
407 std::vector<const char*> dex_filenames;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800408 int zip_fd = -1;
409 std::string zip_name;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700410 std::string oat_filename;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800411 int oat_fd = -1;
412 std::string oat_name;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700413 const char* image_filename = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800414 const char* image_classes_filename = NULL;
Brian Carlstromb0011262011-12-09 12:17:24 -0800415 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700416 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700417 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700418 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700419
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700420 for (int i = 0; i < argc; i++) {
421 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800422 bool log_options = false;
423 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700424 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
425 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700426 if (option.starts_with("--dex-file=")) {
427 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800428 } else if (option.starts_with("--zip-fd=")) {
429 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800430 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstrom866c8622012-01-06 16:35:13 -0800431 fprintf(stderr, "could not parse --zip-fd argument '%s' as an integer\n", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800432 usage();
433 }
434 } else if (option.starts_with("--zip-name=")) {
435 zip_name = option.substr(strlen("--zip-name=")).data();
436 } else if (option.starts_with("--oat-file=")) {
437 oat_filename = option.substr(strlen("--oat-file=")).data();
438 } else if (option.starts_with("--oat-fd=")) {
439 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800440 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstrom866c8622012-01-06 16:35:13 -0800441 fprintf(stderr, "could not parse --oat-fd argument '%s' as an integer\n", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800442 usage();
443 }
444 } else if (option.starts_with("--oat-name=")) {
445 oat_name = option.substr(strlen("--oat-name=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700446 } else if (option.starts_with("--image=")) {
447 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800448 } else if (option.starts_with("--image-classes=")) {
449 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700450 } else if (option.starts_with("--base=")) {
451 const char* image_base_str = option.substr(strlen("--base=")).data();
452 char* end;
453 image_base = strtoul(image_base_str, &end, 16);
454 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700455 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700456 usage();
457 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700458 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800459 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700460 } else if (option.starts_with("--host-prefix=")) {
461 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700462 } else if (option == "--runtime-arg") {
463 if (++i >= argc) {
464 fprintf(stderr, "Missing required argument for --runtime-arg\n");
465 usage();
466 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800467 if (log_options) {
468 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
469 }
jeffhao5d840402011-10-24 17:09:45 -0700470 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700471 } else {
472 fprintf(stderr, "unknown argument %s\n", option.data());
473 usage();
474 }
475 }
476
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800477 if (oat_filename.empty() && oat_fd == -1) {
478 fprintf(stderr, "Output must be supplied with either --oat-file or --oat-fd\n");
479 return EXIT_FAILURE;
480 }
481
482 if (!oat_filename.empty() && oat_fd != -1) {
483 fprintf(stderr, "--oat-file should not be used with --oat-fd\n");
484 return EXIT_FAILURE;
485 }
486
487 if (!oat_filename.empty() && oat_fd != -1) {
488 fprintf(stderr, "--oat-file should not be used with --oat-fd\n");
489 return EXIT_FAILURE;
490 }
491
492 if (!oat_filename.empty() && !oat_name.empty()) {
493 fprintf(stderr, "--oat-file should not be used with --oat-name\n");
494 return EXIT_FAILURE;
495 }
496
497 if (oat_fd != -1 && oat_name.empty()) {
498 fprintf(stderr, "--oat-name should be supplied with --oat-fd\n");
499 return EXIT_FAILURE;
500 }
501
502 if (oat_fd != -1 && image_filename != NULL) {
503 fprintf(stderr, "--oat-fd should not be used with --image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700504 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505 }
506
Brian Carlstromb0011262011-12-09 12:17:24 -0800507 if (host_prefix.empty()) {
508 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
509 if (android_product_out != NULL) {
510 host_prefix = android_product_out;
511 }
512 }
513
Brian Carlstromae826982011-11-09 01:33:42 -0800514 bool image = (image_filename != NULL);
Brian Carlstromb0011262011-12-09 12:17:24 -0800515 if (!image && boot_image_filename.empty()) {
516 boot_image_filename += host_prefix;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800517 boot_image_filename += "/system/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800518 }
519 std::string boot_image_option;
520 if (boot_image_filename != NULL) {
521 boot_image_option += "-Ximage:";
522 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700523 }
524
Brian Carlstromae826982011-11-09 01:33:42 -0800525 if (image_classes_filename != NULL && !image) {
526 fprintf(stderr, "--image-classes should only be used with --image\n");
527 return EXIT_FAILURE;
528 }
529
530 if (image_classes_filename != NULL && !boot_image_option.empty()) {
531 fprintf(stderr, "--image-classes should not be used with --boot-image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700532 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700533 }
534
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800535 if (dex_filenames.empty() && zip_fd == -1) {
536 fprintf(stderr, "Input must be supplied with either --dex-file or --zip-fd\n");
537 return EXIT_FAILURE;
538 }
539
540 if (!dex_filenames.empty() && zip_fd != -1) {
541 fprintf(stderr, "--dex-file should not be used with --zip-fd\n");
542 return EXIT_FAILURE;
543 }
544
545 if (!dex_filenames.empty() && !zip_name.empty()) {
546 fprintf(stderr, "--dex-file should not be used with --zip-name\n");
547 return EXIT_FAILURE;
548 }
549
550 if (zip_fd != -1 && zip_name.empty()) {
551 fprintf(stderr, "--zip-name should be supplied with --zip-fd\n");
552 return EXIT_FAILURE;
553 }
554
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700555 if (boot_image_option.empty()) {
556 if (image_base == 0) {
557 fprintf(stderr, "non-zero --base not specified\n");
558 return EXIT_FAILURE;
559 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700560 }
561
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800562 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800563 UniquePtr<File> oat_file;
564 if (!oat_filename.empty()) {
565 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
566 oat_name = oat_filename;
567 } else {
568 oat_file.reset(OS::FileFromFd(oat_name.c_str(), oat_fd));
569 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800570 if (oat_file.get() == NULL) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800571 PLOG(ERROR) << "Unable to create oat file: " << oat_name;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800572 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700573 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800574
575 LOG(INFO) << "dex2oat: " << oat_name;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700576
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700577 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700578 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700579 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700580 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700581 boot_class_path_string += "-Xbootclasspath:";
582 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
583 boot_class_path_string += dex_filenames[i];
584 boot_class_path_string += ":";
585 }
586 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
587 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700588 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700589 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
590 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700591 if (!host_prefix.empty()) {
592 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
593 }
jeffhao5d840402011-10-24 17:09:45 -0700594 for (size_t i = 0; i < runtime_args.size(); i++) {
595 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
596 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700597
Brian Carlstromae826982011-11-09 01:33:42 -0800598 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700599
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800600 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800601 UniquePtr<const std::set<std::string> > image_classes(NULL);
602 if (image_classes_filename != NULL) {
603 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
604 if (image_classes.get() == NULL) {
605 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
606 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700607 }
608 }
609
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800610 std::vector<const DexFile*> dex_files;
611 if (boot_image_option.empty()) {
612 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
613 } else {
614 if (dex_filenames.empty()) {
615 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
616 if (zip_archive.get() == NULL) {
617 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_name;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800618 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800619 }
620 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_name);
621 if (dex_file == NULL) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800622 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_name;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800623 return EXIT_FAILURE;
624 }
625 dex_files.push_back(dex_file);
626 } else {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800627 OpenDexFiles(dex_filenames, dex_files, host_prefix);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800628 }
629 }
630
Brian Carlstromae826982011-11-09 01:33:42 -0800631 if (!dex2oat->CreateOatFile(boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800632 dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800633 oat_file.get(),
634 image,
635 image_classes.get())) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800636 LOG(ERROR) << "Failed to create oat file: " << oat_name;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700637 return EXIT_FAILURE;
638 }
639
Brian Carlstromae826982011-11-09 01:33:42 -0800640 if (!image) {
Elliott Hughesfd8cba42012-01-11 14:34:28 -0800641 LOG(INFO) << "Oat file written successfully: " << oat_name;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700642 return EXIT_SUCCESS;
643 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700644
Brian Carlstromae826982011-11-09 01:33:42 -0800645 if (!dex2oat->CreateImageFile(image_filename,
646 image_base,
647 image_classes.get(),
648 oat_filename,
649 host_prefix)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700650 return EXIT_FAILURE;
651 }
652
Brian Carlstromae826982011-11-09 01:33:42 -0800653 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800654 LOG(INFO) << "Oat file written successfully: " << oat_filename;
655 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700656 return EXIT_SUCCESS;
657}
658
659} // namespace art
660
661int main(int argc, char** argv) {
662 return art::dex2oat(argc, argv);
663}