blob: 07d7f63b0ea3229dda41869de828db46426461e0 [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)
107 uint32_t GetCodeElfIndex() const {
108 return code_elf_idx_;
Brian Carlstromae826982011-11-09 01:33:42 -0800109 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800110 uint32_t GetInvokeStubElfIndex() const {
111 return invoke_stub_elf_idx_;
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700112 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800113#endif
114
115 bool IsCodeInElf() const {
116#if defined(ART_USE_LLVM_COMPILER)
117 return (code_elf_idx_ != -1u);
118#else
119 return false;
120#endif
121 }
122
123 bool IsInvokeStubInElf() const {
124#if defined(ART_USE_LLVM_COMPILER)
125 return (invoke_stub_elf_idx_ != -1u);
126#else
127 return false;
128#endif
129 }
130
131 const void* GetCode() const;
132 uint32_t GetCodeSize() const;
133
Brian Carlstromae826982011-11-09 01:33:42 -0800134 const uint32_t* GetMappingTable() const {
135 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
136 }
137 const uint16_t* GetVmapTable() const {
138 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
139 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800140 const uint8_t* GetGcMap() const {
141 return GetOatPointer<const uint8_t*>(gc_map_offset_);
142 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800143
144 const Method::InvokeStub* GetInvokeStub() const;
145 uint32_t GetInvokeStubSize() const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700146
147 ~OatMethod();
148
Brian Carlstromae826982011-11-09 01:33:42 -0800149 // Create an OatMethod with offsets relative to the given base address
150 OatMethod(const byte* base,
151 const uint32_t code_offset,
152 const size_t frame_size_in_bytes,
153 const uint32_t core_spill_mask,
154 const uint32_t fp_spill_mask,
155 const uint32_t mapping_table_offset,
156 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800157 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800158 const uint32_t invoke_stub_offset
159#if defined(ART_USE_LLVM_COMPILER)
160 , const compiler_llvm::ElfLoader* elf_loader,
161 const uint32_t code_elf_idx,
162 const uint32_t invoke_stub_elf_idx
163#endif
164 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700165
Brian Carlstromae826982011-11-09 01:33:42 -0800166 private:
167 template<class T>
168 T GetOatPointer(uint32_t offset) const {
169 if (offset == 0) {
170 return NULL;
171 }
Ian Rogers30fab402012-01-23 15:43:46 -0800172 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800173 }
174
Ian Rogers30fab402012-01-23 15:43:46 -0800175 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800176
177 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700178 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700179 uint32_t core_spill_mask_;
180 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800181 uint32_t mapping_table_offset_;
182 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800183 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800184 uint32_t invoke_stub_offset_;
185
Logan Chien0c717dd2012-03-28 18:31:07 +0800186#if defined(ART_USE_LLVM_COMPILER)
187 const compiler_llvm::ElfLoader* elf_loader_;
188
189 uint32_t code_elf_idx_;
190 uint32_t invoke_stub_elf_idx_;
191#endif
192
Brian Carlstromae826982011-11-09 01:33:42 -0800193 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700194 };
195
Brian Carlstrome24fa612011-09-29 00:53:55 -0700196 class OatClass {
197 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800198 Class::Status GetStatus() const;
199
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700200 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700201 // defintion. direct methods come first, followed by virtual
202 // methods. note that runtime created methods such as miranda
203 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700204 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700205 ~OatClass();
206
207 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800208 OatClass(const OatFile* oat_file,
209 Class::Status status,
210 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700211
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800213 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700214 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700215
216 friend class OatDexFile;
217 };
218
219 class OatDexFile {
220 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800221 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700222 const OatClass* GetOatClass(uint32_t class_def_index) const;
223
224 const std::string& GetDexFileLocation() const {
225 return dex_file_location_;
226 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700227
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800228 uint32_t GetDexFileLocationChecksum() const {
229 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700230 }
231
Brian Carlstrome24fa612011-09-29 00:53:55 -0700232 ~OatDexFile();
233 private:
234 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800235 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800237 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800238 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239
240 const OatFile* oat_file_;
241 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800242 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800243 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800244 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245
246 friend class OatFile;
247 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
248 };
249
Logan Chien0cc6ab62012-03-20 22:57:52 +0800250 class OatElfImage {
251 public:
252 const byte* begin() const {
253 return elf_addr_;
254 }
255
256 const byte* end() const {
257 return (elf_addr_ + elf_size_);
258 }
259
260 size_t size() const {
261 return elf_size_;
262 }
263
264 private:
265 OatElfImage(const OatFile* oat_file, const byte* addr, uint32_t size);
266
267 const OatFile* oat_file_;
268 const byte* elf_addr_;
269 uint32_t elf_size_;
270
271 friend class OatFile;
272 DISALLOW_COPY_AND_ASSIGN(OatElfImage);
273 };
274
Ian Rogers7fe2c692011-12-06 16:35:59 -0800275 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
276 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700277 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700278
Logan Chien0c717dd2012-03-28 18:31:07 +0800279#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800280 const OatElfImage* GetOatElfImage(size_t i) const {
281 return oat_elf_images_[i];
282 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800283#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800284
Ian Rogers30fab402012-01-23 15:43:46 -0800285 size_t Size() const {
286 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700287 }
288
Logan Chien0c717dd2012-03-28 18:31:07 +0800289 void RelocateExecutable();
290
Brian Carlstrome24fa612011-09-29 00:53:55 -0700291 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700292 explicit OatFile(const std::string& filename);
Logan Chien0c717dd2012-03-28 18:31:07 +0800293 bool Map(File& file, byte* requested_base, RelocationBehavior reloc, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294
Ian Rogers30fab402012-01-23 15:43:46 -0800295 const byte* Begin() const;
296 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297
298 // The oat file name.
299 //
300 // The image will embed this to link its associated oat file.
301 const std::string location_;
302
303 // backing memory map for oat file
304 UniquePtr<MemMap> mem_map_;
305
306 typedef std::map<std::string, const OatDexFile*> Table;
307 Table oat_dex_files_;
308
Logan Chien0c717dd2012-03-28 18:31:07 +0800309#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +0800310 std::vector<OatElfImage*> oat_elf_images_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800311 UniquePtr<compiler_llvm::ElfLoader> elf_loader_;
312#endif
Logan Chien0cc6ab62012-03-20 22:57:52 +0800313
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314 friend class OatClass;
315 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800316 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700317 DISALLOW_COPY_AND_ASSIGN(OatFile);
318};
319
320} // namespace art
321
322#endif // ART_SRC_OAT_WRITER_H_