blob: 4335a90323fde24035990b3e3543ac641adea66b [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>
6#include <vector>
7
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07008#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07009#include "file.h"
10#include "globals.h"
11#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070012#include "image.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070013#include "logging.h"
14#include "object.h"
15#include "space.h"
16#include "utils.h"
17
18namespace art {
19
20bool ImageWriter::Write(Space* space, const char* filename, byte* image_base) {
21 image_base_ = image_base;
22 if (!Init(space)) {
23 return false;
24 }
25 CalculateNewObjectOffsets();
26 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070027
Brian Carlstrom4e777d42011-08-15 13:53:52 -070028 scoped_ptr<File> file(OS::OpenFile(filename, true));
Brian Carlstromdb4d5402011-08-09 12:18:28 -070029 if (file == NULL) {
30 return false;
31 }
Brian Carlstrom4e777d42011-08-15 13:53:52 -070032 return file->WriteFully(image_->GetAddress(), image_top_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070033}
34
35bool ImageWriter::Init(Space* space) {
36 size_t size = space->Size();
37 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070039 image_.reset(MemMap::Map(length, prot));
Brian Carlstrom4e777d42011-08-15 13:53:52 -070040 if (image_ == NULL) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070041 return false;
42 }
43 return true;
44}
45
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046void ImageWriter::CalculateNewObjectOffsetsCallback(Object *obj, void *arg) {
47 DCHECK(obj != NULL);
48 DCHECK(arg != NULL);
49 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom4e777d42011-08-15 13:53:52 -070050 image_writer->SetImageOffset(obj, image_writer->image_top_);
Elliott Hughes04b63fd2011-08-16 09:40:10 -070051 image_writer->image_top_ += RoundUp(obj->SizeOf(), 8); // 64-bit alignment
Brian Carlstrom4e777d42011-08-15 13:53:52 -070052 DCHECK_LT(image_writer->image_top_, image_writer->image_->GetLength());
53}
54
55void ImageWriter::CalculateNewObjectOffsets() {
56 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
57 DCHECK(heap_bitmap != NULL);
58 DCHECK_EQ(0U, image_top_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070059 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_));
60 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
61 image_top_ += RoundUp(sizeof(image_header), 8); // 64-bit-alignment
Brian Carlstrom4e777d42011-08-15 13:53:52 -070062 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this);
63 DCHECK_LT(image_top_, image_->GetLength());
64 // Note that top_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -070065}
66
67void ImageWriter::CopyAndFixupObjects() {
68 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
69 DCHECK(heap_bitmap != NULL);
70 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this);
71}
72
73void ImageWriter::CopyAndFixupObjectsCallback(Object *obj, void *arg) {
74 DCHECK(obj != NULL);
75 DCHECK(arg != NULL);
76 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070077
Brian Carlstromdb4d5402011-08-09 12:18:28 -070078 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -070079 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070080 byte* src = reinterpret_cast<byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -070081 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -070082 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070083 memcpy(dst, src, n);
84 Object* copy = reinterpret_cast<Object*>(dst);
85 image_writer->FixupObject(obj, copy);
86}
87
88void ImageWriter::FixupObject(Object* orig, Object* copy) {
89 DCHECK(orig != NULL);
90 DCHECK(copy != NULL);
91 copy->klass_ = down_cast<Class*>(GetImageAddress(orig->klass_));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070092 // TODO: special case init of pointers to malloc data (or removal of these pointers)
93 if (orig->IsClass()) {
94 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
95 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070096 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
97 } else {
98 FixupInstanceFields(orig, copy);
99 }
100}
101
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700102void ImageWriter::FixupClass(Class* orig, Class* copy) {
103 FixupInstanceFields(orig, copy);
104 copy->descriptor_ = down_cast<String*>(GetImageAddress(orig->descriptor_));
105 copy->dex_cache_ = down_cast<DexCache*>(GetImageAddress(orig->dex_cache_));
106 copy->verify_error_class_ = down_cast<Class*>(GetImageAddress(orig->verify_error_class_));
107 copy->component_type_ = down_cast<Class*>(GetImageAddress(orig->component_type_));
108 copy->super_class_ = down_cast<Class*>(GetImageAddress(orig->super_class_));
109 copy->class_loader_ = down_cast<ClassLoader*>(GetImageAddress(orig->class_loader_));
110 copy->interfaces_ = down_cast<ObjectArray<Class>*>(GetImageAddress(orig->interfaces_));
111 copy->direct_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->direct_methods_));
112 copy->virtual_methods_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->virtual_methods_));
113 copy->vtable_ = down_cast<ObjectArray<Method>*>(GetImageAddress(orig->vtable_));
114 // TODO: convert iftable_ to heap allocated storage
115 copy->ifields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->ifields_));
116 copy->sfields_ = down_cast<ObjectArray<Field>*>(GetImageAddress(orig->sfields_));
117 copy->static_references_ = down_cast<ObjectArray<Object>*>(GetImageAddress(orig->static_references_));
118}
119
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700120void ImageWriter::FixupObjectArray(ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700121 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700122 const Object* element = orig->Get(i);
123 copy->Set(i, GetImageAddress(element));
124 }
125}
126
127void ImageWriter::FixupInstanceFields(Object* orig, Object* copy) {
128 uint32_t ref_offsets = orig->GetClass()->GetReferenceOffsets();
129 if (ref_offsets != CLASS_WALK_SUPER) {
130 // Found a reference offset bitmap. Fixup the specified offsets.
131 while (ref_offsets != 0) {
132 size_t right_shift = CLZ(ref_offsets);
133 size_t byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
134 const Object* ref = orig->GetFieldObject(byte_offset);
135 copy->SetFieldObject(byte_offset, GetImageAddress(ref));
136 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
137 }
138 } else {
139 // There is no reference offset bitmap for this class. Walk up
140 // the class inheritance hierarchy and find reference offsets the
141 // hard way.
142 for (Class *klass = orig->GetClass();
143 klass != NULL;
144 klass = klass->GetSuperClass()) {
145 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
146 size_t field_offset = klass->GetInstanceField(i)->GetOffset();
147 const Object* ref = orig->GetFieldObject(field_offset);
148 copy->SetFieldObject(field_offset, GetImageAddress(ref));
149 }
150 }
151 }
152}
153
154} // namespace art