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