blob: 20872e1aa4a06ff62c362039d34d7801bbc0e621 [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#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +080087 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
88 UsageError(" Example: --bitcode=/system/framework/boot.bc");
89 UsageError("");
90#endif
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080091 UsageError(" --image=<file.art>: specifies the output image filename.");
92 UsageError(" Example: --image=/system/framework/boot.art");
93 UsageError("");
94 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
95 UsageError(" Example: --image=frameworks/base/preloaded-classes");
96 UsageError("");
97 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
98 UsageError(" Example: --base=0x50000000");
99 UsageError("");
100 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
101 UsageError(" Example: --boot-image=/system/framework/boot.art");
102 UsageError(" Default: <host-prefix>/system/framework/boot.art");
103 UsageError("");
104 UsageError(" --host-prefix may be used to translate host paths to target paths during");
105 UsageError(" cross compilation.");
106 UsageError(" Example: --host-prefix=out/target/product/crespo");
107 UsageError(" Default: $ANDROID_PRODUCT_OUT");
108 UsageError("");
jeffhao1f71ae82012-05-24 16:08:24 -0700109 UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction");
Ian Rogers49c48942012-03-11 15:15:37 -0700110 UsageError(" set.");
jeffhao1f71ae82012-05-24 16:08:24 -0700111 UsageError(" Example: --instruction-set=x86");
112 UsageError(" Default: arm");
Ian Rogers49c48942012-03-11 15:15:37 -0700113 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800114 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
115 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
116 UsageError(" Use a separate --runtime-arg switch for each argument.");
117 UsageError(" Example: --runtime-arg -Xms256m");
118 UsageError("");
119 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700120 exit(EXIT_FAILURE);
121}
122
Brian Carlstromae826982011-11-09 01:33:42 -0800123class Dex2Oat {
124 public:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, InstructionSet instruction_set,
126 size_t thread_count, bool support_debugging)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 if (!CreateRuntime(options, instruction_set)) {
129 *p_dex2oat = NULL;
130 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800131 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 *p_dex2oat = new Dex2Oat(Runtime::Current(), instruction_set, thread_count, support_debugging);
133 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800134 }
135
136 ~Dex2Oat() {
137 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800138 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800139 }
140
141 // Make a list of descriptors for classes to include in the image
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800144 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
145 if (image_classes_file.get() == NULL) {
146 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
147 return NULL;
148 }
149
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800150 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800151 ClassLinker* class_linker = runtime_->GetClassLinker();
Ian Rogers1f539342012-10-03 21:09:42 -0700152 Thread* self = Thread::Current();
Brian Carlstromae826982011-11-09 01:33:42 -0800153 while (image_classes_file->good()) {
154 std::string dot;
155 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800156 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800157 continue;
158 }
Elliott Hughes95572412011-12-13 18:14:20 -0800159 std::string descriptor(DotToDescriptor(dot.c_str()));
Ian Rogers1f539342012-10-03 21:09:42 -0700160 SirtRef<Class> klass(self, class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800161 if (klass.get() == NULL) {
162 LOG(WARNING) << "Failed to find class " << descriptor;
163 Thread::Current()->ClearException();
164 }
165 }
166 image_classes_file->close();
167
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800168 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
169 // exceptions are resolved by the verifier when there is a catch block in an interested method.
170 // Do this here so that exception classes appear to have been specified image classes.
171 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
Ian Rogers1f539342012-10-03 21:09:42 -0700172 SirtRef<Class> java_lang_Throwable(self,
173 class_linker->FindSystemClass("Ljava/lang/Throwable;"));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800174 do {
175 unresolved_exception_types.clear();
176 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
177 &unresolved_exception_types);
178 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
179 for (It it = unresolved_exception_types.begin(),
180 end = unresolved_exception_types.end();
181 it != end; ++it) {
182 uint16_t exception_type_idx = it->first;
183 const DexFile* dex_file = it->second;
184 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
185 ClassLoader* class_loader = NULL;
Ian Rogers1f539342012-10-03 21:09:42 -0700186 SirtRef<Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx,
187 dex_cache, class_loader));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800188 if (klass.get() == NULL) {
189 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
190 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
191 LOG(FATAL) << "Failed to resolve class " << descriptor;
192 }
Elliott Hughes35be9b12012-05-29 17:59:26 -0700193 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get()));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800194 }
195 // Resolving exceptions may load classes that reference more exceptions, iterate until no
196 // more are found
197 } while (!unresolved_exception_types.empty());
198
Brian Carlstromae826982011-11-09 01:33:42 -0800199 // We walk the roots looking for classes so that we'll pick up the
200 // above classes plus any classes them depend on such super
201 // classes, interfaces, and the required ClassLinker roots.
202 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800203 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800204 CHECK_NE(image_classes->size(), 0U);
205 return image_classes.release();
206 }
207
Brian Carlstromf5822582012-03-19 22:34:31 -0700208 const Compiler* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700209 const std::string* host_prefix,
Brian Carlstromf5822582012-03-19 22:34:31 -0700210 const std::vector<const DexFile*>& dex_files,
211 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800212#if defined(ART_USE_LLVM_COMPILER)
Logan Chiende08e842012-03-21 00:34:12 +0800213 const std::string& bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800214#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700215 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700216 const std::set<std::string>* image_classes,
217 bool dump_stats,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 bool dump_timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800220 // SirtRef and ClassLoader creation needs to come after Runtime::Create
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 jobject class_loader = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800222 if (!boot_image_option.empty()) {
223 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800224 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800225 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800226 for (size_t i = 0; i < class_path_files.size(); i++) {
227 class_linker->RegisterDexFile(*class_path_files[i]);
228 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 ScopedObjectAccessUnchecked soa(Thread::Current());
230 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
231 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
232 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
233 class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
234 Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800235 }
236
Brian Carlstromf5822582012-03-19 22:34:31 -0700237 UniquePtr<Compiler> compiler(new Compiler(instruction_set_,
238 image,
239 thread_count_,
240 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700241 image_classes,
242 dump_stats,
243 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800244
245#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700246 compiler->SetBitcodeFileName(bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +0800247#endif
248
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
250
251 compiler->CompileAll(class_loader, dex_files);
252
253 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstromae826982011-11-09 01:33:42 -0800254
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700255 std::string image_file_location;
Brian Carlstrom28db0122012-10-18 16:20:41 -0700256 uint32_t image_file_location_oat_checksum = 0;
257 uint32_t image_file_location_oat_begin = 0;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700258 Heap* heap = Runtime::Current()->GetHeap();
259 if (heap->GetSpaces().size() > 1) {
260 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700261 image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
262 image_file_location_oat_begin = reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700263 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700264 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
265 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700266 }
267 }
268
269 if (!OatWriter::Create(oat_file,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 class_loader,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700271 dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700272 image_file_location_oat_checksum,
273 image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700274 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700275 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800276 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700277 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800278 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700279 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800280 }
281
Brian Carlstroma004aa92012-02-08 18:05:09 -0800282 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800283 uintptr_t image_base,
284 const std::set<std::string>* image_classes,
285 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700286 const std::string& oat_location,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 const Compiler& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700288 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800289 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700290 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800291 LOG(ERROR) << "Failed to create image file " << image_filename;
292 return false;
293 }
294 return true;
295 }
296
297 private:
Ian Rogers49c48942012-03-11 15:15:37 -0700298 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
299 bool support_debugging)
300 : instruction_set_(instruction_set),
301 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800302 thread_count_(thread_count),
303 support_debugging_(support_debugging),
304 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800305 }
Brian Carlstromae826982011-11-09 01:33:42 -0800306
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700309 if (!Runtime::Create(options, false)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800310 LOG(ERROR) << "Failed to create runtime";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800312 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 Runtime* runtime = Runtime::Current();
Brian Carlstromae826982011-11-09 01:33:42 -0800314 // if we loaded an existing image, we will reuse values from the image roots.
315 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700316 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800317 }
318 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700319 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800320 }
321 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
322 Runtime::TrampolineType type = Runtime::TrampolineType(i);
323 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700324 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800325 }
326 }
Ian Rogers19846512012-02-24 11:42:47 -0800327 if (!runtime->HasResolutionMethod()) {
328 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
329 }
Brian Carlstromae826982011-11-09 01:33:42 -0800330 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
331 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
332 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700333 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800334 }
335 }
Ian Rogers19846512012-02-24 11:42:47 -0800336 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800338 }
339
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800340 static void ResolveExceptionsForMethod(MethodHelper* mh,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800343 const DexFile::CodeItem* code_item = mh->GetCodeItem();
344 if (code_item == NULL) {
345 return; // native or abstract method
346 }
347 if (code_item->tries_size_ == 0) {
348 return; // nothing to process
349 }
350 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
351 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
352 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
353 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
354 bool has_catch_all = false;
355 if (encoded_catch_handler_size <= 0) {
356 encoded_catch_handler_size = -encoded_catch_handler_size;
357 has_catch_all = true;
358 }
359 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
360 uint16_t encoded_catch_handler_handlers_type_idx =
361 DecodeUnsignedLeb128(&encoded_catch_handler_list);
362 // Add to set of types to resolve if not already in the dex cache resolved types
363 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
364 exceptions_to_resolve.insert(
365 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
366 &mh->GetDexFile()));
367 }
368 // ignore address associated with catch handler
369 DecodeUnsignedLeb128(&encoded_catch_handler_list);
370 }
371 if (has_catch_all) {
372 // ignore catch all address
373 DecodeUnsignedLeb128(&encoded_catch_handler_list);
374 }
375 }
376 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377
378 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700379 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800380 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
381 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
382 MethodHelper mh;
383 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700384 AbstractMethod* m = c->GetVirtualMethod(i);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800385 mh.ChangeMethod(m);
386 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
387 }
388 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700389 AbstractMethod* m = c->GetDirectMethod(i);
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800390 mh.ChangeMethod(m);
391 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
392 }
393 return true;
394 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395
396 static bool RecordImageClassesVisitor(Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700397 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800398 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
399 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500400 return true;
401 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800402 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800403 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500404 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500405
Brian Carlstromae826982011-11-09 01:33:42 -0800406 // Appends to dex_files any elements of class_path that it doesn't already
407 // contain. This will open those dex files as necessary.
408 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
409 std::vector<std::string> parsed;
410 Split(class_path, ':', parsed);
411 for (size_t i = 0; i < parsed.size(); ++i) {
412 if (DexFilesContains(dex_files, parsed[i])) {
413 continue;
414 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800415 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800416 if (dex_file == NULL) {
417 LOG(WARNING) << "Failed to open dex file " << parsed[i];
418 } else {
419 dex_files.push_back(dex_file);
420 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500421 }
422 }
Brian Carlstromae826982011-11-09 01:33:42 -0800423
424 // Returns true if dex_files has a dex with the named location.
425 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
426 for (size_t i = 0; i < dex_files.size(); ++i) {
427 if (dex_files[i]->GetLocation() == location) {
428 return true;
429 }
430 }
431 return false;
432 }
433
Ian Rogers49c48942012-03-11 15:15:37 -0700434 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800435
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800436 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800437 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800438 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800439 uint64_t start_ns_;
440
Brian Carlstromae826982011-11-09 01:33:42 -0800441 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
442};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500443
Elliott Hughes72395bf2012-04-24 13:45:26 -0700444static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800445 char* end;
446 int result = strtol(in, &end, 10);
447 if (in == end || *end != '\0') {
448 return false;
449 }
450 *out = result;
451 return true;
452}
453
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700454static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
455 const std::vector<const char*>& dex_locations,
456 std::vector<const DexFile*>& dex_files) {
457 size_t failure_count = 0;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800458 for (size_t i = 0; i < dex_filenames.size(); i++) {
459 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800460 const char* dex_location = dex_locations[i];
461 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800462 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800463 LOG(WARNING) << "could not open .dex from file " << dex_filename;
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700464 ++failure_count;
jeffhao60f83e32012-02-13 17:16:30 -0800465 } else {
466 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800467 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800468 }
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700469 return failure_count;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800470}
471
Elliott Hughes72395bf2012-04-24 13:45:26 -0700472static int dex2oat(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -0700473 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -0700474
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700475 // Skip over argv[0].
476 argv++;
477 argc--;
478
479 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800480 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700481 }
482
483 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800484 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800485 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800486 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800488 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800489 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800490#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800491 std::string bitcode_filename;
492#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800493 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800494 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800495 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700496 uintptr_t image_base = 0;
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700497 UniquePtr<std::string> host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700498 std::vector<const char*> runtime_args;
Elliott Hughes5c599942012-06-13 16:45:05 -0700499 int thread_count = sysconf(_SC_NPROCESSORS_CONF);
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800500 bool support_debugging = false;
jeffhao9ad4f222012-05-30 18:51:19 -0700501#if defined(__arm__)
Ian Rogers49c48942012-03-11 15:15:37 -0700502 InstructionSet instruction_set = kThumb2;
jeffhao9ad4f222012-05-30 18:51:19 -0700503#elif defined(__i386__)
504 InstructionSet instruction_set = kX86;
505#elif defined(__mips__)
506 InstructionSet instruction_set = kMips;
507#else
508#error "Unsupported architecture"
509#endif
Elliott Hughes67d92002012-03-26 15:08:51 -0700510 bool dump_stats = kIsDebugBuild;
511 bool dump_timings = kIsDebugBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700512
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700513 for (int i = 0; i < argc; i++) {
514 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800515 bool log_options = false;
516 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700517 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
518 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700519 if (option.starts_with("--dex-file=")) {
520 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800521 } else if (option.starts_with("--dex-location=")) {
522 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800523 } else if (option.starts_with("--zip-fd=")) {
524 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800525 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800526 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800527 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800528 } else if (option.starts_with("--zip-location=")) {
529 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800530 } else if (option.starts_with("--oat-file=")) {
531 oat_filename = option.substr(strlen("--oat-file=")).data();
532 } else if (option.starts_with("--oat-fd=")) {
533 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800534 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800535 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800536 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800537 } else if (option.starts_with("-g")) {
538 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800539 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800540 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800541 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800542 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800543 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800544 } else if (option.starts_with("--oat-location=")) {
545 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800546#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800547 } else if (option.starts_with("--bitcode=")) {
548 bitcode_filename = option.substr(strlen("--bitcode=")).data();
549#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700550 } else if (option.starts_with("--image=")) {
551 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800552 } else if (option.starts_with("--image-classes=")) {
553 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700554 } else if (option.starts_with("--base=")) {
555 const char* image_base_str = option.substr(strlen("--base=")).data();
556 char* end;
557 image_base = strtoul(image_base_str, &end, 16);
558 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800559 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700560 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700561 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800562 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700563 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700564 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Ian Rogers49c48942012-03-11 15:15:37 -0700565 } else if (option.starts_with("--instruction-set=")) {
566 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
jeffhao1f71ae82012-05-24 16:08:24 -0700567 if (instruction_set_str == "arm") {
Ian Rogers49c48942012-03-11 15:15:37 -0700568 instruction_set = kThumb2;
jeffhao1f71ae82012-05-24 16:08:24 -0700569 } else if (instruction_set_str == "mips") {
Ian Rogers49c48942012-03-11 15:15:37 -0700570 instruction_set = kMips;
jeffhao1f71ae82012-05-24 16:08:24 -0700571 } else if (instruction_set_str == "x86") {
Ian Rogers49c48942012-03-11 15:15:37 -0700572 instruction_set = kX86;
573 }
jeffhao5d840402011-10-24 17:09:45 -0700574 } else if (option == "--runtime-arg") {
575 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800576 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700577 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800578 if (log_options) {
579 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
580 }
jeffhao5d840402011-10-24 17:09:45 -0700581 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700582 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800583 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700584 }
585 }
586
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800587 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800588 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800589 }
590
591 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800592 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800593 }
594
595 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800596 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800597 }
598
Brian Carlstroma004aa92012-02-08 18:05:09 -0800599 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800600 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700601 }
602
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700603 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800604 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
605 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700606 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800607 }
608 }
609
Brian Carlstroma004aa92012-02-08 18:05:09 -0800610 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800611 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700612 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800613 boot_image_filename += GetAndroidRoot();
614 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700615 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800616 boot_image_filename += "/system";
617 }
618 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800619 }
620 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800621 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800622 boot_image_option += "-Ximage:";
623 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700624 }
625
Brian Carlstromae826982011-11-09 01:33:42 -0800626 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800627 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800628 }
629
630 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800631 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700632 }
633
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800634 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800635 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800636 }
637
638 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800639 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800640 }
641
Brian Carlstroma004aa92012-02-08 18:05:09 -0800642 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800643 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800644 }
645
Brian Carlstroma004aa92012-02-08 18:05:09 -0800646 if (dex_locations.empty()) {
647 for (size_t i = 0; i < dex_filenames.size(); i++) {
648 dex_locations.push_back(dex_filenames[i]);
649 }
650 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800651 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800652 }
653
654 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800655 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800656 }
657
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700658 if (boot_image_option.empty()) {
659 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800660 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700661 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700662 }
663
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800664 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800665 UniquePtr<File> oat_file;
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700666 bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
667 if (create_file) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800668 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800669 if (oat_location.empty()) {
670 oat_location = oat_filename;
671 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800672 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800673 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800674 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800675 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700676 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
677 return EXIT_FAILURE;
678 }
679 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
680 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800681 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700682 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800683
Brian Carlstroma004aa92012-02-08 18:05:09 -0800684 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700685
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700686 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700687 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800688 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700689 if (boot_image_option.empty()) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700690 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
691 if (failure_count > 0) {
692 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
693 return EXIT_FAILURE;
694 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800695 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700696 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700697 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
698 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700699 if (host_prefix.get() != NULL) {
700 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700701 }
jeffhao5d840402011-10-24 17:09:45 -0700702 for (size_t i = 0; i < runtime_args.size(); i++) {
703 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
704 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700705
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 Dex2Oat* p_dex2oat;
707 if (!Dex2Oat::Create(&p_dex2oat, options, instruction_set, thread_count, support_debugging)) {
708 LOG(ERROR) << "Failed to create dex2oat";
709 return EXIT_FAILURE;
710 }
711 UniquePtr<Dex2Oat> dex2oat(p_dex2oat);
712 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
713 // give it away now and then switch to a more managable ScopedObjectAccess.
714 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
715 // Whilst we're in native take the opportunity to initialize well known classes.
716 WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv());
717 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700718
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800719 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800720 UniquePtr<const std::set<std::string> > image_classes(NULL);
721 if (image_classes_filename != NULL) {
722 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
723 if (image_classes.get() == NULL) {
724 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
725 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700726 }
727 }
728
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800729 std::vector<const DexFile*> dex_files;
730 if (boot_image_option.empty()) {
731 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
732 } else {
733 if (dex_filenames.empty()) {
734 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
735 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800736 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800737 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800738 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800739 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800740 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800741 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800742 return EXIT_FAILURE;
743 }
744 dex_files.push_back(dex_file);
745 } else {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700746 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
747 if (failure_count > 0) {
748 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
749 return EXIT_FAILURE;
750 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800751 }
752 }
753
Brian Carlstromf5822582012-03-19 22:34:31 -0700754 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700755 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700756 dex_files,
757 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800758#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700759 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800760#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700761 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700762 image_classes.get(),
763 dump_stats,
764 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700765
766 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800767 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700768 return EXIT_FAILURE;
769 }
770
Brian Carlstromae826982011-11-09 01:33:42 -0800771 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800772 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700773 return EXIT_SUCCESS;
774 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700775
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700776 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
777 bool image_creation_success = dex2oat->CreateImageFile(image_filename,
778 image_base,
779 image_classes.get(),
780 oat_filename,
781 oat_location,
782 *compiler.get());
783 Thread::Current()->TransitionFromSuspendedToRunnable();
784 if (!image_creation_success) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700785 return EXIT_FAILURE;
786 }
787
Brian Carlstromae826982011-11-09 01:33:42 -0800788 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800789 LOG(INFO) << "Oat file written successfully: " << oat_filename;
790 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700791 return EXIT_SUCCESS;
792}
793
794} // namespace art
795
796int main(int argc, char** argv) {
797 return art::dex2oat(argc, argv);
798}