blob: ef577aff14be154da7f90524298191c665471e93 [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 Carlstromaded5f72011-10-07 17:15:04 -070028 CHECK(image_filename != NULL);
29
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070030 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 Carlstrom58ae9412011-10-04 00:56:06 -070037 CHECK(!source_space_->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070038
Brian Carlstrome24fa612011-09-29 00:53:55 -070039 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 Carlstrom69b15fb2011-09-03 12:25:21 -070045 if (!Init()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046 return false;
47 }
Brian Carlstrom693267a2011-09-06 09:25:34 -070048 Heap::CollectGarbage();
Ian Rogers5d76c432011-10-31 21:42:49 -070049 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070050 CalculateNewObjectOffsets();
51 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070052
Brian Carlstrome24fa612011-09-29 00:53:55 -070053 UniquePtr<File> file(OS::OpenFile(image_filename, true));
Elliott Hughes90a33692011-08-30 13:27:07 -070054 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070056 return false;
57 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070058 bool success = file->WriteFully(image_->GetAddress(), image_top_);
59 if (!success) {
60 PLOG(ERROR) << "Failed to write image file " << image_filename;
61 return false;
62 }
63 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070064}
65
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070066bool ImageWriter::Init() {
67 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070068 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069 size_t length = RoundUp(size, kPageSize);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080070 image_.reset(MemMap::Map("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070071 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -070073 return false;
74 }
75 return true;
76}
77
Brian Carlstrom78128a62011-09-15 17:21:19 -070078void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070079 DCHECK(obj != NULL);
80 DCHECK(arg != NULL);
81 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070082 if (!image_writer->InSourceSpace(obj)) {
83 return;
84 }
Brian Carlstromc74255f2011-09-11 22:47:39 -070085
86 // if it is a string, we want to intern it if its not interned.
87 if (obj->IsString()) {
88 // we must be an interned string that was forward referenced and already assigned
89 if (IsImageOffsetAssigned(obj)) {
90 DCHECK_EQ(obj, obj->AsString()->Intern());
91 return;
92 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070093 SirtRef<String> interned(obj->AsString()->Intern());
94 if (obj != interned.get()) {
95 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -070096 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -070097 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -070098 }
99 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700100 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700101 return;
102 }
103 // else (obj == interned), nothing to do but fall through to the normal case
104 }
105
106 image_writer->AssignImageOffset(obj);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700107
108 // sniff out the DexCaches on this pass for use on the next pass
109 if (obj->IsClass()) {
110 Class* klass = obj->AsClass();
111 DexCache* dex_cache = klass->GetDexCache();
112 if (dex_cache != NULL) {
113 image_writer->dex_caches_.insert(dex_cache);
114 } else {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115 DCHECK(klass->IsArrayClass() || klass->IsPrimitive()) << PrettyClass(klass);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700116 }
117 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700118}
119
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700121 Runtime* runtime = Runtime::Current();
122 ClassLinker* class_linker = runtime->GetClassLinker();
123 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700124
125 // build an Object[] of all the DexCaches used in the source_space_
126 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
127 std::vector<DexCache*> source_space_dex_caches;
128 for (size_t i = 0; i < all_dex_caches.size(); i++) {
129 DexCache* dex_cache = all_dex_caches[i];
130 if (InSourceSpace(dex_cache)) {
131 source_space_dex_caches.push_back(dex_cache);
132 }
133 }
134 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
135 source_space_dex_caches.size());
136 for (size_t i = 0; i < source_space_dex_caches.size(); i++) {
137 dex_caches->Set(i, source_space_dex_caches[i]);
138 }
139
140 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700141 SirtRef<ObjectArray<Object> > image_roots(
142 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800143 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
145 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700146 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
147 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
148 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
149 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
150 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
151 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700152 image_roots->Set(ImageHeader::kCalleeSaveMethod,
153 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
154 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
155 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
156 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
157 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700158 image_roots->Set(ImageHeader::kOatLocation,
159 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700160 image_roots->Set(ImageHeader::kDexCaches,
161 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700162 image_roots->Set(ImageHeader::kClassRoots,
163 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
165 CHECK(image_roots->Get(i) != NULL);
166 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700167 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700168}
169
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700170void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700171 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700172
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700173 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
174 DCHECK(heap_bitmap != NULL);
175 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700176
Brian Carlstrom16192862011-09-12 17:50:06 -0700177 // leave space for the header, but do not write it yet, we need to
178 // know where image_roots is going to end up
Brian Carlstroma663ea52011-08-19 23:33:41 -0700179 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
180
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700181 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700182 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700183
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184 // Note that image_top_ is left at end of used space
185 oat_base_ = image_base_ + RoundUp(image_top_, kPageSize);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700186 const byte* oat_limit = oat_base_ + oat_file_->GetSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700187
Brian Carlstrom16192862011-09-12 17:50:06 -0700188 // return to write header at start of image with future location of image_roots
189 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700190 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700191 oat_file_->GetOatHeader().GetChecksum(),
192 reinterpret_cast<uint32_t>(oat_base_),
193 reinterpret_cast<uint32_t>(oat_limit));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700194 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700195}
196
197void ImageWriter::CopyAndFixupObjects() {
198 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
199 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 // TODO: heap validation can't handle this fix up pass
201 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700202 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
203 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700204}
205
Brian Carlstrom78128a62011-09-15 17:21:19 -0700206void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700207 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700208 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700209 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700210 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700211 if (!image_writer->InSourceSpace(object)) {
212 return;
213 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700214
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700215 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700217 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700218 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700219 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700220 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700221 memcpy(dst, src, n);
222 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700223 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700224 image_writer->FixupObject(obj, copy);
225}
226
Brian Carlstrom4873d462011-08-21 15:23:39 -0700227void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700228 DCHECK(orig != NULL);
229 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700231 // TODO: special case init of pointers to malloc data (or removal of these pointers)
232 if (orig->IsClass()) {
233 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
234 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700235 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700236 } else if (orig->IsMethod()) {
237 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700238 } else {
239 FixupInstanceFields(orig, copy);
240 }
241}
242
Brian Carlstrom4873d462011-08-21 15:23:39 -0700243void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700244 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700245 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700246}
247
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700248const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
249 // TODO: change to DCHECK when all code compiling
250 if (copy_code_array == NULL) {
251 return NULL;
252 }
253 const void* copy_code = copy_code_array->GetData();
254 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
255 if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700256 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700257 }
258 return copy_code;
259}
260
Brian Carlstrom4873d462011-08-21 15:23:39 -0700261void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700262 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700264 // OatWriter replaces the code_ and invoke_stub_ with offset values.
265 // Here we readjust to a pointer relative to oat_base_
266
267 // Every type of method can have an invoke stub
268 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
269 const byte* invoke_stub = (invoke_stub_offset != 0) ? (oat_base_ + invoke_stub_offset) : 0;
270 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
271
272 if (orig->IsAbstract()) {
273 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
274 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
275 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
276 copy->code_ = copy_ame_stub_array_->GetData();
277 return;
278 }
279
280 // Non-abstract methods typically have code
281 uint32_t code_offset = orig->GetOatCodeOffset();
282 const byte* code = (code_offset != 0) ? (oat_base_ + code_offset) : 0;
283 copy->code_ = code;
284
Brian Carlstrom16192862011-09-12 17:50:06 -0700285 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700286 // The native method's pointer is directed to a stub to lookup via dlsym.
287 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800288 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700289 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
290 copy->native_method_ = copy_jni_stub_array_->GetData();
291 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700292 // normal (non-abstract non-native) methods have mapping tables to relocate
293 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
294 const byte* mapping_table = (mapping_table_off != 0) ? (oat_base_ + mapping_table_off) : 0;
295 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
296
297 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
298 const byte* vmap_table = (vmap_table_offset != 0) ? (oat_base_ + vmap_table_offset) : 0;
299 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrom16192862011-09-12 17:50:06 -0700300 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700301}
302
Brian Carlstrom4873d462011-08-21 15:23:39 -0700303void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700304 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700305 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700307 }
308}
309
Brian Carlstrom4873d462011-08-21 15:23:39 -0700310void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
311 DCHECK(orig != NULL);
312 DCHECK(copy != NULL);
313 Class* klass = orig->GetClass();
314 DCHECK(klass != NULL);
315 FixupFields(orig,
316 copy,
317 klass->GetReferenceInstanceOffsets(),
318 false);
319}
320
321void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
322 DCHECK(orig != NULL);
323 DCHECK(copy != NULL);
324 FixupFields(orig,
325 copy,
326 orig->GetReferenceStaticOffsets(),
327 true);
328}
329
330void ImageWriter::FixupFields(const Object* orig,
331 Object* copy,
332 uint32_t ref_offsets,
333 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700334 if (ref_offsets != CLASS_WALK_SUPER) {
335 // Found a reference offset bitmap. Fixup the specified offsets.
336 while (ref_offsets != 0) {
337 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
339 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
340 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700341 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
342 }
343 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700344 // There is no reference offset bitmap. In the non-static case,
345 // walk up the class inheritance hierarchy and find reference
346 // offsets the hard way. In the static case, just consider this
347 // class.
348 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700349 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700350 klass = is_static ? NULL : klass->GetSuperClass()) {
351 size_t num_reference_fields = (is_static
352 ? klass->NumReferenceStaticFields()
353 : klass->NumReferenceInstanceFields());
354 for (size_t i = 0; i < num_reference_fields; ++i) {
355 Field* field = (is_static
356 ? klass->GetStaticField(i)
357 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358 MemberOffset field_offset = field->GetOffset();
359 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
360 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700361 }
362 }
363 }
364}
365
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700366void ImageWriter::FixupDexCaches() {
367 typedef Set::const_iterator It; // TODO: C++0x auto
368 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
369 DexCache* orig = *it;
370 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
371 FixupDexCache(orig, copy);
372 }
373}
374
375void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
376 CHECK(orig != NULL);
377 CHECK(copy != NULL);
378
Ian Rogersad25ac52011-10-04 19:13:33 -0700379 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700380 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700381 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700382 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700383
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700384 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700385 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
386 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700387 if (orig_method != NULL && !InSourceSpace(orig_method)) {
388 continue;
389 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700390 // if it was resolved in the original, resolve it in the copy
Ian Rogersad25ac52011-10-04 19:13:33 -0700391 if (orig_method == NULL || (orig_method->IsStatic() &&
392 !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700393 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
394 if (orig_res_stub_code == 0) {
395 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700396 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700397 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
398 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
399 // Do we need to relocate this for this space?
400 if (!InSourceSpace(orig_res_stub_array)) {
401 continue;
402 }
403 // Compute the delta from the start of the resolution stub to its starting code.
404 // For ARM and X86 this is 0, for Thumb2 it is 1.
405 static size_t res_stub_delta = 0xFFFF;
406 if (res_stub_delta == 0xFFFF) {
407 uint32_t orig_res_stub_array_data =
408 reinterpret_cast<uint32_t>(orig_res_stub_array->GetData());
409 res_stub_delta = orig_res_stub_code - orig_res_stub_array_data;
410 DCHECK(res_stub_delta == 0 || res_stub_delta == 1);
411 }
412 // Compute address in image of resolution stub and the code address
413 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
414 int32_t image_res_stub_code =
415 reinterpret_cast<int32_t>(image_res_stub_array->GetData()) + res_stub_delta;
416 // Put the image code address in the array
417 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700418 } else if (orig_method->IsDirect()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700419 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
420 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
421 reinterpret_cast<int32_t>(copy_method->code_));
422 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
423 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
424 }
425 }
426}
427
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700428} // namespace art