rel32_utils.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "components/zucchini/rel32_utils.h"
  5. #include <algorithm>
  6. #include "base/check_op.h"
  7. #include "components/zucchini/io_utils.h"
  8. namespace zucchini {
  9. /******** Rel32ReaderX86 ********/
  10. Rel32ReaderX86::Rel32ReaderX86(ConstBufferView image,
  11. offset_t lo,
  12. offset_t hi,
  13. const std::deque<offset_t>* locations,
  14. const AddressTranslator& translator)
  15. : image_(image),
  16. target_rva_to_offset_(translator),
  17. location_offset_to_rva_(translator),
  18. hi_(hi),
  19. last_(locations->end()) {
  20. DCHECK_LE(lo, image.size());
  21. DCHECK_LE(hi, image.size());
  22. current_ = std::lower_bound(locations->begin(), locations->end(), lo);
  23. }
  24. Rel32ReaderX86::~Rel32ReaderX86() = default;
  25. absl::optional<Reference> Rel32ReaderX86::GetNext() {
  26. while (current_ < last_ && *current_ < hi_) {
  27. offset_t loc_offset = *(current_++);
  28. DCHECK_LE(loc_offset + 4, image_.size()); // Sanity check.
  29. rva_t loc_rva = location_offset_to_rva_.Convert(loc_offset);
  30. rva_t target_rva = loc_rva + 4 + image_.read<int32_t>(loc_offset);
  31. offset_t target_offset = target_rva_to_offset_.Convert(target_rva);
  32. // |locations| is valid by assumption (see class description).
  33. DCHECK_NE(kInvalidOffset, target_offset);
  34. return Reference{loc_offset, target_offset};
  35. }
  36. return absl::nullopt;
  37. }
  38. /******** Rel32ReceptorX86 ********/
  39. Rel32WriterX86::Rel32WriterX86(MutableBufferView image,
  40. const AddressTranslator& translator)
  41. : image_(image),
  42. target_offset_to_rva_(translator),
  43. location_offset_to_rva_(translator) {}
  44. Rel32WriterX86::~Rel32WriterX86() = default;
  45. void Rel32WriterX86::PutNext(Reference ref) {
  46. rva_t target_rva = target_offset_to_rva_.Convert(ref.target);
  47. rva_t loc_rva = location_offset_to_rva_.Convert(ref.location);
  48. // Subtraction underflow is okay
  49. uint32_t code =
  50. static_cast<uint32_t>(target_rva) - (static_cast<uint32_t>(loc_rva) + 4);
  51. image_.write<uint32_t>(ref.location, code);
  52. }
  53. void OutputArmCopyDispFailure(uint32_t addr_type) {
  54. // Failed to mix old payload bits with new operation bits. The main cause of
  55. // this rare failure is when BL (encoding T1) with payload bits representing
  56. // disp % 4 == 2 transforms into BLX (encoding T2). Error arises because BLX
  57. // requires payload bits to have disp == 0 (mod 4). Mixing failures are not
  58. // fatal to patching; we simply fall back to direct copy and forgo benefits
  59. // from mixing for these cases. TODO(huangs, etiennep): Ongoing discussion on
  60. // whether we should just nullify all payload disp so we won't have to deal
  61. // with this case, but at the cost of having Zucchini-apply do more work.
  62. static int output_quota = 10;
  63. if (output_quota > 0) {
  64. LOG(WARNING) << "Reference byte mix failed with type = " << addr_type << "."
  65. << std::endl;
  66. --output_quota;
  67. if (!output_quota)
  68. LOG(WARNING) << "(Additional output suppressed)";
  69. }
  70. }
  71. } // namespace zucchini