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