blob: 2fcf5fd56a27a991404e5c0e7a60e2187158630f [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
Vladimir Marko74527972016-11-29 15:57:32 +000017#ifndef ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
18#define ART_DEX2OAT_LINKER_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>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include <ostream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include <set>
Mathieu Chartier496577f2016-09-20 15:33:31 -070027#include <stack>
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include <string>
29
Alex Lightdba61482016-12-21 08:20:29 -080030#include "art_method.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010031#include "base/bit_utils.h"
Vladimir Marko944da602016-02-19 12:27:55 +000032#include "base/dchecked_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070033#include "base/enums.h"
Vladimir Marko54159c62018-06-20 14:30:08 +010034#include "base/hash_set.h"
Alex Lighte64300b2015-12-15 15:02:47 -080035#include "base/length_prefixed_array.h"
Igor Murashkin46774762014-10-22 11:37:02 -070036#include "base/macros.h"
David Sehr79e26072018-04-06 17:58:50 -070037#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080038#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080039#include "base/safe_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080040#include "base/utils.h"
Andreas Gampe2ff3b972017-06-05 18:14:53 -070041#include "class_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070042#include "driver/compiler_driver.h"
Mathieu Chartierceb07b32015-12-10 09:33:21 -080043#include "image.h"
Andreas Gampeb2d18fa2017-06-06 20:46:10 -070044#include "intern_table.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070045#include "lock_word.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070046#include "mirror/dex_cache.h"
Mathieu Chartier8c19d242017-03-06 12:35:10 -080047#include "oat_file.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070048#include "obj_ptr.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070049
50namespace art {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080051namespace gc {
Andreas Gamped4901292017-05-30 18:41:34 -070052namespace accounting {
53template <size_t kAlignment> class SpaceBitmap;
54typedef SpaceBitmap<kObjectAlignment> ContinuousSpaceBitmap;
55} // namespace accounting
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080056namespace space {
57class ImageSpace;
58} // namespace space
59} // namespace gc
Brian Carlstrom7940e442013-07-12 13:46:57 -070060
Vladimir Marko8d6768d2017-03-14 10:13:21 +000061namespace mirror {
62class ClassLoader;
63} // namespace mirror
64
Vladimir Markoc5798bf2016-12-09 10:20:54 +000065class ClassLoaderVisitor;
Vladimir Marko2c64a832018-01-04 11:31:56 +000066class ImTable;
Vladimir Marko8d6768d2017-03-14 10:13:21 +000067class ImtConflictTable;
Mathieu Chartier703b82a2018-04-10 15:52:32 -070068class TimingLogger;
Mathieu Chartier1f47b672016-01-07 16:29:01 -080069
Mathieu Chartierfbc31082016-01-24 11:59:56 -080070static constexpr int kInvalidFd = -1;
Mathieu Chartiera90c7722015-10-29 15:41:36 -070071
Vladimir Marko74527972016-11-29 15:57:32 +000072namespace linker {
73
Brian Carlstrom7940e442013-07-12 13:46:57 -070074// Write a Space built during compilation for use during execution.
Igor Murashkin46774762014-10-22 11:37:02 -070075class ImageWriter FINAL {
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 public:
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080077 ImageWriter(const CompilerDriver& compiler_driver,
78 uintptr_t image_begin,
79 bool compile_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080080 bool compile_app_image,
Jeff Haodcdc85b2015-12-04 14:06:18 -080081 ImageHeader::StorageMode image_storage_mode,
Vladimir Marko944da602016-02-19 12:27:55 +000082 const std::vector<const char*>& oat_filenames,
Jeff Haoc23b0c02017-07-27 18:19:38 -070083 const std::unordered_map<const DexFile*, size_t>& dex_file_oat_index_map,
Vladimir Marko54159c62018-06-20 14:30:08 +010084 const HashSet<std::string>* dirty_image_objects);
Brian Carlstrom7940e442013-07-12 13:46:57 -070085
Mathieu Chartier703b82a2018-04-10 15:52:32 -070086 bool PrepareImageAddressSpace(TimingLogger* timings);
Vladimir Markof4da6752014-08-01 19:04:18 +010087
88 bool IsImageAddressSpaceReady() const {
Vladimir Marko944da602016-02-19 12:27:55 +000089 DCHECK(!image_infos_.empty());
90 for (const ImageInfo& image_info : image_infos_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080091 if (image_info.image_roots_address_ == 0u) {
92 return false;
93 }
94 }
Vladimir Marko944da602016-02-19 12:27:55 +000095 return true;
Vladimir Markof4da6752014-08-01 19:04:18 +010096 }
97
Vladimir Marko8d6768d2017-03-14 10:13:21 +000098 ObjPtr<mirror::ClassLoader> GetClassLoader() {
99 CHECK_EQ(class_loaders_.size(), compile_app_image_ ? 1u : 0u);
100 return compile_app_image_ ? *class_loaders_.begin() : nullptr;
101 }
102
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103 template <typename T>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 T* GetImageAddress(T* object) const REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800105 if (object == nullptr || IsInBootImage(object)) {
106 return object;
107 } else {
Vladimir Marko944da602016-02-19 12:27:55 +0000108 size_t oat_index = GetOatIndex(object);
109 const ImageInfo& image_info = GetImageInfo(oat_index);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800110 return reinterpret_cast<T*>(image_info.image_begin_ + GetImageOffset(object));
111 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100112 }
113
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 ArtMethod* GetImageMethodAddress(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115
Vladimir Marko944da602016-02-19 12:27:55 +0000116 size_t GetOatFileOffset(size_t oat_index) const {
117 return GetImageInfo(oat_index).oat_offset_;
118 }
119
120 const uint8_t* GetOatFileBegin(size_t oat_index) const {
121 return GetImageInfo(oat_index).oat_file_begin_;
122 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100123
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800124 // If image_fd is not kInvalidFd, then we use that for the image file. Otherwise we open
Jeff Haodcdc85b2015-12-04 14:06:18 -0800125 // the names in image_filenames.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800126 // If oat_fd is not kInvalidFd, then we use that for the oat file. Otherwise we open
127 // the names in oat_filenames.
Mathieu Chartiera90c7722015-10-29 15:41:36 -0700128 bool Write(int image_fd,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800129 const std::vector<const char*>& image_filenames,
Vladimir Marko944da602016-02-19 12:27:55 +0000130 const std::vector<const char*>& oat_filenames)
Mathieu Chartier90443472015-07-16 20:32:27 -0700131 REQUIRES(!Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132
Vladimir Marko944da602016-02-19 12:27:55 +0000133 uintptr_t GetOatDataBegin(size_t oat_index) {
134 return reinterpret_cast<uintptr_t>(GetImageInfo(oat_index).oat_data_begin_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 }
136
Vladimir Marko944da602016-02-19 12:27:55 +0000137 // Get the index of the oat file containing the dex file.
138 //
139 // This "oat_index" is used to retrieve information about the the memory layout
140 // of the oat file and its associated image file, needed for link-time patching
141 // of references to the image or across oat files.
142 size_t GetOatIndexForDexFile(const DexFile* dex_file) const;
143
144 // Get the index of the oat file containing the dex file served by the dex cache.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700145 size_t GetOatIndexForDexCache(ObjPtr<mirror::DexCache> dex_cache) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800147
Vladimir Marko944da602016-02-19 12:27:55 +0000148 // Update the oat layout for the given oat file.
149 // This will make the oat_offset for the next oat file valid.
150 void UpdateOatFileLayout(size_t oat_index,
151 size_t oat_loaded_size,
152 size_t oat_data_offset,
153 size_t oat_data_size);
154 // Update information about the oat header, i.e. checksum and trampoline offsets.
155 void UpdateOatFileHeader(size_t oat_index, const OatHeader& oat_header);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800156
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 private:
Mathieu Chartier496577f2016-09-20 15:33:31 -0700158 using WorkStack = std::stack<std::pair<mirror::Object*, size_t>>;
159
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 bool AllocMemory();
161
Mathieu Chartier31e89252013-08-28 11:29:12 -0700162 // Mark the objects defined in this space in the given live bitmap.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700163 void RecordImageAllocations() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700164
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800165 // Classify different kinds of bins that objects end up getting packed into during image writing.
Mathieu Chartier2ba04ea2016-04-08 19:01:05 -0700166 // Ordered from dirtiest to cleanest (until ArtMethods).
Vladimir Markod504c7e2017-12-04 15:56:51 +0000167 enum class Bin {
168 kKnownDirty, // Known dirty objects from --dirty-image-objects list
169 kMiscDirty, // Dex caches, object locks, etc...
170 kClassVerified, // Class verified, but initializers haven't been run
Mathieu Chartierd464fa12016-04-08 18:54:36 -0700171 // Unknown mix of clean/dirty:
Vladimir Markod504c7e2017-12-04 15:56:51 +0000172 kRegular,
173 kClassInitialized, // Class initializers have been run
Mathieu Chartierd464fa12016-04-08 18:54:36 -0700174 // All classes get their own bins since their fields often dirty
Vladimir Markod504c7e2017-12-04 15:56:51 +0000175 kClassInitializedFinalStatics, // Class initializers have been run, no non-final statics
Mathieu Chartier2ba04ea2016-04-08 19:01:05 -0700176 // Likely-clean:
Vladimir Markod504c7e2017-12-04 15:56:51 +0000177 kString, // [String] Almost always immutable (except for obj header).
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800178 // Add more bins here if we add more segregation code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700179 // Non mirror fields must be below.
180 // ArtFields should be always clean.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000181 kArtField,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 // If the class is initialized, then the ArtMethods are probably clean.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000183 kArtMethodClean,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
185 // initialized.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000186 kArtMethodDirty,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000187 // IMT (clean)
Vladimir Markod504c7e2017-12-04 15:56:51 +0000188 kImTable,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700189 // Conflict tables (clean).
Vladimir Markod504c7e2017-12-04 15:56:51 +0000190 kIMTConflictTable,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700191 // Runtime methods (always clean, do not have a length prefix array).
Vladimir Markod504c7e2017-12-04 15:56:51 +0000192 kRuntimeMethod,
Vladimir Marko05792b92015-08-03 11:56:49 +0100193 // Dex cache arrays have a special slot for PC-relative addressing. Since they are
194 // huge, and as such their dirtiness is not important for the clean/dirty separation,
195 // we arbitrarily keep them at the end of the native data.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000196 kDexCacheArray, // Arrays belonging to dex cache.
197 kLast = kDexCacheArray,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700198 // Number of bins which are for mirror objects.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000199 kMirrorCount = kArtField,
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800200 };
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800201 friend std::ostream& operator<<(std::ostream& stream, const Bin& bin);
202
Vladimir Markod504c7e2017-12-04 15:56:51 +0000203 enum class NativeObjectRelocationType {
204 kArtField,
205 kArtFieldArray,
206 kArtMethodClean,
207 kArtMethodArrayClean,
208 kArtMethodDirty,
209 kArtMethodArrayDirty,
210 kRuntimeMethod,
211 kIMTable,
212 kIMTConflictTable,
213 kDexCacheArray,
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700214 };
215 friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type);
216
Vladimir Markod504c7e2017-12-04 15:56:51 +0000217 enum class StubType {
218 kInterpreterToInterpreterBridge,
219 kInterpreterToCompiledCodeBridge,
220 kJNIDlsymLookup,
221 kQuickGenericJNITrampoline,
222 kQuickIMTConflictTrampoline,
223 kQuickResolutionTrampoline,
224 kQuickToInterpreterBridge,
225 kLast = kQuickToInterpreterBridge,
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800226 };
Vladimir Markod504c7e2017-12-04 15:56:51 +0000227 friend std::ostream& operator<<(std::ostream& stream, const StubType& stub_type);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800228
Vladimir Markod504c7e2017-12-04 15:56:51 +0000229 static constexpr size_t kBinBits =
230 MinimumBitsToStore<uint32_t>(static_cast<size_t>(Bin::kMirrorCount) - 1);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800231 // uint32 = typeof(lockword_)
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
233 // failures due to invalid read barrier bits during object field reads.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700234 static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits - LockWord::kGCStateSize;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800235 // 111000.....0
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800237
Vladimir Markod504c7e2017-12-04 15:56:51 +0000238 // Number of bins, including non-mirror bins.
239 static constexpr size_t kNumberOfBins = static_cast<size_t>(Bin::kLast) + 1u;
240
241 // Number of stub types.
242 static constexpr size_t kNumberOfStubTypes = static_cast<size_t>(StubType::kLast) + 1u;
243
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800244 // We use the lock word to store the bin # and bin index of the object in the image.
245 //
246 // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
247 // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
248 struct BinSlot {
249 explicit BinSlot(uint32_t lockword);
250 BinSlot(Bin bin, uint32_t index);
251
252 // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
253 Bin GetBin() const;
254 // The offset in bytes from the beginning of the bin. Aligned to object size.
255 uint32_t GetIndex() const;
256 // Pack into a single uint32_t, for storing into a lock word.
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700257 uint32_t Uint32Value() const { return lockword_; }
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800258 // Comparison operator for map support
259 bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
260
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800261 private:
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800262 // Must be the same size as LockWord, any larger and we would truncate the data.
263 const uint32_t lockword_;
264 };
265
Jeff Haodcdc85b2015-12-04 14:06:18 -0800266 struct ImageInfo {
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800267 ImageInfo();
268 ImageInfo(ImageInfo&&) = default;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800269
Mathieu Chartiera06ba052016-01-06 13:51:52 -0800270 // Create the image sections into the out sections variable, returns the size of the image
271 // excluding the bitmap.
Vladimir Markoe47f60c2018-02-21 13:43:28 +0000272 size_t CreateImageSections(ImageSection* out_sections) const;
Mathieu Chartiera06ba052016-01-06 13:51:52 -0800273
Vladimir Markod504c7e2017-12-04 15:56:51 +0000274 size_t GetStubOffset(StubType stub_type) const {
275 DCHECK_LT(static_cast<size_t>(stub_type), kNumberOfStubTypes);
276 return stub_offsets_[static_cast<size_t>(stub_type)];
277 }
278
279 void SetStubOffset(StubType stub_type, size_t offset) {
280 DCHECK_LT(static_cast<size_t>(stub_type), kNumberOfStubTypes);
281 stub_offsets_[static_cast<size_t>(stub_type)] = offset;
282 }
283
284 size_t GetBinSlotOffset(Bin bin) const {
285 DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
286 return bin_slot_offsets_[static_cast<size_t>(bin)];
287 }
288
289 void IncrementBinSlotSize(Bin bin, size_t size_to_add) {
290 DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
291 bin_slot_sizes_[static_cast<size_t>(bin)] += size_to_add;
292 }
293
294 size_t GetBinSlotSize(Bin bin) const {
295 DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
296 return bin_slot_sizes_[static_cast<size_t>(bin)];
297 }
298
299 void IncrementBinSlotCount(Bin bin, size_t count_to_add) {
300 DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
301 bin_slot_count_[static_cast<size_t>(bin)] += count_to_add;
302 }
303
304 // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
305 size_t GetBinSizeSum(Bin up_to) const;
306
Jeff Haodcdc85b2015-12-04 14:06:18 -0800307 std::unique_ptr<MemMap> image_; // Memory mapped for generating the image.
308
309 // Target begin of this image. Notes: It is not valid to write here, this is the address
310 // of the target image, not necessarily where image_ is mapped. The address is only valid
311 // after layouting (otherwise null).
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800312 uint8_t* image_begin_ = nullptr;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800313
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800314 // Offset to the free space in image_, initially size of image header.
315 size_t image_end_ = RoundUp(sizeof(ImageHeader), kObjectAlignment);
316 uint32_t image_roots_address_ = 0; // The image roots address in the image.
317 size_t image_offset_ = 0; // Offset of this image from the start of the first image.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800318
319 // Image size is the *address space* covered by this image. As the live bitmap is aligned
320 // to the page size, the live bitmap will cover more address space than necessary. But live
321 // bitmaps may not overlap, so an image has a "shadow," which is accounted for in the size.
322 // The next image may only start at image_begin_ + image_size_ (which is guaranteed to be
323 // page-aligned).
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800324 size_t image_size_ = 0;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800325
326 // Oat data.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800327 // Offset of the oat file for this image from start of oat files. This is
328 // valid when the previous oat file has been written.
329 size_t oat_offset_ = 0;
Vladimir Marko944da602016-02-19 12:27:55 +0000330 // Layout of the loaded ELF file containing the oat file, valid after UpdateOatFileLayout().
331 const uint8_t* oat_file_begin_ = nullptr;
332 size_t oat_loaded_size_ = 0;
333 const uint8_t* oat_data_begin_ = nullptr;
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800334 size_t oat_size_ = 0; // Size of the corresponding oat data.
Vladimir Marko944da602016-02-19 12:27:55 +0000335 // The oat header checksum, valid after UpdateOatFileHeader().
336 uint32_t oat_checksum_ = 0u;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800337
338 // Image bitmap which lets us know where the objects inside of the image reside.
339 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_;
340
341 // The start offsets of the dex cache arrays.
342 SafeMap<const DexFile*, size_t> dex_cache_array_starts_;
343
344 // Offset from oat_data_begin_ to the stubs.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000345 uint32_t stub_offsets_[kNumberOfStubTypes] = {};
Jeff Haodcdc85b2015-12-04 14:06:18 -0800346
347 // Bin slot tracking for dirty object packing.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000348 size_t bin_slot_sizes_[kNumberOfBins] = {}; // Number of bytes in a bin.
349 size_t bin_slot_offsets_[kNumberOfBins] = {}; // Number of bytes in previous bins.
350 size_t bin_slot_count_[kNumberOfBins] = {}; // Number of objects in a bin.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800351
352 // Cached size of the intern table for when we allocate memory.
353 size_t intern_table_bytes_ = 0;
354
Mathieu Chartier1f47b672016-01-07 16:29:01 -0800355 // Number of image class table bytes.
356 size_t class_table_bytes_ = 0;
357
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800358 // Number of object fixup bytes.
359 size_t object_fixup_bytes_ = 0;
360
361 // Number of pointer fixup bytes.
362 size_t pointer_fixup_bytes_ = 0;
363
Mathieu Chartier1f47b672016-01-07 16:29:01 -0800364 // Intern table associated with this image for serialization.
Mathieu Chartierea0831f2015-12-29 13:17:37 -0800365 std::unique_ptr<InternTable> intern_table_;
Mathieu Chartier1f47b672016-01-07 16:29:01 -0800366
367 // Class table associated with this image for serialization.
368 std::unique_ptr<ClassTable> class_table_;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800369 };
370
Mathieu Chartier31e89252013-08-28 11:29:12 -0700371 // We use the lock word to store the offset of the object in the image.
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800372 void AssignImageOffset(mirror::Object* object, BinSlot bin_slot)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700373 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700374 void SetImageOffset(mirror::Object* object, size_t offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700375 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700376 bool IsImageOffsetAssigned(mirror::Object* object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700377 REQUIRES_SHARED(Locks::mutator_lock_);
378 size_t GetImageOffset(mirror::Object* object) const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 void UpdateImageOffset(mirror::Object* obj, uintptr_t offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700380 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700382 void PrepareDexCacheArraySlots() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700383 void AssignImageBinSlot(mirror::Object* object, size_t oat_index)
384 REQUIRES_SHARED(Locks::mutator_lock_);
385 mirror::Object* TryAssignBinSlot(WorkStack& work_stack, mirror::Object* obj, size_t oat_index)
386 REQUIRES_SHARED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800387 void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700388 REQUIRES_SHARED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800389 bool IsImageBinSlotAssigned(mirror::Object* object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700390 REQUIRES_SHARED(Locks::mutator_lock_);
391 BinSlot GetImageBinSlot(mirror::Object* object) const REQUIRES_SHARED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800392
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700393 void AddDexCacheArrayRelocation(void* array, size_t offset, ObjPtr<mirror::DexCache> dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700394 REQUIRES_SHARED(Locks::mutator_lock_);
395 void AddMethodPointerArray(mirror::PointerArray* arr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396
Alex Lighta59dd802014-07-02 16:28:08 -0700397 static void* GetImageAddressCallback(void* writer, mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700398 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lighta59dd802014-07-02 16:28:08 -0700399 return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj);
400 }
401
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700402 mirror::Object* GetLocalAddress(mirror::Object* object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700403 REQUIRES_SHARED(Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 size_t offset = GetImageOffset(object);
Vladimir Marko944da602016-02-19 12:27:55 +0000405 size_t oat_index = GetOatIndex(object);
406 const ImageInfo& image_info = GetImageInfo(oat_index);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800407 uint8_t* dst = image_info.image_->Begin() + offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 return reinterpret_cast<mirror::Object*>(dst);
409 }
410
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800411 // Returns the address in the boot image if we are compiling the app image.
Vladimir Markod504c7e2017-12-04 15:56:51 +0000412 const uint8_t* GetOatAddress(StubType type) const;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800413
Jeff Haodcdc85b2015-12-04 14:06:18 -0800414 const uint8_t* GetOatAddressForOffset(uint32_t offset, const ImageInfo& image_info) const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 // With Quick, code is within the OatFile, as there are all in one
Jeff Haodcdc85b2015-12-04 14:06:18 -0800416 // .o ELF object. But interpret it as signed.
417 DCHECK_LE(static_cast<int32_t>(offset), static_cast<int32_t>(image_info.oat_size_));
418 DCHECK(image_info.oat_data_begin_ != nullptr);
419 return offset == 0u ? nullptr : image_info.oat_data_begin_ + static_cast<int32_t>(offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 }
421
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 // Returns true if the class was in the original requested image classes list.
Vladimir Markof25cc732017-03-16 16:18:15 +0000423 bool KeepClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424
425 // Debug aid that list of requested image classes.
426 void DumpImageClasses();
427
428 // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying.
429 void ComputeLazyFieldsForImageClasses()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700430 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000432 // Visit all class loaders.
433 void VisitClassLoaders(ClassLoaderVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
434
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 // Remove unwanted classes from various roots.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700436 void PruneNonImageClasses() REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437
Vladimir Markof25cc732017-03-16 16:18:15 +0000438 // Remove unwanted classes from the DexCache roots and preload deterministic DexCache contents.
439 void PruneAndPreloadDexCache(ObjPtr<mirror::DexCache> dex_cache,
440 ObjPtr<mirror::ClassLoader> class_loader)
441 REQUIRES_SHARED(Locks::mutator_lock_)
442 REQUIRES(!Locks::classlinker_classes_lock_);
443
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 // Verify unwanted classes removed.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 void CheckNonImageClassesRemoved() REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446
447 // Lays out where the image objects will be at runtime.
Vladimir Markof4da6752014-08-01 19:04:18 +0100448 void CalculateNewObjectOffsets()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700449 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier496577f2016-09-20 15:33:31 -0700450 void ProcessWorkStack(WorkStack* work_stack)
451 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko944da602016-02-19 12:27:55 +0000452 void CreateHeader(size_t oat_index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700453 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markof75613c2018-06-05 12:51:04 +0100454 ObjPtr<mirror::ObjectArray<mirror::Object>> CollectDexCaches(Thread* self, size_t oat_index) const
455 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko944da602016-02-19 12:27:55 +0000456 mirror::ObjectArray<mirror::Object>* CreateImageRoots(size_t oat_index) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700457 REQUIRES_SHARED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800458 void CalculateObjectBinSlots(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700459 REQUIRES_SHARED(Locks::mutator_lock_);
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800460 void UnbinObjectsIntoOffset(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700461 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700462
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 // Creates the contiguous image in memory and adjusts pointers.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700464 void CopyAndFixupNativeData(size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
465 void CopyAndFixupObjects() REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700466 void CopyAndFixupObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800467 void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy, const ImageInfo& image_info)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700468 REQUIRES_SHARED(Locks::mutator_lock_);
469 void CopyAndFixupImTable(ImTable* orig, ImTable* copy) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700470 void CopyAndFixupImtConflictTable(ImtConflictTable* orig, ImtConflictTable* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700471 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700472 void FixupClass(mirror::Class* orig, mirror::Class* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700473 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800474 void FixupObject(mirror::Object* orig, mirror::Object* copy)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700475 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100476 void FixupDexCache(mirror::DexCache* orig_dex_cache, mirror::DexCache* copy_dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700477 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800478 void FixupPointerArray(mirror::Object* dst,
479 mirror::PointerArray* arr,
480 mirror::Class* klass,
481 Bin array_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700482 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700484 // Get quick code for non-resolution/imt_conflict/abstract method.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800485 const uint8_t* GetQuickCode(ArtMethod* method,
486 const ImageInfo& image_info,
487 bool* quick_is_interpreted)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700489
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 // Return true if a method is likely to be dirtied at runtime.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700491 bool WillMethodBeDirty(ArtMethod* m) const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700492
493 // Assign the offset for an ArtMethod.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800494 void AssignMethodOffset(ArtMethod* method,
495 NativeObjectRelocationType type,
Vladimir Marko944da602016-02-19 12:27:55 +0000496 size_t oat_index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700497 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700498
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800499 // Return true if imt was newly inserted.
500 bool TryAssignImTableOffset(ImTable* imt, size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000501
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700502 // Assign the offset for an IMT conflict table. Does nothing if the table already has a native
503 // relocation.
504 void TryAssignConflictTableOffset(ImtConflictTable* table, size_t oat_index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700505 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700506
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800507 // Return true if klass is loaded by the boot class loader but not in the boot image.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700508 bool IsBootClassLoaderNonImageClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800509
Mathieu Chartier901e0702016-02-19 13:42:48 -0800510 // Return true if klass depends on a boot class loader non image class. We want to prune these
511 // classes since we do not want any boot class loader classes in the image. This means that
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800512 // we also cannot have any classes which refer to these boot class loader non image classes.
Mathieu Chartier901e0702016-02-19 13:42:48 -0800513 // PruneAppImageClass also prunes if klass depends on a non-image class according to the compiler
514 // driver.
Vladimir Markof25cc732017-03-16 16:18:15 +0000515 bool PruneAppImageClass(ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700516 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800517
Mathieu Chartier945c1c12015-11-24 15:37:12 -0800518 // early_exit is true if we had a cyclic dependency anywhere down the chain.
Vladimir Markof25cc732017-03-16 16:18:15 +0000519 bool PruneAppImageClassInternal(ObjPtr<mirror::Class> klass,
Mathieu Chartier901e0702016-02-19 13:42:48 -0800520 bool* early_exit,
Chang Xingb2e8adc2017-07-07 17:35:37 -0700521 std::unordered_set<mirror::Object*>* visited)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700522 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier945c1c12015-11-24 15:37:12 -0800523
Mathieu Chartier496577f2016-09-20 15:33:31 -0700524 bool IsMultiImage() const {
525 return image_infos_.size() > 1;
526 }
527
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700528 static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
529
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700530 uintptr_t NativeOffsetInImage(void* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko05792b92015-08-03 11:56:49 +0100531
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800532 // Location of where the object will be when the image is loaded at runtime.
Vladimir Marko05792b92015-08-03 11:56:49 +0100533 template <typename T>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700534 T* NativeLocationInImage(T* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe245ee002014-12-04 21:25:04 -0800535
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800536 // Location of where the temporary copy of the object currently is.
537 template <typename T>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700538 T* NativeCopyLocation(T* obj, mirror::DexCache* dex_cache) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4b00d342015-11-13 10:42:08 -0800539
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800540 // Return true of obj is inside of the boot image space. This may only return true if we are
541 // compiling an app image.
542 bool IsInBootImage(const void* obj) const;
543
544 // Return true if ptr is within the boot oat file.
545 bool IsInBootOatFile(const void* ptr) const;
546
Vladimir Marko944da602016-02-19 12:27:55 +0000547 // Get the index of the oat file associated with the object.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700548 size_t GetOatIndex(mirror::Object* object) const REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800549
Vladimir Marko944da602016-02-19 12:27:55 +0000550 // The oat index for shared data in multi-image and all data in single-image compilation.
551 size_t GetDefaultOatIndex() const {
552 return 0u;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800553 }
554
Vladimir Marko944da602016-02-19 12:27:55 +0000555 ImageInfo& GetImageInfo(size_t oat_index) {
556 return image_infos_[oat_index];
557 }
558
559 const ImageInfo& GetImageInfo(size_t oat_index) const {
560 return image_infos_[oat_index];
561 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800562
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800563 // Find an already strong interned string in the other images or in the boot image. Used to
564 // remove duplicates in the multi image and app image case.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700565 mirror::String* FindInternedString(mirror::String* string) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800566
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700567 // Return true if there already exists a native allocation for an object.
568 bool NativeRelocationAssigned(void* ptr) const;
569
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800570 void CopyReference(mirror::HeapReference<mirror::Object>* dest, ObjPtr<mirror::Object> src)
571 REQUIRES_SHARED(Locks::mutator_lock_);
572
573 void CopyReference(mirror::CompressedReference<mirror::Object>* dest, ObjPtr<mirror::Object> src)
574 REQUIRES_SHARED(Locks::mutator_lock_);
575
576 void CopyAndFixupPointer(void** target, void* value);
577
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 const CompilerDriver& compiler_driver_;
579
Jeff Haodcdc85b2015-12-04 14:06:18 -0800580 // Beginning target image address for the first image.
581 uint8_t* global_image_begin_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100582
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800583 // Offset from image_begin_ to where the first object is in image_.
584 size_t image_objects_offset_begin_;
585
Mathieu Chartiere401d142015-04-22 13:56:20 -0700586 // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need
587 // to keep track. These include vtable arrays, iftable arrays, and dex caches.
588 std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_;
589
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700590 // Saved hash codes. We use these to restore lockwords which were temporarily used to have
591 // forwarding addresses as well as copying over hash codes.
592 std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800593
Mathieu Chartier496577f2016-09-20 15:33:31 -0700594 // Oat index map for objects.
595 std::unordered_map<mirror::Object*, uint32_t> oat_index_map_;
596
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800597 // Boolean flags.
Igor Murashkin46774762014-10-22 11:37:02 -0700598 const bool compile_pic_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800599 const bool compile_app_image_;
600
Mathieu Chartier2d721012014-11-10 11:08:06 -0800601 // Size of pointers on the target architecture.
Andreas Gampe542451c2016-07-26 09:02:02 -0700602 PointerSize target_ptr_size_;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800603
Vladimir Marko944da602016-02-19 12:27:55 +0000604 // Image data indexed by the oat file index.
605 dchecked_vector<ImageInfo> image_infos_;
Igor Murashkinf5b4c502014-11-14 15:01:59 -0800606
Mathieu Chartiere401d142015-04-22 13:56:20 -0700607 // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
608 // have one entry per art field for convenience. ArtFields are placed right after the end of the
609 // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700610 struct NativeObjectRelocation {
Vladimir Marko944da602016-02-19 12:27:55 +0000611 size_t oat_index;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700612 uintptr_t offset;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700613 NativeObjectRelocationType type;
614
615 bool IsArtMethodRelocation() const {
Vladimir Markod504c7e2017-12-04 15:56:51 +0000616 return type == NativeObjectRelocationType::kArtMethodClean ||
617 type == NativeObjectRelocationType::kArtMethodDirty ||
618 type == NativeObjectRelocationType::kRuntimeMethod;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700619 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 };
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700621 std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700622
Mathieu Chartiere401d142015-04-22 13:56:20 -0700623 // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
624 ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
625
626 // Counters for measurements, used for logging only.
627 uint64_t dirty_methods_;
628 uint64_t clean_methods_;
Andreas Gampe245ee002014-12-04 21:25:04 -0800629
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800630 // Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass.
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800631 std::unordered_map<mirror::Class*, bool> prune_class_memo_;
632
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800633 // Class loaders with a class table to write out. There should only be one class loader because
634 // dex2oat loads the dex files to be compiled into a single class loader. For the boot image,
635 // null is a valid entry.
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800636 std::unordered_set<mirror::ClassLoader*> class_loaders_;
637
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800638 // Which mode the image is stored as, see image.h
639 const ImageHeader::StorageMode image_storage_mode_;
640
Vladimir Marko944da602016-02-19 12:27:55 +0000641 // The file names of oat files.
642 const std::vector<const char*>& oat_filenames_;
643
644 // Map of dex files to the indexes of oat files that they were compiled into.
645 const std::unordered_map<const DexFile*, size_t>& dex_file_oat_index_map_;
Jeff Haodcdc85b2015-12-04 14:06:18 -0800646
Jeff Haoc23b0c02017-07-27 18:19:38 -0700647 // Set of objects known to be dirty in the image. Can be nullptr if there are none.
Vladimir Marko54159c62018-06-20 14:30:08 +0100648 const HashSet<std::string>* dirty_image_objects_;
Jeff Haoc23b0c02017-07-27 18:19:38 -0700649
Vladimir Markoad06b982016-11-17 16:38:59 +0000650 class ComputeLazyFieldsForClassesVisitor;
651 class FixupClassVisitor;
652 class FixupRootVisitor;
653 class FixupVisitor;
Mathieu Chartier496577f2016-09-20 15:33:31 -0700654 class GetRootsVisitor;
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800655 class ImageAddressVisitorForDexCacheArray;
Vladimir Markoad06b982016-11-17 16:38:59 +0000656 class NativeLocationVisitor;
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000657 class PruneClassesVisitor;
658 class PruneClassLoaderClassesVisitor;
Mathieu Chartier8c19d242017-03-06 12:35:10 -0800659 class RegisterBootClassPathClassesVisitor;
Mathieu Chartier496577f2016-09-20 15:33:31 -0700660 class VisitReferencesVisitor;
Chang Xingb2e8adc2017-07-07 17:35:37 -0700661 class PruneObjectReferenceVisitor;
Vladimir Markoad06b982016-11-17 16:38:59 +0000662
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700663 DISALLOW_COPY_AND_ASSIGN(ImageWriter);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664};
665
Vladimir Marko74527972016-11-29 15:57:32 +0000666} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667} // namespace art
668
Vladimir Marko74527972016-11-29 15:57:32 +0000669#endif // ART_DEX2OAT_LINKER_IMAGE_WRITER_H_