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> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | #include <cstddef> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 22 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 23 | |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 24 | #include "base/array_ref.h" |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 25 | #include "base/dchecked_vector.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 26 | #include "linker/relative_patcher.h" // For linker::RelativePatcherTargetProvider. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | #include "mem_map.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 28 | #include "method_reference.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 29 | #include "mirror/class.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 30 | #include "oat.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 31 | #include "os.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 32 | #include "safe_map.h" |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 33 | #include "string_reference.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 37 | class BitVector; |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 38 | class CompiledMethod; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 39 | class CompilerDriver; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 40 | class ImageWriter; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 41 | class OutputStream; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 42 | class TimingLogger; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 43 | class TypeLookupTable; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 44 | class ZipEntry; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 46 | namespace debug { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 47 | struct MethodDebugInfo; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 48 | } // namespace debug |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 49 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 50 | namespace linker { |
| 51 | class MultiOatRelativePatcher; |
| 52 | } // namespace linker |
| 53 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 54 | namespace verifier { |
| 55 | class VerifierDeps; |
| 56 | } // namespace verifier |
| 57 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 58 | // OatHeader variable length with count of D OatDexFiles |
| 59 | // |
| 60 | // OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses |
| 61 | // OatDexFile[1] |
| 62 | // ... |
| 63 | // OatDexFile[D] |
| 64 | // |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 65 | // TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile. |
| 66 | // TypeLookupTable[1] |
| 67 | // ... |
| 68 | // TypeLookupTable[D] |
| 69 | // |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 70 | // ClassOffsets[0] one table of OatClass offsets for each class def for each OatDexFile. |
| 71 | // ClassOffsets[1] |
| 72 | // ... |
| 73 | // ClassOffsets[D] |
| 74 | // |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 75 | // OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs |
| 76 | // OatClass[1] contains OatClass entries with class status, offsets to code, etc. |
| 77 | // ... |
| 78 | // OatClass[C] |
| 79 | // |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 80 | // GcMap one variable sized blob with GC map. |
| 81 | // GcMap GC maps are deduplicated. |
| 82 | // ... |
| 83 | // GcMap |
| 84 | // |
| 85 | // VmapTable one variable sized VmapTable blob (quick compiler only). |
| 86 | // VmapTable VmapTables are deduplicated. |
| 87 | // ... |
| 88 | // VmapTable |
| 89 | // |
| 90 | // MappingTable one variable sized blob with MappingTable (quick compiler only). |
| 91 | // MappingTable MappingTables are deduplicated. |
| 92 | // ... |
| 93 | // MappingTable |
| 94 | // |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 95 | // padding if necessary so that the following code will be page aligned |
| 96 | // |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 97 | // OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode. |
| 98 | // MethodCode one variable sized blob with the code of a CompiledMethod. |
| 99 | // OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated. |
| 100 | // MethodCode |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 101 | // ... |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 102 | // OatMethodHeader |
| 103 | // MethodCode |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 104 | // |
| 105 | class OatWriter { |
| 106 | public: |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 107 | enum class CreateTypeLookupTable { |
| 108 | kCreate, |
| 109 | kDontCreate, |
| 110 | kDefault = kCreate |
| 111 | }; |
| 112 | |
| 113 | OatWriter(bool compiling_boot_image, TimingLogger* timings); |
| 114 | |
| 115 | // To produce a valid oat file, the user must first add sources with any combination of |
| 116 | // - AddDexFileSource(), |
| 117 | // - AddZippedDexFilesSource(), |
| 118 | // - AddRawDexFileSource(). |
| 119 | // Then the user must call in order |
| 120 | // - WriteAndOpenDexFiles() |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 121 | // - Initialize() |
| 122 | // - WriteVerifierDeps() |
| 123 | // - WriteQuickeningInfo() |
| 124 | // - WriteVdexHeader() |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 125 | // - PrepareLayout(), |
| 126 | // - WriteRodata(), |
| 127 | // - WriteCode(), |
| 128 | // - WriteHeader(). |
| 129 | |
| 130 | // Add dex file source(s) from a file, either a plain dex file or |
| 131 | // a zip file with one or more dex files. |
| 132 | bool AddDexFileSource( |
| 133 | const char* filename, |
| 134 | const char* location, |
| 135 | CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault); |
| 136 | // Add dex file source(s) from a zip file specified by a file handle. |
| 137 | bool AddZippedDexFilesSource( |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 138 | File&& zip_fd, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 139 | const char* location, |
| 140 | CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault); |
| 141 | // Add dex file source from raw memory. |
| 142 | bool AddRawDexFileSource( |
| 143 | const ArrayRef<const uint8_t>& data, |
| 144 | const char* location, |
| 145 | uint32_t location_checksum, |
| 146 | CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault); |
| 147 | dchecked_vector<const char*> GetSourceLocations() const; |
| 148 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 149 | // Write raw dex files to the vdex file, mmap the file and open the dex files from it. |
| 150 | // Supporting data structures are written into the .rodata section of the oat file. |
| 151 | // The `verify` setting dictates whether the dex file verifier should check the dex files. |
| 152 | // This is generally the case, and should only be false for tests. |
| 153 | bool WriteAndOpenDexFiles(File* vdex_file, |
| 154 | OutputStream* oat_rodata, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 155 | InstructionSet instruction_set, |
| 156 | const InstructionSetFeatures* instruction_set_features, |
| 157 | SafeMap<std::string, std::string>* key_value_store, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 158 | bool verify, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 159 | /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map, |
| 160 | /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 161 | bool WriteQuickeningInfo(OutputStream* vdex_out); |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 162 | bool WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps); |
| 163 | bool WriteVdexHeader(OutputStream* vdex_out); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 164 | // Initialize the writer with the given parameters. |
| 165 | void Initialize(const CompilerDriver* compiler, |
| 166 | ImageWriter* image_writer, |
| 167 | const std::vector<const DexFile*>& dex_files) { |
| 168 | compiler_driver_ = compiler; |
| 169 | image_writer_ = image_writer; |
| 170 | dex_files_ = &dex_files; |
| 171 | } |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 172 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 173 | // Prepare layout of remaining data. |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 174 | void PrepareLayout(linker::MultiOatRelativePatcher* relative_patcher); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 175 | // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps). |
| 176 | bool WriteRodata(OutputStream* out); |
| 177 | // Write the code to the .text section. |
| 178 | bool WriteCode(OutputStream* out); |
| 179 | // Write the oat header. This finalizes the oat file. |
| 180 | bool WriteHeader(OutputStream* out, |
| 181 | uint32_t image_file_location_oat_checksum, |
| 182 | uintptr_t image_file_location_oat_begin, |
| 183 | int32_t image_patch_delta); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 184 | |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 185 | // Returns whether the oat file has an associated image. |
| 186 | bool HasImage() const { |
| 187 | // Since the image is being created at the same time as the oat file, |
| 188 | // check if there's an image writer. |
| 189 | return image_writer_ != nullptr; |
| 190 | } |
| 191 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 192 | bool HasBootImage() const { |
| 193 | return compiling_boot_image_; |
| 194 | } |
| 195 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 196 | const OatHeader& GetOatHeader() const { |
| 197 | return *oat_header_; |
| 198 | } |
| 199 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 200 | size_t GetOatSize() const { |
| 201 | return oat_size_; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 204 | size_t GetBssSize() const { |
| 205 | return bss_size_; |
| 206 | } |
| 207 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 208 | size_t GetBssRootsOffset() const { |
| 209 | return bss_roots_offset_; |
| 210 | } |
| 211 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 212 | size_t GetOatDataOffset() const { |
| 213 | return oat_data_offset_; |
| 214 | } |
| 215 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 216 | ArrayRef<const uintptr_t> GetAbsolutePatchLocations() const { |
| 217 | return ArrayRef<const uintptr_t>(absolute_patch_locations_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 220 | ~OatWriter(); |
| 221 | |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 222 | void AddMethodDebugInfos(const std::vector<debug::MethodDebugInfo>& infos) { |
| 223 | method_info_.insert(method_info_.end(), infos.begin(), infos.end()); |
| 224 | } |
| 225 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 226 | ArrayRef<const debug::MethodDebugInfo> GetMethodDebugInfo() const { |
| 227 | return ArrayRef<const debug::MethodDebugInfo>(method_info_); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 230 | const CompilerDriver* GetCompilerDriver() { |
| 231 | return compiler_driver_; |
| 232 | } |
| 233 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 234 | private: |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 235 | class DexFileSource; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 236 | class OatClass; |
| 237 | class OatDexFile; |
| 238 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 239 | // The function VisitDexMethods() below iterates through all the methods in all |
| 240 | // the compiled dex files in order of their definitions. The method visitor |
| 241 | // classes provide individual bits of processing for each of the passes we need to |
| 242 | // first collect the data we want to write to the oat file and then, in later passes, |
| 243 | // to actually write it. |
| 244 | class DexMethodVisitor; |
| 245 | class OatDexMethodVisitor; |
| 246 | class InitOatClassesMethodVisitor; |
| 247 | class InitCodeMethodVisitor; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 248 | class InitMapMethodVisitor; |
| 249 | class InitImageMethodVisitor; |
| 250 | class WriteCodeMethodVisitor; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 251 | class WriteMapMethodVisitor; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 252 | class WriteQuickeningInfoMethodVisitor; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 253 | |
| 254 | // Visit all the methods in all the compiled dex files in their definition order |
| 255 | // with a given DexMethodVisitor. |
| 256 | bool VisitDexMethods(DexMethodVisitor* visitor); |
| 257 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 258 | bool WriteDexFiles(OutputStream* out, File* file); |
| 259 | bool WriteDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file); |
| 260 | bool SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file); |
| 261 | bool WriteDexFile(OutputStream* out, |
| 262 | File* file, |
| 263 | OatDexFile* oat_dex_file, |
| 264 | ZipEntry* dex_file); |
| 265 | bool WriteDexFile(OutputStream* out, |
| 266 | File* file, |
| 267 | OatDexFile* oat_dex_file, |
| 268 | File* dex_file); |
| 269 | bool WriteDexFile(OutputStream* out, OatDexFile* oat_dex_file, const uint8_t* dex_file); |
| 270 | bool OpenDexFiles(File* file, |
| 271 | bool verify, |
| 272 | /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map, |
| 273 | /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files); |
| 274 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 275 | size_t InitOatHeader(InstructionSet instruction_set, |
| 276 | const InstructionSetFeatures* instruction_set_features, |
| 277 | uint32_t num_dex_files, |
| 278 | SafeMap<std::string, std::string>* key_value_store); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 279 | size_t InitOatDexFiles(size_t offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 280 | size_t InitOatClasses(size_t offset); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 281 | size_t InitOatMaps(size_t offset); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 282 | size_t InitOatCode(size_t offset); |
| 283 | size_t InitOatCodeDexFiles(size_t offset); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 284 | void InitBssLayout(InstructionSet instruction_set); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 285 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 286 | bool WriteClassOffsets(OutputStream* out); |
| 287 | bool WriteClasses(OutputStream* out); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 288 | size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset); |
| 289 | size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 290 | size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 291 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 292 | bool RecordOatDataOffset(OutputStream* out); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 293 | bool ReadDexFileHeader(File* oat_file, OatDexFile* oat_dex_file); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 294 | bool ValidateDexFileHeader(const uint8_t* raw_header, const char* location); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 295 | bool WriteOatDexFiles(OutputStream* oat_rodata); |
| 296 | bool WriteTypeLookupTables(OutputStream* oat_rodata, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 297 | const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 298 | bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 299 | void SetMultiOatRelativePatcherAdjustment(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 300 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 301 | enum class WriteState { |
| 302 | kAddingDexFileSources, |
| 303 | kPrepareLayout, |
| 304 | kWriteRoData, |
| 305 | kWriteText, |
| 306 | kWriteHeader, |
| 307 | kDone |
| 308 | }; |
| 309 | |
| 310 | WriteState write_state_; |
| 311 | TimingLogger* timings_; |
| 312 | |
| 313 | std::vector<std::unique_ptr<File>> raw_dex_files_; |
| 314 | std::vector<std::unique_ptr<ZipArchive>> zip_archives_; |
| 315 | std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_; |
| 316 | |
| 317 | // Using std::list<> which doesn't move elements around on push/emplace_back(). |
| 318 | // We need this because we keep plain pointers to the strings' c_str(). |
| 319 | std::list<std::string> zipped_dex_file_locations_; |
| 320 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 321 | dchecked_vector<debug::MethodDebugInfo> method_info_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 322 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 323 | const CompilerDriver* compiler_driver_; |
| 324 | ImageWriter* image_writer_; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 325 | const bool compiling_boot_image_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 326 | |
| 327 | // note OatFile does not take ownership of the DexFiles |
| 328 | const std::vector<const DexFile*>* dex_files_; |
| 329 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 330 | // Size required for Vdex data structures. |
| 331 | size_t vdex_size_; |
| 332 | |
| 333 | // Offset of section holding Dex files inside Vdex. |
| 334 | size_t vdex_dex_files_offset_; |
| 335 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 336 | // Offset of section holding VerifierDeps inside Vdex. |
| 337 | size_t vdex_verifier_deps_offset_; |
| 338 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 339 | // Offset of section holding quickening info inside Vdex. |
| 340 | size_t vdex_quickening_info_offset_; |
| 341 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 342 | // Size required for Oat data structures. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 343 | size_t oat_size_; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 344 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 345 | // The start of the required .bss section. |
| 346 | size_t bss_start_; |
| 347 | |
| 348 | // The size of the required .bss section holding the DexCache data and GC roots. |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 349 | size_t bss_size_; |
| 350 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 351 | // The offset of the GC roots in .bss section. |
| 352 | size_t bss_roots_offset_; |
| 353 | |
| 354 | // Map for allocating String entries in .bss. Indexed by StringReference for the source |
| 355 | // string in the dex file with the "string value comparator" for deduplication. The value |
| 356 | // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`. |
| 357 | SafeMap<StringReference, size_t, StringReferenceValueComparator> bss_string_entries_; |
| 358 | |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 359 | // Offsets of the dex cache arrays for each app dex file. For the |
| 360 | // boot image, this information is provided by the ImageWriter. |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 361 | SafeMap<const DexFile*, size_t> dex_cache_arrays_offsets_; // DexFiles not owned. |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 362 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 363 | // Offset of the oat data from the start of the mmapped region of the elf file. |
| 364 | size_t oat_data_offset_; |
| 365 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 366 | // data to write |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 367 | std::unique_ptr<OatHeader> oat_header_; |
| 368 | dchecked_vector<OatDexFile> oat_dex_files_; |
| 369 | dchecked_vector<OatClass> oat_classes_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 370 | std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 371 | std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_; |
| 372 | std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_; |
| 373 | std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_; |
| 374 | std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 375 | |
| 376 | // output stats |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 377 | uint32_t size_vdex_header_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 378 | uint32_t size_dex_file_alignment_; |
| 379 | uint32_t size_executable_offset_alignment_; |
| 380 | uint32_t size_oat_header_; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 381 | uint32_t size_oat_header_key_value_store_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 382 | uint32_t size_dex_file_; |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 383 | uint32_t size_verifier_deps_; |
| 384 | uint32_t size_verifier_deps_alignment_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 385 | uint32_t size_quickening_info_; |
| 386 | uint32_t size_quickening_info_alignment_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 387 | uint32_t size_interpreter_to_interpreter_bridge_; |
| 388 | uint32_t size_interpreter_to_compiled_code_bridge_; |
| 389 | uint32_t size_jni_dlsym_lookup_; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 390 | uint32_t size_quick_generic_jni_trampoline_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 391 | uint32_t size_quick_imt_conflict_trampoline_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 392 | uint32_t size_quick_resolution_trampoline_; |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 393 | uint32_t size_quick_to_interpreter_bridge_; |
| 394 | uint32_t size_trampoline_alignment_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 395 | uint32_t size_method_header_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 396 | uint32_t size_code_; |
| 397 | uint32_t size_code_alignment_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 398 | uint32_t size_relative_call_thunks_; |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 399 | uint32_t size_misc_thunks_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 400 | uint32_t size_vmap_table_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 401 | uint32_t size_oat_dex_file_location_size_; |
| 402 | uint32_t size_oat_dex_file_location_data_; |
| 403 | uint32_t size_oat_dex_file_location_checksum_; |
| 404 | uint32_t size_oat_dex_file_offset_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 405 | uint32_t size_oat_dex_file_class_offsets_offset_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 406 | uint32_t size_oat_dex_file_lookup_table_offset_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 407 | uint32_t size_oat_lookup_table_alignment_; |
| 408 | uint32_t size_oat_lookup_table_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 409 | uint32_t size_oat_class_offsets_alignment_; |
| 410 | uint32_t size_oat_class_offsets_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 411 | uint32_t size_oat_class_type_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 412 | uint32_t size_oat_class_status_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 413 | uint32_t size_oat_class_method_bitmaps_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 414 | uint32_t size_oat_class_method_offsets_; |
| 415 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 416 | // The helper for processing relative patches is external so that we can patch across oat files. |
| 417 | linker::MultiOatRelativePatcher* relative_patcher_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 418 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 419 | // The locations of absolute patches relative to the start of the executable section. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 420 | dchecked_vector<uintptr_t> absolute_patch_locations_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 421 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 422 | DISALLOW_COPY_AND_ASSIGN(OatWriter); |
| 423 | }; |
| 424 | |
| 425 | } // namespace art |
| 426 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 427 | #endif // ART_COMPILER_OAT_WRITER_H_ |