Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_OAT_WRITER_H_ |
| 4 | #define ART_SRC_OAT_WRITER_H_ |
| 5 | |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <cstddef> |
| 9 | |
| 10 | #include "UniquePtr.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 11 | #include "compiler.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 12 | #include "dex_cache.h" |
| 13 | #include "mem_map.h" |
| 14 | #include "oat.h" |
| 15 | #include "object.h" |
| 16 | #include "os.h" |
| 17 | #include "space.h" |
| 18 | |
| 19 | namespace art { |
| 20 | |
| 21 | // OatHeader fixed length with count of D OatDexFiles |
| 22 | // |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 23 | // OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 24 | // OatDexFile[1] |
| 25 | // ... |
| 26 | // OatDexFile[D] |
| 27 | // |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 28 | // Dex[0] one variable sized DexFile for each OatDexFile. |
| 29 | // Dex[1] these are literal copies of the input .dex files. |
| 30 | // ... |
| 31 | // Dex[D] |
| 32 | // |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 33 | // OatClasses[0] one variable sized OatClasses for each OatDexFile |
| 34 | // OatClasses[1] contains DexFile::NumClassDefs offsets to OatMethods for each ClassDef |
| 35 | // ... |
| 36 | // OatClasses[D] |
| 37 | // |
| 38 | // OatMethods[0] one variable sized OatMethods for each of C DexFile::ClassDefs |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 39 | // OatMethods[1] contains OatMethod entries with offsets to code, method properities, etc. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 40 | // ... |
| 41 | // OatMethods[C] |
| 42 | // |
| 43 | // padding if necessary so that the follow code will be page aligned |
| 44 | // |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 45 | // CompiledMethod one variable sized blob with the contents of each CompiledMethod |
| 46 | // CompiledMethod |
| 47 | // CompiledMethod |
| 48 | // CompiledMethod |
| 49 | // CompiledMethod |
| 50 | // CompiledMethod |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | // ... |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 52 | // CompiledMethod |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 53 | // |
| 54 | class OatWriter { |
| 55 | public: |
| 56 | // Write an oat file. Returns true on success, false on failure. |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 57 | static bool Create(File* file, const ClassLoader* class_loader, const Compiler& compiler); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 61 | OatWriter(const std::vector<const DexFile*>& dex_files, |
| 62 | const ClassLoader* class_loader, |
| 63 | const Compiler& compiler); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 64 | ~OatWriter(); |
| 65 | |
| 66 | size_t InitOatHeader(); |
| 67 | size_t InitOatDexFiles(size_t offset); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 68 | size_t InitDexFiles(size_t offset); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 69 | size_t InitOatClasses(size_t offset); |
| 70 | size_t InitOatMethods(size_t offset); |
| 71 | size_t InitOatCode(size_t offset); |
| 72 | size_t InitOatCodeDexFiles(size_t offset); |
| 73 | size_t InitOatCodeDexFile(size_t offset, |
| 74 | size_t& oat_class_index, |
| 75 | const DexFile& dex_file); |
| 76 | size_t InitOatCodeClassDef(size_t offset, |
| 77 | size_t oat_class_index, |
| 78 | const DexFile& dex_file, |
| 79 | const DexFile::ClassDef& class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 80 | size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_method_index, |
| 81 | bool is_static, bool is_direct, uint32_t method_idx, const DexFile*); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 82 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 83 | bool Write(File* file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 84 | bool WriteTables(File* file); |
| 85 | size_t WriteCode(File* file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 86 | size_t WriteCodeDexFiles(File* file, size_t offset); |
| 87 | size_t WriteCodeDexFile(File* file, size_t offset, size_t& oat_class_index, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 88 | const DexFile& dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 89 | size_t WriteCodeClassDef(File* file, size_t offset, size_t oat_class_index, |
| 90 | const DexFile& dex_file, const DexFile::ClassDef& class_def); |
| 91 | size_t WriteCodeMethod(File* file, size_t offset, size_t oat_class_index, |
| 92 | size_t class_def_method_index, bool is_static, uint32_t method_idx, |
| 93 | const DexFile& dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 94 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 95 | void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file, |
| 96 | File* f) const; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 97 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 98 | class OatDexFile { |
| 99 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 100 | explicit OatDexFile(const DexFile& dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 101 | size_t SizeOf() const; |
| 102 | void UpdateChecksum(OatHeader& oat_header) const; |
| 103 | bool Write(File* file) const; |
| 104 | |
| 105 | // data to write |
| 106 | uint32_t dex_file_location_size_; |
| 107 | const uint8_t* dex_file_location_data_; |
| 108 | uint32_t dex_file_checksum_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 109 | uint32_t dex_file_offset_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 110 | uint32_t classes_offset_; |
| 111 | |
| 112 | private: |
| 113 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 114 | }; |
| 115 | |
| 116 | class OatClasses { |
| 117 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 118 | explicit OatClasses(const DexFile& dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 119 | size_t SizeOf() const; |
| 120 | void UpdateChecksum(OatHeader& oat_header) const; |
| 121 | bool Write(File* file) const; |
| 122 | |
| 123 | // data to write |
| 124 | std::vector<uint32_t> methods_offsets_; |
| 125 | |
| 126 | private: |
| 127 | DISALLOW_COPY_AND_ASSIGN(OatClasses); |
| 128 | }; |
| 129 | |
| 130 | class OatMethods { |
| 131 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 132 | explicit OatMethods(uint32_t methods_count); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 133 | size_t SizeOf() const; |
| 134 | void UpdateChecksum(OatHeader& oat_header) const; |
| 135 | bool Write(File* file) const; |
| 136 | |
| 137 | // data to write |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 138 | std::vector<OatMethodOffsets> method_offsets_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | DISALLOW_COPY_AND_ASSIGN(OatMethods); |
| 142 | }; |
| 143 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 144 | const Compiler* compiler_; |
| 145 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 146 | // TODO: remove the ClassLoader when the code storage moves out of Method |
| 147 | const ClassLoader* class_loader_; |
| 148 | |
| 149 | // note OatFile does not take ownership of the DexFiles |
| 150 | const std::vector<const DexFile*>* dex_files_; |
| 151 | |
| 152 | // data to write |
| 153 | OatHeader* oat_header_; |
| 154 | std::vector<OatDexFile*> oat_dex_files_; |
| 155 | std::vector<OatClasses*> oat_classes_; |
| 156 | std::vector<OatMethods*> oat_methods_; |
| 157 | uint32_t executable_offset_padding_length_; |
| 158 | |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame] | 159 | template <class T> struct MapCompare { |
| 160 | public: |
| 161 | bool operator() (const T* const &a, const T* const &b) const { |
| 162 | return *a < *b; |
| 163 | } |
| 164 | }; |
| 165 | |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 166 | // code mappings for deduplication |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame] | 167 | std::map<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_; |
| 168 | std::map<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_; |
| 169 | std::map<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 170 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 171 | DISALLOW_COPY_AND_ASSIGN(OatWriter); |
| 172 | }; |
| 173 | |
| 174 | } // namespace art |
| 175 | |
| 176 | #endif // ART_SRC_OAT_WRITER_H_ |