blob: ffe4ec3f252484a26b96876d0a1308cea8ee7697 [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 Carlstroma004aa92012-02-08 18:05:09 -080044bool ImageWriter::Write(const std::string& 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,
Brian Carlstroma004aa92012-02-08 18:05:09 -080047 const std::string& oat_location) {
48 CHECK(!image_filename.empty());
Brian Carlstromaded5f72011-10-07 17:15:04 -070049
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 Carlstroma004aa92012-02-08 18:05:09 -080068 oat_file_.reset(OatFile::Open(oat_filename, oat_location, NULL));
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 if (oat_file_.get() == NULL) {
70 LOG(ERROR) << "Failed to open oat file " << oat_filename;
71 return false;
72 }
73
Ian Rogersd857b632012-02-06 20:42:33 -080074 PruneNonImageClasses(); // Remove junk
75 ComputeLazyFieldsForImageClasses(); // Add useful information
76 Heap::CollectGarbage(false); // Remove garbage
77 Heap::GetAllocSpace()->Trim(); // Trim size of source_space
Brian Carlstromae826982011-11-09 01:33:42 -080078 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070079 return false;
80 }
Brian Carlstromae826982011-11-09 01:33:42 -080081#ifndef NDEBUG
82 CheckNonImageClassesRemoved();
83#endif
Ian Rogers5d76c432011-10-31 21:42:49 -070084 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070085 CalculateNewObjectOffsets();
86 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070087
Brian Carlstroma004aa92012-02-08 18:05:09 -080088 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), true));
Elliott Hughes90a33692011-08-30 13:27:07 -070089 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070091 return false;
92 }
Ian Rogers30fab402012-01-23 15:43:46 -080093 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070094 if (!success) {
95 PLOG(ERROR) << "Failed to write image file " << image_filename;
96 return false;
97 }
98 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070099}
100
Brian Carlstromae826982011-11-09 01:33:42 -0800101bool ImageWriter::AllocMemory() {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700102 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700103 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700104 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800105 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700106 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700107 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700108 return false;
109 }
110 return true;
111}
112
Ian Rogersd418eda2012-01-30 12:14:28 -0800113void ImageWriter::ComputeLazyFieldsForImageClasses() {
114 Runtime* runtime = Runtime::Current();
115 ClassLinker* class_linker = runtime->GetClassLinker();
116 class_linker->VisitClasses(ComputeLazyFieldsForClassesVisitor, NULL);
117}
118
119bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* klass, void* arg) {
120 klass->ComputeName();
121 return true;
122}
123
Brian Carlstromae826982011-11-09 01:33:42 -0800124bool ImageWriter::IsImageClass(const Class* klass) {
125 if (image_classes_ == NULL) {
126 return true;
127 }
128 while (klass->IsArrayClass()) {
129 klass = klass->GetComponentType();
130 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800131 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800132 return true;
133 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800135 return image_classes_->find(descriptor) != image_classes_->end();
136}
137
138
139struct NonImageClasses {
140 ImageWriter* image_writer;
141 std::set<std::string>* non_image_classes;
142};
143
144void ImageWriter::PruneNonImageClasses() {
145 if (image_classes_ == NULL) {
146 return;
147 }
148 Runtime* runtime = Runtime::Current();
149 ClassLinker* class_linker = runtime->GetClassLinker();
150
151 std::set<std::string> non_image_classes;
152 NonImageClasses context;
153 context.image_writer = this;
154 context.non_image_classes = &non_image_classes;
155 class_linker->VisitClasses(NonImageClassesVisitor, &context);
156
157 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
158 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800159 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800160 }
161
162 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
163 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
164 DexCache* dex_cache = *it;
165 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
166 Class* klass = dex_cache->GetResolvedType(i);
167 if (klass != NULL && !IsImageClass(klass)) {
168 dex_cache->SetResolvedType(i, NULL);
169 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
170 }
171 }
172 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
173 Method* method = dex_cache->GetResolvedMethod(i);
174 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
175 dex_cache->SetResolvedMethod(i, NULL);
176 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
177 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
178 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethodTrampoline(i, res_trampoline);
179 }
180 }
181 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
182 Field* field = dex_cache->GetResolvedField(i);
183 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
184 dex_cache->SetResolvedField(i, NULL);
185 }
186 }
187 }
188}
189
190bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
191 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
192 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800194 }
195 return true;
196}
197
198void ImageWriter::CheckNonImageClassesRemoved() {
199 if (image_classes_ == NULL) {
200 return;
201 }
202 Heap::GetLiveBits()->Walk(CheckNonImageClassesRemovedCallback, this);
203}
204
205void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
206 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
207 if (!obj->IsClass()) {
208 return;
209 }
210 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800211 if (!image_writer->IsImageClass(klass)) {
212 image_writer->DumpImageClasses();
213 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
214 << " " << PrettyDescriptor(klass);
215 }
216}
217
218void ImageWriter::DumpImageClasses() {
219 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
220 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
221 LOG(INFO) << " " << *it;
222 }
Brian Carlstromae826982011-11-09 01:33:42 -0800223}
224
Brian Carlstrom78128a62011-09-15 17:21:19 -0700225void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700226 DCHECK(obj != NULL);
227 DCHECK(arg != NULL);
228 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700229 if (!image_writer->InSourceSpace(obj)) {
230 return;
231 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700232
233 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800234 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700235 // we must be an interned string that was forward referenced and already assigned
236 if (IsImageOffsetAssigned(obj)) {
237 DCHECK_EQ(obj, obj->AsString()->Intern());
238 return;
239 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700240 SirtRef<String> interned(obj->AsString()->Intern());
241 if (obj != interned.get()) {
242 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700243 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700244 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700245 }
246 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700247 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700248 return;
249 }
250 // else (obj == interned), nothing to do but fall through to the normal case
251 }
252
253 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700254}
255
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700257 Runtime* runtime = Runtime::Current();
258 ClassLinker* class_linker = runtime->GetClassLinker();
259 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700260
261 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700262 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800263 dex_caches_.size());
264 int i = 0;
265 typedef Set::const_iterator It; // TODO: C++0x auto
266 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
267 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700268 }
269
270 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700271 SirtRef<ObjectArray<Object> > image_roots(
272 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800273 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
275 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700276 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
277 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
278 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
279 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
280 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
281 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700282 image_roots->Set(ImageHeader::kCalleeSaveMethod,
283 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
284 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
285 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
286 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
287 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700288 image_roots->Set(ImageHeader::kOatLocation,
289 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700290 image_roots->Set(ImageHeader::kDexCaches,
291 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700292 image_roots->Set(ImageHeader::kClassRoots,
293 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
295 CHECK(image_roots->Get(i) != NULL);
296 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700297 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700298}
299
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700300void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700301 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700302
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700303 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
304 DCHECK(heap_bitmap != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -0800305 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700306
Brian Carlstrom16192862011-09-12 17:50:06 -0700307 // leave space for the header, but do not write it yet, we need to
308 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800309 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700310
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700311 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Ian Rogers30fab402012-01-23 15:43:46 -0800312 DCHECK_LT(image_end_, image_->Size());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700313
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800315 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
316 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700317
Brian Carlstrom16192862011-09-12 17:50:06 -0700318 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800319 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700320 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800322 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700323 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800324 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700325}
326
327void ImageWriter::CopyAndFixupObjects() {
328 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
329 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 // TODO: heap validation can't handle this fix up pass
331 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700332 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
333 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700334}
335
Brian Carlstrom78128a62011-09-15 17:21:19 -0700336void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700337 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700338 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700339 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700340 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700341 if (!image_writer->InSourceSpace(object)) {
342 return;
343 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700344
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700345 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700346 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800347 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700348 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700349 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800350 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700351 memcpy(dst, src, n);
352 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800353 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700354 image_writer->FixupObject(obj, copy);
355}
356
Brian Carlstrom4873d462011-08-21 15:23:39 -0700357void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700358 DCHECK(orig != NULL);
359 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700360 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700361 // TODO: special case init of pointers to malloc data (or removal of these pointers)
362 if (orig->IsClass()) {
363 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
364 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700365 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700366 } else if (orig->IsMethod()) {
367 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700368 } else {
369 FixupInstanceFields(orig, copy);
370 }
371}
372
Brian Carlstrom4873d462011-08-21 15:23:39 -0700373void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700374 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700375 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700376}
377
Brian Carlstromae826982011-11-09 01:33:42 -0800378static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700379 // TODO: change to DCHECK when all code compiling
380 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800381 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700382 }
Brian Carlstromae826982011-11-09 01:33:42 -0800383 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700384 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800385 if ((orig_code % 2) == 1) {
386 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700387 }
388 return copy_code;
389}
390
Brian Carlstrom4873d462011-08-21 15:23:39 -0700391void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700392 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700394 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800395 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700396
397 // Every type of method can have an invoke stub
398 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800399 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700400 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
401
402 if (orig->IsAbstract()) {
403 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
404 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
405 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
406 copy->code_ = copy_ame_stub_array_->GetData();
407 return;
408 }
409
410 // Non-abstract methods typically have code
411 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800412 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700413 copy->code_ = code;
414
Brian Carlstrom16192862011-09-12 17:50:06 -0700415 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700416 // The native method's pointer is directed to a stub to lookup via dlsym.
417 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800418 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700419 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
420 copy->native_method_ = copy_jni_stub_array_->GetData();
421 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700422 // normal (non-abstract non-native) methods have mapping tables to relocate
423 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800424 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
426
427 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800428 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700429 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800430
431 uint32_t gc_map_offset = orig->GetOatGcMapOffset();
432 const byte* gc_map = GetOatAddress(gc_map_offset);
433 copy->gc_map_ = reinterpret_cast<const uint8_t*>(gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700434 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700435}
436
Brian Carlstrom4873d462011-08-21 15:23:39 -0700437void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700438 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700439 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700440 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700441 }
442}
443
Brian Carlstrom4873d462011-08-21 15:23:39 -0700444void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
445 DCHECK(orig != NULL);
446 DCHECK(copy != NULL);
447 Class* klass = orig->GetClass();
448 DCHECK(klass != NULL);
449 FixupFields(orig,
450 copy,
451 klass->GetReferenceInstanceOffsets(),
452 false);
453}
454
455void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
456 DCHECK(orig != NULL);
457 DCHECK(copy != NULL);
458 FixupFields(orig,
459 copy,
460 orig->GetReferenceStaticOffsets(),
461 true);
462}
463
464void ImageWriter::FixupFields(const Object* orig,
465 Object* copy,
466 uint32_t ref_offsets,
467 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700468 if (ref_offsets != CLASS_WALK_SUPER) {
469 // Found a reference offset bitmap. Fixup the specified offsets.
470 while (ref_offsets != 0) {
471 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700472 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
473 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
474 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700475 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
476 }
477 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700478 // There is no reference offset bitmap. In the non-static case,
479 // walk up the class inheritance hierarchy and find reference
480 // offsets the hard way. In the static case, just consider this
481 // class.
482 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700483 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700484 klass = is_static ? NULL : klass->GetSuperClass()) {
485 size_t num_reference_fields = (is_static
486 ? klass->NumReferenceStaticFields()
487 : klass->NumReferenceInstanceFields());
488 for (size_t i = 0; i < num_reference_fields; ++i) {
489 Field* field = (is_static
490 ? klass->GetStaticField(i)
491 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700492 MemberOffset field_offset = field->GetOffset();
493 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
494 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700495 }
496 }
497 }
498}
499
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700500void ImageWriter::FixupDexCaches() {
501 typedef Set::const_iterator It; // TODO: C++0x auto
502 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
503 DexCache* orig = *it;
504 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
505 FixupDexCache(orig, copy);
506 }
507}
508
509void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
510 CHECK(orig != NULL);
511 CHECK(copy != NULL);
512
Ian Rogersad25ac52011-10-04 19:13:33 -0700513 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700514 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700515 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700516 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700517
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700518 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700519 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
520 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700521 if (orig_method != NULL && !InSourceSpace(orig_method)) {
522 continue;
523 }
Brian Carlstromae826982011-11-09 01:33:42 -0800524 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
525 // we need to use the stub in the static method case to ensure <clinit> is run.
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800526 if (orig_method == NULL
Brian Carlstromae826982011-11-09 01:33:42 -0800527 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700528 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
529 if (orig_res_stub_code == 0) {
530 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700531 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700532 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
533 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
534 // Do we need to relocate this for this space?
535 if (!InSourceSpace(orig_res_stub_array)) {
536 continue;
537 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700538 // Compute address in image of resolution stub and the code address
539 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800540 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700541 // Put the image code address in the array
542 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700543 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800544 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700545 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
546 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
547 reinterpret_cast<int32_t>(copy_method->code_));
548 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
549 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
550 }
551 }
552}
553
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700554} // namespace art