blob: e18a6282ba8a84797f18d4d894fe59c8012c6a0d [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
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080042static void UsageErrorV(const char* fmt, va_list ap) {
43 std::string error;
44 StringAppendV(&error, fmt, ap);
45 LOG(ERROR) << error;
46}
47
48static void UsageError(const char* fmt, ...) {
49 va_list ap;
50 va_start(ap, fmt);
51 UsageErrorV(fmt, ap);
52 va_end(ap);
53}
54
55static void Usage(const char* fmt, ...) {
56 va_list ap;
57 va_start(ap, fmt);
58 UsageErrorV(fmt, ap);
59 va_end(ap);
60
61 UsageError("Usage: dex2oat [options]...");
62 UsageError("");
63 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
64 UsageError(" Example: --dex-file=/system/framework/core.jar");
65 UsageError("");
66 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
67 UsageError(" containing a classes.dex file to compile.");
68 UsageError(" Example: --zip-fd=5");
69 UsageError("");
70 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
71 UsageError(" to the file descriptor specified by --zip-fd.");
72 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
73 UsageError("");
74 UsageError(" --oat-file=<file.oat>: specifies the required oat filename.");
75 UsageError(" Example: --oat-file=/system/framework/boot.oat");
76 UsageError("");
77 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
78 UsageError(" to the file descriptor specified by --oat-fd.");
79 UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat");
80 UsageError("");
Logan Chien8b977d32012-02-21 19:14:55 +080081#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +080082 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
83 UsageError(" Example: --bitcode=/system/framework/boot.bc");
84 UsageError("");
85#endif
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080086 UsageError(" --image=<file.art>: specifies the output image filename.");
87 UsageError(" Example: --image=/system/framework/boot.art");
88 UsageError("");
89 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
90 UsageError(" Example: --image=frameworks/base/preloaded-classes");
91 UsageError("");
92 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
93 UsageError(" Example: --base=0x50000000");
94 UsageError("");
95 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
96 UsageError(" Example: --boot-image=/system/framework/boot.art");
97 UsageError(" Default: <host-prefix>/system/framework/boot.art");
98 UsageError("");
99 UsageError(" --host-prefix may be used to translate host paths to target paths during");
100 UsageError(" cross compilation.");
101 UsageError(" Example: --host-prefix=out/target/product/crespo");
102 UsageError(" Default: $ANDROID_PRODUCT_OUT");
103 UsageError("");
Ian Rogers49c48942012-03-11 15:15:37 -0700104 UsageError(" --instruction-set=(ARM|Thumb2|MIPS|X86): compile for a particular instruction");
105 UsageError(" set.");
106 UsageError(" Example: --instruction-set=X86");
107 UsageError(" Default: Thumb2");
108 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800109 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
110 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
111 UsageError(" Use a separate --runtime-arg switch for each argument.");
112 UsageError(" Example: --runtime-arg -Xms256m");
113 UsageError("");
114 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700115 exit(EXIT_FAILURE);
116}
117
Brian Carlstromae826982011-11-09 01:33:42 -0800118class Dex2Oat {
119 public:
120
Ian Rogers49c48942012-03-11 15:15:37 -0700121 static Dex2Oat* Create(Runtime::Options& options, InstructionSet instruction_set,
122 size_t thread_count, bool support_debugging) {
123 UniquePtr<Runtime> runtime(CreateRuntime(options, instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800124 if (runtime.get() == NULL) {
125 return NULL;
126 }
Ian Rogers49c48942012-03-11 15:15:37 -0700127 return new Dex2Oat(runtime.release(), instruction_set, thread_count, support_debugging);
Brian Carlstromae826982011-11-09 01:33:42 -0800128 }
129
130 ~Dex2Oat() {
131 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800132 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800133 }
134
135 // Make a list of descriptors for classes to include in the image
136 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
137 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
138 if (image_classes_file.get() == NULL) {
139 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
140 return NULL;
141 }
142
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800143 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800144 ClassLinker* class_linker = runtime_->GetClassLinker();
145 while (image_classes_file->good()) {
146 std::string dot;
147 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800148 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800149 continue;
150 }
Elliott Hughes95572412011-12-13 18:14:20 -0800151 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800152 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800153 if (klass.get() == NULL) {
154 LOG(WARNING) << "Failed to find class " << descriptor;
155 Thread::Current()->ClearException();
156 }
157 }
158 image_classes_file->close();
159
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800160 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
161 // exceptions are resolved by the verifier when there is a catch block in an interested method.
162 // Do this here so that exception classes appear to have been specified image classes.
163 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
164 do {
165 unresolved_exception_types.clear();
166 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
167 &unresolved_exception_types);
168 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
169 for (It it = unresolved_exception_types.begin(),
170 end = unresolved_exception_types.end();
171 it != end; ++it) {
172 uint16_t exception_type_idx = it->first;
173 const DexFile* dex_file = it->second;
174 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
175 ClassLoader* class_loader = NULL;
176 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
177 class_loader));
178 if (klass.get() == NULL) {
179 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
180 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
181 LOG(FATAL) << "Failed to resolve class " << descriptor;
182 }
183 DCHECK(klass->IsThrowableClass());
184 }
185 // Resolving exceptions may load classes that reference more exceptions, iterate until no
186 // more are found
187 } while (!unresolved_exception_types.empty());
188
Brian Carlstromae826982011-11-09 01:33:42 -0800189 // We walk the roots looking for classes so that we'll pick up the
190 // above classes plus any classes them depend on such super
191 // classes, interfaces, and the required ClassLinker roots.
192 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800193 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800194 CHECK_NE(image_classes->size(), 0U);
195 return image_classes.release();
196 }
197
Brian Carlstromf5822582012-03-19 22:34:31 -0700198 const Compiler* CreateOatFile(const std::string& boot_image_option,
199 const std::string& host_prefix,
200 const std::vector<const DexFile*>& dex_files,
201 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800202#if defined(ART_USE_LLVM_COMPILER)
Logan Chiende08e842012-03-21 00:34:12 +0800203 const std::string& bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800204#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700205 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700206 const std::set<std::string>* image_classes,
207 bool dump_stats,
208 bool dump_timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800209 // SirtRef and ClassLoader creation needs to come after Runtime::Create
210 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
211 if (class_loader.get() == NULL) {
212 LOG(ERROR) << "Failed to create SirtRef for class loader";
213 return false;
214 }
215
Brian Carlstromae826982011-11-09 01:33:42 -0800216 if (!boot_image_option.empty()) {
217 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800218 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800219 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800220 for (size_t i = 0; i < class_path_files.size(); i++) {
221 class_linker->RegisterDexFile(*class_path_files[i]);
222 }
223 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800224 }
225
Brian Carlstromf5822582012-03-19 22:34:31 -0700226 UniquePtr<Compiler> compiler(new Compiler(instruction_set_,
227 image,
228 thread_count_,
229 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700230 image_classes,
231 dump_stats,
232 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800233
234#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700235 compiler->SetBitcodeFileName(bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +0800236#endif
237
Brian Carlstromf5822582012-03-19 22:34:31 -0700238 compiler->CompileAll(class_loader->get(), dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800239
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700240 std::string image_file_location;
241 uint32_t image_file_location_checksum = 0;
242 Heap* heap = Runtime::Current()->GetHeap();
243 if (heap->GetSpaces().size() > 1) {
244 ImageSpace* image_space = heap->GetImageSpace();
245 image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum();
246 image_file_location = image_space->GetImageFilename();
247 if (!host_prefix.empty() && StartsWith(image_file_location, host_prefix.c_str())) {
248 image_file_location = image_file_location.substr(host_prefix.size());
249 }
250 }
251
252 if (!OatWriter::Create(oat_file,
253 class_loader->get(),
254 dex_files,
255 image_file_location_checksum,
256 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700257 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800258 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700259 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800260 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700261 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800262 }
263
Brian Carlstroma004aa92012-02-08 18:05:09 -0800264 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800265 uintptr_t image_base,
266 const std::set<std::string>* image_classes,
267 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700268 const std::string& oat_location,
269 const Compiler& compiler) {
Brian Carlstromae826982011-11-09 01:33:42 -0800270 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700271 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800272 LOG(ERROR) << "Failed to create image file " << image_filename;
273 return false;
274 }
275 return true;
276 }
277
278 private:
279
Ian Rogers49c48942012-03-11 15:15:37 -0700280 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
281 bool support_debugging)
282 : instruction_set_(instruction_set),
283 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800284 thread_count_(thread_count),
285 support_debugging_(support_debugging),
286 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800287 }
Brian Carlstromae826982011-11-09 01:33:42 -0800288
Ian Rogers49c48942012-03-11 15:15:37 -0700289 static Runtime* CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) {
Brian Carlstromae826982011-11-09 01:33:42 -0800290 Runtime* runtime = Runtime::Create(options, false);
291 if (runtime == NULL) {
292 LOG(ERROR) << "Failed to create runtime";
293 return NULL;
294 }
295
296 // if we loaded an existing image, we will reuse values from the image roots.
297 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700298 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800299 }
300 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700301 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800302 }
303 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
304 Runtime::TrampolineType type = Runtime::TrampolineType(i);
305 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700306 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800307 }
308 }
Ian Rogers19846512012-02-24 11:42:47 -0800309 if (!runtime->HasResolutionMethod()) {
310 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
311 }
Brian Carlstromae826982011-11-09 01:33:42 -0800312 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
313 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
314 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700315 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800316 }
317 }
Ian Rogers19846512012-02-24 11:42:47 -0800318 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800319 return runtime;
320 }
321
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800322 static void ResolveExceptionsForMethod(MethodHelper* mh,
323 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
324 const DexFile::CodeItem* code_item = mh->GetCodeItem();
325 if (code_item == NULL) {
326 return; // native or abstract method
327 }
328 if (code_item->tries_size_ == 0) {
329 return; // nothing to process
330 }
331 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
332 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
333 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
334 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
335 bool has_catch_all = false;
336 if (encoded_catch_handler_size <= 0) {
337 encoded_catch_handler_size = -encoded_catch_handler_size;
338 has_catch_all = true;
339 }
340 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
341 uint16_t encoded_catch_handler_handlers_type_idx =
342 DecodeUnsignedLeb128(&encoded_catch_handler_list);
343 // Add to set of types to resolve if not already in the dex cache resolved types
344 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
345 exceptions_to_resolve.insert(
346 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
347 &mh->GetDexFile()));
348 }
349 // ignore address associated with catch handler
350 DecodeUnsignedLeb128(&encoded_catch_handler_list);
351 }
352 if (has_catch_all) {
353 // ignore catch all address
354 DecodeUnsignedLeb128(&encoded_catch_handler_list);
355 }
356 }
357 }
358 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
359 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
360 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
361 MethodHelper mh;
362 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
363 Method* m = c->GetVirtualMethod(i);
364 mh.ChangeMethod(m);
365 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
366 }
367 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
368 Method* m = c->GetDirectMethod(i);
369 mh.ChangeMethod(m);
370 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
371 }
372 return true;
373 }
374 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800375 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
376 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500377 return true;
378 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800379 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800380 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500381 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500382
Brian Carlstromae826982011-11-09 01:33:42 -0800383 // Appends to dex_files any elements of class_path that it doesn't already
384 // contain. This will open those dex files as necessary.
385 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
386 std::vector<std::string> parsed;
387 Split(class_path, ':', parsed);
388 for (size_t i = 0; i < parsed.size(); ++i) {
389 if (DexFilesContains(dex_files, parsed[i])) {
390 continue;
391 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800392 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800393 if (dex_file == NULL) {
394 LOG(WARNING) << "Failed to open dex file " << parsed[i];
395 } else {
396 dex_files.push_back(dex_file);
397 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500398 }
399 }
Brian Carlstromae826982011-11-09 01:33:42 -0800400
401 // Returns true if dex_files has a dex with the named location.
402 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
403 for (size_t i = 0; i < dex_files.size(); ++i) {
404 if (dex_files[i]->GetLocation() == location) {
405 return true;
406 }
407 }
408 return false;
409 }
410
Ian Rogers49c48942012-03-11 15:15:37 -0700411 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800412
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800413 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800414 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800415 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800416 uint64_t start_ns_;
417
Brian Carlstromae826982011-11-09 01:33:42 -0800418 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
419};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500420
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800421bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800422 char* end;
423 int result = strtol(in, &end, 10);
424 if (in == end || *end != '\0') {
425 return false;
426 }
427 *out = result;
428 return true;
429}
430
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800431void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800432 const std::vector<const char*>& dex_locations,
433 std::vector<const DexFile*>& dex_files) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800434 for (size_t i = 0; i < dex_filenames.size(); i++) {
435 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800436 const char* dex_location = dex_locations[i];
437 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800438 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800439 LOG(WARNING) << "could not open .dex from file " << dex_filename;
440 } else {
441 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800442 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800443 }
444}
445
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700446int dex2oat(int argc, char** argv) {
447 // Skip over argv[0].
448 argv++;
449 argc--;
450
451 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800452 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700453 }
454
455 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800456 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800457 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800458 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800460 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800461 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800462#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800463 std::string bitcode_filename;
464#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800465 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800466 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800467 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700468 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700469 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700470 std::vector<const char*> runtime_args;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800471 int thread_count = 2;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800472 bool support_debugging = false;
Ian Rogers49c48942012-03-11 15:15:37 -0700473 InstructionSet instruction_set = kThumb2;
Elliott Hughes67d92002012-03-26 15:08:51 -0700474 bool dump_stats = kIsDebugBuild;
475 bool dump_timings = kIsDebugBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700476
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700477 for (int i = 0; i < argc; i++) {
478 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800479 bool log_options = false;
480 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700481 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
482 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700483 if (option.starts_with("--dex-file=")) {
484 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800485 } else if (option.starts_with("--dex-location=")) {
486 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800487 } else if (option.starts_with("--zip-fd=")) {
488 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800489 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800490 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800491 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800492 } else if (option.starts_with("--zip-location=")) {
493 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800494 } else if (option.starts_with("--oat-file=")) {
495 oat_filename = option.substr(strlen("--oat-file=")).data();
496 } else if (option.starts_with("--oat-fd=")) {
497 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800498 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800499 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800500 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800501 } else if (option.starts_with("-g")) {
502 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800503 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800504 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800505 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800506 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800507 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800508 } else if (option.starts_with("--oat-location=")) {
509 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800510#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800511 } else if (option.starts_with("--bitcode=")) {
512 bitcode_filename = option.substr(strlen("--bitcode=")).data();
513#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700514 } else if (option.starts_with("--image=")) {
515 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800516 } else if (option.starts_with("--image-classes=")) {
517 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700518 } else if (option.starts_with("--base=")) {
519 const char* image_base_str = option.substr(strlen("--base=")).data();
520 char* end;
521 image_base = strtoul(image_base_str, &end, 16);
522 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800523 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700524 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700525 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800526 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700527 } else if (option.starts_with("--host-prefix=")) {
528 host_prefix = option.substr(strlen("--host-prefix=")).data();
Ian Rogers49c48942012-03-11 15:15:37 -0700529 } else if (option.starts_with("--instruction-set=")) {
530 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
531 if (instruction_set_str == "Thumb2" || instruction_set_str == "ARM") {
532 instruction_set = kThumb2;
533 } else if (instruction_set_str == "MIPS") {
534 instruction_set = kMips;
535 } else if (instruction_set_str == "X86") {
536 instruction_set = kX86;
537 }
jeffhao5d840402011-10-24 17:09:45 -0700538 } else if (option == "--runtime-arg") {
539 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800540 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700541 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800542 if (log_options) {
543 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
544 }
jeffhao5d840402011-10-24 17:09:45 -0700545 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700546 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800547 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700548 }
549 }
550
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800551 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800552 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800553 }
554
555 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800556 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800557 }
558
559 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800560 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800561 }
562
Brian Carlstroma004aa92012-02-08 18:05:09 -0800563 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800564 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700565 }
566
Brian Carlstromb0011262011-12-09 12:17:24 -0800567 if (host_prefix.empty()) {
568 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
569 if (android_product_out != NULL) {
570 host_prefix = android_product_out;
571 }
572 }
573
Brian Carlstroma004aa92012-02-08 18:05:09 -0800574 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800575 if (!image && boot_image_filename.empty()) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800576 if (host_prefix.empty()) {
577 boot_image_filename += GetAndroidRoot();
578 } else {
579 boot_image_filename += host_prefix;
580 boot_image_filename += "/system";
581 }
582 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800583 }
584 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800585 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800586 boot_image_option += "-Ximage:";
587 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700588 }
589
Brian Carlstromae826982011-11-09 01:33:42 -0800590 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800591 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800592 }
593
594 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800595 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700596 }
597
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800598 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800599 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800600 }
601
602 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800603 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800604 }
605
Brian Carlstroma004aa92012-02-08 18:05:09 -0800606 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800607 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800608 }
609
Brian Carlstroma004aa92012-02-08 18:05:09 -0800610 if (dex_locations.empty()) {
611 for (size_t i = 0; i < dex_filenames.size(); i++) {
612 dex_locations.push_back(dex_filenames[i]);
613 }
614 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800615 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800616 }
617
618 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800619 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800620 }
621
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700622 if (boot_image_option.empty()) {
623 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800624 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700625 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700626 }
627
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800628 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800629 UniquePtr<File> oat_file;
630 if (!oat_filename.empty()) {
631 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800632 if (oat_location.empty()) {
633 oat_location = oat_filename;
634 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800635 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800636 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800637 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800638 if (oat_file.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800639 PLOG(ERROR) << "Unable to create oat file: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800640 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700641 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800642
Brian Carlstroma004aa92012-02-08 18:05:09 -0800643 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700644
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700645 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700646 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800647 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700648 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800649 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
650 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700651 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700652 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
653 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700654 if (!host_prefix.empty()) {
655 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
656 }
jeffhao5d840402011-10-24 17:09:45 -0700657 for (size_t i = 0; i < runtime_args.size(); i++) {
658 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
659 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700660
Ian Rogers49c48942012-03-11 15:15:37 -0700661 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, instruction_set, thread_count,
662 support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700663
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800664 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800665 UniquePtr<const std::set<std::string> > image_classes(NULL);
666 if (image_classes_filename != NULL) {
667 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
668 if (image_classes.get() == NULL) {
669 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
670 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700671 }
672 }
673
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800674 std::vector<const DexFile*> dex_files;
675 if (boot_image_option.empty()) {
676 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
677 } else {
678 if (dex_filenames.empty()) {
679 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
680 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800681 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800682 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800683 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800684 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800685 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800686 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800687 return EXIT_FAILURE;
688 }
689 dex_files.push_back(dex_file);
690 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800691 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800692 }
693 }
694
Brian Carlstromf5822582012-03-19 22:34:31 -0700695 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
696 host_prefix,
697 dex_files,
698 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800699#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700700 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800701#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700702 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700703 image_classes.get(),
704 dump_stats,
705 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700706
707 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800708 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700709 return EXIT_FAILURE;
710 }
711
Brian Carlstromae826982011-11-09 01:33:42 -0800712 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800713 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700714 return EXIT_SUCCESS;
715 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700716
Brian Carlstromae826982011-11-09 01:33:42 -0800717 if (!dex2oat->CreateImageFile(image_filename,
718 image_base,
719 image_classes.get(),
720 oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700721 oat_location,
722 *compiler.get())) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700723 return EXIT_FAILURE;
724 }
725
Brian Carlstromae826982011-11-09 01:33:42 -0800726 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800727 LOG(INFO) << "Oat file written successfully: " << oat_filename;
728 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700729 return EXIT_SUCCESS;
730}
731
732} // namespace art
733
734int main(int argc, char** argv) {
735 return art::dex2oat(argc, argv);
736}