blob: 34ad95559e2047e645ed6e948e8ffdb4e764f8e6 [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);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080087 image_.reset(MemMap::Map("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 }
102 if (klass->IsPrimitive()) {
103 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) {
130 class_linker->RemoveClass(*it, NULL);
131 }
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();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800182 CHECK(image_writer->IsImageClass(klass)) << PrettyDescriptor(klass);
Brian Carlstromae826982011-11-09 01:33:42 -0800183}
184
Brian Carlstrom78128a62011-09-15 17:21:19 -0700185void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700186 DCHECK(obj != NULL);
187 DCHECK(arg != NULL);
188 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700189 if (!image_writer->InSourceSpace(obj)) {
190 return;
191 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700192
193 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800194 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700195 // we must be an interned string that was forward referenced and already assigned
196 if (IsImageOffsetAssigned(obj)) {
197 DCHECK_EQ(obj, obj->AsString()->Intern());
198 return;
199 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700200 SirtRef<String> interned(obj->AsString()->Intern());
201 if (obj != interned.get()) {
202 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700203 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700204 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700205 }
206 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700207 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700208 return;
209 }
210 // else (obj == interned), nothing to do but fall through to the normal case
211 }
212
213 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700214}
215
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700217 Runtime* runtime = Runtime::Current();
218 ClassLinker* class_linker = runtime->GetClassLinker();
219 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700220
221 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700222 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800223 dex_caches_.size());
224 int i = 0;
225 typedef Set::const_iterator It; // TODO: C++0x auto
226 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
227 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700228 }
229
230 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700231 SirtRef<ObjectArray<Object> > image_roots(
232 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800233 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
235 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700236 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
237 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
238 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
239 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
240 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
241 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700242 image_roots->Set(ImageHeader::kCalleeSaveMethod,
243 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
244 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
245 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
246 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
247 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 image_roots->Set(ImageHeader::kOatLocation,
249 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700250 image_roots->Set(ImageHeader::kDexCaches,
251 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700252 image_roots->Set(ImageHeader::kClassRoots,
253 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
255 CHECK(image_roots->Get(i) != NULL);
256 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700257 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700258}
259
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700260void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700261 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700262
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700263 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
264 DCHECK(heap_bitmap != NULL);
265 DCHECK_EQ(0U, image_top_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700266
Brian Carlstrom16192862011-09-12 17:50:06 -0700267 // leave space for the header, but do not write it yet, we need to
268 // know where image_roots is going to end up
Brian Carlstroma663ea52011-08-19 23:33:41 -0700269 image_top_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
270
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700271 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700272 DCHECK_LT(image_top_, image_->GetLength());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700273
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274 // Note that image_top_ is left at end of used space
275 oat_base_ = image_base_ + RoundUp(image_top_, kPageSize);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700276 const byte* oat_limit = oat_base_ + oat_file_->GetSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277
Brian Carlstrom16192862011-09-12 17:50:06 -0700278 // return to write header at start of image with future location of image_roots
279 ImageHeader image_header(reinterpret_cast<uint32_t>(image_base_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700280 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281 oat_file_->GetOatHeader().GetChecksum(),
282 reinterpret_cast<uint32_t>(oat_base_),
283 reinterpret_cast<uint32_t>(oat_limit));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700284 memcpy(image_->GetAddress(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700285}
286
287void ImageWriter::CopyAndFixupObjects() {
288 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
289 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700290 // TODO: heap validation can't handle this fix up pass
291 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700292 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
293 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700294}
295
Brian Carlstrom78128a62011-09-15 17:21:19 -0700296void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700297 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700298 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700299 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700300 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700301 if (!image_writer->InSourceSpace(object)) {
302 return;
303 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700304
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700305 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700306 size_t offset = image_writer->GetImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700307 byte* dst = image_writer->image_->GetAddress() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700308 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700309 size_t n = obj->SizeOf();
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700310 DCHECK_LT(offset + n, image_writer->image_->GetLength());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700311 memcpy(dst, src, n);
312 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700313 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700314 image_writer->FixupObject(obj, copy);
315}
316
Brian Carlstrom4873d462011-08-21 15:23:39 -0700317void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700318 DCHECK(orig != NULL);
319 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700321 // TODO: special case init of pointers to malloc data (or removal of these pointers)
322 if (orig->IsClass()) {
323 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
324 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700325 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700326 } else if (orig->IsMethod()) {
327 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700328 } else {
329 FixupInstanceFields(orig, copy);
330 }
331}
332
Brian Carlstrom4873d462011-08-21 15:23:39 -0700333void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700334 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700335 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700336}
337
Brian Carlstromae826982011-11-09 01:33:42 -0800338static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700339 // TODO: change to DCHECK when all code compiling
340 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800341 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700342 }
Brian Carlstromae826982011-11-09 01:33:42 -0800343 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700344 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800345 if ((orig_code % 2) == 1) {
346 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700347 }
348 return copy_code;
349}
350
Brian Carlstrom4873d462011-08-21 15:23:39 -0700351void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700352 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700353
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 // OatWriter replaces the code_ and invoke_stub_ with offset values.
355 // Here we readjust to a pointer relative to oat_base_
356
357 // Every type of method can have an invoke stub
358 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800359 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700360 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
361
362 if (orig->IsAbstract()) {
363 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
364 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
365 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
366 copy->code_ = copy_ame_stub_array_->GetData();
367 return;
368 }
369
370 // Non-abstract methods typically have code
371 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800372 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373 copy->code_ = code;
374
Brian Carlstrom16192862011-09-12 17:50:06 -0700375 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700376 // The native method's pointer is directed to a stub to lookup via dlsym.
377 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800378 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700379 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
380 copy->native_method_ = copy_jni_stub_array_->GetData();
381 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700382 // normal (non-abstract non-native) methods have mapping tables to relocate
383 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800384 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700385 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
386
387 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800388 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700389 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrom16192862011-09-12 17:50:06 -0700390 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700391}
392
Brian Carlstrom4873d462011-08-21 15:23:39 -0700393void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700394 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700395 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700397 }
398}
399
Brian Carlstrom4873d462011-08-21 15:23:39 -0700400void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
401 DCHECK(orig != NULL);
402 DCHECK(copy != NULL);
403 Class* klass = orig->GetClass();
404 DCHECK(klass != NULL);
405 FixupFields(orig,
406 copy,
407 klass->GetReferenceInstanceOffsets(),
408 false);
409}
410
411void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
412 DCHECK(orig != NULL);
413 DCHECK(copy != NULL);
414 FixupFields(orig,
415 copy,
416 orig->GetReferenceStaticOffsets(),
417 true);
418}
419
420void ImageWriter::FixupFields(const Object* orig,
421 Object* copy,
422 uint32_t ref_offsets,
423 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700424 if (ref_offsets != CLASS_WALK_SUPER) {
425 // Found a reference offset bitmap. Fixup the specified offsets.
426 while (ref_offsets != 0) {
427 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
429 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
430 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700431 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
432 }
433 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700434 // There is no reference offset bitmap. In the non-static case,
435 // walk up the class inheritance hierarchy and find reference
436 // offsets the hard way. In the static case, just consider this
437 // class.
438 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700439 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700440 klass = is_static ? NULL : klass->GetSuperClass()) {
441 size_t num_reference_fields = (is_static
442 ? klass->NumReferenceStaticFields()
443 : klass->NumReferenceInstanceFields());
444 for (size_t i = 0; i < num_reference_fields; ++i) {
445 Field* field = (is_static
446 ? klass->GetStaticField(i)
447 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700448 MemberOffset field_offset = field->GetOffset();
449 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
450 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700451 }
452 }
453 }
454}
455
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700456void ImageWriter::FixupDexCaches() {
457 typedef Set::const_iterator It; // TODO: C++0x auto
458 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
459 DexCache* orig = *it;
460 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
461 FixupDexCache(orig, copy);
462 }
463}
464
465void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
466 CHECK(orig != NULL);
467 CHECK(copy != NULL);
468
Ian Rogersad25ac52011-10-04 19:13:33 -0700469 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700470 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700471 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700472 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700473
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700474 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700475 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
476 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700477 if (orig_method != NULL && !InSourceSpace(orig_method)) {
478 continue;
479 }
Brian Carlstromae826982011-11-09 01:33:42 -0800480 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
481 // we need to use the stub in the static method case to ensure <clinit> is run.
482 if (orig_method == NULL
483 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700484 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
485 if (orig_res_stub_code == 0) {
486 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700487 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700488 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
489 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
490 // Do we need to relocate this for this space?
491 if (!InSourceSpace(orig_res_stub_array)) {
492 continue;
493 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700494 // Compute address in image of resolution stub and the code address
495 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800496 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700497 // Put the image code address in the array
498 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700499 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800500 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700501 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
502 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
503 reinterpret_cast<int32_t>(copy_method->code_));
504 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
505 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
506 }
507 }
508}
509
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700510} // namespace art