blob: 7e82d6d1d0e3d75332d5b847520970a8f6e327cd [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 Carlstromae826982011-11-09 01:33:42 -080026#include "compiled_method.h"
Ian Rogers1212a022013-03-04 10:48:41 -080027#include "compiler/driver/compiler_driver.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "gc/card_table-inl.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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/array-inl.h"
37#include "mirror/class-inl.h"
38#include "mirror/class_loader.h"
39#include "mirror/dex_cache.h"
40#include "mirror/field-inl.h"
41#include "mirror/abstract_method-inl.h"
42#include "mirror/object-inl.h"
43#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044#include "oat.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080045#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080046#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070047#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070049#include "sirt_ref.h"
Elliott Hughesa168c832012-06-12 15:34:20 -070050#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051#include "utils.h"
52
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053using namespace art::mirror;
54
Brian Carlstromdb4d5402011-08-09 12:18:28 -070055namespace art {
56
Brian Carlstroma004aa92012-02-08 18:05:09 -080057bool ImageWriter::Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080058 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080059 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070060 const std::string& oat_location,
Ian Rogers1212a022013-03-04 10:48:41 -080061 const CompilerDriver& compiler_driver) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080062 CHECK(!image_filename.empty());
Brian Carlstromaded5f72011-10-07 17:15:04 -070063
Ian Rogers30fab402012-01-23 15:43:46 -080064 CHECK_NE(image_begin, 0U);
65 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070066
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080067 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070068 const Spaces& spaces = heap->GetSpaces();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070069
Brian Carlstromae826982011-11-09 01:33:42 -080070 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
71 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
72 for (size_t i = 0; i < all_dex_caches.size(); i++) {
73 DexCache* dex_cache = all_dex_caches[i];
Ian Rogers64b6d142012-10-29 16:34:15 -070074 dex_caches_.insert(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -080075 }
76
Brian Carlstrom700c8d32012-11-05 10:42:02 -080077 UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false));
78 if (oat_file.get() == NULL) {
79 LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 return false;
81 }
Brian Carlstrom265091e2013-01-30 14:08:26 -080082 oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location);
Elliott Hughesb25c3f62012-03-26 16:35:06 -070083 class_linker->RegisterOatFile(*oat_file_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070084
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 {
86 Thread::Current()->TransitionFromSuspendedToRunnable();
87 PruneNonImageClasses(); // Remove junk
88 ComputeLazyFieldsForImageClasses(); // Add useful information
89 ComputeEagerResolvedStrings();
90 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
91 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080092 heap->CollectGarbage(false); // Remove garbage
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070093 // Trim size of alloc spaces
94 // TODO: C++0x auto
95 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
96 if ((*cur)->IsAllocSpace()) {
97 (*cur)->AsAllocSpace()->Trim();
98 }
99 }
100
Brian Carlstromae826982011-11-09 01:33:42 -0800101 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700102 return false;
103 }
Brian Carlstromae826982011-11-09 01:33:42 -0800104#ifndef NDEBUG
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700105 {
106 ScopedObjectAccess soa(Thread::Current());
107 CheckNonImageClassesRemoved();
108 }
Brian Carlstromae826982011-11-09 01:33:42 -0800109#endif
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700110 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111 size_t oat_loaded_size = 0;
112 size_t oat_data_offset = 0;
Ian Rogers1212a022013-03-04 10:48:41 -0800113 compiler_driver.GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800114 CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700115 CopyAndFixupObjects();
Ian Rogers1212a022013-03-04 10:48:41 -0800116 PatchOatCodeAndMethods(compiler_driver);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700117 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700118
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119 UniquePtr<File> image_file(OS::OpenFile(image_filename.c_str(), true));
120 if (image_file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700122 return false;
123 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800124 if (fchmod(image_file->Fd(), 0644) != 0) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700125 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
126 return EXIT_FAILURE;
127 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128 bool success = image_file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129 if (!success) {
130 PLOG(ERROR) << "Failed to write image file " << image_filename;
131 return false;
132 }
133 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700134}
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);
Ian Rogersa40307e2013-02-22 11:32:44 -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 Carlstromc74255f2011-09-11 22:47:39 -0700310
311 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800312 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700313 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800314 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700315 DCHECK_EQ(obj, obj->AsString()->Intern());
316 return;
317 }
Ian Rogers1f539342012-10-03 21:09:42 -0700318 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700319 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800320 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700321 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700322 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700323 }
324 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800325 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700326 return;
327 }
328 // else (obj == interned), nothing to do but fall through to the normal case
329 }
330
331 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700332}
333
Brian Carlstrome24fa612011-09-29 00:53:55 -0700334ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700335 Runtime* runtime = Runtime::Current();
336 ClassLinker* class_linker = runtime->GetClassLinker();
337 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Ian Rogers50b35e22012-10-04 10:09:15 -0700338 Thread* self = Thread::Current();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700339
340 // build an Object[] of all the DexCaches used in the source_space_
Ian Rogers50b35e22012-10-04 10:09:15 -0700341 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800342 dex_caches_.size());
343 int i = 0;
344 typedef Set::const_iterator It; // TODO: C++0x auto
345 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
346 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700347 }
348
349 // build an Object[] of the roots needed to restore the runtime
Ian Rogers1f539342012-10-03 21:09:42 -0700350 SirtRef<ObjectArray<Object> >
Ian Rogers50b35e22012-10-04 10:09:15 -0700351 image_roots(self,
352 ObjectArray<Object>::Alloc(self, object_array_class,
353 ImageHeader::kImageRootsMax));
Ian Rogers19846512012-02-24 11:42:47 -0800354 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700355 image_roots->Set(ImageHeader::kCalleeSaveMethod,
356 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
357 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
358 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
359 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
360 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361 image_roots->Set(ImageHeader::kOatLocation,
Ian Rogers50b35e22012-10-04 10:09:15 -0700362 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700363 image_roots->Set(ImageHeader::kDexCaches,
364 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700365 image_roots->Set(ImageHeader::kClassRoots,
366 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
368 CHECK(image_roots->Get(i) != NULL);
369 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700370 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700371}
372
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800373void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
374 CHECK_NE(0U, oat_loaded_size);
Ian Rogers1f539342012-10-03 21:09:42 -0700375 Thread* self = Thread::Current();
376 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700377
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700378 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700379 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700380 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800381 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700382
Brian Carlstrom16192862011-09-12 17:50:06 -0700383 // leave space for the header, but do not write it yet, we need to
384 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800385 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700386
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700387 {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700388 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700389 heap->FlushAllocStack();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700390 // TODO: Image spaces only?
391 // TODO: Add InOrderWalk to heap bitmap.
Ian Rogers1f539342012-10-03 21:09:42 -0700392 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700393 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
394 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700395 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700396 DCHECK_LT(image_end_, image_->Size());
397 }
Ian Rogers1f539342012-10-03 21:09:42 -0700398 self->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700399 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700400
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800401 const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
402 const byte* oat_file_end = oat_file_begin + oat_loaded_size;
403 oat_data_begin_ = oat_file_begin + oat_data_offset;
404 const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700405
Brian Carlstrom16192862011-09-12 17:50:06 -0700406 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800407 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700408 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409 oat_file_->GetOatHeader().GetChecksum(),
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800410 reinterpret_cast<uint32_t>(oat_file_begin),
411 reinterpret_cast<uint32_t>(oat_data_begin_),
412 reinterpret_cast<uint32_t>(oat_data_end),
413 reinterpret_cast<uint32_t>(oat_file_end));
Ian Rogers30fab402012-01-23 15:43:46 -0800414 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800415
416 // Note that image_end_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700417}
418
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700419void ImageWriter::CopyAndFixupObjects()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700420 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700421 Thread* self = Thread::Current();
422 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800423 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800425 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700426 // TODO: Image spaces only?
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700427 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700428 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700429 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700430 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700431}
432
Brian Carlstrom78128a62011-09-15 17:21:19 -0700433void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700434 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700435 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700436 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700437 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700438
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700439 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700440 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800441 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700442 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700443 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800444 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700445 memcpy(dst, src, n);
446 Object* copy = reinterpret_cast<Object*>(dst);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447 copy->SetField32(Object::MonitorOffset(), 0, false); // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700448 image_writer->FixupObject(obj, copy);
449}
450
Brian Carlstrom4873d462011-08-21 15:23:39 -0700451void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700452 DCHECK(orig != NULL);
453 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700454 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700455 // TODO: special case init of pointers to malloc data (or removal of these pointers)
456 if (orig->IsClass()) {
457 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
458 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700459 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700460 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700461 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700462 } else {
463 FixupInstanceFields(orig, copy);
464 }
465}
466
Brian Carlstrom4873d462011-08-21 15:23:39 -0700467void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700468 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700469 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700470}
471
Mathieu Chartier66f19252012-09-18 08:57:04 -0700472void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700473 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700474
Jeff Hao74180ca2013-03-27 15:29:11 -0700475 // OatWriter replaces the code_ with an offset value.
Ian Rogers30fab402012-01-23 15:43:46 -0800476 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 if (orig->IsAbstract()) {
Jeff Hao79fe5392013-04-24 18:41:58 -0700478 // Code for abstract methods is set to the abstract method error stub when we load the image.
Jeff Haoaa4a7932013-05-13 11:28:27 -0700479 copy->SetEntryPointFromCompiledCode(NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480 return;
481 }
482
Ian Rogers19846512012-02-24 11:42:47 -0800483 if (orig == Runtime::Current()->GetResolutionMethod()) {
Jeff Hao58df3272013-04-22 15:28:53 -0700484 // The resolution method's code is set to the resolution trampoline when we load the image.
Jeff Haoaa4a7932013-05-13 11:28:27 -0700485 copy->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800486 return;
487 }
488
Jeff Hao58df3272013-04-22 15:28:53 -0700489 // Non-abstract methods have code
Jeff Haoaa4a7932013-05-13 11:28:27 -0700490 copy->SetEntryPointFromCompiledCode(GetOatAddress(orig->GetOatCodeOffset()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700491
Brian Carlstrom16192862011-09-12 17:50:06 -0700492 if (orig->IsNative()) {
Jeff Hao79fe5392013-04-24 18:41:58 -0700493 // The native method's pointer is set to a stub to lookup via dlsym when we load the image.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700494 // Note this is not the code_ pointer, that is handled above.
Jeff Hao79fe5392013-04-24 18:41:58 -0700495 copy->SetNativeMethod(NULL);
Brian Carlstrom16192862011-09-12 17:50:06 -0700496 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 // normal (non-abstract non-native) methods have mapping tables to relocate
498 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800499 const byte* mapping_table = GetOatAddress(mapping_table_off);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800500 copy->SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700501
502 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800503 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800504 copy->SetVmapTable(reinterpret_cast<const uint16_t*>(vmap_table));
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800505
Ian Rogers0c7abda2012-09-19 13:33:42 -0700506 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
507 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508 copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map));
Brian Carlstrom16192862011-09-12 17:50:06 -0700509 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700510}
511
Brian Carlstrom4873d462011-08-21 15:23:39 -0700512void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700513 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700514 const Object* element = orig->Get(i);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700515 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700516 }
517}
518
Brian Carlstrom4873d462011-08-21 15:23:39 -0700519void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
520 DCHECK(orig != NULL);
521 DCHECK(copy != NULL);
522 Class* klass = orig->GetClass();
523 DCHECK(klass != NULL);
524 FixupFields(orig,
525 copy,
526 klass->GetReferenceInstanceOffsets(),
527 false);
528}
529
530void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
531 DCHECK(orig != NULL);
532 DCHECK(copy != NULL);
533 FixupFields(orig,
534 copy,
535 orig->GetReferenceStaticOffsets(),
536 true);
537}
538
539void ImageWriter::FixupFields(const Object* orig,
540 Object* copy,
541 uint32_t ref_offsets,
542 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700543 if (ref_offsets != CLASS_WALK_SUPER) {
544 // Found a reference offset bitmap. Fixup the specified offsets.
545 while (ref_offsets != 0) {
546 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700547 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
548 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700549 // Use SetFieldPtr to avoid card marking since we are writing to the image.
550 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700551 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
552 }
553 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700554 // There is no reference offset bitmap. In the non-static case,
555 // walk up the class inheritance hierarchy and find reference
556 // offsets the hard way. In the static case, just consider this
557 // class.
558 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700559 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700560 klass = is_static ? NULL : klass->GetSuperClass()) {
561 size_t num_reference_fields = (is_static
562 ? klass->NumReferenceStaticFields()
563 : klass->NumReferenceInstanceFields());
564 for (size_t i = 0; i < num_reference_fields; ++i) {
565 Field* field = (is_static
566 ? klass->GetStaticField(i)
567 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700568 MemberOffset field_offset = field->GetOffset();
569 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700570 // Use SetFieldPtr to avoid card marking since we are writing to the image.
571 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700572 }
573 }
574 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700575 if (!is_static && orig->IsReferenceInstance()) {
576 // Fix-up referent, that isn't marked as an object field, for References.
577 Field* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
578 MemberOffset field_offset = field->GetOffset();
579 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
580 // Use SetFieldPtr to avoid card marking since we are writing to the image.
581 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
582 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700583}
584
Ian Rogers1212a022013-03-04 10:48:41 -0800585static AbstractMethod* GetTargetMethod(const CompilerDriver::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700587 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700589 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700590 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700591 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700592 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700593 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700594 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700595 CHECK(method != NULL)
596 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700597 CHECK(!method->IsRuntimeMethod())
598 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700600 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700601 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700602 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700603 return method;
604}
605
Ian Rogers1212a022013-03-04 10:48:41 -0800606void ImageWriter::PatchOatCodeAndMethods(const CompilerDriver& compiler) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700607 Thread* self = Thread::Current();
Brian Carlstromf5822582012-03-19 22:34:31 -0700608 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700609 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Brian Carlstromf5822582012-03-19 22:34:31 -0700610
Ian Rogers1212a022013-03-04 10:48:41 -0800611 typedef std::vector<const CompilerDriver::PatchInformation*> Patches;
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700612 const Patches& code_to_patch = compiler.GetCodeToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700613 for (size_t i = 0; i < code_to_patch.size(); i++) {
Ian Rogers1212a022013-03-04 10:48:41 -0800614 const CompilerDriver::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700615 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700616 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
617 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
618 uint32_t code_offset = code - code_base;
619 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
620 }
621
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700622 const Patches& methods_to_patch = compiler.GetMethodsToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700623 for (size_t i = 0; i < methods_to_patch.size(); i++) {
Ian Rogers1212a022013-03-04 10:48:41 -0800624 const CompilerDriver::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700625 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700626 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
627 }
Brian Carlstroma85b8372012-10-18 17:00:32 -0700628
629 // Update the image header with the new checksum after patching
630 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
631 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700632 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromf5822582012-03-19 22:34:31 -0700633}
634
Ian Rogers1212a022013-03-04 10:48:41 -0800635void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700636 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700637 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
638 patch->GetReferrerMethodIdx());
Brian Carlstroma85b8372012-10-18 17:00:32 -0700639 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
Brian Carlstromf5822582012-03-19 22:34:31 -0700640 // TODO: make this Thumb2 specific
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700641 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
Brian Carlstromf5822582012-03-19 22:34:31 -0700642 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
643#ifndef NDEBUG
644 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
645 uint32_t expected = reinterpret_cast<uint32_t>(&id);
646 uint32_t actual = *patch_location;
647 CHECK(actual == expected || actual == value) << std::hex
648 << "actual=" << actual
649 << "expected=" << expected
650 << "value=" << value;
651#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700652 *patch_location = value;
Brian Carlstroma85b8372012-10-18 17:00:32 -0700653 oat_header.UpdateChecksum(patch_location, sizeof(value));
Brian Carlstromf5822582012-03-19 22:34:31 -0700654}
655
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700656} // namespace art