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 | 389efb0 | 2012-01-11 12:06:26 -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 | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 33 | // OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs |
| 34 | // OatClass[1] contains OatClass entries with class status, offsets to code, etc. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 35 | // ... |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 36 | // OatClass[C] |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 37 | // |
| 38 | // padding if necessary so that the follow code will be page aligned |
| 39 | // |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 40 | // CompiledMethod one variable sized blob with the contents of each CompiledMethod |
| 41 | // CompiledMethod |
| 42 | // CompiledMethod |
| 43 | // CompiledMethod |
| 44 | // CompiledMethod |
| 45 | // CompiledMethod |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 46 | // ... |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 47 | // CompiledMethod |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 48 | // |
| 49 | class OatWriter { |
| 50 | public: |
| 51 | // Write an oat file. Returns true on success, false on failure. |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 52 | static bool Create(File* file, const ClassLoader* class_loader, const Compiler& compiler); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 56 | OatWriter(const std::vector<const DexFile*>& dex_files, |
| 57 | const ClassLoader* class_loader, |
| 58 | const Compiler& compiler); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 59 | ~OatWriter(); |
| 60 | |
| 61 | size_t InitOatHeader(); |
| 62 | size_t InitOatDexFiles(size_t offset); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 63 | size_t InitDexFiles(size_t offset); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 64 | size_t InitOatClasses(size_t offset); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 65 | size_t InitOatCode(size_t offset); |
| 66 | size_t InitOatCodeDexFiles(size_t offset); |
| 67 | size_t InitOatCodeDexFile(size_t offset, |
| 68 | size_t& oat_class_index, |
| 69 | const DexFile& dex_file); |
| 70 | size_t InitOatCodeClassDef(size_t offset, |
| 71 | size_t oat_class_index, |
| 72 | const DexFile& dex_file, |
| 73 | const DexFile::ClassDef& class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 74 | size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_method_index, |
| 75 | bool is_static, bool is_direct, uint32_t method_idx, const DexFile*); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 77 | bool Write(File* file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 78 | bool WriteTables(File* file); |
| 79 | size_t WriteCode(File* file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 80 | size_t WriteCodeDexFiles(File* file, size_t offset); |
| 81 | 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] | 82 | const DexFile& dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 83 | size_t WriteCodeClassDef(File* file, size_t offset, size_t oat_class_index, |
| 84 | const DexFile& dex_file, const DexFile::ClassDef& class_def); |
| 85 | size_t WriteCodeMethod(File* file, size_t offset, size_t oat_class_index, |
| 86 | size_t class_def_method_index, bool is_static, uint32_t method_idx, |
| 87 | const DexFile& dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 88 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 89 | void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file, |
| 90 | File* f) const; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 91 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 92 | class OatDexFile { |
| 93 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 94 | explicit OatDexFile(const DexFile& dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 95 | size_t SizeOf() const; |
| 96 | void UpdateChecksum(OatHeader& oat_header) const; |
| 97 | bool Write(File* file) const; |
| 98 | |
| 99 | // data to write |
| 100 | uint32_t dex_file_location_size_; |
| 101 | const uint8_t* dex_file_location_data_; |
| 102 | uint32_t dex_file_checksum_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 103 | uint32_t dex_file_offset_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 104 | std::vector<uint32_t> methods_offsets_; |
| 105 | |
| 106 | private: |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 107 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 110 | class OatClass { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 111 | public: |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 112 | explicit OatClass(Class::Status status, uint32_t methods_count); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 113 | size_t SizeOf() const; |
| 114 | void UpdateChecksum(OatHeader& oat_header) const; |
| 115 | bool Write(File* file) const; |
| 116 | |
| 117 | // data to write |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 118 | Class::Status status_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 119 | std::vector<OatMethodOffsets> method_offsets_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(OatClass); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 125 | const Compiler* compiler_; |
| 126 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 127 | // TODO: remove the ClassLoader when the code storage moves out of Method |
| 128 | const ClassLoader* class_loader_; |
| 129 | |
| 130 | // note OatFile does not take ownership of the DexFiles |
| 131 | const std::vector<const DexFile*>* dex_files_; |
| 132 | |
| 133 | // data to write |
| 134 | OatHeader* oat_header_; |
| 135 | std::vector<OatDexFile*> oat_dex_files_; |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 136 | std::vector<OatClass*> oat_classes_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 137 | uint32_t executable_offset_padding_length_; |
| 138 | |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame] | 139 | template <class T> struct MapCompare { |
| 140 | public: |
| 141 | bool operator() (const T* const &a, const T* const &b) const { |
| 142 | return *a < *b; |
| 143 | } |
| 144 | }; |
| 145 | |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 146 | // code mappings for deduplication |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame] | 147 | std::map<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_; |
| 148 | std::map<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_; |
| 149 | std::map<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame^] | 150 | std::map<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > gc_map_offsets_; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 151 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(OatWriter); |
| 153 | }; |
| 154 | |
| 155 | } // namespace art |
| 156 | |
| 157 | #endif // ART_SRC_OAT_WRITER_H_ |