blob: 19495ca92c137cfc0e591ace311da795078c0fa2 [file] [log] [blame]
Mathieu Chartier8892c6b2018-01-09 15:10:17 -08001/*
2 * Copyright (C) 2018 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 */
16
17#include "compact_dex_debug_info.h"
18
19#include "compact_dex_utils.h"
20#include "leb128.h"
21
22namespace art {
23
24constexpr size_t CompactDexDebugInfoOffsetTable::kElementsPerIndex;
25
26CompactDexDebugInfoOffsetTable::Accessor::Accessor(const uint8_t* data_begin,
27 uint32_t debug_info_base,
28 uint32_t debug_info_table_offset)
29 : table_(reinterpret_cast<const uint32_t*>(data_begin + debug_info_table_offset)),
30 debug_info_base_(debug_info_base),
31 data_begin_(data_begin) {}
32
33uint32_t CompactDexDebugInfoOffsetTable::Accessor::GetDebugInfoOffset(uint32_t method_idx) const {
34 const uint32_t offset = table_[method_idx / kElementsPerIndex];
35 const size_t bit_index = method_idx % kElementsPerIndex;
36
37 const uint8_t* block = data_begin_ + offset;
38 uint16_t bit_mask = *block;
39 ++block;
40 bit_mask = (bit_mask << kBitsPerByte) | *block;
41 ++block;
42 if ((bit_mask & (1 << bit_index)) == 0) {
43 // Bit is not set means the offset is 0 for the debug info.
44 return 0u;
45 }
46 // Trim off the bits above the index we want and count how many bits are set. This is how many
47 // lebs we need to decode.
48 size_t count = POPCOUNT(static_cast<uintptr_t>(bit_mask) << (kBitsPerIntPtrT - 1 - bit_index));
49 DCHECK_GT(count, 0u);
50 uint32_t current_offset = debug_info_base_;
51 do {
52 current_offset += DecodeUnsignedLeb128(&block);
53 --count;
54 } while (count > 0);
55 return current_offset;
56}
57
58void CompactDexDebugInfoOffsetTable::Build(const std::vector<uint32_t>& debug_info_offsets,
59 std::vector<uint8_t>* out_data,
60 uint32_t* out_min_offset,
61 uint32_t* out_table_offset) {
62 DCHECK(out_data != nullptr);
63 DCHECK(out_data->empty());
64 // Calculate the base offset and return it.
65 *out_min_offset = std::numeric_limits<uint32_t>::max();
66 for (const uint32_t offset : debug_info_offsets) {
67 if (offset != 0u) {
68 *out_min_offset = std::min(*out_min_offset, offset);
69 }
70 }
71 // Write the leb blocks and store the important offsets (each kElementsPerIndex elements).
72 size_t block_start = 0;
73
74 std::vector<uint32_t> offset_table;
75
76 // Write data first then the table.
77 while (block_start < debug_info_offsets.size()) {
78 // Write the offset of the block for each block.
79 offset_table.push_back(out_data->size());
80
81 // Block size of up to kElementsPerIndex
82 const size_t block_size = std::min(debug_info_offsets.size() - block_start, kElementsPerIndex);
83
84 // Calculate bit mask since need to write that first.
85 uint16_t bit_mask = 0u;
86 for (size_t i = 0; i < block_size; ++i) {
87 if (debug_info_offsets[block_start + i] != 0u) {
88 bit_mask |= 1 << i;
89 }
90 }
91 // Write bit mask.
92 out_data->push_back(static_cast<uint8_t>(bit_mask >> kBitsPerByte));
93 out_data->push_back(static_cast<uint8_t>(bit_mask));
94
95 // Write debug info offsets relative to the current offset.
96 uint32_t current_offset = *out_min_offset;
97 for (size_t i = 0; i < block_size; ++i) {
98 const uint32_t debug_info_offset = debug_info_offsets[block_start + i];
99 if (debug_info_offset != 0u) {
100 uint32_t delta = debug_info_offset - current_offset;
101 EncodeUnsignedLeb128(out_data, delta);
102 current_offset = debug_info_offset;
103 }
104 }
105
106 block_start += block_size;
107 }
108
109 // Write the offset table.
110 AlignmentPadVector(out_data, alignof(uint32_t));
111 *out_table_offset = out_data->size();
112 out_data->insert(out_data->end(),
113 reinterpret_cast<const uint8_t*>(&offset_table[0]),
114 reinterpret_cast<const uint8_t*>(&offset_table[0] + offset_table.size()));
115}
116
117} // namespace art