blob: d8bb5ea4ed184c86439e6567f52e1a5c5570d4a6 [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
Brian Carlstromae826982011-11-09 01:33:42 -080042bool ImageWriter::Write(const char* image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080043 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080044 const std::string& oat_filename,
45 const std::string& strip_location_prefix) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070046 CHECK(image_filename != NULL);
47
Ian Rogers30fab402012-01-23 15:43:46 -080048 CHECK_NE(image_begin, 0U);
49 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070050
51 const std::vector<Space*>& spaces = Heap::GetSpaces();
52 // currently just write the last space, assuming it is the space that was being used for allocation
53 CHECK_GE(spaces.size(), 1U);
54 source_space_ = spaces[spaces.size()-1];
Brian Carlstrom58ae9412011-10-04 00:56:06 -070055 CHECK(!source_space_->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070056
Brian Carlstromae826982011-11-09 01:33:42 -080057 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
58 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
59 for (size_t i = 0; i < all_dex_caches.size(); i++) {
60 DexCache* dex_cache = all_dex_caches[i];
61 if (InSourceSpace(dex_cache)) {
62 dex_caches_.insert(dex_cache);
63 }
64 }
65
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 oat_file_.reset(OatFile::Open(oat_filename, strip_location_prefix, NULL));
67 if (oat_file_.get() == NULL) {
68 LOG(ERROR) << "Failed to open oat file " << oat_filename;
69 return false;
70 }
71
Brian Carlstromae826982011-11-09 01:33:42 -080072 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070073 return false;
74 }
Brian Carlstromae826982011-11-09 01:33:42 -080075 PruneNonImageClasses();
Ian Rogersd418eda2012-01-30 12:14:28 -080076 ComputeLazyFieldsForImageClasses();
Ian Rogers30fab402012-01-23 15:43:46 -080077 Heap::CollectGarbage(false);
Brian Carlstromae826982011-11-09 01:33:42 -080078#ifndef NDEBUG
79 CheckNonImageClassesRemoved();
80#endif
Ian Rogers5d76c432011-10-31 21:42:49 -070081 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070082 CalculateNewObjectOffsets();
83 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070084
Brian Carlstrome24fa612011-09-29 00:53:55 -070085 UniquePtr<File> file(OS::OpenFile(image_filename, true));
Elliott Hughes90a33692011-08-30 13:27:07 -070086 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070088 return false;
89 }
Ian Rogers30fab402012-01-23 15:43:46 -080090 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 if (!success) {
92 PLOG(ERROR) << "Failed to write image file " << image_filename;
93 return false;
94 }
95 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070096}
97
Brian Carlstromae826982011-11-09 01:33:42 -080098bool ImageWriter::AllocMemory() {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070099 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700100 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700101 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800102 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700103 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700104 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700105 return false;
106 }
107 return true;
108}
109
Ian Rogersd418eda2012-01-30 12:14:28 -0800110void ImageWriter::ComputeLazyFieldsForImageClasses() {
111 Runtime* runtime = Runtime::Current();
112 ClassLinker* class_linker = runtime->GetClassLinker();
113 class_linker->VisitClasses(ComputeLazyFieldsForClassesVisitor, NULL);
114}
115
116bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* klass, void* arg) {
117 klass->ComputeName();
118 return true;
119}
120
Brian Carlstromae826982011-11-09 01:33:42 -0800121bool ImageWriter::IsImageClass(const Class* klass) {
122 if (image_classes_ == NULL) {
123 return true;
124 }
125 while (klass->IsArrayClass()) {
126 klass = klass->GetComponentType();
127 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800128 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800129 return true;
130 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800131 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800132 return image_classes_->find(descriptor) != image_classes_->end();
133}
134
135
136struct NonImageClasses {
137 ImageWriter* image_writer;
138 std::set<std::string>* non_image_classes;
139};
140
141void ImageWriter::PruneNonImageClasses() {
142 if (image_classes_ == NULL) {
143 return;
144 }
145 Runtime* runtime = Runtime::Current();
146 ClassLinker* class_linker = runtime->GetClassLinker();
147
148 std::set<std::string> non_image_classes;
149 NonImageClasses context;
150 context.image_writer = this;
151 context.non_image_classes = &non_image_classes;
152 class_linker->VisitClasses(NonImageClassesVisitor, &context);
153
154 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
155 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800156 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800157 }
158
159 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
160 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
161 DexCache* dex_cache = *it;
162 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
163 Class* klass = dex_cache->GetResolvedType(i);
164 if (klass != NULL && !IsImageClass(klass)) {
165 dex_cache->SetResolvedType(i, NULL);
166 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
167 }
168 }
169 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
170 Method* method = dex_cache->GetResolvedMethod(i);
171 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
172 dex_cache->SetResolvedMethod(i, NULL);
173 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
174 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
175 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethodTrampoline(i, res_trampoline);
176 }
177 }
178 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
179 Field* field = dex_cache->GetResolvedField(i);
180 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
181 dex_cache->SetResolvedField(i, NULL);
182 }
183 }
184 }
185}
186
187bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
188 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
189 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800190 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800191 }
192 return true;
193}
194
195void ImageWriter::CheckNonImageClassesRemoved() {
196 if (image_classes_ == NULL) {
197 return;
198 }
199 Heap::GetLiveBits()->Walk(CheckNonImageClassesRemovedCallback, this);
200}
201
202void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
203 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
204 if (!obj->IsClass()) {
205 return;
206 }
207 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800208 if (!image_writer->IsImageClass(klass)) {
209 image_writer->DumpImageClasses();
210 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
211 << " " << PrettyDescriptor(klass);
212 }
213}
214
215void ImageWriter::DumpImageClasses() {
216 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
217 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
218 LOG(INFO) << " " << *it;
219 }
Brian Carlstromae826982011-11-09 01:33:42 -0800220}
221
Brian Carlstrom78128a62011-09-15 17:21:19 -0700222void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700223 DCHECK(obj != NULL);
224 DCHECK(arg != NULL);
225 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700226 if (!image_writer->InSourceSpace(obj)) {
227 return;
228 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700229
230 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800231 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700232 // we must be an interned string that was forward referenced and already assigned
233 if (IsImageOffsetAssigned(obj)) {
234 DCHECK_EQ(obj, obj->AsString()->Intern());
235 return;
236 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700237 SirtRef<String> interned(obj->AsString()->Intern());
238 if (obj != interned.get()) {
239 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700240 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700241 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700242 }
243 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700244 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700245 return;
246 }
247 // else (obj == interned), nothing to do but fall through to the normal case
248 }
249
250 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700251}
252
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700254 Runtime* runtime = Runtime::Current();
255 ClassLinker* class_linker = runtime->GetClassLinker();
256 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700257
258 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700259 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800260 dex_caches_.size());
261 int i = 0;
262 typedef Set::const_iterator It; // TODO: C++0x auto
263 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
264 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700265 }
266
267 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700268 SirtRef<ObjectArray<Object> > image_roots(
269 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800270 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
272 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700273 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
274 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
275 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
276 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
277 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
278 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700279 image_roots->Set(ImageHeader::kCalleeSaveMethod,
280 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
281 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
282 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
283 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
284 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700285 image_roots->Set(ImageHeader::kOatLocation,
286 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700287 image_roots->Set(ImageHeader::kDexCaches,
288 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700289 image_roots->Set(ImageHeader::kClassRoots,
290 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700291 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
292 CHECK(image_roots->Get(i) != NULL);
293 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700294 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700295}
296
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700297void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700298 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700299
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700300 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
301 DCHECK(heap_bitmap != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -0800302 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700303
Brian Carlstrom16192862011-09-12 17:50:06 -0700304 // leave space for the header, but do not write it yet, we need to
305 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800306 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700307
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700308 heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Ian Rogers30fab402012-01-23 15:43:46 -0800309 DCHECK_LT(image_end_, image_->Size());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700310
Brian Carlstrome24fa612011-09-29 00:53:55 -0700311 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800312 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
313 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314
Brian Carlstrom16192862011-09-12 17:50:06 -0700315 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800316 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700317 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700318 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800319 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800321 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700322}
323
324void ImageWriter::CopyAndFixupObjects() {
325 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
326 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 // TODO: heap validation can't handle this fix up pass
328 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700329 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
330 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700331}
332
Brian Carlstrom78128a62011-09-15 17:21:19 -0700333void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700334 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700335 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700336 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700337 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700338 if (!image_writer->InSourceSpace(object)) {
339 return;
340 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700341
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700342 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700343 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800344 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700345 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700346 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800347 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700348 memcpy(dst, src, n);
349 Object* copy = reinterpret_cast<Object*>(dst);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700350 ResetImageOffset(copy);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700351 image_writer->FixupObject(obj, copy);
352}
353
Brian Carlstrom4873d462011-08-21 15:23:39 -0700354void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700355 DCHECK(orig != NULL);
356 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700358 // TODO: special case init of pointers to malloc data (or removal of these pointers)
359 if (orig->IsClass()) {
360 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
361 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700362 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700363 } else if (orig->IsMethod()) {
364 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700365 } else {
366 FixupInstanceFields(orig, copy);
367 }
368}
369
Brian Carlstrom4873d462011-08-21 15:23:39 -0700370void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700371 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700372 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700373}
374
Brian Carlstromae826982011-11-09 01:33:42 -0800375static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700376 // TODO: change to DCHECK when all code compiling
377 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800378 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700379 }
Brian Carlstromae826982011-11-09 01:33:42 -0800380 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700381 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800382 if ((orig_code % 2) == 1) {
383 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700384 }
385 return copy_code;
386}
387
Brian Carlstrom4873d462011-08-21 15:23:39 -0700388void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700389 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700390
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700391 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800392 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393
394 // Every type of method can have an invoke stub
395 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800396 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700397 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
398
399 if (orig->IsAbstract()) {
400 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
401 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
402 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
403 copy->code_ = copy_ame_stub_array_->GetData();
404 return;
405 }
406
407 // Non-abstract methods typically have code
408 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800409 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700410 copy->code_ = code;
411
Brian Carlstrom16192862011-09-12 17:50:06 -0700412 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700413 // The native method's pointer is directed to a stub to lookup via dlsym.
414 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800415 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700416 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
417 copy->native_method_ = copy_jni_stub_array_->GetData();
418 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419 // normal (non-abstract non-native) methods have mapping tables to relocate
420 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800421 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700422 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
423
424 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800425 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700426 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800427
428 uint32_t gc_map_offset = orig->GetOatGcMapOffset();
429 const byte* gc_map = GetOatAddress(gc_map_offset);
430 copy->gc_map_ = reinterpret_cast<const uint8_t*>(gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700431 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700432}
433
Brian Carlstrom4873d462011-08-21 15:23:39 -0700434void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700435 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700436 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700438 }
439}
440
Brian Carlstrom4873d462011-08-21 15:23:39 -0700441void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
442 DCHECK(orig != NULL);
443 DCHECK(copy != NULL);
444 Class* klass = orig->GetClass();
445 DCHECK(klass != NULL);
446 FixupFields(orig,
447 copy,
448 klass->GetReferenceInstanceOffsets(),
449 false);
450}
451
452void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
453 DCHECK(orig != NULL);
454 DCHECK(copy != NULL);
455 FixupFields(orig,
456 copy,
457 orig->GetReferenceStaticOffsets(),
458 true);
459}
460
461void ImageWriter::FixupFields(const Object* orig,
462 Object* copy,
463 uint32_t ref_offsets,
464 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700465 if (ref_offsets != CLASS_WALK_SUPER) {
466 // Found a reference offset bitmap. Fixup the specified offsets.
467 while (ref_offsets != 0) {
468 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700469 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
470 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
471 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700472 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
473 }
474 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700475 // There is no reference offset bitmap. In the non-static case,
476 // walk up the class inheritance hierarchy and find reference
477 // offsets the hard way. In the static case, just consider this
478 // class.
479 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700480 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700481 klass = is_static ? NULL : klass->GetSuperClass()) {
482 size_t num_reference_fields = (is_static
483 ? klass->NumReferenceStaticFields()
484 : klass->NumReferenceInstanceFields());
485 for (size_t i = 0; i < num_reference_fields; ++i) {
486 Field* field = (is_static
487 ? klass->GetStaticField(i)
488 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700489 MemberOffset field_offset = field->GetOffset();
490 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
491 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700492 }
493 }
494 }
495}
496
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700497void ImageWriter::FixupDexCaches() {
498 typedef Set::const_iterator It; // TODO: C++0x auto
499 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
500 DexCache* orig = *it;
501 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
502 FixupDexCache(orig, copy);
503 }
504}
505
506void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
507 CHECK(orig != NULL);
508 CHECK(copy != NULL);
509
Ian Rogersad25ac52011-10-04 19:13:33 -0700510 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700511 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700512 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700513 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700514
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700515 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700516 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
517 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700518 if (orig_method != NULL && !InSourceSpace(orig_method)) {
519 continue;
520 }
Brian Carlstromae826982011-11-09 01:33:42 -0800521 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
522 // we need to use the stub in the static method case to ensure <clinit> is run.
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800523 if (orig_method == NULL
Brian Carlstromae826982011-11-09 01:33:42 -0800524 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700525 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
526 if (orig_res_stub_code == 0) {
527 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700528 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700529 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
530 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
531 // Do we need to relocate this for this space?
532 if (!InSourceSpace(orig_res_stub_array)) {
533 continue;
534 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700535 // Compute address in image of resolution stub and the code address
536 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800537 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700538 // Put the image code address in the array
539 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700540 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800541 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700542 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
543 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
544 reinterpret_cast<int32_t>(copy_method->code_));
545 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
546 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
547 }
548 }
549}
550
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700551} // namespace art