oat_file.h revision 7940e44f4517de5e2634a7e07d58d0fb26160513
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_SRC_OAT_FILE_H_ 18#define ART_SRC_OAT_FILE_H_ 19 20#include <string> 21#include <vector> 22 23#include "dex_file.h" 24#include "invoke_type.h" 25#include "mem_map.h" 26#include "mirror/abstract_method.h" 27#include "oat.h" 28#include "os.h" 29 30namespace art { 31 32class ElfFile; 33class MemMap; 34class OatMethodOffsets; 35class OatHeader; 36 37class OatFile { 38 public: 39 // Returns an .odex file name next adjacent to the dex location. 40 // For example, for "/foo/bar/baz.jar", return "/foo/bar/baz.odex". 41 static std::string DexFilenameToOdexFilename(const std::string& location); 42 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 49 // Open an oat file from an already opened File. 50 // Does not use dlopen underneath so cannot be used for runtime use 51 // where relocations may be required. Currently used from 52 // ImageWriter which wants to open a writable version from an existing 53 // file descriptor for patching. 54 static OatFile* OpenWritable(File* file, const std::string& location); 55 56 // Open an oat file backed by a std::vector with the given location. 57 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents, 58 const std::string& location); 59 60 ~OatFile(); 61 62 const std::string& GetLocation() const { 63 return location_; 64 } 65 66 const OatHeader& GetOatHeader() const; 67 68 class OatDexFile; 69 70 class OatMethod { 71 public: 72 void LinkMethod(mirror::AbstractMethod* method) const; 73 74 uint32_t GetCodeOffset() const { 75 return code_offset_; 76 } 77 size_t GetFrameSizeInBytes() const { 78 return frame_size_in_bytes_; 79 } 80 uint32_t GetCoreSpillMask() const { 81 return core_spill_mask_; 82 } 83 uint32_t GetFpSpillMask() const { 84 return fp_spill_mask_; 85 } 86 uint32_t GetMappingTableOffset() const { 87 return mapping_table_offset_; 88 } 89 uint32_t GetVmapTableOffset() const { 90 return vmap_table_offset_; 91 } 92 uint32_t GetNativeGcMapOffset() const { 93 return native_gc_map_offset_; 94 } 95 96 const void* GetCode() const; 97 uint32_t GetCodeSize() const; 98 99 const uint32_t* GetMappingTable() const { 100 return GetOatPointer<const uint32_t*>(mapping_table_offset_); 101 } 102 const uint16_t* GetVmapTable() const { 103 return GetOatPointer<const uint16_t*>(vmap_table_offset_); 104 } 105 const uint8_t* GetNativeGcMap() const { 106 return GetOatPointer<const uint8_t*>(native_gc_map_offset_); 107 } 108 109 ~OatMethod(); 110 111 // Create an OatMethod with offsets relative to the given base address 112 OatMethod(const byte* base, 113 const uint32_t code_offset, 114 const size_t frame_size_in_bytes, 115 const uint32_t core_spill_mask, 116 const uint32_t fp_spill_mask, 117 const uint32_t mapping_table_offset, 118 const uint32_t vmap_table_offset, 119 const uint32_t gc_map_offset); 120 121 private: 122 template<class T> 123 T GetOatPointer(uint32_t offset) const { 124 if (offset == 0) { 125 return NULL; 126 } 127 return reinterpret_cast<T>(begin_ + offset); 128 } 129 130 const byte* begin_; 131 132 uint32_t code_offset_; 133 size_t frame_size_in_bytes_; 134 uint32_t core_spill_mask_; 135 uint32_t fp_spill_mask_; 136 uint32_t mapping_table_offset_; 137 uint32_t vmap_table_offset_; 138 uint32_t native_gc_map_offset_; 139 140 friend class OatClass; 141 }; 142 143 class OatClass { 144 public: 145 mirror::Class::Status GetStatus() const; 146 147 // get the OatMethod entry based on its index into the class 148 // defintion. direct methods come first, followed by virtual 149 // methods. note that runtime created methods such as miranda 150 // methods are not included. 151 const OatMethod GetOatMethod(uint32_t method_index) const; 152 ~OatClass(); 153 154 private: 155 OatClass(const OatFile* oat_file, 156 mirror::Class::Status status, 157 const OatMethodOffsets* methods_pointer); 158 159 const OatFile* oat_file_; 160 const mirror::Class::Status status_; 161 const OatMethodOffsets* methods_pointer_; 162 163 friend class OatDexFile; 164 }; 165 166 class OatDexFile { 167 public: 168 const DexFile* OpenDexFile() const; 169 const OatClass* GetOatClass(uint32_t class_def_index) const; 170 size_t FileSize() const; 171 172 const std::string& GetDexFileLocation() const { 173 return dex_file_location_; 174 } 175 176 uint32_t GetDexFileLocationChecksum() const { 177 return dex_file_location_checksum_; 178 } 179 180 ~OatDexFile(); 181 182 private: 183 OatDexFile(const OatFile* oat_file, 184 const std::string& dex_file_location, 185 uint32_t dex_file_checksum, 186 const byte* dex_file_pointer, 187 const uint32_t* oat_class_offsets_pointer); 188 189 const OatFile* oat_file_; 190 std::string dex_file_location_; 191 uint32_t dex_file_location_checksum_; 192 const byte* dex_file_pointer_; 193 const uint32_t* oat_class_offsets_pointer_; 194 195 friend class OatFile; 196 DISALLOW_COPY_AND_ASSIGN(OatDexFile); 197 }; 198 199 const OatDexFile* GetOatDexFile(const std::string& dex_file_location, 200 bool exception_if_not_found = true) const 201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 202 std::vector<const OatDexFile*> GetOatDexFiles() const; 203 204 size_t Size() const { 205 return End() - Begin(); 206 } 207 208 private: 209 static void CheckLocation(const std::string& location); 210 211 static OatFile* OpenDlopen(const std::string& elf_filename, 212 const std::string& location, 213 byte* requested_base); 214 215 static OatFile* OpenElfFile(File* file, 216 const std::string& location, 217 byte* requested_base, 218 bool writable); 219 220 explicit OatFile(const std::string& filename); 221 bool Dlopen(const std::string& elf_filename, byte* requested_base); 222 bool ElfFileOpen(File* file, byte* requested_base, bool writable); 223 bool Setup(); 224 225 const byte* Begin() const; 226 const byte* End() const; 227 228 // The oat file name. 229 // 230 // The image will embed this to link its associated oat file. 231 const std::string location_; 232 233 // Pointer to OatHeader. 234 const byte* begin_; 235 236 // Pointer to end of oat region for bounds checking. 237 const byte* end_; 238 239 // Backing memory map for oat file during when opened by ElfWriter during initial compilation. 240 UniquePtr<MemMap> mem_map_; 241 242 // Backing memory map for oat file during cross compilation. 243 UniquePtr<ElfFile> elf_file_; 244 245 // dlopen handle during runtime. 246 void* dlopen_handle_; 247 248 typedef SafeMap<std::string, const OatDexFile*> Table; 249 Table oat_dex_files_; 250 251 friend class OatClass; 252 friend class OatDexFile; 253 friend class OatDumper; // For GetBase and GetLimit 254 DISALLOW_COPY_AND_ASSIGN(OatFile); 255}; 256 257} // namespace art 258 259#endif // ART_SRC_OAT_WRITER_H_ 260