blob: 151aeb3863de79549e8c36b8fcaa0e358b5ceb9f [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstromdb4d5402011-08-09 12:18:28 -070016
17#include "image_writer.h"
18
19#include <sys/mman.h>
Elliott Hughes90a33692011-08-30 13:27:07 -070020
Brian Carlstromdb4d5402011-08-09 12:18:28 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070024#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070025#include "class_loader.h"
Brian Carlstromae826982011-11-09 01:33:42 -080026#include "compiled_method.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070027#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028#include "file.h"
29#include "globals.h"
30#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070031#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070032#include "intern_table.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070033#include "logging.h"
34#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070037#include "space.h"
38#include "utils.h"
39
40namespace art {
41
Elliott Hughesd9c67be2012-02-02 19:54:06 -080042std::map<const Object*, size_t> ImageWriter::offsets_;
43
Brian Carlstromae826982011-11-09 01:33:42 -080044bool ImageWriter::Write(const char* image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080045 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080046 const std::string& oat_filename,
47 const std::string& strip_location_prefix) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070048 CHECK(image_filename != NULL);
49
Ian Rogers30fab402012-01-23 15:43:46 -080050 CHECK_NE(image_begin, 0U);
51 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070052
53 const std::vector<Space*>& spaces = Heap::GetSpaces();
54 // currently just write the last space, assuming it is the space that was being used for allocation
55 CHECK_GE(spaces.size(), 1U);
56 source_space_ = spaces[spaces.size()-1];
Brian Carlstrom58ae9412011-10-04 00:56:06 -070057 CHECK(!source_space_->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070058
Brian Carlstromae826982011-11-09 01:33:42 -080059 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
60 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
61 for (size_t i = 0; i < all_dex_caches.size(); i++) {
62 DexCache* dex_cache = all_dex_caches[i];
63 if (InSourceSpace(dex_cache)) {
64 dex_caches_.insert(dex_cache);
65 }
66 }
67
Brian Carlstrome24fa612011-09-29 00:53:55 -070068 oat_file_.reset(OatFile::Open(oat_filename, strip_location_prefix, NULL));
69 if (oat_file_.get() == NULL) {
70 LOG(ERROR) << "Failed to open oat file " << oat_filename;
71 return false;
72 }
73
Brian Carlstromae826982011-11-09 01:33:42 -080074 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070075 return false;
76 }
Brian Carlstromae826982011-11-09 01:33:42 -080077 PruneNonImageClasses();
Ian Rogersd418eda2012-01-30 12:14:28 -080078 ComputeLazyFieldsForImageClasses();
Ian Rogers30fab402012-01-23 15:43:46 -080079 Heap::CollectGarbage(false);
Brian Carlstromae826982011-11-09 01:33:42 -080080#ifndef NDEBUG
81 CheckNonImageClassesRemoved();
82#endif
Ian Rogers5d76c432011-10-31 21:42:49 -070083 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070084 CalculateNewObjectOffsets();
85 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070086
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 UniquePtr<File> file(OS::OpenFile(image_filename, true));
Elliott Hughes90a33692011-08-30 13:27:07 -070088 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070090 return false;
91 }
Ian Rogers30fab402012-01-23 15:43:46 -080092 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070093 if (!success) {
94 PLOG(ERROR) << "Failed to write image file " << image_filename;
95 return false;
96 }
97 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070098}
99
Brian Carlstromae826982011-11-09 01:33:42 -0800100bool ImageWriter::AllocMemory() {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700101 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700102 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700103 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800104 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700105 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700106 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700107 return false;
108 }
109 return true;
110}
111
Ian Rogersd418eda2012-01-30 12:14:28 -0800112void ImageWriter::ComputeLazyFieldsForImageClasses() {
113 Runtime* runtime = Runtime::Current();
114 ClassLinker* class_linker = runtime->GetClassLinker();
115 class_linker->VisitClasses(ComputeLazyFieldsForClassesVisitor, NULL);
116}
117
118bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* klass, void* arg) {
119 klass->ComputeName();
120 return true;
121}
122
Brian Carlstromae826982011-11-09 01:33:42 -0800123bool ImageWriter::IsImageClass(const Class* klass) {
124 if (image_classes_ == NULL) {
125 return true;
126 }
127 while (klass->IsArrayClass()) {
128 klass = klass->GetComponentType();
129 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800130 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800131 return true;
132 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800133 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800134 return image_classes_->find(descriptor) != image_classes_->end();
135}
136
137
138struct NonImageClasses {
139 ImageWriter* image_writer;
140 std::set<std::string>* non_image_classes;
141};
142
143void ImageWriter::PruneNonImageClasses() {
144 if (image_classes_ == NULL) {
145 return;
146 }
147 Runtime* runtime = Runtime::Current();
148 ClassLinker* class_linker = runtime->GetClassLinker();
149
150 std::set<std::string> non_image_classes;
151 NonImageClasses context;
152 context.image_writer = this;
153 context.non_image_classes = &non_image_classes;
154 class_linker->VisitClasses(NonImageClassesVisitor, &context);
155
156 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
157 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800158 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800159 }
160
161 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
162 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
163 DexCache* dex_cache = *it;
164 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
165 Class* klass = dex_cache->GetResolvedType(i);
166 if (klass != NULL && !IsImageClass(klass)) {
167 dex_cache->SetResolvedType(i, NULL);
168 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
169 }
170 }
171 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
172 Method* method = dex_cache->GetResolvedMethod(i);
173 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
174 dex_cache->SetResolvedMethod(i, NULL);
175 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
176 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
177 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethodTrampoline(i, res_trampoline);
178 }
179 }
180 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
181 Field* field = dex_cache->GetResolvedField(i);
182 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
183 dex_cache->SetResolvedField(i, NULL);
184 }
185 }
186 }
187}
188
189bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
190 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
191 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800192 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800193 }
194 return true;
195}
196
197void ImageWriter::CheckNonImageClassesRemoved() {
198 if (image_classes_ == NULL) {
199 return;
200 }
201 Heap::GetLiveBits()->Walk(CheckNonImageClassesRemovedCallback, this);
202}
203
204void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
205 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
206 if (!obj->IsClass()) {
207 return;
208 }
209 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800210 if (!image_writer->IsImageClass(klass)) {
211 image_writer->DumpImageClasses();
212 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
213 << " " << PrettyDescriptor(klass);
214 }
215}
216
217void ImageWriter::DumpImageClasses() {
218 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
219 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
220 LOG(INFO) << " " << *it;
221 }
Brian Carlstromae826982011-11-09 01:33:42 -0800222}
223
Brian Carlstrom78128a62011-09-15 17:21:19 -0700224void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700225 DCHECK(obj != NULL);
226 DCHECK(arg != NULL);
227 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700228 if (!image_writer->InSourceSpace(obj)) {
229 return;
230 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700231
232 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800233 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700234 // we must be an interned string that was forward referenced and already assigned
235 if (IsImageOffsetAssigned(obj)) {
236 DCHECK_EQ(obj, obj->AsString()->Intern());
237 return;
238 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700239 SirtRef<String> interned(obj->AsString()->Intern());
240 if (obj != interned.get()) {
241 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700242 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700243 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700244 }
245 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700246 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700247 return;
248 }
249 // else (obj == interned), nothing to do but fall through to the normal case
250 }
251
252 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700253}
254
Brian Carlstrome24fa612011-09-29 00:53:55 -0700255ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700256 Runtime* runtime = Runtime::Current();
257 ClassLinker* class_linker = runtime->GetClassLinker();
258 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700259
260 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700261 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800262 dex_caches_.size());
263 int i = 0;
264 typedef Set::const_iterator It; // TODO: C++0x auto
265 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
266 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700267 }
268
269 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700270 SirtRef<ObjectArray<Object> > image_roots(
271 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800272 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700273 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
274 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700275 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
276 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
277 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
278 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
279 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
280 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700281 image_roots->Set(ImageHeader::kCalleeSaveMethod,
282 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
283 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
284 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
285 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
286 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700287 image_roots->Set(ImageHeader::kOatLocation,
288 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700289 image_roots->Set(ImageHeader::kDexCaches,
290 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700291 image_roots->Set(ImageHeader::kClassRoots,
292 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
294 CHECK(image_roots->Get(i) != NULL);
295 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700296 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700297}
298
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700299void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700300 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700301
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700302 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
303 DCHECK(heap_bitmap != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -0800304 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700305
Brian Carlstrom16192862011-09-12 17:50:06 -0700306 // leave space for the header, but do not write it yet, we need to
307 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800308 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700309
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700310 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Ian Rogers30fab402012-01-23 15:43:46 -0800311 DCHECK_LT(image_end_, image_->Size());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700312
Brian Carlstrome24fa612011-09-29 00:53:55 -0700313 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800314 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
315 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700316
Brian Carlstrom16192862011-09-12 17:50:06 -0700317 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800318 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700319 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800321 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700322 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800323 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700324}
325
326void ImageWriter::CopyAndFixupObjects() {
327 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
328 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 // TODO: heap validation can't handle this fix up pass
330 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700331 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
332 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700333}
334
Brian Carlstrom78128a62011-09-15 17:21:19 -0700335void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700336 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700337 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700338 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700339 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700340 if (!image_writer->InSourceSpace(object)) {
341 return;
342 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700343
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700344 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700345 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800346 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700347 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700348 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800349 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700350 memcpy(dst, src, n);
351 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800352 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700353 image_writer->FixupObject(obj, copy);
354}
355
Brian Carlstrom4873d462011-08-21 15:23:39 -0700356void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700357 DCHECK(orig != NULL);
358 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700359 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700360 // TODO: special case init of pointers to malloc data (or removal of these pointers)
361 if (orig->IsClass()) {
362 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
363 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700364 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700365 } else if (orig->IsMethod()) {
366 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700367 } else {
368 FixupInstanceFields(orig, copy);
369 }
370}
371
Brian Carlstrom4873d462011-08-21 15:23:39 -0700372void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700373 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700374 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700375}
376
Brian Carlstromae826982011-11-09 01:33:42 -0800377static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700378 // TODO: change to DCHECK when all code compiling
379 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800380 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700381 }
Brian Carlstromae826982011-11-09 01:33:42 -0800382 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700383 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800384 if ((orig_code % 2) == 1) {
385 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700386 }
387 return copy_code;
388}
389
Brian Carlstrom4873d462011-08-21 15:23:39 -0700390void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700391 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700392
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800394 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700395
396 // Every type of method can have an invoke stub
397 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800398 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700399 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
400
401 if (orig->IsAbstract()) {
402 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
403 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
404 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
405 copy->code_ = copy_ame_stub_array_->GetData();
406 return;
407 }
408
409 // Non-abstract methods typically have code
410 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800411 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700412 copy->code_ = code;
413
Brian Carlstrom16192862011-09-12 17:50:06 -0700414 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700415 // The native method's pointer is directed to a stub to lookup via dlsym.
416 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800417 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700418 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
419 copy->native_method_ = copy_jni_stub_array_->GetData();
420 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700421 // normal (non-abstract non-native) methods have mapping tables to relocate
422 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800423 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700424 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
425
426 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800427 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700428 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800429
430 uint32_t gc_map_offset = orig->GetOatGcMapOffset();
431 const byte* gc_map = GetOatAddress(gc_map_offset);
432 copy->gc_map_ = reinterpret_cast<const uint8_t*>(gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700433 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700434}
435
Brian Carlstrom4873d462011-08-21 15:23:39 -0700436void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700437 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700438 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700440 }
441}
442
Brian Carlstrom4873d462011-08-21 15:23:39 -0700443void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
444 DCHECK(orig != NULL);
445 DCHECK(copy != NULL);
446 Class* klass = orig->GetClass();
447 DCHECK(klass != NULL);
448 FixupFields(orig,
449 copy,
450 klass->GetReferenceInstanceOffsets(),
451 false);
452}
453
454void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
455 DCHECK(orig != NULL);
456 DCHECK(copy != NULL);
457 FixupFields(orig,
458 copy,
459 orig->GetReferenceStaticOffsets(),
460 true);
461}
462
463void ImageWriter::FixupFields(const Object* orig,
464 Object* copy,
465 uint32_t ref_offsets,
466 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700467 if (ref_offsets != CLASS_WALK_SUPER) {
468 // Found a reference offset bitmap. Fixup the specified offsets.
469 while (ref_offsets != 0) {
470 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700471 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
472 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
473 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700474 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
475 }
476 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700477 // There is no reference offset bitmap. In the non-static case,
478 // walk up the class inheritance hierarchy and find reference
479 // offsets the hard way. In the static case, just consider this
480 // class.
481 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700482 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700483 klass = is_static ? NULL : klass->GetSuperClass()) {
484 size_t num_reference_fields = (is_static
485 ? klass->NumReferenceStaticFields()
486 : klass->NumReferenceInstanceFields());
487 for (size_t i = 0; i < num_reference_fields; ++i) {
488 Field* field = (is_static
489 ? klass->GetStaticField(i)
490 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700491 MemberOffset field_offset = field->GetOffset();
492 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
493 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700494 }
495 }
496 }
497}
498
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700499void ImageWriter::FixupDexCaches() {
500 typedef Set::const_iterator It; // TODO: C++0x auto
501 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
502 DexCache* orig = *it;
503 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
504 FixupDexCache(orig, copy);
505 }
506}
507
508void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
509 CHECK(orig != NULL);
510 CHECK(copy != NULL);
511
Ian Rogersad25ac52011-10-04 19:13:33 -0700512 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700513 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700514 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700515 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700516
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700517 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700518 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
519 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700520 if (orig_method != NULL && !InSourceSpace(orig_method)) {
521 continue;
522 }
Brian Carlstromae826982011-11-09 01:33:42 -0800523 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
524 // we need to use the stub in the static method case to ensure <clinit> is run.
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800525 if (orig_method == NULL
Brian Carlstromae826982011-11-09 01:33:42 -0800526 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700527 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
528 if (orig_res_stub_code == 0) {
529 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700530 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700531 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
532 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
533 // Do we need to relocate this for this space?
534 if (!InSourceSpace(orig_res_stub_array)) {
535 continue;
536 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700537 // Compute address in image of resolution stub and the code address
538 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800539 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700540 // Put the image code address in the array
541 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700542 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800543 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700544 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
545 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
546 reinterpret_cast<int32_t>(copy_method->code_));
547 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
548 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
549 }
550 }
551}
552
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700553} // namespace art