blob: 6065760ae0da3c14000e3db7c64f7062b65cb1ac [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 Hughes76160052012-12-12 16:31:20 -080023#include "base/unix_file/fd_file.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"
Brian Carlstromf5822582012-03-19 22:34:31 -070027#include "compiler.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "dex_cache.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];
Ian Rogers64b6d142012-10-29 16:34:15 -070064 dex_caches_.insert(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -080065 }
66
Logan Chien0c717dd2012-03-28 18:31:07 +080067 oat_file_ = OatFile::Open(oat_filename, oat_location, NULL,
68 OatFile::kRelocNone, true);
Brian Carlstromf5822582012-03-19 22:34:31 -070069 if (oat_file_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070070 LOG(ERROR) << "Failed to open oat file " << oat_filename;
71 return false;
72 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -070073 class_linker->RegisterOatFile(*oat_file_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070074
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 {
76 Thread::Current()->TransitionFromSuspendedToRunnable();
77 PruneNonImageClasses(); // Remove junk
78 ComputeLazyFieldsForImageClasses(); // Add useful information
79 ComputeEagerResolvedStrings();
80 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
81 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080082 heap->CollectGarbage(false); // Remove garbage
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070083 // Trim size of alloc spaces
84 // TODO: C++0x auto
85 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
86 if ((*cur)->IsAllocSpace()) {
87 (*cur)->AsAllocSpace()->Trim();
88 }
89 }
90
Brian Carlstromae826982011-11-09 01:33:42 -080091 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070092 return false;
93 }
Brian Carlstromae826982011-11-09 01:33:42 -080094#ifndef NDEBUG
Mathieu Chartier357e9be2012-08-01 11:00:14 -070095 {
96 ScopedObjectAccess soa(Thread::Current());
97 CheckNonImageClassesRemoved();
98 }
Brian Carlstromae826982011-11-09 01:33:42 -080099#endif
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700100 Thread::Current()->TransitionFromSuspendedToRunnable();
101 CalculateNewObjectOffsets();
102 CopyAndFixupObjects();
103 PatchOatCodeAndMethods(compiler);
104 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700105
Brian Carlstroma004aa92012-02-08 18:05:09 -0800106 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), true));
Elliott Hughes90a33692011-08-30 13:27:07 -0700107 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700109 return false;
110 }
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700111 if (fchmod(file->Fd(), 0644) != 0) {
112 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
113 return EXIT_FAILURE;
114 }
Ian Rogers30fab402012-01-23 15:43:46 -0800115 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116 if (!success) {
117 PLOG(ERROR) << "Failed to write image file " << image_filename;
118 return false;
119 }
120 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700121}
122
Brian Carlstromae826982011-11-09 01:33:42 -0800123bool ImageWriter::AllocMemory() {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700124 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700125 size_t size = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700126 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
127 if ((*it)->IsAllocSpace()) {
128 size += (*it)->Size();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700129 }
130 }
131
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700132 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700133 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800134 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700135 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700136 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700137 return false;
138 }
139 return true;
140}
141
Ian Rogersd418eda2012-01-30 12:14:28 -0800142void ImageWriter::ComputeLazyFieldsForImageClasses() {
143 Runtime* runtime = Runtime::Current();
144 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
Ian Rogersd418eda2012-01-30 12:14:28 -0800146}
147
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700148bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
149 c->ComputeName();
Ian Rogersd418eda2012-01-30 12:14:28 -0800150 return true;
151}
152
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800153void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
154 if (!obj->GetClass()->IsStringClass()) {
155 return;
156 }
157 String* string = obj->AsString();
158 std::string utf8_string(string->ToModifiedUtf8());
159 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800160 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
161 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
162 DexCache* dex_cache = *it;
Ian Rogers4445a7e2012-10-05 17:19:13 -0700163 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800164 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
165 if (string_id != NULL) {
166 // This string occurs in this dex file, assign the dex cache entry.
167 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
168 if (dex_cache->GetResolvedString(string_idx) == NULL) {
169 dex_cache->SetResolvedString(string_idx, string);
170 }
171 }
172 }
173}
174
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700175void ImageWriter::ComputeEagerResolvedStrings()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700177 // TODO: Check image spaces only?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700178 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700179 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700180 heap->FlushAllocStack();
181 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800182}
183
Brian Carlstromae826982011-11-09 01:33:42 -0800184bool ImageWriter::IsImageClass(const Class* klass) {
185 if (image_classes_ == NULL) {
186 return true;
187 }
188 while (klass->IsArrayClass()) {
189 klass = klass->GetComponentType();
190 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800191 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800192 return true;
193 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800194 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800195 return image_classes_->find(descriptor) != image_classes_->end();
196}
197
198
199struct NonImageClasses {
200 ImageWriter* image_writer;
201 std::set<std::string>* non_image_classes;
202};
203
204void ImageWriter::PruneNonImageClasses() {
205 if (image_classes_ == NULL) {
206 return;
207 }
208 Runtime* runtime = Runtime::Current();
209 ClassLinker* class_linker = runtime->GetClassLinker();
210
211 std::set<std::string> non_image_classes;
212 NonImageClasses context;
213 context.image_writer = this;
214 context.non_image_classes = &non_image_classes;
215 class_linker->VisitClasses(NonImageClassesVisitor, &context);
216
217 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
218 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800219 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800220 }
221
Mathieu Chartier66f19252012-09-18 08:57:04 -0700222 AbstractMethod* resolution_method = runtime->GetResolutionMethod();
Brian Carlstromae826982011-11-09 01:33:42 -0800223 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
224 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
225 DexCache* dex_cache = *it;
226 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
227 Class* klass = dex_cache->GetResolvedType(i);
228 if (klass != NULL && !IsImageClass(klass)) {
229 dex_cache->SetResolvedType(i, NULL);
230 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
231 }
232 }
233 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700234 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstromae826982011-11-09 01:33:42 -0800235 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
Ian Rogers19846512012-02-24 11:42:47 -0800236 dex_cache->SetResolvedMethod(i, resolution_method);
Brian Carlstromae826982011-11-09 01:33:42 -0800237 }
238 }
239 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
240 Field* field = dex_cache->GetResolvedField(i);
241 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
242 dex_cache->SetResolvedField(i, NULL);
243 }
244 }
245 }
246}
247
248bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
249 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
250 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800251 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800252 }
253 return true;
254}
255
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700256void ImageWriter::CheckNonImageClassesRemoved()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800258 if (image_classes_ == NULL) {
259 return;
260 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700261
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700262 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700263 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700264 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700265 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700266 heap->FlushAllocStack();
267 }
268
Ian Rogers50b35e22012-10-04 10:09:15 -0700269 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700270 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
Brian Carlstromae826982011-11-09 01:33:42 -0800271}
272
273void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
274 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
275 if (!obj->IsClass()) {
276 return;
277 }
278 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800279 if (!image_writer->IsImageClass(klass)) {
280 image_writer->DumpImageClasses();
281 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
282 << " " << PrettyDescriptor(klass);
283 }
284}
285
286void ImageWriter::DumpImageClasses() {
287 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
288 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
289 LOG(INFO) << " " << *it;
290 }
Brian Carlstromae826982011-11-09 01:33:42 -0800291}
292
Brian Carlstrom78128a62011-09-15 17:21:19 -0700293void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700294 DCHECK(obj != NULL);
295 DCHECK(arg != NULL);
296 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700297
298 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800299 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700300 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800301 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700302 DCHECK_EQ(obj, obj->AsString()->Intern());
303 return;
304 }
Ian Rogers1f539342012-10-03 21:09:42 -0700305 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700306 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800307 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700308 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700309 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700310 }
311 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800312 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700313 return;
314 }
315 // else (obj == interned), nothing to do but fall through to the normal case
316 }
317
318 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700319}
320
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700322 Runtime* runtime = Runtime::Current();
323 ClassLinker* class_linker = runtime->GetClassLinker();
324 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Ian Rogers50b35e22012-10-04 10:09:15 -0700325 Thread* self = Thread::Current();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700326
327 // build an Object[] of all the DexCaches used in the source_space_
Ian Rogers50b35e22012-10-04 10:09:15 -0700328 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800329 dex_caches_.size());
330 int i = 0;
331 typedef Set::const_iterator It; // TODO: C++0x auto
332 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
333 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700334 }
335
336 // build an Object[] of the roots needed to restore the runtime
Ian Rogers1f539342012-10-03 21:09:42 -0700337 SirtRef<ObjectArray<Object> >
Ian Rogers50b35e22012-10-04 10:09:15 -0700338 image_roots(self,
339 ObjectArray<Object>::Alloc(self, object_array_class,
340 ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800341 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700342 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
343 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700344 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
345 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
346 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
347 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers19846512012-02-24 11:42:47 -0800348 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700349 image_roots->Set(ImageHeader::kCalleeSaveMethod,
350 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
351 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
352 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
353 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
354 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700355 image_roots->Set(ImageHeader::kOatLocation,
Ian Rogers50b35e22012-10-04 10:09:15 -0700356 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700357 image_roots->Set(ImageHeader::kDexCaches,
358 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700359 image_roots->Set(ImageHeader::kClassRoots,
360 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
362 CHECK(image_roots->Get(i) != NULL);
363 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700364 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700365}
366
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700367void ImageWriter::CalculateNewObjectOffsets() {
Ian Rogers1f539342012-10-03 21:09:42 -0700368 Thread* self = Thread::Current();
369 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700370
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700371 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700372 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700373 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800374 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700375
Brian Carlstrom16192862011-09-12 17:50:06 -0700376 // leave space for the header, but do not write it yet, we need to
377 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800378 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700379
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700380 {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700381 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700382 heap->FlushAllocStack();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700383 // TODO: Image spaces only?
384 // TODO: Add InOrderWalk to heap bitmap.
Ian Rogers1f539342012-10-03 21:09:42 -0700385 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700386 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
387 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700388 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700389 DCHECK_LT(image_end_, image_->Size());
390 }
Ian Rogers1f539342012-10-03 21:09:42 -0700391 self->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700392 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700393
Brian Carlstrome24fa612011-09-29 00:53:55 -0700394 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800395 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
396 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700397
Brian Carlstrom16192862011-09-12 17:50:06 -0700398 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800399 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700400 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800402 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700403 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800404 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700405}
406
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700407void ImageWriter::CopyAndFixupObjects()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700408 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700409 Thread* self = Thread::Current();
410 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800411 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700412 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800413 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700414 // TODO: Image spaces only?
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700415 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700416 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700417 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700418 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700419}
420
Brian Carlstrom78128a62011-09-15 17:21:19 -0700421void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700422 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700423 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700424 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700425 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700426
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700427 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700428 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800429 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700430 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700431 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800432 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700433 memcpy(dst, src, n);
434 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800435 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700436 image_writer->FixupObject(obj, copy);
437}
438
Brian Carlstrom4873d462011-08-21 15:23:39 -0700439void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700440 DCHECK(orig != NULL);
441 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700442 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700443 // TODO: special case init of pointers to malloc data (or removal of these pointers)
444 if (orig->IsClass()) {
445 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
446 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700447 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700448 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700449 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700450 } else {
451 FixupInstanceFields(orig, copy);
452 }
453}
454
Brian Carlstrom4873d462011-08-21 15:23:39 -0700455void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700456 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700457 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700458}
459
Mathieu Chartier66f19252012-09-18 08:57:04 -0700460void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700461 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700462
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700463 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800464 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700465
466 // Every type of method can have an invoke stub
467 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800468 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700469 copy->invoke_stub_ = reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(invoke_stub));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700470
471 if (orig->IsAbstract()) {
472 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
473 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
474 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
475 copy->code_ = copy_ame_stub_array_->GetData();
476 return;
477 }
478
Ian Rogers19846512012-02-24 11:42:47 -0800479 if (orig == Runtime::Current()->GetResolutionMethod()) {
480 // The resolution stub's code points at the unknown resolution trampoline
481 ByteArray* orig_res_stub_array_ =
482 Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod);
483 CHECK(orig->GetCode() == orig_res_stub_array_->GetData());
484 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
485 copy->code_ = copy_res_stub_array_->GetData();
486 return;
487 }
488
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700489 // Non-abstract methods typically have code
490 uint32_t code_offset = orig->GetOatCodeOffset();
Ian Rogers19846512012-02-24 11:42:47 -0800491 const byte* code = NULL;
492 if (orig->IsStatic()) {
493 // Static methods may point at the resolution trampoline stub
494 ByteArray* orig_res_stub_array_ =
495 Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod);
496 if (reinterpret_cast<int8_t*>(code_offset) == orig_res_stub_array_->GetData()) {
497 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
498 code = reinterpret_cast<const byte*>(copy_res_stub_array_->GetData());
499 }
500 }
501 if (code == NULL) {
502 code = GetOatAddress(code_offset);
503 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700504 copy->code_ = code;
505
Brian Carlstrom16192862011-09-12 17:50:06 -0700506 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 // The native method's pointer is directed to a stub to lookup via dlsym.
508 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800509 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700510 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
511 copy->native_method_ = copy_jni_stub_array_->GetData();
512 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700513 // normal (non-abstract non-native) methods have mapping tables to relocate
514 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800515 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700516 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
517
518 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800519 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700520 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800521
Ian Rogers0c7abda2012-09-19 13:33:42 -0700522 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
523 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
524 copy->native_gc_map_ = reinterpret_cast<const uint8_t*>(native_gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700525 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700526}
527
Brian Carlstrom4873d462011-08-21 15:23:39 -0700528void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700529 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700530 const Object* element = orig->Get(i);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700531 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700532 }
533}
534
Brian Carlstrom4873d462011-08-21 15:23:39 -0700535void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
536 DCHECK(orig != NULL);
537 DCHECK(copy != NULL);
538 Class* klass = orig->GetClass();
539 DCHECK(klass != NULL);
540 FixupFields(orig,
541 copy,
542 klass->GetReferenceInstanceOffsets(),
543 false);
544}
545
546void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
547 DCHECK(orig != NULL);
548 DCHECK(copy != NULL);
549 FixupFields(orig,
550 copy,
551 orig->GetReferenceStaticOffsets(),
552 true);
553}
554
555void ImageWriter::FixupFields(const Object* orig,
556 Object* copy,
557 uint32_t ref_offsets,
558 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700559 if (ref_offsets != CLASS_WALK_SUPER) {
560 // Found a reference offset bitmap. Fixup the specified offsets.
561 while (ref_offsets != 0) {
562 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700563 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
564 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700565 // Use SetFieldPtr to avoid card marking since we are writing to the image.
566 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700567 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
568 }
569 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700570 // There is no reference offset bitmap. In the non-static case,
571 // walk up the class inheritance hierarchy and find reference
572 // offsets the hard way. In the static case, just consider this
573 // class.
574 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700575 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700576 klass = is_static ? NULL : klass->GetSuperClass()) {
577 size_t num_reference_fields = (is_static
578 ? klass->NumReferenceStaticFields()
579 : klass->NumReferenceInstanceFields());
580 for (size_t i = 0; i < num_reference_fields; ++i) {
581 Field* field = (is_static
582 ? klass->GetStaticField(i)
583 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700584 MemberOffset field_offset = field->GetOffset();
585 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700586 // Use SetFieldPtr to avoid card marking since we are writing to the image.
587 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700588 }
589 }
590 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700591 if (!is_static && orig->IsReferenceInstance()) {
592 // Fix-up referent, that isn't marked as an object field, for References.
593 Field* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
594 MemberOffset field_offset = field->GetOffset();
595 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
596 // Use SetFieldPtr to avoid card marking since we are writing to the image.
597 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
598 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700599}
600
Mathieu Chartier66f19252012-09-18 08:57:04 -0700601static AbstractMethod* GetTargetMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700602 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700603 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700605 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700606 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700608 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700609 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700610 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700611 CHECK(method != NULL)
612 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700613 CHECK(!method->IsRuntimeMethod())
614 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700616 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700618 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700619 return method;
620}
621
622void ImageWriter::PatchOatCodeAndMethods(const Compiler& compiler) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700623 Thread* self = Thread::Current();
Brian Carlstromf5822582012-03-19 22:34:31 -0700624 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700625 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Brian Carlstromf5822582012-03-19 22:34:31 -0700626
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700627 typedef std::vector<const Compiler::PatchInformation*> Patches;
628 const Patches& code_to_patch = compiler.GetCodeToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700629 for (size_t i = 0; i < code_to_patch.size(); i++) {
630 const Compiler::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700631 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700632 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
633 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
634 uint32_t code_offset = code - code_base;
635 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
636 }
637
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700638 const Patches& methods_to_patch = compiler.GetMethodsToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700639 for (size_t i = 0; i < methods_to_patch.size(); i++) {
640 const Compiler::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700641 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700642 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
643 }
Brian Carlstroma85b8372012-10-18 17:00:32 -0700644
645 // Update the image header with the new checksum after patching
646 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
647 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700648 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromf5822582012-03-19 22:34:31 -0700649}
650
651void ImageWriter::SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) {
652 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700653 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
654 patch->GetReferrerMethodIdx());
Brian Carlstroma85b8372012-10-18 17:00:32 -0700655 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
Brian Carlstromf5822582012-03-19 22:34:31 -0700656 // TODO: make this Thumb2 specific
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700657 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
Brian Carlstromf5822582012-03-19 22:34:31 -0700658 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
659#ifndef NDEBUG
660 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
661 uint32_t expected = reinterpret_cast<uint32_t>(&id);
662 uint32_t actual = *patch_location;
663 CHECK(actual == expected || actual == value) << std::hex
664 << "actual=" << actual
665 << "expected=" << expected
666 << "value=" << value;
667#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700668 *patch_location = value;
Brian Carlstroma85b8372012-10-18 17:00:32 -0700669 oat_header.UpdateChecksum(patch_location, sizeof(value));
Brian Carlstromf5822582012-03-19 22:34:31 -0700670}
671
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700672} // namespace art