blob: 8601b19613f8872bd139de0d729fcffbf300111d [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
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080017#include "compact_offset_table.h"
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080018
19#include "compact_dex_utils.h"
David Sehr67bf42e2018-02-26 16:43:04 -080020#include "base/leb128.h"
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080021
22namespace art {
23
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080024constexpr size_t CompactOffsetTable::kElementsPerIndex;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080025
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080026CompactOffsetTable::Accessor::Accessor(const uint8_t* data_begin,
27 uint32_t minimum_offset,
28 uint32_t table_offset)
29 : table_(reinterpret_cast<const uint32_t*>(data_begin + table_offset)),
30 minimum_offset_(minimum_offset),
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080031 data_begin_(data_begin) {}
32
Mathieu Chartier2daa1342018-02-20 16:19:28 -080033CompactOffsetTable::Accessor::Accessor(const uint8_t* data_begin)
34 : Accessor(data_begin + 2 * sizeof(uint32_t),
35 reinterpret_cast<const uint32_t*>(data_begin)[0],
36 reinterpret_cast<const uint32_t*>(data_begin)[1]) {}
37
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080038uint32_t CompactOffsetTable::Accessor::GetOffset(uint32_t index) const {
39 const uint32_t offset = table_[index / kElementsPerIndex];
40 const size_t bit_index = index % kElementsPerIndex;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080041
42 const uint8_t* block = data_begin_ + offset;
43 uint16_t bit_mask = *block;
44 ++block;
45 bit_mask = (bit_mask << kBitsPerByte) | *block;
46 ++block;
47 if ((bit_mask & (1 << bit_index)) == 0) {
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080048 // Bit is not set means the offset is 0.
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080049 return 0u;
50 }
51 // Trim off the bits above the index we want and count how many bits are set. This is how many
52 // lebs we need to decode.
53 size_t count = POPCOUNT(static_cast<uintptr_t>(bit_mask) << (kBitsPerIntPtrT - 1 - bit_index));
54 DCHECK_GT(count, 0u);
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080055 uint32_t current_offset = minimum_offset_;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080056 do {
57 current_offset += DecodeUnsignedLeb128(&block);
58 --count;
59 } while (count > 0);
60 return current_offset;
61}
62
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080063void CompactOffsetTable::Build(const std::vector<uint32_t>& offsets,
Mathieu Chartier2daa1342018-02-20 16:19:28 -080064 std::vector<uint8_t>* out_data) {
65 static constexpr size_t kNumOffsets = 2;
66 uint32_t out_offsets[kNumOffsets] = {};
67 CompactOffsetTable::Build(offsets, out_data, &out_offsets[0], &out_offsets[1]);
68 // Write the offsets at the start of the debug info.
69 out_data->insert(out_data->begin(),
70 reinterpret_cast<const uint8_t*>(&out_offsets[0]),
71 reinterpret_cast<const uint8_t*>(&out_offsets[kNumOffsets]));
72}
73
74void CompactOffsetTable::Build(const std::vector<uint32_t>& offsets,
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080075 std::vector<uint8_t>* out_data,
76 uint32_t* out_min_offset,
77 uint32_t* out_table_offset) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080078 DCHECK(out_data != nullptr);
79 DCHECK(out_data->empty());
80 // Calculate the base offset and return it.
81 *out_min_offset = std::numeric_limits<uint32_t>::max();
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080082 for (const uint32_t offset : offsets) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080083 if (offset != 0u) {
84 *out_min_offset = std::min(*out_min_offset, offset);
85 }
86 }
87 // Write the leb blocks and store the important offsets (each kElementsPerIndex elements).
88 size_t block_start = 0;
89
90 std::vector<uint32_t> offset_table;
91
92 // Write data first then the table.
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080093 while (block_start < offsets.size()) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080094 // Write the offset of the block for each block.
95 offset_table.push_back(out_data->size());
96
97 // Block size of up to kElementsPerIndex
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -080098 const size_t block_size = std::min(offsets.size() - block_start, kElementsPerIndex);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080099
100 // Calculate bit mask since need to write that first.
101 uint16_t bit_mask = 0u;
102 for (size_t i = 0; i < block_size; ++i) {
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -0800103 if (offsets[block_start + i] != 0u) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800104 bit_mask |= 1 << i;
105 }
106 }
107 // Write bit mask.
108 out_data->push_back(static_cast<uint8_t>(bit_mask >> kBitsPerByte));
109 out_data->push_back(static_cast<uint8_t>(bit_mask));
110
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -0800111 // Write offsets relative to the previous offset.
112 uint32_t prev_offset = *out_min_offset;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800113 for (size_t i = 0; i < block_size; ++i) {
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -0800114 const uint32_t offset = offsets[block_start + i];
115 if (offset != 0u) {
116 uint32_t delta = offset - prev_offset;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800117 EncodeUnsignedLeb128(out_data, delta);
Mathieu Chartier5e3cfa22018-02-20 16:53:37 -0800118 prev_offset = offset;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800119 }
120 }
121
122 block_start += block_size;
123 }
124
125 // Write the offset table.
126 AlignmentPadVector(out_data, alignof(uint32_t));
127 *out_table_offset = out_data->size();
128 out_data->insert(out_data->end(),
129 reinterpret_cast<const uint8_t*>(&offset_table[0]),
130 reinterpret_cast<const uint8_t*>(&offset_table[0] + offset_table.size()));
131}
132
133} // namespace art