blob: 1481ce5f1caa7e9b20fa04fdb87a1d6208c45ac4 [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
20#include <vector>
21
22#include "dex_file.h"
Logan Chien0c717dd2012-03-28 18:31:07 +080023#include "invoke_type.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "mem_map.h"
25#include "oat.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070026#include "object.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070027
28namespace art {
29
30class OatFile {
31 public:
Logan Chien0c717dd2012-03-28 18:31:07 +080032 enum RelocationBehavior {
33 kRelocNone,
34 kRelocAll,
35 };
Brian Carlstrome24fa612011-09-29 00:53:55 -070036
Brian Carlstromb7bbba42011-10-13 14:58:47 -070037 // Returns an OatFile name based on a DexFile location
jeffhao262bf462011-10-20 18:36:32 -070038 static std::string DexFilenameToOatFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070039
Brian Carlstrome24fa612011-09-29 00:53:55 -070040 // Open an oat file. Returns NULL on failure. Requested base can
41 // optionally be used to request where the file should be loaded.
42 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080043 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070044 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080045 RelocationBehavior reloc,
Elliott Hughesb25c3f62012-03-26 16:35:06 -070046 bool writable = false);
Brian Carlstrome24fa612011-09-29 00:53:55 -070047
Brian Carlstrom5b332c82012-02-01 15:02:31 -080048 // Open an oat file from an already opened File with the given location.
49 static OatFile* Open(File& file,
50 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070051 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080052 RelocationBehavior reloc,
Elliott Hughesb25c3f62012-03-26 16:35:06 -070053 bool writable = false);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080054
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 ~OatFile();
56
57 const std::string& GetLocation() const {
58 return location_;
59 }
60
61 const OatHeader& GetOatHeader() const;
62
63 class OatDexFile;
64
Brian Carlstrom3320cf42011-10-04 14:58:28 -070065 class OatMethod {
66 public:
Brian Carlstromae826982011-11-09 01:33:42 -080067 // Link Method for execution using the contents of this OatMethod
Mathieu Chartier66f19252012-09-18 08:57:04 -070068 void LinkMethodPointers(AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080069
70 // Link Method for image writing using the contents of this OatMethod
Mathieu Chartier66f19252012-09-18 08:57:04 -070071 void LinkMethodOffsets(AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080072
73 uint32_t GetCodeOffset() const {
74 return code_offset_;
75 }
76 size_t GetFrameSizeInBytes() const {
77 return frame_size_in_bytes_;
78 }
79 uint32_t GetCoreSpillMask() const {
80 return core_spill_mask_;
81 }
82 uint32_t GetFpSpillMask() const {
83 return fp_spill_mask_;
84 }
85 uint32_t GetMappingTableOffset() const {
86 return mapping_table_offset_;
87 }
88 uint32_t GetVmapTableOffset() const {
89 return vmap_table_offset_;
90 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070091 uint32_t GetNativeGcMapOffset() const {
92 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080093 }
Brian Carlstromae826982011-11-09 01:33:42 -080094 uint32_t GetInvokeStubOffset() const {
95 return invoke_stub_offset_;
96 }
97
Logan Chien0c717dd2012-03-28 18:31:07 +080098 const void* GetCode() const;
99 uint32_t GetCodeSize() const;
100
Brian Carlstromae826982011-11-09 01:33:42 -0800101 const uint32_t* GetMappingTable() const {
102 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
103 }
104 const uint16_t* GetVmapTable() const {
105 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
106 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700107 const uint8_t* GetNativeGcMap() const {
108 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800109 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800110
Mathieu Chartier66f19252012-09-18 08:57:04 -0700111 AbstractMethod::InvokeStub* GetInvokeStub() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800112 uint32_t GetInvokeStubSize() const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113
TDYa127eead4ac2012-06-03 07:15:25 -0700114#if defined(ART_USE_LLVM_COMPILER)
115 const void* GetProxyStub() const;
116#endif
117
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700118 ~OatMethod();
119
Brian Carlstromae826982011-11-09 01:33:42 -0800120 // Create an OatMethod with offsets relative to the given base address
121 OatMethod(const byte* base,
122 const uint32_t code_offset,
123 const size_t frame_size_in_bytes,
124 const uint32_t core_spill_mask,
125 const uint32_t fp_spill_mask,
126 const uint32_t mapping_table_offset,
127 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800128 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800129 const uint32_t invoke_stub_offset
130#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800131 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800132#endif
133 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700134
Brian Carlstromae826982011-11-09 01:33:42 -0800135 private:
136 template<class T>
137 T GetOatPointer(uint32_t offset) const {
138 if (offset == 0) {
139 return NULL;
140 }
Ian Rogers30fab402012-01-23 15:43:46 -0800141 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800142 }
143
Ian Rogers30fab402012-01-23 15:43:46 -0800144 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800145
146 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700147 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148 uint32_t core_spill_mask_;
149 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800150 uint32_t mapping_table_offset_;
151 uint32_t vmap_table_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700152 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800153 uint32_t invoke_stub_offset_;
154
Logan Chien0c717dd2012-03-28 18:31:07 +0800155#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800156 uint32_t proxy_stub_offset_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800157#endif
158
Brian Carlstromae826982011-11-09 01:33:42 -0800159 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700160 };
161
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162 class OatClass {
163 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800164 Class::Status GetStatus() const;
165
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700166 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 // defintion. direct methods come first, followed by virtual
168 // methods. note that runtime created methods such as miranda
169 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700170 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700171 ~OatClass();
172
173 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800174 OatClass(const OatFile* oat_file,
175 Class::Status status,
176 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700177
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800179 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700180 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181
182 friend class OatDexFile;
183 };
184
185 class OatDexFile {
186 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800187 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700188 const OatClass* GetOatClass(uint32_t class_def_index) const;
Ian Rogers05f28c62012-10-23 18:12:13 -0700189 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700190
191 const std::string& GetDexFileLocation() const {
192 return dex_file_location_;
193 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700194
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800195 uint32_t GetDexFileLocationChecksum() const {
196 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700197 }
198
Brian Carlstrome24fa612011-09-29 00:53:55 -0700199 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700200
Brian Carlstrome24fa612011-09-29 00:53:55 -0700201 private:
202 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800203 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800205 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800206 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207
208 const OatFile* oat_file_;
209 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800210 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800211 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800212 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700213
214 friend class OatFile;
215 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
216 };
217
Ian Rogers7fe2c692011-12-06 16:35:59 -0800218 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
219 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221
Ian Rogers30fab402012-01-23 15:43:46 -0800222 size_t Size() const {
223 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224 }
225
Logan Chien0c717dd2012-03-28 18:31:07 +0800226 void RelocateExecutable();
227
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700229 explicit OatFile(const std::string& filename);
Logan Chien0c717dd2012-03-28 18:31:07 +0800230 bool Map(File& file, byte* requested_base, RelocationBehavior reloc, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700231
Ian Rogers30fab402012-01-23 15:43:46 -0800232 const byte* Begin() const;
233 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234
235 // The oat file name.
236 //
237 // The image will embed this to link its associated oat file.
238 const std::string location_;
239
240 // backing memory map for oat file
241 UniquePtr<MemMap> mem_map_;
242
Elliott Hughesa0e18062012-04-13 15:59:59 -0700243 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700244 Table oat_dex_files_;
245
246 friend class OatClass;
247 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800248 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249 DISALLOW_COPY_AND_ASSIGN(OatFile);
250};
251
252} // namespace art
253
254#endif // ART_SRC_OAT_WRITER_H_