xref: /aosp_15_r20/external/zucchini/rel32_utils.cc (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/zucchini/rel32_utils.h"
6 
7 #include <algorithm>
8 
9 #include "base/check_op.h"
10 #include "components/zucchini/io_utils.h"
11 
12 namespace zucchini {
13 
14 /******** Rel32ReaderX86 ********/
15 
Rel32ReaderX86(ConstBufferView image,offset_t lo,offset_t hi,const std::deque<offset_t> * locations,const AddressTranslator & translator)16 Rel32ReaderX86::Rel32ReaderX86(ConstBufferView image,
17                                offset_t lo,
18                                offset_t hi,
19                                const std::deque<offset_t>* locations,
20                                const AddressTranslator& translator)
21     : image_(image),
22       target_rva_to_offset_(translator),
23       location_offset_to_rva_(translator),
24       hi_(hi),
25       last_(locations->end()) {
26   DCHECK_LE(lo, image.size());
27   DCHECK_LE(hi, image.size());
28   current_ = std::lower_bound(locations->begin(), locations->end(), lo);
29 }
30 
31 Rel32ReaderX86::~Rel32ReaderX86() = default;
32 
GetNext()33 std::optional<Reference> Rel32ReaderX86::GetNext() {
34   while (current_ < last_ && *current_ < hi_) {
35     offset_t loc_offset = *(current_++);
36     DCHECK_LE(loc_offset + 4, image_.size());  // Sanity check.
37     rva_t loc_rva = location_offset_to_rva_.Convert(loc_offset);
38     rva_t target_rva = loc_rva + 4 + image_.read<int32_t>(loc_offset);
39     offset_t target_offset = target_rva_to_offset_.Convert(target_rva);
40     // |locations| is valid by assumption (see class description).
41     DCHECK_NE(kInvalidOffset, target_offset);
42     return Reference{loc_offset, target_offset};
43   }
44   return std::nullopt;
45 }
46 
47 /******** Rel32ReceptorX86 ********/
48 
Rel32WriterX86(MutableBufferView image,const AddressTranslator & translator)49 Rel32WriterX86::Rel32WriterX86(MutableBufferView image,
50                                const AddressTranslator& translator)
51     : image_(image),
52       target_offset_to_rva_(translator),
53       location_offset_to_rva_(translator) {}
54 
55 Rel32WriterX86::~Rel32WriterX86() = default;
56 
PutNext(Reference ref)57 void Rel32WriterX86::PutNext(Reference ref) {
58   rva_t target_rva = target_offset_to_rva_.Convert(ref.target);
59   rva_t loc_rva = location_offset_to_rva_.Convert(ref.location);
60 
61   // Subtraction underflow is okay
62   uint32_t code =
63       static_cast<uint32_t>(target_rva) - (static_cast<uint32_t>(loc_rva) + 4);
64   image_.write<uint32_t>(ref.location, code);
65 }
66 
67 }  // namespace zucchini
68