blob: 6e502b3f26830d12cdc31e917e04947f86a5fae2 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
#include <lib/arch/nop.h>
#include <zircon/assert.h>
#include <cstddef>
#include <cstring>
#include <fbl/span.h>
// This file is concerned with the facilities of code-patching.
namespace code_patching {
// A patch directive, giving the 'what' of an instruction range and the 'how'
// and 'when' of a patch case identifier.
struct Directive {
uint64_t range_start;
uint32_t range_size;
// A patch case identifier, corresponding to particular hard-coded details on
// how and when code should be the replaced.
uint32_t id;
// Ensures against alignment padding.
// Replaces a range of instuctions with the minimal number of `nop`
// instructions.
using arch::NopFill;
// Performs a code patch, replacing a range of instructions with an opaque
// blob.
inline void Patch(fbl::Span<std::byte> instructions, fbl::Span<const std::byte> blob) {
ZX_ASSERT_MSG(instructions.size() >= blob.size(), "%zu >= %zu", instructions.size(), blob.size());
memcpy(,, blob.size());
} // namespace code_patching