blob: 642cee6a63f479f44a326ad3c89a14efec41a984 [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
Brian Carlstrom6cd40e52012-05-03 14:15:11 -070019#include <sys/stat.h>
Elliott Hughes90a33692011-08-30 13:27:07 -070020
Brian Carlstromdb4d5402011-08-09 12:18:28 -070021#include <vector>
22
Brian Carlstroma663ea52011-08-19 23:33:41 -070023#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_loader.h"
Brian Carlstromae826982011-11-09 01:33:42 -080025#include "compiled_method.h"
Brian Carlstromf5822582012-03-19 22:34:31 -070026#include "compiler.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070027#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028#include "file.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070029#include "gc/large_object_space.h"
30#include "gc/space.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070031#include "globals.h"
32#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070033#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070034#include "intern_table.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070035#include "logging.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080036#include "oat_file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070037#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080038#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070039#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070041#include "sirt_ref.h"
Elliott Hughesa168c832012-06-12 15:34:20 -070042#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043#include "utils.h"
44
45namespace art {
46
Brian Carlstroma004aa92012-02-08 18:05:09 -080047bool ImageWriter::Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080048 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080049 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070050 const std::string& oat_location,
51 const Compiler& compiler) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080052 CHECK(!image_filename.empty());
Brian Carlstromaded5f72011-10-07 17:15:04 -070053
Ian Rogers30fab402012-01-23 15:43:46 -080054 CHECK_NE(image_begin, 0U);
55 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070056
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080057 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070058 const Spaces& spaces = heap->GetSpaces();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070059
Brian Carlstromae826982011-11-09 01:33:42 -080060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
62 for (size_t i = 0; i < all_dex_caches.size(); i++) {
63 DexCache* dex_cache = all_dex_caches[i];
64 if (InSourceSpace(dex_cache)) {
65 dex_caches_.insert(dex_cache);
66 }
67 }
68
Logan Chien0c717dd2012-03-28 18:31:07 +080069 oat_file_ = OatFile::Open(oat_filename, oat_location, NULL,
70 OatFile::kRelocNone, true);
Brian Carlstromf5822582012-03-19 22:34:31 -070071 if (oat_file_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 LOG(ERROR) << "Failed to open oat file " << oat_filename;
73 return false;
74 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -070075 class_linker->RegisterOatFile(*oat_file_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070076
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077 {
78 Thread::Current()->TransitionFromSuspendedToRunnable();
79 PruneNonImageClasses(); // Remove junk
80 ComputeLazyFieldsForImageClasses(); // Add useful information
81 ComputeEagerResolvedStrings();
82 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
83 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080084 heap->CollectGarbage(false); // Remove garbage
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070085 // Trim size of alloc spaces
86 // TODO: C++0x auto
87 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
88 if ((*cur)->IsAllocSpace()) {
89 (*cur)->AsAllocSpace()->Trim();
90 }
91 }
92
Brian Carlstromae826982011-11-09 01:33:42 -080093 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070094 return false;
95 }
Brian Carlstromae826982011-11-09 01:33:42 -080096#ifndef NDEBUG
Mathieu Chartier357e9be2012-08-01 11:00:14 -070097 {
98 ScopedObjectAccess soa(Thread::Current());
99 CheckNonImageClassesRemoved();
100 }
Brian Carlstromae826982011-11-09 01:33:42 -0800101#endif
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700102 Thread::Current()->TransitionFromSuspendedToRunnable();
103 CalculateNewObjectOffsets();
104 CopyAndFixupObjects();
105 PatchOatCodeAndMethods(compiler);
106 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700107
Brian Carlstroma004aa92012-02-08 18:05:09 -0800108 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), true));
Elliott Hughes90a33692011-08-30 13:27:07 -0700109 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700111 return false;
112 }
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700113 if (fchmod(file->Fd(), 0644) != 0) {
114 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
115 return EXIT_FAILURE;
116 }
Ian Rogers30fab402012-01-23 15:43:46 -0800117 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118 if (!success) {
119 PLOG(ERROR) << "Failed to write image file " << image_filename;
120 return false;
121 }
122 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700123}
124
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700125bool ImageWriter::InSourceSpace(const Object* object) const {
126 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
127 // TODO: C++0x auto
128 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
129 if ((*cur)->IsAllocSpace() && (*cur)->Contains(object)) {
130 return true;
131 }
132 }
133 return false;
134}
135
Brian Carlstromae826982011-11-09 01:33:42 -0800136bool ImageWriter::AllocMemory() {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700137 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 size_t size = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700139 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
140 if ((*it)->IsAllocSpace()) {
141 size += (*it)->Size();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700142 }
143 }
144
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700145 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700146 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800147 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700148 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700150 return false;
151 }
152 return true;
153}
154
Ian Rogersd418eda2012-01-30 12:14:28 -0800155void ImageWriter::ComputeLazyFieldsForImageClasses() {
156 Runtime* runtime = Runtime::Current();
157 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
Ian Rogersd418eda2012-01-30 12:14:28 -0800159}
160
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700161bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
162 c->ComputeName();
Ian Rogersd418eda2012-01-30 12:14:28 -0800163 return true;
164}
165
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800166void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
167 if (!obj->GetClass()->IsStringClass()) {
168 return;
169 }
170 String* string = obj->AsString();
171 std::string utf8_string(string->ToModifiedUtf8());
172 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800173 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
174 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
175 DexCache* dex_cache = *it;
Ian Rogers4445a7e2012-10-05 17:19:13 -0700176 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800177 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
178 if (string_id != NULL) {
179 // This string occurs in this dex file, assign the dex cache entry.
180 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
181 if (dex_cache->GetResolvedString(string_idx) == NULL) {
182 dex_cache->SetResolvedString(string_idx, string);
183 }
184 }
185 }
186}
187
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700188void ImageWriter::ComputeEagerResolvedStrings()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700190 // TODO: Check image spaces only?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700191 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700192 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700193 heap->FlushAllocStack();
194 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800195}
196
Brian Carlstromae826982011-11-09 01:33:42 -0800197bool ImageWriter::IsImageClass(const Class* klass) {
198 if (image_classes_ == NULL) {
199 return true;
200 }
201 while (klass->IsArrayClass()) {
202 klass = klass->GetComponentType();
203 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800204 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800205 return true;
206 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800208 return image_classes_->find(descriptor) != image_classes_->end();
209}
210
211
212struct NonImageClasses {
213 ImageWriter* image_writer;
214 std::set<std::string>* non_image_classes;
215};
216
217void ImageWriter::PruneNonImageClasses() {
218 if (image_classes_ == NULL) {
219 return;
220 }
221 Runtime* runtime = Runtime::Current();
222 ClassLinker* class_linker = runtime->GetClassLinker();
223
224 std::set<std::string> non_image_classes;
225 NonImageClasses context;
226 context.image_writer = this;
227 context.non_image_classes = &non_image_classes;
228 class_linker->VisitClasses(NonImageClassesVisitor, &context);
229
230 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
231 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800232 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800233 }
234
Mathieu Chartier66f19252012-09-18 08:57:04 -0700235 AbstractMethod* resolution_method = runtime->GetResolutionMethod();
Brian Carlstromae826982011-11-09 01:33:42 -0800236 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
237 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
238 DexCache* dex_cache = *it;
239 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
240 Class* klass = dex_cache->GetResolvedType(i);
241 if (klass != NULL && !IsImageClass(klass)) {
242 dex_cache->SetResolvedType(i, NULL);
243 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
244 }
245 }
246 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700247 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstromae826982011-11-09 01:33:42 -0800248 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
Ian Rogers19846512012-02-24 11:42:47 -0800249 dex_cache->SetResolvedMethod(i, resolution_method);
Brian Carlstromae826982011-11-09 01:33:42 -0800250 }
251 }
252 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
253 Field* field = dex_cache->GetResolvedField(i);
254 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
255 dex_cache->SetResolvedField(i, NULL);
256 }
257 }
258 }
259}
260
261bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
262 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
263 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800264 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800265 }
266 return true;
267}
268
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700269void ImageWriter::CheckNonImageClassesRemoved()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800271 if (image_classes_ == NULL) {
272 return;
273 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700274
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700275 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700276 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700277 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700278 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700279 heap->FlushAllocStack();
280 }
281
Ian Rogers50b35e22012-10-04 10:09:15 -0700282 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700283 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
Brian Carlstromae826982011-11-09 01:33:42 -0800284}
285
286void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
287 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
288 if (!obj->IsClass()) {
289 return;
290 }
291 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800292 if (!image_writer->IsImageClass(klass)) {
293 image_writer->DumpImageClasses();
294 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
295 << " " << PrettyDescriptor(klass);
296 }
297}
298
299void ImageWriter::DumpImageClasses() {
300 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
301 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
302 LOG(INFO) << " " << *it;
303 }
Brian Carlstromae826982011-11-09 01:33:42 -0800304}
305
Brian Carlstrom78128a62011-09-15 17:21:19 -0700306void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700307 DCHECK(obj != NULL);
308 DCHECK(arg != NULL);
309 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700310 if (!image_writer->InSourceSpace(obj)) {
311 return;
312 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700313
314 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800315 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700316 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800317 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700318 DCHECK_EQ(obj, obj->AsString()->Intern());
319 return;
320 }
Ian Rogers1f539342012-10-03 21:09:42 -0700321 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700322 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800323 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700324 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700325 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700326 }
327 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800328 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700329 return;
330 }
331 // else (obj == interned), nothing to do but fall through to the normal case
332 }
333
334 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700335}
336
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700338 Runtime* runtime = Runtime::Current();
339 ClassLinker* class_linker = runtime->GetClassLinker();
340 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Ian Rogers50b35e22012-10-04 10:09:15 -0700341 Thread* self = Thread::Current();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700342
343 // build an Object[] of all the DexCaches used in the source_space_
Ian Rogers50b35e22012-10-04 10:09:15 -0700344 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800345 dex_caches_.size());
346 int i = 0;
347 typedef Set::const_iterator It; // TODO: C++0x auto
348 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
349 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700350 }
351
352 // build an Object[] of the roots needed to restore the runtime
Ian Rogers1f539342012-10-03 21:09:42 -0700353 SirtRef<ObjectArray<Object> >
Ian Rogers50b35e22012-10-04 10:09:15 -0700354 image_roots(self,
355 ObjectArray<Object>::Alloc(self, object_array_class,
356 ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800357 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
359 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700360 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
361 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
362 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
363 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers19846512012-02-24 11:42:47 -0800364 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700365 image_roots->Set(ImageHeader::kCalleeSaveMethod,
366 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
367 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
368 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
369 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
370 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700371 image_roots->Set(ImageHeader::kOatLocation,
Ian Rogers50b35e22012-10-04 10:09:15 -0700372 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700373 image_roots->Set(ImageHeader::kDexCaches,
374 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700375 image_roots->Set(ImageHeader::kClassRoots,
376 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700377 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
378 CHECK(image_roots->Get(i) != NULL);
379 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700380 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700381}
382
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700383void ImageWriter::CalculateNewObjectOffsets() {
Ian Rogers1f539342012-10-03 21:09:42 -0700384 Thread* self = Thread::Current();
385 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700386
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700387 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700388 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700389 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800390 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700391
Brian Carlstrom16192862011-09-12 17:50:06 -0700392 // leave space for the header, but do not write it yet, we need to
393 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800394 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700395
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700396 {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700397 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700398 heap->FlushAllocStack();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700399 // TODO: Image spaces only?
400 // TODO: Add InOrderWalk to heap bitmap.
Ian Rogers1f539342012-10-03 21:09:42 -0700401 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700402 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
403 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700404 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700405 DCHECK_LT(image_end_, image_->Size());
406 }
Ian Rogers1f539342012-10-03 21:09:42 -0700407 self->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700408 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700409
Brian Carlstrome24fa612011-09-29 00:53:55 -0700410 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800411 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
412 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700413
Brian Carlstrom16192862011-09-12 17:50:06 -0700414 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800415 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700416 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800418 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700419 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800420 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700421}
422
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700423void ImageWriter::CopyAndFixupObjects()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700424 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700425 Thread* self = Thread::Current();
426 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800427 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800429 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700430 // TODO: Image spaces only?
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700431 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700432 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700433 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700434 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700435}
436
Brian Carlstrom78128a62011-09-15 17:21:19 -0700437void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700438 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700439 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700440 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700441 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700442 if (!image_writer->InSourceSpace(object)) {
443 return;
444 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700445
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700446 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700447 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800448 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700449 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700450 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800451 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700452 memcpy(dst, src, n);
453 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800454 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700455 image_writer->FixupObject(obj, copy);
456}
457
Brian Carlstrom4873d462011-08-21 15:23:39 -0700458void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700459 DCHECK(orig != NULL);
460 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700461 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700462 // TODO: special case init of pointers to malloc data (or removal of these pointers)
463 if (orig->IsClass()) {
464 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
465 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700466 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700467 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700468 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700469 } else {
470 FixupInstanceFields(orig, copy);
471 }
472}
473
Brian Carlstrom4873d462011-08-21 15:23:39 -0700474void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700475 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700476 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700477}
478
Mathieu Chartier66f19252012-09-18 08:57:04 -0700479void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700480 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700481
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800483 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700484
485 // Every type of method can have an invoke stub
486 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800487 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700488 copy->invoke_stub_ = reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(invoke_stub));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700489
490 if (orig->IsAbstract()) {
491 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
492 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
493 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
494 copy->code_ = copy_ame_stub_array_->GetData();
495 return;
496 }
497
Ian Rogers19846512012-02-24 11:42:47 -0800498 if (orig == Runtime::Current()->GetResolutionMethod()) {
499 // The resolution stub's code points at the unknown resolution trampoline
500 ByteArray* orig_res_stub_array_ =
501 Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod);
502 CHECK(orig->GetCode() == orig_res_stub_array_->GetData());
503 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
504 copy->code_ = copy_res_stub_array_->GetData();
505 return;
506 }
507
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700508 // Non-abstract methods typically have code
509 uint32_t code_offset = orig->GetOatCodeOffset();
Ian Rogers19846512012-02-24 11:42:47 -0800510 const byte* code = NULL;
511 if (orig->IsStatic()) {
512 // Static methods may point at the resolution trampoline stub
513 ByteArray* orig_res_stub_array_ =
514 Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod);
515 if (reinterpret_cast<int8_t*>(code_offset) == orig_res_stub_array_->GetData()) {
516 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
517 code = reinterpret_cast<const byte*>(copy_res_stub_array_->GetData());
518 }
519 }
520 if (code == NULL) {
521 code = GetOatAddress(code_offset);
522 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 copy->code_ = code;
524
Brian Carlstrom16192862011-09-12 17:50:06 -0700525 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700526 // The native method's pointer is directed to a stub to lookup via dlsym.
527 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800528 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700529 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
530 copy->native_method_ = copy_jni_stub_array_->GetData();
531 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700532 // normal (non-abstract non-native) methods have mapping tables to relocate
533 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800534 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
536
537 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800538 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700539 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800540
Ian Rogers0c7abda2012-09-19 13:33:42 -0700541 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
542 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
543 copy->native_gc_map_ = reinterpret_cast<const uint8_t*>(native_gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700544 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700545}
546
Brian Carlstrom4873d462011-08-21 15:23:39 -0700547void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700548 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700549 const Object* element = orig->Get(i);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700550 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700551 }
552}
553
Brian Carlstrom4873d462011-08-21 15:23:39 -0700554void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
555 DCHECK(orig != NULL);
556 DCHECK(copy != NULL);
557 Class* klass = orig->GetClass();
558 DCHECK(klass != NULL);
559 FixupFields(orig,
560 copy,
561 klass->GetReferenceInstanceOffsets(),
562 false);
563}
564
565void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
566 DCHECK(orig != NULL);
567 DCHECK(copy != NULL);
568 FixupFields(orig,
569 copy,
570 orig->GetReferenceStaticOffsets(),
571 true);
572}
573
574void ImageWriter::FixupFields(const Object* orig,
575 Object* copy,
576 uint32_t ref_offsets,
577 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700578 if (ref_offsets != CLASS_WALK_SUPER) {
579 // Found a reference offset bitmap. Fixup the specified offsets.
580 while (ref_offsets != 0) {
581 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700582 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
583 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700584 // Use SetFieldPtr to avoid card marking since we are writing to the image.
585 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700586 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
587 }
588 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700589 // There is no reference offset bitmap. In the non-static case,
590 // walk up the class inheritance hierarchy and find reference
591 // offsets the hard way. In the static case, just consider this
592 // class.
593 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700594 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700595 klass = is_static ? NULL : klass->GetSuperClass()) {
596 size_t num_reference_fields = (is_static
597 ? klass->NumReferenceStaticFields()
598 : klass->NumReferenceInstanceFields());
599 for (size_t i = 0; i < num_reference_fields; ++i) {
600 Field* field = (is_static
601 ? klass->GetStaticField(i)
602 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700603 MemberOffset field_offset = field->GetOffset();
604 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700605 // Use SetFieldPtr to avoid card marking since we are writing to the image.
606 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700607 }
608 }
609 }
610}
611
Mathieu Chartier66f19252012-09-18 08:57:04 -0700612static AbstractMethod* GetTargetMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700613 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700614 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700616 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700617 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700619 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700620 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700621 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700622 CHECK(method != NULL)
623 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700624 CHECK(!method->IsRuntimeMethod())
625 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700627 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700629 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700630 return method;
631}
632
633void ImageWriter::PatchOatCodeAndMethods(const Compiler& compiler) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700634 Thread* self = Thread::Current();
Brian Carlstromf5822582012-03-19 22:34:31 -0700635 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700636 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Brian Carlstromf5822582012-03-19 22:34:31 -0700637
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700638 typedef std::vector<const Compiler::PatchInformation*> Patches;
639 const Patches& code_to_patch = compiler.GetCodeToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700640 for (size_t i = 0; i < code_to_patch.size(); i++) {
641 const Compiler::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700642 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700643 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
644 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
645 uint32_t code_offset = code - code_base;
646 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
647 }
648
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700649 const Patches& methods_to_patch = compiler.GetMethodsToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700650 for (size_t i = 0; i < methods_to_patch.size(); i++) {
651 const Compiler::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700652 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700653 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
654 }
Brian Carlstroma85b8372012-10-18 17:00:32 -0700655
656 // Update the image header with the new checksum after patching
657 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
658 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700659 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromf5822582012-03-19 22:34:31 -0700660}
661
662void ImageWriter::SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) {
663 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700664 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
665 patch->GetReferrerMethodIdx());
Brian Carlstroma85b8372012-10-18 17:00:32 -0700666 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
Brian Carlstromf5822582012-03-19 22:34:31 -0700667 // TODO: make this Thumb2 specific
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700668 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
Brian Carlstromf5822582012-03-19 22:34:31 -0700669 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
670#ifndef NDEBUG
671 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
672 uint32_t expected = reinterpret_cast<uint32_t>(&id);
673 uint32_t actual = *patch_location;
674 CHECK(actual == expected || actual == value) << std::hex
675 << "actual=" << actual
676 << "expected=" << expected
677 << "value=" << value;
678#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700679 *patch_location = value;
Brian Carlstroma85b8372012-10-18 17:00:32 -0700680 oat_header.UpdateChecksum(patch_location, sizeof(value));
Brian Carlstromf5822582012-03-19 22:34:31 -0700681}
682
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700683} // namespace art