blob: 7cf54b4eeeb19bf45c4b8957ce4f954219af7c83 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070016
17#include <stdio.h>
18#include <stdlib.h>
Brian Carlstrom6cd40e52012-05-03 14:15:11 -070019#include <sys/stat.h>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070020
Brian Carlstromae826982011-11-09 01:33:42 -080021#include <iostream>
22#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070023#include <string>
24#include <vector>
25
Elliott Hughes1aa246d2012-12-13 09:29:36 -080026#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080027#include "base/stringpiece.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080028#include "base/timing_logger.h"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "base/unix_file/fd_file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070030#include "class_linker.h"
Ian Rogers1212a022013-03-04 10:48:41 -080031#include "compiler/driver/compiler_driver.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/space/image_space.h"
34#include "gc/space/space-inl.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070035#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080036#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/abstract_method-inl.h"
38#include "mirror/class-inl.h"
39#include "mirror/class_loader.h"
40#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070042#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070044#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070045#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046#include "ScopedLocalRef.h"
47#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070048#include "sirt_ref.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "vector_output_stream.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050#include "well_known_classes.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080051#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070052
53namespace art {
54
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080055static void UsageErrorV(const char* fmt, va_list ap) {
56 std::string error;
57 StringAppendV(&error, fmt, ap);
58 LOG(ERROR) << error;
59}
60
61static void UsageError(const char* fmt, ...) {
62 va_list ap;
63 va_start(ap, fmt);
64 UsageErrorV(fmt, ap);
65 va_end(ap);
66}
67
68static void Usage(const char* fmt, ...) {
69 va_list ap;
70 va_start(ap, fmt);
71 UsageErrorV(fmt, ap);
72 va_end(ap);
73
74 UsageError("Usage: dex2oat [options]...");
75 UsageError("");
76 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
77 UsageError(" Example: --dex-file=/system/framework/core.jar");
78 UsageError("");
79 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
80 UsageError(" containing a classes.dex file to compile.");
81 UsageError(" Example: --zip-fd=5");
82 UsageError("");
83 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
84 UsageError(" to the file descriptor specified by --zip-fd.");
85 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
86 UsageError("");
Brian Carlstrom265091e2013-01-30 14:08:26 -080087 UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename.");
88 UsageError(" Example: --oat-file=/system/framework/boot.oat");
89 UsageError("");
90 UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor.");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080091 UsageError(" Example: --oat-file=/system/framework/boot.oat");
92 UsageError("");
93 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
94 UsageError(" to the file descriptor specified by --oat-fd.");
Brian Carlstrom7675e162013-06-10 16:18:04 -070095 UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080096 UsageError("");
Brian Carlstrom265091e2013-01-30 14:08:26 -080097 UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols.");
98 UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat");
99 UsageError("");
Logan Chien8b977d32012-02-21 19:14:55 +0800100 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
101 UsageError(" Example: --bitcode=/system/framework/boot.bc");
102 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800103 UsageError(" --image=<file.art>: specifies the output image filename.");
104 UsageError(" Example: --image=/system/framework/boot.art");
105 UsageError("");
106 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
107 UsageError(" Example: --image=frameworks/base/preloaded-classes");
108 UsageError("");
109 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
110 UsageError(" Example: --base=0x50000000");
111 UsageError("");
112 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
113 UsageError(" Example: --boot-image=/system/framework/boot.art");
114 UsageError(" Default: <host-prefix>/system/framework/boot.art");
115 UsageError("");
Brian Carlstrom265091e2013-01-30 14:08:26 -0800116 UsageError(" --host-prefix=<path>: used to translate host paths to target paths during");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800117 UsageError(" cross compilation.");
118 UsageError(" Example: --host-prefix=out/target/product/crespo");
119 UsageError(" Default: $ANDROID_PRODUCT_OUT");
120 UsageError("");
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800121 UsageError(" --android-root=<path>: used to locate libraries for portable linking.");
122 UsageError(" Example: --android-root=out/host/linux-x86");
123 UsageError(" Default: $ANDROID_ROOT");
124 UsageError("");
jeffhao1f71ae82012-05-24 16:08:24 -0700125 UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction");
Ian Rogers49c48942012-03-11 15:15:37 -0700126 UsageError(" set.");
jeffhao1f71ae82012-05-24 16:08:24 -0700127 UsageError(" Example: --instruction-set=x86");
128 UsageError(" Default: arm");
Ian Rogers49c48942012-03-11 15:15:37 -0700129 UsageError("");
buzbeec531cef2012-10-18 07:09:20 -0700130 UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend");
131 UsageError(" set.");
132 UsageError(" Example: --instruction-set=Portable");
133 UsageError(" Default: Quick");
Brian Carlstrom265091e2013-01-30 14:08:26 -0800134 UsageError("");
135 UsageError(" --host: used with Portable backend to link against host runtime libraries");
136 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800137 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
138 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
139 UsageError(" Use a separate --runtime-arg switch for each argument.");
140 UsageError(" Example: --runtime-arg -Xms256m");
141 UsageError("");
142 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700143 exit(EXIT_FAILURE);
144}
145
Brian Carlstromae826982011-11-09 01:33:42 -0800146class Dex2Oat {
147 public:
buzbeec531cef2012-10-18 07:09:20 -0700148 static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, CompilerBackend compiler_backend,
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700149 InstructionSet instruction_set, size_t thread_count, bool support_debugging)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 if (!CreateRuntime(options, instruction_set)) {
152 *p_dex2oat = NULL;
153 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800154 }
buzbeec531cef2012-10-18 07:09:20 -0700155 *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count,
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700156 support_debugging);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800158 }
159
160 ~Dex2Oat() {
161 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800162 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800163 }
164
Anwar Ghuloumc4f105d2013-04-10 16:12:11 -0700165
Brian Carlstrom96391602013-06-13 19:49:50 -0700166 // Reads the class names (java.lang.Object) and returns as set of class descriptors (Ljava/lang/Object;)
167 CompilerDriver::DescriptorSet* ReadImageClasses(const char* image_classes_filename) {
Brian Carlstromae826982011-11-09 01:33:42 -0800168 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
169 if (image_classes_file.get() == NULL) {
170 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
171 return NULL;
172 }
173
Brian Carlstrom96391602013-06-13 19:49:50 -0700174 UniquePtr<CompilerDriver::DescriptorSet> image_classes(new CompilerDriver::DescriptorSet);
Brian Carlstromae826982011-11-09 01:33:42 -0800175 while (image_classes_file->good()) {
176 std::string dot;
177 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800178 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800179 continue;
180 }
Elliott Hughes95572412011-12-13 18:14:20 -0800181 std::string descriptor(DotToDescriptor(dot.c_str()));
Brian Carlstrom96391602013-06-13 19:49:50 -0700182 image_classes->insert(descriptor);
Brian Carlstromae826982011-11-09 01:33:42 -0800183 }
184 image_classes_file->close();
Brian Carlstromae826982011-11-09 01:33:42 -0800185 return image_classes.release();
186 }
187
Ian Rogers1212a022013-03-04 10:48:41 -0800188 const CompilerDriver* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800189 const std::string* host_prefix,
190 const std::string& android_root,
191 bool is_host,
192 const std::vector<const DexFile*>& dex_files,
193 File* oat_file,
194 const std::string& bitcode_filename,
195 bool image,
Brian Carlstrom96391602013-06-13 19:49:50 -0700196 UniquePtr<CompilerDriver::DescriptorSet>& image_classes,
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800197 bool dump_stats,
198 bool dump_timings)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800200 // SirtRef and ClassLoader creation needs to come after Runtime::Create
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 jobject class_loader = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800202 if (!boot_image_option.empty()) {
203 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800204 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800205 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800206 for (size_t i = 0; i < class_path_files.size(); i++) {
207 class_linker->RegisterDexFile(*class_path_files[i]);
208 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209 ScopedObjectAccessUnchecked soa(Thread::Current());
210 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
211 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
212 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
213 class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
214 Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800215 }
216
Ian Rogers1212a022013-03-04 10:48:41 -0800217 UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_,
218 instruction_set_,
219 image,
Brian Carlstrom96391602013-06-13 19:49:50 -0700220 image_classes.release(),
Ian Rogers1212a022013-03-04 10:48:41 -0800221 thread_count_,
222 support_debugging_,
Ian Rogers1212a022013-03-04 10:48:41 -0800223 dump_stats,
224 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800225
Ian Rogersc928de92013-02-27 14:30:44 -0800226 if (compiler_backend_ == kPortable) {
Ian Rogers1212a022013-03-04 10:48:41 -0800227 driver->SetBitcodeFileName(bitcode_filename);
buzbeec531cef2012-10-18 07:09:20 -0700228 }
Logan Chien8b977d32012-02-21 19:14:55 +0800229
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700230
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
232
Ian Rogers1212a022013-03-04 10:48:41 -0800233 driver->CompileAll(class_loader, dex_files);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234
235 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstromae826982011-11-09 01:33:42 -0800236
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700237 std::string image_file_location;
Brian Carlstrom28db0122012-10-18 16:20:41 -0700238 uint32_t image_file_location_oat_checksum = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800239 uint32_t image_file_location_oat_data_begin = 0;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700240 if (!driver->IsImage()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700241 gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700242 image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
Ian Rogers39ebcb82013-05-30 16:57:23 -0700243 image_file_location_oat_data_begin =
244 reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700245 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700246 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
247 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700248 }
249 }
250
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800251 std::vector<uint8_t> oat_contents;
Ian Rogers39ebcb82013-05-30 16:57:23 -0700252 // TODO: change ElfWriterQuick to not require the creation of oat_contents. The old pre-mclinker
253 // OatWriter streamed directly to disk. The new could can be adapted to do it as follows:
254 // 1.) use first pass of OatWriter to calculate size of oat structure,
255 // 2.) call ElfWriterQuick with pointer to OatWriter instead of contents,
256 // 3.) have ElfWriterQuick call back to OatWriter to stream generate the output directly in
257 // place in the elf file.
258 oat_contents.reserve(5 * MB);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800259 VectorOutputStream vector_output_stream(oat_file->GetPath(), oat_contents);
260 if (!OatWriter::Create(vector_output_stream,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700261 dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700262 image_file_location_oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800263 image_file_location_oat_data_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700264 image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -0800265 *driver.get())) {
Elliott Hughes76160052012-12-12 16:31:20 -0800266 LOG(ERROR) << "Failed to create oat file " << oat_file->GetPath();
Brian Carlstromf5822582012-03-19 22:34:31 -0700267 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800268 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800269
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800270 if (!driver->WriteElf(android_root, is_host, dex_files, oat_contents, oat_file)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800271 LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
272 return NULL;
273 }
274
Ian Rogers1212a022013-03-04 10:48:41 -0800275 return driver.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800276 }
277
Brian Carlstroma004aa92012-02-08 18:05:09 -0800278 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800279 uintptr_t image_base,
Brian Carlstromae826982011-11-09 01:33:42 -0800280 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700281 const std::string& oat_location,
Ian Rogers1212a022013-03-04 10:48:41 -0800282 const CompilerDriver& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700283 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800284 uintptr_t oat_data_begin;
285 {
286 // ImageWriter is scoped so it can free memory before doing FixupElf
Brian Carlstrom96391602013-06-13 19:49:50 -0700287 ImageWriter image_writer(compiler);
288 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800289 LOG(ERROR) << "Failed to create image file " << image_filename;
290 return false;
291 }
292 oat_data_begin = image_writer.GetOatDataBegin();
293 }
294
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800295 UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800296 if (oat_file.get() == NULL) {
297 PLOG(ERROR) << "Failed to open ELF file: " << oat_filename;
Brian Carlstromae826982011-11-09 01:33:42 -0800298 return false;
299 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800300 if (!compiler.FixupElf(oat_file.get(), oat_data_begin)) {
301 LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath();
302 return false;
303 }
Brian Carlstromae826982011-11-09 01:33:42 -0800304 return true;
305 }
306
307 private:
buzbeec531cef2012-10-18 07:09:20 -0700308 explicit Dex2Oat(Runtime* runtime, CompilerBackend compiler_backend, InstructionSet instruction_set,
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700309 size_t thread_count, bool support_debugging)
buzbeec531cef2012-10-18 07:09:20 -0700310 : compiler_backend_(compiler_backend),
311 instruction_set_(instruction_set),
Ian Rogers49c48942012-03-11 15:15:37 -0700312 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800313 thread_count_(thread_count),
314 support_debugging_(support_debugging),
315 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800316 }
Brian Carlstromae826982011-11-09 01:33:42 -0800317
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 if (!Runtime::Create(options, false)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800321 LOG(ERROR) << "Failed to create runtime";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700322 return false;
Brian Carlstromae826982011-11-09 01:33:42 -0800323 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 Runtime* runtime = Runtime::Current();
Brian Carlstromae826982011-11-09 01:33:42 -0800325 // if we loaded an existing image, we will reuse values from the image roots.
Ian Rogers19846512012-02-24 11:42:47 -0800326 if (!runtime->HasResolutionMethod()) {
327 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
328 }
Brian Carlstromae826982011-11-09 01:33:42 -0800329 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
330 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
331 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700332 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800333 }
334 }
Ian Rogers19846512012-02-24 11:42:47 -0800335 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800337 }
338
Brian Carlstromae826982011-11-09 01:33:42 -0800339 // Appends to dex_files any elements of class_path that it doesn't already
340 // contain. This will open those dex files as necessary.
341 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
342 std::vector<std::string> parsed;
343 Split(class_path, ':', parsed);
Ian Rogers33e95662013-05-20 20:29:14 -0700344 // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained.
345 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromae826982011-11-09 01:33:42 -0800346 for (size_t i = 0; i < parsed.size(); ++i) {
347 if (DexFilesContains(dex_files, parsed[i])) {
348 continue;
349 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800350 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800351 if (dex_file == NULL) {
352 LOG(WARNING) << "Failed to open dex file " << parsed[i];
353 } else {
354 dex_files.push_back(dex_file);
355 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500356 }
357 }
Brian Carlstromae826982011-11-09 01:33:42 -0800358
359 // Returns true if dex_files has a dex with the named location.
360 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
361 for (size_t i = 0; i < dex_files.size(); ++i) {
362 if (dex_files[i]->GetLocation() == location) {
363 return true;
364 }
365 }
366 return false;
367 }
368
buzbeec531cef2012-10-18 07:09:20 -0700369 const CompilerBackend compiler_backend_;
370
Ian Rogers49c48942012-03-11 15:15:37 -0700371 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800372
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800373 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800374 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800375 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800376 uint64_t start_ns_;
377
Brian Carlstromae826982011-11-09 01:33:42 -0800378 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
379};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500380
Elliott Hughes72395bf2012-04-24 13:45:26 -0700381static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800382 char* end;
383 int result = strtol(in, &end, 10);
384 if (in == end || *end != '\0') {
385 return false;
386 }
387 *out = result;
388 return true;
389}
390
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700391static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
392 const std::vector<const char*>& dex_locations,
393 std::vector<const DexFile*>& dex_files) {
394 size_t failure_count = 0;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800395 for (size_t i = 0; i < dex_filenames.size(); i++) {
396 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800397 const char* dex_location = dex_locations[i];
398 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800399 if (dex_file == NULL) {
Ian Rogers33e95662013-05-20 20:29:14 -0700400 LOG(WARNING) << "Could not open .dex from file '" << dex_filename << "'\n";
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700401 ++failure_count;
jeffhao60f83e32012-02-13 17:16:30 -0800402 } else {
403 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800404 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800405 }
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700406 return failure_count;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800407}
408
Brian Carlstrombcc29262012-11-02 11:36:03 -0700409// The primary goal of the watchdog is to prevent stuck build servers
410// during development when fatal aborts lead to a cascade of failures
411// that result in a deadlock.
412class WatchDog {
413
414// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks
415#undef CHECK_PTHREAD_CALL
416#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \
417 do { \
418 int rc = call args; \
419 if (rc != 0) { \
420 errno = rc; \
421 std::string message(# call); \
422 message += " failed for "; \
423 message += reason; \
Brian Carlstromed115642013-03-15 16:04:40 -0700424 Fatal(message); \
Brian Carlstrombcc29262012-11-02 11:36:03 -0700425 } \
426 } while (false)
427
428 public:
Brian Carlstrom994d62a2012-11-05 20:43:45 -0800429 WatchDog(bool is_watch_dog_enabled) {
430 is_watch_dog_enabled_ = is_watch_dog_enabled;
431 if (!is_watch_dog_enabled_) {
Brian Carlstrombcc29262012-11-02 11:36:03 -0700432 return;
433 }
434 shutting_down_ = false;
435 const char* reason = "dex2oat watch dog thread startup";
436 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason);
437 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason);
438 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason);
439 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason);
440 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
441 }
442 ~WatchDog() {
Brian Carlstrom994d62a2012-11-05 20:43:45 -0800443 if (!is_watch_dog_enabled_) {
Brian Carlstrombcc29262012-11-02 11:36:03 -0700444 return;
445 }
446 const char* reason = "dex2oat watch dog thread shutdown";
447 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
448 shutting_down_ = true;
449 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason);
450 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
451
452 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason);
453
454 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason);
455 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason);
456 }
457
458 private:
459 static void* CallBack(void* arg) {
460 WatchDog* self = reinterpret_cast<WatchDog*>(arg);
Ian Rogers1d54e732013-05-02 21:10:01 -0700461 ::art::SetThreadName("dex2oat watch dog");
Brian Carlstrombcc29262012-11-02 11:36:03 -0700462 self->Wait();
463 return NULL;
464 }
465
Brian Carlstromed115642013-03-15 16:04:40 -0700466 static void Message(char severity, const std::string& message) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700467 // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error
468 // cases.
Brian Carlstromed115642013-03-15 16:04:40 -0700469 fprintf(stderr, "dex2oat%s %c %d %d %s\n",
470 kIsDebugBuild ? "d" : "",
471 severity,
472 getpid(),
473 GetTid(),
474 message.c_str());
Ian Rogerseb5cb602013-02-25 15:06:01 -0800475 }
476
Brian Carlstromed115642013-03-15 16:04:40 -0700477 static void Warn(const std::string& message) {
478 Message('W', message);
479 }
480
481 static void Fatal(const std::string& message) {
482 Message('F', message);
Brian Carlstrombcc29262012-11-02 11:36:03 -0700483 exit(1);
484 }
485
486 void Wait() {
Ian Rogerseb5cb602013-02-25 15:06:01 -0800487 bool warning = true;
Ian Rogerseb5cb602013-02-25 15:06:01 -0800488 CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds);
Ian Rogers1d54e732013-05-02 21:10:01 -0700489 // TODO: tune the multiplier for GC verification, the following is just to make the timeout
490 // large.
491 int64_t multiplier = gc::kDesiredHeapVerification > gc::kVerifyAllFast ? 100 : 1;
Brian Carlstromed115642013-03-15 16:04:40 -0700492 timespec warning_ts;
Ian Rogers1d54e732013-05-02 21:10:01 -0700493 InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts);
Brian Carlstromed115642013-03-15 16:04:40 -0700494 timespec timeout_ts;
Ian Rogers1d54e732013-05-02 21:10:01 -0700495 InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts);
Brian Carlstrombcc29262012-11-02 11:36:03 -0700496 const char* reason = "dex2oat watch dog thread waiting";
497 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
498 while (!shutting_down_) {
Ian Rogerseb5cb602013-02-25 15:06:01 -0800499 int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_,
500 warning ? &warning_ts
501 : &timeout_ts));
Brian Carlstrombcc29262012-11-02 11:36:03 -0700502 if (rc == ETIMEDOUT) {
503 std::string message(StringPrintf("dex2oat did not finish after %d seconds",
Ian Rogerseb5cb602013-02-25 15:06:01 -0800504 warning ? kWatchDogWarningSeconds
505 : kWatchDogTimeoutSeconds));
506 if (warning) {
507 Warn(message.c_str());
508 warning = false;
509 } else {
Brian Carlstromed115642013-03-15 16:04:40 -0700510 Fatal(message.c_str());
Ian Rogerseb5cb602013-02-25 15:06:01 -0800511 }
512 } else if (rc != 0) {
Brian Carlstrombcc29262012-11-02 11:36:03 -0700513 std::string message(StringPrintf("pthread_cond_timedwait failed: %s",
514 strerror(errno)));
Brian Carlstromed115642013-03-15 16:04:40 -0700515 Fatal(message.c_str());
Brian Carlstrombcc29262012-11-02 11:36:03 -0700516 }
517 }
518 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
519 }
520
Brian Carlstromed115642013-03-15 16:04:40 -0700521 // When setting timeouts, keep in mind that the build server may not be as fast as your desktop.
522#if ART_USE_PORTABLE_COMPILER
523 static const unsigned int kWatchDogWarningSeconds = 2 * 60; // 2 minutes.
Ian Rogerseb5cb602013-02-25 15:06:01 -0800524 static const unsigned int kWatchDogTimeoutSeconds = 30 * 60; // 25 minutes + buffer.
Brian Carlstrombcc29262012-11-02 11:36:03 -0700525#else
Brian Carlstromed115642013-03-15 16:04:40 -0700526 static const unsigned int kWatchDogWarningSeconds = 1 * 60; // 1 minute.
527 static const unsigned int kWatchDogTimeoutSeconds = 6 * 60; // 5 minutes + buffer.
Brian Carlstrombcc29262012-11-02 11:36:03 -0700528#endif
529
Brian Carlstrom994d62a2012-11-05 20:43:45 -0800530 bool is_watch_dog_enabled_;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700531 bool shutting_down_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700532 // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases.
Brian Carlstrombcc29262012-11-02 11:36:03 -0700533 pthread_mutex_t mutex_;
534 pthread_cond_t cond_;
535 pthread_attr_t attr_;
536 pthread_t pthread_;
537};
Ian Rogers1c653d52013-06-13 15:04:30 -0700538const unsigned int WatchDog::kWatchDogWarningSeconds;
539const unsigned int WatchDog::kWatchDogTimeoutSeconds;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700540
Elliott Hughes72395bf2012-04-24 13:45:26 -0700541static int dex2oat(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -0700542 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -0700543
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700544 // Skip over argv[0].
545 argv++;
546 argc--;
547
548 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800549 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700550 }
551
552 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800553 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800554 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800555 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700556 std::string oat_filename;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800557 std::string oat_symbols;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800558 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800559 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800560 std::string bitcode_filename;
Brian Carlstromae826982011-11-09 01:33:42 -0800561 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800562 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800563 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700564 uintptr_t image_base = 0;
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700565 UniquePtr<std::string> host_prefix;
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800566 std::string android_root;
jeffhao5d840402011-10-24 17:09:45 -0700567 std::vector<const char*> runtime_args;
Elliott Hughes5c599942012-06-13 16:45:05 -0700568 int thread_count = sysconf(_SC_NPROCESSORS_CONF);
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800569 bool support_debugging = false;
buzbeec531cef2012-10-18 07:09:20 -0700570#if defined(ART_USE_PORTABLE_COMPILER)
571 CompilerBackend compiler_backend = kPortable;
buzbeec531cef2012-10-18 07:09:20 -0700572#else
573 CompilerBackend compiler_backend = kQuick;
574#endif
jeffhao9ad4f222012-05-30 18:51:19 -0700575#if defined(__arm__)
Ian Rogers49c48942012-03-11 15:15:37 -0700576 InstructionSet instruction_set = kThumb2;
jeffhao9ad4f222012-05-30 18:51:19 -0700577#elif defined(__i386__)
578 InstructionSet instruction_set = kX86;
579#elif defined(__mips__)
580 InstructionSet instruction_set = kMips;
581#else
582#error "Unsupported architecture"
583#endif
Brian Carlstrom265091e2013-01-30 14:08:26 -0800584 bool is_host = false;
Elliott Hughes67d92002012-03-26 15:08:51 -0700585 bool dump_stats = kIsDebugBuild;
586 bool dump_timings = kIsDebugBuild;
Brian Carlstrom994d62a2012-11-05 20:43:45 -0800587 bool watch_dog_enabled = !kIsTargetBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700588
Anwar Ghuloumc4f105d2013-04-10 16:12:11 -0700589
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700590 for (int i = 0; i < argc; i++) {
591 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800592 bool log_options = false;
593 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700594 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
595 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700596 if (option.starts_with("--dex-file=")) {
597 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800598 } else if (option.starts_with("--dex-location=")) {
599 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800600 } else if (option.starts_with("--zip-fd=")) {
601 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800602 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800603 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800604 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800605 } else if (option.starts_with("--zip-location=")) {
606 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800607 } else if (option.starts_with("--oat-file=")) {
608 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800609 } else if (option.starts_with("--oat-symbols=")) {
610 oat_symbols = option.substr(strlen("--oat-symbols=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800611 } else if (option.starts_with("--oat-fd=")) {
612 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800613 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800614 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800615 }
Brian Carlstrom994d62a2012-11-05 20:43:45 -0800616 } else if (option == "-g") {
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800617 support_debugging = true;
Brian Carlstrom994d62a2012-11-05 20:43:45 -0800618 } else if (option == "--watch-dog") {
619 watch_dog_enabled = true;
620 } else if (option == "--no-watch-dog") {
621 watch_dog_enabled = false;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800622 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800623 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800624 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800625 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800626 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800627 } else if (option.starts_with("--oat-location=")) {
628 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800629 } else if (option.starts_with("--bitcode=")) {
630 bitcode_filename = option.substr(strlen("--bitcode=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700631 } else if (option.starts_with("--image=")) {
632 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800633 } else if (option.starts_with("--image-classes=")) {
634 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700635 } else if (option.starts_with("--base=")) {
636 const char* image_base_str = option.substr(strlen("--base=")).data();
637 char* end;
638 image_base = strtoul(image_base_str, &end, 16);
639 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800640 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700641 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700642 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800643 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700644 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700645 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800646 } else if (option.starts_with("--android-root=")) {
647 android_root = option.substr(strlen("--android-root=")).data();
Ian Rogers49c48942012-03-11 15:15:37 -0700648 } else if (option.starts_with("--instruction-set=")) {
649 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
jeffhao1f71ae82012-05-24 16:08:24 -0700650 if (instruction_set_str == "arm") {
Ian Rogers49c48942012-03-11 15:15:37 -0700651 instruction_set = kThumb2;
jeffhao1f71ae82012-05-24 16:08:24 -0700652 } else if (instruction_set_str == "mips") {
Ian Rogers49c48942012-03-11 15:15:37 -0700653 instruction_set = kMips;
jeffhao1f71ae82012-05-24 16:08:24 -0700654 } else if (instruction_set_str == "x86") {
Ian Rogers49c48942012-03-11 15:15:37 -0700655 instruction_set = kX86;
656 }
buzbeec531cef2012-10-18 07:09:20 -0700657 } else if (option.starts_with("--compiler-backend=")) {
658 StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data();
659 if (backend_str == "Quick") {
660 compiler_backend = kQuick;
buzbeec531cef2012-10-18 07:09:20 -0700661 } else if (backend_str == "Portable") {
662 compiler_backend = kPortable;
663 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800664 } else if (option == "--host") {
665 is_host = true;
jeffhao5d840402011-10-24 17:09:45 -0700666 } else if (option == "--runtime-arg") {
667 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800668 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700669 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800670 if (log_options) {
671 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
672 }
jeffhao5d840402011-10-24 17:09:45 -0700673 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700674 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800675 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700676 }
677 }
678
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800679 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800680 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800681 }
682
683 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800684 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800685 }
686
Brian Carlstrom265091e2013-01-30 14:08:26 -0800687 if (!oat_symbols.empty() && oat_fd != -1) {
688 Usage("--oat-symbols should not be used with --oat-fd");
689 }
690
691 if (!oat_symbols.empty() && is_host) {
692 Usage("--oat-symbols should not be used with --host");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800693 }
694
Brian Carlstroma004aa92012-02-08 18:05:09 -0800695 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800696 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700697 }
698
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700699 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800700 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
701 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700702 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800703 }
704 }
705
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800706 if (android_root.empty()) {
707 const char* android_root_env_var = getenv("ANDROID_ROOT");
708 if (android_root_env_var == NULL) {
Brian Carlstrom54d22c22013-03-13 15:14:57 -0700709 Usage("--android-root unspecified and ANDROID_ROOT not set");
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800710 }
711 android_root += android_root_env_var;
712 }
713
Brian Carlstroma004aa92012-02-08 18:05:09 -0800714 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800715 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700716 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800717 boot_image_filename += GetAndroidRoot();
718 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700719 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800720 boot_image_filename += "/system";
721 }
722 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800723 }
724 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800725 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800726 boot_image_option += "-Ximage:";
727 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700728 }
729
Brian Carlstromae826982011-11-09 01:33:42 -0800730 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800731 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800732 }
733
734 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800735 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700736 }
737
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800738 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800739 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800740 }
741
742 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800743 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800744 }
745
Brian Carlstroma004aa92012-02-08 18:05:09 -0800746 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800747 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800748 }
749
Brian Carlstroma004aa92012-02-08 18:05:09 -0800750 if (dex_locations.empty()) {
751 for (size_t i = 0; i < dex_filenames.size(); i++) {
752 dex_locations.push_back(dex_filenames[i]);
753 }
754 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800755 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800756 }
757
758 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800759 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800760 }
761
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700762 if (boot_image_option.empty()) {
763 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800764 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700765 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700766 }
767
Brian Carlstrom265091e2013-01-30 14:08:26 -0800768 std::string oat_stripped(oat_filename);
769 std::string oat_unstripped;
770 if (!oat_symbols.empty()) {
771 oat_unstripped += oat_symbols;
772 } else {
773 oat_unstripped += oat_filename;
774 }
775
Brian Carlstrom994d62a2012-11-05 20:43:45 -0800776 // Done with usage checks, enable watchdog if requested
777 WatchDog watch_dog(watch_dog_enabled);
778
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800779 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800780 UniquePtr<File> oat_file;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800781 bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700782 if (create_file) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800783 oat_file.reset(OS::OpenFile(oat_unstripped.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800784 if (oat_location.empty()) {
785 oat_location = oat_filename;
786 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800787 } else {
Elliott Hughes76160052012-12-12 16:31:20 -0800788 oat_file.reset(new File(oat_fd, oat_location));
789 oat_file->DisableAutoClose();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800790 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800791 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700792 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
793 return EXIT_FAILURE;
794 }
795 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
796 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800797 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700798 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800799
Brian Carlstroma004aa92012-02-08 18:05:09 -0800800 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700801
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700802 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700803 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800804 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700805 if (boot_image_option.empty()) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700806 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
807 if (failure_count > 0) {
808 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
809 return EXIT_FAILURE;
810 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800811 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700812 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700813 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
814 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700815 if (host_prefix.get() != NULL) {
816 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700817 }
jeffhao5d840402011-10-24 17:09:45 -0700818 for (size_t i = 0; i < runtime_args.size(); i++) {
819 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
820 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700821
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700822#if ART_SMALL_MODE
823 options.push_back(std::make_pair("-small", reinterpret_cast<void*>(NULL)));
824#endif // ART_SMALL_MODE
825
Dragos Sbirlea7467ee02013-06-21 09:20:34 -0700826
827#ifdef ART_SEA_IR_MODE
828 options.push_back(std::make_pair("-sea_ir", reinterpret_cast<void*>(NULL)));
829#endif
830
831
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 Dex2Oat* p_dex2oat;
Brian Carlstrom96391602013-06-13 19:49:50 -0700833 if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count,
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700834 support_debugging)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 LOG(ERROR) << "Failed to create dex2oat";
836 return EXIT_FAILURE;
837 }
838 UniquePtr<Dex2Oat> dex2oat(p_dex2oat);
839 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
840 // give it away now and then switch to a more managable ScopedObjectAccess.
841 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
842 // Whilst we're in native take the opportunity to initialize well known classes.
843 WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv());
844 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700845
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800846 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstrom96391602013-06-13 19:49:50 -0700847 UniquePtr<CompilerDriver::DescriptorSet> image_classes(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800848 if (image_classes_filename != NULL) {
Brian Carlstrom96391602013-06-13 19:49:50 -0700849 image_classes.reset(dex2oat->ReadImageClasses(image_classes_filename));
Brian Carlstromae826982011-11-09 01:33:42 -0800850 if (image_classes.get() == NULL) {
851 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
852 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700853 }
854 }
855
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800856 std::vector<const DexFile*> dex_files;
857 if (boot_image_option.empty()) {
858 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
859 } else {
860 if (dex_filenames.empty()) {
861 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
862 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800863 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800864 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800865 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800866 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800867 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800868 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800869 return EXIT_FAILURE;
870 }
871 dex_files.push_back(dex_file);
872 } else {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700873 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
874 if (failure_count > 0) {
875 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
876 return EXIT_FAILURE;
877 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800878 }
879 }
880
Anwar Ghuloum8447d842013-04-30 17:27:40 -0700881 // If we're in small mode, but the program is small, turn off small mode.
882 // It doesn't make a difference for the boot image, so let's skip the check
883 // altogether.
884 if (Runtime::Current()->IsSmallMode() && !image) {
885 size_t num_methods = 0;
886 for (size_t i = 0; i != dex_files.size(); ++i) {
887 const DexFile* dex_file = dex_files[i];
888 CHECK(dex_file != NULL);
889 num_methods += dex_file->NumMethodIds();
890 }
891 if (num_methods <= Runtime::Current()->GetSmallModeMethodThreshold()) {
892 Runtime::Current()->SetSmallMode(false);
893 LOG(INFO) << "Below method threshold, compiling anyways";
894 }
895 }
Anwar Ghuloumc4f105d2013-04-10 16:12:11 -0700896
Ian Rogers1212a022013-03-04 10:48:41 -0800897 UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option,
898 host_prefix.get(),
Brian Carlstrom3f47c122013-03-07 00:02:40 -0800899 android_root,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800900 is_host,
Ian Rogers1212a022013-03-04 10:48:41 -0800901 dex_files,
902 oat_file.get(),
903 bitcode_filename,
904 image,
Brian Carlstrom96391602013-06-13 19:49:50 -0700905 image_classes,
Ian Rogers1212a022013-03-04 10:48:41 -0800906 dump_stats,
907 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700908
909 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800910 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700911 return EXIT_FAILURE;
912 }
913
Brian Carlstrom265091e2013-01-30 14:08:26 -0800914 LOG(INFO) << "Oat file written successfully (unstripped): " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700915
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800916 // Notes on the interleaving of creating the image and oat file to
917 // ensure the references between the two are correct.
918 //
919 // Currently we have a memory layout that looks something like this:
920 //
921 // +--------------+
922 // | image |
923 // +--------------+
924 // | boot oat |
925 // +--------------+
926 // | alloc spaces |
927 // +--------------+
928 //
929 // There are several constraints on the loading of the imag and boot.oat.
930 //
931 // 1. The image is expected to be loaded at an absolute address and
932 // contains Objects with absolute pointers within the image.
933 //
934 // 2. There are absolute pointers from Methods in the image to their
935 // code in the oat.
936 //
937 // 3. There are absolute pointers from the code in the oat to Methods
938 // in the image.
939 //
940 // 4. There are absolute pointers from code in the oat to other code
941 // in the oat.
942 //
943 // To get this all correct, we go through several steps.
944 //
945 // 1. We have already created that oat file above with
946 // CreateOatFile. Originally this was just our own proprietary file
947 // but now it is contained within an ELF dynamic object (aka .so
948 // file). The Compiler returned by CreateOatFile provides
949 // PatchInformation for references to oat code and Methods that need
950 // to be update once we know where the oat file will be located
951 // after the image.
952 //
953 // 2. We create the image file. It needs to know where the oat file
954 // will be loaded after itself. Originally when oat file was simply
955 // memory mapped so we could predict where its contents were based
956 // on the file size. Now that it is an ELF file, we need to inspect
957 // the ELF file to understand the in memory segment layout including
958 // where the oat header is located within. ImageWriter's
959 // PatchOatCodeAndMethods uses the PatchInformation from the
960 // Compiler to touch up absolute references in the oat file.
961 //
962 // 3. We fixup the ELF program headers so that dlopen will try to
963 // load the .so at the desired location at runtime by offsetting the
964 // Elf32_Phdr.p_vaddr values by the desired base address.
965 //
Brian Carlstrom265091e2013-01-30 14:08:26 -0800966 if (image) {
967 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
968 bool image_creation_success = dex2oat->CreateImageFile(image_filename,
969 image_base,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800970 oat_unstripped,
971 oat_location,
972 *compiler.get());
973 Thread::Current()->TransitionFromSuspendedToRunnable();
974 LOG(INFO) << "Image written successfully: " << image_filename;
975 if (!image_creation_success) {
976 return EXIT_FAILURE;
977 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700978 }
979
Brian Carlstrom265091e2013-01-30 14:08:26 -0800980 if (is_host) {
981 return EXIT_SUCCESS;
982 }
983
984 // If we don't want to strip in place, copy from unstripped location to stripped location.
985 // We need to strip after image creation because FixupElf needs to use .strtab.
986 if (oat_unstripped != oat_stripped) {
987 oat_file.reset();
988 UniquePtr<File> in(OS::OpenFile(oat_unstripped.c_str(), false));
989 UniquePtr<File> out(OS::OpenFile(oat_stripped.c_str(), true));
990 size_t buffer_size = 8192;
991 UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]);
992 while (true) {
993 int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size));
994 if (bytes_read <= 0) {
995 break;
996 }
997 bool write_ok = out->WriteFully(buffer.get(), bytes_read);
998 CHECK(write_ok);
999 }
1000 oat_file.reset(out.release());
1001 LOG(INFO) << "Oat file copied successfully (stripped): " << oat_stripped;
1002 }
1003
1004 // Strip unneeded sections for target
1005 off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET);
1006 CHECK_EQ(0, seek_actual);
1007 compiler->StripElf(oat_file.get());
1008
Brian Carlstromae826982011-11-09 01:33:42 -08001009 // We wrote the oat file successfully, and want to keep it.
Brian Carlstrom54d22c22013-03-13 15:14:57 -07001010 LOG(INFO) << "Oat file written successfully (stripped): " << oat_location;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001011 return EXIT_SUCCESS;
1012}
1013
1014} // namespace art
1015
1016int main(int argc, char** argv) {
1017 return art::dex2oat(argc, argv);
1018}