blob: f491a5a29611c85ea70e78752b8f2837088e6e18 [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 Chartiera90c7722015-10-29 15:41:36 -070052static constexpr int kInvalidImageFd = -1;
53
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 Chartiera90c7722015-10-29 15:41:36 -0700106 // If image_fd is not kInvalidImageFd, then we use that for the file. Otherwise we open
Jeff Haodcdc85b2015-12-04 14:06:18 -0800107 // the names in image_filenames.
Mathieu Chartiera90c7722015-10-29 15:41:36 -0700108 bool Write(int image_fd,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800109 const std::vector<const char*>& image_filenames,
110 const std::vector<const char*>& oat_filenames)
Mathieu Chartier90443472015-07-16 20:32:27 -0700111 REQUIRES(!Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112
Jeff Haodcdc85b2015-12-04 14:06:18 -0800113 uintptr_t GetOatDataBegin(const char* oat_filename) {
114 return reinterpret_cast<uintptr_t>(GetImageInfo(oat_filename).oat_data_begin_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 }
116
Jeff Haodcdc85b2015-12-04 14:06:18 -0800117 const char* GetOatFilenameForDexCache(mirror::DexCache* dex_cache) const
118 SHARED_REQUIRES(Locks::mutator_lock_);
119
120 // Update the oat size for the given oat file. This will make the oat_offset for the next oat
121 // file valid.
122 void UpdateOatFile(const char* oat_filename);
123
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 private:
125 bool AllocMemory();
126
Mathieu Chartier31e89252013-08-28 11:29:12 -0700127 // Mark the objects defined in this space in the given live bitmap.
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700129
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800130 // Classify different kinds of bins that objects end up getting packed into during image writing.
131 enum Bin {
132 // Likely-clean:
133 kBinString, // [String] Almost always immutable (except for obj header).
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800134 // Unknown mix of clean/dirty:
135 kBinRegular,
136 // Likely-dirty:
137 // All classes get their own bins since their fields often dirty
138 kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
139 kBinClassInitialized, // Class initializers have been run
140 kBinClassVerified, // Class verified, but initializers haven't been run
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800141 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142 // Non mirror fields must be below.
143 // ArtFields should be always clean.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700144 kBinArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145 // If the class is initialized, then the ArtMethods are probably clean.
146 kBinArtMethodClean,
147 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
148 // initialized.
149 kBinArtMethodDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100150 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
151 // huge, and as such their dirtiness is not important for the clean/dirty separation,
152 // we arbitrarily keep them at the end of the native data.
153 kBinDexCacheArray, // Arrays belonging to dex cache.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800154 kBinSize,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700155 // Number of bins which are for mirror objects.
156 kBinMirrorCount = kBinArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800157 };
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800158 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
159
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700160 enum NativeObjectRelocationType {
161 kNativeObjectRelocationTypeArtField,
162 kNativeObjectRelocationTypeArtFieldArray,
163 kNativeObjectRelocationTypeArtMethodClean,
164 kNativeObjectRelocationTypeArtMethodArrayClean,
165 kNativeObjectRelocationTypeArtMethodDirty,
166 kNativeObjectRelocationTypeArtMethodArrayDirty,
Vladimir Marko05792b92015-08-03 11:56:49 +0100167 kNativeObjectRelocationTypeDexCacheArray,
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700168 };
169 friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type);
170
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800171 enum OatAddress {
172 kOatAddressInterpreterToInterpreterBridge,
173 kOatAddressInterpreterToCompiledCodeBridge,
174 kOatAddressJNIDlsymLookup,
175 kOatAddressQuickGenericJNITrampoline,
176 kOatAddressQuickIMTConflictTrampoline,
177 kOatAddressQuickResolutionTrampoline,
178 kOatAddressQuickToInterpreterBridge,
179 // Number of elements in the enum.
180 kOatAddressCount,
181 };
182 friend std::ostream& operator<<(std::ostream& stream, const OatAddress& oat_address);
183
Vladimir Marko80afd022015-05-19 18:08:00 +0100184 static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800185 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
187 // failures due to invalid read barrier bits during object field reads.
188 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits -
189 LockWord::kReadBarrierStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800190 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700191 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800192
193 // We use the lock word to store the bin # and bin index of the object in the image.
194 //
195 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
196 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
197 struct BinSlot {
198 explicit BinSlot(uint32_t lockword);
199 BinSlot(Bin bin, uint32_t index);
200
201 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
202 Bin GetBin() const;
203 // The offset in bytes from the beginning of the bin. Aligned to object size.
204 uint32_t GetIndex() const;
205 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700206 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800207 // Comparison operator for map support
208 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
209
210 private:
211 // Must be the same size as LockWord, any larger and we would truncate the data.
212 const uint32_t lockword_;
213 };
214
Jeff Haodcdc85b2015-12-04 14:06:18 -0800215 struct ImageInfo {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800216 ImageInfo();
217 ImageInfo(ImageInfo&&) = default;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800218
Mathieu Chartiera06ba052016-01-06 13:51:52 -0800219 // Create the image sections into the out sections variable, returns the size of the image
220 // excluding the bitmap.
221 size_t CreateImageSections(size_t target_ptr_size, ImageSection* out_sections) const;
222
Jeff Haodcdc85b2015-12-04 14:06:18 -0800223 std::unique_ptr<MemMap> image_; // Memory mapped for generating the image.
224
225 // Target begin of this image. Notes: It is not valid to write here, this is the address
226 // of the target image, not necessarily where image_ is mapped. The address is only valid
227 // after layouting (otherwise null).
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800228 uint8_t* image_begin_ = nullptr;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800229
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800230 // Offset to the free space in image_, initially size of image header.
231 size_t image_end_ = RoundUp(sizeof(ImageHeader), kObjectAlignment);
232 uint32_t image_roots_address_ = 0; // The image roots address in the image.
233 size_t image_offset_ = 0; // Offset of this image from the start of the first image.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800234
235 // Image size is the *address space* covered by this image. As the live bitmap is aligned
236 // to the page size, the live bitmap will cover more address space than necessary. But live
237 // bitmaps may not overlap, so an image has a "shadow," which is accounted for in the size.
238 // The next image may only start at image_begin_ + image_size_ (which is guaranteed to be
239 // page-aligned).
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800240 size_t image_size_ = 0;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800241
242 // Oat data.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800243 // Offset of the oat file for this image from start of oat files. This is
244 // valid when the previous oat file has been written.
245 size_t oat_offset_ = 0;
246 // Start of oatdata in the corresponding oat file. This is
247 // valid when the images have been layed out.
248 uint8_t* oat_data_begin_ = nullptr;
249 size_t oat_size_ = 0; // Size of the corresponding oat data.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800250
251 // Image bitmap which lets us know where the objects inside of the image reside.
252 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
253
254 // The start offsets of the dex cache arrays.
255 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
256
257 // Offset from oat_data_begin_ to the stubs.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800258 uint32_t oat_address_offsets_[kOatAddressCount] = {};
Jeff Haodcdc85b2015-12-04 14:06:18 -0800259
260 // Bin slot tracking for dirty object packing.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800261 size_t bin_slot_sizes_[kBinSize] = {}; // Number of bytes in a bin.
262 size_t bin_slot_offsets_[kBinSize] = {}; // Number of bytes in previous bins.
263 size_t bin_slot_count_[kBinSize] = {}; // Number of objects in a bin.
264
265 // Cached size of the intern table for when we allocate memory.
266 size_t intern_table_bytes_ = 0;
267
Mathieu Chartier1f47b672016-01-07 16:29:01 -0800268 // Number of image class table bytes.
269 size_t class_table_bytes_ = 0;
270
271 // Intern table associated with this image for serialization.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800272 std::unique_ptr<InternTable> intern_table_;
Mathieu Chartier1f47b672016-01-07 16:29:01 -0800273
274 // Class table associated with this image for serialization.
275 std::unique_ptr<ClassTable> class_table_;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800276 };
277
Mathieu Chartier31e89252013-08-28 11:29:12 -0700278 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800279 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700280 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700281 void SetImageOffset(mirror::Object* object, size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700283 bool IsImageOffsetAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700284 SHARED_REQUIRES(Locks::mutator_lock_);
285 size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700286 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700287 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288
Mathieu Chartier90443472015-07-16 20:32:27 -0700289 void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_);
290 void AssignImageBinSlot(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800291 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
Mathieu Chartier90443472015-07-16 20:32:27 -0700292 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800293 bool IsImageBinSlotAssigned(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700294 SHARED_REQUIRES(Locks::mutator_lock_);
295 BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800296
Jeff Haodcdc85b2015-12-04 14:06:18 -0800297 void AddDexCacheArrayRelocation(void* array, size_t offset, mirror::DexCache* dex_cache)
298 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700299 void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300
Alex Lighta59dd802014-07-02 16:28:08 -0700301 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700302 SHARED_REQUIRES(Locks::mutator_lock_) {
Alex Lighta59dd802014-07-02 16:28:08 -0700303 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
304 }
305
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700306 mirror::Object* GetLocalAddress(mirror::Object* object) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700307 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 size_t offset = GetImageOffset(object);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800309 const char* oat_filename = GetOatFilename(object);
310 const ImageInfo& image_info = GetConstImageInfo(oat_filename);
311 uint8_t* dst = image_info.image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 return reinterpret_cast<mirror::Object*>(dst);
313 }
314
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800315 // Returns the address in the boot image if we are compiling the app image.
316 const uint8_t* GetOatAddress(OatAddress type) const;
317
Jeff Haodcdc85b2015-12-04 14:06:18 -0800318 const uint8_t* GetOatAddressForOffset(uint32_t offset, const ImageInfo& image_info) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 // With Quick, code is within the OatFile, as there are all in one
Jeff Haodcdc85b2015-12-04 14:06:18 -0800320 // .o ELF object. But interpret it as signed.
321 DCHECK_LE(static_cast<int32_t>(offset), static_cast<int32_t>(image_info.oat_size_));
322 DCHECK(image_info.oat_data_begin_ != nullptr);
323 return offset == 0u ? nullptr : image_info.oat_data_begin_ + static_cast<int32_t>(offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 }
325
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 // Returns true if the class was in the original requested image classes list.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800327 bool KeepClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328
329 // Debug aid that list of requested image classes.
330 void DumpImageClasses();
331
332 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
333 void ComputeLazyFieldsForImageClasses()
Mathieu Chartier90443472015-07-16 20:32:27 -0700334 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 // Remove unwanted classes from various roots.
Mathieu Chartier90443472015-07-16 20:32:27 -0700337 void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338
339 // Verify unwanted classes removed.
Mathieu Chartier90443472015-07-16 20:32:27 -0700340 void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700342 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343
344 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100345 void CalculateNewObjectOffsets()
Mathieu Chartier90443472015-07-16 20:32:27 -0700346 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100347 void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700348 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800349 mirror::ObjectArray<mirror::Object>* CreateImageRoots(const char* oat_filename) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700350 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800351 void CalculateObjectBinSlots(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700352 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800353 void UnbinObjectsIntoOffset(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700354 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700355
356 void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700357 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700358 void WalkFieldsInOrder(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700359 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700360 static void WalkFieldsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700361 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800362 static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700363 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364
365 // Creates the contiguous image in memory and adjusts pointers.
Mathieu Chartier90443472015-07-16 20:32:27 -0700366 void CopyAndFixupNativeData() SHARED_REQUIRES(Locks::mutator_lock_);
367 void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700369 SHARED_REQUIRES(Locks::mutator_lock_);
370 void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800371 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy, const ImageInfo& image_info)
Mathieu Chartier90443472015-07-16 20:32:27 -0700372 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700373 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700374 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800375 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Mathieu Chartier90443472015-07-16 20:32:27 -0700376 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100377 void FixupDexCache(mirror::DexCache* orig_dex_cache, mirror::DexCache* copy_dex_cache)
378 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800379 void FixupPointerArray(mirror::Object* dst,
380 mirror::PointerArray* arr,
381 mirror::Class* klass,
382 Bin array_type)
383 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700385 // Get quick code for non-resolution/imt_conflict/abstract method.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800386 const uint8_t* GetQuickCode(ArtMethod* method,
387 const ImageInfo& image_info,
388 bool* quick_is_interpreted)
Mathieu Chartier90443472015-07-16 20:32:27 -0700389 SHARED_REQUIRES(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700390
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 // Patches references in OatFile to expect runtime addresses.
Vladimir Markof4da6752014-08-01 19:04:18 +0100392 void SetOatChecksumFromElfFile(File* elf_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800394 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800395 size_t GetBinSizeSum(ImageInfo& image_info, Bin up_to = kBinSize) const;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800396
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 // Return true if a method is likely to be dirtied at runtime.
Mathieu Chartier90443472015-07-16 20:32:27 -0700398 bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399
400 // Assign the offset for an ArtMethod.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800401 void AssignMethodOffset(ArtMethod* method,
402 NativeObjectRelocationType type,
403 const char* oat_filename)
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700404 SHARED_REQUIRES(Locks::mutator_lock_);
405
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800406 // Return true if klass is loaded by the boot class loader but not in the boot image.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800407 bool IsBootClassLoaderNonImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
408
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800409 // Return true if klass depends on a boot class loader non image class live. We want to prune
410 // these classes since we do not want any boot class loader classes in the image. This means that
411 // we also cannot have any classes which refer to these boot class loader non image classes.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800412 bool ContainsBootClassLoaderNonImageClass(mirror::Class* klass)
413 SHARED_REQUIRES(Locks::mutator_lock_);
414
Mathieu Chartier945c1c12015-11-24 15:37:12 -0800415 // early_exit is true if we had a cyclic dependency anywhere down the chain.
416 bool ContainsBootClassLoaderNonImageClassInternal(mirror::Class* klass,
417 bool* early_exit,
418 std::unordered_set<mirror::Class*>* visited)
419 SHARED_REQUIRES(Locks::mutator_lock_);
420
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700421 static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
422
Vladimir Marko05792b92015-08-03 11:56:49 +0100423 uintptr_t NativeOffsetInImage(void* obj);
424
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800425 // Location of where the object will be when the image is loaded at runtime.
Vladimir Marko05792b92015-08-03 11:56:49 +0100426 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800427 T* NativeLocationInImage(T* obj, const char* oat_filename) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe245ee002014-12-04 21:25:04 -0800428
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800429 // Location of where the temporary copy of the object currently is.
430 template <typename T>
Jeff Haodcdc85b2015-12-04 14:06:18 -0800431 T* NativeCopyLocation(T* obj, mirror::DexCache* dex_cache) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800432
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800433 // Return true of obj is inside of the boot image space. This may only return true if we are
434 // compiling an app image.
435 bool IsInBootImage(const void* obj) const;
436
437 // Return true if ptr is within the boot oat file.
438 bool IsInBootOatFile(const void* ptr) const;
439
Jeff Haodcdc85b2015-12-04 14:06:18 -0800440 const char* GetOatFilename(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_);
441
442 const char* GetDefaultOatFilename() const {
443 return default_oat_filename_;
444 }
445
446 ImageInfo& GetImageInfo(const char* oat_filename);
447 const ImageInfo& GetConstImageInfo(const char* oat_filename) const;
448 const ImageInfo& GetImageInfo(size_t index) const;
449
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 const CompilerDriver& compiler_driver_;
451
Jeff Haodcdc85b2015-12-04 14:06:18 -0800452 // Beginning target image address for the first image.
453 uint8_t* global_image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100454
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800455 // Offset from image_begin_ to where the first object is in image_.
456 size_t image_objects_offset_begin_;
457
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 // oat file with code for this image
459 OatFile* oat_file_;
460
Mathieu Chartiere401d142015-04-22 13:56:20 -0700461 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
462 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
463 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
464
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700465 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
466 // forwarding addresses as well as copying over hash codes.
467 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800468
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800469 // Boolean flags.
Igor Murashkin46774762014-10-22 11:37:02 -0700470 const bool compile_pic_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800471 const bool compile_app_image_;
472
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800473 // Cache the boot image space in this class for faster lookups.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800474 gc::space::ImageSpace* boot_image_space_;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700475
Mathieu Chartier2d721012014-11-10 11:08:06 -0800476 // Size of pointers on the target architecture.
477 size_t target_ptr_size_;
478
Jeff Haodcdc85b2015-12-04 14:06:18 -0800479 // Mapping of oat filename to image data.
480 std::unordered_map<std::string, ImageInfo> image_info_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800481
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
483 // have one entry per art field for convenience. ArtFields are placed right after the end of the
484 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700485 struct NativeObjectRelocation {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800486 const char* oat_filename;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 uintptr_t offset;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700488 NativeObjectRelocationType type;
489
490 bool IsArtMethodRelocation() const {
491 return type == kNativeObjectRelocationTypeArtMethodClean ||
492 type == kNativeObjectRelocationTypeArtMethodDirty;
493 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700494 };
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700495 std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700496
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
498 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -0700499 // Fake length prefixed array for image methods. This array does not contain the actual
500 // ArtMethods. We only use it for the header and relocation addresses.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700501 LengthPrefixedArray<ArtMethod> image_method_array_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700502
503 // Counters for measurements, used for logging only.
504 uint64_t dirty_methods_;
505 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800506
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800507 // Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800508 std::unordered_map<mirror::Class*, bool> prune_class_memo_;
509
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800510 // Class loaders with a class table to write out. There should only be one class loader because
511 // dex2oat loads the dex files to be compiled into a single class loader. For the boot image,
512 // null is a valid entry.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800513 std::unordered_set<mirror::ClassLoader*> class_loaders_;
514
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800515 // Which mode the image is stored as, see image.h
516 const ImageHeader::StorageMode image_storage_mode_;
517
Jeff Haodcdc85b2015-12-04 14:06:18 -0800518 // Map of dex files to the oat filenames that they were compiled into.
519 const std::unordered_map<const DexFile*, const char*>& dex_file_oat_filename_map_;
520 const std::vector<const char*> oat_filenames_;
521 const char* default_oat_filename_;
522
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800523 friend class ContainsBootClassLoaderNonImageClassVisitor;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700524 friend class FixupClassVisitor;
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700525 friend class FixupRootVisitor;
526 friend class FixupVisitor;
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800527 friend class NativeLocationVisitor;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700528 friend class NonImageClassesVisitor;
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700529 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530};
531
532} // namespace art
533
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700534#endif // ART_COMPILER_IMAGE_WRITER_H_