blob: a35d6ad9c91e9ab0e32fdcb1fc9fd965780f4feb [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>
Andreas Gampe245ee002014-12-04 21:25:04 -080021#include <valgrind.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 Chartiere401d142015-04-22 13:56:20 -070057 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
Ian Rogers13735952014-10-08 12:43:28 -070087 uint8_t* GetOatFileBegin() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 return image_begin_ + RoundUp(
89 image_end_ + bin_slot_sizes_[kBinArtField] + bin_slot_sizes_[kBinArtMethodDirty] +
90 bin_slot_sizes_[kBinArtMethodClean], kPageSize);
Vladimir Markof4da6752014-08-01 19:04:18 +010091 }
92
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 bool Write(const std::string& image_filename, const std::string& oat_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 const std::string& oat_location)
95 LOCKS_EXCLUDED(Locks::mutator_lock_);
96
97 uintptr_t GetOatDataBegin() {
98 return reinterpret_cast<uintptr_t>(oat_data_begin_);
99 }
100
101 private:
102 bool AllocMemory();
103
Mathieu Chartier31e89252013-08-28 11:29:12 -0700104 // Mark the objects defined in this space in the given live bitmap.
105 void RecordImageAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800107 // Classify different kinds of bins that objects end up getting packed into during image writing.
108 enum Bin {
Vladimir Marko20f85592015-03-19 10:07:02 +0000109 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
110 // huge, and as such their dirtiness is not important for the clean/dirty separation,
111 // we arbitrarily keep them at the beginning.
112 kBinDexCacheArray, // Object arrays belonging to dex cache.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800113 // Likely-clean:
114 kBinString, // [String] Almost always immutable (except for obj header).
115 kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty
116 // Unknown mix of clean/dirty:
117 kBinRegular,
118 // Likely-dirty:
119 // All classes get their own bins since their fields often dirty
120 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
121 kBinClassInitialized, // Class initializers have been run
122 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800123 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 // Non mirror fields must be below.
125 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700126 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 // If the class is initialized, then the ArtMethods are probably clean.
128 kBinArtMethodClean,
129 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
130 // initialized.
131 kBinArtMethodDirty,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800132 kBinSize,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700133 // Number of bins which are for mirror objects.
134 kBinMirrorCount = kBinArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800135 };
136
137 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
138
Vladimir Marko80afd022015-05-19 18:08:00 +0100139 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800140 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
142 // failures due to invalid read barrier bits during object field reads.
143 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
144 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800145 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800147
148 // We use the lock word to store the bin # and bin index of the object in the image.
149 //
150 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
151 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
152 struct BinSlot {
153 explicit BinSlot(uint32_t lockword);
154 BinSlot(Bin bin, uint32_t index);
155
156 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
157 Bin GetBin() const;
158 // The offset in bytes from the beginning of the bin. Aligned to object size.
159 uint32_t GetIndex() const;
160 // Pack into a single uint32_t, for storing into a lock word.
161 explicit operator uint32_t() const { return lockword_; }
162 // Comparison operator for map support
163 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
164
165 private:
166 // Must be the same size as LockWord, any larger and we would truncate the data.
167 const uint32_t lockword_;
168 };
169
Mathieu Chartier31e89252013-08-28 11:29:12 -0700170 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800171 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173 void SetImageOffset(mirror::Object* object, BinSlot bin_slot, size_t offset)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700175 bool IsImageOffsetAssigned(mirror::Object* object) const
176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177 size_t GetImageOffset(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180
Vladimir Marko20f85592015-03-19 10:07:02 +0000181 void PrepareDexCacheArraySlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800182 void AssignImageBinSlot(mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
183 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185 bool IsImageBinSlotAssigned(mirror::Object* object) const
186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
187 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
188
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
190
Alex Lighta59dd802014-07-02 16:28:08 -0700191 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
193 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
194 }
195
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700196 mirror::Object* GetLocalAddress(mirror::Object* object) const
197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700199 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 return reinterpret_cast<mirror::Object*>(dst);
201 }
202
Ian Rogers13735952014-10-08 12:43:28 -0700203 const uint8_t* GetOatAddress(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 // With Quick, code is within the OatFile, as there are all in one
Elliott Hughes956af0f2014-12-11 14:34:28 -0800205 // .o ELF object.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 DCHECK_LT(offset, oat_file_->Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207 DCHECK(oat_data_begin_ != nullptr);
208 return offset == 0u ? nullptr : oat_data_begin_ + offset;
209 }
210
211 static bool IsArtMethodBin(Bin bin) {
212 return bin == kBinArtMethodClean || bin == kBinArtMethodDirty;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 }
214
215 // Returns true if the class was in the original requested image classes list.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800216 bool IsImageClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217
218 // Debug aid that list of requested image classes.
219 void DumpImageClasses();
220
221 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
222 void ComputeLazyFieldsForImageClasses()
223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
224 static bool ComputeLazyFieldsForClassesVisitor(mirror::Class* klass, void* arg)
225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
226
227 // Wire dex cache resolved strings to strings in the image to avoid runtime resolution.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700228 void ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg)
230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
231
232 // Remove unwanted classes from various roots.
233 void PruneNonImageClasses() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
234 static bool NonImageClassesVisitor(mirror::Class* c, void* arg)
235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
236
237 // Verify unwanted classes removed.
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800238 void CheckNonImageClassesRemoved() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
241
242 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100243 void CalculateNewObjectOffsets()
244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
245 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
247 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800249 void CalculateObjectBinSlots(mirror::Object* obj)
250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
251 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
253
254 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
256 void WalkFieldsInOrder(mirror::Object* obj)
257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
258 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800260 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262
263 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700264 void CopyAndFixupNativeData() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800265 void CopyAndFixupObjects() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700268 void CopyAndFixupObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700271 void FixupClass(mirror::Class* orig, mirror::Class* copy)
272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800273 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 void FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, mirror::Class* klass,
276 Bin array_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700278 // Get quick code for non-resolution/imt_conflict/abstract method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 const uint8_t* GetQuickCode(ArtMethod* method, bool* quick_is_interpreted)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
281
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 const uint8_t* GetQuickEntryPoint(ArtMethod* method)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700283 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
284
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100286 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800288 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
289 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
290
Mathieu Chartiere401d142015-04-22 13:56:20 -0700291 // Return true if a method is likely to be dirtied at runtime.
292 bool WillMethodBeDirty(ArtMethod* m) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
293
294 // Assign the offset for an ArtMethod.
295 void AssignMethodOffset(ArtMethod* method, Bin bin) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe245ee002014-12-04 21:25:04 -0800296
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 const CompilerDriver& compiler_driver_;
298
Vladimir Markof4da6752014-08-01 19:04:18 +0100299 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700300 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100301
302 // Offset to the free space in image_.
303 size_t image_end_;
304
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800305 // Offset from image_begin_ to where the first object is in image_.
306 size_t image_objects_offset_begin_;
307
Vladimir Markof4da6752014-08-01 19:04:18 +0100308 // The image roots address in the image.
309 uint32_t image_roots_address_;
310
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 // oat file with code for this image
312 OatFile* oat_file_;
313
314 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700315 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316
Mathieu Chartierc7853442015-03-27 14:35:38 -0700317 // Indexes, lengths for dex cache arrays (objects are inside of the image so that they don't
318 // move).
319 struct DexCacheArrayLocation {
320 size_t offset_;
321 size_t length_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 Bin bin_type_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700323 };
324 SafeMap<mirror::Object*, DexCacheArrayLocation> dex_cache_array_indexes_;
Vladimir Marko20f85592015-03-19 10:07:02 +0000325
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
327 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
328 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
329
Vladimir Marko20f85592015-03-19 10:07:02 +0000330 // The start offsets of the dex cache arrays.
331 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
332
Mathieu Chartier590fee92013-09-13 13:46:47 -0700333 // Saved hashes (objects are inside of the image so that they don't move).
Ian Rogers700a4022014-05-19 16:49:03 -0700334 std::vector<std::pair<mirror::Object*, uint32_t>> saved_hashes_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700335
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800336 // Saved hashes (objects are bin slots to inside of the image, not yet allocated an address).
337 std::map<BinSlot, uint32_t> saved_hashes_map_;
338
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700340 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341
Mathieu Chartier31e89252013-08-28 11:29:12 -0700342 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700343 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700344
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 // Offset from oat_data_begin_ to the stubs.
Ian Rogers848871b2013-08-05 10:56:33 -0700346 uint32_t interpreter_to_interpreter_bridge_offset_;
347 uint32_t interpreter_to_compiled_code_bridge_offset_;
348 uint32_t jni_dlsym_lookup_offset_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800349 uint32_t quick_generic_jni_trampoline_offset_;
Jeff Hao88474b42013-10-23 16:24:40 -0700350 uint32_t quick_imt_conflict_trampoline_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 uint32_t quick_resolution_trampoline_offset_;
Ian Rogers848871b2013-08-05 10:56:33 -0700352 uint32_t quick_to_interpreter_bridge_offset_;
Igor Murashkin46774762014-10-22 11:37:02 -0700353 const bool compile_pic_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700354
Mathieu Chartier2d721012014-11-10 11:08:06 -0800355 // Size of pointers on the target architecture.
356 size_t target_ptr_size_;
357
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800358 // Bin slot tracking for dirty object packing
359 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
Vladimir Marko20f85592015-03-19 10:07:02 +0000360 size_t bin_slot_previous_sizes_[kBinSize]; // Number of bytes in previous bins.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800361 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
362
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
364 // have one entry per art field for convenience. ArtFields are placed right after the end of the
365 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
366 struct NativeObjectReloc {
367 uintptr_t offset;
368 Bin bin_type;
369 };
370 std::unordered_map<void*, NativeObjectReloc> native_object_reloc_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700371
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
373 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
374
375 // Counters for measurements, used for logging only.
376 uint64_t dirty_methods_;
377 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800378
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700379 friend class FixupVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700380 friend class FixupClassVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700381 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382};
383
384} // namespace art
385
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700386#endif // ART_COMPILER_IMAGE_WRITER_H_