blob: 30c6020898e321eb4a5bcf90dd260212e6f85d1c [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_IMAGE_WRITER_H_
18#define ART_COMPILER_IMAGE_WRITER_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include <stdint.h>
21
22#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include <set>
25#include <string>
26
Igor Murashkin90ca5c02014-10-22 11:37:02 -070027#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#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"
Igor Murashkin3f735bd2014-11-14 15:01:59 -080034#include "gc/space/space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36namespace art {
37
38// Write a Space built during compilation for use during execution.
Igor Murashkin90ca5c02014-10-22 11:37:02 -070039class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 public:
41 explicit ImageWriter(const CompilerDriver& compiler_driver)
Igor Murashkin3f735bd2014-11-14 15:01:59 -080042 : compiler_driver_(compiler_driver), oat_file_(NULL), image_end_(0),
43 image_objects_offset_begin_(0), image_begin_(NULL),
Ian Rogers848871b2013-08-05 10:56:33 -070044 oat_data_begin_(NULL), interpreter_to_interpreter_bridge_offset_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070045 interpreter_to_compiled_code_bridge_offset_(0), portable_imt_conflict_trampoline_offset_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080046 portable_resolution_trampoline_offset_(0), quick_generic_jni_trampoline_offset_(0),
Igor Murashkin90ca5c02014-10-22 11:37:02 -070047 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
Igor Murashkin3f735bd2014-11-14 15:01:59 -080048 compile_pic_(false), target_ptr_size_(0), bin_slot_sizes_(), bin_slot_count_() {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070049
50 ~ImageWriter() {}
51
52 bool Write(const std::string& image_filename,
53 uintptr_t image_begin,
54 const std::string& oat_filename,
Igor Murashkin90ca5c02014-10-22 11:37:02 -070055 const std::string& oat_location,
56 bool compile_pic)
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 LOCKS_EXCLUDED(Locks::mutator_lock_);
58
59 uintptr_t GetOatDataBegin() {
60 return reinterpret_cast<uintptr_t>(oat_data_begin_);
61 }
62
63 private:
64 bool AllocMemory();
65
Mathieu Chartier31e89252013-08-28 11:29:12 -070066 // Mark the objects defined in this space in the given live bitmap.
67 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
68
Igor Murashkin3f735bd2014-11-14 15:01:59 -080069 // Classify different kinds of bins that objects end up getting packed into during image writing.
70 enum Bin {
71 // Likely-clean:
72 kBinString, // [String] Almost always immutable (except for obj header).
73 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
74 // Unknown mix of clean/dirty:
75 kBinRegular,
76 // Likely-dirty:
77 // All classes get their own bins since their fields often dirty
78 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
79 kBinClassInitialized, // Class initializers have been run
80 kBinClassVerified, // Class verified, but initializers haven't been run
81 kBinArtMethodNative, // Art method that is actually native
82 kBinArtMethodNotInitialized, // Art method with a declaring class that wasn't initialized
83 // Don't care about other art methods since they don't dirty
84 // Add more bins here if we add more segregation code.
85 kBinSize,
86 };
87
88 static constexpr size_t kBinBits = MinimumBitsToStore(kBinSize - 1);
89 // uint32 = typeof(lockword_)
90 static constexpr size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits;
91 // 111000.....0
92 static constexpr size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
93
94 // We use the lock word to store the bin # and bin index of the object in the image.
95 //
96 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
97 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
98 struct BinSlot {
99 explicit BinSlot(uint32_t lockword);
100 BinSlot(Bin bin, uint32_t index);
101
102 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
103 Bin GetBin() const;
104 // The offset in bytes from the beginning of the bin. Aligned to object size.
105 uint32_t GetIndex() const;
106 // Pack into a single uint32_t, for storing into a lock word.
107 explicit operator uint32_t() const { return lockword_; }
108 // Comparison operator for map support
109 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
110
111 private:
112 // Must be the same size as LockWord, any larger and we would truncate the data.
113 const uint32_t lockword_;
114 };
115
Mathieu Chartier31e89252013-08-28 11:29:12 -0700116 // We use the lock word to store the offset of the object in the image.
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800117 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
119 void SetImageOffset(mirror::Object* object, BinSlot bin_slot, size_t offset)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700121 bool IsImageOffsetAssigned(mirror::Object* object) const
122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800125 void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128 bool IsImageBinSlotAssigned(mirror::Object* object) const
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
130 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131
Alex Lighta59dd802014-07-02 16:28:08 -0700132 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
134 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
135 }
136
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700137 mirror::Object* GetImageAddress(mirror::Object* object) const
138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 if (object == NULL) {
140 return NULL;
141 }
142 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
143 }
144
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700145 mirror::Object* GetLocalAddress(mirror::Object* object) const
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 size_t offset = GetImageOffset(object);
148 byte* dst = image_->Begin() + offset;
149 return reinterpret_cast<mirror::Object*>(dst);
150 }
151
152 const byte* GetOatAddress(uint32_t offset) const {
153#if !defined(ART_USE_PORTABLE_COMPILER)
154 // With Quick, code is within the OatFile, as there are all in one
155 // .o ELF object. However with Portable, the code is always in
156 // different .o ELF objects.
157 DCHECK_LT(offset, oat_file_->Size());
158#endif
Igor Murashkin90ca5c02014-10-22 11:37:02 -0700159 if (offset == 0u) {
160 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 }
162 return oat_data_begin_ + offset;
163 }
164
165 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167
168 // Debug aid that list of requested image classes.
169 void DumpImageClasses();
170
171 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
172 void ComputeLazyFieldsForImageClasses()
173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
174 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
176
177 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700178 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
181
182 // Remove unwanted classes from various roots.
183 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186
187 // Verify unwanted classes removed.
Piotr Jastrzebski3a7cf8e2015-05-07 09:44:22 +0100188 void CheckNonImageClassesRemoved();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
191
192 // Lays out where the image objects will be at runtime.
193 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
195 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800197 void CalculateObjectBinSlots(mirror::Object* obj)
198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
199 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
201
202 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
204 void WalkFieldsInOrder(mirror::Object* obj)
205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800208 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210
211 // Creates the contiguous image in memory and adjusts pointers.
Piotr Jastrzebski3a7cf8e2015-05-07 09:44:22 +0100212 void CopyAndFixupObjects();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700220 // Get quick code for non-resolution/imt_conflict/abstract method.
221 const byte* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
223
224 const byte* GetQuickEntryPoint(mirror::ArtMethod* method)
225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
226
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 // Patches references in OatFile to expect runtime addresses.
Alex Light53cb16b2014-06-12 11:26:29 -0700228 void PatchOatCodeAndMethods(File* elf_file)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800231 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
232 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
233
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 const CompilerDriver& compiler_driver_;
235
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 // oat file with code for this image
237 OatFile* oat_file_;
238
239 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700240 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241
242 // Offset to the free space in image_.
243 size_t image_end_;
244
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800245 // Offset from image_begin_ to where the first object is in image_.
246 size_t image_objects_offset_begin_;
247
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 // Beginning target image address for the output image.
249 byte* image_begin_;
250
Mathieu Chartier590fee92013-09-13 13:46:47 -0700251 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700252 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700253
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800254 // Saved hashes (objects are bin slots to inside of the image, not yet allocated an address).
255 std::map<BinSlot, uint32_t> saved_hashes_map_;
256
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 // Beginning target oat address for the pointers from the output image to its oat file.
258 const byte* oat_data_begin_;
259
Mathieu Chartier31e89252013-08-28 11:29:12 -0700260 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700261 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700262
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700264 uint32_t interpreter_to_interpreter_bridge_offset_;
265 uint32_t interpreter_to_compiled_code_bridge_offset_;
266 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700267 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700269 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800270 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700271 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700273 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin90ca5c02014-10-22 11:37:02 -0700274 bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700275
Mathieu Chartiere832e642014-11-10 11:08:06 -0800276 // Size of pointers on the target architecture.
277 size_t target_ptr_size_;
278
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800279 // Bin slot tracking for dirty object packing
280 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
281 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
282
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700283 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700284 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700285 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286};
287
288} // namespace art
289
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700290#endif // ART_COMPILER_IMAGE_WRITER_H_