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