2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_RUNTIME_OAT_FILE_H_
18 #define ART_RUNTIME_OAT_FILE_H_
24 #include "base/mutex.h"
25 #include "base/stringpiece.h"
27 #include "invoke_type.h"
29 #include "mirror/class.h"
39 class OatMethodOffsets;
46 } // namespace collector
51 typedef art::OatDexFile OatDexFile;
53 // Opens an oat file contained within the given elf file. This is always opened as
54 // non-executable at the moment.
55 static OatFile* OpenWithElfFile(ElfFile* elf_file, const std::string& location,
56 const char* abs_dex_location,
57 std::string* error_msg);
58 // Open an oat file. Returns null on failure. Requested base can
59 // optionally be used to request where the file should be loaded.
60 // See the ResolveRelativeEncodedDexLocation for a description of how the
61 // abs_dex_location argument is used.
62 static OatFile* Open(const std::string& filename,
63 const std::string& location,
64 uint8_t* requested_base,
65 uint8_t* oat_file_begin,
68 const char* abs_dex_location,
69 std::string* error_msg);
71 // Open an oat file from an already opened File.
72 // Does not use dlopen underneath so cannot be used for runtime use
73 // where relocations may be required. Currently used from
74 // ImageWriter which wants to open a writable version from an existing
75 // file descriptor for patching.
76 static OatFile* OpenWritable(File* file, const std::string& location,
77 const char* abs_dex_location,
78 std::string* error_msg);
79 // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
80 static OatFile* OpenReadable(File* file, const std::string& location,
81 const char* abs_dex_location,
82 std::string* error_msg);
86 bool IsExecutable() const {
87 return is_executable_;
90 bool HasPatchInfo() const;
94 // Indicates whether the oat file was compiled with full debugging capability.
95 bool IsDebuggable() const;
97 CompilerFilter::Filter GetCompilerFilter() const;
99 const std::string& GetLocation() const {
103 const OatHeader& GetOatHeader() const;
105 class OatMethod FINAL {
107 void LinkMethod(ArtMethod* method) const;
109 uint32_t GetCodeOffset() const;
111 const void* GetQuickCode() const;
113 // Returns size of quick code.
114 uint32_t GetQuickCodeSize() const;
115 uint32_t GetQuickCodeSizeOffset() const;
117 // Returns OatQuickMethodHeader for debugging. Most callers should
118 // use more specific methods such as GetQuickCodeSize.
119 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
120 uint32_t GetOatQuickMethodHeaderOffset() const;
122 size_t GetFrameSizeInBytes() const;
123 uint32_t GetCoreSpillMask() const;
124 uint32_t GetFpSpillMask() const;
126 const uint8_t* GetVmapTable() const;
127 uint32_t GetVmapTableOffset() const;
128 uint32_t GetVmapTableOffsetOffset() const;
130 // Create an OatMethod with offsets relative to the given base address
131 OatMethod(const uint8_t* base, const uint32_t code_offset)
132 : begin_(base), code_offset_(code_offset) {
134 OatMethod(const OatMethod&) = default;
137 OatMethod& operator=(const OatMethod&) = default;
139 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
140 // See ClassLinker::FindOatMethodFor.
141 static const OatMethod Invalid() {
142 return OatMethod(nullptr, -1);
147 T GetOatPointer(uint32_t offset) const {
151 return reinterpret_cast<T>(begin_ + offset);
154 const uint8_t* begin_;
155 uint32_t code_offset_;
157 friend class OatClass;
160 class OatClass FINAL {
162 mirror::Class::Status GetStatus() const {
166 OatClassType GetType() const {
170 // Get the OatMethod entry based on its index into the class
171 // defintion. Direct methods come first, followed by virtual
172 // methods. Note that runtime created methods such as miranda
173 // methods are not included.
174 const OatMethod GetOatMethod(uint32_t method_index) const;
176 // Return a pointer to the OatMethodOffsets for the requested
177 // method_index, or null if none is present. Note that most
178 // callers should use GetOatMethod.
179 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
181 // Return the offset from the start of the OatFile to the
182 // OatMethodOffsets for the requested method_index, or 0 if none
183 // is present. Note that most callers should use GetOatMethod.
184 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
186 // A representation of an invalid OatClass, used when an OatClass can't be found.
187 // See ClassLinker::FindOatClass.
188 static OatClass Invalid() {
189 return OatClass(nullptr, mirror::Class::kStatusError, kOatClassNoneCompiled, 0, nullptr,
194 OatClass(const OatFile* oat_file,
195 mirror::Class::Status status,
197 uint32_t bitmap_size,
198 const uint32_t* bitmap_pointer,
199 const OatMethodOffsets* methods_pointer);
201 const OatFile* const oat_file_;
203 const mirror::Class::Status status_;
205 const OatClassType type_;
207 const uint32_t* const bitmap_;
209 const OatMethodOffsets* const methods_pointer_;
211 friend class art::OatDexFile;
213 const OatDexFile* GetOatDexFile(const char* dex_location,
214 const uint32_t* const dex_location_checksum,
215 bool exception_if_not_found = true) const
216 REQUIRES(!secondary_lookup_lock_);
218 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
219 return oat_dex_files_storage_;
222 size_t Size() const {
223 return End() - Begin();
226 bool Contains(const void* p) const {
227 return p >= Begin() && p < End();
230 size_t BssSize() const {
231 return BssEnd() - BssBegin();
234 const uint8_t* Begin() const;
235 const uint8_t* End() const;
237 const uint8_t* BssBegin() const;
238 const uint8_t* BssEnd() const;
240 // Returns the absolute dex location for the encoded relative dex location.
242 // If not null, abs_dex_location is used to resolve the absolute dex
243 // location of relative dex locations encoded in the oat file.
244 // For example, given absolute location "/data/app/foo/base.apk", encoded
245 // dex locations "base.apk", "base.apk:classes2.dex", etc. would be resolved
246 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk:classes2.dex", etc.
247 // Relative encoded dex locations that don't match the given abs_dex_location
248 // are left unchanged.
249 static std::string ResolveRelativeEncodedDexLocation(
250 const char* abs_dex_location, const std::string& rel_dex_location);
252 // Create a dependency list (dex locations and checksums) for the given dex files.
253 static std::string EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files);
255 // Check the given dependency list against their dex files - thus the name "Static," this does
256 // not check the class-loader environment, only whether there have been file updates.
257 static bool CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg);
259 // Get the dex locations of a dependency list. Note: this is *not* cleaned for synthetic
260 // locations of multidex files.
261 static bool GetDexLocationsFromDependencies(const char* dex_dependencies,
262 std::vector<std::string>* locations);
265 OatFile(const std::string& filename, bool executable);
268 // The oat file name.
270 // The image will embed this to link its associated oat file.
271 const std::string location_;
273 // Pointer to OatHeader.
274 const uint8_t* begin_;
276 // Pointer to end of oat region for bounds checking.
279 // Pointer to the .bss section, if present, otherwise null.
282 // Pointer to the end of the .bss section, if present, otherwise null.
285 // Was this oat_file loaded executable?
286 const bool is_executable_;
288 // Owning storage for the OatDexFile objects.
289 std::vector<const OatDexFile*> oat_dex_files_storage_;
291 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
292 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
293 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
294 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
295 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
296 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
298 // Map each location and canonical location (if different) retrieved from the
299 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
300 // and therefore doesn't need any locking and provides the cheapest dex file lookup
301 // for GetOatDexFile() for a very frequent use case. Never contains a null value.
302 Table oat_dex_files_;
304 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
305 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
307 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
308 // the results of all previous secondary lookups, whether successful (non-null) or
309 // failed (null). If it doesn't contain an entry we need to calculate the canonical
310 // location and use oat_dex_files_by_canonical_location_.
311 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
313 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
314 // and the lazily initialized oat_dex_files_by_canonical_location_.
315 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
316 // new strings to the end. The adding of a new element must not touch any previously stored
317 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
318 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
320 friend class gc::collector::DummyOatFile; // For modifying begin_ and end_.
321 friend class OatClass;
322 friend class art::OatDexFile;
323 friend class OatDumper; // For GetBase and GetLimit
324 friend class OatFileBase;
325 DISALLOW_COPY_AND_ASSIGN(OatFile);
328 // OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
329 // support forward declarations of inner classes, and we want to
330 // forward-declare OatDexFile so that we can store an opaque pointer to an
331 // OatDexFile in DexFile.
332 class OatDexFile FINAL {
334 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
335 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
337 const OatFile* GetOatFile() const {
341 // Returns the size of the DexFile refered to by this OatDexFile.
342 size_t FileSize() const;
344 // Returns original path of DexFile that was the source of this OatDexFile.
345 const std::string& GetDexFileLocation() const {
346 return dex_file_location_;
349 // Returns the canonical location of DexFile that was the source of this OatDexFile.
350 const std::string& GetCanonicalDexFileLocation() const {
351 return canonical_dex_file_location_;
354 // Returns checksum of original DexFile that was the source of this OatDexFile;
355 uint32_t GetDexFileLocationChecksum() const {
356 return dex_file_location_checksum_;
359 // Returns the OatClass for the class specified by the given DexFile class_def_index.
360 OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
362 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
363 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
365 uint8_t* GetDexCacheArrays() const {
366 return dex_cache_arrays_;
369 const uint8_t* GetLookupTableData() const {
370 return lookup_table_data_;
376 OatDexFile(const OatFile* oat_file,
377 const std::string& dex_file_location,
378 const std::string& canonical_dex_file_location,
379 uint32_t dex_file_checksum,
380 const uint8_t* dex_file_pointer,
381 const uint8_t* lookup_table_data,
382 const uint32_t* oat_class_offsets_pointer,
383 uint8_t* dex_cache_arrays);
385 const OatFile* const oat_file_;
386 const std::string dex_file_location_;
387 const std::string canonical_dex_file_location_;
388 const uint32_t dex_file_location_checksum_;
389 const uint8_t* const dex_file_pointer_;
390 const uint8_t* lookup_table_data_;
391 const uint32_t* const oat_class_offsets_pointer_;
392 uint8_t* const dex_cache_arrays_;
394 friend class OatFile;
395 friend class OatFileBase;
396 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
401 #endif // ART_RUNTIME_OAT_FILE_H_