Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/stat.h> |
Ian Rogers | 2672a9f | 2013-09-05 17:24:22 -0700 | [diff] [blame] | 20 | #include <valgrind.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | |
| 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "base/stl_util.h" |
| 29 | #include "base/stringpiece.h" |
| 30 | #include "base/timing_logger.h" |
| 31 | #include "base/unix_file/fd_file.h" |
| 32 | #include "class_linker.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 33 | #include "compiler_callbacks.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | #include "dex_file-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 35 | #include "dex/verified_methods_data.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | #include "driver/compiler_driver.h" |
| 37 | #include "elf_fixup.h" |
| 38 | #include "elf_stripper.h" |
| 39 | #include "gc/space/image_space.h" |
| 40 | #include "gc/space/space-inl.h" |
| 41 | #include "image_writer.h" |
| 42 | #include "leb128.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 43 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 44 | #include "mirror/class-inl.h" |
| 45 | #include "mirror/class_loader.h" |
| 46 | #include "mirror/object-inl.h" |
| 47 | #include "mirror/object_array-inl.h" |
| 48 | #include "oat_writer.h" |
| 49 | #include "object_utils.h" |
| 50 | #include "os.h" |
| 51 | #include "runtime.h" |
| 52 | #include "ScopedLocalRef.h" |
| 53 | #include "scoped_thread_state_change.h" |
| 54 | #include "sirt_ref.h" |
| 55 | #include "vector_output_stream.h" |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 56 | #include "verifier/method_verifier.h" |
| 57 | #include "verifier/method_verifier-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 58 | #include "well_known_classes.h" |
| 59 | #include "zip_archive.h" |
| 60 | |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 61 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
| 62 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 63 | namespace art { |
| 64 | |
| 65 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 66 | std::string error; |
| 67 | StringAppendV(&error, fmt, ap); |
| 68 | LOG(ERROR) << error; |
| 69 | } |
| 70 | |
| 71 | static void UsageError(const char* fmt, ...) { |
| 72 | va_list ap; |
| 73 | va_start(ap, fmt); |
| 74 | UsageErrorV(fmt, ap); |
| 75 | va_end(ap); |
| 76 | } |
| 77 | |
| 78 | static void Usage(const char* fmt, ...) { |
| 79 | va_list ap; |
| 80 | va_start(ap, fmt); |
| 81 | UsageErrorV(fmt, ap); |
| 82 | va_end(ap); |
| 83 | |
| 84 | UsageError("Usage: dex2oat [options]..."); |
| 85 | UsageError(""); |
| 86 | UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); |
| 87 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 88 | UsageError(""); |
| 89 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 90 | UsageError(" containing a classes.dex file to compile."); |
| 91 | UsageError(" Example: --zip-fd=5"); |
| 92 | UsageError(""); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 93 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file"); |
| 94 | UsageError(" corresponding to the file descriptor specified by --zip-fd."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 95 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 96 | UsageError(""); |
| 97 | UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); |
| 98 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 99 | UsageError(""); |
| 100 | UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); |
| 101 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 102 | UsageError(""); |
| 103 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 104 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 105 | UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat"); |
| 106 | UsageError(""); |
| 107 | UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); |
| 108 | UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); |
| 109 | UsageError(""); |
| 110 | UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); |
| 111 | UsageError(" Example: --bitcode=/system/framework/boot.bc"); |
| 112 | UsageError(""); |
| 113 | UsageError(" --image=<file.art>: specifies the output image filename."); |
| 114 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 115 | UsageError(""); |
| 116 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 117 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 118 | UsageError(""); |
| 119 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 120 | UsageError(" Example: --base=0x50000000"); |
| 121 | UsageError(""); |
| 122 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
| 123 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
| 124 | UsageError(" Default: <host-prefix>/system/framework/boot.art"); |
| 125 | UsageError(""); |
| 126 | UsageError(" --host-prefix=<path>: used to translate host paths to target paths during"); |
| 127 | UsageError(" cross compilation."); |
| 128 | UsageError(" Example: --host-prefix=out/target/product/crespo"); |
| 129 | UsageError(" Default: $ANDROID_PRODUCT_OUT"); |
| 130 | UsageError(""); |
| 131 | UsageError(" --android-root=<path>: used to locate libraries for portable linking."); |
| 132 | UsageError(" Example: --android-root=out/host/linux-x86"); |
| 133 | UsageError(" Default: $ANDROID_ROOT"); |
| 134 | UsageError(""); |
| 135 | UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction"); |
| 136 | UsageError(" set."); |
| 137 | UsageError(" Example: --instruction-set=x86"); |
| 138 | UsageError(" Default: arm"); |
| 139 | UsageError(""); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 140 | UsageError(" --instruction-set-features=...,: Specify instruction set features"); |
| 141 | UsageError(" Example: --instruction-set-features=div"); |
| 142 | UsageError(" Default: default"); |
| 143 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 144 | UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend"); |
| 145 | UsageError(" set."); |
Brian Carlstrom | 635733d | 2013-10-30 23:19:31 -0700 | [diff] [blame] | 146 | UsageError(" Example: --compiler-backend=Portable"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 147 | UsageError(" Default: Quick"); |
| 148 | UsageError(""); |
| 149 | UsageError(" --host: used with Portable backend to link against host runtime libraries"); |
| 150 | UsageError(""); |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 151 | UsageError(" --dump-timing: display a breakdown of where time was spent"); |
| 152 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 153 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 154 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 155 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 156 | UsageError(" Example: --runtime-arg -Xms256m"); |
| 157 | UsageError(""); |
| 158 | std::cerr << "See log for usage error information\n"; |
| 159 | exit(EXIT_FAILURE); |
| 160 | } |
| 161 | |
| 162 | class Dex2Oat { |
| 163 | public: |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 164 | static bool Create(Dex2Oat** p_dex2oat, |
| 165 | Runtime::Options& options, |
| 166 | CompilerBackend compiler_backend, |
| 167 | InstructionSet instruction_set, |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 168 | InstructionSetFeatures instruction_set_features, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 169 | size_t thread_count) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 170 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 171 | UniquePtr<Dex2Oat> dex2oat(new Dex2Oat(compiler_backend, instruction_set, |
| 172 | instruction_set_features, thread_count)); |
| 173 | if (!dex2oat->CreateRuntime(options, instruction_set)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 174 | *p_dex2oat = NULL; |
| 175 | return false; |
| 176 | } |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 177 | *p_dex2oat = dex2oat.release(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | ~Dex2Oat() { |
| 182 | delete runtime_; |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 183 | VLOG(compiler) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 184 | << " (threads: " << thread_count_ << ")"; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 188 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 189 | CompilerDriver::DescriptorSet* ReadImageClassesFromFile(const char* image_classes_filename) { |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 190 | UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, |
| 191 | std::ifstream::in)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 192 | if (image_classes_file.get() == NULL) { |
| 193 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 194 | return NULL; |
| 195 | } |
| 196 | UniquePtr<CompilerDriver::DescriptorSet> result(ReadImageClasses(*image_classes_file.get())); |
| 197 | image_classes_file->close(); |
| 198 | return result.release(); |
| 199 | } |
| 200 | |
| 201 | CompilerDriver::DescriptorSet* ReadImageClasses(std::istream& image_classes_stream) { |
| 202 | UniquePtr<CompilerDriver::DescriptorSet> image_classes(new CompilerDriver::DescriptorSet); |
| 203 | while (image_classes_stream.good()) { |
| 204 | std::string dot; |
| 205 | std::getline(image_classes_stream, dot); |
| 206 | if (StartsWith(dot, "#") || dot.empty()) { |
| 207 | continue; |
| 208 | } |
| 209 | std::string descriptor(DotToDescriptor(dot.c_str())); |
| 210 | image_classes->insert(descriptor); |
| 211 | } |
| 212 | return image_classes.release(); |
| 213 | } |
| 214 | |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 215 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 216 | CompilerDriver::DescriptorSet* ReadImageClassesFromZip(const char* zip_filename, |
| 217 | const char* image_classes_filename, |
| 218 | std::string* error_msg) { |
| 219 | UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename, error_msg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 220 | if (zip_archive.get() == NULL) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 223 | UniquePtr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename, error_msg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 224 | if (zip_entry.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 225 | *error_msg = StringPrintf("Failed to find '%s' within '%s': %s", image_classes_filename, |
| 226 | zip_filename, error_msg->c_str()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 227 | return NULL; |
| 228 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 229 | UniquePtr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(image_classes_filename, |
| 230 | error_msg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 231 | if (image_classes_file.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 232 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", image_classes_filename, |
| 233 | zip_filename, error_msg->c_str()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | return NULL; |
| 235 | } |
| 236 | const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()), |
| 237 | image_classes_file->Size()); |
| 238 | std::istringstream image_classes_stream(image_classes_string); |
| 239 | return ReadImageClasses(image_classes_stream); |
| 240 | } |
| 241 | |
| 242 | const CompilerDriver* CreateOatFile(const std::string& boot_image_option, |
| 243 | const std::string* host_prefix, |
| 244 | const std::string& android_root, |
| 245 | bool is_host, |
| 246 | const std::vector<const DexFile*>& dex_files, |
| 247 | File* oat_file, |
| 248 | const std::string& bitcode_filename, |
| 249 | bool image, |
| 250 | UniquePtr<CompilerDriver::DescriptorSet>& image_classes, |
| 251 | bool dump_stats, |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 252 | TimingLogger& timings) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 253 | // SirtRef and ClassLoader creation needs to come after Runtime::Create |
| 254 | jobject class_loader = NULL; |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 255 | Thread* self = Thread::Current(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 256 | if (!boot_image_option.empty()) { |
| 257 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 258 | std::vector<const DexFile*> class_path_files(dex_files); |
| 259 | OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 260 | ScopedObjectAccess soa(self); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 261 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 262 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 263 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 264 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); |
| 265 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 266 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 267 | class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 268 | Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); |
| 269 | } |
| 270 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 271 | UniquePtr<CompilerDriver> driver(new CompilerDriver(verified_methods_data_.get(), |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 272 | method_inliner_map_.get(), |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 273 | compiler_backend_, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 274 | instruction_set_, |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 275 | instruction_set_features_, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 276 | image, |
| 277 | image_classes.release(), |
| 278 | thread_count_, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 279 | dump_stats)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 280 | |
| 281 | if (compiler_backend_ == kPortable) { |
| 282 | driver->SetBitcodeFileName(bitcode_filename); |
| 283 | } |
| 284 | |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 285 | driver->CompileAll(class_loader, dex_files, timings); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 286 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 287 | timings.NewSplit("dex2oat OatWriter"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 288 | std::string image_file_location; |
| 289 | uint32_t image_file_location_oat_checksum = 0; |
| 290 | uint32_t image_file_location_oat_data_begin = 0; |
| 291 | if (!driver->IsImage()) { |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 292 | TimingLogger::ScopedSplit split("Loading image checksum", &timings); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 293 | gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 294 | image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); |
| 295 | image_file_location_oat_data_begin = |
| 296 | reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatDataBegin()); |
| 297 | image_file_location = image_space->GetImageFilename(); |
| 298 | if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { |
| 299 | image_file_location = image_file_location.substr(host_prefix->size()); |
| 300 | } |
| 301 | } |
| 302 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 303 | OatWriter oat_writer(dex_files, |
| 304 | image_file_location_oat_checksum, |
| 305 | image_file_location_oat_data_begin, |
| 306 | image_file_location, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 307 | driver.get(), |
| 308 | &timings); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 309 | |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 310 | TimingLogger::ScopedSplit split("Writing ELF", &timings); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 311 | if (!driver->WriteElf(android_root, is_host, dex_files, oat_writer, oat_file)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 312 | LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | return driver.release(); |
| 317 | } |
| 318 | |
| 319 | bool CreateImageFile(const std::string& image_filename, |
| 320 | uintptr_t image_base, |
| 321 | const std::string& oat_filename, |
| 322 | const std::string& oat_location, |
| 323 | const CompilerDriver& compiler) |
| 324 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
| 325 | uintptr_t oat_data_begin; |
| 326 | { |
| 327 | // ImageWriter is scoped so it can free memory before doing FixupElf |
| 328 | ImageWriter image_writer(compiler); |
| 329 | if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location)) { |
| 330 | LOG(ERROR) << "Failed to create image file " << image_filename; |
| 331 | return false; |
| 332 | } |
| 333 | oat_data_begin = image_writer.GetOatDataBegin(); |
| 334 | } |
| 335 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 336 | UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 337 | if (oat_file.get() == NULL) { |
| 338 | PLOG(ERROR) << "Failed to open ELF file: " << oat_filename; |
| 339 | return false; |
| 340 | } |
| 341 | if (!ElfFixup::Fixup(oat_file.get(), oat_data_begin)) { |
| 342 | LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); |
| 343 | return false; |
| 344 | } |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | private: |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 349 | class Dex2OatCompilerCallbacks : public CompilerCallbacks { |
| 350 | public: |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 351 | Dex2OatCompilerCallbacks(VerifiedMethodsData* verified_methods_data, |
| 352 | DexFileToMethodInlinerMap* method_inliner_map) |
| 353 | : verified_methods_data_(verified_methods_data), |
| 354 | method_inliner_map_(method_inliner_map) { } |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 355 | virtual ~Dex2OatCompilerCallbacks() { } |
| 356 | |
| 357 | virtual bool MethodVerified(verifier::MethodVerifier* verifier) |
| 358 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 359 | bool result = verified_methods_data_->ProcessVerifiedMethod(verifier); |
| 360 | if (result && method_inliner_map_ != nullptr) { |
| 361 | MethodReference ref = verifier->GetMethodReference(); |
| 362 | method_inliner_map_->GetMethodInliner(ref.dex_file) |
| 363 | ->AnalyseMethodCode(ref.dex_method_index, verifier->CodeItem()); |
| 364 | } |
| 365 | return result; |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 366 | } |
| 367 | virtual void ClassRejected(ClassReference ref) { |
| 368 | verified_methods_data_->AddRejectedClass(ref); |
| 369 | } |
| 370 | |
| 371 | private: |
| 372 | VerifiedMethodsData* verified_methods_data_; |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 373 | DexFileToMethodInlinerMap* method_inliner_map_; |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | explicit Dex2Oat(CompilerBackend compiler_backend, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 377 | InstructionSet instruction_set, |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 378 | InstructionSetFeatures instruction_set_features, |
Brian Carlstrom | 0177fe2 | 2013-07-21 12:21:36 -0700 | [diff] [blame] | 379 | size_t thread_count) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 380 | : compiler_backend_(compiler_backend), |
| 381 | instruction_set_(instruction_set), |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 382 | instruction_set_features_(instruction_set_features), |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 383 | verified_methods_data_(new VerifiedMethodsData), |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 384 | method_inliner_map_(compiler_backend == kQuick ? new DexFileToMethodInlinerMap : nullptr), |
| 385 | callbacks_(verified_methods_data_.get(), method_inliner_map_.get()), |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 386 | runtime_(nullptr), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 387 | thread_count_(thread_count), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 388 | start_ns_(NanoTime()) { |
| 389 | } |
| 390 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 391 | bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 392 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 393 | options.push_back( |
| 394 | std::make_pair("compilercallbacks", static_cast<CompilerCallbacks*>(&callbacks_))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 395 | if (!Runtime::Create(options, false)) { |
| 396 | LOG(ERROR) << "Failed to create runtime"; |
| 397 | return false; |
| 398 | } |
| 399 | Runtime* runtime = Runtime::Current(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 400 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 401 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 402 | if (!runtime->HasCalleeSaveMethod(type)) { |
| 403 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); |
| 404 | } |
| 405 | } |
| 406 | runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 407 | runtime_ = runtime; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 408 | return true; |
| 409 | } |
| 410 | |
| 411 | // Appends to dex_files any elements of class_path that it doesn't already |
| 412 | // contain. This will open those dex files as necessary. |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 413 | static void OpenClassPathFiles(const std::string& class_path, |
| 414 | std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 415 | std::vector<std::string> parsed; |
| 416 | Split(class_path, ':', parsed); |
| 417 | // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained. |
| 418 | ScopedObjectAccess soa(Thread::Current()); |
| 419 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 420 | if (DexFilesContains(dex_files, parsed[i])) { |
| 421 | continue; |
| 422 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 423 | std::string error_msg; |
| 424 | const DexFile* dex_file = DexFile::Open(parsed[i].c_str(), parsed[i].c_str(), &error_msg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 425 | if (dex_file == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 426 | LOG(WARNING) << "Failed to open dex file '" << parsed[i] << "': " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 427 | } else { |
| 428 | dex_files.push_back(dex_file); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // Returns true if dex_files has a dex with the named location. |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 434 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, |
| 435 | const std::string& location) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 436 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 437 | if (dex_files[i]->GetLocation() == location) { |
| 438 | return true; |
| 439 | } |
| 440 | } |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | const CompilerBackend compiler_backend_; |
| 445 | |
| 446 | const InstructionSet instruction_set_; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 447 | const InstructionSetFeatures instruction_set_features_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 448 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 449 | UniquePtr<VerifiedMethodsData> verified_methods_data_; |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 450 | UniquePtr<DexFileToMethodInlinerMap> method_inliner_map_; |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 451 | Dex2OatCompilerCallbacks callbacks_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 452 | Runtime* runtime_; |
| 453 | size_t thread_count_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 454 | uint64_t start_ns_; |
| 455 | |
| 456 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 457 | }; |
| 458 | |
| 459 | static bool ParseInt(const char* in, int* out) { |
| 460 | char* end; |
| 461 | int result = strtol(in, &end, 10); |
| 462 | if (in == end || *end != '\0') { |
| 463 | return false; |
| 464 | } |
| 465 | *out = result; |
| 466 | return true; |
| 467 | } |
| 468 | |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 469 | static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, |
| 470 | const std::vector<const char*>& dex_locations, |
| 471 | std::vector<const DexFile*>& dex_files) { |
| 472 | size_t failure_count = 0; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 473 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 474 | const char* dex_filename = dex_filenames[i]; |
| 475 | const char* dex_location = dex_locations[i]; |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame^] | 476 | ATRACE_BEGIN(StringPrintf("Opening dex file '%s'", dex_filenames[i]).c_str()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 477 | std::string error_msg; |
Brian Carlstrom | d5aba59 | 2013-11-12 01:52:44 -0800 | [diff] [blame] | 478 | if (!OS::FileExists(dex_filename)) { |
| 479 | LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'"; |
| 480 | continue; |
| 481 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 482 | const DexFile* dex_file = DexFile::Open(dex_filename, dex_location, &error_msg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 483 | if (dex_file == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 484 | LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg; |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 485 | ++failure_count; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 486 | } else { |
| 487 | dex_files.push_back(dex_file); |
| 488 | } |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame^] | 489 | ATRACE_END(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 490 | } |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 491 | return failure_count; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // The primary goal of the watchdog is to prevent stuck build servers |
| 495 | // during development when fatal aborts lead to a cascade of failures |
| 496 | // that result in a deadlock. |
| 497 | class WatchDog { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 498 | // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks |
| 499 | #undef CHECK_PTHREAD_CALL |
| 500 | #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ |
| 501 | do { \ |
| 502 | int rc = call args; \ |
| 503 | if (rc != 0) { \ |
| 504 | errno = rc; \ |
| 505 | std::string message(# call); \ |
| 506 | message += " failed for "; \ |
| 507 | message += reason; \ |
| 508 | Fatal(message); \ |
| 509 | } \ |
| 510 | } while (false) |
| 511 | |
| 512 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 513 | explicit WatchDog(bool is_watch_dog_enabled) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 514 | is_watch_dog_enabled_ = is_watch_dog_enabled; |
| 515 | if (!is_watch_dog_enabled_) { |
| 516 | return; |
| 517 | } |
| 518 | shutting_down_ = false; |
| 519 | const char* reason = "dex2oat watch dog thread startup"; |
| 520 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason); |
| 521 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason); |
| 522 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); |
| 523 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); |
| 524 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); |
| 525 | } |
| 526 | ~WatchDog() { |
| 527 | if (!is_watch_dog_enabled_) { |
| 528 | return; |
| 529 | } |
| 530 | const char* reason = "dex2oat watch dog thread shutdown"; |
| 531 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 532 | shutting_down_ = true; |
| 533 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); |
| 534 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 535 | |
| 536 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason); |
| 537 | |
| 538 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); |
| 539 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); |
| 540 | } |
| 541 | |
| 542 | private: |
| 543 | static void* CallBack(void* arg) { |
| 544 | WatchDog* self = reinterpret_cast<WatchDog*>(arg); |
| 545 | ::art::SetThreadName("dex2oat watch dog"); |
| 546 | self->Wait(); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | static void Message(char severity, const std::string& message) { |
| 551 | // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error |
| 552 | // cases. |
| 553 | fprintf(stderr, "dex2oat%s %c %d %d %s\n", |
| 554 | kIsDebugBuild ? "d" : "", |
| 555 | severity, |
| 556 | getpid(), |
| 557 | GetTid(), |
| 558 | message.c_str()); |
| 559 | } |
| 560 | |
| 561 | static void Warn(const std::string& message) { |
| 562 | Message('W', message); |
| 563 | } |
| 564 | |
| 565 | static void Fatal(const std::string& message) { |
| 566 | Message('F', message); |
| 567 | exit(1); |
| 568 | } |
| 569 | |
| 570 | void Wait() { |
| 571 | bool warning = true; |
| 572 | CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds); |
| 573 | // TODO: tune the multiplier for GC verification, the following is just to make the timeout |
| 574 | // large. |
| 575 | int64_t multiplier = gc::kDesiredHeapVerification > gc::kVerifyAllFast ? 100 : 1; |
| 576 | timespec warning_ts; |
| 577 | InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts); |
| 578 | timespec timeout_ts; |
| 579 | InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); |
| 580 | const char* reason = "dex2oat watch dog thread waiting"; |
| 581 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 582 | while (!shutting_down_) { |
| 583 | int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, |
| 584 | warning ? &warning_ts |
| 585 | : &timeout_ts)); |
| 586 | if (rc == ETIMEDOUT) { |
| 587 | std::string message(StringPrintf("dex2oat did not finish after %d seconds", |
| 588 | warning ? kWatchDogWarningSeconds |
| 589 | : kWatchDogTimeoutSeconds)); |
| 590 | if (warning) { |
| 591 | Warn(message.c_str()); |
| 592 | warning = false; |
| 593 | } else { |
| 594 | Fatal(message.c_str()); |
| 595 | } |
| 596 | } else if (rc != 0) { |
| 597 | std::string message(StringPrintf("pthread_cond_timedwait failed: %s", |
| 598 | strerror(errno))); |
| 599 | Fatal(message.c_str()); |
| 600 | } |
| 601 | } |
| 602 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 603 | } |
| 604 | |
| 605 | // When setting timeouts, keep in mind that the build server may not be as fast as your desktop. |
| 606 | #if ART_USE_PORTABLE_COMPILER |
| 607 | static const unsigned int kWatchDogWarningSeconds = 2 * 60; // 2 minutes. |
| 608 | static const unsigned int kWatchDogTimeoutSeconds = 30 * 60; // 25 minutes + buffer. |
| 609 | #else |
| 610 | static const unsigned int kWatchDogWarningSeconds = 1 * 60; // 1 minute. |
| 611 | static const unsigned int kWatchDogTimeoutSeconds = 6 * 60; // 5 minutes + buffer. |
| 612 | #endif |
| 613 | |
| 614 | bool is_watch_dog_enabled_; |
| 615 | bool shutting_down_; |
| 616 | // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases. |
| 617 | pthread_mutex_t mutex_; |
| 618 | pthread_cond_t cond_; |
| 619 | pthread_attr_t attr_; |
| 620 | pthread_t pthread_; |
| 621 | }; |
| 622 | const unsigned int WatchDog::kWatchDogWarningSeconds; |
| 623 | const unsigned int WatchDog::kWatchDogTimeoutSeconds; |
| 624 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 625 | // Given a set of instruction features from the build, parse it. The |
| 626 | // input 'str' is a comma separated list of feature names. Parse it and |
| 627 | // return the InstructionSetFeatures object. |
| 628 | static InstructionSetFeatures ParseFeatureList(std::string str) { |
| 629 | InstructionSetFeatures result; |
| 630 | typedef std::vector<std::string> FeatureList; |
| 631 | FeatureList features; |
| 632 | Split(str, ',', features); |
| 633 | for (FeatureList::iterator i = features.begin(); i != features.end(); i++) { |
| 634 | std::string feature = Trim(*i); |
| 635 | if (feature == "default") { |
| 636 | // Nothing to do. |
| 637 | } else if (feature == "div") { |
| 638 | // Supports divide instruction. |
| 639 | result.SetHasDivideInstruction(true); |
| 640 | } else if (feature == "nodiv") { |
| 641 | // Turn off support for divide instruction. |
| 642 | result.SetHasDivideInstruction(false); |
| 643 | } else { |
| 644 | Usage("Unknown instruction set feature: '%s'", feature.c_str()); |
| 645 | } |
| 646 | } |
| 647 | // others... |
| 648 | return result; |
| 649 | } |
| 650 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 651 | static int dex2oat(int argc, char** argv) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 652 | TimingLogger timings("compiler", false, false); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 653 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 654 | InitLogging(argv); |
| 655 | |
| 656 | // Skip over argv[0]. |
| 657 | argv++; |
| 658 | argc--; |
| 659 | |
| 660 | if (argc == 0) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 661 | Usage("No arguments specified"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | std::vector<const char*> dex_filenames; |
| 665 | std::vector<const char*> dex_locations; |
| 666 | int zip_fd = -1; |
| 667 | std::string zip_location; |
| 668 | std::string oat_filename; |
| 669 | std::string oat_symbols; |
| 670 | std::string oat_location; |
| 671 | int oat_fd = -1; |
| 672 | std::string bitcode_filename; |
| 673 | const char* image_classes_zip_filename = NULL; |
| 674 | const char* image_classes_filename = NULL; |
| 675 | std::string image_filename; |
| 676 | std::string boot_image_filename; |
| 677 | uintptr_t image_base = 0; |
| 678 | UniquePtr<std::string> host_prefix; |
| 679 | std::string android_root; |
| 680 | std::vector<const char*> runtime_args; |
| 681 | int thread_count = sysconf(_SC_NPROCESSORS_CONF); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 682 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 683 | CompilerBackend compiler_backend = kPortable; |
| 684 | #else |
| 685 | CompilerBackend compiler_backend = kQuick; |
| 686 | #endif |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 687 | |
Brian Carlstrom | 1bd2ceb | 2013-11-06 00:29:48 -0800 | [diff] [blame] | 688 | // Take the default set of instruction features from the build. |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 689 | InstructionSetFeatures instruction_set_features = |
Brian Carlstrom | 1bd2ceb | 2013-11-06 00:29:48 -0800 | [diff] [blame] | 690 | ParseFeatureList(STRINGIFY(ART_DEFAULT_INSTRUCTION_SET_FEATURES)); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 691 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 692 | #if defined(__arm__) |
| 693 | InstructionSet instruction_set = kThumb2; |
| 694 | #elif defined(__i386__) |
| 695 | InstructionSet instruction_set = kX86; |
| 696 | #elif defined(__mips__) |
| 697 | InstructionSet instruction_set = kMips; |
| 698 | #else |
| 699 | #error "Unsupported architecture" |
| 700 | #endif |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 701 | |
| 702 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 703 | bool is_host = false; |
Ian Rogers | e732ef1 | 2013-10-09 15:22:24 -0700 | [diff] [blame] | 704 | bool dump_stats = false; |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 705 | bool dump_timing = false; |
| 706 | bool dump_slow_timing = kIsDebugBuild; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 707 | bool watch_dog_enabled = !kIsTargetBuild; |
| 708 | |
| 709 | |
| 710 | for (int i = 0; i < argc; i++) { |
| 711 | const StringPiece option(argv[i]); |
| 712 | bool log_options = false; |
| 713 | if (log_options) { |
| 714 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 715 | } |
| 716 | if (option.starts_with("--dex-file=")) { |
| 717 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
| 718 | } else if (option.starts_with("--dex-location=")) { |
| 719 | dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); |
| 720 | } else if (option.starts_with("--zip-fd=")) { |
| 721 | const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); |
| 722 | if (!ParseInt(zip_fd_str, &zip_fd)) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 723 | Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 724 | } |
| 725 | } else if (option.starts_with("--zip-location=")) { |
| 726 | zip_location = option.substr(strlen("--zip-location=")).data(); |
| 727 | } else if (option.starts_with("--oat-file=")) { |
| 728 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
| 729 | } else if (option.starts_with("--oat-symbols=")) { |
| 730 | oat_symbols = option.substr(strlen("--oat-symbols=")).data(); |
| 731 | } else if (option.starts_with("--oat-fd=")) { |
| 732 | const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); |
| 733 | if (!ParseInt(oat_fd_str, &oat_fd)) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 734 | Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 735 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 736 | } else if (option == "--watch-dog") { |
| 737 | watch_dog_enabled = true; |
| 738 | } else if (option == "--no-watch-dog") { |
| 739 | watch_dog_enabled = false; |
| 740 | } else if (option.starts_with("-j")) { |
| 741 | const char* thread_count_str = option.substr(strlen("-j")).data(); |
| 742 | if (!ParseInt(thread_count_str, &thread_count)) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 743 | Usage("Failed to parse -j argument '%s' as an integer", thread_count_str); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 744 | } |
| 745 | } else if (option.starts_with("--oat-location=")) { |
| 746 | oat_location = option.substr(strlen("--oat-location=")).data(); |
| 747 | } else if (option.starts_with("--bitcode=")) { |
| 748 | bitcode_filename = option.substr(strlen("--bitcode=")).data(); |
| 749 | } else if (option.starts_with("--image=")) { |
| 750 | image_filename = option.substr(strlen("--image=")).data(); |
| 751 | } else if (option.starts_with("--image-classes=")) { |
| 752 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
| 753 | } else if (option.starts_with("--image-classes-zip=")) { |
| 754 | image_classes_zip_filename = option.substr(strlen("--image-classes-zip=")).data(); |
| 755 | } else if (option.starts_with("--base=")) { |
| 756 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 757 | char* end; |
| 758 | image_base = strtoul(image_base_str, &end, 16); |
| 759 | if (end == image_base_str || *end != '\0') { |
| 760 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
| 761 | } |
| 762 | } else if (option.starts_with("--boot-image=")) { |
| 763 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
| 764 | } else if (option.starts_with("--host-prefix=")) { |
| 765 | host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); |
| 766 | } else if (option.starts_with("--android-root=")) { |
| 767 | android_root = option.substr(strlen("--android-root=")).data(); |
| 768 | } else if (option.starts_with("--instruction-set=")) { |
| 769 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
| 770 | if (instruction_set_str == "arm") { |
| 771 | instruction_set = kThumb2; |
| 772 | } else if (instruction_set_str == "mips") { |
| 773 | instruction_set = kMips; |
| 774 | } else if (instruction_set_str == "x86") { |
| 775 | instruction_set = kX86; |
| 776 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 777 | } else if (option.starts_with("--instruction-set-features=")) { |
| 778 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 779 | instruction_set_features = ParseFeatureList(str.as_string()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 780 | } else if (option.starts_with("--compiler-backend=")) { |
| 781 | StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); |
| 782 | if (backend_str == "Quick") { |
| 783 | compiler_backend = kQuick; |
| 784 | } else if (backend_str == "Portable") { |
| 785 | compiler_backend = kPortable; |
| 786 | } |
| 787 | } else if (option == "--host") { |
| 788 | is_host = true; |
| 789 | } else if (option == "--runtime-arg") { |
| 790 | if (++i >= argc) { |
| 791 | Usage("Missing required argument for --runtime-arg"); |
| 792 | } |
| 793 | if (log_options) { |
| 794 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 795 | } |
| 796 | runtime_args.push_back(argv[i]); |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 797 | } else if (option == "--dump-timing") { |
| 798 | dump_timing = true; |
Ian Rogers | e732ef1 | 2013-10-09 15:22:24 -0700 | [diff] [blame] | 799 | } else if (option == "--dump-stats") { |
| 800 | dump_stats = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 801 | } else { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 802 | Usage("Unknown argument %s", option.data()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
| 806 | if (oat_filename.empty() && oat_fd == -1) { |
| 807 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
| 808 | } |
| 809 | |
| 810 | if (!oat_filename.empty() && oat_fd != -1) { |
| 811 | Usage("--oat-file should not be used with --oat-fd"); |
| 812 | } |
| 813 | |
| 814 | if (!oat_symbols.empty() && oat_fd != -1) { |
| 815 | Usage("--oat-symbols should not be used with --oat-fd"); |
| 816 | } |
| 817 | |
| 818 | if (!oat_symbols.empty() && is_host) { |
| 819 | Usage("--oat-symbols should not be used with --host"); |
| 820 | } |
| 821 | |
| 822 | if (oat_fd != -1 && !image_filename.empty()) { |
| 823 | Usage("--oat-fd should not be used with --image"); |
| 824 | } |
| 825 | |
| 826 | if (host_prefix.get() == NULL) { |
| 827 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 828 | if (android_product_out != NULL) { |
| 829 | host_prefix.reset(new std::string(android_product_out)); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (android_root.empty()) { |
| 834 | const char* android_root_env_var = getenv("ANDROID_ROOT"); |
| 835 | if (android_root_env_var == NULL) { |
| 836 | Usage("--android-root unspecified and ANDROID_ROOT not set"); |
| 837 | } |
| 838 | android_root += android_root_env_var; |
| 839 | } |
| 840 | |
| 841 | bool image = (!image_filename.empty()); |
| 842 | if (!image && boot_image_filename.empty()) { |
| 843 | if (host_prefix.get() == NULL) { |
| 844 | boot_image_filename += GetAndroidRoot(); |
| 845 | } else { |
| 846 | boot_image_filename += *host_prefix.get(); |
| 847 | boot_image_filename += "/system"; |
| 848 | } |
| 849 | boot_image_filename += "/framework/boot.art"; |
| 850 | } |
| 851 | std::string boot_image_option; |
| 852 | if (!boot_image_filename.empty()) { |
| 853 | boot_image_option += "-Ximage:"; |
| 854 | boot_image_option += boot_image_filename; |
| 855 | } |
| 856 | |
| 857 | if (image_classes_filename != NULL && !image) { |
| 858 | Usage("--image-classes should only be used with --image"); |
| 859 | } |
| 860 | |
| 861 | if (image_classes_filename != NULL && !boot_image_option.empty()) { |
| 862 | Usage("--image-classes should not be used with --boot-image"); |
| 863 | } |
| 864 | |
| 865 | if (image_classes_zip_filename != NULL && image_classes_filename == NULL) { |
| 866 | Usage("--image-classes-zip should be used with --image-classes"); |
| 867 | } |
| 868 | |
| 869 | if (dex_filenames.empty() && zip_fd == -1) { |
| 870 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
| 871 | } |
| 872 | |
| 873 | if (!dex_filenames.empty() && zip_fd != -1) { |
| 874 | Usage("--dex-file should not be used with --zip-fd"); |
| 875 | } |
| 876 | |
| 877 | if (!dex_filenames.empty() && !zip_location.empty()) { |
| 878 | Usage("--dex-file should not be used with --zip-location"); |
| 879 | } |
| 880 | |
| 881 | if (dex_locations.empty()) { |
| 882 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 883 | dex_locations.push_back(dex_filenames[i]); |
| 884 | } |
| 885 | } else if (dex_locations.size() != dex_filenames.size()) { |
| 886 | Usage("--dex-location arguments do not match --dex-file arguments"); |
| 887 | } |
| 888 | |
| 889 | if (zip_fd != -1 && zip_location.empty()) { |
| 890 | Usage("--zip-location should be supplied with --zip-fd"); |
| 891 | } |
| 892 | |
| 893 | if (boot_image_option.empty()) { |
| 894 | if (image_base == 0) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 895 | Usage("Non-zero --base not specified"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
| 899 | std::string oat_stripped(oat_filename); |
| 900 | std::string oat_unstripped; |
| 901 | if (!oat_symbols.empty()) { |
| 902 | oat_unstripped += oat_symbols; |
| 903 | } else { |
| 904 | oat_unstripped += oat_filename; |
| 905 | } |
| 906 | |
| 907 | // Done with usage checks, enable watchdog if requested |
| 908 | WatchDog watch_dog(watch_dog_enabled); |
| 909 | |
| 910 | // Check early that the result of compilation can be written |
| 911 | UniquePtr<File> oat_file; |
| 912 | bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor |
| 913 | if (create_file) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 914 | oat_file.reset(OS::CreateEmptyFile(oat_unstripped.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 915 | if (oat_location.empty()) { |
| 916 | oat_location = oat_filename; |
| 917 | } |
| 918 | } else { |
| 919 | oat_file.reset(new File(oat_fd, oat_location)); |
| 920 | oat_file->DisableAutoClose(); |
| 921 | } |
| 922 | if (oat_file.get() == NULL) { |
| 923 | PLOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 924 | return EXIT_FAILURE; |
| 925 | } |
| 926 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 927 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; |
| 928 | return EXIT_FAILURE; |
| 929 | } |
| 930 | |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 931 | timings.StartSplit("dex2oat Setup"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 932 | LOG(INFO) << "dex2oat: " << oat_location; |
| 933 | |
Ian Rogers | f30f6da | 2013-08-28 17:33:30 -0700 | [diff] [blame] | 934 | if (image) { |
| 935 | bool has_compiler_filter = false; |
| 936 | for (const char* r : runtime_args) { |
| 937 | if (strncmp(r, "-compiler-filter:", 17) == 0) { |
| 938 | has_compiler_filter = true; |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | if (!has_compiler_filter) { |
| 943 | runtime_args.push_back("-compiler-filter:everything"); |
| 944 | } |
| 945 | } |
| 946 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 947 | Runtime::Options options; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 948 | std::vector<const DexFile*> boot_class_path; |
| 949 | if (boot_image_option.empty()) { |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 950 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); |
| 951 | if (failure_count > 0) { |
| 952 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 953 | return EXIT_FAILURE; |
| 954 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 955 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
| 956 | } else { |
| 957 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 958 | } |
| 959 | if (host_prefix.get() != NULL) { |
| 960 | options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); |
| 961 | } |
| 962 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 963 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 964 | } |
| 965 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 966 | #ifdef ART_SEA_IR_MODE |
| 967 | options.push_back(std::make_pair("-sea_ir", reinterpret_cast<void*>(NULL))); |
| 968 | #endif |
| 969 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 970 | Dex2Oat* p_dex2oat; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 971 | if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, |
| 972 | instruction_set_features, thread_count)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 973 | LOG(ERROR) << "Failed to create dex2oat"; |
| 974 | return EXIT_FAILURE; |
| 975 | } |
| 976 | UniquePtr<Dex2Oat> dex2oat(p_dex2oat); |
| 977 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 978 | // give it away now so that we don't starve GC. |
| 979 | Thread* self = Thread::Current(); |
| 980 | self->TransitionFromRunnableToSuspended(kNative); |
Ian Rogers | 0f40ac3 | 2013-08-13 22:10:30 -0700 | [diff] [blame] | 981 | // If we're doing the image, override the compiler filter to force full compilation. Must be |
buzbee | fe9ca40 | 2013-08-21 09:48:11 -0700 | [diff] [blame] | 982 | // done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force |
| 983 | // compilation of class initializers. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 984 | // Whilst we're in native take the opportunity to initialize well known classes. |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 985 | WellKnownClasses::Init(self->GetJniEnv()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 986 | |
| 987 | // If --image-classes was specified, calculate the full list of classes to include in the image |
| 988 | UniquePtr<CompilerDriver::DescriptorSet> image_classes(NULL); |
| 989 | if (image_classes_filename != NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 990 | std::string error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 991 | if (image_classes_zip_filename != NULL) { |
| 992 | image_classes.reset(dex2oat->ReadImageClassesFromZip(image_classes_zip_filename, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 993 | image_classes_filename, |
| 994 | &error_msg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 995 | } else { |
| 996 | image_classes.reset(dex2oat->ReadImageClassesFromFile(image_classes_filename)); |
| 997 | } |
| 998 | if (image_classes.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 999 | LOG(ERROR) << "Failed to create list of image classes from '" << image_classes_filename << |
| 1000 | "': " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1001 | return EXIT_FAILURE; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | std::vector<const DexFile*> dex_files; |
| 1006 | if (boot_image_option.empty()) { |
| 1007 | dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 1008 | } else { |
| 1009 | if (dex_filenames.empty()) { |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame^] | 1010 | ATRACE_BEGIN("Opening zip archive from file descriptor"); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1011 | std::string error_msg; |
| 1012 | UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd, zip_location.c_str(), |
| 1013 | &error_msg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1014 | if (zip_archive.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1015 | LOG(ERROR) << "Failed to open zip from file descriptor for '" << zip_location << "': " |
| 1016 | << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1017 | return EXIT_FAILURE; |
| 1018 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1019 | const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location, &error_msg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1020 | if (dex_file == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1021 | LOG(ERROR) << "Failed to open dex from file descriptor for zip file '" << zip_location |
| 1022 | << "': " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1023 | return EXIT_FAILURE; |
| 1024 | } |
| 1025 | dex_files.push_back(dex_file); |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame^] | 1026 | ATRACE_END(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1027 | } else { |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 1028 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); |
| 1029 | if (failure_count > 0) { |
| 1030 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 1031 | return EXIT_FAILURE; |
| 1032 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1033 | } |
Brian Carlstrom | d76e083 | 2013-08-29 15:17:42 -0700 | [diff] [blame] | 1034 | |
| 1035 | // Ensure opened dex files are writable for dex-to-dex transformations. |
| 1036 | for (const auto& dex_file : dex_files) { |
| 1037 | if (!dex_file->EnableWrite()) { |
| 1038 | PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n"; |
| 1039 | } |
| 1040 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * If we're not in interpret-only mode, go ahead and compile small applications. Don't |
| 1045 | * bother to check if we're doing the image. |
| 1046 | */ |
| 1047 | if (!image && (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1048 | size_t num_methods = 0; |
| 1049 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1050 | const DexFile* dex_file = dex_files[i]; |
| 1051 | CHECK(dex_file != NULL); |
| 1052 | num_methods += dex_file->NumMethodIds(); |
| 1053 | } |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 1054 | if (num_methods <= Runtime::Current()->GetNumDexMethodsThreshold()) { |
| 1055 | Runtime::Current()->SetCompilerFilter(Runtime::kSpeed); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1056 | VLOG(compiler) << "Below method threshold, compiling anyways"; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | UniquePtr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option, |
| 1061 | host_prefix.get(), |
| 1062 | android_root, |
| 1063 | is_host, |
| 1064 | dex_files, |
| 1065 | oat_file.get(), |
| 1066 | bitcode_filename, |
| 1067 | image, |
| 1068 | image_classes, |
| 1069 | dump_stats, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1070 | timings)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1071 | |
| 1072 | if (compiler.get() == NULL) { |
| 1073 | LOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 1074 | return EXIT_FAILURE; |
| 1075 | } |
| 1076 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1077 | VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1078 | |
| 1079 | // Notes on the interleaving of creating the image and oat file to |
| 1080 | // ensure the references between the two are correct. |
| 1081 | // |
| 1082 | // Currently we have a memory layout that looks something like this: |
| 1083 | // |
| 1084 | // +--------------+ |
| 1085 | // | image | |
| 1086 | // +--------------+ |
| 1087 | // | boot oat | |
| 1088 | // +--------------+ |
| 1089 | // | alloc spaces | |
| 1090 | // +--------------+ |
| 1091 | // |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1092 | // There are several constraints on the loading of the image and boot.oat. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1093 | // |
| 1094 | // 1. The image is expected to be loaded at an absolute address and |
| 1095 | // contains Objects with absolute pointers within the image. |
| 1096 | // |
| 1097 | // 2. There are absolute pointers from Methods in the image to their |
| 1098 | // code in the oat. |
| 1099 | // |
| 1100 | // 3. There are absolute pointers from the code in the oat to Methods |
| 1101 | // in the image. |
| 1102 | // |
| 1103 | // 4. There are absolute pointers from code in the oat to other code |
| 1104 | // in the oat. |
| 1105 | // |
| 1106 | // To get this all correct, we go through several steps. |
| 1107 | // |
| 1108 | // 1. We have already created that oat file above with |
| 1109 | // CreateOatFile. Originally this was just our own proprietary file |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1110 | // but now it is contained within an ELF dynamic object (aka an .so |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1111 | // file). The Compiler returned by CreateOatFile provides |
| 1112 | // PatchInformation for references to oat code and Methods that need |
| 1113 | // to be update once we know where the oat file will be located |
| 1114 | // after the image. |
| 1115 | // |
| 1116 | // 2. We create the image file. It needs to know where the oat file |
| 1117 | // will be loaded after itself. Originally when oat file was simply |
| 1118 | // memory mapped so we could predict where its contents were based |
| 1119 | // on the file size. Now that it is an ELF file, we need to inspect |
| 1120 | // the ELF file to understand the in memory segment layout including |
| 1121 | // where the oat header is located within. ImageWriter's |
| 1122 | // PatchOatCodeAndMethods uses the PatchInformation from the |
| 1123 | // Compiler to touch up absolute references in the oat file. |
| 1124 | // |
| 1125 | // 3. We fixup the ELF program headers so that dlopen will try to |
| 1126 | // load the .so at the desired location at runtime by offsetting the |
| 1127 | // Elf32_Phdr.p_vaddr values by the desired base address. |
| 1128 | // |
| 1129 | if (image) { |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1130 | timings.NewSplit("dex2oat ImageWriter"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1131 | bool image_creation_success = dex2oat->CreateImageFile(image_filename, |
| 1132 | image_base, |
| 1133 | oat_unstripped, |
| 1134 | oat_location, |
| 1135 | *compiler.get()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1136 | if (!image_creation_success) { |
| 1137 | return EXIT_FAILURE; |
| 1138 | } |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1139 | VLOG(compiler) << "Image written successfully: " << image_filename; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | if (is_host) { |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 1143 | if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1144 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1145 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1146 | return EXIT_SUCCESS; |
| 1147 | } |
| 1148 | |
| 1149 | // If we don't want to strip in place, copy from unstripped location to stripped location. |
| 1150 | // We need to strip after image creation because FixupElf needs to use .strtab. |
| 1151 | if (oat_unstripped != oat_stripped) { |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1152 | timings.NewSplit("dex2oat OatFile copy"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1153 | oat_file.reset(); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1154 | UniquePtr<File> in(OS::OpenFileForReading(oat_unstripped.c_str())); |
| 1155 | UniquePtr<File> out(OS::CreateEmptyFile(oat_stripped.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1156 | size_t buffer_size = 8192; |
| 1157 | UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]); |
| 1158 | while (true) { |
| 1159 | int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); |
| 1160 | if (bytes_read <= 0) { |
| 1161 | break; |
| 1162 | } |
| 1163 | bool write_ok = out->WriteFully(buffer.get(), bytes_read); |
| 1164 | CHECK(write_ok); |
| 1165 | } |
| 1166 | oat_file.reset(out.release()); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1167 | VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
Brian Carlstrom | 7fcba11 | 2013-07-22 10:28:48 -0700 | [diff] [blame] | 1170 | #if ART_USE_PORTABLE_COMPILER // We currently only generate symbols on Portable |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1171 | timings.NewSplit("dex2oat ElfStripper"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1172 | // Strip unneeded sections for target |
| 1173 | off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET); |
| 1174 | CHECK_EQ(0, seek_actual); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1175 | std::string error_msg; |
| 1176 | CHECK(ElfStripper::Strip(oat_file.get(), &error_msg)) << error_msg; |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1177 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1178 | |
| 1179 | // We wrote the oat file successfully, and want to keep it. |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1180 | VLOG(compiler) << "Oat file written successfully (stripped): " << oat_location; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1181 | #endif // ART_USE_PORTABLE_COMPILER |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1182 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1183 | timings.EndSplit(); |
| 1184 | |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 1185 | if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1186 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1187 | } |
Ian Rogers | 2672a9f | 2013-09-05 17:24:22 -0700 | [diff] [blame] | 1188 | |
| 1189 | // Everything was successfully written, do an explicit exit here to avoid running Runtime |
| 1190 | // destructors that take time (bug 10645725) unless we're a debug build or running on valgrind. |
| 1191 | if (!kIsDebugBuild || (RUNNING_ON_VALGRIND == 0)) { |
| 1192 | exit(EXIT_SUCCESS); |
| 1193 | } |
| 1194 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1195 | return EXIT_SUCCESS; |
| 1196 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1197 | } // namespace art |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1198 | |
| 1199 | int main(int argc, char** argv) { |
| 1200 | return art::dex2oat(argc, argv); |
| 1201 | } |