blob: 861a878bf267324d5c7dc94c8061a8fc95692d4b [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"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070022#include "runtime.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070023#include "space.h"
24#include "utils.h"
25
26namespace art {
27
Brian Carlstromae826982011-11-09 01:33:42 -080028bool ImageWriter::Write(const char* image_filename,
29 uintptr_t image_base,
30 const std::string& oat_filename,
31 const std::string& strip_location_prefix) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070032 CHECK(image_filename != NULL);
33
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070034 CHECK_NE(image_base, 0U);
35 image_base_ = reinterpret_cast<byte*>(image_base);
36
37 const std::vector<Space*>& spaces = Heap::GetSpaces();
38 // currently just write the last space, assuming it is the space that was being used for allocation
39 CHECK_GE(spaces.size(), 1U);
40 source_space_ = spaces[spaces.size()-1];
Brian Carlstrom58ae9412011-10-04 00:56:06 -070041 CHECK(!source_space_->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070042
Brian Carlstromae826982011-11-09 01:33:42 -080043 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
44 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
45 for (size_t i = 0; i < all_dex_caches.size(); i++) {
46 DexCache* dex_cache = all_dex_caches[i];
47 if (InSourceSpace(dex_cache)) {
48 dex_caches_.insert(dex_cache);
49 }
50 }
51
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 oat_file_.reset(OatFile::Open(oat_filename, strip_location_prefix, NULL));
53 if (oat_file_.get() == NULL) {
54 LOG(ERROR) << "Failed to open oat file " << oat_filename;
55 return false;
56 }
57
Brian Carlstromae826982011-11-09 01:33:42 -080058 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070059 return false;
60 }
Brian Carlstromae826982011-11-09 01:33:42 -080061 PruneNonImageClasses();
Brian Carlstrom693267a2011-09-06 09:25:34 -070062 Heap::CollectGarbage();
Brian Carlstromae826982011-11-09 01:33:42 -080063#ifndef NDEBUG
64 CheckNonImageClassesRemoved();
65#endif
Ian Rogers5d76c432011-10-31 21:42:49 -070066 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070067 CalculateNewObjectOffsets();
68 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070069
Brian Carlstrome24fa612011-09-29 00:53:55 -070070 UniquePtr<File> file(OS::OpenFile(image_filename, true));
Elliott Hughes90a33692011-08-30 13:27:07 -070071 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070073 return false;
74 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 bool success = file->WriteFully(image_->GetAddress(), image_top_);
76 if (!success) {
77 PLOG(ERROR) << "Failed to write image file " << image_filename;
78 return false;
79 }
80 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070081}
82
Brian Carlstromae826982011-11-09 01:33:42 -080083bool ImageWriter::AllocMemory() {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070084 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070085 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070086 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -080087 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070088 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -070090 return false;
91 }
92 return true;
93}
94
Brian Carlstromae826982011-11-09 01:33:42 -080095bool ImageWriter::IsImageClass(const Class* klass) {
96 if (image_classes_ == NULL) {
97 return true;
98 }
99 while (klass->IsArrayClass()) {
100 klass = klass->GetComponentType();
101 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800102 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800103 return true;
104 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800106 return image_classes_->find(descriptor) != image_classes_->end();
107}
108
109
110struct NonImageClasses {
111 ImageWriter* image_writer;
112 std::set<std::string>* non_image_classes;
113};
114
115void ImageWriter::PruneNonImageClasses() {
116 if (image_classes_ == NULL) {
117 return;
118 }
119 Runtime* runtime = Runtime::Current();
120 ClassLinker* class_linker = runtime->GetClassLinker();
121
122 std::set<std::string> non_image_classes;
123 NonImageClasses context;
124 context.image_writer = this;
125 context.non_image_classes = &non_image_classes;
126 class_linker->VisitClasses(NonImageClassesVisitor, &context);
127
128 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
129 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800130 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800131 }
132
133 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
134 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
135 DexCache* dex_cache = *it;
136 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
137 Class* klass = dex_cache->GetResolvedType(i);
138 if (klass != NULL && !IsImageClass(klass)) {
139 dex_cache->SetResolvedType(i, NULL);
140 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
141 }
142 }
143 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
144 Method* method = dex_cache->GetResolvedMethod(i);
145 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
146 dex_cache->SetResolvedMethod(i, NULL);
147 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
148 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
149 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethodTrampoline(i, res_trampoline);
150 }
151 }
152 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
153 Field* field = dex_cache->GetResolvedField(i);
154 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
155 dex_cache->SetResolvedField(i, NULL);
156 }
157 }
158 }
159}
160
161bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
162 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
163 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800164 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800165 }
166 return true;
167}
168
169void ImageWriter::CheckNonImageClassesRemoved() {
170 if (image_classes_ == NULL) {
171 return;
172 }
173 Heap::GetLiveBits()->Walk(CheckNonImageClassesRemovedCallback, this);
174}
175
176void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
177 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
178 if (!obj->IsClass()) {
179 return;
180 }
181 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800182 if (!image_writer->IsImageClass(klass)) {
183 image_writer->DumpImageClasses();
184 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
185 << " " << PrettyDescriptor(klass);
186 }
187}
188
189void ImageWriter::DumpImageClasses() {
190 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
191 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
192 LOG(INFO) << " " << *it;
193 }
Brian Carlstromae826982011-11-09 01:33:42 -0800194}
195
Brian Carlstrom78128a62011-09-15 17:21:19 -0700196void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700197 DCHECK(obj != NULL);
198 DCHECK(arg != NULL);
199 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700200 if (!image_writer->InSourceSpace(obj)) {
201 return;
202 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700203
204 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800205 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700206 // we must be an interned string that was forward referenced and already assigned
207 if (IsImageOffsetAssigned(obj)) {
208 DCHECK_EQ(obj, obj->AsString()->Intern());
209 return;
210 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700211 SirtRef<String> interned(obj->AsString()->Intern());
212 if (obj != interned.get()) {
213 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700214 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700215 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700216 }
217 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700218 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700219 return;
220 }
221 // else (obj == interned), nothing to do but fall through to the normal case
222 }
223
224 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700225}
226
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700228 Runtime* runtime = Runtime::Current();
229 ClassLinker* class_linker = runtime->GetClassLinker();
230 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700231
232 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700233 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800234 dex_caches_.size());
235 int i = 0;
236 typedef Set::const_iterator It; // TODO: C++0x auto
237 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
238 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700239 }
240
241 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700242 SirtRef<ObjectArray<Object> > image_roots(
243 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800244 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
246 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700247 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
248 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
249 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
250 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
251 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
252 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700253 image_roots->Set(ImageHeader::kCalleeSaveMethod,
254 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
255 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
256 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
257 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
258 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700259 image_roots->Set(ImageHeader::kOatLocation,
260 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700261 image_roots->Set(ImageHeader::kDexCaches,
262 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700263 image_roots->Set(ImageHeader::kClassRoots,
264 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700265 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
266 CHECK(image_roots->Get(i) != NULL);
267 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700268 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700269}
270
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700271void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700272 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700273
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700274 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
275 DCHECK(heap_bitmap != NULL);
276 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700277
Brian Carlstrom16192862011-09-12 17:50:06 -0700278 // leave space for the header, but do not write it yet, we need to
279 // know where image_roots is going to end up
Brian Carlstroma663ea52011-08-19 23:33:41 -0700280 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
281
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700282 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700283 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700284
Brian Carlstrome24fa612011-09-29 00:53:55 -0700285 // Note that image_top_ is left at end of used space
286 oat_base_ = image_base_ + RoundUp(image_top_, kPageSize);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700287 const byte* oat_limit = oat_base_ + oat_file_->GetSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700288
Brian Carlstrom16192862011-09-12 17:50:06 -0700289 // return to write header at start of image with future location of image_roots
290 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700291 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700292 oat_file_->GetOatHeader().GetChecksum(),
293 reinterpret_cast<uint32_t>(oat_base_),
294 reinterpret_cast<uint32_t>(oat_limit));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700295 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700296}
297
298void ImageWriter::CopyAndFixupObjects() {
299 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
300 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 // TODO: heap validation can't handle this fix up pass
302 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700303 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
304 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700305}
306
Brian Carlstrom78128a62011-09-15 17:21:19 -0700307void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700308 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700309 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700310 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700311 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700312 if (!image_writer->InSourceSpace(object)) {
313 return;
314 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700315
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700316 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700317 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700318 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700319 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700320 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700321 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700322 memcpy(dst, src, n);
323 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700324 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700325 image_writer->FixupObject(obj, copy);
326}
327
Brian Carlstrom4873d462011-08-21 15:23:39 -0700328void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700329 DCHECK(orig != NULL);
330 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700332 // TODO: special case init of pointers to malloc data (or removal of these pointers)
333 if (orig->IsClass()) {
334 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
335 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700336 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700337 } else if (orig->IsMethod()) {
338 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700339 } else {
340 FixupInstanceFields(orig, copy);
341 }
342}
343
Brian Carlstrom4873d462011-08-21 15:23:39 -0700344void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700345 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700346 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700347}
348
Brian Carlstromae826982011-11-09 01:33:42 -0800349static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700350 // TODO: change to DCHECK when all code compiling
351 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800352 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700353 }
Brian Carlstromae826982011-11-09 01:33:42 -0800354 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700355 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800356 if ((orig_code % 2) == 1) {
357 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700358 }
359 return copy_code;
360}
361
Brian Carlstrom4873d462011-08-21 15:23:39 -0700362void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700363 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700364
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700365 // OatWriter replaces the code_ and invoke_stub_ with offset values.
366 // Here we readjust to a pointer relative to oat_base_
367
368 // Every type of method can have an invoke stub
369 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800370 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700371 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
372
373 if (orig->IsAbstract()) {
374 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
375 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
376 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
377 copy->code_ = copy_ame_stub_array_->GetData();
378 return;
379 }
380
381 // Non-abstract methods typically have code
382 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800383 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700384 copy->code_ = code;
385
Brian Carlstrom16192862011-09-12 17:50:06 -0700386 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700387 // The native method's pointer is directed to a stub to lookup via dlsym.
388 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800389 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700390 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
391 copy->native_method_ = copy_jni_stub_array_->GetData();
392 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393 // normal (non-abstract non-native) methods have mapping tables to relocate
394 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800395 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700396 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
397
398 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800399 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700400 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800401
402 uint32_t gc_map_offset = orig->GetOatGcMapOffset();
403 const byte* gc_map = GetOatAddress(gc_map_offset);
404 copy->gc_map_ = reinterpret_cast<const uint8_t*>(gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700405 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700406}
407
Brian Carlstrom4873d462011-08-21 15:23:39 -0700408void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700409 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700410 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700411 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700412 }
413}
414
Brian Carlstrom4873d462011-08-21 15:23:39 -0700415void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
416 DCHECK(orig != NULL);
417 DCHECK(copy != NULL);
418 Class* klass = orig->GetClass();
419 DCHECK(klass != NULL);
420 FixupFields(orig,
421 copy,
422 klass->GetReferenceInstanceOffsets(),
423 false);
424}
425
426void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
427 DCHECK(orig != NULL);
428 DCHECK(copy != NULL);
429 FixupFields(orig,
430 copy,
431 orig->GetReferenceStaticOffsets(),
432 true);
433}
434
435void ImageWriter::FixupFields(const Object* orig,
436 Object* copy,
437 uint32_t ref_offsets,
438 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700439 if (ref_offsets != CLASS_WALK_SUPER) {
440 // Found a reference offset bitmap. Fixup the specified offsets.
441 while (ref_offsets != 0) {
442 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700443 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
444 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
445 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700446 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
447 }
448 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700449 // There is no reference offset bitmap. In the non-static case,
450 // walk up the class inheritance hierarchy and find reference
451 // offsets the hard way. In the static case, just consider this
452 // class.
453 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700454 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700455 klass = is_static ? NULL : klass->GetSuperClass()) {
456 size_t num_reference_fields = (is_static
457 ? klass->NumReferenceStaticFields()
458 : klass->NumReferenceInstanceFields());
459 for (size_t i = 0; i < num_reference_fields; ++i) {
460 Field* field = (is_static
461 ? klass->GetStaticField(i)
462 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 MemberOffset field_offset = field->GetOffset();
464 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
465 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700466 }
467 }
468 }
469}
470
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700471void ImageWriter::FixupDexCaches() {
472 typedef Set::const_iterator It; // TODO: C++0x auto
473 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
474 DexCache* orig = *it;
475 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
476 FixupDexCache(orig, copy);
477 }
478}
479
480void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
481 CHECK(orig != NULL);
482 CHECK(copy != NULL);
483
Ian Rogersad25ac52011-10-04 19:13:33 -0700484 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700485 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700486 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700487 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700488
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700489 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700490 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
491 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700492 if (orig_method != NULL && !InSourceSpace(orig_method)) {
493 continue;
494 }
Brian Carlstromae826982011-11-09 01:33:42 -0800495 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
496 // we need to use the stub in the static method case to ensure <clinit> is run.
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800497 if (orig_method == NULL
Brian Carlstromae826982011-11-09 01:33:42 -0800498 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700499 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
500 if (orig_res_stub_code == 0) {
501 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700502 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700503 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
504 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
505 // Do we need to relocate this for this space?
506 if (!InSourceSpace(orig_res_stub_array)) {
507 continue;
508 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700509 // Compute address in image of resolution stub and the code address
510 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800511 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700512 // Put the image code address in the array
513 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700514 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800515 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700516 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
517 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
518 reinterpret_cast<int32_t>(copy_method->code_));
519 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
520 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
521 }
522 }
523}
524
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700525} // namespace art