Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Etienne Pierre-doray | efbbccf | 2018-07-24 15:42:33 | [diff] [blame] | 5 | #include "components/zucchini/reloc_win32.h" |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | #include <tuple> |
| 9 | #include <utility> |
| 10 | |
Samuel Huang | ff0bc47 | 2017-10-26 06:55:15 | [diff] [blame] | 11 | #include "base/logging.h" |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 12 | #include "base/numerics/safe_conversions.h" |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 13 | #include "components/zucchini/algorithm.h" |
| 14 | #include "components/zucchini/io_utils.h" |
| 15 | #include "components/zucchini/type_win_pe.h" |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 16 | |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 17 | namespace zucchini { |
| 18 | |
| 19 | /******** RelocUnitWin32 ********/ |
| 20 | |
| 21 | RelocUnitWin32::RelocUnitWin32() = default; |
| 22 | RelocUnitWin32::RelocUnitWin32(uint8_t type_in, |
| 23 | offset_t location_in, |
| 24 | rva_t target_rva_in) |
| 25 | : type(type_in), location(location_in), target_rva(target_rva_in) {} |
| 26 | |
| 27 | bool operator==(const RelocUnitWin32& a, const RelocUnitWin32& b) { |
| 28 | return std::tie(a.type, a.location, a.target_rva) == |
| 29 | std::tie(b.type, b.location, b.target_rva); |
| 30 | } |
| 31 | |
| 32 | /******** RelocRvaReaderWin32 ********/ |
| 33 | |
| 34 | // static |
| 35 | bool RelocRvaReaderWin32::FindRelocBlocks( |
| 36 | ConstBufferView image, |
| 37 | BufferRegion reloc_region, |
| 38 | std::vector<offset_t>* reloc_block_offsets) { |
| 39 | CHECK_LT(reloc_region.size, kOffsetBound); |
| 40 | ConstBufferView reloc_data = image[reloc_region]; |
| 41 | reloc_block_offsets->clear(); |
| 42 | while (reloc_data.size() >= sizeof(pe::RelocHeader)) { |
Etienne Pierre-doray | f6c70a2 | 2018-09-11 13:40:46 | [diff] [blame] | 43 | reloc_block_offsets->push_back( |
| 44 | base::checked_cast<offset_t>(reloc_data.begin() - image.begin())); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 45 | auto size = reloc_data.read<pe::RelocHeader>(0).size; |
| 46 | // |size| must be aligned to 4-bytes. |
| 47 | if (size < sizeof(pe::RelocHeader) || size % 4 || size > reloc_data.size()) |
| 48 | return false; |
| 49 | reloc_data.remove_prefix(size); |
| 50 | } |
| 51 | return reloc_data.empty(); // Fail if trailing data exist. |
| 52 | } |
| 53 | |
| 54 | RelocRvaReaderWin32::RelocRvaReaderWin32( |
| 55 | ConstBufferView image, |
| 56 | BufferRegion reloc_region, |
| 57 | const std::vector<offset_t>& reloc_block_offsets, |
| 58 | offset_t lo, |
| 59 | offset_t hi) |
| 60 | : image_(image) { |
| 61 | CHECK_LE(lo, hi); |
| 62 | lo = base::checked_cast<offset_t>(reloc_region.InclusiveClamp(lo)); |
| 63 | hi = base::checked_cast<offset_t>(reloc_region.InclusiveClamp(hi)); |
| 64 | end_it_ = image_.begin() + hi; |
| 65 | |
| 66 | // By default, get GetNext() to produce empty output. |
| 67 | cur_reloc_units_ = BufferSource(end_it_, 0); |
| 68 | if (reloc_block_offsets.empty()) |
| 69 | return; |
| 70 | |
| 71 | // Find the block that contains |lo|. |
| 72 | auto block_it = std::upper_bound(reloc_block_offsets.begin(), |
| 73 | reloc_block_offsets.end(), lo); |
| 74 | DCHECK(block_it != reloc_block_offsets.begin()); |
| 75 | --block_it; |
| 76 | |
| 77 | // Initialize |cur_reloc_units_| and |rva_hi_bits_|. |
| 78 | if (!LoadRelocBlock(image_.begin() + *block_it)) |
| 79 | return; // Nothing left. |
| 80 | |
| 81 | // Skip |cur_reloc_units_| to |lo|, truncating up. |
Etienne Pierre-doray | f6c70a2 | 2018-09-11 13:40:46 | [diff] [blame] | 82 | offset_t cur_reloc_units_offset = |
| 83 | base::checked_cast<offset_t>(cur_reloc_units_.begin() - image_.begin()); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 84 | if (lo > cur_reloc_units_offset) { |
| 85 | offset_t delta = |
Samuel Huang | 2c50b5af | 2018-06-13 17:47:39 | [diff] [blame] | 86 | AlignCeil<offset_t>(lo - cur_reloc_units_offset, kRelocUnitSize); |
Samuel Huang | c019fe9 | 2023-05-31 04:14:21 | [diff] [blame^] | 87 | // Okay if this empties |cur_reloc_units_|. |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 88 | cur_reloc_units_.Skip(delta); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | RelocRvaReaderWin32::RelocRvaReaderWin32(RelocRvaReaderWin32&&) = default; |
| 93 | |
| 94 | RelocRvaReaderWin32::~RelocRvaReaderWin32() = default; |
| 95 | |
| 96 | // Unrolls a nested loop: outer = reloc blocks and inner = reloc entries. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 97 | absl::optional<RelocUnitWin32> RelocRvaReaderWin32::GetNext() { |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 98 | // "Outer loop" to find non-empty reloc block. |
| 99 | while (cur_reloc_units_.Remaining() < kRelocUnitSize) { |
| 100 | if (!LoadRelocBlock(cur_reloc_units_.end())) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 101 | return absl::nullopt; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 102 | } |
| 103 | if (end_it_ - cur_reloc_units_.begin() < kRelocUnitSize) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 104 | return absl::nullopt; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 105 | // "Inner loop" to extract single reloc unit. |
Etienne Pierre-doray | f6c70a2 | 2018-09-11 13:40:46 | [diff] [blame] | 106 | offset_t location = |
| 107 | base::checked_cast<offset_t>(cur_reloc_units_.begin() - image_.begin()); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 108 | uint16_t entry = cur_reloc_units_.read<uint16_t>(0); |
| 109 | uint8_t type = static_cast<uint8_t>(entry >> 12); |
| 110 | rva_t rva = rva_hi_bits_ + (entry & 0xFFF); |
Samuel Huang | c019fe9 | 2023-05-31 04:14:21 | [diff] [blame^] | 111 | if (!cur_reloc_units_.Skip(kRelocUnitSize)) { |
| 112 | return absl::nullopt; |
| 113 | } |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 114 | return RelocUnitWin32{type, location, rva}; |
| 115 | } |
| 116 | |
| 117 | bool RelocRvaReaderWin32::LoadRelocBlock( |
| 118 | ConstBufferView::const_iterator block_begin) { |
| 119 | ConstBufferView header_buf(block_begin, sizeof(pe::RelocHeader)); |
| 120 | if (header_buf.end() >= end_it_ || |
| 121 | end_it_ - header_buf.end() < kRelocUnitSize) { |
| 122 | return false; |
| 123 | } |
| 124 | const auto& header = header_buf.read<pe::RelocHeader>(0); |
| 125 | rva_hi_bits_ = header.rva_hi; |
| 126 | uint32_t block_size = header.size; |
Calder Kitagawa | 9754448 | 2018-04-05 12:56:50 | [diff] [blame] | 127 | if (block_size < sizeof(pe::RelocHeader)) |
| 128 | return false; |
| 129 | if ((block_size - sizeof(pe::RelocHeader)) % kRelocUnitSize != 0) |
| 130 | return false; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 131 | cur_reloc_units_ = BufferSource(block_begin, block_size); |
Samuel Huang | c019fe9 | 2023-05-31 04:14:21 | [diff] [blame^] | 132 | cur_reloc_units_.Skip(sizeof(pe::RelocHeader)); // Always succeeds. |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /******** RelocReaderWin32 ********/ |
| 137 | |
| 138 | RelocReaderWin32::RelocReaderWin32(RelocRvaReaderWin32&& reloc_rva_reader, |
| 139 | uint16_t reloc_type, |
| 140 | offset_t offset_bound, |
| 141 | const AddressTranslator& translator) |
| 142 | : reloc_rva_reader_(std::move(reloc_rva_reader)), |
| 143 | reloc_type_(reloc_type), |
| 144 | offset_bound_(offset_bound), |
| 145 | entry_rva_to_offset_(translator) {} |
| 146 | |
| 147 | RelocReaderWin32::~RelocReaderWin32() = default; |
| 148 | |
| 149 | // ReferenceReader: |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 150 | absl::optional<Reference> RelocReaderWin32::GetNext() { |
| 151 | for (absl::optional<RelocUnitWin32> unit = reloc_rva_reader_.GetNext(); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 152 | unit.has_value(); unit = reloc_rva_reader_.GetNext()) { |
| 153 | if (unit->type != reloc_type_) |
| 154 | continue; |
| 155 | offset_t target = entry_rva_to_offset_.Convert(unit->target_rva); |
| 156 | if (target == kInvalidOffset) |
| 157 | continue; |
Samuel Huang | b6d108f | 2018-10-10 15:48:10 | [diff] [blame] | 158 | // Ensure that |target| (abs32 reference) lies entirely within the image. |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 159 | if (target >= offset_bound_) |
| 160 | continue; |
Calder Kitagawa | 79c5e3545 | 2018-04-10 20:03:08 | [diff] [blame] | 161 | offset_t location = unit->location; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 162 | return Reference{location, target}; |
| 163 | } |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 164 | return absl::nullopt; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /******** RelocWriterWin32 ********/ |
| 168 | |
| 169 | RelocWriterWin32::RelocWriterWin32( |
| 170 | uint16_t reloc_type, |
| 171 | MutableBufferView image, |
| 172 | BufferRegion reloc_region, |
| 173 | const std::vector<offset_t>& reloc_block_offsets, |
| 174 | const AddressTranslator& translator) |
| 175 | : reloc_type_(reloc_type), |
| 176 | image_(image), |
| 177 | reloc_region_(reloc_region), |
| 178 | reloc_block_offsets_(reloc_block_offsets), |
| 179 | target_offset_to_rva_(translator) {} |
| 180 | |
| 181 | RelocWriterWin32::~RelocWriterWin32() = default; |
| 182 | |
| 183 | void RelocWriterWin32::PutNext(Reference ref) { |
| 184 | DCHECK_GE(ref.location, reloc_region_.lo()); |
| 185 | DCHECK_LT(ref.location, reloc_region_.hi()); |
Ali Hijazi | a709b48b | 2022-11-09 01:27:44 | [diff] [blame] | 186 | auto block_it = std::upper_bound(reloc_block_offsets_->begin(), |
| 187 | reloc_block_offsets_->end(), ref.location); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 188 | --block_it; |
| 189 | rva_t rva_hi_bits = image_.read<pe::RelocHeader>(*block_it).rva_hi; |
| 190 | rva_t target_rva = target_offset_to_rva_.Convert(ref.target); |
Calder Kitagawa | e8d1c97 | 2018-07-09 18:51:54 | [diff] [blame] | 191 | rva_t rva_lo_bits = (target_rva - rva_hi_bits) & 0xFFF; |
| 192 | if (target_rva != rva_hi_bits + rva_lo_bits) { |
| 193 | LOG(ERROR) << "Invalid RVA at " << AsHex<8>(ref.location) << "."; |
| 194 | return; |
| 195 | } |
| 196 | image_.write<uint16_t>(ref.location, rva_lo_bits | (reloc_type_ << 12)); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | } // namespace zucchini |