Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [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 | */ |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_OAT_WRITER_H_ |
| 18 | #define ART_SRC_OAT_WRITER_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <cstddef> |
| 23 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 24 | #include "compiler.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 25 | #include "dex_cache.h" |
| 26 | #include "mem_map.h" |
| 27 | #include "oat.h" |
| 28 | #include "object.h" |
| 29 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 30 | #include "safe_map.h" |
| 31 | #include "UniquePtr.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 35 | // OatHeader variable length with count of D OatDexFiles |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 36 | // |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 37 | // OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 38 | // OatDexFile[1] |
| 39 | // ... |
| 40 | // OatDexFile[D] |
| 41 | // |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 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 | // |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 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. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 49 | // ... |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 50 | // OatClass[C] |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | // |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 52 | // OatElfImage[0] one OatElfImage for each ELF image |
| 53 | // OatElfImage[1] contains the size, checksum, and offset to the ELF image. |
| 54 | // ... |
| 55 | // OatElfImage[E] |
| 56 | // |
| 57 | // ELF[0] |
| 58 | // ELF[1] |
| 59 | // ... |
| 60 | // ELF[E] |
| 61 | // |
Brian Carlstrom | f03c288 | 2012-03-05 20:29:06 -0800 | [diff] [blame] | 62 | // padding if necessary so that the following code will be page aligned |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 63 | // |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 64 | // CompiledMethod one variable sized blob with the contents of each CompiledMethod |
| 65 | // CompiledMethod |
| 66 | // CompiledMethod |
| 67 | // CompiledMethod |
| 68 | // CompiledMethod |
| 69 | // CompiledMethod |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 70 | // ... |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 71 | // CompiledMethod |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 72 | // |
| 73 | class OatWriter { |
| 74 | public: |
| 75 | // Write an oat file. Returns true on success, false on failure. |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 76 | static bool Create(File* file, |
| 77 | const ClassLoader* class_loader, |
| 78 | const std::vector<const DexFile*>& dex_files, |
| 79 | uint32_t image_file_location_checksum, |
| 80 | const std::string& image_file_location, |
| 81 | const Compiler& compiler); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 85 | OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 86 | uint32_t image_file_location_checksum, |
| 87 | const std::string& image_file_location, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 88 | const ClassLoader* class_loader, |
| 89 | const Compiler& compiler); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 90 | ~OatWriter(); |
| 91 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 92 | size_t InitOatHeader(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 93 | size_t InitOatDexFiles(size_t offset); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 94 | size_t InitDexFiles(size_t offset); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 95 | size_t InitOatClasses(size_t offset); |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 96 | size_t InitOatElfImages(size_t offset); |
| 97 | size_t InitElfImages(size_t offset); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 98 | size_t InitOatCode(size_t offset); |
| 99 | size_t InitOatCodeDexFiles(size_t offset); |
| 100 | size_t InitOatCodeDexFile(size_t offset, |
| 101 | size_t& oat_class_index, |
| 102 | const DexFile& dex_file); |
| 103 | size_t InitOatCodeClassDef(size_t offset, |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 104 | size_t oat_class_index, size_t class_def_index, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 105 | const DexFile& dex_file, |
| 106 | const DexFile::ClassDef& class_def); |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 107 | size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index, |
| 108 | size_t class_def_method_index, bool is_native, bool is_static, |
| 109 | bool is_direct, uint32_t method_idx, const DexFile*); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 110 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 111 | bool Write(File* file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 112 | bool WriteTables(File* file); |
| 113 | size_t WriteCode(File* file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 114 | size_t WriteCodeDexFiles(File* file, size_t offset); |
| 115 | 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] | 116 | const DexFile& dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 117 | size_t WriteCodeClassDef(File* file, size_t offset, size_t oat_class_index, |
| 118 | const DexFile& dex_file, const DexFile::ClassDef& class_def); |
| 119 | size_t WriteCodeMethod(File* file, size_t offset, size_t oat_class_index, |
| 120 | size_t class_def_method_index, bool is_static, uint32_t method_idx, |
| 121 | const DexFile& dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 122 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 123 | void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file, |
| 124 | File* f) const; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 125 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 126 | class OatDexFile { |
| 127 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 128 | explicit OatDexFile(const DexFile& dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 129 | size_t SizeOf() const; |
| 130 | void UpdateChecksum(OatHeader& oat_header) const; |
| 131 | bool Write(File* file) const; |
| 132 | |
| 133 | // data to write |
| 134 | uint32_t dex_file_location_size_; |
| 135 | const uint8_t* dex_file_location_data_; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 136 | uint32_t dex_file_location_checksum_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 137 | uint32_t dex_file_offset_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 138 | std::vector<uint32_t> methods_offsets_; |
| 139 | |
| 140 | private: |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 141 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 144 | class OatClass { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 145 | public: |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 146 | explicit OatClass(Class::Status status, uint32_t methods_count); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 147 | size_t SizeOf() const; |
| 148 | void UpdateChecksum(OatHeader& oat_header) const; |
| 149 | bool Write(File* file) const; |
| 150 | |
| 151 | // data to write |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 152 | Class::Status status_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 153 | std::vector<OatMethodOffsets> method_offsets_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 154 | |
| 155 | private: |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(OatClass); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 159 | class OatElfImage { |
| 160 | public: |
| 161 | explicit OatElfImage(const ElfImage& elf_image); |
| 162 | size_t SizeOf() const; |
| 163 | uint32_t GetElfSize() const; |
| 164 | uint32_t GetElfOffset() const; |
| 165 | void SetElfOffset(uint32_t offset); |
| 166 | bool Write(File* file) const; |
| 167 | bool WriteElfImage(File* file) const; |
| 168 | |
| 169 | private: |
| 170 | // data to write |
| 171 | uint32_t elf_offset_; |
| 172 | const uint32_t elf_size_; |
| 173 | |
| 174 | const byte* const elf_addr_; |
| 175 | DISALLOW_COPY_AND_ASSIGN(OatElfImage); |
| 176 | }; |
| 177 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 178 | const Compiler* compiler_; |
| 179 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 180 | // TODO: remove the ClassLoader when the code storage moves out of Method |
| 181 | const ClassLoader* class_loader_; |
| 182 | |
| 183 | // note OatFile does not take ownership of the DexFiles |
| 184 | const std::vector<const DexFile*>* dex_files_; |
| 185 | |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 186 | std::vector<ElfImage> elf_images_; |
| 187 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 188 | // dependency on the image |
| 189 | uint32_t image_file_location_checksum_; |
| 190 | std::string image_file_location_; |
| 191 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 192 | // data to write |
| 193 | OatHeader* oat_header_; |
| 194 | std::vector<OatDexFile*> oat_dex_files_; |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 195 | std::vector<OatClass*> oat_classes_; |
Logan Chien | 25ae640 | 2012-03-20 20:19:26 +0800 | [diff] [blame] | 196 | std::vector<OatElfImage*> oat_elf_images_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 197 | uint32_t executable_offset_padding_length_; |
| 198 | |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame] | 199 | template <class T> struct MapCompare { |
| 200 | public: |
| 201 | bool operator() (const T* const &a, const T* const &b) const { |
| 202 | return *a < *b; |
| 203 | } |
| 204 | }; |
| 205 | |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 206 | // code mappings for deduplication |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 207 | SafeMap<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_; |
| 208 | SafeMap<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_; |
| 209 | SafeMap<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_; |
| 210 | SafeMap<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] | 211 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 212 | DISALLOW_COPY_AND_ASSIGN(OatWriter); |
| 213 | }; |
| 214 | |
| 215 | } // namespace art |
| 216 | |
| 217 | #endif // ART_SRC_OAT_WRITER_H_ |