blob: 754fe844f8d67c621b296d40cf18d8164ba4fb0c [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>
Evgenii Stepanov1e133742015-05-20 12:30:59 -070021#include "base/memory_tool.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022
23#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070024#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include <set>
26#include <string>
Igor Murashkinf5b4c502014-11-14 15:01:59 -080027#include <ostream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
Vladimir Marko80afd022015-05-19 18:08:00 +010029#include "base/bit_utils.h"
Igor Murashkin46774762014-10-22 11:37:02 -070030#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "driver/compiler_driver.h"
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -080032#include "gc/space/space.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "lock_word.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#include "mem_map.h"
35#include "oat_file.h"
36#include "mirror/dex_cache.h"
37#include "os.h"
38#include "safe_map.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070040
41namespace art {
42
43// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070044class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 public:
Igor Murashkin46774762014-10-22 11:37:02 -070046 ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin,
47 bool compile_pic)
Ian Rogers13735952014-10-08 12:43:28 -070048 : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<uint8_t*>(image_begin)),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080049 image_end_(0), image_objects_offset_begin_(0), image_roots_address_(0), oat_file_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -070050 oat_data_begin_(nullptr), interpreter_to_interpreter_bridge_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010051 interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0),
Elliott Hughes956af0f2014-12-11 14:34:28 -080052 quick_generic_jni_trampoline_offset_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010053 quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0),
Mathieu Chartier2d721012014-11-10 11:08:06 -080054 quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080055 target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())),
Vladimir Marko20f85592015-03-19 10:07:02 +000056 bin_slot_sizes_(), bin_slot_previous_sizes_(), bin_slot_count_(),
Mathieu Chartierd39645e2015-06-09 17:50:29 -070057 intern_table_bytes_(0u), dirty_methods_(0u), clean_methods_(0u) {
Vladimir Markof4da6752014-08-01 19:04:18 +010058 CHECK_NE(image_begin, 0U);
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 std::fill(image_methods_, image_methods_ + arraysize(image_methods_), nullptr);
Vladimir Markof4da6752014-08-01 19:04:18 +010060 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070061
Andreas Gampe245ee002014-12-04 21:25:04 -080062 ~ImageWriter() {
Andreas Gampe245ee002014-12-04 21:25:04 -080063 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070064
Vladimir Markof4da6752014-08-01 19:04:18 +010065 bool PrepareImageAddressSpace();
66
67 bool IsImageAddressSpaceReady() const {
68 return image_roots_address_ != 0u;
69 }
70
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 template <typename T>
72 T* GetImageAddress(T* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
73 return object == nullptr ? nullptr :
74 reinterpret_cast<T*>(image_begin_ + GetImageOffset(object));
Vladimir Markof4da6752014-08-01 19:04:18 +010075 }
76
Mathieu Chartiere401d142015-04-22 13:56:20 -070077 ArtMethod* GetImageMethodAddress(ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
78
Vladimir Marko20f85592015-03-19 10:07:02 +000079 mirror::HeapReference<mirror::Object>* GetDexCacheArrayElementImageAddress(
80 const DexFile* dex_file, uint32_t offset) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81 auto it = dex_cache_array_starts_.find(dex_file);
82 DCHECK(it != dex_cache_array_starts_.end());
83 return reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
84 image_begin_ + RoundUp(sizeof(ImageHeader), kObjectAlignment) + it->second + offset);
85 }
86
Mathieu Chartierd39645e2015-06-09 17:50:29 -070087 uint8_t* GetOatFileBegin() const;
Vladimir Markof4da6752014-08-01 19:04:18 +010088
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 bool Write(const std::string& image_filename, const std::string& oat_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 const std::string& oat_location)
91 LOCKS_EXCLUDED(Locks::mutator_lock_);
92
93 uintptr_t GetOatDataBegin() {
94 return reinterpret_cast<uintptr_t>(oat_data_begin_);
95 }
96
97 private:
98 bool AllocMemory();
99
Mathieu Chartier31e89252013-08-28 11:29:12 -0700100 // Mark the objects defined in this space in the given live bitmap.
101 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800103 // Classify different kinds of bins that objects end up getting packed into during image writing.
104 enum Bin {
Vladimir Marko20f85592015-03-19 10:07:02 +0000105 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
106 // huge, and as such their dirtiness is not important for the clean/dirty separation,
107 // we arbitrarily keep them at the beginning.
108 kBinDexCacheArray, // Object arrays belonging to dex cache.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800109 // Likely-clean:
110 kBinString, // [String] Almost always immutable (except for obj header).
111 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
112 // Unknown mix of clean/dirty:
113 kBinRegular,
114 // Likely-dirty:
115 // All classes get their own bins since their fields often dirty
116 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
117 kBinClassInitialized, // Class initializers have been run
118 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800119 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120 // Non mirror fields must be below.
121 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700122 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 // If the class is initialized, then the ArtMethods are probably clean.
124 kBinArtMethodClean,
125 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
126 // initialized.
127 kBinArtMethodDirty,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800128 kBinSize,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700129 // Number of bins which are for mirror objects.
130 kBinMirrorCount = kBinArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800131 };
132
133 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
134
Vladimir Marko80afd022015-05-19 18:08:00 +0100135 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800136 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
138 // failures due to invalid read barrier bits during object field reads.
139 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
140 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800141 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800143
144 // We use the lock word to store the bin # and bin index of the object in the image.
145 //
146 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
147 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
148 struct BinSlot {
149 explicit BinSlot(uint32_t lockword);
150 BinSlot(Bin bin, uint32_t index);
151
152 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
153 Bin GetBin() const;
154 // The offset in bytes from the beginning of the bin. Aligned to object size.
155 uint32_t GetIndex() const;
156 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700157 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800158 // Comparison operator for map support
159 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
160
161 private:
162 // Must be the same size as LockWord, any larger and we would truncate the data.
163 const uint32_t lockword_;
164 };
165
Mathieu Chartier31e89252013-08-28 11:29:12 -0700166 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800167 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700169 void SetImageOffset(mirror::Object* object, size_t offset)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700171 bool IsImageOffsetAssigned(mirror::Object* object) const
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176
Vladimir Marko20f85592015-03-19 10:07:02 +0000177 void PrepareDexCacheArraySlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800178 void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
181 bool IsImageBinSlotAssigned(mirror::Object* object) const
182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
183 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186
Alex Lighta59dd802014-07-02 16:28:08 -0700187 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
189 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
190 }
191
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700192 mirror::Object* GetLocalAddress(mirror::Object* object) const
193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700195 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 return reinterpret_cast<mirror::Object*>(dst);
197 }
198
Ian Rogers13735952014-10-08 12:43:28 -0700199 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 // With Quick, code is within the OatFile, as there are all in one
Elliott Hughes956af0f2014-12-11 14:34:28 -0800201 // .o ELF object.
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000202 DCHECK_LT(offset, oat_file_->Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 DCHECK(oat_data_begin_ != nullptr);
204 return offset == 0u ? nullptr : oat_data_begin_ + offset;
205 }
206
207 static bool IsArtMethodBin(Bin bin) {
208 return bin == kBinArtMethodClean || bin == kBinArtMethodDirty;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 }
210
211 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800212 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213
214 // Debug aid that list of requested image classes.
215 void DumpImageClasses();
216
217 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
218 void ComputeLazyFieldsForImageClasses()
219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
220 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
222
223 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700224 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
227
228 // Remove unwanted classes from various roots.
229 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
232
233 // Verify unwanted classes removed.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800234 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
237
238 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100239 void CalculateNewObjectOffsets()
240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
241 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
243 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800245 void CalculateObjectBinSlots(mirror::Object* obj)
246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
247 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249
250 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
252 void WalkFieldsInOrder(mirror::Object* obj)
253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
254 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800256 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
259 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700260 void CopyAndFixupNativeData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800261 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700264 void CopyAndFixupObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700267 void FixupClass(mirror::Class* orig, mirror::Class* copy)
268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 void FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, mirror::Class* klass,
272 Bin array_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700274 // Get quick code for non-resolution/imt_conflict/abstract method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 const uint8_t* GetQuickCode(ArtMethod* method, bool* quick_is_interpreted)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
277
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 const uint8_t* GetQuickEntryPoint(ArtMethod* method)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
280
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100282 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800284 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
285 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
286
Mathieu Chartiere401d142015-04-22 13:56:20 -0700287 // Return true if a method is likely to be dirtied at runtime.
288 bool WillMethodBeDirty(ArtMethod* m) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
289
290 // Assign the offset for an ArtMethod.
291 void AssignMethodOffset(ArtMethod* method, Bin bin) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe245ee002014-12-04 21:25:04 -0800292
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 const CompilerDriver& compiler_driver_;
294
Vladimir Markof4da6752014-08-01 19:04:18 +0100295 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700296 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100297
298 // Offset to the free space in image_.
299 size_t image_end_;
300
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800301 // Offset from image_begin_ to where the first object is in image_.
302 size_t image_objects_offset_begin_;
303
Vladimir Markof4da6752014-08-01 19:04:18 +0100304 // The image roots address in the image.
305 uint32_t image_roots_address_;
306
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 // oat file with code for this image
308 OatFile* oat_file_;
309
310 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700311 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312
Mathieu Chartierc7853442015-03-27 14:35:38 -0700313 // Indexes, lengths for dex cache arrays (objects are inside of the image so that they don't
314 // move).
315 struct DexCacheArrayLocation {
316 size_t offset_;
317 size_t length_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 Bin bin_type_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700319 };
320 SafeMap<mirror::Object*, DexCacheArrayLocation> dex_cache_array_indexes_;
Vladimir Marko20f85592015-03-19 10:07:02 +0000321
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
323 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
324 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
325
Vladimir Marko20f85592015-03-19 10:07:02 +0000326 // The start offsets of the dex cache arrays.
327 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
328
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700329 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
330 // forwarding addresses as well as copying over hash codes.
331 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800332
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700334 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335
Mathieu Chartier31e89252013-08-28 11:29:12 -0700336 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700337 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700338
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700340 uint32_t interpreter_to_interpreter_bridge_offset_;
341 uint32_t interpreter_to_compiled_code_bridge_offset_;
342 uint32_t jni_dlsym_lookup_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800343 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700344 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700346 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700347 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700348
Mathieu Chartier2d721012014-11-10 11:08:06 -0800349 // Size of pointers on the target architecture.
350 size_t target_ptr_size_;
351
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800352 // Bin slot tracking for dirty object packing
353 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
Vladimir Marko20f85592015-03-19 10:07:02 +0000354 size_t bin_slot_previous_sizes_[kBinSize]; // Number of bytes in previous bins.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800355 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
356
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700357 // Cached size of the intern table for when we allocate memory.
358 size_t intern_table_bytes_;
359
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
361 // have one entry per art field for convenience. ArtFields are placed right after the end of the
362 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
363 struct NativeObjectReloc {
364 uintptr_t offset;
365 Bin bin_type;
366 };
367 std::unordered_map<void*, NativeObjectReloc> native_object_reloc_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700368
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
370 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
371
372 // Counters for measurements, used for logging only.
373 uint64_t dirty_methods_;
374 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800375
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700376 friend class FixupClassVisitor;
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700377 friend class FixupRootVisitor;
378 friend class FixupVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700379 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380};
381
382} // namespace art
383
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700384#endif // ART_COMPILER_IMAGE_WRITER_H_