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 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 21 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 25 | #include "compiled_class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 27 | #include "dex/verification_results.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | #include "gc/space/space.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 29 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/array.h" |
| 31 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 33 | #include "os.h" |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 34 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 35 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 36 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 37 | #include "handle_scope-inl.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 38 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 42 | #define DCHECK_OFFSET() \ |
| 43 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 44 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 45 | |
| 46 | #define DCHECK_OFFSET_() \ |
| 47 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 48 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 49 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 50 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 51 | uint32_t image_file_location_oat_checksum, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 52 | uintptr_t image_file_location_oat_begin, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 53 | int32_t image_patch_delta, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 54 | const CompilerDriver* compiler, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 55 | TimingLogger* timings, |
| 56 | SafeMap<std::string, std::string>* key_value_store) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 57 | : compiler_driver_(compiler), |
| 58 | dex_files_(&dex_files), |
| 59 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 60 | image_file_location_oat_begin_(image_file_location_oat_begin), |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 61 | image_patch_delta_(image_patch_delta), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 62 | key_value_store_(key_value_store), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 63 | oat_header_(NULL), |
| 64 | size_dex_file_alignment_(0), |
| 65 | size_executable_offset_alignment_(0), |
| 66 | size_oat_header_(0), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 67 | size_oat_header_key_value_store_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 68 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 69 | size_interpreter_to_interpreter_bridge_(0), |
| 70 | size_interpreter_to_compiled_code_bridge_(0), |
| 71 | size_jni_dlsym_lookup_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 72 | size_portable_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 73 | size_portable_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 74 | size_portable_to_interpreter_bridge_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 75 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 76 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 77 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 78 | size_quick_to_interpreter_bridge_(0), |
| 79 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 80 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 81 | size_code_(0), |
| 82 | size_code_alignment_(0), |
| 83 | size_mapping_table_(0), |
| 84 | size_vmap_table_(0), |
| 85 | size_gc_map_(0), |
| 86 | size_oat_dex_file_location_size_(0), |
| 87 | size_oat_dex_file_location_data_(0), |
| 88 | size_oat_dex_file_location_checksum_(0), |
| 89 | size_oat_dex_file_offset_(0), |
| 90 | size_oat_dex_file_methods_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 91 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 92 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 93 | size_oat_class_method_bitmaps_(0), |
Dave Allison | d6ed642 | 2014-04-09 23:36:15 +0000 | [diff] [blame] | 94 | size_oat_class_method_offsets_(0) { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 95 | CHECK(key_value_store != nullptr); |
| 96 | |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 97 | size_t offset; |
| 98 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 99 | TimingLogger::ScopedTiming split("InitOatHeader", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 100 | offset = InitOatHeader(); |
| 101 | } |
| 102 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 103 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 104 | offset = InitOatDexFiles(offset); |
| 105 | } |
| 106 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 107 | TimingLogger::ScopedTiming split("InitDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 108 | offset = InitDexFiles(offset); |
| 109 | } |
| 110 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 111 | TimingLogger::ScopedTiming split("InitOatClasses", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 112 | offset = InitOatClasses(offset); |
| 113 | } |
| 114 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 115 | TimingLogger::ScopedTiming split("InitOatMaps", timings); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 116 | offset = InitOatMaps(offset); |
| 117 | } |
| 118 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 119 | TimingLogger::ScopedTiming split("InitOatCode", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 120 | offset = InitOatCode(offset); |
| 121 | } |
| 122 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 123 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 124 | offset = InitOatCodeDexFiles(offset); |
| 125 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 126 | size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 127 | |
| 128 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 129 | CHECK_EQ(compiler->IsImage(), |
| 130 | key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 131 | CHECK_ALIGNED(image_patch_delta_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 134 | OatWriter::~OatWriter() { |
| 135 | delete oat_header_; |
| 136 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 137 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 140 | struct OatWriter::GcMapDataAccess { |
| 141 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 142 | return &compiled_method->GetGcMap(); |
| 143 | } |
| 144 | |
| 145 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
| 146 | return oat_class->method_offsets_[method_offsets_index].gc_map_offset_; |
| 147 | } |
| 148 | |
| 149 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 150 | ALWAYS_INLINE { |
| 151 | oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset; |
| 152 | } |
| 153 | |
| 154 | static const char* Name() ALWAYS_INLINE { |
| 155 | return "GC map"; |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | struct OatWriter::MappingTableDataAccess { |
| 160 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 161 | return &compiled_method->GetMappingTable(); |
| 162 | } |
| 163 | |
| 164 | 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] | 165 | uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_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 { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 172 | oat_class->method_headers_[method_offsets_index].mapping_table_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 | |
| 176 | static const char* Name() ALWAYS_INLINE { |
| 177 | return "mapping table"; |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | struct OatWriter::VmapTableDataAccess { |
| 182 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 183 | return &compiled_method->GetVmapTable(); |
| 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].vmap_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].vmap_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 | |
| 198 | static const char* Name() ALWAYS_INLINE { |
| 199 | return "vmap table"; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | class OatWriter::DexMethodVisitor { |
| 204 | public: |
| 205 | DexMethodVisitor(OatWriter* writer, size_t offset) |
| 206 | : writer_(writer), |
| 207 | offset_(offset), |
| 208 | dex_file_(nullptr), |
| 209 | class_def_index_(DexFile::kDexNoIndex) { |
| 210 | } |
| 211 | |
| 212 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 213 | DCHECK(dex_file_ == nullptr); |
| 214 | DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex); |
| 215 | dex_file_ = dex_file; |
| 216 | class_def_index_ = class_def_index; |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 221 | |
| 222 | virtual bool EndClass() { |
| 223 | if (kIsDebugBuild) { |
| 224 | dex_file_ = nullptr; |
| 225 | class_def_index_ = DexFile::kDexNoIndex; |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | size_t GetOffset() const { |
| 231 | return offset_; |
| 232 | } |
| 233 | |
| 234 | protected: |
| 235 | virtual ~DexMethodVisitor() { } |
| 236 | |
| 237 | OatWriter* const writer_; |
| 238 | |
| 239 | // The offset is usually advanced for each visited method by the derived class. |
| 240 | size_t offset_; |
| 241 | |
| 242 | // The dex file and class def index are set in StartClass(). |
| 243 | const DexFile* dex_file_; |
| 244 | size_t class_def_index_; |
| 245 | }; |
| 246 | |
| 247 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 248 | public: |
| 249 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
| 250 | : DexMethodVisitor(writer, offset), |
| 251 | oat_class_index_(0u), |
| 252 | method_offsets_index_(0u) { |
| 253 | } |
| 254 | |
| 255 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 256 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 257 | DCHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 258 | method_offsets_index_ = 0u; |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | bool EndClass() { |
| 263 | ++oat_class_index_; |
| 264 | return DexMethodVisitor::EndClass(); |
| 265 | } |
| 266 | |
| 267 | protected: |
| 268 | size_t oat_class_index_; |
| 269 | size_t method_offsets_index_; |
| 270 | }; |
| 271 | |
| 272 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 273 | public: |
| 274 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
| 275 | : DexMethodVisitor(writer, offset), |
| 276 | compiled_methods_(), |
| 277 | num_non_null_compiled_methods_(0u) { |
| 278 | compiled_methods_.reserve(256u); |
| 279 | } |
| 280 | |
| 281 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 282 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 283 | compiled_methods_.clear(); |
| 284 | num_non_null_compiled_methods_ = 0u; |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 289 | // Fill in the compiled_methods_ array for methods that have a |
| 290 | // CompiledMethod. We track the number of non-null entries in |
| 291 | // num_non_null_compiled_methods_ since we only want to allocate |
| 292 | // OatMethodOffsets for the compiled methods. |
| 293 | uint32_t method_idx = it.GetMemberIndex(); |
| 294 | CompiledMethod* compiled_method = |
| 295 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 296 | compiled_methods_.push_back(compiled_method); |
| 297 | if (compiled_method != nullptr) { |
| 298 | ++num_non_null_compiled_methods_; |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | bool EndClass() { |
| 304 | ClassReference class_ref(dex_file_, class_def_index_); |
| 305 | CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref); |
| 306 | mirror::Class::Status status; |
| 307 | if (compiled_class != NULL) { |
| 308 | status = compiled_class->GetStatus(); |
| 309 | } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 310 | status = mirror::Class::kStatusError; |
| 311 | } else { |
| 312 | status = mirror::Class::kStatusNotReady; |
| 313 | } |
| 314 | |
| 315 | OatClass* oat_class = new OatClass(offset_, compiled_methods_, |
| 316 | num_non_null_compiled_methods_, status); |
| 317 | writer_->oat_classes_.push_back(oat_class); |
| 318 | offset_ += oat_class->SizeOf(); |
| 319 | return DexMethodVisitor::EndClass(); |
| 320 | } |
| 321 | |
| 322 | private: |
| 323 | std::vector<CompiledMethod*> compiled_methods_; |
| 324 | size_t num_non_null_compiled_methods_; |
| 325 | }; |
| 326 | |
| 327 | class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor { |
| 328 | public: |
| 329 | InitCodeMethodVisitor(OatWriter* writer, size_t offset) |
| 330 | : OatDexMethodVisitor(writer, offset) { |
| 331 | } |
| 332 | |
| 333 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 334 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 335 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 336 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 337 | |
| 338 | if (compiled_method != nullptr) { |
| 339 | // Derived from CompiledMethod. |
| 340 | uint32_t quick_code_offset = 0; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 341 | |
| 342 | const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode(); |
| 343 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 344 | if (portable_code != nullptr) { |
| 345 | CHECK(quick_code == nullptr); |
| 346 | size_t oat_method_offsets_offset = |
| 347 | oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index); |
| 348 | compiled_method->AddOatdataOffsetToCompliledCodeOffset( |
| 349 | oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_)); |
| 350 | } else { |
| 351 | CHECK(quick_code != nullptr); |
| 352 | offset_ = compiled_method->AlignCode(offset_); |
| 353 | DCHECK_ALIGNED_PARAM(offset_, |
| 354 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 355 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 356 | CHECK_NE(code_size, 0U); |
| 357 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 358 | quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 359 | |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 360 | bool force_debug_capture = false; |
| 361 | bool deduped = false; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 362 | |
| 363 | // Deduplicate code arrays. |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 364 | auto lb = dedupe_map_.lower_bound(compiled_method); |
| 365 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) { |
| 366 | quick_code_offset = lb->second; |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 367 | deduped = true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 368 | } else { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 369 | dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // Update quick method header. |
| 373 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 374 | OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
| 375 | uint32_t mapping_table_offset = method_header->mapping_table_offset_; |
| 376 | uint32_t vmap_table_offset = method_header->vmap_table_offset_; |
| 377 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 378 | // to 0-offset and we need to adjust it by code_offset. |
| 379 | uint32_t code_offset = quick_code_offset - thumb_offset; |
| 380 | if (mapping_table_offset != 0u) { |
| 381 | mapping_table_offset += code_offset; |
| 382 | DCHECK_LT(mapping_table_offset, code_offset); |
| 383 | } |
| 384 | if (vmap_table_offset != 0u) { |
| 385 | vmap_table_offset += code_offset; |
| 386 | DCHECK_LT(vmap_table_offset, code_offset); |
| 387 | } |
| 388 | uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 389 | uint32_t core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 390 | uint32_t fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 391 | *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset, |
| 392 | frame_size_in_bytes, core_spill_mask, fp_spill_mask, |
| 393 | code_size); |
| 394 | |
| 395 | // Update checksum if this wasn't a duplicate. |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 396 | if (!deduped) { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 397 | writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header)); |
| 398 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 399 | writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 400 | offset_ += code_size; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 401 | } |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 402 | |
| 403 | uint32_t quick_code_start = quick_code_offset - writer_->oat_header_->GetExecutableOffset(); |
| 404 | std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation(); |
| 405 | if (cfi_info != nullptr) { |
| 406 | // Copy in the FDE, if present |
| 407 | const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo(); |
| 408 | if (fde != nullptr) { |
| 409 | // Copy the information into cfi_info and then fix the address in the new copy. |
| 410 | int cur_offset = cfi_info->size(); |
| 411 | cfi_info->insert(cfi_info->end(), fde->begin(), fde->end()); |
| 412 | |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 413 | // Set the 'CIE_pointer' field to cur_offset+4. |
| 414 | uint32_t CIE_pointer = cur_offset + 4; |
| 415 | uint32_t offset_to_update = cur_offset + sizeof(uint32_t); |
| 416 | (*cfi_info)[offset_to_update+0] = CIE_pointer; |
| 417 | (*cfi_info)[offset_to_update+1] = CIE_pointer >> 8; |
| 418 | (*cfi_info)[offset_to_update+2] = CIE_pointer >> 16; |
| 419 | (*cfi_info)[offset_to_update+3] = CIE_pointer >> 24; |
| 420 | |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 421 | // Set the 'initial_location' field to address the start of the method. |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 422 | offset_to_update = cur_offset + 2*sizeof(uint32_t); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 423 | (*cfi_info)[offset_to_update+0] = quick_code_start; |
| 424 | (*cfi_info)[offset_to_update+1] = quick_code_start >> 8; |
| 425 | (*cfi_info)[offset_to_update+2] = quick_code_start >> 16; |
| 426 | (*cfi_info)[offset_to_update+3] = quick_code_start >> 24; |
| 427 | force_debug_capture = true; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | |
| 432 | if (writer_->compiler_driver_->DidIncludeDebugSymbols() || force_debug_capture) { |
| 433 | // Record debug information for this function if we are doing that or |
| 434 | // we have CFI and so need it. |
| 435 | std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
| 436 | if (deduped) { |
| 437 | // TODO We should place the DEDUPED tag on the first instance of a |
| 438 | // deduplicated symbol so that it will show up in a debuggerd crash |
| 439 | // report. |
| 440 | name += " [ DEDUPED ]"; |
| 441 | } |
| 442 | writer_->method_info_.push_back(DebugInfo(name, quick_code_start, |
| 443 | quick_code_start + code_size)); |
| 444 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 445 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 446 | |
| 447 | if (kIsDebugBuild) { |
| 448 | // We expect GC maps except when the class hasn't been verified or the method is native. |
| 449 | const CompilerDriver* compiler_driver = writer_->compiler_driver_; |
| 450 | ClassReference class_ref(dex_file_, class_def_index_); |
| 451 | CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref); |
| 452 | mirror::Class::Status status; |
| 453 | if (compiled_class != NULL) { |
| 454 | status = compiled_class->GetStatus(); |
| 455 | } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 456 | status = mirror::Class::kStatusError; |
| 457 | } else { |
| 458 | status = mirror::Class::kStatusNotReady; |
| 459 | } |
| 460 | const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap(); |
| 461 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
| 462 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
| 463 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
| 464 | << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " |
| 465 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 466 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 467 | } |
| 468 | |
| 469 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 470 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 471 | offsets->code_offset_ = quick_code_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 472 | ++method_offsets_index_; |
| 473 | } |
| 474 | |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | private: |
| 479 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 480 | // 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] | 481 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | template <typename DataAccess> |
| 485 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 486 | public: |
| 487 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 488 | : OatDexMethodVisitor(writer, offset) { |
| 489 | } |
| 490 | |
| 491 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 492 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 493 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 494 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 495 | |
| 496 | if (compiled_method != nullptr) { |
| 497 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 498 | DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u); |
| 499 | |
| 500 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
| 501 | uint32_t map_size = map->size() * sizeof((*map)[0]); |
| 502 | if (map_size != 0u) { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 503 | auto lb = dedupe_map_.lower_bound(map); |
| 504 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) { |
| 505 | DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 506 | } else { |
| 507 | DataAccess::SetOffset(oat_class, method_offsets_index_, offset_); |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 508 | dedupe_map_.PutBefore(lb, map, offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 509 | offset_ += map_size; |
| 510 | writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size); |
| 511 | } |
| 512 | } |
| 513 | ++method_offsets_index_; |
| 514 | } |
| 515 | |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | private: |
| 520 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 521 | // so we can simply compare the pointers to find out if things are duplicated. |
| 522 | SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_; |
| 523 | }; |
| 524 | |
| 525 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 526 | public: |
| 527 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
| 528 | : OatDexMethodVisitor(writer, offset) { |
| 529 | } |
| 530 | |
| 531 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 532 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 533 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 534 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 535 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 536 | OatMethodOffsets offsets(0u, 0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 537 | if (compiled_method != nullptr) { |
| 538 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 539 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 540 | ++method_offsets_index_; |
| 541 | } |
| 542 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 543 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 544 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 545 | // Unchecked as we hold mutator_lock_ on entry. |
| 546 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 547 | StackHandleScope<2> hs(soa.Self()); |
| 548 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_))); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 549 | mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 550 | NullHandle<mirror::ClassLoader>(), |
| 551 | NullHandle<mirror::ArtMethod>(), |
| 552 | invoke_type); |
Brian Carlstrom | 3c911d6 | 2014-06-13 18:23:20 -0700 | [diff] [blame] | 553 | CHECK(method != NULL) << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 554 | // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking. |
| 555 | method->SetQuickOatCodeOffset(offsets.code_offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 556 | method->SetOatNativeGcMapOffset(offsets.gc_map_offset_); |
| 557 | |
| 558 | return true; |
| 559 | } |
| 560 | }; |
| 561 | |
| 562 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 563 | public: |
| 564 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
| 565 | size_t relative_offset) |
| 566 | : OatDexMethodVisitor(writer, relative_offset), |
| 567 | out_(out), |
| 568 | file_offset_(file_offset) { |
| 569 | } |
| 570 | |
| 571 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 572 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 573 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 574 | |
| 575 | if (compiled_method != NULL) { // ie. not an abstract method |
| 576 | size_t file_offset = file_offset_; |
| 577 | OutputStream* out = out_; |
| 578 | |
| 579 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 580 | if (quick_code != nullptr) { |
| 581 | CHECK(compiled_method->GetPortableCode() == nullptr); |
| 582 | uint32_t aligned_offset = compiled_method->AlignCode(offset_); |
| 583 | uint32_t aligned_code_delta = aligned_offset - offset_; |
| 584 | if (aligned_code_delta != 0) { |
| 585 | static const uint8_t kPadding[] = { |
| 586 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 587 | }; |
| 588 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 589 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 590 | ReportWriteFailure("code alignment padding", it); |
| 591 | return false; |
| 592 | } |
| 593 | writer_->size_code_alignment_ += aligned_code_delta; |
| 594 | offset_ += aligned_code_delta; |
| 595 | DCHECK_OFFSET_(); |
| 596 | } |
| 597 | DCHECK_ALIGNED_PARAM(offset_, |
| 598 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 599 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 600 | CHECK_NE(code_size, 0U); |
| 601 | |
| 602 | // Deduplicate code arrays. |
| 603 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 604 | DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ == |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 605 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 606 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 607 | if (method_offsets.code_offset_ >= offset_) { |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 608 | const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 609 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
| 610 | ReportWriteFailure("method header", it); |
| 611 | return false; |
| 612 | } |
| 613 | writer_->size_method_header_ += sizeof(method_header); |
| 614 | offset_ += sizeof(method_header); |
| 615 | DCHECK_OFFSET_(); |
| 616 | if (!out->WriteFully(&(*quick_code)[0], code_size)) { |
| 617 | ReportWriteFailure("method code", it); |
| 618 | return false; |
| 619 | } |
| 620 | writer_->size_code_ += code_size; |
| 621 | offset_ += code_size; |
| 622 | } |
| 623 | DCHECK_OFFSET_(); |
| 624 | } |
| 625 | ++method_offsets_index_; |
| 626 | } |
| 627 | |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | private: |
| 632 | OutputStream* const out_; |
| 633 | size_t const file_offset_; |
| 634 | |
| 635 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 636 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 637 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 638 | } |
| 639 | }; |
| 640 | |
| 641 | template <typename DataAccess> |
| 642 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 643 | public: |
| 644 | WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
| 645 | size_t relative_offset) |
| 646 | : OatDexMethodVisitor(writer, relative_offset), |
| 647 | out_(out), |
| 648 | file_offset_(file_offset) { |
| 649 | } |
| 650 | |
| 651 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 652 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 653 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 654 | |
| 655 | if (compiled_method != NULL) { // ie. not an abstract method |
| 656 | size_t file_offset = file_offset_; |
| 657 | OutputStream* out = out_; |
| 658 | |
| 659 | uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_); |
| 660 | ++method_offsets_index_; |
| 661 | |
| 662 | // Write deduplicated map. |
| 663 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
| 664 | size_t map_size = map->size() * sizeof((*map)[0]); |
| 665 | DCHECK((map_size == 0u && map_offset == 0u) || |
| 666 | (map_size != 0u && map_offset != 0u && map_offset <= offset_)) |
| 667 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 668 | if (map_size != 0u && map_offset == offset_) { |
| 669 | if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) { |
| 670 | ReportWriteFailure(it); |
| 671 | return false; |
| 672 | } |
| 673 | offset_ += map_size; |
| 674 | } |
| 675 | DCHECK_OFFSET_(); |
| 676 | } |
| 677 | |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | private: |
| 682 | OutputStream* const out_; |
| 683 | size_t const file_offset_; |
| 684 | |
| 685 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 686 | PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for " |
| 687 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 688 | } |
| 689 | }; |
| 690 | |
| 691 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 692 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 693 | for (const DexFile* dex_file : *dex_files_) { |
| 694 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 695 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 696 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 697 | return false; |
| 698 | } |
| 699 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 700 | const byte* class_data = dex_file->GetClassData(class_def); |
| 701 | if (class_data != NULL) { // ie not an empty class, such as a marker interface |
| 702 | ClassDataItemIterator it(*dex_file, class_data); |
| 703 | while (it.HasNextStaticField()) { |
| 704 | it.Next(); |
| 705 | } |
| 706 | while (it.HasNextInstanceField()) { |
| 707 | it.Next(); |
| 708 | } |
| 709 | size_t class_def_method_index = 0u; |
| 710 | while (it.HasNextDirectMethod()) { |
| 711 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 712 | return false; |
| 713 | } |
| 714 | ++class_def_method_index; |
| 715 | it.Next(); |
| 716 | } |
| 717 | while (it.HasNextVirtualMethod()) { |
| 718 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 719 | return false; |
| 720 | } |
| 721 | ++class_def_method_index; |
| 722 | it.Next(); |
| 723 | } |
| 724 | } |
| 725 | if (UNLIKELY(!visitor->EndClass())) { |
| 726 | return false; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | return true; |
| 731 | } |
| 732 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 733 | size_t OatWriter::InitOatHeader() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 734 | oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(), |
| 735 | compiler_driver_->GetInstructionSetFeatures(), |
| 736 | dex_files_, |
| 737 | image_file_location_oat_checksum_, |
| 738 | image_file_location_oat_begin_, |
| 739 | key_value_store_); |
| 740 | |
| 741 | return oat_header_->GetHeaderSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 745 | // create the OatDexFiles |
| 746 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 747 | const DexFile* dex_file = (*dex_files_)[i]; |
| 748 | CHECK(dex_file != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 749 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 750 | oat_dex_files_.push_back(oat_dex_file); |
| 751 | offset += oat_dex_file->SizeOf(); |
| 752 | } |
| 753 | return offset; |
| 754 | } |
| 755 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 756 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 757 | // calculate the offsets within OatDexFiles to the DexFiles |
| 758 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 759 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 760 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 761 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 762 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 763 | |
| 764 | // set offset in OatDexFile to DexFile |
| 765 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 766 | |
| 767 | const DexFile* dex_file = (*dex_files_)[i]; |
| 768 | offset += dex_file->GetHeader().file_size_; |
| 769 | } |
| 770 | return offset; |
| 771 | } |
| 772 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 773 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 774 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 775 | InitOatClassesMethodVisitor visitor(this, offset); |
| 776 | bool success = VisitDexMethods(&visitor); |
| 777 | CHECK(success); |
| 778 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 779 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 780 | // Update oat_dex_files_. |
| 781 | auto oat_class_it = oat_classes_.begin(); |
| 782 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
| 783 | for (uint32_t& offset : oat_dex_file->methods_offsets_) { |
| 784 | DCHECK(oat_class_it != oat_classes_.end()); |
| 785 | offset = (*oat_class_it)->offset_; |
| 786 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 787 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 788 | oat_dex_file->UpdateChecksum(oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 789 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 790 | CHECK(oat_class_it == oat_classes_.end()); |
| 791 | |
| 792 | return offset; |
| 793 | } |
| 794 | |
| 795 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 796 | #define VISIT(VisitorType) \ |
| 797 | do { \ |
| 798 | VisitorType visitor(this, offset); \ |
| 799 | bool success = VisitDexMethods(&visitor); \ |
| 800 | DCHECK(success); \ |
| 801 | offset = visitor.GetOffset(); \ |
| 802 | } while (false) |
| 803 | |
| 804 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 805 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 806 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 807 | |
| 808 | #undef VISIT |
| 809 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 810 | return offset; |
| 811 | } |
| 812 | |
| 813 | size_t OatWriter::InitOatCode(size_t offset) { |
| 814 | // calculate the offsets within OatHeader to executable code |
| 815 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 816 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 817 | // required to be on a new page boundary |
| 818 | offset = RoundUp(offset, kPageSize); |
| 819 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 820 | size_executable_offset_alignment_ = offset - old_offset; |
| 821 | if (compiler_driver_->IsImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 822 | CHECK_EQ(image_patch_delta_, 0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 823 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 824 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 825 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 826 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 827 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 828 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 829 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 830 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 831 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 832 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge); |
| 833 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge); |
| 834 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 835 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 836 | DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline); |
| 837 | DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 838 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 839 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 840 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 841 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 842 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 843 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 844 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 845 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 846 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 847 | oat_header_->SetJniDlsymLookupOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 848 | oat_header_->SetPortableImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 849 | oat_header_->SetPortableResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 850 | oat_header_->SetPortableToInterpreterBridgeOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 851 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 852 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 853 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 854 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 855 | oat_header_->SetImagePatchDelta(image_patch_delta_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 856 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 857 | return offset; |
| 858 | } |
| 859 | |
| 860 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 861 | #define VISIT(VisitorType) \ |
| 862 | do { \ |
| 863 | VisitorType visitor(this, offset); \ |
| 864 | bool success = VisitDexMethods(&visitor); \ |
| 865 | DCHECK(success); \ |
| 866 | offset = visitor.GetOffset(); \ |
| 867 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 868 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 869 | VISIT(InitCodeMethodVisitor); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 870 | if (compiler_driver_->IsImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 871 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 872 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 873 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 874 | #undef VISIT |
| 875 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 876 | return offset; |
| 877 | } |
| 878 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 879 | bool OatWriter::Write(OutputStream* out) { |
| 880 | const size_t file_offset = out->Seek(0, kSeekCurrent); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 881 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 882 | size_t header_size = oat_header_->GetHeaderSize(); |
| 883 | if (!out->WriteFully(oat_header_, header_size)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 884 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 885 | return false; |
| 886 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 887 | size_oat_header_ += sizeof(OatHeader); |
| 888 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 889 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 890 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 891 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 892 | return false; |
| 893 | } |
| 894 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 895 | size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset; |
| 896 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 897 | if (relative_offset == 0) { |
| 898 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 899 | return false; |
| 900 | } |
| 901 | |
| 902 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 903 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 904 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 905 | return false; |
| 906 | } |
| 907 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 908 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 909 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 910 | 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] | 911 | return false; |
| 912 | } |
| 913 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 914 | if (kIsDebugBuild) { |
| 915 | uint32_t size_total = 0; |
| 916 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 917 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 918 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 919 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 920 | DO_STAT(size_dex_file_alignment_); |
| 921 | DO_STAT(size_executable_offset_alignment_); |
| 922 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 923 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 924 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 925 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 926 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 927 | DO_STAT(size_jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 928 | DO_STAT(size_portable_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 929 | DO_STAT(size_portable_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 930 | DO_STAT(size_portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 931 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 932 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 933 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 934 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 935 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 936 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 937 | DO_STAT(size_code_); |
| 938 | DO_STAT(size_code_alignment_); |
| 939 | DO_STAT(size_mapping_table_); |
| 940 | DO_STAT(size_vmap_table_); |
| 941 | DO_STAT(size_gc_map_); |
| 942 | DO_STAT(size_oat_dex_file_location_size_); |
| 943 | DO_STAT(size_oat_dex_file_location_data_); |
| 944 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 945 | DO_STAT(size_oat_dex_file_offset_); |
| 946 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 947 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 948 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 949 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 950 | DO_STAT(size_oat_class_method_offsets_); |
| 951 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 952 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 953 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 954 | CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent))); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 955 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 956 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 957 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 958 | CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent))); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 959 | CHECK_EQ(size_, relative_offset); |
| 960 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 961 | return true; |
| 962 | } |
| 963 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 964 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 965 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 966 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 967 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 968 | return false; |
| 969 | } |
| 970 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 971 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 972 | 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] | 973 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 974 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 975 | const DexFile* dex_file = (*dex_files_)[i]; |
| 976 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 977 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 978 | return false; |
| 979 | } |
| 980 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 981 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 982 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 983 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 984 | return false; |
| 985 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 986 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 987 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 988 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 989 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 990 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 991 | return false; |
| 992 | } |
| 993 | } |
| 994 | return true; |
| 995 | } |
| 996 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 997 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 998 | #define VISIT(VisitorType) \ |
| 999 | do { \ |
| 1000 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1001 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1002 | return 0; \ |
| 1003 | } \ |
| 1004 | relative_offset = visitor.GetOffset(); \ |
| 1005 | } while (false) |
| 1006 | |
| 1007 | size_t gc_maps_offset = relative_offset; |
| 1008 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 1009 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 1010 | |
| 1011 | size_t mapping_tables_offset = relative_offset; |
| 1012 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1013 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1014 | |
| 1015 | size_t vmap_tables_offset = relative_offset; |
| 1016 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1017 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1018 | |
| 1019 | #undef VISIT |
| 1020 | |
| 1021 | return relative_offset; |
| 1022 | } |
| 1023 | |
| 1024 | size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1025 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1026 | relative_offset += size_executable_offset_alignment_; |
| 1027 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1028 | size_t expected_file_offset = file_offset + relative_offset; |
| 1029 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1030 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1031 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1032 | return 0; |
| 1033 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1034 | DCHECK_OFFSET(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1035 | if (compiler_driver_->IsImage()) { |
| 1036 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1037 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1038 | #define DO_TRAMPOLINE(field) \ |
| 1039 | do { \ |
| 1040 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1041 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1042 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1043 | size_trampoline_alignment_ += alignment_padding; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1044 | if (!out->WriteFully(&(*field)[0], field->size())) { \ |
| 1045 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1046 | return false; \ |
| 1047 | } \ |
| 1048 | size_ ## field += field->size(); \ |
| 1049 | relative_offset += alignment_padding + field->size(); \ |
| 1050 | DCHECK_OFFSET(); \ |
| 1051 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1052 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1053 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_); |
| 1054 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_); |
| 1055 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1056 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1057 | DO_TRAMPOLINE(portable_resolution_trampoline_); |
| 1058 | DO_TRAMPOLINE(portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1059 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1060 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1061 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1062 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1063 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1064 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1065 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1068 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1069 | const size_t file_offset, |
| 1070 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1071 | #define VISIT(VisitorType) \ |
| 1072 | do { \ |
| 1073 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1074 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1075 | return 0; \ |
| 1076 | } \ |
| 1077 | relative_offset = visitor.GetOffset(); \ |
| 1078 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1079 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1080 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1081 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1082 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1083 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1084 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1087 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1088 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1089 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1090 | dex_file_location_size_ = location.size(); |
| 1091 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1092 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1093 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1094 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1098 | return sizeof(dex_file_location_size_) |
| 1099 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1100 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1101 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1102 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1105 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const { |
| 1106 | oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 1107 | oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 1108 | oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
| 1109 | oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
| 1110 | oat_header->UpdateChecksum(&methods_offsets_[0], |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1111 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1114 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1115 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1116 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1117 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1118 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 1119 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1120 | return false; |
| 1121 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1122 | 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] | 1123 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 1124 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1127 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1128 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 1129 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1132 | 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] | 1133 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 1134 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1135 | return false; |
| 1136 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1137 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1138 | if (!out->WriteFully(&methods_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1139 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1140 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1141 | return false; |
| 1142 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1143 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 1144 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1145 | return true; |
| 1146 | } |
| 1147 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1148 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1149 | const std::vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1150 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1151 | mirror::Class::Status status) |
| 1152 | : compiled_methods_(compiled_methods) { |
| 1153 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1154 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1155 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1156 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1157 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1158 | |
| 1159 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1160 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1161 | // methods means kOatClassNoneCompiled and that we won't use |
| 1162 | // kOatClassAllCompiled unless there is at least one compiled |
| 1163 | // method. This means in an interpretter only system, we can assert |
| 1164 | // that all classes are kOatClassNoneCompiled. |
| 1165 | if (num_non_null_compiled_methods == 0) { |
| 1166 | type_ = kOatClassNoneCompiled; |
| 1167 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1168 | type_ = kOatClassAllCompiled; |
| 1169 | } else { |
| 1170 | type_ = kOatClassSomeCompiled; |
| 1171 | } |
| 1172 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1173 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1174 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1175 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1176 | |
| 1177 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1178 | if (type_ == kOatClassSomeCompiled) { |
| 1179 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 1180 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1181 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1182 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1183 | } else { |
| 1184 | method_bitmap_ = NULL; |
| 1185 | method_bitmap_size_ = 0; |
| 1186 | } |
| 1187 | |
| 1188 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1189 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1190 | if (compiled_method == NULL) { |
| 1191 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1192 | } else { |
| 1193 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1194 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1195 | if (type_ == kOatClassSomeCompiled) { |
| 1196 | method_bitmap_->SetBit(i); |
| 1197 | } |
| 1198 | } |
| 1199 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1202 | OatWriter::OatClass::~OatClass() { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 1203 | delete method_bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1206 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1207 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1208 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1209 | if (method_offset == 0) { |
| 1210 | return 0; |
| 1211 | } |
| 1212 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1216 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1217 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1221 | return sizeof(status_) |
| 1222 | + sizeof(type_) |
| 1223 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1224 | + method_bitmap_size_ |
| 1225 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1228 | void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const { |
| 1229 | oat_header->UpdateChecksum(&status_, sizeof(status_)); |
| 1230 | oat_header->UpdateChecksum(&type_, sizeof(type_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1231 | if (method_bitmap_size_ != 0) { |
| 1232 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1233 | oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1234 | oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1235 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1236 | oat_header->UpdateChecksum(&method_offsets_[0], |
| 1237 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1240 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1241 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1242 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1243 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1244 | if (!out->WriteFully(&status_, sizeof(status_))) { |
| 1245 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1246 | return false; |
| 1247 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1248 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1249 | if (!out->WriteFully(&type_, sizeof(type_))) { |
| 1250 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1251 | return false; |
| 1252 | } |
| 1253 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1254 | if (method_bitmap_size_ != 0) { |
| 1255 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1256 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1257 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1258 | return false; |
| 1259 | } |
| 1260 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1261 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1262 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1263 | return false; |
| 1264 | } |
| 1265 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1266 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1267 | if (!out->WriteFully(&method_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1268 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1269 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1270 | return false; |
| 1271 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1272 | 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] | 1273 | return true; |
| 1274 | } |
| 1275 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1276 | } // namespace art |