blob: adc7b780afc372a2115bb6727d7f0f9f127c2bec [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"
Brian Carlstromae826982011-11-09 01:33:42 -080012#include "compiled_method.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070013#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070014#include "file.h"
15#include "globals.h"
16#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070017#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070018#include "intern_table.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070019#include "logging.h"
20#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "runtime.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070022#include "space.h"
23#include "utils.h"
24
25namespace art {
26
Brian Carlstromae826982011-11-09 01:33:42 -080027bool ImageWriter::Write(const char* image_filename,
28 uintptr_t image_base,
29 const std::string& oat_filename,
30 const std::string& strip_location_prefix) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070031 CHECK(image_filename != NULL);
32
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070033 CHECK_NE(image_base, 0U);
34 image_base_ = reinterpret_cast<byte*>(image_base);
35
36 const std::vector<Space*>& spaces = Heap::GetSpaces();
37 // currently just write the last space, assuming it is the space that was being used for allocation
38 CHECK_GE(spaces.size(), 1U);
39 source_space_ = spaces[spaces.size()-1];
Brian Carlstrom58ae9412011-10-04 00:56:06 -070040 CHECK(!source_space_->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070041
Brian Carlstromae826982011-11-09 01:33:42 -080042 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
43 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
44 for (size_t i = 0; i < all_dex_caches.size(); i++) {
45 DexCache* dex_cache = all_dex_caches[i];
46 if (InSourceSpace(dex_cache)) {
47 dex_caches_.insert(dex_cache);
48 }
49 }
50
Brian Carlstrome24fa612011-09-29 00:53:55 -070051 oat_file_.reset(OatFile::Open(oat_filename, strip_location_prefix, NULL));
52 if (oat_file_.get() == NULL) {
53 LOG(ERROR) << "Failed to open oat file " << oat_filename;
54 return false;
55 }
56
Brian Carlstromae826982011-11-09 01:33:42 -080057 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070058 return false;
59 }
Brian Carlstromae826982011-11-09 01:33:42 -080060 PruneNonImageClasses();
Brian Carlstrom693267a2011-09-06 09:25:34 -070061 Heap::CollectGarbage();
Brian Carlstromae826982011-11-09 01:33:42 -080062#ifndef NDEBUG
63 CheckNonImageClassesRemoved();
64#endif
Ian Rogers5d76c432011-10-31 21:42:49 -070065 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070066 CalculateNewObjectOffsets();
67 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070068
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 UniquePtr<File> file(OS::OpenFile(image_filename, true));
Elliott Hughes90a33692011-08-30 13:27:07 -070070 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070072 return false;
73 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 bool success = file->WriteFully(image_->GetAddress(), image_top_);
75 if (!success) {
76 PLOG(ERROR) << "Failed to write image file " << image_filename;
77 return false;
78 }
79 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070080}
81
Brian Carlstromae826982011-11-09 01:33:42 -080082bool ImageWriter::AllocMemory() {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070083 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070084 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070085 size_t length = RoundUp(size, kPageSize);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080086 image_.reset(MemMap::Map("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070087 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070088 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -070089 return false;
90 }
91 return true;
92}
93
Brian Carlstromae826982011-11-09 01:33:42 -080094bool ImageWriter::IsImageClass(const Class* klass) {
95 if (image_classes_ == NULL) {
96 return true;
97 }
98 while (klass->IsArrayClass()) {
99 klass = klass->GetComponentType();
100 }
101 if (klass->IsPrimitive()) {
102 return true;
103 }
104 const std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
105 return image_classes_->find(descriptor) != image_classes_->end();
106}
107
108
109struct NonImageClasses {
110 ImageWriter* image_writer;
111 std::set<std::string>* non_image_classes;
112};
113
114void ImageWriter::PruneNonImageClasses() {
115 if (image_classes_ == NULL) {
116 return;
117 }
118 Runtime* runtime = Runtime::Current();
119 ClassLinker* class_linker = runtime->GetClassLinker();
120
121 std::set<std::string> non_image_classes;
122 NonImageClasses context;
123 context.image_writer = this;
124 context.non_image_classes = &non_image_classes;
125 class_linker->VisitClasses(NonImageClassesVisitor, &context);
126
127 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
128 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
129 class_linker->RemoveClass(*it, NULL);
130 }
131
132 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
133 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
134 DexCache* dex_cache = *it;
135 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
136 Class* klass = dex_cache->GetResolvedType(i);
137 if (klass != NULL && !IsImageClass(klass)) {
138 dex_cache->SetResolvedType(i, NULL);
139 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
140 }
141 }
142 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
143 Method* method = dex_cache->GetResolvedMethod(i);
144 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
145 dex_cache->SetResolvedMethod(i, NULL);
146 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
147 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
148 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethodTrampoline(i, res_trampoline);
149 }
150 }
151 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
152 Field* field = dex_cache->GetResolvedField(i);
153 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
154 dex_cache->SetResolvedField(i, NULL);
155 }
156 }
157 }
158}
159
160bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
161 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
162 if (!context->image_writer->IsImageClass(klass)) {
163 context->non_image_classes->insert(klass->GetDescriptor()->ToModifiedUtf8());
164 }
165 return true;
166}
167
168void ImageWriter::CheckNonImageClassesRemoved() {
169 if (image_classes_ == NULL) {
170 return;
171 }
172 Heap::GetLiveBits()->Walk(CheckNonImageClassesRemovedCallback, this);
173}
174
175void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
176 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
177 if (!obj->IsClass()) {
178 return;
179 }
180 Class* klass = obj->AsClass();
181 CHECK(image_writer->IsImageClass(klass)) << klass->GetDescriptor()->ToModifiedUtf8();
182}
183
Brian Carlstrom78128a62011-09-15 17:21:19 -0700184void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700185 DCHECK(obj != NULL);
186 DCHECK(arg != NULL);
187 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700188 if (!image_writer->InSourceSpace(obj)) {
189 return;
190 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700191
192 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800193 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700194 // we must be an interned string that was forward referenced and already assigned
195 if (IsImageOffsetAssigned(obj)) {
196 DCHECK_EQ(obj, obj->AsString()->Intern());
197 return;
198 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700199 SirtRef<String> interned(obj->AsString()->Intern());
200 if (obj != interned.get()) {
201 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700202 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700203 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700204 }
205 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700206 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700207 return;
208 }
209 // else (obj == interned), nothing to do but fall through to the normal case
210 }
211
212 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700213}
214
Brian Carlstrome24fa612011-09-29 00:53:55 -0700215ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700216 Runtime* runtime = Runtime::Current();
217 ClassLinker* class_linker = runtime->GetClassLinker();
218 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700219
220 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700221 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800222 dex_caches_.size());
223 int i = 0;
224 typedef Set::const_iterator It; // TODO: C++0x auto
225 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
226 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700227 }
228
229 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700230 SirtRef<ObjectArray<Object> > image_roots(
231 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800232 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
234 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700235 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
236 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
237 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
238 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
239 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
240 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700241 image_roots->Set(ImageHeader::kCalleeSaveMethod,
242 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
243 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
244 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
245 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
246 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247 image_roots->Set(ImageHeader::kOatLocation,
248 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700249 image_roots->Set(ImageHeader::kDexCaches,
250 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700251 image_roots->Set(ImageHeader::kClassRoots,
252 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
254 CHECK(image_roots->Get(i) != NULL);
255 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700256 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700257}
258
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700259void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700260 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700261
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700262 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
263 DCHECK(heap_bitmap != NULL);
264 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700265
Brian Carlstrom16192862011-09-12 17:50:06 -0700266 // leave space for the header, but do not write it yet, we need to
267 // know where image_roots is going to end up
Brian Carlstroma663ea52011-08-19 23:33:41 -0700268 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
269
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700270 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700271 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700272
Brian Carlstrome24fa612011-09-29 00:53:55 -0700273 // Note that image_top_ is left at end of used space
274 oat_base_ = image_base_ + RoundUp(image_top_, kPageSize);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700275 const byte* oat_limit = oat_base_ + oat_file_->GetSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700276
Brian Carlstrom16192862011-09-12 17:50:06 -0700277 // return to write header at start of image with future location of image_roots
278 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700279 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700280 oat_file_->GetOatHeader().GetChecksum(),
281 reinterpret_cast<uint32_t>(oat_base_),
282 reinterpret_cast<uint32_t>(oat_limit));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700283 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700284}
285
286void ImageWriter::CopyAndFixupObjects() {
287 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
288 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 // TODO: heap validation can't handle this fix up pass
290 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700291 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
292 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700293}
294
Brian Carlstrom78128a62011-09-15 17:21:19 -0700295void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700296 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700297 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700298 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700299 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700300 if (!image_writer->InSourceSpace(object)) {
301 return;
302 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700303
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700304 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700305 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700306 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700307 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700308 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700309 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700310 memcpy(dst, src, n);
311 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700312 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700313 image_writer->FixupObject(obj, copy);
314}
315
Brian Carlstrom4873d462011-08-21 15:23:39 -0700316void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700317 DCHECK(orig != NULL);
318 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700319 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700320 // TODO: special case init of pointers to malloc data (or removal of these pointers)
321 if (orig->IsClass()) {
322 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
323 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700324 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700325 } else if (orig->IsMethod()) {
326 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700327 } else {
328 FixupInstanceFields(orig, copy);
329 }
330}
331
Brian Carlstrom4873d462011-08-21 15:23:39 -0700332void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700333 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700334 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700335}
336
Brian Carlstromae826982011-11-09 01:33:42 -0800337static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700338 // TODO: change to DCHECK when all code compiling
339 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800340 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700341 }
Brian Carlstromae826982011-11-09 01:33:42 -0800342 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700343 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800344 if ((orig_code % 2) == 1) {
345 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700346 }
347 return copy_code;
348}
349
Brian Carlstrom4873d462011-08-21 15:23:39 -0700350void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700351 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700353 // OatWriter replaces the code_ and invoke_stub_ with offset values.
354 // Here we readjust to a pointer relative to oat_base_
355
356 // Every type of method can have an invoke stub
357 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800358 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
360
361 if (orig->IsAbstract()) {
362 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
363 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
364 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
365 copy->code_ = copy_ame_stub_array_->GetData();
366 return;
367 }
368
369 // Non-abstract methods typically have code
370 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800371 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372 copy->code_ = code;
373
Brian Carlstrom16192862011-09-12 17:50:06 -0700374 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700375 // The native method's pointer is directed to a stub to lookup via dlsym.
376 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800377 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700378 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
379 copy->native_method_ = copy_jni_stub_array_->GetData();
380 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700381 // normal (non-abstract non-native) methods have mapping tables to relocate
382 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800383 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700384 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
385
386 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800387 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700388 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrom16192862011-09-12 17:50:06 -0700389 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700390}
391
Brian Carlstrom4873d462011-08-21 15:23:39 -0700392void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700393 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700394 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700396 }
397}
398
Brian Carlstrom4873d462011-08-21 15:23:39 -0700399void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
400 DCHECK(orig != NULL);
401 DCHECK(copy != NULL);
402 Class* klass = orig->GetClass();
403 DCHECK(klass != NULL);
404 FixupFields(orig,
405 copy,
406 klass->GetReferenceInstanceOffsets(),
407 false);
408}
409
410void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
411 DCHECK(orig != NULL);
412 DCHECK(copy != NULL);
413 FixupFields(orig,
414 copy,
415 orig->GetReferenceStaticOffsets(),
416 true);
417}
418
419void ImageWriter::FixupFields(const Object* orig,
420 Object* copy,
421 uint32_t ref_offsets,
422 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700423 if (ref_offsets != CLASS_WALK_SUPER) {
424 // Found a reference offset bitmap. Fixup the specified offsets.
425 while (ref_offsets != 0) {
426 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700427 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
428 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
429 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700430 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
431 }
432 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700433 // There is no reference offset bitmap. In the non-static case,
434 // walk up the class inheritance hierarchy and find reference
435 // offsets the hard way. In the static case, just consider this
436 // class.
437 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700438 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700439 klass = is_static ? NULL : klass->GetSuperClass()) {
440 size_t num_reference_fields = (is_static
441 ? klass->NumReferenceStaticFields()
442 : klass->NumReferenceInstanceFields());
443 for (size_t i = 0; i < num_reference_fields; ++i) {
444 Field* field = (is_static
445 ? klass->GetStaticField(i)
446 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700447 MemberOffset field_offset = field->GetOffset();
448 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
449 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700450 }
451 }
452 }
453}
454
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700455void ImageWriter::FixupDexCaches() {
456 typedef Set::const_iterator It; // TODO: C++0x auto
457 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
458 DexCache* orig = *it;
459 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
460 FixupDexCache(orig, copy);
461 }
462}
463
464void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
465 CHECK(orig != NULL);
466 CHECK(copy != NULL);
467
Ian Rogersad25ac52011-10-04 19:13:33 -0700468 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700469 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700470 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700471 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700472
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700473 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700474 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
475 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700476 if (orig_method != NULL && !InSourceSpace(orig_method)) {
477 continue;
478 }
Brian Carlstromae826982011-11-09 01:33:42 -0800479 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
480 // we need to use the stub in the static method case to ensure <clinit> is run.
481 if (orig_method == NULL
482 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700483 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
484 if (orig_res_stub_code == 0) {
485 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700486 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700487 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
488 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
489 // Do we need to relocate this for this space?
490 if (!InSourceSpace(orig_res_stub_array)) {
491 continue;
492 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700493 // Compute address in image of resolution stub and the code address
494 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800495 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700496 // Put the image code address in the array
497 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700498 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800499 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700500 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
501 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
502 reinterpret_cast<int32_t>(copy_method->code_));
503 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
504 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
505 }
506 }
507}
508
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700509} // namespace art