image_writer.cc revision c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899
1/*
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 */
16
17#include "image_writer.h"
18
19#include <sys/stat.h>
20
21#include <vector>
22
23#include "base/logging.h"
24#include "base/unix_file/fd_file.h"
25#include "class_linker.h"
26#include "compiled_method.h"
27#include "dex_file-inl.h"
28#include "driver/compiler_driver.h"
29#include "elf_writer.h"
30#include "gc/accounting/card_table-inl.h"
31#include "gc/accounting/heap_bitmap.h"
32#include "gc/heap.h"
33#include "gc/space/large_object_space.h"
34#include "gc/space/space-inl.h"
35#include "globals.h"
36#include "image.h"
37#include "intern_table.h"
38#include "mirror/array-inl.h"
39#include "mirror/class-inl.h"
40#include "mirror/class_loader.h"
41#include "mirror/dex_cache-inl.h"
42#include "mirror/field-inl.h"
43#include "mirror/abstract_method-inl.h"
44#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
46#include "oat.h"
47#include "oat_file.h"
48#include "object_utils.h"
49#include "runtime.h"
50#include "scoped_thread_state_change.h"
51#include "sirt_ref.h"
52#include "UniquePtr.h"
53#include "utils.h"
54
55using ::art::mirror::AbstractMethod;
56using ::art::mirror::Class;
57using ::art::mirror::DexCache;
58using ::art::mirror::EntryPointFromInterpreter;
59using ::art::mirror::Field;
60using ::art::mirror::Object;
61using ::art::mirror::ObjectArray;
62using ::art::mirror::String;
63
64namespace art {
65
66bool ImageWriter::Write(const std::string& image_filename,
67                        uintptr_t image_begin,
68                        const std::string& oat_filename,
69                        const std::string& oat_location) {
70  CHECK(!image_filename.empty());
71
72  CHECK_NE(image_begin, 0U);
73  image_begin_ = reinterpret_cast<byte*>(image_begin);
74
75  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
76  const std::vector<DexCache*>& all_dex_caches = class_linker->GetDexCaches();
77  for (size_t i = 0; i < all_dex_caches.size(); i++) {
78    DexCache* dex_cache = all_dex_caches[i];
79    dex_caches_.insert(dex_cache);
80  }
81
82  UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false));
83  if (oat_file.get() == NULL) {
84    LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
85    return false;
86  }
87  oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location);
88  if (oat_file_ == NULL) {
89    LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location;
90    return false;
91  }
92  class_linker->RegisterOatFile(*oat_file_);
93  interpreter_to_interpreter_entry_offset_ = oat_file_->GetOatHeader().GetInterpreterToInterpreterEntryOffset();
94  interpreter_to_quick_entry_offset_ = oat_file_->GetOatHeader().GetInterpreterToQuickEntryOffset();
95  portable_resolution_trampoline_offset_ = oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset();
96  quick_resolution_trampoline_offset_ = oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset();
97
98  {
99    Thread::Current()->TransitionFromSuspendedToRunnable();
100    PruneNonImageClasses();  // Remove junk
101    ComputeLazyFieldsForImageClasses();  // Add useful information
102    ComputeEagerResolvedStrings();
103    Thread::Current()->TransitionFromRunnableToSuspended(kNative);
104  }
105  gc::Heap* heap = Runtime::Current()->GetHeap();
106  heap->CollectGarbage(false);  // Remove garbage.
107  // Trim size of alloc spaces.
108  const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
109  // TODO: C++0x auto
110  typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
111  for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
112    gc::space::ContinuousSpace* space = *it;
113    if (space->IsDlMallocSpace()) {
114      space->AsDlMallocSpace()->Trim();
115    }
116  }
117
118  if (!AllocMemory()) {
119    return false;
120  }
121#ifndef NDEBUG
122  { // NOLINT(whitespace/braces)
123    ScopedObjectAccess soa(Thread::Current());
124    CheckNonImageClassesRemoved();
125  }
126#endif
127  Thread::Current()->TransitionFromSuspendedToRunnable();
128  size_t oat_loaded_size = 0;
129  size_t oat_data_offset = 0;
130  ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
131  CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
132  CopyAndFixupObjects();
133  PatchOatCodeAndMethods();
134  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
135
136  UniquePtr<File> image_file(OS::OpenFile(image_filename.c_str(), true));
137  if (image_file.get() == NULL) {
138    LOG(ERROR) << "Failed to open image file " << image_filename;
139    return false;
140  }
141  if (fchmod(image_file->Fd(), 0644) != 0) {
142    PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
143    return EXIT_FAILURE;
144  }
145  bool success = image_file->WriteFully(image_->Begin(), image_end_);
146  if (!success) {
147    PLOG(ERROR) << "Failed to write image file " << image_filename;
148    return false;
149  }
150  return true;
151}
152
153bool ImageWriter::AllocMemory() {
154  gc::Heap* heap = Runtime::Current()->GetHeap();
155  const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
156  size_t size = 0;
157  // TODO: C++0x auto
158  typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
159  for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
160    gc::space::ContinuousSpace* space = *it;
161    if (space->IsDlMallocSpace()) {
162      size += space->Size();
163    }
164  }
165
166  int prot = PROT_READ | PROT_WRITE;
167  size_t length = RoundUp(size, kPageSize);
168  image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, prot));
169  if (image_.get() == NULL) {
170    LOG(ERROR) << "Failed to allocate memory for image file generation";
171    return false;
172  }
173  return true;
174}
175
176void ImageWriter::ComputeLazyFieldsForImageClasses() {
177  Runtime* runtime = Runtime::Current();
178  ClassLinker* class_linker = runtime->GetClassLinker();
179  class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
180}
181
182bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
183  c->ComputeName();
184  return true;
185}
186
187void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
188  if (!obj->GetClass()->IsStringClass()) {
189    return;
190  }
191  String* string = obj->AsString();
192  const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset();
193  ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
194  typedef Set::const_iterator CacheIt;  // TODO: C++0x auto
195  for (CacheIt it = writer->dex_caches_.begin(), end = writer->dex_caches_.end(); it != end; ++it) {
196    DexCache* dex_cache = *it;
197    const DexFile& dex_file = *dex_cache->GetDexFile();
198    const DexFile::StringId* string_id = dex_file.FindStringId(utf16_string);
199    if (string_id != NULL) {
200      // This string occurs in this dex file, assign the dex cache entry.
201      uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
202      if (dex_cache->GetResolvedString(string_idx) == NULL) {
203        dex_cache->SetResolvedString(string_idx, string);
204      }
205    }
206  }
207}
208
209void ImageWriter::ComputeEagerResolvedStrings()
210    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
211  // TODO: Check image spaces only?
212  gc::Heap* heap = Runtime::Current()->GetHeap();
213  WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
214  heap->FlushAllocStack();
215  heap->GetLiveBitmap()->Walk(ComputeEagerResolvedStringsCallback, this);
216}
217
218bool ImageWriter::IsImageClass(const Class* klass) {
219  return compiler_driver_.IsImageClass(ClassHelper(klass).GetDescriptor());
220}
221
222struct NonImageClasses {
223  ImageWriter* image_writer;
224  std::set<std::string>* non_image_classes;
225};
226
227void ImageWriter::PruneNonImageClasses() {
228  if (compiler_driver_.GetImageClasses() == NULL) {
229    return;
230  }
231  Runtime* runtime = Runtime::Current();
232  ClassLinker* class_linker = runtime->GetClassLinker();
233
234  // Make a list of classes we would like to prune.
235  std::set<std::string> non_image_classes;
236  NonImageClasses context;
237  context.image_writer = this;
238  context.non_image_classes = &non_image_classes;
239  class_linker->VisitClasses(NonImageClassesVisitor, &context);
240
241  // Remove the undesired classes from the class roots.
242  typedef std::set<std::string>::const_iterator ClassIt;  // TODO: C++0x auto
243  for (ClassIt it = non_image_classes.begin(), end = non_image_classes.end(); it != end; ++it) {
244    class_linker->RemoveClass((*it).c_str(), NULL);
245  }
246
247  // Clear references to removed classes from the DexCaches.
248  AbstractMethod* resolution_method = runtime->GetResolutionMethod();
249  typedef Set::const_iterator CacheIt;  // TODO: C++0x auto
250  for (CacheIt it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it) {
251    DexCache* dex_cache = *it;
252    for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
253      Class* klass = dex_cache->GetResolvedType(i);
254      if (klass != NULL && !IsImageClass(klass)) {
255        dex_cache->SetResolvedType(i, NULL);
256        dex_cache->GetInitializedStaticStorage()->Set(i, NULL);
257      }
258    }
259    for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
260      AbstractMethod* method = dex_cache->GetResolvedMethod(i);
261      if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
262        dex_cache->SetResolvedMethod(i, resolution_method);
263      }
264    }
265    for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
266      Field* field = dex_cache->GetResolvedField(i);
267      if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
268        dex_cache->SetResolvedField(i, NULL);
269      }
270    }
271  }
272}
273
274bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
275  NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
276  if (!context->image_writer->IsImageClass(klass)) {
277    context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
278  }
279  return true;
280}
281
282void ImageWriter::CheckNonImageClassesRemoved()
283    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
284  if (compiler_driver_.GetImageClasses() == NULL) {
285    return;
286  }
287
288  gc::Heap* heap = Runtime::Current()->GetHeap();
289  Thread* self = Thread::Current();
290  {
291    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
292    heap->FlushAllocStack();
293  }
294
295  ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
296  heap->GetLiveBitmap()->Walk(CheckNonImageClassesRemovedCallback, this);
297}
298
299void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
300  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
301  if (!obj->IsClass()) {
302    return;
303  }
304  Class* klass = obj->AsClass();
305  if (!image_writer->IsImageClass(klass)) {
306    image_writer->DumpImageClasses();
307    CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
308                                             << " " << PrettyDescriptor(klass);
309  }
310}
311
312void ImageWriter::DumpImageClasses() {
313  CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses();
314  CHECK(image_classes != NULL);
315  typedef std::set<std::string>::const_iterator It;  // TODO: C++0x auto
316  for (It it = image_classes->begin(), end = image_classes->end(); it != end; ++it) {
317    LOG(INFO) << " " << *it;
318  }
319}
320
321void ImageWriter::CalculateNewObjectOffsetsCallback(Object* obj, void* arg) {
322  DCHECK(obj != NULL);
323  DCHECK(arg != NULL);
324  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
325
326  // if it is a string, we want to intern it if its not interned.
327  if (obj->GetClass()->IsStringClass()) {
328    // we must be an interned string that was forward referenced and already assigned
329    if (image_writer->IsImageOffsetAssigned(obj)) {
330      DCHECK_EQ(obj, obj->AsString()->Intern());
331      return;
332    }
333    SirtRef<String> interned(Thread::Current(), obj->AsString()->Intern());
334    if (obj != interned.get()) {
335      if (!image_writer->IsImageOffsetAssigned(interned.get())) {
336        // interned obj is after us, allocate its location early
337        image_writer->AssignImageOffset(interned.get());
338      }
339      // point those looking for this object to the interned version.
340      image_writer->SetImageOffset(obj, image_writer->GetImageOffset(interned.get()));
341      return;
342    }
343    // else (obj == interned), nothing to do but fall through to the normal case
344  }
345
346  image_writer->AssignImageOffset(obj);
347}
348
349ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
350  Runtime* runtime = Runtime::Current();
351  ClassLinker* class_linker = runtime->GetClassLinker();
352  Class* object_array_class = class_linker->FindSystemClass("[Ljava/lang/Object;");
353  Thread* self = Thread::Current();
354
355  // build an Object[] of all the DexCaches used in the source_space_
356  ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class,
357                                                               dex_caches_.size());
358  int i = 0;
359  typedef Set::const_iterator It;  // TODO: C++0x auto
360  for (It it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ++it, ++i) {
361    dex_caches->Set(i, *it);
362  }
363
364  // build an Object[] of the roots needed to restore the runtime
365  SirtRef<ObjectArray<Object> >
366      image_roots(self,
367                  ObjectArray<Object>::Alloc(self, object_array_class,
368                                             ImageHeader::kImageRootsMax));
369  image_roots->Set(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
370  image_roots->Set(ImageHeader::kCalleeSaveMethod,
371                   runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
372  image_roots->Set(ImageHeader::kRefsOnlySaveMethod,
373                   runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
374  image_roots->Set(ImageHeader::kRefsAndArgsSaveMethod,
375                   runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
376  image_roots->Set(ImageHeader::kOatLocation,
377                   String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str()));
378  image_roots->Set(ImageHeader::kDexCaches,
379                   dex_caches);
380  image_roots->Set(ImageHeader::kClassRoots,
381                   class_linker->GetClassRoots());
382  for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
383    CHECK(image_roots->Get(i) != NULL);
384  }
385  return image_roots.get();
386}
387
388void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
389  CHECK_NE(0U, oat_loaded_size);
390  Thread* self = Thread::Current();
391  SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
392
393  gc::Heap* heap = Runtime::Current()->GetHeap();
394  const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
395  DCHECK(!spaces.empty());
396  DCHECK_EQ(0U, image_end_);
397
398  // leave space for the header, but do not write it yet, we need to
399  // know where image_roots is going to end up
400  image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
401
402  {
403    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
404    heap->FlushAllocStack();
405    // TODO: Image spaces only?
406    // TODO: Add InOrderWalk to heap bitmap.
407    const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
408    DCHECK(heap->GetLargeObjectsSpace()->GetLiveObjects()->IsEmpty());
409    // TODO: C++0x auto
410    typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
411    for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
412      gc::space::ContinuousSpace* space = *it;
413      space->GetLiveBitmap()->InOrderWalk(CalculateNewObjectOffsetsCallback, this);
414      DCHECK_LT(image_end_, image_->Size());
415    }
416    self->EndAssertNoThreadSuspension(old);
417  }
418
419  const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
420  const byte* oat_file_end = oat_file_begin + oat_loaded_size;
421  oat_data_begin_ = oat_file_begin + oat_data_offset;
422  const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
423
424  // return to write header at start of image with future location of image_roots
425  ImageHeader image_header(reinterpret_cast<uint32_t>(image_begin_),
426                           reinterpret_cast<uint32_t>(GetImageAddress(image_roots.get())),
427                           oat_file_->GetOatHeader().GetChecksum(),
428                           reinterpret_cast<uint32_t>(oat_file_begin),
429                           reinterpret_cast<uint32_t>(oat_data_begin_),
430                           reinterpret_cast<uint32_t>(oat_data_end),
431                           reinterpret_cast<uint32_t>(oat_file_end));
432  memcpy(image_->Begin(), &image_header, sizeof(image_header));
433
434  // Note that image_end_ is left at end of used space
435}
436
437void ImageWriter::CopyAndFixupObjects()
438    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
439  Thread* self = Thread::Current();
440  const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
441  gc::Heap* heap = Runtime::Current()->GetHeap();
442  // TODO: heap validation can't handle this fix up pass
443  heap->DisableObjectValidation();
444  // TODO: Image spaces only?
445  WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
446  heap->FlushAllocStack();
447  heap->GetLiveBitmap()->Walk(CopyAndFixupObjectsCallback, this);
448  self->EndAssertNoThreadSuspension(old_cause);
449}
450
451void ImageWriter::CopyAndFixupObjectsCallback(Object* object, void* arg) {
452  DCHECK(object != NULL);
453  DCHECK(arg != NULL);
454  const Object* obj = object;
455  ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
456
457  // see GetLocalAddress for similar computation
458  size_t offset = image_writer->GetImageOffset(obj);
459  byte* dst = image_writer->image_->Begin() + offset;
460  const byte* src = reinterpret_cast<const byte*>(obj);
461  size_t n = obj->SizeOf();
462  DCHECK_LT(offset + n, image_writer->image_->Size());
463  memcpy(dst, src, n);
464  Object* copy = reinterpret_cast<Object*>(dst);
465  copy->SetField32(Object::MonitorOffset(), 0, false); // We may have inflated the lock during compilation.
466  image_writer->FixupObject(obj, copy);
467}
468
469void ImageWriter::FixupObject(const Object* orig, Object* copy) {
470  DCHECK(orig != NULL);
471  DCHECK(copy != NULL);
472  copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass())));
473  // TODO: special case init of pointers to malloc data (or removal of these pointers)
474  if (orig->IsClass()) {
475    FixupClass(orig->AsClass(), down_cast<Class*>(copy));
476  } else if (orig->IsObjectArray()) {
477    FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy));
478  } else if (orig->IsMethod()) {
479    FixupMethod(orig->AsMethod(), down_cast<AbstractMethod*>(copy));
480  } else {
481    FixupInstanceFields(orig, copy);
482  }
483}
484
485void ImageWriter::FixupClass(const Class* orig, Class* copy) {
486  FixupInstanceFields(orig, copy);
487  FixupStaticFields(orig, copy);
488}
489
490void ImageWriter::FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) {
491  FixupInstanceFields(orig, copy);
492
493  // OatWriter replaces the code_ with an offset value.
494  // Here we readjust to a pointer relative to oat_begin_
495  if (orig->IsAbstract()) {
496    // Code for abstract methods is set to the abstract method error stub when we load the image.
497    copy->SetEntryPointFromCompiledCode(NULL);
498    copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
499                                       (GetOatAddress(interpreter_to_interpreter_entry_offset_)));
500    return;
501  } else {
502    copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*>
503                                       (GetOatAddress(interpreter_to_quick_entry_offset_)));
504  }
505
506  if (orig == Runtime::Current()->GetResolutionMethod()) {
507#if defined(ART_USE_PORTABLE_COMPILER)
508    copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
509#else
510    copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
511#endif
512    return;
513  }
514
515  // Use original code if it exists. Otherwise, set the code pointer to the resolution trampoline.
516  const byte* code = GetOatAddress(orig->GetOatCodeOffset());
517  if (code != NULL) {
518    copy->SetEntryPointFromCompiledCode(code);
519  } else {
520#if defined(ART_USE_PORTABLE_COMPILER)
521    copy->SetEntryPointFromCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_));
522#else
523    copy->SetEntryPointFromCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_));
524#endif
525  }
526
527  if (orig->IsNative()) {
528    // The native method's pointer is set to a stub to lookup via dlsym when we load the image.
529    // Note this is not the code_ pointer, that is handled above.
530    copy->SetNativeMethod(NULL);
531  } else {
532    // normal (non-abstract non-native) methods have mapping tables to relocate
533    uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
534    const byte* mapping_table = GetOatAddress(mapping_table_off);
535    copy->SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table));
536
537    uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
538    const byte* vmap_table = GetOatAddress(vmap_table_offset);
539    copy->SetVmapTable(reinterpret_cast<const uint16_t*>(vmap_table));
540
541    uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
542    const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
543    copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map));
544  }
545}
546
547void ImageWriter::FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
548  for (int32_t i = 0; i < orig->GetLength(); ++i) {
549    const Object* element = orig->Get(i);
550    copy->SetPtrWithoutChecks(i, GetImageAddress(element));
551  }
552}
553
554void ImageWriter::FixupInstanceFields(const Object* orig, Object* copy) {
555  DCHECK(orig != NULL);
556  DCHECK(copy != NULL);
557  Class* klass = orig->GetClass();
558  DCHECK(klass != NULL);
559  FixupFields(orig,
560              copy,
561              klass->GetReferenceInstanceOffsets(),
562              false);
563}
564
565void ImageWriter::FixupStaticFields(const Class* orig, Class* copy) {
566  DCHECK(orig != NULL);
567  DCHECK(copy != NULL);
568  FixupFields(orig,
569              copy,
570              orig->GetReferenceStaticOffsets(),
571              true);
572}
573
574void ImageWriter::FixupFields(const Object* orig,
575                              Object* copy,
576                              uint32_t ref_offsets,
577                              bool is_static) {
578  if (ref_offsets != CLASS_WALK_SUPER) {
579    // Found a reference offset bitmap.  Fixup the specified offsets.
580    while (ref_offsets != 0) {
581      size_t right_shift = CLZ(ref_offsets);
582      MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
583      const Object* ref = orig->GetFieldObject<const Object*>(byte_offset, false);
584      // Use SetFieldPtr to avoid card marking since we are writing to the image.
585      copy->SetFieldPtr(byte_offset, GetImageAddress(ref), false);
586      ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
587    }
588  } else {
589    // 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();
594         klass != NULL;
595         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));
603        MemberOffset field_offset = field->GetOffset();
604        const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
605        // Use SetFieldPtr to avoid card marking since we are writing to the image.
606        copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
607      }
608    }
609  }
610  if (!is_static && orig->IsReferenceInstance()) {
611    // Fix-up referent, that isn't marked as an object field, for References.
612    Field* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
613    MemberOffset field_offset = field->GetOffset();
614    const Object* ref = orig->GetFieldObject<const Object*>(field_offset, false);
615    // Use SetFieldPtr to avoid card marking since we are writing to the image.
616    copy->SetFieldPtr(field_offset, GetImageAddress(ref), false);
617  }
618}
619
620static AbstractMethod* GetTargetMethod(const CompilerDriver::PatchInformation* patch)
621    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
622  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
623  DexCache* dex_cache = class_linker->FindDexCache(patch->GetDexFile());
624  AbstractMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
625                                               patch->GetTargetMethodIdx(),
626                                               dex_cache,
627                                               NULL,
628                                               NULL,
629                                               patch->GetTargetInvokeType());
630  CHECK(method != NULL)
631    << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
632  CHECK(!method->IsRuntimeMethod())
633    << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
634  CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
635    << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
636    << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
637    << PrettyMethod(method);
638  return method;
639}
640
641void ImageWriter::PatchOatCodeAndMethods() {
642  Thread* self = Thread::Current();
643  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
644  const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
645
646  typedef std::vector<const CompilerDriver::PatchInformation*> Patches;
647  const Patches& code_to_patch = compiler_driver_.GetCodeToPatch();
648  for (size_t i = 0; i < code_to_patch.size(); i++) {
649    const CompilerDriver::PatchInformation* patch = code_to_patch[i];
650    AbstractMethod* target = GetTargetMethod(patch);
651    uint32_t code = reinterpret_cast<uint32_t>(class_linker->GetOatCodeFor(target));
652    uint32_t code_base = reinterpret_cast<uint32_t>(&oat_file_->GetOatHeader());
653    uint32_t code_offset = code - code_base;
654    SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetOatAddress(code_offset)));
655  }
656
657  const Patches& methods_to_patch = compiler_driver_.GetMethodsToPatch();
658  for (size_t i = 0; i < methods_to_patch.size(); i++) {
659    const CompilerDriver::PatchInformation* patch = methods_to_patch[i];
660    AbstractMethod* target = GetTargetMethod(patch);
661    SetPatchLocation(patch, reinterpret_cast<uint32_t>(GetImageAddress(target)));
662  }
663
664  // Update the image header with the new checksum after patching
665  ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
666  image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
667  self->EndAssertNoThreadSuspension(old_cause);
668}
669
670void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
671  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
672  const void* oat_code = class_linker->GetOatCodeFor(patch->GetDexFile(),
673                                                     patch->GetReferrerMethodIdx());
674  OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
675  // TODO: make this Thumb2 specific
676  uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uint32_t>(oat_code) & ~0x1);
677  uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
678#ifndef NDEBUG
679  const DexFile::MethodId& id = patch->GetDexFile().GetMethodId(patch->GetTargetMethodIdx());
680  uint32_t expected = reinterpret_cast<uint32_t>(&id);
681  uint32_t actual = *patch_location;
682  CHECK(actual == expected || actual == value) << std::hex
683    << "actual=" << actual
684    << "expected=" << expected
685    << "value=" << value;
686#endif
687  *patch_location = value;
688  oat_header.UpdateChecksum(patch_location, sizeof(value));
689}
690
691}  // namespace art
692