blob: e21ff72d5736a82179a377bfbfcc2380ca170ab9 [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"
Brian Carlstromf5822582012-03-19 22:34:31 -070027#include "compiler.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "gc/card_table-inl.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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/array-inl.h"
36#include "mirror/class-inl.h"
37#include "mirror/class_loader.h"
38#include "mirror/dex_cache.h"
39#include "mirror/field-inl.h"
40#include "mirror/abstract_method-inl.h"
41#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043#include "oat.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080044#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080045#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070046#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070048#include "sirt_ref.h"
Elliott Hughesa168c832012-06-12 15:34:20 -070049#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070050#include "utils.h"
51
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052using namespace art::mirror;
53
Brian Carlstromdb4d5402011-08-09 12:18:28 -070054namespace art {
55
Brian Carlstroma004aa92012-02-08 18:05:09 -080056bool ImageWriter::Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080057 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080058 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070059 const std::string& oat_location,
60 const Compiler& compiler) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080061 CHECK(!image_filename.empty());
Brian Carlstromaded5f72011-10-07 17:15:04 -070062
Ian Rogers30fab402012-01-23 15:43:46 -080063 CHECK_NE(image_begin, 0U);
64 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070065
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080066 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070067 const Spaces& spaces = heap->GetSpaces();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070068
Brian Carlstromae826982011-11-09 01:33:42 -080069 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
70 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
71 for (size_t i = 0; i < all_dex_caches.size(); i++) {
72 DexCache* dex_cache = all_dex_caches[i];
Ian Rogers64b6d142012-10-29 16:34:15 -070073 dex_caches_.insert(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -080074 }
75
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076 UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false));
77 if (oat_file.get() == NULL) {
78 LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 return false;
80 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080081 oat_file_ = OatFile::Open(oat_file.get(), oat_location, NULL, true);
Elliott Hughesb25c3f62012-03-26 16:35:06 -070082 class_linker->RegisterOatFile(*oat_file_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070083
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 {
85 Thread::Current()->TransitionFromSuspendedToRunnable();
86 PruneNonImageClasses(); // Remove junk
87 ComputeLazyFieldsForImageClasses(); // Add useful information
88 ComputeEagerResolvedStrings();
89 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
90 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080091 heap->CollectGarbage(false); // Remove garbage
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070092 // Trim size of alloc spaces
93 // TODO: C++0x auto
94 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
95 if ((*cur)->IsAllocSpace()) {
96 (*cur)->AsAllocSpace()->Trim();
97 }
98 }
99
Brian Carlstromae826982011-11-09 01:33:42 -0800100 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700101 return false;
102 }
Brian Carlstromae826982011-11-09 01:33:42 -0800103#ifndef NDEBUG
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700104 {
105 ScopedObjectAccess soa(Thread::Current());
106 CheckNonImageClassesRemoved();
107 }
Brian Carlstromae826982011-11-09 01:33:42 -0800108#endif
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700109 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110 size_t oat_loaded_size = 0;
111 size_t oat_data_offset = 0;
112 compiler.GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
113 CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700114 CopyAndFixupObjects();
115 PatchOatCodeAndMethods(compiler);
116 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700117
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800118 UniquePtr<File> image_file(OS::OpenFile(image_filename.c_str(), true));
119 if (image_file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700121 return false;
122 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800123 if (fchmod(image_file->Fd(), 0644) != 0) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700124 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
125 return EXIT_FAILURE;
126 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800127 bool success = image_file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 if (!success) {
129 PLOG(ERROR) << "Failed to write image file " << image_filename;
130 return false;
131 }
132 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700133}
134
Brian Carlstromae826982011-11-09 01:33:42 -0800135bool ImageWriter::AllocMemory() {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700136 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700137 size_t size = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700138 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
139 if ((*it)->IsAllocSpace()) {
140 size += (*it)->Size();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700141 }
142 }
143
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700144 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700145 size_t length = RoundUp(size, kPageSize);
Ian Rogersa40307e2013-02-22 11:32:44 -0800146 image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700147 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700149 return false;
150 }
151 return true;
152}
153
Ian Rogersd418eda2012-01-30 12:14:28 -0800154void ImageWriter::ComputeLazyFieldsForImageClasses() {
155 Runtime* runtime = Runtime::Current();
156 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
Ian Rogersd418eda2012-01-30 12:14:28 -0800158}
159
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700160bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
161 c->ComputeName();
Ian Rogersd418eda2012-01-30 12:14:28 -0800162 return true;
163}
164
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800165void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
166 if (!obj->GetClass()->IsStringClass()) {
167 return;
168 }
169 String* string = obj->AsString();
170 std::string utf8_string(string->ToModifiedUtf8());
171 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800172 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
173 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
174 DexCache* dex_cache = *it;
Ian Rogers4445a7e2012-10-05 17:19:13 -0700175 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800176 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
177 if (string_id != NULL) {
178 // This string occurs in this dex file, assign the dex cache entry.
179 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
180 if (dex_cache->GetResolvedString(string_idx) == NULL) {
181 dex_cache->SetResolvedString(string_idx, string);
182 }
183 }
184 }
185}
186
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700187void ImageWriter::ComputeEagerResolvedStrings()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700189 // TODO: Check image spaces only?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700190 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700191 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700192 heap->FlushAllocStack();
193 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800194}
195
Brian Carlstromae826982011-11-09 01:33:42 -0800196bool ImageWriter::IsImageClass(const Class* klass) {
197 if (image_classes_ == NULL) {
198 return true;
199 }
200 while (klass->IsArrayClass()) {
201 klass = klass->GetComponentType();
202 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800203 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800204 return true;
205 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800207 return image_classes_->find(descriptor) != image_classes_->end();
208}
209
210
211struct NonImageClasses {
212 ImageWriter* image_writer;
213 std::set<std::string>* non_image_classes;
214};
215
216void ImageWriter::PruneNonImageClasses() {
217 if (image_classes_ == NULL) {
218 return;
219 }
220 Runtime* runtime = Runtime::Current();
221 ClassLinker* class_linker = runtime->GetClassLinker();
222
223 std::set<std::string> non_image_classes;
224 NonImageClasses context;
225 context.image_writer = this;
226 context.non_image_classes = &non_image_classes;
227 class_linker->VisitClasses(NonImageClassesVisitor, &context);
228
229 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
230 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800231 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800232 }
233
Mathieu Chartier66f19252012-09-18 08:57:04 -0700234 AbstractMethod* resolution_method = runtime->GetResolutionMethod();
Brian Carlstromae826982011-11-09 01:33:42 -0800235 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
236 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
237 DexCache* dex_cache = *it;
238 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
239 Class* klass = dex_cache->GetResolvedType(i);
240 if (klass != NULL && !IsImageClass(klass)) {
241 dex_cache->SetResolvedType(i, NULL);
242 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
243 }
244 }
245 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700246 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstromae826982011-11-09 01:33:42 -0800247 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
Ian Rogers19846512012-02-24 11:42:47 -0800248 dex_cache->SetResolvedMethod(i, resolution_method);
Brian Carlstromae826982011-11-09 01:33:42 -0800249 }
250 }
251 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
252 Field* field = dex_cache->GetResolvedField(i);
253 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
254 dex_cache->SetResolvedField(i, NULL);
255 }
256 }
257 }
258}
259
260bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
261 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
262 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800263 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800264 }
265 return true;
266}
267
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700268void ImageWriter::CheckNonImageClassesRemoved()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700269 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800270 if (image_classes_ == NULL) {
271 return;
272 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700273
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700274 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700275 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700276 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700277 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700278 heap->FlushAllocStack();
279 }
280
Ian Rogers50b35e22012-10-04 10:09:15 -0700281 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700282 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
Brian Carlstromae826982011-11-09 01:33:42 -0800283}
284
285void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
286 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
287 if (!obj->IsClass()) {
288 return;
289 }
290 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800291 if (!image_writer->IsImageClass(klass)) {
292 image_writer->DumpImageClasses();
293 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
294 << " " << PrettyDescriptor(klass);
295 }
296}
297
298void ImageWriter::DumpImageClasses() {
299 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
300 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
301 LOG(INFO) << " " << *it;
302 }
Brian Carlstromae826982011-11-09 01:33:42 -0800303}
304
Brian Carlstrom78128a62011-09-15 17:21:19 -0700305void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700306 DCHECK(obj != NULL);
307 DCHECK(arg != NULL);
308 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700309
310 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800311 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700312 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800313 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700314 DCHECK_EQ(obj, obj->AsString()->Intern());
315 return;
316 }
Ian Rogers1f539342012-10-03 21:09:42 -0700317 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700318 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800319 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700320 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700321 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700322 }
323 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800324 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700325 return;
326 }
327 // else (obj == interned), nothing to do but fall through to the normal case
328 }
329
330 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700331}
332
Brian Carlstrome24fa612011-09-29 00:53:55 -0700333ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700334 Runtime* runtime = Runtime::Current();
335 ClassLinker* class_linker = runtime->GetClassLinker();
336 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Ian Rogers50b35e22012-10-04 10:09:15 -0700337 Thread* self = Thread::Current();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700338
339 // build an Object[] of all the DexCaches used in the source_space_
Ian Rogers50b35e22012-10-04 10:09:15 -0700340 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800341 dex_caches_.size());
342 int i = 0;
343 typedef Set::const_iterator It; // TODO: C++0x auto
344 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
345 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700346 }
347
348 // build an Object[] of the roots needed to restore the runtime
Ian Rogers1f539342012-10-03 21:09:42 -0700349 SirtRef<ObjectArray<Object> >
Ian Rogers50b35e22012-10-04 10:09:15 -0700350 image_roots(self,
351 ObjectArray<Object>::Alloc(self, object_array_class,
352 ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800353 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
355 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700356 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
357 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
358 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
359 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers19846512012-02-24 11:42:47 -0800360 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700361 image_roots->Set(ImageHeader::kCalleeSaveMethod,
362 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
363 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
364 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
365 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
366 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 image_roots->Set(ImageHeader::kOatLocation,
Ian Rogers50b35e22012-10-04 10:09:15 -0700368 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700369 image_roots->Set(ImageHeader::kDexCaches,
370 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700371 image_roots->Set(ImageHeader::kClassRoots,
372 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700373 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
374 CHECK(image_roots->Get(i) != NULL);
375 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700376 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700377}
378
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800379void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
380 CHECK_NE(0U, oat_loaded_size);
Ian Rogers1f539342012-10-03 21:09:42 -0700381 Thread* self = Thread::Current();
382 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700383
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700384 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700385 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700386 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800387 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700388
Brian Carlstrom16192862011-09-12 17:50:06 -0700389 // leave space for the header, but do not write it yet, we need to
390 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800391 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700392
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700393 {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700394 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700395 heap->FlushAllocStack();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700396 // TODO: Image spaces only?
397 // TODO: Add InOrderWalk to heap bitmap.
Ian Rogers1f539342012-10-03 21:09:42 -0700398 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700399 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
400 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700401 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700402 DCHECK_LT(image_end_, image_->Size());
403 }
Ian Rogers1f539342012-10-03 21:09:42 -0700404 self->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700405 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700406
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800407 const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
408 const byte* oat_file_end = oat_file_begin + oat_loaded_size;
409 oat_data_begin_ = oat_file_begin + oat_data_offset;
410 const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700411
Brian Carlstrom16192862011-09-12 17:50:06 -0700412 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800413 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700414 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700415 oat_file_->GetOatHeader().GetChecksum(),
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800416 reinterpret_cast<uint32_t>(oat_file_begin),
417 reinterpret_cast<uint32_t>(oat_data_begin_),
418 reinterpret_cast<uint32_t>(oat_data_end),
419 reinterpret_cast<uint32_t>(oat_file_end));
Ian Rogers30fab402012-01-23 15:43:46 -0800420 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800421
422 // Note that image_end_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700423}
424
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700425void ImageWriter::CopyAndFixupObjects()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700427 Thread* self = Thread::Current();
428 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800429 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800431 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700432 // TODO: Image spaces only?
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700433 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700434 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700435 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700436 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700437}
438
Brian Carlstrom78128a62011-09-15 17:21:19 -0700439void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700440 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700441 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700442 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700443 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700444
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700445 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700446 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800447 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700448 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700449 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800450 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700451 memcpy(dst, src, n);
452 Object* copy = reinterpret_cast<Object*>(dst);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 copy->SetField32(Object::MonitorOffset(), 0, false); // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700454 image_writer->FixupObject(obj, copy);
455}
456
Brian Carlstrom4873d462011-08-21 15:23:39 -0700457void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700458 DCHECK(orig != NULL);
459 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700460 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700461 // TODO: special case init of pointers to malloc data (or removal of these pointers)
462 if (orig->IsClass()) {
463 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
464 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700465 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700466 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700467 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700468 } else {
469 FixupInstanceFields(orig, copy);
470 }
471}
472
Brian Carlstrom4873d462011-08-21 15:23:39 -0700473void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700474 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700475 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700476}
477
Mathieu Chartier66f19252012-09-18 08:57:04 -0700478void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700479 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800482 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700483
484 // Every type of method can have an invoke stub
485 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800486 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487 copy->SetInvokeStub(reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(invoke_stub)));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700488
489 if (orig->IsAbstract()) {
490 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
491 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
492 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493 copy->SetCode(copy_ame_stub_array_->GetData());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700494 return;
495 }
496
Ian Rogers19846512012-02-24 11:42:47 -0800497 if (orig == Runtime::Current()->GetResolutionMethod()) {
498 // The resolution stub's code points at the unknown resolution trampoline
499 ByteArray* orig_res_stub_array_ =
500 Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod);
501 CHECK(orig->GetCode() == orig_res_stub_array_->GetData());
502 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800503 copy->SetCode(copy_res_stub_array_->GetData());
Ian Rogers19846512012-02-24 11:42:47 -0800504 return;
505 }
506
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 // Non-abstract methods typically have code
508 uint32_t code_offset = orig->GetOatCodeOffset();
Ian Rogers19846512012-02-24 11:42:47 -0800509 const byte* code = NULL;
510 if (orig->IsStatic()) {
511 // Static methods may point at the resolution trampoline stub
512 ByteArray* orig_res_stub_array_ =
513 Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod);
514 if (reinterpret_cast<int8_t*>(code_offset) == orig_res_stub_array_->GetData()) {
515 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
516 code = reinterpret_cast<const byte*>(copy_res_stub_array_->GetData());
517 }
518 }
519 if (code == NULL) {
520 code = GetOatAddress(code_offset);
521 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522 copy->SetCode(code);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523
Brian Carlstrom16192862011-09-12 17:50:06 -0700524 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525 // The native method's pointer is directed to a stub to lookup via dlsym.
526 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800527 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700528 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800529 copy->SetNativeMethod(copy_jni_stub_array_->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700530 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700531 // normal (non-abstract non-native) methods have mapping tables to relocate
532 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800533 const byte* mapping_table = GetOatAddress(mapping_table_off);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534 copy->SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535
536 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800537 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800538 copy->SetVmapTable(reinterpret_cast<const uint16_t*>(vmap_table));
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800539
Ian Rogers0c7abda2012-09-19 13:33:42 -0700540 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
541 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800542 copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map));
Brian Carlstrom16192862011-09-12 17:50:06 -0700543 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700544}
545
Brian Carlstrom4873d462011-08-21 15:23:39 -0700546void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700547 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700548 const Object* element = orig->Get(i);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700549 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700550 }
551}
552
Brian Carlstrom4873d462011-08-21 15:23:39 -0700553void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
554 DCHECK(orig != NULL);
555 DCHECK(copy != NULL);
556 Class* klass = orig->GetClass();
557 DCHECK(klass != NULL);
558 FixupFields(orig,
559 copy,
560 klass->GetReferenceInstanceOffsets(),
561 false);
562}
563
564void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
565 DCHECK(orig != NULL);
566 DCHECK(copy != NULL);
567 FixupFields(orig,
568 copy,
569 orig->GetReferenceStaticOffsets(),
570 true);
571}
572
573void ImageWriter::FixupFields(const Object* orig,
574 Object* copy,
575 uint32_t ref_offsets,
576 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700577 if (ref_offsets != CLASS_WALK_SUPER) {
578 // Found a reference offset bitmap. Fixup the specified offsets.
579 while (ref_offsets != 0) {
580 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700581 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
582 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700583 // Use SetFieldPtr to avoid card marking since we are writing to the image.
584 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700585 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
586 }
587 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700588 // There is no reference offset bitmap. In the non-static case,
589 // walk up the class inheritance hierarchy and find reference
590 // offsets the hard way. In the static case, just consider this
591 // class.
592 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700593 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700594 klass = is_static ? NULL : klass->GetSuperClass()) {
595 size_t num_reference_fields = (is_static
596 ? klass->NumReferenceStaticFields()
597 : klass->NumReferenceInstanceFields());
598 for (size_t i = 0; i < num_reference_fields; ++i) {
599 Field* field = (is_static
600 ? klass->GetStaticField(i)
601 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700602 MemberOffset field_offset = field->GetOffset();
603 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700604 // Use SetFieldPtr to avoid card marking since we are writing to the image.
605 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700606 }
607 }
608 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700609 if (!is_static && orig->IsReferenceInstance()) {
610 // Fix-up referent, that isn't marked as an object field, for References.
611 Field* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
612 MemberOffset field_offset = field->GetOffset();
613 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
614 // Use SetFieldPtr to avoid card marking since we are writing to the image.
615 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
616 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700617}
618
Mathieu Chartier66f19252012-09-18 08:57:04 -0700619static AbstractMethod* GetTargetMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700621 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700623 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700624 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700626 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700627 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700628 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700629 CHECK(method != NULL)
630 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700631 CHECK(!method->IsRuntimeMethod())
632 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700633 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700634 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700636 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700637 return method;
638}
639
640void ImageWriter::PatchOatCodeAndMethods(const Compiler& compiler) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700641 Thread* self = Thread::Current();
Brian Carlstromf5822582012-03-19 22:34:31 -0700642 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700643 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Brian Carlstromf5822582012-03-19 22:34:31 -0700644
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700645 typedef std::vector<const Compiler::PatchInformation*> Patches;
646 const Patches& code_to_patch = compiler.GetCodeToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700647 for (size_t i = 0; i < code_to_patch.size(); i++) {
648 const Compiler::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700649 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700650 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
651 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
652 uint32_t code_offset = code - code_base;
653 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
654 }
655
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700656 const Patches& methods_to_patch = compiler.GetMethodsToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700657 for (size_t i = 0; i < methods_to_patch.size(); i++) {
658 const Compiler::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700659 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700660 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
661 }
Brian Carlstroma85b8372012-10-18 17:00:32 -0700662
663 // Update the image header with the new checksum after patching
664 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
665 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700666 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromf5822582012-03-19 22:34:31 -0700667}
668
669void ImageWriter::SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) {
670 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700671 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
672 patch->GetReferrerMethodIdx());
Brian Carlstroma85b8372012-10-18 17:00:32 -0700673 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
Brian Carlstromf5822582012-03-19 22:34:31 -0700674 // TODO: make this Thumb2 specific
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700675 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
Brian Carlstromf5822582012-03-19 22:34:31 -0700676 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
677#ifndef NDEBUG
678 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
679 uint32_t expected = reinterpret_cast<uint32_t>(&id);
680 uint32_t actual = *patch_location;
681 CHECK(actual == expected || actual == value) << std::hex
682 << "actual=" << actual
683 << "expected=" << expected
684 << "value=" << value;
685#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700686 *patch_location = value;
Brian Carlstroma85b8372012-10-18 17:00:32 -0700687 oat_header.UpdateChecksum(patch_location, sizeof(value));
Brian Carlstromf5822582012-03-19 22:34:31 -0700688}
689
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700690} // namespace art