blob: 33d37c7667da0899aade7bc38528233f62c1360c [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"
Ian Rogers1f539342012-10-03 21:09:42 -070039#include "sirt_ref.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040#include "space.h"
Elliott Hughesa168c832012-06-12 15:34:20 -070041#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070042#include "utils.h"
43
44namespace art {
45
Brian Carlstroma004aa92012-02-08 18:05:09 -080046bool ImageWriter::Write(const std::string& image_filename,
Ian Rogers30fab402012-01-23 15:43:46 -080047 uintptr_t image_begin,
Brian Carlstromae826982011-11-09 01:33:42 -080048 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -070049 const std::string& oat_location,
50 const Compiler& compiler) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080051 CHECK(!image_filename.empty());
Brian Carlstromaded5f72011-10-07 17:15:04 -070052
Ian Rogers30fab402012-01-23 15:43:46 -080053 CHECK_NE(image_begin, 0U);
54 image_begin_ = reinterpret_cast<byte*>(image_begin);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070055
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080056 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070057 const Spaces& spaces = heap->GetSpaces();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070058
Brian Carlstromae826982011-11-09 01:33:42 -080059 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
60 const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
61 for (size_t i = 0; i < all_dex_caches.size(); i++) {
62 DexCache* dex_cache = all_dex_caches[i];
63 if (InSourceSpace(dex_cache)) {
64 dex_caches_.insert(dex_cache);
65 }
66 }
67
Logan Chien0c717dd2012-03-28 18:31:07 +080068 oat_file_ = OatFile::Open(oat_filename, oat_location, NULL,
69 OatFile::kRelocNone, true);
Brian Carlstromf5822582012-03-19 22:34:31 -070070 if (oat_file_ == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 LOG(ERROR) << "Failed to open oat file " << oat_filename;
72 return false;
73 }
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
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800101 heap->DisableCardMarking();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 {
103 Thread::Current()->TransitionFromSuspendedToRunnable();
104 CalculateNewObjectOffsets();
105 CopyAndFixupObjects();
106 PatchOatCodeAndMethods(compiler);
107 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
108 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700109
Brian Carlstroma004aa92012-02-08 18:05:09 -0800110 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), true));
Elliott Hughes90a33692011-08-30 13:27:07 -0700111 if (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 Carlstrom6cd40e52012-05-03 14:15:11 -0700115 if (fchmod(file->Fd(), 0644) != 0) {
116 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
117 return EXIT_FAILURE;
118 }
Ian Rogers30fab402012-01-23 15:43:46 -0800119 bool success = 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
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700127bool ImageWriter::InSourceSpace(const Object* object) const {
128 const Spaces& spaces = Runtime::Current()->GetHeap()->GetSpaces();
129 // TODO: C++0x auto
130 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
131 if ((*cur)->IsAllocSpace() && (*cur)->Contains(object)) {
132 return true;
133 }
134 }
135 return false;
136}
137
Brian Carlstromae826982011-11-09 01:33:42 -0800138bool ImageWriter::AllocMemory() {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139 typedef std::vector<Space*> SpaceVec;
140 const SpaceVec& spaces = Runtime::Current()->GetHeap()->GetSpaces();
141 size_t size = 0;
142 for (SpaceVec::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
143 if ((*cur)->IsAllocSpace()) {
144 size += (*cur)->Size();
145 }
146 }
147
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700148 int prot = PROT_READ | PROT_WRITE;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700149 size_t length = RoundUp(size, kPageSize);
Brian Carlstrom89521892011-12-07 22:05:07 -0800150 image_.reset(MemMap::MapAnonymous("image-writer-image", NULL, length, prot));
Elliott Hughes90a33692011-08-30 13:27:07 -0700151 if (image_.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152 LOG(ERROR) << "Failed to allocate memory for image file generation";
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700153 return false;
154 }
155 return true;
156}
157
Ian Rogersd418eda2012-01-30 12:14:28 -0800158void ImageWriter::ComputeLazyFieldsForImageClasses() {
159 Runtime* runtime = Runtime::Current();
160 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
Ian Rogersd418eda2012-01-30 12:14:28 -0800162}
163
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700164bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
165 c->ComputeName();
Ian Rogersd418eda2012-01-30 12:14:28 -0800166 return true;
167}
168
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800169void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
170 if (!obj->GetClass()->IsStringClass()) {
171 return;
172 }
173 String* string = obj->AsString();
174 std::string utf8_string(string->ToModifiedUtf8());
175 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
176 ClassLinker* linker = Runtime::Current()->GetClassLinker();
177 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
178 for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
179 DexCache* dex_cache = *it;
180 const DexFile& dex_file = linker->FindDexFile(dex_cache);
181 const DexFile::StringId* string_id = dex_file.FindStringId(utf8_string);
182 if (string_id != NULL) {
183 // This string occurs in this dex file, assign the dex cache entry.
184 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
185 if (dex_cache->GetResolvedString(string_idx) == NULL) {
186 dex_cache->SetResolvedString(string_idx, string);
187 }
188 }
189 }
190}
191
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700192void ImageWriter::ComputeEagerResolvedStrings()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700194 // TODO: Check image spaces only?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700195 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700197 heap->FlushAllocStack();
198 heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
Ian Rogersd1f1bf02012-02-26 16:59:20 -0800199}
200
Brian Carlstromae826982011-11-09 01:33:42 -0800201bool ImageWriter::IsImageClass(const Class* klass) {
202 if (image_classes_ == NULL) {
203 return true;
204 }
205 while (klass->IsArrayClass()) {
206 klass = klass->GetComponentType();
207 }
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800208 if (klass->IsPrimitive()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800209 return true;
210 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 const std::string descriptor(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800212 return image_classes_->find(descriptor) != image_classes_->end();
213}
214
215
216struct NonImageClasses {
217 ImageWriter* image_writer;
218 std::set<std::string>* non_image_classes;
219};
220
221void ImageWriter::PruneNonImageClasses() {
222 if (image_classes_ == NULL) {
223 return;
224 }
225 Runtime* runtime = Runtime::Current();
226 ClassLinker* class_linker = runtime->GetClassLinker();
227
228 std::set<std::string> non_image_classes;
229 NonImageClasses context;
230 context.image_writer = this;
231 context.non_image_classes = &non_image_classes;
232 class_linker->VisitClasses(NonImageClassesVisitor, &context);
233
234 typedef std::set<std::string>::const_iterator ClassIt; // TODO: C++0x auto
235 for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800236 class_linker->RemoveClass((*it).c_str(), NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800237 }
238
Mathieu Chartier66f19252012-09-18 08:57:04 -0700239 AbstractMethod* resolution_method = runtime->GetResolutionMethod();
Brian Carlstromae826982011-11-09 01:33:42 -0800240 typedef Set::const_iterator CacheIt; // TODO: C++0x auto
241 for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
242 DexCache* dex_cache = *it;
243 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
244 Class* klass = dex_cache->GetResolvedType(i);
245 if (klass != NULL && !IsImageClass(klass)) {
246 dex_cache->SetResolvedType(i, NULL);
247 dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
248 }
249 }
250 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700251 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstromae826982011-11-09 01:33:42 -0800252 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
Ian Rogers19846512012-02-24 11:42:47 -0800253 dex_cache->SetResolvedMethod(i, resolution_method);
Brian Carlstromae826982011-11-09 01:33:42 -0800254 }
255 }
256 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
257 Field* field = dex_cache->GetResolvedField(i);
258 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
259 dex_cache->SetResolvedField(i, NULL);
260 }
261 }
262 }
263}
264
265bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
266 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
267 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800269 }
270 return true;
271}
272
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700273void ImageWriter::CheckNonImageClassesRemoved()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromae826982011-11-09 01:33:42 -0800275 if (image_classes_ == NULL) {
276 return;
277 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700278
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700279 Heap* heap = Runtime::Current()->GetHeap();
280 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700281 WriterMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700282 heap->FlushAllocStack();
283 }
284
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700286 heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
Brian Carlstromae826982011-11-09 01:33:42 -0800287}
288
289void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
290 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
291 if (!obj->IsClass()) {
292 return;
293 }
294 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800295 if (!image_writer->IsImageClass(klass)) {
296 image_writer->DumpImageClasses();
297 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
298 << " " << PrettyDescriptor(klass);
299 }
300}
301
302void ImageWriter::DumpImageClasses() {
303 typedef std::set<std::string>::const_iterator It; // TODO: C++0x auto
304 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end; ++it) {
305 LOG(INFO) << " " << *it;
306 }
Brian Carlstromae826982011-11-09 01:33:42 -0800307}
308
Brian Carlstrom78128a62011-09-15 17:21:19 -0700309void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700310 DCHECK(obj != NULL);
311 DCHECK(arg != NULL);
312 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700313 if (!image_writer->InSourceSpace(obj)) {
314 return;
315 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700316
317 // if it is a string, we want to intern it if its not interned.
Elliott Hughesdbb40792011-11-18 17:05:22 -0800318 if (obj->GetClass()->IsStringClass()) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700319 // we must be an interned string that was forward referenced and already assigned
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800320 if (image_writer->IsImageOffsetAssigned(obj)) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700321 DCHECK_EQ(obj, obj->AsString()->Intern());
322 return;
323 }
Ian Rogers1f539342012-10-03 21:09:42 -0700324 SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700325 if (obj != interned.get()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800326 if (!image_writer->IsImageOffsetAssigned(interned.get())) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700327 // interned obj is after us, allocate its location early
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700328 image_writer->AssignImageOffset(interned.get());
Brian Carlstromc74255f2011-09-11 22:47:39 -0700329 }
330 // point those looking for this object to the interned version.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800331 image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700332 return;
333 }
334 // else (obj == interned), nothing to do but fall through to the normal case
335 }
336
337 image_writer->AssignImageOffset(obj);
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700338}
339
Brian Carlstrome24fa612011-09-29 00:53:55 -0700340ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700341 Runtime* runtime = Runtime::Current();
342 ClassLinker* class_linker = runtime->GetClassLinker();
343 Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700344
345 // build an Object[] of all the DexCaches used in the source_space_
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700346 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(object_array_class,
Brian Carlstromae826982011-11-09 01:33:42 -0800347 dex_caches_.size());
348 int i = 0;
349 typedef Set::const_iterator It; // TODO: C++0x auto
350 for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
351 dex_caches->Set(i, *it);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700352 }
353
354 // build an Object[] of the roots needed to restore the runtime
Ian Rogers1f539342012-10-03 21:09:42 -0700355 SirtRef<ObjectArray<Object> >
356 image_roots(Thread::Current(),
357 ObjectArray<Object>::Alloc(object_array_class, ImageHeader::kImageRootsMax));
Ian Rogers169c9a72011-11-13 20:13:17 -0800358 image_roots->Set(ImageHeader::kJniStubArray, runtime->GetJniDlsymLookupStub());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 image_roots->Set(ImageHeader::kAbstractMethodErrorStubArray,
360 runtime->GetAbstractMethodErrorStubArray());
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700361 image_roots->Set(ImageHeader::kStaticResolutionStubArray,
362 runtime->GetResolutionStubArray(Runtime::kStaticMethod));
363 image_roots->Set(ImageHeader::kUnknownMethodResolutionStubArray,
364 runtime->GetResolutionStubArray(Runtime::kUnknownMethod));
Ian Rogers19846512012-02-24 11:42:47 -0800365 image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700366 image_roots->Set(ImageHeader::kCalleeSaveMethod,
367 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
368 image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
369 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
370 image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
371 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372 image_roots->Set(ImageHeader::kOatLocation,
373 String::AllocFromModifiedUtf8(oat_file_->GetLocation().c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700374 image_roots->Set(ImageHeader::kDexCaches,
375 dex_caches);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700376 image_roots->Set(ImageHeader::kClassRoots,
377 class_linker->GetClassRoots());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700378 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
379 CHECK(image_roots->Get(i) != NULL);
380 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700381 return image_roots.get();
Brian Carlstrom16192862011-09-12 17:50:06 -0700382}
383
Brian Carlstrom4e777d42011-08-15 13:53:52 -0700384void ImageWriter::CalculateNewObjectOffsets() {
Ian Rogers1f539342012-10-03 21:09:42 -0700385 Thread* self = Thread::Current();
386 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
Brian Carlstrom16192862011-09-12 17:50:06 -0700387
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700388 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700389 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700390 DCHECK(!spaces.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800391 DCHECK_EQ(0U, image_end_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700392
Brian Carlstrom16192862011-09-12 17:50:06 -0700393 // leave space for the header, but do not write it yet, we need to
394 // know where image_roots is going to end up
Ian Rogers30fab402012-01-23 15:43:46 -0800395 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstroma663ea52011-08-19 23:33:41 -0700396
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700397 {
Ian Rogers1f539342012-10-03 21:09:42 -0700398 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700399 heap->FlushAllocStack();
400 }
401
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700402 {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700403 // TODO: Image spaces only?
404 // TODO: Add InOrderWalk to heap bitmap.
Ian Rogers1f539342012-10-03 21:09:42 -0700405 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700406 DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
407 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700408 (*it)->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700409 DCHECK_LT(image_end_, image_->Size());
410 }
Ian Rogers1f539342012-10-03 21:09:42 -0700411 self->EndAssertNoThreadSuspension(old);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700412 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700413
Brian Carlstrome24fa612011-09-29 00:53:55 -0700414 // Note that image_top_ is left at end of used space
Ian Rogers30fab402012-01-23 15:43:46 -0800415 oat_begin_ = image_begin_ + RoundUp(image_end_, kPageSize);
416 const byte* oat_limit = oat_begin_ + oat_file_->Size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417
Brian Carlstrom16192862011-09-12 17:50:06 -0700418 // return to write header at start of image with future location of image_roots
Ian Rogers30fab402012-01-23 15:43:46 -0800419 ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700420 reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700421 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogers30fab402012-01-23 15:43:46 -0800422 reinterpret_cast<uint32_t>(oat_begin_),
Brian Carlstrome24fa612011-09-29 00:53:55 -0700423 reinterpret_cast<uint32_t>(oat_limit));
Ian Rogers30fab402012-01-23 15:43:46 -0800424 memcpy(image_->Begin(), &image_header, sizeof(image_header));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700425}
426
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700427void ImageWriter::CopyAndFixupObjects()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700428 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700429 const char* old_cause = Thread::Current()->StartAssertNoThreadSuspension("ImageWriter");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800430 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700431 // TODO: heap validation can't handle this fix up pass
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800432 heap->DisableObjectValidation();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700433 // TODO: Image spaces only?
Ian Rogersb726dcb2012-09-05 08:57:23 -0700434 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700435 heap->FlushAllocStack();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700436 heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700437 Thread::Current()->EndAssertNoThreadSuspension(old_cause);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700438}
439
Brian Carlstrom78128a62011-09-15 17:21:19 -0700440void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700441 DCHECK(object != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700442 DCHECK(arg != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700443 const Object* obj = object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700444 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700445 if (!image_writer->InSourceSpace(object)) {
446 return;
447 }
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700448
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700449 // see GetLocalAddress for similar computation
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700450 size_t offset = image_writer->GetImageOffset(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800451 byte* dst = image_writer->image_->Begin() + offset;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700452 const byte* src = reinterpret_cast<const byte*>(obj);
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700453 size_t n = obj->SizeOf();
Ian Rogers30fab402012-01-23 15:43:46 -0800454 DCHECK_LT(offset + n, image_writer->image_->Size());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700455 memcpy(dst, src, n);
456 Object* copy = reinterpret_cast<Object*>(dst);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800457 copy->monitor_ = 0; // We may have inflated the lock during compilation.
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700458 image_writer->FixupObject(obj, copy);
459}
460
Brian Carlstrom4873d462011-08-21 15:23:39 -0700461void ImageWriter::FixupObject(const Object* orig, Object* copy) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700462 DCHECK(orig != NULL);
463 DCHECK(copy != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700464 copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700465 // TODO: special case init of pointers to malloc data (or removal of these pointers)
466 if (orig->IsClass()) {
467 FixupClass(orig->AsClass(), down_cast<Class*>(copy));
468 } else if (orig->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700469 FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
Brian Carlstrom16192862011-09-12 17:50:06 -0700470 } else if (orig->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700471 FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700472 } else {
473 FixupInstanceFields(orig, copy);
474 }
475}
476
Brian Carlstrom4873d462011-08-21 15:23:39 -0700477void ImageWriter::FixupClass(const Class* orig, Class* copy) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700478 FixupInstanceFields(orig, copy);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700479 FixupStaticFields(orig, copy);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700480}
481
Mathieu Chartier66f19252012-09-18 08:57:04 -0700482void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700483 FixupInstanceFields(orig, copy);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700484
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700485 // OatWriter replaces the code_ and invoke_stub_ with offset values.
Ian Rogers30fab402012-01-23 15:43:46 -0800486 // Here we readjust to a pointer relative to oat_begin_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700487
488 // Every type of method can have an invoke stub
489 uint32_t invoke_stub_offset = orig->GetOatInvokeStubOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800490 const byte* invoke_stub = GetOatAddress(invoke_stub_offset);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700491 copy->invoke_stub_ = reinterpret_cast<AbstractMethod::InvokeStub*>(const_cast<byte*>(invoke_stub));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492
493 if (orig->IsAbstract()) {
494 // Abstract methods are pointed to a stub that will throw AbstractMethodError if they are called
495 ByteArray* orig_ame_stub_array_ = Runtime::Current()->GetAbstractMethodErrorStubArray();
496 ByteArray* copy_ame_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_ame_stub_array_));
497 copy->code_ = copy_ame_stub_array_->GetData();
498 return;
499 }
500
Ian Rogers19846512012-02-24 11:42:47 -0800501 if (orig == Runtime::Current()->GetResolutionMethod()) {
502 // The resolution stub's code points at the unknown resolution trampoline
503 ByteArray* orig_res_stub_array_ =
504 Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod);
505 CHECK(orig->GetCode() == orig_res_stub_array_->GetData());
506 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
507 copy->code_ = copy_res_stub_array_->GetData();
508 return;
509 }
510
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700511 // Non-abstract methods typically have code
512 uint32_t code_offset = orig->GetOatCodeOffset();
Ian Rogers19846512012-02-24 11:42:47 -0800513 const byte* code = NULL;
514 if (orig->IsStatic()) {
515 // Static methods may point at the resolution trampoline stub
516 ByteArray* orig_res_stub_array_ =
517 Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod);
518 if (reinterpret_cast<int8_t*>(code_offset) == orig_res_stub_array_->GetData()) {
519 ByteArray* copy_res_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_res_stub_array_));
520 code = reinterpret_cast<const byte*>(copy_res_stub_array_->GetData());
521 }
522 }
523 if (code == NULL) {
524 code = GetOatAddress(code_offset);
525 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700526 copy->code_ = code;
527
Brian Carlstrom16192862011-09-12 17:50:06 -0700528 if (orig->IsNative()) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700529 // The native method's pointer is directed to a stub to lookup via dlsym.
530 // Note this is not the code_ pointer, that is handled above.
Ian Rogers169c9a72011-11-13 20:13:17 -0800531 ByteArray* orig_jni_stub_array_ = Runtime::Current()->GetJniDlsymLookupStub();
Brian Carlstrom16192862011-09-12 17:50:06 -0700532 ByteArray* copy_jni_stub_array_ = down_cast<ByteArray*>(GetImageAddress(orig_jni_stub_array_));
533 copy->native_method_ = copy_jni_stub_array_->GetData();
534 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535 // normal (non-abstract non-native) methods have mapping tables to relocate
536 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800537 const byte* mapping_table = GetOatAddress(mapping_table_off);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700538 copy->mapping_table_ = reinterpret_cast<const uint32_t*>(mapping_table);
539
540 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
Brian Carlstromae826982011-11-09 01:33:42 -0800541 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700542 copy->vmap_table_ = reinterpret_cast<const uint16_t*>(vmap_table);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800543
Ian Rogers0c7abda2012-09-19 13:33:42 -0700544 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
545 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
546 copy->native_gc_map_ = reinterpret_cast<const uint8_t*>(native_gc_map);
Brian Carlstrom16192862011-09-12 17:50:06 -0700547 }
Brian Carlstroma663ea52011-08-19 23:33:41 -0700548}
549
Brian Carlstrom4873d462011-08-21 15:23:39 -0700550void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700551 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700552 const Object* element = orig->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700553 copy->SetWithoutChecks(i, GetImageAddress(element));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700554 }
555}
556
Brian Carlstrom4873d462011-08-21 15:23:39 -0700557void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
558 DCHECK(orig != NULL);
559 DCHECK(copy != NULL);
560 Class* klass = orig->GetClass();
561 DCHECK(klass != NULL);
562 FixupFields(orig,
563 copy,
564 klass->GetReferenceInstanceOffsets(),
565 false);
566}
567
568void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
569 DCHECK(orig != NULL);
570 DCHECK(copy != NULL);
571 FixupFields(orig,
572 copy,
573 orig->GetReferenceStaticOffsets(),
574 true);
575}
576
577void ImageWriter::FixupFields(const Object* orig,
578 Object* copy,
579 uint32_t ref_offsets,
580 bool is_static) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700581 if (ref_offsets != CLASS_WALK_SUPER) {
582 // Found a reference offset bitmap. Fixup the specified offsets.
583 while (ref_offsets != 0) {
584 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700585 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
586 const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
587 copy->SetFieldObject(byte_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700588 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
589 }
590 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700591 // There is no reference offset bitmap. In the non-static case,
592 // walk up the class inheritance hierarchy and find reference
593 // offsets the hard way. In the static case, just consider this
594 // class.
595 for (const Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700596 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700597 klass = is_static ? NULL : klass->GetSuperClass()) {
598 size_t num_reference_fields = (is_static
599 ? klass->NumReferenceStaticFields()
600 : klass->NumReferenceInstanceFields());
601 for (size_t i = 0; i < num_reference_fields; ++i) {
602 Field* field = (is_static
603 ? klass->GetStaticField(i)
604 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700605 MemberOffset field_offset = field->GetOffset();
606 const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
607 copy->SetFieldObject(field_offset, GetImageAddress(ref), false);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700608 }
609 }
610 }
611}
612
Mathieu Chartier66f19252012-09-18 08:57:04 -0700613static AbstractMethod* GetReferrerMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700614 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 ScopedObjectAccessUnchecked soa(Thread::Current());
Brian Carlstromf5822582012-03-19 22:34:31 -0700616 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700618 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700619 patch->GetReferrerMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700621 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700622 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700623 patch->GetReferrerInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700624 CHECK(method != NULL)
625 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700626 CHECK(!method->IsRuntimeMethod())
627 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetReferrerMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700629 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetReferrerMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700631 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700632 return method;
633}
634
Mathieu Chartier66f19252012-09-18 08:57:04 -0700635static AbstractMethod* GetTargetMethod(const Compiler::PatchInformation* patch)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700636 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700637 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700639 AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700640 patch->GetTargetMethodIdx(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 dex_cache,
Brian Carlstromf5822582012-03-19 22:34:31 -0700642 NULL,
jeffhaoc0228b82012-08-29 18:15:05 -0700643 NULL,
Ian Rogers08f753d2012-08-24 14:35:25 -0700644 patch->GetTargetInvokeType());
Brian Carlstromf5822582012-03-19 22:34:31 -0700645 CHECK(method != NULL)
646 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom0637e272012-03-20 01:07:52 -0700647 CHECK(!method->IsRuntimeMethod())
648 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Brian Carlstrom0637e272012-03-20 01:07:52 -0700650 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
Brian Carlstrom0637e272012-03-20 01:07:52 -0700652 << PrettyMethod(method);
Brian Carlstromf5822582012-03-19 22:34:31 -0700653 return method;
654}
655
656void ImageWriter::PatchOatCodeAndMethods(const Compiler& compiler) {
657 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
658
659 const std::vector<const Compiler::PatchInformation*>& code_to_patch = compiler.GetCodeToPatch();
660 for (size_t i = 0; i < code_to_patch.size(); i++) {
661 const Compiler::PatchInformation* patch = code_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700662 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700663 uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
664 uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
665 uint32_t code_offset = code - code_base;
666 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
667 }
668
669 const std::vector<const Compiler::PatchInformation*>& methods_to_patch
670 = compiler.GetMethodsToPatch();
671 for (size_t i = 0; i < methods_to_patch.size(); i++) {
672 const Compiler::PatchInformation* patch = methods_to_patch[i];
Mathieu Chartier66f19252012-09-18 08:57:04 -0700673 AbstractMethod* target = GetTargetMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700674 SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
675 }
676}
677
678void ImageWriter::SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) {
679 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700680 AbstractMethod* method = GetReferrerMethod(patch);
Brian Carlstromf5822582012-03-19 22:34:31 -0700681 // Goodbye const, we are about to modify some code.
682 void* code = const_cast<void*>(class_linker->GetOatCodeFor(method));
683 // TODO: make this Thumb2 specific
684 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(code) & ~0x1);
685 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
686#ifndef NDEBUG
687 const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
688 uint32_t expected = reinterpret_cast<uint32_t>(&id);
689 uint32_t actual = *patch_location;
690 CHECK(actual == expected || actual == value) << std::hex
691 << "actual=" << actual
692 << "expected=" << expected
693 << "value=" << value;
694#endif
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700695 *patch_location = value;
Brian Carlstromf5822582012-03-19 22:34:31 -0700696}
697
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700698} // namespace art