blob: b8d782b61ababe36ca357cd51bc084455ef491eb [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("");
107 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
108 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
109 UsageError(" Use a separate --runtime-arg switch for each argument.");
110 UsageError(" Example: --runtime-arg -Xms256m");
111 UsageError("");
112 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700113 exit(EXIT_FAILURE);
114}
115
Brian Carlstromae826982011-11-09 01:33:42 -0800116class Dex2Oat {
117 public:
118
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800119 static Dex2Oat* Create(Runtime::Options& options, size_t thread_count, bool support_debugging) {
Brian Carlstromae826982011-11-09 01:33:42 -0800120 UniquePtr<Runtime> runtime(CreateRuntime(options));
121 if (runtime.get() == NULL) {
122 return NULL;
123 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800124 return new Dex2Oat(runtime.release(), thread_count, support_debugging);
Brian Carlstromae826982011-11-09 01:33:42 -0800125 }
126
127 ~Dex2Oat() {
128 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800129 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800130 }
131
132 // Make a list of descriptors for classes to include in the image
133 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
134 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
135 if (image_classes_file.get() == NULL) {
136 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
137 return NULL;
138 }
139
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800140 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800141 ClassLinker* class_linker = runtime_->GetClassLinker();
142 while (image_classes_file->good()) {
143 std::string dot;
144 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800145 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800146 continue;
147 }
Elliott Hughes95572412011-12-13 18:14:20 -0800148 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800149 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800150 if (klass.get() == NULL) {
151 LOG(WARNING) << "Failed to find class " << descriptor;
152 Thread::Current()->ClearException();
153 }
154 }
155 image_classes_file->close();
156
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800157 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
158 // exceptions are resolved by the verifier when there is a catch block in an interested method.
159 // Do this here so that exception classes appear to have been specified image classes.
160 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
161 do {
162 unresolved_exception_types.clear();
163 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
164 &unresolved_exception_types);
165 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
166 for (It it = unresolved_exception_types.begin(),
167 end = unresolved_exception_types.end();
168 it != end; ++it) {
169 uint16_t exception_type_idx = it->first;
170 const DexFile* dex_file = it->second;
171 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
172 ClassLoader* class_loader = NULL;
173 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
174 class_loader));
175 if (klass.get() == NULL) {
176 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
177 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
178 LOG(FATAL) << "Failed to resolve class " << descriptor;
179 }
180 DCHECK(klass->IsThrowableClass());
181 }
182 // Resolving exceptions may load classes that reference more exceptions, iterate until no
183 // more are found
184 } while (!unresolved_exception_types.empty());
185
Brian Carlstromae826982011-11-09 01:33:42 -0800186 // We walk the roots looking for classes so that we'll pick up the
187 // above classes plus any classes them depend on such super
188 // classes, interfaces, and the required ClassLinker roots.
189 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800190 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800191 CHECK_NE(image_classes->size(), 0U);
192 return image_classes.release();
193 }
194
195 bool CreateOatFile(const std::string& boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800196 const std::vector<const DexFile*>& dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800197 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800198#if defined(ART_USE_LLVM_COMPILER)
199 std::string const& elf_filename,
200 std::string const& bitcode_filename,
201#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800202 bool image,
203 const std::set<std::string>* image_classes) {
204 // SirtRef and ClassLoader creation needs to come after Runtime::Create
205 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
206 if (class_loader.get() == NULL) {
207 LOG(ERROR) << "Failed to create SirtRef for class loader";
208 return false;
209 }
210
Brian Carlstromae826982011-11-09 01:33:42 -0800211 if (!boot_image_option.empty()) {
212 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800213 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800214 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800215 for (size_t i = 0; i < class_path_files.size(); i++) {
216 class_linker->RegisterDexFile(*class_path_files[i]);
217 }
218 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800219 }
220
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800221 Compiler compiler(instruction_set_, image, thread_count_, support_debugging_, image_classes);
Logan Chien8b977d32012-02-21 19:14:55 +0800222
223#if defined(ART_USE_LLVM_COMPILER)
224 compiler.SetElfFileName(elf_filename);
225 compiler.SetBitcodeFileName(bitcode_filename);
226#endif
227
Brian Carlstromae826982011-11-09 01:33:42 -0800228 compiler.CompileAll(class_loader->get(), dex_files);
229
jeffhao10037c82012-01-23 15:06:23 -0800230 if (!OatWriter::Create(oat_file, class_loader->get(), dex_files, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800231 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
232 return false;
233 }
234 return true;
235 }
236
Brian Carlstroma004aa92012-02-08 18:05:09 -0800237 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800238 uintptr_t image_base,
239 const std::set<std::string>* image_classes,
240 const std::string& oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800241 const std::string& oat_location) {
Brian Carlstromae826982011-11-09 01:33:42 -0800242 // If we have an existing boot image, position new space after its oat file
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800243 Heap* heap = Runtime::Current()->GetHeap();
244 if (heap->GetSpaces().size() > 1) {
Ian Rogers30fab402012-01-23 15:43:46 -0800245 ImageSpace* last_image_space = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800246 const std::vector<Space*>& spaces = heap->GetSpaces();
Ian Rogers30fab402012-01-23 15:43:46 -0800247 for (size_t i=0; i < spaces.size(); i++) {
248 if (spaces[i]->IsImageSpace()) {
249 last_image_space = spaces[i]->AsImageSpace();
250 }
251 }
Brian Carlstromae826982011-11-09 01:33:42 -0800252 CHECK(last_image_space != NULL);
253 CHECK(last_image_space->IsImageSpace());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800254 CHECK(!heap->GetSpaces()[heap->GetSpaces().size()-1]->IsImageSpace());
Ian Rogers30fab402012-01-23 15:43:46 -0800255 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatEnd();
Brian Carlstromae826982011-11-09 01:33:42 -0800256 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
257 }
258
259 ImageWriter image_writer(image_classes);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800260 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800261 LOG(ERROR) << "Failed to create image file " << image_filename;
262 return false;
263 }
264 return true;
265 }
266
267 private:
268
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800269 explicit Dex2Oat(Runtime* runtime, size_t thread_count, bool support_debugging)
270 : runtime_(runtime),
271 thread_count_(thread_count),
272 support_debugging_(support_debugging),
273 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800274 }
Brian Carlstromae826982011-11-09 01:33:42 -0800275
276 static Runtime* CreateRuntime(Runtime::Options& options) {
277 Runtime* runtime = Runtime::Create(options, false);
278 if (runtime == NULL) {
279 LOG(ERROR) << "Failed to create runtime";
280 return NULL;
281 }
282
283 // if we loaded an existing image, we will reuse values from the image roots.
284 if (!runtime->HasJniDlsymLookupStub()) {
Elliott Hughes8add92d2012-01-18 18:18:43 -0800285 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set_));
Brian Carlstromae826982011-11-09 01:33:42 -0800286 }
287 if (!runtime->HasAbstractMethodErrorStubArray()) {
288 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_));
289 }
290 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
291 Runtime::TrampolineType type = Runtime::TrampolineType(i);
292 if (!runtime->HasResolutionStubArray(type)) {
293 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type);
294 }
295 }
Ian Rogers19846512012-02-24 11:42:47 -0800296 if (!runtime->HasResolutionMethod()) {
297 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
298 }
Brian Carlstromae826982011-11-09 01:33:42 -0800299 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
300 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
301 if (!runtime->HasCalleeSaveMethod(type)) {
302 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type);
303 }
304 }
Ian Rogers19846512012-02-24 11:42:47 -0800305 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800306 return runtime;
307 }
308
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800309 static void ResolveExceptionsForMethod(MethodHelper* mh,
310 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
311 const DexFile::CodeItem* code_item = mh->GetCodeItem();
312 if (code_item == NULL) {
313 return; // native or abstract method
314 }
315 if (code_item->tries_size_ == 0) {
316 return; // nothing to process
317 }
318 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
319 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
320 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
321 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
322 bool has_catch_all = false;
323 if (encoded_catch_handler_size <= 0) {
324 encoded_catch_handler_size = -encoded_catch_handler_size;
325 has_catch_all = true;
326 }
327 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
328 uint16_t encoded_catch_handler_handlers_type_idx =
329 DecodeUnsignedLeb128(&encoded_catch_handler_list);
330 // Add to set of types to resolve if not already in the dex cache resolved types
331 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
332 exceptions_to_resolve.insert(
333 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
334 &mh->GetDexFile()));
335 }
336 // ignore address associated with catch handler
337 DecodeUnsignedLeb128(&encoded_catch_handler_list);
338 }
339 if (has_catch_all) {
340 // ignore catch all address
341 DecodeUnsignedLeb128(&encoded_catch_handler_list);
342 }
343 }
344 }
345 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
346 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
347 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
348 MethodHelper mh;
349 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
350 Method* m = c->GetVirtualMethod(i);
351 mh.ChangeMethod(m);
352 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
353 }
354 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
355 Method* m = c->GetDirectMethod(i);
356 mh.ChangeMethod(m);
357 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
358 }
359 return true;
360 }
361 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800362 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
363 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500364 return true;
365 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800366 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800367 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500368 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500369
Brian Carlstromae826982011-11-09 01:33:42 -0800370 // Appends to dex_files any elements of class_path that it doesn't already
371 // contain. This will open those dex files as necessary.
372 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
373 std::vector<std::string> parsed;
374 Split(class_path, ':', parsed);
375 for (size_t i = 0; i < parsed.size(); ++i) {
376 if (DexFilesContains(dex_files, parsed[i])) {
377 continue;
378 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800379 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800380 if (dex_file == NULL) {
381 LOG(WARNING) << "Failed to open dex file " << parsed[i];
382 } else {
383 dex_files.push_back(dex_file);
384 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500385 }
386 }
Brian Carlstromae826982011-11-09 01:33:42 -0800387
388 // Returns true if dex_files has a dex with the named location.
389 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
390 for (size_t i = 0; i < dex_files.size(); ++i) {
391 if (dex_files[i]->GetLocation() == location) {
392 return true;
393 }
394 }
395 return false;
396 }
397
Brian Carlstromae826982011-11-09 01:33:42 -0800398 static const InstructionSet instruction_set_ = kThumb2;
399
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800400 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800401 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800402 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800403 uint64_t start_ns_;
404
Brian Carlstromae826982011-11-09 01:33:42 -0800405 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
406};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500407
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800408bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800409 char* end;
410 int result = strtol(in, &end, 10);
411 if (in == end || *end != '\0') {
412 return false;
413 }
414 *out = result;
415 return true;
416}
417
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800418void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800419 const std::vector<const char*>& dex_locations,
420 std::vector<const DexFile*>& dex_files) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800421 for (size_t i = 0; i < dex_filenames.size(); i++) {
422 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800423 const char* dex_location = dex_locations[i];
424 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800425 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800426 LOG(WARNING) << "could not open .dex from file " << dex_filename;
427 } else {
428 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800429 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800430 }
431}
432
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700433int dex2oat(int argc, char** argv) {
434 // Skip over argv[0].
435 argv++;
436 argc--;
437
438 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800439 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700440 }
441
442 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800443 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800444 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800445 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800447 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800448 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800449#if defined(ART_USE_LLVM_COMPILER)
450 std::string elf_filename;
451 std::string bitcode_filename;
452#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800453 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800454 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800455 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700456 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700457 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700458 std::vector<const char*> runtime_args;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800459 int thread_count = 2;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800460 bool support_debugging = false;
Brian Carlstrom16192862011-09-12 17:50:06 -0700461
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700462 for (int i = 0; i < argc; i++) {
463 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800464 bool log_options = false;
465 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700466 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
467 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700468 if (option.starts_with("--dex-file=")) {
469 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800470 } else if (option.starts_with("--dex-location=")) {
471 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800472 } else if (option.starts_with("--zip-fd=")) {
473 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800474 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800475 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800476 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800477 } else if (option.starts_with("--zip-location=")) {
478 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800479 } else if (option.starts_with("--oat-file=")) {
480 oat_filename = option.substr(strlen("--oat-file=")).data();
481 } else if (option.starts_with("--oat-fd=")) {
482 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800483 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800484 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800485 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800486 } else if (option.starts_with("-g")) {
487 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800488 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800489 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800490 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800491 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800492 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800493 } else if (option.starts_with("--oat-location=")) {
494 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800495#if defined(ART_USE_LLVM_COMPILER)
496 } else if (option.starts_with("--elf-file=")) {
497 elf_filename = option.substr(strlen("--elf-file=")).data();
498 } else if (option.starts_with("--bitcode=")) {
499 bitcode_filename = option.substr(strlen("--bitcode=")).data();
500#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700501 } else if (option.starts_with("--image=")) {
502 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800503 } else if (option.starts_with("--image-classes=")) {
504 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700505 } else if (option.starts_with("--base=")) {
506 const char* image_base_str = option.substr(strlen("--base=")).data();
507 char* end;
508 image_base = strtoul(image_base_str, &end, 16);
509 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800510 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700511 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700512 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800513 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700514 } else if (option.starts_with("--host-prefix=")) {
515 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700516 } else if (option == "--runtime-arg") {
517 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800518 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700519 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800520 if (log_options) {
521 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
522 }
jeffhao5d840402011-10-24 17:09:45 -0700523 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700524 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800525 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700526 }
527 }
528
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800529 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800530 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800531 }
532
533 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800534 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800535 }
536
537 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800538 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800539 }
540
Brian Carlstroma004aa92012-02-08 18:05:09 -0800541 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800542 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700543 }
544
Brian Carlstromb0011262011-12-09 12:17:24 -0800545 if (host_prefix.empty()) {
546 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
547 if (android_product_out != NULL) {
548 host_prefix = android_product_out;
549 }
550 }
551
Brian Carlstroma004aa92012-02-08 18:05:09 -0800552 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800553 if (!image && boot_image_filename.empty()) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800554 if (host_prefix.empty()) {
555 boot_image_filename += GetAndroidRoot();
556 } else {
557 boot_image_filename += host_prefix;
558 boot_image_filename += "/system";
559 }
560 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800561 }
562 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800563 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800564 boot_image_option += "-Ximage:";
565 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700566 }
567
Brian Carlstromae826982011-11-09 01:33:42 -0800568 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800569 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800570 }
571
572 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800573 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700574 }
575
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800576 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800577 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800578 }
579
580 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800581 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800582 }
583
Brian Carlstroma004aa92012-02-08 18:05:09 -0800584 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800585 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800586 }
587
Brian Carlstroma004aa92012-02-08 18:05:09 -0800588 if (dex_locations.empty()) {
589 for (size_t i = 0; i < dex_filenames.size(); i++) {
590 dex_locations.push_back(dex_filenames[i]);
591 }
592 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800593 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800594 }
595
596 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800597 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800598 }
599
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700600 if (boot_image_option.empty()) {
601 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800602 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700603 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700604 }
605
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800606 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800607 UniquePtr<File> oat_file;
608 if (!oat_filename.empty()) {
609 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800610 if (oat_location.empty()) {
611 oat_location = oat_filename;
612 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800613 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800614 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800615 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800616 if (oat_file.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800617 PLOG(ERROR) << "Unable to create oat file: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800618 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700619 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800620
Brian Carlstroma004aa92012-02-08 18:05:09 -0800621 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700622
Logan Chien8b977d32012-02-21 19:14:55 +0800623#if defined(ART_USE_LLVM_COMPILER)
624 if (elf_filename.empty()) {
625 if (oat_filename.empty()) {
626 LOG(FATAL) << "Both --oat-file and --elf-file are not specified";
627 }
628
629 StringPiece elf_filename_sp(oat_filename);
630 if (elf_filename_sp.ends_with(".oat")) {
631 elf_filename_sp.remove_suffix(strlen(".oat"));
632 }
633
634 elf_filename = elf_filename_sp.ToString() + ".elf";
635 }
Logan Chien8b977d32012-02-21 19:14:55 +0800636#endif
637
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700638 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700639 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800640 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700641 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800642 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
643 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700644 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700645 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
646 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700647 if (!host_prefix.empty()) {
648 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
649 }
jeffhao5d840402011-10-24 17:09:45 -0700650 for (size_t i = 0; i < runtime_args.size(); i++) {
651 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
652 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700653
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800654 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, thread_count, support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700655
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800656 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800657 UniquePtr<const std::set<std::string> > image_classes(NULL);
658 if (image_classes_filename != NULL) {
659 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
660 if (image_classes.get() == NULL) {
661 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
662 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700663 }
664 }
665
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800666 std::vector<const DexFile*> dex_files;
667 if (boot_image_option.empty()) {
668 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
669 } else {
670 if (dex_filenames.empty()) {
671 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
672 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800673 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800674 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800675 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800676 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800677 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800678 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800679 return EXIT_FAILURE;
680 }
681 dex_files.push_back(dex_file);
682 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800683 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800684 }
685 }
686
Brian Carlstromae826982011-11-09 01:33:42 -0800687 if (!dex2oat->CreateOatFile(boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800688 dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800689 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800690#if defined(ART_USE_LLVM_COMPILER)
691 elf_filename,
692 bitcode_filename,
693#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800694 image,
695 image_classes.get())) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800696 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700697 return EXIT_FAILURE;
698 }
699
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800700#if defined(ART_USE_LLVM_COMPILER)
701 LOG(INFO) << "oat=" << oat_location << " elf=" << elf_filename;
702#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800703 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800704 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700705 return EXIT_SUCCESS;
706 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700707
Brian Carlstromae826982011-11-09 01:33:42 -0800708 if (!dex2oat->CreateImageFile(image_filename,
709 image_base,
710 image_classes.get(),
711 oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800712 oat_location)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700713 return EXIT_FAILURE;
714 }
715
Brian Carlstromae826982011-11-09 01:33:42 -0800716 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800717 LOG(INFO) << "Oat file written successfully: " << oat_filename;
718 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700719 return EXIT_SUCCESS;
720}
721
722} // namespace art
723
724int main(int argc, char** argv) {
725 return art::dex2oat(argc, argv);
726}