Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 17 | #ifndef ART_SRC_IMAGE_WRITER_H_ |
| 18 | #define ART_SRC_IMAGE_WRITER_H_ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 19 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 22 | #include <cstddef> |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 23 | #include <set> |
| 24 | #include <string> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 25 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 26 | #include "compiler.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 27 | #include "dex_cache.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 28 | #include "mem_map.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 29 | #include "oat_file.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 30 | #include "object.h" |
| 31 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 32 | #include "safe_map.h" |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 33 | #include "gc/space.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 34 | #include "UniquePtr.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 38 | // Write a Space built during compilation for use during execution. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 39 | class ImageWriter { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 40 | public: |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 41 | explicit ImageWriter(const std::set<std::string>* image_classes) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 42 | : oat_file_(NULL), image_end_(0), image_begin_(NULL), image_classes_(image_classes), |
| 43 | oat_begin_(NULL) {} |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 45 | ~ImageWriter() {} |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 46 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 47 | bool Write(const std::string& image_filename, |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 48 | uintptr_t image_begin, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 49 | const std::string& oat_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 50 | const std::string& oat_location, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 51 | const Compiler& compiler) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 52 | LOCKS_EXCLUDED(Locks::mutator_lock_); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 53 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 54 | private: |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 55 | bool AllocMemory(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 56 | |
| 57 | // we use the lock word to store the offset of the object in the image |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 58 | void AssignImageOffset(Object* object) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 59 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 60 | DCHECK(object != NULL); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 61 | SetImageOffset(object, image_end_); |
| 62 | image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment |
| 63 | DCHECK_LT(image_end_, image_->Size()); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 64 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 65 | |
| 66 | void SetImageOffset(Object* object, size_t offset) { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 67 | DCHECK(object != NULL); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 68 | DCHECK_NE(offset, 0U); |
| 69 | DCHECK(!IsImageOffsetAssigned(object)); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 70 | offsets_.Put(object, offset); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 71 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 72 | |
| 73 | size_t IsImageOffsetAssigned(const Object* object) const { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 74 | DCHECK(object != NULL); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 75 | return offsets_.find(object) != offsets_.end(); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 76 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 77 | |
| 78 | size_t GetImageOffset(const Object* object) const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 79 | DCHECK(object != NULL); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 80 | DCHECK(IsImageOffsetAssigned(object)); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 81 | return offsets_.find(object)->second; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 84 | bool InSourceSpace(const Object* object) const; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 85 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 86 | Object* GetImageAddress(const Object* object) const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 87 | if (object == NULL) { |
| 88 | return NULL; |
| 89 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 90 | // if object outside the relocating source_space_, assume unchanged |
| 91 | if (!InSourceSpace(object)) { |
| 92 | return const_cast<Object*>(object); |
| 93 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 94 | return reinterpret_cast<Object*>(image_begin_ + GetImageOffset(object)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 95 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 96 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 97 | Object* GetLocalAddress(const Object* object) const { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 98 | size_t offset = GetImageOffset(object); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 99 | byte* dst = image_->Begin() + offset; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 100 | return reinterpret_cast<Object*>(dst); |
| 101 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 102 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 103 | const byte* GetOatAddress(uint32_t offset) const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 104 | DCHECK_LT(offset, oat_file_->Size()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 105 | if (offset == 0) { |
| 106 | return NULL; |
| 107 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 108 | return oat_begin_ + offset; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 111 | bool IsImageClass(const Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 112 | void DumpImageClasses(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 113 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 114 | void ComputeLazyFieldsForImageClasses() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 115 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 116 | static bool ComputeLazyFieldsForClassesVisitor(Class* klass, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 117 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 118 | |
Ian Rogers | d1f1bf0 | 2012-02-26 16:59:20 -0800 | [diff] [blame] | 119 | // Wire dex cache resolved strings to strings in the image to avoid runtime resolution |
| 120 | void ComputeEagerResolvedStrings(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 121 | static void ComputeEagerResolvedStringsCallback(Object* obj, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 122 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | d1f1bf0 | 2012-02-26 16:59:20 -0800 | [diff] [blame] | 123 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 124 | void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | static bool NonImageClassesVisitor(Class* c, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 126 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 127 | |
| 128 | void CheckNonImageClassesRemoved(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | static void CheckNonImageClassesRemovedCallback(Object* obj, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 130 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 131 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 132 | void CalculateNewObjectOffsets() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 133 | ObjectArray<Object>* CreateImageRoots() const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 134 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 135 | static void CalculateNewObjectOffsetsCallback(Object* obj, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 136 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 137 | |
| 138 | void CopyAndFixupObjects(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 139 | static void CopyAndFixupObjectsCallback(Object* obj, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 140 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 141 | void FixupClass(const Class* orig, Class* copy) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 142 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 143 | void FixupMethod(const AbstractMethod* orig, AbstractMethod* copy) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 144 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 145 | void FixupObject(const Object* orig, Object* copy) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 146 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 147 | void FixupObjectArray(const ObjectArray<Object>* orig, ObjectArray<Object>* copy) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 148 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 149 | void FixupInstanceFields(const Object* orig, Object* copy) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 150 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 151 | void FixupStaticFields(const Class* orig, Class* copy) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 152 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 153 | void FixupFields(const Object* orig, Object* copy, uint32_t ref_offsets, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 154 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 155 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 156 | void PatchOatCodeAndMethods(const Compiler& compiler) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 157 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | void SetPatchLocation(const Compiler::PatchInformation* patch, uint32_t value) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 159 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 161 | SafeMap<const Object*, size_t> offsets_; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 162 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 163 | // oat file with code for this image |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 164 | OatFile* oat_file_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 166 | // memory mapped for generating the image |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 167 | UniquePtr<MemMap> image_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 168 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 169 | // Offset to the free space in image_ |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 170 | size_t image_end_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 171 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 172 | // Beginning target image address for the output image |
| 173 | byte* image_begin_; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 174 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 175 | // Set of classes to be include in the image, or NULL for all. |
| 176 | const std::set<std::string>* image_classes_; |
| 177 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 178 | // Beginning target oat address for the pointers from the output image to its oat file |
| 179 | const byte* oat_begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 180 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 181 | // DexCaches seen while scanning for fixing up CodeAndDirectMethods |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 182 | typedef std::set<DexCache*> Set; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 183 | Set dex_caches_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | } // namespace art |
| 187 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 188 | #endif // ART_SRC_IMAGE_WRITER_H_ |