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