blob: 0b1ebac884facb8b572cae03e7a987aea61b822b [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
153 ~OatMethod();
154
Brian Carlstromae826982011-11-09 01:33:42 -0800155 // Create an OatMethod with offsets relative to the given base address
156 OatMethod(const byte* base,
157 const uint32_t code_offset,
158 const size_t frame_size_in_bytes,
159 const uint32_t core_spill_mask,
160 const uint32_t fp_spill_mask,
161 const uint32_t mapping_table_offset,
162 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800163 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800164 const uint32_t invoke_stub_offset
165#if defined(ART_USE_LLVM_COMPILER)
166 , const compiler_llvm::ElfLoader* elf_loader,
Logan Chien937105a2012-04-02 02:37:37 +0800167 const uint16_t code_elf_idx,
168 const uint16_t code_elf_func_idx,
169 const uint16_t invoke_stub_elf_idx,
170 const uint16_t invoke_stub_elf_func_idx
Logan Chien0c717dd2012-03-28 18:31:07 +0800171#endif
172 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700173
Brian Carlstromae826982011-11-09 01:33:42 -0800174 private:
175 template<class T>
176 T GetOatPointer(uint32_t offset) const {
177 if (offset == 0) {
178 return NULL;
179 }
Ian Rogers30fab402012-01-23 15:43:46 -0800180 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800181 }
182
Ian Rogers30fab402012-01-23 15:43:46 -0800183 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800184
185 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700186 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700187 uint32_t core_spill_mask_;
188 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800189 uint32_t mapping_table_offset_;
190 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800191 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800192 uint32_t invoke_stub_offset_;
193
Logan Chien0c717dd2012-03-28 18:31:07 +0800194#if defined(ART_USE_LLVM_COMPILER)
195 const compiler_llvm::ElfLoader* elf_loader_;
196
Logan Chien937105a2012-04-02 02:37:37 +0800197 uint16_t code_elf_idx_;
198 uint16_t code_elf_func_idx_;
199 uint16_t invoke_stub_elf_idx_;
200 uint16_t invoke_stub_elf_func_idx_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800201#endif
202
Brian Carlstromae826982011-11-09 01:33:42 -0800203 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700204 };
205
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206 class OatClass {
207 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800208 Class::Status GetStatus() const;
209
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700210 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 // defintion. direct methods come first, followed by virtual
212 // methods. note that runtime created methods such as miranda
213 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700214 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700215 ~OatClass();
216
217 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800218 OatClass(const OatFile* oat_file,
219 Class::Status status,
220 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700221
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800223 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700224 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225
226 friend class OatDexFile;
227 };
228
229 class OatDexFile {
230 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800231 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700232 const OatClass* GetOatClass(uint32_t class_def_index) const;
233
234 const std::string& GetDexFileLocation() const {
235 return dex_file_location_;
236 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700237
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800238 uint32_t GetDexFileLocationChecksum() const {
239 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700240 }
241
Brian Carlstrome24fa612011-09-29 00:53:55 -0700242 ~OatDexFile();
243 private:
244 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800245 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700246 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800247 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800248 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249
250 const OatFile* oat_file_;
251 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800252 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800253 const 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 friend class OatFile;
257 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
258 };
259
Logan Chien937105a2012-04-02 02:37:37 +0800260#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800261 class OatElfImage {
262 public:
263 const byte* begin() const {
264 return elf_addr_;
265 }
266
267 const byte* end() const {
268 return (elf_addr_ + elf_size_);
269 }
270
271 size_t size() const {
272 return elf_size_;
273 }
274
275 private:
276 OatElfImage(const OatFile* oat_file, const byte* addr, uint32_t size);
277
278 const OatFile* oat_file_;
279 const byte* elf_addr_;
280 uint32_t elf_size_;
281
282 friend class OatFile;
283 DISALLOW_COPY_AND_ASSIGN(OatElfImage);
284 };
Logan Chien937105a2012-04-02 02:37:37 +0800285#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800286
Ian Rogers7fe2c692011-12-06 16:35:59 -0800287 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
288 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700289 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700290
Logan Chien0c717dd2012-03-28 18:31:07 +0800291#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800292 const OatElfImage* GetOatElfImage(size_t i) const {
293 return oat_elf_images_[i];
294 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800295#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800296
Ian Rogers30fab402012-01-23 15:43:46 -0800297 size_t Size() const {
298 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299 }
300
Logan Chien0c717dd2012-03-28 18:31:07 +0800301 void RelocateExecutable();
302
Brian Carlstrome24fa612011-09-29 00:53:55 -0700303 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700304 explicit OatFile(const std::string& filename);
Logan Chien0c717dd2012-03-28 18:31:07 +0800305 bool Map(File& file, byte* requested_base, RelocationBehavior reloc, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306
Ian Rogers30fab402012-01-23 15:43:46 -0800307 const byte* Begin() const;
308 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309
310 // The oat file name.
311 //
312 // The image will embed this to link its associated oat file.
313 const std::string location_;
314
315 // backing memory map for oat file
316 UniquePtr<MemMap> mem_map_;
317
Elliott Hughesa0e18062012-04-13 15:59:59 -0700318 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700319 Table oat_dex_files_;
320
Logan Chien0c717dd2012-03-28 18:31:07 +0800321#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800322 std::vector<OatElfImage*> oat_elf_images_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800323 UniquePtr<compiler_llvm::ElfLoader> elf_loader_;
324#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800325
Brian Carlstrome24fa612011-09-29 00:53:55 -0700326 friend class OatClass;
327 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800328 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700329 DISALLOW_COPY_AND_ASSIGN(OatFile);
330};
331
332} // namespace art
333
334#endif // ART_SRC_OAT_WRITER_H_