blob: 40e27a9f2f88ab247a7fc60fe1e3b12bc4808da1 [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"
Brian Carlstroma004aa92012-02-08 18:05:09 -080035#include "stl_util.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070036#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080037#include "timing_logger.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080038#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070039
40namespace art {
41
42static void usage() {
43 fprintf(stderr,
44 "Usage: dex2oat [options]...\n"
45 "\n");
46 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080047 " --dex-file=<dex-file>: specifies a .dex file to compile.\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070048 " Example: --dex-file=/system/framework/core.jar\n"
49 "\n");
50 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080051 " --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file\n"
52 " containing a classes.dex file to compile.\n"
53 " Example: --zip-fd=5\n"
54 "\n");
55 fprintf(stderr,
Brian Carlstroma004aa92012-02-08 18:05:09 -080056 " --zip-location=<zip-location>: specifies a symbolic name for the file corresponding\n"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080057 " to the file descriptor specified by --zip-fd.\n"
Brian Carlstroma004aa92012-02-08 18:05:09 -080058 " Example: --zip-location=/system/app/Calculator.apk\n"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080059 "\n");
60 fprintf(stderr,
61 " --oat-file=<file.oat>: specifies the required oat filename.\n"
62 " Example: --oat-file=/system/framework/boot.oat\n"
63 "\n");
64 fprintf(stderr,
Brian Carlstroma004aa92012-02-08 18:05:09 -080065 " --oat-location=<oat-name>: specifies a symbolic name for the file corresponding\n"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080066 " to the file descriptor specified by --oat-fd.\n"
Brian Carlstroma004aa92012-02-08 18:05:09 -080067 " Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat\n"
Brian Carlstromae826982011-11-09 01:33:42 -080068 "\n");
69 fprintf(stderr,
70 " --image=<file.art>: specifies the output image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080071 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080074 " --image-classes=<classname-file>: specifies classes to include in an image.\n"
75 " Example: --image=frameworks/base/preloaded-classes\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070076 "\n");
77 fprintf(stderr,
78 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
79 " Example: --base=0x50000000\n"
80 "\n");
81 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070082 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080083 " Example: --boot-image=/system/framework/boot.art\n"
84 " Default: <host-prefix>/system/framework/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070085 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070086 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070087 " --host-prefix may be used to translate host paths to target paths during\n"
88 " cross compilation.\n"
89 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromb0011262011-12-09 12:17:24 -080090 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070091 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070092 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070093 " --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
94 " such as initial heap size, maximum heap size, and verbose output.\n"
95 " Use a separate --runtime-arg switch for each argument.\n"
96 " Example: --runtime-arg -Xms256m\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070097 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070098 exit(EXIT_FAILURE);
99}
100
Brian Carlstromae826982011-11-09 01:33:42 -0800101class Dex2Oat {
102 public:
103
Elliott Hughes5523ee02012-02-03 18:18:34 -0800104 static Dex2Oat* Create(Runtime::Options& options, size_t thread_count) {
Brian Carlstromae826982011-11-09 01:33:42 -0800105 UniquePtr<Runtime> runtime(CreateRuntime(options));
106 if (runtime.get() == NULL) {
107 return NULL;
108 }
Elliott Hughes5523ee02012-02-03 18:18:34 -0800109 return new Dex2Oat(runtime.release(), thread_count);
Brian Carlstromae826982011-11-09 01:33:42 -0800110 }
111
112 ~Dex2Oat() {
113 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800114 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);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800130 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800131 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);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800195 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800196 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
Elliott Hughes5523ee02012-02-03 18:18:34 -0800202 Compiler compiler(instruction_set_, image, thread_count_, image_classes);
Brian Carlstromae826982011-11-09 01:33:42 -0800203 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
Brian Carlstroma004aa92012-02-08 18:05:09 -0800212 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800213 uintptr_t image_base,
214 const std::set<std::string>* image_classes,
215 const std::string& oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800216 const std::string& oat_location) {
Brian Carlstromae826982011-11-09 01:33:42 -0800217 // 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);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800234 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800235 LOG(ERROR) << "Failed to create image file " << image_filename;
236 return false;
237 }
238 return true;
239 }
240
241 private:
242
Elliott Hughes5523ee02012-02-03 18:18:34 -0800243 explicit Dex2Oat(Runtime* runtime, size_t thread_count)
244 : runtime_(runtime), thread_count_(thread_count), start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800245 }
Brian Carlstromae826982011-11-09 01:33:42 -0800246
247 static Runtime* CreateRuntime(Runtime::Options& options) {
248 Runtime* runtime = Runtime::Create(options, false);
249 if (runtime == NULL) {
250 LOG(ERROR) << "Failed to create runtime";
251 return NULL;
252 }
253
254 // if we loaded an existing image, we will reuse values from the image roots.
255 if (!runtime->HasJniDlsymLookupStub()) {
Elliott Hughes8add92d2012-01-18 18:18:43 -0800256 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set_));
Brian Carlstromae826982011-11-09 01:33:42 -0800257 }
258 if (!runtime->HasAbstractMethodErrorStubArray()) {
259 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_));
260 }
261 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
262 Runtime::TrampolineType type = Runtime::TrampolineType(i);
263 if (!runtime->HasResolutionStubArray(type)) {
264 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type);
265 }
266 }
267 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
268 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
269 if (!runtime->HasCalleeSaveMethod(type)) {
270 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type);
271 }
272 }
273 return runtime;
274 }
275
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800276 static void ResolveExceptionsForMethod(MethodHelper* mh,
277 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
278 const DexFile::CodeItem* code_item = mh->GetCodeItem();
279 if (code_item == NULL) {
280 return; // native or abstract method
281 }
282 if (code_item->tries_size_ == 0) {
283 return; // nothing to process
284 }
285 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
286 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
287 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
288 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
289 bool has_catch_all = false;
290 if (encoded_catch_handler_size <= 0) {
291 encoded_catch_handler_size = -encoded_catch_handler_size;
292 has_catch_all = true;
293 }
294 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
295 uint16_t encoded_catch_handler_handlers_type_idx =
296 DecodeUnsignedLeb128(&encoded_catch_handler_list);
297 // Add to set of types to resolve if not already in the dex cache resolved types
298 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
299 exceptions_to_resolve.insert(
300 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
301 &mh->GetDexFile()));
302 }
303 // ignore address associated with catch handler
304 DecodeUnsignedLeb128(&encoded_catch_handler_list);
305 }
306 if (has_catch_all) {
307 // ignore catch all address
308 DecodeUnsignedLeb128(&encoded_catch_handler_list);
309 }
310 }
311 }
312 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
313 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
314 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
315 MethodHelper mh;
316 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
317 Method* m = c->GetVirtualMethod(i);
318 mh.ChangeMethod(m);
319 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
320 }
321 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
322 Method* m = c->GetDirectMethod(i);
323 mh.ChangeMethod(m);
324 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
325 }
326 return true;
327 }
328 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800329 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
330 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500331 return true;
332 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800333 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800334 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500335 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500336
Brian Carlstromae826982011-11-09 01:33:42 -0800337 // Appends to dex_files any elements of class_path that it doesn't already
338 // contain. This will open those dex files as necessary.
339 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
340 std::vector<std::string> parsed;
341 Split(class_path, ':', parsed);
342 for (size_t i = 0; i < parsed.size(); ++i) {
343 if (DexFilesContains(dex_files, parsed[i])) {
344 continue;
345 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800346 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800347 if (dex_file == NULL) {
348 LOG(WARNING) << "Failed to open dex file " << parsed[i];
349 } else {
350 dex_files.push_back(dex_file);
351 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500352 }
353 }
Brian Carlstromae826982011-11-09 01:33:42 -0800354
355 // Returns true if dex_files has a dex with the named location.
356 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
357 for (size_t i = 0; i < dex_files.size(); ++i) {
358 if (dex_files[i]->GetLocation() == location) {
359 return true;
360 }
361 }
362 return false;
363 }
364
Brian Carlstromae826982011-11-09 01:33:42 -0800365 static const InstructionSet instruction_set_ = kThumb2;
366
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800367 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800368 size_t thread_count_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800369 uint64_t start_ns_;
370
Brian Carlstromae826982011-11-09 01:33:42 -0800371 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
372};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500373
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800374bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800375 char* end;
376 int result = strtol(in, &end, 10);
377 if (in == end || *end != '\0') {
378 return false;
379 }
380 *out = result;
381 return true;
382}
383
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800384void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800385 const std::vector<const char*>& dex_locations,
386 std::vector<const DexFile*>& dex_files) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800387 for (size_t i = 0; i < dex_filenames.size(); i++) {
388 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800389 const char* dex_location = dex_locations[i];
390 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800391 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800392 LOG(WARNING) << "could not open .dex from file " << dex_filename;
393 } else {
394 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800395 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800396 }
397}
398
399
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700400int dex2oat(int argc, char** argv) {
401 // Skip over argv[0].
402 argv++;
403 argc--;
404
405 if (argc == 0) {
406 fprintf(stderr, "no arguments specified\n");
407 usage();
408 }
409
410 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800411 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800412 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800413 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700414 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800415 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800416 int oat_fd = -1;
Brian Carlstromae826982011-11-09 01:33:42 -0800417 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800418 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800419 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700420 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700421 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700422 std::vector<const char*> runtime_args;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800423 int thread_count = 2;
Brian Carlstrom16192862011-09-12 17:50:06 -0700424
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700425 for (int i = 0; i < argc; i++) {
426 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800427 bool log_options = false;
428 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700429 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
430 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700431 if (option.starts_with("--dex-file=")) {
432 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800433 } else if (option.starts_with("--dex-location=")) {
434 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800435 } else if (option.starts_with("--zip-fd=")) {
436 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800437 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstrom866c8622012-01-06 16:35:13 -0800438 fprintf(stderr, "could not parse --zip-fd argument '%s' as an integer\n", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800439 usage();
440 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800441 } else if (option.starts_with("--zip-location=")) {
442 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800443 } else if (option.starts_with("--oat-file=")) {
444 oat_filename = option.substr(strlen("--oat-file=")).data();
445 } else if (option.starts_with("--oat-fd=")) {
446 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800447 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstrom866c8622012-01-06 16:35:13 -0800448 fprintf(stderr, "could not parse --oat-fd argument '%s' as an integer\n", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800449 usage();
450 }
Elliott Hughes5523ee02012-02-03 18:18:34 -0800451 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800452 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800453 if (!ParseInt(thread_count_str, &thread_count)) {
454 fprintf(stderr, "could not parse -j argument '%s' as an integer\n", thread_count_str);
455 usage();
456 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800457 } else if (option.starts_with("--oat-location=")) {
458 oat_location = option.substr(strlen("--oat-location=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700459 } else if (option.starts_with("--image=")) {
460 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800461 } else if (option.starts_with("--image-classes=")) {
462 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700463 } else if (option.starts_with("--base=")) {
464 const char* image_base_str = option.substr(strlen("--base=")).data();
465 char* end;
466 image_base = strtoul(image_base_str, &end, 16);
467 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700468 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700469 usage();
470 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700471 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800472 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700473 } else if (option.starts_with("--host-prefix=")) {
474 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700475 } else if (option == "--runtime-arg") {
476 if (++i >= argc) {
477 fprintf(stderr, "Missing required argument for --runtime-arg\n");
478 usage();
479 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800480 if (log_options) {
481 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
482 }
jeffhao5d840402011-10-24 17:09:45 -0700483 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700484 } else {
485 fprintf(stderr, "unknown argument %s\n", option.data());
486 usage();
487 }
488 }
489
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800490 if (oat_filename.empty() && oat_fd == -1) {
491 fprintf(stderr, "Output must be supplied with either --oat-file or --oat-fd\n");
492 return EXIT_FAILURE;
493 }
494
495 if (!oat_filename.empty() && oat_fd != -1) {
496 fprintf(stderr, "--oat-file should not be used with --oat-fd\n");
497 return EXIT_FAILURE;
498 }
499
500 if (!oat_filename.empty() && oat_fd != -1) {
501 fprintf(stderr, "--oat-file should not be used with --oat-fd\n");
502 return EXIT_FAILURE;
503 }
504
Brian Carlstroma004aa92012-02-08 18:05:09 -0800505 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800506 fprintf(stderr, "--oat-fd should not be used with --image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700507 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700508 }
509
Brian Carlstromb0011262011-12-09 12:17:24 -0800510 if (host_prefix.empty()) {
511 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
512 if (android_product_out != NULL) {
513 host_prefix = android_product_out;
514 }
515 }
516
Brian Carlstroma004aa92012-02-08 18:05:09 -0800517 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800518 if (!image && boot_image_filename.empty()) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800519 if (host_prefix.empty()) {
520 boot_image_filename += GetAndroidRoot();
521 } else {
522 boot_image_filename += host_prefix;
523 boot_image_filename += "/system";
524 }
525 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800526 }
527 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800528 if (!boot_image_filename.empty()) {
529 if (!OS::FileExists(boot_image_filename.c_str())) {
530 fprintf(stderr, "Failed to find boot image file %s\n", boot_image_filename.c_str());
531 return EXIT_FAILURE;
532 }
Brian Carlstromb0011262011-12-09 12:17:24 -0800533 boot_image_option += "-Ximage:";
534 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700535 }
536
Brian Carlstromae826982011-11-09 01:33:42 -0800537 if (image_classes_filename != NULL && !image) {
538 fprintf(stderr, "--image-classes should only be used with --image\n");
539 return EXIT_FAILURE;
540 }
541
542 if (image_classes_filename != NULL && !boot_image_option.empty()) {
543 fprintf(stderr, "--image-classes should not be used with --boot-image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700544 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700545 }
546
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800547 if (dex_filenames.empty() && zip_fd == -1) {
548 fprintf(stderr, "Input must be supplied with either --dex-file or --zip-fd\n");
549 return EXIT_FAILURE;
550 }
551
552 if (!dex_filenames.empty() && zip_fd != -1) {
553 fprintf(stderr, "--dex-file should not be used with --zip-fd\n");
554 return EXIT_FAILURE;
555 }
556
Brian Carlstroma004aa92012-02-08 18:05:09 -0800557 if (!dex_filenames.empty() && !zip_location.empty()) {
558 fprintf(stderr, "--dex-file should not be used with --zip-location\n");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800559 return EXIT_FAILURE;
560 }
561
Brian Carlstroma004aa92012-02-08 18:05:09 -0800562 if (dex_locations.empty()) {
563 for (size_t i = 0; i < dex_filenames.size(); i++) {
564 dex_locations.push_back(dex_filenames[i]);
565 }
566 } else if (dex_locations.size() != dex_filenames.size()) {
567 fprintf(stderr, "--dex-location arguments do not match --dex-file arguments\n");
568 return EXIT_FAILURE;
569 }
570
571 if (zip_fd != -1 && zip_location.empty()) {
572 fprintf(stderr, "--zip-location should be supplied with --zip-fd\n");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800573 return EXIT_FAILURE;
574 }
575
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700576 if (boot_image_option.empty()) {
577 if (image_base == 0) {
578 fprintf(stderr, "non-zero --base not specified\n");
579 return EXIT_FAILURE;
580 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700581 }
582
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800583 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800584 UniquePtr<File> oat_file;
585 if (!oat_filename.empty()) {
586 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800587 if (oat_location.empty()) {
588 oat_location = oat_filename;
589 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800590 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800591 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800592 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800593 if (oat_file.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800594 PLOG(ERROR) << "Unable to create oat file: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800595 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700596 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800597
Brian Carlstroma004aa92012-02-08 18:05:09 -0800598 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700599
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700600 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700601 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800602 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700603 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800604 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
605 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700606 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700607 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
608 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700609 if (!host_prefix.empty()) {
610 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
611 }
jeffhao5d840402011-10-24 17:09:45 -0700612 for (size_t i = 0; i < runtime_args.size(); i++) {
613 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
614 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700615
Elliott Hughes5523ee02012-02-03 18:18:34 -0800616 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, thread_count));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700617
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800618 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800619 UniquePtr<const std::set<std::string> > image_classes(NULL);
620 if (image_classes_filename != NULL) {
621 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
622 if (image_classes.get() == NULL) {
623 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
624 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700625 }
626 }
627
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800628 std::vector<const DexFile*> dex_files;
629 if (boot_image_option.empty()) {
630 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
631 } else {
632 if (dex_filenames.empty()) {
633 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
634 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800635 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800636 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800637 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800638 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800639 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800640 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800641 return EXIT_FAILURE;
642 }
643 dex_files.push_back(dex_file);
644 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800645 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800646 }
647 }
648
Brian Carlstromae826982011-11-09 01:33:42 -0800649 if (!dex2oat->CreateOatFile(boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800650 dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800651 oat_file.get(),
652 image,
653 image_classes.get())) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800654 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700655 return EXIT_FAILURE;
656 }
657
Brian Carlstromae826982011-11-09 01:33:42 -0800658 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800659 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700660 return EXIT_SUCCESS;
661 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700662
Brian Carlstromae826982011-11-09 01:33:42 -0800663 if (!dex2oat->CreateImageFile(image_filename,
664 image_base,
665 image_classes.get(),
666 oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800667 oat_location)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700668 return EXIT_FAILURE;
669 }
670
Brian Carlstromae826982011-11-09 01:33:42 -0800671 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800672 LOG(INFO) << "Oat file written successfully: " << oat_filename;
673 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700674 return EXIT_SUCCESS;
675}
676
677} // namespace art
678
679int main(int argc, char** argv) {
680 return art::dex2oat(argc, argv);
681}