xref: /aosp_15_r20/art/dex2oat/linker/image_writer.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
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 
17 #ifndef ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
18 #define ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
19 
20 #include <stdint.h>
21 #include "base/memory_tool.h"
22 
23 #include <cstddef>
24 #include <memory>
25 #include <ostream>
26 #include <set>
27 #include <stack>
28 #include <string>
29 
30 #include "art_method.h"
31 #include "base/bit_utils.h"
32 #include "base/dchecked_vector.h"
33 #include "base/unix_file/fd_file.h"
34 #include "base/hash_map.h"
35 #include "base/hash_set.h"
36 #include "base/length_prefixed_array.h"
37 #include "base/macros.h"
38 #include "base/mem_map.h"
39 #include "base/os.h"
40 #include "base/pointer_size.h"
41 #include "base/utils.h"
42 #include "class_table.h"
43 #include "gc/accounting/space_bitmap.h"
44 #include "intern_table.h"
45 #include "lock_word.h"
46 #include "mirror/dex_cache.h"
47 #include "oat/image.h"
48 #include "oat/jni_stub_hash_map.h"
49 #include "oat/oat.h"
50 #include "oat/oat_file.h"
51 #include "obj_ptr.h"
52 
53 namespace art {
54 namespace gc {
55 namespace accounting {
56 template <size_t kAlignment> class SpaceBitmap;
57 using ContinuousSpaceBitmap = SpaceBitmap<kObjectAlignment>;
58 }  // namespace accounting
59 namespace space {
60 class ImageSpace;
61 }  // namespace space
62 }  // namespace gc
63 
64 namespace mirror {
65 class ClassLoader;
66 }  // namespace mirror
67 
68 class ClassLoaderVisitor;
69 class CompilerOptions;
70 template<class T> class Handle;
71 class ImTable;
72 class ImtConflictTable;
73 class JavaVMExt;
74 class TimingLogger;
75 
76 namespace linker {
77 
78 // Write a Space built during compilation for use during execution.
79 class ImageWriter final {
80  public:
81   ImageWriter(const CompilerOptions& compiler_options,
82               uintptr_t image_begin,
83               ImageHeader::StorageMode image_storage_mode,
84               const std::vector<std::string>& oat_filenames,
85               const HashMap<const DexFile*, size_t>& dex_file_oat_index_map,
86               jobject class_loader,
87               const std::vector<std::string>* dirty_image_objects);
88   ~ImageWriter();
89 
90   /*
91    * Modifies the heap and collects information about objects and code so that
92    * they can be written to the boot or app image later.
93    *
94    * First, unneeded classes are removed from the managed heap.  Next, we
95    * remove cached values and calculate necessary metadata for later in the
96    * process. Optionally some debugging information is collected and used to
97    * verify the state of the heap at this point.  Next, metadata from earlier
98    * is used to calculate offsets of references to strings to speed up string
99    * interning when the image is loaded.  Lastly, we allocate enough memory to
100    * fit all image data minus the bitmap and relocation sections.
101    *
102    * This function should only be called when all objects to be included in the
103    * image have been initialized and all native methods have been generated.  In
104    * addition, no other thread should be modifying the heap.
105    */
106   bool PrepareImageAddressSpace(TimingLogger* timings);
107 
IsImageAddressSpaceReady()108   bool IsImageAddressSpaceReady() const {
109     DCHECK(!image_infos_.empty());
110     for (const ImageInfo& image_info : image_infos_) {
111       if (image_info.image_roots_address_ == 0u) {
112         return false;
113       }
114     }
115     return true;
116   }
117 
118   ObjPtr<mirror::ClassLoader> GetAppClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_);
119 
120   template <typename T>
GetImageAddress(T * object)121   T* GetImageAddress(T* object) const REQUIRES_SHARED(Locks::mutator_lock_) {
122     if (object == nullptr || IsInBootImage(object)) {
123       return object;
124     } else {
125       size_t oat_index = GetOatIndex(object);
126       const ImageInfo& image_info = GetImageInfo(oat_index);
127       return reinterpret_cast<T*>(image_info.image_begin_ + GetImageOffset(object, oat_index));
128     }
129   }
130 
GetGlobalImageOffset(ArtMethod * method)131   uint32_t GetGlobalImageOffset(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
132     return reinterpret_cast<uint8_t*>(GetImageMethodAddress(method)) - global_image_begin_;
133   }
134 
GetGlobalImageOffset(mirror::Object * object)135   uint32_t GetGlobalImageOffset(mirror::Object* object) const REQUIRES_SHARED(Locks::mutator_lock_) {
136     DCHECK(object != nullptr);
137     DCHECK(!IsInBootImage(object));
138     size_t oat_index = GetOatIndex(object);
139     const ImageInfo& image_info = GetImageInfo(oat_index);
140     return dchecked_integral_cast<uint32_t>(
141         image_info.image_begin_ + GetImageOffset(object, oat_index) - global_image_begin_);
142   }
143 
144   ArtMethod* GetImageMethodAddress(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_);
145   const void* GetIntrinsicReferenceAddress(uint32_t intrinsic_data)
146       REQUIRES_SHARED(Locks::mutator_lock_);
147 
GetOatFileOffset(size_t oat_index)148   size_t GetOatFileOffset(size_t oat_index) const {
149     return GetImageInfo(oat_index).oat_offset_;
150   }
151 
GetOatFileBegin(size_t oat_index)152   const uint8_t* GetOatFileBegin(size_t oat_index) const {
153     return GetImageInfo(oat_index).oat_file_begin_;
154   }
155 
156   // If image_fd is not File::kInvalidFd, then we use that for the image file. Otherwise we open
157   // the names in image_filenames.
158   // If oat_fd is not File::kInvalidFd, then we use that for the oat file. Otherwise we open
159   // the names in oat_filenames.
160   bool Write(int image_fd,
161              const std::vector<std::string>& image_filenames,
162              size_t component_count)
163       REQUIRES(!Locks::mutator_lock_);
164 
GetOatDataBegin(size_t oat_index)165   uintptr_t GetOatDataBegin(size_t oat_index) {
166     return reinterpret_cast<uintptr_t>(GetImageInfo(oat_index).oat_data_begin_);
167   }
168 
169   // Get the index of the oat file containing the dex file.
170   //
171   // This "oat_index" is used to retrieve information about the the memory layout
172   // of the oat file and its associated image file, needed for link-time patching
173   // of references to the image or across oat files.
174   size_t GetOatIndexForDexFile(const DexFile* dex_file) const;
175 
176   // Get the index of the oat file containing the definition of the class.
177   size_t GetOatIndexForClass(ObjPtr<mirror::Class> klass) const
178       REQUIRES_SHARED(Locks::mutator_lock_);
179 
180   // Update the oat layout for the given oat file.
181   // This will make the oat_offset for the next oat file valid.
182   void UpdateOatFileLayout(size_t oat_index,
183                            size_t oat_loaded_size,
184                            size_t oat_data_offset,
185                            size_t oat_data_size);
186   // Update information about the oat header, i.e. checksum and trampoline offsets.
187   void UpdateOatFileHeader(size_t oat_index, const OatHeader& oat_header);
188 
189  private:
190   bool AllocMemory();
191 
192   // Mark the objects defined in this space in the given live bitmap.
193   void RecordImageAllocations() REQUIRES_SHARED(Locks::mutator_lock_);
194 
195   // Classify different kinds of bins that objects end up getting packed into during image writing.
196   // Ordered from dirtiest to cleanest (until ArtMethods).
197   enum class Bin {
198     kKnownDirty,                  // Known dirty objects from --dirty-image-objects list
199     kMiscDirty,                   // Dex caches, object locks, etc...
200     kClassVerified,               // Class verified, but initializers haven't been run
201     // Unknown mix of clean/dirty:
202     kRegular,
203     kClassInitialized,            // Class initializers have been run
204     // All classes get their own bins since their fields often dirty
205     kClassInitializedFinalStatics,  // Class initializers have been run, no non-final statics
206     // Likely-clean:
207     kString,                      // [String] Almost always immutable (except for obj header).
208     // Definitely clean:
209     kInternalClean,               // ART internal: image roots, boot image live objects, vtables
210                                   // and interface tables, Object[]/int[]/long[].
211     // Add more bins here if we add more segregation code.
212     // Non mirror fields must be below.
213     // ArtFields should be always clean.
214     kArtField,
215     // If the class is initialized, then the ArtMethods are probably clean.
216     kArtMethodClean,
217     // ArtMethods may be dirty if the class has native methods or a declaring class that isn't
218     // initialized.
219     kArtMethodDirty,
220     // IMT (clean)
221     kImTable,
222     // Conflict tables (clean).
223     kIMTConflictTable,
224     // Runtime methods (always clean, do not have a length prefix array).
225     kRuntimeMethod,
226     // Methods with unique JNI stubs.
227     kJniStubMethod,
228     // Metadata bin for data that is temporary during image lifetime.
229     kMetadata,
230     kLast = kMetadata,
231     // Number of bins which are for mirror objects.
232     kMirrorCount = kArtField,
233   };
234   friend std::ostream& operator<<(std::ostream& stream, Bin bin);
235 
236   enum class NativeObjectRelocationType {
237     kArtFieldArray,
238     kArtMethodClean,
239     kArtMethodArrayClean,
240     kArtMethodDirty,
241     kArtMethodArrayDirty,
242     kGcRootPointer,
243     kRuntimeMethod,
244     kIMTable,
245     kIMTConflictTable,
246   };
247   friend std::ostream& operator<<(std::ostream& stream, NativeObjectRelocationType type);
248 
249   static constexpr size_t kBinBits =
250       MinimumBitsToStore<uint32_t>(static_cast<size_t>(Bin::kMirrorCount) - 1);
251   // uint32 = typeof(lockword_)
252   // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK
253   // failures due to invalid read barrier bits during object field reads.
254   static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits - LockWord::kGCStateSize;
255   // 111000.....0
256   static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
257 
258   // Number of bins, including non-mirror bins.
259   static constexpr size_t kNumberOfBins = static_cast<size_t>(Bin::kLast) + 1u;
260 
261   // Number of stub types.
262   static constexpr size_t kNumberOfStubTypes = static_cast<size_t>(StubType::kLast) + 1u;
263 
264   // We use the lock word to store the bin # and bin index of the object in the image.
265   //
266   // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up
267   // stored in the lock word bit-for-bit when object forwarding addresses are being calculated.
268   struct BinSlot {
269     explicit BinSlot(uint32_t lockword);
270     BinSlot(Bin bin, uint32_t index);
271 
272     // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
273     Bin GetBin() const;
274     // The offset in bytes from the beginning of the bin. Aligned to object size.
275     uint32_t GetOffset() const;
276     // Pack into a single uint32_t, for storing into a lock word.
Uint32ValueBinSlot277     uint32_t Uint32Value() const { return lockword_; }
278     // Comparison operator for map support
279     bool operator<(const BinSlot& other) const  { return lockword_ < other.lockword_; }
280 
281    private:
282     // Must be the same size as LockWord, any larger and we would truncate the data.
283     uint32_t lockword_;
284   };
285 
286   struct ImageInfo {
287     ImageInfo();
288     ImageInfo(ImageInfo&&) = default;
289 
290     /*
291      * Creates ImageSection objects that describe most of the sections of a
292      * boot or AppImage. The following sections are not included:
293      *   - ImageHeader::kSectionImageBitmap
294      *
295      * In addition, the ImageHeader is not covered here.
296      *
297      * This function will return the total size of the covered sections as well
298      * as a vector containing the individual ImageSection objects.
299      */
300     std::pair<size_t, dchecked_vector<ImageSection>> CreateImageSections() const;
301 
GetStubOffsetImageInfo302     size_t GetStubOffset(StubType stub_type) const {
303       DCHECK_LT(static_cast<size_t>(stub_type), kNumberOfStubTypes);
304       return stub_offsets_[static_cast<size_t>(stub_type)];
305     }
306 
SetStubOffsetImageInfo307     void SetStubOffset(StubType stub_type, size_t offset) {
308       DCHECK_LT(static_cast<size_t>(stub_type), kNumberOfStubTypes);
309       stub_offsets_[static_cast<size_t>(stub_type)] = offset;
310     }
311 
GetBinSlotOffsetImageInfo312     size_t GetBinSlotOffset(Bin bin) const {
313       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
314       return bin_slot_offsets_[static_cast<size_t>(bin)];
315     }
316 
IncrementBinSlotSizeImageInfo317     void IncrementBinSlotSize(Bin bin, size_t size_to_add) {
318       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
319       bin_slot_sizes_[static_cast<size_t>(bin)] += size_to_add;
320     }
321 
GetBinSlotSizeImageInfo322     size_t GetBinSlotSize(Bin bin) const {
323       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
324       return bin_slot_sizes_[static_cast<size_t>(bin)];
325     }
326 
IncrementBinSlotCountImageInfo327     void IncrementBinSlotCount(Bin bin, size_t count_to_add) {
328       DCHECK_LT(static_cast<size_t>(bin), kNumberOfBins);
329       bin_slot_count_[static_cast<size_t>(bin)] += count_to_add;
330     }
331 
332     // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
333     size_t GetBinSizeSum(Bin up_to) const;
334 
335     MemMap image_;  // Memory mapped for generating the image.
336 
337     // Target begin of this image. Notes: It is not valid to write here, this is the address
338     // of the target image, not necessarily where image_ is mapped. The address is only valid
339     // after layouting (otherwise null).
340     uint8_t* image_begin_ = nullptr;
341 
342     // Offset to the free space in image_, initially size of image header.
343     size_t image_end_ = RoundUp(sizeof(ImageHeader), kObjectAlignment);
344     uint32_t image_roots_address_ = 0;  // The image roots address in the image.
345     size_t image_offset_ = 0;  // Offset of this image from the start of the first image.
346 
347     // Image size is the *address space* covered by this image. As the live bitmap is aligned
348     // to the page size, the live bitmap will cover more address space than necessary. But live
349     // bitmaps may not overlap, so an image has a "shadow," which is accounted for in the size.
350     // The next image may only start at image_begin_ + image_size_ (which is guaranteed to be
351     // page-aligned).
352     size_t image_size_ = 0;
353 
354     // Oat data.
355     // Offset of the oat file for this image from start of oat files. This is
356     // valid when the previous oat file has been written.
357     size_t oat_offset_ = 0;
358     // Layout of the loaded ELF file containing the oat file, valid after UpdateOatFileLayout().
359     const uint8_t* oat_file_begin_ = nullptr;
360     size_t oat_loaded_size_ = 0;
361     const uint8_t* oat_data_begin_ = nullptr;
362     size_t oat_size_ = 0;  // Size of the corresponding oat data.
363     // The oat header checksum, valid after UpdateOatFileHeader().
364     uint32_t oat_checksum_ = 0u;
365 
366     // Image bitmap which lets us know where the objects inside of the image reside.
367     gc::accounting::ContinuousSpaceBitmap image_bitmap_;
368 
369     // Offset from oat_data_begin_ to the stubs.
370     uint32_t stub_offsets_[kNumberOfStubTypes] = {};
371 
372     // Bin slot tracking for dirty object packing.
373     size_t bin_slot_sizes_[kNumberOfBins] = {};  // Number of bytes in a bin.
374     size_t bin_slot_offsets_[kNumberOfBins] = {};  // Number of bytes in previous bins.
375     size_t bin_slot_count_[kNumberOfBins] = {};  // Number of objects in a bin.
376 
377     // Cached size of the intern table for when we allocate memory.
378     size_t intern_table_bytes_ = 0;
379 
380     // Number of image class table bytes.
381     size_t class_table_bytes_ = 0;
382 
383     // Number of object fixup bytes.
384     size_t object_fixup_bytes_ = 0;
385 
386     // Number of pointer fixup bytes.
387     size_t pointer_fixup_bytes_ = 0;
388 
389     // Number of offsets to string references that will be written to the
390     // StringFieldOffsets section.
391     size_t num_string_references_ = 0;
392 
393     // Offsets into the image that indicate where string references are recorded.
394     dchecked_vector<AppImageReferenceOffsetInfo> string_reference_offsets_;
395 
396     // Intern table associated with this image for serialization.
397     size_t intern_table_size_ = 0;
398     std::unique_ptr<GcRoot<mirror::String>[]> intern_table_buffer_;
399     std::optional<InternTable::UnorderedSet> intern_table_;
400 
401     // Class table associated with this image for serialization.
402     size_t class_table_size_ = 0;
403     std::unique_ptr<ClassTable::ClassSet::value_type[]> class_table_buffer_;
404     std::optional<ClassTable::ClassSet> class_table_;
405 
406     // Padding offsets to ensure region alignment (if required).
407     // Objects need to be added from the recorded offset until the end of the region.
408     dchecked_vector<size_t> padding_offsets_;
409   };
410 
411   // We use the lock word to store the offset of the object in the image.
412   size_t GetImageOffset(mirror::Object* object, size_t oat_index) const
413       REQUIRES_SHARED(Locks::mutator_lock_);
414 
415   Bin GetImageBin(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_);
416   void AssignImageBinSlot(mirror::Object* object, size_t oat_index, Bin bin)
417       REQUIRES_SHARED(Locks::mutator_lock_);
418   void RecordNativeRelocations(ObjPtr<mirror::Class> klass, size_t oat_index)
419       REQUIRES_SHARED(Locks::mutator_lock_);
420   void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot)
421       REQUIRES_SHARED(Locks::mutator_lock_);
422   bool IsImageBinSlotAssigned(mirror::Object* object) const
423       REQUIRES_SHARED(Locks::mutator_lock_);
424   BinSlot GetImageBinSlot(mirror::Object* object, size_t oat_index) const
425       REQUIRES_SHARED(Locks::mutator_lock_);
426   void UpdateImageBinSlotOffset(mirror::Object* object, size_t oat_index, size_t new_offset)
427       REQUIRES_SHARED(Locks::mutator_lock_);
428 
429   // Returns the address in the boot image if we are compiling the app image.
430   const uint8_t* GetOatAddress(StubType type) const;
431 
GetOatAddressForOffset(uint32_t offset,const ImageInfo & image_info)432   const uint8_t* GetOatAddressForOffset(uint32_t offset, const ImageInfo& image_info) const {
433     // With Quick, code is within the OatFile, as there are all in one
434     // .o ELF object. But interpret it as signed.
435     DCHECK_LE(static_cast<int32_t>(offset), static_cast<int32_t>(image_info.oat_size_));
436     DCHECK(image_info.oat_data_begin_ != nullptr);
437     return offset == 0u ? nullptr : image_info.oat_data_begin_ + static_cast<int32_t>(offset);
438   }
439 
440   // Returns true if the class was in the original requested image classes list.
441   bool KeepClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
442 
443   // Debug aid that list of requested image classes.
444   void DumpImageClasses();
445 
446   // Visit all class loaders.
447   void VisitClassLoaders(ClassLoaderVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
448 
449   // Remove unwanted classes from various roots.
450   void PruneNonImageClasses() REQUIRES_SHARED(Locks::mutator_lock_);
451 
452   // Find dex caches for pruning or preloading.
453   dchecked_vector<ObjPtr<mirror::DexCache>> FindDexCaches(Thread* self)
454       REQUIRES_SHARED(Locks::mutator_lock_)
455       REQUIRES(!Locks::classlinker_classes_lock_);
456 
457   // Verify unwanted classes removed.
458   void CheckNonImageClassesRemoved() REQUIRES_SHARED(Locks::mutator_lock_);
459 
460   // Lays out where the image objects will be at runtime.
461   void CalculateNewObjectOffsets()
462       REQUIRES_SHARED(Locks::mutator_lock_);
463   void CreateHeader(size_t oat_index, size_t component_count)
464       REQUIRES_SHARED(Locks::mutator_lock_);
465   bool CreateImageRoots() REQUIRES_SHARED(Locks::mutator_lock_);
466 
467   // Creates the contiguous image in memory and adjusts pointers.
468   void CopyAndFixupNativeData(size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
469   void CopyAndFixupJniStubMethods(size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
470   void CopyAndFixupObjects() REQUIRES_SHARED(Locks::mutator_lock_);
471   void CopyAndFixupObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
472   template <bool kCheckIfDone>
473   mirror::Object* CopyObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
474   void CopyAndFixupMethodPointerArray(mirror::PointerArray* arr)
475       REQUIRES_SHARED(Locks::mutator_lock_);
476   void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy, size_t oat_index)
477       REQUIRES_SHARED(Locks::mutator_lock_);
478   void CopyAndFixupImTable(ImTable* orig, ImTable* copy)
479       REQUIRES_SHARED(Locks::mutator_lock_);
480   void CopyAndFixupImtConflictTable(ImtConflictTable* orig, ImtConflictTable* copy)
481       REQUIRES_SHARED(Locks::mutator_lock_);
482 
483   /*
484    * Copies metadata from the heap into a buffer that will be compressed and
485    * written to the image.
486    *
487    * This function copies the string offset metadata from a local vector to an
488    * offset inside the image_ field of an ImageInfo struct.  The offset into the
489    * memory pointed to by the image_ field is obtained from the ImageSection
490    * object for the String Offsets section.
491    *
492    * All data for the image, besides the object bitmap and the relocation data,
493    * will also be copied into the memory region pointed to by image_.
494    */
495   void CopyMetadata();
496 
497   void FixupClass(mirror::Class* orig, mirror::Class* copy)
498       REQUIRES_SHARED(Locks::mutator_lock_);
499   void FixupObject(mirror::Object* orig, mirror::Object* copy)
500       REQUIRES_SHARED(Locks::mutator_lock_);
501 
502   // Get quick code for non-resolution/imt_conflict/abstract method.
503   const uint8_t* GetQuickCode(ArtMethod* method, const ImageInfo& image_info)
504       REQUIRES_SHARED(Locks::mutator_lock_);
505 
506   // Return true if a method is likely to be dirtied at runtime.
507   bool WillMethodBeDirty(ArtMethod* m) const REQUIRES_SHARED(Locks::mutator_lock_);
508 
509   // Assign the offset for an ArtMethod.
510   void AssignMethodOffset(ArtMethod* method,
511                           NativeObjectRelocationType type,
512                           size_t oat_index)
513       REQUIRES_SHARED(Locks::mutator_lock_);
514 
515   // Assign the offset for a method with unique JNI stub.
516   void AssignJniStubMethodOffset(ArtMethod* method, size_t oat_index)
517       REQUIRES_SHARED(Locks::mutator_lock_);
518 
519   // Return true if imt was newly inserted.
520   bool TryAssignImTableOffset(ImTable* imt, size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
521 
522   // Assign the offset for an IMT conflict table. Does nothing if the table already has a native
523   // relocation.
524   void TryAssignConflictTableOffset(ImtConflictTable* table, size_t oat_index)
525       REQUIRES_SHARED(Locks::mutator_lock_);
526 
527   // Return true if `klass` depends on a class defined by the boot class path
528   // we're compiling against but not present in the boot image spaces. We want
529   // to prune these classes since we cannot guarantee that they will not be
530   // already loaded at run time when loading this image. This means that we
531   // also cannot have any classes which refer to these non image classes.
532   bool PruneImageClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
533 
534   // early_exit is true if we had a cyclic dependency anywhere down the chain.
535   bool PruneImageClassInternal(ObjPtr<mirror::Class> klass,
536                                bool* early_exit,
537                                HashSet<mirror::Object*>* visited)
538       REQUIRES_SHARED(Locks::mutator_lock_);
539 
540   void PromoteWeakInternsToStrong(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
541 
IsMultiImage()542   bool IsMultiImage() const {
543     return image_infos_.size() > 1;
544   }
545 
546   static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type);
547 
548   struct NativeObjectRelocation {
549     size_t oat_index;
550     uintptr_t offset;
551     NativeObjectRelocationType type;
552   };
553 
554   struct JniStubMethodRelocation {
555     size_t oat_index;
556     uintptr_t offset;
557   };
558 
559   NativeObjectRelocation GetNativeRelocation(void* obj) const REQUIRES_SHARED(Locks::mutator_lock_);
560 
561   // Location of where the object will be when the image is loaded at runtime.
562   template <typename T>
563   T* NativeLocationInImage(T* obj) REQUIRES_SHARED(Locks::mutator_lock_);
564   ArtField* NativeLocationInImage(ArtField* src_field) REQUIRES_SHARED(Locks::mutator_lock_);
565 
566   // Return true if `dex_cache` belongs to the image we're writing.
567   // For a boot image, this is true for all dex caches.
568   // For an app image, boot class path dex caches are excluded.
569   bool IsImageDexCache(ObjPtr<mirror::DexCache> dex_cache) const
570       REQUIRES_SHARED(Locks::mutator_lock_);
571 
572   // Return true if `obj` is inside of a boot image space that we're compiling against.
573   // (Always false when compiling the boot image.)
IsInBootImage(const void * obj)574   ALWAYS_INLINE bool IsInBootImage(const void* obj) const {
575     return reinterpret_cast<uintptr_t>(obj) - boot_image_begin_ < boot_image_size_;
576   }
577 
578   template <typename MirrorType>
579   static ObjPtr<MirrorType> DecodeGlobalWithoutRB(JavaVMExt* vm, jobject obj)
580       REQUIRES_SHARED(Locks::mutator_lock_);
581 
582   template <typename MirrorType>
583   static ObjPtr<MirrorType> DecodeWeakGlobalWithoutRB(
584       JavaVMExt* vm, Thread* self, jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
585 
586   // Get the index of the oat file associated with the object.
587   size_t GetOatIndex(mirror::Object* object) const REQUIRES_SHARED(Locks::mutator_lock_);
588 
589   // The oat index for shared data in multi-image and all data in single-image compilation.
GetDefaultOatIndex()590   static constexpr size_t GetDefaultOatIndex() {
591     return 0u;
592   }
593 
GetImageInfo(size_t oat_index)594   ImageInfo& GetImageInfo(size_t oat_index) {
595     return image_infos_[oat_index];
596   }
597 
GetImageInfo(size_t oat_index)598   const ImageInfo& GetImageInfo(size_t oat_index) const {
599     return image_infos_[oat_index];
600   }
601 
602   // Return true if there already exists a native allocation for an object.
603   bool NativeRelocationAssigned(void* ptr) const;
604 
605   // Copy a reference, translating source pointer to the target pointer.
606   template <typename DestType>
607   void CopyAndFixupReference(DestType* dest, ObjPtr<mirror::Object> src)
608       REQUIRES_SHARED(Locks::mutator_lock_);
609 
610   // Translate a native pointer to the destination value and store in the target location.
611   template <typename ValueType>
612   void CopyAndFixupPointer(void** target, ValueType src_value, PointerSize pointer_size)
613       REQUIRES_SHARED(Locks::mutator_lock_);
614   template <typename ValueType>
615   void CopyAndFixupPointer(void** target, ValueType src_value)
616       REQUIRES_SHARED(Locks::mutator_lock_);
617   template <typename ValueType>
618   void CopyAndFixupPointer(
619       void* object, MemberOffset offset, ValueType src_value, PointerSize pointer_size)
620       REQUIRES_SHARED(Locks::mutator_lock_);
621   template <typename ValueType>
622   void CopyAndFixupPointer(void* object, MemberOffset offset, ValueType src_value)
623       REQUIRES_SHARED(Locks::mutator_lock_);
624 
625   ALWAYS_INLINE
626   static bool IsStronglyInternedString(ObjPtr<mirror::String> str)
627       REQUIRES_SHARED(Locks::mutator_lock_);
628 
629   /*
630    * Tests an object to see if it will be contained in an AppImage.
631    *
632    * An object reference is considered to be a AppImage String reference iff:
633    *   - It isn't null
634    *   - The referred-object isn't in the boot image
635    *   - The referred-object is a Java String
636    */
637   ALWAYS_INLINE
638   bool IsInternedAppImageStringReference(ObjPtr<mirror::Object> referred_obj) const
639       REQUIRES_SHARED(Locks::mutator_lock_);
640 
641   const CompilerOptions& compiler_options_;
642 
643   // Size of pointers on the target architecture.
644   PointerSize target_ptr_size_;
645 
646   // Whether to mark non-abstract, non-intrinsic methods as "memory shared methods".
647   bool mark_memory_shared_methods_;
648 
649   // Cached boot image begin and size. This includes heap, native objects and oat files.
650   const uint32_t boot_image_begin_;
651   const uint32_t boot_image_size_;
652 
653   // Beginning target image address for the first image.
654   uint8_t* global_image_begin_;
655 
656   // Offset from image_begin_ to where the first object is in image_.
657   size_t image_objects_offset_begin_;
658 
659   // Saved hash codes. We use these to restore lockwords which were temporarily used to have
660   // forwarding addresses as well as copying over hash codes.
661   HashMap<mirror::Object*, uint32_t> saved_hashcode_map_;
662 
663   // Oat index map for objects.
664   HashMap<mirror::Object*, uint32_t> oat_index_map_;
665 
666   // Image data indexed by the oat file index.
667   dchecked_vector<ImageInfo> image_infos_;
668 
669   // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to
670   // have one entry per art field for convenience. ArtFields are placed right after the end of the
671   // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
672   HashMap<void*, NativeObjectRelocation> native_object_relocations_;
673 
674   // HashMap used for generating JniStubMethodsSection.
675   JniStubHashMap<std::pair<ArtMethod*, JniStubMethodRelocation>> jni_stub_map_;
676 
677   // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
678   ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
679 
680   // Counters for measurements, used for logging only.
681   uint64_t dirty_methods_;
682   uint64_t clean_methods_;
683 
684   // Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass.
685   HashMap<mirror::Class*, bool> prune_class_memo_;
686 
687   // The application class loader. Null for boot image.
688   jobject app_class_loader_;
689 
690   // Boot image live objects, invalid for app image.
691   mirror::ObjectArray<mirror::Object>* boot_image_live_objects_;
692 
693   // Image roots corresponding to individual image files.
694   dchecked_vector<jobject> image_roots_;
695 
696   // Which mode the image is stored as, see image.h
697   const ImageHeader::StorageMode image_storage_mode_;
698 
699   // The file names of oat files.
700   const std::vector<std::string>& oat_filenames_;
701 
702   // Map of dex files to the indexes of oat files that they were compiled into.
703   const HashMap<const DexFile*, size_t>& dex_file_oat_index_map_;
704 
705   // Set of classes/objects known to be dirty in the image. Can be nullptr if there are none.
706   // Each entry contains a class descriptor with zero or more reference fields, which denote a path
707   // to the dirty object.
708   const std::vector<std::string>* dirty_image_objects_;
709 
710   // Dirty object instances and their sort keys parsed from dirty_image_object_
711   HashMap<mirror::Object*, uint32_t> dirty_objects_;
712 
713   // Objects are guaranteed to not cross the region size boundary.
714   size_t region_size_ = 0u;
715 
716   // Region alignment bytes wasted.
717   size_t region_alignment_wasted_ = 0u;
718 
719   class FixupClassVisitor;
720   class FixupRootVisitor;
721   class FixupVisitor;
722   class LayoutHelper;
723   class NativeLocationVisitor;
724   class PruneClassesVisitor;
725   class PruneClassLoaderClassesVisitor;
726   class PruneObjectReferenceVisitor;
727 
728   // A visitor used by the VerifyNativeGCRootInvariants() function.
729   class NativeGCRootInvariantVisitor;
730 
731   DISALLOW_COPY_AND_ASSIGN(ImageWriter);
732 };
733 
734 std::ostream& operator<<(std::ostream& stream, ImageWriter::Bin bin);
735 std::ostream& operator<<(std::ostream& stream, ImageWriter::NativeObjectRelocationType type);
736 
737 }  // namespace linker
738 }  // namespace art
739 
740 #endif  // ART_DEX2OAT_LINKER_IMAGE_WRITER_H_
741