blob: 05ff0c0cf31c39a9454c6497f673f1e80d6102fa [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>
Brian Carlstrom6cd40e52012-05-03 14:15:11 -070019#include <sys/stat.h>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070020
Brian Carlstromae826982011-11-09 01:33:42 -080021#include <iostream>
22#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070023#include <string>
24#include <vector>
25
26#include "class_linker.h"
27#include "class_loader.h"
28#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070029#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070030#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080031#include "leb128.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070034#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070035#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036#include "ScopedLocalRef.h"
37#include "scoped_thread_state_change.h"
Brian Carlstroma004aa92012-02-08 18:05:09 -080038#include "stl_util.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070039#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080040#include "timing_logger.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041#include "well_known_classes.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080042#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070043
44namespace art {
45
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080046static void UsageErrorV(const char* fmt, va_list ap) {
47 std::string error;
48 StringAppendV(&error, fmt, ap);
49 LOG(ERROR) << error;
50}
51
52static void UsageError(const char* fmt, ...) {
53 va_list ap;
54 va_start(ap, fmt);
55 UsageErrorV(fmt, ap);
56 va_end(ap);
57}
58
59static void Usage(const char* fmt, ...) {
60 va_list ap;
61 va_start(ap, fmt);
62 UsageErrorV(fmt, ap);
63 va_end(ap);
64
65 UsageError("Usage: dex2oat [options]...");
66 UsageError("");
67 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
68 UsageError(" Example: --dex-file=/system/framework/core.jar");
69 UsageError("");
70 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
71 UsageError(" containing a classes.dex file to compile.");
72 UsageError(" Example: --zip-fd=5");
73 UsageError("");
74 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
75 UsageError(" to the file descriptor specified by --zip-fd.");
76 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
77 UsageError("");
78 UsageError(" --oat-file=<file.oat>: specifies the required oat filename.");
79 UsageError(" Example: --oat-file=/system/framework/boot.oat");
80 UsageError("");
81 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
82 UsageError(" to the file descriptor specified by --oat-fd.");
83 UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat");
84 UsageError("");
Logan Chien8b977d32012-02-21 19:14:55 +080085#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +080086 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
87 UsageError(" Example: --bitcode=/system/framework/boot.bc");
88 UsageError("");
89#endif
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080090 UsageError(" --image=<file.art>: specifies the output image filename.");
91 UsageError(" Example: --image=/system/framework/boot.art");
92 UsageError("");
93 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
94 UsageError(" Example: --image=frameworks/base/preloaded-classes");
95 UsageError("");
96 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
97 UsageError(" Example: --base=0x50000000");
98 UsageError("");
99 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
100 UsageError(" Example: --boot-image=/system/framework/boot.art");
101 UsageError(" Default: <host-prefix>/system/framework/boot.art");
102 UsageError("");
103 UsageError(" --host-prefix may be used to translate host paths to target paths during");
104 UsageError(" cross compilation.");
105 UsageError(" Example: --host-prefix=out/target/product/crespo");
106 UsageError(" Default: $ANDROID_PRODUCT_OUT");
107 UsageError("");
jeffhao1f71ae82012-05-24 16:08:24 -0700108 UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction");
Ian Rogers49c48942012-03-11 15:15:37 -0700109 UsageError(" set.");
jeffhao1f71ae82012-05-24 16:08:24 -0700110 UsageError(" Example: --instruction-set=x86");
111 UsageError(" Default: arm");
Ian Rogers49c48942012-03-11 15:15:37 -0700112 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800113 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
114 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
115 UsageError(" Use a separate --runtime-arg switch for each argument.");
116 UsageError(" Example: --runtime-arg -Xms256m");
117 UsageError("");
118 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700119 exit(EXIT_FAILURE);
120}
121
Brian Carlstromae826982011-11-09 01:33:42 -0800122class Dex2Oat {
123 public:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, InstructionSet instruction_set,
125 size_t thread_count, bool support_debugging)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700126 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 if (!CreateRuntime(options, instruction_set)) {
128 *p_dex2oat = NULL;
129 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800130 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 *p_dex2oat = new Dex2Oat(Runtime::Current(), instruction_set, thread_count, support_debugging);
132 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800133 }
134
135 ~Dex2Oat() {
136 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800137 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800138 }
139
140 // Make a list of descriptors for classes to include in the image
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800143 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
144 if (image_classes_file.get() == NULL) {
145 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
146 return NULL;
147 }
148
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800149 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800150 ClassLinker* class_linker = runtime_->GetClassLinker();
151 while (image_classes_file->good()) {
152 std::string dot;
153 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800154 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800155 continue;
156 }
Elliott Hughes95572412011-12-13 18:14:20 -0800157 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800158 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800159 if (klass.get() == NULL) {
160 LOG(WARNING) << "Failed to find class " << descriptor;
161 Thread::Current()->ClearException();
162 }
163 }
164 image_classes_file->close();
165
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800166 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
167 // exceptions are resolved by the verifier when there is a catch block in an interested method.
168 // Do this here so that exception classes appear to have been specified image classes.
169 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
Elliott Hughes35be9b12012-05-29 17:59:26 -0700170 SirtRef<Class> java_lang_Throwable(class_linker->FindSystemClass("Ljava/lang/Throwable;"));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800171 do {
172 unresolved_exception_types.clear();
173 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
174 &unresolved_exception_types);
175 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
176 for (It it = unresolved_exception_types.begin(),
177 end = unresolved_exception_types.end();
178 it != end; ++it) {
179 uint16_t exception_type_idx = it->first;
180 const DexFile* dex_file = it->second;
181 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
182 ClassLoader* class_loader = NULL;
183 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
184 class_loader));
185 if (klass.get() == NULL) {
186 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
187 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
188 LOG(FATAL) << "Failed to resolve class " << descriptor;
189 }
Elliott Hughes35be9b12012-05-29 17:59:26 -0700190 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get()));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800191 }
192 // Resolving exceptions may load classes that reference more exceptions, iterate until no
193 // more are found
194 } while (!unresolved_exception_types.empty());
195
Brian Carlstromae826982011-11-09 01:33:42 -0800196 // We walk the roots looking for classes so that we'll pick up the
197 // above classes plus any classes them depend on such super
198 // classes, interfaces, and the required ClassLinker roots.
199 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800200 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800201 CHECK_NE(image_classes->size(), 0U);
202 return image_classes.release();
203 }
204
Brian Carlstromf5822582012-03-19 22:34:31 -0700205 const Compiler* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700206 const std::string* host_prefix,
Brian Carlstromf5822582012-03-19 22:34:31 -0700207 const std::vector<const DexFile*>& dex_files,
208 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800209#if defined(ART_USE_LLVM_COMPILER)
Logan Chiende08e842012-03-21 00:34:12 +0800210 const std::string& bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800211#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700212 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700213 const std::set<std::string>* image_classes,
214 bool dump_stats,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 bool dump_timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800217 // SirtRef and ClassLoader creation needs to come after Runtime::Create
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 jobject class_loader = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800219 if (!boot_image_option.empty()) {
220 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800221 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800222 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800223 for (size_t i = 0; i < class_path_files.size(); i++) {
224 class_linker->RegisterDexFile(*class_path_files[i]);
225 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 ScopedObjectAccessUnchecked soa(Thread::Current());
227 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
228 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
229 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
230 class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
231 Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800232 }
233
Brian Carlstromf5822582012-03-19 22:34:31 -0700234 UniquePtr<Compiler> compiler(new Compiler(instruction_set_,
235 image,
236 thread_count_,
237 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700238 image_classes,
239 dump_stats,
240 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800241
242#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700243 compiler->SetBitcodeFileName(bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +0800244#endif
245
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
247
248 compiler->CompileAll(class_loader, dex_files);
249
250 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstromae826982011-11-09 01:33:42 -0800251
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700252 std::string image_file_location;
253 uint32_t image_file_location_checksum = 0;
254 Heap* heap = Runtime::Current()->GetHeap();
255 if (heap->GetSpaces().size() > 1) {
256 ImageSpace* image_space = heap->GetImageSpace();
257 image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum();
258 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700259 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
260 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700261 }
262 }
263
264 if (!OatWriter::Create(oat_file,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 class_loader,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700266 dex_files,
267 image_file_location_checksum,
268 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700269 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800270 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700271 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800272 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700273 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800274 }
275
Brian Carlstroma004aa92012-02-08 18:05:09 -0800276 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800277 uintptr_t image_base,
278 const std::set<std::string>* image_classes,
279 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700280 const std::string& oat_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 const Compiler& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700282 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800283 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700284 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800285 LOG(ERROR) << "Failed to create image file " << image_filename;
286 return false;
287 }
288 return true;
289 }
290
291 private:
Ian Rogers49c48942012-03-11 15:15:37 -0700292 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
293 bool support_debugging)
294 : instruction_set_(instruction_set),
295 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800296 thread_count_(thread_count),
297 support_debugging_(support_debugging),
298 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800299 }
Brian Carlstromae826982011-11-09 01:33:42 -0800300
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700302 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 if (!Runtime::Create(options, false)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800304 LOG(ERROR) << "Failed to create runtime";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800306 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 Runtime* runtime = Runtime::Current();
Brian Carlstromae826982011-11-09 01:33:42 -0800308 // if we loaded an existing image, we will reuse values from the image roots.
309 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700310 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800311 }
312 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700313 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800314 }
315 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
316 Runtime::TrampolineType type = Runtime::TrampolineType(i);
317 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700318 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800319 }
320 }
Ian Rogers19846512012-02-24 11:42:47 -0800321 if (!runtime->HasResolutionMethod()) {
322 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
323 }
Brian Carlstromae826982011-11-09 01:33:42 -0800324 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
325 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
326 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700327 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800328 }
329 }
Ian Rogers19846512012-02-24 11:42:47 -0800330 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800332 }
333
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800334 static void ResolveExceptionsForMethod(MethodHelper* mh,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800337 const DexFile::CodeItem* code_item = mh->GetCodeItem();
338 if (code_item == NULL) {
339 return; // native or abstract method
340 }
341 if (code_item->tries_size_ == 0) {
342 return; // nothing to process
343 }
344 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
345 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
346 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
347 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
348 bool has_catch_all = false;
349 if (encoded_catch_handler_size <= 0) {
350 encoded_catch_handler_size = -encoded_catch_handler_size;
351 has_catch_all = true;
352 }
353 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
354 uint16_t encoded_catch_handler_handlers_type_idx =
355 DecodeUnsignedLeb128(&encoded_catch_handler_list);
356 // Add to set of types to resolve if not already in the dex cache resolved types
357 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
358 exceptions_to_resolve.insert(
359 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
360 &mh->GetDexFile()));
361 }
362 // ignore address associated with catch handler
363 DecodeUnsignedLeb128(&encoded_catch_handler_list);
364 }
365 if (has_catch_all) {
366 // ignore catch all address
367 DecodeUnsignedLeb128(&encoded_catch_handler_list);
368 }
369 }
370 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371
372 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800374 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
375 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
376 MethodHelper mh;
377 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700378 AbstractMethod* m = c->GetVirtualMethod(i);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800379 mh.ChangeMethod(m);
380 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
381 }
382 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700383 AbstractMethod* m = c->GetDirectMethod(i);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800384 mh.ChangeMethod(m);
385 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
386 }
387 return true;
388 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389
390 static bool RecordImageClassesVisitor(Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800392 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
393 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500394 return true;
395 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800396 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800397 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500398 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500399
Brian Carlstromae826982011-11-09 01:33:42 -0800400 // Appends to dex_files any elements of class_path that it doesn't already
401 // contain. This will open those dex files as necessary.
402 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
403 std::vector<std::string> parsed;
404 Split(class_path, ':', parsed);
405 for (size_t i = 0; i < parsed.size(); ++i) {
406 if (DexFilesContains(dex_files, parsed[i])) {
407 continue;
408 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800409 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800410 if (dex_file == NULL) {
411 LOG(WARNING) << "Failed to open dex file " << parsed[i];
412 } else {
413 dex_files.push_back(dex_file);
414 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500415 }
416 }
Brian Carlstromae826982011-11-09 01:33:42 -0800417
418 // Returns true if dex_files has a dex with the named location.
419 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
420 for (size_t i = 0; i < dex_files.size(); ++i) {
421 if (dex_files[i]->GetLocation() == location) {
422 return true;
423 }
424 }
425 return false;
426 }
427
Ian Rogers49c48942012-03-11 15:15:37 -0700428 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800429
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800430 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800431 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800432 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800433 uint64_t start_ns_;
434
Brian Carlstromae826982011-11-09 01:33:42 -0800435 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
436};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500437
Elliott Hughes72395bf2012-04-24 13:45:26 -0700438static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800439 char* end;
440 int result = strtol(in, &end, 10);
441 if (in == end || *end != '\0') {
442 return false;
443 }
444 *out = result;
445 return true;
446}
447
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700448static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
449 const std::vector<const char*>& dex_locations,
450 std::vector<const DexFile*>& dex_files) {
451 size_t failure_count = 0;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800452 for (size_t i = 0; i < dex_filenames.size(); i++) {
453 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800454 const char* dex_location = dex_locations[i];
455 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800456 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800457 LOG(WARNING) << "could not open .dex from file " << dex_filename;
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700458 ++failure_count;
jeffhao60f83e32012-02-13 17:16:30 -0800459 } else {
460 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800461 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800462 }
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700463 return failure_count;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800464}
465
Elliott Hughes72395bf2012-04-24 13:45:26 -0700466static int dex2oat(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -0700467 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -0700468
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700469 // Skip over argv[0].
470 argv++;
471 argc--;
472
473 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800474 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700475 }
476
477 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800478 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800479 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800480 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700481 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800482 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800483 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800484#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800485 std::string bitcode_filename;
486#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800487 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800488 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800489 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700490 uintptr_t image_base = 0;
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700491 UniquePtr<std::string> host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700492 std::vector<const char*> runtime_args;
buzbeea1da8a52012-07-09 14:00:21 -0700493#if defined(ART_USE_QUICK_COMPILER)
494 int thread_count = 1;
495#else
Elliott Hughes5c599942012-06-13 16:45:05 -0700496 int thread_count = sysconf(_SC_NPROCESSORS_CONF);
buzbeea1da8a52012-07-09 14:00:21 -0700497#endif
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800498 bool support_debugging = false;
jeffhao9ad4f222012-05-30 18:51:19 -0700499#if defined(__arm__)
Ian Rogers49c48942012-03-11 15:15:37 -0700500 InstructionSet instruction_set = kThumb2;
jeffhao9ad4f222012-05-30 18:51:19 -0700501#elif defined(__i386__)
502 InstructionSet instruction_set = kX86;
503#elif defined(__mips__)
504 InstructionSet instruction_set = kMips;
505#else
506#error "Unsupported architecture"
507#endif
Elliott Hughes67d92002012-03-26 15:08:51 -0700508 bool dump_stats = kIsDebugBuild;
509 bool dump_timings = kIsDebugBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700510
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700511 for (int i = 0; i < argc; i++) {
512 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800513 bool log_options = false;
514 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700515 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
516 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700517 if (option.starts_with("--dex-file=")) {
518 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800519 } else if (option.starts_with("--dex-location=")) {
520 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800521 } else if (option.starts_with("--zip-fd=")) {
522 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800523 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800524 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800525 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800526 } else if (option.starts_with("--zip-location=")) {
527 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800528 } else if (option.starts_with("--oat-file=")) {
529 oat_filename = option.substr(strlen("--oat-file=")).data();
530 } else if (option.starts_with("--oat-fd=")) {
531 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800532 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800533 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800534 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800535 } else if (option.starts_with("-g")) {
536 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800537 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800538 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800539 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800540 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800541 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800542 } else if (option.starts_with("--oat-location=")) {
543 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800544#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800545 } else if (option.starts_with("--bitcode=")) {
546 bitcode_filename = option.substr(strlen("--bitcode=")).data();
547#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700548 } else if (option.starts_with("--image=")) {
549 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800550 } else if (option.starts_with("--image-classes=")) {
551 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700552 } else if (option.starts_with("--base=")) {
553 const char* image_base_str = option.substr(strlen("--base=")).data();
554 char* end;
555 image_base = strtoul(image_base_str, &end, 16);
556 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800557 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700558 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700559 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800560 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700561 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700562 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Ian Rogers49c48942012-03-11 15:15:37 -0700563 } else if (option.starts_with("--instruction-set=")) {
564 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
jeffhao1f71ae82012-05-24 16:08:24 -0700565 if (instruction_set_str == "arm") {
Ian Rogers49c48942012-03-11 15:15:37 -0700566 instruction_set = kThumb2;
jeffhao1f71ae82012-05-24 16:08:24 -0700567 } else if (instruction_set_str == "mips") {
Ian Rogers49c48942012-03-11 15:15:37 -0700568 instruction_set = kMips;
jeffhao1f71ae82012-05-24 16:08:24 -0700569 } else if (instruction_set_str == "x86") {
Ian Rogers49c48942012-03-11 15:15:37 -0700570 instruction_set = kX86;
571 }
jeffhao5d840402011-10-24 17:09:45 -0700572 } else if (option == "--runtime-arg") {
573 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800574 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700575 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800576 if (log_options) {
577 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
578 }
jeffhao5d840402011-10-24 17:09:45 -0700579 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700580 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800581 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700582 }
583 }
584
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800585 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800586 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800587 }
588
589 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800590 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800591 }
592
593 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800594 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800595 }
596
Brian Carlstroma004aa92012-02-08 18:05:09 -0800597 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800598 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700599 }
600
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700601 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800602 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
603 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700604 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800605 }
606 }
607
Brian Carlstroma004aa92012-02-08 18:05:09 -0800608 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800609 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700610 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800611 boot_image_filename += GetAndroidRoot();
612 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700613 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800614 boot_image_filename += "/system";
615 }
616 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800617 }
618 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800619 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800620 boot_image_option += "-Ximage:";
621 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700622 }
623
Brian Carlstromae826982011-11-09 01:33:42 -0800624 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800625 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800626 }
627
628 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800629 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700630 }
631
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800632 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800633 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800634 }
635
636 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800637 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800638 }
639
Brian Carlstroma004aa92012-02-08 18:05:09 -0800640 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800641 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800642 }
643
Brian Carlstroma004aa92012-02-08 18:05:09 -0800644 if (dex_locations.empty()) {
645 for (size_t i = 0; i < dex_filenames.size(); i++) {
646 dex_locations.push_back(dex_filenames[i]);
647 }
648 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800649 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800650 }
651
652 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800653 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800654 }
655
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700656 if (boot_image_option.empty()) {
657 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800658 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700659 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700660 }
661
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800662 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800663 UniquePtr<File> oat_file;
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700664 bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
665 if (create_file) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800666 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800667 if (oat_location.empty()) {
668 oat_location = oat_filename;
669 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800670 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800671 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800672 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800673 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700674 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
675 return EXIT_FAILURE;
676 }
677 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
678 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800679 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700680 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800681
Brian Carlstroma004aa92012-02-08 18:05:09 -0800682 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700683
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700684 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700685 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800686 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700687 if (boot_image_option.empty()) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700688 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
689 if (failure_count > 0) {
690 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
691 return EXIT_FAILURE;
692 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800693 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700694 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700695 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
696 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700697 if (host_prefix.get() != NULL) {
698 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700699 }
jeffhao5d840402011-10-24 17:09:45 -0700700 for (size_t i = 0; i < runtime_args.size(); i++) {
701 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
702 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700703
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 Dex2Oat* p_dex2oat;
705 if (!Dex2Oat::Create(&p_dex2oat, options, instruction_set, thread_count, support_debugging)) {
706 LOG(ERROR) << "Failed to create dex2oat";
707 return EXIT_FAILURE;
708 }
709 UniquePtr<Dex2Oat> dex2oat(p_dex2oat);
710 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
711 // give it away now and then switch to a more managable ScopedObjectAccess.
712 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
713 // Whilst we're in native take the opportunity to initialize well known classes.
714 WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv());
715 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700716
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800717 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800718 UniquePtr<const std::set<std::string> > image_classes(NULL);
719 if (image_classes_filename != NULL) {
720 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
721 if (image_classes.get() == NULL) {
722 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
723 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700724 }
725 }
726
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800727 std::vector<const DexFile*> dex_files;
728 if (boot_image_option.empty()) {
729 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
730 } else {
731 if (dex_filenames.empty()) {
732 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
733 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800734 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800735 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800736 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800737 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800738 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800739 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800740 return EXIT_FAILURE;
741 }
742 dex_files.push_back(dex_file);
743 } else {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700744 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
745 if (failure_count > 0) {
746 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
747 return EXIT_FAILURE;
748 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800749 }
750 }
751
Brian Carlstromf5822582012-03-19 22:34:31 -0700752 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700753 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700754 dex_files,
755 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800756#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700757 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800758#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700759 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700760 image_classes.get(),
761 dump_stats,
762 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700763
764 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800765 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700766 return EXIT_FAILURE;
767 }
768
Brian Carlstromae826982011-11-09 01:33:42 -0800769 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800770 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700771 return EXIT_SUCCESS;
772 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700773
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
775 bool image_creation_success = dex2oat->CreateImageFile(image_filename,
776 image_base,
777 image_classes.get(),
778 oat_filename,
779 oat_location,
780 *compiler.get());
781 Thread::Current()->TransitionFromSuspendedToRunnable();
782 if (!image_creation_success) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700783 return EXIT_FAILURE;
784 }
785
Brian Carlstromae826982011-11-09 01:33:42 -0800786 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800787 LOG(INFO) << "Oat file written successfully: " << oat_filename;
788 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700789 return EXIT_SUCCESS;
790}
791
792} // namespace art
793
794int main(int argc, char** argv) {
795 return art::dex2oat(argc, argv);
796}