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()); |
| 110 | return image_roots; |
| 111 | } |
| 112 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 113 | void ImageWriter::CalculateNewObjectOffsets() { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 114 | ObjectArray<Object>* image_roots = CreateImageRoots(); |
| 115 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 116 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 117 | DCHECK(heap_bitmap != NULL); |
| 118 | DCHECK_EQ(0U, image_top_); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 119 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 120 | // leave space for the header, but do not write it yet, we need to |
| 121 | // know where image_roots is going to end up |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 122 | image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment |
| 123 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 124 | heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 125 | DCHECK_LT(image_top_, image_->GetLength()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 126 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 127 | // return to write header at start of image with future location of image_roots |
| 128 | ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_), |
| 129 | reinterpret_cast<uint32_t>(GetImageAddress(image_roots))); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 130 | memcpy(image_->GetAddress(), &image_header, sizeof(image_header)); |
| 131 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 132 | // Note that top_ is left at end of used space |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void ImageWriter::CopyAndFixupObjects() { |
| 136 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 137 | DCHECK(heap_bitmap != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 138 | // TODO: heap validation can't handle this fix up pass |
| 139 | Heap::DisableObjectValidation(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 140 | heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk |
| 141 | FixupDexCaches(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 144 | void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 145 | DCHECK(object != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 146 | DCHECK(arg != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 147 | const Object* obj = object; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 148 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 149 | if (!image_writer->InSourceSpace(object)) { |
| 150 | return; |
| 151 | } |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 152 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 153 | // see GetLocalAddress for similar computation |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 154 | size_t offset = image_writer->GetImageOffset(obj); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 155 | byte* dst = image_writer->image_->GetAddress() + offset; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 156 | const byte* src = reinterpret_cast<const byte*>(obj); |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 157 | size_t n = obj->SizeOf(); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 158 | DCHECK_LT(offset + n, image_writer->image_->GetLength()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 159 | memcpy(dst, src, n); |
| 160 | Object* copy = reinterpret_cast<Object*>(dst); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 161 | ResetImageOffset(copy); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 162 | image_writer->FixupObject(obj, copy); |
| 163 | } |
| 164 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 165 | void ImageWriter::FixupObject(const Object* orig, Object* copy) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 166 | DCHECK(orig != NULL); |
| 167 | DCHECK(copy != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 168 | copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass()))); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 169 | // TODO: special case init of pointers to malloc data (or removal of these pointers) |
| 170 | if (orig->IsClass()) { |
| 171 | FixupClass(orig->AsClass(), down_cast<Class*>(copy)); |
| 172 | } else if (orig->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 173 | FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy)); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 174 | } else if (orig->IsMethod()) { |
| 175 | FixupMethod(orig->AsMethod(), down_cast<Method*>(copy)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 176 | } else { |
| 177 | FixupInstanceFields(orig, copy); |
| 178 | } |
| 179 | } |
| 180 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 181 | void ImageWriter::FixupClass(const Class* orig, Class* copy) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 182 | FixupInstanceFields(orig, copy); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 183 | FixupStaticFields(orig, copy); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 186 | const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) { |
| 187 | // TODO: change to DCHECK when all code compiling |
| 188 | if (copy_code_array == NULL) { |
| 189 | return NULL; |
| 190 | } |
| 191 | const void* copy_code = copy_code_array->GetData(); |
| 192 | // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup? |
| 193 | if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 194 | return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 195 | } |
| 196 | return copy_code; |
| 197 | } |
| 198 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 199 | void ImageWriter::FixupMethod(const Method* orig, Method* copy) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 200 | FixupInstanceFields(orig, copy); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 201 | copy->code_ = FixupCode(copy->code_array_, orig->code_); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 202 | 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] | 203 | if (orig->IsNative()) { |
| 204 | ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniStubArray(); |
| 205 | ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_)); |
| 206 | copy->native_method_ = copy_jni_stub_array_->GetData(); |
| 207 | } else { |
| 208 | DCHECK(copy->native_method_ == NULL); |
| 209 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 212 | void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 213 | for (int32_t i = 0; i < orig->GetLength(); ++i) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 214 | const Object* element = orig->Get(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 215 | copy->SetWithoutChecks(i, GetImageAddress(element)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 219 | void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) { |
| 220 | DCHECK(orig != NULL); |
| 221 | DCHECK(copy != NULL); |
| 222 | Class* klass = orig->GetClass(); |
| 223 | DCHECK(klass != NULL); |
| 224 | FixupFields(orig, |
| 225 | copy, |
| 226 | klass->GetReferenceInstanceOffsets(), |
| 227 | false); |
| 228 | } |
| 229 | |
| 230 | void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) { |
| 231 | DCHECK(orig != NULL); |
| 232 | DCHECK(copy != NULL); |
| 233 | FixupFields(orig, |
| 234 | copy, |
| 235 | orig->GetReferenceStaticOffsets(), |
| 236 | true); |
| 237 | } |
| 238 | |
| 239 | void ImageWriter::FixupFields(const Object* orig, |
| 240 | Object* copy, |
| 241 | uint32_t ref_offsets, |
| 242 | bool is_static) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 243 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 244 | // Found a reference offset bitmap. Fixup the specified offsets. |
| 245 | while (ref_offsets != 0) { |
| 246 | size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 247 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 248 | const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false); |
| 249 | copy->SetFieldObject(byte_offset, GetImageAddress(ref), false); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 250 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 251 | } |
| 252 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 253 | // There is no reference offset bitmap. In the non-static case, |
| 254 | // walk up the class inheritance hierarchy and find reference |
| 255 | // offsets the hard way. In the static case, just consider this |
| 256 | // class. |
| 257 | for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 258 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 259 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 260 | size_t num_reference_fields = (is_static |
| 261 | ? klass->NumReferenceStaticFields() |
| 262 | : klass->NumReferenceInstanceFields()); |
| 263 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 264 | Field* field = (is_static |
| 265 | ? klass->GetStaticField(i) |
| 266 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 267 | MemberOffset field_offset = field->GetOffset(); |
| 268 | const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false); |
| 269 | copy->SetFieldObject(field_offset, GetImageAddress(ref), false); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 275 | void ImageWriter::FixupDexCaches() { |
| 276 | typedef Set::const_iterator It; // TODO: C++0x auto |
| 277 | for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) { |
| 278 | DexCache* orig = *it; |
| 279 | DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig)); |
| 280 | FixupDexCache(orig, copy); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) { |
| 285 | CHECK(orig != NULL); |
| 286 | CHECK(copy != NULL); |
| 287 | |
| 288 | CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods(); |
| 289 | CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms)); |
| 290 | for (size_t i = 0; i < orig->NumResolvedMethods(); i++) { |
| 291 | Method* orig_method = orig->GetResolvedMethod(i); |
| 292 | // if it was resolved in the original, resolve it in the copy |
| 293 | if (orig_method != NULL |
| 294 | && InSourceSpace(orig_method) |
| 295 | && orig_method == orig_cadms->GetResolvedMethod(i)) { |
| 296 | Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method)); |
| 297 | copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), |
| 298 | reinterpret_cast<int32_t>(copy_method->code_)); |
| 299 | copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i), |
| 300 | reinterpret_cast<int32_t>(GetImageAddress(orig_method))); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 305 | } // namespace art |