blob: fd2ccae4a51643cc2b62596c6c6ee5aad123abdb [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
24#include "driver/compiler_driver.h"
25#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010026#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "oat.h"
28#include "mirror/class.h"
29#include "safe_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
31namespace art {
32
Brian Carlstromba150c32013-08-27 17:31:03 -070033class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070034class CompiledMethod;
Vladimir Markof4da6752014-08-01 19:04:18 +010035class ImageWriter;
Brian Carlstrom7940e442013-07-12 13:46:57 -070036class OutputStream;
37
38// OatHeader variable length with count of D OatDexFiles
39//
40// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
41// OatDexFile[1]
42// ...
43// OatDexFile[D]
44//
45// Dex[0] one variable sized DexFile for each OatDexFile.
46// Dex[1] these are literal copies of the input .dex files.
47// ...
48// Dex[D]
49//
50// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
51// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
52// ...
53// OatClass[C]
54//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010055// GcMap one variable sized blob with GC map.
56// GcMap GC maps are deduplicated.
57// ...
58// GcMap
59//
60// VmapTable one variable sized VmapTable blob (quick compiler only).
61// VmapTable VmapTables are deduplicated.
62// ...
63// VmapTable
64//
65// MappingTable one variable sized blob with MappingTable (quick compiler only).
66// MappingTable MappingTables are deduplicated.
67// ...
68// MappingTable
69//
Brian Carlstrom7940e442013-07-12 13:46:57 -070070// padding if necessary so that the following code will be page aligned
71//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010072// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
73// MethodCode one variable sized blob with the code of a CompiledMethod.
74// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
75// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070076// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010077// OatMethodHeader
78// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070079//
80class OatWriter {
81 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 OatWriter(const std::vector<const DexFile*>& dex_files,
83 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070085 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080086 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010087 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070088 TimingLogger* timings,
89 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070090
91 const OatHeader& GetOatHeader() const {
92 return *oat_header_;
93 }
94
95 size_t GetSize() const {
96 return size_;
97 }
98
Vladimir Marko5c42c292015-02-25 12:02:49 +000099 size_t GetBssSize() const {
100 return bss_size_;
101 }
102
Vladimir Markof4da6752014-08-01 19:04:18 +0100103 const std::vector<uintptr_t>& GetAbsolutePatchLocations() const {
104 return absolute_patch_locations_;
105 }
106
107 void SetOatDataOffset(size_t oat_data_offset) {
108 oat_data_offset_ = oat_data_offset;
109 }
110
Ian Rogers3d504072014-03-01 09:16:49 -0800111 bool Write(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700112
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 ~OatWriter();
114
Mark Mendellae9fd932014-02-10 16:14:35 -0800115 struct DebugInfo {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700116 DebugInfo(const std::string& method_name, const char* src_file_name,
117 uint32_t low_pc, uint32_t high_pc, const uint8_t* dbgstream,
Andreas Gampe79273802014-08-05 20:21:05 -0700118 CompiledMethod* compiled_method)
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700119 : method_name_(method_name), src_file_name_(src_file_name),
120 low_pc_(low_pc), high_pc_(high_pc), dbgstream_(dbgstream),
Andreas Gampe79273802014-08-05 20:21:05 -0700121 compiled_method_(compiled_method) {
Mark Mendellae9fd932014-02-10 16:14:35 -0800122 }
Andreas Gampe79273802014-08-05 20:21:05 -0700123 std::string method_name_; // Note: this name is a pretty-printed name.
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700124 const char* src_file_name_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800125 uint32_t low_pc_;
126 uint32_t high_pc_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700127 const uint8_t* dbgstream_;
Andreas Gampe79273802014-08-05 20:21:05 -0700128 CompiledMethod* compiled_method_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800129 };
130
131 const std::vector<DebugInfo>& GetCFIMethodInfo() const {
132 return method_info_;
133 }
134
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700135 private:
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100136 // The DataAccess classes are helper classes that provide access to members related to
137 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
138 // we can share a lot of code for processing the maps with template classes below.
139 struct GcMapDataAccess;
140 struct MappingTableDataAccess;
141 struct VmapTableDataAccess;
142
143 // The function VisitDexMethods() below iterates through all the methods in all
144 // the compiled dex files in order of their definitions. The method visitor
145 // classes provide individual bits of processing for each of the passes we need to
146 // first collect the data we want to write to the oat file and then, in later passes,
147 // to actually write it.
148 class DexMethodVisitor;
149 class OatDexMethodVisitor;
150 class InitOatClassesMethodVisitor;
151 class InitCodeMethodVisitor;
152 template <typename DataAccess>
153 class InitMapMethodVisitor;
154 class InitImageMethodVisitor;
155 class WriteCodeMethodVisitor;
156 template <typename DataAccess>
157 class WriteMapMethodVisitor;
158
159 // Visit all the methods in all the compiled dex files in their definition order
160 // with a given DexMethodVisitor.
161 bool VisitDexMethods(DexMethodVisitor* visitor);
162
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 size_t InitOatHeader();
164 size_t InitOatDexFiles(size_t offset);
165 size_t InitDexFiles(size_t offset);
166 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100167 size_t InitOatMaps(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 size_t InitOatCode(size_t offset)
169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170 size_t InitOatCodeDexFiles(size_t offset)
171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172
Ian Rogers3d504072014-03-01 09:16:49 -0800173 bool WriteTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100174 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
175 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800176 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177
Vladimir Markof4da6752014-08-01 19:04:18 +0100178 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
179
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 class OatDexFile {
181 public:
182 explicit OatDexFile(size_t offset, const DexFile& dex_file);
183 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800184 void UpdateChecksum(OatHeader* oat_header) const;
185 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186
187 // Offset of start of OatDexFile from beginning of OatHeader. It is
188 // used to validate file position when writing.
189 size_t offset_;
190
191 // data to write
192 uint32_t dex_file_location_size_;
193 const uint8_t* dex_file_location_data_;
194 uint32_t dex_file_location_checksum_;
195 uint32_t dex_file_offset_;
196 std::vector<uint32_t> methods_offsets_;
197
198 private:
199 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
200 };
201
202 class OatClass {
203 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700204 explicit OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100205 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -0700206 uint32_t num_non_null_compiled_methods,
207 mirror::Class::Status status);
208 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
210 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
211 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800212 void UpdateChecksum(OatHeader* oat_header) const;
213 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214
Brian Carlstromba150c32013-08-27 17:31:03 -0700215 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100216 DCHECK_LT(class_def_method_index, compiled_methods_.size());
217 return compiled_methods_[class_def_method_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700218 }
219
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 // Offset of start of OatClass from beginning of OatHeader. It is
Elliott Hughes956af0f2014-12-11 14:34:28 -0800221 // used to validate file position when writing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 size_t offset_;
223
Brian Carlstromba150c32013-08-27 17:31:03 -0700224 // CompiledMethods for each class_def_method_index, or NULL if no method is available.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100225 std::vector<CompiledMethod*> compiled_methods_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700226
227 // Offset from OatClass::offset_ to the OatMethodOffsets for the
228 // class_def_method_index. If 0, it means the corresponding
229 // CompiledMethod entry in OatClass::compiled_methods_ should be
230 // NULL and that the OatClass::type_ should be kOatClassBitmap.
231 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
232
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700234
Andreas Gampe785d2f22014-11-03 22:57:30 -0800235 static_assert(mirror::Class::Status::kStatusMax < (2 ^ 16), "class status won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700236 int16_t status_;
237
Andreas Gampe785d2f22014-11-03 22:57:30 -0800238 static_assert(OatClassType::kOatClassMax < (2 ^ 16), "oat_class type won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700239 uint16_t type_;
240
241 uint32_t method_bitmap_size_;
242
243 // bit vector indexed by ClassDef method index. When
244 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
245 // method has an OatMethodOffsets in methods_offsets_, otherwise
246 // the entry was ommited to save space. If OatClassType::type_ is
247 // not is kOatClassBitmap, the bitmap will be NULL.
248 BitVector* method_bitmap_;
249
Vladimir Marko8a630572014-04-09 18:45:35 +0100250 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
251 // present in the OatClass. Note that some may be missing if
Brian Carlstromba150c32013-08-27 17:31:03 -0700252 // OatClass::compiled_methods_ contains NULL values (and
253 // oat_method_offsets_offsets_from_oat_class_ should contain 0
254 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 std::vector<OatMethodOffsets> method_offsets_;
Vladimir Marko7624d252014-05-02 14:40:15 +0100256 std::vector<OatQuickMethodHeader> method_headers_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257
258 private:
259 DISALLOW_COPY_AND_ASSIGN(OatClass);
260 };
261
Mark Mendellae9fd932014-02-10 16:14:35 -0800262 std::vector<DebugInfo> method_info_;
263
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 const CompilerDriver* const compiler_driver_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100265 ImageWriter* const image_writer_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266
267 // note OatFile does not take ownership of the DexFiles
268 const std::vector<const DexFile*>* dex_files_;
269
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700270 // Size required for Oat data structures.
271 size_t size_;
272
Vladimir Marko5c42c292015-02-25 12:02:49 +0000273 // The size of the required .bss section holding the DexCache data.
274 size_t bss_size_;
275
Vladimir Markof4da6752014-08-01 19:04:18 +0100276 // Offset of the oat data from the start of the mmapped region of the elf file.
277 size_t oat_data_offset_;
278
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 // dependencies on the image.
280 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800281 uintptr_t image_file_location_oat_begin_;
Alex Lighta59dd802014-07-02 16:28:08 -0700282 int32_t image_patch_delta_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283
284 // data to write
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700285 SafeMap<std::string, std::string>* key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 OatHeader* oat_header_;
287 std::vector<OatDexFile*> oat_dex_files_;
288 std::vector<OatClass*> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700289 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_interpreter_bridge_;
290 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_compiled_code_bridge_;
291 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700292 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
293 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
294 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
295 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296
297 // output stats
298 uint32_t size_dex_file_alignment_;
299 uint32_t size_executable_offset_alignment_;
300 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700301 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700303 uint32_t size_interpreter_to_interpreter_bridge_;
304 uint32_t size_interpreter_to_compiled_code_bridge_;
305 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800306 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700307 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700309 uint32_t size_quick_to_interpreter_bridge_;
310 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100311 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 uint32_t size_code_;
313 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100314 uint32_t size_relative_call_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 uint32_t size_mapping_table_;
316 uint32_t size_vmap_table_;
317 uint32_t size_gc_map_;
318 uint32_t size_oat_dex_file_location_size_;
319 uint32_t size_oat_dex_file_location_data_;
320 uint32_t size_oat_dex_file_location_checksum_;
321 uint32_t size_oat_dex_file_offset_;
322 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700323 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700325 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 uint32_t size_oat_class_method_offsets_;
327
Vladimir Markof4da6752014-08-01 19:04:18 +0100328 class RelativeCallPatcher;
329 class NoRelativeCallPatcher;
330 class X86RelativeCallPatcher;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100331 class ArmBaseRelativeCallPatcher;
Vladimir Markof4da6752014-08-01 19:04:18 +0100332 class Thumb2RelativeCallPatcher;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100333 class Arm64RelativeCallPatcher;
Vladimir Markof4da6752014-08-01 19:04:18 +0100334
335 std::unique_ptr<RelativeCallPatcher> relative_call_patcher_;
336
337 // The locations of absolute patches relative to the start of the executable section.
338 std::vector<uintptr_t> absolute_patch_locations_;
339
340 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> method_offset_map_;
341
Vladimir Marko8a630572014-04-09 18:45:35 +0100342 struct CodeOffsetsKeyComparator {
343 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
344 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
345 return lhs->GetQuickCode() < rhs->GetQuickCode();
346 }
347 // If the code is the same, all other fields are likely to be the same as well.
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000348 if (UNLIKELY(lhs->GetMappingTable() != rhs->GetMappingTable())) {
349 return lhs->GetMappingTable() < rhs->GetMappingTable();
Vladimir Marko8a630572014-04-09 18:45:35 +0100350 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800351 if (UNLIKELY(lhs->GetVmapTable() != rhs->GetVmapTable())) {
352 return lhs->GetVmapTable() < rhs->GetVmapTable();
Vladimir Marko8a630572014-04-09 18:45:35 +0100353 }
Mathieu Chartier31158772014-11-25 11:20:28 -0800354 if (UNLIKELY(lhs->GetGcMap() != rhs->GetGcMap())) {
355 return lhs->GetGcMap() < rhs->GetGcMap();
356 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100357 const auto& lhs_patches = lhs->GetPatches();
358 const auto& rhs_patches = rhs->GetPatches();
359 if (UNLIKELY(lhs_patches.size() != rhs_patches.size())) {
360 return lhs_patches.size() < rhs_patches.size();
361 }
362 auto rit = rhs_patches.begin();
363 for (const LinkerPatch& lpatch : lhs_patches) {
364 if (UNLIKELY(!(lpatch == *rit))) {
365 return lpatch < *rit;
366 }
367 ++rit;
368 }
Vladimir Marko8a630572014-04-09 18:45:35 +0100369 return false;
370 }
371 };
372
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 DISALLOW_COPY_AND_ASSIGN(OatWriter);
374};
375
376} // namespace art
377
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700378#endif // ART_COMPILER_OAT_WRITER_H_