oat_file.h revision 5369c40f75fdcb1be7a7c06db212ce965c83a164
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_FILE_H_
18#define ART_RUNTIME_OAT_FILE_H_
19
20#include <list>
21#include <string>
22#include <vector>
23
24#include "base/mutex.h"
25#include "base/stringpiece.h"
26#include "dex_file.h"
27#include "invoke_type.h"
28#include "mem_map.h"
29#include "mirror/class.h"
30#include "oat.h"
31#include "os.h"
32
33namespace art {
34
35class BitVector;
36class ElfFile;
37class MemMap;
38class OatMethodOffsets;
39class OatHeader;
40
41class OatFile {
42 public:
43  // Open an oat file. Returns NULL on failure.  Requested base can
44  // optionally be used to request where the file should be loaded.
45  static OatFile* Open(const std::string& filename,
46                       const std::string& location,
47                       byte* requested_base,
48                       bool executable,
49                       std::string* error_msg);
50
51  // Open an oat file from an already opened File.
52  // Does not use dlopen underneath so cannot be used for runtime use
53  // where relocations may be required. Currently used from
54  // ImageWriter which wants to open a writable version from an existing
55  // file descriptor for patching.
56  static OatFile* OpenWritable(File* file, const std::string& location, std::string* error_msg);
57  // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
58  static OatFile* OpenReadable(File* file, const std::string& location, std::string* error_msg);
59
60  // Open an oat file backed by a std::vector with the given location.
61  static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
62                             const std::string& location,
63                             std::string* error_msg);
64
65  ~OatFile();
66
67  bool IsExecutable() const {
68    return is_executable_;
69  }
70
71  ElfFile* GetElfFile() const {
72    CHECK_NE(reinterpret_cast<uintptr_t>(elf_file_.get()), reinterpret_cast<uintptr_t>(nullptr))
73        << "Cannot get an elf file from " << GetLocation();
74    return elf_file_.get();
75  }
76
77  const std::string& GetLocation() const {
78    return location_;
79  }
80
81  const OatHeader& GetOatHeader() const;
82
83  class OatDexFile;
84
85  class OatMethod {
86   public:
87    void LinkMethod(mirror::ArtMethod* method) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88
89    uint32_t GetCodeOffset() const {
90      return code_offset_;
91    }
92    uint32_t GetNativeGcMapOffset() const {
93      return native_gc_map_offset_;
94    }
95
96    const void* GetPortableCode() const {
97      // TODO: encode whether code is portable/quick in flags within OatMethod.
98      if (kUsePortableCompiler) {
99        return GetOatPointer<const void*>(code_offset_);
100      } else {
101        return nullptr;
102      }
103    }
104
105    const void* GetQuickCode() const {
106      if (kUsePortableCompiler) {
107        return nullptr;
108      } else {
109        return GetOatPointer<const void*>(code_offset_);
110      }
111    }
112
113    uint32_t GetPortableCodeSize() const {
114      // TODO: With Quick, we store the size before the code. With Portable, the code is in a .o
115      // file we don't manage ourselves. ELF symbols do have a concept of size, so we could capture
116      // that and store it somewhere, such as the OatMethod.
117      return 0;
118    }
119    uint32_t GetQuickCodeSize() const;
120
121    const uint8_t* GetNativeGcMap() const {
122      return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
123    }
124
125    size_t GetFrameSizeInBytes() const;
126    uint32_t GetCoreSpillMask() const;
127    uint32_t GetFpSpillMask() const;
128    uint32_t GetMappingTableOffset() const;
129    uint32_t GetVmapTableOffset() const;
130    const uint8_t* GetMappingTable() const;
131    const uint8_t* GetVmapTable() const;
132
133    ~OatMethod();
134
135    // Create an OatMethod with offsets relative to the given base address
136    OatMethod(const byte* base,
137              const uint32_t code_offset,
138              const uint32_t gc_map_offset);
139
140    OatMethod() {}
141
142   private:
143    template<class T>
144    T GetOatPointer(uint32_t offset) const {
145      if (offset == 0) {
146        return NULL;
147      }
148      return reinterpret_cast<T>(begin_ + offset);
149    }
150
151    const byte* begin_;
152
153    uint32_t code_offset_;
154    uint32_t native_gc_map_offset_;
155
156    friend class OatClass;
157  };
158
159  class OatClass {
160   public:
161    mirror::Class::Status GetStatus() const {
162      return status_;
163    }
164
165    OatClassType GetType() const {
166      return type_;
167    }
168
169    // Get the OatMethod entry based on its index into the class
170    // defintion. direct methods come first, followed by virtual
171    // methods. note that runtime created methods such as miranda
172    // methods are not included.
173    const OatMethod GetOatMethod(uint32_t method_index) const;
174
175    OatClass() {}
176
177   private:
178    OatClass(const OatFile* oat_file,
179             mirror::Class::Status status,
180             OatClassType type,
181             uint32_t bitmap_size,
182             const uint32_t* bitmap_pointer,
183             const OatMethodOffsets* methods_pointer);
184
185    const OatFile* oat_file_;
186
187    mirror::Class::Status status_;
188
189    OatClassType type_;
190
191    const uint32_t* bitmap_;
192
193    const OatMethodOffsets* methods_pointer_;
194
195    friend class OatDexFile;
196  };
197
198  class OatDexFile {
199   public:
200    // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
201    const DexFile* OpenDexFile(std::string* error_msg) const;
202
203    // Returns the size of the DexFile refered to by this OatDexFile.
204    size_t FileSize() const;
205
206    // Returns original path of DexFile that was the source of this OatDexFile.
207    const std::string& GetDexFileLocation() const {
208      return dex_file_location_;
209    }
210
211    // Returns checksum of original DexFile that was the source of this OatDexFile;
212    uint32_t GetDexFileLocationChecksum() const {
213      return dex_file_location_checksum_;
214    }
215
216    // Returns the OatClass for the class specified by the given DexFile class_def_index.
217    OatClass GetOatClass(uint16_t class_def_index) const;
218
219    ~OatDexFile();
220
221   private:
222    OatDexFile(const OatFile* oat_file,
223               const std::string& dex_file_location,
224               uint32_t dex_file_checksum,
225               const byte* dex_file_pointer,
226               const uint32_t* oat_class_offsets_pointer);
227
228    const OatFile* const oat_file_;
229    const std::string dex_file_location_;
230    const uint32_t dex_file_location_checksum_;
231    const byte* const dex_file_pointer_;
232    const uint32_t* const oat_class_offsets_pointer_;
233
234    friend class OatFile;
235    DISALLOW_COPY_AND_ASSIGN(OatDexFile);
236  };
237
238  const OatDexFile* GetOatDexFile(const char* dex_location,
239                                  const uint32_t* const dex_location_checksum,
240                                  bool exception_if_not_found = true) const
241      LOCKS_EXCLUDED(secondary_lookup_lock_);
242
243  std::vector<const OatDexFile*> GetOatDexFiles() const;
244
245  size_t Size() const {
246    return End() - Begin();
247  }
248
249  const byte* Begin() const;
250  const byte* End() const;
251
252 private:
253  static void CheckLocation(const std::string& location);
254
255  static OatFile* OpenDlopen(const std::string& elf_filename,
256                             const std::string& location,
257                             byte* requested_base,
258                             std::string* error_msg);
259
260  static OatFile* OpenElfFile(File* file,
261                              const std::string& location,
262                              byte* requested_base,
263                              bool writable,
264                              bool executable,
265                              std::string* error_msg);
266
267  explicit OatFile(const std::string& filename, bool executable);
268  bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg);
269  bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
270                   std::string* error_msg);
271  bool Setup(std::string* error_msg);
272
273  // The oat file name.
274  //
275  // The image will embed this to link its associated oat file.
276  const std::string location_;
277
278  // Pointer to OatHeader.
279  const byte* begin_;
280
281  // Pointer to end of oat region for bounds checking.
282  const byte* end_;
283
284  // Was this oat_file loaded executable?
285  const bool is_executable_;
286
287  // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
288  std::unique_ptr<MemMap> mem_map_;
289
290  // Backing memory map for oat file during cross compilation.
291  std::unique_ptr<ElfFile> elf_file_;
292
293  // dlopen handle during runtime.
294  void* dlopen_handle_;
295
296  // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
297  // lookup with a const char* key. The StringPiece doesn't own its backing storage,
298  // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
299  // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
300  // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
301  typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
302
303  // Map each plain dex file location retrieved from the oat file to its OatDexFile.
304  // This map doesn't change after it's constructed in Setup() and therefore doesn't
305  // need any locking and provides the cheapest dex file lookup for GetOatDexFile()
306  // for a very frequent use case. Never contains a nullptr value.
307  Table oat_dex_files_;                         // Owns the OatDexFile* values.
308
309  // Lock guarding all members needed for secondary lookup in GetOatDexFile().
310  mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
311
312  // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
313  // the results of all previous secondary lookups, whether successful (non-null) or
314  // failed (null). If it doesn't contain an entry we need to calculate the canonical
315  // location and use oat_dex_files_by_canonical_location_.
316  mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
317
318  // Map the canonical location to an OatDexFile. This lazily constructed map is used
319  // when we're doing the secondary lookup for a given location for the first time.
320  mutable Table oat_dex_files_by_canonical_location_ GUARDED_BY(secondary_lookup_lock_);
321
322  // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
323  // and the lazily initialized oat_dex_files_by_canonical_location_.
324  // NOTE: We're keeping references to contained strings in form of StringPiece and adding
325  // new strings to the end. The adding of a new element must not touch any previously stored
326  // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
327  mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
328
329  friend class OatClass;
330  friend class OatDexFile;
331  friend class OatDumper;  // For GetBase and GetLimit
332  DISALLOW_COPY_AND_ASSIGN(OatFile);
333};
334
335}  // namespace art
336
337#endif  // ART_RUNTIME_OAT_FILE_H_
338