1 /*
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 
17 #ifndef ART_COMPILER_OAT_WRITER_H_
18 #define ART_COMPILER_OAT_WRITER_H_
19 
20 #include <stdint.h>
21 #include <cstddef>
22 #include <memory>
23 
24 #include "linker/relative_patcher.h"  // For linker::RelativePatcherTargetProvider.
25 #include "mem_map.h"
26 #include "method_reference.h"
27 #include "oat.h"
28 #include "mirror/class.h"
29 #include "safe_map.h"
30 
31 namespace art {
32 
33 class BitVector;
34 class CompiledMethod;
35 class CompilerDriver;
36 class ImageWriter;
37 class OutputStream;
38 class TimingLogger;
39 
40 // OatHeader         variable length with count of D OatDexFiles
41 //
42 // OatDexFile[0]     one variable sized OatDexFile with offsets to Dex and OatClasses
43 // OatDexFile[1]
44 // ...
45 // OatDexFile[D]
46 //
47 // Dex[0]            one variable sized DexFile for each OatDexFile.
48 // Dex[1]            these are literal copies of the input .dex files.
49 // ...
50 // Dex[D]
51 //
52 // OatClass[0]       one variable sized OatClass for each of C DexFile::ClassDefs
53 // OatClass[1]       contains OatClass entries with class status, offsets to code, etc.
54 // ...
55 // OatClass[C]
56 //
57 // GcMap             one variable sized blob with GC map.
58 // GcMap             GC maps are deduplicated.
59 // ...
60 // GcMap
61 //
62 // VmapTable         one variable sized VmapTable blob (quick compiler only).
63 // VmapTable         VmapTables are deduplicated.
64 // ...
65 // VmapTable
66 //
67 // MappingTable      one variable sized blob with MappingTable (quick compiler only).
68 // MappingTable      MappingTables are deduplicated.
69 // ...
70 // MappingTable
71 //
72 // padding           if necessary so that the following code will be page aligned
73 //
74 // OatMethodHeader   fixed size header for a CompiledMethod including the size of the MethodCode.
75 // MethodCode        one variable sized blob with the code of a CompiledMethod.
76 // OatMethodHeader   (OatMethodHeader, MethodCode) pairs are deduplicated.
77 // MethodCode
78 // ...
79 // OatMethodHeader
80 // MethodCode
81 //
82 class OatWriter {
83  public:
84   OatWriter(const std::vector<const DexFile*>& dex_files,
85             uint32_t image_file_location_oat_checksum,
86             uintptr_t image_file_location_oat_begin,
87             int32_t image_patch_delta,
88             const CompilerDriver* compiler,
89             ImageWriter* image_writer,
90             TimingLogger* timings,
91             SafeMap<std::string, std::string>* key_value_store);
92 
GetOatHeader()93   const OatHeader& GetOatHeader() const {
94     return *oat_header_;
95   }
96 
GetSize()97   size_t GetSize() const {
98     return size_;
99   }
100 
GetBssSize()101   size_t GetBssSize() const {
102     return bss_size_;
103   }
104 
GetAbsolutePatchLocations()105   const std::vector<uintptr_t>& GetAbsolutePatchLocations() const {
106     return absolute_patch_locations_;
107   }
108 
109   bool WriteRodata(OutputStream* out);
110   bool WriteCode(OutputStream* out);
111 
112   ~OatWriter();
113 
114   struct DebugInfo {
115     const DexFile* dex_file_;
116     size_t class_def_index_;
117     uint32_t dex_method_index_;
118     uint32_t access_flags_;
119     const DexFile::CodeItem *code_item_;
120     bool deduped_;
121     uint32_t low_pc_;
122     uint32_t high_pc_;
123     CompiledMethod* compiled_method_;
124   };
125 
GetMethodDebugInfo()126   const std::vector<DebugInfo>& GetMethodDebugInfo() const {
127     return method_info_;
128   }
129 
GetCompilerDriver()130   const CompilerDriver* GetCompilerDriver() {
131     return compiler_driver_;
132   }
133 
134  private:
135   // The DataAccess classes are helper classes that provide access to members related to
136   // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
137   // we can share a lot of code for processing the maps with template classes below.
138   struct GcMapDataAccess;
139   struct MappingTableDataAccess;
140   struct VmapTableDataAccess;
141 
142   // The function VisitDexMethods() below iterates through all the methods in all
143   // the compiled dex files in order of their definitions. The method visitor
144   // classes provide individual bits of processing for each of the passes we need to
145   // first collect the data we want to write to the oat file and then, in later passes,
146   // to actually write it.
147   class DexMethodVisitor;
148   class OatDexMethodVisitor;
149   class InitOatClassesMethodVisitor;
150   class InitCodeMethodVisitor;
151   template <typename DataAccess>
152   class InitMapMethodVisitor;
153   class InitImageMethodVisitor;
154   class WriteCodeMethodVisitor;
155   template <typename DataAccess>
156   class WriteMapMethodVisitor;
157 
158   // Visit all the methods in all the compiled dex files in their definition order
159   // with a given DexMethodVisitor.
160   bool VisitDexMethods(DexMethodVisitor* visitor);
161 
162   size_t InitOatHeader();
163   size_t InitOatDexFiles(size_t offset);
164   size_t InitDexFiles(size_t offset);
165   size_t InitOatClasses(size_t offset);
166   size_t InitOatMaps(size_t offset);
167   size_t InitOatCode(size_t offset)
168       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169   size_t InitOatCodeDexFiles(size_t offset)
170       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
171 
172   bool WriteTables(OutputStream* out, const size_t file_offset);
173   size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
174   size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
175   size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
176 
177   bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
178 
179   class OatDexFile {
180    public:
181     explicit OatDexFile(size_t offset, const DexFile& dex_file);
182     size_t SizeOf() const;
183     void UpdateChecksum(OatHeader* oat_header) const;
184     bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
185 
186     // Offset of start of OatDexFile from beginning of OatHeader. It is
187     // used to validate file position when writing.
188     size_t offset_;
189 
190     // data to write
191     uint32_t dex_file_location_size_;
192     const uint8_t* dex_file_location_data_;
193     uint32_t dex_file_location_checksum_;
194     uint32_t dex_file_offset_;
195     std::vector<uint32_t> methods_offsets_;
196 
197    private:
198     DISALLOW_COPY_AND_ASSIGN(OatDexFile);
199   };
200 
201   class OatClass {
202    public:
203     explicit OatClass(size_t offset,
204                       const std::vector<CompiledMethod*>& compiled_methods,
205                       uint32_t num_non_null_compiled_methods,
206                       mirror::Class::Status status);
207     ~OatClass();
208     size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
209     size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
210     size_t SizeOf() const;
211     void UpdateChecksum(OatHeader* oat_header) const;
212     bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
213 
GetCompiledMethod(size_t class_def_method_index)214     CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
215       DCHECK_LT(class_def_method_index, compiled_methods_.size());
216       return compiled_methods_[class_def_method_index];
217     }
218 
219     // Offset of start of OatClass from beginning of OatHeader. It is
220     // used to validate file position when writing.
221     size_t offset_;
222 
223     // CompiledMethods for each class_def_method_index, or null if no method is available.
224     std::vector<CompiledMethod*> compiled_methods_;
225 
226     // Offset from OatClass::offset_ to the OatMethodOffsets for the
227     // class_def_method_index. If 0, it means the corresponding
228     // CompiledMethod entry in OatClass::compiled_methods_ should be
229     // null and that the OatClass::type_ should be kOatClassBitmap.
230     std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
231 
232     // data to write
233 
234     static_assert(mirror::Class::Status::kStatusMax < (2 ^ 16), "class status won't fit in 16bits");
235     int16_t status_;
236 
237     static_assert(OatClassType::kOatClassMax < (2 ^ 16), "oat_class type won't fit in 16bits");
238     uint16_t type_;
239 
240     uint32_t method_bitmap_size_;
241 
242     // bit vector indexed by ClassDef method index. When
243     // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
244     // method has an OatMethodOffsets in methods_offsets_, otherwise
245     // the entry was ommited to save space. If OatClassType::type_ is
246     // not is kOatClassBitmap, the bitmap will be null.
247     BitVector* method_bitmap_;
248 
249     // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
250     // present in the OatClass. Note that some may be missing if
251     // OatClass::compiled_methods_ contains null values (and
252     // oat_method_offsets_offsets_from_oat_class_ should contain 0
253     // values in this case).
254     std::vector<OatMethodOffsets> method_offsets_;
255     std::vector<OatQuickMethodHeader> method_headers_;
256 
257    private:
258     DISALLOW_COPY_AND_ASSIGN(OatClass);
259   };
260 
261   std::vector<DebugInfo> method_info_;
262 
263   const CompilerDriver* const compiler_driver_;
264   ImageWriter* const image_writer_;
265 
266   // note OatFile does not take ownership of the DexFiles
267   const std::vector<const DexFile*>* dex_files_;
268 
269   // Size required for Oat data structures.
270   size_t size_;
271 
272   // The size of the required .bss section holding the DexCache data.
273   size_t bss_size_;
274 
275   // Offset of the oat data from the start of the mmapped region of the elf file.
276   size_t oat_data_offset_;
277 
278   // dependencies on the image.
279   uint32_t image_file_location_oat_checksum_;
280   uintptr_t image_file_location_oat_begin_;
281   int32_t image_patch_delta_;
282 
283   // data to write
284   SafeMap<std::string, std::string>* key_value_store_;
285   OatHeader* oat_header_;
286   std::vector<OatDexFile*> oat_dex_files_;
287   std::vector<OatClass*> oat_classes_;
288   std::unique_ptr<const std::vector<uint8_t>> interpreter_to_interpreter_bridge_;
289   std::unique_ptr<const std::vector<uint8_t>> interpreter_to_compiled_code_bridge_;
290   std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
291   std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
292   std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
293   std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
294   std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
295 
296   // output stats
297   uint32_t size_dex_file_alignment_;
298   uint32_t size_executable_offset_alignment_;
299   uint32_t size_oat_header_;
300   uint32_t size_oat_header_key_value_store_;
301   uint32_t size_dex_file_;
302   uint32_t size_interpreter_to_interpreter_bridge_;
303   uint32_t size_interpreter_to_compiled_code_bridge_;
304   uint32_t size_jni_dlsym_lookup_;
305   uint32_t size_quick_generic_jni_trampoline_;
306   uint32_t size_quick_imt_conflict_trampoline_;
307   uint32_t size_quick_resolution_trampoline_;
308   uint32_t size_quick_to_interpreter_bridge_;
309   uint32_t size_trampoline_alignment_;
310   uint32_t size_method_header_;
311   uint32_t size_code_;
312   uint32_t size_code_alignment_;
313   uint32_t size_relative_call_thunks_;
314   uint32_t size_misc_thunks_;
315   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_;
323   uint32_t size_oat_class_type_;
324   uint32_t size_oat_class_status_;
325   uint32_t size_oat_class_method_bitmaps_;
326   uint32_t size_oat_class_method_offsets_;
327 
328   std::unique_ptr<linker::RelativePatcher> relative_patcher_;
329 
330   // The locations of absolute patches relative to the start of the executable section.
331   std::vector<uintptr_t> absolute_patch_locations_;
332 
333   // Map method reference to assigned offset.
334   // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
335   class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
336    public:
337     std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE;
338     SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
339   };
340   MethodOffsetMap method_offset_map_;
341 
342   DISALLOW_COPY_AND_ASSIGN(OatWriter);
343 };
344 
345 }  // namespace art
346 
347 #endif  // ART_COMPILER_OAT_WRITER_H_
348