blob: 8c79b441538d22194f173d477d9500998253132d [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>
David Srbecky2f6cdb02015-04-11 00:17:53 +010022#include <map>
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
Vladimir Markob163bb72015-03-31 21:49:49 +010025#include "linker/relative_patcher.h" // For linker::RelativePatcherTargetProvider.
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010027#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "oat.h"
29#include "mirror/class.h"
30#include "safe_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031
32namespace art {
33
Brian Carlstromba150c32013-08-27 17:31:03 -070034class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070035class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000036class CompilerDriver;
Vladimir Markof4da6752014-08-01 19:04:18 +010037class ImageWriter;
Brian Carlstrom7940e442013-07-12 13:46:57 -070038class OutputStream;
Vladimir Marko20f85592015-03-19 10:07:02 +000039class TimingLogger;
Brian Carlstrom7940e442013-07-12 13:46:57 -070040
41// OatHeader variable length with count of D OatDexFiles
42//
43// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
44// OatDexFile[1]
45// ...
46// OatDexFile[D]
47//
48// Dex[0] one variable sized DexFile for each OatDexFile.
49// Dex[1] these are literal copies of the input .dex files.
50// ...
51// Dex[D]
52//
53// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
54// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
55// ...
56// OatClass[C]
57//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010058// GcMap one variable sized blob with GC map.
59// GcMap GC maps are deduplicated.
60// ...
61// GcMap
62//
63// VmapTable one variable sized VmapTable blob (quick compiler only).
64// VmapTable VmapTables are deduplicated.
65// ...
66// VmapTable
67//
68// MappingTable one variable sized blob with MappingTable (quick compiler only).
69// MappingTable MappingTables are deduplicated.
70// ...
71// MappingTable
72//
Brian Carlstrom7940e442013-07-12 13:46:57 -070073// padding if necessary so that the following code will be page aligned
74//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010075// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
76// MethodCode one variable sized blob with the code of a CompiledMethod.
77// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
78// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070079// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010080// OatMethodHeader
81// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070082//
83class OatWriter {
84 public:
David Srbecky2f6cdb02015-04-11 00:17:53 +010085 typedef std::map<std::string, std::unique_ptr<std::vector<uintptr_t>>> PatchLocationsMap;
86
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 OatWriter(const std::vector<const DexFile*>& dex_files,
88 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080089 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070090 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080091 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010092 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070093 TimingLogger* timings,
94 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070095
96 const OatHeader& GetOatHeader() const {
97 return *oat_header_;
98 }
99
100 size_t GetSize() const {
101 return size_;
102 }
103
Vladimir Marko5c42c292015-02-25 12:02:49 +0000104 size_t GetBssSize() const {
105 return bss_size_;
106 }
107
David Srbecky2f6cdb02015-04-11 00:17:53 +0100108 const PatchLocationsMap& GetAbsolutePatchLocations() const {
Vladimir Markof4da6752014-08-01 19:04:18 +0100109 return absolute_patch_locations_;
110 }
111
David Srbecky2f6cdb02015-04-11 00:17:53 +0100112 std::vector<uintptr_t>* GetAbsolutePatchLocationsFor(const char* section_name) {
113 auto it = absolute_patch_locations_.emplace(
114 std::string(section_name), std::unique_ptr<std::vector<uintptr_t>>());
115 if (it.second) { // Inserted new item.
116 it.first->second.reset(new std::vector<uintptr_t>());
117 }
118 return it.first->second.get();
119 }
120
Vladimir Markof4da6752014-08-01 19:04:18 +0100121 void SetOatDataOffset(size_t oat_data_offset) {
122 oat_data_offset_ = oat_data_offset;
123 }
124
Ian Rogers3d504072014-03-01 09:16:49 -0800125 bool Write(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700126
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 ~OatWriter();
128
Mark Mendellae9fd932014-02-10 16:14:35 -0800129 struct DebugInfo {
David Srbecky0df9e1f2015-04-07 19:02:58 +0100130 const DexFile* dex_file_;
131 size_t class_def_index_;
132 uint32_t dex_method_index_;
133 uint32_t access_flags_;
134 const DexFile::CodeItem *code_item_;
135 bool deduped_;
136 uint32_t low_pc_;
137 uint32_t high_pc_;
Andreas Gampe79273802014-08-05 20:21:05 -0700138 CompiledMethod* compiled_method_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800139 };
140
David Srbecky3b9d57a2015-04-10 00:22:14 +0100141 const std::vector<DebugInfo>& GetMethodDebugInfo() const {
Mark Mendellae9fd932014-02-10 16:14:35 -0800142 return method_info_;
143 }
144
Vladimir Markob163bb72015-03-31 21:49:49 +0100145 const CompilerDriver* GetCompilerDriver() {
146 return compiler_driver_;
147 }
148
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700149 private:
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100150 // The DataAccess classes are helper classes that provide access to members related to
151 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
152 // we can share a lot of code for processing the maps with template classes below.
153 struct GcMapDataAccess;
154 struct MappingTableDataAccess;
155 struct VmapTableDataAccess;
156
157 // The function VisitDexMethods() below iterates through all the methods in all
158 // the compiled dex files in order of their definitions. The method visitor
159 // classes provide individual bits of processing for each of the passes we need to
160 // first collect the data we want to write to the oat file and then, in later passes,
161 // to actually write it.
162 class DexMethodVisitor;
163 class OatDexMethodVisitor;
164 class InitOatClassesMethodVisitor;
165 class InitCodeMethodVisitor;
166 template <typename DataAccess>
167 class InitMapMethodVisitor;
168 class InitImageMethodVisitor;
169 class WriteCodeMethodVisitor;
170 template <typename DataAccess>
171 class WriteMapMethodVisitor;
172
173 // Visit all the methods in all the compiled dex files in their definition order
174 // with a given DexMethodVisitor.
175 bool VisitDexMethods(DexMethodVisitor* visitor);
176
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 size_t InitOatHeader();
178 size_t InitOatDexFiles(size_t offset);
179 size_t InitDexFiles(size_t offset);
180 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100181 size_t InitOatMaps(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 size_t InitOatCode(size_t offset)
183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184 size_t InitOatCodeDexFiles(size_t offset)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186
Ian Rogers3d504072014-03-01 09:16:49 -0800187 bool WriteTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100188 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
189 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800190 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191
Vladimir Markof4da6752014-08-01 19:04:18 +0100192 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
193
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 class OatDexFile {
195 public:
196 explicit OatDexFile(size_t offset, const DexFile& dex_file);
197 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800198 void UpdateChecksum(OatHeader* oat_header) const;
199 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
201 // Offset of start of OatDexFile from beginning of OatHeader. It is
202 // used to validate file position when writing.
203 size_t offset_;
204
205 // data to write
206 uint32_t dex_file_location_size_;
207 const uint8_t* dex_file_location_data_;
208 uint32_t dex_file_location_checksum_;
209 uint32_t dex_file_offset_;
210 std::vector<uint32_t> methods_offsets_;
211
212 private:
213 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
214 };
215
216 class OatClass {
217 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700218 explicit OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100219 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -0700220 uint32_t num_non_null_compiled_methods,
221 mirror::Class::Status status);
222 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
224 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
225 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800226 void UpdateChecksum(OatHeader* oat_header) const;
227 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228
Brian Carlstromba150c32013-08-27 17:31:03 -0700229 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100230 DCHECK_LT(class_def_method_index, compiled_methods_.size());
231 return compiled_methods_[class_def_method_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700232 }
233
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 // Offset of start of OatClass from beginning of OatHeader. It is
Elliott Hughes956af0f2014-12-11 14:34:28 -0800235 // used to validate file position when writing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 size_t offset_;
237
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700238 // CompiledMethods for each class_def_method_index, or null if no method is available.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100239 std::vector<CompiledMethod*> compiled_methods_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700240
241 // Offset from OatClass::offset_ to the OatMethodOffsets for the
242 // class_def_method_index. If 0, it means the corresponding
243 // CompiledMethod entry in OatClass::compiled_methods_ should be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 // null and that the OatClass::type_ should be kOatClassBitmap.
Brian Carlstromba150c32013-08-27 17:31:03 -0700245 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
246
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700248
Andreas Gampe785d2f22014-11-03 22:57:30 -0800249 static_assert(mirror::Class::Status::kStatusMax < (2 ^ 16), "class status won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700250 int16_t status_;
251
Andreas Gampe785d2f22014-11-03 22:57:30 -0800252 static_assert(OatClassType::kOatClassMax < (2 ^ 16), "oat_class type won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700253 uint16_t type_;
254
255 uint32_t method_bitmap_size_;
256
257 // bit vector indexed by ClassDef method index. When
258 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
259 // method has an OatMethodOffsets in methods_offsets_, otherwise
260 // the entry was ommited to save space. If OatClassType::type_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700261 // not is kOatClassBitmap, the bitmap will be null.
Brian Carlstromba150c32013-08-27 17:31:03 -0700262 BitVector* method_bitmap_;
263
Vladimir Marko8a630572014-04-09 18:45:35 +0100264 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
265 // present in the OatClass. Note that some may be missing if
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700266 // OatClass::compiled_methods_ contains null values (and
Brian Carlstromba150c32013-08-27 17:31:03 -0700267 // oat_method_offsets_offsets_from_oat_class_ should contain 0
268 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 std::vector<OatMethodOffsets> method_offsets_;
Vladimir Marko7624d252014-05-02 14:40:15 +0100270 std::vector<OatQuickMethodHeader> method_headers_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271
272 private:
273 DISALLOW_COPY_AND_ASSIGN(OatClass);
274 };
275
Mark Mendellae9fd932014-02-10 16:14:35 -0800276 std::vector<DebugInfo> method_info_;
277
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 const CompilerDriver* const compiler_driver_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100279 ImageWriter* const image_writer_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280
281 // note OatFile does not take ownership of the DexFiles
282 const std::vector<const DexFile*>* dex_files_;
283
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700284 // Size required for Oat data structures.
285 size_t size_;
286
Vladimir Marko5c42c292015-02-25 12:02:49 +0000287 // The size of the required .bss section holding the DexCache data.
288 size_t bss_size_;
289
Vladimir Markof4da6752014-08-01 19:04:18 +0100290 // Offset of the oat data from the start of the mmapped region of the elf file.
291 size_t oat_data_offset_;
292
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 // dependencies on the image.
294 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 uintptr_t image_file_location_oat_begin_;
Alex Lighta59dd802014-07-02 16:28:08 -0700296 int32_t image_patch_delta_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297
298 // data to write
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700299 SafeMap<std::string, std::string>* key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 OatHeader* oat_header_;
301 std::vector<OatDexFile*> oat_dex_files_;
302 std::vector<OatClass*> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700303 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_interpreter_bridge_;
304 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_compiled_code_bridge_;
305 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700306 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
307 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
308 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
309 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310
311 // output stats
312 uint32_t size_dex_file_alignment_;
313 uint32_t size_executable_offset_alignment_;
314 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700315 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700317 uint32_t size_interpreter_to_interpreter_bridge_;
318 uint32_t size_interpreter_to_compiled_code_bridge_;
319 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800320 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700321 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700323 uint32_t size_quick_to_interpreter_bridge_;
324 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100325 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 uint32_t size_code_;
327 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100328 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100329 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 uint32_t size_mapping_table_;
331 uint32_t size_vmap_table_;
332 uint32_t size_gc_map_;
333 uint32_t size_oat_dex_file_location_size_;
334 uint32_t size_oat_dex_file_location_data_;
335 uint32_t size_oat_dex_file_location_checksum_;
336 uint32_t size_oat_dex_file_offset_;
337 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700338 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700340 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 uint32_t size_oat_class_method_offsets_;
342
Vladimir Markob163bb72015-03-31 21:49:49 +0100343 std::unique_ptr<linker::RelativePatcher> relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100344
David Srbecky2f6cdb02015-04-11 00:17:53 +0100345 // The locations of absolute patches relative to the start of section.
346 // The map's key is the ELF's section name (including the dot).
347 PatchLocationsMap absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100348
Vladimir Markob163bb72015-03-31 21:49:49 +0100349 // Map method reference to assigned offset.
350 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
351 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
352 public:
353 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE;
354 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
355 };
356 MethodOffsetMap method_offset_map_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100357
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 DISALLOW_COPY_AND_ASSIGN(OatWriter);
359};
360
361} // namespace art
362
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700363#endif // ART_COMPILER_OAT_WRITER_H_