blob: c22f8d6c0125b5ceabbfeabe4388609c7becb874 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 Chartier31e89252013-08-28 11:29:12 -070032#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#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"
Mathieu Chartierad2541a2013-10-25 10:05:23 -070039#include "lock_word.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070040#include "mirror/art_field-inl.h"
41#include "mirror/art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070042#include "mirror/array-inl.h"
43#include "mirror/class-inl.h"
44#include "mirror/class_loader.h"
45#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
48#include "oat.h"
49#include "oat_file.h"
50#include "object_utils.h"
51#include "runtime.h"
52#include "scoped_thread_state_change.h"
53#include "sirt_ref.h"
54#include "UniquePtr.h"
55#include "utils.h"
56
Brian Carlstromea46f952013-07-30 01:26:50 -070057using ::art::mirror::ArtField;
58using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070059using ::art::mirror::Class;
60using ::art::mirror::DexCache;
61using ::art::mirror::EntryPointFromInterpreter;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070062using ::art::mirror::Object;
63using ::art::mirror::ObjectArray;
64using ::art::mirror::String;
Brian Carlstrom7940e442013-07-12 13:46:57 -070065
66namespace art {
67
68bool ImageWriter::Write(const std::string& image_filename,
69 uintptr_t image_begin,
70 const std::string& oat_filename,
71 const std::string& oat_location) {
72 CHECK(!image_filename.empty());
73
74 CHECK_NE(image_begin, 0U);
75 image_begin_ = reinterpret_cast<byte*>(image_begin);
76
77 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -070078
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070079 UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 if (oat_file.get() == NULL) {
81 LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
82 return false;
83 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070084 std::string error_msg;
85 oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg);
86 if (oat_file_ == nullptr) {
87 LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location
88 << ": " << error_msg;
Brian Carlstromc50d8e12013-07-23 22:35:16 -070089 return false;
90 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092
Ian Rogers848871b2013-08-05 10:56:33 -070093 interpreter_to_interpreter_bridge_offset_ =
94 oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset();
95 interpreter_to_compiled_code_bridge_offset_ =
96 oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset();
97
98 jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset();
99
Jeff Hao88474b42013-10-23 16:24:40 -0700100 portable_imt_conflict_trampoline_offset_ =
101 oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset();
Ian Rogers848871b2013-08-05 10:56:33 -0700102 portable_resolution_trampoline_offset_ =
103 oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset();
104 portable_to_interpreter_bridge_offset_ =
105 oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset();
106
Jeff Hao88474b42013-10-23 16:24:40 -0700107 quick_imt_conflict_trampoline_offset_ =
108 oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset();
Ian Rogers848871b2013-08-05 10:56:33 -0700109 quick_resolution_trampoline_offset_ =
110 oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset();
111 quick_to_interpreter_bridge_offset_ =
112 oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 {
114 Thread::Current()->TransitionFromSuspendedToRunnable();
115 PruneNonImageClasses(); // Remove junk
116 ComputeLazyFieldsForImageClasses(); // Add useful information
117 ComputeEagerResolvedStrings();
118 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
119 }
120 gc::Heap* heap = Runtime::Current()->GetHeap();
121 heap->CollectGarbage(false); // Remove garbage.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122
123 if (!AllocMemory()) {
124 return false;
125 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700126
127 if (kIsDebugBuild) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 ScopedObjectAccess soa(Thread::Current());
129 CheckNonImageClassesRemoved();
130 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 Thread::Current()->TransitionFromSuspendedToRunnable();
133 size_t oat_loaded_size = 0;
134 size_t oat_data_offset = 0;
135 ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
136 CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
137 CopyAndFixupObjects();
138 PatchOatCodeAndMethods();
139 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
140
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700141 UniquePtr<File> image_file(OS::CreateEmptyFile(image_filename.c_str()));
Mathieu Chartier31e89252013-08-28 11:29:12 -0700142 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 if (image_file.get() == NULL) {
144 LOG(ERROR) << "Failed to open image file " << image_filename;
145 return false;
146 }
147 if (fchmod(image_file->Fd(), 0644) != 0) {
148 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
149 return EXIT_FAILURE;
150 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700151
152 // Write out the image.
153 CHECK_EQ(image_end_, image_header->GetImageSize());
154 if (!image_file->WriteFully(image_->Begin(), image_end_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 PLOG(ERROR) << "Failed to write image file " << image_filename;
156 return false;
157 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700158
159 // Write out the image bitmap at the page aligned start of the image end.
160 CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize);
161 if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()),
162 image_header->GetImageBitmapSize(),
163 image_header->GetImageBitmapOffset())) {
164 PLOG(ERROR) << "Failed to write image file " << image_filename;
165 return false;
166 }
167
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 return true;
169}
170
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) {
172 DCHECK(object != nullptr);
173 DCHECK_NE(offset, 0U);
174 DCHECK(!IsImageOffsetAssigned(object));
175 mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset);
176 DCHECK_ALIGNED(obj, kObjectAlignment);
177 image_bitmap_->Set(obj);
178 // Before we stomp over the lock word, save the hash code for later.
179 Monitor::Deflate(Thread::Current(), object);;
180 LockWord lw(object->GetLockWord());
181 switch (lw.GetState()) {
182 case LockWord::kFatLocked: {
183 LOG(FATAL) << "Fat locked object " << obj << " found during object copy";
184 break;
185 }
186 case LockWord::kThinLocked: {
187 LOG(FATAL) << "Thin locked object " << obj << " found during object copy";
188 break;
189 }
190 case LockWord::kUnlocked:
191 // No hash, don't need to save it.
192 break;
193 case LockWord::kHashCode:
194 saved_hashes_.push_back(std::make_pair(obj, lw.GetHashCode()));
195 break;
196 default:
197 LOG(FATAL) << "Unreachable.";
198 break;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700199 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700200 object->SetLockWord(LockWord::FromForwardingAddress(offset));
201 DCHECK(IsImageOffsetAssigned(object));
202}
203
204void ImageWriter::AssignImageOffset(mirror::Object* object) {
205 DCHECK(object != nullptr);
206 SetImageOffset(object, image_end_);
207 image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment
208 DCHECK_LT(image_end_, image_->Size());
209}
210
211bool ImageWriter::IsImageOffsetAssigned(const mirror::Object* object) const {
212 DCHECK(object != nullptr);
213 return object->GetLockWord().GetState() == LockWord::kForwardingAddress;
214}
215
216size_t ImageWriter::GetImageOffset(const mirror::Object* object) const {
217 DCHECK(object != nullptr);
218 DCHECK(IsImageOffsetAssigned(object));
219 LockWord lock_word = object->GetLockWord();
220 size_t offset = lock_word.ForwardingAddress();
221 DCHECK_LT(offset, image_end_);
222 return offset;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700223}
224
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225bool ImageWriter::AllocMemory() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700226 size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700227 std::string error_msg;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700228 image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, PROT_READ | PROT_WRITE,
229 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 if (UNLIKELY(image_.get() == nullptr)) {
231 LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 return false;
233 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700234
235 // Create the image bitmap.
236 image_bitmap_.reset(gc::accounting::SpaceBitmap::Create("image bitmap", image_->Begin(),
237 length));
238 if (image_bitmap_.get() == nullptr) {
239 LOG(ERROR) << "Failed to allocate memory for image bitmap";
240 return false;
241 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 return true;
243}
244
245void ImageWriter::ComputeLazyFieldsForImageClasses() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700246 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
248}
249
250bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
251 c->ComputeName();
252 return true;
253}
254
255void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
256 if (!obj->GetClass()->IsStringClass()) {
257 return;
258 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700259 mirror::String* string = obj->AsString();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700261 for (DexCache* dex_cache : Runtime::Current()->GetClassLinker()->GetDexCaches()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 const DexFile& dex_file = *dex_cache->GetDexFile();
263 const DexFile::StringId* string_id = dex_file.FindStringId(utf16_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700264 if (string_id != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 // This string occurs in this dex file, assign the dex cache entry.
266 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
267 if (dex_cache->GetResolvedString(string_idx) == NULL) {
268 dex_cache->SetResolvedString(string_idx, string);
269 }
270 }
271 }
272}
273
Mathieu Chartier590fee92013-09-13 13:46:47 -0700274void ImageWriter::ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
275 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
276 Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277}
278
279bool ImageWriter::IsImageClass(const Class* klass) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700280 return compiler_driver_.IsImageClass(ClassHelper(klass).GetDescriptor());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281}
282
283struct NonImageClasses {
284 ImageWriter* image_writer;
285 std::set<std::string>* non_image_classes;
286};
287
288void ImageWriter::PruneNonImageClasses() {
289 if (compiler_driver_.GetImageClasses() == NULL) {
290 return;
291 }
292 Runtime* runtime = Runtime::Current();
293 ClassLinker* class_linker = runtime->GetClassLinker();
294
295 // Make a list of classes we would like to prune.
296 std::set<std::string> non_image_classes;
297 NonImageClasses context;
298 context.image_writer = this;
299 context.non_image_classes = &non_image_classes;
300 class_linker->VisitClasses(NonImageClassesVisitor, &context);
301
302 // Remove the undesired classes from the class roots.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700303 for (const std::string& it : non_image_classes) {
304 class_linker->RemoveClass(it.c_str(), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 }
306
307 // Clear references to removed classes from the DexCaches.
Brian Carlstromea46f952013-07-30 01:26:50 -0700308 ArtMethod* resolution_method = runtime->GetResolutionMethod();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700309 for (DexCache* dex_cache : class_linker->GetDexCaches()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
311 Class* klass = dex_cache->GetResolvedType(i);
312 if (klass != NULL && !IsImageClass(klass)) {
313 dex_cache->SetResolvedType(i, NULL);
314 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
315 }
316 }
317 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700318 ArtMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
320 dex_cache->SetResolvedMethod(i, resolution_method);
321 }
322 }
323 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700324 ArtField* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
326 dex_cache->SetResolvedField(i, NULL);
327 }
328 }
329 }
330}
331
332bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
333 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
334 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700335 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 }
337 return true;
338}
339
340void ImageWriter::CheckNonImageClassesRemoved()
341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700342 if (compiler_driver_.GetImageClasses() != nullptr) {
343 gc::Heap* heap = Runtime::Current()->GetHeap();
344 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
345 heap->VisitObjects(CheckNonImageClassesRemovedCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347}
348
349void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
350 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700351 if (obj->IsClass()) {
352 Class* klass = obj->AsClass();
353 if (!image_writer->IsImageClass(klass)) {
354 image_writer->DumpImageClasses();
355 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
356 << " " << PrettyDescriptor(klass);
357 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 }
359}
360
361void ImageWriter::DumpImageClasses() {
362 CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses();
363 CHECK(image_classes != NULL);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700364 for (const std::string& image_class : *image_classes) {
365 LOG(INFO) << " " << image_class;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 }
367}
368
Mathieu Chartier590fee92013-09-13 13:46:47 -0700369void ImageWriter::CalculateObjectOffsets(Object* obj) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 DCHECK(obj != NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 // if it is a string, we want to intern it if its not interned.
372 if (obj->GetClass()->IsStringClass()) {
373 // we must be an interned string that was forward referenced and already assigned
Mathieu Chartier590fee92013-09-13 13:46:47 -0700374 if (IsImageOffsetAssigned(obj)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 DCHECK_EQ(obj, obj->AsString()->Intern());
376 return;
377 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700378 Thread* self = Thread::Current();
379 SirtRef<Object> sirt_obj(self, obj);
380 mirror::String* interned = obj->AsString()->Intern();
381 if (sirt_obj.get() != interned) {
382 if (!IsImageOffsetAssigned(interned)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 // interned obj is after us, allocate its location early
Mathieu Chartier590fee92013-09-13 13:46:47 -0700384 AssignImageOffset(interned);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 }
386 // point those looking for this object to the interned version.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700387 SetImageOffset(sirt_obj.get(), GetImageOffset(interned));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 return;
389 }
390 // else (obj == interned), nothing to do but fall through to the normal case
391 }
392
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 AssignImageOffset(obj);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394}
395
396ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
397 Runtime* runtime = Runtime::Current();
398 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 Thread* self = Thread::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700400 SirtRef<Class> object_array_class(self, class_linker->FindSystemClass("[Ljava/lang/Object;"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401
402 // build an Object[] of all the DexCaches used in the source_space_
Mathieu Chartier590fee92013-09-13 13:46:47 -0700403 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class.get(),
404 class_linker->GetDexCaches().size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 int i = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700406 for (DexCache* dex_cache : class_linker->GetDexCaches()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700407 dex_caches->Set(i++, dex_cache);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 }
409
410 // build an Object[] of the roots needed to restore the runtime
Mathieu Chartier590fee92013-09-13 13:46:47 -0700411 SirtRef<ObjectArray<Object> > image_roots(
412 self, ObjectArray<Object>::Alloc(self, object_array_class.get(), ImageHeader::kImageRootsMax));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Jeff Hao88474b42013-10-23 16:24:40 -0700414 image_roots->Set(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod());
415 image_roots->Set(ImageHeader::kDefaultImt, runtime->GetDefaultImt());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 image_roots->Set(ImageHeader::kCalleeSaveMethod,
417 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
418 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
419 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
420 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
421 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
422 image_roots->Set(ImageHeader::kOatLocation,
423 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700424 image_roots->Set(ImageHeader::kDexCaches, dex_caches);
425 image_roots->Set(ImageHeader::kClassRoots, class_linker->GetClassRoots());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
427 CHECK(image_roots->Get(i) != NULL);
428 }
429 return image_roots.get();
430}
431
Mathieu Chartier590fee92013-09-13 13:46:47 -0700432// Walk instance fields of the given Class. Separate function to allow recursion on the super
433// class.
434void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) {
435 // Visit fields of parent classes first.
436 SirtRef<mirror::Class> sirt_class(Thread::Current(), klass);
437 mirror::Class* super = sirt_class->GetSuperClass();
438 if (super != nullptr) {
439 WalkInstanceFields(obj, super);
440 }
441 //
442 size_t num_reference_fields = sirt_class->NumReferenceInstanceFields();
443 for (size_t i = 0; i < num_reference_fields; ++i) {
444 mirror::ArtField* field = sirt_class->GetInstanceField(i);
445 MemberOffset field_offset = field->GetOffset();
446 mirror::Object* value = obj->GetFieldObject<mirror::Object*>(field_offset, false);
447 if (value != nullptr) {
448 WalkFieldsInOrder(value);
449 }
450 }
451}
452
453// For an unvisited object, visit it then all its children found via fields.
454void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) {
455 if (!IsImageOffsetAssigned(obj)) {
456 // Walk instance fields of all objects
457 Thread* self = Thread::Current();
458 SirtRef<mirror::Object> sirt_obj(self, obj);
459 SirtRef<mirror::Class> klass(self, obj->GetClass());
460 // visit the object itself.
461 CalculateObjectOffsets(sirt_obj.get());
462 WalkInstanceFields(sirt_obj.get(), klass.get());
463 // Walk static fields of a Class.
464 if (sirt_obj->IsClass()) {
465 size_t num_static_fields = klass->NumReferenceStaticFields();
466 for (size_t i = 0; i < num_static_fields; ++i) {
467 mirror::ArtField* field = klass->GetStaticField(i);
468 MemberOffset field_offset = field->GetOffset();
469 mirror::Object* value = sirt_obj->GetFieldObject<mirror::Object*>(field_offset, false);
470 if (value != nullptr) {
471 WalkFieldsInOrder(value);
472 }
473 }
474 } else if (sirt_obj->IsObjectArray()) {
475 // Walk elements of an object array.
476 int32_t length = sirt_obj->AsObjectArray<mirror::Object>()->GetLength();
477 for (int32_t i = 0; i < length; i++) {
478 mirror::ObjectArray<mirror::Object>* obj_array = sirt_obj->AsObjectArray<mirror::Object>();
479 mirror::Object* value = obj_array->Get(i);
480 if (value != nullptr) {
481 WalkFieldsInOrder(value);
482 }
483 }
484 }
485 }
486}
487
488void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) {
489 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
490 DCHECK(writer != nullptr);
491 writer->WalkFieldsInOrder(obj);
492}
493
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
495 CHECK_NE(0U, oat_loaded_size);
496 Thread* self = Thread::Current();
497 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
498
499 gc::Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 DCHECK_EQ(0U, image_end_);
501
Mathieu Chartier31e89252013-08-28 11:29:12 -0700502 // Leave space for the header, but do not write it yet, we need to
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 // know where image_roots is going to end up
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700504 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505
506 {
507 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 // TODO: Image spaces only?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700510 DCHECK_LT(image_end_, image_->Size());
511 // Clear any pre-existing monitors which may have been in the monitor words.
512 heap->VisitObjects(WalkFieldsCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 self->EndAssertNoThreadSuspension(old);
514 }
515
516 const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
517 const byte* oat_file_end = oat_file_begin + oat_loaded_size;
518 oat_data_begin_ = oat_file_begin + oat_data_offset;
519 const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
520
Mathieu Chartier31e89252013-08-28 11:29:12 -0700521 // Return to write header at start of image with future location of image_roots. At this point,
522 // image_end_ is the size of the image (excluding bitmaps).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700524 static_cast<uint32_t>(image_end_),
525 RoundUp(image_end_, kPageSize),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700526 RoundUp(image_end_ / gc::accounting::SpaceBitmap::kAlignment,
527 sizeof(size_t)),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
529 oat_file_->GetOatHeader().GetChecksum(),
530 reinterpret_cast<uint32_t>(oat_file_begin),
531 reinterpret_cast<uint32_t>(oat_data_begin_),
532 reinterpret_cast<uint32_t>(oat_data_end),
533 reinterpret_cast<uint32_t>(oat_file_end));
534 memcpy(image_->Begin(), &image_header, sizeof(image_header));
535
536 // Note that image_end_ is left at end of used space
537}
538
539void ImageWriter::CopyAndFixupObjects()
540 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
541 Thread* self = Thread::Current();
542 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
543 gc::Heap* heap = Runtime::Current()->GetHeap();
544 // TODO: heap validation can't handle this fix up pass
545 heap->DisableObjectValidation();
546 // TODO: Image spaces only?
547 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700548 heap->VisitObjects(CopyAndFixupObjectsCallback, this);
549 // Fix up the object previously had hash codes.
550 for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) {
551 hash_pair.first->SetLockWord(LockWord::FromHashCode(hash_pair.second));
552 }
553 saved_hashes_.clear();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 self->EndAssertNoThreadSuspension(old_cause);
555}
556
Mathieu Chartier590fee92013-09-13 13:46:47 -0700557void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) {
558 DCHECK(obj != NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 DCHECK(arg != NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 // see GetLocalAddress for similar computation
562 size_t offset = image_writer->GetImageOffset(obj);
563 byte* dst = image_writer->image_->Begin() + offset;
564 const byte* src = reinterpret_cast<const byte*>(obj);
565 size_t n = obj->SizeOf();
566 DCHECK_LT(offset + n, image_writer->image_->Size());
567 memcpy(dst, src, n);
568 Object* copy = reinterpret_cast<Object*>(dst);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700569 // Write in a hash code of objects which have inflated monitors or a hash code in their monitor
570 // word.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700571 copy->SetLockWord(LockWord());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 image_writer->FixupObject(obj, copy);
573}
574
575void ImageWriter::FixupObject(const Object* orig, Object* copy) {
576 DCHECK(orig != NULL);
577 DCHECK(copy != NULL);
578 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
579 // TODO: special case init of pointers to malloc data (or removal of these pointers)
580 if (orig->IsClass()) {
581 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
582 } else if (orig->IsObjectArray()) {
583 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 } else if (orig->IsArtMethod()) {
585 FixupMethod(orig->AsArtMethod(), down_cast<ArtMethod*>(copy));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 } else {
587 FixupInstanceFields(orig, copy);
588 }
589}
590
591void ImageWriter::FixupClass(const Class* orig, Class* copy) {
592 FixupInstanceFields(orig, copy);
593 FixupStaticFields(orig, copy);
594}
595
Brian Carlstromea46f952013-07-30 01:26:50 -0700596void ImageWriter::FixupMethod(const ArtMethod* orig, ArtMethod* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 FixupInstanceFields(orig, copy);
598
Ian Rogers848871b2013-08-05 10:56:33 -0700599 // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to
600 // oat_begin_
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601
Ian Rogers848871b2013-08-05 10:56:33 -0700602 // The resolution method has a special trampoline to call.
603 if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604#if defined(ART_USE_PORTABLE_COMPILER)
605 copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
606#else
607 copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
608#endif
Jeff Hao88474b42013-10-23 16:24:40 -0700609 } else if (UNLIKELY(orig == Runtime::Current()->GetImtConflictMethod())) {
610#if defined(ART_USE_PORTABLE_COMPILER)
611 copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_imt_conflict_trampoline_offset_));
612#else
613 copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_imt_conflict_trampoline_offset_));
614#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700616 // We assume all methods have code. If they don't currently then we set them to the use the
617 // resolution trampoline. Abstract methods never have code and so we need to make sure their
618 // use results in an AbstractMethodError. We use the interpreter to achieve this.
619 if (UNLIKELY(orig->IsAbstract())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers848871b2013-08-05 10:56:33 -0700621 copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_to_interpreter_bridge_offset_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622#else
Ian Rogers848871b2013-08-05 10:56:33 -0700623 copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_to_interpreter_bridge_offset_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624#endif
Ian Rogers848871b2013-08-05 10:56:33 -0700625 copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
626 (GetOatAddress(interpreter_to_interpreter_bridge_offset_)));
627 } else {
628 copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
629 (GetOatAddress(interpreter_to_compiled_code_bridge_offset_)));
630 // Use original code if it exists. Otherwise, set the code pointer to the resolution
631 // trampoline.
632 const byte* code = GetOatAddress(orig->GetOatCodeOffset());
633 if (code != NULL) {
634 copy->SetEntryPointFromCompiledCode(code);
635 } else {
636#if defined(ART_USE_PORTABLE_COMPILER)
637 copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
638#else
639 copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
640#endif
641 }
642 if (orig->IsNative()) {
643 // The native method's pointer is set to a stub to lookup via dlsym.
644 // Note this is not the code_ pointer, that is handled above.
645 copy->SetNativeMethod(GetOatAddress(jni_dlsym_lookup_offset_));
646 } else {
647 // Normal (non-abstract non-native) methods have various tables to relocate.
648 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
649 const byte* mapping_table = GetOatAddress(mapping_table_off);
Ian Rogers1809a722013-08-09 22:05:32 -0700650 copy->SetMappingTable(mapping_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651
Ian Rogers848871b2013-08-05 10:56:33 -0700652 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
653 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Ian Rogers1809a722013-08-09 22:05:32 -0700654 copy->SetVmapTable(vmap_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655
Ian Rogers848871b2013-08-05 10:56:33 -0700656 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
657 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
658 copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map));
659 }
660 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 }
662}
663
664void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
665 for (int32_t i = 0; i < orig->GetLength(); ++i) {
666 const Object* element = orig->Get(i);
667 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
668 }
669}
670
671void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
672 DCHECK(orig != NULL);
673 DCHECK(copy != NULL);
674 Class* klass = orig->GetClass();
675 DCHECK(klass != NULL);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700676 FixupFields(orig, copy, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677}
678
679void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
680 DCHECK(orig != NULL);
681 DCHECK(copy != NULL);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700682 FixupFields(orig, copy, orig->GetReferenceStaticOffsets(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683}
684
685void ImageWriter::FixupFields(const Object* orig,
686 Object* copy,
687 uint32_t ref_offsets,
688 bool is_static) {
689 if (ref_offsets != CLASS_WALK_SUPER) {
690 // Found a reference offset bitmap. Fixup the specified offsets.
691 while (ref_offsets != 0) {
692 size_t right_shift = CLZ(ref_offsets);
693 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
694 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
695 // Use SetFieldPtr to avoid card marking since we are writing to the image.
696 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
697 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
698 }
699 } else {
700 // There is no reference offset bitmap. In the non-static case,
701 // walk up the class inheritance hierarchy and find reference
702 // offsets the hard way. In the static case, just consider this
703 // class.
704 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
705 klass != NULL;
706 klass = is_static ? NULL : klass->GetSuperClass()) {
707 size_t num_reference_fields = (is_static
708 ? klass->NumReferenceStaticFields()
709 : klass->NumReferenceInstanceFields());
710 for (size_t i = 0; i < num_reference_fields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700711 ArtField* field = (is_static
712 ? klass->GetStaticField(i)
713 : klass->GetInstanceField(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 MemberOffset field_offset = field->GetOffset();
715 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
716 // Use SetFieldPtr to avoid card marking since we are writing to the image.
717 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
718 }
719 }
720 }
721 if (!is_static && orig->IsReferenceInstance()) {
722 // Fix-up referent, that isn't marked as an object field, for References.
Brian Carlstromea46f952013-07-30 01:26:50 -0700723 ArtField* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 MemberOffset field_offset = field->GetOffset();
725 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
726 // Use SetFieldPtr to avoid card marking since we are writing to the image.
727 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
728 }
729}
730
Brian Carlstromea46f952013-07-30 01:26:50 -0700731static ArtMethod* GetTargetMethod(const CompilerDriver::PatchInformation* patch)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
733 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700734 Thread* self = Thread::Current();
735 SirtRef<mirror::DexCache> dex_cache(self, class_linker->FindDexCache(patch->GetDexFile()));
736 SirtRef<mirror::ClassLoader> class_loader(self, nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700737 ArtMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
738 patch->GetTargetMethodIdx(),
739 dex_cache,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700740 class_loader,
Brian Carlstromea46f952013-07-30 01:26:50 -0700741 NULL,
742 patch->GetTargetInvokeType());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 CHECK(method != NULL)
744 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
745 CHECK(!method->IsRuntimeMethod())
746 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
747 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
748 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
749 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
750 << PrettyMethod(method);
751 return method;
752}
753
754void ImageWriter::PatchOatCodeAndMethods() {
755 Thread* self = Thread::Current();
756 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
757 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
758
759 typedef std::vector<const CompilerDriver::PatchInformation*> Patches;
760 const Patches& code_to_patch = compiler_driver_.GetCodeToPatch();
761 for (size_t i = 0; i < code_to_patch.size(); i++) {
762 const CompilerDriver::PatchInformation* patch = code_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700763 ArtMethod* target = GetTargetMethod(patch);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
765 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
766 uint32_t code_offset = code - code_base;
767 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
768 }
769
770 const Patches& methods_to_patch = compiler_driver_.GetMethodsToPatch();
771 for (size_t i = 0; i < methods_to_patch.size(); i++) {
772 const CompilerDriver::PatchInformation* patch = methods_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700773 ArtMethod* target = GetTargetMethod(patch);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
775 }
776
777 // Update the image header with the new checksum after patching
778 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
779 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
780 self->EndAssertNoThreadSuspension(old_cause);
781}
782
783void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
784 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
785 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700786 patch->GetReferrerClassDefIdx(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 patch->GetReferrerMethodIdx());
788 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
789 // TODO: make this Thumb2 specific
790 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
791 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700792 if (kIsDebugBuild) {
793 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
794 uint32_t expected = reinterpret_cast<uint32_t>(&id);
795 uint32_t actual = *patch_location;
796 CHECK(actual == expected || actual == value) << std::hex
797 << "actual=" << actual
798 << "expected=" << expected
799 << "value=" << value;
800 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 *patch_location = value;
802 oat_header.UpdateChecksum(patch_location, sizeof(value));
803}
804
805} // namespace art