Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 5 | #include <sys/file.h> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "class_linker.h" |
| 11 | #include "class_loader.h" |
| 12 | #include "compiler.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 13 | #include "file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 14 | #include "image_writer.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 15 | #include "oat_writer.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 16 | #include "os.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 17 | #include "runtime.h" |
| 18 | #include "stringpiece.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | static void usage() { |
| 23 | fprintf(stderr, |
| 24 | "Usage: dex2oat [options]...\n" |
| 25 | "\n"); |
| 26 | fprintf(stderr, |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 27 | " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n" |
| 28 | " file must be specified. \n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 29 | " Example: --dex-file=/system/framework/core.jar\n" |
| 30 | "\n"); |
| 31 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | " --image=<file.art>: specifies the required output image filename.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 33 | " Example: --image=/data/art-cache/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 34 | "\n"); |
| 35 | // TODO: remove this by inferring from --image |
| 36 | fprintf(stderr, |
| 37 | " --oat=<file.oat>: specifies the required oat filename.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 38 | " Example: --image=/data/art-cache/boot.oat\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 39 | "\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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 45 | " --boot-image=<file.art>: provide the image file for the boot class path.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 46 | " Example: --boot-image=/data/art-cache/boot.art\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 47 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 48 | 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 Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 52 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 53 | " --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 Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 56 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 57 | fprintf(stderr, |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 58 | " --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 Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 62 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 63 | exit(EXIT_FAILURE); |
| 64 | } |
| 65 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 66 | class FileJanitor { |
| 67 | public: |
| 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 Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 78 | int rc = TEMP_FAILURE_RETRY(flock(fd_, LOCK_UN)); |
| 79 | if (rc == -1) { |
| 80 | PLOG(ERROR) << "Failed to unlock " << filename_; |
| 81 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 82 | } |
| 83 | if (do_unlink_) { |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 84 | int rc = TEMP_FAILURE_RETRY(unlink(filename_.c_str())); |
| 85 | if (rc == -1) { |
| 86 | PLOG(ERROR) << "Failed to unlink " << filename_; |
| 87 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | std::string filename_; |
| 93 | int fd_; |
| 94 | bool do_unlink_; |
| 95 | }; |
| 96 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 97 | int 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 109 | std::string oat_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 110 | const char* image_filename = NULL; |
| 111 | std::string boot_image_option; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 112 | uintptr_t image_base = 0; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 113 | std::string host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 114 | std::vector<const char*> runtime_args; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 115 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 116 | for (int i = 0; i < argc; i++) { |
| 117 | const StringPiece option(argv[i]); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 118 | if (false) { |
| 119 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 120 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 121 | 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 125 | } else if (option.starts_with("--oat=")) { |
| 126 | oat_filename = option.substr(strlen("--oat=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 127 | } 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 Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 134 | fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 135 | usage(); |
| 136 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 137 | } else if (option.starts_with("--boot-image=")) { |
| 138 | const char* boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 139 | boot_image_option.clear(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 140 | boot_image_option += "-Ximage:"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 141 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 142 | } else if (option.starts_with("--host-prefix=")) { |
| 143 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 144 | } 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 Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 150 | } else { |
| 151 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 152 | usage(); |
| 153 | } |
| 154 | } |
| 155 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 156 | if (oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 157 | fprintf(stderr, "--oat file name not specified\n"); |
| 158 | return EXIT_FAILURE; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 161 | if (image_filename == NULL && boot_image_option.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 162 | fprintf(stderr, "Either --image or --boot-image must be specified\n"); |
| 163 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 166 | if (dex_filenames.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 167 | fprintf(stderr, "no --dex-file values specified\n"); |
| 168 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 171 | 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 Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 178 | // 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 Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 183 | PLOG(ERROR) << "Unable to create oat file " << oat_filename; |
| 184 | return EXIT_FAILURE; |
| 185 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 186 | 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 Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 192 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 193 | |
| 194 | // Handles removing the file on failure and unlocking on both failure and success. |
| 195 | FileJanitor file_janitor(oat_filename, fd); |
| 196 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 197 | // 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 Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 203 | while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 204 | // 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 Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 209 | while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) == 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 210 | // Give up the lock and hope the creator has taken the lock next time round. |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 211 | int rc = TEMP_FAILURE_RETRY(flock(fd, LOCK_UN)); |
| 212 | if (rc == -1) { |
| 213 | PLOG(FATAL) << "Failed to unlock " << oat_filename; |
| 214 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 215 | } |
| 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 Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 219 | if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 220 | 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 Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 235 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 236 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 237 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 238 | std::string boot_class_path_string; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 239 | if (boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 240 | 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 Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 247 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 248 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 249 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 250 | if (!host_prefix.empty()) { |
| 251 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 252 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 253 | 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 Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 256 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 257 | if (runtime.get() == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 258 | LOG(ERROR) << "Could not create runtime"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 259 | return EXIT_FAILURE; |
| 260 | } |
| 261 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 262 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 263 | // If we have an existing boot image, position new space after its oat file |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 264 | 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 270 | image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // ClassLoader creation needs to come after Runtime::Create |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 274 | SirtRef<ClassLoader> class_loader(NULL); |
| 275 | if (!boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 276 | std::vector<const DexFile*> dex_files; |
| 277 | DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 278 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 279 | class_linker->RegisterDexFile(*dex_files[i]); |
| 280 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 281 | class_loader.reset(PathClassLoader::AllocCompileTime(dex_files)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 284 | // if we loaded an existing image, we will reuse values from the image roots. |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 285 | if (!runtime->HasJniDlsymLookupStub()) { |
| 286 | runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlysmLookupStub(kThumb2)); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 287 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 288 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
| 289 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2)); |
| 290 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 291 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 292 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 293 | if (!runtime->HasResolutionStubArray(type)) { |
| 294 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type); |
| 295 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 296 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 297 | 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 Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 302 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 303 | Compiler compiler(kThumb2, image_filename != NULL); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 304 | if (method_names.empty()) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 305 | compiler.CompileAll(class_loader.get()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 306 | } 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 Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 313 | LOG(ERROR) << "Could not find class descriptor in method " << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 314 | 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 Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 320 | LOG(ERROR) << "Could not find start of method signature in method '" << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 321 | 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 Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 327 | Class* klass = class_linker->FindClass(class_descriptor, class_loader.get()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 328 | if (klass == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 329 | LOG(ERROR) << "Could not find class for descriptor '" << class_descriptor |
| 330 | << "' in method '" << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 331 | 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 Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 338 | LOG(ERROR) << "Could not find method '" << method_name << "' with signature '" |
| 339 | << signature << "' in class '" << class_descriptor << "' for method argument '" |
| 340 | << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 341 | return EXIT_FAILURE; |
| 342 | } |
| 343 | compiler.CompileOne(method); |
| 344 | } |
| 345 | } |
| 346 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 347 | if (!OatWriter::Create(oat_file.get(), class_loader.get(), compiler)) { |
| 348 | LOG(ERROR) << "Failed to create oat file " << oat_file->name(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 349 | return EXIT_FAILURE; |
| 350 | } |
| 351 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 352 | if (image_filename == NULL) { |
Ian Rogers | 9717051 | 2011-11-06 21:06:20 -0800 | [diff] [blame] | 353 | file_janitor.KeepFile(); |
| 354 | LOG(INFO) << "Oat file written successfully " << oat_file->name(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 355 | return EXIT_SUCCESS; |
| 356 | } |
| 357 | CHECK(compiler.IsImage()); |
| 358 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 359 | ImageWriter image_writer; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 360 | if (!image_writer.Write(image_filename, image_base, oat_file->name(), host_prefix)) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 361 | LOG(ERROR) << "Failed to create image file " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 362 | return EXIT_FAILURE; |
| 363 | } |
| 364 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 365 | // We wrote the file successfully, and want to keep it. |
| 366 | LOG(INFO) << "Image written successfully " << image_filename; |
| 367 | file_janitor.KeepFile(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 368 | return EXIT_SUCCESS; |
| 369 | } |
| 370 | |
| 371 | } // namespace art |
| 372 | |
| 373 | int main(int argc, char** argv) { |
| 374 | return art::dex2oat(argc, argv); |
| 375 | } |