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 "image_writer.h" |
| 18 | |
| 19 | #include <sys/stat.h> |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 20 | #include <lz4.h> |
Mathieu Chartier | 167e638 | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 21 | #include <lz4hc.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 24 | #include <numeric> |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 25 | #include <unordered_set> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 28 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 29 | #include "art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 30 | #include "base/logging.h" |
| 31 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 32 | #include "class_linker-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "compiled_method.h" |
| 34 | #include "dex_file-inl.h" |
| 35 | #include "driver/compiler_driver.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 36 | #include "elf_file.h" |
| 37 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | #include "elf_writer.h" |
| 39 | #include "gc/accounting/card_table-inl.h" |
| 40 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 41 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 42 | #include "gc/heap.h" |
| 43 | #include "gc/space/large_object_space.h" |
| 44 | #include "gc/space/space-inl.h" |
| 45 | #include "globals.h" |
| 46 | #include "image.h" |
| 47 | #include "intern_table.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 48 | #include "linear_alloc.h" |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 49 | #include "lock_word.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 50 | #include "mirror/abstract_method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | #include "mirror/array-inl.h" |
| 52 | #include "mirror/class-inl.h" |
| 53 | #include "mirror/class_loader.h" |
| 54 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 55 | #include "mirror/method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | #include "mirror/object-inl.h" |
| 57 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 58 | #include "mirror/string-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 59 | #include "oat.h" |
| 60 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 61 | #include "oat_file_manager.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 62 | #include "runtime.h" |
| 63 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 64 | #include "handle_scope-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 65 | #include "utils/dex_cache_arrays_layout-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 66 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 67 | using ::art::mirror::Class; |
| 68 | using ::art::mirror::DexCache; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 69 | using ::art::mirror::Object; |
| 70 | using ::art::mirror::ObjectArray; |
| 71 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 72 | |
| 73 | namespace art { |
| 74 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 75 | // Separate objects into multiple bins to optimize dirty memory use. |
| 76 | static constexpr bool kBinObjects = true; |
| 77 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 78 | // Return true if an object is already in an image space. |
| 79 | bool ImageWriter::IsInBootImage(const void* obj) const { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 80 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 81 | if (!compile_app_image_) { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 82 | DCHECK(heap->GetBootImageSpaces().empty()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 83 | return false; |
| 84 | } |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 85 | for (gc::space::ImageSpace* boot_image_space : heap->GetBootImageSpaces()) { |
| 86 | const uint8_t* image_begin = boot_image_space->Begin(); |
| 87 | // Real image end including ArtMethods and ArtField sections. |
| 88 | const uint8_t* image_end = image_begin + boot_image_space->GetImageHeader().GetImageSize(); |
| 89 | if (image_begin <= obj && obj < image_end) { |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | return false; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool ImageWriter::IsInBootOatFile(const void* ptr) const { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 97 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 98 | if (!compile_app_image_) { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 99 | DCHECK(heap->GetBootImageSpaces().empty()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 100 | return false; |
| 101 | } |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 102 | for (gc::space::ImageSpace* boot_image_space : heap->GetBootImageSpaces()) { |
| 103 | const ImageHeader& image_header = boot_image_space->GetImageHeader(); |
| 104 | if (image_header.GetOatFileBegin() <= ptr && ptr < image_header.GetOatFileEnd()) { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | return false; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 111 | static void CheckNoDexObjectsCallback(Object* obj, void* arg ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 112 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 113 | Class* klass = obj->GetClass(); |
| 114 | CHECK_NE(PrettyClass(klass), "com.android.dex.Dex"); |
| 115 | } |
| 116 | |
| 117 | static void CheckNoDexObjects() { |
| 118 | ScopedObjectAccess soa(Thread::Current()); |
| 119 | Runtime::Current()->GetHeap()->VisitObjects(CheckNoDexObjectsCallback, nullptr); |
| 120 | } |
| 121 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 122 | bool ImageWriter::PrepareImageAddressSpace() { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 123 | target_ptr_size_ = InstructionSetPointerSize(compiler_driver_.GetInstructionSet()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 124 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 125 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 126 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 127 | PruneNonImageClasses(); // Remove junk |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 128 | if (!compile_app_image_) { |
| 129 | // Avoid for app image since this may increase RAM and image size. |
| 130 | ComputeLazyFieldsForImageClasses(); // Add useful information |
| 131 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 132 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 133 | heap->CollectGarbage(false); // Remove garbage. |
| 134 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 135 | // Dex caches must not have their dex fields set in the image. These are memory buffers of mapped |
| 136 | // dex files. |
| 137 | // |
| 138 | // We may open them in the unstarted-runtime code for class metadata. Their fields should all be |
| 139 | // reset in PruneNonImageClasses and the objects reclaimed in the GC. Make sure that's actually |
| 140 | // true. |
| 141 | if (kIsDebugBuild) { |
| 142 | CheckNoDexObjects(); |
| 143 | } |
| 144 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 145 | if (kIsDebugBuild) { |
| 146 | ScopedObjectAccess soa(Thread::Current()); |
| 147 | CheckNonImageClassesRemoved(); |
| 148 | } |
| 149 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 150 | { |
| 151 | ScopedObjectAccess soa(Thread::Current()); |
| 152 | CalculateNewObjectOffsets(); |
| 153 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 154 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 155 | // This needs to happen after CalculateNewObjectOffsets since it relies on intern_table_bytes_ and |
| 156 | // bin size sums being calculated. |
| 157 | if (!AllocMemory()) { |
| 158 | return false; |
| 159 | } |
| 160 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 164 | bool ImageWriter::Write(int image_fd, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 165 | const std::vector<const char*>& image_filenames, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 166 | int oat_fd, |
| 167 | const std::vector<const char*>& oat_filenames, |
| 168 | const std::string& oat_location) { |
| 169 | // If image_fd or oat_fd are not kInvalidFd then we may have empty strings in image_filenames or |
| 170 | // oat_filenames. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 171 | CHECK(!image_filenames.empty()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 172 | if (image_fd != kInvalidFd) { |
| 173 | CHECK_EQ(image_filenames.size(), 1u); |
| 174 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 175 | CHECK(!oat_filenames.empty()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 176 | if (oat_fd != kInvalidFd) { |
| 177 | CHECK_EQ(oat_filenames.size(), 1u); |
| 178 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 179 | CHECK_EQ(image_filenames.size(), oat_filenames.size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 180 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 181 | size_t oat_file_offset = 0; |
| 182 | |
| 183 | for (size_t i = 0; i < oat_filenames.size(); ++i) { |
| 184 | const char* oat_filename = oat_filenames[i]; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 185 | std::unique_ptr<File> oat_file; |
| 186 | |
| 187 | if (oat_fd != -1) { |
| 188 | if (strlen(oat_filename) == 0u) { |
| 189 | oat_file.reset(new File(oat_fd, false)); |
| 190 | } else { |
| 191 | oat_file.reset(new File(oat_fd, oat_filename, false)); |
| 192 | } |
| 193 | int length = oat_file->GetLength(); |
| 194 | if (length < 0) { |
| 195 | PLOG(ERROR) << "Oat file has negative length " << length; |
| 196 | return false; |
| 197 | } else { |
| 198 | // Leave the fd open since dex2oat still needs to write out the oat file with the fd. |
| 199 | oat_file->DisableAutoClose(); |
| 200 | } |
| 201 | } else { |
| 202 | oat_file.reset(OS::OpenFileReadWrite(oat_filename)); |
| 203 | } |
| 204 | if (oat_file == nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 205 | PLOG(ERROR) << "Failed to open oat file " << oat_filename; |
| 206 | return false; |
| 207 | } |
| 208 | std::string error_msg; |
| 209 | oat_file_ = OatFile::OpenReadable(oat_file.get(), oat_filename, nullptr, &error_msg); |
| 210 | if (oat_file_ == nullptr) { |
| 211 | PLOG(ERROR) << "Failed to open writable oat file " << oat_filename; |
| 212 | oat_file->Erase(); |
| 213 | return false; |
| 214 | } |
| 215 | Runtime::Current()->GetOatFileManager().RegisterOatFile( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 216 | std::unique_ptr<const OatFile>(oat_file_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 217 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 218 | const OatHeader& oat_header = oat_file_->GetOatHeader(); |
| 219 | ImageInfo& image_info = GetImageInfo(oat_filename); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 220 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 221 | size_t oat_loaded_size = 0; |
| 222 | size_t oat_data_offset = 0; |
| 223 | ElfWriter::GetOatElfInformation(oat_file.get(), &oat_loaded_size, &oat_data_offset); |
| 224 | |
| 225 | DCHECK_EQ(image_info.oat_offset_, oat_file_offset); |
| 226 | oat_file_offset += oat_loaded_size; |
| 227 | |
| 228 | if (i == 0) { |
| 229 | // Primary oat file, read the trampolines. |
| 230 | image_info.oat_address_offsets_[kOatAddressInterpreterToInterpreterBridge] = |
| 231 | oat_header.GetInterpreterToInterpreterBridgeOffset(); |
| 232 | image_info.oat_address_offsets_[kOatAddressInterpreterToCompiledCodeBridge] = |
| 233 | oat_header.GetInterpreterToCompiledCodeBridgeOffset(); |
| 234 | image_info.oat_address_offsets_[kOatAddressJNIDlsymLookup] = |
| 235 | oat_header.GetJniDlsymLookupOffset(); |
| 236 | image_info.oat_address_offsets_[kOatAddressQuickGenericJNITrampoline] = |
| 237 | oat_header.GetQuickGenericJniTrampolineOffset(); |
| 238 | image_info.oat_address_offsets_[kOatAddressQuickIMTConflictTrampoline] = |
| 239 | oat_header.GetQuickImtConflictTrampolineOffset(); |
| 240 | image_info.oat_address_offsets_[kOatAddressQuickResolutionTrampoline] = |
| 241 | oat_header.GetQuickResolutionTrampolineOffset(); |
| 242 | image_info.oat_address_offsets_[kOatAddressQuickToInterpreterBridge] = |
| 243 | oat_header.GetQuickToInterpreterBridgeOffset(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
| 247 | { |
| 248 | ScopedObjectAccess soa(Thread::Current()); |
| 249 | CreateHeader(oat_loaded_size, oat_data_offset); |
| 250 | CopyAndFixupNativeData(); |
| 251 | } |
| 252 | |
| 253 | SetOatChecksumFromElfFile(oat_file.get()); |
| 254 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 255 | if (oat_fd != -1) { |
| 256 | // Leave fd open for caller. |
| 257 | if (oat_file->Flush() != 0) { |
| 258 | LOG(ERROR) << "Failed to flush oat file " << oat_filename << " for " << oat_location; |
| 259 | return false; |
| 260 | } |
| 261 | } else if (oat_file->FlushCloseOrErase() != 0) { |
| 262 | LOG(ERROR) << "Failed to flush and close oat file " << oat_filename |
| 263 | << " for " << oat_location; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 267 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 268 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 269 | // TODO: heap validation can't handle these fix up passes. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 270 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 271 | Runtime::Current()->GetHeap()->DisableObjectValidation(); |
| 272 | CopyAndFixupObjects(); |
| 273 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 274 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 275 | for (size_t i = 0; i < image_filenames.size(); ++i) { |
| 276 | const char* image_filename = image_filenames[i]; |
| 277 | const char* oat_filename = oat_filenames[i]; |
| 278 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 279 | std::unique_ptr<File> image_file; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 280 | if (image_fd != kInvalidFd) { |
| 281 | if (strlen(image_filename) == 0u) { |
| 282 | image_file.reset(new File(image_fd, unix_file::kCheckSafeUsage)); |
Mathieu Chartier | 784bb09 | 2016-01-28 12:02:00 -0800 | [diff] [blame] | 283 | // Empty the file in case it already exists. |
| 284 | if (image_file != nullptr) { |
| 285 | TEMP_FAILURE_RETRY(image_file->SetLength(0)); |
| 286 | TEMP_FAILURE_RETRY(image_file->Flush()); |
| 287 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 288 | } else { |
| 289 | LOG(ERROR) << "image fd " << image_fd << " name " << image_filename; |
| 290 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 291 | } else { |
| 292 | image_file.reset(OS::CreateEmptyFile(image_filename)); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 293 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 294 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 295 | if (image_file == nullptr) { |
| 296 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 297 | return false; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 298 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 299 | |
| 300 | if (!compile_app_image_ && fchmod(image_file->Fd(), 0644) != 0) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 301 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
| 302 | image_file->Erase(); |
| 303 | return EXIT_FAILURE; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 304 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 305 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 306 | std::unique_ptr<char[]> compressed_data; |
| 307 | // Image data size excludes the bitmap and the header. |
| 308 | ImageHeader* const image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
| 309 | const size_t image_data_size = image_header->GetImageSize() - sizeof(ImageHeader); |
| 310 | char* image_data = reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader); |
| 311 | size_t data_size; |
| 312 | const char* image_data_to_write; |
Mathieu Chartier | 167e638 | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 313 | const uint64_t compress_start_time = NanoTime(); |
Nicolas Geoffray | 83d4d72 | 2015-12-10 08:26:32 +0000 | [diff] [blame] | 314 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 315 | CHECK_EQ(image_header->storage_mode_, image_storage_mode_); |
| 316 | switch (image_storage_mode_) { |
| 317 | case ImageHeader::kStorageModeLZ4: { |
Mathieu Chartier | 167e638 | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 318 | const size_t compressed_max_size = LZ4_compressBound(image_data_size); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 319 | compressed_data.reset(new char[compressed_max_size]); |
| 320 | data_size = LZ4_compress( |
| 321 | reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader), |
| 322 | &compressed_data[0], |
| 323 | image_data_size); |
Mathieu Chartier | 167e638 | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 324 | |
| 325 | break; |
| 326 | } |
| 327 | case ImageHeader::kStorageModeLZ4HC: { |
| 328 | // Bound is same as non HC. |
| 329 | const size_t compressed_max_size = LZ4_compressBound(image_data_size); |
| 330 | compressed_data.reset(new char[compressed_max_size]); |
| 331 | data_size = LZ4_compressHC( |
| 332 | reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader), |
| 333 | &compressed_data[0], |
| 334 | image_data_size); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | case ImageHeader::kStorageModeUncompressed: { |
| 338 | data_size = image_data_size; |
| 339 | image_data_to_write = image_data; |
| 340 | break; |
| 341 | } |
| 342 | default: { |
| 343 | LOG(FATAL) << "Unsupported"; |
| 344 | UNREACHABLE(); |
| 345 | } |
| 346 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 347 | |
Mathieu Chartier | 167e638 | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 348 | if (compressed_data != nullptr) { |
| 349 | image_data_to_write = &compressed_data[0]; |
| 350 | VLOG(compiler) << "Compressed from " << image_data_size << " to " << data_size << " in " |
| 351 | << PrettyDuration(NanoTime() - compress_start_time); |
| 352 | } |
| 353 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 354 | // Write header first, as uncompressed. |
| 355 | image_header->data_size_ = data_size; |
| 356 | if (!image_file->WriteFully(image_info.image_->Begin(), sizeof(ImageHeader))) { |
| 357 | PLOG(ERROR) << "Failed to write image file header " << image_filename; |
| 358 | image_file->Erase(); |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | // Write out the image + fields + methods. |
| 363 | const bool is_compressed = compressed_data != nullptr; |
| 364 | if (!image_file->WriteFully(image_data_to_write, data_size)) { |
| 365 | PLOG(ERROR) << "Failed to write image file data " << image_filename; |
| 366 | image_file->Erase(); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | // Write out the image bitmap at the page aligned start of the image end, also uncompressed for |
| 371 | // convenience. |
| 372 | const ImageSection& bitmap_section = image_header->GetImageSection( |
| 373 | ImageHeader::kSectionImageBitmap); |
| 374 | // Align up since data size may be unaligned if the image is compressed. |
| 375 | size_t bitmap_position_in_file = RoundUp(sizeof(ImageHeader) + data_size, kPageSize); |
| 376 | if (!is_compressed) { |
| 377 | CHECK_EQ(bitmap_position_in_file, bitmap_section.Offset()); |
| 378 | } |
| 379 | if (!image_file->Write(reinterpret_cast<char*>(image_info.image_bitmap_->Begin()), |
| 380 | bitmap_section.Size(), |
| 381 | bitmap_position_in_file)) { |
| 382 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 383 | image_file->Erase(); |
| 384 | return false; |
| 385 | } |
| 386 | CHECK_EQ(bitmap_position_in_file + bitmap_section.Size(), |
| 387 | static_cast<size_t>(image_file->GetLength())); |
| 388 | if (image_file->FlushCloseOrErase() != 0) { |
| 389 | PLOG(ERROR) << "Failed to flush and close image file " << image_filename; |
| 390 | return false; |
| 391 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 392 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 393 | return true; |
| 394 | } |
| 395 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 396 | void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 397 | DCHECK(object != nullptr); |
| 398 | DCHECK_NE(offset, 0U); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 399 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 400 | // The object is already deflated from when we set the bin slot. Just overwrite the lock word. |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 401 | object->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 402 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 403 | DCHECK(IsImageOffsetAssigned(object)); |
| 404 | } |
| 405 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 406 | void ImageWriter::UpdateImageOffset(mirror::Object* obj, uintptr_t offset) { |
| 407 | DCHECK(IsImageOffsetAssigned(obj)) << obj << " " << offset; |
| 408 | obj->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
| 409 | DCHECK_EQ(obj->GetLockWord(false).ReadBarrierState(), 0u); |
| 410 | } |
| 411 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 412 | void ImageWriter::AssignImageOffset(mirror::Object* object, ImageWriter::BinSlot bin_slot) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 413 | DCHECK(object != nullptr); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 414 | DCHECK_NE(image_objects_offset_begin_, 0u); |
| 415 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 416 | const char* oat_filename = GetOatFilename(object); |
| 417 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 418 | size_t bin_slot_offset = image_info.bin_slot_offsets_[bin_slot.GetBin()]; |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 419 | size_t new_offset = bin_slot_offset + bin_slot.GetIndex(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 420 | DCHECK_ALIGNED(new_offset, kObjectAlignment); |
| 421 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 422 | SetImageOffset(object, new_offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 423 | DCHECK_LT(new_offset, image_info.image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 426 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 427 | // Will also return true if the bin slot was assigned since we are reusing the lock word. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 428 | DCHECK(object != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 429 | return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 432 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 433 | DCHECK(object != nullptr); |
| 434 | DCHECK(IsImageOffsetAssigned(object)); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 435 | LockWord lock_word = object->GetLockWord(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 436 | size_t offset = lock_word.ForwardingAddress(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 437 | const char* oat_filename = GetOatFilename(object); |
| 438 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 439 | DCHECK_LT(offset, image_info.image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 440 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 443 | void ImageWriter::SetImageBinSlot(mirror::Object* object, BinSlot bin_slot) { |
| 444 | DCHECK(object != nullptr); |
| 445 | DCHECK(!IsImageOffsetAssigned(object)); |
| 446 | DCHECK(!IsImageBinSlotAssigned(object)); |
| 447 | |
| 448 | // Before we stomp over the lock word, save the hash code for later. |
| 449 | Monitor::Deflate(Thread::Current(), object);; |
| 450 | LockWord lw(object->GetLockWord(false)); |
| 451 | switch (lw.GetState()) { |
| 452 | case LockWord::kFatLocked: { |
| 453 | LOG(FATAL) << "Fat locked object " << object << " found during object copy"; |
| 454 | break; |
| 455 | } |
| 456 | case LockWord::kThinLocked: { |
| 457 | LOG(FATAL) << "Thin locked object " << object << " found during object copy"; |
| 458 | break; |
| 459 | } |
| 460 | case LockWord::kUnlocked: |
| 461 | // No hash, don't need to save it. |
| 462 | break; |
| 463 | case LockWord::kHashCode: |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 464 | DCHECK(saved_hashcode_map_.find(object) == saved_hashcode_map_.end()); |
| 465 | saved_hashcode_map_.emplace(object, lw.GetHashCode()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 466 | break; |
| 467 | default: |
| 468 | LOG(FATAL) << "Unreachable."; |
| 469 | UNREACHABLE(); |
| 470 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 471 | object->SetLockWord(LockWord::FromForwardingAddress(bin_slot.Uint32Value()), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 472 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 473 | DCHECK(IsImageBinSlotAssigned(object)); |
| 474 | } |
| 475 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 476 | void ImageWriter::PrepareDexCacheArraySlots() { |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 477 | // Prepare dex cache array starts based on the ordering specified in the CompilerDriver. |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 478 | // Set the slot size early to avoid DCHECK() failures in IsImageBinSlotAssigned() |
| 479 | // when AssignImageBinSlot() assigns their indexes out or order. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 480 | for (const DexFile* dex_file : compiler_driver_.GetDexFilesForOatFile()) { |
| 481 | auto it = dex_file_oat_filename_map_.find(dex_file); |
| 482 | DCHECK(it != dex_file_oat_filename_map_.end()) << dex_file->GetLocation(); |
| 483 | ImageInfo& image_info = GetImageInfo(it->second); |
| 484 | image_info.dex_cache_array_starts_.Put(dex_file, image_info.bin_slot_sizes_[kBinDexCacheArray]); |
| 485 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
| 486 | image_info.bin_slot_sizes_[kBinDexCacheArray] += layout.Size(); |
| 487 | } |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 488 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 489 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 490 | Thread* const self = Thread::Current(); |
| 491 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 492 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 493 | mirror::DexCache* dex_cache = |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 494 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 495 | if (dex_cache == nullptr || IsInBootImage(dex_cache)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 496 | continue; |
| 497 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 498 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 499 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 500 | DCHECK(layout.Valid()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 501 | const char* oat_filename = GetOatFilenameForDexCache(dex_cache); |
| 502 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 503 | uint32_t start = image_info.dex_cache_array_starts_.Get(dex_file); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 504 | DCHECK_EQ(dex_file->NumTypeIds() != 0u, dex_cache->GetResolvedTypes() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 505 | AddDexCacheArrayRelocation(dex_cache->GetResolvedTypes(), |
| 506 | start + layout.TypesOffset(), |
| 507 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 508 | DCHECK_EQ(dex_file->NumMethodIds() != 0u, dex_cache->GetResolvedMethods() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 509 | AddDexCacheArrayRelocation(dex_cache->GetResolvedMethods(), |
| 510 | start + layout.MethodsOffset(), |
| 511 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 512 | DCHECK_EQ(dex_file->NumFieldIds() != 0u, dex_cache->GetResolvedFields() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 513 | AddDexCacheArrayRelocation(dex_cache->GetResolvedFields(), |
| 514 | start + layout.FieldsOffset(), |
| 515 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 516 | DCHECK_EQ(dex_file->NumStringIds() != 0u, dex_cache->GetStrings() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 517 | AddDexCacheArrayRelocation(dex_cache->GetStrings(), start + layout.StringsOffset(), dex_cache); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 518 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 521 | void ImageWriter::AddDexCacheArrayRelocation(void* array, size_t offset, DexCache* dex_cache) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 522 | if (array != nullptr) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 523 | DCHECK(!IsInBootImage(array)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 524 | const char* oat_filename = GetOatFilenameForDexCache(dex_cache); |
| 525 | native_object_relocations_.emplace(array, |
| 526 | NativeObjectRelocation { oat_filename, offset, kNativeObjectRelocationTypeDexCacheArray }); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 530 | void ImageWriter::AddMethodPointerArray(mirror::PointerArray* arr) { |
| 531 | DCHECK(arr != nullptr); |
| 532 | if (kIsDebugBuild) { |
| 533 | for (size_t i = 0, len = arr->GetLength(); i < len; i++) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 534 | ArtMethod* method = arr->GetElementPtrSize<ArtMethod*>(i, target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 535 | if (method != nullptr && !method->IsRuntimeMethod()) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 536 | mirror::Class* klass = method->GetDeclaringClass(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 537 | CHECK(klass == nullptr || KeepClass(klass)) |
| 538 | << PrettyClass(klass) << " should be a kept class"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | } |
| 542 | // kBinArtMethodClean picked arbitrarily, just required to differentiate between ArtFields and |
| 543 | // ArtMethods. |
| 544 | pointer_arrays_.emplace(arr, kBinArtMethodClean); |
| 545 | } |
| 546 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 547 | void ImageWriter::AssignImageBinSlot(mirror::Object* object) { |
| 548 | DCHECK(object != nullptr); |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 549 | size_t object_size = object->SizeOf(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 550 | |
| 551 | // The magic happens here. We segregate objects into different bins based |
| 552 | // on how likely they are to get dirty at runtime. |
| 553 | // |
| 554 | // Likely-to-dirty objects get packed together into the same bin so that |
| 555 | // at runtime their page dirtiness ratio (how many dirty objects a page has) is |
| 556 | // maximized. |
| 557 | // |
| 558 | // This means more pages will stay either clean or shared dirty (with zygote) and |
| 559 | // the app will use less of its own (private) memory. |
| 560 | Bin bin = kBinRegular; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 561 | size_t current_offset = 0u; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 562 | |
| 563 | if (kBinObjects) { |
| 564 | // |
| 565 | // Changing the bin of an object is purely a memory-use tuning. |
| 566 | // It has no change on runtime correctness. |
| 567 | // |
| 568 | // Memory analysis has determined that the following types of objects get dirtied |
| 569 | // the most: |
| 570 | // |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 571 | // * Dex cache arrays are stored in a special bin. The arrays for each dex cache have |
| 572 | // a fixed layout which helps improve generated code (using PC-relative addressing), |
| 573 | // so we pre-calculate their offsets separately in PrepareDexCacheArraySlots(). |
| 574 | // Since these arrays are huge, most pages do not overlap other objects and it's not |
| 575 | // really important where they are for the clean/dirty separation. Due to their |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 576 | // special PC-relative addressing, we arbitrarily keep them at the end. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 577 | // * Class'es which are verified [their clinit runs only at runtime] |
| 578 | // - classes in general [because their static fields get overwritten] |
| 579 | // - initialized classes with all-final statics are unlikely to be ever dirty, |
| 580 | // so bin them separately |
| 581 | // * Art Methods that are: |
| 582 | // - native [their native entry point is not looked up until runtime] |
| 583 | // - have declaring classes that aren't initialized |
| 584 | // [their interpreter/quick entry points are trampolines until the class |
| 585 | // becomes initialized] |
| 586 | // |
| 587 | // We also assume the following objects get dirtied either never or extremely rarely: |
| 588 | // * Strings (they are immutable) |
| 589 | // * Art methods that aren't native and have initialized declared classes |
| 590 | // |
| 591 | // We assume that "regular" bin objects are highly unlikely to become dirtied, |
| 592 | // so packing them together will not result in a noticeably tighter dirty-to-clean ratio. |
| 593 | // |
| 594 | if (object->IsClass()) { |
| 595 | bin = kBinClassVerified; |
| 596 | mirror::Class* klass = object->AsClass(); |
| 597 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 598 | // Add non-embedded vtable to the pointer array table if there is one. |
| 599 | auto* vtable = klass->GetVTable(); |
| 600 | if (vtable != nullptr) { |
| 601 | AddMethodPointerArray(vtable); |
| 602 | } |
| 603 | auto* iftable = klass->GetIfTable(); |
| 604 | if (iftable != nullptr) { |
| 605 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 606 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 607 | AddMethodPointerArray(iftable->GetMethodArray(i)); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 612 | if (klass->GetStatus() == Class::kStatusInitialized) { |
| 613 | bin = kBinClassInitialized; |
| 614 | |
| 615 | // If the class's static fields are all final, put it into a separate bin |
| 616 | // since it's very likely it will stay clean. |
| 617 | uint32_t num_static_fields = klass->NumStaticFields(); |
| 618 | if (num_static_fields == 0) { |
| 619 | bin = kBinClassInitializedFinalStatics; |
| 620 | } else { |
| 621 | // Maybe all the statics are final? |
| 622 | bool all_final = true; |
| 623 | for (uint32_t i = 0; i < num_static_fields; ++i) { |
| 624 | ArtField* field = klass->GetStaticField(i); |
| 625 | if (!field->IsFinal()) { |
| 626 | all_final = false; |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | if (all_final) { |
| 632 | bin = kBinClassInitializedFinalStatics; |
| 633 | } |
| 634 | } |
| 635 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 636 | } else if (object->GetClass<kVerifyNone>()->IsStringClass()) { |
| 637 | bin = kBinString; // Strings are almost always immutable (except for object header). |
| 638 | } // else bin = kBinRegular |
| 639 | } |
| 640 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 641 | const char* oat_filename = GetOatFilename(object); |
| 642 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 643 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 644 | size_t offset_delta = RoundUp(object_size, kObjectAlignment); // 64-bit alignment |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 645 | current_offset = image_info.bin_slot_sizes_[bin]; // How many bytes the current bin is at (aligned). |
| 646 | // Move the current bin size up to accommodate the object we just assigned a bin slot. |
| 647 | image_info.bin_slot_sizes_[bin] += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 648 | |
| 649 | BinSlot new_bin_slot(bin, current_offset); |
| 650 | SetImageBinSlot(object, new_bin_slot); |
| 651 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 652 | ++image_info.bin_slot_count_[bin]; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 653 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 654 | // Grow the image closer to the end by the object we just assigned. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 655 | image_info.image_end_ += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 658 | bool ImageWriter::WillMethodBeDirty(ArtMethod* m) const { |
| 659 | if (m->IsNative()) { |
| 660 | return true; |
| 661 | } |
| 662 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 663 | // Initialized is highly unlikely to dirty since there's no entry points to mutate. |
| 664 | return declaring_class == nullptr || declaring_class->GetStatus() != Class::kStatusInitialized; |
| 665 | } |
| 666 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 667 | bool ImageWriter::IsImageBinSlotAssigned(mirror::Object* object) const { |
| 668 | DCHECK(object != nullptr); |
| 669 | |
| 670 | // We always stash the bin slot into a lockword, in the 'forwarding address' state. |
| 671 | // If it's in some other state, then we haven't yet assigned an image bin slot. |
| 672 | if (object->GetLockWord(false).GetState() != LockWord::kForwardingAddress) { |
| 673 | return false; |
| 674 | } else if (kIsDebugBuild) { |
| 675 | LockWord lock_word = object->GetLockWord(false); |
| 676 | size_t offset = lock_word.ForwardingAddress(); |
| 677 | BinSlot bin_slot(offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 678 | const char* oat_filename = GetOatFilename(object); |
| 679 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 680 | DCHECK_LT(bin_slot.GetIndex(), image_info.bin_slot_sizes_[bin_slot.GetBin()]) |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 681 | << "bin slot offset should not exceed the size of that bin"; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | ImageWriter::BinSlot ImageWriter::GetImageBinSlot(mirror::Object* object) const { |
| 687 | DCHECK(object != nullptr); |
| 688 | DCHECK(IsImageBinSlotAssigned(object)); |
| 689 | |
| 690 | LockWord lock_word = object->GetLockWord(false); |
| 691 | size_t offset = lock_word.ForwardingAddress(); // TODO: ForwardingAddress should be uint32_t |
| 692 | DCHECK_LE(offset, std::numeric_limits<uint32_t>::max()); |
| 693 | |
| 694 | BinSlot bin_slot(static_cast<uint32_t>(offset)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 695 | const char* oat_filename = GetOatFilename(object); |
| 696 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 697 | DCHECK_LT(bin_slot.GetIndex(), image_info.bin_slot_sizes_[bin_slot.GetBin()]); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 698 | |
| 699 | return bin_slot; |
| 700 | } |
| 701 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 702 | bool ImageWriter::AllocMemory() { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 703 | for (const char* oat_filename : oat_filenames_) { |
| 704 | ImageInfo& image_info = GetImageInfo(oat_filename); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 705 | ImageSection unused_sections[ImageHeader::kSectionCount]; |
| 706 | const size_t length = RoundUp( |
| 707 | image_info.CreateImageSections(target_ptr_size_, unused_sections), |
| 708 | kPageSize); |
| 709 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 710 | std::string error_msg; |
| 711 | image_info.image_.reset(MemMap::MapAnonymous("image writer image", |
| 712 | nullptr, |
| 713 | length, |
| 714 | PROT_READ | PROT_WRITE, |
| 715 | false, |
| 716 | false, |
| 717 | &error_msg)); |
| 718 | if (UNLIKELY(image_info.image_.get() == nullptr)) { |
| 719 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
| 720 | return false; |
| 721 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 722 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 723 | // Create the image bitmap, only needs to cover mirror object section which is up to image_end_. |
| 724 | CHECK_LE(image_info.image_end_, length); |
| 725 | image_info.image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create( |
| 726 | "image bitmap", image_info.image_->Begin(), RoundUp(image_info.image_end_, kPageSize))); |
| 727 | if (image_info.image_bitmap_.get() == nullptr) { |
| 728 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 729 | return false; |
| 730 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 731 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 732 | return true; |
| 733 | } |
| 734 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 735 | class ComputeLazyFieldsForClassesVisitor : public ClassVisitor { |
| 736 | public: |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 737 | bool operator()(Class* c) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 738 | StackHandleScope<1> hs(Thread::Current()); |
| 739 | mirror::Class::ComputeName(hs.NewHandle(c)); |
| 740 | return true; |
| 741 | } |
| 742 | }; |
| 743 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 744 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 745 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 746 | ComputeLazyFieldsForClassesVisitor visitor; |
| 747 | class_linker->VisitClassesWithoutClassesLock(&visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 750 | static bool IsBootClassLoaderClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 751 | return klass->GetClassLoader() == nullptr; |
| 752 | } |
| 753 | |
| 754 | bool ImageWriter::IsBootClassLoaderNonImageClass(mirror::Class* klass) { |
| 755 | return IsBootClassLoaderClass(klass) && !IsInBootImage(klass); |
| 756 | } |
| 757 | |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 758 | bool ImageWriter::PruneAppImageClass(mirror::Class* klass) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 759 | bool early_exit = false; |
| 760 | std::unordered_set<mirror::Class*> visited; |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 761 | return PruneAppImageClassInternal(klass, &early_exit, &visited); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 762 | } |
| 763 | |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 764 | bool ImageWriter::PruneAppImageClassInternal( |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 765 | mirror::Class* klass, |
| 766 | bool* early_exit, |
| 767 | std::unordered_set<mirror::Class*>* visited) { |
| 768 | DCHECK(early_exit != nullptr); |
| 769 | DCHECK(visited != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 770 | DCHECK(compile_app_image_); |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 771 | if (klass == nullptr || IsInBootImage(klass)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 772 | return false; |
| 773 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 774 | auto found = prune_class_memo_.find(klass); |
| 775 | if (found != prune_class_memo_.end()) { |
| 776 | // Already computed, return the found value. |
| 777 | return found->second; |
| 778 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 779 | // Circular dependencies, return false but do not store the result in the memoization table. |
| 780 | if (visited->find(klass) != visited->end()) { |
| 781 | *early_exit = true; |
| 782 | return false; |
| 783 | } |
| 784 | visited->emplace(klass); |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 785 | bool result = IsBootClassLoaderClass(klass); |
| 786 | std::string temp; |
| 787 | // Prune if not an image class, this handles any broken sets of image classes such as having a |
| 788 | // class in the set but not it's superclass. |
| 789 | result = result || !compiler_driver_.IsImageClass(klass->GetDescriptor(&temp)); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 790 | bool my_early_exit = false; // Only for ourselves, ignore caller. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 791 | // Remove classes that failed to verify since we don't want to have java.lang.VerifyError in the |
| 792 | // app image. |
| 793 | if (klass->GetStatus() == mirror::Class::kStatusError) { |
| 794 | result = true; |
| 795 | } else { |
| 796 | CHECK(klass->GetVerifyError() == nullptr) << PrettyClass(klass); |
| 797 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 798 | if (!result) { |
| 799 | // Check interfaces since these wont be visited through VisitReferences.) |
| 800 | mirror::IfTable* if_table = klass->GetIfTable(); |
| 801 | for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) { |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 802 | result = result || PruneAppImageClassInternal(if_table->GetInterface(i), |
| 803 | &my_early_exit, |
| 804 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 805 | } |
| 806 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 807 | if (klass->IsObjectArrayClass()) { |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 808 | result = result || PruneAppImageClassInternal(klass->GetComponentType(), |
| 809 | &my_early_exit, |
| 810 | visited); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 811 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 812 | // Check static fields and their classes. |
| 813 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
| 814 | if (num_static_fields != 0 && klass->IsResolved()) { |
| 815 | // Presumably GC can happen when we are cross compiling, it should not cause performance |
| 816 | // problems to do pointer size logic. |
| 817 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset( |
| 818 | Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 819 | for (size_t i = 0u; i < num_static_fields; ++i) { |
| 820 | mirror::Object* ref = klass->GetFieldObject<mirror::Object>(field_offset); |
| 821 | if (ref != nullptr) { |
| 822 | if (ref->IsClass()) { |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 823 | result = result || PruneAppImageClassInternal(ref->AsClass(), |
| 824 | &my_early_exit, |
| 825 | visited); |
| 826 | } else { |
| 827 | result = result || PruneAppImageClassInternal(ref->GetClass(), |
| 828 | &my_early_exit, |
| 829 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 830 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 831 | } |
| 832 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 833 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 834 | } |
| 835 | } |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 836 | result = result || PruneAppImageClassInternal(klass->GetSuperClass(), |
| 837 | &my_early_exit, |
| 838 | visited); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 839 | // Erase the element we stored earlier since we are exiting the function. |
| 840 | auto it = visited->find(klass); |
| 841 | DCHECK(it != visited->end()); |
| 842 | visited->erase(it); |
| 843 | // Only store result if it is true or none of the calls early exited due to circular |
| 844 | // dependencies. If visited is empty then we are the root caller, in this case the cycle was in |
| 845 | // a child call and we can remember the result. |
| 846 | if (result == true || !my_early_exit || visited->empty()) { |
| 847 | prune_class_memo_[klass] = result; |
| 848 | } |
| 849 | *early_exit |= my_early_exit; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 850 | return result; |
| 851 | } |
| 852 | |
| 853 | bool ImageWriter::KeepClass(Class* klass) { |
| 854 | if (klass == nullptr) { |
| 855 | return false; |
| 856 | } |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 857 | if (compile_app_image_ && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 858 | // Already in boot image, return true. |
| 859 | return true; |
| 860 | } |
| 861 | std::string temp; |
| 862 | if (!compiler_driver_.IsImageClass(klass->GetDescriptor(&temp))) { |
| 863 | return false; |
| 864 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 865 | if (compile_app_image_) { |
| 866 | // For app images, we need to prune boot loader classes that are not in the boot image since |
| 867 | // these may have already been loaded when the app image is loaded. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 868 | // Keep classes in the boot image space since we don't want to re-resolve these. |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 869 | return !PruneAppImageClass(klass); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 870 | } |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 871 | return true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 874 | class NonImageClassesVisitor : public ClassVisitor { |
| 875 | public: |
| 876 | explicit NonImageClassesVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
| 877 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 878 | bool operator()(Class* klass) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 879 | if (!image_writer_->KeepClass(klass)) { |
| 880 | classes_to_prune_.insert(klass); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 881 | } |
| 882 | return true; |
| 883 | } |
| 884 | |
Mathieu Chartier | 9b1c9b7 | 2016-02-02 10:09:58 -0800 | [diff] [blame] | 885 | std::unordered_set<mirror::Class*> classes_to_prune_; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 886 | ImageWriter* const image_writer_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 887 | }; |
| 888 | |
| 889 | void ImageWriter::PruneNonImageClasses() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 890 | Runtime* runtime = Runtime::Current(); |
| 891 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 892 | Thread* self = Thread::Current(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 893 | |
| 894 | // Make a list of classes we would like to prune. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 895 | NonImageClassesVisitor visitor(this); |
| 896 | class_linker->VisitClasses(&visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 897 | |
| 898 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 899 | VLOG(compiler) << "Pruning " << visitor.classes_to_prune_.size() << " classes"; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 900 | for (mirror::Class* klass : visitor.classes_to_prune_) { |
| 901 | std::string temp; |
| 902 | const char* name = klass->GetDescriptor(&temp); |
| 903 | VLOG(compiler) << "Pruning class " << name; |
| 904 | if (!compile_app_image_) { |
| 905 | DCHECK(IsBootClassLoaderClass(klass)); |
| 906 | } |
| 907 | bool result = class_linker->RemoveClass(name, klass->GetClassLoader()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 908 | DCHECK(result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | // Clear references to removed classes from the DexCaches. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 912 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 913 | |
| 914 | ScopedAssertNoThreadSuspension sa(self, __FUNCTION__); |
| 915 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); // For ClassInClassTable |
| 916 | ReaderMutexLock mu2(self, *class_linker->DexLock()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 917 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 918 | if (self->IsJWeakCleared(data.weak_root)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 919 | continue; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 920 | } |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 921 | mirror::DexCache* dex_cache = self->DecodeJObject(data.weak_root)->AsDexCache(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 922 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 923 | Class* klass = dex_cache->GetResolvedType(i); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 924 | if (klass != nullptr && !KeepClass(klass)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 925 | dex_cache->SetResolvedType(i, nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 928 | ArtMethod** resolved_methods = dex_cache->GetResolvedMethods(); |
| 929 | for (size_t i = 0, num = dex_cache->NumResolvedMethods(); i != num; ++i) { |
| 930 | ArtMethod* method = |
| 931 | mirror::DexCache::GetElementPtrSize(resolved_methods, i, target_ptr_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 932 | DCHECK(method != nullptr) << "Expected resolution method instead of null method"; |
| 933 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Alex Light | d07e1b3 | 2016-02-17 11:59:05 -0800 | [diff] [blame] | 934 | // Copied methods may be held live by a class which was not an image class but have a |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 935 | // declaring class which is an image class. Set it to the resolution method to be safe and |
| 936 | // prevent dangling pointers. |
Alex Light | 7ead0c0 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 937 | if (method->IsCopied() || !KeepClass(declaring_class)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 938 | mirror::DexCache::SetElementPtrSize(resolved_methods, |
| 939 | i, |
| 940 | resolution_method, |
| 941 | target_ptr_size_); |
| 942 | } else { |
| 943 | // Check that the class is still in the classes table. |
| 944 | DCHECK(class_linker->ClassInClassTable(declaring_class)) << "Class " |
| 945 | << PrettyClass(declaring_class) << " not in class linker table"; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 946 | } |
| 947 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 948 | ArtField** resolved_fields = dex_cache->GetResolvedFields(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 949 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 950 | ArtField* field = mirror::DexCache::GetElementPtrSize(resolved_fields, i, target_ptr_size_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 951 | if (field != nullptr && !KeepClass(field->GetDeclaringClass())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 952 | dex_cache->SetResolvedField(i, nullptr, target_ptr_size_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 955 | // Clean the dex field. It might have been populated during the initialization phase, but |
| 956 | // contains data only valid during a real run. |
| 957 | dex_cache->SetFieldObject<false>(mirror::DexCache::DexOffset(), nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 958 | } |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 959 | |
| 960 | // Drop the array class cache in the ClassLinker, as these are roots holding those classes live. |
| 961 | class_linker->DropFindArrayClassCache(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 962 | |
| 963 | // Clear to save RAM. |
| 964 | prune_class_memo_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 967 | void ImageWriter::CheckNonImageClassesRemoved() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 968 | if (compiler_driver_.GetImageClasses() != nullptr) { |
| 969 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 970 | heap->VisitObjects(CheckNonImageClassesRemovedCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 971 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 975 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 976 | if (obj->IsClass() && !image_writer->IsInBootImage(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 977 | Class* klass = obj->AsClass(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 978 | if (!image_writer->KeepClass(klass)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 979 | image_writer->DumpImageClasses(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 980 | std::string temp; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 981 | CHECK(image_writer->KeepClass(klass)) << klass->GetDescriptor(&temp) |
| 982 | << " " << PrettyDescriptor(klass); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 983 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
| 987 | void ImageWriter::DumpImageClasses() { |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 988 | auto image_classes = compiler_driver_.GetImageClasses(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 989 | CHECK(image_classes != nullptr); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 990 | for (const std::string& image_class : *image_classes) { |
| 991 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 995 | mirror::String* ImageWriter::FindInternedString(mirror::String* string) { |
| 996 | Thread* const self = Thread::Current(); |
| 997 | for (auto& pair : image_info_map_) { |
| 998 | const ImageInfo& image_info = pair.second; |
| 999 | mirror::String* const found = image_info.intern_table_->LookupStrong(self, string); |
| 1000 | DCHECK(image_info.intern_table_->LookupWeak(self, string) == nullptr) |
| 1001 | << string->ToModifiedUtf8(); |
| 1002 | if (found != nullptr) { |
| 1003 | return found; |
| 1004 | } |
| 1005 | } |
| 1006 | if (compile_app_image_) { |
| 1007 | Runtime* const runtime = Runtime::Current(); |
| 1008 | mirror::String* found = runtime->GetInternTable()->LookupStrong(self, string); |
| 1009 | // If we found it in the runtime intern table it could either be in the boot image or interned |
| 1010 | // during app image compilation. If it was in the boot image return that, otherwise return null |
| 1011 | // since it belongs to another image space. |
| 1012 | if (found != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(found)) { |
| 1013 | return found; |
| 1014 | } |
| 1015 | DCHECK(runtime->GetInternTable()->LookupWeak(self, string) == nullptr) |
| 1016 | << string->ToModifiedUtf8(); |
| 1017 | } |
| 1018 | return nullptr; |
| 1019 | } |
| 1020 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1021 | void ImageWriter::CalculateObjectBinSlots(Object* obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1022 | DCHECK(obj != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1023 | // if it is a string, we want to intern it if its not interned. |
| 1024 | if (obj->GetClass()->IsStringClass()) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1025 | const char* oat_filename = GetOatFilename(obj); |
| 1026 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1027 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1028 | // we must be an interned string that was forward referenced and already assigned |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1029 | if (IsImageBinSlotAssigned(obj)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1030 | DCHECK_EQ(obj, FindInternedString(obj->AsString())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1031 | return; |
| 1032 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1033 | // Need to check if the string is already interned in another image info so that we don't have |
| 1034 | // the intern tables of two different images contain the same string. |
| 1035 | mirror::String* interned = FindInternedString(obj->AsString()); |
| 1036 | if (interned == nullptr) { |
| 1037 | // Not in another image space, insert to our table. |
| 1038 | interned = image_info.intern_table_->InternStrongImageString(obj->AsString()); |
| 1039 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1040 | if (obj != interned) { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1041 | if (!IsImageBinSlotAssigned(interned)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1042 | // interned obj is after us, allocate its location early |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1043 | AssignImageBinSlot(interned); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1044 | } |
| 1045 | // point those looking for this object to the interned version. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1046 | SetImageBinSlot(obj, GetImageBinSlot(interned)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1047 | return; |
| 1048 | } |
| 1049 | // else (obj == interned), nothing to do but fall through to the normal case |
| 1050 | } |
| 1051 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1052 | AssignImageBinSlot(obj); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1055 | ObjectArray<Object>* ImageWriter::CreateImageRoots(const char* oat_filename) const { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1056 | Runtime* runtime = Runtime::Current(); |
| 1057 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1058 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1059 | StackHandleScope<3> hs(self); |
| 1060 | Handle<Class> object_array_class(hs.NewHandle( |
| 1061 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1062 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1063 | std::unordered_set<const DexFile*> image_dex_files; |
| 1064 | for (auto& pair : dex_file_oat_filename_map_) { |
| 1065 | const DexFile* image_dex_file = pair.first; |
| 1066 | const char* image_oat_filename = pair.second; |
| 1067 | if (strcmp(oat_filename, image_oat_filename) == 0) { |
| 1068 | image_dex_files.insert(image_dex_file); |
| 1069 | } |
| 1070 | } |
| 1071 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1072 | // build an Object[] of all the DexCaches used in the source_space_. |
| 1073 | // Since we can't hold the dex lock when allocating the dex_caches |
| 1074 | // ObjectArray, we lock the dex lock twice, first to get the number |
| 1075 | // of dex caches first and then lock it again to copy the dex |
| 1076 | // caches. We check that the number of dex caches does not change. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1077 | size_t dex_cache_count = 0; |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1078 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1079 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1080 | // Count number of dex caches not in the boot image. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1081 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 1082 | mirror::DexCache* dex_cache = |
| 1083 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1084 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 1085 | if (!IsInBootImage(dex_cache)) { |
| 1086 | dex_cache_count += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; |
| 1087 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1088 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1089 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1090 | Handle<ObjectArray<Object>> dex_caches( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1091 | hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(), dex_cache_count))); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1092 | CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array."; |
| 1093 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1094 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1095 | size_t non_image_dex_caches = 0; |
| 1096 | // Re-count number of non image dex caches. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1097 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 1098 | mirror::DexCache* dex_cache = |
| 1099 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1100 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 1101 | if (!IsInBootImage(dex_cache)) { |
| 1102 | non_image_dex_caches += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; |
| 1103 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1104 | } |
| 1105 | CHECK_EQ(dex_cache_count, non_image_dex_caches) |
| 1106 | << "The number of non-image dex caches changed."; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1107 | size_t i = 0; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1108 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 1109 | mirror::DexCache* dex_cache = |
| 1110 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1111 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 1112 | if (!IsInBootImage(dex_cache) && image_dex_files.find(dex_file) != image_dex_files.end()) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1113 | dex_caches->Set<false>(i, dex_cache); |
| 1114 | ++i; |
| 1115 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1116 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | // build an Object[] of the roots needed to restore the runtime |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1120 | auto image_roots(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1121 | ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1122 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1123 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1124 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1125 | CHECK(image_roots->Get(i) != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1126 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1127 | return image_roots.Get(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1130 | // Walk instance fields of the given Class. Separate function to allow recursion on the super |
| 1131 | // class. |
| 1132 | void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) { |
| 1133 | // Visit fields of parent classes first. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1134 | StackHandleScope<1> hs(Thread::Current()); |
| 1135 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 1136 | mirror::Class* super = h_class->GetSuperClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1137 | if (super != nullptr) { |
| 1138 | WalkInstanceFields(obj, super); |
| 1139 | } |
| 1140 | // |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1141 | size_t num_reference_fields = h_class->NumReferenceInstanceFields(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 1142 | MemberOffset field_offset = h_class->GetFirstReferenceInstanceFieldOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1143 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1144 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1145 | if (value != nullptr) { |
| 1146 | WalkFieldsInOrder(value); |
| 1147 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 1148 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1149 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | // For an unvisited object, visit it then all its children found via fields. |
| 1154 | void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1155 | if (IsInBootImage(obj)) { |
| 1156 | // Object is in the image, don't need to fix it up. |
| 1157 | return; |
| 1158 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1159 | // Use our own visitor routine (instead of GC visitor) to get better locality between |
| 1160 | // an object and its fields |
| 1161 | if (!IsImageBinSlotAssigned(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1162 | // Walk instance fields of all objects |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1163 | StackHandleScope<2> hs(Thread::Current()); |
| 1164 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
| 1165 | Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1166 | // visit the object itself. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1167 | CalculateObjectBinSlots(h_obj.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1168 | WalkInstanceFields(h_obj.Get(), klass.Get()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1169 | // Walk static fields of a Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1170 | if (h_obj->IsClass()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1171 | size_t num_reference_static_fields = klass->NumReferenceStaticFields(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1172 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset(target_ptr_size_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1173 | for (size_t i = 0; i < num_reference_static_fields; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1174 | mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1175 | if (value != nullptr) { |
| 1176 | WalkFieldsInOrder(value); |
| 1177 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 1178 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1179 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1180 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1181 | // Visit and assign offsets for fields and field arrays. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1182 | auto* as_klass = h_obj->AsClass(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1183 | mirror::DexCache* dex_cache = as_klass->GetDexCache(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1184 | DCHECK_NE(klass->GetStatus(), mirror::Class::kStatusError); |
| 1185 | if (compile_app_image_) { |
| 1186 | // Extra sanity, no boot loader classes should be left! |
| 1187 | CHECK(!IsBootClassLoaderClass(as_klass)) << PrettyClass(as_klass); |
| 1188 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1189 | LengthPrefixedArray<ArtField>* fields[] = { |
| 1190 | as_klass->GetSFieldsPtr(), as_klass->GetIFieldsPtr(), |
| 1191 | }; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1192 | const char* oat_file = GetOatFilenameForDexCache(dex_cache); |
| 1193 | ImageInfo& image_info = GetImageInfo(oat_file); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1194 | { |
| 1195 | // Note: This table is only accessed from the image writer, so the lock is technically |
| 1196 | // unnecessary. |
| 1197 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1198 | // Insert in the class table for this iamge. |
| 1199 | image_info.class_table_->Insert(as_klass); |
| 1200 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1201 | for (LengthPrefixedArray<ArtField>* cur_fields : fields) { |
| 1202 | // Total array length including header. |
| 1203 | if (cur_fields != nullptr) { |
| 1204 | const size_t header_size = LengthPrefixedArray<ArtField>::ComputeSize(0); |
| 1205 | // Forward the entire array at once. |
| 1206 | auto it = native_object_relocations_.find(cur_fields); |
| 1207 | CHECK(it == native_object_relocations_.end()) << "Field array " << cur_fields |
| 1208 | << " already forwarded"; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1209 | size_t& offset = image_info.bin_slot_sizes_[kBinArtField]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1210 | DCHECK(!IsInBootImage(cur_fields)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1211 | native_object_relocations_.emplace(cur_fields, |
| 1212 | NativeObjectRelocation {oat_file, offset, kNativeObjectRelocationTypeArtFieldArray }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1213 | offset += header_size; |
| 1214 | // Forward individual fields so that we can quickly find where they belong. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1215 | for (size_t i = 0, count = cur_fields->size(); i < count; ++i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1216 | // Need to forward arrays separate of fields. |
| 1217 | ArtField* field = &cur_fields->At(i); |
| 1218 | auto it2 = native_object_relocations_.find(field); |
| 1219 | CHECK(it2 == native_object_relocations_.end()) << "Field at index=" << i |
| 1220 | << " already assigned " << PrettyField(field) << " static=" << field->IsStatic(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1221 | DCHECK(!IsInBootImage(field)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1222 | native_object_relocations_.emplace(field, |
| 1223 | NativeObjectRelocation {oat_file, offset, kNativeObjectRelocationTypeArtField }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1224 | offset += sizeof(ArtField); |
| 1225 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1226 | } |
| 1227 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1228 | // Visit and assign offsets for methods. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1229 | size_t num_methods = as_klass->NumMethods(); |
| 1230 | if (num_methods != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1231 | bool any_dirty = false; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1232 | for (auto& m : as_klass->GetMethods(target_ptr_size_)) { |
| 1233 | if (WillMethodBeDirty(&m)) { |
| 1234 | any_dirty = true; |
| 1235 | break; |
| 1236 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1237 | } |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1238 | NativeObjectRelocationType type = any_dirty |
| 1239 | ? kNativeObjectRelocationTypeArtMethodDirty |
| 1240 | : kNativeObjectRelocationTypeArtMethodClean; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1241 | Bin bin_type = BinTypeForNativeRelocationType(type); |
| 1242 | // Forward the entire array at once, but header first. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1243 | const size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
| 1244 | const size_t method_size = ArtMethod::Size(target_ptr_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1245 | const size_t header_size = LengthPrefixedArray<ArtMethod>::ComputeSize(0, |
| 1246 | method_size, |
| 1247 | method_alignment); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1248 | LengthPrefixedArray<ArtMethod>* array = as_klass->GetMethodsPtr(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1249 | auto it = native_object_relocations_.find(array); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1250 | CHECK(it == native_object_relocations_.end()) |
| 1251 | << "Method array " << array << " already forwarded"; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1252 | size_t& offset = image_info.bin_slot_sizes_[bin_type]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1253 | DCHECK(!IsInBootImage(array)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1254 | native_object_relocations_.emplace(array, |
| 1255 | NativeObjectRelocation { |
| 1256 | oat_file, |
| 1257 | offset, |
| 1258 | any_dirty ? kNativeObjectRelocationTypeArtMethodArrayDirty |
| 1259 | : kNativeObjectRelocationTypeArtMethodArrayClean }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1260 | offset += header_size; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1261 | for (auto& m : as_klass->GetMethods(target_ptr_size_)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1262 | AssignMethodOffset(&m, type, oat_file); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1263 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1264 | (any_dirty ? dirty_methods_ : clean_methods_) += num_methods; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1265 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1266 | } else if (h_obj->IsObjectArray()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1267 | // Walk elements of an object array. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1268 | int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1269 | for (int32_t i = 0; i < length; i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1270 | mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1271 | mirror::Object* value = obj_array->Get(i); |
| 1272 | if (value != nullptr) { |
| 1273 | WalkFieldsInOrder(value); |
| 1274 | } |
| 1275 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1276 | } else if (h_obj->IsClassLoader()) { |
| 1277 | // Register the class loader if it has a class table. |
| 1278 | // The fake boot class loader should not get registered and we should end up with only one |
| 1279 | // class loader. |
| 1280 | mirror::ClassLoader* class_loader = h_obj->AsClassLoader(); |
| 1281 | if (class_loader->GetClassTable() != nullptr) { |
| 1282 | class_loaders_.insert(class_loader); |
| 1283 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1288 | void ImageWriter::AssignMethodOffset(ArtMethod* method, |
| 1289 | NativeObjectRelocationType type, |
| 1290 | const char* oat_filename) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1291 | DCHECK(!IsInBootImage(method)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1292 | auto it = native_object_relocations_.find(method); |
| 1293 | CHECK(it == native_object_relocations_.end()) << "Method " << method << " already assigned " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1294 | << PrettyMethod(method); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1295 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1296 | size_t& offset = image_info.bin_slot_sizes_[BinTypeForNativeRelocationType(type)]; |
| 1297 | native_object_relocations_.emplace(method, NativeObjectRelocation { oat_filename, offset, type }); |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1298 | offset += ArtMethod::Size(target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1301 | void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) { |
| 1302 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 1303 | DCHECK(writer != nullptr); |
| 1304 | writer->WalkFieldsInOrder(obj); |
| 1305 | } |
| 1306 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1307 | void ImageWriter::UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg) { |
| 1308 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 1309 | DCHECK(writer != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1310 | if (!writer->IsInBootImage(obj)) { |
| 1311 | writer->UnbinObjectsIntoOffset(obj); |
| 1312 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | void ImageWriter::UnbinObjectsIntoOffset(mirror::Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1316 | DCHECK(!IsInBootImage(obj)); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1317 | CHECK(obj != nullptr); |
| 1318 | |
| 1319 | // We know the bin slot, and the total bin sizes for all objects by now, |
| 1320 | // so calculate the object's final image offset. |
| 1321 | |
| 1322 | DCHECK(IsImageBinSlotAssigned(obj)); |
| 1323 | BinSlot bin_slot = GetImageBinSlot(obj); |
| 1324 | // Change the lockword from a bin slot into an offset |
| 1325 | AssignImageOffset(obj, bin_slot); |
| 1326 | } |
| 1327 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1328 | void ImageWriter::CalculateNewObjectOffsets() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1329 | Thread* const self = Thread::Current(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1330 | StackHandleScopeCollection handles(self); |
| 1331 | std::vector<Handle<ObjectArray<Object>>> image_roots; |
| 1332 | for (const char* oat_filename : oat_filenames_) { |
| 1333 | std::string image_filename = oat_filename; |
| 1334 | image_roots.push_back(handles.NewHandle(CreateImageRoots(image_filename.c_str()))); |
| 1335 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1336 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1337 | auto* runtime = Runtime::Current(); |
| 1338 | auto* heap = runtime->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1339 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1340 | // Leave space for the header, but do not write it yet, we need to |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1341 | // know where image_roots is going to end up |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1342 | image_objects_offset_begin_ = RoundUp(sizeof(ImageHeader), kObjectAlignment); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1343 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1344 | // Clear any pre-existing monitors which may have been in the monitor words, assign bin slots. |
| 1345 | heap->VisitObjects(WalkFieldsCallback, this); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1346 | // Write the image runtime methods. |
| 1347 | image_methods_[ImageHeader::kResolutionMethod] = runtime->GetResolutionMethod(); |
| 1348 | image_methods_[ImageHeader::kImtConflictMethod] = runtime->GetImtConflictMethod(); |
| 1349 | image_methods_[ImageHeader::kImtUnimplementedMethod] = runtime->GetImtUnimplementedMethod(); |
| 1350 | image_methods_[ImageHeader::kCalleeSaveMethod] = runtime->GetCalleeSaveMethod(Runtime::kSaveAll); |
| 1351 | image_methods_[ImageHeader::kRefsOnlySaveMethod] = |
| 1352 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly); |
| 1353 | image_methods_[ImageHeader::kRefsAndArgsSaveMethod] = |
| 1354 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1355 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1356 | // Add room for fake length prefixed array for holding the image methods. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1357 | const auto image_method_type = kNativeObjectRelocationTypeArtMethodArrayClean; |
| 1358 | auto it = native_object_relocations_.find(&image_method_array_); |
| 1359 | CHECK(it == native_object_relocations_.end()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1360 | ImageInfo& default_image_info = GetImageInfo(default_oat_filename_); |
| 1361 | size_t& offset = |
| 1362 | default_image_info.bin_slot_sizes_[BinTypeForNativeRelocationType(image_method_type)]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1363 | if (!compile_app_image_) { |
| 1364 | native_object_relocations_.emplace(&image_method_array_, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1365 | NativeObjectRelocation { default_oat_filename_, offset, image_method_type }); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1366 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1367 | size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1368 | const size_t array_size = LengthPrefixedArray<ArtMethod>::ComputeSize( |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1369 | 0, ArtMethod::Size(target_ptr_size_), method_alignment); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1370 | CHECK_ALIGNED_PARAM(array_size, method_alignment); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1371 | offset += array_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1372 | for (auto* m : image_methods_) { |
| 1373 | CHECK(m != nullptr); |
| 1374 | CHECK(m->IsRuntimeMethod()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1375 | DCHECK_EQ(compile_app_image_, IsInBootImage(m)) << "Trampolines should be in boot image"; |
| 1376 | if (!IsInBootImage(m)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1377 | AssignMethodOffset(m, kNativeObjectRelocationTypeArtMethodClean, default_oat_filename_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1378 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1379 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1380 | // Calculate size of the dex cache arrays slot and prepare offsets. |
| 1381 | PrepareDexCacheArraySlots(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1382 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1383 | // Calculate the sizes of the intern tables and class tables. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1384 | for (const char* oat_filename : oat_filenames_) { |
| 1385 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1386 | // Calculate how big the intern table will be after being serialized. |
| 1387 | InternTable* const intern_table = image_info.intern_table_.get(); |
| 1388 | CHECK_EQ(intern_table->WeakSize(), 0u) << " should have strong interned all the strings"; |
| 1389 | image_info.intern_table_bytes_ = intern_table->WriteToMemory(nullptr); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1390 | // Calculate the size of the class table. |
| 1391 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1392 | image_info.class_table_bytes_ += image_info.class_table_->WriteToMemory(nullptr); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1393 | } |
| 1394 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1395 | // Calculate bin slot offsets. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1396 | for (const char* oat_filename : oat_filenames_) { |
| 1397 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1398 | size_t bin_offset = image_objects_offset_begin_; |
| 1399 | for (size_t i = 0; i != kBinSize; ++i) { |
| 1400 | image_info.bin_slot_offsets_[i] = bin_offset; |
| 1401 | bin_offset += image_info.bin_slot_sizes_[i]; |
| 1402 | if (i == kBinArtField) { |
| 1403 | static_assert(kBinArtField + 1 == kBinArtMethodClean, "Methods follow fields."); |
| 1404 | static_assert(alignof(ArtField) == 4u, "ArtField alignment is 4."); |
| 1405 | DCHECK_ALIGNED(bin_offset, 4u); |
| 1406 | DCHECK(method_alignment == 4u || method_alignment == 8u); |
| 1407 | bin_offset = RoundUp(bin_offset, method_alignment); |
| 1408 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1409 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1410 | // NOTE: There may be additional padding between the bin slots and the intern table. |
| 1411 | DCHECK_EQ(image_info.image_end_, |
| 1412 | GetBinSizeSum(image_info, kBinMirrorCount) + image_objects_offset_begin_); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1413 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1414 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1415 | // Calculate image offsets. |
| 1416 | size_t image_offset = 0; |
| 1417 | for (const char* oat_filename : oat_filenames_) { |
| 1418 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1419 | image_info.image_begin_ = global_image_begin_ + image_offset; |
| 1420 | image_info.image_offset_ = image_offset; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1421 | ImageSection unused_sections[ImageHeader::kSectionCount]; |
| 1422 | image_info.image_size_ = RoundUp( |
| 1423 | image_info.CreateImageSections(target_ptr_size_, unused_sections), |
| 1424 | kPageSize); |
| 1425 | // There should be no gaps until the next image. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1426 | image_offset += image_info.image_size_; |
| 1427 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1428 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1429 | // Transform each object's bin slot into an offset which will be used to do the final copy. |
| 1430 | heap->VisitObjects(UnbinObjectsIntoOffsetCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1431 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1432 | // DCHECK_EQ(image_end_, GetBinSizeSum(kBinMirrorCount) + image_objects_offset_begin_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1433 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1434 | size_t i = 0; |
| 1435 | for (const char* oat_filename : oat_filenames_) { |
| 1436 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1437 | image_info.image_roots_address_ = PointerToLowMemUInt32(GetImageAddress(image_roots[i].Get())); |
| 1438 | i++; |
| 1439 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1440 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1441 | // Update the native relocations by adding their bin sums. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1442 | for (auto& pair : native_object_relocations_) { |
| 1443 | NativeObjectRelocation& relocation = pair.second; |
| 1444 | Bin bin_type = BinTypeForNativeRelocationType(relocation.type); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1445 | ImageInfo& image_info = GetImageInfo(relocation.oat_filename); |
| 1446 | relocation.offset += image_info.bin_slot_offsets_[bin_type]; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1449 | // Note that image_info.image_end_ is left at end of used mirror object section. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1452 | size_t ImageWriter::ImageInfo::CreateImageSections(size_t target_ptr_size, |
| 1453 | ImageSection* out_sections) const { |
| 1454 | DCHECK(out_sections != nullptr); |
| 1455 | // Objects section |
| 1456 | auto* objects_section = &out_sections[ImageHeader::kSectionObjects]; |
| 1457 | *objects_section = ImageSection(0u, image_end_); |
| 1458 | size_t cur_pos = objects_section->End(); |
| 1459 | // Add field section. |
| 1460 | auto* field_section = &out_sections[ImageHeader::kSectionArtFields]; |
| 1461 | *field_section = ImageSection(cur_pos, bin_slot_sizes_[kBinArtField]); |
| 1462 | CHECK_EQ(bin_slot_offsets_[kBinArtField], field_section->Offset()); |
| 1463 | cur_pos = field_section->End(); |
| 1464 | // Round up to the alignment the required by the method section. |
| 1465 | cur_pos = RoundUp(cur_pos, ArtMethod::Alignment(target_ptr_size)); |
| 1466 | // Add method section. |
| 1467 | auto* methods_section = &out_sections[ImageHeader::kSectionArtMethods]; |
| 1468 | *methods_section = ImageSection(cur_pos, |
| 1469 | bin_slot_sizes_[kBinArtMethodClean] + |
| 1470 | bin_slot_sizes_[kBinArtMethodDirty]); |
| 1471 | CHECK_EQ(bin_slot_offsets_[kBinArtMethodClean], methods_section->Offset()); |
| 1472 | cur_pos = methods_section->End(); |
| 1473 | // Add dex cache arrays section. |
| 1474 | auto* dex_cache_arrays_section = &out_sections[ImageHeader::kSectionDexCacheArrays]; |
| 1475 | *dex_cache_arrays_section = ImageSection(cur_pos, bin_slot_sizes_[kBinDexCacheArray]); |
| 1476 | CHECK_EQ(bin_slot_offsets_[kBinDexCacheArray], dex_cache_arrays_section->Offset()); |
| 1477 | cur_pos = dex_cache_arrays_section->End(); |
| 1478 | // Round up to the alignment the string table expects. See HashSet::WriteToMemory. |
| 1479 | cur_pos = RoundUp(cur_pos, sizeof(uint64_t)); |
| 1480 | // Calculate the size of the interned strings. |
| 1481 | auto* interned_strings_section = &out_sections[ImageHeader::kSectionInternedStrings]; |
| 1482 | *interned_strings_section = ImageSection(cur_pos, intern_table_bytes_); |
| 1483 | cur_pos = interned_strings_section->End(); |
| 1484 | // Round up to the alignment the class table expects. See HashSet::WriteToMemory. |
| 1485 | cur_pos = RoundUp(cur_pos, sizeof(uint64_t)); |
| 1486 | // Calculate the size of the class table section. |
| 1487 | auto* class_table_section = &out_sections[ImageHeader::kSectionClassTable]; |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1488 | *class_table_section = ImageSection(cur_pos, class_table_bytes_); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1489 | cur_pos = class_table_section->End(); |
| 1490 | // Image end goes right before the start of the image bitmap. |
| 1491 | return cur_pos; |
| 1492 | } |
| 1493 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1494 | void ImageWriter::CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) { |
| 1495 | CHECK_NE(0U, oat_loaded_size); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1496 | const char* oat_filename = oat_file_->GetLocation().c_str(); |
| 1497 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1498 | const uint8_t* oat_file_begin = GetOatFileBegin(oat_filename); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1499 | const uint8_t* oat_file_end = oat_file_begin + oat_loaded_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1500 | image_info.oat_data_begin_ = const_cast<uint8_t*>(oat_file_begin) + oat_data_offset; |
| 1501 | const uint8_t* oat_data_end = image_info.oat_data_begin_ + oat_file_->Size(); |
| 1502 | image_info.oat_size_ = oat_file_->Size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1503 | |
| 1504 | // Create the image sections. |
| 1505 | ImageSection sections[ImageHeader::kSectionCount]; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1506 | const size_t image_end = image_info.CreateImageSections(target_ptr_size_, sections); |
| 1507 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1508 | // Finally bitmap section. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1509 | const size_t bitmap_bytes = image_info.image_bitmap_->Size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1510 | auto* bitmap_section = §ions[ImageHeader::kSectionImageBitmap]; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1511 | *bitmap_section = ImageSection(RoundUp(image_end, kPageSize), RoundUp(bitmap_bytes, kPageSize)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1512 | if (VLOG_IS_ON(compiler)) { |
| 1513 | LOG(INFO) << "Creating header for " << oat_filename; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1514 | size_t idx = 0; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1515 | for (const ImageSection& section : sections) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1516 | LOG(INFO) << static_cast<ImageHeader::ImageSections>(idx) << " " << section; |
| 1517 | ++idx; |
| 1518 | } |
| 1519 | LOG(INFO) << "Methods: clean=" << clean_methods_ << " dirty=" << dirty_methods_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1520 | LOG(INFO) << "Image roots address=" << std::hex << image_info.image_roots_address_ << std::dec; |
| 1521 | LOG(INFO) << "Image begin=" << std::hex << reinterpret_cast<uintptr_t>(global_image_begin_) |
| 1522 | << " Image offset=" << image_info.image_offset_ << std::dec; |
| 1523 | LOG(INFO) << "Oat file begin=" << std::hex << reinterpret_cast<uintptr_t>(oat_file_begin) |
| 1524 | << " Oat data begin=" << reinterpret_cast<uintptr_t>(image_info.oat_data_begin_) |
| 1525 | << " Oat data end=" << reinterpret_cast<uintptr_t>(oat_data_end) |
| 1526 | << " Oat file end=" << reinterpret_cast<uintptr_t>(oat_file_end); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1527 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1528 | // Store boot image info for app image so that we can relocate. |
| 1529 | uint32_t boot_image_begin = 0; |
| 1530 | uint32_t boot_image_end = 0; |
| 1531 | uint32_t boot_oat_begin = 0; |
| 1532 | uint32_t boot_oat_end = 0; |
| 1533 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1534 | heap->GetBootImagesSize(&boot_image_begin, &boot_image_end, &boot_oat_begin, &boot_oat_end); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1535 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1536 | // Create the header, leave 0 for data size since we will fill this in as we are writing the |
| 1537 | // image. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1538 | new (image_info.image_->Begin()) ImageHeader(PointerToLowMemUInt32(image_info.image_begin_), |
| 1539 | image_end, |
| 1540 | sections, |
| 1541 | image_info.image_roots_address_, |
| 1542 | oat_file_->GetOatHeader().GetChecksum(), |
| 1543 | PointerToLowMemUInt32(oat_file_begin), |
| 1544 | PointerToLowMemUInt32(image_info.oat_data_begin_), |
| 1545 | PointerToLowMemUInt32(oat_data_end), |
| 1546 | PointerToLowMemUInt32(oat_file_end), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1547 | boot_image_begin, |
| 1548 | boot_image_end - boot_image_begin, |
| 1549 | boot_oat_begin, |
| 1550 | boot_oat_end - boot_oat_begin, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1551 | target_ptr_size_, |
| 1552 | compile_pic_, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1553 | /*is_pic*/compile_app_image_, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1554 | image_storage_mode_, |
| 1555 | /*data_size*/0u); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | ArtMethod* ImageWriter::GetImageMethodAddress(ArtMethod* method) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1559 | auto it = native_object_relocations_.find(method); |
| 1560 | CHECK(it != native_object_relocations_.end()) << PrettyMethod(method) << " @ " << method; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1561 | const char* oat_filename = GetOatFilename(method->GetDexCache()); |
| 1562 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1563 | CHECK_GE(it->second.offset, image_info.image_end_) << "ArtMethods should be after Objects"; |
| 1564 | return reinterpret_cast<ArtMethod*>(image_info.image_begin_ + it->second.offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1567 | class FixupRootVisitor : public RootVisitor { |
| 1568 | public: |
| 1569 | explicit FixupRootVisitor(ImageWriter* image_writer) : image_writer_(image_writer) { |
| 1570 | } |
| 1571 | |
| 1572 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1573 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1574 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1575 | *roots[i] = image_writer_->GetImageAddress(*roots[i]); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 1580 | const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1581 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1582 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1583 | roots[i]->Assign(image_writer_->GetImageAddress(roots[i]->AsMirrorPtr())); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | private: |
| 1588 | ImageWriter* const image_writer_; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1589 | }; |
| 1590 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1591 | void ImageWriter::CopyAndFixupNativeData() { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1592 | const char* oat_filename = oat_file_->GetLocation().c_str(); |
| 1593 | ImageInfo& image_info = GetImageInfo(oat_filename); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1594 | // Copy ArtFields and methods to their locations and update the array for convenience. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1595 | for (auto& pair : native_object_relocations_) { |
| 1596 | NativeObjectRelocation& relocation = pair.second; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1597 | // Only work with fields and methods that are in the current oat file. |
| 1598 | if (strcmp(relocation.oat_filename, oat_filename) != 0) { |
| 1599 | continue; |
| 1600 | } |
| 1601 | auto* dest = image_info.image_->Begin() + relocation.offset; |
| 1602 | DCHECK_GE(dest, image_info.image_->Begin() + image_info.image_end_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1603 | DCHECK(!IsInBootImage(pair.first)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1604 | switch (relocation.type) { |
| 1605 | case kNativeObjectRelocationTypeArtField: { |
| 1606 | memcpy(dest, pair.first, sizeof(ArtField)); |
| 1607 | reinterpret_cast<ArtField*>(dest)->SetDeclaringClass( |
| 1608 | GetImageAddress(reinterpret_cast<ArtField*>(pair.first)->GetDeclaringClass())); |
| 1609 | break; |
| 1610 | } |
| 1611 | case kNativeObjectRelocationTypeArtMethodClean: |
| 1612 | case kNativeObjectRelocationTypeArtMethodDirty: { |
| 1613 | CopyAndFixupMethod(reinterpret_cast<ArtMethod*>(pair.first), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1614 | reinterpret_cast<ArtMethod*>(dest), |
| 1615 | image_info); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1616 | break; |
| 1617 | } |
| 1618 | // For arrays, copy just the header since the elements will get copied by their corresponding |
| 1619 | // relocations. |
| 1620 | case kNativeObjectRelocationTypeArtFieldArray: { |
| 1621 | memcpy(dest, pair.first, LengthPrefixedArray<ArtField>::ComputeSize(0)); |
| 1622 | break; |
| 1623 | } |
| 1624 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 1625 | case kNativeObjectRelocationTypeArtMethodArrayDirty: { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1626 | memcpy(dest, pair.first, LengthPrefixedArray<ArtMethod>::ComputeSize( |
| 1627 | 0, |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1628 | ArtMethod::Size(target_ptr_size_), |
| 1629 | ArtMethod::Alignment(target_ptr_size_))); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1630 | break; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1631 | case kNativeObjectRelocationTypeDexCacheArray: |
| 1632 | // Nothing to copy here, everything is done in FixupDexCache(). |
| 1633 | break; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1634 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | // Fixup the image method roots. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1638 | auto* image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1639 | const ImageSection& methods_section = image_header->GetMethodsSection(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1640 | for (size_t i = 0; i < ImageHeader::kImageMethodsCount; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1641 | ArtMethod* method = image_methods_[i]; |
| 1642 | CHECK(method != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1643 | // Only place runtime methods in the image of the default oat file. |
| 1644 | if (method->IsRuntimeMethod() && strcmp(default_oat_filename_, oat_filename) != 0) { |
| 1645 | continue; |
| 1646 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1647 | if (!IsInBootImage(method)) { |
| 1648 | auto it = native_object_relocations_.find(method); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1649 | CHECK(it != native_object_relocations_.end()) << "No forwarding for " << PrettyMethod(method); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1650 | NativeObjectRelocation& relocation = it->second; |
| 1651 | CHECK(methods_section.Contains(relocation.offset)) << relocation.offset << " not in " |
| 1652 | << methods_section; |
| 1653 | CHECK(relocation.IsArtMethodRelocation()) << relocation.type; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1654 | method = reinterpret_cast<ArtMethod*>(global_image_begin_ + it->second.offset); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1655 | } |
| 1656 | image_header->SetImageMethod(static_cast<ImageHeader::ImageMethod>(i), method); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1657 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1658 | FixupRootVisitor root_visitor(this); |
| 1659 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1660 | // Write the intern table into the image. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1661 | if (image_info.intern_table_bytes_ > 0) { |
| 1662 | const ImageSection& intern_table_section = image_header->GetImageSection( |
| 1663 | ImageHeader::kSectionInternedStrings); |
| 1664 | InternTable* const intern_table = image_info.intern_table_.get(); |
| 1665 | uint8_t* const intern_table_memory_ptr = |
| 1666 | image_info.image_->Begin() + intern_table_section.Offset(); |
| 1667 | const size_t intern_table_bytes = intern_table->WriteToMemory(intern_table_memory_ptr); |
| 1668 | CHECK_EQ(intern_table_bytes, image_info.intern_table_bytes_); |
| 1669 | // Fixup the pointers in the newly written intern table to contain image addresses. |
| 1670 | InternTable temp_intern_table; |
| 1671 | // Note that we require that ReadFromMemory does not make an internal copy of the elements so that |
| 1672 | // the VisitRoots() will update the memory directly rather than the copies. |
| 1673 | // This also relies on visit roots not doing any verification which could fail after we update |
| 1674 | // the roots to be the image addresses. |
| 1675 | temp_intern_table.AddTableFromMemory(intern_table_memory_ptr); |
| 1676 | CHECK_EQ(temp_intern_table.Size(), intern_table->Size()); |
| 1677 | temp_intern_table.VisitRoots(&root_visitor, kVisitRootFlagAllRoots); |
| 1678 | } |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 1679 | // Write the class table(s) into the image. class_table_bytes_ may be 0 if there are multiple |
| 1680 | // class loaders. Writing multiple class tables into the image is currently unsupported. |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1681 | if (image_info.class_table_bytes_ > 0u) { |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 1682 | const ImageSection& class_table_section = image_header->GetImageSection( |
| 1683 | ImageHeader::kSectionClassTable); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1684 | uint8_t* const class_table_memory_ptr = |
| 1685 | image_info.image_->Begin() + class_table_section.Offset(); |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 1686 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1687 | |
| 1688 | ClassTable* table = image_info.class_table_.get(); |
| 1689 | CHECK(table != nullptr); |
| 1690 | const size_t class_table_bytes = table->WriteToMemory(class_table_memory_ptr); |
| 1691 | CHECK_EQ(class_table_bytes, image_info.class_table_bytes_); |
| 1692 | // Fixup the pointers in the newly written class table to contain image addresses. See |
| 1693 | // above comment for intern tables. |
| 1694 | ClassTable temp_class_table; |
| 1695 | temp_class_table.ReadFromMemory(class_table_memory_ptr); |
| 1696 | CHECK_EQ(temp_class_table.NumZygoteClasses(), table->NumNonZygoteClasses() + |
| 1697 | table->NumZygoteClasses()); |
| 1698 | BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(&root_visitor, |
| 1699 | RootInfo(kRootUnknown)); |
| 1700 | temp_class_table.VisitRoots(buffered_visitor); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1701 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 1704 | void ImageWriter::CopyAndFixupObjects() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1705 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1706 | heap->VisitObjects(CopyAndFixupObjectsCallback, this); |
| 1707 | // Fix up the object previously had hash codes. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1708 | for (const auto& hash_pair : saved_hashcode_map_) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1709 | Object* obj = hash_pair.first; |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 1710 | DCHECK_EQ(obj->GetLockWord<kVerifyNone>(false).ReadBarrierState(), 0U); |
| 1711 | obj->SetLockWord<kVerifyNone>(LockWord::FromHashCode(hash_pair.second, 0U), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1712 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1713 | saved_hashcode_map_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1714 | } |
| 1715 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1716 | void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1717 | DCHECK(obj != nullptr); |
| 1718 | DCHECK(arg != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1719 | reinterpret_cast<ImageWriter*>(arg)->CopyAndFixupObject(obj); |
| 1720 | } |
| 1721 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1722 | void ImageWriter::FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, |
| 1723 | mirror::Class* klass, Bin array_type) { |
| 1724 | CHECK(klass->IsArrayClass()); |
| 1725 | CHECK(arr->IsIntArray() || arr->IsLongArray()) << PrettyClass(klass) << " " << arr; |
| 1726 | // Fixup int and long pointers for the ArtMethod or ArtField arrays. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1727 | const size_t num_elements = arr->GetLength(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1728 | dst->SetClass(GetImageAddress(arr->GetClass())); |
| 1729 | auto* dest_array = down_cast<mirror::PointerArray*>(dst); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1730 | for (size_t i = 0, count = num_elements; i < count; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1731 | void* elem = arr->GetElementPtrSize<void*>(i, target_ptr_size_); |
| 1732 | if (elem != nullptr && !IsInBootImage(elem)) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1733 | auto it = native_object_relocations_.find(elem); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1734 | if (UNLIKELY(it == native_object_relocations_.end())) { |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1735 | if (it->second.IsArtMethodRelocation()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1736 | auto* method = reinterpret_cast<ArtMethod*>(elem); |
| 1737 | LOG(FATAL) << "No relocation entry for ArtMethod " << PrettyMethod(method) << " @ " |
| 1738 | << method << " idx=" << i << "/" << num_elements << " with declaring class " |
| 1739 | << PrettyClass(method->GetDeclaringClass()); |
| 1740 | } else { |
| 1741 | CHECK_EQ(array_type, kBinArtField); |
| 1742 | auto* field = reinterpret_cast<ArtField*>(elem); |
| 1743 | LOG(FATAL) << "No relocation entry for ArtField " << PrettyField(field) << " @ " |
| 1744 | << field << " idx=" << i << "/" << num_elements << " with declaring class " |
| 1745 | << PrettyClass(field->GetDeclaringClass()); |
| 1746 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1747 | UNREACHABLE(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1748 | } else { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1749 | ImageInfo& image_info = GetImageInfo(it->second.oat_filename); |
| 1750 | elem = image_info.image_begin_ + it->second.offset; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1751 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1752 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1753 | dest_array->SetElementPtrSize<false, true>(i, elem, target_ptr_size_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1754 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | void ImageWriter::CopyAndFixupObject(Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1758 | if (IsInBootImage(obj)) { |
| 1759 | return; |
| 1760 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1761 | size_t offset = GetImageOffset(obj); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1762 | const char* oat_filename = GetOatFilename(obj); |
| 1763 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1764 | auto* dst = reinterpret_cast<Object*>(image_info.image_->Begin() + offset); |
| 1765 | DCHECK_LT(offset, image_info.image_end_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1766 | const auto* src = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1767 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1768 | image_info.image_bitmap_->Set(dst); // Mark the obj as live. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1769 | |
| 1770 | const size_t n = obj->SizeOf(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1771 | DCHECK_LE(offset + n, image_info.image_->Size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1772 | memcpy(dst, src, n); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1773 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1774 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 1775 | // word. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1776 | const auto it = saved_hashcode_map_.find(obj); |
| 1777 | dst->SetLockWord(it != saved_hashcode_map_.end() ? |
| 1778 | LockWord::FromHashCode(it->second, 0u) : LockWord::Default(), false); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1779 | FixupObject(obj, dst); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1782 | // Rewrite all the references in the copied object to point to their image address equivalent |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1783 | class FixupVisitor { |
| 1784 | public: |
| 1785 | FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) { |
| 1786 | } |
| 1787 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1788 | // Ignore class roots since we don't have a way to map them to the destination. These are handled |
| 1789 | // with other logic. |
| 1790 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 1791 | const {} |
| 1792 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 1793 | |
| 1794 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1795 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1796 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 1797 | Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1798 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 1799 | // image. |
| 1800 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1801 | offset, |
| 1802 | image_writer_->GetImageAddress(ref)); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | // java.lang.ref.Reference visitor. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1806 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, mirror::Reference* ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1807 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1808 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1809 | mirror::Reference::ReferentOffset(), |
| 1810 | image_writer_->GetImageAddress(ref->GetReferent())); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1813 | protected: |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1814 | ImageWriter* const image_writer_; |
| 1815 | mirror::Object* const copy_; |
| 1816 | }; |
| 1817 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1818 | class FixupClassVisitor FINAL : public FixupVisitor { |
| 1819 | public: |
| 1820 | FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) { |
| 1821 | } |
| 1822 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1823 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1824 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1825 | DCHECK(obj->IsClass()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1826 | FixupVisitor::operator()(obj, offset, /*is_static*/false); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1829 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, |
| 1830 | mirror::Reference* ref ATTRIBUTE_UNUSED) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1831 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1832 | LOG(FATAL) << "Reference not expected here."; |
| 1833 | } |
| 1834 | }; |
| 1835 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1836 | uintptr_t ImageWriter::NativeOffsetInImage(void* obj) { |
| 1837 | DCHECK(obj != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1838 | DCHECK(!IsInBootImage(obj)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1839 | auto it = native_object_relocations_.find(obj); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1840 | CHECK(it != native_object_relocations_.end()) << obj << " spaces " |
| 1841 | << Runtime::Current()->GetHeap()->DumpSpaces(); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1842 | const NativeObjectRelocation& relocation = it->second; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1843 | return relocation.offset; |
| 1844 | } |
| 1845 | |
| 1846 | template <typename T> |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1847 | T* ImageWriter::NativeLocationInImage(T* obj) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1848 | if (obj == nullptr || IsInBootImage(obj)) { |
| 1849 | return obj; |
| 1850 | } else { |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1851 | auto it = native_object_relocations_.find(obj); |
| 1852 | CHECK(it != native_object_relocations_.end()) << obj << " spaces " |
| 1853 | << Runtime::Current()->GetHeap()->DumpSpaces(); |
| 1854 | const NativeObjectRelocation& relocation = it->second; |
| 1855 | ImageInfo& image_info = GetImageInfo(relocation.oat_filename); |
| 1856 | return reinterpret_cast<T*>(image_info.image_begin_ + relocation.offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1857 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1860 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1861 | T* ImageWriter::NativeCopyLocation(T* obj, mirror::DexCache* dex_cache) { |
| 1862 | if (obj == nullptr || IsInBootImage(obj)) { |
| 1863 | return obj; |
| 1864 | } else { |
| 1865 | const char* oat_filename = GetOatFilenameForDexCache(dex_cache); |
| 1866 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1867 | return reinterpret_cast<T*>(image_info.image_->Begin() + NativeOffsetInImage(obj)); |
| 1868 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | class NativeLocationVisitor { |
| 1872 | public: |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1873 | explicit NativeLocationVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1874 | |
| 1875 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1876 | T* operator()(T* ptr) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1877 | return image_writer_->NativeLocationInImage(ptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1878 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1879 | |
| 1880 | private: |
| 1881 | ImageWriter* const image_writer_; |
| 1882 | }; |
| 1883 | |
| 1884 | void ImageWriter::FixupClass(mirror::Class* orig, mirror::Class* copy) { |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1885 | orig->FixupNativePointers(copy, target_ptr_size_, NativeLocationVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1886 | FixupClassVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1887 | static_cast<mirror::Object*>(orig)->VisitReferences(visitor, visitor); |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 1888 | |
| 1889 | // Remove the clinitThreadId. This is required for image determinism. |
| 1890 | copy->SetClinitThreadId(static_cast<pid_t>(0)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1893 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1894 | DCHECK(orig != nullptr); |
| 1895 | DCHECK(copy != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 1896 | if (kUseBakerOrBrooksReadBarrier) { |
| 1897 | orig->AssertReadBarrierPointer(); |
| 1898 | if (kUseBrooksReadBarrier) { |
| 1899 | // Note the address 'copy' isn't the same as the image address of 'orig'. |
| 1900 | copy->SetReadBarrierPointer(GetImageAddress(orig)); |
| 1901 | DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig)); |
| 1902 | } |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 1903 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1904 | auto* klass = orig->GetClass(); |
| 1905 | if (klass->IsIntArrayClass() || klass->IsLongArrayClass()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1906 | // Is this a native pointer array? |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1907 | auto it = pointer_arrays_.find(down_cast<mirror::PointerArray*>(orig)); |
| 1908 | if (it != pointer_arrays_.end()) { |
| 1909 | // Should only need to fixup every pointer array exactly once. |
| 1910 | FixupPointerArray(copy, down_cast<mirror::PointerArray*>(orig), klass, it->second); |
| 1911 | pointer_arrays_.erase(it); |
| 1912 | return; |
| 1913 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1914 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1915 | if (orig->IsClass()) { |
| 1916 | FixupClass(orig->AsClass<kVerifyNone>(), down_cast<mirror::Class*>(copy)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1917 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1918 | if (klass == mirror::Method::StaticClass() || klass == mirror::Constructor::StaticClass()) { |
| 1919 | // Need to go update the ArtMethod. |
| 1920 | auto* dest = down_cast<mirror::AbstractMethod*>(copy); |
| 1921 | auto* src = down_cast<mirror::AbstractMethod*>(orig); |
| 1922 | ArtMethod* src_method = src->GetArtMethod(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1923 | auto it = native_object_relocations_.find(src_method); |
| 1924 | CHECK(it != native_object_relocations_.end()) |
| 1925 | << "Missing relocation for AbstractMethod.artMethod " << PrettyMethod(src_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1926 | dest->SetArtMethod( |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1927 | reinterpret_cast<ArtMethod*>(global_image_begin_ + it->second.offset)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1928 | } else if (!klass->IsArrayClass()) { |
| 1929 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1930 | if (klass == class_linker->GetClassRoot(ClassLinker::kJavaLangDexCache)) { |
| 1931 | FixupDexCache(down_cast<mirror::DexCache*>(orig), down_cast<mirror::DexCache*>(copy)); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1932 | } else if (klass->IsClassLoaderClass()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1933 | mirror::ClassLoader* copy_loader = down_cast<mirror::ClassLoader*>(copy); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1934 | // If src is a ClassLoader, set the class table to null so that it gets recreated by the |
| 1935 | // ClassLoader. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1936 | copy_loader->SetClassTable(nullptr); |
Mathieu Chartier | 5550c56 | 2015-09-22 15:18:04 -0700 | [diff] [blame] | 1937 | // Also set allocator to null to be safe. The allocator is created when we create the class |
| 1938 | // table. We also never expect to unload things in the image since they are held live as |
| 1939 | // roots. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1940 | copy_loader->SetAllocator(nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1941 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1942 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1943 | FixupVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1944 | orig->VisitReferences(visitor, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1945 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1948 | |
| 1949 | class ImageAddressVisitor { |
| 1950 | public: |
| 1951 | explicit ImageAddressVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
| 1952 | |
| 1953 | template <typename T> |
| 1954 | T* operator()(T* ptr) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1955 | return image_writer_->GetImageAddress(ptr); |
| 1956 | } |
| 1957 | |
| 1958 | private: |
| 1959 | ImageWriter* const image_writer_; |
| 1960 | }; |
| 1961 | |
| 1962 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1963 | void ImageWriter::FixupDexCache(mirror::DexCache* orig_dex_cache, |
| 1964 | mirror::DexCache* copy_dex_cache) { |
| 1965 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 1966 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 1967 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 1968 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
| 1969 | GcRoot<mirror::String>* orig_strings = orig_dex_cache->GetStrings(); |
| 1970 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1971 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::StringsOffset(), |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1972 | NativeLocationInImage(orig_strings), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1973 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1974 | orig_dex_cache->FixupStrings(NativeCopyLocation(orig_strings, orig_dex_cache), |
| 1975 | ImageAddressVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1976 | } |
| 1977 | GcRoot<mirror::Class>* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 1978 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1979 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedTypesOffset(), |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1980 | NativeLocationInImage(orig_types), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1981 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1982 | orig_dex_cache->FixupResolvedTypes(NativeCopyLocation(orig_types, orig_dex_cache), |
| 1983 | ImageAddressVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1984 | } |
| 1985 | ArtMethod** orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 1986 | if (orig_methods != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1987 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedMethodsOffset(), |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1988 | NativeLocationInImage(orig_methods), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1989 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1990 | ArtMethod** copy_methods = NativeCopyLocation(orig_methods, orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1991 | for (size_t i = 0, num = orig_dex_cache->NumResolvedMethods(); i != num; ++i) { |
| 1992 | ArtMethod* orig = mirror::DexCache::GetElementPtrSize(orig_methods, i, target_ptr_size_); |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 1993 | // NativeLocationInImage also handles runtime methods since these have relocation info. |
| 1994 | ArtMethod* copy = NativeLocationInImage(orig); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1995 | mirror::DexCache::SetElementPtrSize(copy_methods, i, copy, target_ptr_size_); |
| 1996 | } |
| 1997 | } |
| 1998 | ArtField** orig_fields = orig_dex_cache->GetResolvedFields(); |
| 1999 | if (orig_fields != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2000 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedFieldsOffset(), |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2001 | NativeLocationInImage(orig_fields), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2002 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2003 | ArtField** copy_fields = NativeCopyLocation(orig_fields, orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2004 | for (size_t i = 0, num = orig_dex_cache->NumResolvedFields(); i != num; ++i) { |
| 2005 | ArtField* orig = mirror::DexCache::GetElementPtrSize(orig_fields, i, target_ptr_size_); |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2006 | ArtField* copy = NativeLocationInImage(orig); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2007 | mirror::DexCache::SetElementPtrSize(copy_fields, i, copy, target_ptr_size_); |
| 2008 | } |
| 2009 | } |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2010 | |
| 2011 | // Remove the DexFile pointers. They will be fixed up when the runtime loads the oat file. Leaving |
| 2012 | // compiler pointers in here will make the output non-deterministic. |
| 2013 | copy_dex_cache->SetDexFile(nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2014 | } |
| 2015 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2016 | const uint8_t* ImageWriter::GetOatAddress(OatAddress type) const { |
| 2017 | DCHECK_LT(type, kOatAddressCount); |
| 2018 | // If we are compiling an app image, we need to use the stubs of the boot image. |
| 2019 | if (compile_app_image_) { |
| 2020 | // Use the current image pointers. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2021 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2022 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 2023 | DCHECK(!image_spaces.empty()); |
| 2024 | const OatFile* oat_file = image_spaces[0]->GetOatFile(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2025 | CHECK(oat_file != nullptr); |
| 2026 | const OatHeader& header = oat_file->GetOatHeader(); |
| 2027 | switch (type) { |
| 2028 | // TODO: We could maybe clean this up if we stored them in an array in the oat header. |
| 2029 | case kOatAddressQuickGenericJNITrampoline: |
| 2030 | return static_cast<const uint8_t*>(header.GetQuickGenericJniTrampoline()); |
| 2031 | case kOatAddressInterpreterToInterpreterBridge: |
| 2032 | return static_cast<const uint8_t*>(header.GetInterpreterToInterpreterBridge()); |
| 2033 | case kOatAddressInterpreterToCompiledCodeBridge: |
| 2034 | return static_cast<const uint8_t*>(header.GetInterpreterToCompiledCodeBridge()); |
| 2035 | case kOatAddressJNIDlsymLookup: |
| 2036 | return static_cast<const uint8_t*>(header.GetJniDlsymLookup()); |
| 2037 | case kOatAddressQuickIMTConflictTrampoline: |
| 2038 | return static_cast<const uint8_t*>(header.GetQuickImtConflictTrampoline()); |
| 2039 | case kOatAddressQuickResolutionTrampoline: |
| 2040 | return static_cast<const uint8_t*>(header.GetQuickResolutionTrampoline()); |
| 2041 | case kOatAddressQuickToInterpreterBridge: |
| 2042 | return static_cast<const uint8_t*>(header.GetQuickToInterpreterBridge()); |
| 2043 | default: |
| 2044 | UNREACHABLE(); |
| 2045 | } |
| 2046 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2047 | const ImageInfo& primary_image_info = GetImageInfo(0); |
| 2048 | return GetOatAddressForOffset(primary_image_info.oat_address_offsets_[type], primary_image_info); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2049 | } |
| 2050 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2051 | const uint8_t* ImageWriter::GetQuickCode(ArtMethod* method, |
| 2052 | const ImageInfo& image_info, |
| 2053 | bool* quick_is_interpreted) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2054 | DCHECK(!method->IsResolutionMethod()) << PrettyMethod(method); |
| 2055 | DCHECK(!method->IsImtConflictMethod()) << PrettyMethod(method); |
| 2056 | DCHECK(!method->IsImtUnimplementedMethod()) << PrettyMethod(method); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2057 | DCHECK(method->IsInvokable()) << PrettyMethod(method); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2058 | DCHECK(!IsInBootImage(method)) << PrettyMethod(method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2059 | |
| 2060 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 2061 | // trampoline. |
| 2062 | |
| 2063 | // Quick entrypoint: |
Igor Murashkin | 6e2237d | 2016-02-19 16:41:44 -0800 | [diff] [blame] | 2064 | const void* quick_oat_entry_point = |
| 2065 | method->GetEntryPointFromQuickCompiledCodePtrSize(target_ptr_size_); |
| 2066 | const uint8_t* quick_code; |
| 2067 | |
| 2068 | if (UNLIKELY(IsInBootImage(method->GetDeclaringClass()))) { |
Alex Light | 7ead0c0 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 2069 | DCHECK(method->IsCopied()); |
Igor Murashkin | 6e2237d | 2016-02-19 16:41:44 -0800 | [diff] [blame] | 2070 | // If the code is not in the oat file corresponding to this image (e.g. default methods) |
| 2071 | quick_code = reinterpret_cast<const uint8_t*>(quick_oat_entry_point); |
| 2072 | } else { |
| 2073 | uint32_t quick_oat_code_offset = PointerToLowMemUInt32(quick_oat_entry_point); |
| 2074 | quick_code = GetOatAddressForOffset(quick_oat_code_offset, image_info); |
| 2075 | } |
| 2076 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2077 | *quick_is_interpreted = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2078 | if (quick_code != nullptr && (!method->IsStatic() || method->IsConstructor() || |
| 2079 | method->GetDeclaringClass()->IsInitialized())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2080 | // We have code for a non-static or initialized method, just use the code. |
| 2081 | } else if (quick_code == nullptr && method->IsNative() && |
| 2082 | (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) { |
| 2083 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2084 | quick_code = GetOatAddress(kOatAddressQuickGenericJNITrampoline); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2085 | } else if (quick_code == nullptr && !method->IsNative()) { |
| 2086 | // We don't have code at all for a non-native method, use the interpreter. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2087 | quick_code = GetOatAddress(kOatAddressQuickToInterpreterBridge); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2088 | *quick_is_interpreted = true; |
| 2089 | } else { |
| 2090 | CHECK(!method->GetDeclaringClass()->IsInitialized()); |
| 2091 | // We have code for a static method, but need to go through the resolution stub for class |
| 2092 | // initialization. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2093 | quick_code = GetOatAddress(kOatAddressQuickResolutionTrampoline); |
| 2094 | } |
| 2095 | if (!IsInBootOatFile(quick_code)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2096 | // DCHECK_GE(quick_code, oat_data_begin_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2097 | } |
| 2098 | return quick_code; |
| 2099 | } |
| 2100 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2101 | void ImageWriter::CopyAndFixupMethod(ArtMethod* orig, |
| 2102 | ArtMethod* copy, |
| 2103 | const ImageInfo& image_info) { |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 2104 | memcpy(copy, orig, ArtMethod::Size(target_ptr_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2105 | |
| 2106 | copy->SetDeclaringClass(GetImageAddress(orig->GetDeclaringClassUnchecked())); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2107 | |
| 2108 | ArtMethod** orig_resolved_methods = orig->GetDexCacheResolvedMethods(target_ptr_size_); |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2109 | copy->SetDexCacheResolvedMethods(NativeLocationInImage(orig_resolved_methods), target_ptr_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2110 | GcRoot<mirror::Class>* orig_resolved_types = orig->GetDexCacheResolvedTypes(target_ptr_size_); |
Mathieu Chartier | 4ba85a6 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2111 | copy->SetDexCacheResolvedTypes(NativeLocationInImage(orig_resolved_types), target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2112 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2113 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 2114 | // oat_begin_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2115 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2116 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 2117 | Runtime* runtime = Runtime::Current(); |
| 2118 | if (UNLIKELY(orig == runtime->GetResolutionMethod())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2119 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2120 | GetOatAddress(kOatAddressQuickResolutionTrampoline), target_ptr_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 2121 | } else if (UNLIKELY(orig == runtime->GetImtConflictMethod() || |
| 2122 | orig == runtime->GetImtUnimplementedMethod())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2123 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2124 | GetOatAddress(kOatAddressQuickIMTConflictTrampoline), target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2125 | } else if (UNLIKELY(orig->IsRuntimeMethod())) { |
| 2126 | bool found_one = false; |
| 2127 | for (size_t i = 0; i < static_cast<size_t>(Runtime::kLastCalleeSaveType); ++i) { |
| 2128 | auto idx = static_cast<Runtime::CalleeSaveType>(i); |
| 2129 | if (runtime->HasCalleeSaveMethod(idx) && runtime->GetCalleeSaveMethod(idx) == orig) { |
| 2130 | found_one = true; |
| 2131 | break; |
| 2132 | } |
| 2133 | } |
| 2134 | CHECK(found_one) << "Expected to find callee save method but got " << PrettyMethod(orig); |
| 2135 | CHECK(copy->IsRuntimeMethod()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2136 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2137 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 2138 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 2139 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2140 | if (UNLIKELY(!orig->IsInvokable())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2141 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2142 | GetOatAddress(kOatAddressQuickToInterpreterBridge), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2143 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2144 | bool quick_is_interpreted; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2145 | const uint8_t* quick_code = GetQuickCode(orig, image_info, &quick_is_interpreted); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2146 | copy->SetEntryPointFromQuickCompiledCodePtrSize(quick_code, target_ptr_size_); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 2147 | |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 2148 | // JNI entrypoint: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2149 | if (orig->IsNative()) { |
| 2150 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 2151 | // Note this is not the code_ pointer, that is handled above. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2152 | copy->SetEntryPointFromJniPtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2153 | GetOatAddress(kOatAddressJNIDlsymLookup), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2154 | } |
| 2155 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2156 | } |
| 2157 | } |
| 2158 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2159 | static OatHeader* GetOatHeaderFromElf(ElfFile* elf) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2160 | uint64_t data_sec_offset; |
| 2161 | bool has_data_sec = elf->GetSectionOffsetAndSize(".rodata", &data_sec_offset, nullptr); |
| 2162 | if (!has_data_sec) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2163 | return nullptr; |
| 2164 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2165 | return reinterpret_cast<OatHeader*>(elf->Begin() + data_sec_offset); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 2166 | } |
| 2167 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2168 | void ImageWriter::SetOatChecksumFromElfFile(File* elf_file) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2169 | std::string error_msg; |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 2170 | std::unique_ptr<ElfFile> elf(ElfFile::Open(elf_file, |
| 2171 | PROT_READ | PROT_WRITE, |
| 2172 | MAP_SHARED, |
| 2173 | &error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2174 | if (elf.get() == nullptr) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2175 | LOG(FATAL) << "Unable open oat file: " << error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2176 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2177 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2178 | OatHeader* oat_header = GetOatHeaderFromElf(elf.get()); |
| 2179 | CHECK(oat_header != nullptr); |
| 2180 | CHECK(oat_header->IsValid()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2181 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2182 | ImageInfo& image_info = GetImageInfo(oat_file_->GetLocation().c_str()); |
| 2183 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2184 | image_header->SetOatChecksum(oat_header->GetChecksum()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2187 | size_t ImageWriter::GetBinSizeSum(ImageWriter::ImageInfo& image_info, ImageWriter::Bin up_to) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2188 | DCHECK_LE(up_to, kBinSize); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2189 | return std::accumulate(&image_info.bin_slot_sizes_[0], |
| 2190 | &image_info.bin_slot_sizes_[up_to], |
| 2191 | /*init*/0); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | ImageWriter::BinSlot::BinSlot(uint32_t lockword) : lockword_(lockword) { |
| 2195 | // These values may need to get updated if more bins are added to the enum Bin |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2196 | static_assert(kBinBits == 3, "wrong number of bin bits"); |
| 2197 | static_assert(kBinShift == 27, "wrong number of shift"); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2198 | static_assert(sizeof(BinSlot) == sizeof(LockWord), "BinSlot/LockWord must have equal sizes"); |
| 2199 | |
| 2200 | DCHECK_LT(GetBin(), kBinSize); |
| 2201 | DCHECK_ALIGNED(GetIndex(), kObjectAlignment); |
| 2202 | } |
| 2203 | |
| 2204 | ImageWriter::BinSlot::BinSlot(Bin bin, uint32_t index) |
| 2205 | : BinSlot(index | (static_cast<uint32_t>(bin) << kBinShift)) { |
| 2206 | DCHECK_EQ(index, GetIndex()); |
| 2207 | } |
| 2208 | |
| 2209 | ImageWriter::Bin ImageWriter::BinSlot::GetBin() const { |
| 2210 | return static_cast<Bin>((lockword_ & kBinMask) >> kBinShift); |
| 2211 | } |
| 2212 | |
| 2213 | uint32_t ImageWriter::BinSlot::GetIndex() const { |
| 2214 | return lockword_ & ~kBinMask; |
| 2215 | } |
| 2216 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2217 | uint8_t* ImageWriter::GetOatFileBegin(const char* oat_filename) const { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2218 | uintptr_t last_image_end = 0; |
| 2219 | for (const char* oat_fn : oat_filenames_) { |
| 2220 | const ImageInfo& image_info = GetConstImageInfo(oat_fn); |
| 2221 | DCHECK(image_info.image_begin_ != nullptr); |
| 2222 | uintptr_t this_end = reinterpret_cast<uintptr_t>(image_info.image_begin_) + |
| 2223 | image_info.image_size_; |
| 2224 | last_image_end = std::max(this_end, last_image_end); |
| 2225 | } |
| 2226 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 2227 | return reinterpret_cast<uint8_t*>(last_image_end) + image_info.oat_offset_; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2230 | ImageWriter::Bin ImageWriter::BinTypeForNativeRelocationType(NativeObjectRelocationType type) { |
| 2231 | switch (type) { |
| 2232 | case kNativeObjectRelocationTypeArtField: |
| 2233 | case kNativeObjectRelocationTypeArtFieldArray: |
| 2234 | return kBinArtField; |
| 2235 | case kNativeObjectRelocationTypeArtMethodClean: |
| 2236 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 2237 | return kBinArtMethodClean; |
| 2238 | case kNativeObjectRelocationTypeArtMethodDirty: |
| 2239 | case kNativeObjectRelocationTypeArtMethodArrayDirty: |
| 2240 | return kBinArtMethodDirty; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2241 | case kNativeObjectRelocationTypeDexCacheArray: |
| 2242 | return kBinDexCacheArray; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2243 | } |
| 2244 | UNREACHABLE(); |
| 2245 | } |
| 2246 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2247 | const char* ImageWriter::GetOatFilename(mirror::Object* obj) const { |
| 2248 | if (compile_app_image_) { |
| 2249 | return default_oat_filename_; |
| 2250 | } else { |
| 2251 | return GetOatFilenameForDexCache(obj->IsDexCache() ? obj->AsDexCache() : |
| 2252 | obj->IsClass() ? obj->AsClass()->GetDexCache() : obj->GetClass()->GetDexCache()); |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | const char* ImageWriter::GetOatFilenameForDexCache(mirror::DexCache* dex_cache) const { |
| 2257 | if (compile_app_image_ || dex_cache == nullptr) { |
| 2258 | return default_oat_filename_; |
| 2259 | } else { |
| 2260 | auto it = dex_file_oat_filename_map_.find(dex_cache->GetDexFile()); |
| 2261 | DCHECK(it != dex_file_oat_filename_map_.end()) << dex_cache->GetDexFile()->GetLocation(); |
| 2262 | return it->second; |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | ImageWriter::ImageInfo& ImageWriter::GetImageInfo(const char* oat_filename) { |
| 2267 | auto it = image_info_map_.find(oat_filename); |
| 2268 | DCHECK(it != image_info_map_.end()); |
| 2269 | return it->second; |
| 2270 | } |
| 2271 | |
| 2272 | const ImageWriter::ImageInfo& ImageWriter::GetConstImageInfo(const char* oat_filename) const { |
| 2273 | auto it = image_info_map_.find(oat_filename); |
| 2274 | DCHECK(it != image_info_map_.end()); |
| 2275 | return it->second; |
| 2276 | } |
| 2277 | |
| 2278 | const ImageWriter::ImageInfo& ImageWriter::GetImageInfo(size_t index) const { |
| 2279 | DCHECK_LT(index, oat_filenames_.size()); |
| 2280 | return GetConstImageInfo(oat_filenames_[index]); |
| 2281 | } |
| 2282 | |
Vladimir Marko | d8904a5 | 2016-01-29 16:27:27 +0000 | [diff] [blame] | 2283 | void ImageWriter::UpdateOatFile(File* oat_file, const char* oat_filename) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2284 | DCHECK(oat_file != nullptr); |
Mathieu Chartier | 14567fd | 2016-01-28 20:33:36 -0800 | [diff] [blame] | 2285 | if (compile_app_image_) { |
| 2286 | CHECK_EQ(oat_filenames_.size(), 1u) << "App image should have no next image."; |
| 2287 | return; |
| 2288 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2289 | ImageInfo& cur_image_info = GetImageInfo(oat_filename); |
| 2290 | |
| 2291 | // Update the oat_offset of the next image info. |
| 2292 | auto it = std::find(oat_filenames_.begin(), oat_filenames_.end(), oat_filename); |
| 2293 | DCHECK(it != oat_filenames_.end()); |
| 2294 | |
| 2295 | it++; |
| 2296 | if (it != oat_filenames_.end()) { |
Mathieu Chartier | 14567fd | 2016-01-28 20:33:36 -0800 | [diff] [blame] | 2297 | size_t oat_loaded_size = 0; |
| 2298 | size_t oat_data_offset = 0; |
| 2299 | ElfWriter::GetOatElfInformation(oat_file, &oat_loaded_size, &oat_data_offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2300 | // There is a following one. |
| 2301 | ImageInfo& next_image_info = GetImageInfo(*it); |
| 2302 | next_image_info.oat_offset_ = cur_image_info.oat_offset_ + oat_loaded_size; |
| 2303 | } |
| 2304 | } |
| 2305 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2306 | ImageWriter::ImageWriter( |
| 2307 | const CompilerDriver& compiler_driver, |
| 2308 | uintptr_t image_begin, |
| 2309 | bool compile_pic, |
| 2310 | bool compile_app_image, |
| 2311 | ImageHeader::StorageMode image_storage_mode, |
| 2312 | const std::vector<const char*> oat_filenames, |
| 2313 | const std::unordered_map<const DexFile*, const char*>& dex_file_oat_filename_map) |
| 2314 | : compiler_driver_(compiler_driver), |
| 2315 | global_image_begin_(reinterpret_cast<uint8_t*>(image_begin)), |
| 2316 | image_objects_offset_begin_(0), |
| 2317 | oat_file_(nullptr), |
| 2318 | compile_pic_(compile_pic), |
| 2319 | compile_app_image_(compile_app_image), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2320 | target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())), |
| 2321 | image_method_array_(ImageHeader::kImageMethodsCount), |
| 2322 | dirty_methods_(0u), |
| 2323 | clean_methods_(0u), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2324 | image_storage_mode_(image_storage_mode), |
| 2325 | dex_file_oat_filename_map_(dex_file_oat_filename_map), |
| 2326 | oat_filenames_(oat_filenames), |
| 2327 | default_oat_filename_(oat_filenames[0]) { |
| 2328 | CHECK_NE(image_begin, 0U); |
| 2329 | for (const char* oat_filename : oat_filenames) { |
| 2330 | image_info_map_.emplace(oat_filename, ImageInfo()); |
| 2331 | } |
| 2332 | std::fill_n(image_methods_, arraysize(image_methods_), nullptr); |
Mathieu Chartier | 7fccc2d | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 2333 | CHECK_EQ(compile_app_image, !Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()) |
| 2334 | << "Compiling a boot image should occur iff there are no boot image spaces loaded"; |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2335 | } |
| 2336 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 2337 | ImageWriter::ImageInfo::ImageInfo() |
| 2338 | : intern_table_(new InternTable), |
| 2339 | class_table_(new ClassTable) {} |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2340 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2341 | } // namespace art |