blob: 8af9cf6267b8133ba81ab5fc0afd58008b2cda4e [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "image_writer.h"
4
5#include <sys/mman.h>
Elliott Hughes90a33692011-08-30 13:27:07 -07006
Brian Carlstromdb4d5402011-08-09 12:18:28 -07007#include <vector>
8
Elliott Hughes90a33692011-08-30 13:27:07 -07009#include "UniquePtr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070010#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "class_loader.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070012#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070013#include "file.h"
14#include "globals.h"
15#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070016#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "intern_table.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070018#include "logging.h"
19#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include "runtime.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070021#include "space.h"
22#include "utils.h"
23
24namespace art {
25
Brian Carlstrome24fa612011-09-29 00:53:55 -070026bool ImageWriter::Write(const char* image_filename, uintptr_t image_base,
27 const std::string& oat_filename, const std::string& strip_location_prefix) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070028 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 Carlstrom58ae9412011-10-04 00:56:06 -070035 CHECK(!source_space_->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070036
Brian Carlstrome24fa612011-09-29 00:53:55 -070037 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 Carlstrom69b15fb2011-09-03 12:25:21 -070043 if (!Init()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 return false;
45 }
Brian Carlstrom693267a2011-09-06 09:25:34 -070046 Heap::CollectGarbage();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070047 CalculateNewObjectOffsets();
48 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070049
Brian Carlstrome24fa612011-09-29 00:53:55 -070050 UniquePtr<File> file(OS::OpenFile(image_filename, true));
Elliott Hughes90a33692011-08-30 13:27:07 -070051 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070053 return false;
54 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 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 Carlstromdb4d5402011-08-09 12:18:28 -070061}
62
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070063bool ImageWriter::Init() {
64 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070065 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070066 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070067 image_.reset(MemMap::Map(length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070068 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -070070 return false;
71 }
72 return true;
73}
74
Brian Carlstrom78128a62011-09-15 17:21:19 -070075void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070076 DCHECK(obj != NULL);
77 DCHECK(arg != NULL);
78 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070079 if (!image_writer->InSourceSpace(obj)) {
80 return;
81 }
Brian Carlstromc74255f2011-09-11 22:47:39 -070082
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 Carlstrom69b15fb2011-09-03 12:25:21 -0700104
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 Carlstrome24fa612011-09-29 00:53:55 -0700112 DCHECK(klass->IsArrayClass() || klass->IsPrimitive()) << PrettyClass(klass);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700113 }
114 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700115}
116
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700118 Runtime* runtime = Runtime::Current();
119 ClassLinker* class_linker = runtime->GetClassLinker();
120 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700121
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 Carlstrom16192862011-09-12 17:50:06 -0700138 ObjectArray<Object>* image_roots = ObjectArray<Object>::Alloc(object_array_class,
139 ImageHeader::kImageRootsMax);
Ian Rogersad25ac52011-10-04 19:13:33 -0700140 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniStubArray());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700141 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
142 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700143 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
144 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
145 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
146 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
147 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
148 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700149 image_roots->Set(ImageHeader::kCalleeSaveMethod,
150 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
151 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
152 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
153 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
154 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700155 image_roots->Set(ImageHeader::kOatLocation,
156 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700157 image_roots->Set(ImageHeader::kDexCaches,
158 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700159 image_roots->Set(ImageHeader::kClassRoots,
160 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700161 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
162 CHECK(image_roots->Get(i) != NULL);
163 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700164 return image_roots;
165}
166
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700167void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom16192862011-09-12 17:50:06 -0700168 ObjectArray<Object>* image_roots = CreateImageRoots();
169
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700170 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
171 DCHECK(heap_bitmap != NULL);
172 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700173
Brian Carlstrom16192862011-09-12 17:50:06 -0700174 // leave space for the header, but do not write it yet, we need to
175 // know where image_roots is going to end up
Brian Carlstroma663ea52011-08-19 23:33:41 -0700176 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
177
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700178 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700179 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700180
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181 // Note that image_top_ is left at end of used space
182 oat_base_ = image_base_ + RoundUp(image_top_, kPageSize);
183 byte* oat_limit = oat_base_ + oat_file_->GetSize();
184
Brian Carlstrom16192862011-09-12 17:50:06 -0700185 // return to write header at start of image with future location of image_roots
186 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700187 reinterpret_cast<uint32_t>(GetImageAddress(image_roots)),
188 oat_file_->GetOatHeader().GetChecksum(),
189 reinterpret_cast<uint32_t>(oat_base_),
190 reinterpret_cast<uint32_t>(oat_limit));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700191 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700192}
193
194void ImageWriter::CopyAndFixupObjects() {
195 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
196 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197 // TODO: heap validation can't handle this fix up pass
198 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700199 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
200 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700201}
202
Brian Carlstrom78128a62011-09-15 17:21:19 -0700203void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700204 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700205 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700206 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700207 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700208 if (!image_writer->InSourceSpace(object)) {
209 return;
210 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700211
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700212 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700213 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700214 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700215 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700216 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700217 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700218 memcpy(dst, src, n);
219 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700220 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700221 image_writer->FixupObject(obj, copy);
222}
223
Brian Carlstrom4873d462011-08-21 15:23:39 -0700224void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700225 DCHECK(orig != NULL);
226 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700228 // TODO: special case init of pointers to malloc data (or removal of these pointers)
229 if (orig->IsClass()) {
230 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
231 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700232 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700233 } else if (orig->IsMethod()) {
234 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700235 } else {
236 FixupInstanceFields(orig, copy);
237 }
238}
239
Brian Carlstrom4873d462011-08-21 15:23:39 -0700240void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700241 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700242 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700243}
244
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700245const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
246 // TODO: change to DCHECK when all code compiling
247 if (copy_code_array == NULL) {
248 return NULL;
249 }
250 const void* copy_code = copy_code_array->GetData();
251 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
252 if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700253 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700254 }
255 return copy_code;
256}
257
Brian Carlstrom4873d462011-08-21 15:23:39 -0700258void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700259 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700260
261 // OatWriter clears the code_array_ after writing the code.
262 // It replaces the code_ with an offset value we now adjust to be a pointer.
263 DCHECK(copy->code_array_ == NULL)
264 << PrettyMethod(orig)
265 << " orig_code_array_=" << orig->GetCodeArray() << " orig_code_=" << orig->GetCode()
266 << " copy_code_array_=" << copy->code_array_ << " orig_code_=" << copy->code_
267 << " jni_stub=" << Runtime::Current()->GetJniStubArray()
268 << " ame_stub=" << Runtime::Current()->GetAbstractMethodErrorStubArray();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700269 copy->invoke_stub_ = reinterpret_cast<Method::InvokeStub*>(FixupCode(copy->invoke_stub_array_, reinterpret_cast<void*>(orig->invoke_stub_)));
Brian Carlstrom16192862011-09-12 17:50:06 -0700270 if (orig->IsNative()) {
271 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniStubArray();
272 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
273 copy->native_method_ = copy_jni_stub_array_->GetData();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274 copy->code_ = oat_base_ + orig->GetOatCodeOffset();
Brian Carlstrom16192862011-09-12 17:50:06 -0700275 } else {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700276 DCHECK(copy->native_method_ == NULL) << copy->native_method_;
277 if (orig->IsAbstract()) {
278 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
279 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
280 copy->code_ = copy_ame_stub_array_->GetData();
281 } else {
282 copy->code_ = oat_base_ + orig->GetOatCodeOffset();
283 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700284 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700285}
286
Brian Carlstrom4873d462011-08-21 15:23:39 -0700287void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700288 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700289 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700290 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700291 }
292}
293
Brian Carlstrom4873d462011-08-21 15:23:39 -0700294void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
295 DCHECK(orig != NULL);
296 DCHECK(copy != NULL);
297 Class* klass = orig->GetClass();
298 DCHECK(klass != NULL);
299 FixupFields(orig,
300 copy,
301 klass->GetReferenceInstanceOffsets(),
302 false);
303}
304
305void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
306 DCHECK(orig != NULL);
307 DCHECK(copy != NULL);
308 FixupFields(orig,
309 copy,
310 orig->GetReferenceStaticOffsets(),
311 true);
312}
313
314void ImageWriter::FixupFields(const Object* orig,
315 Object* copy,
316 uint32_t ref_offsets,
317 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700318 if (ref_offsets != CLASS_WALK_SUPER) {
319 // Found a reference offset bitmap. Fixup the specified offsets.
320 while (ref_offsets != 0) {
321 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
323 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
324 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700325 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
326 }
327 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700328 // There is no reference offset bitmap. In the non-static case,
329 // walk up the class inheritance hierarchy and find reference
330 // offsets the hard way. In the static case, just consider this
331 // class.
332 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700333 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700334 klass = is_static ? NULL : klass->GetSuperClass()) {
335 size_t num_reference_fields = (is_static
336 ? klass->NumReferenceStaticFields()
337 : klass->NumReferenceInstanceFields());
338 for (size_t i = 0; i < num_reference_fields; ++i) {
339 Field* field = (is_static
340 ? klass->GetStaticField(i)
341 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 MemberOffset field_offset = field->GetOffset();
343 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
344 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700345 }
346 }
347 }
348}
349
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700350void ImageWriter::FixupDexCaches() {
351 typedef Set::const_iterator It; // TODO: C++0x auto
352 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
353 DexCache* orig = *it;
354 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
355 FixupDexCache(orig, copy);
356 }
357}
358
359void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
360 CHECK(orig != NULL);
361 CHECK(copy != NULL);
362
Ian Rogersad25ac52011-10-04 19:13:33 -0700363 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700364 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700365 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700366 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700367
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700368 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700369 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
370 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700371 if (orig_method != NULL && !InSourceSpace(orig_method)) {
372 continue;
373 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700374 // if it was resolved in the original, resolve it in the copy
Ian Rogersad25ac52011-10-04 19:13:33 -0700375 if (orig_method == NULL || (orig_method->IsStatic() &&
376 !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700377 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
378 if (orig_res_stub_code == 0) {
379 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700380 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700381 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
382 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
383 // Do we need to relocate this for this space?
384 if (!InSourceSpace(orig_res_stub_array)) {
385 continue;
386 }
387 // Compute the delta from the start of the resolution stub to its starting code.
388 // For ARM and X86 this is 0, for Thumb2 it is 1.
389 static size_t res_stub_delta = 0xFFFF;
390 if (res_stub_delta == 0xFFFF) {
391 uint32_t orig_res_stub_array_data =
392 reinterpret_cast<uint32_t>(orig_res_stub_array->GetData());
393 res_stub_delta = orig_res_stub_code - orig_res_stub_array_data;
394 DCHECK(res_stub_delta == 0 || res_stub_delta == 1);
395 }
396 // Compute address in image of resolution stub and the code address
397 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
398 int32_t image_res_stub_code =
399 reinterpret_cast<int32_t>(image_res_stub_array->GetData()) + res_stub_delta;
400 // Put the image code address in the array
401 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700402 } else if (orig_method->IsDirect()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700403 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
404 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
405 reinterpret_cast<int32_t>(copy_method->code_));
406 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
407 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
408 }
409 }
410}
411
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700412} // namespace art