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> |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 11 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 12 | #include "dex_cache.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 13 | #include "mem_map.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 14 | #include "oat_file.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 15 | #include "object.h" |
| 16 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 17 | #include "space.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 18 | #include "UniquePtr.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 22 | // Write a Space built during compilation for use during execution. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 23 | class ImageWriter { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 24 | public: |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame^] | 25 | explicit ImageWriter(const std::set<std::string>* image_classes) |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 26 | : source_space_(NULL), image_top_(0), image_base_(NULL), image_classes_(image_classes) {} |
| 27 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 28 | ~ImageWriter() {} |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 30 | bool Write(const char* image_filename, |
| 31 | uintptr_t image_base, |
| 32 | const std::string& oat_filename, |
| 33 | const std::string& strip_location_prefix); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 34 | private: |
| 35 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 36 | bool AllocMemory(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 37 | |
| 38 | // we use the lock word to store the offset of the object in the image |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 39 | void AssignImageOffset(Object* object) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 40 | DCHECK(object != NULL); |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 41 | DCHECK_EQ(object->monitor_, 0U); // should be no lock |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 42 | SetImageOffset(object, image_top_); |
| 43 | image_top_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment |
| 44 | DCHECK_LT(image_top_, image_->GetLength()); |
| 45 | } |
| 46 | static void SetImageOffset(Object* object, size_t offset) { |
| 47 | DCHECK(object != NULL); |
| 48 | // should be no lock (but it might be forward referenced interned string) |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 49 | DCHECK(object->monitor_ == 0 || object->GetClass()->IsStringClass()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 50 | DCHECK_NE(0U, offset); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 51 | object->monitor_ = offset; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 52 | } |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 53 | static size_t IsImageOffsetAssigned(const Object* object) { |
| 54 | DCHECK(object != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 55 | size_t offset = object->monitor_; |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 56 | return offset != 0U; |
| 57 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 58 | static size_t GetImageOffset(const Object* object) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 59 | DCHECK(object != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 60 | size_t offset = object->monitor_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 61 | DCHECK_NE(0U, offset); |
| 62 | return offset; |
| 63 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 64 | static void ResetImageOffset(Object* object) { |
| 65 | DCHECK(object != NULL); |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 66 | DCHECK_NE(object->monitor_, 0U); // should be an offset |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 67 | object->monitor_ = 0; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 70 | bool InSourceSpace(const Object* object) const { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 71 | DCHECK(source_space_ != NULL); |
| 72 | const byte* o = reinterpret_cast<const byte*>(object); |
| 73 | return (o >= source_space_->GetBase() && o < source_space_->GetLimit()); |
| 74 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 75 | Object* GetImageAddress(const Object* object) const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 76 | if (object == NULL) { |
| 77 | return NULL; |
| 78 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 79 | // if object outside the relocating source_space_, assume unchanged |
| 80 | if (!InSourceSpace(object)) { |
| 81 | return const_cast<Object*>(object); |
| 82 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 83 | return reinterpret_cast<Object*>(image_base_ + GetImageOffset(object)); |
| 84 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 85 | Object* GetLocalAddress(const Object* object) const { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 86 | size_t offset = GetImageOffset(object); |
| 87 | byte* dst = image_->GetAddress() + offset; |
| 88 | return reinterpret_cast<Object*>(dst); |
| 89 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 90 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 91 | const byte* GetOatAddress(uint32_t offset) const { |
| 92 | DCHECK_LT(offset, oat_file_->GetSize()); |
| 93 | if (offset == 0) { |
| 94 | return NULL; |
| 95 | } |
| 96 | return oat_base_ + offset; |
| 97 | } |
| 98 | |
| 99 | bool IsImageClass(const Class* klass); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 100 | void DumpImageClasses(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 101 | |
| 102 | void PruneNonImageClasses(); |
| 103 | static bool NonImageClassesVisitor(Class* c, void* arg); |
| 104 | |
| 105 | void CheckNonImageClassesRemoved(); |
| 106 | static void CheckNonImageClassesRemovedCallback(Object* obj, void* arg); |
| 107 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 108 | void CalculateNewObjectOffsets(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 109 | ObjectArray<Object>* CreateImageRoots() const; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 110 | static void CalculateNewObjectOffsetsCallback(Object* obj, void* arg); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 111 | |
| 112 | void CopyAndFixupObjects(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 113 | static void CopyAndFixupObjectsCallback(Object* obj, void* arg); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 114 | void FixupClass(const Class* orig, Class* copy); |
| 115 | void FixupMethod(const Method* orig, Method* copy); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 116 | void FixupObject(const Object* orig, Object* copy); |
| 117 | void FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy); |
| 118 | void FixupInstanceFields(const Object* orig, Object* copy); |
| 119 | void FixupStaticFields(const Class* orig, Class* copy); |
| 120 | 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] | 121 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 122 | void FixupDexCaches(); |
| 123 | void FixupDexCache(const DexCache* orig, DexCache* copy); |
| 124 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 125 | // oat file with code for this image |
| 126 | UniquePtr<OatFile> oat_file_; |
| 127 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 128 | // Space we are writing objects from |
| 129 | const Space* source_space_; |
| 130 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 131 | // memory mapped for generating the image |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 132 | UniquePtr<MemMap> image_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 133 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 134 | // Offset to the free space in image_ |
| 135 | size_t image_top_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 136 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 137 | // Target image base address for the output image |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 138 | byte* image_base_; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 139 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 140 | // Set of classes to be include in the image, or NULL for all. |
| 141 | const std::set<std::string>* image_classes_; |
| 142 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 143 | // Target oat base address for the pointers from the output image to its oat file |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 144 | const byte* oat_base_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 145 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 146 | // DexCaches seen while scanning for fixing up CodeAndDirectMethods |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 147 | typedef std::set<DexCache*> Set; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 148 | Set dex_caches_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } // namespace art |
| 152 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 153 | #endif // ART_SRC_IMAGE_WRITER_H_ |