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