blob: f75aeb7c74eeb431d9d78564f1262642b4e97bb9 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstromae826982011-11-09 01:33:42 -080020#include <iostream>
21#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070022#include <string>
23#include <vector>
24
25#include "class_linker.h"
26#include "class_loader.h"
27#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070028#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070029#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080030#include "leb128.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070033#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070034#include "runtime.h"
Brian Carlstroma004aa92012-02-08 18:05:09 -080035#include "stl_util.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070036#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080037#include "timing_logger.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080038#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070039
40namespace art {
41
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080042static void UsageErrorV(const char* fmt, va_list ap) {
43 std::string error;
44 StringAppendV(&error, fmt, ap);
45 LOG(ERROR) << error;
46}
47
48static void UsageError(const char* fmt, ...) {
49 va_list ap;
50 va_start(ap, fmt);
51 UsageErrorV(fmt, ap);
52 va_end(ap);
53}
54
55static void Usage(const char* fmt, ...) {
56 va_list ap;
57 va_start(ap, fmt);
58 UsageErrorV(fmt, ap);
59 va_end(ap);
60
61 UsageError("Usage: dex2oat [options]...");
62 UsageError("");
63 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
64 UsageError(" Example: --dex-file=/system/framework/core.jar");
65 UsageError("");
66 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
67 UsageError(" containing a classes.dex file to compile.");
68 UsageError(" Example: --zip-fd=5");
69 UsageError("");
70 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
71 UsageError(" to the file descriptor specified by --zip-fd.");
72 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
73 UsageError("");
74 UsageError(" --oat-file=<file.oat>: specifies the required oat filename.");
75 UsageError(" Example: --oat-file=/system/framework/boot.oat");
76 UsageError("");
77 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
78 UsageError(" to the file descriptor specified by --oat-fd.");
79 UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat");
80 UsageError("");
Logan Chien8b977d32012-02-21 19:14:55 +080081#if defined(ART_USE_LLVM_COMPILER)
82 UsageError(" --elf-file=<file.elf>: specifies the required elf filename.");
83 UsageError(" Example: --elf-file=/system/framework/boot.elf");
84 UsageError("");
85 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
86 UsageError(" Example: --bitcode=/system/framework/boot.bc");
87 UsageError("");
88#endif
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("");
Ian Rogers49c48942012-03-11 15:15:37 -0700107 UsageError(" --instruction-set=(ARM|Thumb2|MIPS|X86): compile for a particular instruction");
108 UsageError(" set.");
109 UsageError(" Example: --instruction-set=X86");
110 UsageError(" Default: Thumb2");
111 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800112 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
113 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
114 UsageError(" Use a separate --runtime-arg switch for each argument.");
115 UsageError(" Example: --runtime-arg -Xms256m");
116 UsageError("");
117 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700118 exit(EXIT_FAILURE);
119}
120
Brian Carlstromae826982011-11-09 01:33:42 -0800121class Dex2Oat {
122 public:
123
Ian Rogers49c48942012-03-11 15:15:37 -0700124 static Dex2Oat* Create(Runtime::Options& options, InstructionSet instruction_set,
125 size_t thread_count, bool support_debugging) {
126 UniquePtr<Runtime> runtime(CreateRuntime(options, instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800127 if (runtime.get() == NULL) {
128 return NULL;
129 }
Ian Rogers49c48942012-03-11 15:15:37 -0700130 return new Dex2Oat(runtime.release(), instruction_set, thread_count, support_debugging);
Brian Carlstromae826982011-11-09 01:33:42 -0800131 }
132
133 ~Dex2Oat() {
134 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800135 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800136 }
137
138 // Make a list of descriptors for classes to include in the image
139 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
140 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
141 if (image_classes_file.get() == NULL) {
142 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
143 return NULL;
144 }
145
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800146 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800147 ClassLinker* class_linker = runtime_->GetClassLinker();
148 while (image_classes_file->good()) {
149 std::string dot;
150 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800151 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800152 continue;
153 }
Elliott Hughes95572412011-12-13 18:14:20 -0800154 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800155 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800156 if (klass.get() == NULL) {
157 LOG(WARNING) << "Failed to find class " << descriptor;
158 Thread::Current()->ClearException();
159 }
160 }
161 image_classes_file->close();
162
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800163 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
164 // exceptions are resolved by the verifier when there is a catch block in an interested method.
165 // Do this here so that exception classes appear to have been specified image classes.
166 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
167 do {
168 unresolved_exception_types.clear();
169 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
170 &unresolved_exception_types);
171 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
172 for (It it = unresolved_exception_types.begin(),
173 end = unresolved_exception_types.end();
174 it != end; ++it) {
175 uint16_t exception_type_idx = it->first;
176 const DexFile* dex_file = it->second;
177 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
178 ClassLoader* class_loader = NULL;
179 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
180 class_loader));
181 if (klass.get() == NULL) {
182 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
183 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
184 LOG(FATAL) << "Failed to resolve class " << descriptor;
185 }
186 DCHECK(klass->IsThrowableClass());
187 }
188 // Resolving exceptions may load classes that reference more exceptions, iterate until no
189 // more are found
190 } while (!unresolved_exception_types.empty());
191
Brian Carlstromae826982011-11-09 01:33:42 -0800192 // We walk the roots looking for classes so that we'll pick up the
193 // above classes plus any classes them depend on such super
194 // classes, interfaces, and the required ClassLinker roots.
195 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800196 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800197 CHECK_NE(image_classes->size(), 0U);
198 return image_classes.release();
199 }
200
201 bool CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700202 const std::string& host_prefix,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800203 const std::vector<const DexFile*>& dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800204 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800205#if defined(ART_USE_LLVM_COMPILER)
206 std::string const& elf_filename,
207 std::string const& bitcode_filename,
208#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800209 bool image,
210 const std::set<std::string>* image_classes) {
211 // SirtRef and ClassLoader creation needs to come after Runtime::Create
212 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
213 if (class_loader.get() == NULL) {
214 LOG(ERROR) << "Failed to create SirtRef for class loader";
215 return false;
216 }
217
Brian Carlstromae826982011-11-09 01:33:42 -0800218 if (!boot_image_option.empty()) {
219 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800220 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800221 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800222 for (size_t i = 0; i < class_path_files.size(); i++) {
223 class_linker->RegisterDexFile(*class_path_files[i]);
224 }
225 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800226 }
227
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800228 Compiler compiler(instruction_set_, image, thread_count_, support_debugging_, image_classes);
Logan Chien8b977d32012-02-21 19:14:55 +0800229
230#if defined(ART_USE_LLVM_COMPILER)
231 compiler.SetElfFileName(elf_filename);
232 compiler.SetBitcodeFileName(bitcode_filename);
233#endif
234
Brian Carlstromae826982011-11-09 01:33:42 -0800235 compiler.CompileAll(class_loader->get(), dex_files);
236
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700237 std::string image_file_location;
238 uint32_t image_file_location_checksum = 0;
239 Heap* heap = Runtime::Current()->GetHeap();
240 if (heap->GetSpaces().size() > 1) {
241 ImageSpace* image_space = heap->GetImageSpace();
242 image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum();
243 image_file_location = image_space->GetImageFilename();
244 if (!host_prefix.empty() && StartsWith(image_file_location, host_prefix.c_str())) {
245 image_file_location = image_file_location.substr(host_prefix.size());
246 }
247 }
248
249 if (!OatWriter::Create(oat_file,
250 class_loader->get(),
251 dex_files,
252 image_file_location_checksum,
253 image_file_location,
254 compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800255 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
256 return false;
257 }
258 return true;
259 }
260
Brian Carlstroma004aa92012-02-08 18:05:09 -0800261 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800262 uintptr_t image_base,
263 const std::set<std::string>* image_classes,
264 const std::string& oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800265 const std::string& oat_location) {
Brian Carlstromae826982011-11-09 01:33:42 -0800266 ImageWriter image_writer(image_classes);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800267 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800268 LOG(ERROR) << "Failed to create image file " << image_filename;
269 return false;
270 }
271 return true;
272 }
273
274 private:
275
Ian Rogers49c48942012-03-11 15:15:37 -0700276 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
277 bool support_debugging)
278 : instruction_set_(instruction_set),
279 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800280 thread_count_(thread_count),
281 support_debugging_(support_debugging),
282 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800283 }
Brian Carlstromae826982011-11-09 01:33:42 -0800284
Ian Rogers49c48942012-03-11 15:15:37 -0700285 static Runtime* CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) {
Brian Carlstromae826982011-11-09 01:33:42 -0800286 Runtime* runtime = Runtime::Create(options, false);
287 if (runtime == NULL) {
288 LOG(ERROR) << "Failed to create runtime";
289 return NULL;
290 }
291
292 // if we loaded an existing image, we will reuse values from the image roots.
293 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700294 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800295 }
296 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700297 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800298 }
299 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
300 Runtime::TrampolineType type = Runtime::TrampolineType(i);
301 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700302 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800303 }
304 }
Ian Rogers19846512012-02-24 11:42:47 -0800305 if (!runtime->HasResolutionMethod()) {
306 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
307 }
Brian Carlstromae826982011-11-09 01:33:42 -0800308 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
309 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
310 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700311 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800312 }
313 }
Ian Rogers19846512012-02-24 11:42:47 -0800314 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800315 return runtime;
316 }
317
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800318 static void ResolveExceptionsForMethod(MethodHelper* mh,
319 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
320 const DexFile::CodeItem* code_item = mh->GetCodeItem();
321 if (code_item == NULL) {
322 return; // native or abstract method
323 }
324 if (code_item->tries_size_ == 0) {
325 return; // nothing to process
326 }
327 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
328 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
329 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
330 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
331 bool has_catch_all = false;
332 if (encoded_catch_handler_size <= 0) {
333 encoded_catch_handler_size = -encoded_catch_handler_size;
334 has_catch_all = true;
335 }
336 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
337 uint16_t encoded_catch_handler_handlers_type_idx =
338 DecodeUnsignedLeb128(&encoded_catch_handler_list);
339 // Add to set of types to resolve if not already in the dex cache resolved types
340 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
341 exceptions_to_resolve.insert(
342 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
343 &mh->GetDexFile()));
344 }
345 // ignore address associated with catch handler
346 DecodeUnsignedLeb128(&encoded_catch_handler_list);
347 }
348 if (has_catch_all) {
349 // ignore catch all address
350 DecodeUnsignedLeb128(&encoded_catch_handler_list);
351 }
352 }
353 }
354 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
355 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
356 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
357 MethodHelper mh;
358 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
359 Method* m = c->GetVirtualMethod(i);
360 mh.ChangeMethod(m);
361 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
362 }
363 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
364 Method* m = c->GetDirectMethod(i);
365 mh.ChangeMethod(m);
366 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
367 }
368 return true;
369 }
370 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800371 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
372 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500373 return true;
374 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800375 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800376 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500377 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500378
Brian Carlstromae826982011-11-09 01:33:42 -0800379 // Appends to dex_files any elements of class_path that it doesn't already
380 // contain. This will open those dex files as necessary.
381 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
382 std::vector<std::string> parsed;
383 Split(class_path, ':', parsed);
384 for (size_t i = 0; i < parsed.size(); ++i) {
385 if (DexFilesContains(dex_files, parsed[i])) {
386 continue;
387 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800388 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800389 if (dex_file == NULL) {
390 LOG(WARNING) << "Failed to open dex file " << parsed[i];
391 } else {
392 dex_files.push_back(dex_file);
393 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500394 }
395 }
Brian Carlstromae826982011-11-09 01:33:42 -0800396
397 // Returns true if dex_files has a dex with the named location.
398 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
399 for (size_t i = 0; i < dex_files.size(); ++i) {
400 if (dex_files[i]->GetLocation() == location) {
401 return true;
402 }
403 }
404 return false;
405 }
406
Ian Rogers49c48942012-03-11 15:15:37 -0700407 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800408
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800409 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800410 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800411 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800412 uint64_t start_ns_;
413
Brian Carlstromae826982011-11-09 01:33:42 -0800414 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
415};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500416
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800417bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800418 char* end;
419 int result = strtol(in, &end, 10);
420 if (in == end || *end != '\0') {
421 return false;
422 }
423 *out = result;
424 return true;
425}
426
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800427void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800428 const std::vector<const char*>& dex_locations,
429 std::vector<const DexFile*>& dex_files) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800430 for (size_t i = 0; i < dex_filenames.size(); i++) {
431 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800432 const char* dex_location = dex_locations[i];
433 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800434 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800435 LOG(WARNING) << "could not open .dex from file " << dex_filename;
436 } else {
437 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800438 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800439 }
440}
441
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700442int dex2oat(int argc, char** argv) {
443 // Skip over argv[0].
444 argv++;
445 argc--;
446
447 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800448 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700449 }
450
451 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800452 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800453 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800454 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700455 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800456 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800457 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800458#if defined(ART_USE_LLVM_COMPILER)
459 std::string elf_filename;
460 std::string bitcode_filename;
461#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800462 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800463 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800464 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700465 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700466 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700467 std::vector<const char*> runtime_args;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800468 int thread_count = 2;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800469 bool support_debugging = false;
Ian Rogers49c48942012-03-11 15:15:37 -0700470 InstructionSet instruction_set = kThumb2;
Brian Carlstrom16192862011-09-12 17:50:06 -0700471
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700472 for (int i = 0; i < argc; i++) {
473 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800474 bool log_options = false;
475 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700476 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
477 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700478 if (option.starts_with("--dex-file=")) {
479 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800480 } else if (option.starts_with("--dex-location=")) {
481 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800482 } else if (option.starts_with("--zip-fd=")) {
483 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800484 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800485 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800486 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800487 } else if (option.starts_with("--zip-location=")) {
488 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800489 } else if (option.starts_with("--oat-file=")) {
490 oat_filename = option.substr(strlen("--oat-file=")).data();
491 } else if (option.starts_with("--oat-fd=")) {
492 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800493 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800494 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800495 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800496 } else if (option.starts_with("-g")) {
497 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800498 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800499 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800500 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800501 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800502 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800503 } else if (option.starts_with("--oat-location=")) {
504 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800505#if defined(ART_USE_LLVM_COMPILER)
506 } else if (option.starts_with("--elf-file=")) {
507 elf_filename = option.substr(strlen("--elf-file=")).data();
508 } else if (option.starts_with("--bitcode=")) {
509 bitcode_filename = option.substr(strlen("--bitcode=")).data();
510#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700511 } else if (option.starts_with("--image=")) {
512 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800513 } else if (option.starts_with("--image-classes=")) {
514 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700515 } else if (option.starts_with("--base=")) {
516 const char* image_base_str = option.substr(strlen("--base=")).data();
517 char* end;
518 image_base = strtoul(image_base_str, &end, 16);
519 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800520 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700521 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700522 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800523 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700524 } else if (option.starts_with("--host-prefix=")) {
525 host_prefix = option.substr(strlen("--host-prefix=")).data();
Ian Rogers49c48942012-03-11 15:15:37 -0700526 } else if (option.starts_with("--instruction-set=")) {
527 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
528 if (instruction_set_str == "Thumb2" || instruction_set_str == "ARM") {
529 instruction_set = kThumb2;
530 } else if (instruction_set_str == "MIPS") {
531 instruction_set = kMips;
532 } else if (instruction_set_str == "X86") {
533 instruction_set = kX86;
534 }
jeffhao5d840402011-10-24 17:09:45 -0700535 } else if (option == "--runtime-arg") {
536 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800537 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700538 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800539 if (log_options) {
540 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
541 }
jeffhao5d840402011-10-24 17:09:45 -0700542 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700543 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800544 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700545 }
546 }
547
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800548 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800549 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800550 }
551
552 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800553 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800554 }
555
556 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800557 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800558 }
559
Brian Carlstroma004aa92012-02-08 18:05:09 -0800560 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800561 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700562 }
563
Brian Carlstromb0011262011-12-09 12:17:24 -0800564 if (host_prefix.empty()) {
565 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
566 if (android_product_out != NULL) {
567 host_prefix = android_product_out;
568 }
569 }
570
Brian Carlstroma004aa92012-02-08 18:05:09 -0800571 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800572 if (!image && boot_image_filename.empty()) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800573 if (host_prefix.empty()) {
574 boot_image_filename += GetAndroidRoot();
575 } else {
576 boot_image_filename += host_prefix;
577 boot_image_filename += "/system";
578 }
579 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800580 }
581 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800582 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800583 boot_image_option += "-Ximage:";
584 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700585 }
586
Brian Carlstromae826982011-11-09 01:33:42 -0800587 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800588 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800589 }
590
591 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800592 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700593 }
594
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800595 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800596 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800597 }
598
599 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800600 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800601 }
602
Brian Carlstroma004aa92012-02-08 18:05:09 -0800603 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800604 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800605 }
606
Brian Carlstroma004aa92012-02-08 18:05:09 -0800607 if (dex_locations.empty()) {
608 for (size_t i = 0; i < dex_filenames.size(); i++) {
609 dex_locations.push_back(dex_filenames[i]);
610 }
611 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800612 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800613 }
614
615 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800616 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800617 }
618
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700619 if (boot_image_option.empty()) {
620 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800621 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700622 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700623 }
624
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800625 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800626 UniquePtr<File> oat_file;
627 if (!oat_filename.empty()) {
628 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800629 if (oat_location.empty()) {
630 oat_location = oat_filename;
631 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800632 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800633 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800634 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800635 if (oat_file.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800636 PLOG(ERROR) << "Unable to create oat file: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800637 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700638 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800639
Brian Carlstroma004aa92012-02-08 18:05:09 -0800640 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700641
Logan Chien8b977d32012-02-21 19:14:55 +0800642#if defined(ART_USE_LLVM_COMPILER)
643 if (elf_filename.empty()) {
644 if (oat_filename.empty()) {
645 LOG(FATAL) << "Both --oat-file and --elf-file are not specified";
646 }
647
648 StringPiece elf_filename_sp(oat_filename);
649 if (elf_filename_sp.ends_with(".oat")) {
650 elf_filename_sp.remove_suffix(strlen(".oat"));
651 }
652
653 elf_filename = elf_filename_sp.ToString() + ".elf";
654 }
Logan Chien8b977d32012-02-21 19:14:55 +0800655#endif
656
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700657 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700658 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800659 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700660 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800661 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
662 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700663 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700664 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
665 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700666 if (!host_prefix.empty()) {
667 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
668 }
jeffhao5d840402011-10-24 17:09:45 -0700669 for (size_t i = 0; i < runtime_args.size(); i++) {
670 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
671 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700672
Ian Rogers49c48942012-03-11 15:15:37 -0700673 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, instruction_set, thread_count,
674 support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700675
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800676 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800677 UniquePtr<const std::set<std::string> > image_classes(NULL);
678 if (image_classes_filename != NULL) {
679 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
680 if (image_classes.get() == NULL) {
681 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
682 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700683 }
684 }
685
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800686 std::vector<const DexFile*> dex_files;
687 if (boot_image_option.empty()) {
688 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
689 } else {
690 if (dex_filenames.empty()) {
691 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
692 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800693 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800694 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800695 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800696 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800697 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800698 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800699 return EXIT_FAILURE;
700 }
701 dex_files.push_back(dex_file);
702 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800703 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800704 }
705 }
706
Brian Carlstromae826982011-11-09 01:33:42 -0800707 if (!dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700708 host_prefix,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800709 dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800710 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800711#if defined(ART_USE_LLVM_COMPILER)
712 elf_filename,
713 bitcode_filename,
714#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800715 image,
716 image_classes.get())) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800717 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700718 return EXIT_FAILURE;
719 }
720
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800721#if defined(ART_USE_LLVM_COMPILER)
722 LOG(INFO) << "oat=" << oat_location << " elf=" << elf_filename;
723#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800724 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800725 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700726 return EXIT_SUCCESS;
727 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700728
Brian Carlstromae826982011-11-09 01:33:42 -0800729 if (!dex2oat->CreateImageFile(image_filename,
730 image_base,
731 image_classes.get(),
732 oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800733 oat_location)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700734 return EXIT_FAILURE;
735 }
736
Brian Carlstromae826982011-11-09 01:33:42 -0800737 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800738 LOG(INFO) << "Oat file written successfully: " << oat_filename;
739 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700740 return EXIT_SUCCESS;
741}
742
743} // namespace art
744
745int main(int argc, char** argv) {
746 return art::dex2oat(argc, argv);
747}