blob: c265838a134900613949d75de2bce40269c8150b [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 Carlstrom69b15fb2011-09-03 12:25:21 -070026bool ImageWriter::Write(const char* filename, uintptr_t image_base) {
27 CHECK_NE(image_base, 0U);
28 image_base_ = reinterpret_cast<byte*>(image_base);
29
30 const std::vector<Space*>& spaces = Heap::GetSpaces();
31 // currently just write the last space, assuming it is the space that was being used for allocation
32 CHECK_GE(spaces.size(), 1U);
33 source_space_ = spaces[spaces.size()-1];
34
35 if (!Init()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070036 return false;
37 }
Brian Carlstrom693267a2011-09-06 09:25:34 -070038 Heap::CollectGarbage();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039 CalculateNewObjectOffsets();
40 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070041
Elliott Hughes90a33692011-08-30 13:27:07 -070042 UniquePtr<File> file(OS::OpenFile(filename, true));
43 if (file.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 return false;
45 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -070046 return file->WriteFully(image_->GetAddress(), image_top_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070047}
48
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070049bool ImageWriter::Init() {
50 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070053 image_.reset(MemMap::Map(length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070054 if (image_.get() == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070055 return false;
56 }
57 return true;
58}
59
Brian Carlstrom4873d462011-08-21 15:23:39 -070060void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void *arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070061 DCHECK(obj != NULL);
62 DCHECK(arg != NULL);
63 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070064 if (!image_writer->InSourceSpace(obj)) {
65 return;
66 }
Brian Carlstromc74255f2011-09-11 22:47:39 -070067
68 // if it is a string, we want to intern it if its not interned.
69 if (obj->IsString()) {
70 // we must be an interned string that was forward referenced and already assigned
71 if (IsImageOffsetAssigned(obj)) {
72 DCHECK_EQ(obj, obj->AsString()->Intern());
73 return;
74 }
75 String* interned = obj->AsString()->Intern();
76 if (obj != interned) {
77 if (!IsImageOffsetAssigned(interned)) {
78 // interned obj is after us, allocate its location early
79 image_writer->AssignImageOffset(interned);
80 }
81 // point those looking for this object to the interned version.
82 SetImageOffset(obj, GetImageOffset(interned));
83 return;
84 }
85 // else (obj == interned), nothing to do but fall through to the normal case
86 }
87
88 image_writer->AssignImageOffset(obj);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070089
90 // sniff out the DexCaches on this pass for use on the next pass
91 if (obj->IsClass()) {
92 Class* klass = obj->AsClass();
93 DexCache* dex_cache = klass->GetDexCache();
94 if (dex_cache != NULL) {
95 image_writer->dex_caches_.insert(dex_cache);
96 } else {
97 DCHECK(klass->IsArrayClass() || klass->IsPrimitive());
98 }
99 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700100}
101
102void ImageWriter::CalculateNewObjectOffsets() {
103 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
104 DCHECK(heap_bitmap != NULL);
105 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700106
Brian Carlstromc74255f2011-09-11 22:47:39 -0700107 // leave space for the header, but do not write it yet
Brian Carlstroma663ea52011-08-19 23:33:41 -0700108 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
109
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700110 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700111 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700112
Brian Carlstromc74255f2011-09-11 22:47:39 -0700113 // return to write header at start of image
114 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700115 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
116
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700117 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700118}
119
120void ImageWriter::CopyAndFixupObjects() {
121 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
122 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700123 // TODO: heap validation can't handle this fix up pass
124 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700125 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
126 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700127}
128
Brian Carlstrom4873d462011-08-21 15:23:39 -0700129void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void *arg) {
130 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700131 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700132 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700133 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700134 if (!image_writer->InSourceSpace(object)) {
135 return;
136 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700137
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700138 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700139 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700140 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700141 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700142 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700143 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700144 memcpy(dst, src, n);
145 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700146 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700147 image_writer->FixupObject(obj, copy);
148}
149
Brian Carlstrom4873d462011-08-21 15:23:39 -0700150void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700151 DCHECK(orig != NULL);
152 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700153 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700154 // TODO: special case init of pointers to malloc data (or removal of these pointers)
155 if (orig->IsClass()) {
156 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
157 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700158 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
159 } else {
160 FixupInstanceFields(orig, copy);
161 }
162}
163
Brian Carlstrom4873d462011-08-21 15:23:39 -0700164void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700165 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700166 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700167}
168
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700169const void* FixupCode(const ByteArray* copy_code_array, const void* orig_code) {
170 // TODO: change to DCHECK when all code compiling
171 if (copy_code_array == NULL) {
172 return NULL;
173 }
174 const void* copy_code = copy_code_array->GetData();
175 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
176 if ((reinterpret_cast<uintptr_t>(orig_code) % 2) == 1) {
177 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(copy_code) + 1);
178 }
179 return copy_code;
180}
181
Brian Carlstrom4873d462011-08-21 15:23:39 -0700182void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700183 FixupInstanceFields(orig, copy);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700184 // TODO: convert shorty_ to heap allocated storage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700185 copy->code_ = FixupCode(copy->code_array_, orig->code_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700186 copy->invoke_stub_ = reinterpret_cast<Method::InvokeStub*>(FixupCode(copy->invoke_stub_array_, reinterpret_cast<void*>(orig->invoke_stub_)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700187}
188
Brian Carlstrom4873d462011-08-21 15:23:39 -0700189void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700190 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700191 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700193 }
194}
195
Brian Carlstrom4873d462011-08-21 15:23:39 -0700196void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
197 DCHECK(orig != NULL);
198 DCHECK(copy != NULL);
199 Class* klass = orig->GetClass();
200 DCHECK(klass != NULL);
201 FixupFields(orig,
202 copy,
203 klass->GetReferenceInstanceOffsets(),
204 false);
205}
206
207void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
208 DCHECK(orig != NULL);
209 DCHECK(copy != NULL);
210 FixupFields(orig,
211 copy,
212 orig->GetReferenceStaticOffsets(),
213 true);
214}
215
216void ImageWriter::FixupFields(const Object* orig,
217 Object* copy,
218 uint32_t ref_offsets,
219 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700220 if (ref_offsets != CLASS_WALK_SUPER) {
221 // Found a reference offset bitmap. Fixup the specified offsets.
222 while (ref_offsets != 0) {
223 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
225 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
226 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700227 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
228 }
229 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700230 // There is no reference offset bitmap. In the non-static case,
231 // walk up the class inheritance hierarchy and find reference
232 // offsets the hard way. In the static case, just consider this
233 // class.
234 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700235 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700236 klass = is_static ? NULL : klass->GetSuperClass()) {
237 size_t num_reference_fields = (is_static
238 ? klass->NumReferenceStaticFields()
239 : klass->NumReferenceInstanceFields());
240 for (size_t i = 0; i < num_reference_fields; ++i) {
241 Field* field = (is_static
242 ? klass->GetStaticField(i)
243 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700244 MemberOffset field_offset = field->GetOffset();
245 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
246 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700247 }
248 }
249 }
250}
251
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700252void ImageWriter::FixupDexCaches() {
253 typedef Set::const_iterator It; // TODO: C++0x auto
254 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
255 DexCache* orig = *it;
256 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
257 FixupDexCache(orig, copy);
258 }
259}
260
261void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
262 CHECK(orig != NULL);
263 CHECK(copy != NULL);
264
265 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
266 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
267 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
268 Method* orig_method = orig->GetResolvedMethod(i);
269 // if it was resolved in the original, resolve it in the copy
270 if (orig_method != NULL
271 && InSourceSpace(orig_method)
272 && orig_method == orig_cadms->GetResolvedMethod(i)) {
273 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
274 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
275 reinterpret_cast<int32_t>(copy_method->code_));
276 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
277 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
278 }
279 }
280}
281
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700282} // namespace art