blob: e3b79d8965f3771a67ab6e1e69c96227f12957c4 [file] [log] [blame]
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4#include <stdlib.h>
Elliott Hughes234da572011-11-03 22:13:06 -07005#include <sys/file.h>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07006
7#include <string>
8#include <vector>
9
10#include "class_linker.h"
11#include "class_loader.h"
12#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070013#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070014#include "image_writer.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070015#include "oat_writer.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070016#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070017#include "runtime.h"
18#include "stringpiece.h"
19
20namespace art {
21
22static void usage() {
23 fprintf(stderr,
24 "Usage: dex2oat [options]...\n"
25 "\n");
26 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070027 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
28 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070029 " Example: --dex-file=/system/framework/core.jar\n"
30 "\n");
31 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070032 " --image=<file.art>: specifies the required output image filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070033 " Example: --image=/data/art-cache/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070034 "\n");
35 // TODO: remove this by inferring from --image
36 fprintf(stderr,
37 " --oat=<file.oat>: specifies the required oat filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070038 " Example: --image=/data/art-cache/boot.oat\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070039 "\n");
40 fprintf(stderr,
41 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
42 " Example: --base=0x50000000\n"
43 "\n");
44 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070046 " Example: --boot-image=/data/art-cache/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070047 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070048 fprintf(stderr,
49 " --method may be used to limit compilation to a subset of methods.\n"
50 " Example: --method=Ljava/lang/Object;<init>()V\n"
51 "\n");
Brian Carlstrom16192862011-09-12 17:50:06 -070052 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070053 " --host-prefix may be used to translate host paths to target paths during\n"
54 " cross compilation.\n"
55 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070056 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070057 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070058 " --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
59 " such as initial heap size, maximum heap size, and verbose output.\n"
60 " Use a separate --runtime-arg switch for each argument.\n"
61 " Example: --runtime-arg -Xms256m\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070062 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070063 exit(EXIT_FAILURE);
64}
65
Elliott Hughes234da572011-11-03 22:13:06 -070066class FileJanitor {
67public:
68 FileJanitor(const std::string& filename, int fd)
69 : filename_(filename), fd_(fd), do_unlink_(true) {
70 }
71
72 void KeepFile() {
73 do_unlink_ = false;
74 }
75
76 ~FileJanitor() {
77 if (fd_ != -1) {
78 flock(fd_, LOCK_UN);
79 }
80 if (do_unlink_) {
81 unlink(filename_.c_str());
82 }
83 }
84
85private:
86 std::string filename_;
87 int fd_;
88 bool do_unlink_;
89};
90
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070091int dex2oat(int argc, char** argv) {
92 // Skip over argv[0].
93 argv++;
94 argc--;
95
96 if (argc == 0) {
97 fprintf(stderr, "no arguments specified\n");
98 usage();
99 }
100
101 std::vector<const char*> dex_filenames;
102 std::vector<const char*> method_names;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700104 const char* image_filename = NULL;
105 std::string boot_image_option;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700106 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700107 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700108 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700109
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700110 for (int i = 0; i < argc; i++) {
111 const StringPiece option(argv[i]);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700112 if (false) {
113 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
114 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700115 if (option.starts_with("--dex-file=")) {
116 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
117 } else if (option.starts_with("--method=")) {
118 method_names.push_back(option.substr(strlen("--method=")).data());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 } else if (option.starts_with("--oat=")) {
120 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700121 } else if (option.starts_with("--image=")) {
122 image_filename = option.substr(strlen("--image=")).data();
123 } else if (option.starts_with("--base=")) {
124 const char* image_base_str = option.substr(strlen("--base=")).data();
125 char* end;
126 image_base = strtoul(image_base_str, &end, 16);
127 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700128 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700129 usage();
130 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 } else if (option.starts_with("--boot-image=")) {
132 const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700133 boot_image_option.clear();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700134 boot_image_option += "-Ximage:";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700135 boot_image_option += boot_image_filename;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700136 } else if (option.starts_with("--host-prefix=")) {
137 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700138 } else if (option == "--runtime-arg") {
139 if (++i >= argc) {
140 fprintf(stderr, "Missing required argument for --runtime-arg\n");
141 usage();
142 }
143 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700144 } else {
145 fprintf(stderr, "unknown argument %s\n", option.data());
146 usage();
147 }
148 }
149
Brian Carlstrome24fa612011-09-29 00:53:55 -0700150 if (oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700151 fprintf(stderr, "--oat file name not specified\n");
152 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 }
154
Brian Carlstromaded5f72011-10-07 17:15:04 -0700155 if (image_filename == NULL && boot_image_option.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700156 fprintf(stderr, "Either --image or --boot-image must be specified\n");
157 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700158 }
159
Brian Carlstrom78128a62011-09-15 17:21:19 -0700160 if (dex_filenames.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700161 fprintf(stderr, "no --dex-file values specified\n");
162 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700163 }
164
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700165 if (boot_image_option.empty()) {
166 if (image_base == 0) {
167 fprintf(stderr, "non-zero --base not specified\n");
168 return EXIT_FAILURE;
169 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700170 }
171
Elliott Hughes234da572011-11-03 22:13:06 -0700172 // Create the output file if we can, or open it read-only if we weren't first.
173 bool did_create = true;
174 int fd = open(oat_filename.c_str(), O_EXCL | O_CREAT | O_TRUNC | O_RDWR, 0666);
175 if (fd == -1) {
176 if (errno != EEXIST) {
Ian Rogers5e863dd2011-11-02 20:00:10 -0700177 PLOG(ERROR) << "Unable to create oat file " << oat_filename;
178 return EXIT_FAILURE;
179 }
Elliott Hughes234da572011-11-03 22:13:06 -0700180 did_create = false;
181 fd = open(oat_filename.c_str(), O_RDONLY);
182 if (fd == -1) {
183 PLOG(ERROR) << "Unable to open oat file for reading " << oat_filename;
184 return EXIT_FAILURE;
185 }
Ian Rogers5e863dd2011-11-02 20:00:10 -0700186 }
Elliott Hughes234da572011-11-03 22:13:06 -0700187
188 // Handles removing the file on failure and unlocking on both failure and success.
189 FileJanitor file_janitor(oat_filename, fd);
190
191 // TODO: TEMP_FAILURE_RETRY on flock calls
192
193 // If we won the creation race, block trying to take the lock (since we're going to be doing
194 // the work, we need the lock). If we lost the creation race, spin trying to take the lock
195 // non-blocking until we fail -- at which point we know the other guy has the lock -- and then
196 // block trying to take the now-taken lock.
197 if (did_create) {
198 LOG(INFO) << "This process created " << oat_filename;
199 while (flock(fd, LOCK_EX) != 0) {
200 // Try again.
201 }
202 LOG(INFO) << "This process created and locked " << oat_filename;
203 } else {
204 LOG(INFO) << "Another process has already created " << oat_filename;
205 while (flock(fd, LOCK_EX | LOCK_NB) == 0) {
206 // Give up the lock and hope the creator has taken the lock next time round.
207 flock(fd, LOCK_UN);
208 }
209 // Now a non-blocking attempt to take the lock has failed, we know the other guy has the
210 // lock, so block waiting to take it.
211 LOG(INFO) << "Another process is already working on " << oat_filename;
212 if (flock(fd, LOCK_EX) != 0) {
213 PLOG(ERROR) << "Waiter unable to wait for creator to finish " << oat_filename;
214 return EXIT_FAILURE;
215 }
216 // We have the lock and the creator has finished.
217 // TODO: check the creator did a good job by checking the header.
218 LOG(INFO) << "Another process finished working on " << oat_filename;
219 // Job done.
220 file_janitor.KeepFile();
221 return EXIT_SUCCESS;
222 }
223
224 // If we get this far, we won the creation race and have locked the file.
225 UniquePtr<File> oat_file(OS::FileFromFd(oat_filename.c_str(), fd));
226
227 LOG(INFO) << "dex2oat: " << oat_file->name();
Ian Rogers5e863dd2011-11-02 20:00:10 -0700228
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700229 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700230 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700231 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700232 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700233 boot_class_path_string += "-Xbootclasspath:";
234 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
235 boot_class_path_string += dex_filenames[i];
236 boot_class_path_string += ":";
237 }
238 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
239 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700240 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700241 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
242 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700243 if (!host_prefix.empty()) {
244 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
245 }
jeffhao5d840402011-10-24 17:09:45 -0700246 for (size_t i = 0; i < runtime_args.size(); i++) {
247 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
248 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700249 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
250 if (runtime.get() == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700251 LOG(ERROR) << "Could not create runtime";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700252 return EXIT_FAILURE;
253 }
254 ClassLinker* class_linker = runtime->GetClassLinker();
255
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256 // If we have an existing boot image, position new space after its oat file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700257 if (Heap::GetSpaces().size() > 1) {
258 Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
259 CHECK(last_image_space != NULL);
260 CHECK(last_image_space->IsImageSpace());
261 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
262 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700264 }
265
266 // ClassLoader creation needs to come after Runtime::Create
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700267 SirtRef<ClassLoader> class_loader(NULL);
268 if (!boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700269 std::vector<const DexFile*> dex_files;
270 DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700271 for (size_t i = 0; i < dex_files.size(); i++) {
272 class_linker->RegisterDexFile(*dex_files[i]);
273 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700274 class_loader.reset(PathClassLoader::AllocCompileTime(dex_files));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700275 }
276
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277 // if we loaded an existing image, we will reuse values from the image roots.
Brian Carlstrom16192862011-09-12 17:50:06 -0700278 if (!runtime->HasJniStubArray()) {
279 runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
280 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281 if (!runtime->HasAbstractMethodErrorStubArray()) {
282 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2));
283 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700284 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700285 Runtime::TrampolineType type = Runtime::TrampolineType(i);
286 if (!runtime->HasResolutionStubArray(type)) {
287 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type);
288 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700289 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700290 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
291 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
292 if (!runtime->HasCalleeSaveMethod(type)) {
293 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2, type), type);
294 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700295 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700296 Compiler compiler(kThumb2, image_filename != NULL);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700297 if (method_names.empty()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700298 compiler.CompileAll(class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700299 } else {
300 for (size_t i = 0; i < method_names.size(); i++) {
301 // names are actually class_descriptor + name + signature.
302 // example: Ljava/lang/Object;<init>()V
303 StringPiece method_name = method_names[i];
304 size_t end_of_class_descriptor = method_name.find(';');
305 if (end_of_class_descriptor == method_name.npos) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700306 LOG(ERROR) << "Could not find class descriptor in method " << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700307 return EXIT_FAILURE;
308 }
309 end_of_class_descriptor++; // want to include ;
310 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
311 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
312 if (end_of_name == method_name.npos) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700313 LOG(ERROR) << "Could not find start of method signature in method '" << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700314 return EXIT_FAILURE;
315 }
316 std::string name = method_name.substr(end_of_class_descriptor,
317 end_of_name - end_of_class_descriptor).ToString();
318 std::string signature = method_name.substr(end_of_name).ToString();
319
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700320 Class* klass = class_linker->FindClass(class_descriptor, class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700321 if (klass == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700322 LOG(ERROR) << "Could not find class for descriptor '" << class_descriptor
323 << "' in method '" << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700324 return EXIT_FAILURE;
325 }
326 Method* method = klass->FindDirectMethod(name, signature);
327 if (method == NULL) {
328 method = klass->FindVirtualMethod(name, signature);
329 }
330 if (method == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700331 LOG(ERROR) << "Could not find method '" << method_name << "' with signature '"
332 << signature << "' in class '" << class_descriptor << "' for method argument '"
333 << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700334 return EXIT_FAILURE;
335 }
336 compiler.CompileOne(method);
337 }
338 }
339
Elliott Hughes234da572011-11-03 22:13:06 -0700340 if (!OatWriter::Create(oat_file.get(), class_loader.get(), compiler)) {
341 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700342 return EXIT_FAILURE;
343 }
344
Brian Carlstromaded5f72011-10-07 17:15:04 -0700345 if (image_filename == NULL) {
Ian Rogers97170512011-11-06 21:06:20 -0800346 file_janitor.KeepFile();
347 LOG(INFO) << "Oat file written successfully " << oat_file->name();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700348 return EXIT_SUCCESS;
349 }
350 CHECK(compiler.IsImage());
351
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352 ImageWriter image_writer;
Elliott Hughes234da572011-11-03 22:13:06 -0700353 if (!image_writer.Write(image_filename, image_base, oat_file->name(), host_prefix)) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700354 LOG(ERROR) << "Failed to create image file " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700355 return EXIT_FAILURE;
356 }
357
Elliott Hughes234da572011-11-03 22:13:06 -0700358 // We wrote the file successfully, and want to keep it.
359 LOG(INFO) << "Image written successfully " << image_filename;
360 file_janitor.KeepFile();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700361 return EXIT_SUCCESS;
362}
363
364} // namespace art
365
366int main(int argc, char** argv) {
367 return art::dex2oat(argc, argv);
368}