blob: 2ec47ec876749ed79cc3ec2a9db3175ae8a2f645 [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
Brian Carlstroma663ea52011-08-19 23:33:41 -070023#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_loader.h"
Brian Carlstromae826982011-11-09 01:33:42 -080025#include "compiled_method.h"
Brian Carlstromf5822582012-03-19 22:34:31 -070026#include "compiler.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070027#include "dex_cache.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028#include "file.h"
29#include "globals.h"
30#include "heap.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070031#include "image.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070032#include "intern_table.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070033#include "logging.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080034#include "oat_file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070035#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080036#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070037#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038#include "scoped_thread_state_change.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039#include "space.h"
Elliott Hughesa168c832012-06-12 15:34:20 -070040#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070041#include "utils.h"
42
43namespace art {
44
Brian Carlstroma004aa92012-02-08 18:05:09 -080045bool ImageWriter::Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080046 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080047 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070048 const std::string& oat_location,
49 const Compiler& compiler) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080050 CHECK(!image_filename.empty());
Brian Carlstromaded5f72011-10-07 17:15:04 -070051
Ian Rogers30fab402012-01-23 15:43:46 -080052 CHECK_NE(image_begin, 0U);
53 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070054
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080055 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070056 const Spaces& spaces = heap->GetSpaces();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070057
Brian Carlstromae826982011-11-09 01:33:42 -080058 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
59 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
60 for (size_t i = 0; i < all_dex_caches.size(); i++) {
61 DexCache* dex_cache = all_dex_caches[i];
62 if (InSourceSpace(dex_cache)) {
63 dex_caches_.insert(dex_cache);
64 }
65 }
66
Logan Chien0c717dd2012-03-28 18:31:07 +080067 oat_file_ = OatFile::Open(oat_filename, oat_location, NULL,
68 OatFile::kRelocNone, true);
Brian Carlstromf5822582012-03-19 22:34:31 -070069 if (oat_file_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070070 LOG(ERROR) << "Failed to open oat file " << oat_filename;
71 return false;
72 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -070073 class_linker->RegisterOatFile(*oat_file_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070074
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 {
76 Thread::Current()->TransitionFromSuspendedToRunnable();
77 PruneNonImageClasses(); // Remove junk
78 ComputeLazyFieldsForImageClasses(); // Add useful information
79 ComputeEagerResolvedStrings();
80 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
81 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080082 heap->CollectGarbage(false); // Remove garbage
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070083 // Trim size of alloc spaces
84 // TODO: C++0x auto
85 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
86 if ((*cur)->IsAllocSpace()) {
87 (*cur)->AsAllocSpace()->Trim();
88 }
89 }
90
Brian Carlstromae826982011-11-09 01:33:42 -080091 if (!AllocMemory()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070092 return false;
93 }
Brian Carlstromae826982011-11-09 01:33:42 -080094#ifndef NDEBUG
Mathieu Chartier357e9be2012-08-01 11:00:14 -070095 {
96 ScopedObjectAccess soa(Thread::Current());
97 CheckNonImageClassesRemoved();
98 }
Brian Carlstromae826982011-11-09 01:33:42 -080099#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800100 heap->DisableCardMarking();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 {
102 Thread::Current()->TransitionFromSuspendedToRunnable();
103 CalculateNewObjectOffsets();
104 CopyAndFixupObjects();
105 PatchOatCodeAndMethods(compiler);
106 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
107 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700108
Brian Carlstroma004aa92012-02-08 18:05:09 -0800109 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), true));
Elliott Hughes90a33692011-08-30 13:27:07 -0700110 if (file.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 LOG(ERROR) << "Failed to open image file " << image_filename;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700112 return false;
113 }
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700114 if (fchmod(file->Fd(), 0644) != 0) {
115 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
116 return EXIT_FAILURE;
117 }
Ian Rogers30fab402012-01-23 15:43:46 -0800118 bool success = file->WriteFully(image_->Begin(), image_end_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 if (!success) {
120 PLOG(ERROR) << "Failed to write image file " << image_filename;
121 return false;
122 }
123 return true;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700124}
125
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700126bool ImageWriter::InSourceSpace(const Object* object) const {
127 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
128 // TODO: C++0x auto
129 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
130 if ((*cur)->IsAllocSpace() && (*cur)->Contains(object)) {
131 return true;
132 }
133 }
134 return false;
135}
136
Brian Carlstromae826982011-11-09 01:33:42 -0800137bool ImageWriter::AllocMemory() {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 typedef std::vector<Space*> SpaceVec;
139 const SpaceVec& spaces = Runtime::Current()->GetHeap()->GetSpaces();
140 size_t size = 0;
141 for (SpaceVec::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
142 if ((*cur)->IsAllocSpace()) {
143 size += (*cur)->Size();
144 }
145 }
146
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700147 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700148 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800149 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700150 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700152 return false;
153 }
154 return true;
155}
156
Ian Rogersd418eda2012-01-30 12:14:28 -0800157void ImageWriter::ComputeLazyFieldsForImageClasses() {
158 Runtime* runtime = Runtime::Current();
159 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
Ian Rogersd418eda2012-01-30 12:14:28 -0800161}
162
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700163bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
164 c->ComputeName();
Ian Rogersd418eda2012-01-30 12:14:28 -0800165 return true;
166}
167
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800168void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
169 if (!obj->GetClass()->IsStringClass()) {
170 return;
171 }
172 String* string = obj->AsString();
173 std::string utf8_string(string->ToModifiedUtf8());
174 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
175 ClassLinker* linker = Runtime::Current()->GetClassLinker();
176 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
177 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
178 DexCache* dex_cache = *it;
179 const DexFile& dex_file = linker->FindDexFile(dex_cache);
180 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
181 if (string_id != NULL) {
182 // This string occurs in this dex file, assign the dex cache entry.
183 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
184 if (dex_cache->GetResolvedString(string_idx) == NULL) {
185 dex_cache->SetResolvedString(string_idx, string);
186 }
187 }
188 }
189}
190
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700191void ImageWriter::ComputeEagerResolvedStrings()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700193 // TODO: Check image spaces only?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700194 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700195 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700196 heap->FlushAllocStack();
197 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800198}
199
Brian Carlstromae826982011-11-09 01:33:42 -0800200bool ImageWriter::IsImageClass(const Class* klass) {
201 if (image_classes_ == NULL) {
202 return true;
203 }
204 while (klass->IsArrayClass()) {
205 klass = klass->GetComponentType();
206 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800207 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800208 return true;
209 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800210 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800211 return image_classes_->find(descriptor) != image_classes_->end();
212}
213
214
215struct NonImageClasses {
216 ImageWriter* image_writer;
217 std::set<std::string>* non_image_classes;
218};
219
220void ImageWriter::PruneNonImageClasses() {
221 if (image_classes_ == NULL) {
222 return;
223 }
224 Runtime* runtime = Runtime::Current();
225 ClassLinker* class_linker = runtime->GetClassLinker();
226
227 std::set<std::string> non_image_classes;
228 NonImageClasses context;
229 context.image_writer = this;
230 context.non_image_classes = &non_image_classes;
231 class_linker->VisitClasses(NonImageClassesVisitor, &context);
232
233 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
234 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800235 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800236 }
237
Mathieu Chartier66f19252012-09-18 08:57:04 -0700238 AbstractMethod* resolution_method = runtime->GetResolutionMethod();
Brian Carlstromae826982011-11-09 01:33:42 -0800239 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
240 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
241 DexCache* dex_cache = *it;
242 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
243 Class* klass = dex_cache->GetResolvedType(i);
244 if (klass != NULL && !IsImageClass(klass)) {
245 dex_cache->SetResolvedType(i, NULL);
246 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
247 }
248 }
249 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700250 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstromae826982011-11-09 01:33:42 -0800251 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
Ian Rogers19846512012-02-24 11:42:47 -0800252 dex_cache->SetResolvedMethod(i, resolution_method);
Brian Carlstromae826982011-11-09 01:33:42 -0800253 }
254 }
255 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
256 Field* field = dex_cache->GetResolvedField(i);
257 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
258 dex_cache->SetResolvedField(i, NULL);
259 }
260 }
261 }
262}
263
264bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
265 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
266 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800268 }
269 return true;
270}
271
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700272void ImageWriter::CheckNonImageClassesRemoved()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800274 if (image_classes_ == NULL) {
275 return;
276 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700277
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700278 Heap* heap = Runtime::Current()->GetHeap();
279 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 WriterMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700281 heap->FlushAllocStack();
282 }
283
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700285 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
Brian Carlstromae826982011-11-09 01:33:42 -0800286}
287
288void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
289 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
290 if (!obj->IsClass()) {
291 return;
292 }
293 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800294 if (!image_writer->IsImageClass(klass)) {
295 image_writer->DumpImageClasses();
296 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
297 << " " << PrettyDescriptor(klass);
298 }
299}
300
301void ImageWriter::DumpImageClasses() {
302 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
303 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
304 LOG(INFO) << " " << *it;
305 }
Brian Carlstromae826982011-11-09 01:33:42 -0800306}
307
Brian Carlstrom78128a62011-09-15 17:21:19 -0700308void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700309 DCHECK(obj != NULL);
310 DCHECK(arg != NULL);
311 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700312 if (!image_writer->InSourceSpace(obj)) {
313 return;
314 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700315
316 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800317 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700318 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800319 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700320 DCHECK_EQ(obj, obj->AsString()->Intern());
321 return;
322 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700323 SirtRef<String> interned(obj->AsString()->Intern());
324 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800325 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700326 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700327 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700328 }
329 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800330 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700331 return;
332 }
333 // else (obj == interned), nothing to do but fall through to the normal case
334 }
335
336 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700337}
338
Brian Carlstrome24fa612011-09-29 00:53:55 -0700339ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700340 Runtime* runtime = Runtime::Current();
341 ClassLinker* class_linker = runtime->GetClassLinker();
342 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700343
344 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700345 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800346 dex_caches_.size());
347 int i = 0;
348 typedef Set::const_iterator It; // TODO: C++0x auto
349 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
350 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700351 }
352
353 // build an Object[] of the roots needed to restore the runtime
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700354 SirtRef<ObjectArray<Object> > image_roots(
355 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800356 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
358 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700359 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
360 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
361 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
362 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers19846512012-02-24 11:42:47 -0800363 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700364 image_roots->Set(ImageHeader::kCalleeSaveMethod,
365 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
366 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
367 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
368 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
369 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700370 image_roots->Set(ImageHeader::kOatLocation,
371 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700372 image_roots->Set(ImageHeader::kDexCaches,
373 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700374 image_roots->Set(ImageHeader::kClassRoots,
375 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
377 CHECK(image_roots->Get(i) != NULL);
378 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700379 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700380}
381
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700382void ImageWriter::CalculateNewObjectOffsets() {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700383 SirtRef<ObjectArray<Object> > image_roots(CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700384
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700385 Heap* heap = Runtime::Current()->GetHeap();
386 typedef std::vector<Space*> SpaceVec;
387 const SpaceVec& spaces = heap->GetSpaces();
388 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800389 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700390
Brian Carlstrom16192862011-09-12 17:50:06 -0700391 // leave space for the header, but do not write it yet, we need to
392 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800393 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700394
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700395 {
396 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700397 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700398 heap->FlushAllocStack();
399 }
400
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700401 {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700402 // TODO: Image spaces only?
403 // TODO: Add InOrderWalk to heap bitmap.
404 const char* old = Thread::Current()->StartAssertNoThreadSuspension("ImageWriter");
405 for (SpaceVec::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
406 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700407 DCHECK_LT(image_end_, image_->Size());
408 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700409 Thread::Current()->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700410 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700411
Brian Carlstrome24fa612011-09-29 00:53:55 -0700412 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800413 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
414 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700415
Brian Carlstrom16192862011-09-12 17:50:06 -0700416 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800417 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700418 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700419 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800420 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700421 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800422 memcpy(image_->Begin(), &image_header, sizeof(image_header));
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_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700427 const char* old_cause = Thread::Current()->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800428 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800430 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700431 // TODO: Image spaces only?
Ian Rogersb726dcb2012-09-05 08:57:23 -0700432 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700433 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700434 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700435 Thread::Current()->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700436}
437
Brian Carlstrom78128a62011-09-15 17:21:19 -0700438void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700439 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700440 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700441 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700442 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700443 if (!image_writer->InSourceSpace(object)) {
444 return;
445 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700446
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700447 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700448 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800449 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700450 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700451 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800452 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700453 memcpy(dst, src, n);
454 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800455 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700456 image_writer->FixupObject(obj, copy);
457}
458
Brian Carlstrom4873d462011-08-21 15:23:39 -0700459void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700460 DCHECK(orig != NULL);
461 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700462 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700463 // TODO: special case init of pointers to malloc data (or removal of these pointers)
464 if (orig->IsClass()) {
465 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
466 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700467 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700468 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700469 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700470 } else {
471 FixupInstanceFields(orig, copy);
472 }
473}
474
Brian Carlstrom4873d462011-08-21 15:23:39 -0700475void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700476 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700477 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700478}
479
Mathieu Chartier66f19252012-09-18 08:57:04 -0700480void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700481 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700482
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700483 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800484 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700485
486 // Every type of method can have an invoke stub
487 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800488 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700489 copy->invoke_stub_ = reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(invoke_stub));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700490
491 if (orig->IsAbstract()) {
492 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
493 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
494 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
495 copy->code_ = copy_ame_stub_array_->GetData();
496 return;
497 }
498
Ian Rogers19846512012-02-24 11:42:47 -0800499 if (orig == Runtime::Current()->GetResolutionMethod()) {
500 // The resolution stub's code points at the unknown resolution trampoline
501 ByteArray* orig_res_stub_array_ =
502 Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod);
503 CHECK(orig->GetCode() == orig_res_stub_array_->GetData());
504 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
505 copy->code_ = copy_res_stub_array_->GetData();
506 return;
507 }
508
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700509 // Non-abstract methods typically have code
510 uint32_t code_offset = orig->GetOatCodeOffset();
Ian Rogers19846512012-02-24 11:42:47 -0800511 const byte* code = NULL;
512 if (orig->IsStatic()) {
513 // Static methods may point at the resolution trampoline stub
514 ByteArray* orig_res_stub_array_ =
515 Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod);
516 if (reinterpret_cast<int8_t*>(code_offset) == orig_res_stub_array_->GetData()) {
517 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
518 code = reinterpret_cast<const byte*>(copy_res_stub_array_->GetData());
519 }
520 }
521 if (code == NULL) {
522 code = GetOatAddress(code_offset);
523 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700524 copy->code_ = code;
525
Brian Carlstrom16192862011-09-12 17:50:06 -0700526 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700527 // The native method's pointer is directed to a stub to lookup via dlsym.
528 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800529 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700530 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
531 copy->native_method_ = copy_jni_stub_array_->GetData();
532 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700533 // normal (non-abstract non-native) methods have mapping tables to relocate
534 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800535 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700536 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
537
538 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800539 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700540 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800541
Ian Rogers0c7abda2012-09-19 13:33:42 -0700542 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
543 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
544 copy->native_gc_map_ = reinterpret_cast<const uint8_t*>(native_gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700545 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700546}
547
Brian Carlstrom4873d462011-08-21 15:23:39 -0700548void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700549 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700550 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700551 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700552 }
553}
554
Brian Carlstrom4873d462011-08-21 15:23:39 -0700555void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
556 DCHECK(orig != NULL);
557 DCHECK(copy != NULL);
558 Class* klass = orig->GetClass();
559 DCHECK(klass != NULL);
560 FixupFields(orig,
561 copy,
562 klass->GetReferenceInstanceOffsets(),
563 false);
564}
565
566void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
567 DCHECK(orig != NULL);
568 DCHECK(copy != NULL);
569 FixupFields(orig,
570 copy,
571 orig->GetReferenceStaticOffsets(),
572 true);
573}
574
575void ImageWriter::FixupFields(const Object* orig,
576 Object* copy,
577 uint32_t ref_offsets,
578 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700579 if (ref_offsets != CLASS_WALK_SUPER) {
580 // Found a reference offset bitmap. Fixup the specified offsets.
581 while (ref_offsets != 0) {
582 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700583 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
584 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
585 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700586 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
587 }
588 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700589 // There is no reference offset bitmap. In the non-static case,
590 // walk up the class inheritance hierarchy and find reference
591 // offsets the hard way. In the static case, just consider this
592 // class.
593 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700594 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700595 klass = is_static ? NULL : klass->GetSuperClass()) {
596 size_t num_reference_fields = (is_static
597 ? klass->NumReferenceStaticFields()
598 : klass->NumReferenceInstanceFields());
599 for (size_t i = 0; i < num_reference_fields; ++i) {
600 Field* field = (is_static
601 ? klass->GetStaticField(i)
602 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700603 MemberOffset field_offset = field->GetOffset();
604 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
605 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700606 }
607 }
608 }
609}
610
Mathieu Chartier66f19252012-09-18 08:57:04 -0700611static AbstractMethod* GetReferrerMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 ScopedObjectAccessUnchecked soa(Thread::Current());
Brian Carlstromf5822582012-03-19 22:34:31 -0700614 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700616 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700617 patch->GetReferrerMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700619 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700620 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700621 patch->GetReferrerInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700622 CHECK(method != NULL)
623 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700624 CHECK(!method->IsRuntimeMethod())
625 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetReferrerMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700627 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetReferrerMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700629 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700630 return method;
631}
632
Mathieu Chartier66f19252012-09-18 08:57:04 -0700633static AbstractMethod* GetTargetMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700634 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700635 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700637 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700638 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700640 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700641 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700642 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700643 CHECK(method != NULL)
644 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700645 CHECK(!method->IsRuntimeMethod())
646 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700648 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700650 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700651 return method;
652}
653
654void ImageWriter::PatchOatCodeAndMethods(const Compiler& compiler) {
655 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
656
657 const std::vector<const Compiler::PatchInformation*>& code_to_patch = compiler.GetCodeToPatch();
658 for (size_t i = 0; i < code_to_patch.size(); i++) {
659 const Compiler::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700660 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700661 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
662 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
663 uint32_t code_offset = code - code_base;
664 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
665 }
666
667 const std::vector<const Compiler::PatchInformation*>& methods_to_patch
668 = compiler.GetMethodsToPatch();
669 for (size_t i = 0; i < methods_to_patch.size(); i++) {
670 const Compiler::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700671 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700672 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
673 }
674}
675
676void ImageWriter::SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) {
677 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700678 AbstractMethod* method = GetReferrerMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700679 // Goodbye const, we are about to modify some code.
680 void* code = const_cast<void*>(class_linker->GetOatCodeFor(method));
681 // TODO: make this Thumb2 specific
682 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(code) & ~0x1);
683 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
684#ifndef NDEBUG
685 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
686 uint32_t expected = reinterpret_cast<uint32_t>(&id);
687 uint32_t actual = *patch_location;
688 CHECK(actual == expected || actual == value) << std::hex
689 << "actual=" << actual
690 << "expected=" << expected
691 << "value=" << value;
692#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700693 *patch_location = value;
Brian Carlstromf5822582012-03-19 22:34:31 -0700694}
695
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700696} // namespace art