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