blob: c20d83639a5b75a214ea466fed0b27fcb38be151 [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 Chartierceb07b32015-12-10 09:33:21 -080033#include "image.h"
Mathieu Chartier54d220e2015-07-30 16:20:06 -070034#include "length_prefixed_array.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "lock_word.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#include "mem_map.h"
37#include "oat_file.h"
38#include "mirror/dex_cache.h"
39#include "os.h"
40#include "safe_map.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070041#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070042
43namespace art {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080044namespace gc {
45namespace space {
46class ImageSpace;
47} // namespace space
48} // namespace gc
Brian Carlstrom7940e442013-07-12 13:46:57 -070049
Mathieu Chartiera90c7722015-10-29 15:41:36 -070050static constexpr int kInvalidImageFd = -1;
51
Brian Carlstrom7940e442013-07-12 13:46:57 -070052// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070053class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 public:
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080055 ImageWriter(const CompilerDriver& compiler_driver,
56 uintptr_t image_begin,
57 bool compile_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080058 bool compile_app_image,
59 ImageHeader::StorageMode image_storage_mode)
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080060 : compiler_driver_(compiler_driver),
61 image_begin_(reinterpret_cast<uint8_t*>(image_begin)),
62 image_end_(0),
63 image_objects_offset_begin_(0),
64 image_roots_address_(0),
65 oat_file_(nullptr),
66 oat_data_begin_(nullptr),
67 compile_pic_(compile_pic),
68 compile_app_image_(compile_app_image),
69 boot_image_space_(nullptr),
Igor Murashkinf5b4c502014-11-14 15:01:59 -080070 target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())),
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080071 bin_slot_sizes_(),
72 bin_slot_offsets_(),
73 bin_slot_count_(),
74 intern_table_bytes_(0u),
75 image_method_array_(ImageHeader::kImageMethodsCount),
76 dirty_methods_(0u),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -080077 clean_methods_(0u),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080078 class_table_bytes_(0u),
79 image_storage_mode_(image_storage_mode) {
Vladimir Markof4da6752014-08-01 19:04:18 +010080 CHECK_NE(image_begin, 0U);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080081 std::fill_n(image_methods_, arraysize(image_methods_), nullptr);
82 std::fill_n(oat_address_offsets_, arraysize(oat_address_offsets_), 0);
Vladimir Markof4da6752014-08-01 19:04:18 +010083 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070084
Andreas Gampe245ee002014-12-04 21:25:04 -080085 ~ImageWriter() {
Andreas Gampe245ee002014-12-04 21:25:04 -080086 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070087
Vladimir Markof4da6752014-08-01 19:04:18 +010088 bool PrepareImageAddressSpace();
89
90 bool IsImageAddressSpaceReady() const {
91 return image_roots_address_ != 0u;
92 }
93
Mathieu Chartiere401d142015-04-22 13:56:20 -070094 template <typename T>
Mathieu Chartier90443472015-07-16 20:32:27 -070095 T* GetImageAddress(T* object) const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080096 return (object == nullptr || IsInBootImage(object))
97 ? object
98 : reinterpret_cast<T*>(image_begin_ + GetImageOffset(object));
Vladimir Markof4da6752014-08-01 19:04:18 +010099 }
100
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 ArtMethod* GetImageMethodAddress(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102
Vladimir Marko05792b92015-08-03 11:56:49 +0100103 template <typename PtrType>
104 PtrType GetDexCacheArrayElementImageAddress(const DexFile* dex_file, uint32_t offset)
105 const SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000106 auto it = dex_cache_array_starts_.find(dex_file);
107 DCHECK(it != dex_cache_array_starts_.end());
Vladimir Marko05792b92015-08-03 11:56:49 +0100108 return reinterpret_cast<PtrType>(
109 image_begin_ + bin_slot_offsets_[kBinDexCacheArray] + it->second + offset);
Vladimir Marko20f85592015-03-19 10:07:02 +0000110 }
111
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700112 uint8_t* GetOatFileBegin() const;
Vladimir Markof4da6752014-08-01 19:04:18 +0100113
Mathieu Chartiera90c7722015-10-29 15:41:36 -0700114 // If image_fd is not kInvalidImageFd, then we use that for the file. Otherwise we open
115 // image_filename.
116 bool Write(int image_fd,
117 const std::string& image_filename,
118 const std::string& oat_filename,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 const std::string& oat_location)
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 REQUIRES(!Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121
122 uintptr_t GetOatDataBegin() {
123 return reinterpret_cast<uintptr_t>(oat_data_begin_);
124 }
125
126 private:
127 bool AllocMemory();
128
Mathieu Chartier31e89252013-08-28 11:29:12 -0700129 // Mark the objects defined in this space in the given live bitmap.
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700131
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800132 // Classify different kinds of bins that objects end up getting packed into during image writing.
133 enum Bin {
134 // Likely-clean:
135 kBinString, // [String] Almost always immutable (except for obj header).
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800136 // Unknown mix of clean/dirty:
137 kBinRegular,
138 // Likely-dirty:
139 // All classes get their own bins since their fields often dirty
140 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
141 kBinClassInitialized, // Class initializers have been run
142 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800143 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 // Non mirror fields must be below.
145 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700146 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147 // If the class is initialized, then the ArtMethods are probably clean.
148 kBinArtMethodClean,
149 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
150 // initialized.
151 kBinArtMethodDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100152 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
153 // huge, and as such their dirtiness is not important for the clean/dirty separation,
154 // we arbitrarily keep them at the end of the native data.
155 kBinDexCacheArray, // Arrays belonging to dex cache.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800156 kBinSize,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700157 // Number of bins which are for mirror objects.
158 kBinMirrorCount = kBinArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800159 };
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800160 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
161
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700162 enum NativeObjectRelocationType {
163 kNativeObjectRelocationTypeArtField,
164 kNativeObjectRelocationTypeArtFieldArray,
165 kNativeObjectRelocationTypeArtMethodClean,
166 kNativeObjectRelocationTypeArtMethodArrayClean,
167 kNativeObjectRelocationTypeArtMethodDirty,
168 kNativeObjectRelocationTypeArtMethodArrayDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100169 kNativeObjectRelocationTypeDexCacheArray,
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700170 };
171 friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type);
172
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800173 enum OatAddress {
174 kOatAddressInterpreterToInterpreterBridge,
175 kOatAddressInterpreterToCompiledCodeBridge,
176 kOatAddressJNIDlsymLookup,
177 kOatAddressQuickGenericJNITrampoline,
178 kOatAddressQuickIMTConflictTrampoline,
179 kOatAddressQuickResolutionTrampoline,
180 kOatAddressQuickToInterpreterBridge,
181 // Number of elements in the enum.
182 kOatAddressCount,
183 };
184 friend std::ostream& operator<<(std::ostream& stream, const OatAddress& oat_address);
185
Vladimir Marko80afd022015-05-19 18:08:00 +0100186 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800187 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
189 // failures due to invalid read barrier bits during object field reads.
190 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
191 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800192 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700193 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800194
195 // We use the lock word to store the bin # and bin index of the object in the image.
196 //
197 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
198 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
199 struct BinSlot {
200 explicit BinSlot(uint32_t lockword);
201 BinSlot(Bin bin, uint32_t index);
202
203 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
204 Bin GetBin() const;
205 // The offset in bytes from the beginning of the bin. Aligned to object size.
206 uint32_t GetIndex() const;
207 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700208 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800209 // Comparison operator for map support
210 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
211
212 private:
213 // Must be the same size as LockWord, any larger and we would truncate the data.
214 const uint32_t lockword_;
215 };
216
Mathieu Chartier31e89252013-08-28 11:29:12 -0700217 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800218 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700219 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700220 void SetImageOffset(mirror::Object* object, size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700221 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700222 bool IsImageOffsetAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700223 SHARED_REQUIRES(Locks::mutator_lock_);
224 size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700226 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227
Mathieu Chartier90443472015-07-16 20:32:27 -0700228 void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_);
229 void AssignImageBinSlot(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800230 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700231 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800232 bool IsImageBinSlotAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700233 SHARED_REQUIRES(Locks::mutator_lock_);
234 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800235
Vladimir Marko05792b92015-08-03 11:56:49 +0100236 void AddDexCacheArrayRelocation(void* array, size_t offset) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700237 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238
Alex Lighta59dd802014-07-02 16:28:08 -0700239 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700240 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Lighta59dd802014-07-02 16:28:08 -0700241 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
242 }
243
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700244 mirror::Object* GetLocalAddress(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700245 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 size_t offset = GetImageOffset(object);
Ian Rogers13735952014-10-08 12:43:28 -0700247 uint8_t* dst = image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 return reinterpret_cast<mirror::Object*>(dst);
249 }
250
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800251 // Returns the address in the boot image if we are compiling the app image.
252 const uint8_t* GetOatAddress(OatAddress type) const;
253
254 const uint8_t* GetOatAddressForOffset(uint32_t offset) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 // With Quick, code is within the OatFile, as there are all in one
Elliott Hughes956af0f2014-12-11 14:34:28 -0800256 // .o ELF object.
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100257 DCHECK_LE(offset, oat_file_->Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 DCHECK(oat_data_begin_ != nullptr);
259 return offset == 0u ? nullptr : oat_data_begin_ + offset;
260 }
261
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 // Returns true if the class was in the original requested image classes list.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800263 bool KeepClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264
265 // Debug aid that list of requested image classes.
266 void DumpImageClasses();
267
268 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
269 void ComputeLazyFieldsForImageClasses()
Mathieu Chartier90443472015-07-16 20:32:27 -0700270 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 // Remove unwanted classes from various roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700273 void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274
275 // Verify unwanted classes removed.
Mathieu Chartier90443472015-07-16 20:32:27 -0700276 void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700278 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279
280 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100281 void CalculateNewObjectOffsets()
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100283 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700284 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 mirror::ObjectArray<mirror::Object>* CreateImageRoots() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700286 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800287 void CalculateObjectBinSlots(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700288 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800289 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700290 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700291
292 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700293 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700294 void WalkFieldsInOrder(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700295 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700296 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700297 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800298 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700299 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300
301 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartier90443472015-07-16 20:32:27 -0700302 void CopyAndFixupNativeData() SHARED_REQUIRES(Locks::mutator_lock_);
303 void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700305 SHARED_REQUIRES(Locks::mutator_lock_);
306 void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700308 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700309 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700310 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800311 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700312 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100313 void FixupDexCache(mirror::DexCache* orig_dex_cache, mirror::DexCache* copy_dex_cache)
314 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800315 void FixupPointerArray(mirror::Object* dst,
316 mirror::PointerArray* arr,
317 mirror::Class* klass,
318 Bin array_type)
319 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700321 // Get quick code for non-resolution/imt_conflict/abstract method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 const uint8_t* GetQuickCode(ArtMethod* method, bool* quick_is_interpreted)
Mathieu Chartier90443472015-07-16 20:32:27 -0700323 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700324
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 const uint8_t* GetQuickEntryPoint(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700326 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700327
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100329 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800331 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
332 size_t GetBinSizeSum(Bin up_to = kBinSize) const;
333
Mathieu Chartiere401d142015-04-22 13:56:20 -0700334 // Return true if a method is likely to be dirtied at runtime.
Mathieu Chartier90443472015-07-16 20:32:27 -0700335 bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336
337 // Assign the offset for an ArtMethod.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700338 void AssignMethodOffset(ArtMethod* method, NativeObjectRelocationType type)
339 SHARED_REQUIRES(Locks::mutator_lock_);
340
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800341 // Return true if klass is loaded by the boot class loader but not in the boot image.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800342 bool IsBootClassLoaderNonImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
343
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800344 // Return true if klass depends on a boot class loader non image class live. We want to prune
345 // these classes since we do not want any boot class loader classes in the image. This means that
346 // we also cannot have any classes which refer to these boot class loader non image classes.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800347 bool ContainsBootClassLoaderNonImageClass(mirror::Class* klass)
348 SHARED_REQUIRES(Locks::mutator_lock_);
349
Mathieu Chartier945c1c12015-11-24 15:37:12 -0800350 // early_exit is true if we had a cyclic dependency anywhere down the chain.
351 bool ContainsBootClassLoaderNonImageClassInternal(mirror::Class* klass,
352 bool* early_exit,
353 std::unordered_set<mirror::Class*>* visited)
354 SHARED_REQUIRES(Locks::mutator_lock_);
355
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700356 static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
357
Vladimir Marko05792b92015-08-03 11:56:49 +0100358 uintptr_t NativeOffsetInImage(void* obj);
359
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800360 // Location of where the object will be when the image is loaded at runtime.
Vladimir Marko05792b92015-08-03 11:56:49 +0100361 template <typename T>
362 T* NativeLocationInImage(T* obj);
Andreas Gampe245ee002014-12-04 21:25:04 -0800363
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800364 // Location of where the temporary copy of the object currently is.
365 template <typename T>
366 T* NativeCopyLocation(T* obj);
367
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800368 // Return true of obj is inside of the boot image space. This may only return true if we are
369 // compiling an app image.
370 bool IsInBootImage(const void* obj) const;
371
372 // Return true if ptr is within the boot oat file.
373 bool IsInBootOatFile(const void* ptr) const;
374
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 const CompilerDriver& compiler_driver_;
376
Vladimir Markof4da6752014-08-01 19:04:18 +0100377 // Beginning target image address for the output image.
Ian Rogers13735952014-10-08 12:43:28 -0700378 uint8_t* image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100379
380 // Offset to the free space in image_.
381 size_t image_end_;
382
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800383 // Offset from image_begin_ to where the first object is in image_.
384 size_t image_objects_offset_begin_;
385
Vladimir Markof4da6752014-08-01 19:04:18 +0100386 // The image roots address in the image.
387 uint32_t image_roots_address_;
388
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 // oat file with code for this image
390 OatFile* oat_file_;
391
392 // Memory mapped for generating the image.
Ian Rogers700a4022014-05-19 16:49:03 -0700393 std::unique_ptr<MemMap> image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
396 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
397 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
398
Vladimir Marko20f85592015-03-19 10:07:02 +0000399 // The start offsets of the dex cache arrays.
400 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
401
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700402 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
403 // forwarding addresses as well as copying over hash codes.
404 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800405
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 // Beginning target oat address for the pointers from the output image to its oat file.
Ian Rogers13735952014-10-08 12:43:28 -0700407 const uint8_t* oat_data_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408
Mathieu Chartier31e89252013-08-28 11:29:12 -0700409 // Image bitmap which lets us know where the objects inside of the image reside.
Ian Rogers700a4022014-05-19 16:49:03 -0700410 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700411
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 // Offset from oat_data_begin_ to the stubs.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800413 uint32_t oat_address_offsets_[kOatAddressCount];
414
415 // Boolean flags.
Igor Murashkin46774762014-10-22 11:37:02 -0700416 const bool compile_pic_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800417 const bool compile_app_image_;
418
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800419 // Cache the boot image space in this class for faster lookups.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800420 gc::space::ImageSpace* boot_image_space_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700421
Mathieu Chartier2d721012014-11-10 11:08:06 -0800422 // Size of pointers on the target architecture.
423 size_t target_ptr_size_;
424
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800425 // Bin slot tracking for dirty object packing
426 size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin
Vladimir Markocf36d492015-08-12 19:27:26 +0100427 size_t bin_slot_offsets_[kBinSize]; // Number of bytes in previous bins.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800428 size_t bin_slot_count_[kBinSize]; // Number of objects in a bin
429
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700430 // Cached size of the intern table for when we allocate memory.
431 size_t intern_table_bytes_;
432
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
434 // have one entry per art field for convenience. ArtFields are placed right after the end of the
435 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700436 struct NativeObjectRelocation {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 uintptr_t offset;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700438 NativeObjectRelocationType type;
439
440 bool IsArtMethodRelocation() const {
441 return type == kNativeObjectRelocationTypeArtMethodClean ||
442 type == kNativeObjectRelocationTypeArtMethodDirty;
443 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 };
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700445 std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700446
Mathieu Chartiere401d142015-04-22 13:56:20 -0700447 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
448 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -0700449 // Fake length prefixed array for image methods. This array does not contain the actual
450 // ArtMethods. We only use it for the header and relocation addresses.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700451 LengthPrefixedArray<ArtMethod> image_method_array_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452
453 // Counters for measurements, used for logging only.
454 uint64_t dirty_methods_;
455 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800456
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800457 // Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800458 std::unordered_map<mirror::Class*, bool> prune_class_memo_;
459
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800460 // Class loaders with a class table to write out. Should only be one currently.
461 std::unordered_set<mirror::ClassLoader*> class_loaders_;
462
463 // Number of image class table bytes.
464 size_t class_table_bytes_;
465
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800466 // Which mode the image is stored as, see image.h
467 const ImageHeader::StorageMode image_storage_mode_;
468
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800469 friend class ContainsBootClassLoaderNonImageClassVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700470 friend class FixupClassVisitor;
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700471 friend class FixupRootVisitor;
472 friend class FixupVisitor;
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800473 friend class NativeLocationVisitor;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700474 friend class NonImageClassesVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700475 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476};
477
478} // namespace art
479
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700480#endif // ART_COMPILER_IMAGE_WRITER_H_