xref: /aosp_15_r20/art/runtime/oat/oat_file.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_RUNTIME_OAT_OAT_FILE_H_
18 #define ART_RUNTIME_OAT_OAT_FILE_H_
19 
20 #include <list>
21 #include <memory>
22 #include <string>
23 #include <string_view>
24 #include <vector>
25 
26 #include "base/array_ref.h"
27 #include "base/compiler_filter.h"
28 #include "base/macros.h"
29 #include "base/mutex.h"
30 #include "base/os.h"
31 #include "base/safe_map.h"
32 #include "base/tracking_safe_map.h"
33 #include "class_status.h"
34 #include "dex/dex_file.h"
35 #include "dex/dex_file_layout.h"
36 #include "dex/type_lookup_table.h"
37 #include "dex/utf.h"
38 #include "index_bss_mapping.h"
39 #include "mirror/object.h"
40 
41 namespace art HIDDEN {
42 
43 class BitVector;
44 class ClassLinker;
45 class ClassLoaderContext;
46 class ElfFile;
47 class DexLayoutSections;
48 template <class MirrorType> class GcRoot;
49 class MemMap;
50 class OatDexFile;
51 class OatHeader;
52 class OatMethodOffsets;
53 class OatQuickMethodHeader;
54 class VdexFile;
55 
56 namespace dex {
57 struct ClassDef;
58 }  // namespace dex
59 
60 namespace gc {
61 namespace collector {
62 class FakeOatFile;
63 }  // namespace collector
64 }  // namespace gc
65 
66 // A special compilation reason to indicate that only the VDEX file is usable. Keep in sync with
67 // `ArtConstants::REASON_VDEX` in artd/binder/com/android/server/art/ArtConstants.aidl.
68 static constexpr const char* kReasonVdex = "vdex";
69 
70 // OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can
71 // save even one OatMethodOffsets struct, the more complicated encoding
72 // using a bitmap pays for itself since few classes will have 160
73 // methods.
74 enum class OatClassType : uint8_t {
75   kAllCompiled = 0,   // OatClass is followed by an OatMethodOffsets for each method.
76   kSomeCompiled = 1,  // A bitmap of OatMethodOffsets that are present follows the OatClass.
77   kNoneCompiled = 2,  // All methods are interpreted so no OatMethodOffsets are necessary.
78   kLast = kNoneCompiled
79 };
80 
81 EXPORT std::ostream& operator<<(std::ostream& os, OatClassType rhs);
82 
83 class PACKED(4) OatMethodOffsets {
84  public:
code_offset_(code_offset)85   explicit OatMethodOffsets(uint32_t code_offset = 0) : code_offset_(code_offset) {}
86 
~OatMethodOffsets()87   ~OatMethodOffsets() {}
88 
89   OatMethodOffsets(const OatMethodOffsets&) = default;
90   OatMethodOffsets& operator=(const OatMethodOffsets&) = default;
91 
92   uint32_t code_offset_;
93 };
94 
95 // Runtime representation of the OAT file format which holds compiler output.
96 // The class opens an OAT file from storage and maps it to memory, typically with
97 // dlopen and provides access to its internal data structures (see OatWriter for
98 // for more details about the OAT format).
99 // In the process of loading OAT, the class also loads the associated VDEX file
100 // with the input DEX files (see VdexFile for details about the VDEX format).
101 // The raw DEX data are accessible transparently through the OatDexFile objects.
102 
103 class OatFile {
104  public:
105   // Open an oat file. Returns null on failure.
106   // The `dex_filenames` argument, if provided, overrides the dex locations
107   // from oat file when opening the dex files if they are not embedded in the
108   // vdex file. These may differ for cross-compilation (the dex file name is
109   // the host path and dex location is the future path on target) and testing.
110   EXPORT static OatFile* Open(int zip_fd,
111                               const std::string& filename,
112                               const std::string& location,
113                               bool executable,
114                               bool low_4gb,
115                               ArrayRef<const std::string> dex_filenames,
116                               ArrayRef<File> dex_files,
117                               /*inout*/ MemMap* reservation,  // Where to load if not null.
118                               /*out*/ std::string* error_msg);
119   // Helper overload that takes a single dex filename and no reservation.
Open(int zip_fd,const std::string & filename,const std::string & location,bool executable,bool low_4gb,const std::string & dex_filename,std::string * error_msg)120   EXPORT static OatFile* Open(int zip_fd,
121                               const std::string& filename,
122                               const std::string& location,
123                               bool executable,
124                               bool low_4gb,
125                               const std::string& dex_filename,
126                               /*out*/ std::string* error_msg) {
127     return Open(zip_fd,
128                 filename,
129                 location,
130                 executable,
131                 low_4gb,
132                 ArrayRef<const std::string>(&dex_filename, /*size=*/1u),
133                 /*dex_files=*/{},  // not currently supported
134                 /*reservation=*/nullptr,
135                 error_msg);
136   }
137   // Helper overload that takes no dex filename and no reservation.
Open(int zip_fd,const std::string & filename,const std::string & location,bool executable,bool low_4gb,std::string * error_msg)138   static OatFile* Open(int zip_fd,
139                        const std::string& filename,
140                        const std::string& location,
141                        bool executable,
142                        bool low_4gb,
143                        /*out*/std::string* error_msg) {
144     return Open(zip_fd,
145                 filename,
146                 location,
147                 executable,
148                 low_4gb,
149                 /*dex_filenames=*/{},
150                 /*dex_files=*/{},  // not currently supported
151                 /*reservation=*/nullptr,
152                 error_msg);
153   }
154 
155   // Similar to OatFile::Open(const std::string...), but accepts input vdex and
156   // odex files as file descriptors. We also take zip_fd in case the vdex does not
157   // contain the dex code, and we need to read it from the zip file.
158   static OatFile* Open(int zip_fd,
159                        int vdex_fd,
160                        int oat_fd,
161                        const std::string& oat_location,
162                        bool executable,
163                        bool low_4gb,
164                        ArrayRef<const std::string> dex_filenames,
165                        ArrayRef<File> dex_files,
166                        /*inout*/ MemMap* reservation,  // Where to load if not null.
167                        /*out*/ std::string* error_msg);
168 
169   // Initialize OatFile instance from an already loaded VdexFile. This assumes
170   // the vdex does not have a dex section and accepts a vector of DexFiles separately.
171   static OatFile* OpenFromVdex(const std::vector<const DexFile*>& dex_files,
172                                std::unique_ptr<VdexFile>&& vdex_file,
173                                const std::string& location,
174                                ClassLoaderContext* context);
175 
176   // Initialize OatFile instance from an already loaded VdexFile. The dex files
177   // will be opened through `zip_fd` or `dex_location` if `zip_fd` is -1.
178   static OatFile* OpenFromVdex(int zip_fd,
179                                std::unique_ptr<VdexFile>&& vdex_file,
180                                const std::string& location,
181                                ClassLoaderContext* context,
182                                std::string* error_msg);
183 
184   // Set the start of the app image.
185   // Needed for initializing app image relocations in the .data.img.rel.ro section.
SetAppImageBegin(uint8_t * app_image_begin)186   void SetAppImageBegin(uint8_t* app_image_begin) const {
187     app_image_begin_ = app_image_begin;
188   }
189 
190   // Return whether the `OatFile` uses a vdex-only file.
191   bool IsBackedByVdexOnly() const;
192 
193   virtual ~OatFile();
194 
IsExecutable()195   bool IsExecutable() const {
196     return is_executable_;
197   }
198 
199   // Indicates whether the oat file was compiled with full debugging capability.
200   bool IsDebuggable() const;
201 
202   EXPORT CompilerFilter::Filter GetCompilerFilter() const;
203 
204   std::string GetClassLoaderContext() const;
205 
206   const char* GetCompilationReason() const;
207 
GetLocation()208   const std::string& GetLocation() const {
209     return location_;
210   }
211 
212   EXPORT const OatHeader& GetOatHeader() const;
213 
214   class OatMethod final {
215    public:
GetCodeOffset()216     uint32_t GetCodeOffset() const { return code_offset_; }
217 
218     const void* GetQuickCode() const;
219 
220     // Returns size of quick code.
221     uint32_t GetQuickCodeSize() const;
222 
223     // Returns OatQuickMethodHeader for debugging. Most callers should
224     // use more specific methods such as GetQuickCodeSize.
225     const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
226     uint32_t GetOatQuickMethodHeaderOffset() const;
227 
228     size_t GetFrameSizeInBytes() const;
229     uint32_t GetCoreSpillMask() const;
230     uint32_t GetFpSpillMask() const;
231 
232     const uint8_t* GetVmapTable() const;
233     uint32_t GetVmapTableOffset() const;
234 
235     // Create an OatMethod with offsets relative to the given base address
OatMethod(const uint8_t * base,const uint32_t code_offset)236     OatMethod(const uint8_t* base, const uint32_t code_offset)
237         : begin_(base), code_offset_(code_offset) {
238     }
239     OatMethod(const OatMethod&) = default;
~OatMethod()240     ~OatMethod() {}
241 
242     OatMethod& operator=(const OatMethod&) = default;
243 
244     // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
245     // See ClassLinker::FindOatMethodFor.
Invalid()246     static const OatMethod Invalid() {
247       return OatMethod(nullptr, -1);
248     }
249 
250    private:
251     const uint8_t* begin_;
252     uint32_t code_offset_;
253 
254     friend class OatClass;
255   };
256 
257   class OatClass final {
258    public:
GetStatus()259     ClassStatus GetStatus() const {
260       return status_;
261     }
262 
GetType()263     OatClassType GetType() const {
264       return type_;
265     }
266 
267     // Get the OatMethod entry based on its index into the class
268     // defintion. Direct methods come first, followed by virtual
269     // methods. Note that runtime created methods such as miranda
270     // methods are not included.
271     EXPORT const OatMethod GetOatMethod(uint32_t method_index) const;
272 
273     // Return a pointer to the OatMethodOffsets for the requested
274     // method_index, or null if none is present. Note that most
275     // callers should use GetOatMethod.
276     EXPORT const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
277 
278     // Return the offset from the start of the OatFile to the
279     // OatMethodOffsets for the requested method_index, or 0 if none
280     // is present. Note that most callers should use GetOatMethod.
281     EXPORT uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
282 
283     // A representation of an invalid OatClass, used when an OatClass can't be found.
284     // See FindOatClass().
Invalid()285     static OatClass Invalid() {
286       return OatClass(/* oat_file= */ nullptr,
287                       ClassStatus::kErrorUnresolved,
288                       OatClassType::kNoneCompiled,
289                       /* num_methods= */ 0,
290                       /* bitmap_pointer= */ nullptr,
291                       /* methods_pointer= */ nullptr);
292     }
293 
294    private:
295     OatClass(const OatFile* oat_file,
296              ClassStatus status,
297              OatClassType type,
298              uint32_t num_methods,
299              const uint32_t* bitmap_pointer,
300              const OatMethodOffsets* methods_pointer);
301 
302     const OatFile* const oat_file_;
303     const ClassStatus status_;
304     const OatClassType type_;
305     const uint32_t num_methods_;
306     const uint32_t* const bitmap_;
307     const OatMethodOffsets* const methods_pointer_;
308 
309     friend class ClassLinker;
310     friend class OatDexFile;
311   };
312 
313   // Get the OatDexFile for the given dex_location within this oat file.
314   // If dex_location_checksum is non-null, the OatDexFile will only be
315   // returned if it has a matching checksum.
316   // If error_msg is non-null and no OatDexFile is returned, error_msg will
317   // be updated with a description of why no OatDexFile was returned.
318   const OatDexFile* GetOatDexFile(const char* dex_location,
319                                   /*out*/ std::string* error_msg = nullptr) const
320       REQUIRES(!secondary_lookup_lock_);
321 
GetOatDexFiles()322   const std::vector<const OatDexFile*>& GetOatDexFiles() const {
323     return oat_dex_files_storage_;
324   }
325 
Size()326   size_t Size() const {
327     return End() - Begin();
328   }
329 
Contains(const void * p)330   bool Contains(const void* p) const {
331     return p >= Begin() && p < End();
332   }
333 
DataImgRelRoSize()334   size_t DataImgRelRoSize() const {
335     return DataImgRelRoEnd() - DataImgRelRoBegin();
336   }
337 
DataImgRelRoAppImageOffset()338   size_t DataImgRelRoAppImageOffset() const {
339     return DataImgRelRoAppImage() - DataImgRelRoBegin();
340   }
341 
BssSize()342   size_t BssSize() const {
343     return BssEnd() - BssBegin();
344   }
345 
VdexSize()346   size_t VdexSize() const {
347     return VdexEnd() - VdexBegin();
348   }
349 
BssMethodsOffset()350   size_t BssMethodsOffset() const {
351     // Note: This is used only for symbolizer and needs to return a valid .bss offset.
352     return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset();
353   }
354 
BssRootsOffset()355   size_t BssRootsOffset() const {
356     // Note: This is used only for symbolizer and needs to return a valid .bss offset.
357     return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize();
358   }
359 
DexSize()360   size_t DexSize() const {
361     return DexEnd() - DexBegin();
362   }
363 
364   // Returns the base address of the ELF file, or nullptr if the oat file is not backed by an ELF
365   // file or an error occurred.
366   virtual const uint8_t* ComputeElfBegin(std::string* error_msg) const = 0;
367 
368   EXPORT const uint8_t* Begin() const;
369   EXPORT const uint8_t* End() const;
370 
DataImgRelRoBegin()371   const uint8_t* DataImgRelRoBegin() const { return data_img_rel_ro_begin_; }
DataImgRelRoEnd()372   const uint8_t* DataImgRelRoEnd() const { return data_img_rel_ro_end_; }
DataImgRelRoAppImage()373   const uint8_t* DataImgRelRoAppImage() const { return data_img_rel_ro_app_image_; }
374 
BssBegin()375   const uint8_t* BssBegin() const { return bss_begin_; }
BssEnd()376   const uint8_t* BssEnd() const { return bss_end_; }
377 
VdexBegin()378   const uint8_t* VdexBegin() const { return vdex_begin_; }
VdexEnd()379   const uint8_t* VdexEnd() const { return vdex_end_; }
380 
381   EXPORT const uint8_t* DexBegin() const;
382   EXPORT const uint8_t* DexEnd() const;
383 
384   EXPORT ArrayRef<const uint32_t> GetBootImageRelocations() const;
385   EXPORT ArrayRef<const uint32_t> GetAppImageRelocations() const;
386   EXPORT ArrayRef<ArtMethod*> GetBssMethods() const;
387   EXPORT ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const;
388 
389   // Initialize relocation sections (.data.img.rel.ro and .bss).
390   void InitializeRelocations() const;
391 
392   // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
393   // error and sets found to false.
394   EXPORT static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found);
395 
GetVdexFile()396   VdexFile* GetVdexFile() const {
397     return vdex_.get();
398   }
399 
400   // Whether the OatFile embeds the Dex code.
ContainsDexCode()401   bool ContainsDexCode() const {
402     return external_dex_files_.empty();
403   }
404 
405   // Returns whether an image (e.g. app image) is required to safely execute this OAT file.
406   bool RequiresImage() const;
407 
408   struct BssMappingInfo {
409     const IndexBssMapping* method_bss_mapping = nullptr;
410     const IndexBssMapping* type_bss_mapping = nullptr;
411     const IndexBssMapping* public_type_bss_mapping = nullptr;
412     const IndexBssMapping* package_type_bss_mapping = nullptr;
413     const IndexBssMapping* string_bss_mapping = nullptr;
414     const IndexBssMapping* method_type_bss_mapping = nullptr;
415   };
416 
GetBcpBssInfo()417   ArrayRef<const BssMappingInfo> GetBcpBssInfo() const {
418     return ArrayRef<const BssMappingInfo>(bcp_bss_info_);
419   }
420 
421   // Returns the mapping info of `dex_file` if found in the BcpBssInfo, or nullptr otherwise.
422   const BssMappingInfo* FindBcpMappingInfo(const DexFile* dex_file) const;
423 
424  protected:
425   OatFile(const std::string& filename, bool executable);
426 
427  private:
428   // The oat file name.
429   //
430   // The image will embed this to link its associated oat file.
431   const std::string location_;
432 
433   // Pointer to the Vdex file with the Dex files for this Oat file.
434   std::unique_ptr<VdexFile> vdex_;
435 
436   // Pointer to OatHeader.
437   const uint8_t* begin_;
438 
439   // Pointer to end of oat region for bounds checking.
440   const uint8_t* end_;
441 
442   // Pointer to the .data.img.rel.ro section, if present, otherwise null.
443   const uint8_t* data_img_rel_ro_begin_;
444 
445   // Pointer to the end of the .data.img.rel.ro section, if present, otherwise null.
446   const uint8_t* data_img_rel_ro_end_;
447 
448   // Pointer to the beginning of the app image relocations in the .data.img.rel.ro section,
449   // if present, otherwise null.
450   const uint8_t* data_img_rel_ro_app_image_;
451 
452   // Pointer to the .bss section, if present, otherwise null.
453   uint8_t* bss_begin_;
454 
455   // Pointer to the end of the .bss section, if present, otherwise null.
456   uint8_t* bss_end_;
457 
458   // Pointer to the beginning of the ArtMethod*s in the .bss section, if present, otherwise null.
459   uint8_t* bss_methods_;
460 
461   // Pointer to the beginning of the GC roots in the .bss section, if present, otherwise null.
462   uint8_t* bss_roots_;
463 
464   // Was this oat_file loaded executable?
465   const bool is_executable_;
466 
467   // Pointer to the .vdex section, if present, otherwise null.
468   uint8_t* vdex_begin_;
469 
470   // Pointer to the end of the .vdex section, if present, otherwise null.
471   uint8_t* vdex_end_;
472 
473   // Pointer to the beginning of the app image, if any.
474   mutable uint8_t* app_image_begin_;
475 
476   // Owning storage for the OatDexFile objects.
477   std::vector<const OatDexFile*> oat_dex_files_storage_;
478 
479   // Mapping info for DexFiles in the BCP.
480   std::vector<BssMappingInfo> bcp_bss_info_;
481 
482   // NOTE: We use a std::string_view as the key type to avoid a memory allocation on every
483   // lookup with a const char* key. The std::string_view doesn't own its backing storage,
484   // therefore we're using the OatFile's stored dex location as the backing storage
485   // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
486   // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
487   using Table =
488       AllocationTrackingSafeMap<std::string_view, const OatDexFile*, kAllocatorTagOatFile>;
489 
490   // Map each location and canonical location (if different) retrieved from the
491   // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
492   // and therefore doesn't need any locking and provides the cheapest dex file lookup
493   // for GetOatDexFile() for a very frequent use case. Never contains a null value.
494   Table oat_dex_files_;
495 
496   // Lock guarding all members needed for secondary lookup in GetOatDexFile().
497   mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
498 
499   // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
500   // the results of all previous secondary lookups, whether successful (non-null) or
501   // failed (null). If it doesn't contain an entry we need to calculate the canonical
502   // location and use oat_dex_files_by_canonical_location_.
503   mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
504 
505   // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
506   // and the lazily initialized oat_dex_files_by_canonical_location_.
507   // NOTE: We're keeping references to contained strings in form of std::string_view and adding
508   // new strings to the end. The adding of a new element must not touch any previously stored
509   // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
510   mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
511 
512   // Dex files opened directly from a file referenced from the oat file or specifed
513   // by the `dex_filenames` parameter, in case the OatFile does not embed the dex code.
514   std::vector<std::unique_ptr<const DexFile>> external_dex_files_;
515 
516   friend class gc::collector::FakeOatFile;  // For modifying begin_ and end_.
517   friend class OatClass;
518   friend class art::OatDexFile;
519   friend class OatDumper;  // For GetBase and GetLimit
520   friend class OatFileBackedByVdex;
521   friend class OatFileBase;
522   DISALLOW_COPY_AND_ASSIGN(OatFile);
523 };
524 
525 // OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
526 // support forward declarations of inner classes, and we want to
527 // forward-declare OatDexFile so that we can store an opaque pointer to an
528 // OatDexFile in DexFile.
529 class OatDexFile final {
530  public:
531   // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
532   EXPORT std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
533 
534   // May return null if the OatDexFile only contains a type lookup table. This case only happens
535   // for the compiler to speed up compilation, or in jitzygote.
GetOatFile()536   const OatFile* GetOatFile() const {
537     return oat_file_;
538   }
539 
540   // Returns the size of the DexFile refered to by this OatDexFile.
541   EXPORT size_t FileSize() const;
542 
543   // Returns original path of DexFile that was the source of this OatDexFile.
GetDexFileLocation()544   const std::string& GetDexFileLocation() const {
545     return dex_file_location_;
546   }
547 
548   // Returns original path of DexFile that was the source of this OatDexFile.
GetLocation()549   const std::string& GetLocation() const { return dex_file_location_; }
550 
551   // Returns the canonical location of DexFile that was the source of this OatDexFile.
GetCanonicalDexFileLocation()552   const std::string& GetCanonicalDexFileLocation() const {
553     return canonical_dex_file_location_;
554   }
555 
GetMagic()556   DexFile::Magic GetMagic() const { return dex_file_magic_; }
557 
558   uint32_t GetDexVersion() const;
559 
560   // Returns checksum of original DexFile that was the source of this OatDexFile;
GetDexFileLocationChecksum()561   uint32_t GetDexFileLocationChecksum() const {
562     return dex_file_location_checksum_;
563   }
564 
565   // Returns checksum of original DexFile that was the source of this OatDexFile;
GetLocationChecksum()566   uint32_t GetLocationChecksum() const { return dex_file_location_checksum_; }
567 
GetSha1()568   DexFile::Sha1 GetSha1() const { return dex_file_sha1_; }
569 
570   // Returns the OatClass for the class specified by the given DexFile class_def_index.
571   EXPORT OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
572 
573   // Returns the offset to the OatClass information. Most callers should use GetOatClass.
574   EXPORT uint32_t GetOatClassOffset(uint16_t class_def_index) const;
575 
GetLookupTableData()576   const uint8_t* GetLookupTableData() const {
577     return lookup_table_data_;
578   }
579 
GetMethodBssMapping()580   const IndexBssMapping* GetMethodBssMapping() const {
581     return bss_mapping_info_.method_bss_mapping;
582   }
583 
GetTypeBssMapping()584   const IndexBssMapping* GetTypeBssMapping() const {
585     return bss_mapping_info_.type_bss_mapping;
586   }
587 
GetPublicTypeBssMapping()588   const IndexBssMapping* GetPublicTypeBssMapping() const {
589     return bss_mapping_info_.public_type_bss_mapping;
590   }
591 
GetPackageTypeBssMapping()592   const IndexBssMapping* GetPackageTypeBssMapping() const {
593     return bss_mapping_info_.package_type_bss_mapping;
594   }
595 
GetStringBssMapping()596   const IndexBssMapping* GetStringBssMapping() const {
597     return bss_mapping_info_.string_bss_mapping;
598   }
599 
GetMethodTypeBssMapping()600   const IndexBssMapping* GetMethodTypeBssMapping() const {
601     return bss_mapping_info_.method_type_bss_mapping;
602   }
603 
GetDexFilePointer()604   const uint8_t* GetDexFilePointer() const {
605     return dex_file_pointer_;
606   }
607 
608   // Looks up a class definition by its class descriptor. Hash must be
609   // ComputeModifiedUtf8Hash(descriptor).
610   EXPORT static const dex::ClassDef* FindClassDef(const DexFile& dex_file,
611                                                   std::string_view descriptor,
612                                                   size_t hash);
613 
GetTypeLookupTable()614   const TypeLookupTable& GetTypeLookupTable() const {
615     return lookup_table_;
616   }
617 
618   EXPORT ~OatDexFile();
619 
620   // Create only with a type lookup table, used by the compiler to speed up compilation.
621   EXPORT explicit OatDexFile(TypeLookupTable&& lookup_table);
622 
623   // Return the dex layout sections.
GetDexLayoutSections()624   const DexLayoutSections* GetDexLayoutSections() const {
625     return dex_layout_sections_;
626   }
627 
628  private:
629   OatDexFile(const OatFile* oat_file,
630              const std::string& dex_file_location,
631              const std::string& canonical_dex_file_location,
632              DexFile::Magic dex_file_magic,
633              uint32_t dex_file_checksum,
634              DexFile::Sha1 dex_file_sha1,
635              const std::shared_ptr<DexFileContainer>& dex_file_container_,
636              const uint8_t* dex_file_pointer,
637              const uint8_t* lookup_table_data,
638              const OatFile::BssMappingInfo& bss_mapping_info,
639              const uint32_t* oat_class_offsets_pointer,
640              const DexLayoutSections* dex_layout_sections);
641 
642   // Create an OatDexFile wrapping an existing DexFile. Will set the OatDexFile
643   // pointer in the DexFile.
644   OatDexFile(const OatFile* oat_file,
645              const std::shared_ptr<DexFileContainer>& dex_file_container_,
646              const uint8_t* dex_file_pointer,
647              DexFile::Magic dex_file_magic,
648              uint32_t dex_file_checksum,
649              DexFile::Sha1 dex_file_sha1,
650              const std::string& dex_file_location,
651              const std::string& canonical_dex_file_location,
652              const uint8_t* lookup_table_data);
653 
654   bool IsBackedByVdexOnly() const;
655   void InitializeTypeLookupTable();
656 
657   static void AssertAotCompiler();
658 
659   const OatFile* const oat_file_ = nullptr;
660   const std::string dex_file_location_;
661   const std::string canonical_dex_file_location_;
662   const DexFile::Magic dex_file_magic_ = {};
663   const uint32_t dex_file_location_checksum_ = 0u;
664   const DexFile::Sha1 dex_file_sha1_ = {};
665   const std::shared_ptr<DexFileContainer> dex_file_container_;
666   const uint8_t* const dex_file_pointer_ = nullptr;
667   const uint8_t* const lookup_table_data_ = nullptr;
668   const OatFile::BssMappingInfo bss_mapping_info_;
669   const uint32_t* const oat_class_offsets_pointer_ = nullptr;
670   TypeLookupTable lookup_table_;
671   const DexLayoutSections* const dex_layout_sections_ = nullptr;
672 
673   friend class OatFile;
674   friend class OatFileBase;
675   friend class OatFileBackedByVdex;
676   DISALLOW_COPY_AND_ASSIGN(OatDexFile);
677 };
678 
679 }  // namespace art
680 
681 #endif  // ART_RUNTIME_OAT_OAT_FILE_H_
682