blob: 1ad6e5d4c9ad94a78c98b6bd99f85de7bd2a6ffc [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
Logan Chien0c717dd2012-03-28 18:31:07 +080028#if defined(ART_USE_LLVM_COMPILER)
29namespace art {
30 namespace compiler_llvm {
31 class ElfLoader;
32 }
33}
34#endif
35
Brian Carlstrome24fa612011-09-29 00:53:55 -070036namespace art {
37
38class OatFile {
39 public:
Logan Chien0c717dd2012-03-28 18:31:07 +080040 enum RelocationBehavior {
41 kRelocNone,
42 kRelocAll,
43 };
Brian Carlstrome24fa612011-09-29 00:53:55 -070044
Brian Carlstromb7bbba42011-10-13 14:58:47 -070045 // Returns an OatFile name based on a DexFile location
jeffhao262bf462011-10-20 18:36:32 -070046 static std::string DexFilenameToOatFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070047
Brian Carlstrome24fa612011-09-29 00:53:55 -070048 // Open an oat file. Returns NULL on failure. Requested base can
49 // optionally be used to request where the file should be loaded.
50 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080051 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070052 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080053 RelocationBehavior reloc,
Elliott Hughesb25c3f62012-03-26 16:35:06 -070054 bool writable = false);
Brian Carlstrome24fa612011-09-29 00:53:55 -070055
Brian Carlstrom5b332c82012-02-01 15:02:31 -080056 // Open an oat file from an already opened File with the given location.
57 static OatFile* Open(File& file,
58 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070059 byte* requested_base,
Logan Chien0c717dd2012-03-28 18:31:07 +080060 RelocationBehavior reloc,
Elliott Hughesb25c3f62012-03-26 16:35:06 -070061 bool writable = false);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080062
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 ~OatFile();
64
65 const std::string& GetLocation() const {
66 return location_;
67 }
68
69 const OatHeader& GetOatHeader() const;
70
71 class OatDexFile;
72
Brian Carlstrom3320cf42011-10-04 14:58:28 -070073 class OatMethod {
74 public:
Brian Carlstromae826982011-11-09 01:33:42 -080075 // Link Method for execution using the contents of this OatMethod
76 void LinkMethodPointers(Method* method) const;
77
78 // Link Method for image writing using the contents of this OatMethod
79 void LinkMethodOffsets(Method* method) const;
80
81 uint32_t GetCodeOffset() const {
82 return code_offset_;
83 }
84 size_t GetFrameSizeInBytes() const {
85 return frame_size_in_bytes_;
86 }
87 uint32_t GetCoreSpillMask() const {
88 return core_spill_mask_;
89 }
90 uint32_t GetFpSpillMask() const {
91 return fp_spill_mask_;
92 }
93 uint32_t GetMappingTableOffset() const {
94 return mapping_table_offset_;
95 }
96 uint32_t GetVmapTableOffset() const {
97 return vmap_table_offset_;
98 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080099 uint32_t GetGcMapOffset() const {
100 return gc_map_offset_;
101 }
Brian Carlstromae826982011-11-09 01:33:42 -0800102 uint32_t GetInvokeStubOffset() const {
103 return invoke_stub_offset_;
104 }
105
Logan Chien0c717dd2012-03-28 18:31:07 +0800106#if defined(ART_USE_LLVM_COMPILER)
Logan Chien937105a2012-04-02 02:37:37 +0800107 uint16_t GetCodeElfIndex() const {
Logan Chien0c717dd2012-03-28 18:31:07 +0800108 return code_elf_idx_;
Brian Carlstromae826982011-11-09 01:33:42 -0800109 }
Logan Chien937105a2012-04-02 02:37:37 +0800110 uint16_t GetCodeElfFuncIndex() const {
111 return code_elf_func_idx_;
112 }
113 uint16_t GetInvokeStubElfIndex() const {
Logan Chien0c717dd2012-03-28 18:31:07 +0800114 return invoke_stub_elf_idx_;
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700115 }
Logan Chien937105a2012-04-02 02:37:37 +0800116 uint16_t GetInvokeStubElfFuncIndex() const {
117 return invoke_stub_elf_func_idx_;
118 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800119#endif
120
121 bool IsCodeInElf() const {
122#if defined(ART_USE_LLVM_COMPILER)
Logan Chien937105a2012-04-02 02:37:37 +0800123 return (code_elf_idx_ != static_cast<uint16_t>(-1));
Logan Chien0c717dd2012-03-28 18:31:07 +0800124#else
125 return false;
126#endif
127 }
128
129 bool IsInvokeStubInElf() const {
130#if defined(ART_USE_LLVM_COMPILER)
Logan Chien937105a2012-04-02 02:37:37 +0800131 return (invoke_stub_elf_idx_ != static_cast<uint16_t>(-1));
Logan Chien0c717dd2012-03-28 18:31:07 +0800132#else
133 return false;
134#endif
135 }
136
137 const void* GetCode() const;
138 uint32_t GetCodeSize() const;
139
Brian Carlstromae826982011-11-09 01:33:42 -0800140 const uint32_t* GetMappingTable() const {
141 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
142 }
143 const uint16_t* GetVmapTable() const {
144 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
145 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800146 const uint8_t* GetGcMap() const {
147 return GetOatPointer<const uint8_t*>(gc_map_offset_);
148 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800149
150 const Method::InvokeStub* GetInvokeStub() const;
151 uint32_t GetInvokeStubSize() const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700152
TDYa127eead4ac2012-06-03 07:15:25 -0700153#if defined(ART_USE_LLVM_COMPILER)
154 const void* GetProxyStub() const;
155#endif
156
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700157 ~OatMethod();
158
Brian Carlstromae826982011-11-09 01:33:42 -0800159 // Create an OatMethod with offsets relative to the given base address
160 OatMethod(const byte* base,
161 const uint32_t code_offset,
162 const size_t frame_size_in_bytes,
163 const uint32_t core_spill_mask,
164 const uint32_t fp_spill_mask,
165 const uint32_t mapping_table_offset,
166 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800167 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800168 const uint32_t invoke_stub_offset
169#if defined(ART_USE_LLVM_COMPILER)
170 , const compiler_llvm::ElfLoader* elf_loader,
Logan Chien937105a2012-04-02 02:37:37 +0800171 const uint16_t code_elf_idx,
172 const uint16_t code_elf_func_idx,
173 const uint16_t invoke_stub_elf_idx,
TDYa127eead4ac2012-06-03 07:15:25 -0700174 const uint16_t invoke_stub_elf_func_idx,
Logan Chien7a2a23a2012-06-06 11:01:00 +0800175 const uint16_t proxy_stub_elf_idx,
TDYa127eead4ac2012-06-03 07:15:25 -0700176 const uint16_t proxy_stub_elf_func_idx
Logan Chien0c717dd2012-03-28 18:31:07 +0800177#endif
178 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700179
Brian Carlstromae826982011-11-09 01:33:42 -0800180 private:
181 template<class T>
182 T GetOatPointer(uint32_t offset) const {
183 if (offset == 0) {
184 return NULL;
185 }
Ian Rogers30fab402012-01-23 15:43:46 -0800186 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800187 }
188
Ian Rogers30fab402012-01-23 15:43:46 -0800189 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800190
191 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700192 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700193 uint32_t core_spill_mask_;
194 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800195 uint32_t mapping_table_offset_;
196 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800197 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800198 uint32_t invoke_stub_offset_;
199
Logan Chien0c717dd2012-03-28 18:31:07 +0800200#if defined(ART_USE_LLVM_COMPILER)
201 const compiler_llvm::ElfLoader* elf_loader_;
202
Logan Chien937105a2012-04-02 02:37:37 +0800203 uint16_t code_elf_idx_;
204 uint16_t code_elf_func_idx_;
205 uint16_t invoke_stub_elf_idx_;
206 uint16_t invoke_stub_elf_func_idx_;
Logan Chien7a2a23a2012-06-06 11:01:00 +0800207 uint16_t proxy_stub_elf_idx_;
TDYa127eead4ac2012-06-03 07:15:25 -0700208 uint16_t proxy_stub_elf_func_idx_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800209#endif
210
Brian Carlstromae826982011-11-09 01:33:42 -0800211 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700212 };
213
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 class OatClass {
215 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800216 Class::Status GetStatus() const;
217
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700218 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700219 // defintion. direct methods come first, followed by virtual
220 // methods. note that runtime created methods such as miranda
221 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700222 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223 ~OatClass();
224
225 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800226 OatClass(const OatFile* oat_file,
227 Class::Status status,
228 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700229
Brian Carlstrome24fa612011-09-29 00:53:55 -0700230 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800231 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700232 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233
234 friend class OatDexFile;
235 };
236
237 class OatDexFile {
238 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800239 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700240 const OatClass* GetOatClass(uint32_t class_def_index) const;
241
242 const std::string& GetDexFileLocation() const {
243 return dex_file_location_;
244 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700245
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800246 uint32_t GetDexFileLocationChecksum() const {
247 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700248 }
249
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250 ~OatDexFile();
251 private:
252 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800253 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800255 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800256 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700257
258 const OatFile* oat_file_;
259 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800260 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800261 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800262 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263
264 friend class OatFile;
265 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
266 };
267
Logan Chien937105a2012-04-02 02:37:37 +0800268#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800269 class OatElfImage {
270 public:
271 const byte* begin() const {
272 return elf_addr_;
273 }
274
275 const byte* end() const {
276 return (elf_addr_ + elf_size_);
277 }
278
279 size_t size() const {
280 return elf_size_;
281 }
282
283 private:
284 OatElfImage(const OatFile* oat_file, const byte* addr, uint32_t size);
285
286 const OatFile* oat_file_;
287 const byte* elf_addr_;
288 uint32_t elf_size_;
289
290 friend class OatFile;
291 DISALLOW_COPY_AND_ASSIGN(OatElfImage);
292 };
Logan Chien937105a2012-04-02 02:37:37 +0800293#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800294
Ian Rogers7fe2c692011-12-06 16:35:59 -0800295 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
296 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700297 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298
Logan Chien0c717dd2012-03-28 18:31:07 +0800299#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800300 const OatElfImage* GetOatElfImage(size_t i) const {
301 return oat_elf_images_[i];
302 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800303#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800304
Ian Rogers30fab402012-01-23 15:43:46 -0800305 size_t Size() const {
306 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700307 }
308
Logan Chien0c717dd2012-03-28 18:31:07 +0800309 void RelocateExecutable();
310
Brian Carlstrome24fa612011-09-29 00:53:55 -0700311 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700312 explicit OatFile(const std::string& filename);
Logan Chien0c717dd2012-03-28 18:31:07 +0800313 bool Map(File& file, byte* requested_base, RelocationBehavior reloc, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314
Ian Rogers30fab402012-01-23 15:43:46 -0800315 const byte* Begin() const;
316 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700317
318 // The oat file name.
319 //
320 // The image will embed this to link its associated oat file.
321 const std::string location_;
322
323 // backing memory map for oat file
324 UniquePtr<MemMap> mem_map_;
325
Elliott Hughesa0e18062012-04-13 15:59:59 -0700326 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700327 Table oat_dex_files_;
328
Logan Chien0c717dd2012-03-28 18:31:07 +0800329#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800330 std::vector<OatElfImage*> oat_elf_images_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800331 UniquePtr<compiler_llvm::ElfLoader> elf_loader_;
332#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800333
Brian Carlstrome24fa612011-09-29 00:53:55 -0700334 friend class OatClass;
335 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800336 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337 DISALLOW_COPY_AND_ASSIGN(OatFile);
338};
339
340} // namespace art
341
342#endif // ART_SRC_OAT_WRITER_H_