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