oat_file.h revision 756ee4e090bc1e1812b41fb7b4661df601a32ef9
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
16e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_OAT_FILE_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_OAT_FILE_H_
19e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
20700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include <string>
21e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include <vector>
22e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "dex_file.h"
242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "invoke_type.h"
252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mem_map.h"
26ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method.h"
272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "oat.h"
28700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "os.h"
29e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
30e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstromnamespace art {
31e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
32700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstromclass ElfFile;
33700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstromclass MemMap;
34700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstromclass OatMethodOffsets;
3533e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogersclass OatHeader;
36700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
37e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstromclass OatFile {
38e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom public:
3930e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom  // Returns an .odex file name next adjacent to the dex location.
4030e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom  // For example, for "/foo/bar/baz.jar", return "/foo/bar/baz.odex".
4130e2ea4a701d53f28431041af68dc0669e60c569Brian Carlstrom  static std::string DexFilenameToOdexFilename(const std::string& location);
42b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom
43e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  // Open an oat file. Returns NULL on failure.  Requested base can
44e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  // optionally be used to request where the file should be loaded.
45e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  static OatFile* Open(const std::string& filename,
46a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom                       const std::string& location,
47f1d3455064792ac1c486a4a9c24279a37b4af473Brian Carlstrom                       byte* requested_base,
48f1d3455064792ac1c486a4a9c24279a37b4af473Brian Carlstrom                       bool executable);
49e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
50700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Open an oat file from an already opened File.
51265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // Does not use dlopen underneath so cannot be used for runtime use
52265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // where relocations may be required. Currently used from
53265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // ImageWriter which wants to open a writable version from an existing
54265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  // file descriptor for patching.
55265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  static OatFile* OpenWritable(File* file, const std::string& location);
56700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
57700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Open an oat file backed by a std::vector with the given location.
58265091e581c9f643b37e7966890911f09e223269Brian Carlstrom  static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
59265091e581c9f643b37e7966890911f09e223269Brian Carlstrom                             const std::string& location);
605b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom
61e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  ~OatFile();
62e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
63e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  const std::string& GetLocation() const {
64e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    return location_;
65e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  }
66e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
67e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  const OatHeader& GetOatHeader() const;
68e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
69e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  class OatDexFile;
70e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
713320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  class OatMethod {
723320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom   public:
73ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    void LinkMethod(mirror::ArtMethod* method) const;
74ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
75ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t GetCodeOffset() const {
76ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return code_offset_;
77ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
78ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    size_t GetFrameSizeInBytes() const {
79ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return frame_size_in_bytes_;
80ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
81ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t GetCoreSpillMask() const {
82ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return core_spill_mask_;
83ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
84ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t GetFpSpillMask() const {
85ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return fp_spill_mask_;
86ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
87ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t GetMappingTableOffset() const {
88ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return mapping_table_offset_;
89ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
90ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t GetVmapTableOffset() const {
91ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      return vmap_table_offset_;
92ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
930c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers    uint32_t GetNativeGcMapOffset() const {
940c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers      return native_gc_map_offset_;
95e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom    }
96ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
970c717dd1c56bd29cf860d0feda8e629dab2cadb3Logan Chien    const void* GetCode() const;
980c717dd1c56bd29cf860d0feda8e629dab2cadb3Logan Chien    uint32_t GetCodeSize() const;
990c717dd1c56bd29cf860d0feda8e629dab2cadb3Logan Chien
1001809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* GetMappingTable() const {
1011809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      return GetOatPointer<const uint8_t*>(mapping_table_offset_);
102ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
1031809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* GetVmapTable() const {
1041809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      return GetOatPointer<const uint8_t*>(vmap_table_offset_);
105ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
1060c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers    const uint8_t* GetNativeGcMap() const {
1070c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers      return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
108e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom    }
1090c717dd1c56bd29cf860d0feda8e629dab2cadb3Logan Chien
110ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    ~OatMethod();
111ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
112ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    // Create an OatMethod with offsets relative to the given base address
113ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    OatMethod(const byte* base,
114ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom              const uint32_t code_offset,
1153320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom              const size_t frame_size_in_bytes,
1163320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom              const uint32_t core_spill_mask,
1173320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom              const uint32_t fp_spill_mask,
118ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom              const uint32_t mapping_table_offset,
119ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom              const uint32_t vmap_table_offset,
1206a47b9dc850b903aabefcfab4adb132cb68bba2eBrian Carlstrom              const uint32_t gc_map_offset);
1213320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
122ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom   private:
123ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    template<class T>
124ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    T GetOatPointer(uint32_t offset) const {
125ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      if (offset == 0) {
126ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom        return NULL;
127ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom      }
12830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      return reinterpret_cast<T>(begin_ + offset);
129ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    }
1303320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
13130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    const byte* begin_;
1323320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
133ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t code_offset_;
1343320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom    size_t frame_size_in_bytes_;
1353320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom    uint32_t core_spill_mask_;
1363320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom    uint32_t fp_spill_mask_;
137ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t mapping_table_offset_;
138ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    uint32_t vmap_table_offset_;
1390c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers    uint32_t native_gc_map_offset_;
140ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
141ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom    friend class OatClass;
1423320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  };
1433320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
144e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  class OatClass {
145e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   public:
1462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class::Status GetStatus() const;
1470755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom
1483320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom    // get the OatMethod entry based on its index into the class
149e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    // defintion. direct methods come first, followed by virtual
150e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    // methods. note that runtime created methods such as miranda
151e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    // methods are not included.
152aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    const OatMethod GetOatMethod(uint32_t method_index) const;
153e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    ~OatClass();
154e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
155e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   private:
1560755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom    OatClass(const OatFile* oat_file,
1572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers             mirror::Class::Status status,
1580755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom             const OatMethodOffsets* methods_pointer);
1593320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
160e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    const OatFile* oat_file_;
1612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    const mirror::Class::Status status_;
1623320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom    const OatMethodOffsets* methods_pointer_;
163e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
164e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    friend class OatDexFile;
165e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  };
166e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
167e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  class OatDexFile {
168e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   public:
16956d947fbc9bc2992e2f93112fafb73e50d2aaa7aBrian Carlstrom    // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
17089521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom    const DexFile* OpenDexFile() const;
17156d947fbc9bc2992e2f93112fafb73e50d2aaa7aBrian Carlstrom
17256d947fbc9bc2992e2f93112fafb73e50d2aaa7aBrian Carlstrom    // Returns the size of the DexFile refered to by this OatDexFile.
17305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    size_t FileSize() const;
174aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
17556d947fbc9bc2992e2f93112fafb73e50d2aaa7aBrian Carlstrom    // Returns original path of DexFile that was the source of this OatDexFile.
176aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    const std::string& GetDexFileLocation() const {
177aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      return dex_file_location_;
178aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
17958ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom
18056d947fbc9bc2992e2f93112fafb73e50d2aaa7aBrian Carlstrom    // Returns checksum of original DexFile that was the source of this OatDexFile;
1815b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    uint32_t GetDexFileLocationChecksum() const {
1825b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom      return dex_file_location_checksum_;
18358ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom    }
18458ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom
18556d947fbc9bc2992e2f93112fafb73e50d2aaa7aBrian Carlstrom    // Returns the OatClass for the class specified by the given DexFile class_def_index.
186ee39a10e45a6a0880e8b829525c40d6055818560Ian Rogers    const OatClass* GetOatClass(uint16_t class_def_index) const;
18756d947fbc9bc2992e2f93112fafb73e50d2aaa7aBrian Carlstrom
188e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    ~OatDexFile();
189a21039c3ae2b20e44ceb2735251c04d0aac89afdElliott Hughes
190e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   private:
191e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    OatDexFile(const OatFile* oat_file,
192aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes               const std::string& dex_file_location,
193e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom               uint32_t dex_file_checksum,
194700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom               const byte* dex_file_pointer,
1950755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom               const uint32_t* oat_class_offsets_pointer);
196e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
197e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    const OatFile* oat_file_;
198e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    std::string dex_file_location_;
1995b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    uint32_t dex_file_location_checksum_;
20089521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom    const byte* dex_file_pointer_;
2010755ec5ea1dce0b549fc1adefeb52d89f119ebecBrian Carlstrom    const uint32_t* oat_class_offsets_pointer_;
202e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
203e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    friend class OatFile;
204e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    DISALLOW_COPY_AND_ASSIGN(OatDexFile);
205e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  };
206e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
207756ee4e090bc1e1812b41fb7b4661df601a32ef9Brian Carlstrom  const OatDexFile* GetOatDexFile(const std::string& dex_location,
208756ee4e090bc1e1812b41fb7b4661df601a32ef9Brian Carlstrom                                  const uint32_t* const dex_location_checksum,
20933e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers                                  bool exception_if_not_found = true) const
21033e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
211aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  std::vector<const OatDexFile*> GetOatDexFiles() const;
212e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
21330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t Size() const {
21430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    return End() - Begin();
215e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  }
216e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
217e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom private:
218700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  static void CheckLocation(const std::string& location);
219700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
220700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  static OatFile* OpenDlopen(const std::string& elf_filename,
221700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom                             const std::string& location,
222700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom                             byte* requested_base);
223700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
224700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  static OatFile* OpenElfFile(File* file,
225700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom                              const std::string& location,
226700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom                              byte* requested_base,
227f1d3455064792ac1c486a4a9c24279a37b4af473Brian Carlstrom                              bool writable,
228f1d3455064792ac1c486a4a9c24279a37b4af473Brian Carlstrom                              bool executable);
229700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
230a51a3dd5603daf3d368b7735067e1d9eb54c4c40Elliott Hughes  explicit OatFile(const std::string& filename);
231700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  bool Dlopen(const std::string& elf_filename, byte* requested_base);
232f1d3455064792ac1c486a4a9c24279a37b4af473Brian Carlstrom  bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable);
233f1b3030832bb33efb9161d851b2915d7d94bedf7Brian Carlstrom  bool Setup();
234e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
23530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  const byte* Begin() const;
23630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  const byte* End() const;
237e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
238e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  // The oat file name.
239e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  //
240e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  // The image will embed this to link its associated oat file.
241e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  const std::string location_;
242e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
243700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Pointer to OatHeader.
244700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  const byte* begin_;
245700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
246700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Pointer to end of oat region for bounds checking.
247700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  const byte* end_;
248700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
249700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
250e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  UniquePtr<MemMap> mem_map_;
251e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
252700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Backing memory map for oat file during cross compilation.
253700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  UniquePtr<ElfFile> elf_file_;
254700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
255700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // dlopen handle during runtime.
256700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  void* dlopen_handle_;
257700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
258a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  typedef SafeMap<std::string, const OatDexFile*> Table;
259e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  Table oat_dex_files_;
260e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
261e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  friend class OatClass;
262e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  friend class OatDexFile;
263e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  friend class OatDumper;  // For GetBase and GetLimit
264e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  DISALLOW_COPY_AND_ASSIGN(OatFile);
265e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom};
266e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
267e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}  // namespace art
268e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
269fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_OAT_FILE_H_
270