oat_file.h revision a62d2f04a6ecf804f8a78e722a6ca8ccb2dfa931
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#include "utils.h" 33 34namespace art { 35 36class BitVector; 37class ElfFile; 38class MemMap; 39class OatMethodOffsets; 40class OatHeader; 41class OatDexFile; 42 43namespace gc { 44namespace collector { 45class DummyOatFile; 46} // namespace collector 47} // namespace gc 48 49class OatFile { 50 public: 51 typedef art::OatDexFile OatDexFile; 52 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, 66 bool executable, 67 bool low_4gb, 68 const char* abs_dex_location, 69 std::string* error_msg); 70 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); 83 84 virtual ~OatFile(); 85 86 bool IsExecutable() const { 87 return is_executable_; 88 } 89 90 bool IsPic() const; 91 92 // Indicates whether the oat file was compiled with full debugging capability. 93 bool IsDebuggable() const; 94 95 CompilerFilter::Filter GetCompilerFilter() const; 96 97 const std::string& GetLocation() const { 98 return location_; 99 } 100 101 const OatHeader& GetOatHeader() const; 102 103 class OatMethod FINAL { 104 public: 105 void LinkMethod(ArtMethod* method) const; 106 107 uint32_t GetCodeOffset() const; 108 109 const void* GetQuickCode() const; 110 111 // Returns size of quick code. 112 uint32_t GetQuickCodeSize() const; 113 uint32_t GetQuickCodeSizeOffset() const; 114 115 // Returns OatQuickMethodHeader for debugging. Most callers should 116 // use more specific methods such as GetQuickCodeSize. 117 const OatQuickMethodHeader* GetOatQuickMethodHeader() const; 118 uint32_t GetOatQuickMethodHeaderOffset() const; 119 120 size_t GetFrameSizeInBytes() const; 121 uint32_t GetCoreSpillMask() const; 122 uint32_t GetFpSpillMask() const; 123 124 const uint8_t* GetMappingTable() const; 125 uint32_t GetMappingTableOffset() const; 126 uint32_t GetMappingTableOffsetOffset() const; 127 128 const uint8_t* GetVmapTable() const; 129 uint32_t GetVmapTableOffset() const; 130 uint32_t GetVmapTableOffsetOffset() const; 131 132 const uint8_t* GetGcMap() const; 133 uint32_t GetGcMapOffset() const; 134 uint32_t GetGcMapOffsetOffset() const; 135 136 // Create an OatMethod with offsets relative to the given base address 137 OatMethod(const uint8_t* base, const uint32_t code_offset) 138 : begin_(base), code_offset_(code_offset) { 139 } 140 OatMethod(const OatMethod&) = default; 141 ~OatMethod() {} 142 143 OatMethod& operator=(const OatMethod&) = default; 144 145 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found. 146 // See ClassLinker::FindOatMethodFor. 147 static const OatMethod Invalid() { 148 return OatMethod(nullptr, -1); 149 } 150 151 private: 152 template<class T> 153 T GetOatPointer(uint32_t offset) const { 154 if (offset == 0) { 155 return nullptr; 156 } 157 return reinterpret_cast<T>(begin_ + offset); 158 } 159 160 const uint8_t* begin_; 161 uint32_t code_offset_; 162 163 friend class OatClass; 164 }; 165 166 class OatClass FINAL { 167 public: 168 mirror::Class::Status GetStatus() const { 169 return status_; 170 } 171 172 OatClassType GetType() const { 173 return type_; 174 } 175 176 // Get the OatMethod entry based on its index into the class 177 // defintion. Direct methods come first, followed by virtual 178 // methods. Note that runtime created methods such as miranda 179 // methods are not included. 180 const OatMethod GetOatMethod(uint32_t method_index) const; 181 182 // Return a pointer to the OatMethodOffsets for the requested 183 // method_index, or null if none is present. Note that most 184 // callers should use GetOatMethod. 185 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const; 186 187 // Return the offset from the start of the OatFile to the 188 // OatMethodOffsets for the requested method_index, or 0 if none 189 // is present. Note that most callers should use GetOatMethod. 190 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const; 191 192 // A representation of an invalid OatClass, used when an OatClass can't be found. 193 // See ClassLinker::FindOatClass. 194 static OatClass Invalid() { 195 return OatClass(nullptr, mirror::Class::kStatusError, kOatClassNoneCompiled, 0, nullptr, 196 nullptr); 197 } 198 199 private: 200 OatClass(const OatFile* oat_file, 201 mirror::Class::Status status, 202 OatClassType type, 203 uint32_t bitmap_size, 204 const uint32_t* bitmap_pointer, 205 const OatMethodOffsets* methods_pointer); 206 207 const OatFile* const oat_file_; 208 209 const mirror::Class::Status status_; 210 211 const OatClassType type_; 212 213 const uint32_t* const bitmap_; 214 215 const OatMethodOffsets* const methods_pointer_; 216 217 friend class art::OatDexFile; 218 }; 219 const OatDexFile* GetOatDexFile(const char* dex_location, 220 const uint32_t* const dex_location_checksum, 221 bool exception_if_not_found = true) const 222 REQUIRES(!secondary_lookup_lock_); 223 224 const std::vector<const OatDexFile*>& GetOatDexFiles() const { 225 return oat_dex_files_storage_; 226 } 227 228 size_t Size() const { 229 return End() - Begin(); 230 } 231 232 bool Contains(const void* p) const { 233 return p >= Begin() && p < End(); 234 } 235 236 size_t BssSize() const { 237 return BssEnd() - BssBegin(); 238 } 239 240 const uint8_t* Begin() const; 241 const uint8_t* End() const; 242 243 const uint8_t* BssBegin() const; 244 const uint8_t* BssEnd() const; 245 246 // Returns the absolute dex location for the encoded relative dex location. 247 // 248 // If not null, abs_dex_location is used to resolve the absolute dex 249 // location of relative dex locations encoded in the oat file. 250 // For example, given absolute location "/data/app/foo/base.apk", encoded 251 // dex locations "base.apk", "base.apk:classes2.dex", etc. would be resolved 252 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk:classes2.dex", etc. 253 // Relative encoded dex locations that don't match the given abs_dex_location 254 // are left unchanged. 255 static std::string ResolveRelativeEncodedDexLocation( 256 const char* abs_dex_location, const std::string& rel_dex_location); 257 258 // Create a dependency list (dex locations and checksums) for the given dex files. 259 static std::string EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files); 260 261 // Check the given dependency list against their dex files - thus the name "Static," this does 262 // not check the class-loader environment, only whether there have been file updates. 263 static bool CheckStaticDexFileDependencies(const char* dex_dependencies, std::string* msg); 264 265 // Get the dex locations of a dependency list. Note: this is *not* cleaned for synthetic 266 // locations of multidex files. 267 static bool GetDexLocationsFromDependencies(const char* dex_dependencies, 268 std::vector<std::string>* locations); 269 270 protected: 271 OatFile(const std::string& filename, bool executable); 272 273 private: 274 // The oat file name. 275 // 276 // The image will embed this to link its associated oat file. 277 const std::string location_; 278 279 // Pointer to OatHeader. 280 const uint8_t* begin_; 281 282 // Pointer to end of oat region for bounds checking. 283 const uint8_t* end_; 284 285 // Pointer to the .bss section, if present, otherwise null. 286 uint8_t* bss_begin_; 287 288 // Pointer to the end of the .bss section, if present, otherwise null. 289 uint8_t* bss_end_; 290 291 // Was this oat_file loaded executable? 292 const bool is_executable_; 293 294 // Owning storage for the OatDexFile objects. 295 std::vector<const OatDexFile*> oat_dex_files_storage_; 296 297 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every 298 // lookup with a const char* key. The StringPiece doesn't own its backing storage, 299 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage 300 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage 301 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_. 302 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table; 303 304 // Map each location and canonical location (if different) retrieved from the 305 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup() 306 // and therefore doesn't need any locking and provides the cheapest dex file lookup 307 // for GetOatDexFile() for a very frequent use case. Never contains a null value. 308 Table oat_dex_files_; 309 310 // Lock guarding all members needed for secondary lookup in GetOatDexFile(). 311 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 312 313 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores 314 // the results of all previous secondary lookups, whether successful (non-null) or 315 // failed (null). If it doesn't contain an entry we need to calculate the canonical 316 // location and use oat_dex_files_by_canonical_location_. 317 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_); 318 319 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_ 320 // and the lazily initialized oat_dex_files_by_canonical_location_. 321 // NOTE: We're keeping references to contained strings in form of StringPiece and adding 322 // new strings to the end. The adding of a new element must not touch any previously stored 323 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't. 324 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_); 325 326 friend class gc::collector::DummyOatFile; // For modifying begin_ and end_. 327 friend class OatClass; 328 friend class art::OatDexFile; 329 friend class OatDumper; // For GetBase and GetLimit 330 friend class OatFileBase; 331 DISALLOW_COPY_AND_ASSIGN(OatFile); 332}; 333 334// OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't 335// support forward declarations of inner classes, and we want to 336// forward-declare OatDexFile so that we can store an opaque pointer to an 337// OatDexFile in DexFile. 338class OatDexFile FINAL { 339 public: 340 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile. 341 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const; 342 343 const OatFile* GetOatFile() const { 344 return oat_file_; 345 } 346 347 // Returns the size of the DexFile refered to by this OatDexFile. 348 size_t FileSize() const; 349 350 // Returns original path of DexFile that was the source of this OatDexFile. 351 const std::string& GetDexFileLocation() const { 352 return dex_file_location_; 353 } 354 355 // Returns the canonical location of DexFile that was the source of this OatDexFile. 356 const std::string& GetCanonicalDexFileLocation() const { 357 return canonical_dex_file_location_; 358 } 359 360 // Returns checksum of original DexFile that was the source of this OatDexFile; 361 uint32_t GetDexFileLocationChecksum() const { 362 return dex_file_location_checksum_; 363 } 364 365 // Returns the OatClass for the class specified by the given DexFile class_def_index. 366 OatFile::OatClass GetOatClass(uint16_t class_def_index) const; 367 368 // Returns the offset to the OatClass information. Most callers should use GetOatClass. 369 uint32_t GetOatClassOffset(uint16_t class_def_index) const; 370 371 uint8_t* GetDexCacheArrays() const { 372 return dex_cache_arrays_; 373 } 374 375 const uint8_t* GetLookupTableData() const { 376 return lookup_table_data_; 377 } 378 379 ~OatDexFile(); 380 381 private: 382 OatDexFile(const OatFile* oat_file, 383 const std::string& dex_file_location, 384 const std::string& canonical_dex_file_location, 385 uint32_t dex_file_checksum, 386 const uint8_t* dex_file_pointer, 387 const uint8_t* lookup_table_data, 388 const uint32_t* oat_class_offsets_pointer, 389 uint8_t* dex_cache_arrays); 390 391 const OatFile* const oat_file_; 392 const std::string dex_file_location_; 393 const std::string canonical_dex_file_location_; 394 const uint32_t dex_file_location_checksum_; 395 const uint8_t* const dex_file_pointer_; 396 const uint8_t* lookup_table_data_; 397 const uint32_t* const oat_class_offsets_pointer_; 398 uint8_t* const dex_cache_arrays_; 399 400 friend class OatFile; 401 friend class OatFileBase; 402 DISALLOW_COPY_AND_ASSIGN(OatDexFile); 403}; 404 405} // namespace art 406 407#endif // ART_RUNTIME_OAT_FILE_H_ 408