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