blob: 78d36281d82a5ef79ae43e8ef3d6b199027fdd91 [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>
5
6#include <string>
7#include <vector>
8
9#include "class_linker.h"
10#include "class_loader.h"
11#include "compiler.h"
12#include "image_writer.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070013#include "oat_writer.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070014#include "runtime.h"
15#include "stringpiece.h"
16
17namespace art {
18
19static void usage() {
20 fprintf(stderr,
21 "Usage: dex2oat [options]...\n"
22 "\n");
23 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070024 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
25 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070026 " Example: --dex-file=/system/framework/core.jar\n"
27 "\n");
28 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070029 " --image=<file.art>: specifies the required output image filename.\n"
30 " Example: --image=/system/framework/boot.art\n"
31 "\n");
32 // TODO: remove this by inferring from --image
33 fprintf(stderr,
34 " --oat=<file.oat>: specifies the required oat filename.\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070035 " Example: --image=/system/framework/boot.oat\n"
36 "\n");
37 fprintf(stderr,
38 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
39 " Example: --base=0x50000000\n"
40 "\n");
41 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070042 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
43 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070044 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 // TODO: remove this by inferring from --boot-image
46 fprintf(stderr,
47 " --boot-oat=<file.oat>: provide the oat file for the boot class path.\n"
48 " Example: --boot-oat=/system/framework/boot.oat\n"
49 "\n");
50 // TODO: remove this by inderring from --boot-image or --boot-oat
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070051 fprintf(stderr,
52 " --boot-dex-file=<dex-file>: specifies a .dex file that is part of the boot\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070053 " image specified with --boot. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070054 " Example: --boot-dex-file=/system/framework/core.jar\n"
55 "\n");
56 fprintf(stderr,
57 " --method may be used to limit compilation to a subset of methods.\n"
58 " Example: --method=Ljava/lang/Object;<init>()V\n"
59 "\n");
Brian Carlstrom16192862011-09-12 17:50:06 -070060 fprintf(stderr,
61 " --strip-prefix may be used to strip a path prefix from dex file names in the\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070062 " the generated image to match the target file system layout.\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070063 " Example: --strip-prefix=out/target/product/crespo\n"
64 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070065 fprintf(stderr,
66 " -Xms<n> may be used to specify an initial heap size for the runtime used to\n"
67 " run dex2oat\n"
68 " Example: -Xms256m\n"
69 "\n");
70 fprintf(stderr,
71 " -Xmx<n> may be used to specify a maximum heap size for the runtime used to\n"
72 " run dex2oat\n"
73 " Example: -Xmx256m\n"
74 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070075 exit(EXIT_FAILURE);
76}
77
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070078int dex2oat(int argc, char** argv) {
79 // Skip over argv[0].
80 argv++;
81 argc--;
82
83 if (argc == 0) {
84 fprintf(stderr, "no arguments specified\n");
85 usage();
86 }
87
88 std::vector<const char*> dex_filenames;
89 std::vector<const char*> method_names;
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070091 const char* image_filename = NULL;
92 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -070093 std::string boot_oat_option;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070094 std::vector<const char*> boot_dex_filenames;
95 uintptr_t image_base = 0;
Brian Carlstrom16192862011-09-12 17:50:06 -070096 std::string strip_location_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070097 const char* Xms = NULL;
98 const char* Xmx = NULL;
Brian Carlstrom16192862011-09-12 17:50:06 -070099
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700100 for (int i = 0; i < argc; i++) {
101 const StringPiece option(argv[i]);
102 if (option.starts_with("--dex-file=")) {
103 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
104 } else if (option.starts_with("--method=")) {
105 method_names.push_back(option.substr(strlen("--method=")).data());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700106 } else if (option.starts_with("--oat=")) {
107 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700108 } else if (option.starts_with("--image=")) {
109 image_filename = option.substr(strlen("--image=")).data();
110 } else if (option.starts_with("--base=")) {
111 const char* image_base_str = option.substr(strlen("--base=")).data();
112 char* end;
113 image_base = strtoul(image_base_str, &end, 16);
114 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700115 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700116 usage();
117 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118 } else if (option.starts_with("--boot-image=")) {
119 const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700120 boot_image_option.clear();
121 boot_image_option += "-Xbootimage:";
122 boot_image_option += boot_image_filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123 } else if (option.starts_with("--boot-oat=")) {
124 const char* boot_oat_filename = option.substr(strlen("--boot-oat=")).data();
125 boot_oat_option.clear();
126 boot_oat_option += "-Xbootoat:";
127 boot_oat_option += boot_oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700128 } else if (option.starts_with("--boot-dex-file=")) {
129 boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data());
Brian Carlstrom16192862011-09-12 17:50:06 -0700130 } else if (option.starts_with("--strip-prefix=")) {
131 strip_location_prefix = option.substr(strlen("--strip-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700132 } else if (option.starts_with("-Xms")) {
133 Xms = option.data();
134 } else if (option.starts_with("-Xmx")) {
135 Xmx = option.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700136 } else {
137 fprintf(stderr, "unknown argument %s\n", option.data());
138 usage();
139 }
140 }
141
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 if (oat_filename == NULL) {
143 fprintf(stderr, "--oat file name not specified\n");
144 return EXIT_FAILURE;
145 }
146
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700147 if (image_filename == NULL) {
148 fprintf(stderr, "--image file name not specified\n");
149 return EXIT_FAILURE;
150 }
151
Brian Carlstrom78128a62011-09-15 17:21:19 -0700152 if (dex_filenames.empty()) {
153 fprintf(stderr, "no --dex-file values specified\n");
154 return EXIT_FAILURE;
155 }
156
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157 if (boot_image_option.empty() != boot_oat_option.empty()) {
158 fprintf(stderr, "--boot-image and --boat-oat must be specified together or not at all\n");
159 return EXIT_FAILURE;
160 }
161
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700162 if (boot_image_option.empty()) {
163 if (image_base == 0) {
164 fprintf(stderr, "non-zero --base not specified\n");
165 return EXIT_FAILURE;
166 }
167 } else {
168 if (boot_dex_filenames.empty()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 fprintf(stderr, "no --boot-dex-file values specified with --boot-image\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700170 return EXIT_FAILURE;
171 }
172 }
173
174 std::vector<const DexFile*> dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700175 DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700176
177 std::vector<const DexFile*> boot_dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700178 DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700179
180 Runtime::Options options;
181 if (boot_image_option.empty()) {
182 options.push_back(std::make_pair("bootclasspath", &dex_files));
183 } else {
184 options.push_back(std::make_pair("bootclasspath", &boot_dex_files));
185 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700186 options.push_back(std::make_pair(boot_oat_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700187 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700188 if (Xms != NULL) {
189 options.push_back(std::make_pair(Xms, reinterpret_cast<void*>(NULL)));
190 }
191 if (Xmx != NULL) {
192 options.push_back(std::make_pair(Xmx, reinterpret_cast<void*>(NULL)));
193 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700194 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
195 if (runtime.get() == NULL) {
196 fprintf(stderr, "could not create runtime\n");
197 return EXIT_FAILURE;
198 }
199 ClassLinker* class_linker = runtime->GetClassLinker();
200
Brian Carlstrome24fa612011-09-29 00:53:55 -0700201 // If we have an existing boot image, position new space after its oat file
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700202 if (!boot_image_option.empty()) {
203 Space* boot_space = Heap::GetBootSpace();
204 CHECK(boot_space != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700205 byte* oat_limit_addr = boot_space->GetImageHeader().GetOatLimitAddr();
206 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700207 }
208
209 // ClassLoader creation needs to come after Runtime::Create
210 const ClassLoader* class_loader;
211 if (boot_image_option.empty()) {
212 class_loader = NULL;
213 } else {
214 for (size_t i = 0; i < dex_files.size(); i++) {
215 class_linker->RegisterDexFile(*dex_files[i]);
216 }
217 class_loader = PathClassLoader::Alloc(dex_files);
218 }
219
Brian Carlstrome24fa612011-09-29 00:53:55 -0700220 // if we loaded an existing image, we will reuse values from the image roots.
Brian Carlstrom16192862011-09-12 17:50:06 -0700221 if (!runtime->HasJniStubArray()) {
222 runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
223 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224 if (!runtime->HasAbstractMethodErrorStubArray()) {
225 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2));
226 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700227 if (!runtime->HasCalleeSaveMethod()) {
228 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2));
229 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700230 Compiler compiler(kThumb2);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700231 if (method_names.empty()) {
232 compiler.CompileAll(class_loader);
233 } else {
234 for (size_t i = 0; i < method_names.size(); i++) {
235 // names are actually class_descriptor + name + signature.
236 // example: Ljava/lang/Object;<init>()V
237 StringPiece method_name = method_names[i];
238 size_t end_of_class_descriptor = method_name.find(';');
239 if (end_of_class_descriptor == method_name.npos) {
240 fprintf(stderr, "could not find class descriptor in method %s\n", method_name.data());
241 return EXIT_FAILURE;
242 }
243 end_of_class_descriptor++; // want to include ;
244 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
245 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
246 if (end_of_name == method_name.npos) {
247 fprintf(stderr, "could not find start of method signature in method %s\n", method_name.data());
248 return EXIT_FAILURE;
249 }
250 std::string name = method_name.substr(end_of_class_descriptor,
251 end_of_name - end_of_class_descriptor).ToString();
252 std::string signature = method_name.substr(end_of_name).ToString();
253
254 Class* klass = class_linker->FindClass(class_descriptor, class_loader);
255 if (klass == NULL) {
256 fprintf(stderr, "could not find class for descriptor %s in method %s\n",
257 class_descriptor.c_str(), method_name.data());
258 return EXIT_FAILURE;
259 }
260 Method* method = klass->FindDirectMethod(name, signature);
261 if (method == NULL) {
262 method = klass->FindVirtualMethod(name, signature);
263 }
264 if (method == NULL) {
265 fprintf(stderr, "could not find method %s with signature %s in class %s for method argument %s\n",
266 name.c_str(),
267 signature.c_str(),
268 class_descriptor.c_str(),
269 method_name.data());
270 return EXIT_FAILURE;
271 }
272 compiler.CompileOne(method);
273 }
274 }
275
Brian Carlstrome24fa612011-09-29 00:53:55 -0700276 if (!OatWriter::Create(oat_filename, class_loader)) {
277 fprintf(stderr, "Failed to create oat file %s\n", oat_filename.c_str());
278 return EXIT_FAILURE;
279 }
280
281 ImageWriter image_writer;
282 if (!image_writer.Write(image_filename, image_base, oat_filename, strip_location_prefix)) {
283 fprintf(stderr, "Failed to create image file %s\n", image_filename);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700284 return EXIT_FAILURE;
285 }
286
287 return EXIT_SUCCESS;
288}
289
290} // namespace art
291
292int main(int argc, char** argv) {
293 return art::dex2oat(argc, argv);
294}