blob: 7a01230d1212103d48bd8120b6e8af58bdd7df5f [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "base/macros.h"
#include "base/optional.h"
#include "components/zucchini/address_translator.h"
#include "components/zucchini/buffer_view.h"
#include "components/zucchini/image_utils.h"
namespace zucchini {
// A visitor that emits References (locations and target) from a specified
// portion of an x86 / x64 image, given a list of valid locations.
class Rel32ReaderX86 : public ReferenceReader {
// |image| is an image containing x86 / x64 code in [|lo|, |hi|).
// |locations| is a sorted list of offsets of rel32 reference locations.
// |translator| (for |image|) is embedded into |target_rva_to_offset_| and
// |location_offset_to_rva_| for address translation, and therefore must
// outlive |*this|.
Rel32ReaderX86(ConstBufferView image,
offset_t lo,
offset_t hi,
const std::vector<offset_t>* locations,
const AddressTranslator& translator);
~Rel32ReaderX86() override;
// Returns the next reference, or base::nullopt if exhausted.
base::Optional<Reference> GetNext() override;
ConstBufferView image_;
AddressTranslator::RvaToOffsetCache target_rva_to_offset_;
AddressTranslator::OffsetToRvaCache location_offset_to_rva_;
const offset_t hi_;
const std::vector<offset_t>::const_iterator last_;
std::vector<offset_t>::const_iterator current_;
// Writer for x86 / x64 rel32 references.
class Rel32WriterX86 : public ReferenceWriter {
// |image| wraps the raw bytes of a binary in which rel32 references will be
// written. |translator| (for |image|) is embedded into
// |target_offset_to_rva_| and |location_offset_to_rva_| for address
// translation, and therefore must outlive |*this|.
Rel32WriterX86(MutableBufferView image, const AddressTranslator& translator);
~Rel32WriterX86() override;
void PutNext(Reference ref) override;
MutableBufferView image_;
AddressTranslator::OffsetToRvaCache target_offset_to_rva_;
AddressTranslator::OffsetToRvaCache location_offset_to_rva_;
} // namespace zucchini