blob: 6cf6df60f9cb188f43f978ac5fb6e44b75b5cea6 [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) {
Elliott Hughesb1f9f222011-11-04 18:04:00 -070078 int rc = TEMP_FAILURE_RETRY(flock(fd_, LOCK_UN));
79 if (rc == -1) {
80 PLOG(ERROR) << "Failed to unlock " << filename_;
81 }
Elliott Hughes234da572011-11-03 22:13:06 -070082 }
83 if (do_unlink_) {
Elliott Hughesb1f9f222011-11-04 18:04:00 -070084 int rc = TEMP_FAILURE_RETRY(unlink(filename_.c_str()));
85 if (rc == -1) {
86 PLOG(ERROR) << "Failed to unlink " << filename_;
87 }
Elliott Hughes234da572011-11-03 22:13:06 -070088 }
89 }
90
91private:
92 std::string filename_;
93 int fd_;
94 bool do_unlink_;
95};
96
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070097int dex2oat(int argc, char** argv) {
98 // Skip over argv[0].
99 argv++;
100 argc--;
101
102 if (argc == 0) {
103 fprintf(stderr, "no arguments specified\n");
104 usage();
105 }
106
107 std::vector<const char*> dex_filenames;
108 std::vector<const char*> method_names;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700110 const char* image_filename = NULL;
111 std::string boot_image_option;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700112 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700113 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700114 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700115
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700116 for (int i = 0; i < argc; i++) {
117 const StringPiece option(argv[i]);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700118 if (false) {
119 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
120 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700121 if (option.starts_with("--dex-file=")) {
122 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
123 } else if (option.starts_with("--method=")) {
124 method_names.push_back(option.substr(strlen("--method=")).data());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125 } else if (option.starts_with("--oat=")) {
126 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700127 } else if (option.starts_with("--image=")) {
128 image_filename = option.substr(strlen("--image=")).data();
129 } else if (option.starts_with("--base=")) {
130 const char* image_base_str = option.substr(strlen("--base=")).data();
131 char* end;
132 image_base = strtoul(image_base_str, &end, 16);
133 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700134 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700135 usage();
136 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 } else if (option.starts_with("--boot-image=")) {
138 const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700139 boot_image_option.clear();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700140 boot_image_option += "-Ximage:";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700141 boot_image_option += boot_image_filename;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700142 } else if (option.starts_with("--host-prefix=")) {
143 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700144 } else if (option == "--runtime-arg") {
145 if (++i >= argc) {
146 fprintf(stderr, "Missing required argument for --runtime-arg\n");
147 usage();
148 }
149 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700150 } else {
151 fprintf(stderr, "unknown argument %s\n", option.data());
152 usage();
153 }
154 }
155
Brian Carlstrome24fa612011-09-29 00:53:55 -0700156 if (oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700157 fprintf(stderr, "--oat file name not specified\n");
158 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159 }
160
Brian Carlstromaded5f72011-10-07 17:15:04 -0700161 if (image_filename == NULL && boot_image_option.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700162 fprintf(stderr, "Either --image or --boot-image must be specified\n");
163 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700164 }
165
Brian Carlstrom78128a62011-09-15 17:21:19 -0700166 if (dex_filenames.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700167 fprintf(stderr, "no --dex-file values specified\n");
168 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700169 }
170
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700171 if (boot_image_option.empty()) {
172 if (image_base == 0) {
173 fprintf(stderr, "non-zero --base not specified\n");
174 return EXIT_FAILURE;
175 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700176 }
177
Elliott Hughes234da572011-11-03 22:13:06 -0700178 // Create the output file if we can, or open it read-only if we weren't first.
179 bool did_create = true;
180 int fd = open(oat_filename.c_str(), O_EXCL | O_CREAT | O_TRUNC | O_RDWR, 0666);
181 if (fd == -1) {
182 if (errno != EEXIST) {
Ian Rogers5e863dd2011-11-02 20:00:10 -0700183 PLOG(ERROR) << "Unable to create oat file " << oat_filename;
184 return EXIT_FAILURE;
185 }
Elliott Hughes234da572011-11-03 22:13:06 -0700186 did_create = false;
187 fd = open(oat_filename.c_str(), O_RDONLY);
188 if (fd == -1) {
189 PLOG(ERROR) << "Unable to open oat file for reading " << oat_filename;
190 return EXIT_FAILURE;
191 }
Ian Rogers5e863dd2011-11-02 20:00:10 -0700192 }
Elliott Hughes234da572011-11-03 22:13:06 -0700193
194 // Handles removing the file on failure and unlocking on both failure and success.
195 FileJanitor file_janitor(oat_filename, fd);
196
Elliott Hughes234da572011-11-03 22:13:06 -0700197 // If we won the creation race, block trying to take the lock (since we're going to be doing
198 // the work, we need the lock). If we lost the creation race, spin trying to take the lock
199 // non-blocking until we fail -- at which point we know the other guy has the lock -- and then
200 // block trying to take the now-taken lock.
201 if (did_create) {
202 LOG(INFO) << "This process created " << oat_filename;
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700203 while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700204 // Try again.
205 }
206 LOG(INFO) << "This process created and locked " << oat_filename;
207 } else {
208 LOG(INFO) << "Another process has already created " << oat_filename;
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700209 while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700210 // Give up the lock and hope the creator has taken the lock next time round.
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700211 int rc = TEMP_FAILURE_RETRY(flock(fd, LOCK_UN));
212 if (rc == -1) {
213 PLOG(FATAL) << "Failed to unlock " << oat_filename;
214 }
Elliott Hughes234da572011-11-03 22:13:06 -0700215 }
216 // Now a non-blocking attempt to take the lock has failed, we know the other guy has the
217 // lock, so block waiting to take it.
218 LOG(INFO) << "Another process is already working on " << oat_filename;
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700219 if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700220 PLOG(ERROR) << "Waiter unable to wait for creator to finish " << oat_filename;
221 return EXIT_FAILURE;
222 }
223 // We have the lock and the creator has finished.
224 // TODO: check the creator did a good job by checking the header.
225 LOG(INFO) << "Another process finished working on " << oat_filename;
226 // Job done.
227 file_janitor.KeepFile();
228 return EXIT_SUCCESS;
229 }
230
231 // If we get this far, we won the creation race and have locked the file.
232 UniquePtr<File> oat_file(OS::FileFromFd(oat_filename.c_str(), fd));
233
234 LOG(INFO) << "dex2oat: " << oat_file->name();
Ian Rogers5e863dd2011-11-02 20:00:10 -0700235
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700236 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700237 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700238 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700239 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700240 boot_class_path_string += "-Xbootclasspath:";
241 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
242 boot_class_path_string += dex_filenames[i];
243 boot_class_path_string += ":";
244 }
245 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
246 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700247 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700248 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
249 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700250 if (!host_prefix.empty()) {
251 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
252 }
jeffhao5d840402011-10-24 17:09:45 -0700253 for (size_t i = 0; i < runtime_args.size(); i++) {
254 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
255 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700256 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
257 if (runtime.get() == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700258 LOG(ERROR) << "Could not create runtime";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700259 return EXIT_FAILURE;
260 }
261 ClassLinker* class_linker = runtime->GetClassLinker();
262
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263 // If we have an existing boot image, position new space after its oat file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700264 if (Heap::GetSpaces().size() > 1) {
265 Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
266 CHECK(last_image_space != NULL);
267 CHECK(last_image_space->IsImageSpace());
268 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
269 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700271 }
272
273 // ClassLoader creation needs to come after Runtime::Create
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700274 SirtRef<ClassLoader> class_loader(NULL);
275 if (!boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700276 std::vector<const DexFile*> dex_files;
277 DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700278 for (size_t i = 0; i < dex_files.size(); i++) {
279 class_linker->RegisterDexFile(*dex_files[i]);
280 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700281 class_loader.reset(PathClassLoader::AllocCompileTime(dex_files));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700282 }
283
Brian Carlstrome24fa612011-09-29 00:53:55 -0700284 // if we loaded an existing image, we will reuse values from the image roots.
Ian Rogers169c9a72011-11-13 20:13:17 -0800285 if (!runtime->HasJniDlsymLookupStub()) {
286 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlysmLookupStub(kThumb2));
Brian Carlstrom16192862011-09-12 17:50:06 -0700287 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700288 if (!runtime->HasAbstractMethodErrorStubArray()) {
289 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2));
290 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700291 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700292 Runtime::TrampolineType type = Runtime::TrampolineType(i);
293 if (!runtime->HasResolutionStubArray(type)) {
294 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type);
295 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700296 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700297 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
298 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
299 if (!runtime->HasCalleeSaveMethod(type)) {
300 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2, type), type);
301 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700302 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700303 Compiler compiler(kThumb2, image_filename != NULL);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700304 if (method_names.empty()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700305 compiler.CompileAll(class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700306 } else {
307 for (size_t i = 0; i < method_names.size(); i++) {
308 // names are actually class_descriptor + name + signature.
309 // example: Ljava/lang/Object;<init>()V
310 StringPiece method_name = method_names[i];
311 size_t end_of_class_descriptor = method_name.find(';');
312 if (end_of_class_descriptor == method_name.npos) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700313 LOG(ERROR) << "Could not find class descriptor in method " << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700314 return EXIT_FAILURE;
315 }
316 end_of_class_descriptor++; // want to include ;
317 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
318 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
319 if (end_of_name == method_name.npos) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700320 LOG(ERROR) << "Could not find start of method signature in method '" << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700321 return EXIT_FAILURE;
322 }
323 std::string name = method_name.substr(end_of_class_descriptor,
324 end_of_name - end_of_class_descriptor).ToString();
325 std::string signature = method_name.substr(end_of_name).ToString();
326
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700327 Class* klass = class_linker->FindClass(class_descriptor, class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700328 if (klass == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700329 LOG(ERROR) << "Could not find class for descriptor '" << class_descriptor
330 << "' in method '" << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700331 return EXIT_FAILURE;
332 }
333 Method* method = klass->FindDirectMethod(name, signature);
334 if (method == NULL) {
335 method = klass->FindVirtualMethod(name, signature);
336 }
337 if (method == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700338 LOG(ERROR) << "Could not find method '" << method_name << "' with signature '"
339 << signature << "' in class '" << class_descriptor << "' for method argument '"
340 << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700341 return EXIT_FAILURE;
342 }
343 compiler.CompileOne(method);
344 }
345 }
346
Elliott Hughes234da572011-11-03 22:13:06 -0700347 if (!OatWriter::Create(oat_file.get(), class_loader.get(), compiler)) {
348 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700349 return EXIT_FAILURE;
350 }
351
Brian Carlstromaded5f72011-10-07 17:15:04 -0700352 if (image_filename == NULL) {
Ian Rogers97170512011-11-06 21:06:20 -0800353 file_janitor.KeepFile();
354 LOG(INFO) << "Oat file written successfully " << oat_file->name();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700355 return EXIT_SUCCESS;
356 }
357 CHECK(compiler.IsImage());
358
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 ImageWriter image_writer;
Elliott Hughes234da572011-11-03 22:13:06 -0700360 if (!image_writer.Write(image_filename, image_base, oat_file->name(), host_prefix)) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700361 LOG(ERROR) << "Failed to create image file " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700362 return EXIT_FAILURE;
363 }
364
Elliott Hughes234da572011-11-03 22:13:06 -0700365 // We wrote the file successfully, and want to keep it.
366 LOG(INFO) << "Image written successfully " << image_filename;
367 file_janitor.KeepFile();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700368 return EXIT_SUCCESS;
369}
370
371} // namespace art
372
373int main(int argc, char** argv) {
374 return art::dex2oat(argc, argv);
375}