blob: 186cb6cb433ad18b172088e1bc7e873212a31bf7 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/unix_file/fd_file.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Brian Carlstromae826982011-11-09 01:33:42 -080027#include "compiled_method.h"
Brian Carlstromf5822582012-03-19 22:34:31 -070028#include "compiler.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070029#include "dex_cache.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070030#include "gc/large_object_space.h"
31#include "gc/space.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070032#include "globals.h"
33#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070034#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070035#include "intern_table.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];
Ian Rogers64b6d142012-10-29 16:34:15 -070064 dex_caches_.insert(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -080065 }
66
Brian Carlstrom1cac3432012-12-12 10:56:22 -080067 oat_file_ = OatFile::Open(oat_filename, oat_location, NULL, true);
Brian Carlstromf5822582012-03-19 22:34:31 -070068 if (oat_file_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 LOG(ERROR) << "Failed to open oat file " << oat_filename;
70 return false;
71 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -070072 class_linker->RegisterOatFile(*oat_file_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070073
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 {
75 Thread::Current()->TransitionFromSuspendedToRunnable();
76 PruneNonImageClasses(); // Remove junk
77 ComputeLazyFieldsForImageClasses(); // Add useful information
78 ComputeEagerResolvedStrings();
79 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
80 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080081 heap->CollectGarbage(false); // Remove garbage
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070082 // Trim size of alloc spaces
83 // TODO: C++0x auto
84 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
85 if ((*cur)->IsAllocSpace()) {
86 (*cur)->AsAllocSpace()->Trim();
87 }
88 }
89
Brian Carlstromae826982011-11-09 01:33:42 -080090 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070091 return false;
92 }
Brian Carlstromae826982011-11-09 01:33:42 -080093#ifndef NDEBUG
Mathieu Chartier357e9be2012-08-01 11:00:14 -070094 {
95 ScopedObjectAccess soa(Thread::Current());
96 CheckNonImageClassesRemoved();
97 }
Brian Carlstromae826982011-11-09 01:33:42 -080098#endif
Mathieu Chartiere35517a2012-10-30 18:49:55 -070099 Thread::Current()->TransitionFromSuspendedToRunnable();
100 CalculateNewObjectOffsets();
101 CopyAndFixupObjects();
102 PatchOatCodeAndMethods(compiler);
103 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700104
Brian Carlstroma004aa92012-02-08 18:05:09 -0800105 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), true));
Elliott Hughes90a33692011-08-30 13:27:07 -0700106 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700107 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700108 return false;
109 }
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700110 if (fchmod(file->Fd(), 0644) != 0) {
111 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
112 return EXIT_FAILURE;
113 }
Ian Rogers30fab402012-01-23 15:43:46 -0800114 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115 if (!success) {
116 PLOG(ERROR) << "Failed to write image file " << image_filename;
117 return false;
118 }
119 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700120}
121
Brian Carlstromae826982011-11-09 01:33:42 -0800122bool ImageWriter::AllocMemory() {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700123 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700124 size_t size = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700125 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
126 if ((*it)->IsAllocSpace()) {
127 size += (*it)->Size();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700128 }
129 }
130
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700131 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700132 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800133 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700134 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700136 return false;
137 }
138 return true;
139}
140
Ian Rogersd418eda2012-01-30 12:14:28 -0800141void ImageWriter::ComputeLazyFieldsForImageClasses() {
142 Runtime* runtime = Runtime::Current();
143 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
Ian Rogersd418eda2012-01-30 12:14:28 -0800145}
146
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700147bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
148 c->ComputeName();
Ian Rogersd418eda2012-01-30 12:14:28 -0800149 return true;
150}
151
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800152void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
153 if (!obj->GetClass()->IsStringClass()) {
154 return;
155 }
156 String* string = obj->AsString();
157 std::string utf8_string(string->ToModifiedUtf8());
158 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800159 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
160 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
161 DexCache* dex_cache = *it;
Ian Rogers4445a7e2012-10-05 17:19:13 -0700162 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800163 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
164 if (string_id != NULL) {
165 // This string occurs in this dex file, assign the dex cache entry.
166 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
167 if (dex_cache->GetResolvedString(string_idx) == NULL) {
168 dex_cache->SetResolvedString(string_idx, string);
169 }
170 }
171 }
172}
173
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700174void ImageWriter::ComputeEagerResolvedStrings()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700176 // TODO: Check image spaces only?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700177 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700178 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700179 heap->FlushAllocStack();
180 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800181}
182
Brian Carlstromae826982011-11-09 01:33:42 -0800183bool ImageWriter::IsImageClass(const Class* klass) {
184 if (image_classes_ == NULL) {
185 return true;
186 }
187 while (klass->IsArrayClass()) {
188 klass = klass->GetComponentType();
189 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800190 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800191 return true;
192 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800194 return image_classes_->find(descriptor) != image_classes_->end();
195}
196
197
198struct NonImageClasses {
199 ImageWriter* image_writer;
200 std::set<std::string>* non_image_classes;
201};
202
203void ImageWriter::PruneNonImageClasses() {
204 if (image_classes_ == NULL) {
205 return;
206 }
207 Runtime* runtime = Runtime::Current();
208 ClassLinker* class_linker = runtime->GetClassLinker();
209
210 std::set<std::string> non_image_classes;
211 NonImageClasses context;
212 context.image_writer = this;
213 context.non_image_classes = &non_image_classes;
214 class_linker->VisitClasses(NonImageClassesVisitor, &context);
215
216 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
217 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800218 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800219 }
220
Mathieu Chartier66f19252012-09-18 08:57:04 -0700221 AbstractMethod* resolution_method = runtime->GetResolutionMethod();
Brian Carlstromae826982011-11-09 01:33:42 -0800222 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
223 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
224 DexCache* dex_cache = *it;
225 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
226 Class* klass = dex_cache->GetResolvedType(i);
227 if (klass != NULL && !IsImageClass(klass)) {
228 dex_cache->SetResolvedType(i, NULL);
229 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
230 }
231 }
232 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700233 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstromae826982011-11-09 01:33:42 -0800234 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
Ian Rogers19846512012-02-24 11:42:47 -0800235 dex_cache->SetResolvedMethod(i, resolution_method);
Brian Carlstromae826982011-11-09 01:33:42 -0800236 }
237 }
238 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
239 Field* field = dex_cache->GetResolvedField(i);
240 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
241 dex_cache->SetResolvedField(i, NULL);
242 }
243 }
244 }
245}
246
247bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
248 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
249 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800250 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800251 }
252 return true;
253}
254
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700255void ImageWriter::CheckNonImageClassesRemoved()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800257 if (image_classes_ == NULL) {
258 return;
259 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700260
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700261 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700262 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700263 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700264 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700265 heap->FlushAllocStack();
266 }
267
Ian Rogers50b35e22012-10-04 10:09:15 -0700268 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700269 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
Brian Carlstromae826982011-11-09 01:33:42 -0800270}
271
272void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
273 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
274 if (!obj->IsClass()) {
275 return;
276 }
277 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800278 if (!image_writer->IsImageClass(klass)) {
279 image_writer->DumpImageClasses();
280 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
281 << " " << PrettyDescriptor(klass);
282 }
283}
284
285void ImageWriter::DumpImageClasses() {
286 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
287 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
288 LOG(INFO) << " " << *it;
289 }
Brian Carlstromae826982011-11-09 01:33:42 -0800290}
291
Brian Carlstrom78128a62011-09-15 17:21:19 -0700292void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700293 DCHECK(obj != NULL);
294 DCHECK(arg != NULL);
295 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700296
297 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800298 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700299 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800300 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700301 DCHECK_EQ(obj, obj->AsString()->Intern());
302 return;
303 }
Ian Rogers1f539342012-10-03 21:09:42 -0700304 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700305 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800306 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700307 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700308 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700309 }
310 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800311 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700312 return;
313 }
314 // else (obj == interned), nothing to do but fall through to the normal case
315 }
316
317 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700318}
319
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700321 Runtime* runtime = Runtime::Current();
322 ClassLinker* class_linker = runtime->GetClassLinker();
323 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Ian Rogers50b35e22012-10-04 10:09:15 -0700324 Thread* self = Thread::Current();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700325
326 // build an Object[] of all the DexCaches used in the source_space_
Ian Rogers50b35e22012-10-04 10:09:15 -0700327 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800328 dex_caches_.size());
329 int i = 0;
330 typedef Set::const_iterator It; // TODO: C++0x auto
331 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
332 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700333 }
334
335 // build an Object[] of the roots needed to restore the runtime
Ian Rogers1f539342012-10-03 21:09:42 -0700336 SirtRef<ObjectArray<Object> >
Ian Rogers50b35e22012-10-04 10:09:15 -0700337 image_roots(self,
338 ObjectArray<Object>::Alloc(self, object_array_class,
339 ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800340 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700341 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
342 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700343 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
344 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
345 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
346 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers19846512012-02-24 11:42:47 -0800347 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700348 image_roots->Set(ImageHeader::kCalleeSaveMethod,
349 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
350 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
351 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
352 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
353 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354 image_roots->Set(ImageHeader::kOatLocation,
Ian Rogers50b35e22012-10-04 10:09:15 -0700355 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700356 image_roots->Set(ImageHeader::kDexCaches,
357 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700358 image_roots->Set(ImageHeader::kClassRoots,
359 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700360 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
361 CHECK(image_roots->Get(i) != NULL);
362 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700363 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700364}
365
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700366void ImageWriter::CalculateNewObjectOffsets() {
Ian Rogers1f539342012-10-03 21:09:42 -0700367 Thread* self = Thread::Current();
368 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700369
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700370 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700371 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700372 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800373 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700374
Brian Carlstrom16192862011-09-12 17:50:06 -0700375 // leave space for the header, but do not write it yet, we need to
376 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800377 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700378
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700379 {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700380 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700381 heap->FlushAllocStack();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700382 // TODO: Image spaces only?
383 // TODO: Add InOrderWalk to heap bitmap.
Ian Rogers1f539342012-10-03 21:09:42 -0700384 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700385 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
386 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700387 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700388 DCHECK_LT(image_end_, image_->Size());
389 }
Ian Rogers1f539342012-10-03 21:09:42 -0700390 self->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700391 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700392
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800394 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
395 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700396
Brian Carlstrom16192862011-09-12 17:50:06 -0700397 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800398 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700399 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700400 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800401 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700402 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800403 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700404}
405
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700406void ImageWriter::CopyAndFixupObjects()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700408 Thread* self = Thread::Current();
409 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800410 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700411 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800412 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700413 // TODO: Image spaces only?
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700414 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700415 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700416 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700417 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700418}
419
Brian Carlstrom78128a62011-09-15 17:21:19 -0700420void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700421 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700422 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700423 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700424 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700425
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700426 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700427 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800428 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700429 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700430 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800431 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700432 memcpy(dst, src, n);
433 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800434 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700435 image_writer->FixupObject(obj, copy);
436}
437
Brian Carlstrom4873d462011-08-21 15:23:39 -0700438void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700439 DCHECK(orig != NULL);
440 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700441 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700442 // TODO: special case init of pointers to malloc data (or removal of these pointers)
443 if (orig->IsClass()) {
444 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
445 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700446 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700447 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700448 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700449 } else {
450 FixupInstanceFields(orig, copy);
451 }
452}
453
Brian Carlstrom4873d462011-08-21 15:23:39 -0700454void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700455 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700456 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700457}
458
Mathieu Chartier66f19252012-09-18 08:57:04 -0700459void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700460 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700462 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800463 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700464
465 // Every type of method can have an invoke stub
466 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800467 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700468 copy->invoke_stub_ = reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(invoke_stub));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700469
470 if (orig->IsAbstract()) {
471 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
472 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
473 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
474 copy->code_ = copy_ame_stub_array_->GetData();
475 return;
476 }
477
Ian Rogers19846512012-02-24 11:42:47 -0800478 if (orig == Runtime::Current()->GetResolutionMethod()) {
479 // The resolution stub's code points at the unknown resolution trampoline
480 ByteArray* orig_res_stub_array_ =
481 Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod);
482 CHECK(orig->GetCode() == orig_res_stub_array_->GetData());
483 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
484 copy->code_ = copy_res_stub_array_->GetData();
485 return;
486 }
487
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700488 // Non-abstract methods typically have code
489 uint32_t code_offset = orig->GetOatCodeOffset();
Ian Rogers19846512012-02-24 11:42:47 -0800490 const byte* code = NULL;
491 if (orig->IsStatic()) {
492 // Static methods may point at the resolution trampoline stub
493 ByteArray* orig_res_stub_array_ =
494 Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod);
495 if (reinterpret_cast<int8_t*>(code_offset) == orig_res_stub_array_->GetData()) {
496 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
497 code = reinterpret_cast<const byte*>(copy_res_stub_array_->GetData());
498 }
499 }
500 if (code == NULL) {
501 code = GetOatAddress(code_offset);
502 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 copy->code_ = code;
504
Brian Carlstrom16192862011-09-12 17:50:06 -0700505 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700506 // The native method's pointer is directed to a stub to lookup via dlsym.
507 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800508 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700509 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
510 copy->native_method_ = copy_jni_stub_array_->GetData();
511 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700512 // normal (non-abstract non-native) methods have mapping tables to relocate
513 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800514 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700515 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
516
517 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800518 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700519 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800520
Ian Rogers0c7abda2012-09-19 13:33:42 -0700521 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
522 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
523 copy->native_gc_map_ = reinterpret_cast<const uint8_t*>(native_gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700524 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700525}
526
Brian Carlstrom4873d462011-08-21 15:23:39 -0700527void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700528 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700529 const Object* element = orig->Get(i);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700530 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700531 }
532}
533
Brian Carlstrom4873d462011-08-21 15:23:39 -0700534void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
535 DCHECK(orig != NULL);
536 DCHECK(copy != NULL);
537 Class* klass = orig->GetClass();
538 DCHECK(klass != NULL);
539 FixupFields(orig,
540 copy,
541 klass->GetReferenceInstanceOffsets(),
542 false);
543}
544
545void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
546 DCHECK(orig != NULL);
547 DCHECK(copy != NULL);
548 FixupFields(orig,
549 copy,
550 orig->GetReferenceStaticOffsets(),
551 true);
552}
553
554void ImageWriter::FixupFields(const Object* orig,
555 Object* copy,
556 uint32_t ref_offsets,
557 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700558 if (ref_offsets != CLASS_WALK_SUPER) {
559 // Found a reference offset bitmap. Fixup the specified offsets.
560 while (ref_offsets != 0) {
561 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700562 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
563 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700564 // Use SetFieldPtr to avoid card marking since we are writing to the image.
565 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700566 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
567 }
568 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700569 // There is no reference offset bitmap. In the non-static case,
570 // walk up the class inheritance hierarchy and find reference
571 // offsets the hard way. In the static case, just consider this
572 // class.
573 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700574 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700575 klass = is_static ? NULL : klass->GetSuperClass()) {
576 size_t num_reference_fields = (is_static
577 ? klass->NumReferenceStaticFields()
578 : klass->NumReferenceInstanceFields());
579 for (size_t i = 0; i < num_reference_fields; ++i) {
580 Field* field = (is_static
581 ? klass->GetStaticField(i)
582 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700583 MemberOffset field_offset = field->GetOffset();
584 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700585 // Use SetFieldPtr to avoid card marking since we are writing to the image.
586 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700587 }
588 }
589 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700590 if (!is_static && orig->IsReferenceInstance()) {
591 // Fix-up referent, that isn't marked as an object field, for References.
592 Field* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
593 MemberOffset field_offset = field->GetOffset();
594 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
595 // Use SetFieldPtr to avoid card marking since we are writing to the image.
596 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
597 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700598}
599
Mathieu Chartier66f19252012-09-18 08:57:04 -0700600static AbstractMethod* GetTargetMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700602 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700604 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700605 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700607 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700608 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700609 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700610 CHECK(method != NULL)
611 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700612 CHECK(!method->IsRuntimeMethod())
613 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700615 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700616 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700617 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700618 return method;
619}
620
621void ImageWriter::PatchOatCodeAndMethods(const Compiler& compiler) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700622 Thread* self = Thread::Current();
Brian Carlstromf5822582012-03-19 22:34:31 -0700623 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700624 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Brian Carlstromf5822582012-03-19 22:34:31 -0700625
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700626 typedef std::vector<const Compiler::PatchInformation*> Patches;
627 const Patches& code_to_patch = compiler.GetCodeToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700628 for (size_t i = 0; i < code_to_patch.size(); i++) {
629 const Compiler::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700630 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700631 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
632 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
633 uint32_t code_offset = code - code_base;
634 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
635 }
636
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700637 const Patches& methods_to_patch = compiler.GetMethodsToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700638 for (size_t i = 0; i < methods_to_patch.size(); i++) {
639 const Compiler::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700640 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700641 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
642 }
Brian Carlstroma85b8372012-10-18 17:00:32 -0700643
644 // Update the image header with the new checksum after patching
645 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
646 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700647 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromf5822582012-03-19 22:34:31 -0700648}
649
650void ImageWriter::SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) {
651 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700652 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
653 patch->GetReferrerMethodIdx());
Brian Carlstroma85b8372012-10-18 17:00:32 -0700654 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
Brian Carlstromf5822582012-03-19 22:34:31 -0700655 // TODO: make this Thumb2 specific
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700656 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
Brian Carlstromf5822582012-03-19 22:34:31 -0700657 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
658#ifndef NDEBUG
659 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
660 uint32_t expected = reinterpret_cast<uint32_t>(&id);
661 uint32_t actual = *patch_location;
662 CHECK(actual == expected || actual == value) << std::hex
663 << "actual=" << actual
664 << "expected=" << expected
665 << "value=" << value;
666#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700667 *patch_location = value;
Brian Carlstroma85b8372012-10-18 17:00:32 -0700668 oat_header.UpdateChecksum(patch_location, sizeof(value));
Brian Carlstromf5822582012-03-19 22:34:31 -0700669}
670
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700671} // namespace art