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 | |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame^] | 97 | // Returns true if dex_files has a dex with the named location. |
| 98 | bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { |
| 99 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 100 | if (dex_files[i]->GetLocation() == location) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // Appends to dex_files any elements of class_path that it doesn't already |
| 108 | // contain. This will open those dex files as necessary. |
| 109 | void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { |
| 110 | std::vector<std::string> parsed; |
| 111 | Split(class_path, ':', parsed); |
| 112 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 113 | if (DexFilesContains(dex_files, parsed[i])) { |
| 114 | continue; |
| 115 | } |
| 116 | const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix()); |
| 117 | if (dex_file == NULL) { |
| 118 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 119 | } else { |
| 120 | dex_files.push_back(dex_file); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 125 | int dex2oat(int argc, char** argv) { |
| 126 | // Skip over argv[0]. |
| 127 | argv++; |
| 128 | argc--; |
| 129 | |
| 130 | if (argc == 0) { |
| 131 | fprintf(stderr, "no arguments specified\n"); |
| 132 | usage(); |
| 133 | } |
| 134 | |
| 135 | std::vector<const char*> dex_filenames; |
| 136 | std::vector<const char*> method_names; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 137 | std::string oat_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 138 | const char* image_filename = NULL; |
| 139 | std::string boot_image_option; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 140 | uintptr_t image_base = 0; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 141 | std::string host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 142 | std::vector<const char*> runtime_args; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 143 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 144 | for (int i = 0; i < argc; i++) { |
| 145 | const StringPiece option(argv[i]); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 146 | if (false) { |
| 147 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 148 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 149 | if (option.starts_with("--dex-file=")) { |
| 150 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
| 151 | } else if (option.starts_with("--method=")) { |
| 152 | method_names.push_back(option.substr(strlen("--method=")).data()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 153 | } else if (option.starts_with("--oat=")) { |
| 154 | oat_filename = option.substr(strlen("--oat=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 155 | } else if (option.starts_with("--image=")) { |
| 156 | image_filename = option.substr(strlen("--image=")).data(); |
| 157 | } else if (option.starts_with("--base=")) { |
| 158 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 159 | char* end; |
| 160 | image_base = strtoul(image_base_str, &end, 16); |
| 161 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 162 | 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] | 163 | usage(); |
| 164 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 165 | } else if (option.starts_with("--boot-image=")) { |
| 166 | const char* boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 167 | boot_image_option.clear(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 168 | boot_image_option += "-Ximage:"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 169 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 170 | } else if (option.starts_with("--host-prefix=")) { |
| 171 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 172 | } else if (option == "--runtime-arg") { |
| 173 | if (++i >= argc) { |
| 174 | fprintf(stderr, "Missing required argument for --runtime-arg\n"); |
| 175 | usage(); |
| 176 | } |
| 177 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 178 | } else { |
| 179 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 180 | usage(); |
| 181 | } |
| 182 | } |
| 183 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 184 | if (oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 185 | fprintf(stderr, "--oat file name not specified\n"); |
| 186 | return EXIT_FAILURE; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 189 | if (image_filename == NULL && boot_image_option.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 190 | fprintf(stderr, "Either --image or --boot-image must be specified\n"); |
| 191 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 194 | if (dex_filenames.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 195 | fprintf(stderr, "no --dex-file values specified\n"); |
| 196 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 199 | if (boot_image_option.empty()) { |
| 200 | if (image_base == 0) { |
| 201 | fprintf(stderr, "non-zero --base not specified\n"); |
| 202 | return EXIT_FAILURE; |
| 203 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 206 | // Create the output file if we can, or open it read-only if we weren't first. |
| 207 | bool did_create = true; |
| 208 | int fd = open(oat_filename.c_str(), O_EXCL | O_CREAT | O_TRUNC | O_RDWR, 0666); |
| 209 | if (fd == -1) { |
| 210 | if (errno != EEXIST) { |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 211 | PLOG(ERROR) << "Unable to create oat file " << oat_filename; |
| 212 | return EXIT_FAILURE; |
| 213 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 214 | did_create = false; |
| 215 | fd = open(oat_filename.c_str(), O_RDONLY); |
| 216 | if (fd == -1) { |
| 217 | PLOG(ERROR) << "Unable to open oat file for reading " << oat_filename; |
| 218 | return EXIT_FAILURE; |
| 219 | } |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 220 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 221 | |
| 222 | // Handles removing the file on failure and unlocking on both failure and success. |
| 223 | FileJanitor file_janitor(oat_filename, fd); |
| 224 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 225 | // If we won the creation race, block trying to take the lock (since we're going to be doing |
| 226 | // the work, we need the lock). If we lost the creation race, spin trying to take the lock |
| 227 | // non-blocking until we fail -- at which point we know the other guy has the lock -- and then |
| 228 | // block trying to take the now-taken lock. |
| 229 | if (did_create) { |
| 230 | LOG(INFO) << "This process created " << oat_filename; |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 231 | while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 232 | // Try again. |
| 233 | } |
| 234 | LOG(INFO) << "This process created and locked " << oat_filename; |
| 235 | } else { |
| 236 | LOG(INFO) << "Another process has already created " << oat_filename; |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 237 | while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) == 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 238 | // 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] | 239 | int rc = TEMP_FAILURE_RETRY(flock(fd, LOCK_UN)); |
| 240 | if (rc == -1) { |
| 241 | PLOG(FATAL) << "Failed to unlock " << oat_filename; |
| 242 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 243 | } |
| 244 | // Now a non-blocking attempt to take the lock has failed, we know the other guy has the |
| 245 | // lock, so block waiting to take it. |
| 246 | LOG(INFO) << "Another process is already working on " << oat_filename; |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 247 | if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 248 | PLOG(ERROR) << "Waiter unable to wait for creator to finish " << oat_filename; |
| 249 | return EXIT_FAILURE; |
| 250 | } |
| 251 | // We have the lock and the creator has finished. |
| 252 | // TODO: check the creator did a good job by checking the header. |
| 253 | LOG(INFO) << "Another process finished working on " << oat_filename; |
| 254 | // Job done. |
| 255 | file_janitor.KeepFile(); |
| 256 | return EXIT_SUCCESS; |
| 257 | } |
| 258 | |
| 259 | // If we get this far, we won the creation race and have locked the file. |
| 260 | UniquePtr<File> oat_file(OS::FileFromFd(oat_filename.c_str(), fd)); |
| 261 | |
| 262 | LOG(INFO) << "dex2oat: " << oat_file->name(); |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 263 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 264 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 265 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 266 | std::string boot_class_path_string; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 267 | if (boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 268 | boot_class_path_string += "-Xbootclasspath:"; |
| 269 | for (size_t i = 0; i < dex_filenames.size()-1; i++) { |
| 270 | boot_class_path_string += dex_filenames[i]; |
| 271 | boot_class_path_string += ":"; |
| 272 | } |
| 273 | boot_class_path_string += dex_filenames[dex_filenames.size()-1]; |
| 274 | 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] | 275 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 276 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 277 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 278 | if (!host_prefix.empty()) { |
| 279 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 280 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 281 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 282 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 283 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 284 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 285 | if (runtime.get() == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 286 | LOG(ERROR) << "Could not create runtime"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 287 | return EXIT_FAILURE; |
| 288 | } |
| 289 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 290 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 291 | // 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] | 292 | if (Heap::GetSpaces().size() > 1) { |
| 293 | Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]; |
| 294 | CHECK(last_image_space != NULL); |
| 295 | CHECK(last_image_space->IsImageSpace()); |
| 296 | CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace()); |
| 297 | byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 298 | image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // ClassLoader creation needs to come after Runtime::Create |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 302 | SirtRef<ClassLoader> class_loader(NULL); |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame^] | 303 | std::vector<const DexFile*> dex_files; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 304 | if (!boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 305 | DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix); |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame^] | 306 | std::vector<const DexFile*> class_path_files(dex_files); |
| 307 | OpenClassPathFiles(runtime->GetClassPath(), class_path_files); |
| 308 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 309 | class_linker->RegisterDexFile(*class_path_files[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 310 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame^] | 311 | class_loader.reset(PathClassLoader::AllocCompileTime(class_path_files)); |
| 312 | } else { |
| 313 | dex_files = runtime->GetClassLinker()->GetBootClassPath(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 316 | // 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] | 317 | if (!runtime->HasJniDlsymLookupStub()) { |
| 318 | runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlysmLookupStub(kThumb2)); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 319 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 320 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
| 321 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2)); |
| 322 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 323 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 324 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 325 | if (!runtime->HasResolutionStubArray(type)) { |
| 326 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type); |
| 327 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 328 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 329 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 330 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 331 | if (!runtime->HasCalleeSaveMethod(type)) { |
| 332 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2, type), type); |
| 333 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 334 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 335 | Compiler compiler(kThumb2, image_filename != NULL); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 336 | if (method_names.empty()) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame^] | 337 | compiler.CompileAll(class_loader.get(), dex_files); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 338 | } else { |
| 339 | for (size_t i = 0; i < method_names.size(); i++) { |
| 340 | // names are actually class_descriptor + name + signature. |
| 341 | // example: Ljava/lang/Object;<init>()V |
| 342 | StringPiece method_name = method_names[i]; |
| 343 | size_t end_of_class_descriptor = method_name.find(';'); |
| 344 | if (end_of_class_descriptor == method_name.npos) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 345 | LOG(ERROR) << "Could not find class descriptor in method " << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 346 | return EXIT_FAILURE; |
| 347 | } |
| 348 | end_of_class_descriptor++; // want to include ; |
| 349 | std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString(); |
| 350 | size_t end_of_name = method_name.find('(', end_of_class_descriptor); |
| 351 | if (end_of_name == method_name.npos) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 352 | 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] | 353 | return EXIT_FAILURE; |
| 354 | } |
| 355 | std::string name = method_name.substr(end_of_class_descriptor, |
| 356 | end_of_name - end_of_class_descriptor).ToString(); |
| 357 | std::string signature = method_name.substr(end_of_name).ToString(); |
| 358 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 359 | Class* klass = class_linker->FindClass(class_descriptor, class_loader.get()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 360 | if (klass == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 361 | LOG(ERROR) << "Could not find class for descriptor '" << class_descriptor |
| 362 | << "' in method '" << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 363 | return EXIT_FAILURE; |
| 364 | } |
| 365 | Method* method = klass->FindDirectMethod(name, signature); |
| 366 | if (method == NULL) { |
| 367 | method = klass->FindVirtualMethod(name, signature); |
| 368 | } |
| 369 | if (method == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 370 | LOG(ERROR) << "Could not find method '" << method_name << "' with signature '" |
| 371 | << signature << "' in class '" << class_descriptor << "' for method argument '" |
| 372 | << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 373 | return EXIT_FAILURE; |
| 374 | } |
| 375 | compiler.CompileOne(method); |
| 376 | } |
| 377 | } |
| 378 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 379 | if (!OatWriter::Create(oat_file.get(), class_loader.get(), compiler)) { |
| 380 | LOG(ERROR) << "Failed to create oat file " << oat_file->name(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 381 | return EXIT_FAILURE; |
| 382 | } |
| 383 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 384 | if (image_filename == NULL) { |
Ian Rogers | 9717051 | 2011-11-06 21:06:20 -0800 | [diff] [blame] | 385 | file_janitor.KeepFile(); |
| 386 | LOG(INFO) << "Oat file written successfully " << oat_file->name(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 387 | return EXIT_SUCCESS; |
| 388 | } |
| 389 | CHECK(compiler.IsImage()); |
| 390 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 391 | ImageWriter image_writer; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 392 | 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] | 393 | LOG(ERROR) << "Failed to create image file " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 394 | return EXIT_FAILURE; |
| 395 | } |
| 396 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 397 | // We wrote the file successfully, and want to keep it. |
| 398 | LOG(INFO) << "Image written successfully " << image_filename; |
| 399 | file_janitor.KeepFile(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 400 | return EXIT_SUCCESS; |
| 401 | } |
| 402 | |
| 403 | } // namespace art |
| 404 | |
| 405 | int main(int argc, char** argv) { |
| 406 | return art::dex2oat(argc, argv); |
| 407 | } |