blob: 895163441f6ed7140c24e727802cd8b5fa7d0468 [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>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010025#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "oat.h"
27#include "mirror/class.h"
28#include "safe_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
30namespace art {
31
Brian Carlstromba150c32013-08-27 17:31:03 -070032class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070033class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000034class CompilerDriver;
Vladimir Markof4da6752014-08-01 19:04:18 +010035class ImageWriter;
Brian Carlstrom7940e442013-07-12 13:46:57 -070036class OutputStream;
Vladimir Marko20f85592015-03-19 10:07:02 +000037class TimingLogger;
Brian Carlstrom7940e442013-07-12 13:46:57 -070038
39// OatHeader variable length with count of D OatDexFiles
40//
41// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
42// OatDexFile[1]
43// ...
44// OatDexFile[D]
45//
46// Dex[0] one variable sized DexFile for each OatDexFile.
47// Dex[1] these are literal copies of the input .dex files.
48// ...
49// Dex[D]
50//
51// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
52// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
53// ...
54// OatClass[C]
55//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010056// GcMap one variable sized blob with GC map.
57// GcMap GC maps are deduplicated.
58// ...
59// GcMap
60//
61// VmapTable one variable sized VmapTable blob (quick compiler only).
62// VmapTable VmapTables are deduplicated.
63// ...
64// VmapTable
65//
66// MappingTable one variable sized blob with MappingTable (quick compiler only).
67// MappingTable MappingTables are deduplicated.
68// ...
69// MappingTable
70//
Brian Carlstrom7940e442013-07-12 13:46:57 -070071// padding if necessary so that the following code will be page aligned
72//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010073// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
74// MethodCode one variable sized blob with the code of a CompiledMethod.
75// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
76// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070077// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010078// OatMethodHeader
79// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070080//
81class OatWriter {
82 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 OatWriter(const std::vector<const DexFile*>& dex_files,
84 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070086 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080087 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010088 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070089 TimingLogger* timings,
90 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070091
92 const OatHeader& GetOatHeader() const {
93 return *oat_header_;
94 }
95
96 size_t GetSize() const {
97 return size_;
98 }
99
Vladimir Marko5c42c292015-02-25 12:02:49 +0000100 size_t GetBssSize() const {
101 return bss_size_;
102 }
103
Vladimir Markof4da6752014-08-01 19:04:18 +0100104 const std::vector<uintptr_t>& GetAbsolutePatchLocations() const {
105 return absolute_patch_locations_;
106 }
107
108 void SetOatDataOffset(size_t oat_data_offset) {
109 oat_data_offset_ = oat_data_offset;
110 }
111
Ian Rogers3d504072014-03-01 09:16:49 -0800112 bool Write(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700113
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 ~OatWriter();
115
Mark Mendellae9fd932014-02-10 16:14:35 -0800116 struct DebugInfo {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700117 DebugInfo(const std::string& method_name, const char* src_file_name,
118 uint32_t low_pc, uint32_t high_pc, const uint8_t* dbgstream,
Andreas Gampe79273802014-08-05 20:21:05 -0700119 CompiledMethod* compiled_method)
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700120 : method_name_(method_name), src_file_name_(src_file_name),
121 low_pc_(low_pc), high_pc_(high_pc), dbgstream_(dbgstream),
Andreas Gampe79273802014-08-05 20:21:05 -0700122 compiled_method_(compiled_method) {
Mark Mendellae9fd932014-02-10 16:14:35 -0800123 }
Andreas Gampe79273802014-08-05 20:21:05 -0700124 std::string method_name_; // Note: this name is a pretty-printed name.
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700125 const char* src_file_name_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800126 uint32_t low_pc_;
127 uint32_t high_pc_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700128 const uint8_t* dbgstream_;
Andreas Gampe79273802014-08-05 20:21:05 -0700129 CompiledMethod* compiled_method_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800130 };
131
132 const std::vector<DebugInfo>& GetCFIMethodInfo() const {
133 return method_info_;
134 }
135
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700136 private:
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100137 // The DataAccess classes are helper classes that provide access to members related to
138 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
139 // we can share a lot of code for processing the maps with template classes below.
140 struct GcMapDataAccess;
141 struct MappingTableDataAccess;
142 struct VmapTableDataAccess;
143
144 // The function VisitDexMethods() below iterates through all the methods in all
145 // the compiled dex files in order of their definitions. The method visitor
146 // classes provide individual bits of processing for each of the passes we need to
147 // first collect the data we want to write to the oat file and then, in later passes,
148 // to actually write it.
149 class DexMethodVisitor;
150 class OatDexMethodVisitor;
151 class InitOatClassesMethodVisitor;
152 class InitCodeMethodVisitor;
153 template <typename DataAccess>
154 class InitMapMethodVisitor;
155 class InitImageMethodVisitor;
156 class WriteCodeMethodVisitor;
157 template <typename DataAccess>
158 class WriteMapMethodVisitor;
159
160 // Visit all the methods in all the compiled dex files in their definition order
161 // with a given DexMethodVisitor.
162 bool VisitDexMethods(DexMethodVisitor* visitor);
163
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 size_t InitOatHeader();
165 size_t InitOatDexFiles(size_t offset);
166 size_t InitDexFiles(size_t offset);
167 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100168 size_t InitOatMaps(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 size_t InitOatCode(size_t offset)
170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
171 size_t InitOatCodeDexFiles(size_t offset)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173
Ian Rogers3d504072014-03-01 09:16:49 -0800174 bool WriteTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100175 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
176 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800177 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178
Vladimir Markof4da6752014-08-01 19:04:18 +0100179 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
180
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 class OatDexFile {
182 public:
183 explicit OatDexFile(size_t offset, const DexFile& dex_file);
184 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800185 void UpdateChecksum(OatHeader* oat_header) const;
186 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
188 // Offset of start of OatDexFile from beginning of OatHeader. It is
189 // used to validate file position when writing.
190 size_t offset_;
191
192 // data to write
193 uint32_t dex_file_location_size_;
194 const uint8_t* dex_file_location_data_;
195 uint32_t dex_file_location_checksum_;
196 uint32_t dex_file_offset_;
197 std::vector<uint32_t> methods_offsets_;
198
199 private:
200 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
201 };
202
203 class OatClass {
204 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700205 explicit OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100206 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -0700207 uint32_t num_non_null_compiled_methods,
208 mirror::Class::Status status);
209 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
211 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
212 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800213 void UpdateChecksum(OatHeader* oat_header) const;
214 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215
Brian Carlstromba150c32013-08-27 17:31:03 -0700216 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100217 DCHECK_LT(class_def_method_index, compiled_methods_.size());
218 return compiled_methods_[class_def_method_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700219 }
220
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 // Offset of start of OatClass from beginning of OatHeader. It is
Elliott Hughes956af0f2014-12-11 14:34:28 -0800222 // used to validate file position when writing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 size_t offset_;
224
Brian Carlstromba150c32013-08-27 17:31:03 -0700225 // CompiledMethods for each class_def_method_index, or NULL if no method is available.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100226 std::vector<CompiledMethod*> compiled_methods_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700227
228 // Offset from OatClass::offset_ to the OatMethodOffsets for the
229 // class_def_method_index. If 0, it means the corresponding
230 // CompiledMethod entry in OatClass::compiled_methods_ should be
231 // NULL and that the OatClass::type_ should be kOatClassBitmap.
232 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
233
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700235
Andreas Gampe785d2f22014-11-03 22:57:30 -0800236 static_assert(mirror::Class::Status::kStatusMax < (2 ^ 16), "class status won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700237 int16_t status_;
238
Andreas Gampe785d2f22014-11-03 22:57:30 -0800239 static_assert(OatClassType::kOatClassMax < (2 ^ 16), "oat_class type won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700240 uint16_t type_;
241
242 uint32_t method_bitmap_size_;
243
244 // bit vector indexed by ClassDef method index. When
245 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
246 // method has an OatMethodOffsets in methods_offsets_, otherwise
247 // the entry was ommited to save space. If OatClassType::type_ is
248 // not is kOatClassBitmap, the bitmap will be NULL.
249 BitVector* method_bitmap_;
250
Vladimir Marko8a630572014-04-09 18:45:35 +0100251 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
252 // present in the OatClass. Note that some may be missing if
Brian Carlstromba150c32013-08-27 17:31:03 -0700253 // OatClass::compiled_methods_ contains NULL values (and
254 // oat_method_offsets_offsets_from_oat_class_ should contain 0
255 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 std::vector<OatMethodOffsets> method_offsets_;
Vladimir Marko7624d252014-05-02 14:40:15 +0100257 std::vector<OatQuickMethodHeader> method_headers_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
259 private:
260 DISALLOW_COPY_AND_ASSIGN(OatClass);
261 };
262
Mark Mendellae9fd932014-02-10 16:14:35 -0800263 std::vector<DebugInfo> method_info_;
264
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 const CompilerDriver* const compiler_driver_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100266 ImageWriter* const image_writer_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267
268 // note OatFile does not take ownership of the DexFiles
269 const std::vector<const DexFile*>* dex_files_;
270
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700271 // Size required for Oat data structures.
272 size_t size_;
273
Vladimir Marko5c42c292015-02-25 12:02:49 +0000274 // The size of the required .bss section holding the DexCache data.
275 size_t bss_size_;
276
Vladimir Markof4da6752014-08-01 19:04:18 +0100277 // Offset of the oat data from the start of the mmapped region of the elf file.
278 size_t oat_data_offset_;
279
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 // dependencies on the image.
281 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800282 uintptr_t image_file_location_oat_begin_;
Alex Lighta59dd802014-07-02 16:28:08 -0700283 int32_t image_patch_delta_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284
285 // data to write
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700286 SafeMap<std::string, std::string>* key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 OatHeader* oat_header_;
288 std::vector<OatDexFile*> oat_dex_files_;
289 std::vector<OatClass*> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700290 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_interpreter_bridge_;
291 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_compiled_code_bridge_;
292 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700293 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
294 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
295 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
296 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297
298 // output stats
299 uint32_t size_dex_file_alignment_;
300 uint32_t size_executable_offset_alignment_;
301 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700302 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700304 uint32_t size_interpreter_to_interpreter_bridge_;
305 uint32_t size_interpreter_to_compiled_code_bridge_;
306 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800307 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700308 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700310 uint32_t size_quick_to_interpreter_bridge_;
311 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100312 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 uint32_t size_code_;
314 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100315 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100316 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 uint32_t size_mapping_table_;
318 uint32_t size_vmap_table_;
319 uint32_t size_gc_map_;
320 uint32_t size_oat_dex_file_location_size_;
321 uint32_t size_oat_dex_file_location_data_;
322 uint32_t size_oat_dex_file_location_checksum_;
323 uint32_t size_oat_dex_file_offset_;
324 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700325 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700327 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 uint32_t size_oat_class_method_offsets_;
329
Vladimir Marko20f85592015-03-19 10:07:02 +0000330 class RelativePatcher;
331 class NoRelativePatcher;
Vladimir Markodc56cc52015-03-27 18:18:36 +0000332 class X86BaseRelativePatcher;
Vladimir Marko20f85592015-03-19 10:07:02 +0000333 class X86RelativePatcher;
Vladimir Markodc56cc52015-03-27 18:18:36 +0000334 class X86_64RelativePatcher;
Vladimir Marko20f85592015-03-19 10:07:02 +0000335 class ArmBaseRelativePatcher;
336 class Thumb2RelativePatcher;
337 class Arm64RelativePatcher;
Vladimir Markof4da6752014-08-01 19:04:18 +0100338
Vladimir Marko20f85592015-03-19 10:07:02 +0000339 std::unique_ptr<RelativePatcher> relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100340
341 // The locations of absolute patches relative to the start of the executable section.
342 std::vector<uintptr_t> absolute_patch_locations_;
343
344 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> method_offset_map_;
345
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 DISALLOW_COPY_AND_ASSIGN(OatWriter);
347};
348
349} // namespace art
350
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700351#endif // ART_COMPILER_OAT_WRITER_H_