Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 3 | #ifndef ART_SRC_IMAGE_WRITER_H_ |
| 4 | #define ART_SRC_IMAGE_WRITER_H_ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 5 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 8 | #include <cstddef> |
| 9 | |
| 10 | #include "UniquePtr.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 11 | #include "dex_cache.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 12 | #include "mem_map.h" |
| 13 | #include "object.h" |
| 14 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 15 | #include "space.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
| 17 | namespace art { |
| 18 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 19 | // Write a Space built during compilation for use during execution. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 20 | class ImageWriter { |
| 21 | |
| 22 | public: |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 23 | ImageWriter() : source_space_(NULL), image_top_(0), image_base_(NULL) {}; |
| 24 | bool Write(const char* filename, uintptr_t image_base); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 25 | ~ImageWriter() {}; |
| 26 | |
| 27 | private: |
| 28 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 29 | bool Init(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 30 | |
| 31 | // we use the lock word to store the offset of the object in the image |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 32 | static void SetImageOffset(Object* object, size_t offset) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 33 | DCHECK(object != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 34 | DCHECK(object->GetMonitor() == NULL); // should be no lock |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 35 | DCHECK_NE(0U, offset); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 36 | object->SetMonitor(reinterpret_cast<Monitor*>(offset)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 37 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 38 | static size_t GetImageOffset(const Object* object) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 39 | DCHECK(object != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 40 | size_t offset = reinterpret_cast<size_t>(object->GetMonitor()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 41 | DCHECK_NE(0U, offset); |
| 42 | return offset; |
| 43 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 44 | static void ResetImageOffset(Object* object) { |
| 45 | DCHECK(object != NULL); |
| 46 | DCHECK(object->GetMonitor() != NULL); // should be an offset |
| 47 | object->SetMonitor(reinterpret_cast<Monitor*>(0)); |
| 48 | } |
| 49 | |
| 50 | bool InSourceSpace(const Object* object) { |
| 51 | DCHECK(source_space_ != NULL); |
| 52 | const byte* o = reinterpret_cast<const byte*>(object); |
| 53 | return (o >= source_space_->GetBase() && o < source_space_->GetLimit()); |
| 54 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 55 | Object* GetImageAddress(const Object* object) { |
| 56 | if (object == NULL) { |
| 57 | return NULL; |
| 58 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 59 | // if object outside the relocating source_space_, assume unchanged |
| 60 | if (!InSourceSpace(object)) { |
| 61 | return const_cast<Object*>(object); |
| 62 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 63 | return reinterpret_cast<Object*>(image_base_ + GetImageOffset(object)); |
| 64 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 65 | Object* GetLocalAddress(const Object* object) { |
| 66 | size_t offset = GetImageOffset(object); |
| 67 | byte* dst = image_->GetAddress() + offset; |
| 68 | return reinterpret_cast<Object*>(dst); |
| 69 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 70 | |
| 71 | void CalculateNewObjectOffsets(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 72 | static void CalculateNewObjectOffsetsCallback(Object* obj, void *arg); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 73 | |
| 74 | void CopyAndFixupObjects(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 75 | static void CopyAndFixupObjectsCallback(Object* obj, void *arg); |
| 76 | void FixupClass(const Class* orig, Class* copy); |
| 77 | void FixupMethod(const Method* orig, Method* copy); |
| 78 | void FixupField(const Field* orig, Field* copy); |
| 79 | void FixupObject(const Object* orig, Object* copy); |
| 80 | void FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy); |
| 81 | void FixupInstanceFields(const Object* orig, Object* copy); |
| 82 | void FixupStaticFields(const Class* orig, Class* copy); |
| 83 | void FixupFields(const Object* orig, Object* copy, uint32_t ref_offsets, bool is_static); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 84 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 85 | void FixupDexCaches(); |
| 86 | void FixupDexCache(const DexCache* orig, DexCache* copy); |
| 87 | |
| 88 | // Space we are writing objects from |
| 89 | const Space* source_space_; |
| 90 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 91 | // memory mapped for generating the image |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 92 | UniquePtr<MemMap> image_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 93 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 94 | // Offset to the free space in image_ |
| 95 | size_t image_top_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 96 | |
| 97 | // Target base address for the output image |
| 98 | byte* image_base_; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame^] | 99 | |
| 100 | // DexCaches seen while scanning for fixing up CodeAndDirectMethods |
| 101 | typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set; |
| 102 | Set dex_caches_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace art |
| 106 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 107 | #endif // ART_SRC_IMAGE_WRITER_H_ |