blob: efcfc5e2eb553f062d12d7f7a3bb66dda7eaad59 [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"
Mathieu Chartier23c1d0c2014-11-14 19:34:18 -080029#include "gc/space/space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "mem_map.h"
31#include "oat_file.h"
32#include "mirror/dex_cache.h"
33#include "os.h"
34#include "safe_map.h"
Igor Murashkin3f735bd2014-11-14 15:01:59 -080035#include "gc/space/space.h"
36#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070037
38namespace art {
39
40// Write a Space built during compilation for use during execution.
Igor Murashkin90ca5c02014-10-22 11:37:02 -070041class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 public:
43 explicit ImageWriter(const CompilerDriver& compiler_driver)
Igor Murashkin3f735bd2014-11-14 15:01:59 -080044 : compiler_driver_(compiler_driver), oat_file_(NULL), image_end_(0),
45 image_objects_offset_begin_(0), image_begin_(NULL),
Ian Rogers848871b2013-08-05 10:56:33 -070046 oat_data_begin_(NULL), interpreter_to_interpreter_bridge_offset_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070047 interpreter_to_compiled_code_bridge_offset_(0), portable_imt_conflict_trampoline_offset_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080048 portable_resolution_trampoline_offset_(0), quick_generic_jni_trampoline_offset_(0),
Igor Murashkin90ca5c02014-10-22 11:37:02 -070049 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
Igor Murashkin3f735bd2014-11-14 15:01:59 -080050 compile_pic_(false), target_ptr_size_(0), bin_slot_sizes_(), bin_slot_count_() {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070051
52 ~ImageWriter() {}
53
54 bool Write(const std::string& image_filename,
55 uintptr_t image_begin,
56 const std::string& oat_filename,
Igor Murashkin90ca5c02014-10-22 11:37:02 -070057 const std::string& oat_location,
58 bool compile_pic)
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 LOCKS_EXCLUDED(Locks::mutator_lock_);
60
61 uintptr_t GetOatDataBegin() {
62 return reinterpret_cast<uintptr_t>(oat_data_begin_);
63 }
64
65 private:
66 bool AllocMemory();
67
Mathieu Chartier31e89252013-08-28 11:29:12 -070068 // Mark the objects defined in this space in the given live bitmap.
69 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
70
Igor Murashkin3f735bd2014-11-14 15:01:59 -080071 // Classify different kinds of bins that objects end up getting packed into during image writing.
72 enum Bin {
73 // Likely-clean:
74 kBinString, // [String] Almost always immutable (except for obj header).
75 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
76 // Unknown mix of clean/dirty:
77 kBinRegular,
78 // Likely-dirty:
79 // All classes get their own bins since their fields often dirty
80 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
81 kBinClassInitialized, // Class initializers have been run
82 kBinClassVerified, // Class verified, but initializers haven't been run
83 kBinArtMethodNative, // Art method that is actually native
84 kBinArtMethodNotInitialized, // Art method with a declaring class that wasn't initialized
85 // Don't care about other art methods since they don't dirty
86 // Add more bins here if we add more segregation code.
87 kBinSize,
88 };
89
90 static constexpr size_t kBinBits = MinimumBitsToStore(kBinSize - 1);
91 // uint32 = typeof(lockword_)
92 static constexpr size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits;
93 // 111000.....0
94 static constexpr size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
95
96 // We use the lock word to store the bin # and bin index of the object in the image.
97 //
98 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
99 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
100 struct BinSlot {
101 explicit BinSlot(uint32_t lockword);
102 BinSlot(Bin bin, uint32_t index);
103
104 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
105 Bin GetBin() const;
106 // The offset in bytes from the beginning of the bin. Aligned to object size.
107 uint32_t GetIndex() const;
108 // Pack into a single uint32_t, for storing into a lock word.
109 explicit operator uint32_t() const { return lockword_; }
110 // Comparison operator for map support
111 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
112
113 private:
114 // Must be the same size as LockWord, any larger and we would truncate the data.
115 const uint32_t lockword_;
116 };
117
Mathieu Chartier31e89252013-08-28 11:29:12 -0700118 // We use the lock word to store the offset of the object in the image.
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800119 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121 void SetImageOffset(mirror::Object* object, BinSlot bin_slot, size_t offset)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700123 bool IsImageOffsetAssigned(mirror::Object* object) const
124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
125 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800127 void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
130 bool IsImageBinSlotAssigned(mirror::Object* object) const
131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
133
Alex Lighta59dd802014-07-02 16:28:08 -0700134 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
137 }
138
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700139 mirror::Object* GetImageAddress(mirror::Object* object) const
140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 if (object == NULL) {
142 return NULL;
143 }
144 return reinterpret_cast<mirror::Object*>(image_begin_ + GetImageOffset(object));
145 }
146
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700147 mirror::Object* GetLocalAddress(mirror::Object* object) const
148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 size_t offset = GetImageOffset(object);
150 byte* dst = image_->Begin() + offset;
151 return reinterpret_cast<mirror::Object*>(dst);
152 }
153
154 const byte* GetOatAddress(uint32_t offset) const {
155#if !defined(ART_USE_PORTABLE_COMPILER)
156 // With Quick, code is within the OatFile, as there are all in one
157 // .o ELF object. However with Portable, the code is always in
158 // different .o ELF objects.
159 DCHECK_LT(offset, oat_file_->Size());
160#endif
Igor Murashkin90ca5c02014-10-22 11:37:02 -0700161 if (offset == 0u) {
162 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 }
164 return oat_data_begin_ + offset;
165 }
166
167 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169
170 // Debug aid that list of requested image classes.
171 void DumpImageClasses();
172
173 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
174 void ComputeLazyFieldsForImageClasses()
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
176 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
178
179 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700180 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
183
Mathieu Chartier23c1d0c2014-11-14 19:34:18 -0800184 // Combine string char arrays.
185 void ProcessStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 // Remove unwanted classes from various roots.
188 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
189 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
191
192 // Verify unwanted classes removed.
Mathieu Chartier23c1d0c2014-11-14 19:34:18 -0800193 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
196
197 // Lays out where the image objects will be at runtime.
198 void CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
200 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800202 void CalculateObjectBinSlots(mirror::Object* obj)
203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
204 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206
207 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
209 void WalkFieldsInOrder(mirror::Object* obj)
210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
211 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800213 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215
216 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartier23c1d0c2014-11-14 19:34:18 -0800217 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800220 void FixupMethod(mirror::ArtMethod* orig, mirror::ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800222 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700225 // Get quick code for non-resolution/imt_conflict/abstract method.
226 const byte* GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted)
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228
229 const byte* GetQuickEntryPoint(mirror::ArtMethod* method)
230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
231
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 // Patches references in OatFile to expect runtime addresses.
Alex Light53cb16b2014-06-12 11:26:29 -0700233 void PatchOatCodeAndMethods(File* elf_file)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800236 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
237 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
238
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 const CompilerDriver& compiler_driver_;
240
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 // oat file with code for this image
242 OatFile* oat_file_;
243
244 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700245 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246
247 // Offset to the free space in image_.
248 size_t image_end_;
249
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800250 // Offset from image_begin_ to where the first object is in image_.
251 size_t image_objects_offset_begin_;
252
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 // Beginning target image address for the output image.
254 byte* image_begin_;
255
Mathieu Chartier590fee92013-09-13 13:46:47 -0700256 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700257 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700258
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800259 // Saved hashes (objects are bin slots to inside of the image, not yet allocated an address).
260 std::map<BinSlot, uint32_t> saved_hashes_map_;
261
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 // Beginning target oat address for the pointers from the output image to its oat file.
263 const byte* oat_data_begin_;
264
Mathieu Chartier31e89252013-08-28 11:29:12 -0700265 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700266 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700267
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700269 uint32_t interpreter_to_interpreter_bridge_offset_;
270 uint32_t interpreter_to_compiled_code_bridge_offset_;
271 uint32_t jni_dlsym_lookup_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700272 uint32_t portable_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 uint32_t portable_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700274 uint32_t portable_to_interpreter_bridge_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800275 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700276 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700278 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin90ca5c02014-10-22 11:37:02 -0700279 bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700280
Mathieu Chartiere832e642014-11-10 11:08:06 -0800281 // Size of pointers on the target architecture.
282 size_t target_ptr_size_;
283
Igor Murashkin3f735bd2014-11-14 15:01:59 -0800284 // Bin slot tracking for dirty object packing
285 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
286 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
287
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700288 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700289 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700290 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291};
292
293} // namespace art
294
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700295#endif // ART_COMPILER_IMAGE_WRITER_H_