blob: e958c4d1c20db6616082a902553dbf61286b543e [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,
175 const uint16_t proxy_stub_elf_func_idx
Logan Chien0c717dd2012-03-28 18:31:07 +0800176#endif
177 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700178
Brian Carlstromae826982011-11-09 01:33:42 -0800179 private:
180 template<class T>
181 T GetOatPointer(uint32_t offset) const {
182 if (offset == 0) {
183 return NULL;
184 }
Ian Rogers30fab402012-01-23 15:43:46 -0800185 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800186 }
187
Ian Rogers30fab402012-01-23 15:43:46 -0800188 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800189
190 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700191 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700192 uint32_t core_spill_mask_;
193 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800194 uint32_t mapping_table_offset_;
195 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800196 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800197 uint32_t invoke_stub_offset_;
198
Logan Chien0c717dd2012-03-28 18:31:07 +0800199#if defined(ART_USE_LLVM_COMPILER)
200 const compiler_llvm::ElfLoader* elf_loader_;
201
Logan Chien937105a2012-04-02 02:37:37 +0800202 uint16_t code_elf_idx_;
203 uint16_t code_elf_func_idx_;
204 uint16_t invoke_stub_elf_idx_;
205 uint16_t invoke_stub_elf_func_idx_;
TDYa127eead4ac2012-06-03 07:15:25 -0700206 uint16_t proxy_stub_elf_func_idx_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800207#endif
208
Brian Carlstromae826982011-11-09 01:33:42 -0800209 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700210 };
211
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212 class OatClass {
213 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800214 Class::Status GetStatus() const;
215
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700216 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217 // defintion. direct methods come first, followed by virtual
218 // methods. note that runtime created methods such as miranda
219 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221 ~OatClass();
222
223 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800224 OatClass(const OatFile* oat_file,
225 Class::Status status,
226 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700227
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800229 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700230 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700231
232 friend class OatDexFile;
233 };
234
235 class OatDexFile {
236 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800237 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700238 const OatClass* GetOatClass(uint32_t class_def_index) const;
239
240 const std::string& GetDexFileLocation() const {
241 return dex_file_location_;
242 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700243
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800244 uint32_t GetDexFileLocationChecksum() const {
245 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700246 }
247
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 ~OatDexFile();
249 private:
250 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800251 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800253 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800254 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700255
256 const OatFile* oat_file_;
257 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800258 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800259 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800260 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261
262 friend class OatFile;
263 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
264 };
265
Logan Chien937105a2012-04-02 02:37:37 +0800266#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800267 class OatElfImage {
268 public:
269 const byte* begin() const {
270 return elf_addr_;
271 }
272
273 const byte* end() const {
274 return (elf_addr_ + elf_size_);
275 }
276
277 size_t size() const {
278 return elf_size_;
279 }
280
281 private:
282 OatElfImage(const OatFile* oat_file, const byte* addr, uint32_t size);
283
284 const OatFile* oat_file_;
285 const byte* elf_addr_;
286 uint32_t elf_size_;
287
288 friend class OatFile;
289 DISALLOW_COPY_AND_ASSIGN(OatElfImage);
290 };
Logan Chien937105a2012-04-02 02:37:37 +0800291#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800292
Ian Rogers7fe2c692011-12-06 16:35:59 -0800293 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
294 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700295 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700296
Logan Chien0c717dd2012-03-28 18:31:07 +0800297#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800298 const OatElfImage* GetOatElfImage(size_t i) const {
299 return oat_elf_images_[i];
300 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800301#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800302
Ian Rogers30fab402012-01-23 15:43:46 -0800303 size_t Size() const {
304 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700305 }
306
Logan Chien0c717dd2012-03-28 18:31:07 +0800307 void RelocateExecutable();
308
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700310 explicit OatFile(const std::string& filename);
Logan Chien0c717dd2012-03-28 18:31:07 +0800311 bool Map(File& file, byte* requested_base, RelocationBehavior reloc, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700312
Ian Rogers30fab402012-01-23 15:43:46 -0800313 const byte* Begin() const;
314 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700315
316 // The oat file name.
317 //
318 // The image will embed this to link its associated oat file.
319 const std::string location_;
320
321 // backing memory map for oat file
322 UniquePtr<MemMap> mem_map_;
323
Elliott Hughesa0e18062012-04-13 15:59:59 -0700324 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700325 Table oat_dex_files_;
326
Logan Chien0c717dd2012-03-28 18:31:07 +0800327#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800328 std::vector<OatElfImage*> oat_elf_images_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800329 UniquePtr<compiler_llvm::ElfLoader> elf_loader_;
330#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800331
Brian Carlstrome24fa612011-09-29 00:53:55 -0700332 friend class OatClass;
333 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800334 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700335 DISALLOW_COPY_AND_ASSIGN(OatFile);
336};
337
338} // namespace art
339
340#endif // ART_SRC_OAT_WRITER_H_