Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "image_writer.h" |
| 4 | |
| 5 | #include <sys/mman.h> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 6 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 9 | #include "UniquePtr.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 10 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 11 | #include "class_loader.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 12 | #include "dex_cache.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 13 | #include "file.h" |
| 14 | #include "globals.h" |
| 15 | #include "heap.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 16 | #include "image.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 17 | #include "intern_table.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 18 | #include "logging.h" |
| 19 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 20 | #include "runtime.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 21 | #include "space.h" |
| 22 | #include "utils.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 26 | bool ImageWriter::Write(const char* filename, uintptr_t image_base) { |
| 27 | CHECK_NE(image_base, 0U); |
| 28 | image_base_ = reinterpret_cast<byte*>(image_base); |
| 29 | |
| 30 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 31 | // currently just write the last space, assuming it is the space that was being used for allocation |
| 32 | CHECK_GE(spaces.size(), 1U); |
| 33 | source_space_ = spaces[spaces.size()-1]; |
| 34 | |
| 35 | if (!Init()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 36 | return false; |
| 37 | } |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 38 | Heap::CollectGarbage(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 39 | CalculateNewObjectOffsets(); |
| 40 | CopyAndFixupObjects(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 42 | UniquePtr<File> file(OS::OpenFile(filename, true)); |
| 43 | if (file.get() == NULL) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 46 | return file->WriteFully(image_->GetAddress(), image_top_); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 49 | bool ImageWriter::Init() { |
| 50 | size_t size = source_space_->Size(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 51 | int prot = PROT_READ | PROT_WRITE; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 52 | size_t length = RoundUp(size, kPageSize); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 53 | image_.reset(MemMap::Map(length, prot)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 54 | if (image_.get() == NULL) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 60 | void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 61 | DCHECK(obj != NULL); |
| 62 | DCHECK(arg != NULL); |
| 63 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 64 | if (!image_writer->InSourceSpace(obj)) { |
| 65 | return; |
| 66 | } |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 67 | |
| 68 | // if it is a string, we want to intern it if its not interned. |
| 69 | if (obj->IsString()) { |
| 70 | // we must be an interned string that was forward referenced and already assigned |
| 71 | if (IsImageOffsetAssigned(obj)) { |
| 72 | DCHECK_EQ(obj, obj->AsString()->Intern()); |
| 73 | return; |
| 74 | } |
| 75 | String* interned = obj->AsString()->Intern(); |
| 76 | if (obj != interned) { |
| 77 | if (!IsImageOffsetAssigned(interned)) { |
| 78 | // interned obj is after us, allocate its location early |
| 79 | image_writer->AssignImageOffset(interned); |
| 80 | } |
| 81 | // point those looking for this object to the interned version. |
| 82 | SetImageOffset(obj, GetImageOffset(interned)); |
| 83 | return; |
| 84 | } |
| 85 | // else (obj == interned), nothing to do but fall through to the normal case |
| 86 | } |
| 87 | |
| 88 | image_writer->AssignImageOffset(obj); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 89 | |
| 90 | // sniff out the DexCaches on this pass for use on the next pass |
| 91 | if (obj->IsClass()) { |
| 92 | Class* klass = obj->AsClass(); |
| 93 | DexCache* dex_cache = klass->GetDexCache(); |
| 94 | if (dex_cache != NULL) { |
| 95 | image_writer->dex_caches_.insert(dex_cache); |
| 96 | } else { |
| 97 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive()); |
| 98 | } |
| 99 | } |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 102 | ObjectArray<Object>* CreateImageRoots() { |
| 103 | // build a Object[] of the roots needed to restore the runtime |
| 104 | Runtime* runtime = Runtime::Current(); |
| 105 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 106 | Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;"); |
| 107 | ObjectArray<Object>* image_roots = ObjectArray<Object>::Alloc(object_array_class, |
| 108 | ImageHeader::kImageRootsMax); |
| 109 | image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniStubArray()); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 110 | image_roots->Set(ImageHeader::kCalleeSaveMethod, runtime->GetCalleeSaveMethod()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 111 | return image_roots; |
| 112 | } |
| 113 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 114 | void ImageWriter::CalculateNewObjectOffsets() { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 115 | ObjectArray<Object>* image_roots = CreateImageRoots(); |
| 116 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 117 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 118 | DCHECK(heap_bitmap != NULL); |
| 119 | DCHECK_EQ(0U, image_top_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 120 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 121 | // leave space for the header, but do not write it yet, we need to |
| 122 | // know where image_roots is going to end up |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 123 | image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment |
| 124 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 125 | heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 126 | DCHECK_LT(image_top_, image_->GetLength()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 127 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 128 | // return to write header at start of image with future location of image_roots |
| 129 | ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_), |
| 130 | reinterpret_cast<uint32_t>(GetImageAddress(image_roots))); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 131 | memcpy(image_->GetAddress(), &image_header, sizeof(image_header)); |
| 132 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 133 | // Note that top_ is left at end of used space |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void ImageWriter::CopyAndFixupObjects() { |
| 137 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 138 | DCHECK(heap_bitmap != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 139 | // TODO: heap validation can't handle this fix up pass |
| 140 | Heap::DisableObjectValidation(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 141 | heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk |
| 142 | FixupDexCaches(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 145 | void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 146 | DCHECK(object != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 147 | DCHECK(arg != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 148 | const Object* obj = object; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 149 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 150 | if (!image_writer->InSourceSpace(object)) { |
| 151 | return; |
| 152 | } |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 153 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 154 | // see GetLocalAddress for similar computation |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 155 | size_t offset = image_writer->GetImageOffset(obj); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 156 | byte* dst = image_writer->image_->GetAddress() + offset; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 157 | const byte* src = reinterpret_cast<const byte*>(obj); |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 158 | size_t n = obj->SizeOf(); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 159 | DCHECK_LT(offset + n, image_writer->image_->GetLength()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 160 | memcpy(dst, src, n); |
| 161 | Object* copy = reinterpret_cast<Object*>(dst); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 162 | ResetImageOffset(copy); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 163 | image_writer->FixupObject(obj, copy); |
| 164 | } |
| 165 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 166 | void ImageWriter::FixupObject(const Object* orig, Object* copy) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 167 | DCHECK(orig != NULL); |
| 168 | DCHECK(copy != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 169 | copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass()))); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 170 | // TODO: special case init of pointers to malloc data (or removal of these pointers) |
| 171 | if (orig->IsClass()) { |
| 172 | FixupClass(orig->AsClass(), down_cast<Class*>(copy)); |
| 173 | } else if (orig->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 174 | FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy)); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 175 | } else if (orig->IsMethod()) { |
| 176 | FixupMethod(orig->AsMethod(), down_cast<Method*>(copy)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 177 | } else { |
| 178 | FixupInstanceFields(orig, copy); |
| 179 | } |
| 180 | } |
| 181 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 182 | void ImageWriter::FixupClass(const Class* orig, Class* copy) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 183 | FixupInstanceFields(orig, copy); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 184 | FixupStaticFields(orig, copy); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 187 | const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) { |
| 188 | // TODO: change to DCHECK when all code compiling |
| 189 | if (copy_code_array == NULL) { |
| 190 | return NULL; |
| 191 | } |
| 192 | const void* copy_code = copy_code_array->GetData(); |
| 193 | // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup? |
| 194 | if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 195 | return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 196 | } |
| 197 | return copy_code; |
| 198 | } |
| 199 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 200 | void ImageWriter::FixupMethod(const Method* orig, Method* copy) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 201 | FixupInstanceFields(orig, copy); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 202 | copy->code_ = FixupCode(copy->code_array_, orig->code_); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 203 | copy->invoke_stub_ = reinterpret_cast<Method::InvokeStub*>(FixupCode(copy->invoke_stub_array_, reinterpret_cast<void*>(orig->invoke_stub_))); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 204 | if (orig->IsNative()) { |
| 205 | ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniStubArray(); |
| 206 | ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_)); |
| 207 | copy->native_method_ = copy_jni_stub_array_->GetData(); |
| 208 | } else { |
| 209 | DCHECK(copy->native_method_ == NULL); |
| 210 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 213 | void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 214 | for (int32_t i = 0; i < orig->GetLength(); ++i) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 215 | const Object* element = orig->Get(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 216 | copy->SetWithoutChecks(i, GetImageAddress(element)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 220 | void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) { |
| 221 | DCHECK(orig != NULL); |
| 222 | DCHECK(copy != NULL); |
| 223 | Class* klass = orig->GetClass(); |
| 224 | DCHECK(klass != NULL); |
| 225 | FixupFields(orig, |
| 226 | copy, |
| 227 | klass->GetReferenceInstanceOffsets(), |
| 228 | false); |
| 229 | } |
| 230 | |
| 231 | void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) { |
| 232 | DCHECK(orig != NULL); |
| 233 | DCHECK(copy != NULL); |
| 234 | FixupFields(orig, |
| 235 | copy, |
| 236 | orig->GetReferenceStaticOffsets(), |
| 237 | true); |
| 238 | } |
| 239 | |
| 240 | void ImageWriter::FixupFields(const Object* orig, |
| 241 | Object* copy, |
| 242 | uint32_t ref_offsets, |
| 243 | bool is_static) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 244 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 245 | // Found a reference offset bitmap. Fixup the specified offsets. |
| 246 | while (ref_offsets != 0) { |
| 247 | size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 248 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 249 | const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false); |
| 250 | copy->SetFieldObject(byte_offset, GetImageAddress(ref), false); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 251 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 252 | } |
| 253 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 254 | // There is no reference offset bitmap. In the non-static case, |
| 255 | // walk up the class inheritance hierarchy and find reference |
| 256 | // offsets the hard way. In the static case, just consider this |
| 257 | // class. |
| 258 | for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 259 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 260 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 261 | size_t num_reference_fields = (is_static |
| 262 | ? klass->NumReferenceStaticFields() |
| 263 | : klass->NumReferenceInstanceFields()); |
| 264 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 265 | Field* field = (is_static |
| 266 | ? klass->GetStaticField(i) |
| 267 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 268 | MemberOffset field_offset = field->GetOffset(); |
| 269 | const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false); |
| 270 | copy->SetFieldObject(field_offset, GetImageAddress(ref), false); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 276 | void ImageWriter::FixupDexCaches() { |
| 277 | typedef Set::const_iterator It; // TODO: C++0x auto |
| 278 | for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) { |
| 279 | DexCache* orig = *it; |
| 280 | DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig)); |
| 281 | FixupDexCache(orig, copy); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) { |
| 286 | CHECK(orig != NULL); |
| 287 | CHECK(copy != NULL); |
| 288 | |
| 289 | CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods(); |
| 290 | CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms)); |
| 291 | for (size_t i = 0; i < orig->NumResolvedMethods(); i++) { |
| 292 | Method* orig_method = orig->GetResolvedMethod(i); |
| 293 | // if it was resolved in the original, resolve it in the copy |
| 294 | if (orig_method != NULL |
| 295 | && InSourceSpace(orig_method) |
| 296 | && orig_method == orig_cadms->GetResolvedMethod(i)) { |
| 297 | Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method)); |
| 298 | copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), |
| 299 | reinterpret_cast<int32_t>(copy_method->code_)); |
| 300 | copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i), |
| 301 | reinterpret_cast<int32_t>(GetImageAddress(orig_method))); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 306 | } // namespace art |