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