blob: 120de97620a3e0743e233016207ebb50624be7dc [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 Chartier54d220e2015-07-30 16:20:06 -070033#include "length_prefixed_array.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "lock_word.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "mem_map.h"
36#include "oat_file.h"
37#include "mirror/dex_cache.h"
38#include "os.h"
39#include "safe_map.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070040#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070041
42namespace art {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080043namespace gc {
44namespace space {
45class ImageSpace;
46} // namespace space
47} // namespace gc
Brian Carlstrom7940e442013-07-12 13:46:57 -070048
Mathieu Chartiera90c7722015-10-29 15:41:36 -070049static constexpr int kInvalidImageFd = -1;
50
Brian Carlstrom7940e442013-07-12 13:46:57 -070051// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070052class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 public:
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080054 ImageWriter(const CompilerDriver& compiler_driver,
55 uintptr_t image_begin,
56 bool compile_pic,
57 bool compile_app_image)
58 : compiler_driver_(compiler_driver),
59 image_begin_(reinterpret_cast<uint8_t*>(image_begin)),
60 image_end_(0),
61 image_objects_offset_begin_(0),
62 image_roots_address_(0),
63 oat_file_(nullptr),
64 oat_data_begin_(nullptr),
65 compile_pic_(compile_pic),
66 compile_app_image_(compile_app_image),
67 boot_image_space_(nullptr),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080068 target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())),
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080069 bin_slot_sizes_(),
70 bin_slot_offsets_(),
71 bin_slot_count_(),
72 intern_table_bytes_(0u),
73 image_method_array_(ImageHeader::kImageMethodsCount),
74 dirty_methods_(0u),
75 clean_methods_(0u) {
Vladimir Markof4da6752014-08-01 19:04:18 +010076 CHECK_NE(image_begin, 0U);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080077 std::fill_n(image_methods_, arraysize(image_methods_), nullptr);
78 std::fill_n(oat_address_offsets_, arraysize(oat_address_offsets_), 0);
Vladimir Markof4da6752014-08-01 19:04:18 +010079 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070080
Andreas Gampe245ee002014-12-04 21:25:04 -080081 ~ImageWriter() {
Andreas Gampe245ee002014-12-04 21:25:04 -080082 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070083
Vladimir Markof4da6752014-08-01 19:04:18 +010084 bool PrepareImageAddressSpace();
85
86 bool IsImageAddressSpaceReady() const {
87 return image_roots_address_ != 0u;
88 }
89
Mathieu Chartiere401d142015-04-22 13:56:20 -070090 template <typename T>
Mathieu Chartier90443472015-07-16 20:32:27 -070091 T* GetImageAddress(T* object) const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080092 return (object == nullptr || IsInBootImage(object))
93 ? object
94 : reinterpret_cast<T*>(image_begin_ + GetImageOffset(object));
Vladimir Markof4da6752014-08-01 19:04:18 +010095 }
96
Mathieu Chartier90443472015-07-16 20:32:27 -070097 ArtMethod* GetImageMethodAddress(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070098
Vladimir Marko05792b92015-08-03 11:56:49 +010099 template <typename PtrType>
100 PtrType GetDexCacheArrayElementImageAddress(const DexFile* dex_file, uint32_t offset)
101 const SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000102 auto it = dex_cache_array_starts_.find(dex_file);
103 DCHECK(it != dex_cache_array_starts_.end());
Vladimir Marko05792b92015-08-03 11:56:49 +0100104 return reinterpret_cast<PtrType>(
105 image_begin_ + bin_slot_offsets_[kBinDexCacheArray] + it->second + offset);
Vladimir Marko20f85592015-03-19 10:07:02 +0000106 }
107
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700108 uint8_t* GetOatFileBegin() const;
Vladimir Markof4da6752014-08-01 19:04:18 +0100109
Mathieu Chartiera90c7722015-10-29 15:41:36 -0700110 // If image_fd is not kInvalidImageFd, then we use that for the file. Otherwise we open
111 // image_filename.
112 bool Write(int image_fd,
113 const std::string& image_filename,
114 const std::string& oat_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 const std::string& oat_location)
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 REQUIRES(!Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117
118 uintptr_t GetOatDataBegin() {
119 return reinterpret_cast<uintptr_t>(oat_data_begin_);
120 }
121
122 private:
123 bool AllocMemory();
124
Mathieu Chartier31e89252013-08-28 11:29:12 -0700125 // Mark the objects defined in this space in the given live bitmap.
Mathieu Chartier90443472015-07-16 20:32:27 -0700126 void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700127
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800128 // Classify different kinds of bins that objects end up getting packed into during image writing.
129 enum Bin {
130 // Likely-clean:
131 kBinString, // [String] Almost always immutable (except for obj header).
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800132 // Unknown mix of clean/dirty:
133 kBinRegular,
134 // Likely-dirty:
135 // All classes get their own bins since their fields often dirty
136 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
137 kBinClassInitialized, // Class initializers have been run
138 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800139 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 // Non mirror fields must be below.
141 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700142 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143 // If the class is initialized, then the ArtMethods are probably clean.
144 kBinArtMethodClean,
145 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
146 // initialized.
147 kBinArtMethodDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100148 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
149 // huge, and as such their dirtiness is not important for the clean/dirty separation,
150 // we arbitrarily keep them at the end of the native data.
151 kBinDexCacheArray, // Arrays belonging to dex cache.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800152 kBinSize,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700153 // Number of bins which are for mirror objects.
154 kBinMirrorCount = kBinArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800155 };
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800156 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
157
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700158 enum NativeObjectRelocationType {
159 kNativeObjectRelocationTypeArtField,
160 kNativeObjectRelocationTypeArtFieldArray,
161 kNativeObjectRelocationTypeArtMethodClean,
162 kNativeObjectRelocationTypeArtMethodArrayClean,
163 kNativeObjectRelocationTypeArtMethodDirty,
164 kNativeObjectRelocationTypeArtMethodArrayDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100165 kNativeObjectRelocationTypeDexCacheArray,
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700166 };
167 friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type);
168
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800169 enum OatAddress {
170 kOatAddressInterpreterToInterpreterBridge,
171 kOatAddressInterpreterToCompiledCodeBridge,
172 kOatAddressJNIDlsymLookup,
173 kOatAddressQuickGenericJNITrampoline,
174 kOatAddressQuickIMTConflictTrampoline,
175 kOatAddressQuickResolutionTrampoline,
176 kOatAddressQuickToInterpreterBridge,
177 // Number of elements in the enum.
178 kOatAddressCount,
179 };
180 friend std::ostream& operator<<(std::ostream& stream, const OatAddress& oat_address);
181
Vladimir Marko80afd022015-05-19 18:08:00 +0100182 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800183 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
185 // failures due to invalid read barrier bits during object field reads.
186 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
187 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800188 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800190
191 // We use the lock word to store the bin # and bin index of the object in the image.
192 //
193 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
194 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
195 struct BinSlot {
196 explicit BinSlot(uint32_t lockword);
197 BinSlot(Bin bin, uint32_t index);
198
199 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
200 Bin GetBin() const;
201 // The offset in bytes from the beginning of the bin. Aligned to object size.
202 uint32_t GetIndex() const;
203 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700204 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800205 // Comparison operator for map support
206 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
207
208 private:
209 // Must be the same size as LockWord, any larger and we would truncate the data.
210 const uint32_t lockword_;
211 };
212
Mathieu Chartier31e89252013-08-28 11:29:12 -0700213 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800214 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700215 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700216 void SetImageOffset(mirror::Object* object, size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700217 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700218 bool IsImageOffsetAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700219 SHARED_REQUIRES(Locks::mutator_lock_);
220 size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700221 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700222 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223
Mathieu Chartier90443472015-07-16 20:32:27 -0700224 void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_);
225 void AssignImageBinSlot(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800226 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700227 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800228 bool IsImageBinSlotAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700229 SHARED_REQUIRES(Locks::mutator_lock_);
230 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800231
Vladimir Marko05792b92015-08-03 11:56:49 +0100232 void AddDexCacheArrayRelocation(void* array, size_t offset) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700233 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234
Alex Lighta59dd802014-07-02 16:28:08 -0700235 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700236 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Lighta59dd802014-07-02 16:28:08 -0700237 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
238 }
239
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700240 mirror::Object* GetLocalAddress(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700241 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700243 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 return reinterpret_cast<mirror::Object*>(dst);
245 }
246
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800247 // Returns the address in the boot image if we are compiling the app image.
248 const uint8_t* GetOatAddress(OatAddress type) const;
249
250 const uint8_t* GetOatAddressForOffset(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 // With Quick, code is within the OatFile, as there are all in one
Elliott Hughes956af0f2014-12-11 14:34:28 -0800252 // .o ELF object.
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100253 DCHECK_LE(offset, oat_file_->Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 DCHECK(oat_data_begin_ != nullptr);
255 return offset == 0u ? nullptr : oat_data_begin_ + offset;
256 }
257
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 // Returns true if the class was in the original requested image classes list.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800259 bool KeepClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260
261 // Debug aid that list of requested image classes.
262 void DumpImageClasses();
263
264 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
265 void ComputeLazyFieldsForImageClasses()
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 // Remove unwanted classes from various roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700269 void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270
271 // Verify unwanted classes removed.
Mathieu Chartier90443472015-07-16 20:32:27 -0700272 void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700274 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275
276 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100277 void CalculateNewObjectOffsets()
Mathieu Chartier90443472015-07-16 20:32:27 -0700278 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100279 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700280 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800283 void CalculateObjectBinSlots(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700284 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800285 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700286 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287
288 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700289 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700290 void WalkFieldsInOrder(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700291 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700292 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700293 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800294 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700295 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296
297 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartier90443472015-07-16 20:32:27 -0700298 void CopyAndFixupNativeData() SHARED_REQUIRES(Locks::mutator_lock_);
299 void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700301 SHARED_REQUIRES(Locks::mutator_lock_);
302 void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700304 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700305 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700306 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800307 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700308 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100309 void FixupDexCache(mirror::DexCache* orig_dex_cache, mirror::DexCache* copy_dex_cache)
310 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 void FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, mirror::Class* klass,
Mathieu Chartier90443472015-07-16 20:32:27 -0700312 Bin array_type) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700314 // Get quick code for non-resolution/imt_conflict/abstract method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 const uint8_t* GetQuickCode(ArtMethod* method, bool* quick_is_interpreted)
Mathieu Chartier90443472015-07-16 20:32:27 -0700316 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700317
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 const uint8_t* GetQuickEntryPoint(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700319 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100322 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800324 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
325 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
326
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 // Return true if a method is likely to be dirtied at runtime.
Mathieu Chartier90443472015-07-16 20:32:27 -0700328 bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329
330 // Assign the offset for an ArtMethod.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700331 void AssignMethodOffset(ArtMethod* method, NativeObjectRelocationType type)
332 SHARED_REQUIRES(Locks::mutator_lock_);
333
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800334 bool IsBootClassLoaderNonImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
335
336 bool ContainsBootClassLoaderNonImageClass(mirror::Class* klass)
337 SHARED_REQUIRES(Locks::mutator_lock_);
338
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700339 static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
340
Vladimir Marko05792b92015-08-03 11:56:49 +0100341 uintptr_t NativeOffsetInImage(void* obj);
342
343 template <typename T>
344 T* NativeLocationInImage(T* obj);
Andreas Gampe245ee002014-12-04 21:25:04 -0800345
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800346 // Return true of obj is inside of the boot image space. This may only return true if we are
347 // compiling an app image.
348 bool IsInBootImage(const void* obj) const;
349
350 // Return true if ptr is within the boot oat file.
351 bool IsInBootOatFile(const void* ptr) const;
352
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 const CompilerDriver& compiler_driver_;
354
Vladimir Markof4da6752014-08-01 19:04:18 +0100355 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700356 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100357
358 // Offset to the free space in image_.
359 size_t image_end_;
360
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800361 // Offset from image_begin_ to where the first object is in image_.
362 size_t image_objects_offset_begin_;
363
Vladimir Markof4da6752014-08-01 19:04:18 +0100364 // The image roots address in the image.
365 uint32_t image_roots_address_;
366
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 // oat file with code for this image
368 OatFile* oat_file_;
369
370 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700371 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
374 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
375 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
376
Vladimir Marko20f85592015-03-19 10:07:02 +0000377 // The start offsets of the dex cache arrays.
378 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
379
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700380 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
381 // forwarding addresses as well as copying over hash codes.
382 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800383
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700385 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386
Mathieu Chartier31e89252013-08-28 11:29:12 -0700387 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700388 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700389
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 // Offset from oat_data_begin_ to the stubs.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800391 uint32_t oat_address_offsets_[kOatAddressCount];
392
393 // Boolean flags.
Igor Murashkin46774762014-10-22 11:37:02 -0700394 const bool compile_pic_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800395 const bool compile_app_image_;
396
397 // Boot image space for fast lookups.
398 gc::space::ImageSpace* boot_image_space_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700399
Mathieu Chartier2d721012014-11-10 11:08:06 -0800400 // Size of pointers on the target architecture.
401 size_t target_ptr_size_;
402
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800403 // Bin slot tracking for dirty object packing
404 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
Vladimir Markocf36d492015-08-12 19:27:26 +0100405 size_t bin_slot_offsets_[kBinSize]; // Number of bytes in previous bins.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800406 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
407
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700408 // Cached size of the intern table for when we allocate memory.
409 size_t intern_table_bytes_;
410
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
412 // have one entry per art field for convenience. ArtFields are placed right after the end of the
413 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700414 struct NativeObjectRelocation {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 uintptr_t offset;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700416 NativeObjectRelocationType type;
417
418 bool IsArtMethodRelocation() const {
419 return type == kNativeObjectRelocationTypeArtMethodClean ||
420 type == kNativeObjectRelocationTypeArtMethodDirty;
421 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 };
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700423 std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700424
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
426 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -0700427 // Fake length prefixed array for image methods. This array does not contain the actual
428 // ArtMethods. We only use it for the header and relocation addresses.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700429 LengthPrefixedArray<ArtMethod> image_method_array_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430
431 // Counters for measurements, used for logging only.
432 uint64_t dirty_methods_;
433 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800434
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800435 // Prune class memoization table.
436 std::unordered_map<mirror::Class*, bool> prune_class_memo_;
437
438 friend class ContainsBootClassLoaderNonImageClassVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700439 friend class FixupClassVisitor;
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700440 friend class FixupRootVisitor;
441 friend class FixupVisitor;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700442 friend class NonImageClassesVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700443 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444};
445
446} // namespace art
447
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700448#endif // ART_COMPILER_IMAGE_WRITER_H_