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 | #include "oat_writer.h" |
| 18 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 19 | #include <zlib.h> |
| 20 | |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 21 | #include "arch/arm64/instruction_set_features_arm64.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "art_method-inl.h" |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 23 | #include "base/allocator.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 24 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 25 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 27 | #include "class_linker.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 28 | #include "compiled_class.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 29 | #include "compiled_method.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "dex_file-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 31 | #include "dex/verification_results.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 32 | #include "driver/compiler_driver.h" |
| 33 | #include "driver/compiler_options.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 34 | #include "gc/space/image_space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 35 | #include "gc/space/space.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 36 | #include "image_writer.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 37 | #include "linker/relative_patcher.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | #include "mirror/array.h" |
| 39 | #include "mirror/class_loader.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 40 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 41 | #include "mirror/object-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 42 | #include "oat_quick_method_header.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 43 | #include "os.h" |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 44 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 45 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 46 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 47 | #include "handle_scope-inl.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 48 | #include "utils/dex_cache_arrays_layout-inl.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 49 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 50 | |
| 51 | namespace art { |
| 52 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 53 | #define DCHECK_OFFSET() \ |
| 54 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 55 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 56 | |
| 57 | #define DCHECK_OFFSET_() \ |
| 58 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 59 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 60 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 61 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 62 | uint32_t image_file_location_oat_checksum, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 63 | uintptr_t image_file_location_oat_begin, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 64 | int32_t image_patch_delta, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 65 | const CompilerDriver* compiler, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 66 | ImageWriter* image_writer, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 67 | TimingLogger* timings, |
| 68 | SafeMap<std::string, std::string>* key_value_store) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 69 | : compiler_driver_(compiler), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 70 | image_writer_(image_writer), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 71 | dex_files_(&dex_files), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 72 | size_(0u), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 73 | bss_size_(0u), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 74 | oat_data_offset_(0u), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 75 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 76 | image_file_location_oat_begin_(image_file_location_oat_begin), |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 77 | image_patch_delta_(image_patch_delta), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 78 | key_value_store_(key_value_store), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 79 | oat_header_(nullptr), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 80 | size_dex_file_alignment_(0), |
| 81 | size_executable_offset_alignment_(0), |
| 82 | size_oat_header_(0), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 83 | size_oat_header_key_value_store_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 84 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 85 | size_interpreter_to_interpreter_bridge_(0), |
| 86 | size_interpreter_to_compiled_code_bridge_(0), |
| 87 | size_jni_dlsym_lookup_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 88 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 89 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 90 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 91 | size_quick_to_interpreter_bridge_(0), |
| 92 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 93 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 94 | size_code_(0), |
| 95 | size_code_alignment_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 96 | size_relative_call_thunks_(0), |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 97 | size_misc_thunks_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 98 | size_mapping_table_(0), |
| 99 | size_vmap_table_(0), |
| 100 | size_gc_map_(0), |
| 101 | size_oat_dex_file_location_size_(0), |
| 102 | size_oat_dex_file_location_data_(0), |
| 103 | size_oat_dex_file_location_checksum_(0), |
| 104 | size_oat_dex_file_offset_(0), |
| 105 | size_oat_dex_file_methods_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 106 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 107 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 108 | size_oat_class_method_bitmaps_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 109 | size_oat_class_method_offsets_(0), |
| 110 | method_offset_map_() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 111 | CHECK(key_value_store != nullptr); |
| 112 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 113 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
| 114 | const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures(); |
| 115 | relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features, |
| 116 | &method_offset_map_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 117 | |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 118 | size_t offset; |
| 119 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 120 | TimingLogger::ScopedTiming split("InitOatHeader", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 121 | offset = InitOatHeader(); |
| 122 | } |
| 123 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 124 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 125 | offset = InitOatDexFiles(offset); |
| 126 | } |
| 127 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 128 | TimingLogger::ScopedTiming split("InitDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 129 | offset = InitDexFiles(offset); |
| 130 | } |
| 131 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 132 | TimingLogger::ScopedTiming split("InitOatClasses", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 133 | offset = InitOatClasses(offset); |
| 134 | } |
| 135 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 136 | TimingLogger::ScopedTiming split("InitOatMaps", timings); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 137 | offset = InitOatMaps(offset); |
| 138 | } |
| 139 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 140 | TimingLogger::ScopedTiming split("InitOatCode", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 141 | offset = InitOatCode(offset); |
| 142 | } |
| 143 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 144 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 145 | offset = InitOatCodeDexFiles(offset); |
| 146 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 147 | size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 148 | |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 149 | if (!HasImage()) { |
| 150 | // Allocate space for app dex cache arrays in the .bss section. |
| 151 | size_t bss_start = RoundUp(size_, kPageSize); |
| 152 | size_t pointer_size = GetInstructionSetPointerSize(instruction_set); |
| 153 | bss_size_ = 0u; |
| 154 | for (const DexFile* dex_file : dex_files) { |
| 155 | dex_cache_arrays_offsets_.Put(dex_file, bss_start + bss_size_); |
| 156 | DexCacheArraysLayout layout(pointer_size, dex_file); |
| 157 | bss_size_ += layout.Size(); |
| 158 | } |
| 159 | } |
| 160 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 161 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 162 | CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 163 | CHECK_EQ(compiler->IsImage(), |
| 164 | key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 165 | CHECK_ALIGNED(image_patch_delta_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 168 | OatWriter::~OatWriter() { |
| 169 | delete oat_header_; |
| 170 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 171 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 174 | struct OatWriter::GcMapDataAccess { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 175 | static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 176 | return compiled_method->GetGcMap(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 180 | uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_offset_; |
| 181 | return offset == 0u ? 0u : |
| 182 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 186 | ALWAYS_INLINE { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 187 | oat_class->method_headers_[method_offsets_index].gc_map_offset_ = |
| 188 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 191 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 192 | return "GC map"; |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | struct OatWriter::MappingTableDataAccess { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 197 | static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 198 | return compiled_method->GetMappingTable(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 202 | uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_; |
| 203 | return offset == 0u ? 0u : |
| 204 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 208 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 209 | oat_class->method_headers_[method_offsets_index].mapping_table_offset_ = |
| 210 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 213 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 214 | return "mapping table"; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | struct OatWriter::VmapTableDataAccess { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 219 | static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 220 | return compiled_method->GetVmapTable(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 224 | uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_; |
| 225 | return offset == 0u ? 0u : |
| 226 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 230 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 231 | oat_class->method_headers_[method_offsets_index].vmap_table_offset_ = |
| 232 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 235 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 236 | return "vmap table"; |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | class OatWriter::DexMethodVisitor { |
| 241 | public: |
| 242 | DexMethodVisitor(OatWriter* writer, size_t offset) |
| 243 | : writer_(writer), |
| 244 | offset_(offset), |
| 245 | dex_file_(nullptr), |
| 246 | class_def_index_(DexFile::kDexNoIndex) { |
| 247 | } |
| 248 | |
| 249 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 250 | DCHECK(dex_file_ == nullptr); |
| 251 | DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex); |
| 252 | dex_file_ = dex_file; |
| 253 | class_def_index_ = class_def_index; |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 258 | |
| 259 | virtual bool EndClass() { |
| 260 | if (kIsDebugBuild) { |
| 261 | dex_file_ = nullptr; |
| 262 | class_def_index_ = DexFile::kDexNoIndex; |
| 263 | } |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | size_t GetOffset() const { |
| 268 | return offset_; |
| 269 | } |
| 270 | |
| 271 | protected: |
| 272 | virtual ~DexMethodVisitor() { } |
| 273 | |
| 274 | OatWriter* const writer_; |
| 275 | |
| 276 | // The offset is usually advanced for each visited method by the derived class. |
| 277 | size_t offset_; |
| 278 | |
| 279 | // The dex file and class def index are set in StartClass(). |
| 280 | const DexFile* dex_file_; |
| 281 | size_t class_def_index_; |
| 282 | }; |
| 283 | |
| 284 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 285 | public: |
| 286 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
| 287 | : DexMethodVisitor(writer, offset), |
| 288 | oat_class_index_(0u), |
| 289 | method_offsets_index_(0u) { |
| 290 | } |
| 291 | |
| 292 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 293 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 294 | DCHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 295 | method_offsets_index_ = 0u; |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | bool EndClass() { |
| 300 | ++oat_class_index_; |
| 301 | return DexMethodVisitor::EndClass(); |
| 302 | } |
| 303 | |
| 304 | protected: |
| 305 | size_t oat_class_index_; |
| 306 | size_t method_offsets_index_; |
| 307 | }; |
| 308 | |
| 309 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 310 | public: |
| 311 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
| 312 | : DexMethodVisitor(writer, offset), |
| 313 | compiled_methods_(), |
| 314 | num_non_null_compiled_methods_(0u) { |
| 315 | compiled_methods_.reserve(256u); |
| 316 | } |
| 317 | |
| 318 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 319 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 320 | compiled_methods_.clear(); |
| 321 | num_non_null_compiled_methods_ = 0u; |
| 322 | return true; |
| 323 | } |
| 324 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 325 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, const ClassDataItemIterator& it) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 326 | // Fill in the compiled_methods_ array for methods that have a |
| 327 | // CompiledMethod. We track the number of non-null entries in |
| 328 | // num_non_null_compiled_methods_ since we only want to allocate |
| 329 | // OatMethodOffsets for the compiled methods. |
| 330 | uint32_t method_idx = it.GetMemberIndex(); |
| 331 | CompiledMethod* compiled_method = |
| 332 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 333 | compiled_methods_.push_back(compiled_method); |
| 334 | if (compiled_method != nullptr) { |
| 335 | ++num_non_null_compiled_methods_; |
| 336 | } |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | bool EndClass() { |
| 341 | ClassReference class_ref(dex_file_, class_def_index_); |
| 342 | CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref); |
| 343 | mirror::Class::Status status; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 344 | if (compiled_class != nullptr) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 345 | status = compiled_class->GetStatus(); |
| 346 | } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 347 | status = mirror::Class::kStatusError; |
| 348 | } else { |
| 349 | status = mirror::Class::kStatusNotReady; |
| 350 | } |
| 351 | |
| 352 | OatClass* oat_class = new OatClass(offset_, compiled_methods_, |
| 353 | num_non_null_compiled_methods_, status); |
| 354 | writer_->oat_classes_.push_back(oat_class); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 355 | oat_class->UpdateChecksum(writer_->oat_header_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 356 | offset_ += oat_class->SizeOf(); |
| 357 | return DexMethodVisitor::EndClass(); |
| 358 | } |
| 359 | |
| 360 | private: |
| 361 | std::vector<CompiledMethod*> compiled_methods_; |
| 362 | size_t num_non_null_compiled_methods_; |
| 363 | }; |
| 364 | |
| 365 | class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor { |
| 366 | public: |
| 367 | InitCodeMethodVisitor(OatWriter* writer, size_t offset) |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 368 | : OatDexMethodVisitor(writer, offset), |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 369 | debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 370 | writer_->absolute_patch_locations_.reserve( |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 371 | writer_->compiler_driver_->GetNonRelativeLinkerPatchCount()); |
| 372 | } |
| 373 | |
| 374 | bool EndClass() { |
| 375 | OatDexMethodVisitor::EndClass(); |
| 376 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 377 | offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 378 | } |
| 379 | return true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 383 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 384 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 385 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 386 | |
| 387 | if (compiled_method != nullptr) { |
| 388 | // Derived from CompiledMethod. |
| 389 | uint32_t quick_code_offset = 0; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 390 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 391 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 392 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 393 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 394 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 395 | // Deduplicate code arrays if we are not producing debuggable code. |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 396 | bool deduped = false; |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 397 | if (debuggable_) { |
| 398 | quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset); |
| 399 | } else { |
| 400 | auto lb = dedupe_map_.lower_bound(compiled_method); |
| 401 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) { |
| 402 | quick_code_offset = lb->second; |
| 403 | deduped = true; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 404 | } else { |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 405 | quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset); |
| 406 | dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset); |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 407 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 408 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 409 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 410 | if (code_size != 0) { |
| 411 | MethodReference method_ref(dex_file_, it.GetMemberIndex()); |
| 412 | auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref); |
| 413 | if (method_lb != writer_->method_offset_map_.map.end() && |
| 414 | !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) { |
| 415 | // TODO: Should this be a hard failure? |
| 416 | LOG(WARNING) << "Multiple definitions of " |
| 417 | << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file) |
| 418 | << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : ""); |
| 419 | } else { |
| 420 | writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset); |
| 421 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 422 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 423 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 424 | // Update quick method header. |
| 425 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 426 | OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
| 427 | uint32_t mapping_table_offset = method_header->mapping_table_offset_; |
| 428 | uint32_t vmap_table_offset = method_header->vmap_table_offset_; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 429 | // If we don't have quick code, then we must have a vmap, as that is how the dex2dex |
| 430 | // compiler records its transformations. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 431 | DCHECK(!quick_code.empty() || vmap_table_offset != 0); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 432 | uint32_t gc_map_offset = method_header->gc_map_offset_; |
| 433 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 434 | // to 0-offset and we need to adjust it by code_offset. |
| 435 | uint32_t code_offset = quick_code_offset - thumb_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 436 | if (mapping_table_offset != 0u && code_offset != 0u) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 437 | mapping_table_offset += code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 438 | DCHECK_LT(mapping_table_offset, code_offset) << "Overflow in oat offsets"; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 439 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 440 | if (vmap_table_offset != 0u && code_offset != 0u) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 441 | vmap_table_offset += code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 442 | DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets"; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 443 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 444 | if (gc_map_offset != 0u && code_offset != 0u) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 445 | gc_map_offset += code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 446 | DCHECK_LT(gc_map_offset, code_offset) << "Overflow in oat offsets"; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 447 | } |
| 448 | uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 449 | uint32_t core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 450 | uint32_t fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 451 | *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset, |
| 452 | gc_map_offset, frame_size_in_bytes, core_spill_mask, |
| 453 | fp_spill_mask, code_size); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 454 | |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 455 | if (!deduped) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 456 | // Update offsets. (Checksum is updated when writing.) |
| 457 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
| 458 | offset_ += code_size; |
| 459 | // Record absolute patch locations. |
| 460 | if (!compiled_method->GetPatches().empty()) { |
| 461 | uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset(); |
| 462 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 463 | if (!patch.IsPcRelative()) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 464 | writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 465 | } |
| 466 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 467 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 468 | } |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 469 | |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 470 | if (writer_->compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 471 | // Record debug information for this function if we are doing that. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 472 | const uint32_t quick_code_start = quick_code_offset - |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 473 | writer_->oat_header_->GetExecutableOffset() - thumb_offset; |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 474 | writer_->method_info_.push_back(DebugInfo { |
| 475 | dex_file_, |
| 476 | class_def_index_, |
| 477 | it.GetMemberIndex(), |
| 478 | it.GetMethodAccessFlags(), |
| 479 | it.GetMethodCodeItem(), |
| 480 | deduped, |
| 481 | quick_code_start, |
| 482 | quick_code_start + code_size, |
| 483 | compiled_method}); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 484 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 485 | |
| 486 | if (kIsDebugBuild) { |
| 487 | // We expect GC maps except when the class hasn't been verified or the method is native. |
| 488 | const CompilerDriver* compiler_driver = writer_->compiler_driver_; |
| 489 | ClassReference class_ref(dex_file_, class_def_index_); |
| 490 | CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref); |
| 491 | mirror::Class::Status status; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 492 | if (compiled_class != nullptr) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 493 | status = compiled_class->GetStatus(); |
| 494 | } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 495 | status = mirror::Class::kStatusError; |
| 496 | } else { |
| 497 | status = mirror::Class::kStatusNotReady; |
| 498 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 499 | ArrayRef<const uint8_t> gc_map = compiled_method->GetGcMap(); |
| 500 | if (!gc_map.empty()) { |
| 501 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 502 | bool is_native = it.MemberIsNative(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 503 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 504 | << gc_map_size << " " << (is_native ? "true" : "false") << " " |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 505 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 506 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 507 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 511 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 512 | offsets->code_offset_ = quick_code_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 513 | ++method_offsets_index_; |
| 514 | } |
| 515 | |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | private: |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 520 | struct CodeOffsetsKeyComparator { |
| 521 | bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 522 | // Code is deduplicated by CompilerDriver, compare only data pointers. |
| 523 | if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) { |
| 524 | return lhs->GetQuickCode().data() < rhs->GetQuickCode().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 525 | } |
| 526 | // If the code is the same, all other fields are likely to be the same as well. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 527 | if (UNLIKELY(lhs->GetMappingTable().data() != rhs->GetMappingTable().data())) { |
| 528 | return lhs->GetMappingTable().data() < rhs->GetMappingTable().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 529 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 530 | if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) { |
| 531 | return lhs->GetVmapTable().data() < rhs->GetVmapTable().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 532 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 533 | if (UNLIKELY(lhs->GetGcMap().data() != rhs->GetGcMap().data())) { |
| 534 | return lhs->GetGcMap().data() < rhs->GetGcMap().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 535 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 536 | if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) { |
| 537 | return lhs->GetPatches().data() < rhs->GetPatches().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 538 | } |
| 539 | return false; |
| 540 | } |
| 541 | }; |
| 542 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 543 | uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method, |
| 544 | const ClassDataItemIterator& it, |
| 545 | uint32_t thumb_offset) { |
| 546 | offset_ = writer_->relative_patcher_->ReserveSpace( |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 547 | offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex())); |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 548 | offset_ = compiled_method->AlignCode(offset_); |
| 549 | DCHECK_ALIGNED_PARAM(offset_, |
| 550 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 551 | return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
| 552 | } |
| 553 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 554 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 555 | // so we can simply compare the pointers to find out if things are duplicated. |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 556 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 557 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 558 | // Cache of compiler's --debuggable option. |
| 559 | const bool debuggable_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 560 | }; |
| 561 | |
| 562 | template <typename DataAccess> |
| 563 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 564 | public: |
| 565 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 566 | : OatDexMethodVisitor(writer, offset) { |
| 567 | } |
| 568 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 569 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 570 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 571 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 572 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 573 | |
| 574 | if (compiled_method != nullptr) { |
| 575 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 576 | DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u); |
| 577 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 578 | ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method); |
| 579 | uint32_t map_size = map.size() * sizeof(map[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 580 | if (map_size != 0u) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 581 | auto lb = dedupe_map_.lower_bound(map.data()); |
| 582 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map.data(), lb->first)) { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 583 | DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 584 | } else { |
| 585 | DataAccess::SetOffset(oat_class, method_offsets_index_, offset_); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 586 | dedupe_map_.PutBefore(lb, map.data(), offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 587 | offset_ += map_size; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 588 | writer_->oat_header_->UpdateChecksum(&map[0], map_size); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | ++method_offsets_index_; |
| 592 | } |
| 593 | |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | private: |
| 598 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 599 | // so we can simply compare the pointers to find out if things are duplicated. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 600 | SafeMap<const uint8_t*, uint32_t> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 601 | }; |
| 602 | |
| 603 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 604 | public: |
| 605 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 606 | : OatDexMethodVisitor(writer, offset), |
| 607 | pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 611 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 612 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 613 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 614 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 615 | OatMethodOffsets offsets(0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 616 | if (compiled_method != nullptr) { |
| 617 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 618 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 619 | ++method_offsets_index_; |
| 620 | } |
| 621 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 622 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 623 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 624 | // Unchecked as we hold mutator_lock_ on entry. |
| 625 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 626 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 627 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache( |
| 628 | Thread::Current(), *dex_file_))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 629 | ArtMethod* method = linker->ResolveMethod( |
| 630 | *dex_file_, it.GetMemberIndex(), dex_cache, NullHandle<mirror::ClassLoader>(), nullptr, |
| 631 | invoke_type); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 632 | if (method == nullptr) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 633 | LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: " |
| 634 | << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 635 | soa.Self()->AssertPendingException(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 636 | mirror::Throwable* exc = soa.Self()->GetException(); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 637 | std::string dump = exc->Dump(); |
| 638 | LOG(FATAL) << dump; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 639 | UNREACHABLE(); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 640 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 641 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 642 | if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 643 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 644 | reinterpret_cast<void*>(offsets.code_offset_), pointer_size_); |
| 645 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 646 | |
| 647 | return true; |
| 648 | } |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 649 | |
| 650 | protected: |
| 651 | const size_t pointer_size_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 655 | public: |
| 656 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 657 | size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 658 | : OatDexMethodVisitor(writer, relative_offset), |
| 659 | out_(out), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 660 | file_offset_(file_offset), |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 661 | soa_(Thread::Current()), |
| 662 | no_thread_suspension_(soa_.Self(), "OatWriter patching"), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 663 | class_linker_(Runtime::Current()->GetClassLinker()), |
| 664 | dex_cache_(nullptr) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 665 | patched_code_.reserve(16 * KB); |
| 666 | if (writer_->HasImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 667 | // If we're creating the image, the address space must be ready so that we can apply patches. |
| 668 | CHECK(writer_->image_writer_->IsImageAddressSpaceReady()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 669 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 672 | ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | bool StartClass(const DexFile* dex_file, size_t class_def_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 676 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 677 | OatDexMethodVisitor::StartClass(dex_file, class_def_index); |
| 678 | if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 679 | dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 680 | } |
| 681 | return true; |
| 682 | } |
| 683 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 684 | bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 685 | bool result = OatDexMethodVisitor::EndClass(); |
| 686 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
| 687 | DCHECK(result); // OatDexMethodVisitor::EndClass() never fails. |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 688 | offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 689 | if (UNLIKELY(offset_ == 0u)) { |
| 690 | PLOG(ERROR) << "Failed to write final relative call thunks"; |
| 691 | result = false; |
| 692 | } |
| 693 | } |
| 694 | return result; |
| 695 | } |
| 696 | |
| 697 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 698 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 699 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 700 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 701 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 702 | // No thread suspension since dex_cache_ that may get invalidated if that occurs. |
| 703 | ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 704 | if (compiled_method != nullptr) { // ie. not an abstract method |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 705 | size_t file_offset = file_offset_; |
| 706 | OutputStream* out = out_; |
| 707 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 708 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 709 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 710 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 711 | // Deduplicate code arrays. |
| 712 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 713 | if (method_offsets.code_offset_ > offset_) { |
| 714 | offset_ = writer_->relative_patcher_->WriteThunks(out, offset_); |
| 715 | if (offset_ == 0u) { |
| 716 | ReportWriteFailure("relative call thunk", it); |
| 717 | return false; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 718 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 719 | uint32_t aligned_offset = compiled_method->AlignCode(offset_); |
| 720 | uint32_t aligned_code_delta = aligned_offset - offset_; |
| 721 | if (aligned_code_delta != 0) { |
| 722 | if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) { |
| 723 | ReportWriteFailure("code alignment padding", it); |
| 724 | return false; |
| 725 | } |
| 726 | offset_ += aligned_code_delta; |
| 727 | DCHECK_OFFSET_(); |
| 728 | } |
| 729 | DCHECK_ALIGNED_PARAM(offset_, |
| 730 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 731 | DCHECK_EQ(method_offsets.code_offset_, |
| 732 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
| 733 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 734 | const OatQuickMethodHeader& method_header = |
| 735 | oat_class->method_headers_[method_offsets_index_]; |
| 736 | writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header)); |
| 737 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
| 738 | ReportWriteFailure("method header", it); |
| 739 | return false; |
| 740 | } |
| 741 | writer_->size_method_header_ += sizeof(method_header); |
| 742 | offset_ += sizeof(method_header); |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 743 | DCHECK_OFFSET_(); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 744 | |
| 745 | if (!compiled_method->GetPatches().empty()) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 746 | patched_code_.assign(quick_code.begin(), quick_code.end()); |
| 747 | quick_code = ArrayRef<const uint8_t>(patched_code_); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 748 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 749 | if (patch.Type() == kLinkerPatchCallRelative) { |
| 750 | // NOTE: Relative calls across oat files are not supported. |
| 751 | uint32_t target_offset = GetTargetOffset(patch); |
| 752 | uint32_t literal_offset = patch.LiteralOffset(); |
| 753 | writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset, |
| 754 | offset_ + literal_offset, target_offset); |
| 755 | } else if (patch.Type() == kLinkerPatchDexCacheArray) { |
| 756 | uint32_t target_offset = GetDexCacheOffset(patch); |
| 757 | uint32_t literal_offset = patch.LiteralOffset(); |
| 758 | writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch, |
| 759 | offset_ + literal_offset, |
| 760 | target_offset); |
| 761 | } else if (patch.Type() == kLinkerPatchCall) { |
| 762 | uint32_t target_offset = GetTargetOffset(patch); |
| 763 | PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset); |
| 764 | } else if (patch.Type() == kLinkerPatchMethod) { |
| 765 | ArtMethod* method = GetTargetMethod(patch); |
| 766 | PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method); |
| 767 | } else if (patch.Type() == kLinkerPatchType) { |
| 768 | mirror::Class* type = GetTargetType(patch); |
| 769 | PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 774 | writer_->oat_header_->UpdateChecksum(quick_code.data(), code_size); |
| 775 | if (!out->WriteFully(quick_code.data(), code_size)) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 776 | ReportWriteFailure("method code", it); |
| 777 | return false; |
| 778 | } |
| 779 | writer_->size_code_ += code_size; |
| 780 | offset_ += code_size; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 781 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 782 | DCHECK_OFFSET_(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 783 | ++method_offsets_index_; |
| 784 | } |
| 785 | |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | private: |
| 790 | OutputStream* const out_; |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 791 | const size_t file_offset_; |
| 792 | const ScopedObjectAccess soa_; |
| 793 | const ScopedAssertNoThreadSuspension no_thread_suspension_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 794 | ClassLinker* const class_linker_; |
| 795 | mirror::DexCache* dex_cache_; |
| 796 | std::vector<uint8_t> patched_code_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 797 | |
| 798 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 799 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 800 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 801 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 802 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 803 | ArtMethod* GetTargetMethod(const LinkerPatch& patch) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 804 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 805 | MethodReference ref = patch.TargetMethod(); |
| 806 | mirror::DexCache* dex_cache = |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 807 | (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache( |
| 808 | Thread::Current(), *ref.dex_file); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 809 | ArtMethod* method = dex_cache->GetResolvedMethod( |
| 810 | ref.dex_method_index, class_linker_->GetImagePointerSize()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 811 | CHECK(method != nullptr); |
| 812 | return method; |
| 813 | } |
| 814 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 815 | uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 816 | auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 817 | uint32_t target_offset = |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 818 | (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 819 | // If there's no compiled code, point to the correct trampoline. |
| 820 | if (UNLIKELY(target_offset == 0)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 821 | ArtMethod* target = GetTargetMethod(patch); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 822 | DCHECK(target != nullptr); |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 823 | size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet()); |
| 824 | const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size); |
| 825 | if (oat_code_offset != 0) { |
| 826 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset)); |
| 827 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset)); |
| 828 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset)); |
| 829 | target_offset = PointerToLowMemUInt32(oat_code_offset); |
| 830 | } else { |
| 831 | target_offset = target->IsNative() |
| 832 | ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset() |
| 833 | : writer_->oat_header_->GetQuickToInterpreterBridgeOffset(); |
| 834 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 835 | } |
| 836 | return target_offset; |
| 837 | } |
| 838 | |
| 839 | mirror::Class* GetTargetType(const LinkerPatch& patch) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 840 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 841 | mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile()) |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 842 | ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 843 | mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex()); |
| 844 | CHECK(type != nullptr); |
| 845 | return type; |
| 846 | } |
| 847 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 848 | uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 849 | if (writer_->HasImage()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 850 | auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<const uint8_t*>( |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 851 | patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset()); |
| 852 | const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 853 | return element - oat_data; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 854 | } else { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 855 | size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile()); |
| 856 | return start + patch.TargetDexCacheElementOffset(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 860 | void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 861 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 862 | if (writer_->HasImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 863 | object = writer_->image_writer_->GetImageAddress(object); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 864 | } else { |
| 865 | // NOTE: We're using linker patches for app->boot references when the image can |
| 866 | // be relocated and therefore we need to emit .oat_patches. We're not using this |
| 867 | // for app->app references, so check that the object is in the image space. |
| 868 | DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 869 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 870 | // Note: We only patch targeting Objects in image which is in the low 4gb. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 871 | uint32_t address = PointerToLowMemUInt32(object); |
| 872 | DCHECK_LE(offset + 4, code->size()); |
| 873 | uint8_t* data = &(*code)[offset]; |
| 874 | data[0] = address & 0xffu; |
| 875 | data[1] = (address >> 8) & 0xffu; |
| 876 | data[2] = (address >> 16) & 0xffu; |
| 877 | data[3] = (address >> 24) & 0xffu; |
| 878 | } |
| 879 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 880 | void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 881 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 882 | if (writer_->HasImage()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 883 | method = writer_->image_writer_->GetImageMethodAddress(method); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 884 | } else if (kIsDebugBuild) { |
| 885 | // NOTE: We're using linker patches for app->boot references when the image can |
| 886 | // be relocated and therefore we need to emit .oat_patches. We're not using this |
| 887 | // for app->app references, so check that the method is an image method. |
| 888 | gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 889 | size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin(); |
| 890 | CHECK(image_space->GetImageHeader().GetMethodsSection().Contains(method_offset)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 891 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 892 | // Note: We only patch targeting ArtMethods in image which is in the low 4gb. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 893 | uint32_t address = PointerToLowMemUInt32(method); |
| 894 | DCHECK_LE(offset + 4, code->size()); |
| 895 | uint8_t* data = &(*code)[offset]; |
| 896 | data[0] = address & 0xffu; |
| 897 | data[1] = (address >> 8) & 0xffu; |
| 898 | data[2] = (address >> 16) & 0xffu; |
| 899 | data[3] = (address >> 24) & 0xffu; |
| 900 | } |
| 901 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 902 | void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 903 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 904 | uint32_t address = target_offset; |
| 905 | if (writer_->HasImage()) { |
| 906 | address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() + |
| 907 | writer_->oat_data_offset_ + target_offset); |
| 908 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 909 | DCHECK_LE(offset + 4, code->size()); |
| 910 | uint8_t* data = &(*code)[offset]; |
| 911 | data[0] = address & 0xffu; |
| 912 | data[1] = (address >> 8) & 0xffu; |
| 913 | data[2] = (address >> 16) & 0xffu; |
| 914 | data[3] = (address >> 24) & 0xffu; |
| 915 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 916 | }; |
| 917 | |
| 918 | template <typename DataAccess> |
| 919 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 920 | public: |
| 921 | WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 922 | size_t relative_offset) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 923 | : OatDexMethodVisitor(writer, relative_offset), |
| 924 | out_(out), |
| 925 | file_offset_(file_offset) { |
| 926 | } |
| 927 | |
| 928 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 929 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 930 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 931 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 932 | if (compiled_method != nullptr) { // ie. not an abstract method |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 933 | size_t file_offset = file_offset_; |
| 934 | OutputStream* out = out_; |
| 935 | |
| 936 | uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_); |
| 937 | ++method_offsets_index_; |
| 938 | |
| 939 | // Write deduplicated map. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 940 | ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method); |
| 941 | size_t map_size = map.size() * sizeof(map[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 942 | DCHECK((map_size == 0u && map_offset == 0u) || |
| 943 | (map_size != 0u && map_offset != 0u && map_offset <= offset_)) |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 944 | << map_size << " " << map_offset << " " << offset_ << " " |
| 945 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 946 | if (map_size != 0u && map_offset == offset_) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame^] | 947 | if (UNLIKELY(!out->WriteFully(&map[0], map_size))) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 948 | ReportWriteFailure(it); |
| 949 | return false; |
| 950 | } |
| 951 | offset_ += map_size; |
| 952 | } |
| 953 | DCHECK_OFFSET_(); |
| 954 | } |
| 955 | |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | private: |
| 960 | OutputStream* const out_; |
| 961 | size_t const file_offset_; |
| 962 | |
| 963 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 964 | PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for " |
| 965 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 966 | } |
| 967 | }; |
| 968 | |
| 969 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 970 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 971 | for (const DexFile* dex_file : *dex_files_) { |
| 972 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 973 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 974 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 975 | return false; |
| 976 | } |
| 977 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 978 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 979 | if (class_data != nullptr) { // ie not an empty class, such as a marker interface |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 980 | ClassDataItemIterator it(*dex_file, class_data); |
| 981 | while (it.HasNextStaticField()) { |
| 982 | it.Next(); |
| 983 | } |
| 984 | while (it.HasNextInstanceField()) { |
| 985 | it.Next(); |
| 986 | } |
| 987 | size_t class_def_method_index = 0u; |
| 988 | while (it.HasNextDirectMethod()) { |
| 989 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 990 | return false; |
| 991 | } |
| 992 | ++class_def_method_index; |
| 993 | it.Next(); |
| 994 | } |
| 995 | while (it.HasNextVirtualMethod()) { |
| 996 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 997 | return false; |
| 998 | } |
| 999 | ++class_def_method_index; |
| 1000 | it.Next(); |
| 1001 | } |
| 1002 | } |
| 1003 | if (UNLIKELY(!visitor->EndClass())) { |
| 1004 | return false; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | return true; |
| 1009 | } |
| 1010 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1011 | size_t OatWriter::InitOatHeader() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1012 | oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(), |
| 1013 | compiler_driver_->GetInstructionSetFeatures(), |
| 1014 | dex_files_, |
| 1015 | image_file_location_oat_checksum_, |
| 1016 | image_file_location_oat_begin_, |
| 1017 | key_value_store_); |
| 1018 | |
| 1019 | return oat_header_->GetHeaderSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 1023 | // create the OatDexFiles |
| 1024 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 1025 | const DexFile* dex_file = (*dex_files_)[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1026 | CHECK(dex_file != nullptr); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1027 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1028 | oat_dex_files_.push_back(oat_dex_file); |
| 1029 | offset += oat_dex_file->SizeOf(); |
| 1030 | } |
| 1031 | return offset; |
| 1032 | } |
| 1033 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1034 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 1035 | // calculate the offsets within OatDexFiles to the DexFiles |
| 1036 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 1037 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1038 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1039 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1040 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1041 | |
| 1042 | // set offset in OatDexFile to DexFile |
| 1043 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 1044 | |
| 1045 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1046 | offset += dex_file->GetHeader().file_size_; |
| 1047 | } |
| 1048 | return offset; |
| 1049 | } |
| 1050 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1051 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1052 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1053 | InitOatClassesMethodVisitor visitor(this, offset); |
| 1054 | bool success = VisitDexMethods(&visitor); |
| 1055 | CHECK(success); |
| 1056 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1057 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1058 | // Update oat_dex_files_. |
| 1059 | auto oat_class_it = oat_classes_.begin(); |
| 1060 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1061 | for (uint32_t& method_offset : oat_dex_file->methods_offsets_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1062 | DCHECK(oat_class_it != oat_classes_.end()); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1063 | method_offset = (*oat_class_it)->offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1064 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1065 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1066 | oat_dex_file->UpdateChecksum(oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1067 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1068 | CHECK(oat_class_it == oat_classes_.end()); |
| 1069 | |
| 1070 | return offset; |
| 1071 | } |
| 1072 | |
| 1073 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 1074 | #define VISIT(VisitorType) \ |
| 1075 | do { \ |
| 1076 | VisitorType visitor(this, offset); \ |
| 1077 | bool success = VisitDexMethods(&visitor); \ |
| 1078 | DCHECK(success); \ |
| 1079 | offset = visitor.GetOffset(); \ |
| 1080 | } while (false) |
| 1081 | |
| 1082 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 1083 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 1084 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 1085 | |
| 1086 | #undef VISIT |
| 1087 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1088 | return offset; |
| 1089 | } |
| 1090 | |
| 1091 | size_t OatWriter::InitOatCode(size_t offset) { |
| 1092 | // calculate the offsets within OatHeader to executable code |
| 1093 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1094 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1095 | // required to be on a new page boundary |
| 1096 | offset = RoundUp(offset, kPageSize); |
| 1097 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1098 | size_executable_offset_alignment_ = offset - old_offset; |
| 1099 | if (compiler_driver_->IsImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1100 | CHECK_EQ(image_patch_delta_, 0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1101 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1102 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1103 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 1104 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1105 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 1106 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1107 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 1108 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1109 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1110 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1111 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1112 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1113 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 1114 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1115 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1116 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1117 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1118 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 1119 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 1120 | oat_header_->SetJniDlsymLookupOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1121 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1122 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1123 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1124 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1125 | oat_header_->SetImagePatchDelta(image_patch_delta_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1126 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1127 | return offset; |
| 1128 | } |
| 1129 | |
| 1130 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1131 | #define VISIT(VisitorType) \ |
| 1132 | do { \ |
| 1133 | VisitorType visitor(this, offset); \ |
| 1134 | bool success = VisitDexMethods(&visitor); \ |
| 1135 | DCHECK(success); \ |
| 1136 | offset = visitor.GetOffset(); \ |
| 1137 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1138 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1139 | VISIT(InitCodeMethodVisitor); |
Andreas Gampe | 9edb5b1 | 2015-09-21 11:46:52 -0700 | [diff] [blame] | 1140 | if (compiler_driver_->IsImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1141 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1142 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1143 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1144 | #undef VISIT |
| 1145 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1146 | return offset; |
| 1147 | } |
| 1148 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1149 | bool OatWriter::WriteRodata(OutputStream* out) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1150 | const off_t raw_file_offset = out->Seek(0, kSeekCurrent); |
| 1151 | if (raw_file_offset == (off_t) -1) { |
| 1152 | LOG(ERROR) << "Failed to get file offset in " << out->GetLocation(); |
| 1153 | return false; |
| 1154 | } |
| 1155 | const size_t file_offset = static_cast<size_t>(raw_file_offset); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1156 | oat_data_offset_ = file_offset; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1157 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1158 | // Reserve space for header. It will be written last - after updating the checksum. |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1159 | size_t header_size = oat_header_->GetHeaderSize(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1160 | if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) { |
| 1161 | PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1162 | return false; |
| 1163 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1164 | size_oat_header_ += sizeof(OatHeader); |
| 1165 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1166 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1167 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1168 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1169 | return false; |
| 1170 | } |
| 1171 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1172 | off_t tables_end_offset = out->Seek(0, kSeekCurrent); |
| 1173 | if (tables_end_offset == (off_t) -1) { |
| 1174 | LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation(); |
| 1175 | return false; |
| 1176 | } |
| 1177 | size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1178 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 1179 | if (relative_offset == 0) { |
| 1180 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1184 | // Write padding. |
| 1185 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
| 1186 | relative_offset += size_executable_offset_alignment_; |
| 1187 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
| 1188 | size_t expected_file_offset = file_offset + relative_offset; |
| 1189 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
| 1190 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
| 1191 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
| 1192 | return 0; |
| 1193 | } |
| 1194 | DCHECK_OFFSET(); |
| 1195 | |
| 1196 | return true; |
| 1197 | } |
| 1198 | |
| 1199 | bool OatWriter::WriteCode(OutputStream* out) { |
| 1200 | size_t header_size = oat_header_->GetHeaderSize(); |
| 1201 | const size_t file_offset = oat_data_offset_; |
| 1202 | size_t relative_offset = oat_header_->GetExecutableOffset(); |
| 1203 | DCHECK_OFFSET(); |
| 1204 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1205 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1206 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1207 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1208 | return false; |
| 1209 | } |
| 1210 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1211 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 1212 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1213 | LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1214 | return false; |
| 1215 | } |
| 1216 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1217 | const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent); |
| 1218 | if (oat_end_file_offset == (off_t) -1) { |
| 1219 | LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation(); |
| 1220 | return false; |
| 1221 | } |
| 1222 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1223 | if (kIsDebugBuild) { |
| 1224 | uint32_t size_total = 0; |
| 1225 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1226 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1227 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1228 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1229 | DO_STAT(size_dex_file_alignment_); |
| 1230 | DO_STAT(size_executable_offset_alignment_); |
| 1231 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1232 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1233 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1234 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 1235 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 1236 | DO_STAT(size_jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1237 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1238 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1239 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1240 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 1241 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1242 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1243 | DO_STAT(size_code_); |
| 1244 | DO_STAT(size_code_alignment_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1245 | DO_STAT(size_relative_call_thunks_); |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 1246 | DO_STAT(size_misc_thunks_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1247 | DO_STAT(size_mapping_table_); |
| 1248 | DO_STAT(size_vmap_table_); |
| 1249 | DO_STAT(size_gc_map_); |
| 1250 | DO_STAT(size_oat_dex_file_location_size_); |
| 1251 | DO_STAT(size_oat_dex_file_location_data_); |
| 1252 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 1253 | DO_STAT(size_oat_dex_file_offset_); |
| 1254 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1255 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1256 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1257 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1258 | DO_STAT(size_oat_class_method_offsets_); |
| 1259 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1260 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1261 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1262 | CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset)); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1263 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1264 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1265 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1266 | CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset)); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1267 | CHECK_EQ(size_, relative_offset); |
| 1268 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1269 | // Write the header now that the checksum is final. |
| 1270 | if (out->Seek(file_offset, kSeekSet) == (off_t) -1) { |
| 1271 | PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation(); |
| 1272 | return false; |
| 1273 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1274 | DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent))); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1275 | if (!out->WriteFully(oat_header_, header_size)) { |
| 1276 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
| 1277 | return false; |
| 1278 | } |
| 1279 | if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) { |
| 1280 | PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation(); |
| 1281 | return false; |
| 1282 | } |
| 1283 | DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent)); |
| 1284 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1285 | return true; |
| 1286 | } |
| 1287 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1288 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1289 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1290 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1291 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1292 | return false; |
| 1293 | } |
| 1294 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1295 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1296 | uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1297 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1298 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1299 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1300 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 1301 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 1302 | return false; |
| 1303 | } |
| 1304 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1305 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1306 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1307 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1308 | return false; |
| 1309 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1310 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1311 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1312 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1313 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1314 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1315 | return false; |
| 1316 | } |
| 1317 | } |
| 1318 | return true; |
| 1319 | } |
| 1320 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1321 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 1322 | #define VISIT(VisitorType) \ |
| 1323 | do { \ |
| 1324 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1325 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1326 | return 0; \ |
| 1327 | } \ |
| 1328 | relative_offset = visitor.GetOffset(); \ |
| 1329 | } while (false) |
| 1330 | |
| 1331 | size_t gc_maps_offset = relative_offset; |
| 1332 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 1333 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 1334 | |
| 1335 | size_t mapping_tables_offset = relative_offset; |
| 1336 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1337 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1338 | |
| 1339 | size_t vmap_tables_offset = relative_offset; |
| 1340 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1341 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1342 | |
| 1343 | #undef VISIT |
| 1344 | |
| 1345 | return relative_offset; |
| 1346 | } |
| 1347 | |
| 1348 | size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1349 | if (compiler_driver_->IsImage()) { |
| 1350 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1351 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1352 | #define DO_TRAMPOLINE(field) \ |
| 1353 | do { \ |
| 1354 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1355 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1356 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1357 | size_trampoline_alignment_ += alignment_padding; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1358 | if (!out->WriteFully(&(*field)[0], field->size())) { \ |
| 1359 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1360 | return false; \ |
| 1361 | } \ |
| 1362 | size_ ## field += field->size(); \ |
| 1363 | relative_offset += alignment_padding + field->size(); \ |
| 1364 | DCHECK_OFFSET(); \ |
| 1365 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1366 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1367 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1368 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1369 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1370 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1371 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1372 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1373 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1374 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1377 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1378 | const size_t file_offset, |
| 1379 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1380 | #define VISIT(VisitorType) \ |
| 1381 | do { \ |
| 1382 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1383 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1384 | return 0; \ |
| 1385 | } \ |
| 1386 | relative_offset = visitor.GetOffset(); \ |
| 1387 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1388 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1389 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1390 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1391 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1392 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1393 | size_code_alignment_ += relative_patcher_->CodeAlignmentSize(); |
| 1394 | size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize(); |
| 1395 | size_misc_thunks_ += relative_patcher_->MiscThunksSize(); |
| 1396 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1397 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1400 | bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
| 1401 | static const uint8_t kPadding[] = { |
| 1402 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 1403 | }; |
| 1404 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 1405 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 1406 | return false; |
| 1407 | } |
| 1408 | size_code_alignment_ += aligned_code_delta; |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1412 | std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) { |
| 1413 | auto it = map.find(ref); |
| 1414 | if (it == map.end()) { |
| 1415 | return std::pair<bool, uint32_t>(false, 0u); |
| 1416 | } else { |
| 1417 | return std::pair<bool, uint32_t>(true, it->second); |
| 1418 | } |
| 1419 | } |
| 1420 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1421 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1422 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1423 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1424 | dex_file_location_size_ = location.size(); |
| 1425 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1426 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1427 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1428 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1432 | return sizeof(dex_file_location_size_) |
| 1433 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1434 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1435 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1436 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1439 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const { |
| 1440 | oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 1441 | oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 1442 | oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
| 1443 | oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
| 1444 | oat_header->UpdateChecksum(&methods_offsets_[0], |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1445 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1448 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1449 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1450 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1451 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1452 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 1453 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1454 | return false; |
| 1455 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1456 | oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1457 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 1458 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1459 | return false; |
| 1460 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1461 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1462 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 1463 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1464 | return false; |
| 1465 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1466 | oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1467 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 1468 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1469 | return false; |
| 1470 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1471 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1472 | if (!out->WriteFully(&methods_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1473 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1474 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1475 | return false; |
| 1476 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1477 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 1478 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1479 | return true; |
| 1480 | } |
| 1481 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1482 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1483 | const std::vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1484 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1485 | mirror::Class::Status status) |
| 1486 | : compiled_methods_(compiled_methods) { |
| 1487 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1488 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1489 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1490 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1491 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1492 | |
| 1493 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1494 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1495 | // methods means kOatClassNoneCompiled and that we won't use |
| 1496 | // kOatClassAllCompiled unless there is at least one compiled |
| 1497 | // method. This means in an interpretter only system, we can assert |
| 1498 | // that all classes are kOatClassNoneCompiled. |
| 1499 | if (num_non_null_compiled_methods == 0) { |
| 1500 | type_ = kOatClassNoneCompiled; |
| 1501 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1502 | type_ = kOatClassAllCompiled; |
| 1503 | } else { |
| 1504 | type_ = kOatClassSomeCompiled; |
| 1505 | } |
| 1506 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1507 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1508 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1509 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1510 | |
| 1511 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1512 | if (type_ == kOatClassSomeCompiled) { |
| 1513 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 1514 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1515 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1516 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1517 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1518 | method_bitmap_ = nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1519 | method_bitmap_size_ = 0; |
| 1520 | } |
| 1521 | |
| 1522 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1523 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1524 | if (compiled_method == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1525 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1526 | } else { |
| 1527 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1528 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1529 | if (type_ == kOatClassSomeCompiled) { |
| 1530 | method_bitmap_->SetBit(i); |
| 1531 | } |
| 1532 | } |
| 1533 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1536 | OatWriter::OatClass::~OatClass() { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 1537 | delete method_bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1540 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1541 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1542 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1543 | if (method_offset == 0) { |
| 1544 | return 0; |
| 1545 | } |
| 1546 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1550 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1551 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1555 | return sizeof(status_) |
| 1556 | + sizeof(type_) |
| 1557 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1558 | + method_bitmap_size_ |
| 1559 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1562 | void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const { |
| 1563 | oat_header->UpdateChecksum(&status_, sizeof(status_)); |
| 1564 | oat_header->UpdateChecksum(&type_, sizeof(type_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1565 | if (method_bitmap_size_ != 0) { |
| 1566 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1567 | oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1568 | oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1569 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1570 | oat_header->UpdateChecksum(&method_offsets_[0], |
| 1571 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1574 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1575 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1576 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1577 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1578 | if (!out->WriteFully(&status_, sizeof(status_))) { |
| 1579 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1580 | return false; |
| 1581 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1582 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1583 | if (!out->WriteFully(&type_, sizeof(type_))) { |
| 1584 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1585 | return false; |
| 1586 | } |
| 1587 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1588 | if (method_bitmap_size_ != 0) { |
| 1589 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1590 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1591 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1592 | return false; |
| 1593 | } |
| 1594 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1595 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1596 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1597 | return false; |
| 1598 | } |
| 1599 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1600 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1601 | if (!out->WriteFully(&method_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1602 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1603 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1604 | return false; |
| 1605 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1606 | oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1607 | return true; |
| 1608 | } |
| 1609 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1610 | } // namespace art |