blob: a8f42afbd0b4125b128c5f14c3c824efc915d7fb [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"
Ian Rogers1f539342012-10-03 21:09:42 -070038#include "sirt_ref.h"
Brian Carlstroma004aa92012-02-08 18:05:09 -080039#include "stl_util.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070040#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080041#include "timing_logger.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042#include "well_known_classes.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080043#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070044
45namespace art {
46
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080047static void UsageErrorV(const char* fmt, va_list ap) {
48 std::string error;
49 StringAppendV(&error, fmt, ap);
50 LOG(ERROR) << error;
51}
52
53static void UsageError(const char* fmt, ...) {
54 va_list ap;
55 va_start(ap, fmt);
56 UsageErrorV(fmt, ap);
57 va_end(ap);
58}
59
60static void Usage(const char* fmt, ...) {
61 va_list ap;
62 va_start(ap, fmt);
63 UsageErrorV(fmt, ap);
64 va_end(ap);
65
66 UsageError("Usage: dex2oat [options]...");
67 UsageError("");
68 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
69 UsageError(" Example: --dex-file=/system/framework/core.jar");
70 UsageError("");
71 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
72 UsageError(" containing a classes.dex file to compile.");
73 UsageError(" Example: --zip-fd=5");
74 UsageError("");
75 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
76 UsageError(" to the file descriptor specified by --zip-fd.");
77 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
78 UsageError("");
79 UsageError(" --oat-file=<file.oat>: specifies the required oat filename.");
80 UsageError(" Example: --oat-file=/system/framework/boot.oat");
81 UsageError("");
82 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
83 UsageError(" to the file descriptor specified by --oat-fd.");
84 UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat");
85 UsageError("");
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("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080089 UsageError(" --image=<file.art>: specifies the output image filename.");
90 UsageError(" Example: --image=/system/framework/boot.art");
91 UsageError("");
92 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
93 UsageError(" Example: --image=frameworks/base/preloaded-classes");
94 UsageError("");
95 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
96 UsageError(" Example: --base=0x50000000");
97 UsageError("");
98 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
99 UsageError(" Example: --boot-image=/system/framework/boot.art");
100 UsageError(" Default: <host-prefix>/system/framework/boot.art");
101 UsageError("");
102 UsageError(" --host-prefix may be used to translate host paths to target paths during");
103 UsageError(" cross compilation.");
104 UsageError(" Example: --host-prefix=out/target/product/crespo");
105 UsageError(" Default: $ANDROID_PRODUCT_OUT");
106 UsageError("");
jeffhao1f71ae82012-05-24 16:08:24 -0700107 UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction");
Ian Rogers49c48942012-03-11 15:15:37 -0700108 UsageError(" set.");
jeffhao1f71ae82012-05-24 16:08:24 -0700109 UsageError(" Example: --instruction-set=x86");
110 UsageError(" Default: arm");
Ian Rogers49c48942012-03-11 15:15:37 -0700111 UsageError("");
buzbeec531cef2012-10-18 07:09:20 -0700112 UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend");
113 UsageError(" set.");
114 UsageError(" Example: --instruction-set=Portable");
115 UsageError(" Default: Quick");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800116 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
117 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
118 UsageError(" Use a separate --runtime-arg switch for each argument.");
119 UsageError(" Example: --runtime-arg -Xms256m");
120 UsageError("");
121 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700122 exit(EXIT_FAILURE);
123}
124
Brian Carlstromae826982011-11-09 01:33:42 -0800125class Dex2Oat {
126 public:
buzbeec531cef2012-10-18 07:09:20 -0700127 static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, CompilerBackend compiler_backend,
128 InstructionSet instruction_set, size_t thread_count, bool support_debugging)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 if (!CreateRuntime(options, instruction_set)) {
131 *p_dex2oat = NULL;
132 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800133 }
buzbeec531cef2012-10-18 07:09:20 -0700134 *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count,
135 support_debugging);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800137 }
138
139 ~Dex2Oat() {
140 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800141 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800142 }
143
144 // Make a list of descriptors for classes to include in the image
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800147 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
148 if (image_classes_file.get() == NULL) {
149 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
150 return NULL;
151 }
152
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800153 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800154 ClassLinker* class_linker = runtime_->GetClassLinker();
Ian Rogers1f539342012-10-03 21:09:42 -0700155 Thread* self = Thread::Current();
Brian Carlstromae826982011-11-09 01:33:42 -0800156 while (image_classes_file->good()) {
157 std::string dot;
158 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800159 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800160 continue;
161 }
Elliott Hughes95572412011-12-13 18:14:20 -0800162 std::string descriptor(DotToDescriptor(dot.c_str()));
Ian Rogers1f539342012-10-03 21:09:42 -0700163 SirtRef<Class> klass(self, class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800164 if (klass.get() == NULL) {
165 LOG(WARNING) << "Failed to find class " << descriptor;
166 Thread::Current()->ClearException();
167 }
168 }
169 image_classes_file->close();
170
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800171 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
172 // exceptions are resolved by the verifier when there is a catch block in an interested method.
173 // Do this here so that exception classes appear to have been specified image classes.
174 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
Ian Rogers1f539342012-10-03 21:09:42 -0700175 SirtRef<Class> java_lang_Throwable(self,
176 class_linker->FindSystemClass("Ljava/lang/Throwable;"));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800177 do {
178 unresolved_exception_types.clear();
179 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
180 &unresolved_exception_types);
181 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
182 for (It it = unresolved_exception_types.begin(),
183 end = unresolved_exception_types.end();
184 it != end; ++it) {
185 uint16_t exception_type_idx = it->first;
186 const DexFile* dex_file = it->second;
187 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
188 ClassLoader* class_loader = NULL;
Ian Rogers1f539342012-10-03 21:09:42 -0700189 SirtRef<Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx,
190 dex_cache, class_loader));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800191 if (klass.get() == NULL) {
192 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
193 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
194 LOG(FATAL) << "Failed to resolve class " << descriptor;
195 }
Elliott Hughes35be9b12012-05-29 17:59:26 -0700196 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get()));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800197 }
198 // Resolving exceptions may load classes that reference more exceptions, iterate until no
199 // more are found
200 } while (!unresolved_exception_types.empty());
201
Brian Carlstromae826982011-11-09 01:33:42 -0800202 // We walk the roots looking for classes so that we'll pick up the
203 // above classes plus any classes them depend on such super
204 // classes, interfaces, and the required ClassLinker roots.
205 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800206 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800207 CHECK_NE(image_classes->size(), 0U);
208 return image_classes.release();
209 }
210
Brian Carlstromf5822582012-03-19 22:34:31 -0700211 const Compiler* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700212 const std::string* host_prefix,
Brian Carlstromf5822582012-03-19 22:34:31 -0700213 const std::vector<const DexFile*>& dex_files,
214 File* oat_file,
Logan Chiende08e842012-03-21 00:34:12 +0800215 const std::string& bitcode_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700216 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700217 const std::set<std::string>* image_classes,
218 bool dump_stats,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 bool dump_timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800221 // SirtRef and ClassLoader creation needs to come after Runtime::Create
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 jobject class_loader = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800223 if (!boot_image_option.empty()) {
224 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800225 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800226 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800227 for (size_t i = 0; i < class_path_files.size(); i++) {
228 class_linker->RegisterDexFile(*class_path_files[i]);
229 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 ScopedObjectAccessUnchecked soa(Thread::Current());
231 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
232 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
233 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
234 class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
235 Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800236 }
237
buzbeec531cef2012-10-18 07:09:20 -0700238 UniquePtr<Compiler> compiler(new Compiler(compiler_backend_,
239 instruction_set_,
Brian Carlstromf5822582012-03-19 22:34:31 -0700240 image,
241 thread_count_,
242 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700243 image_classes,
244 dump_stats,
245 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800246
buzbeec531cef2012-10-18 07:09:20 -0700247 if ((compiler_backend_ == kPortable) || (compiler_backend_ == kIceland)) {
248 compiler->SetBitcodeFileName(bitcode_filename);
249 }
Logan Chien8b977d32012-02-21 19:14:55 +0800250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
252
253 compiler->CompileAll(class_loader, dex_files);
254
255 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstromae826982011-11-09 01:33:42 -0800256
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700257 std::string image_file_location;
Brian Carlstrom28db0122012-10-18 16:20:41 -0700258 uint32_t image_file_location_oat_checksum = 0;
259 uint32_t image_file_location_oat_begin = 0;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700260 Heap* heap = Runtime::Current()->GetHeap();
261 if (heap->GetSpaces().size() > 1) {
262 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700263 image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
264 image_file_location_oat_begin = reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700265 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700266 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
267 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700268 }
269 }
270
271 if (!OatWriter::Create(oat_file,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 class_loader,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700273 dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700274 image_file_location_oat_checksum,
275 image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700276 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700277 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800278 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700279 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800280 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700281 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800282 }
283
Brian Carlstroma004aa92012-02-08 18:05:09 -0800284 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800285 uintptr_t image_base,
286 const std::set<std::string>* image_classes,
287 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700288 const std::string& oat_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 const Compiler& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700290 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800291 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700292 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800293 LOG(ERROR) << "Failed to create image file " << image_filename;
294 return false;
295 }
296 return true;
297 }
298
299 private:
buzbeec531cef2012-10-18 07:09:20 -0700300 explicit Dex2Oat(Runtime* runtime, CompilerBackend compiler_backend, InstructionSet instruction_set,
301 size_t thread_count, bool support_debugging)
302 : compiler_backend_(compiler_backend),
303 instruction_set_(instruction_set),
Ian Rogers49c48942012-03-11 15:15:37 -0700304 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800305 thread_count_(thread_count),
306 support_debugging_(support_debugging),
307 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800308 }
Brian Carlstromae826982011-11-09 01:33:42 -0800309
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310 static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700311 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 if (!Runtime::Create(options, false)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800313 LOG(ERROR) << "Failed to create runtime";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800315 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 Runtime* runtime = Runtime::Current();
Brian Carlstromae826982011-11-09 01:33:42 -0800317 // if we loaded an existing image, we will reuse values from the image roots.
318 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700319 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800320 }
321 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700322 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800323 }
324 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
325 Runtime::TrampolineType type = Runtime::TrampolineType(i);
326 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700327 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800328 }
329 }
Ian Rogers19846512012-02-24 11:42:47 -0800330 if (!runtime->HasResolutionMethod()) {
331 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
332 }
Brian Carlstromae826982011-11-09 01:33:42 -0800333 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
334 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
335 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700336 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800337 }
338 }
Ian Rogers19846512012-02-24 11:42:47 -0800339 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700340 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800341 }
342
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800343 static void ResolveExceptionsForMethod(MethodHelper* mh,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800346 const DexFile::CodeItem* code_item = mh->GetCodeItem();
347 if (code_item == NULL) {
348 return; // native or abstract method
349 }
350 if (code_item->tries_size_ == 0) {
351 return; // nothing to process
352 }
353 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
354 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
355 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
356 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
357 bool has_catch_all = false;
358 if (encoded_catch_handler_size <= 0) {
359 encoded_catch_handler_size = -encoded_catch_handler_size;
360 has_catch_all = true;
361 }
362 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
363 uint16_t encoded_catch_handler_handlers_type_idx =
364 DecodeUnsignedLeb128(&encoded_catch_handler_list);
365 // Add to set of types to resolve if not already in the dex cache resolved types
366 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
367 exceptions_to_resolve.insert(
368 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
369 &mh->GetDexFile()));
370 }
371 // ignore address associated with catch handler
372 DecodeUnsignedLeb128(&encoded_catch_handler_list);
373 }
374 if (has_catch_all) {
375 // ignore catch all address
376 DecodeUnsignedLeb128(&encoded_catch_handler_list);
377 }
378 }
379 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380
381 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700382 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800383 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
384 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
385 MethodHelper mh;
386 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700387 AbstractMethod* m = c->GetVirtualMethod(i);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800388 mh.ChangeMethod(m);
389 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
390 }
391 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700392 AbstractMethod* m = c->GetDirectMethod(i);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800393 mh.ChangeMethod(m);
394 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
395 }
396 return true;
397 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700398
399 static bool RecordImageClassesVisitor(Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700400 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800401 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
402 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500403 return true;
404 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800405 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800406 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500407 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500408
Brian Carlstromae826982011-11-09 01:33:42 -0800409 // Appends to dex_files any elements of class_path that it doesn't already
410 // contain. This will open those dex files as necessary.
411 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
412 std::vector<std::string> parsed;
413 Split(class_path, ':', parsed);
414 for (size_t i = 0; i < parsed.size(); ++i) {
415 if (DexFilesContains(dex_files, parsed[i])) {
416 continue;
417 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800418 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800419 if (dex_file == NULL) {
420 LOG(WARNING) << "Failed to open dex file " << parsed[i];
421 } else {
422 dex_files.push_back(dex_file);
423 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500424 }
425 }
Brian Carlstromae826982011-11-09 01:33:42 -0800426
427 // Returns true if dex_files has a dex with the named location.
428 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
429 for (size_t i = 0; i < dex_files.size(); ++i) {
430 if (dex_files[i]->GetLocation() == location) {
431 return true;
432 }
433 }
434 return false;
435 }
436
buzbeec531cef2012-10-18 07:09:20 -0700437 const CompilerBackend compiler_backend_;
438
Ian Rogers49c48942012-03-11 15:15:37 -0700439 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800440
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800441 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800442 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800443 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800444 uint64_t start_ns_;
445
Brian Carlstromae826982011-11-09 01:33:42 -0800446 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
447};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500448
Elliott Hughes72395bf2012-04-24 13:45:26 -0700449static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800450 char* end;
451 int result = strtol(in, &end, 10);
452 if (in == end || *end != '\0') {
453 return false;
454 }
455 *out = result;
456 return true;
457}
458
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700459static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
460 const std::vector<const char*>& dex_locations,
461 std::vector<const DexFile*>& dex_files) {
462 size_t failure_count = 0;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800463 for (size_t i = 0; i < dex_filenames.size(); i++) {
464 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800465 const char* dex_location = dex_locations[i];
466 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800467 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800468 LOG(WARNING) << "could not open .dex from file " << dex_filename;
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700469 ++failure_count;
jeffhao60f83e32012-02-13 17:16:30 -0800470 } else {
471 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800472 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800473 }
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700474 return failure_count;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800475}
476
Elliott Hughes72395bf2012-04-24 13:45:26 -0700477static int dex2oat(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -0700478 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -0700479
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700480 // Skip over argv[0].
481 argv++;
482 argc--;
483
484 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800485 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700486 }
487
488 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800489 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800490 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800491 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700492 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800493 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800494 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800495 std::string bitcode_filename;
Brian Carlstromae826982011-11-09 01:33:42 -0800496 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800497 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800498 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700499 uintptr_t image_base = 0;
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700500 UniquePtr<std::string> host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700501 std::vector<const char*> runtime_args;
Elliott Hughes5c599942012-06-13 16:45:05 -0700502 int thread_count = sysconf(_SC_NPROCESSORS_CONF);
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800503 bool support_debugging = false;
buzbeec531cef2012-10-18 07:09:20 -0700504#if defined(ART_USE_PORTABLE_COMPILER)
505 CompilerBackend compiler_backend = kPortable;
506#elif defined(ART_USE_LLVM_COMPILER)
507 CompilerBackend compiler_backend = kIceland;
508#else
509 CompilerBackend compiler_backend = kQuick;
510#endif
jeffhao9ad4f222012-05-30 18:51:19 -0700511#if defined(__arm__)
Ian Rogers49c48942012-03-11 15:15:37 -0700512 InstructionSet instruction_set = kThumb2;
jeffhao9ad4f222012-05-30 18:51:19 -0700513#elif defined(__i386__)
514 InstructionSet instruction_set = kX86;
515#elif defined(__mips__)
516 InstructionSet instruction_set = kMips;
517#else
518#error "Unsupported architecture"
519#endif
Elliott Hughes67d92002012-03-26 15:08:51 -0700520 bool dump_stats = kIsDebugBuild;
521 bool dump_timings = kIsDebugBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700522
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700523 for (int i = 0; i < argc; i++) {
524 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800525 bool log_options = false;
526 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700527 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
528 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700529 if (option.starts_with("--dex-file=")) {
530 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800531 } else if (option.starts_with("--dex-location=")) {
532 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800533 } else if (option.starts_with("--zip-fd=")) {
534 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800535 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800536 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800537 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800538 } else if (option.starts_with("--zip-location=")) {
539 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800540 } else if (option.starts_with("--oat-file=")) {
541 oat_filename = option.substr(strlen("--oat-file=")).data();
542 } else if (option.starts_with("--oat-fd=")) {
543 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800544 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800545 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800546 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800547 } else if (option.starts_with("-g")) {
548 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800549 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800550 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800551 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800552 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800553 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800554 } else if (option.starts_with("--oat-location=")) {
555 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800556 } else if (option.starts_with("--bitcode=")) {
557 bitcode_filename = option.substr(strlen("--bitcode=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700558 } else if (option.starts_with("--image=")) {
559 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800560 } else if (option.starts_with("--image-classes=")) {
561 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700562 } else if (option.starts_with("--base=")) {
563 const char* image_base_str = option.substr(strlen("--base=")).data();
564 char* end;
565 image_base = strtoul(image_base_str, &end, 16);
566 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800567 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700568 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700569 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800570 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700571 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700572 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Ian Rogers49c48942012-03-11 15:15:37 -0700573 } else if (option.starts_with("--instruction-set=")) {
574 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
jeffhao1f71ae82012-05-24 16:08:24 -0700575 if (instruction_set_str == "arm") {
Ian Rogers49c48942012-03-11 15:15:37 -0700576 instruction_set = kThumb2;
jeffhao1f71ae82012-05-24 16:08:24 -0700577 } else if (instruction_set_str == "mips") {
Ian Rogers49c48942012-03-11 15:15:37 -0700578 instruction_set = kMips;
jeffhao1f71ae82012-05-24 16:08:24 -0700579 } else if (instruction_set_str == "x86") {
Ian Rogers49c48942012-03-11 15:15:37 -0700580 instruction_set = kX86;
581 }
buzbeec531cef2012-10-18 07:09:20 -0700582 } else if (option.starts_with("--compiler-backend=")) {
583 StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data();
584 if (backend_str == "Quick") {
585 compiler_backend = kQuick;
586 } else if (backend_str == "QuickGBC") {
587 compiler_backend = kQuickGBC;
588 } else if (backend_str == "Iceland") {
589 // TODO: remove this when Portable/Iceland merge complete
590 compiler_backend = kIceland;
591 } else if (backend_str == "Portable") {
592 compiler_backend = kPortable;
593 }
jeffhao5d840402011-10-24 17:09:45 -0700594 } else if (option == "--runtime-arg") {
595 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800596 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700597 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800598 if (log_options) {
599 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
600 }
jeffhao5d840402011-10-24 17:09:45 -0700601 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700602 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800603 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700604 }
605 }
606
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800607 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800608 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800609 }
610
611 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800612 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800613 }
614
615 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800616 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800617 }
618
Brian Carlstroma004aa92012-02-08 18:05:09 -0800619 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800620 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700621 }
622
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700623 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800624 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
625 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700626 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800627 }
628 }
629
Brian Carlstroma004aa92012-02-08 18:05:09 -0800630 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800631 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700632 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800633 boot_image_filename += GetAndroidRoot();
634 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700635 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800636 boot_image_filename += "/system";
637 }
638 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800639 }
640 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800641 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800642 boot_image_option += "-Ximage:";
643 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700644 }
645
Brian Carlstromae826982011-11-09 01:33:42 -0800646 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800647 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800648 }
649
650 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800651 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700652 }
653
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800654 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800655 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800656 }
657
658 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800659 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800660 }
661
Brian Carlstroma004aa92012-02-08 18:05:09 -0800662 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800663 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800664 }
665
Brian Carlstroma004aa92012-02-08 18:05:09 -0800666 if (dex_locations.empty()) {
667 for (size_t i = 0; i < dex_filenames.size(); i++) {
668 dex_locations.push_back(dex_filenames[i]);
669 }
670 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800671 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800672 }
673
674 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800675 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800676 }
677
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700678 if (boot_image_option.empty()) {
679 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800680 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700681 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700682 }
683
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800684 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800685 UniquePtr<File> oat_file;
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700686 bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
687 if (create_file) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800688 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800689 if (oat_location.empty()) {
690 oat_location = oat_filename;
691 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800692 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800693 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800694 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800695 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700696 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
697 return EXIT_FAILURE;
698 }
699 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
700 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800701 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700702 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800703
Brian Carlstroma004aa92012-02-08 18:05:09 -0800704 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700705
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700706 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700707 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800708 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700709 if (boot_image_option.empty()) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700710 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
711 if (failure_count > 0) {
712 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
713 return EXIT_FAILURE;
714 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800715 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700716 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700717 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
718 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700719 if (host_prefix.get() != NULL) {
720 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700721 }
jeffhao5d840402011-10-24 17:09:45 -0700722 for (size_t i = 0; i < runtime_args.size(); i++) {
723 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
724 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700725
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 Dex2Oat* p_dex2oat;
buzbeec531cef2012-10-18 07:09:20 -0700727 if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count, support_debugging)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 LOG(ERROR) << "Failed to create dex2oat";
729 return EXIT_FAILURE;
730 }
731 UniquePtr<Dex2Oat> dex2oat(p_dex2oat);
732 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
733 // give it away now and then switch to a more managable ScopedObjectAccess.
734 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
735 // Whilst we're in native take the opportunity to initialize well known classes.
736 WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv());
737 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700738
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800739 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800740 UniquePtr<const std::set<std::string> > image_classes(NULL);
741 if (image_classes_filename != NULL) {
742 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
743 if (image_classes.get() == NULL) {
744 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
745 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700746 }
747 }
748
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800749 std::vector<const DexFile*> dex_files;
750 if (boot_image_option.empty()) {
751 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
752 } else {
753 if (dex_filenames.empty()) {
754 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
755 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800756 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800757 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800758 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800759 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800760 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800761 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800762 return EXIT_FAILURE;
763 }
764 dex_files.push_back(dex_file);
765 } else {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700766 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
767 if (failure_count > 0) {
768 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
769 return EXIT_FAILURE;
770 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800771 }
772 }
773
Brian Carlstromf5822582012-03-19 22:34:31 -0700774 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700775 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700776 dex_files,
777 oat_file.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700778 bitcode_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700779 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700780 image_classes.get(),
781 dump_stats,
782 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700783
784 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800785 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700786 return EXIT_FAILURE;
787 }
788
Brian Carlstromae826982011-11-09 01:33:42 -0800789 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800790 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700791 return EXIT_SUCCESS;
792 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700793
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
795 bool image_creation_success = dex2oat->CreateImageFile(image_filename,
796 image_base,
797 image_classes.get(),
798 oat_filename,
799 oat_location,
800 *compiler.get());
801 Thread::Current()->TransitionFromSuspendedToRunnable();
802 if (!image_creation_success) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700803 return EXIT_FAILURE;
804 }
805
Brian Carlstromae826982011-11-09 01:33:42 -0800806 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800807 LOG(INFO) << "Oat file written successfully: " << oat_filename;
808 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700809 return EXIT_SUCCESS;
810}
811
812} // namespace art
813
814int main(int argc, char** argv) {
815 return art::dex2oat(argc, argv);
816}