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 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include "base/logging.h" |
| 25 | #include "base/unix_file/fd_file.h" |
| 26 | #include "class_linker.h" |
| 27 | #include "compiled_method.h" |
| 28 | #include "dex_file-inl.h" |
| 29 | #include "driver/compiler_driver.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 30 | #include "elf_file.h" |
| 31 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 32 | #include "elf_writer.h" |
| 33 | #include "gc/accounting/card_table-inl.h" |
| 34 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 35 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | #include "gc/heap.h" |
| 37 | #include "gc/space/large_object_space.h" |
| 38 | #include "gc/space/space-inl.h" |
| 39 | #include "globals.h" |
| 40 | #include "image.h" |
| 41 | #include "intern_table.h" |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 42 | #include "lock_word.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 43 | #include "mirror/art_field-inl.h" |
| 44 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | #include "mirror/array-inl.h" |
| 46 | #include "mirror/class-inl.h" |
| 47 | #include "mirror/class_loader.h" |
| 48 | #include "mirror/dex_cache-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 49 | #include "mirror/object-inl.h" |
| 50 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 51 | #include "mirror/string-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 52 | #include "oat.h" |
| 53 | #include "oat_file.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 54 | #include "runtime.h" |
| 55 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 56 | #include "handle_scope-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 57 | #include "utils.h" |
| 58 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 59 | using ::art::mirror::ArtField; |
| 60 | using ::art::mirror::ArtMethod; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 61 | using ::art::mirror::Class; |
| 62 | using ::art::mirror::DexCache; |
| 63 | using ::art::mirror::EntryPointFromInterpreter; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 64 | using ::art::mirror::Object; |
| 65 | using ::art::mirror::ObjectArray; |
| 66 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 67 | |
| 68 | namespace art { |
| 69 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 70 | bool ImageWriter::PrepareImageAddressSpace() { |
| 71 | { |
| 72 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 73 | PruneNonImageClasses(); // Remove junk |
| 74 | ComputeLazyFieldsForImageClasses(); // Add useful information |
| 75 | ComputeEagerResolvedStrings(); |
| 76 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 77 | } |
| 78 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 79 | heap->CollectGarbage(false); // Remove garbage. |
| 80 | |
| 81 | if (!AllocMemory()) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | if (kIsDebugBuild) { |
| 86 | ScopedObjectAccess soa(Thread::Current()); |
| 87 | CheckNonImageClassesRemoved(); |
| 88 | } |
| 89 | |
| 90 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 91 | CalculateNewObjectOffsets(); |
| 92 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 97 | bool ImageWriter::Write(const std::string& image_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 98 | const std::string& oat_filename, |
| 99 | const std::string& oat_location) { |
| 100 | CHECK(!image_filename.empty()); |
| 101 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 102 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 103 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 104 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 105 | if (oat_file.get() == NULL) { |
| 106 | LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location; |
| 107 | return false; |
| 108 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 109 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 110 | oat_file_ = OatFile::OpenReadable(oat_file.get(), oat_location, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 111 | if (oat_file_ == nullptr) { |
| 112 | LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location |
| 113 | << ": " << error_msg; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 116 | CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 117 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 118 | interpreter_to_interpreter_bridge_offset_ = |
| 119 | oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset(); |
| 120 | interpreter_to_compiled_code_bridge_offset_ = |
| 121 | oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset(); |
| 122 | |
| 123 | jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset(); |
| 124 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 125 | portable_imt_conflict_trampoline_offset_ = |
| 126 | oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 127 | portable_resolution_trampoline_offset_ = |
| 128 | oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset(); |
| 129 | portable_to_interpreter_bridge_offset_ = |
| 130 | oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset(); |
| 131 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 132 | quick_generic_jni_trampoline_offset_ = |
| 133 | oat_file_->GetOatHeader().GetQuickGenericJniTrampolineOffset(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 134 | quick_imt_conflict_trampoline_offset_ = |
| 135 | oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 136 | quick_resolution_trampoline_offset_ = |
| 137 | oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset(); |
| 138 | quick_to_interpreter_bridge_offset_ = |
| 139 | oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 140 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 141 | size_t oat_loaded_size = 0; |
| 142 | size_t oat_data_offset = 0; |
| 143 | ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 144 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 145 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 146 | CreateHeader(oat_loaded_size, oat_data_offset); |
| 147 | CopyAndFixupObjects(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 148 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 149 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 150 | SetOatChecksumFromElfFile(oat_file.get()); |
| 151 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 152 | std::unique_ptr<File> image_file(OS::CreateEmptyFile(image_filename.c_str())); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 153 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 154 | if (image_file.get() == NULL) { |
| 155 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 156 | return false; |
| 157 | } |
| 158 | if (fchmod(image_file->Fd(), 0644) != 0) { |
| 159 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
| 160 | return EXIT_FAILURE; |
| 161 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 162 | |
| 163 | // Write out the image. |
| 164 | CHECK_EQ(image_end_, image_header->GetImageSize()); |
| 165 | if (!image_file->WriteFully(image_->Begin(), image_end_)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 166 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 167 | return false; |
| 168 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 169 | |
| 170 | // Write out the image bitmap at the page aligned start of the image end. |
| 171 | CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize); |
| 172 | if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()), |
| 173 | image_header->GetImageBitmapSize(), |
| 174 | image_header->GetImageBitmapOffset())) { |
| 175 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 176 | return false; |
| 177 | } |
| 178 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 182 | void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) { |
| 183 | DCHECK(object != nullptr); |
| 184 | DCHECK_NE(offset, 0U); |
| 185 | DCHECK(!IsImageOffsetAssigned(object)); |
| 186 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset); |
| 187 | DCHECK_ALIGNED(obj, kObjectAlignment); |
| 188 | image_bitmap_->Set(obj); |
| 189 | // Before we stomp over the lock word, save the hash code for later. |
| 190 | Monitor::Deflate(Thread::Current(), object);; |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 191 | LockWord lw(object->GetLockWord(false)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 192 | switch (lw.GetState()) { |
| 193 | case LockWord::kFatLocked: { |
| 194 | LOG(FATAL) << "Fat locked object " << obj << " found during object copy"; |
| 195 | break; |
| 196 | } |
| 197 | case LockWord::kThinLocked: { |
| 198 | LOG(FATAL) << "Thin locked object " << obj << " found during object copy"; |
| 199 | break; |
| 200 | } |
| 201 | case LockWord::kUnlocked: |
| 202 | // No hash, don't need to save it. |
| 203 | break; |
| 204 | case LockWord::kHashCode: |
| 205 | saved_hashes_.push_back(std::make_pair(obj, lw.GetHashCode())); |
| 206 | break; |
| 207 | default: |
| 208 | LOG(FATAL) << "Unreachable."; |
| 209 | break; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 210 | } |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 211 | object->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 212 | DCHECK(IsImageOffsetAssigned(object)); |
| 213 | } |
| 214 | |
| 215 | void ImageWriter::AssignImageOffset(mirror::Object* object) { |
| 216 | DCHECK(object != nullptr); |
| 217 | SetImageOffset(object, image_end_); |
| 218 | image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment |
| 219 | DCHECK_LT(image_end_, image_->Size()); |
| 220 | } |
| 221 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 222 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 223 | DCHECK(object != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 224 | return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 227 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 228 | DCHECK(object != nullptr); |
| 229 | DCHECK(IsImageOffsetAssigned(object)); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 230 | LockWord lock_word = object->GetLockWord(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 231 | size_t offset = lock_word.ForwardingAddress(); |
| 232 | DCHECK_LT(offset, image_end_); |
| 233 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 236 | bool ImageWriter::AllocMemory() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 237 | size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 238 | std::string error_msg; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 239 | image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, PROT_READ | PROT_WRITE, |
Ian Rogers | 3cd86d6 | 2014-08-14 08:53:12 -0700 | [diff] [blame] | 240 | false, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 241 | if (UNLIKELY(image_.get() == nullptr)) { |
| 242 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 245 | |
| 246 | // Create the image bitmap. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 247 | image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create("image bitmap", image_->Begin(), |
| 248 | length)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 249 | if (image_bitmap_.get() == nullptr) { |
| 250 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 251 | return false; |
| 252 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
| 256 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 257 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 258 | class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL); |
| 259 | } |
| 260 | |
| 261 | bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 262 | Thread* self = Thread::Current(); |
| 263 | StackHandleScope<1> hs(self); |
| 264 | mirror::Class::ComputeName(hs.NewHandle(c)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | |
| 268 | void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) { |
| 269 | if (!obj->GetClass()->IsStringClass()) { |
| 270 | return; |
| 271 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 272 | mirror::String* string = obj->AsString(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 274 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 275 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 276 | size_t dex_cache_count = class_linker->GetDexCacheCount(); |
| 277 | for (size_t i = 0; i < dex_cache_count; ++i) { |
| 278 | DexCache* dex_cache = class_linker->GetDexCache(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 279 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 24c534d | 2013-11-14 00:15:00 -0800 | [diff] [blame] | 280 | const DexFile::StringId* string_id; |
| 281 | if (UNLIKELY(string->GetLength() == 0)) { |
| 282 | string_id = dex_file.FindStringId(""); |
| 283 | } else { |
| 284 | string_id = dex_file.FindStringId(utf16_string); |
| 285 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 286 | if (string_id != nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 287 | // This string occurs in this dex file, assign the dex cache entry. |
| 288 | uint32_t string_idx = dex_file.GetIndexForStringId(*string_id); |
| 289 | if (dex_cache->GetResolvedString(string_idx) == NULL) { |
| 290 | dex_cache->SetResolvedString(string_idx, string); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 296 | void ImageWriter::ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 297 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 298 | Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 301 | bool ImageWriter::IsImageClass(Class* klass) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 302 | std::string temp; |
| 303 | return compiler_driver_.IsImageClass(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | struct NonImageClasses { |
| 307 | ImageWriter* image_writer; |
| 308 | std::set<std::string>* non_image_classes; |
| 309 | }; |
| 310 | |
| 311 | void ImageWriter::PruneNonImageClasses() { |
| 312 | if (compiler_driver_.GetImageClasses() == NULL) { |
| 313 | return; |
| 314 | } |
| 315 | Runtime* runtime = Runtime::Current(); |
| 316 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 317 | |
| 318 | // Make a list of classes we would like to prune. |
| 319 | std::set<std::string> non_image_classes; |
| 320 | NonImageClasses context; |
| 321 | context.image_writer = this; |
| 322 | context.non_image_classes = &non_image_classes; |
| 323 | class_linker->VisitClasses(NonImageClassesVisitor, &context); |
| 324 | |
| 325 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 326 | for (const std::string& it : non_image_classes) { |
| 327 | class_linker->RemoveClass(it.c_str(), NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Clear references to removed classes from the DexCaches. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 331 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 332 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 333 | size_t dex_cache_count = class_linker->GetDexCacheCount(); |
| 334 | for (size_t idx = 0; idx < dex_cache_count; ++idx) { |
| 335 | DexCache* dex_cache = class_linker->GetDexCache(idx); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 336 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 337 | Class* klass = dex_cache->GetResolvedType(i); |
| 338 | if (klass != NULL && !IsImageClass(klass)) { |
| 339 | dex_cache->SetResolvedType(i, NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 343 | ArtMethod* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 344 | if (method != NULL && !IsImageClass(method->GetDeclaringClass())) { |
| 345 | dex_cache->SetResolvedMethod(i, resolution_method); |
| 346 | } |
| 347 | } |
| 348 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 349 | ArtField* field = dex_cache->GetResolvedField(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 350 | if (field != NULL && !IsImageClass(field->GetDeclaringClass())) { |
| 351 | dex_cache->SetResolvedField(i, NULL); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) { |
| 358 | NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg); |
| 359 | if (!context->image_writer->IsImageClass(klass)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 360 | std::string temp; |
| 361 | context->non_image_classes->insert(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 362 | } |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | void ImageWriter::CheckNonImageClassesRemoved() |
| 367 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 368 | if (compiler_driver_.GetImageClasses() != nullptr) { |
| 369 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 370 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 371 | heap->VisitObjects(CheckNonImageClassesRemovedCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 372 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 376 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 377 | if (obj->IsClass()) { |
| 378 | Class* klass = obj->AsClass(); |
| 379 | if (!image_writer->IsImageClass(klass)) { |
| 380 | image_writer->DumpImageClasses(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 381 | std::string temp; |
| 382 | CHECK(image_writer->IsImageClass(klass)) << klass->GetDescriptor(&temp) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 383 | << " " << PrettyDescriptor(klass); |
| 384 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | void ImageWriter::DumpImageClasses() { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 389 | const std::set<std::string>* image_classes = compiler_driver_.GetImageClasses(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 390 | CHECK(image_classes != NULL); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 391 | for (const std::string& image_class : *image_classes) { |
| 392 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 396 | void ImageWriter::CalculateObjectOffsets(Object* obj) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 397 | DCHECK(obj != NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 398 | // if it is a string, we want to intern it if its not interned. |
| 399 | if (obj->GetClass()->IsStringClass()) { |
| 400 | // we must be an interned string that was forward referenced and already assigned |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 401 | if (IsImageOffsetAssigned(obj)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 402 | DCHECK_EQ(obj, obj->AsString()->Intern()); |
| 403 | return; |
| 404 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 405 | mirror::String* const interned = obj->AsString()->Intern(); |
| 406 | if (obj != interned) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 407 | if (!IsImageOffsetAssigned(interned)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 408 | // interned obj is after us, allocate its location early |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 409 | AssignImageOffset(interned); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 410 | } |
| 411 | // point those looking for this object to the interned version. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 412 | SetImageOffset(obj, GetImageOffset(interned)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | // else (obj == interned), nothing to do but fall through to the normal case |
| 416 | } |
| 417 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 418 | AssignImageOffset(obj); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | ObjectArray<Object>* ImageWriter::CreateImageRoots() const { |
| 422 | Runtime* runtime = Runtime::Current(); |
| 423 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 424 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 425 | StackHandleScope<3> hs(self); |
| 426 | Handle<Class> object_array_class(hs.NewHandle( |
| 427 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 428 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 429 | // build an Object[] of all the DexCaches used in the source_space_. |
| 430 | // Since we can't hold the dex lock when allocating the dex_caches |
| 431 | // ObjectArray, we lock the dex lock twice, first to get the number |
| 432 | // of dex caches first and then lock it again to copy the dex |
| 433 | // caches. We check that the number of dex caches does not change. |
| 434 | size_t dex_cache_count; |
| 435 | { |
| 436 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 437 | dex_cache_count = class_linker->GetDexCacheCount(); |
| 438 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 439 | Handle<ObjectArray<Object>> dex_caches( |
| 440 | hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(), |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 441 | dex_cache_count))); |
| 442 | CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array."; |
| 443 | { |
| 444 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 445 | CHECK_EQ(dex_cache_count, class_linker->GetDexCacheCount()) |
| 446 | << "The number of dex caches changed."; |
| 447 | for (size_t i = 0; i < dex_cache_count; ++i) { |
| 448 | dex_caches->Set<false>(i, class_linker->GetDexCache(i)); |
| 449 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // build an Object[] of the roots needed to restore the runtime |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 453 | Handle<ObjectArray<Object>> image_roots(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 454 | ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 455 | image_roots->Set<false>(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod()); |
| 456 | image_roots->Set<false>(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod()); |
| 457 | image_roots->Set<false>(ImageHeader::kDefaultImt, runtime->GetDefaultImt()); |
| 458 | image_roots->Set<false>(ImageHeader::kCalleeSaveMethod, |
| 459 | runtime->GetCalleeSaveMethod(Runtime::kSaveAll)); |
| 460 | image_roots->Set<false>(ImageHeader::kRefsOnlySaveMethod, |
| 461 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)); |
| 462 | image_roots->Set<false>(ImageHeader::kRefsAndArgsSaveMethod, |
| 463 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 464 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 465 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 466 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 467 | CHECK(image_roots->Get(i) != NULL); |
| 468 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 469 | return image_roots.Get(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 472 | // Walk instance fields of the given Class. Separate function to allow recursion on the super |
| 473 | // class. |
| 474 | void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) { |
| 475 | // Visit fields of parent classes first. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 476 | StackHandleScope<1> hs(Thread::Current()); |
| 477 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 478 | mirror::Class* super = h_class->GetSuperClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 479 | if (super != nullptr) { |
| 480 | WalkInstanceFields(obj, super); |
| 481 | } |
| 482 | // |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 483 | size_t num_reference_fields = h_class->NumReferenceInstanceFields(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 484 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 485 | mirror::ArtField* field = h_class->GetInstanceField(i); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 486 | MemberOffset field_offset = field->GetOffset(); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 487 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 488 | if (value != nullptr) { |
| 489 | WalkFieldsInOrder(value); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // For an unvisited object, visit it then all its children found via fields. |
| 495 | void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) { |
| 496 | if (!IsImageOffsetAssigned(obj)) { |
| 497 | // Walk instance fields of all objects |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 498 | StackHandleScope<2> hs(Thread::Current()); |
| 499 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
| 500 | Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 501 | // visit the object itself. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 502 | CalculateObjectOffsets(h_obj.Get()); |
| 503 | WalkInstanceFields(h_obj.Get(), klass.Get()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 504 | // Walk static fields of a Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 505 | if (h_obj->IsClass()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 506 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
| 507 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 508 | mirror::ArtField* field = klass->GetStaticField(i); |
| 509 | MemberOffset field_offset = field->GetOffset(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 510 | mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 511 | if (value != nullptr) { |
| 512 | WalkFieldsInOrder(value); |
| 513 | } |
| 514 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 515 | } else if (h_obj->IsObjectArray()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 516 | // Walk elements of an object array. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 517 | int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 518 | for (int32_t i = 0; i < length; i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 519 | mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 520 | mirror::Object* value = obj_array->Get(i); |
| 521 | if (value != nullptr) { |
| 522 | WalkFieldsInOrder(value); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) { |
| 530 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 531 | DCHECK(writer != nullptr); |
| 532 | writer->WalkFieldsInOrder(obj); |
| 533 | } |
| 534 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 535 | void ImageWriter::CalculateNewObjectOffsets() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 536 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 537 | StackHandleScope<1> hs(self); |
| 538 | Handle<ObjectArray<Object>> image_roots(hs.NewHandle(CreateImageRoots())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 539 | |
| 540 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 541 | DCHECK_EQ(0U, image_end_); |
| 542 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 543 | // 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] | 544 | // know where image_roots is going to end up |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 545 | image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 546 | |
| 547 | { |
| 548 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 549 | // TODO: Image spaces only? |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 550 | DCHECK_LT(image_end_, image_->Size()); |
| 551 | // Clear any pre-existing monitors which may have been in the monitor words. |
| 552 | heap->VisitObjects(WalkFieldsCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 555 | image_roots_address_ = PointerToLowMemUInt32(GetImageAddress(image_roots.Get())); |
| 556 | |
| 557 | // Note that image_end_ is left at end of used space |
| 558 | } |
| 559 | |
| 560 | void ImageWriter::CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) { |
| 561 | CHECK_NE(0U, oat_loaded_size); |
| 562 | const byte* oat_file_begin = GetOatFileBegin(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 563 | const byte* oat_file_end = oat_file_begin + oat_loaded_size; |
| 564 | oat_data_begin_ = oat_file_begin + oat_data_offset; |
| 565 | const byte* oat_data_end = oat_data_begin_ + oat_file_->Size(); |
| 566 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 567 | // Return to write header at start of image with future location of image_roots. At this point, |
| 568 | // image_end_ is the size of the image (excluding bitmaps). |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 569 | const size_t heap_bytes_per_bitmap_byte = kBitsPerByte * kObjectAlignment; |
Mathieu Chartier | 12aeccd | 2013-11-13 15:52:06 -0800 | [diff] [blame] | 570 | const size_t bitmap_bytes = RoundUp(image_end_, heap_bytes_per_bitmap_byte) / |
| 571 | heap_bytes_per_bitmap_byte; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 572 | new (image_->Begin()) ImageHeader(PointerToLowMemUInt32(image_begin_), |
| 573 | static_cast<uint32_t>(image_end_), |
| 574 | RoundUp(image_end_, kPageSize), |
| 575 | RoundUp(bitmap_bytes, kPageSize), |
| 576 | image_roots_address_, |
| 577 | oat_file_->GetOatHeader().GetChecksum(), |
| 578 | PointerToLowMemUInt32(oat_file_begin), |
| 579 | PointerToLowMemUInt32(oat_data_begin_), |
| 580 | PointerToLowMemUInt32(oat_data_end), |
| 581 | PointerToLowMemUInt32(oat_file_end)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 584 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 585 | void ImageWriter::CopyAndFixupObjects() |
| 586 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 587 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "ImageWriter"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 588 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 589 | // TODO: heap validation can't handle this fix up pass |
| 590 | heap->DisableObjectValidation(); |
| 591 | // TODO: Image spaces only? |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 592 | WriterMutexLock mu(ants.Self(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 593 | heap->VisitObjects(CopyAndFixupObjectsCallback, this); |
| 594 | // Fix up the object previously had hash codes. |
| 595 | for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 596 | hash_pair.first->SetLockWord(LockWord::FromHashCode(hash_pair.second), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 597 | } |
| 598 | saved_hashes_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 601 | void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 602 | DCHECK(obj != nullptr); |
| 603 | DCHECK(arg != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 604 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 605 | // see GetLocalAddress for similar computation |
| 606 | size_t offset = image_writer->GetImageOffset(obj); |
| 607 | byte* dst = image_writer->image_->Begin() + offset; |
| 608 | const byte* src = reinterpret_cast<const byte*>(obj); |
| 609 | size_t n = obj->SizeOf(); |
| 610 | DCHECK_LT(offset + n, image_writer->image_->Size()); |
| 611 | memcpy(dst, src, n); |
| 612 | Object* copy = reinterpret_cast<Object*>(dst); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 613 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 614 | // word. |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 615 | copy->SetLockWord(LockWord(), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 616 | image_writer->FixupObject(obj, copy); |
| 617 | } |
| 618 | |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 619 | class FixupVisitor { |
| 620 | public: |
| 621 | FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) { |
| 622 | } |
| 623 | |
| 624 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const |
| 625 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 626 | Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 627 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 628 | // image. |
| 629 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 630 | offset, image_writer_->GetImageAddress(ref)); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // java.lang.ref.Reference visitor. |
| 634 | void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const |
| 635 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 636 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 637 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 638 | mirror::Reference::ReferentOffset(), image_writer_->GetImageAddress(ref->GetReferent())); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 641 | protected: |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 642 | ImageWriter* const image_writer_; |
| 643 | mirror::Object* const copy_; |
| 644 | }; |
| 645 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 646 | class FixupClassVisitor FINAL : public FixupVisitor { |
| 647 | public: |
| 648 | FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) { |
| 649 | } |
| 650 | |
| 651 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const |
| 652 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 653 | DCHECK(obj->IsClass()); |
| 654 | FixupVisitor::operator()(obj, offset, false); |
| 655 | |
| 656 | if (offset.Uint32Value() < mirror::Class::EmbeddedVTableOffset().Uint32Value()) { |
| 657 | return; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const |
| 662 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 663 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 664 | LOG(FATAL) << "Reference not expected here."; |
| 665 | } |
| 666 | }; |
| 667 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 668 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 669 | DCHECK(orig != nullptr); |
| 670 | DCHECK(copy != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 671 | if (kUseBakerOrBrooksReadBarrier) { |
| 672 | orig->AssertReadBarrierPointer(); |
| 673 | if (kUseBrooksReadBarrier) { |
| 674 | // Note the address 'copy' isn't the same as the image address of 'orig'. |
| 675 | copy->SetReadBarrierPointer(GetImageAddress(orig)); |
| 676 | DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig)); |
| 677 | } |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 678 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 679 | if (orig->IsClass() && orig->AsClass()->ShouldHaveEmbeddedImtAndVTable()) { |
| 680 | FixupClassVisitor visitor(this, copy); |
| 681 | orig->VisitReferences<true /*visit class*/>(visitor, visitor); |
| 682 | } else { |
| 683 | FixupVisitor visitor(this, copy); |
| 684 | orig->VisitReferences<true /*visit class*/>(visitor, visitor); |
| 685 | } |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 686 | if (orig->IsArtMethod<kVerifyNone>()) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 687 | FixupMethod(orig->AsArtMethod<kVerifyNone>(), down_cast<ArtMethod*>(copy)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 691 | const byte* ImageWriter::GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted) { |
| 692 | DCHECK(!method->IsResolutionMethod() && !method->IsImtConflictMethod() && |
| 693 | !method->IsAbstract()) << PrettyMethod(method); |
| 694 | |
| 695 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 696 | // trampoline. |
| 697 | |
| 698 | // Quick entrypoint: |
| 699 | const byte* quick_code = GetOatAddress(method->GetQuickOatCodeOffset()); |
| 700 | *quick_is_interpreted = false; |
| 701 | if (quick_code != nullptr && |
| 702 | (!method->IsStatic() || method->IsConstructor() || method->GetDeclaringClass()->IsInitialized())) { |
| 703 | // We have code for a non-static or initialized method, just use the code. |
| 704 | } else if (quick_code == nullptr && method->IsNative() && |
| 705 | (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) { |
| 706 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
| 707 | quick_code = GetOatAddress(quick_generic_jni_trampoline_offset_); |
| 708 | } else if (quick_code == nullptr && !method->IsNative()) { |
| 709 | // We don't have code at all for a non-native method, use the interpreter. |
| 710 | quick_code = GetOatAddress(quick_to_interpreter_bridge_offset_); |
| 711 | *quick_is_interpreted = true; |
| 712 | } else { |
| 713 | CHECK(!method->GetDeclaringClass()->IsInitialized()); |
| 714 | // We have code for a static method, but need to go through the resolution stub for class |
| 715 | // initialization. |
| 716 | quick_code = GetOatAddress(quick_resolution_trampoline_offset_); |
| 717 | } |
| 718 | return quick_code; |
| 719 | } |
| 720 | |
| 721 | const byte* ImageWriter::GetQuickEntryPoint(mirror::ArtMethod* method) { |
| 722 | // Calculate the quick entry point following the same logic as FixupMethod() below. |
| 723 | // The resolution method has a special trampoline to call. |
| 724 | if (UNLIKELY(method == Runtime::Current()->GetResolutionMethod())) { |
| 725 | return GetOatAddress(quick_resolution_trampoline_offset_); |
| 726 | } else if (UNLIKELY(method == Runtime::Current()->GetImtConflictMethod())) { |
| 727 | return GetOatAddress(quick_imt_conflict_trampoline_offset_); |
| 728 | } else { |
| 729 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 730 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 731 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 732 | if (UNLIKELY(method->IsAbstract())) { |
| 733 | return GetOatAddress(quick_to_interpreter_bridge_offset_); |
| 734 | } else { |
| 735 | bool quick_is_interpreted; |
| 736 | return GetQuickCode(method, &quick_is_interpreted); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 741 | void ImageWriter::FixupMethod(ArtMethod* orig, ArtMethod* copy) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 742 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 743 | // oat_begin_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 744 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 745 | // The resolution method has a special trampoline to call. |
| 746 | if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 747 | copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_resolution_trampoline_offset_)); |
| 748 | copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_resolution_trampoline_offset_)); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 749 | } else if (UNLIKELY(orig == Runtime::Current()->GetImtConflictMethod())) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 750 | copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_imt_conflict_trampoline_offset_)); |
| 751 | copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_imt_conflict_trampoline_offset_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 752 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 753 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 754 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 755 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 756 | if (UNLIKELY(orig->IsAbstract())) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 757 | copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_to_interpreter_bridge_offset_)); |
| 758 | copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_to_interpreter_bridge_offset_)); |
| 759 | copy->SetEntryPointFromInterpreter<kVerifyNone>(reinterpret_cast<EntryPointFromInterpreter*> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 760 | (const_cast<byte*>(GetOatAddress(interpreter_to_interpreter_bridge_offset_)))); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 761 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 762 | bool quick_is_interpreted; |
| 763 | const byte* quick_code = GetQuickCode(orig, &quick_is_interpreted); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 764 | copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(quick_code); |
| 765 | |
| 766 | // Portable entrypoint: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 767 | const byte* portable_code = GetOatAddress(orig->GetPortableOatCodeOffset()); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 768 | bool portable_is_interpreted = false; |
| 769 | if (portable_code != nullptr && |
| 770 | (!orig->IsStatic() || orig->IsConstructor() || orig->GetDeclaringClass()->IsInitialized())) { |
| 771 | // We have code for a non-static or initialized method, just use the code. |
| 772 | } else if (portable_code == nullptr && orig->IsNative() && |
| 773 | (!orig->IsStatic() || orig->GetDeclaringClass()->IsInitialized())) { |
| 774 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
| 775 | // TODO: generic JNI support for LLVM. |
| 776 | portable_code = GetOatAddress(portable_resolution_trampoline_offset_); |
| 777 | } else if (portable_code == nullptr && !orig->IsNative()) { |
| 778 | // We don't have code at all for a non-native method, use the interpreter. |
| 779 | portable_code = GetOatAddress(portable_to_interpreter_bridge_offset_); |
| 780 | portable_is_interpreted = true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 781 | } else { |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 782 | CHECK(!orig->GetDeclaringClass()->IsInitialized()); |
| 783 | // We have code for a static method, but need to go through the resolution stub for class |
| 784 | // initialization. |
| 785 | portable_code = GetOatAddress(portable_resolution_trampoline_offset_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 786 | } |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 787 | copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(portable_code); |
| 788 | |
| 789 | // JNI entrypoint: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 790 | if (orig->IsNative()) { |
| 791 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 792 | // Note this is not the code_ pointer, that is handled above. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 793 | copy->SetNativeMethod<kVerifyNone>(GetOatAddress(jni_dlsym_lookup_offset_)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 794 | } else { |
| 795 | // Normal (non-abstract non-native) methods have various tables to relocate. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 796 | uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset(); |
| 797 | const byte* native_gc_map = GetOatAddress(native_gc_map_offset); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 798 | copy->SetNativeGcMap<kVerifyNone>(reinterpret_cast<const uint8_t*>(native_gc_map)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 799 | } |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 800 | |
| 801 | // Interpreter entrypoint: |
| 802 | // Set the interpreter entrypoint depending on whether there is compiled code or not. |
| 803 | uint32_t interpreter_code = (quick_is_interpreted && portable_is_interpreted) |
| 804 | ? interpreter_to_interpreter_bridge_offset_ |
| 805 | : interpreter_to_compiled_code_bridge_offset_; |
| 806 | copy->SetEntryPointFromInterpreter<kVerifyNone>( |
| 807 | reinterpret_cast<EntryPointFromInterpreter*>( |
| 808 | const_cast<byte*>(GetOatAddress(interpreter_code)))); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 809 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 813 | static OatHeader* GetOatHeaderFromElf(ElfFile* elf) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame^] | 814 | uint64_t data_sec_offset; |
| 815 | bool has_data_sec = elf->GetSectionOffsetAndSize(".rodata", &data_sec_offset, nullptr); |
| 816 | if (!has_data_sec) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 817 | return nullptr; |
| 818 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame^] | 819 | return reinterpret_cast<OatHeader*>(elf->Begin() + data_sec_offset); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 822 | void ImageWriter::SetOatChecksumFromElfFile(File* elf_file) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 823 | std::string error_msg; |
| 824 | std::unique_ptr<ElfFile> elf(ElfFile::Open(elf_file, PROT_READ|PROT_WRITE, |
| 825 | MAP_SHARED, &error_msg)); |
| 826 | if (elf.get() == nullptr) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 827 | LOG(FATAL) << "Unable open oat file: " << error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 828 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 829 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 830 | OatHeader* oat_header = GetOatHeaderFromElf(elf.get()); |
| 831 | CHECK(oat_header != nullptr); |
| 832 | CHECK(oat_header->IsValid()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 833 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 834 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 835 | image_header->SetOatChecksum(oat_header->GetChecksum()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | } // namespace art |