blob: 293bd96d02f542b174b29af1e1582f4b97e3cf4e [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,
Ian Rogers30fab402012-01-23 15:43:46 -080029 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080030 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
Ian Rogers30fab402012-01-23 15:43:46 -080034 CHECK_NE(image_begin, 0U);
35 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070036
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();
Ian Rogersd418eda2012-01-30 12:14:28 -080062 ComputeLazyFieldsForImageClasses();
Ian Rogers30fab402012-01-23 15:43:46 -080063 Heap::CollectGarbage(false);
Brian Carlstromae826982011-11-09 01:33:42 -080064#ifndef NDEBUG
65 CheckNonImageClassesRemoved();
66#endif
Ian Rogers5d76c432011-10-31 21:42:49 -070067 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070068 CalculateNewObjectOffsets();
69 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070070
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 UniquePtr<File> file(OS::OpenFile(image_filename, true));
Elliott Hughes90a33692011-08-30 13:27:07 -070072 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070074 return false;
75 }
Ian Rogers30fab402012-01-23 15:43:46 -080076 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 if (!success) {
78 PLOG(ERROR) << "Failed to write image file " << image_filename;
79 return false;
80 }
81 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070082}
83
Brian Carlstromae826982011-11-09 01:33:42 -080084bool ImageWriter::AllocMemory() {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070085 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070086 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070087 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -080088 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -070089 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -070091 return false;
92 }
93 return true;
94}
95
Ian Rogersd418eda2012-01-30 12:14:28 -080096void ImageWriter::ComputeLazyFieldsForImageClasses() {
97 Runtime* runtime = Runtime::Current();
98 ClassLinker* class_linker = runtime->GetClassLinker();
99 class_linker->VisitClasses(ComputeLazyFieldsForClassesVisitor, NULL);
100}
101
102bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* klass, void* arg) {
103 klass->ComputeName();
104 return true;
105}
106
Brian Carlstromae826982011-11-09 01:33:42 -0800107bool ImageWriter::IsImageClass(const Class* klass) {
108 if (image_classes_ == NULL) {
109 return true;
110 }
111 while (klass->IsArrayClass()) {
112 klass = klass->GetComponentType();
113 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800114 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800115 return true;
116 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800117 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800118 return image_classes_->find(descriptor) != image_classes_->end();
119}
120
121
122struct NonImageClasses {
123 ImageWriter* image_writer;
124 std::set<std::string>* non_image_classes;
125};
126
127void ImageWriter::PruneNonImageClasses() {
128 if (image_classes_ == NULL) {
129 return;
130 }
131 Runtime* runtime = Runtime::Current();
132 ClassLinker* class_linker = runtime->GetClassLinker();
133
134 std::set<std::string> non_image_classes;
135 NonImageClasses context;
136 context.image_writer = this;
137 context.non_image_classes = &non_image_classes;
138 class_linker->VisitClasses(NonImageClassesVisitor, &context);
139
140 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
141 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800142 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800143 }
144
145 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
146 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
147 DexCache* dex_cache = *it;
148 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
149 Class* klass = dex_cache->GetResolvedType(i);
150 if (klass != NULL && !IsImageClass(klass)) {
151 dex_cache->SetResolvedType(i, NULL);
152 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
153 }
154 }
155 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
156 Method* method = dex_cache->GetResolvedMethod(i);
157 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
158 dex_cache->SetResolvedMethod(i, NULL);
159 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
160 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
161 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethodTrampoline(i, res_trampoline);
162 }
163 }
164 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
165 Field* field = dex_cache->GetResolvedField(i);
166 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
167 dex_cache->SetResolvedField(i, NULL);
168 }
169 }
170 }
171}
172
173bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
174 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
175 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800176 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800177 }
178 return true;
179}
180
181void ImageWriter::CheckNonImageClassesRemoved() {
182 if (image_classes_ == NULL) {
183 return;
184 }
185 Heap::GetLiveBits()->Walk(CheckNonImageClassesRemovedCallback, this);
186}
187
188void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
189 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
190 if (!obj->IsClass()) {
191 return;
192 }
193 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800194 if (!image_writer->IsImageClass(klass)) {
195 image_writer->DumpImageClasses();
196 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
197 << " " << PrettyDescriptor(klass);
198 }
199}
200
201void ImageWriter::DumpImageClasses() {
202 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
203 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
204 LOG(INFO) << " " << *it;
205 }
Brian Carlstromae826982011-11-09 01:33:42 -0800206}
207
Brian Carlstrom78128a62011-09-15 17:21:19 -0700208void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700209 DCHECK(obj != NULL);
210 DCHECK(arg != NULL);
211 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700212 if (!image_writer->InSourceSpace(obj)) {
213 return;
214 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700215
216 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800217 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700218 // we must be an interned string that was forward referenced and already assigned
219 if (IsImageOffsetAssigned(obj)) {
220 DCHECK_EQ(obj, obj->AsString()->Intern());
221 return;
222 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700223 SirtRef<String> interned(obj->AsString()->Intern());
224 if (obj != interned.get()) {
225 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700226 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700227 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700228 }
229 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700230 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700231 return;
232 }
233 // else (obj == interned), nothing to do but fall through to the normal case
234 }
235
236 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700237}
238
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700240 Runtime* runtime = Runtime::Current();
241 ClassLinker* class_linker = runtime->GetClassLinker();
242 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700243
244 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700245 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800246 dex_caches_.size());
247 int i = 0;
248 typedef Set::const_iterator It; // TODO: C++0x auto
249 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
250 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700251 }
252
253 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700254 SirtRef<ObjectArray<Object> > image_roots(
255 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800256 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700257 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
258 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700259 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
260 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
261 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
262 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
263 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
264 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700265 image_roots->Set(ImageHeader::kCalleeSaveMethod,
266 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
267 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
268 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
269 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
270 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 image_roots->Set(ImageHeader::kOatLocation,
272 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700273 image_roots->Set(ImageHeader::kDexCaches,
274 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700275 image_roots->Set(ImageHeader::kClassRoots,
276 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
278 CHECK(image_roots->Get(i) != NULL);
279 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700280 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700281}
282
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700283void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700284 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700285
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700286 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
287 DCHECK(heap_bitmap != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -0800288 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700289
Brian Carlstrom16192862011-09-12 17:50:06 -0700290 // leave space for the header, but do not write it yet, we need to
291 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800292 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700293
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700294 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Ian Rogers30fab402012-01-23 15:43:46 -0800295 DCHECK_LT(image_end_, image_->Size());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700296
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800298 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
299 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300
Brian Carlstrom16192862011-09-12 17:50:06 -0700301 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800302 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700303 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700304 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800305 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800307 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700308}
309
310void ImageWriter::CopyAndFixupObjects() {
311 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
312 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700313 // TODO: heap validation can't handle this fix up pass
314 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700315 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
316 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700317}
318
Brian Carlstrom78128a62011-09-15 17:21:19 -0700319void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700320 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700321 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700322 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700323 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700324 if (!image_writer->InSourceSpace(object)) {
325 return;
326 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700327
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700328 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700329 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800330 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700331 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700332 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800333 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700334 memcpy(dst, src, n);
335 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700337 image_writer->FixupObject(obj, copy);
338}
339
Brian Carlstrom4873d462011-08-21 15:23:39 -0700340void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700341 DCHECK(orig != NULL);
342 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700344 // TODO: special case init of pointers to malloc data (or removal of these pointers)
345 if (orig->IsClass()) {
346 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
347 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700348 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700349 } else if (orig->IsMethod()) {
350 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700351 } else {
352 FixupInstanceFields(orig, copy);
353 }
354}
355
Brian Carlstrom4873d462011-08-21 15:23:39 -0700356void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700357 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700358 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700359}
360
Brian Carlstromae826982011-11-09 01:33:42 -0800361static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700362 // TODO: change to DCHECK when all code compiling
363 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800364 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700365 }
Brian Carlstromae826982011-11-09 01:33:42 -0800366 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700367 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800368 if ((orig_code % 2) == 1) {
369 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700370 }
371 return copy_code;
372}
373
Brian Carlstrom4873d462011-08-21 15:23:39 -0700374void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700375 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700377 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800378 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700379
380 // Every type of method can have an invoke stub
381 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800382 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700383 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
384
385 if (orig->IsAbstract()) {
386 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
387 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
388 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
389 copy->code_ = copy_ame_stub_array_->GetData();
390 return;
391 }
392
393 // Non-abstract methods typically have code
394 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800395 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700396 copy->code_ = code;
397
Brian Carlstrom16192862011-09-12 17:50:06 -0700398 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700399 // The native method's pointer is directed to a stub to lookup via dlsym.
400 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800401 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700402 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
403 copy->native_method_ = copy_jni_stub_array_->GetData();
404 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700405 // normal (non-abstract non-native) methods have mapping tables to relocate
406 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800407 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
409
410 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800411 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700412 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800413
414 uint32_t gc_map_offset = orig->GetOatGcMapOffset();
415 const byte* gc_map = GetOatAddress(gc_map_offset);
416 copy->gc_map_ = reinterpret_cast<const uint8_t*>(gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700417 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700418}
419
Brian Carlstrom4873d462011-08-21 15:23:39 -0700420void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700421 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700422 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700424 }
425}
426
Brian Carlstrom4873d462011-08-21 15:23:39 -0700427void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
428 DCHECK(orig != NULL);
429 DCHECK(copy != NULL);
430 Class* klass = orig->GetClass();
431 DCHECK(klass != NULL);
432 FixupFields(orig,
433 copy,
434 klass->GetReferenceInstanceOffsets(),
435 false);
436}
437
438void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
439 DCHECK(orig != NULL);
440 DCHECK(copy != NULL);
441 FixupFields(orig,
442 copy,
443 orig->GetReferenceStaticOffsets(),
444 true);
445}
446
447void ImageWriter::FixupFields(const Object* orig,
448 Object* copy,
449 uint32_t ref_offsets,
450 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700451 if (ref_offsets != CLASS_WALK_SUPER) {
452 // Found a reference offset bitmap. Fixup the specified offsets.
453 while (ref_offsets != 0) {
454 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
456 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
457 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700458 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
459 }
460 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700461 // There is no reference offset bitmap. In the non-static case,
462 // walk up the class inheritance hierarchy and find reference
463 // offsets the hard way. In the static case, just consider this
464 // class.
465 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700466 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700467 klass = is_static ? NULL : klass->GetSuperClass()) {
468 size_t num_reference_fields = (is_static
469 ? klass->NumReferenceStaticFields()
470 : klass->NumReferenceInstanceFields());
471 for (size_t i = 0; i < num_reference_fields; ++i) {
472 Field* field = (is_static
473 ? klass->GetStaticField(i)
474 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700475 MemberOffset field_offset = field->GetOffset();
476 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
477 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700478 }
479 }
480 }
481}
482
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700483void ImageWriter::FixupDexCaches() {
484 typedef Set::const_iterator It; // TODO: C++0x auto
485 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
486 DexCache* orig = *it;
487 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
488 FixupDexCache(orig, copy);
489 }
490}
491
492void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
493 CHECK(orig != NULL);
494 CHECK(copy != NULL);
495
Ian Rogersad25ac52011-10-04 19:13:33 -0700496 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700497 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700498 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700499 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700500
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700501 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700502 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
503 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700504 if (orig_method != NULL && !InSourceSpace(orig_method)) {
505 continue;
506 }
Brian Carlstromae826982011-11-09 01:33:42 -0800507 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
508 // we need to use the stub in the static method case to ensure <clinit> is run.
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800509 if (orig_method == NULL
Brian Carlstromae826982011-11-09 01:33:42 -0800510 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700511 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
512 if (orig_res_stub_code == 0) {
513 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700514 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700515 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
516 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
517 // Do we need to relocate this for this space?
518 if (!InSourceSpace(orig_res_stub_array)) {
519 continue;
520 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700521 // Compute address in image of resolution stub and the code address
522 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800523 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700524 // Put the image code address in the array
525 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700526 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800527 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700528 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
529 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
530 reinterpret_cast<int32_t>(copy_method->code_));
531 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
532 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
533 }
534 }
535}
536
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700537} // namespace art