blob: 59d113d9e688bbcbd4ddcf016e71d33c262dcc26 [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
Ian Rogersd1f1bf02012-02-26 16:59:20 -080076 ComputeEagerResolvedStrings();
Ian Rogersd857b632012-02-06 20:42:33 -080077 Heap::CollectGarbage(false); // Remove garbage
78 Heap::GetAllocSpace()->Trim(); // Trim size of source_space
Brian Carlstromae826982011-11-09 01:33:42 -080079 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070080 return false;
81 }
Brian Carlstromae826982011-11-09 01:33:42 -080082#ifndef NDEBUG
83 CheckNonImageClassesRemoved();
84#endif
Ian Rogers5d76c432011-10-31 21:42:49 -070085 Heap::DisableCardMarking();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070086 CalculateNewObjectOffsets();
87 CopyAndFixupObjects();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070088
Brian Carlstroma004aa92012-02-08 18:05:09 -080089 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), true));
Elliott Hughes90a33692011-08-30 13:27:07 -070090 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070092 return false;
93 }
Ian Rogers30fab402012-01-23 15:43:46 -080094 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070095 if (!success) {
96 PLOG(ERROR) << "Failed to write image file " << image_filename;
97 return false;
98 }
99 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700100}
101
Brian Carlstromae826982011-11-09 01:33:42 -0800102bool ImageWriter::AllocMemory() {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700103 size_t size = source_space_->Size();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700104 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700105 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800106 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700107 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700109 return false;
110 }
111 return true;
112}
113
Ian Rogersd418eda2012-01-30 12:14:28 -0800114void ImageWriter::ComputeLazyFieldsForImageClasses() {
115 Runtime* runtime = Runtime::Current();
116 ClassLinker* class_linker = runtime->GetClassLinker();
117 class_linker->VisitClasses(ComputeLazyFieldsForClassesVisitor, NULL);
118}
119
120bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* klass, void* arg) {
121 klass->ComputeName();
122 return true;
123}
124
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800125void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
126 if (!obj->GetClass()->IsStringClass()) {
127 return;
128 }
129 String* string = obj->AsString();
130 std::string utf8_string(string->ToModifiedUtf8());
131 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
132 ClassLinker* linker = Runtime::Current()->GetClassLinker();
133 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
134 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
135 DexCache* dex_cache = *it;
136 const DexFile& dex_file = linker->FindDexFile(dex_cache);
137 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
138 if (string_id != NULL) {
139 // This string occurs in this dex file, assign the dex cache entry.
140 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
141 if (dex_cache->GetResolvedString(string_idx) == NULL) {
142 dex_cache->SetResolvedString(string_idx, string);
143 }
144 }
145 }
146}
147
148void ImageWriter::ComputeEagerResolvedStrings() {
149 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
150 DCHECK(heap_bitmap != NULL);
151 heap_bitmap->Walk(ComputeEagerResolvedStringsCallback, this); // TODO: add Space-limited Walk
152}
153
Brian Carlstromae826982011-11-09 01:33:42 -0800154bool ImageWriter::IsImageClass(const Class* klass) {
155 if (image_classes_ == NULL) {
156 return true;
157 }
158 while (klass->IsArrayClass()) {
159 klass = klass->GetComponentType();
160 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800161 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800162 return true;
163 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800164 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800165 return image_classes_->find(descriptor) != image_classes_->end();
166}
167
168
169struct NonImageClasses {
170 ImageWriter* image_writer;
171 std::set<std::string>* non_image_classes;
172};
173
174void ImageWriter::PruneNonImageClasses() {
175 if (image_classes_ == NULL) {
176 return;
177 }
178 Runtime* runtime = Runtime::Current();
179 ClassLinker* class_linker = runtime->GetClassLinker();
180
181 std::set<std::string> non_image_classes;
182 NonImageClasses context;
183 context.image_writer = this;
184 context.non_image_classes = &non_image_classes;
185 class_linker->VisitClasses(NonImageClassesVisitor, &context);
186
187 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
188 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800189 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800190 }
191
192 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
193 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
194 DexCache* dex_cache = *it;
195 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
196 Class* klass = dex_cache->GetResolvedType(i);
197 if (klass != NULL && !IsImageClass(klass)) {
198 dex_cache->SetResolvedType(i, NULL);
199 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
200 }
201 }
202 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
203 Method* method = dex_cache->GetResolvedMethod(i);
204 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
205 dex_cache->SetResolvedMethod(i, NULL);
206 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
207 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
208 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethodTrampoline(i, res_trampoline);
209 }
210 }
211 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
212 Field* field = dex_cache->GetResolvedField(i);
213 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
214 dex_cache->SetResolvedField(i, NULL);
215 }
216 }
217 }
218}
219
220bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
221 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
222 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800224 }
225 return true;
226}
227
228void ImageWriter::CheckNonImageClassesRemoved() {
229 if (image_classes_ == NULL) {
230 return;
231 }
232 Heap::GetLiveBits()->Walk(CheckNonImageClassesRemovedCallback, this);
233}
234
235void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
236 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
237 if (!obj->IsClass()) {
238 return;
239 }
240 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800241 if (!image_writer->IsImageClass(klass)) {
242 image_writer->DumpImageClasses();
243 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
244 << " " << PrettyDescriptor(klass);
245 }
246}
247
248void ImageWriter::DumpImageClasses() {
249 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
250 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
251 LOG(INFO) << " " << *it;
252 }
Brian Carlstromae826982011-11-09 01:33:42 -0800253}
254
Brian Carlstrom78128a62011-09-15 17:21:19 -0700255void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700256 DCHECK(obj != NULL);
257 DCHECK(arg != NULL);
258 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700259 if (!image_writer->InSourceSpace(obj)) {
260 return;
261 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700262
263 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800264 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700265 // we must be an interned string that was forward referenced and already assigned
266 if (IsImageOffsetAssigned(obj)) {
267 DCHECK_EQ(obj, obj->AsString()->Intern());
268 return;
269 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700270 SirtRef<String> interned(obj->AsString()->Intern());
271 if (obj != interned.get()) {
272 if (!IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700273 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700274 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700275 }
276 // point those looking for this object to the interned version.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700277 SetImageOffset(obj, GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700278 return;
279 }
280 // else (obj == interned), nothing to do but fall through to the normal case
281 }
282
283 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700284}
285
Brian Carlstrome24fa612011-09-29 00:53:55 -0700286ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700287 Runtime* runtime = Runtime::Current();
288 ClassLinker* class_linker = runtime->GetClassLinker();
289 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700290
291 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700292 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800293 dex_caches_.size());
294 int i = 0;
295 typedef Set::const_iterator It; // TODO: C++0x auto
296 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
297 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700298 }
299
300 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700301 SirtRef<ObjectArray<Object> > image_roots(
302 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800303 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700304 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
305 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700306 image_roots->Set(ImageHeader::kInstanceResolutionStubArray,
307 runtime->GetResolutionStubArray(Runtime::kInstanceMethod));
308 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
309 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
310 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
311 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700312 image_roots->Set(ImageHeader::kCalleeSaveMethod,
313 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
314 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
315 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
316 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
317 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700318 image_roots->Set(ImageHeader::kOatLocation,
319 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700320 image_roots->Set(ImageHeader::kDexCaches,
321 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700322 image_roots->Set(ImageHeader::kClassRoots,
323 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700324 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
325 CHECK(image_roots->Get(i) != NULL);
326 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700327 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700328}
329
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700330void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700331 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700332
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700333 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
334 DCHECK(heap_bitmap != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -0800335 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700336
Brian Carlstrom16192862011-09-12 17:50:06 -0700337 // leave space for the header, but do not write it yet, we need to
338 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800339 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700340
Ian Rogers1351b672012-02-24 12:22:57 -0800341 heap_bitmap->InOrderWalk(CalculateNewObjectOffsetsCallback, this); // TODO: add Space-limited Walk
Ian Rogers30fab402012-01-23 15:43:46 -0800342 DCHECK_LT(image_end_, image_->Size());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700343
Brian Carlstrome24fa612011-09-29 00:53:55 -0700344 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800345 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
346 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700347
Brian Carlstrom16192862011-09-12 17:50:06 -0700348 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800349 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700350 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700351 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800352 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700353 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800354 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700355}
356
357void ImageWriter::CopyAndFixupObjects() {
358 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
359 DCHECK(heap_bitmap != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700360 // TODO: heap validation can't handle this fix up pass
361 Heap::DisableObjectValidation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700362 heap_bitmap->Walk(CopyAndFixupObjectsCallback, this); // TODO: add Space-limited Walk
363 FixupDexCaches();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700364}
365
Brian Carlstrom78128a62011-09-15 17:21:19 -0700366void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700367 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700368 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700369 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700370 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700371 if (!image_writer->InSourceSpace(object)) {
372 return;
373 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700374
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700375 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700376 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800377 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700378 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700379 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800380 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700381 memcpy(dst, src, n);
382 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800383 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700384 image_writer->FixupObject(obj, copy);
385}
386
Brian Carlstrom4873d462011-08-21 15:23:39 -0700387void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700388 DCHECK(orig != NULL);
389 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700391 // TODO: special case init of pointers to malloc data (or removal of these pointers)
392 if (orig->IsClass()) {
393 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
394 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700395 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700396 } else if (orig->IsMethod()) {
397 FixupMethod(orig->AsMethod(), down_cast<Method*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700398 } else {
399 FixupInstanceFields(orig, copy);
400 }
401}
402
Brian Carlstrom4873d462011-08-21 15:23:39 -0700403void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700404 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700405 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700406}
407
Brian Carlstromae826982011-11-09 01:33:42 -0800408static uint32_t FixupCode(const ByteArray* copy_code_array, uint32_t orig_code) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700409 // TODO: change to DCHECK when all code compiling
410 if (copy_code_array == NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800411 return 0;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700412 }
Brian Carlstromae826982011-11-09 01:33:42 -0800413 uint32_t copy_code = reinterpret_cast<uint32_t>(copy_code_array->GetData());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700414 // TODO: remember InstructionSet with each code array so we know if we need to do thumb fixup?
Brian Carlstromae826982011-11-09 01:33:42 -0800415 if ((orig_code % 2) == 1) {
416 return copy_code + 1;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700417 }
418 return copy_code;
419}
420
Brian Carlstrom4873d462011-08-21 15:23:39 -0700421void ImageWriter::FixupMethod(const Method* orig, Method* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700422 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700423
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700424 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800425 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700426
427 // Every type of method can have an invoke stub
428 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800429 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700430 copy->invoke_stub_ = reinterpret_cast<const Method::InvokeStub*>(invoke_stub);
431
432 if (orig->IsAbstract()) {
433 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
434 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
435 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
436 copy->code_ = copy_ame_stub_array_->GetData();
437 return;
438 }
439
440 // Non-abstract methods typically have code
441 uint32_t code_offset = orig->GetOatCodeOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800442 const byte* code = GetOatAddress(code_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700443 copy->code_ = code;
444
Brian Carlstrom16192862011-09-12 17:50:06 -0700445 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700446 // The native method's pointer is directed to a stub to lookup via dlsym.
447 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800448 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700449 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
450 copy->native_method_ = copy_jni_stub_array_->GetData();
451 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700452 // normal (non-abstract non-native) methods have mapping tables to relocate
453 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800454 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700455 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
456
457 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800458 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700459 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800460
461 uint32_t gc_map_offset = orig->GetOatGcMapOffset();
462 const byte* gc_map = GetOatAddress(gc_map_offset);
463 copy->gc_map_ = reinterpret_cast<const uint8_t*>(gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700464 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700465}
466
Brian Carlstrom4873d462011-08-21 15:23:39 -0700467void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700468 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700469 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700470 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700471 }
472}
473
Brian Carlstrom4873d462011-08-21 15:23:39 -0700474void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
475 DCHECK(orig != NULL);
476 DCHECK(copy != NULL);
477 Class* klass = orig->GetClass();
478 DCHECK(klass != NULL);
479 FixupFields(orig,
480 copy,
481 klass->GetReferenceInstanceOffsets(),
482 false);
483}
484
485void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
486 DCHECK(orig != NULL);
487 DCHECK(copy != NULL);
488 FixupFields(orig,
489 copy,
490 orig->GetReferenceStaticOffsets(),
491 true);
492}
493
494void ImageWriter::FixupFields(const Object* orig,
495 Object* copy,
496 uint32_t ref_offsets,
497 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700498 if (ref_offsets != CLASS_WALK_SUPER) {
499 // Found a reference offset bitmap. Fixup the specified offsets.
500 while (ref_offsets != 0) {
501 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700502 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
503 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
504 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700505 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
506 }
507 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700508 // There is no reference offset bitmap. In the non-static case,
509 // walk up the class inheritance hierarchy and find reference
510 // offsets the hard way. In the static case, just consider this
511 // class.
512 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700513 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700514 klass = is_static ? NULL : klass->GetSuperClass()) {
515 size_t num_reference_fields = (is_static
516 ? klass->NumReferenceStaticFields()
517 : klass->NumReferenceInstanceFields());
518 for (size_t i = 0; i < num_reference_fields; ++i) {
519 Field* field = (is_static
520 ? klass->GetStaticField(i)
521 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700522 MemberOffset field_offset = field->GetOffset();
523 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
524 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700525 }
526 }
527 }
528}
529
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700530void ImageWriter::FixupDexCaches() {
531 typedef Set::const_iterator It; // TODO: C++0x auto
532 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
533 DexCache* orig = *it;
534 DexCache* copy = down_cast<DexCache*>(GetLocalAddress(orig));
535 FixupDexCache(orig, copy);
536 }
537}
538
539void ImageWriter::FixupDexCache(const DexCache* orig, DexCache* copy) {
540 CHECK(orig != NULL);
541 CHECK(copy != NULL);
542
Ian Rogersad25ac52011-10-04 19:13:33 -0700543 // The original array value
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700544 CodeAndDirectMethods* orig_cadms = orig->GetCodeAndDirectMethods();
Ian Rogersad25ac52011-10-04 19:13:33 -0700545 // The compacted object in local memory but not at the correct image address
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700546 CodeAndDirectMethods* copy_cadms = down_cast<CodeAndDirectMethods*>(GetLocalAddress(orig_cadms));
Ian Rogersad25ac52011-10-04 19:13:33 -0700547
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700548 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700549 for (size_t i = 0; i < orig->NumResolvedMethods(); i++) {
550 Method* orig_method = orig->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700551 if (orig_method != NULL && !InSourceSpace(orig_method)) {
552 continue;
553 }
Brian Carlstromae826982011-11-09 01:33:42 -0800554 // if it was unresolved or a resolved static method in an uninit class, use a resolution stub
555 // we need to use the stub in the static method case to ensure <clinit> is run.
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800556 if (orig_method == NULL
Brian Carlstromae826982011-11-09 01:33:42 -0800557 || (orig_method->IsStatic() && !orig_method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700558 uint32_t orig_res_stub_code = orig_cadms->Get(CodeAndDirectMethods::CodeIndex(i));
559 if (orig_res_stub_code == 0) {
560 continue; // NULL maps the same in the image and the original
Ian Rogersad25ac52011-10-04 19:13:33 -0700561 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700562 Runtime::TrampolineType type = Runtime::GetTrampolineType(orig_method); // Type of trampoline
563 ByteArray* orig_res_stub_array = runtime->GetResolutionStubArray(type);
564 // Do we need to relocate this for this space?
565 if (!InSourceSpace(orig_res_stub_array)) {
566 continue;
567 }
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700568 // Compute address in image of resolution stub and the code address
569 ByteArray* image_res_stub_array = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array));
Brian Carlstromae826982011-11-09 01:33:42 -0800570 uint32_t image_res_stub_code = FixupCode(image_res_stub_array, orig_res_stub_code);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700571 // Put the image code address in the array
572 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i), image_res_stub_code);
Ian Rogersad25ac52011-10-04 19:13:33 -0700573 } else if (orig_method->IsDirect()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800574 // if it was resolved in the original, resolve it in the copy
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700575 Method* copy_method = down_cast<Method*>(GetLocalAddress(orig_method));
576 copy_cadms->Set(CodeAndDirectMethods::CodeIndex(i),
577 reinterpret_cast<int32_t>(copy_method->code_));
578 copy_cadms->Set(CodeAndDirectMethods::MethodIndex(i),
579 reinterpret_cast<int32_t>(GetImageAddress(orig_method)));
580 }
581 }
582}
583
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700584} // namespace art