blob: bff48faa7f80660630785ff7f4ff79cd00ecac83 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#ifndef ART_SRC_OAT_FILE_H_
18#define ART_SRC_OAT_FILE_H_
19
Brian Carlstrom700c8d32012-11-05 10:42:02 -080020#include <string>
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include <vector>
22
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "dex_file.h"
24#include "invoke_type.h"
25#include "mem_map.h"
26#include "mirror/abstract_method.h"
27#include "oat.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028#include "os.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070029
30namespace art {
31
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032class ElfFile;
33class MemMap;
34class OatMethodOffsets;
35struct OatHeader;
36
Brian Carlstrome24fa612011-09-29 00:53:55 -070037class OatFile {
38 public:
Brian Carlstromb7bbba42011-10-13 14:58:47 -070039 // Returns an OatFile name based on a DexFile location
jeffhao262bf462011-10-20 18:36:32 -070040 static std::string DexFilenameToOatFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070041
Brian Carlstrome24fa612011-09-29 00:53:55 -070042 // Open an oat file. Returns NULL on failure. Requested base can
43 // optionally be used to request where the file should be loaded.
44 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080045 const std::string& location,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046 byte* requested_base);
Brian Carlstrome24fa612011-09-29 00:53:55 -070047
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048 // Open an oat file from an already opened File.
49 static OatFile* Open(File* file,
Brian Carlstrom5b332c82012-02-01 15:02:31 -080050 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070051 byte* requested_base,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052 bool writable);
53
54 // Open an oat file backed by a std::vector with the given location.
55 static OatFile* Open(std::vector<uint8_t>& oat_contents,
56 const std::string& location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080057
Brian Carlstrome24fa612011-09-29 00:53:55 -070058 ~OatFile();
59
60 const std::string& GetLocation() const {
61 return location_;
62 }
63
64 const OatHeader& GetOatHeader() const;
65
66 class OatDexFile;
67
Brian Carlstrom3320cf42011-10-04 14:58:28 -070068 class OatMethod {
69 public:
Brian Carlstromae826982011-11-09 01:33:42 -080070 // Link Method for execution using the contents of this OatMethod
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 void LinkMethodPointers(mirror::AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080072
73 // Link Method for image writing using the contents of this OatMethod
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 void LinkMethodOffsets(mirror::AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080075
76 uint32_t GetCodeOffset() const {
77 return code_offset_;
78 }
79 size_t GetFrameSizeInBytes() const {
80 return frame_size_in_bytes_;
81 }
82 uint32_t GetCoreSpillMask() const {
83 return core_spill_mask_;
84 }
85 uint32_t GetFpSpillMask() const {
86 return fp_spill_mask_;
87 }
88 uint32_t GetMappingTableOffset() const {
89 return mapping_table_offset_;
90 }
91 uint32_t GetVmapTableOffset() const {
92 return vmap_table_offset_;
93 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070094 uint32_t GetNativeGcMapOffset() const {
95 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080096 }
Brian Carlstromae826982011-11-09 01:33:42 -080097 uint32_t GetInvokeStubOffset() const {
98 return invoke_stub_offset_;
99 }
100
Logan Chien0c717dd2012-03-28 18:31:07 +0800101 const void* GetCode() const;
102 uint32_t GetCodeSize() const;
103
Brian Carlstromae826982011-11-09 01:33:42 -0800104 const uint32_t* GetMappingTable() const {
105 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
106 }
107 const uint16_t* GetVmapTable() const {
108 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
109 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700110 const uint8_t* GetNativeGcMap() const {
111 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800112 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800113
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 mirror::AbstractMethod::InvokeStub* GetInvokeStub() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800115 uint32_t GetInvokeStubSize() const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700116
TDYa127eead4ac2012-06-03 07:15:25 -0700117#if defined(ART_USE_LLVM_COMPILER)
118 const void* GetProxyStub() const;
119#endif
120
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700121 ~OatMethod();
122
Brian Carlstromae826982011-11-09 01:33:42 -0800123 // Create an OatMethod with offsets relative to the given base address
124 OatMethod(const byte* base,
125 const uint32_t code_offset,
126 const size_t frame_size_in_bytes,
127 const uint32_t core_spill_mask,
128 const uint32_t fp_spill_mask,
129 const uint32_t mapping_table_offset,
130 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800131 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800132 const uint32_t invoke_stub_offset
133#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800134 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800135#endif
136 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700137
Brian Carlstromae826982011-11-09 01:33:42 -0800138 private:
139 template<class T>
140 T GetOatPointer(uint32_t offset) const {
141 if (offset == 0) {
142 return NULL;
143 }
Ian Rogers30fab402012-01-23 15:43:46 -0800144 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800145 }
146
Ian Rogers30fab402012-01-23 15:43:46 -0800147 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800148
149 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700150 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700151 uint32_t core_spill_mask_;
152 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800153 uint32_t mapping_table_offset_;
154 uint32_t vmap_table_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700155 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800156 uint32_t invoke_stub_offset_;
157
Logan Chien0c717dd2012-03-28 18:31:07 +0800158#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800159 uint32_t proxy_stub_offset_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800160#endif
161
Brian Carlstromae826982011-11-09 01:33:42 -0800162 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700163 };
164
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165 class OatClass {
166 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 mirror::Class::Status GetStatus() const;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800168
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700169 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700170 // defintion. direct methods come first, followed by virtual
171 // methods. note that runtime created methods such as miranda
172 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174 ~OatClass();
175
176 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800177 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800179 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700180
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181 const OatFile* oat_file_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 const mirror::Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700183 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184
185 friend class OatDexFile;
186 };
187
188 class OatDexFile {
189 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800190 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191 const OatClass* GetOatClass(uint32_t class_def_index) const;
Ian Rogers05f28c62012-10-23 18:12:13 -0700192 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700193
194 const std::string& GetDexFileLocation() const {
195 return dex_file_location_;
196 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700197
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800198 uint32_t GetDexFileLocationChecksum() const {
199 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700200 }
201
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700203
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204 private:
205 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800206 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800208 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800209 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700210
211 const OatFile* oat_file_;
212 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800213 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800214 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800215 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216
217 friend class OatFile;
218 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
219 };
220
Ian Rogers7fe2c692011-12-06 16:35:59 -0800221 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
222 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224
Ian Rogers30fab402012-01-23 15:43:46 -0800225 size_t Size() const {
226 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227 }
228
229 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800230 static void CheckLocation(const std::string& location);
231
232 static OatFile* OpenDlopen(const std::string& elf_filename,
233 const std::string& location,
234 byte* requested_base);
235
236 static OatFile* OpenElfFile(File* file,
237 const std::string& location,
238 byte* requested_base,
239 bool writable);
240
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700241 explicit OatFile(const std::string& filename);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800242 bool Dlopen(const std::string& elf_filename, byte* requested_base);
243 bool ElfFileOpen(File* file, byte* requested_base, bool writable);
244 void Setup();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245
Ian Rogers30fab402012-01-23 15:43:46 -0800246 const byte* Begin() const;
247 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248
249 // The oat file name.
250 //
251 // The image will embed this to link its associated oat file.
252 const std::string location_;
253
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800254 // Pointer to OatHeader.
255 const byte* begin_;
256
257 // Pointer to end of oat region for bounds checking.
258 const byte* end_;
259
260 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261 UniquePtr<MemMap> mem_map_;
262
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800263 // Backing memory map for oat file during cross compilation.
264 UniquePtr<ElfFile> elf_file_;
265
266 // dlopen handle during runtime.
267 void* dlopen_handle_;
268
Elliott Hughesa0e18062012-04-13 15:59:59 -0700269 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270 Table oat_dex_files_;
271
272 friend class OatClass;
273 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800274 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275 DISALLOW_COPY_AND_ASSIGN(OatFile);
276};
277
278} // namespace art
279
280#endif // ART_SRC_OAT_WRITER_H_