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