blob: fc88cbbfc3d2042840702fd033f7073bfaeea335 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstromdb4d5402011-08-09 12:18:28 -070016
17#include "image_writer.h"
18
Brian Carlstrom6cd40e52012-05-03 14:15:11 -070019#include <sys/stat.h>
Elliott Hughes90a33692011-08-30 13:27:07 -070020
Brian Carlstromdb4d5402011-08-09 12:18:28 -070021#include <vector>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/unix_file/fd_file.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Brian Carlstromae826982011-11-09 01:33:42 -080027#include "compiled_method.h"
Brian Carlstromf5822582012-03-19 22:34:31 -070028#include "compiler.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070029#include "dex_cache.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070030#include "gc/large_object_space.h"
31#include "gc/space.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070032#include "globals.h"
33#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070034#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070035#include "intern_table.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036#include "oat.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080037#include "oat_file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070040#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070042#include "sirt_ref.h"
Elliott Hughesa168c832012-06-12 15:34:20 -070043#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044#include "utils.h"
45
46namespace art {
47
Brian Carlstroma004aa92012-02-08 18:05:09 -080048bool ImageWriter::Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080049 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080050 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070051 const std::string& oat_location,
52 const Compiler& compiler) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080053 CHECK(!image_filename.empty());
Brian Carlstromaded5f72011-10-07 17:15:04 -070054
Ian Rogers30fab402012-01-23 15:43:46 -080055 CHECK_NE(image_begin, 0U);
56 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070057
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080058 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070059 const Spaces& spaces = heap->GetSpaces();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070060
Brian Carlstromae826982011-11-09 01:33:42 -080061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
63 for (size_t i = 0; i < all_dex_caches.size(); i++) {
64 DexCache* dex_cache = all_dex_caches[i];
Ian Rogers64b6d142012-10-29 16:34:15 -070065 dex_caches_.insert(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -080066 }
67
Brian Carlstrom700c8d32012-11-05 10:42:02 -080068 UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false));
69 if (oat_file.get() == NULL) {
70 LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 return false;
72 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080073 oat_file_ = OatFile::Open(oat_file.get(), oat_location, NULL, true);
Elliott Hughesb25c3f62012-03-26 16:35:06 -070074 class_linker->RegisterOatFile(*oat_file_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070075
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 {
77 Thread::Current()->TransitionFromSuspendedToRunnable();
78 PruneNonImageClasses(); // Remove junk
79 ComputeLazyFieldsForImageClasses(); // Add useful information
80 ComputeEagerResolvedStrings();
81 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
82 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080083 heap->CollectGarbage(false); // Remove garbage
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070084 // Trim size of alloc spaces
85 // TODO: C++0x auto
86 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
87 if ((*cur)->IsAllocSpace()) {
88 (*cur)->AsAllocSpace()->Trim();
89 }
90 }
91
Brian Carlstromae826982011-11-09 01:33:42 -080092 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070093 return false;
94 }
Brian Carlstromae826982011-11-09 01:33:42 -080095#ifndef NDEBUG
Mathieu Chartier357e9be2012-08-01 11:00:14 -070096 {
97 ScopedObjectAccess soa(Thread::Current());
98 CheckNonImageClassesRemoved();
99 }
Brian Carlstromae826982011-11-09 01:33:42 -0800100#endif
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700101 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102 size_t oat_loaded_size = 0;
103 size_t oat_data_offset = 0;
104 compiler.GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
105 CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700106 CopyAndFixupObjects();
107 PatchOatCodeAndMethods(compiler);
108 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700109
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110 UniquePtr<File> image_file(OS::OpenFile(image_filename.c_str(), true));
111 if (image_file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700113 return false;
114 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800115 if (fchmod(image_file->Fd(), 0644) != 0) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700116 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
117 return EXIT_FAILURE;
118 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119 bool success = image_file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120 if (!success) {
121 PLOG(ERROR) << "Failed to write image file " << image_filename;
122 return false;
123 }
124 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700125}
126
Brian Carlstromae826982011-11-09 01:33:42 -0800127bool ImageWriter::AllocMemory() {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700128 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700129 size_t size = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700130 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
131 if ((*it)->IsAllocSpace()) {
132 size += (*it)->Size();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700133 }
134 }
135
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700136 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700137 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800138 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700139 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700141 return false;
142 }
143 return true;
144}
145
Ian Rogersd418eda2012-01-30 12:14:28 -0800146void ImageWriter::ComputeLazyFieldsForImageClasses() {
147 Runtime* runtime = Runtime::Current();
148 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
Ian Rogersd418eda2012-01-30 12:14:28 -0800150}
151
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700152bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
153 c->ComputeName();
Ian Rogersd418eda2012-01-30 12:14:28 -0800154 return true;
155}
156
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800157void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
158 if (!obj->GetClass()->IsStringClass()) {
159 return;
160 }
161 String* string = obj->AsString();
162 std::string utf8_string(string->ToModifiedUtf8());
163 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800164 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
165 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
166 DexCache* dex_cache = *it;
Ian Rogers4445a7e2012-10-05 17:19:13 -0700167 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800168 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
169 if (string_id != NULL) {
170 // This string occurs in this dex file, assign the dex cache entry.
171 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
172 if (dex_cache->GetResolvedString(string_idx) == NULL) {
173 dex_cache->SetResolvedString(string_idx, string);
174 }
175 }
176 }
177}
178
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700179void ImageWriter::ComputeEagerResolvedStrings()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 // TODO: Check image spaces only?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700182 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700183 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700184 heap->FlushAllocStack();
185 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800186}
187
Brian Carlstromae826982011-11-09 01:33:42 -0800188bool ImageWriter::IsImageClass(const Class* klass) {
189 if (image_classes_ == NULL) {
190 return true;
191 }
192 while (klass->IsArrayClass()) {
193 klass = klass->GetComponentType();
194 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800195 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800196 return true;
197 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800198 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800199 return image_classes_->find(descriptor) != image_classes_->end();
200}
201
202
203struct NonImageClasses {
204 ImageWriter* image_writer;
205 std::set<std::string>* non_image_classes;
206};
207
208void ImageWriter::PruneNonImageClasses() {
209 if (image_classes_ == NULL) {
210 return;
211 }
212 Runtime* runtime = Runtime::Current();
213 ClassLinker* class_linker = runtime->GetClassLinker();
214
215 std::set<std::string> non_image_classes;
216 NonImageClasses context;
217 context.image_writer = this;
218 context.non_image_classes = &non_image_classes;
219 class_linker->VisitClasses(NonImageClassesVisitor, &context);
220
221 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
222 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800223 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800224 }
225
Mathieu Chartier66f19252012-09-18 08:57:04 -0700226 AbstractMethod* resolution_method = runtime->GetResolutionMethod();
Brian Carlstromae826982011-11-09 01:33:42 -0800227 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
228 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
229 DexCache* dex_cache = *it;
230 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
231 Class* klass = dex_cache->GetResolvedType(i);
232 if (klass != NULL && !IsImageClass(klass)) {
233 dex_cache->SetResolvedType(i, NULL);
234 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
235 }
236 }
237 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700238 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstromae826982011-11-09 01:33:42 -0800239 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
Ian Rogers19846512012-02-24 11:42:47 -0800240 dex_cache->SetResolvedMethod(i, resolution_method);
Brian Carlstromae826982011-11-09 01:33:42 -0800241 }
242 }
243 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
244 Field* field = dex_cache->GetResolvedField(i);
245 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
246 dex_cache->SetResolvedField(i, NULL);
247 }
248 }
249 }
250}
251
252bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
253 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
254 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800255 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800256 }
257 return true;
258}
259
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700260void ImageWriter::CheckNonImageClassesRemoved()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800262 if (image_classes_ == NULL) {
263 return;
264 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700265
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700266 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700267 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700268 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700269 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700270 heap->FlushAllocStack();
271 }
272
Ian Rogers50b35e22012-10-04 10:09:15 -0700273 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700274 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
Brian Carlstromae826982011-11-09 01:33:42 -0800275}
276
277void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
278 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
279 if (!obj->IsClass()) {
280 return;
281 }
282 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800283 if (!image_writer->IsImageClass(klass)) {
284 image_writer->DumpImageClasses();
285 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
286 << " " << PrettyDescriptor(klass);
287 }
288}
289
290void ImageWriter::DumpImageClasses() {
291 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
292 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
293 LOG(INFO) << " " << *it;
294 }
Brian Carlstromae826982011-11-09 01:33:42 -0800295}
296
Brian Carlstrom78128a62011-09-15 17:21:19 -0700297void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700298 DCHECK(obj != NULL);
299 DCHECK(arg != NULL);
300 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700301
302 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800303 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700304 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800305 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700306 DCHECK_EQ(obj, obj->AsString()->Intern());
307 return;
308 }
Ian Rogers1f539342012-10-03 21:09:42 -0700309 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700310 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800311 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700312 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700313 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700314 }
315 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800316 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700317 return;
318 }
319 // else (obj == interned), nothing to do but fall through to the normal case
320 }
321
322 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700323}
324
Brian Carlstrome24fa612011-09-29 00:53:55 -0700325ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700326 Runtime* runtime = Runtime::Current();
327 ClassLinker* class_linker = runtime->GetClassLinker();
328 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Ian Rogers50b35e22012-10-04 10:09:15 -0700329 Thread* self = Thread::Current();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700330
331 // build an Object[] of all the DexCaches used in the source_space_
Ian Rogers50b35e22012-10-04 10:09:15 -0700332 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800333 dex_caches_.size());
334 int i = 0;
335 typedef Set::const_iterator It; // TODO: C++0x auto
336 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
337 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700338 }
339
340 // build an Object[] of the roots needed to restore the runtime
Ian Rogers1f539342012-10-03 21:09:42 -0700341 SirtRef<ObjectArray<Object> >
Ian Rogers50b35e22012-10-04 10:09:15 -0700342 image_roots(self,
343 ObjectArray<Object>::Alloc(self, object_array_class,
344 ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800345 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700346 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
347 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700348 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
349 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
350 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
351 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers19846512012-02-24 11:42:47 -0800352 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700353 image_roots->Set(ImageHeader::kCalleeSaveMethod,
354 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
355 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
356 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
357 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
358 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 image_roots->Set(ImageHeader::kOatLocation,
Ian Rogers50b35e22012-10-04 10:09:15 -0700360 String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700361 image_roots->Set(ImageHeader::kDexCaches,
362 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700363 image_roots->Set(ImageHeader::kClassRoots,
364 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700365 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
366 CHECK(image_roots->Get(i) != NULL);
367 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700368 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700369}
370
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800371void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
372 CHECK_NE(0U, oat_loaded_size);
Ian Rogers1f539342012-10-03 21:09:42 -0700373 Thread* self = Thread::Current();
374 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700375
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700376 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700377 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700378 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800379 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700380
Brian Carlstrom16192862011-09-12 17:50:06 -0700381 // leave space for the header, but do not write it yet, we need to
382 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800383 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700384
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700385 {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700386 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700387 heap->FlushAllocStack();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700388 // TODO: Image spaces only?
389 // TODO: Add InOrderWalk to heap bitmap.
Ian Rogers1f539342012-10-03 21:09:42 -0700390 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700391 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
392 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700393 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700394 DCHECK_LT(image_end_, image_->Size());
395 }
Ian Rogers1f539342012-10-03 21:09:42 -0700396 self->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700397 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700398
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800399 const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
400 const byte* oat_file_end = oat_file_begin + oat_loaded_size;
401 oat_data_begin_ = oat_file_begin + oat_data_offset;
402 const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700403
Brian Carlstrom16192862011-09-12 17:50:06 -0700404 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800405 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700406 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700407 oat_file_->GetOatHeader().GetChecksum(),
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800408 reinterpret_cast<uint32_t>(oat_file_begin),
409 reinterpret_cast<uint32_t>(oat_data_begin_),
410 reinterpret_cast<uint32_t>(oat_data_end),
411 reinterpret_cast<uint32_t>(oat_file_end));
Ian Rogers30fab402012-01-23 15:43:46 -0800412 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800413
414 // Note that image_end_ is left at end of used space
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700415}
416
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700417void ImageWriter::CopyAndFixupObjects()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700418 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700419 Thread* self = Thread::Current();
420 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800421 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800423 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700424 // TODO: Image spaces only?
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700425 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700426 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700427 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700428 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700429}
430
Brian Carlstrom78128a62011-09-15 17:21:19 -0700431void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700432 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700433 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700434 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700435 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700436
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700437 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700438 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800439 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700440 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700441 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800442 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700443 memcpy(dst, src, n);
444 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800445 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700446 image_writer->FixupObject(obj, copy);
447}
448
Brian Carlstrom4873d462011-08-21 15:23:39 -0700449void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700450 DCHECK(orig != NULL);
451 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700452 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700453 // TODO: special case init of pointers to malloc data (or removal of these pointers)
454 if (orig->IsClass()) {
455 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
456 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700457 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700458 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700459 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700460 } else {
461 FixupInstanceFields(orig, copy);
462 }
463}
464
Brian Carlstrom4873d462011-08-21 15:23:39 -0700465void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700466 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700467 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700468}
469
Mathieu Chartier66f19252012-09-18 08:57:04 -0700470void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700471 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700472
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800474 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475
476 // Every type of method can have an invoke stub
477 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800478 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700479 copy->invoke_stub_ = reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(invoke_stub));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480
481 if (orig->IsAbstract()) {
482 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
483 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
484 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
485 copy->code_ = copy_ame_stub_array_->GetData();
486 return;
487 }
488
Ian Rogers19846512012-02-24 11:42:47 -0800489 if (orig == Runtime::Current()->GetResolutionMethod()) {
490 // The resolution stub's code points at the unknown resolution trampoline
491 ByteArray* orig_res_stub_array_ =
492 Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod);
493 CHECK(orig->GetCode() == orig_res_stub_array_->GetData());
494 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
495 copy->code_ = copy_res_stub_array_->GetData();
496 return;
497 }
498
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700499 // Non-abstract methods typically have code
500 uint32_t code_offset = orig->GetOatCodeOffset();
Ian Rogers19846512012-02-24 11:42:47 -0800501 const byte* code = NULL;
502 if (orig->IsStatic()) {
503 // Static methods may point at the resolution trampoline stub
504 ByteArray* orig_res_stub_array_ =
505 Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod);
506 if (reinterpret_cast<int8_t*>(code_offset) == orig_res_stub_array_->GetData()) {
507 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
508 code = reinterpret_cast<const byte*>(copy_res_stub_array_->GetData());
509 }
510 }
511 if (code == NULL) {
512 code = GetOatAddress(code_offset);
513 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700514 copy->code_ = code;
515
Brian Carlstrom16192862011-09-12 17:50:06 -0700516 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700517 // The native method's pointer is directed to a stub to lookup via dlsym.
518 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800519 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700520 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
521 copy->native_method_ = copy_jni_stub_array_->GetData();
522 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 // normal (non-abstract non-native) methods have mapping tables to relocate
524 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800525 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700526 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
527
528 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800529 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700530 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800531
Ian Rogers0c7abda2012-09-19 13:33:42 -0700532 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
533 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
534 copy->native_gc_map_ = reinterpret_cast<const uint8_t*>(native_gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700535 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700536}
537
Brian Carlstrom4873d462011-08-21 15:23:39 -0700538void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700539 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700540 const Object* element = orig->Get(i);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700541 copy->SetPtrWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700542 }
543}
544
Brian Carlstrom4873d462011-08-21 15:23:39 -0700545void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
546 DCHECK(orig != NULL);
547 DCHECK(copy != NULL);
548 Class* klass = orig->GetClass();
549 DCHECK(klass != NULL);
550 FixupFields(orig,
551 copy,
552 klass->GetReferenceInstanceOffsets(),
553 false);
554}
555
556void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
557 DCHECK(orig != NULL);
558 DCHECK(copy != NULL);
559 FixupFields(orig,
560 copy,
561 orig->GetReferenceStaticOffsets(),
562 true);
563}
564
565void ImageWriter::FixupFields(const Object* orig,
566 Object* copy,
567 uint32_t ref_offsets,
568 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700569 if (ref_offsets != CLASS_WALK_SUPER) {
570 // Found a reference offset bitmap. Fixup the specified offsets.
571 while (ref_offsets != 0) {
572 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700573 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
574 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700575 // Use SetFieldPtr to avoid card marking since we are writing to the image.
576 copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700577 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
578 }
579 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700580 // There is no reference offset bitmap. In the non-static case,
581 // walk up the class inheritance hierarchy and find reference
582 // offsets the hard way. In the static case, just consider this
583 // class.
584 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700585 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700586 klass = is_static ? NULL : klass->GetSuperClass()) {
587 size_t num_reference_fields = (is_static
588 ? klass->NumReferenceStaticFields()
589 : klass->NumReferenceInstanceFields());
590 for (size_t i = 0; i < num_reference_fields; ++i) {
591 Field* field = (is_static
592 ? klass->GetStaticField(i)
593 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700594 MemberOffset field_offset = field->GetOffset();
595 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700596 // Use SetFieldPtr to avoid card marking since we are writing to the image.
597 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700598 }
599 }
600 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700601 if (!is_static && orig->IsReferenceInstance()) {
602 // Fix-up referent, that isn't marked as an object field, for References.
603 Field* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
604 MemberOffset field_offset = field->GetOffset();
605 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
606 // Use SetFieldPtr to avoid card marking since we are writing to the image.
607 copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
608 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700609}
610
Mathieu Chartier66f19252012-09-18 08:57:04 -0700611static AbstractMethod* GetTargetMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700613 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700615 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700616 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700618 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700619 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700620 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700621 CHECK(method != NULL)
622 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700623 CHECK(!method->IsRuntimeMethod())
624 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700626 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700628 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700629 return method;
630}
631
632void ImageWriter::PatchOatCodeAndMethods(const Compiler& compiler) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700633 Thread* self = Thread::Current();
Brian Carlstromf5822582012-03-19 22:34:31 -0700634 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700635 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
Brian Carlstromf5822582012-03-19 22:34:31 -0700636
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700637 typedef std::vector<const Compiler::PatchInformation*> Patches;
638 const Patches& code_to_patch = compiler.GetCodeToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700639 for (size_t i = 0; i < code_to_patch.size(); i++) {
640 const Compiler::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700641 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700642 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
643 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
644 uint32_t code_offset = code - code_base;
645 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
646 }
647
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700648 const Patches& methods_to_patch = compiler.GetMethodsToPatch();
Brian Carlstromf5822582012-03-19 22:34:31 -0700649 for (size_t i = 0; i < methods_to_patch.size(); i++) {
650 const Compiler::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700651 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700652 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
653 }
Brian Carlstroma85b8372012-10-18 17:00:32 -0700654
655 // Update the image header with the new checksum after patching
656 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
657 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700658 self->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromf5822582012-03-19 22:34:31 -0700659}
660
661void ImageWriter::SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) {
662 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700663 const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
664 patch->GetReferrerMethodIdx());
Brian Carlstroma85b8372012-10-18 17:00:32 -0700665 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
Brian Carlstromf5822582012-03-19 22:34:31 -0700666 // TODO: make this Thumb2 specific
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700667 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
Brian Carlstromf5822582012-03-19 22:34:31 -0700668 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
669#ifndef NDEBUG
670 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
671 uint32_t expected = reinterpret_cast<uint32_t>(&id);
672 uint32_t actual = *patch_location;
673 CHECK(actual == expected || actual == value) << std::hex
674 << "actual=" << actual
675 << "expected=" << expected
676 << "value=" << value;
677#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700678 *patch_location = value;
Brian Carlstroma85b8372012-10-18 17:00:32 -0700679 oat_header.UpdateChecksum(patch_location, sizeof(value));
Brian Carlstromf5822582012-03-19 22:34:31 -0700680}
681
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700682} // namespace art