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