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 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 85 | private: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 86 | size_t InitOatHeader(); |
| 87 | size_t InitOatDexFiles(size_t offset); |
| 88 | size_t InitDexFiles(size_t offset); |
| 89 | size_t InitOatClasses(size_t offset); |
| 90 | size_t InitOatCode(size_t offset) |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 92 | size_t InitOatCodeDexFiles(size_t offset) |
| 93 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 94 | size_t InitOatCodeDexFile(size_t offset, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 95 | size_t* oat_class_index, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 96 | const DexFile& dex_file) |
| 97 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 98 | size_t InitOatCodeClassDef(size_t offset, |
| 99 | size_t oat_class_index, size_t class_def_index, |
| 100 | const DexFile& dex_file, |
| 101 | const DexFile::ClassDef& class_def) |
| 102 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 103 | 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] | 104 | size_t class_def_method_index, size_t* method_offsets_index, |
| 105 | bool is_native, InvokeType type, uint32_t method_idx, const DexFile&) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 106 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 107 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 108 | bool WriteTables(OutputStream* out, const size_t file_offset); |
| 109 | size_t WriteCode(OutputStream* out, const size_t file_offset); |
| 110 | size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset); |
| 111 | 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] | 112 | size_t* oat_class_index, const DexFile& dex_file); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 113 | 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] | 114 | size_t oat_class_index, const DexFile& dex_file, |
| 115 | const DexFile::ClassDef& class_def); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 116 | 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] | 117 | size_t oat_class_index, size_t class_def_method_index, |
| 118 | size_t* method_offsets_index, bool is_static, uint32_t method_idx, |
| 119 | const DexFile& dex_file); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 120 | |
| 121 | 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] | 122 | const OutputStream& out) const; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 123 | |
| 124 | class OatDexFile { |
| 125 | public: |
| 126 | explicit OatDexFile(size_t offset, const DexFile& dex_file); |
| 127 | size_t SizeOf() const; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 128 | void UpdateChecksum(OatHeader* oat_header) const; |
| 129 | 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] | 130 | |
| 131 | // Offset of start of OatDexFile from beginning of OatHeader. It is |
| 132 | // used to validate file position when writing. |
| 133 | size_t offset_; |
| 134 | |
| 135 | // data to write |
| 136 | uint32_t dex_file_location_size_; |
| 137 | const uint8_t* dex_file_location_data_; |
| 138 | uint32_t dex_file_location_checksum_; |
| 139 | uint32_t dex_file_offset_; |
| 140 | std::vector<uint32_t> methods_offsets_; |
| 141 | |
| 142 | private: |
| 143 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 144 | }; |
| 145 | |
| 146 | class OatClass { |
| 147 | public: |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 148 | explicit OatClass(size_t offset, |
| 149 | std::vector<CompiledMethod*>* compiled_methods, |
| 150 | uint32_t num_non_null_compiled_methods, |
| 151 | mirror::Class::Status status); |
| 152 | ~OatClass(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 153 | size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const; |
| 154 | size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const; |
| 155 | size_t SizeOf() const; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 156 | void UpdateChecksum(OatHeader* oat_header) const; |
| 157 | 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] | 158 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 159 | CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const { |
| 160 | DCHECK(compiled_methods_ != NULL); |
| 161 | return (*compiled_methods_)[class_def_method_index]; |
| 162 | } |
| 163 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 164 | // Offset of start of OatClass from beginning of OatHeader. It is |
| 165 | // used to validate file position when writing. For Portable, it |
| 166 | // is also used to calculate the position of the OatMethodOffsets |
| 167 | // so that code pointers within the OatMethodOffsets can be |
| 168 | // patched to point to code in the Portable .o ELF objects. |
| 169 | size_t offset_; |
| 170 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 171 | // CompiledMethods for each class_def_method_index, or NULL if no method is available. |
| 172 | std::vector<CompiledMethod*>* compiled_methods_; |
| 173 | |
| 174 | // Offset from OatClass::offset_ to the OatMethodOffsets for the |
| 175 | // class_def_method_index. If 0, it means the corresponding |
| 176 | // CompiledMethod entry in OatClass::compiled_methods_ should be |
| 177 | // NULL and that the OatClass::type_ should be kOatClassBitmap. |
| 178 | std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_; |
| 179 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 180 | // data to write |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 181 | |
| 182 | COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits); |
| 183 | int16_t status_; |
| 184 | |
| 185 | COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits); |
| 186 | uint16_t type_; |
| 187 | |
| 188 | uint32_t method_bitmap_size_; |
| 189 | |
| 190 | // bit vector indexed by ClassDef method index. When |
| 191 | // OatClassType::type_ is kOatClassBitmap, a set bit indicates the |
| 192 | // method has an OatMethodOffsets in methods_offsets_, otherwise |
| 193 | // the entry was ommited to save space. If OatClassType::type_ is |
| 194 | // not is kOatClassBitmap, the bitmap will be NULL. |
| 195 | BitVector* method_bitmap_; |
| 196 | |
| 197 | // OatMethodOffsets for each CompiledMethod present in the |
| 198 | // OatClass. Note that some may be missing if |
| 199 | // OatClass::compiled_methods_ contains NULL values (and |
| 200 | // oat_method_offsets_offsets_from_oat_class_ should contain 0 |
| 201 | // values in this case). |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 202 | std::vector<OatMethodOffsets> method_offsets_; |
| 203 | |
| 204 | private: |
| 205 | DISALLOW_COPY_AND_ASSIGN(OatClass); |
| 206 | }; |
| 207 | |
| 208 | const CompilerDriver* const compiler_driver_; |
| 209 | |
| 210 | // note OatFile does not take ownership of the DexFiles |
| 211 | const std::vector<const DexFile*>* dex_files_; |
| 212 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 213 | // Size required for Oat data structures. |
| 214 | size_t size_; |
| 215 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 216 | // dependencies on the image. |
| 217 | uint32_t image_file_location_oat_checksum_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 218 | uintptr_t image_file_location_oat_begin_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 219 | std::string image_file_location_; |
| 220 | |
| 221 | // data to write |
| 222 | OatHeader* oat_header_; |
| 223 | std::vector<OatDexFile*> oat_dex_files_; |
| 224 | std::vector<OatClass*> oat_classes_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 225 | UniquePtr<const std::vector<uint8_t> > interpreter_to_interpreter_bridge_; |
| 226 | UniquePtr<const std::vector<uint8_t> > interpreter_to_compiled_code_bridge_; |
| 227 | UniquePtr<const std::vector<uint8_t> > jni_dlsym_lookup_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 228 | UniquePtr<const std::vector<uint8_t> > portable_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 229 | UniquePtr<const std::vector<uint8_t> > portable_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 230 | UniquePtr<const std::vector<uint8_t> > portable_to_interpreter_bridge_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 231 | UniquePtr<const std::vector<uint8_t> > quick_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 232 | UniquePtr<const std::vector<uint8_t> > quick_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 233 | UniquePtr<const std::vector<uint8_t> > quick_to_interpreter_bridge_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | |
| 235 | // output stats |
| 236 | uint32_t size_dex_file_alignment_; |
| 237 | uint32_t size_executable_offset_alignment_; |
| 238 | uint32_t size_oat_header_; |
| 239 | uint32_t size_oat_header_image_file_location_; |
| 240 | uint32_t size_dex_file_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 241 | uint32_t size_interpreter_to_interpreter_bridge_; |
| 242 | uint32_t size_interpreter_to_compiled_code_bridge_; |
| 243 | uint32_t size_jni_dlsym_lookup_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 244 | uint32_t size_portable_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 245 | uint32_t size_portable_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 246 | uint32_t size_portable_to_interpreter_bridge_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 247 | uint32_t size_quick_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 248 | uint32_t size_quick_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 249 | uint32_t size_quick_to_interpreter_bridge_; |
| 250 | uint32_t size_trampoline_alignment_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 251 | uint32_t size_code_size_; |
| 252 | uint32_t size_code_; |
| 253 | uint32_t size_code_alignment_; |
| 254 | uint32_t size_mapping_table_; |
| 255 | uint32_t size_vmap_table_; |
| 256 | uint32_t size_gc_map_; |
| 257 | uint32_t size_oat_dex_file_location_size_; |
| 258 | uint32_t size_oat_dex_file_location_data_; |
| 259 | uint32_t size_oat_dex_file_location_checksum_; |
| 260 | uint32_t size_oat_dex_file_offset_; |
| 261 | uint32_t size_oat_dex_file_methods_offsets_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 262 | uint32_t size_oat_class_type_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 263 | uint32_t size_oat_class_status_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 264 | uint32_t size_oat_class_method_bitmaps_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 265 | uint32_t size_oat_class_method_offsets_; |
| 266 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 267 | // Code mappings for deduplication. Deduplication is already done on a pointer basis by the |
| 268 | // compiler driver, so we can simply compare the pointers to find out if things are duplicated. |
| 269 | SafeMap<const std::vector<uint8_t>*, uint32_t> code_offsets_; |
| 270 | SafeMap<const std::vector<uint8_t>*, uint32_t> vmap_table_offsets_; |
| 271 | SafeMap<const std::vector<uint8_t>*, uint32_t> mapping_table_offsets_; |
| 272 | SafeMap<const std::vector<uint8_t>*, uint32_t> gc_map_offsets_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | |
| 274 | DISALLOW_COPY_AND_ASSIGN(OatWriter); |
| 275 | }; |
| 276 | |
| 277 | } // namespace art |
| 278 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 279 | #endif // ART_COMPILER_OAT_WRITER_H_ |