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