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" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 36 | #include "handle_scope-inl.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 37 | #include "image_writer.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 38 | #include "linker/relative_patcher.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "mirror/array.h" |
| 40 | #include "mirror/class_loader.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 41 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 42 | #include "mirror/object-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 43 | #include "oat_quick_method_header.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 44 | #include "os.h" |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 45 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 46 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | #include "scoped_thread_state_change.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 48 | #include "type_lookup_table.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 49 | #include "utils/dex_cache_arrays_layout-inl.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 50 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | |
| 52 | namespace art { |
| 53 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 54 | #define DCHECK_OFFSET() \ |
| 55 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 56 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 57 | |
| 58 | #define DCHECK_OFFSET_() \ |
| 59 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 60 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 61 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 62 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 63 | uint32_t image_file_location_oat_checksum, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 64 | uintptr_t image_file_location_oat_begin, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 65 | int32_t image_patch_delta, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 66 | const CompilerDriver* compiler, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 67 | ImageWriter* image_writer, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 68 | TimingLogger* timings, |
| 69 | SafeMap<std::string, std::string>* key_value_store) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 70 | : compiler_driver_(compiler), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 71 | image_writer_(image_writer), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 72 | dex_files_(&dex_files), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 73 | size_(0u), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 74 | bss_size_(0u), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 75 | oat_data_offset_(0u), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 76 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 77 | image_file_location_oat_begin_(image_file_location_oat_begin), |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 78 | image_patch_delta_(image_patch_delta), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 79 | key_value_store_(key_value_store), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 80 | oat_header_(nullptr), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 81 | size_dex_file_alignment_(0), |
| 82 | size_executable_offset_alignment_(0), |
| 83 | size_oat_header_(0), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 84 | size_oat_header_key_value_store_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 85 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 86 | size_interpreter_to_interpreter_bridge_(0), |
| 87 | size_interpreter_to_compiled_code_bridge_(0), |
| 88 | size_jni_dlsym_lookup_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 89 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 90 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 91 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 92 | size_quick_to_interpreter_bridge_(0), |
| 93 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 94 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 95 | size_code_(0), |
| 96 | size_code_alignment_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 97 | size_relative_call_thunks_(0), |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 98 | size_misc_thunks_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 99 | size_mapping_table_(0), |
| 100 | size_vmap_table_(0), |
| 101 | size_gc_map_(0), |
| 102 | size_oat_dex_file_location_size_(0), |
| 103 | size_oat_dex_file_location_data_(0), |
| 104 | size_oat_dex_file_location_checksum_(0), |
| 105 | size_oat_dex_file_offset_(0), |
| 106 | size_oat_dex_file_methods_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 107 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 108 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 109 | size_oat_class_method_bitmaps_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 110 | size_oat_class_method_offsets_(0), |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 111 | size_oat_lookup_table_alignment_(0), |
| 112 | size_oat_lookup_table_offset_(0), |
| 113 | size_oat_lookup_table_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 114 | method_offset_map_() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 115 | CHECK(key_value_store != nullptr); |
| 116 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 117 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
| 118 | const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures(); |
| 119 | relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features, |
| 120 | &method_offset_map_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 121 | |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 122 | size_t offset; |
| 123 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 124 | TimingLogger::ScopedTiming split("InitOatHeader", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 125 | offset = InitOatHeader(); |
| 126 | } |
| 127 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 128 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 129 | offset = InitOatDexFiles(offset); |
| 130 | } |
| 131 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 132 | TimingLogger::ScopedTiming split("InitDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 133 | offset = InitDexFiles(offset); |
| 134 | } |
| 135 | { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 136 | TimingLogger::ScopedTiming split("InitLookupTables", timings); |
| 137 | offset = InitLookupTables(offset); |
| 138 | } |
| 139 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 140 | TimingLogger::ScopedTiming split("InitOatClasses", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 141 | offset = InitOatClasses(offset); |
| 142 | } |
| 143 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 144 | TimingLogger::ScopedTiming split("InitOatMaps", timings); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 145 | offset = InitOatMaps(offset); |
| 146 | } |
| 147 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 148 | TimingLogger::ScopedTiming split("InitOatCode", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 149 | offset = InitOatCode(offset); |
| 150 | } |
| 151 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 152 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 153 | offset = InitOatCodeDexFiles(offset); |
| 154 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 155 | size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 156 | |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 157 | if (!HasImage()) { |
| 158 | // Allocate space for app dex cache arrays in the .bss section. |
| 159 | size_t bss_start = RoundUp(size_, kPageSize); |
| 160 | size_t pointer_size = GetInstructionSetPointerSize(instruction_set); |
| 161 | bss_size_ = 0u; |
| 162 | for (const DexFile* dex_file : dex_files) { |
| 163 | dex_cache_arrays_offsets_.Put(dex_file, bss_start + bss_size_); |
| 164 | DexCacheArraysLayout layout(pointer_size, dex_file); |
| 165 | bss_size_ += layout.Size(); |
| 166 | } |
| 167 | } |
| 168 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 169 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 170 | CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 171 | CHECK_EQ(compiler->IsImage(), |
| 172 | key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 173 | CHECK_ALIGNED(image_patch_delta_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 176 | OatWriter::~OatWriter() { |
| 177 | delete oat_header_; |
| 178 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 179 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 182 | struct OatWriter::GcMapDataAccess { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 183 | static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 184 | return compiled_method->GetGcMap(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | 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] | 188 | uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_offset_; |
| 189 | return offset == 0u ? 0u : |
| 190 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 194 | ALWAYS_INLINE { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 195 | oat_class->method_headers_[method_offsets_index].gc_map_offset_ = |
| 196 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 199 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 200 | return "GC map"; |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | struct OatWriter::MappingTableDataAccess { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 205 | static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 206 | return compiled_method->GetMappingTable(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | 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] | 210 | uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_; |
| 211 | return offset == 0u ? 0u : |
| 212 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 216 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 217 | oat_class->method_headers_[method_offsets_index].mapping_table_offset_ = |
| 218 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 221 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 222 | return "mapping table"; |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | struct OatWriter::VmapTableDataAccess { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 227 | static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 228 | return compiled_method->GetVmapTable(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | 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] | 232 | uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_; |
| 233 | return offset == 0u ? 0u : |
| 234 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 238 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 239 | oat_class->method_headers_[method_offsets_index].vmap_table_offset_ = |
| 240 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 243 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 244 | return "vmap table"; |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | class OatWriter::DexMethodVisitor { |
| 249 | public: |
| 250 | DexMethodVisitor(OatWriter* writer, size_t offset) |
| 251 | : writer_(writer), |
| 252 | offset_(offset), |
| 253 | dex_file_(nullptr), |
| 254 | class_def_index_(DexFile::kDexNoIndex) { |
| 255 | } |
| 256 | |
| 257 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 258 | DCHECK(dex_file_ == nullptr); |
| 259 | DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex); |
| 260 | dex_file_ = dex_file; |
| 261 | class_def_index_ = class_def_index; |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 266 | |
| 267 | virtual bool EndClass() { |
| 268 | if (kIsDebugBuild) { |
| 269 | dex_file_ = nullptr; |
| 270 | class_def_index_ = DexFile::kDexNoIndex; |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | size_t GetOffset() const { |
| 276 | return offset_; |
| 277 | } |
| 278 | |
| 279 | protected: |
| 280 | virtual ~DexMethodVisitor() { } |
| 281 | |
| 282 | OatWriter* const writer_; |
| 283 | |
| 284 | // The offset is usually advanced for each visited method by the derived class. |
| 285 | size_t offset_; |
| 286 | |
| 287 | // The dex file and class def index are set in StartClass(). |
| 288 | const DexFile* dex_file_; |
| 289 | size_t class_def_index_; |
| 290 | }; |
| 291 | |
| 292 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 293 | public: |
| 294 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
| 295 | : DexMethodVisitor(writer, offset), |
| 296 | oat_class_index_(0u), |
| 297 | method_offsets_index_(0u) { |
| 298 | } |
| 299 | |
| 300 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 301 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 302 | DCHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 303 | method_offsets_index_ = 0u; |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | bool EndClass() { |
| 308 | ++oat_class_index_; |
| 309 | return DexMethodVisitor::EndClass(); |
| 310 | } |
| 311 | |
| 312 | protected: |
| 313 | size_t oat_class_index_; |
| 314 | size_t method_offsets_index_; |
| 315 | }; |
| 316 | |
| 317 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 318 | public: |
| 319 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
| 320 | : DexMethodVisitor(writer, offset), |
| 321 | compiled_methods_(), |
| 322 | num_non_null_compiled_methods_(0u) { |
| 323 | compiled_methods_.reserve(256u); |
| 324 | } |
| 325 | |
| 326 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 327 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 328 | compiled_methods_.clear(); |
| 329 | num_non_null_compiled_methods_ = 0u; |
| 330 | return true; |
| 331 | } |
| 332 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 333 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, |
| 334 | const ClassDataItemIterator& it) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 335 | // Fill in the compiled_methods_ array for methods that have a |
| 336 | // CompiledMethod. We track the number of non-null entries in |
| 337 | // num_non_null_compiled_methods_ since we only want to allocate |
| 338 | // OatMethodOffsets for the compiled methods. |
| 339 | uint32_t method_idx = it.GetMemberIndex(); |
| 340 | CompiledMethod* compiled_method = |
| 341 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 342 | compiled_methods_.push_back(compiled_method); |
| 343 | if (compiled_method != nullptr) { |
| 344 | ++num_non_null_compiled_methods_; |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | bool EndClass() { |
| 350 | ClassReference class_ref(dex_file_, class_def_index_); |
| 351 | CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref); |
| 352 | mirror::Class::Status status; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 353 | if (compiled_class != nullptr) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 354 | status = compiled_class->GetStatus(); |
| 355 | } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 356 | status = mirror::Class::kStatusError; |
| 357 | } else { |
| 358 | status = mirror::Class::kStatusNotReady; |
| 359 | } |
| 360 | |
| 361 | OatClass* oat_class = new OatClass(offset_, compiled_methods_, |
| 362 | num_non_null_compiled_methods_, status); |
| 363 | writer_->oat_classes_.push_back(oat_class); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 364 | oat_class->UpdateChecksum(writer_->oat_header_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 365 | offset_ += oat_class->SizeOf(); |
| 366 | return DexMethodVisitor::EndClass(); |
| 367 | } |
| 368 | |
| 369 | private: |
| 370 | std::vector<CompiledMethod*> compiled_methods_; |
| 371 | size_t num_non_null_compiled_methods_; |
| 372 | }; |
| 373 | |
| 374 | class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor { |
| 375 | public: |
| 376 | InitCodeMethodVisitor(OatWriter* writer, size_t offset) |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 377 | : OatDexMethodVisitor(writer, offset), |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 378 | debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 379 | writer_->absolute_patch_locations_.reserve( |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 380 | writer_->compiler_driver_->GetNonRelativeLinkerPatchCount()); |
| 381 | } |
| 382 | |
| 383 | bool EndClass() { |
| 384 | OatDexMethodVisitor::EndClass(); |
| 385 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 386 | offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 387 | } |
| 388 | return true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 392 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 393 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 394 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 395 | |
| 396 | if (compiled_method != nullptr) { |
| 397 | // Derived from CompiledMethod. |
| 398 | uint32_t quick_code_offset = 0; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 399 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 400 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 401 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 402 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 403 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 404 | // Deduplicate code arrays if we are not producing debuggable code. |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 405 | bool deduped = false; |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 406 | if (debuggable_) { |
| 407 | quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset); |
| 408 | } else { |
| 409 | auto lb = dedupe_map_.lower_bound(compiled_method); |
| 410 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) { |
| 411 | quick_code_offset = lb->second; |
| 412 | deduped = true; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 413 | } else { |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 414 | quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset); |
| 415 | dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset); |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 416 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 417 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 418 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 419 | if (code_size != 0) { |
| 420 | MethodReference method_ref(dex_file_, it.GetMemberIndex()); |
| 421 | auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref); |
| 422 | if (method_lb != writer_->method_offset_map_.map.end() && |
| 423 | !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) { |
| 424 | // TODO: Should this be a hard failure? |
| 425 | LOG(WARNING) << "Multiple definitions of " |
| 426 | << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file) |
| 427 | << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : ""); |
| 428 | } else { |
| 429 | writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset); |
| 430 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 431 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 432 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 433 | // Update quick method header. |
| 434 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 435 | OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
| 436 | uint32_t mapping_table_offset = method_header->mapping_table_offset_; |
| 437 | uint32_t vmap_table_offset = method_header->vmap_table_offset_; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 438 | // If we don't have quick code, then we must have a vmap, as that is how the dex2dex |
| 439 | // compiler records its transformations. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 440 | DCHECK(!quick_code.empty() || vmap_table_offset != 0); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 441 | uint32_t gc_map_offset = method_header->gc_map_offset_; |
| 442 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 443 | // to 0-offset and we need to adjust it by code_offset. |
| 444 | uint32_t code_offset = quick_code_offset - thumb_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 445 | if (mapping_table_offset != 0u && code_offset != 0u) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 446 | mapping_table_offset += code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 447 | DCHECK_LT(mapping_table_offset, code_offset) << "Overflow in oat offsets"; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 448 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 449 | if (vmap_table_offset != 0u && code_offset != 0u) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 450 | vmap_table_offset += code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 451 | DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets"; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 452 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 453 | if (gc_map_offset != 0u && code_offset != 0u) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 454 | gc_map_offset += code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 455 | DCHECK_LT(gc_map_offset, code_offset) << "Overflow in oat offsets"; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 456 | } |
| 457 | uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 458 | uint32_t core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 459 | uint32_t fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 460 | *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset, |
| 461 | gc_map_offset, frame_size_in_bytes, core_spill_mask, |
| 462 | fp_spill_mask, code_size); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 463 | |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 464 | if (!deduped) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 465 | // Update offsets. (Checksum is updated when writing.) |
| 466 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
| 467 | offset_ += code_size; |
| 468 | // Record absolute patch locations. |
| 469 | if (!compiled_method->GetPatches().empty()) { |
| 470 | uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset(); |
| 471 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 472 | if (!patch.IsPcRelative()) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 473 | writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 474 | } |
| 475 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 476 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 477 | } |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 478 | |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 479 | if (writer_->compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 480 | // Record debug information for this function if we are doing that. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 481 | const uint32_t quick_code_start = quick_code_offset - |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 482 | writer_->oat_header_->GetExecutableOffset() - thumb_offset; |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 483 | writer_->method_info_.push_back(DebugInfo { |
| 484 | dex_file_, |
| 485 | class_def_index_, |
| 486 | it.GetMemberIndex(), |
| 487 | it.GetMethodAccessFlags(), |
| 488 | it.GetMethodCodeItem(), |
| 489 | deduped, |
| 490 | quick_code_start, |
| 491 | quick_code_start + code_size, |
| 492 | compiled_method}); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 493 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 494 | |
| 495 | if (kIsDebugBuild) { |
| 496 | // We expect GC maps except when the class hasn't been verified or the method is native. |
| 497 | const CompilerDriver* compiler_driver = writer_->compiler_driver_; |
| 498 | ClassReference class_ref(dex_file_, class_def_index_); |
| 499 | CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref); |
| 500 | mirror::Class::Status status; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 501 | if (compiled_class != nullptr) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 502 | status = compiled_class->GetStatus(); |
| 503 | } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 504 | status = mirror::Class::kStatusError; |
| 505 | } else { |
| 506 | status = mirror::Class::kStatusNotReady; |
| 507 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 508 | ArrayRef<const uint8_t> gc_map = compiled_method->GetGcMap(); |
| 509 | if (!gc_map.empty()) { |
| 510 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 511 | bool is_native = it.MemberIsNative(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 512 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 513 | << gc_map_size << " " << (is_native ? "true" : "false") << " " |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 514 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 515 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 516 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 520 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 521 | offsets->code_offset_ = quick_code_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 522 | ++method_offsets_index_; |
| 523 | } |
| 524 | |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | private: |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 529 | struct CodeOffsetsKeyComparator { |
| 530 | bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 531 | // Code is deduplicated by CompilerDriver, compare only data pointers. |
| 532 | if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) { |
| 533 | return lhs->GetQuickCode().data() < rhs->GetQuickCode().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 534 | } |
| 535 | // If the code is the same, all other fields are likely to be the same as well. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 536 | if (UNLIKELY(lhs->GetMappingTable().data() != rhs->GetMappingTable().data())) { |
| 537 | return lhs->GetMappingTable().data() < rhs->GetMappingTable().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 538 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 539 | if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) { |
| 540 | return lhs->GetVmapTable().data() < rhs->GetVmapTable().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 541 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 542 | if (UNLIKELY(lhs->GetGcMap().data() != rhs->GetGcMap().data())) { |
| 543 | return lhs->GetGcMap().data() < rhs->GetGcMap().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 544 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 545 | if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) { |
| 546 | return lhs->GetPatches().data() < rhs->GetPatches().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 547 | } |
| 548 | return false; |
| 549 | } |
| 550 | }; |
| 551 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 552 | uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method, |
| 553 | const ClassDataItemIterator& it, |
| 554 | uint32_t thumb_offset) { |
| 555 | offset_ = writer_->relative_patcher_->ReserveSpace( |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 556 | offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex())); |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 557 | offset_ = compiled_method->AlignCode(offset_); |
| 558 | DCHECK_ALIGNED_PARAM(offset_, |
| 559 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 560 | return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
| 561 | } |
| 562 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 563 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 564 | // 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] | 565 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 566 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 567 | // Cache of compiler's --debuggable option. |
| 568 | const bool debuggable_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | template <typename DataAccess> |
| 572 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 573 | public: |
| 574 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 575 | : OatDexMethodVisitor(writer, offset) { |
| 576 | } |
| 577 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 578 | 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] | 579 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 580 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 581 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 582 | |
| 583 | if (compiled_method != nullptr) { |
| 584 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 585 | DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u); |
| 586 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 587 | ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method); |
| 588 | uint32_t map_size = map.size() * sizeof(map[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 589 | if (map_size != 0u) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 590 | auto lb = dedupe_map_.lower_bound(map.data()); |
| 591 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map.data(), lb->first)) { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 592 | DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 593 | } else { |
| 594 | DataAccess::SetOffset(oat_class, method_offsets_index_, offset_); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 595 | dedupe_map_.PutBefore(lb, map.data(), offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 596 | offset_ += map_size; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 597 | writer_->oat_header_->UpdateChecksum(&map[0], map_size); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | ++method_offsets_index_; |
| 601 | } |
| 602 | |
| 603 | return true; |
| 604 | } |
| 605 | |
| 606 | private: |
| 607 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 608 | // so we can simply compare the pointers to find out if things are duplicated. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 609 | SafeMap<const uint8_t*, uint32_t> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 610 | }; |
| 611 | |
| 612 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 613 | public: |
| 614 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 615 | : OatDexMethodVisitor(writer, offset), |
| 616 | pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 620 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 621 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 622 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 623 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 624 | OatMethodOffsets offsets(0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 625 | if (compiled_method != nullptr) { |
| 626 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 627 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 628 | ++method_offsets_index_; |
| 629 | } |
| 630 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 631 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 632 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 633 | // Unchecked as we hold mutator_lock_ on entry. |
| 634 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 635 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 636 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache( |
| 637 | Thread::Current(), *dex_file_))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 638 | ArtMethod* method = linker->ResolveMethod( |
| 639 | *dex_file_, it.GetMemberIndex(), dex_cache, NullHandle<mirror::ClassLoader>(), nullptr, |
| 640 | invoke_type); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 641 | if (method == nullptr) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 642 | LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: " |
| 643 | << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 644 | soa.Self()->AssertPendingException(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 645 | mirror::Throwable* exc = soa.Self()->GetException(); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 646 | std::string dump = exc->Dump(); |
| 647 | LOG(FATAL) << dump; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 648 | UNREACHABLE(); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 649 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 650 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 651 | if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 652 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 653 | reinterpret_cast<void*>(offsets.code_offset_), pointer_size_); |
| 654 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 655 | |
| 656 | return true; |
| 657 | } |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 658 | |
| 659 | protected: |
| 660 | const size_t pointer_size_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 664 | public: |
| 665 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 666 | size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 667 | : OatDexMethodVisitor(writer, relative_offset), |
| 668 | out_(out), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 669 | file_offset_(file_offset), |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 670 | soa_(Thread::Current()), |
| 671 | no_thread_suspension_(soa_.Self(), "OatWriter patching"), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 672 | class_linker_(Runtime::Current()->GetClassLinker()), |
| 673 | dex_cache_(nullptr) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 674 | patched_code_.reserve(16 * KB); |
| 675 | if (writer_->HasImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 676 | // If we're creating the image, the address space must be ready so that we can apply patches. |
| 677 | CHECK(writer_->image_writer_->IsImageAddressSpaceReady()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 678 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 681 | ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | bool StartClass(const DexFile* dex_file, size_t class_def_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 685 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 686 | OatDexMethodVisitor::StartClass(dex_file, class_def_index); |
| 687 | if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 688 | dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 689 | } |
| 690 | return true; |
| 691 | } |
| 692 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 693 | bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 694 | bool result = OatDexMethodVisitor::EndClass(); |
| 695 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
| 696 | DCHECK(result); // OatDexMethodVisitor::EndClass() never fails. |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 697 | offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 698 | if (UNLIKELY(offset_ == 0u)) { |
| 699 | PLOG(ERROR) << "Failed to write final relative call thunks"; |
| 700 | result = false; |
| 701 | } |
| 702 | } |
| 703 | return result; |
| 704 | } |
| 705 | |
| 706 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 707 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 708 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 709 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 710 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 711 | // No thread suspension since dex_cache_ that may get invalidated if that occurs. |
| 712 | ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 713 | if (compiled_method != nullptr) { // ie. not an abstract method |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 714 | size_t file_offset = file_offset_; |
| 715 | OutputStream* out = out_; |
| 716 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 717 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 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()) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 755 | patched_code_.assign(quick_code.begin(), quick_code.end()); |
| 756 | quick_code = ArrayRef<const uint8_t>(patched_code_); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 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 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 783 | writer_->oat_header_->UpdateChecksum(quick_code.data(), code_size); |
| 784 | if (!out->WriteFully(quick_code.data(), code_size)) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 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. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 949 | ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method); |
| 950 | size_t map_size = 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_) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 956 | if (UNLIKELY(!out->WriteFully(&map[0], map_size))) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 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]; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1055 | |
| 1056 | // Initialize type lookup table |
| 1057 | oat_dex_files_[i]->lookup_table_ = dex_file->GetTypeLookupTable(); |
| 1058 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1059 | offset += dex_file->GetHeader().file_size_; |
| 1060 | } |
| 1061 | return offset; |
| 1062 | } |
| 1063 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1064 | size_t OatWriter::InitLookupTables(size_t offset) { |
| 1065 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
| 1066 | if (oat_dex_file->lookup_table_ != nullptr) { |
| 1067 | uint32_t aligned_offset = RoundUp(offset, 4); |
| 1068 | oat_dex_file->lookup_table_offset_ = aligned_offset; |
| 1069 | size_oat_lookup_table_alignment_ += aligned_offset - offset; |
| 1070 | offset = aligned_offset + oat_dex_file->lookup_table_->RawDataLength(); |
| 1071 | } else { |
| 1072 | oat_dex_file->lookup_table_offset_ = 0; |
| 1073 | } |
| 1074 | } |
| 1075 | return offset; |
| 1076 | } |
| 1077 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1078 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1079 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1080 | InitOatClassesMethodVisitor visitor(this, offset); |
| 1081 | bool success = VisitDexMethods(&visitor); |
| 1082 | CHECK(success); |
| 1083 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1084 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1085 | // Update oat_dex_files_. |
| 1086 | auto oat_class_it = oat_classes_.begin(); |
| 1087 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1088 | for (uint32_t& method_offset : oat_dex_file->methods_offsets_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1089 | DCHECK(oat_class_it != oat_classes_.end()); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1090 | method_offset = (*oat_class_it)->offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1091 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1092 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1093 | oat_dex_file->UpdateChecksum(oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1094 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1095 | CHECK(oat_class_it == oat_classes_.end()); |
| 1096 | |
| 1097 | return offset; |
| 1098 | } |
| 1099 | |
| 1100 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 1101 | #define VISIT(VisitorType) \ |
| 1102 | do { \ |
| 1103 | VisitorType visitor(this, offset); \ |
| 1104 | bool success = VisitDexMethods(&visitor); \ |
| 1105 | DCHECK(success); \ |
| 1106 | offset = visitor.GetOffset(); \ |
| 1107 | } while (false) |
| 1108 | |
| 1109 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 1110 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 1111 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 1112 | |
| 1113 | #undef VISIT |
| 1114 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1115 | return offset; |
| 1116 | } |
| 1117 | |
| 1118 | size_t OatWriter::InitOatCode(size_t offset) { |
| 1119 | // calculate the offsets within OatHeader to executable code |
| 1120 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1121 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1122 | // required to be on a new page boundary |
| 1123 | offset = RoundUp(offset, kPageSize); |
| 1124 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1125 | size_executable_offset_alignment_ = offset - old_offset; |
| 1126 | if (compiler_driver_->IsImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1127 | CHECK_EQ(image_patch_delta_, 0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1128 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1129 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1130 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 1131 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1132 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 1133 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1134 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 1135 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1136 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1137 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1138 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1139 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1140 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 1141 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1142 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1143 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1144 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1145 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 1146 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 1147 | oat_header_->SetJniDlsymLookupOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1148 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1149 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1150 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1151 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1152 | oat_header_->SetImagePatchDelta(image_patch_delta_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1153 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1154 | return offset; |
| 1155 | } |
| 1156 | |
| 1157 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1158 | #define VISIT(VisitorType) \ |
| 1159 | do { \ |
| 1160 | VisitorType visitor(this, offset); \ |
| 1161 | bool success = VisitDexMethods(&visitor); \ |
| 1162 | DCHECK(success); \ |
| 1163 | offset = visitor.GetOffset(); \ |
| 1164 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1165 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1166 | VISIT(InitCodeMethodVisitor); |
Andreas Gampe | 9edb5b1 | 2015-09-21 11:46:52 -0700 | [diff] [blame] | 1167 | if (compiler_driver_->IsImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1168 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1169 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1170 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1171 | #undef VISIT |
| 1172 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1173 | return offset; |
| 1174 | } |
| 1175 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1176 | bool OatWriter::WriteRodata(OutputStream* out) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1177 | const off_t raw_file_offset = out->Seek(0, kSeekCurrent); |
| 1178 | if (raw_file_offset == (off_t) -1) { |
| 1179 | LOG(ERROR) << "Failed to get file offset in " << out->GetLocation(); |
| 1180 | return false; |
| 1181 | } |
| 1182 | const size_t file_offset = static_cast<size_t>(raw_file_offset); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1183 | oat_data_offset_ = file_offset; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1184 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1185 | // 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] | 1186 | size_t header_size = oat_header_->GetHeaderSize(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1187 | if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) { |
| 1188 | PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1189 | return false; |
| 1190 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1191 | size_oat_header_ += sizeof(OatHeader); |
| 1192 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1193 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1194 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1195 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1196 | return false; |
| 1197 | } |
| 1198 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1199 | off_t tables_end_offset = out->Seek(0, kSeekCurrent); |
| 1200 | if (tables_end_offset == (off_t) -1) { |
| 1201 | LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation(); |
| 1202 | return false; |
| 1203 | } |
| 1204 | 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] | 1205 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 1206 | if (relative_offset == 0) { |
| 1207 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 1208 | return false; |
| 1209 | } |
| 1210 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1211 | // Write padding. |
| 1212 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
| 1213 | relative_offset += size_executable_offset_alignment_; |
| 1214 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
| 1215 | size_t expected_file_offset = file_offset + relative_offset; |
| 1216 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
| 1217 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
| 1218 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | DCHECK_OFFSET(); |
| 1222 | |
| 1223 | return true; |
| 1224 | } |
| 1225 | |
| 1226 | bool OatWriter::WriteCode(OutputStream* out) { |
| 1227 | size_t header_size = oat_header_->GetHeaderSize(); |
| 1228 | const size_t file_offset = oat_data_offset_; |
| 1229 | size_t relative_offset = oat_header_->GetExecutableOffset(); |
| 1230 | DCHECK_OFFSET(); |
| 1231 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1232 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1233 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1234 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
| 1237 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1238 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 1239 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1240 | 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] | 1241 | return false; |
| 1242 | } |
| 1243 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1244 | const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent); |
| 1245 | if (oat_end_file_offset == (off_t) -1) { |
| 1246 | LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation(); |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1250 | if (kIsDebugBuild) { |
| 1251 | uint32_t size_total = 0; |
| 1252 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1253 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1254 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1255 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1256 | DO_STAT(size_dex_file_alignment_); |
| 1257 | DO_STAT(size_executable_offset_alignment_); |
| 1258 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1259 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1260 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1261 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 1262 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 1263 | DO_STAT(size_jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1264 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1265 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1266 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1267 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 1268 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1269 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1270 | DO_STAT(size_code_); |
| 1271 | DO_STAT(size_code_alignment_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1272 | DO_STAT(size_relative_call_thunks_); |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 1273 | DO_STAT(size_misc_thunks_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1274 | DO_STAT(size_mapping_table_); |
| 1275 | DO_STAT(size_vmap_table_); |
| 1276 | DO_STAT(size_gc_map_); |
| 1277 | DO_STAT(size_oat_dex_file_location_size_); |
| 1278 | DO_STAT(size_oat_dex_file_location_data_); |
| 1279 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 1280 | DO_STAT(size_oat_dex_file_offset_); |
| 1281 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1282 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1283 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1284 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1285 | DO_STAT(size_oat_class_method_offsets_); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1286 | DO_STAT(size_oat_lookup_table_alignment_); |
| 1287 | DO_STAT(size_oat_lookup_table_offset_); |
| 1288 | DO_STAT(size_oat_lookup_table_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1289 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1290 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1291 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1292 | 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] | 1293 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1294 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1295 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1296 | 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] | 1297 | CHECK_EQ(size_, relative_offset); |
| 1298 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1299 | // Write the header now that the checksum is final. |
| 1300 | if (out->Seek(file_offset, kSeekSet) == (off_t) -1) { |
| 1301 | PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation(); |
| 1302 | return false; |
| 1303 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1304 | DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent))); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1305 | if (!out->WriteFully(oat_header_, header_size)) { |
| 1306 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
| 1307 | return false; |
| 1308 | } |
| 1309 | if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) { |
| 1310 | PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation(); |
| 1311 | return false; |
| 1312 | } |
| 1313 | DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent)); |
| 1314 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1315 | return true; |
| 1316 | } |
| 1317 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1318 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1319 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1320 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1321 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1322 | return false; |
| 1323 | } |
| 1324 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1325 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1326 | 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] | 1327 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1328 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1329 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1330 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 1331 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 1332 | return false; |
| 1333 | } |
| 1334 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1335 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1336 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1337 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1338 | return false; |
| 1339 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1340 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1341 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1342 | if (!WriteLookupTables(out, file_offset)) { |
| 1343 | return false; |
| 1344 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1345 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1346 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1347 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1348 | return false; |
| 1349 | } |
| 1350 | } |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1354 | bool OatWriter::WriteLookupTables(OutputStream* out, const size_t file_offset) { |
| 1355 | for (size_t i = 0; i < oat_dex_files_.size(); ++i) { |
| 1356 | const uint32_t lookup_table_offset = oat_dex_files_[i]->lookup_table_offset_; |
| 1357 | const TypeLookupTable* table = oat_dex_files_[i]->lookup_table_; |
| 1358 | DCHECK_EQ(lookup_table_offset == 0, table == nullptr); |
| 1359 | if (lookup_table_offset == 0) { |
| 1360 | continue; |
| 1361 | } |
| 1362 | const uint32_t expected_offset = file_offset + lookup_table_offset; |
| 1363 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
| 1364 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1365 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1366 | PLOG(ERROR) << "Failed to seek to lookup table section. Actual: " << actual_offset |
| 1367 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 1368 | return false; |
| 1369 | } |
| 1370 | if (table != nullptr) { |
| 1371 | if (!out->WriteFully(table->RawData(), table->RawDataLength())) { |
| 1372 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1373 | PLOG(ERROR) << "Failed to write lookup table for " << dex_file->GetLocation() |
| 1374 | << " to " << out->GetLocation(); |
| 1375 | return false; |
| 1376 | } |
| 1377 | size_oat_lookup_table_ += table->RawDataLength(); |
| 1378 | } |
| 1379 | } |
| 1380 | return true; |
| 1381 | } |
| 1382 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1383 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 1384 | #define VISIT(VisitorType) \ |
| 1385 | do { \ |
| 1386 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1387 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1388 | return 0; \ |
| 1389 | } \ |
| 1390 | relative_offset = visitor.GetOffset(); \ |
| 1391 | } while (false) |
| 1392 | |
| 1393 | size_t gc_maps_offset = relative_offset; |
| 1394 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 1395 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 1396 | |
| 1397 | size_t mapping_tables_offset = relative_offset; |
| 1398 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1399 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1400 | |
| 1401 | size_t vmap_tables_offset = relative_offset; |
| 1402 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1403 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1404 | |
| 1405 | #undef VISIT |
| 1406 | |
| 1407 | return relative_offset; |
| 1408 | } |
| 1409 | |
| 1410 | 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] | 1411 | if (compiler_driver_->IsImage()) { |
| 1412 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1413 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1414 | #define DO_TRAMPOLINE(field) \ |
| 1415 | do { \ |
| 1416 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1417 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1418 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1419 | size_trampoline_alignment_ += alignment_padding; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1420 | if (!out->WriteFully(&(*field)[0], field->size())) { \ |
| 1421 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1422 | return false; \ |
| 1423 | } \ |
| 1424 | size_ ## field += field->size(); \ |
| 1425 | relative_offset += alignment_padding + field->size(); \ |
| 1426 | DCHECK_OFFSET(); \ |
| 1427 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1428 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1429 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1430 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1431 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1432 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1433 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1434 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1435 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1436 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1439 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1440 | const size_t file_offset, |
| 1441 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1442 | #define VISIT(VisitorType) \ |
| 1443 | do { \ |
| 1444 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1445 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1446 | return 0; \ |
| 1447 | } \ |
| 1448 | relative_offset = visitor.GetOffset(); \ |
| 1449 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1450 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1451 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1452 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1453 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1454 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1455 | size_code_alignment_ += relative_patcher_->CodeAlignmentSize(); |
| 1456 | size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize(); |
| 1457 | size_misc_thunks_ += relative_patcher_->MiscThunksSize(); |
| 1458 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1459 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1462 | bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
| 1463 | static const uint8_t kPadding[] = { |
| 1464 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 1465 | }; |
| 1466 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 1467 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 1468 | return false; |
| 1469 | } |
| 1470 | size_code_alignment_ += aligned_code_delta; |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1474 | std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) { |
| 1475 | auto it = map.find(ref); |
| 1476 | if (it == map.end()) { |
| 1477 | return std::pair<bool, uint32_t>(false, 0u); |
| 1478 | } else { |
| 1479 | return std::pair<bool, uint32_t>(true, it->second); |
| 1480 | } |
| 1481 | } |
| 1482 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1483 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1484 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1485 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1486 | dex_file_location_size_ = location.size(); |
| 1487 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1488 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1489 | dex_file_offset_ = 0; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1490 | lookup_table_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1491 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1495 | return sizeof(dex_file_location_size_) |
| 1496 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1497 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1498 | + sizeof(dex_file_offset_) |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1499 | + sizeof(lookup_table_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1500 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1503 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const { |
| 1504 | oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 1505 | oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 1506 | oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
| 1507 | oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1508 | oat_header->UpdateChecksum(&lookup_table_offset_, sizeof(lookup_table_offset_)); |
| 1509 | if (lookup_table_ != nullptr) { |
| 1510 | oat_header->UpdateChecksum(lookup_table_->RawData(), lookup_table_->RawDataLength()); |
| 1511 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1512 | oat_header->UpdateChecksum(&methods_offsets_[0], |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1513 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1516 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1517 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1518 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1519 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1520 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 1521 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1522 | return false; |
| 1523 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1524 | 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] | 1525 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 1526 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1527 | return false; |
| 1528 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1529 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1530 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 1531 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1532 | return false; |
| 1533 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1534 | 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] | 1535 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 1536 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1537 | return false; |
| 1538 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1539 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame^] | 1540 | if (!out->WriteFully(&lookup_table_offset_, sizeof(lookup_table_offset_))) { |
| 1541 | PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation(); |
| 1542 | return false; |
| 1543 | } |
| 1544 | oat_writer->size_oat_lookup_table_offset_ += sizeof(lookup_table_offset_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1545 | if (!out->WriteFully(&methods_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1546 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1547 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1548 | return false; |
| 1549 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1550 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 1551 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1552 | return true; |
| 1553 | } |
| 1554 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1555 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1556 | const std::vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1557 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1558 | mirror::Class::Status status) |
| 1559 | : compiled_methods_(compiled_methods) { |
| 1560 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1561 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1562 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1563 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1564 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1565 | |
| 1566 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1567 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1568 | // methods means kOatClassNoneCompiled and that we won't use |
| 1569 | // kOatClassAllCompiled unless there is at least one compiled |
| 1570 | // method. This means in an interpretter only system, we can assert |
| 1571 | // that all classes are kOatClassNoneCompiled. |
| 1572 | if (num_non_null_compiled_methods == 0) { |
| 1573 | type_ = kOatClassNoneCompiled; |
| 1574 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1575 | type_ = kOatClassAllCompiled; |
| 1576 | } else { |
| 1577 | type_ = kOatClassSomeCompiled; |
| 1578 | } |
| 1579 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1580 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1581 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1582 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1583 | |
| 1584 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1585 | if (type_ == kOatClassSomeCompiled) { |
| 1586 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 1587 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1588 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1589 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1590 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1591 | method_bitmap_ = nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1592 | method_bitmap_size_ = 0; |
| 1593 | } |
| 1594 | |
| 1595 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1596 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1597 | if (compiled_method == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1598 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1599 | } else { |
| 1600 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1601 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1602 | if (type_ == kOatClassSomeCompiled) { |
| 1603 | method_bitmap_->SetBit(i); |
| 1604 | } |
| 1605 | } |
| 1606 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1609 | OatWriter::OatClass::~OatClass() { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 1610 | delete method_bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1613 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1614 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1615 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1616 | if (method_offset == 0) { |
| 1617 | return 0; |
| 1618 | } |
| 1619 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1623 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1624 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1628 | return sizeof(status_) |
| 1629 | + sizeof(type_) |
| 1630 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1631 | + method_bitmap_size_ |
| 1632 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1635 | void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const { |
| 1636 | oat_header->UpdateChecksum(&status_, sizeof(status_)); |
| 1637 | oat_header->UpdateChecksum(&type_, sizeof(type_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1638 | if (method_bitmap_size_ != 0) { |
| 1639 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1640 | oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1641 | oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1642 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1643 | oat_header->UpdateChecksum(&method_offsets_[0], |
| 1644 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1647 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1648 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1649 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1650 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1651 | if (!out->WriteFully(&status_, sizeof(status_))) { |
| 1652 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1653 | return false; |
| 1654 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1655 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1656 | if (!out->WriteFully(&type_, sizeof(type_))) { |
| 1657 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1658 | return false; |
| 1659 | } |
| 1660 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1661 | if (method_bitmap_size_ != 0) { |
| 1662 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1663 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1664 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1665 | return false; |
| 1666 | } |
| 1667 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1668 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1669 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1670 | return false; |
| 1671 | } |
| 1672 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1673 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1674 | if (!out->WriteFully(&method_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1675 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1676 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1677 | return false; |
| 1678 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1679 | 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] | 1680 | return true; |
| 1681 | } |
| 1682 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1683 | } // namespace art |