blob: f12c84d23314888e2baeb1aabd9ed118eddf5575 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_OAT_WRITER_H_
18#define ART_COMPILER_OAT_WRITER_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include <stdint.h>
21
22#include <cstddef>
23
24#include "driver/compiler_driver.h"
25#include "mem_map.h"
26#include "oat.h"
27#include "mirror/class.h"
28#include "safe_map.h"
29#include "UniquePtr.h"
30
31namespace art {
32
Brian Carlstromba150c32013-08-27 17:31:03 -070033class BitVector;
Brian Carlstrom7940e442013-07-12 13:46:57 -070034class OutputStream;
35
36// OatHeader variable length with count of D OatDexFiles
37//
38// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
39// OatDexFile[1]
40// ...
41// OatDexFile[D]
42//
43// Dex[0] one variable sized DexFile for each OatDexFile.
44// Dex[1] these are literal copies of the input .dex files.
45// ...
46// Dex[D]
47//
48// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
49// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
50// ...
51// OatClass[C]
52//
53// padding if necessary so that the following code will be page aligned
54//
55// CompiledMethod one variable sized blob with the contents of each CompiledMethod
56// CompiledMethod
57// CompiledMethod
58// CompiledMethod
59// CompiledMethod
60// CompiledMethod
61// ...
62// CompiledMethod
63//
64class OatWriter {
65 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 OatWriter(const std::vector<const DexFile*>& dex_files,
67 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080068 uintptr_t image_file_location_oat_begin,
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080070 const CompilerDriver* compiler,
71 TimingLogger* timings);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070072
73 const OatHeader& GetOatHeader() const {
74 return *oat_header_;
75 }
76
77 size_t GetSize() const {
78 return size_;
79 }
80
Ian Rogers3d504072014-03-01 09:16:49 -080081 bool Write(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070082
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 ~OatWriter();
84
Brian Carlstromc50d8e12013-07-23 22:35:16 -070085 private:
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 size_t InitOatHeader();
87 size_t InitOatDexFiles(size_t offset);
88 size_t InitDexFiles(size_t offset);
89 size_t InitOatClasses(size_t offset);
90 size_t InitOatCode(size_t offset)
91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92 size_t InitOatCodeDexFiles(size_t offset)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94 size_t InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -070095 size_t* oat_class_index,
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 const DexFile& dex_file)
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98 size_t InitOatCodeClassDef(size_t offset,
99 size_t oat_class_index, size_t class_def_index,
100 const DexFile& dex_file,
101 const DexFile::ClassDef& class_def)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700104 size_t class_def_method_index, size_t* method_offsets_index,
105 bool is_native, InvokeType type, uint32_t method_idx, const DexFile&)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107
Ian Rogers3d504072014-03-01 09:16:49 -0800108 bool WriteTables(OutputStream* out, const size_t file_offset);
109 size_t WriteCode(OutputStream* out, const size_t file_offset);
110 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
111 size_t WriteCodeDexFile(OutputStream* out, const size_t file_offset, size_t relative_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700112 size_t* oat_class_index, const DexFile& dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800113 size_t WriteCodeClassDef(OutputStream* out, const size_t file_offset, size_t relative_offset,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700114 size_t oat_class_index, const DexFile& dex_file,
115 const DexFile::ClassDef& class_def);
Ian Rogers3d504072014-03-01 09:16:49 -0800116 size_t WriteCodeMethod(OutputStream* out, const size_t file_offset, size_t relative_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700117 size_t oat_class_index, size_t class_def_method_index,
118 size_t* method_offsets_index, bool is_static, uint32_t method_idx,
119 const DexFile& dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120
121 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
Ian Rogers3d504072014-03-01 09:16:49 -0800122 const OutputStream& out) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123
124 class OatDexFile {
125 public:
126 explicit OatDexFile(size_t offset, const DexFile& dex_file);
127 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800128 void UpdateChecksum(OatHeader* oat_header) const;
129 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130
131 // Offset of start of OatDexFile from beginning of OatHeader. It is
132 // used to validate file position when writing.
133 size_t offset_;
134
135 // data to write
136 uint32_t dex_file_location_size_;
137 const uint8_t* dex_file_location_data_;
138 uint32_t dex_file_location_checksum_;
139 uint32_t dex_file_offset_;
140 std::vector<uint32_t> methods_offsets_;
141
142 private:
143 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
144 };
145
146 class OatClass {
147 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700148 explicit OatClass(size_t offset,
149 std::vector<CompiledMethod*>* compiled_methods,
150 uint32_t num_non_null_compiled_methods,
151 mirror::Class::Status status);
152 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
154 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
155 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800156 void UpdateChecksum(OatHeader* oat_header) const;
157 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158
Brian Carlstromba150c32013-08-27 17:31:03 -0700159 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
160 DCHECK(compiled_methods_ != NULL);
161 return (*compiled_methods_)[class_def_method_index];
162 }
163
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 // Offset of start of OatClass from beginning of OatHeader. It is
165 // used to validate file position when writing. For Portable, it
166 // is also used to calculate the position of the OatMethodOffsets
167 // so that code pointers within the OatMethodOffsets can be
168 // patched to point to code in the Portable .o ELF objects.
169 size_t offset_;
170
Brian Carlstromba150c32013-08-27 17:31:03 -0700171 // CompiledMethods for each class_def_method_index, or NULL if no method is available.
172 std::vector<CompiledMethod*>* compiled_methods_;
173
174 // Offset from OatClass::offset_ to the OatMethodOffsets for the
175 // class_def_method_index. If 0, it means the corresponding
176 // CompiledMethod entry in OatClass::compiled_methods_ should be
177 // NULL and that the OatClass::type_ should be kOatClassBitmap.
178 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
179
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700181
182 COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits);
183 int16_t status_;
184
185 COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits);
186 uint16_t type_;
187
188 uint32_t method_bitmap_size_;
189
190 // bit vector indexed by ClassDef method index. When
191 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
192 // method has an OatMethodOffsets in methods_offsets_, otherwise
193 // the entry was ommited to save space. If OatClassType::type_ is
194 // not is kOatClassBitmap, the bitmap will be NULL.
195 BitVector* method_bitmap_;
196
197 // OatMethodOffsets for each CompiledMethod present in the
198 // OatClass. Note that some may be missing if
199 // OatClass::compiled_methods_ contains NULL values (and
200 // oat_method_offsets_offsets_from_oat_class_ should contain 0
201 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 std::vector<OatMethodOffsets> method_offsets_;
203
204 private:
205 DISALLOW_COPY_AND_ASSIGN(OatClass);
206 };
207
208 const CompilerDriver* const compiler_driver_;
209
210 // note OatFile does not take ownership of the DexFiles
211 const std::vector<const DexFile*>* dex_files_;
212
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700213 // Size required for Oat data structures.
214 size_t size_;
215
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 // dependencies on the image.
217 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800218 uintptr_t image_file_location_oat_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 std::string image_file_location_;
220
221 // data to write
222 OatHeader* oat_header_;
223 std::vector<OatDexFile*> oat_dex_files_;
224 std::vector<OatClass*> oat_classes_;
Ian Rogers468532e2013-08-05 10:56:33 -0700225 UniquePtr<const std::vector<uint8_t> > interpreter_to_interpreter_bridge_;
226 UniquePtr<const std::vector<uint8_t> > interpreter_to_compiled_code_bridge_;
227 UniquePtr<const std::vector<uint8_t> > jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700228 UniquePtr<const std::vector<uint8_t> > portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 UniquePtr<const std::vector<uint8_t> > portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700230 UniquePtr<const std::vector<uint8_t> > portable_to_interpreter_bridge_;
Jeff Hao88474b42013-10-23 16:24:40 -0700231 UniquePtr<const std::vector<uint8_t> > quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 UniquePtr<const std::vector<uint8_t> > quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700233 UniquePtr<const std::vector<uint8_t> > quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234
235 // output stats
236 uint32_t size_dex_file_alignment_;
237 uint32_t size_executable_offset_alignment_;
238 uint32_t size_oat_header_;
239 uint32_t size_oat_header_image_file_location_;
240 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700241 uint32_t size_interpreter_to_interpreter_bridge_;
242 uint32_t size_interpreter_to_compiled_code_bridge_;
243 uint32_t size_jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700244 uint32_t size_portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 uint32_t size_portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700246 uint32_t size_portable_to_interpreter_bridge_;
Jeff Hao88474b42013-10-23 16:24:40 -0700247 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700249 uint32_t size_quick_to_interpreter_bridge_;
250 uint32_t size_trampoline_alignment_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 uint32_t size_code_size_;
252 uint32_t size_code_;
253 uint32_t size_code_alignment_;
254 uint32_t size_mapping_table_;
255 uint32_t size_vmap_table_;
256 uint32_t size_gc_map_;
257 uint32_t size_oat_dex_file_location_size_;
258 uint32_t size_oat_dex_file_location_data_;
259 uint32_t size_oat_dex_file_location_checksum_;
260 uint32_t size_oat_dex_file_offset_;
261 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700262 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700264 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 uint32_t size_oat_class_method_offsets_;
266
Mathieu Chartier193bad92013-08-29 18:46:00 -0700267 // Code mappings for deduplication. Deduplication is already done on a pointer basis by the
268 // compiler driver, so we can simply compare the pointers to find out if things are duplicated.
269 SafeMap<const std::vector<uint8_t>*, uint32_t> code_offsets_;
270 SafeMap<const std::vector<uint8_t>*, uint32_t> vmap_table_offsets_;
271 SafeMap<const std::vector<uint8_t>*, uint32_t> mapping_table_offsets_;
272 SafeMap<const std::vector<uint8_t>*, uint32_t> gc_map_offsets_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273
274 DISALLOW_COPY_AND_ASSIGN(OatWriter);
275};
276
277} // namespace art
278
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700279#endif // ART_COMPILER_OAT_WRITER_H_