blob: 622eb1985b943638f066855a6906383fe8bd9654 [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"
Alex Lighte64300b2015-12-15 15:02:47 -080030#include "base/length_prefixed_array.h"
Igor Murashkin46774762014-10-22 11:37:02 -070031#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "driver/compiler_driver.h"
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -080033#include "gc/space/space.h"
Mathieu Chartierceb07b32015-12-10 09:33:21 -080034#include "image.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 Chartier1f47b672016-01-07 16:29:01 -080050class ClassTable;
51
Mathieu Chartierfbc31082016-01-24 11:59:56 -080052static constexpr int kInvalidFd = -1;
Mathieu Chartiera90c7722015-10-29 15:41:36 -070053
Brian Carlstrom7940e442013-07-12 13:46:57 -070054// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070055class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 public:
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080057 ImageWriter(const CompilerDriver& compiler_driver,
58 uintptr_t image_begin,
59 bool compile_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080060 bool compile_app_image,
Jeff Haodcdc85b2015-12-04 14:06:18 -080061 ImageHeader::StorageMode image_storage_mode,
62 const std::vector<const char*> oat_filenames,
Mathieu Chartierea0831f2015-12-29 13:17:37 -080063 const std::unordered_map<const DexFile*, const char*>& dex_file_oat_filename_map);
Brian Carlstrom7940e442013-07-12 13:46:57 -070064
Vladimir Markof4da6752014-08-01 19:04:18 +010065 bool PrepareImageAddressSpace();
66
67 bool IsImageAddressSpaceReady() const {
Jeff Haodcdc85b2015-12-04 14:06:18 -080068 bool ready = !image_info_map_.empty();
69 for (auto& pair : image_info_map_) {
70 const ImageInfo& image_info = pair.second;
71 if (image_info.image_roots_address_ == 0u) {
72 return false;
73 }
74 }
75 return ready;
Vladimir Markof4da6752014-08-01 19:04:18 +010076 }
77
Mathieu Chartiere401d142015-04-22 13:56:20 -070078 template <typename T>
Mathieu Chartier90443472015-07-16 20:32:27 -070079 T* GetImageAddress(T* object) const SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080080 if (object == nullptr || IsInBootImage(object)) {
81 return object;
82 } else {
83 const char* oat_filename = GetOatFilename(object);
84 const ImageInfo& image_info = GetConstImageInfo(oat_filename);
85 return reinterpret_cast<T*>(image_info.image_begin_ + GetImageOffset(object));
86 }
Vladimir Markof4da6752014-08-01 19:04:18 +010087 }
88
Mathieu Chartier90443472015-07-16 20:32:27 -070089 ArtMethod* GetImageMethodAddress(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070090
Vladimir Marko05792b92015-08-03 11:56:49 +010091 template <typename PtrType>
92 PtrType GetDexCacheArrayElementImageAddress(const DexFile* dex_file, uint32_t offset)
93 const SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080094 auto oat_it = dex_file_oat_filename_map_.find(dex_file);
95 DCHECK(oat_it != dex_file_oat_filename_map_.end());
96 const ImageInfo& image_info = GetConstImageInfo(oat_it->second);
97 auto it = image_info.dex_cache_array_starts_.find(dex_file);
98 DCHECK(it != image_info.dex_cache_array_starts_.end());
Vladimir Marko05792b92015-08-03 11:56:49 +010099 return reinterpret_cast<PtrType>(
Jeff Haodcdc85b2015-12-04 14:06:18 -0800100 image_info.image_begin_ + image_info.bin_slot_offsets_[kBinDexCacheArray] +
101 it->second + offset);
Vladimir Marko20f85592015-03-19 10:07:02 +0000102 }
103
Jeff Haodcdc85b2015-12-04 14:06:18 -0800104 uint8_t* GetOatFileBegin(const char* oat_filename) const;
Vladimir Markof4da6752014-08-01 19:04:18 +0100105
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800106 // If image_fd is not kInvalidFd, then we use that for the image file. Otherwise we open
Jeff Haodcdc85b2015-12-04 14:06:18 -0800107 // the names in image_filenames.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800108 // If oat_fd is not kInvalidFd, then we use that for the oat file. Otherwise we open
109 // the names in oat_filenames.
Mathieu Chartiera90c7722015-10-29 15:41:36 -0700110 bool Write(int image_fd,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800111 const std::vector<const char*>& image_filenames,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800112 int oat_fd,
113 const std::vector<const char*>& oat_filenames,
114 const std::string& oat_location)
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 REQUIRES(!Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116
Jeff Haodcdc85b2015-12-04 14:06:18 -0800117 uintptr_t GetOatDataBegin(const char* oat_filename) {
118 return reinterpret_cast<uintptr_t>(GetImageInfo(oat_filename).oat_data_begin_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 }
120
Jeff Haodcdc85b2015-12-04 14:06:18 -0800121 const char* GetOatFilenameForDexCache(mirror::DexCache* dex_cache) const
122 SHARED_REQUIRES(Locks::mutator_lock_);
123
124 // Update the oat size for the given oat file. This will make the oat_offset for the next oat
125 // file valid.
126 void UpdateOatFile(const char* oat_filename);
127
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 private:
129 bool AllocMemory();
130
Mathieu Chartier31e89252013-08-28 11:29:12 -0700131 // Mark the objects defined in this space in the given live bitmap.
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700133
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800134 // Classify different kinds of bins that objects end up getting packed into during image writing.
135 enum Bin {
136 // Likely-clean:
137 kBinString, // [String] Almost always immutable (except for obj header).
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800138 // Unknown mix of clean/dirty:
139 kBinRegular,
140 // Likely-dirty:
141 // All classes get their own bins since their fields often dirty
142 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
143 kBinClassInitialized, // Class initializers have been run
144 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800145 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 // Non mirror fields must be below.
147 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700148 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149 // If the class is initialized, then the ArtMethods are probably clean.
150 kBinArtMethodClean,
151 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
152 // initialized.
153 kBinArtMethodDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100154 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
155 // huge, and as such their dirtiness is not important for the clean/dirty separation,
156 // we arbitrarily keep them at the end of the native data.
157 kBinDexCacheArray, // Arrays belonging to dex cache.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800158 kBinSize,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700159 // Number of bins which are for mirror objects.
160 kBinMirrorCount = kBinArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800161 };
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800162 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
163
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700164 enum NativeObjectRelocationType {
165 kNativeObjectRelocationTypeArtField,
166 kNativeObjectRelocationTypeArtFieldArray,
167 kNativeObjectRelocationTypeArtMethodClean,
168 kNativeObjectRelocationTypeArtMethodArrayClean,
169 kNativeObjectRelocationTypeArtMethodDirty,
170 kNativeObjectRelocationTypeArtMethodArrayDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100171 kNativeObjectRelocationTypeDexCacheArray,
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700172 };
173 friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type);
174
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800175 enum OatAddress {
176 kOatAddressInterpreterToInterpreterBridge,
177 kOatAddressInterpreterToCompiledCodeBridge,
178 kOatAddressJNIDlsymLookup,
179 kOatAddressQuickGenericJNITrampoline,
180 kOatAddressQuickIMTConflictTrampoline,
181 kOatAddressQuickResolutionTrampoline,
182 kOatAddressQuickToInterpreterBridge,
183 // Number of elements in the enum.
184 kOatAddressCount,
185 };
186 friend std::ostream& operator<<(std::ostream& stream, const OatAddress& oat_address);
187
Vladimir Marko80afd022015-05-19 18:08:00 +0100188 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800189 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700190 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
191 // failures due to invalid read barrier bits during object field reads.
192 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
193 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800194 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700195 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800196
197 // We use the lock word to store the bin # and bin index of the object in the image.
198 //
199 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
200 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
201 struct BinSlot {
202 explicit BinSlot(uint32_t lockword);
203 BinSlot(Bin bin, uint32_t index);
204
205 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
206 Bin GetBin() const;
207 // The offset in bytes from the beginning of the bin. Aligned to object size.
208 uint32_t GetIndex() const;
209 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700210 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800211 // Comparison operator for map support
212 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
213
214 private:
215 // Must be the same size as LockWord, any larger and we would truncate the data.
216 const uint32_t lockword_;
217 };
218
Jeff Haodcdc85b2015-12-04 14:06:18 -0800219 struct ImageInfo {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800220 ImageInfo();
221 ImageInfo(ImageInfo&&) = default;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800222
Mathieu Chartiera06ba052016-01-06 13:51:52 -0800223 // Create the image sections into the out sections variable, returns the size of the image
224 // excluding the bitmap.
225 size_t CreateImageSections(size_t target_ptr_size, ImageSection* out_sections) const;
226
Jeff Haodcdc85b2015-12-04 14:06:18 -0800227 std::unique_ptr<MemMap> image_; // Memory mapped for generating the image.
228
229 // Target begin of this image. Notes: It is not valid to write here, this is the address
230 // of the target image, not necessarily where image_ is mapped. The address is only valid
231 // after layouting (otherwise null).
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800232 uint8_t* image_begin_ = nullptr;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800233
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800234 // Offset to the free space in image_, initially size of image header.
235 size_t image_end_ = RoundUp(sizeof(ImageHeader), kObjectAlignment);
236 uint32_t image_roots_address_ = 0; // The image roots address in the image.
237 size_t image_offset_ = 0; // Offset of this image from the start of the first image.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800238
239 // Image size is the *address space* covered by this image. As the live bitmap is aligned
240 // to the page size, the live bitmap will cover more address space than necessary. But live
241 // bitmaps may not overlap, so an image has a "shadow," which is accounted for in the size.
242 // The next image may only start at image_begin_ + image_size_ (which is guaranteed to be
243 // page-aligned).
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800244 size_t image_size_ = 0;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800245
246 // Oat data.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800247 // Offset of the oat file for this image from start of oat files. This is
248 // valid when the previous oat file has been written.
249 size_t oat_offset_ = 0;
250 // Start of oatdata in the corresponding oat file. This is
251 // valid when the images have been layed out.
252 uint8_t* oat_data_begin_ = nullptr;
253 size_t oat_size_ = 0; // Size of the corresponding oat data.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800254
255 // Image bitmap which lets us know where the objects inside of the image reside.
256 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
257
258 // The start offsets of the dex cache arrays.
259 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
260
261 // Offset from oat_data_begin_ to the stubs.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800262 uint32_t oat_address_offsets_[kOatAddressCount] = {};
Jeff Haodcdc85b2015-12-04 14:06:18 -0800263
264 // Bin slot tracking for dirty object packing.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800265 size_t bin_slot_sizes_[kBinSize] = {}; // Number of bytes in a bin.
266 size_t bin_slot_offsets_[kBinSize] = {}; // Number of bytes in previous bins.
267 size_t bin_slot_count_[kBinSize] = {}; // Number of objects in a bin.
268
269 // Cached size of the intern table for when we allocate memory.
270 size_t intern_table_bytes_ = 0;
271
Mathieu Chartier1f47b672016-01-07 16:29:01 -0800272 // Number of image class table bytes.
273 size_t class_table_bytes_ = 0;
274
275 // Intern table associated with this image for serialization.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800276 std::unique_ptr<InternTable> intern_table_;
Mathieu Chartier1f47b672016-01-07 16:29:01 -0800277
278 // Class table associated with this image for serialization.
279 std::unique_ptr<ClassTable> class_table_;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800280 };
281
Mathieu Chartier31e89252013-08-28 11:29:12 -0700282 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800283 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700284 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700285 void SetImageOffset(mirror::Object* object, size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700286 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700287 bool IsImageOffsetAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700288 SHARED_REQUIRES(Locks::mutator_lock_);
289 size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700291 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292
Mathieu Chartier90443472015-07-16 20:32:27 -0700293 void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_);
294 void AssignImageBinSlot(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800295 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700296 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800297 bool IsImageBinSlotAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700298 SHARED_REQUIRES(Locks::mutator_lock_);
299 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800300
Jeff Haodcdc85b2015-12-04 14:06:18 -0800301 void AddDexCacheArrayRelocation(void* array, size_t offset, mirror::DexCache* dex_cache)
302 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700303 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304
Alex Lighta59dd802014-07-02 16:28:08 -0700305 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700306 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Lighta59dd802014-07-02 16:28:08 -0700307 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
308 }
309
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700310 mirror::Object* GetLocalAddress(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700311 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 size_t offset = GetImageOffset(object);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800313 const char* oat_filename = GetOatFilename(object);
314 const ImageInfo& image_info = GetConstImageInfo(oat_filename);
315 uint8_t* dst = image_info.image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 return reinterpret_cast<mirror::Object*>(dst);
317 }
318
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800319 // Returns the address in the boot image if we are compiling the app image.
320 const uint8_t* GetOatAddress(OatAddress type) const;
321
Jeff Haodcdc85b2015-12-04 14:06:18 -0800322 const uint8_t* GetOatAddressForOffset(uint32_t offset, const ImageInfo& image_info) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 // With Quick, code is within the OatFile, as there are all in one
Jeff Haodcdc85b2015-12-04 14:06:18 -0800324 // .o ELF object. But interpret it as signed.
325 DCHECK_LE(static_cast<int32_t>(offset), static_cast<int32_t>(image_info.oat_size_));
326 DCHECK(image_info.oat_data_begin_ != nullptr);
327 return offset == 0u ? nullptr : image_info.oat_data_begin_ + static_cast<int32_t>(offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700328 }
329
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 // Returns true if the class was in the original requested image classes list.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800331 bool KeepClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332
333 // Debug aid that list of requested image classes.
334 void DumpImageClasses();
335
336 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
337 void ComputeLazyFieldsForImageClasses()
Mathieu Chartier90443472015-07-16 20:32:27 -0700338 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 // Remove unwanted classes from various roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700341 void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342
343 // Verify unwanted classes removed.
Mathieu Chartier90443472015-07-16 20:32:27 -0700344 void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700346 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347
348 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100349 void CalculateNewObjectOffsets()
Mathieu Chartier90443472015-07-16 20:32:27 -0700350 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100351 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700352 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800353 mirror::ObjectArray<mirror::Object>* CreateImageRoots(const char* oat_filename) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700354 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800355 void CalculateObjectBinSlots(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700356 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800357 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700358 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700359
360 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700361 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700362 void WalkFieldsInOrder(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700363 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700365 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800366 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700367 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368
369 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartier90443472015-07-16 20:32:27 -0700370 void CopyAndFixupNativeData() SHARED_REQUIRES(Locks::mutator_lock_);
371 void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700373 SHARED_REQUIRES(Locks::mutator_lock_);
374 void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800375 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy, const ImageInfo& image_info)
Mathieu Chartier90443472015-07-16 20:32:27 -0700376 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700377 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700378 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800379 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700380 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100381 void FixupDexCache(mirror::DexCache* orig_dex_cache, mirror::DexCache* copy_dex_cache)
382 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800383 void FixupPointerArray(mirror::Object* dst,
384 mirror::PointerArray* arr,
385 mirror::Class* klass,
386 Bin array_type)
387 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700389 // Get quick code for non-resolution/imt_conflict/abstract method.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800390 const uint8_t* GetQuickCode(ArtMethod* method,
391 const ImageInfo& image_info,
392 bool* quick_is_interpreted)
Mathieu Chartier90443472015-07-16 20:32:27 -0700393 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700394
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100396 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800398 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800399 size_t GetBinSizeSum(ImageInfo& image_info, Bin up_to = kBinSize) const;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800400
Mathieu Chartiere401d142015-04-22 13:56:20 -0700401 // Return true if a method is likely to be dirtied at runtime.
Mathieu Chartier90443472015-07-16 20:32:27 -0700402 bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403
404 // Assign the offset for an ArtMethod.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800405 void AssignMethodOffset(ArtMethod* method,
406 NativeObjectRelocationType type,
407 const char* oat_filename)
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700408 SHARED_REQUIRES(Locks::mutator_lock_);
409
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800410 // Return true if klass is loaded by the boot class loader but not in the boot image.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800411 bool IsBootClassLoaderNonImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
412
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800413 // Return true if klass depends on a boot class loader non image class live. We want to prune
414 // these classes since we do not want any boot class loader classes in the image. This means that
415 // we also cannot have any classes which refer to these boot class loader non image classes.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800416 bool ContainsBootClassLoaderNonImageClass(mirror::Class* klass)
417 SHARED_REQUIRES(Locks::mutator_lock_);
418
Mathieu Chartier945c1c12015-11-24 15:37:12 -0800419 // early_exit is true if we had a cyclic dependency anywhere down the chain.
420 bool ContainsBootClassLoaderNonImageClassInternal(mirror::Class* klass,
421 bool* early_exit,
422 std::unordered_set<mirror::Class*>* visited)
423 SHARED_REQUIRES(Locks::mutator_lock_);
424
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700425 static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
426
Vladimir Marko05792b92015-08-03 11:56:49 +0100427 uintptr_t NativeOffsetInImage(void* obj);
428
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800429 // Location of where the object will be when the image is loaded at runtime.
Vladimir Marko05792b92015-08-03 11:56:49 +0100430 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800431 T* NativeLocationInImage(T* obj, const char* oat_filename) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe245ee002014-12-04 21:25:04 -0800432
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800433 // Location of where the temporary copy of the object currently is.
434 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800435 T* NativeCopyLocation(T* obj, mirror::DexCache* dex_cache) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800436
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800437 // Return true of obj is inside of the boot image space. This may only return true if we are
438 // compiling an app image.
439 bool IsInBootImage(const void* obj) const;
440
441 // Return true if ptr is within the boot oat file.
442 bool IsInBootOatFile(const void* ptr) const;
443
Jeff Haodcdc85b2015-12-04 14:06:18 -0800444 const char* GetOatFilename(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
445
446 const char* GetDefaultOatFilename() const {
447 return default_oat_filename_;
448 }
449
450 ImageInfo& GetImageInfo(const char* oat_filename);
451 const ImageInfo& GetConstImageInfo(const char* oat_filename) const;
452 const ImageInfo& GetImageInfo(size_t index) const;
453
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800454 // Find an already strong interned string in the other images or in the boot image. Used to
455 // remove duplicates in the multi image and app image case.
456 mirror::String* FindInternedString(mirror::String* string) SHARED_REQUIRES(Locks::mutator_lock_);
457
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 const CompilerDriver& compiler_driver_;
459
Jeff Haodcdc85b2015-12-04 14:06:18 -0800460 // Beginning target image address for the first image.
461 uint8_t* global_image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100462
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800463 // Offset from image_begin_ to where the first object is in image_.
464 size_t image_objects_offset_begin_;
465
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 // oat file with code for this image
467 OatFile* oat_file_;
468
Mathieu Chartiere401d142015-04-22 13:56:20 -0700469 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
470 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
471 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
472
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700473 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
474 // forwarding addresses as well as copying over hash codes.
475 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800476
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800477 // Boolean flags.
Igor Murashkin46774762014-10-22 11:37:02 -0700478 const bool compile_pic_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800479 const bool compile_app_image_;
480
Mathieu Chartier2d721012014-11-10 11:08:06 -0800481 // Size of pointers on the target architecture.
482 size_t target_ptr_size_;
483
Jeff Haodcdc85b2015-12-04 14:06:18 -0800484 // Mapping of oat filename to image data.
485 std::unordered_map<std::string, ImageInfo> image_info_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800486
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
488 // have one entry per art field for convenience. ArtFields are placed right after the end of the
489 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700490 struct NativeObjectRelocation {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800491 const char* oat_filename;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700492 uintptr_t offset;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700493 NativeObjectRelocationType type;
494
495 bool IsArtMethodRelocation() const {
496 return type == kNativeObjectRelocationTypeArtMethodClean ||
497 type == kNativeObjectRelocationTypeArtMethodDirty;
498 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700499 };
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700500 std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700501
Mathieu Chartiere401d142015-04-22 13:56:20 -0700502 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
503 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -0700504 // Fake length prefixed array for image methods. This array does not contain the actual
505 // ArtMethods. We only use it for the header and relocation addresses.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700506 LengthPrefixedArray<ArtMethod> image_method_array_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507
508 // Counters for measurements, used for logging only.
509 uint64_t dirty_methods_;
510 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800511
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800512 // Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800513 std::unordered_map<mirror::Class*, bool> prune_class_memo_;
514
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800515 // Class loaders with a class table to write out. There should only be one class loader because
516 // dex2oat loads the dex files to be compiled into a single class loader. For the boot image,
517 // null is a valid entry.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800518 std::unordered_set<mirror::ClassLoader*> class_loaders_;
519
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800520 // Which mode the image is stored as, see image.h
521 const ImageHeader::StorageMode image_storage_mode_;
522
Jeff Haodcdc85b2015-12-04 14:06:18 -0800523 // Map of dex files to the oat filenames that they were compiled into.
524 const std::unordered_map<const DexFile*, const char*>& dex_file_oat_filename_map_;
525 const std::vector<const char*> oat_filenames_;
526 const char* default_oat_filename_;
527
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800528 friend class ContainsBootClassLoaderNonImageClassVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700529 friend class FixupClassVisitor;
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700530 friend class FixupRootVisitor;
531 friend class FixupVisitor;
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800532 friend class NativeLocationVisitor;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700533 friend class NonImageClassesVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700534 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535};
536
537} // namespace art
538
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700539#endif // ART_COMPILER_IMAGE_WRITER_H_