blob: 9d334baf84652192377429256c640ed4afa37fda [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("");
81 UsageError(" --image=<file.art>: specifies the output image filename.");
82 UsageError(" Example: --image=/system/framework/boot.art");
83 UsageError("");
84 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
85 UsageError(" Example: --image=frameworks/base/preloaded-classes");
86 UsageError("");
87 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
88 UsageError(" Example: --base=0x50000000");
89 UsageError("");
90 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
91 UsageError(" Example: --boot-image=/system/framework/boot.art");
92 UsageError(" Default: <host-prefix>/system/framework/boot.art");
93 UsageError("");
94 UsageError(" --host-prefix may be used to translate host paths to target paths during");
95 UsageError(" cross compilation.");
96 UsageError(" Example: --host-prefix=out/target/product/crespo");
97 UsageError(" Default: $ANDROID_PRODUCT_OUT");
98 UsageError("");
99 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
100 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
101 UsageError(" Use a separate --runtime-arg switch for each argument.");
102 UsageError(" Example: --runtime-arg -Xms256m");
103 UsageError("");
104 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700105 exit(EXIT_FAILURE);
106}
107
Brian Carlstromae826982011-11-09 01:33:42 -0800108class Dex2Oat {
109 public:
110
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800111 static Dex2Oat* Create(Runtime::Options& options, size_t thread_count, bool support_debugging) {
Brian Carlstromae826982011-11-09 01:33:42 -0800112 UniquePtr<Runtime> runtime(CreateRuntime(options));
113 if (runtime.get() == NULL) {
114 return NULL;
115 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800116 return new Dex2Oat(runtime.release(), thread_count, support_debugging);
Brian Carlstromae826982011-11-09 01:33:42 -0800117 }
118
119 ~Dex2Oat() {
120 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800121 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800122 }
123
124 // Make a list of descriptors for classes to include in the image
125 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
126 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
127 if (image_classes_file.get() == NULL) {
128 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
129 return NULL;
130 }
131
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800132 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800133 ClassLinker* class_linker = runtime_->GetClassLinker();
134 while (image_classes_file->good()) {
135 std::string dot;
136 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800137 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800138 continue;
139 }
Elliott Hughes95572412011-12-13 18:14:20 -0800140 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800141 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800142 if (klass.get() == NULL) {
143 LOG(WARNING) << "Failed to find class " << descriptor;
144 Thread::Current()->ClearException();
145 }
146 }
147 image_classes_file->close();
148
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800149 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
150 // exceptions are resolved by the verifier when there is a catch block in an interested method.
151 // Do this here so that exception classes appear to have been specified image classes.
152 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
153 do {
154 unresolved_exception_types.clear();
155 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
156 &unresolved_exception_types);
157 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
158 for (It it = unresolved_exception_types.begin(),
159 end = unresolved_exception_types.end();
160 it != end; ++it) {
161 uint16_t exception_type_idx = it->first;
162 const DexFile* dex_file = it->second;
163 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
164 ClassLoader* class_loader = NULL;
165 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
166 class_loader));
167 if (klass.get() == NULL) {
168 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
169 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
170 LOG(FATAL) << "Failed to resolve class " << descriptor;
171 }
172 DCHECK(klass->IsThrowableClass());
173 }
174 // Resolving exceptions may load classes that reference more exceptions, iterate until no
175 // more are found
176 } while (!unresolved_exception_types.empty());
177
Brian Carlstromae826982011-11-09 01:33:42 -0800178 // We walk the roots looking for classes so that we'll pick up the
179 // above classes plus any classes them depend on such super
180 // classes, interfaces, and the required ClassLinker roots.
181 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800182 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800183 CHECK_NE(image_classes->size(), 0U);
184 return image_classes.release();
185 }
186
187 bool CreateOatFile(const std::string& boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800188 const std::vector<const DexFile*>& dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800189 File* oat_file,
190 bool image,
191 const std::set<std::string>* image_classes) {
192 // SirtRef and ClassLoader creation needs to come after Runtime::Create
193 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
194 if (class_loader.get() == NULL) {
195 LOG(ERROR) << "Failed to create SirtRef for class loader";
196 return false;
197 }
198
Brian Carlstromae826982011-11-09 01:33:42 -0800199 if (!boot_image_option.empty()) {
200 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800201 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800202 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800203 for (size_t i = 0; i < class_path_files.size(); i++) {
204 class_linker->RegisterDexFile(*class_path_files[i]);
205 }
206 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800207 }
208
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800209 Compiler compiler(instruction_set_, image, thread_count_, support_debugging_, image_classes);
Brian Carlstromae826982011-11-09 01:33:42 -0800210 compiler.CompileAll(class_loader->get(), dex_files);
211
jeffhao10037c82012-01-23 15:06:23 -0800212 if (!OatWriter::Create(oat_file, class_loader->get(), dex_files, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800213 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
214 return false;
215 }
216 return true;
217 }
218
Brian Carlstroma004aa92012-02-08 18:05:09 -0800219 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800220 uintptr_t image_base,
221 const std::set<std::string>* image_classes,
222 const std::string& oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800223 const std::string& oat_location) {
Brian Carlstromae826982011-11-09 01:33:42 -0800224 // If we have an existing boot image, position new space after its oat file
225 if (Heap::GetSpaces().size() > 1) {
Ian Rogers30fab402012-01-23 15:43:46 -0800226 ImageSpace* last_image_space = NULL;
227 const std::vector<Space*>& spaces = Heap::GetSpaces();
228 for (size_t i=0; i < spaces.size(); i++) {
229 if (spaces[i]->IsImageSpace()) {
230 last_image_space = spaces[i]->AsImageSpace();
231 }
232 }
Brian Carlstromae826982011-11-09 01:33:42 -0800233 CHECK(last_image_space != NULL);
234 CHECK(last_image_space->IsImageSpace());
235 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
Ian Rogers30fab402012-01-23 15:43:46 -0800236 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatEnd();
Brian Carlstromae826982011-11-09 01:33:42 -0800237 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
238 }
239
240 ImageWriter image_writer(image_classes);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800241 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800242 LOG(ERROR) << "Failed to create image file " << image_filename;
243 return false;
244 }
245 return true;
246 }
247
248 private:
249
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800250 explicit Dex2Oat(Runtime* runtime, size_t thread_count, bool support_debugging)
251 : runtime_(runtime),
252 thread_count_(thread_count),
253 support_debugging_(support_debugging),
254 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800255 }
Brian Carlstromae826982011-11-09 01:33:42 -0800256
257 static Runtime* CreateRuntime(Runtime::Options& options) {
258 Runtime* runtime = Runtime::Create(options, false);
259 if (runtime == NULL) {
260 LOG(ERROR) << "Failed to create runtime";
261 return NULL;
262 }
263
264 // if we loaded an existing image, we will reuse values from the image roots.
265 if (!runtime->HasJniDlsymLookupStub()) {
Elliott Hughes8add92d2012-01-18 18:18:43 -0800266 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set_));
Brian Carlstromae826982011-11-09 01:33:42 -0800267 }
268 if (!runtime->HasAbstractMethodErrorStubArray()) {
269 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_));
270 }
271 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
272 Runtime::TrampolineType type = Runtime::TrampolineType(i);
273 if (!runtime->HasResolutionStubArray(type)) {
274 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type);
275 }
276 }
Ian Rogers19846512012-02-24 11:42:47 -0800277 if (!runtime->HasResolutionMethod()) {
278 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
279 }
Brian Carlstromae826982011-11-09 01:33:42 -0800280 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
281 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
282 if (!runtime->HasCalleeSaveMethod(type)) {
283 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type);
284 }
285 }
Ian Rogers19846512012-02-24 11:42:47 -0800286 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800287 return runtime;
288 }
289
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800290 static void ResolveExceptionsForMethod(MethodHelper* mh,
291 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
292 const DexFile::CodeItem* code_item = mh->GetCodeItem();
293 if (code_item == NULL) {
294 return; // native or abstract method
295 }
296 if (code_item->tries_size_ == 0) {
297 return; // nothing to process
298 }
299 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
300 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
301 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
302 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
303 bool has_catch_all = false;
304 if (encoded_catch_handler_size <= 0) {
305 encoded_catch_handler_size = -encoded_catch_handler_size;
306 has_catch_all = true;
307 }
308 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
309 uint16_t encoded_catch_handler_handlers_type_idx =
310 DecodeUnsignedLeb128(&encoded_catch_handler_list);
311 // Add to set of types to resolve if not already in the dex cache resolved types
312 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
313 exceptions_to_resolve.insert(
314 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
315 &mh->GetDexFile()));
316 }
317 // ignore address associated with catch handler
318 DecodeUnsignedLeb128(&encoded_catch_handler_list);
319 }
320 if (has_catch_all) {
321 // ignore catch all address
322 DecodeUnsignedLeb128(&encoded_catch_handler_list);
323 }
324 }
325 }
326 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
327 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
328 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
329 MethodHelper mh;
330 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
331 Method* m = c->GetVirtualMethod(i);
332 mh.ChangeMethod(m);
333 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
334 }
335 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
336 Method* m = c->GetDirectMethod(i);
337 mh.ChangeMethod(m);
338 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
339 }
340 return true;
341 }
342 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800343 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
344 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500345 return true;
346 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800347 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800348 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500349 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500350
Brian Carlstromae826982011-11-09 01:33:42 -0800351 // Appends to dex_files any elements of class_path that it doesn't already
352 // contain. This will open those dex files as necessary.
353 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
354 std::vector<std::string> parsed;
355 Split(class_path, ':', parsed);
356 for (size_t i = 0; i < parsed.size(); ++i) {
357 if (DexFilesContains(dex_files, parsed[i])) {
358 continue;
359 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800360 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800361 if (dex_file == NULL) {
362 LOG(WARNING) << "Failed to open dex file " << parsed[i];
363 } else {
364 dex_files.push_back(dex_file);
365 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500366 }
367 }
Brian Carlstromae826982011-11-09 01:33:42 -0800368
369 // Returns true if dex_files has a dex with the named location.
370 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
371 for (size_t i = 0; i < dex_files.size(); ++i) {
372 if (dex_files[i]->GetLocation() == location) {
373 return true;
374 }
375 }
376 return false;
377 }
378
Brian Carlstromae826982011-11-09 01:33:42 -0800379 static const InstructionSet instruction_set_ = kThumb2;
380
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800381 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800382 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800383 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800384 uint64_t start_ns_;
385
Brian Carlstromae826982011-11-09 01:33:42 -0800386 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
387};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500388
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800389bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800390 char* end;
391 int result = strtol(in, &end, 10);
392 if (in == end || *end != '\0') {
393 return false;
394 }
395 *out = result;
396 return true;
397}
398
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800399void OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800400 const std::vector<const char*>& dex_locations,
401 std::vector<const DexFile*>& dex_files) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800402 for (size_t i = 0; i < dex_filenames.size(); i++) {
403 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800404 const char* dex_location = dex_locations[i];
405 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800406 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800407 LOG(WARNING) << "could not open .dex from file " << dex_filename;
408 } else {
409 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800410 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800411 }
412}
413
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700414int dex2oat(int argc, char** argv) {
415 // Skip over argv[0].
416 argv++;
417 argc--;
418
419 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800420 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700421 }
422
423 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800424 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800425 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800426 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700427 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800428 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800429 int oat_fd = -1;
Brian Carlstromae826982011-11-09 01:33:42 -0800430 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800431 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800432 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700433 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700434 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700435 std::vector<const char*> runtime_args;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800436 int thread_count = 2;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800437 bool support_debugging = false;
Brian Carlstrom16192862011-09-12 17:50:06 -0700438
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700439 for (int i = 0; i < argc; i++) {
440 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800441 bool log_options = false;
442 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700443 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
444 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700445 if (option.starts_with("--dex-file=")) {
446 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800447 } else if (option.starts_with("--dex-location=")) {
448 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800449 } else if (option.starts_with("--zip-fd=")) {
450 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800451 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800452 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800453 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800454 } else if (option.starts_with("--zip-location=")) {
455 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800456 } else if (option.starts_with("--oat-file=")) {
457 oat_filename = option.substr(strlen("--oat-file=")).data();
458 } else if (option.starts_with("--oat-fd=")) {
459 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800460 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800461 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800462 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800463 } else if (option.starts_with("-g")) {
464 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800465 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800466 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800467 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800468 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800469 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800470 } else if (option.starts_with("--oat-location=")) {
471 oat_location = option.substr(strlen("--oat-location=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700472 } else if (option.starts_with("--image=")) {
473 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800474 } else if (option.starts_with("--image-classes=")) {
475 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700476 } else if (option.starts_with("--base=")) {
477 const char* image_base_str = option.substr(strlen("--base=")).data();
478 char* end;
479 image_base = strtoul(image_base_str, &end, 16);
480 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800481 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700482 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700483 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800484 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700485 } else if (option.starts_with("--host-prefix=")) {
486 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700487 } else if (option == "--runtime-arg") {
488 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800489 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700490 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800491 if (log_options) {
492 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
493 }
jeffhao5d840402011-10-24 17:09:45 -0700494 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700495 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800496 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700497 }
498 }
499
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800500 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800501 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800502 }
503
504 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800505 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800506 }
507
508 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800509 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800510 }
511
Brian Carlstroma004aa92012-02-08 18:05:09 -0800512 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800513 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700514 }
515
Brian Carlstromb0011262011-12-09 12:17:24 -0800516 if (host_prefix.empty()) {
517 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
518 if (android_product_out != NULL) {
519 host_prefix = android_product_out;
520 }
521 }
522
Brian Carlstroma004aa92012-02-08 18:05:09 -0800523 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800524 if (!image && boot_image_filename.empty()) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800525 if (host_prefix.empty()) {
526 boot_image_filename += GetAndroidRoot();
527 } else {
528 boot_image_filename += host_prefix;
529 boot_image_filename += "/system";
530 }
531 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800532 }
533 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800534 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800535 boot_image_option += "-Ximage:";
536 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700537 }
538
Brian Carlstromae826982011-11-09 01:33:42 -0800539 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800540 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800541 }
542
543 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800544 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700545 }
546
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800547 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800548 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800549 }
550
551 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800552 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800553 }
554
Brian Carlstroma004aa92012-02-08 18:05:09 -0800555 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800556 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800557 }
558
Brian Carlstroma004aa92012-02-08 18:05:09 -0800559 if (dex_locations.empty()) {
560 for (size_t i = 0; i < dex_filenames.size(); i++) {
561 dex_locations.push_back(dex_filenames[i]);
562 }
563 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800564 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800565 }
566
567 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800568 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800569 }
570
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700571 if (boot_image_option.empty()) {
572 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800573 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700574 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700575 }
576
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800577 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800578 UniquePtr<File> oat_file;
579 if (!oat_filename.empty()) {
580 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800581 if (oat_location.empty()) {
582 oat_location = oat_filename;
583 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800584 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800585 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800586 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800587 if (oat_file.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800588 PLOG(ERROR) << "Unable to create oat file: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800589 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700590 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800591
Brian Carlstroma004aa92012-02-08 18:05:09 -0800592 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700593
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700594 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700595 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800596 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700597 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800598 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
599 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700600 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700601 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
602 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700603 if (!host_prefix.empty()) {
604 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
605 }
jeffhao5d840402011-10-24 17:09:45 -0700606 for (size_t i = 0; i < runtime_args.size(); i++) {
607 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
608 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700609
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800610 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, thread_count, support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700611
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800612 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800613 UniquePtr<const std::set<std::string> > image_classes(NULL);
614 if (image_classes_filename != NULL) {
615 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
616 if (image_classes.get() == NULL) {
617 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
618 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700619 }
620 }
621
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800622 std::vector<const DexFile*> dex_files;
623 if (boot_image_option.empty()) {
624 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
625 } else {
626 if (dex_filenames.empty()) {
627 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
628 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800629 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800630 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800631 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800632 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800633 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800634 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800635 return EXIT_FAILURE;
636 }
637 dex_files.push_back(dex_file);
638 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800639 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800640 }
641 }
642
Brian Carlstromae826982011-11-09 01:33:42 -0800643 if (!dex2oat->CreateOatFile(boot_image_option,
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800644 dex_files,
Brian Carlstromae826982011-11-09 01:33:42 -0800645 oat_file.get(),
646 image,
647 image_classes.get())) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800648 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649 return EXIT_FAILURE;
650 }
651
Brian Carlstromae826982011-11-09 01:33:42 -0800652 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800653 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700654 return EXIT_SUCCESS;
655 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700656
Brian Carlstromae826982011-11-09 01:33:42 -0800657 if (!dex2oat->CreateImageFile(image_filename,
658 image_base,
659 image_classes.get(),
660 oat_filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800661 oat_location)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700662 return EXIT_FAILURE;
663 }
664
Brian Carlstromae826982011-11-09 01:33:42 -0800665 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800666 LOG(INFO) << "Oat file written successfully: " << oat_filename;
667 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700668 return EXIT_SUCCESS;
669}
670
671} // namespace art
672
673int main(int argc, char** argv) {
674 return art::dex2oat(argc, argv);
675}