Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_IMAGE_WRITER_H_ |
| 18 | #define ART_COMPILER_IMAGE_WRITER_H_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <cstddef> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | #include <set> |
| 25 | #include <string> |
| 26 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 27 | #include "base/macros.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include "driver/compiler_driver.h" |
| 29 | #include "mem_map.h" |
| 30 | #include "oat_file.h" |
| 31 | #include "mirror/dex_cache.h" |
| 32 | #include "os.h" |
| 33 | #include "safe_map.h" |
| 34 | #include "gc/space/space.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | // Write a Space built during compilation for use during execution. |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 39 | class ImageWriter FINAL { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 40 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 41 | ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin, |
| 42 | bool compile_pic) |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 43 | : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<uint8_t*>(image_begin)), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 44 | image_end_(0), image_roots_address_(0), oat_file_(nullptr), |
| 45 | oat_data_begin_(nullptr), interpreter_to_interpreter_bridge_offset_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 46 | interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0), |
| 47 | portable_imt_conflict_trampoline_offset_(0), portable_resolution_trampoline_offset_(0), |
| 48 | portable_to_interpreter_bridge_offset_(0), quick_generic_jni_trampoline_offset_(0), |
| 49 | quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 50 | quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 51 | CHECK_NE(image_begin, 0U); |
| 52 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 53 | |
| 54 | ~ImageWriter() {} |
| 55 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 56 | bool PrepareImageAddressSpace(); |
| 57 | |
| 58 | bool IsImageAddressSpaceReady() const { |
| 59 | return image_roots_address_ != 0u; |
| 60 | } |
| 61 | |
| 62 | mirror::Object* GetImageAddress(mirror::Object* object) const |
| 63 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 64 | if (object == nullptr) { |
| 65 | return nullptr; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 66 | } |
| 67 | return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object)); |
| 68 | } |
| 69 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 70 | uint8_t* GetOatFileBegin() const { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 71 | return image_begin_ + RoundUp(image_end_, kPageSize); |
| 72 | } |
| 73 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 74 | bool Write(const std::string& image_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 75 | const std::string& oat_filename, |
| 76 | const std::string& oat_location) |
| 77 | LOCKS_EXCLUDED(Locks::mutator_lock_); |
| 78 | |
| 79 | uintptr_t GetOatDataBegin() { |
| 80 | return reinterpret_cast<uintptr_t>(oat_data_begin_); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | bool AllocMemory(); |
| 85 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 86 | // Mark the objects defined in this space in the given live bitmap. |
| 87 | void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 88 | |
| 89 | // We use the lock word to store the offset of the object in the image. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 90 | void AssignImageOffset(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 91 | void SetImageOffset(mirror::Object* object, size_t offset) |
| 92 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 93 | bool IsImageOffsetAssigned(mirror::Object* object) const |
| 94 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 95 | size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 96 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 97 | static void* GetImageAddressCallback(void* writer, mirror::Object* obj) |
| 98 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 99 | return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj); |
| 100 | } |
| 101 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 102 | mirror::Object* GetLocalAddress(mirror::Object* object) const |
| 103 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 104 | size_t offset = GetImageOffset(object); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 105 | uint8_t* dst = image_->Begin() + offset; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 106 | return reinterpret_cast<mirror::Object*>(dst); |
| 107 | } |
| 108 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 109 | const uint8_t* GetOatAddress(uint32_t offset) const { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 110 | #if !defined(ART_USE_PORTABLE_COMPILER) |
| 111 | // With Quick, code is within the OatFile, as there are all in one |
| 112 | // .o ELF object. However with Portable, the code is always in |
| 113 | // different .o ELF objects. |
| 114 | DCHECK_LT(offset, oat_file_->Size()); |
| 115 | #endif |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 116 | if (offset == 0u) { |
| 117 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 118 | } |
| 119 | return oat_data_begin_ + offset; |
| 120 | } |
| 121 | |
| 122 | // Returns true if the class was in the original requested image classes list. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 123 | bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 124 | |
| 125 | // Debug aid that list of requested image classes. |
| 126 | void DumpImageClasses(); |
| 127 | |
| 128 | // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying. |
| 129 | void ComputeLazyFieldsForImageClasses() |
| 130 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 131 | static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg) |
| 132 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 133 | |
| 134 | // Wire dex cache resolved strings to strings in the image to avoid runtime resolution. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 135 | void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 136 | static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg) |
| 137 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 138 | |
| 139 | // Remove unwanted classes from various roots. |
| 140 | void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 141 | static bool NonImageClassesVisitor(mirror::Class* c, void* arg) |
| 142 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 143 | |
| 144 | // Verify unwanted classes removed. |
| 145 | void CheckNonImageClassesRemoved(); |
| 146 | static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg) |
| 147 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 148 | |
| 149 | // Lays out where the image objects will be at runtime. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 150 | void CalculateNewObjectOffsets() |
| 151 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 152 | void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 153 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 154 | mirror::ObjectArray<mirror::Object>* CreateImageRoots() const |
| 155 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 156 | void CalculateObjectOffsets(mirror::Object* obj) |
| 157 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 158 | |
| 159 | void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) |
| 160 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 161 | void WalkFieldsInOrder(mirror::Object* obj) |
| 162 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 163 | static void WalkFieldsCallback(mirror::Object* obj, void* arg) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 164 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 165 | |
| 166 | // Creates the contiguous image in memory and adjusts pointers. |
| 167 | void CopyAndFixupObjects(); |
| 168 | static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg) |
| 169 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 170 | void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 171 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 172 | void FixupObject(mirror::Object* orig, mirror::Object* copy) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 173 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 174 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 175 | // Get quick code for non-resolution/imt_conflict/abstract method. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 176 | const uint8_t* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 177 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 178 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 179 | const uint8_t* GetQuickEntryPoint(mirror::ArtMethod* method) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 180 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 181 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 182 | // Patches references in OatFile to expect runtime addresses. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 183 | void SetOatChecksumFromElfFile(File* elf_file); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 184 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 185 | const CompilerDriver& compiler_driver_; |
| 186 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 187 | // Beginning target image address for the output image. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 188 | uint8_t* image_begin_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 189 | |
| 190 | // Offset to the free space in image_. |
| 191 | size_t image_end_; |
| 192 | |
| 193 | // The image roots address in the image. |
| 194 | uint32_t image_roots_address_; |
| 195 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 196 | // oat file with code for this image |
| 197 | OatFile* oat_file_; |
| 198 | |
| 199 | // Memory mapped for generating the image. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 200 | std::unique_ptr<MemMap> image_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 201 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 202 | // Saved hashes (objects are inside of the image so that they don't move). |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 203 | std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 204 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 205 | // Beginning target oat address for the pointers from the output image to its oat file. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 206 | const uint8_t* oat_data_begin_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 207 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 208 | // Image bitmap which lets us know where the objects inside of the image reside. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 209 | std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 210 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 211 | // Offset from oat_data_begin_ to the stubs. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 212 | uint32_t interpreter_to_interpreter_bridge_offset_; |
| 213 | uint32_t interpreter_to_compiled_code_bridge_offset_; |
| 214 | uint32_t jni_dlsym_lookup_offset_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 215 | uint32_t portable_imt_conflict_trampoline_offset_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 216 | uint32_t portable_resolution_trampoline_offset_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 217 | uint32_t portable_to_interpreter_bridge_offset_; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 218 | uint32_t quick_generic_jni_trampoline_offset_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 219 | uint32_t quick_imt_conflict_trampoline_offset_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 220 | uint32_t quick_resolution_trampoline_offset_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 221 | uint32_t quick_to_interpreter_bridge_offset_; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame^] | 222 | const bool compile_pic_; |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 223 | |
| 224 | friend class FixupVisitor; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 225 | friend class FixupClassVisitor; |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 226 | DISALLOW_COPY_AND_ASSIGN(ImageWriter); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | } // namespace art |
| 230 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 231 | #endif // ART_COMPILER_IMAGE_WRITER_H_ |