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> |
| 20 | |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/logging.h" |
| 24 | #include "base/unix_file/fd_file.h" |
| 25 | #include "class_linker.h" |
| 26 | #include "compiled_method.h" |
| 27 | #include "dex_file-inl.h" |
| 28 | #include "driver/compiler_driver.h" |
| 29 | #include "elf_writer.h" |
| 30 | #include "gc/accounting/card_table-inl.h" |
| 31 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 32 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "gc/heap.h" |
| 34 | #include "gc/space/large_object_space.h" |
| 35 | #include "gc/space/space-inl.h" |
| 36 | #include "globals.h" |
| 37 | #include "image.h" |
| 38 | #include "intern_table.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 39 | #include "mirror/art_field-inl.h" |
| 40 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 41 | #include "mirror/array-inl.h" |
| 42 | #include "mirror/class-inl.h" |
| 43 | #include "mirror/class_loader.h" |
| 44 | #include "mirror/dex_cache-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | #include "mirror/object-inl.h" |
| 46 | #include "mirror/object_array-inl.h" |
| 47 | #include "oat.h" |
| 48 | #include "oat_file.h" |
| 49 | #include "object_utils.h" |
| 50 | #include "runtime.h" |
| 51 | #include "scoped_thread_state_change.h" |
| 52 | #include "sirt_ref.h" |
| 53 | #include "UniquePtr.h" |
| 54 | #include "utils.h" |
| 55 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 56 | using ::art::mirror::ArtField; |
| 57 | using ::art::mirror::ArtMethod; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 58 | using ::art::mirror::Class; |
| 59 | using ::art::mirror::DexCache; |
| 60 | using ::art::mirror::EntryPointFromInterpreter; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 61 | using ::art::mirror::Object; |
| 62 | using ::art::mirror::ObjectArray; |
| 63 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 64 | |
| 65 | namespace art { |
| 66 | |
| 67 | bool ImageWriter::Write(const std::string& image_filename, |
| 68 | uintptr_t image_begin, |
| 69 | const std::string& oat_filename, |
| 70 | const std::string& oat_location) { |
| 71 | CHECK(!image_filename.empty()); |
| 72 | |
| 73 | CHECK_NE(image_begin, 0U); |
| 74 | image_begin_ = reinterpret_cast<byte*>(image_begin); |
| 75 | |
| 76 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 77 | const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 78 | dex_caches_.insert(all_dex_caches.begin(), all_dex_caches.end()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 79 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 80 | UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 81 | if (oat_file.get() == NULL) { |
| 82 | LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location; |
| 83 | return false; |
| 84 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame^] | 85 | std::string error_msg; |
| 86 | oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg); |
| 87 | if (oat_file_ == nullptr) { |
| 88 | LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location |
| 89 | << ": " << error_msg; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame^] | 92 | CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 93 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 94 | interpreter_to_interpreter_bridge_offset_ = |
| 95 | oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset(); |
| 96 | interpreter_to_compiled_code_bridge_offset_ = |
| 97 | oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset(); |
| 98 | |
| 99 | jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset(); |
| 100 | |
| 101 | portable_resolution_trampoline_offset_ = |
| 102 | oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset(); |
| 103 | portable_to_interpreter_bridge_offset_ = |
| 104 | oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset(); |
| 105 | |
| 106 | quick_resolution_trampoline_offset_ = |
| 107 | oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset(); |
| 108 | quick_to_interpreter_bridge_offset_ = |
| 109 | oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 110 | { |
| 111 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 112 | PruneNonImageClasses(); // Remove junk |
| 113 | ComputeLazyFieldsForImageClasses(); // Add useful information |
| 114 | ComputeEagerResolvedStrings(); |
| 115 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 116 | } |
| 117 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 118 | heap->CollectGarbage(false); // Remove garbage. |
| 119 | // Trim size of alloc spaces. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 120 | for (const auto& space : heap->GetContinuousSpaces()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 121 | if (space->IsDlMallocSpace()) { |
| 122 | space->AsDlMallocSpace()->Trim(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (!AllocMemory()) { |
| 127 | return false; |
| 128 | } |
| 129 | #ifndef NDEBUG |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 130 | { // NOLINT(whitespace/braces) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 131 | ScopedObjectAccess soa(Thread::Current()); |
| 132 | CheckNonImageClassesRemoved(); |
| 133 | } |
| 134 | #endif |
| 135 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 136 | size_t oat_loaded_size = 0; |
| 137 | size_t oat_data_offset = 0; |
| 138 | ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset); |
| 139 | CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset); |
| 140 | CopyAndFixupObjects(); |
| 141 | PatchOatCodeAndMethods(); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 142 | // Record allocations into the image bitmap. |
| 143 | RecordImageAllocations(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 144 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 145 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 146 | UniquePtr<File> image_file(OS::CreateEmptyFile(image_filename.c_str())); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 147 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 148 | if (image_file.get() == NULL) { |
| 149 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 150 | return false; |
| 151 | } |
| 152 | if (fchmod(image_file->Fd(), 0644) != 0) { |
| 153 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
| 154 | return EXIT_FAILURE; |
| 155 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 156 | |
| 157 | // Write out the image. |
| 158 | CHECK_EQ(image_end_, image_header->GetImageSize()); |
| 159 | if (!image_file->WriteFully(image_->Begin(), image_end_)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 160 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 161 | return false; |
| 162 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 163 | |
| 164 | // Write out the image bitmap at the page aligned start of the image end. |
| 165 | CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize); |
| 166 | if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()), |
| 167 | image_header->GetImageBitmapSize(), |
| 168 | image_header->GetImageBitmapOffset())) { |
| 169 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 170 | return false; |
| 171 | } |
| 172 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 176 | void ImageWriter::RecordImageAllocations() { |
| 177 | uint64_t start_time = NanoTime(); |
| 178 | CHECK(image_bitmap_.get() != nullptr); |
| 179 | for (const auto& it : offsets_) { |
| 180 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + it.second); |
| 181 | DCHECK_ALIGNED(obj, kObjectAlignment); |
| 182 | image_bitmap_->Set(obj); |
| 183 | } |
| 184 | LOG(INFO) << "RecordImageAllocations took " << PrettyDuration(NanoTime() - start_time); |
| 185 | } |
| 186 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 187 | bool ImageWriter::AllocMemory() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 188 | size_t size = 0; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 189 | for (const auto& space : Runtime::Current()->GetHeap()->GetContinuousSpaces()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 190 | if (space->IsDlMallocSpace()) { |
| 191 | size += space->Size(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | int prot = PROT_READ | PROT_WRITE; |
| 196 | size_t length = RoundUp(size, kPageSize); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame^] | 197 | std::string error_msg; |
| 198 | image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, prot, &error_msg)); |
| 199 | if (UNLIKELY(image_.get() == nullptr)) { |
| 200 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
| 207 | Runtime* runtime = Runtime::Current(); |
| 208 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 209 | class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL); |
| 210 | } |
| 211 | |
| 212 | bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) { |
| 213 | c->ComputeName(); |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) { |
| 218 | if (!obj->GetClass()->IsStringClass()) { |
| 219 | return; |
| 220 | } |
| 221 | String* string = obj->AsString(); |
| 222 | const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset(); |
| 223 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 224 | for (DexCache* dex_cache : writer->dex_caches_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 225 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 226 | const DexFile::StringId* string_id = dex_file.FindStringId(utf16_string); |
| 227 | if (string_id != NULL) { |
| 228 | // This string occurs in this dex file, assign the dex cache entry. |
| 229 | uint32_t string_idx = dex_file.GetIndexForStringId(*string_id); |
| 230 | if (dex_cache->GetResolvedString(string_idx) == NULL) { |
| 231 | dex_cache->SetResolvedString(string_idx, string); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void ImageWriter::ComputeEagerResolvedStrings() |
| 238 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 239 | // TODO: Check image spaces only? |
| 240 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 241 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 242 | heap->FlushAllocStack(); |
| 243 | heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this); |
| 244 | } |
| 245 | |
| 246 | bool ImageWriter::IsImageClass(const Class* klass) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 247 | return compiler_driver_.IsImageClass(ClassHelper(klass).GetDescriptorAsStringPiece()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | struct NonImageClasses { |
| 251 | ImageWriter* image_writer; |
| 252 | std::set<std::string>* non_image_classes; |
| 253 | }; |
| 254 | |
| 255 | void ImageWriter::PruneNonImageClasses() { |
| 256 | if (compiler_driver_.GetImageClasses() == NULL) { |
| 257 | return; |
| 258 | } |
| 259 | Runtime* runtime = Runtime::Current(); |
| 260 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 261 | |
| 262 | // Make a list of classes we would like to prune. |
| 263 | std::set<std::string> non_image_classes; |
| 264 | NonImageClasses context; |
| 265 | context.image_writer = this; |
| 266 | context.non_image_classes = &non_image_classes; |
| 267 | class_linker->VisitClasses(NonImageClassesVisitor, &context); |
| 268 | |
| 269 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 270 | for (const std::string& it : non_image_classes) { |
| 271 | class_linker->RemoveClass(it.c_str(), NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Clear references to removed classes from the DexCaches. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 275 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 276 | for (DexCache* dex_cache : dex_caches_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 277 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 278 | Class* klass = dex_cache->GetResolvedType(i); |
| 279 | if (klass != NULL && !IsImageClass(klass)) { |
| 280 | dex_cache->SetResolvedType(i, NULL); |
| 281 | dex_cache->GetInitializedStaticStorage()->Set(i, NULL); |
| 282 | } |
| 283 | } |
| 284 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 285 | ArtMethod* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 286 | if (method != NULL && !IsImageClass(method->GetDeclaringClass())) { |
| 287 | dex_cache->SetResolvedMethod(i, resolution_method); |
| 288 | } |
| 289 | } |
| 290 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 291 | ArtField* field = dex_cache->GetResolvedField(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 292 | if (field != NULL && !IsImageClass(field->GetDeclaringClass())) { |
| 293 | dex_cache->SetResolvedField(i, NULL); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) { |
| 300 | NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg); |
| 301 | if (!context->image_writer->IsImageClass(klass)) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 302 | context->non_image_classes->insert(ClassHelper(klass).GetDescriptorAsStringPiece().as_string()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | void ImageWriter::CheckNonImageClassesRemoved() |
| 308 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 309 | if (compiler_driver_.GetImageClasses() == NULL) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 314 | Thread* self = Thread::Current(); |
| 315 | { |
| 316 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 317 | heap->FlushAllocStack(); |
| 318 | } |
| 319 | |
| 320 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 321 | heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this); |
| 322 | } |
| 323 | |
| 324 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 325 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
| 326 | if (!obj->IsClass()) { |
| 327 | return; |
| 328 | } |
| 329 | Class* klass = obj->AsClass(); |
| 330 | if (!image_writer->IsImageClass(klass)) { |
| 331 | image_writer->DumpImageClasses(); |
| 332 | CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor() |
| 333 | << " " << PrettyDescriptor(klass); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void ImageWriter::DumpImageClasses() { |
| 338 | CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses(); |
| 339 | CHECK(image_classes != NULL); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 340 | for (const std::string& image_class : *image_classes) { |
| 341 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) { |
| 346 | DCHECK(obj != NULL); |
| 347 | DCHECK(arg != NULL); |
| 348 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
| 349 | |
| 350 | // if it is a string, we want to intern it if its not interned. |
| 351 | if (obj->GetClass()->IsStringClass()) { |
| 352 | // we must be an interned string that was forward referenced and already assigned |
| 353 | if (image_writer->IsImageOffsetAssigned(obj)) { |
| 354 | DCHECK_EQ(obj, obj->AsString()->Intern()); |
| 355 | return; |
| 356 | } |
| 357 | SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern()); |
| 358 | if (obj != interned.get()) { |
| 359 | if (!image_writer->IsImageOffsetAssigned(interned.get())) { |
| 360 | // interned obj is after us, allocate its location early |
| 361 | image_writer->AssignImageOffset(interned.get()); |
| 362 | } |
| 363 | // point those looking for this object to the interned version. |
| 364 | image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get())); |
| 365 | return; |
| 366 | } |
| 367 | // else (obj == interned), nothing to do but fall through to the normal case |
| 368 | } |
| 369 | |
| 370 | image_writer->AssignImageOffset(obj); |
| 371 | } |
| 372 | |
| 373 | ObjectArray<Object>* ImageWriter::CreateImageRoots() const { |
| 374 | Runtime* runtime = Runtime::Current(); |
| 375 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 376 | Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;"); |
| 377 | Thread* self = Thread::Current(); |
| 378 | |
| 379 | // build an Object[] of all the DexCaches used in the source_space_ |
| 380 | ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class, |
| 381 | dex_caches_.size()); |
| 382 | int i = 0; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 383 | for (DexCache* dex_cache : dex_caches_) { |
| 384 | dex_caches->Set(i++, dex_cache); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // build an Object[] of the roots needed to restore the runtime |
| 388 | SirtRef<ObjectArray<Object> > |
| 389 | image_roots(self, |
| 390 | ObjectArray<Object>::Alloc(self, object_array_class, |
| 391 | ImageHeader::kImageRootsMax)); |
| 392 | image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod()); |
| 393 | image_roots->Set(ImageHeader::kCalleeSaveMethod, |
| 394 | runtime->GetCalleeSaveMethod(Runtime::kSaveAll)); |
| 395 | image_roots->Set(ImageHeader::kRefsOnlySaveMethod, |
| 396 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)); |
| 397 | image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod, |
| 398 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
| 399 | image_roots->Set(ImageHeader::kOatLocation, |
| 400 | String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str())); |
| 401 | image_roots->Set(ImageHeader::kDexCaches, |
| 402 | dex_caches); |
| 403 | image_roots->Set(ImageHeader::kClassRoots, |
| 404 | class_linker->GetClassRoots()); |
| 405 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 406 | CHECK(image_roots->Get(i) != NULL); |
| 407 | } |
| 408 | return image_roots.get(); |
| 409 | } |
| 410 | |
| 411 | void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) { |
| 412 | CHECK_NE(0U, oat_loaded_size); |
| 413 | Thread* self = Thread::Current(); |
| 414 | SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots()); |
| 415 | |
| 416 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 417 | const auto& spaces = heap->GetContinuousSpaces(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 418 | DCHECK(!spaces.empty()); |
| 419 | DCHECK_EQ(0U, image_end_); |
| 420 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 421 | // 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] | 422 | // know where image_roots is going to end up |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 423 | image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 424 | |
| 425 | { |
| 426 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 427 | heap->FlushAllocStack(); |
| 428 | // TODO: Image spaces only? |
| 429 | // TODO: Add InOrderWalk to heap bitmap. |
| 430 | const char* old = self->StartAssertNoThreadSuspension("ImageWriter"); |
| 431 | DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty()); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 432 | for (const auto& space : spaces) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 433 | space->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this); |
| 434 | DCHECK_LT(image_end_, image_->Size()); |
| 435 | } |
| 436 | self->EndAssertNoThreadSuspension(old); |
| 437 | } |
| 438 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 439 | // Create the image bitmap. |
| 440 | image_bitmap_.reset(gc::accounting::SpaceBitmap::Create("image bitmap", image_->Begin(), |
| 441 | image_end_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 442 | const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize); |
| 443 | const byte* oat_file_end = oat_file_begin + oat_loaded_size; |
| 444 | oat_data_begin_ = oat_file_begin + oat_data_offset; |
| 445 | const byte* oat_data_end = oat_data_begin_ + oat_file_->Size(); |
| 446 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 447 | // Return to write header at start of image with future location of image_roots. At this point, |
| 448 | // image_end_ is the size of the image (excluding bitmaps). |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 449 | ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 450 | static_cast<uint32_t>(image_end_), |
| 451 | RoundUp(image_end_, kPageSize), |
| 452 | image_bitmap_->Size(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 453 | reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())), |
| 454 | oat_file_->GetOatHeader().GetChecksum(), |
| 455 | reinterpret_cast<uint32_t>(oat_file_begin), |
| 456 | reinterpret_cast<uint32_t>(oat_data_begin_), |
| 457 | reinterpret_cast<uint32_t>(oat_data_end), |
| 458 | reinterpret_cast<uint32_t>(oat_file_end)); |
| 459 | memcpy(image_->Begin(), &image_header, sizeof(image_header)); |
| 460 | |
| 461 | // Note that image_end_ is left at end of used space |
| 462 | } |
| 463 | |
| 464 | void ImageWriter::CopyAndFixupObjects() |
| 465 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 466 | Thread* self = Thread::Current(); |
| 467 | const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter"); |
| 468 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 469 | // TODO: heap validation can't handle this fix up pass |
| 470 | heap->DisableObjectValidation(); |
| 471 | // TODO: Image spaces only? |
| 472 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 473 | heap->FlushAllocStack(); |
| 474 | heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this); |
| 475 | self->EndAssertNoThreadSuspension(old_cause); |
| 476 | } |
| 477 | |
| 478 | void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) { |
| 479 | DCHECK(object != NULL); |
| 480 | DCHECK(arg != NULL); |
| 481 | const Object* obj = object; |
| 482 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
| 483 | |
| 484 | // see GetLocalAddress for similar computation |
| 485 | size_t offset = image_writer->GetImageOffset(obj); |
| 486 | byte* dst = image_writer->image_->Begin() + offset; |
| 487 | const byte* src = reinterpret_cast<const byte*>(obj); |
| 488 | size_t n = obj->SizeOf(); |
| 489 | DCHECK_LT(offset + n, image_writer->image_->Size()); |
| 490 | memcpy(dst, src, n); |
| 491 | Object* copy = reinterpret_cast<Object*>(dst); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 492 | copy->SetField32(Object::MonitorOffset(), 0, false); // We may have inflated the lock during compilation. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 493 | image_writer->FixupObject(obj, copy); |
| 494 | } |
| 495 | |
| 496 | void ImageWriter::FixupObject(const Object* orig, Object* copy) { |
| 497 | DCHECK(orig != NULL); |
| 498 | DCHECK(copy != NULL); |
| 499 | copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass()))); |
| 500 | // TODO: special case init of pointers to malloc data (or removal of these pointers) |
| 501 | if (orig->IsClass()) { |
| 502 | FixupClass(orig->AsClass(), down_cast<Class*>(copy)); |
| 503 | } else if (orig->IsObjectArray()) { |
| 504 | FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 505 | } else if (orig->IsArtMethod()) { |
| 506 | FixupMethod(orig->AsArtMethod(), down_cast<ArtMethod*>(copy)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 507 | } else { |
| 508 | FixupInstanceFields(orig, copy); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | void ImageWriter::FixupClass(const Class* orig, Class* copy) { |
| 513 | FixupInstanceFields(orig, copy); |
| 514 | FixupStaticFields(orig, copy); |
| 515 | } |
| 516 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 517 | void ImageWriter::FixupMethod(const ArtMethod* orig, ArtMethod* copy) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 518 | FixupInstanceFields(orig, copy); |
| 519 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 520 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 521 | // oat_begin_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 522 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 523 | // The resolution method has a special trampoline to call. |
| 524 | if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 525 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 526 | copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_)); |
| 527 | #else |
| 528 | copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_)); |
| 529 | #endif |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 530 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 531 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 532 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 533 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 534 | if (UNLIKELY(orig->IsAbstract())) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 535 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 536 | copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_to_interpreter_bridge_offset_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 537 | #else |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 538 | copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_to_interpreter_bridge_offset_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 539 | #endif |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 540 | copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*> |
| 541 | (GetOatAddress(interpreter_to_interpreter_bridge_offset_))); |
| 542 | } else { |
| 543 | copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*> |
| 544 | (GetOatAddress(interpreter_to_compiled_code_bridge_offset_))); |
| 545 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 546 | // trampoline. |
| 547 | const byte* code = GetOatAddress(orig->GetOatCodeOffset()); |
| 548 | if (code != NULL) { |
| 549 | copy->SetEntryPointFromCompiledCode(code); |
| 550 | } else { |
| 551 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 552 | copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_)); |
| 553 | #else |
| 554 | copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_)); |
| 555 | #endif |
| 556 | } |
| 557 | if (orig->IsNative()) { |
| 558 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 559 | // Note this is not the code_ pointer, that is handled above. |
| 560 | copy->SetNativeMethod(GetOatAddress(jni_dlsym_lookup_offset_)); |
| 561 | } else { |
| 562 | // Normal (non-abstract non-native) methods have various tables to relocate. |
| 563 | uint32_t mapping_table_off = orig->GetOatMappingTableOffset(); |
| 564 | const byte* mapping_table = GetOatAddress(mapping_table_off); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 565 | copy->SetMappingTable(mapping_table); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 566 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 567 | uint32_t vmap_table_offset = orig->GetOatVmapTableOffset(); |
| 568 | const byte* vmap_table = GetOatAddress(vmap_table_offset); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 569 | copy->SetVmapTable(vmap_table); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 570 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 571 | uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset(); |
| 572 | const byte* native_gc_map = GetOatAddress(native_gc_map_offset); |
| 573 | copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map)); |
| 574 | } |
| 575 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) { |
| 580 | for (int32_t i = 0; i < orig->GetLength(); ++i) { |
| 581 | const Object* element = orig->Get(i); |
| 582 | copy->SetPtrWithoutChecks(i, GetImageAddress(element)); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) { |
| 587 | DCHECK(orig != NULL); |
| 588 | DCHECK(copy != NULL); |
| 589 | Class* klass = orig->GetClass(); |
| 590 | DCHECK(klass != NULL); |
| 591 | FixupFields(orig, |
| 592 | copy, |
| 593 | klass->GetReferenceInstanceOffsets(), |
| 594 | false); |
| 595 | } |
| 596 | |
| 597 | void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) { |
| 598 | DCHECK(orig != NULL); |
| 599 | DCHECK(copy != NULL); |
| 600 | FixupFields(orig, |
| 601 | copy, |
| 602 | orig->GetReferenceStaticOffsets(), |
| 603 | true); |
| 604 | } |
| 605 | |
| 606 | void ImageWriter::FixupFields(const Object* orig, |
| 607 | Object* copy, |
| 608 | uint32_t ref_offsets, |
| 609 | bool is_static) { |
| 610 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 611 | // Found a reference offset bitmap. Fixup the specified offsets. |
| 612 | while (ref_offsets != 0) { |
| 613 | size_t right_shift = CLZ(ref_offsets); |
| 614 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 615 | const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false); |
| 616 | // Use SetFieldPtr to avoid card marking since we are writing to the image. |
| 617 | copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false); |
| 618 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 619 | } |
| 620 | } else { |
| 621 | // There is no reference offset bitmap. In the non-static case, |
| 622 | // walk up the class inheritance hierarchy and find reference |
| 623 | // offsets the hard way. In the static case, just consider this |
| 624 | // class. |
| 625 | for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass(); |
| 626 | klass != NULL; |
| 627 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 628 | size_t num_reference_fields = (is_static |
| 629 | ? klass->NumReferenceStaticFields() |
| 630 | : klass->NumReferenceInstanceFields()); |
| 631 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 632 | ArtField* field = (is_static |
| 633 | ? klass->GetStaticField(i) |
| 634 | : klass->GetInstanceField(i)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 635 | MemberOffset field_offset = field->GetOffset(); |
| 636 | const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false); |
| 637 | // Use SetFieldPtr to avoid card marking since we are writing to the image. |
| 638 | copy->SetFieldPtr(field_offset, GetImageAddress(ref), false); |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | if (!is_static && orig->IsReferenceInstance()) { |
| 643 | // Fix-up referent, that isn't marked as an object field, for References. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 644 | ArtField* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 645 | MemberOffset field_offset = field->GetOffset(); |
| 646 | const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false); |
| 647 | // Use SetFieldPtr to avoid card marking since we are writing to the image. |
| 648 | copy->SetFieldPtr(field_offset, GetImageAddress(ref), false); |
| 649 | } |
| 650 | } |
| 651 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 652 | static ArtMethod* GetTargetMethod(const CompilerDriver::PatchInformation* patch) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 653 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 654 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 655 | DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 656 | ArtMethod* method = class_linker->ResolveMethod(patch->GetDexFile(), |
| 657 | patch->GetTargetMethodIdx(), |
| 658 | dex_cache, |
| 659 | NULL, |
| 660 | NULL, |
| 661 | patch->GetTargetInvokeType()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 662 | CHECK(method != NULL) |
| 663 | << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx(); |
| 664 | CHECK(!method->IsRuntimeMethod()) |
| 665 | << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx(); |
| 666 | CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method) |
| 667 | << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " " |
| 668 | << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " " |
| 669 | << PrettyMethod(method); |
| 670 | return method; |
| 671 | } |
| 672 | |
| 673 | void ImageWriter::PatchOatCodeAndMethods() { |
| 674 | Thread* self = Thread::Current(); |
| 675 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 676 | const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter"); |
| 677 | |
| 678 | typedef std::vector<const CompilerDriver::PatchInformation*> Patches; |
| 679 | const Patches& code_to_patch = compiler_driver_.GetCodeToPatch(); |
| 680 | for (size_t i = 0; i < code_to_patch.size(); i++) { |
| 681 | const CompilerDriver::PatchInformation* patch = code_to_patch[i]; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 682 | ArtMethod* target = GetTargetMethod(patch); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 683 | uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target)); |
| 684 | uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader()); |
| 685 | uint32_t code_offset = code - code_base; |
| 686 | SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset))); |
| 687 | } |
| 688 | |
| 689 | const Patches& methods_to_patch = compiler_driver_.GetMethodsToPatch(); |
| 690 | for (size_t i = 0; i < methods_to_patch.size(); i++) { |
| 691 | const CompilerDriver::PatchInformation* patch = methods_to_patch[i]; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 692 | ArtMethod* target = GetTargetMethod(patch); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 693 | SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target))); |
| 694 | } |
| 695 | |
| 696 | // Update the image header with the new checksum after patching |
| 697 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 698 | image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum()); |
| 699 | self->EndAssertNoThreadSuspension(old_cause); |
| 700 | } |
| 701 | |
| 702 | void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) { |
| 703 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 704 | const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 705 | patch->GetReferrerClassDefIdx(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 706 | patch->GetReferrerMethodIdx()); |
| 707 | OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader()); |
| 708 | // TODO: make this Thumb2 specific |
| 709 | uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1); |
| 710 | uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset()); |
| 711 | #ifndef NDEBUG |
| 712 | const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx()); |
| 713 | uint32_t expected = reinterpret_cast<uint32_t>(&id); |
| 714 | uint32_t actual = *patch_location; |
| 715 | CHECK(actual == expected || actual == value) << std::hex |
| 716 | << "actual=" << actual |
| 717 | << "expected=" << expected |
| 718 | << "value=" << value; |
| 719 | #endif |
| 720 | *patch_location = value; |
| 721 | oat_header.UpdateChecksum(patch_location, sizeof(value)); |
| 722 | } |
| 723 | |
| 724 | } // namespace art |