Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_OAT_WRITER_H_ |
| 18 | #define ART_COMPILER_OAT_WRITER_H_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <cstddef> |
| 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 | #include "UniquePtr.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 33 | class BitVector; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | class OutputStream; |
| 35 | |
| 36 | // OatHeader variable length with count of D OatDexFiles |
| 37 | // |
| 38 | // OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses |
| 39 | // OatDexFile[1] |
| 40 | // ... |
| 41 | // OatDexFile[D] |
| 42 | // |
| 43 | // Dex[0] one variable sized DexFile for each OatDexFile. |
| 44 | // Dex[1] these are literal copies of the input .dex files. |
| 45 | // ... |
| 46 | // Dex[D] |
| 47 | // |
| 48 | // OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs |
| 49 | // OatClass[1] contains OatClass entries with class status, offsets to code, etc. |
| 50 | // ... |
| 51 | // OatClass[C] |
| 52 | // |
| 53 | // padding if necessary so that the following code will be page aligned |
| 54 | // |
| 55 | // CompiledMethod one variable sized blob with the contents of each CompiledMethod |
| 56 | // CompiledMethod |
| 57 | // CompiledMethod |
| 58 | // CompiledMethod |
| 59 | // CompiledMethod |
| 60 | // CompiledMethod |
| 61 | // ... |
| 62 | // CompiledMethod |
| 63 | // |
| 64 | class OatWriter { |
| 65 | public: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 66 | OatWriter(const std::vector<const DexFile*>& dex_files, |
| 67 | uint32_t image_file_location_oat_checksum, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 68 | uintptr_t image_file_location_oat_begin, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 69 | const std::string& image_file_location, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 70 | const CompilerDriver* compiler, |
| 71 | TimingLogger* timings); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 72 | |
| 73 | const OatHeader& GetOatHeader() const { |
| 74 | return *oat_header_; |
| 75 | } |
| 76 | |
| 77 | size_t GetSize() const { |
| 78 | return size_; |
| 79 | } |
| 80 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 81 | bool Write(OutputStream* out); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 82 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 83 | ~OatWriter(); |
| 84 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 85 | struct DebugInfo { |
| 86 | DebugInfo(const std::string& method_name, uint32_t low_pc, uint32_t high_pc) |
| 87 | : method_name_(method_name), low_pc_(low_pc), high_pc_(high_pc) { |
| 88 | } |
| 89 | std::string method_name_; |
| 90 | uint32_t low_pc_; |
| 91 | uint32_t high_pc_; |
| 92 | }; |
| 93 | |
| 94 | const std::vector<DebugInfo>& GetCFIMethodInfo() const { |
| 95 | return method_info_; |
| 96 | } |
| 97 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 98 | private: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 99 | size_t InitOatHeader(); |
| 100 | size_t InitOatDexFiles(size_t offset); |
| 101 | size_t InitDexFiles(size_t offset); |
| 102 | size_t InitOatClasses(size_t offset); |
| 103 | size_t InitOatCode(size_t offset) |
| 104 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 105 | size_t InitOatCodeDexFiles(size_t offset) |
| 106 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 107 | size_t InitOatCodeDexFile(size_t offset, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 108 | size_t* oat_class_index, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 109 | const DexFile& dex_file) |
| 110 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 111 | size_t InitOatCodeClassDef(size_t offset, |
| 112 | size_t oat_class_index, size_t class_def_index, |
| 113 | const DexFile& dex_file, |
| 114 | const DexFile::ClassDef& class_def) |
| 115 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 116 | size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 117 | size_t class_def_method_index, size_t* method_offsets_index, |
| 118 | bool is_native, InvokeType type, uint32_t method_idx, const DexFile&) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 119 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 120 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 121 | bool WriteTables(OutputStream* out, const size_t file_offset); |
| 122 | size_t WriteCode(OutputStream* out, const size_t file_offset); |
| 123 | size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset); |
| 124 | size_t WriteCodeDexFile(OutputStream* out, const size_t file_offset, size_t relative_offset, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 125 | size_t* oat_class_index, const DexFile& dex_file); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 126 | size_t WriteCodeClassDef(OutputStream* out, const size_t file_offset, size_t relative_offset, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 127 | size_t oat_class_index, const DexFile& dex_file, |
| 128 | const DexFile::ClassDef& class_def); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 129 | size_t WriteCodeMethod(OutputStream* out, const size_t file_offset, size_t relative_offset, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 130 | size_t oat_class_index, size_t class_def_method_index, |
| 131 | size_t* method_offsets_index, bool is_static, uint32_t method_idx, |
| 132 | const DexFile& dex_file); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 133 | |
| 134 | void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 135 | const OutputStream& out) const; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 136 | |
| 137 | class OatDexFile { |
| 138 | public: |
| 139 | explicit OatDexFile(size_t offset, const DexFile& dex_file); |
| 140 | size_t SizeOf() const; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 141 | void UpdateChecksum(OatHeader* oat_header) const; |
| 142 | bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 143 | |
| 144 | // Offset of start of OatDexFile from beginning of OatHeader. It is |
| 145 | // used to validate file position when writing. |
| 146 | size_t offset_; |
| 147 | |
| 148 | // data to write |
| 149 | uint32_t dex_file_location_size_; |
| 150 | const uint8_t* dex_file_location_data_; |
| 151 | uint32_t dex_file_location_checksum_; |
| 152 | uint32_t dex_file_offset_; |
| 153 | std::vector<uint32_t> methods_offsets_; |
| 154 | |
| 155 | private: |
| 156 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 157 | }; |
| 158 | |
| 159 | class OatClass { |
| 160 | public: |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 161 | explicit OatClass(size_t offset, |
| 162 | std::vector<CompiledMethod*>* compiled_methods, |
| 163 | uint32_t num_non_null_compiled_methods, |
| 164 | mirror::Class::Status status); |
| 165 | ~OatClass(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 166 | size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const; |
| 167 | size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const; |
| 168 | size_t SizeOf() const; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 169 | void UpdateChecksum(OatHeader* oat_header) const; |
| 170 | bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 171 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 172 | CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const { |
| 173 | DCHECK(compiled_methods_ != NULL); |
| 174 | return (*compiled_methods_)[class_def_method_index]; |
| 175 | } |
| 176 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 177 | // Offset of start of OatClass from beginning of OatHeader. It is |
| 178 | // used to validate file position when writing. For Portable, it |
| 179 | // is also used to calculate the position of the OatMethodOffsets |
| 180 | // so that code pointers within the OatMethodOffsets can be |
| 181 | // patched to point to code in the Portable .o ELF objects. |
| 182 | size_t offset_; |
| 183 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 184 | // CompiledMethods for each class_def_method_index, or NULL if no method is available. |
| 185 | std::vector<CompiledMethod*>* compiled_methods_; |
| 186 | |
| 187 | // Offset from OatClass::offset_ to the OatMethodOffsets for the |
| 188 | // class_def_method_index. If 0, it means the corresponding |
| 189 | // CompiledMethod entry in OatClass::compiled_methods_ should be |
| 190 | // NULL and that the OatClass::type_ should be kOatClassBitmap. |
| 191 | std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_; |
| 192 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 193 | // data to write |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 194 | |
| 195 | COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits); |
| 196 | int16_t status_; |
| 197 | |
| 198 | COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits); |
| 199 | uint16_t type_; |
| 200 | |
| 201 | uint32_t method_bitmap_size_; |
| 202 | |
| 203 | // bit vector indexed by ClassDef method index. When |
| 204 | // OatClassType::type_ is kOatClassBitmap, a set bit indicates the |
| 205 | // method has an OatMethodOffsets in methods_offsets_, otherwise |
| 206 | // the entry was ommited to save space. If OatClassType::type_ is |
| 207 | // not is kOatClassBitmap, the bitmap will be NULL. |
| 208 | BitVector* method_bitmap_; |
| 209 | |
| 210 | // OatMethodOffsets for each CompiledMethod present in the |
| 211 | // OatClass. Note that some may be missing if |
| 212 | // OatClass::compiled_methods_ contains NULL values (and |
| 213 | // oat_method_offsets_offsets_from_oat_class_ should contain 0 |
| 214 | // values in this case). |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 215 | std::vector<OatMethodOffsets> method_offsets_; |
| 216 | |
| 217 | private: |
| 218 | DISALLOW_COPY_AND_ASSIGN(OatClass); |
| 219 | }; |
| 220 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 221 | std::vector<DebugInfo> method_info_; |
| 222 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 223 | const CompilerDriver* const compiler_driver_; |
| 224 | |
| 225 | // note OatFile does not take ownership of the DexFiles |
| 226 | const std::vector<const DexFile*>* dex_files_; |
| 227 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 228 | // Size required for Oat data structures. |
| 229 | size_t size_; |
| 230 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 231 | // dependencies on the image. |
| 232 | uint32_t image_file_location_oat_checksum_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 233 | uintptr_t image_file_location_oat_begin_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | std::string image_file_location_; |
| 235 | |
| 236 | // data to write |
| 237 | OatHeader* oat_header_; |
| 238 | std::vector<OatDexFile*> oat_dex_files_; |
| 239 | std::vector<OatClass*> oat_classes_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 240 | UniquePtr<const std::vector<uint8_t> > interpreter_to_interpreter_bridge_; |
| 241 | UniquePtr<const std::vector<uint8_t> > interpreter_to_compiled_code_bridge_; |
| 242 | UniquePtr<const std::vector<uint8_t> > jni_dlsym_lookup_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 243 | UniquePtr<const std::vector<uint8_t> > portable_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 244 | UniquePtr<const std::vector<uint8_t> > portable_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 245 | UniquePtr<const std::vector<uint8_t> > portable_to_interpreter_bridge_; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 246 | UniquePtr<const std::vector<uint8_t> > quick_generic_jni_trampoline_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 247 | UniquePtr<const std::vector<uint8_t> > quick_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 248 | UniquePtr<const std::vector<uint8_t> > quick_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 249 | UniquePtr<const std::vector<uint8_t> > quick_to_interpreter_bridge_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 250 | |
| 251 | // output stats |
| 252 | uint32_t size_dex_file_alignment_; |
| 253 | uint32_t size_executable_offset_alignment_; |
| 254 | uint32_t size_oat_header_; |
| 255 | uint32_t size_oat_header_image_file_location_; |
| 256 | uint32_t size_dex_file_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 257 | uint32_t size_interpreter_to_interpreter_bridge_; |
| 258 | uint32_t size_interpreter_to_compiled_code_bridge_; |
| 259 | uint32_t size_jni_dlsym_lookup_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 260 | uint32_t size_portable_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 261 | uint32_t size_portable_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 262 | uint32_t size_portable_to_interpreter_bridge_; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 263 | uint32_t size_quick_generic_jni_trampoline_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 264 | uint32_t size_quick_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 265 | uint32_t size_quick_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 266 | uint32_t size_quick_to_interpreter_bridge_; |
| 267 | uint32_t size_trampoline_alignment_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 268 | uint32_t size_code_size_; |
| 269 | uint32_t size_code_; |
| 270 | uint32_t size_code_alignment_; |
| 271 | uint32_t size_mapping_table_; |
| 272 | uint32_t size_vmap_table_; |
| 273 | uint32_t size_gc_map_; |
| 274 | uint32_t size_oat_dex_file_location_size_; |
| 275 | uint32_t size_oat_dex_file_location_data_; |
| 276 | uint32_t size_oat_dex_file_location_checksum_; |
| 277 | uint32_t size_oat_dex_file_offset_; |
| 278 | uint32_t size_oat_dex_file_methods_offsets_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 279 | uint32_t size_oat_class_type_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 280 | uint32_t size_oat_class_status_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 281 | uint32_t size_oat_class_method_bitmaps_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 282 | uint32_t size_oat_class_method_offsets_; |
| 283 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 284 | // Code mappings for deduplication. Deduplication is already done on a pointer basis by the |
| 285 | // compiler driver, so we can simply compare the pointers to find out if things are duplicated. |
| 286 | SafeMap<const std::vector<uint8_t>*, uint32_t> code_offsets_; |
| 287 | SafeMap<const std::vector<uint8_t>*, uint32_t> vmap_table_offsets_; |
| 288 | SafeMap<const std::vector<uint8_t>*, uint32_t> mapping_table_offsets_; |
| 289 | SafeMap<const std::vector<uint8_t>*, uint32_t> gc_map_offsets_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 290 | |
| 291 | DISALLOW_COPY_AND_ASSIGN(OatWriter); |
| 292 | }; |
| 293 | |
| 294 | } // namespace art |
| 295 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 296 | #endif // ART_COMPILER_OAT_WRITER_H_ |