blob: dd64368abcce0b1f8a9d16b5d901280131bffb0e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_writer.h"
18
Elliott Hughesa0e18062012-04-13 15:59:59 -070019#include <zlib.h>
20
Ian Rogerse77493c2014-08-20 15:08:45 -070021#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070022#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080023#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070025#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "compiled_class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000028#include "dex/verification_results.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/space/space.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010030#include "image_writer.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070031#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/array.h"
33#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080036#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070037#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070039#include "handle_scope-inl.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010040#include "utils/arm/assembler_thumb2.h"
jeffhaoec014232012-09-05 10:42:25 -070041#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070042
43namespace art {
44
Vladimir Markof4da6752014-08-01 19:04:18 +010045class OatWriter::RelativeCallPatcher {
46 public:
47 virtual ~RelativeCallPatcher() { }
48
49 // Reserve space for relative call thunks if needed, return adjusted offset.
50 // After all methods have been processed it's call one last time with compiled_method == nullptr.
51 virtual uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method) = 0;
52
53 // Write relative call thunks if needed, return adjusted offset.
54 virtual uint32_t WriteThunks(OutputStream* out, uint32_t offset) = 0;
55
56 // Patch method code. The input displacement is relative to the patched location,
57 // the patcher may need to adjust it if the correct base is different.
58 virtual void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset,
59 uint32_t target_offset) = 0;
60
61 protected:
62 RelativeCallPatcher() { }
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(RelativeCallPatcher);
66};
67
68class OatWriter::NoRelativeCallPatcher FINAL : public RelativeCallPatcher {
69 public:
70 NoRelativeCallPatcher() { }
71
72 uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method) OVERRIDE {
73 return offset; // No space reserved; no patches expected.
74 }
75
76 uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE {
77 return offset; // No thunks added; no patches expected.
78 }
79
80 void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset,
81 uint32_t target_offset) OVERRIDE {
82 LOG(FATAL) << "Unexpected relative patch.";
83 }
84
85 private:
86 DISALLOW_COPY_AND_ASSIGN(NoRelativeCallPatcher);
87};
88
89class OatWriter::X86RelativeCallPatcher FINAL : public RelativeCallPatcher {
90 public:
91 X86RelativeCallPatcher() { }
92
93 uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method) OVERRIDE {
94 return offset; // No space reserved; no limit on relative call distance.
95 }
96
97 uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE {
98 return offset; // No thunks added; no limit on relative call distance.
99 }
100
101 void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset,
102 uint32_t target_offset) OVERRIDE {
103 DCHECK_LE(literal_offset + 4u, code->size());
104 // Unsigned arithmetic with its well-defined overflow behavior is just fine here.
105 uint32_t displacement = target_offset - patch_offset;
106 displacement -= kPcDisplacement; // The base PC is at the end of the 4-byte patch.
107
108 typedef __attribute__((__aligned__(1))) int32_t unaligned_int32_t;
109 reinterpret_cast<unaligned_int32_t*>(&(*code)[literal_offset])[0] = displacement;
110 }
111
112 private:
113 // PC displacement from patch location; x86 PC for relative calls points to the next
114 // instruction and the patch location is 4 bytes earlier.
115 static constexpr int32_t kPcDisplacement = 4;
116
117 DISALLOW_COPY_AND_ASSIGN(X86RelativeCallPatcher);
118};
119
120class OatWriter::Thumb2RelativeCallPatcher FINAL : public RelativeCallPatcher {
121 public:
122 explicit Thumb2RelativeCallPatcher(OatWriter* writer)
123 : writer_(writer), thunk_code_(CompileThunkCode()),
124 thunk_locations_(), current_thunk_to_write_(0u), unprocessed_patches_() {
125 }
126
127 uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method) OVERRIDE {
128 // NOTE: The final thunk can be reserved from InitCodeMethodVisitor::EndClass() while it
129 // may be written early by WriteCodeMethodVisitor::VisitMethod() for a deduplicated chunk
130 // of code. To avoid any alignment discrepancies for the final chunk, we always align the
131 // offset after reserving of writing any chunk.
132 if (UNLIKELY(compiled_method == nullptr)) {
133 uint32_t aligned_offset = CompiledMethod::AlignCode(offset, kThumb2);
134 bool needs_thunk = ReserveSpaceProcessPatches(aligned_offset);
135 if (needs_thunk) {
136 thunk_locations_.push_back(aligned_offset);
137 offset = CompiledMethod::AlignCode(aligned_offset + thunk_code_.size(), kThumb2);
138 }
139 return offset;
140 }
141 DCHECK(compiled_method->GetQuickCode() != nullptr);
142 uint32_t quick_code_size = compiled_method->GetQuickCode()->size();
143 uint32_t quick_code_offset = compiled_method->AlignCode(offset) + sizeof(OatQuickMethodHeader);
144 uint32_t next_aligned_offset = compiled_method->AlignCode(quick_code_offset + quick_code_size);
145 if (!unprocessed_patches_.empty() &&
146 next_aligned_offset - unprocessed_patches_.front().second > kMaxPositiveDisplacement) {
147 bool needs_thunk = ReserveSpaceProcessPatches(next_aligned_offset);
148 if (needs_thunk) {
149 // A single thunk will cover all pending patches.
150 unprocessed_patches_.clear();
151 uint32_t thunk_location = compiled_method->AlignCode(offset);
152 thunk_locations_.push_back(thunk_location);
153 offset = CompiledMethod::AlignCode(thunk_location + thunk_code_.size(), kThumb2);
154 }
155 }
156 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
157 if (patch.Type() == kLinkerPatchCallRelative) {
158 unprocessed_patches_.emplace_back(patch.TargetMethod(),
159 quick_code_offset + patch.LiteralOffset());
160 }
161 }
162 return offset;
163 }
164
165 uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE {
166 if (current_thunk_to_write_ == thunk_locations_.size()) {
167 return offset;
168 }
169 uint32_t aligned_offset = CompiledMethod::AlignCode(offset, kThumb2);
170 if (UNLIKELY(aligned_offset == thunk_locations_[current_thunk_to_write_])) {
171 ++current_thunk_to_write_;
172 uint32_t aligned_code_delta = aligned_offset - offset;
173 if (aligned_code_delta != 0u && !writer_->WriteCodeAlignment(out, aligned_code_delta)) {
174 return 0u;
175 }
176 if (!out->WriteFully(thunk_code_.data(), thunk_code_.size())) {
177 return 0u;
178 }
179 writer_->size_relative_call_thunks_ += thunk_code_.size();
180 uint32_t thunk_end_offset = aligned_offset + thunk_code_.size();
181 // Align after writing chunk, see the ReserveSpace() above.
182 offset = CompiledMethod::AlignCode(thunk_end_offset, kThumb2);
183 aligned_code_delta = offset - thunk_end_offset;
184 if (aligned_code_delta != 0u && !writer_->WriteCodeAlignment(out, aligned_code_delta)) {
185 return 0u;
186 }
187 }
188 return offset;
189 }
190
191 void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset,
192 uint32_t target_offset) OVERRIDE {
193 DCHECK_LE(literal_offset + 4u, code->size());
194 DCHECK_EQ(literal_offset & 1u, 0u);
195 DCHECK_EQ(patch_offset & 1u, 0u);
196 DCHECK_EQ(target_offset & 1u, 1u); // Thumb2 mode bit.
197 // Unsigned arithmetic with its well-defined overflow behavior is just fine here.
198 uint32_t displacement = target_offset - 1u - patch_offset;
199 // NOTE: With unsigned arithmetic we do mean to use && rather than || below.
200 if (displacement > kMaxPositiveDisplacement && displacement < -kMaxNegativeDisplacement) {
201 // Unwritten thunks have higher offsets, check if it's within range.
202 DCHECK(current_thunk_to_write_ == thunk_locations_.size() ||
203 thunk_locations_[current_thunk_to_write_] > patch_offset);
204 if (current_thunk_to_write_ != thunk_locations_.size() &&
205 thunk_locations_[current_thunk_to_write_] - patch_offset < kMaxPositiveDisplacement) {
206 displacement = thunk_locations_[current_thunk_to_write_] - patch_offset;
207 } else {
208 // We must have a previous thunk then.
209 DCHECK_NE(current_thunk_to_write_, 0u);
210 DCHECK_LT(thunk_locations_[current_thunk_to_write_ - 1], patch_offset);
211 displacement = thunk_locations_[current_thunk_to_write_ - 1] - patch_offset;
212 DCHECK(displacement >= -kMaxNegativeDisplacement);
213 }
214 }
215 displacement -= kPcDisplacement; // The base PC is at the end of the 4-byte patch.
216 DCHECK_EQ(displacement & 1u, 0u);
217 DCHECK((displacement >> 24) == 0u || (displacement >> 24) == 255u); // 25-bit signed.
218 uint32_t signbit = (displacement >> 31) & 0x1;
219 uint32_t i1 = (displacement >> 23) & 0x1;
220 uint32_t i2 = (displacement >> 22) & 0x1;
221 uint32_t imm10 = (displacement >> 12) & 0x03ff;
222 uint32_t imm11 = (displacement >> 1) & 0x07ff;
223 uint32_t j1 = i1 ^ (signbit ^ 1);
224 uint32_t j2 = i2 ^ (signbit ^ 1);
225 uint32_t value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) | imm11;
226 value |= 0xf000d000; // BL
227
228 uint8_t* addr = &(*code)[literal_offset];
229 // Check that we're just overwriting an existing BL.
230 DCHECK_EQ(addr[1] & 0xf8, 0xf0);
231 DCHECK_EQ(addr[3] & 0xd0, 0xd0);
232 // Write the new BL.
233 addr[0] = (value >> 16) & 0xff;
234 addr[1] = (value >> 24) & 0xff;
235 addr[2] = (value >> 0) & 0xff;
236 addr[3] = (value >> 8) & 0xff;
237 }
238
239 private:
240 bool ReserveSpaceProcessPatches(uint32_t next_aligned_offset) {
241 // Process as many patches as possible, stop only on unresolved targets or calls too far back.
242 while (!unprocessed_patches_.empty()) {
243 uint32_t patch_offset = unprocessed_patches_.front().second;
244 auto it = writer_->method_offset_map_.find(unprocessed_patches_.front().first);
245 if (it == writer_->method_offset_map_.end()) {
246 // If still unresolved, check if we have a thunk within range.
247 DCHECK(thunk_locations_.empty() || thunk_locations_.back() <= patch_offset);
248 if (thunk_locations_.empty() ||
249 patch_offset - thunk_locations_.back() > kMaxNegativeDisplacement) {
250 return next_aligned_offset - patch_offset > kMaxPositiveDisplacement;
251 }
252 } else if (it->second >= patch_offset) {
253 DCHECK_LE(it->second - patch_offset, kMaxPositiveDisplacement);
254 } else {
255 // When calling back, check if we have a thunk that's closer than the actual target.
256 uint32_t target_offset = (thunk_locations_.empty() || it->second > thunk_locations_.back())
257 ? it->second
258 : thunk_locations_.back();
259 DCHECK_GT(patch_offset, target_offset);
260 if (patch_offset - target_offset > kMaxNegativeDisplacement) {
261 return true;
262 }
263 }
264 unprocessed_patches_.pop_front();
265 }
266 return false;
267 }
268
269 static std::vector<uint8_t> CompileThunkCode() {
270 // The thunk just uses the entry point in the ArtMethod. This works even for calls
271 // to the generic JNI and interpreter trampolines.
272 arm::Thumb2Assembler assembler;
273 assembler.LoadFromOffset(
274 arm::kLoadWord, arm::PC, arm::R0,
275 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
276 assembler.bkpt(0);
277 std::vector<uint8_t> thunk_code(assembler.CodeSize());
278 MemoryRegion code(thunk_code.data(), thunk_code.size());
279 assembler.FinalizeInstructions(code);
280 return thunk_code;
281 }
282
283 // PC displacement from patch location; Thumb2 PC is always at instruction address + 4.
284 static constexpr int32_t kPcDisplacement = 4;
285
286 // Maximum positive and negative displacement measured from the patch location.
287 // (Signed 25 bit displacement with the last bit 0 has range [-2^24, 2^24-2] measured from
288 // the Thumb2 PC pointing right after the BL, i.e. 4 bytes later than the patch location.)
289 static constexpr uint32_t kMaxPositiveDisplacement = (1u << 24) - 2 + kPcDisplacement;
290 static constexpr uint32_t kMaxNegativeDisplacement = (1u << 24) - kPcDisplacement;
291
292 OatWriter* const writer_;
293 const std::vector<uint8_t> thunk_code_;
294 std::vector<uint32_t> thunk_locations_;
295 size_t current_thunk_to_write_;
296
297 // ReserveSpace() tracks unprocessed patches.
298 typedef std::pair<MethodReference, uint32_t> UnprocessedPatch;
299 std::deque<UnprocessedPatch> unprocessed_patches_;
300
301 DISALLOW_COPY_AND_ASSIGN(Thumb2RelativeCallPatcher);
302};
303
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100304#define DCHECK_OFFSET() \
305 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
306 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
307
308#define DCHECK_OFFSET_() \
309 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
310 << "file_offset=" << file_offset << " offset_=" << offset_
311
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700312OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700313 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800314 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -0700315 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -0800316 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +0100317 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700318 TimingLogger* timings,
319 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700320 : compiler_driver_(compiler),
Vladimir Markof4da6752014-08-01 19:04:18 +0100321 image_writer_(image_writer),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700322 dex_files_(&dex_files),
Vladimir Markof4da6752014-08-01 19:04:18 +0100323 size_(0u),
324 oat_data_offset_(0u),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700325 image_file_location_oat_checksum_(image_file_location_oat_checksum),
326 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -0700327 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700328 key_value_store_(key_value_store),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700329 oat_header_(NULL),
330 size_dex_file_alignment_(0),
331 size_executable_offset_alignment_(0),
332 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700333 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700334 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700335 size_interpreter_to_interpreter_bridge_(0),
336 size_interpreter_to_compiled_code_bridge_(0),
337 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -0700338 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700339 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700340 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -0800341 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -0700342 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700343 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700344 size_quick_to_interpreter_bridge_(0),
345 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100346 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700347 size_code_(0),
348 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100349 size_relative_call_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700350 size_mapping_table_(0),
351 size_vmap_table_(0),
352 size_gc_map_(0),
353 size_oat_dex_file_location_size_(0),
354 size_oat_dex_file_location_data_(0),
355 size_oat_dex_file_location_checksum_(0),
356 size_oat_dex_file_offset_(0),
357 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700358 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700359 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700360 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100361 size_oat_class_method_offsets_(0),
362 method_offset_map_() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700363 CHECK(key_value_store != nullptr);
364
Vladimir Markof4da6752014-08-01 19:04:18 +0100365 switch (compiler_driver_->GetInstructionSet()) {
366 case kX86:
367 case kX86_64:
368 relative_call_patcher_.reset(new X86RelativeCallPatcher);
369 break;
370 case kArm:
371 // Fall through: we generate Thumb2 code for "arm".
372 case kThumb2:
373 relative_call_patcher_.reset(new Thumb2RelativeCallPatcher(this));
374 break;
375 case kArm64:
376 // TODO: Implement relative calls for arm64.
377 default:
378 relative_call_patcher_.reset(new NoRelativeCallPatcher);
379 break;
380 }
381
Ian Rogersca368cb2013-11-15 15:52:08 -0800382 size_t offset;
383 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700384 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800385 offset = InitOatHeader();
386 }
387 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700388 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800389 offset = InitOatDexFiles(offset);
390 }
391 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700392 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800393 offset = InitDexFiles(offset);
394 }
395 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700396 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800397 offset = InitOatClasses(offset);
398 }
399 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700400 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100401 offset = InitOatMaps(offset);
402 }
403 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700404 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800405 offset = InitOatCode(offset);
406 }
407 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700408 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800409 offset = InitOatCodeDexFiles(offset);
410 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700411 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700412
413 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Vladimir Markof4da6752014-08-01 19:04:18 +0100414 CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700415 CHECK_EQ(compiler->IsImage(),
416 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Alex Lighta59dd802014-07-02 16:28:08 -0700417 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700418}
419
Ian Rogers0571d352011-11-03 19:51:38 -0700420OatWriter::~OatWriter() {
421 delete oat_header_;
422 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800423 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700424}
425
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100426struct OatWriter::GcMapDataAccess {
427 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100428 return compiled_method->GetGcMap();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100429 }
430
431 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
432 return oat_class->method_offsets_[method_offsets_index].gc_map_offset_;
433 }
434
435 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
436 ALWAYS_INLINE {
437 oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset;
438 }
439
440 static const char* Name() ALWAYS_INLINE {
441 return "GC map";
442 }
443};
444
445struct OatWriter::MappingTableDataAccess {
446 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
447 return &compiled_method->GetMappingTable();
448 }
449
450 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100451 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
452 return offset == 0u ? 0u :
453 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100454 }
455
456 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
457 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100458 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
459 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100460 }
461
462 static const char* Name() ALWAYS_INLINE {
463 return "mapping table";
464 }
465};
466
467struct OatWriter::VmapTableDataAccess {
468 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
469 return &compiled_method->GetVmapTable();
470 }
471
472 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100473 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
474 return offset == 0u ? 0u :
475 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100476 }
477
478 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
479 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100480 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
481 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100482 }
483
484 static const char* Name() ALWAYS_INLINE {
485 return "vmap table";
486 }
487};
488
489class OatWriter::DexMethodVisitor {
490 public:
491 DexMethodVisitor(OatWriter* writer, size_t offset)
492 : writer_(writer),
493 offset_(offset),
494 dex_file_(nullptr),
495 class_def_index_(DexFile::kDexNoIndex) {
496 }
497
498 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
499 DCHECK(dex_file_ == nullptr);
500 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
501 dex_file_ = dex_file;
502 class_def_index_ = class_def_index;
503 return true;
504 }
505
506 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
507
508 virtual bool EndClass() {
509 if (kIsDebugBuild) {
510 dex_file_ = nullptr;
511 class_def_index_ = DexFile::kDexNoIndex;
512 }
513 return true;
514 }
515
516 size_t GetOffset() const {
517 return offset_;
518 }
519
520 protected:
521 virtual ~DexMethodVisitor() { }
522
523 OatWriter* const writer_;
524
525 // The offset is usually advanced for each visited method by the derived class.
526 size_t offset_;
527
528 // The dex file and class def index are set in StartClass().
529 const DexFile* dex_file_;
530 size_t class_def_index_;
531};
532
533class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
534 public:
535 OatDexMethodVisitor(OatWriter* writer, size_t offset)
536 : DexMethodVisitor(writer, offset),
537 oat_class_index_(0u),
538 method_offsets_index_(0u) {
539 }
540
541 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
542 DexMethodVisitor::StartClass(dex_file, class_def_index);
543 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
544 method_offsets_index_ = 0u;
545 return true;
546 }
547
548 bool EndClass() {
549 ++oat_class_index_;
550 return DexMethodVisitor::EndClass();
551 }
552
553 protected:
554 size_t oat_class_index_;
555 size_t method_offsets_index_;
556};
557
558class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
559 public:
560 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
561 : DexMethodVisitor(writer, offset),
562 compiled_methods_(),
563 num_non_null_compiled_methods_(0u) {
564 compiled_methods_.reserve(256u);
565 }
566
567 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
568 DexMethodVisitor::StartClass(dex_file, class_def_index);
569 compiled_methods_.clear();
570 num_non_null_compiled_methods_ = 0u;
571 return true;
572 }
573
574 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
575 // Fill in the compiled_methods_ array for methods that have a
576 // CompiledMethod. We track the number of non-null entries in
577 // num_non_null_compiled_methods_ since we only want to allocate
578 // OatMethodOffsets for the compiled methods.
579 uint32_t method_idx = it.GetMemberIndex();
580 CompiledMethod* compiled_method =
581 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
582 compiled_methods_.push_back(compiled_method);
583 if (compiled_method != nullptr) {
584 ++num_non_null_compiled_methods_;
585 }
586 return true;
587 }
588
589 bool EndClass() {
590 ClassReference class_ref(dex_file_, class_def_index_);
591 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
592 mirror::Class::Status status;
593 if (compiled_class != NULL) {
594 status = compiled_class->GetStatus();
595 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
596 status = mirror::Class::kStatusError;
597 } else {
598 status = mirror::Class::kStatusNotReady;
599 }
600
601 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
602 num_non_null_compiled_methods_, status);
603 writer_->oat_classes_.push_back(oat_class);
Vladimir Markof4da6752014-08-01 19:04:18 +0100604 oat_class->UpdateChecksum(writer_->oat_header_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100605 offset_ += oat_class->SizeOf();
606 return DexMethodVisitor::EndClass();
607 }
608
609 private:
610 std::vector<CompiledMethod*> compiled_methods_;
611 size_t num_non_null_compiled_methods_;
612};
613
614class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
615 public:
616 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
617 : OatDexMethodVisitor(writer, offset) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100618 writer_->absolute_patch_locations_.reserve(
619 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
620 }
621
622 bool EndClass() {
623 OatDexMethodVisitor::EndClass();
624 if (oat_class_index_ == writer_->oat_classes_.size()) {
625 offset_ = writer_->relative_call_patcher_->ReserveSpace(offset_, nullptr);
626 }
627 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100628 }
629
630 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
631 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
632 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
633 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
634
635 if (compiled_method != nullptr) {
636 // Derived from CompiledMethod.
637 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100638
639 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
640 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
641 if (portable_code != nullptr) {
642 CHECK(quick_code == nullptr);
643 size_t oat_method_offsets_offset =
644 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
645 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
646 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
647 } else {
648 CHECK(quick_code != nullptr);
Vladimir Markof4da6752014-08-01 19:04:18 +0100649 offset_ = writer_->relative_call_patcher_->ReserveSpace(offset_, compiled_method);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100650 offset_ = compiled_method->AlignCode(offset_);
651 DCHECK_ALIGNED_PARAM(offset_,
652 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
653 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
654 CHECK_NE(code_size, 0U);
655 uint32_t thumb_offset = compiled_method->CodeDelta();
Vladimir Marko7624d252014-05-02 14:40:15 +0100656 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100657
Alex Light78382fa2014-06-06 15:45:32 -0700658 bool deduped = false;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100659
660 // Deduplicate code arrays.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100661 auto lb = dedupe_map_.lower_bound(compiled_method);
662 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
663 quick_code_offset = lb->second;
Alex Light78382fa2014-06-06 15:45:32 -0700664 deduped = true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100665 } else {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100666 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
Vladimir Marko7624d252014-05-02 14:40:15 +0100667 }
668
Vladimir Markof4da6752014-08-01 19:04:18 +0100669 MethodReference method_ref(dex_file_, it.GetMemberIndex());
670 auto method_lb = writer_->method_offset_map_.lower_bound(method_ref);
671 if (method_lb != writer_->method_offset_map_.end() &&
672 !writer_->method_offset_map_.key_comp()(method_ref, method_lb->first)) {
673 // TODO: Should this be a hard failure?
674 LOG(WARNING) << "Multiple definitions of "
675 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
676 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
677 } else {
678 writer_->method_offset_map_.PutBefore(method_lb, method_ref, quick_code_offset);
679 }
680
Vladimir Marko7624d252014-05-02 14:40:15 +0100681 // Update quick method header.
682 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
683 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
684 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
685 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
686 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
687 // to 0-offset and we need to adjust it by code_offset.
688 uint32_t code_offset = quick_code_offset - thumb_offset;
689 if (mapping_table_offset != 0u) {
690 mapping_table_offset += code_offset;
691 DCHECK_LT(mapping_table_offset, code_offset);
692 }
693 if (vmap_table_offset != 0u) {
694 vmap_table_offset += code_offset;
695 DCHECK_LT(vmap_table_offset, code_offset);
696 }
697 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
698 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
699 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
700 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
701 frame_size_in_bytes, core_spill_mask, fp_spill_mask,
702 code_size);
703
Vladimir Markobd72fc12014-07-09 16:06:40 +0100704 if (!deduped) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100705 // Update offsets. (Checksum is updated when writing.)
Vladimir Marko8a630572014-04-09 18:45:35 +0100706 offset_ += sizeof(*method_header); // Method header is prepended before code.
Vladimir Marko8a630572014-04-09 18:45:35 +0100707 offset_ += code_size;
Vladimir Markof4da6752014-08-01 19:04:18 +0100708 // Record absolute patch locations.
709 if (!compiled_method->GetPatches().empty()) {
710 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
711 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
712 if (patch.Type() != kLinkerPatchCallRelative) {
713 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
714 }
715 }
716 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100717 }
Alex Light78382fa2014-06-06 15:45:32 -0700718
Andreas Gampe79273802014-08-05 20:21:05 -0700719 if (writer_->compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
720 // Record debug information for this function if we are doing that.
Alex Light78382fa2014-06-06 15:45:32 -0700721
Alex Light78382fa2014-06-06 15:45:32 -0700722 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
723 if (deduped) {
Andreas Gampe79273802014-08-05 20:21:05 -0700724 // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol
725 // so that it will show up in a debuggerd crash report.
Alex Light78382fa2014-06-06 15:45:32 -0700726 name += " [ DEDUPED ]";
727 }
Andreas Gampe79273802014-08-05 20:21:05 -0700728
729 const uint32_t quick_code_start = quick_code_offset -
730 writer_->oat_header_->GetExecutableOffset();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700731 const DexFile::CodeItem *code_item = it.GetMethodCodeItem();
Andreas Gampe79273802014-08-05 20:21:05 -0700732 writer_->method_info_.push_back(DebugInfo(name,
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700733 dex_file_->GetSourceFile(dex_file_->GetClassDef(class_def_index_)),
734 quick_code_start, quick_code_start + code_size,
735 code_item == nullptr ? nullptr : dex_file_->GetDebugInfoStream(code_item),
736 compiled_method));
Alex Light78382fa2014-06-06 15:45:32 -0700737 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100738 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100739
740 if (kIsDebugBuild) {
741 // We expect GC maps except when the class hasn't been verified or the method is native.
742 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
743 ClassReference class_ref(dex_file_, class_def_index_);
744 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
745 mirror::Class::Status status;
746 if (compiled_class != NULL) {
747 status = compiled_class->GetStatus();
748 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
749 status = mirror::Class::kStatusError;
750 } else {
751 status = mirror::Class::kStatusNotReady;
752 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100753 std::vector<uint8_t> const * gc_map = compiled_method->GetGcMap();
754 if (gc_map != nullptr) {
755 size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700756 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100757 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
758 << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
759 << (status < mirror::Class::kStatusVerified) << " " << status << " "
760 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
761 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100762 }
763
764 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
765 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
766 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100767 ++method_offsets_index_;
768 }
769
770 return true;
771 }
772
773 private:
774 // Deduplication is already done on a pointer basis by the compiler driver,
775 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100776 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100777};
778
779template <typename DataAccess>
780class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
781 public:
782 InitMapMethodVisitor(OatWriter* writer, size_t offset)
783 : OatDexMethodVisitor(writer, offset) {
784 }
785
786 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
787 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
788 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
789 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
790
791 if (compiled_method != nullptr) {
792 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
793 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
794
795 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100797 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100798 auto lb = dedupe_map_.lower_bound(map);
799 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
800 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100801 } else {
802 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100803 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100804 offset_ += map_size;
805 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
806 }
807 }
808 ++method_offsets_index_;
809 }
810
811 return true;
812 }
813
814 private:
815 // Deduplication is already done on a pointer basis by the compiler driver,
816 // so we can simply compare the pointers to find out if things are duplicated.
817 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
818};
819
820class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
821 public:
822 InitImageMethodVisitor(OatWriter* writer, size_t offset)
823 : OatDexMethodVisitor(writer, offset) {
824 }
825
826 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
827 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
828 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
829 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
830
Vladimir Marko7624d252014-05-02 14:40:15 +0100831 OatMethodOffsets offsets(0u, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100832 if (compiled_method != nullptr) {
833 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
834 offsets = oat_class->method_offsets_[method_offsets_index_];
835 ++method_offsets_index_;
836 }
837
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100838 ClassLinker* linker = Runtime::Current()->GetClassLinker();
839 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
840 // Unchecked as we hold mutator_lock_ on entry.
841 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700842 StackHandleScope<2> hs(soa.Self());
843 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100844 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700845 NullHandle<mirror::ClassLoader>(),
846 NullHandle<mirror::ArtMethod>(),
847 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700848 if (method == nullptr) {
849 LOG(ERROR) << "Unexpected failure to resolve a method: "
850 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
851 soa.Self()->AssertPendingException();
852 mirror::Throwable* exc = soa.Self()->GetException(nullptr);
853 std::string dump = exc->Dump();
854 LOG(FATAL) << dump;
855 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100856 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
857 method->SetQuickOatCodeOffset(offsets.code_offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100858 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
859
860 return true;
861 }
862};
863
864class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
865 public:
866 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100867 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100868 : OatDexMethodVisitor(writer, relative_offset),
869 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100870 file_offset_(file_offset),
871 self_(Thread::Current()),
872 old_no_thread_suspension_cause_(self_->StartAssertNoThreadSuspension("OatWriter patching")),
873 class_linker_(Runtime::Current()->GetClassLinker()),
874 dex_cache_(nullptr) {
875 if (writer_->image_writer_ != nullptr) {
876 // If we're creating the image, the address space must be ready so that we can apply patches.
877 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
878 patched_code_.reserve(16 * KB);
879 }
880 self_->TransitionFromSuspendedToRunnable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100881 }
882
Vladimir Markof4da6752014-08-01 19:04:18 +0100883 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
884 self_->EndAssertNoThreadSuspension(old_no_thread_suspension_cause_);
885 self_->TransitionFromRunnableToSuspended(kNative);
886 }
887
888 bool StartClass(const DexFile* dex_file, size_t class_def_index)
889 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
890 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
891 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
892 dex_cache_ = class_linker_->FindDexCache(*dex_file);
893 }
894 return true;
895 }
896
897 bool EndClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
898 bool result = OatDexMethodVisitor::EndClass();
899 if (oat_class_index_ == writer_->oat_classes_.size()) {
900 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
901 offset_ = writer_->relative_call_patcher_->WriteThunks(out_, offset_);
902 if (UNLIKELY(offset_ == 0u)) {
903 PLOG(ERROR) << "Failed to write final relative call thunks";
904 result = false;
905 }
906 }
907 return result;
908 }
909
910 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
911 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100912 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
913 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
914
915 if (compiled_method != NULL) { // ie. not an abstract method
916 size_t file_offset = file_offset_;
917 OutputStream* out = out_;
918
919 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
920 if (quick_code != nullptr) {
921 CHECK(compiled_method->GetPortableCode() == nullptr);
Vladimir Markof4da6752014-08-01 19:04:18 +0100922 offset_ = writer_->relative_call_patcher_->WriteThunks(out, offset_);
923 if (offset_ == 0u) {
924 ReportWriteFailure("relative call thunk", it);
925 return false;
926 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100927 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
928 uint32_t aligned_code_delta = aligned_offset - offset_;
929 if (aligned_code_delta != 0) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100930 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100931 ReportWriteFailure("code alignment padding", it);
932 return false;
933 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100934 offset_ += aligned_code_delta;
935 DCHECK_OFFSET_();
936 }
937 DCHECK_ALIGNED_PARAM(offset_,
938 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
939 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
940 CHECK_NE(code_size, 0U);
941
942 // Deduplicate code arrays.
943 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
944 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
Vladimir Marko7624d252014-05-02 14:40:15 +0100945 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100946 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
947 if (method_offsets.code_offset_ >= offset_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100948 const OatQuickMethodHeader& method_header =
949 oat_class->method_headers_[method_offsets_index_];
950 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100951 if (!out->WriteFully(&method_header, sizeof(method_header))) {
952 ReportWriteFailure("method header", it);
953 return false;
954 }
955 writer_->size_method_header_ += sizeof(method_header);
956 offset_ += sizeof(method_header);
957 DCHECK_OFFSET_();
Vladimir Markof4da6752014-08-01 19:04:18 +0100958
959 if (!compiled_method->GetPatches().empty()) {
960 patched_code_ = *quick_code;
961 quick_code = &patched_code_;
962 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
963 if (patch.Type() == kLinkerPatchCallRelative) {
964 // NOTE: Relative calls across oat files are not supported.
965 uint32_t target_offset = GetTargetOffset(patch);
966 uint32_t literal_offset = patch.LiteralOffset();
967 writer_->relative_call_patcher_->Patch(&patched_code_, literal_offset,
968 offset_ + literal_offset, target_offset);
969 } else if (patch.Type() == kLinkerPatchCall) {
970 uint32_t target_offset = GetTargetOffset(patch);
971 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
972 } else if (patch.Type() == kLinkerPatchMethod) {
973 mirror::ArtMethod* method = GetTargetMethod(patch);
974 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), method);
975 } else if (patch.Type() == kLinkerPatchType) {
976 mirror::Class* type = GetTargetType(patch);
977 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
978 }
979 }
980 }
981
982 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100983 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
984 ReportWriteFailure("method code", it);
985 return false;
986 }
987 writer_->size_code_ += code_size;
988 offset_ += code_size;
989 }
990 DCHECK_OFFSET_();
991 }
992 ++method_offsets_index_;
993 }
994
995 return true;
996 }
997
998 private:
999 OutputStream* const out_;
1000 size_t const file_offset_;
Vladimir Markof4da6752014-08-01 19:04:18 +01001001 Thread* const self_;
1002 const char* const old_no_thread_suspension_cause_; // TODO: Use ScopedAssertNoThreadSuspension.
1003 ClassLinker* const class_linker_;
1004 mirror::DexCache* dex_cache_;
1005 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001006
1007 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
1008 PLOG(ERROR) << "Failed to write " << what << " for "
1009 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1010 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001011
1012 mirror::ArtMethod* GetTargetMethod(const LinkerPatch& patch)
1013 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1014 MethodReference ref = patch.TargetMethod();
1015 mirror::DexCache* dex_cache =
1016 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(*ref.dex_file);
1017 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(ref.dex_method_index);
1018 CHECK(method != nullptr);
1019 return method;
1020 }
1021
1022 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1023 auto target_it = writer_->method_offset_map_.find(patch.TargetMethod());
1024 uint32_t target_offset =
1025 (target_it != writer_->method_offset_map_.end()) ? target_it->second : 0u;
1026 // If there's no compiled code, point to the correct trampoline.
1027 if (UNLIKELY(target_offset == 0)) {
1028 mirror::ArtMethod* target = GetTargetMethod(patch);
1029 DCHECK(target != nullptr);
1030 DCHECK_EQ(target->GetQuickOatCodeOffset(), 0u);
1031 target_offset = target->IsNative()
1032 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
1033 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
1034 }
1035 return target_offset;
1036 }
1037
1038 mirror::Class* GetTargetType(const LinkerPatch& patch)
1039 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1040 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
1041 ? dex_cache_ : class_linker_->FindDexCache(*patch.TargetTypeDexFile());
1042 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
1043 CHECK(type != nullptr);
1044 return type;
1045 }
1046
1047 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
1048 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1049 // NOTE: Direct method pointers across oat files don't use linker patches. However, direct
1050 // type pointers across oat files do. (TODO: Investigate why.)
1051 if (writer_->image_writer_ != nullptr) {
1052 object = writer_->image_writer_->GetImageAddress(object);
1053 }
1054 uint32_t address = PointerToLowMemUInt32(object);
1055 DCHECK_LE(offset + 4, code->size());
1056 uint8_t* data = &(*code)[offset];
1057 data[0] = address & 0xffu;
1058 data[1] = (address >> 8) & 0xffu;
1059 data[2] = (address >> 16) & 0xffu;
1060 data[3] = (address >> 24) & 0xffu;
1061 }
1062
1063 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
1064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1065 // NOTE: Direct calls across oat files don't use linker patches.
1066 DCHECK(writer_->image_writer_ != nullptr);
1067 uint32_t address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
1068 writer_->oat_data_offset_ + target_offset);
1069 DCHECK_LE(offset + 4, code->size());
1070 uint8_t* data = &(*code)[offset];
1071 data[0] = address & 0xffu;
1072 data[1] = (address >> 8) & 0xffu;
1073 data[2] = (address >> 16) & 0xffu;
1074 data[3] = (address >> 24) & 0xffu;
1075 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001076};
1077
1078template <typename DataAccess>
1079class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
1080 public:
1081 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
1082 size_t relative_offset)
1083 : OatDexMethodVisitor(writer, relative_offset),
1084 out_(out),
1085 file_offset_(file_offset) {
1086 }
1087
1088 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
1089 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
1090 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
1091
1092 if (compiled_method != NULL) { // ie. not an abstract method
1093 size_t file_offset = file_offset_;
1094 OutputStream* out = out_;
1095
1096 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
1097 ++method_offsets_index_;
1098
1099 // Write deduplicated map.
1100 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001101 size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001102 DCHECK((map_size == 0u && map_offset == 0u) ||
1103 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
1104 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
1105 if (map_size != 0u && map_offset == offset_) {
1106 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
1107 ReportWriteFailure(it);
1108 return false;
1109 }
1110 offset_ += map_size;
1111 }
1112 DCHECK_OFFSET_();
1113 }
1114
1115 return true;
1116 }
1117
1118 private:
1119 OutputStream* const out_;
1120 size_t const file_offset_;
1121
1122 void ReportWriteFailure(const ClassDataItemIterator& it) {
1123 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
1124 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1125 }
1126};
1127
1128// Visit all methods from all classes in all dex files with the specified visitor.
1129bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
1130 for (const DexFile* dex_file : *dex_files_) {
1131 const size_t class_def_count = dex_file->NumClassDefs();
1132 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
1133 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
1134 return false;
1135 }
1136 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
1137 const byte* class_data = dex_file->GetClassData(class_def);
1138 if (class_data != NULL) { // ie not an empty class, such as a marker interface
1139 ClassDataItemIterator it(*dex_file, class_data);
1140 while (it.HasNextStaticField()) {
1141 it.Next();
1142 }
1143 while (it.HasNextInstanceField()) {
1144 it.Next();
1145 }
1146 size_t class_def_method_index = 0u;
1147 while (it.HasNextDirectMethod()) {
1148 if (!visitor->VisitMethod(class_def_method_index, it)) {
1149 return false;
1150 }
1151 ++class_def_method_index;
1152 it.Next();
1153 }
1154 while (it.HasNextVirtualMethod()) {
1155 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
1156 return false;
1157 }
1158 ++class_def_method_index;
1159 it.Next();
1160 }
1161 }
1162 if (UNLIKELY(!visitor->EndClass())) {
1163 return false;
1164 }
1165 }
1166 }
1167 return true;
1168}
1169
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001170size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001171 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
1172 compiler_driver_->GetInstructionSetFeatures(),
1173 dex_files_,
1174 image_file_location_oat_checksum_,
1175 image_file_location_oat_begin_,
1176 key_value_store_);
1177
1178 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001179}
1180
1181size_t OatWriter::InitOatDexFiles(size_t offset) {
1182 // create the OatDexFiles
1183 for (size_t i = 0; i != dex_files_->size(); ++i) {
1184 const DexFile* dex_file = (*dex_files_)[i];
1185 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001186 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001187 oat_dex_files_.push_back(oat_dex_file);
1188 offset += oat_dex_file->SizeOf();
1189 }
1190 return offset;
1191}
1192
Brian Carlstrom89521892011-12-07 22:05:07 -08001193size_t OatWriter::InitDexFiles(size_t offset) {
1194 // calculate the offsets within OatDexFiles to the DexFiles
1195 for (size_t i = 0; i != dex_files_->size(); ++i) {
1196 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001197 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001198 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001199 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001200
1201 // set offset in OatDexFile to DexFile
1202 oat_dex_files_[i]->dex_file_offset_ = offset;
1203
1204 const DexFile* dex_file = (*dex_files_)[i];
1205 offset += dex_file->GetHeader().file_size_;
1206 }
1207 return offset;
1208}
1209
Brian Carlstrom389efb02012-01-11 12:06:26 -08001210size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001211 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001212 InitOatClassesMethodVisitor visitor(this, offset);
1213 bool success = VisitDexMethods(&visitor);
1214 CHECK(success);
1215 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001216
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001217 // Update oat_dex_files_.
1218 auto oat_class_it = oat_classes_.begin();
1219 for (OatDexFile* oat_dex_file : oat_dex_files_) {
1220 for (uint32_t& offset : oat_dex_file->methods_offsets_) {
1221 DCHECK(oat_class_it != oat_classes_.end());
1222 offset = (*oat_class_it)->offset_;
1223 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001224 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001225 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001226 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001227 CHECK(oat_class_it == oat_classes_.end());
1228
1229 return offset;
1230}
1231
1232size_t OatWriter::InitOatMaps(size_t offset) {
1233 #define VISIT(VisitorType) \
1234 do { \
1235 VisitorType visitor(this, offset); \
1236 bool success = VisitDexMethods(&visitor); \
1237 DCHECK(success); \
1238 offset = visitor.GetOffset(); \
1239 } while (false)
1240
1241 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1242 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1243 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1244
1245 #undef VISIT
1246
Brian Carlstrome24fa612011-09-29 00:53:55 -07001247 return offset;
1248}
1249
1250size_t OatWriter::InitOatCode(size_t offset) {
1251 // calculate the offsets within OatHeader to executable code
1252 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001253 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001254 // required to be on a new page boundary
1255 offset = RoundUp(offset, kPageSize);
1256 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001257 size_executable_offset_alignment_ = offset - old_offset;
1258 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001259 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001260 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001261
Ian Rogers848871b2013-08-05 10:56:33 -07001262 #define DO_TRAMPOLINE(field, fn_name) \
1263 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001264 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1265 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001266 field.reset(compiler_driver_->Create ## fn_name()); \
1267 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001268
Ian Rogers848871b2013-08-05 10:56:33 -07001269 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
1270 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
1271 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -07001272 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001273 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
1274 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -08001275 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001276 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001277 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1278 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001279
Ian Rogers848871b2013-08-05 10:56:33 -07001280 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001281 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001282 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1283 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1284 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001285 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001286 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001287 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001288 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001289 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001290 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001291 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001292 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001293 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001294 return offset;
1295}
1296
1297size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001298 #define VISIT(VisitorType) \
1299 do { \
1300 VisitorType visitor(this, offset); \
1301 bool success = VisitDexMethods(&visitor); \
1302 DCHECK(success); \
1303 offset = visitor.GetOffset(); \
1304 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001305
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001306 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -08001307 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001308 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001309 }
Logan Chien8b977d32012-02-21 19:14:55 +08001310
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001311 #undef VISIT
1312
Brian Carlstrome24fa612011-09-29 00:53:55 -07001313 return offset;
1314}
1315
Ian Rogers3d504072014-03-01 09:16:49 -08001316bool OatWriter::Write(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001317 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1318 if (raw_file_offset == (off_t) -1) {
1319 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1320 return false;
1321 }
1322 const size_t file_offset = static_cast<size_t>(raw_file_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001323
Vladimir Markof4da6752014-08-01 19:04:18 +01001324 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001325 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001326 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1327 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001328 return false;
1329 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001330 size_oat_header_ += sizeof(OatHeader);
1331 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001332
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001333 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001334 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001335 return false;
1336 }
1337
Vladimir Markof4da6752014-08-01 19:04:18 +01001338 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1339 if (tables_end_offset == (off_t) -1) {
1340 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1341 return false;
1342 }
1343 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001344 relative_offset = WriteMaps(out, file_offset, relative_offset);
1345 if (relative_offset == 0) {
1346 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1347 return false;
1348 }
1349
1350 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001351 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001352 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001353 return false;
1354 }
1355
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001356 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1357 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001358 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001359 return false;
1360 }
1361
Vladimir Markof4da6752014-08-01 19:04:18 +01001362 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1363 if (oat_end_file_offset == (off_t) -1) {
1364 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1365 return false;
1366 }
1367
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001368 if (kIsDebugBuild) {
1369 uint32_t size_total = 0;
1370 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001371 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001372 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001373
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001374 DO_STAT(size_dex_file_alignment_);
1375 DO_STAT(size_executable_offset_alignment_);
1376 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001377 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001378 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001379 DO_STAT(size_interpreter_to_interpreter_bridge_);
1380 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1381 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001382 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001383 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001384 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001385 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001386 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001387 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001388 DO_STAT(size_quick_to_interpreter_bridge_);
1389 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001390 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001391 DO_STAT(size_code_);
1392 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001393 DO_STAT(size_relative_call_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001394 DO_STAT(size_mapping_table_);
1395 DO_STAT(size_vmap_table_);
1396 DO_STAT(size_gc_map_);
1397 DO_STAT(size_oat_dex_file_location_size_);
1398 DO_STAT(size_oat_dex_file_location_data_);
1399 DO_STAT(size_oat_dex_file_location_checksum_);
1400 DO_STAT(size_oat_dex_file_offset_);
1401 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001402 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001403 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001404 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001405 DO_STAT(size_oat_class_method_offsets_);
1406 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001407
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001408 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001409 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001410 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001411 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001412
Vladimir Markof4da6752014-08-01 19:04:18 +01001413 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001414 CHECK_EQ(size_, relative_offset);
1415
Vladimir Markof4da6752014-08-01 19:04:18 +01001416 // Write the header now that the checksum is final.
1417 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1418 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1419 return false;
1420 }
1421 DCHECK_EQ(raw_file_offset, out->Seek(0, kSeekCurrent));
1422 if (!out->WriteFully(oat_header_, header_size)) {
1423 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1424 return false;
1425 }
1426 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1427 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1428 return false;
1429 }
1430 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1431
Brian Carlstrome24fa612011-09-29 00:53:55 -07001432 return true;
1433}
1434
Ian Rogers3d504072014-03-01 09:16:49 -08001435bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001436 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001437 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001438 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001439 return false;
1440 }
1441 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001442 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001443 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001444 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001445 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1446 const DexFile* dex_file = (*dex_files_)[i];
1447 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1448 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1449 return false;
1450 }
1451 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001452 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001453 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001454 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001455 return false;
1456 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001457 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001458 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001459 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001460 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001461 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001462 return false;
1463 }
1464 }
1465 return true;
1466}
1467
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001468size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1469 #define VISIT(VisitorType) \
1470 do { \
1471 VisitorType visitor(this, out, file_offset, relative_offset); \
1472 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1473 return 0; \
1474 } \
1475 relative_offset = visitor.GetOffset(); \
1476 } while (false)
1477
1478 size_t gc_maps_offset = relative_offset;
1479 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1480 size_gc_map_ = relative_offset - gc_maps_offset;
1481
1482 size_t mapping_tables_offset = relative_offset;
1483 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1484 size_mapping_table_ = relative_offset - mapping_tables_offset;
1485
1486 size_t vmap_tables_offset = relative_offset;
1487 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1488 size_vmap_table_ = relative_offset - vmap_tables_offset;
1489
1490 #undef VISIT
1491
1492 return relative_offset;
1493}
1494
1495size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001496 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001497 relative_offset += size_executable_offset_alignment_;
1498 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001499 size_t expected_file_offset = file_offset + relative_offset;
1500 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001501 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001502 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001503 return 0;
1504 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001505 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001506 if (compiler_driver_->IsImage()) {
1507 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001508
Ian Rogers848871b2013-08-05 10:56:33 -07001509 #define DO_TRAMPOLINE(field) \
1510 do { \
1511 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1512 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001513 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001514 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001515 if (!out->WriteFully(&(*field)[0], field->size())) { \
1516 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001517 return false; \
1518 } \
1519 size_ ## field += field->size(); \
1520 relative_offset += alignment_padding + field->size(); \
1521 DCHECK_OFFSET(); \
1522 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001523
Ian Rogers848871b2013-08-05 10:56:33 -07001524 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1525 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1526 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001527 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001528 DO_TRAMPOLINE(portable_resolution_trampoline_);
1529 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001530 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001531 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001532 DO_TRAMPOLINE(quick_resolution_trampoline_);
1533 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1534 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001535 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001536 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001537}
1538
Ian Rogers3d504072014-03-01 09:16:49 -08001539size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001540 const size_t file_offset,
1541 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001542 #define VISIT(VisitorType) \
1543 do { \
1544 VisitorType visitor(this, out, file_offset, relative_offset); \
1545 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1546 return 0; \
1547 } \
1548 relative_offset = visitor.GetOffset(); \
1549 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001550
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001551 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001552
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001553 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001554
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001555 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001556}
1557
Vladimir Markof4da6752014-08-01 19:04:18 +01001558bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1559 static const uint8_t kPadding[] = {
1560 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1561 };
1562 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1563 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1564 return false;
1565 }
1566 size_code_alignment_ += aligned_code_delta;
1567 return true;
1568}
1569
Brian Carlstrom265091e2013-01-30 14:08:26 -08001570OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1571 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001572 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001573 dex_file_location_size_ = location.size();
1574 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001575 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001576 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001577 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001578}
1579
1580size_t OatWriter::OatDexFile::SizeOf() const {
1581 return sizeof(dex_file_location_size_)
1582 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001583 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001584 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001585 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001586}
1587
Ian Rogers3d504072014-03-01 09:16:49 -08001588void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1589 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1590 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1591 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1592 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1593 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001594 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001595}
1596
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001597bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001598 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001599 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001600 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001601 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1602 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001603 return false;
1604 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001605 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001606 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1607 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001608 return false;
1609 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001610 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001611 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1612 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001613 return false;
1614 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001615 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001616 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1617 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001618 return false;
1619 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001620 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001621 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001622 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001623 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001624 return false;
1625 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001626 oat_writer->size_oat_dex_file_methods_offsets_ +=
1627 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001628 return true;
1629}
1630
Brian Carlstromba150c32013-08-27 17:31:03 -07001631OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001632 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001633 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001634 mirror::Class::Status status)
1635 : compiled_methods_(compiled_methods) {
1636 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001637 CHECK_LE(num_non_null_compiled_methods, num_methods);
1638
Brian Carlstrom265091e2013-01-30 14:08:26 -08001639 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001640 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1641
1642 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1643 // apply when there are 0 methods, we just arbitrarily say that 0
1644 // methods means kOatClassNoneCompiled and that we won't use
1645 // kOatClassAllCompiled unless there is at least one compiled
1646 // method. This means in an interpretter only system, we can assert
1647 // that all classes are kOatClassNoneCompiled.
1648 if (num_non_null_compiled_methods == 0) {
1649 type_ = kOatClassNoneCompiled;
1650 } else if (num_non_null_compiled_methods == num_methods) {
1651 type_ = kOatClassAllCompiled;
1652 } else {
1653 type_ = kOatClassSomeCompiled;
1654 }
1655
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001656 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001657 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001658 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001659
1660 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1661 if (type_ == kOatClassSomeCompiled) {
1662 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1663 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1664 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1665 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1666 } else {
1667 method_bitmap_ = NULL;
1668 method_bitmap_size_ = 0;
1669 }
1670
1671 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001672 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001673 if (compiled_method == NULL) {
1674 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1675 } else {
1676 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1677 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1678 if (type_ == kOatClassSomeCompiled) {
1679 method_bitmap_->SetBit(i);
1680 }
1681 }
1682 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001683}
1684
Brian Carlstromba150c32013-08-27 17:31:03 -07001685OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001686 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001687}
1688
Brian Carlstrom265091e2013-01-30 14:08:26 -08001689size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1690 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001691 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1692 if (method_offset == 0) {
1693 return 0;
1694 }
1695 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001696}
1697
1698size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1699 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001700 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001701}
1702
1703size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001704 return sizeof(status_)
1705 + sizeof(type_)
1706 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1707 + method_bitmap_size_
1708 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001709}
1710
Ian Rogers3d504072014-03-01 09:16:49 -08001711void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1712 oat_header->UpdateChecksum(&status_, sizeof(status_));
1713 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001714 if (method_bitmap_size_ != 0) {
1715 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001716 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1717 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001718 }
Ian Rogers3d504072014-03-01 09:16:49 -08001719 oat_header->UpdateChecksum(&method_offsets_[0],
1720 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001721}
1722
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001723bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001724 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001725 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001726 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001727 if (!out->WriteFully(&status_, sizeof(status_))) {
1728 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001729 return false;
1730 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001731 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001732 if (!out->WriteFully(&type_, sizeof(type_))) {
1733 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001734 return false;
1735 }
1736 oat_writer->size_oat_class_type_ += sizeof(type_);
1737 if (method_bitmap_size_ != 0) {
1738 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001739 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1740 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001741 return false;
1742 }
1743 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001744 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1745 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001746 return false;
1747 }
1748 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1749 }
Ian Rogers3d504072014-03-01 09:16:49 -08001750 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001751 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001752 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001753 return false;
1754 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001755 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001756 return true;
1757}
1758
Brian Carlstrome24fa612011-09-29 00:53:55 -07001759} // namespace art