1#ifndef ROSE_BinaryAnalysis_CodeInserter_H
2#define ROSE_BinaryAnalysis_CodeInserter_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6#include <Rose/BinaryAnalysis/AddressIntervalSet.h>
7#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
10#include <Sawyer/Message.h>
15namespace BinaryAnalysis {
150 size_t minChunkAllocationSize_;
151 size_t chunkAllocationAlignment_;
152 std::string chunkAllocationName_;
155 unsigned aggregationDirection_;
270 size_t startIdx,
size_t nInsns, std::vector<uint8_t> replacement,
271 const std::vector<Relocation> &relocations = std::vector<Relocation>());
279 const std::vector<Relocation> &relocations = std::vector<Relocation>());
287 const std::vector<Relocation> &relocations = std::vector<Relocation>());
296 const std::vector<Relocation> &relocations = std::vector<Relocation>());
305 const std::vector<Relocation> &relocations = std::vector<Relocation>());
323 virtual bool replaceInsns(
const std::vector<SgAsmInstruction*> &toReplace,
const std::vector<uint8_t> &replacement,
324 const std::vector<Relocation> &relocations = std::vector<Relocation>());
337 virtual std::vector<uint8_t>
encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa);
344 virtual std::vector<uint8_t>
applyRelocations(rose_addr_t startVa, std::vector<uint8_t> replacement,
345 const std::vector<Relocation> &relocations,
size_t relocStart,
380 const std::vector<uint8_t> &replacement,
const std::vector<Relocation> &relocations,
393 const std::vector<SgAsmInstruction*> &toReplace,
const std::vector<uint8_t> &replacement,
394 const std::vector<Relocation> &relocations,
size_t relocStart,
403 size_t startIdx,
size_t nDeleted);
Insert new code in place of existing instructions.
void aggregationDirection(unsigned d)
Property: Whether additional instructions can be moved.
const AddressIntervalSet & allocatedChunks() const
Returns the parts of the virtual address space that were allocated for new instructions.
virtual bool replaceByTransfer(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval, const std::vector< SgAsmInstruction * > &toReplace, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations, size_t relocStart, const InstructionInfoMap &insnInfoMap)
Insert new code in allocated area.
Sawyer::Container::Map< int, InstructionInfo > InstructionInfoMap
Information about instructions within the basic block being modified.
void nopPadding(NopPadding p)
Property: Where to add no-ops when padding.
unsigned aggregationDirection() const
Property: Whether additional instructions can be moved.
static Sawyer::Message::Facility mlog
Facility for emitting diagnostics.
void chunkAllocationAlignment(size_t n)
Property: Alignment for large allocated chunks.
static void initDiagnostics()
Initialize diagnostic streams.
virtual bool appendInsns(const Partitioner2::BasicBlockPtr &, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Append code to a basic block.
virtual std::vector< uint8_t > encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa)
Encode an unconditional branch.
virtual void fillWithNops(const AddressIntervalSet &where)
Fill the specified memory with no-op instructions.
virtual void fillWithRandom(const AddressIntervalSet &where)
Fill the specified memory with random data.
void commitAllocation(const AddressInterval &where, Commit::Boolean commit=Commit::YES)
Commit previous allocation.
AggregationDirection
What other instructions can be moved to make room.
@ AGGREGATE_SUCCESSORS
Move succeeding instructions in CFG.
@ AGGREGATE_PREDECESSORS
Move preceding instructions in CFG.
void minChunkAllocationSize(size_t n)
Property: Minimum size of allocated chunks.
void chunkAllocationRegion(const AddressInterval &i)
Property: Where chunks are allocated.
const AddressInterval & chunkAllocationRegion() const
Property: Where chunks are allocated.
NopPadding
How to pad with no-ops.
@ PAD_RANDOM_BACK
Add random data to the end of replacements.
@ PAD_NOP_FRONT
Add no-ops to the front of replacements.
@ PAD_NOP_BACK
Add no-ops to the end of replacements.
void mappedFreeSpace(const AddressIntervalSet &x)
Property: Mapped free-space chunks.
const AddressIntervalSet & mappedFreeSpace() const
Property: Mapped free-space chunks.
AddressIntervalSet & mappedFreeSpace()
Property: Mapped free-space chunks.
virtual AddressInterval allocateMemory(size_t nBytes, rose_addr_t jmpTargetVa, Commit::Boolean commit=Commit::YES)
Allocate virtual memory in the partitioner memory map.
AddressIntervalSet instructionLocations(const std::vector< SgAsmInstruction * > &)
Given a list of functions, return all addresses that the instructions occupy.
virtual bool replaceInsnsAtBack(const Partitioner2::BasicBlockPtr &, size_t nInsns, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace instructions at back of basic block.
NopPadding nopPadding() const
Property: Where to add no-ops when padding.
const std::string & chunkAllocationName() const
Property: Name for newly allocated regions of memory.
void chunkAllocationName(const std::string &s)
Property: Name for newly allocated regions of memory.
InstructionInfoMap computeInstructionInfoMap(const Partitioner2::BasicBlockPtr &, size_t startIdx, size_t nDeleted)
Obtain info about instructions for the basic block being modified.
RelocType
Type of relocation to perform.
@ RELOC_INDEX_ABS_LE32HI
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
@ RELOC_INDEX_ABS_LE32
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
@ RELOC_ADDR_REL_BE32
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
@ RELOC_ADDR_REL_LE32
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
@ RELOC_INDEX_ABS_BE32
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
@ RELOC_INSN_ABS_LE32
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
@ RELOC_INSN_REL_LE32
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
@ RELOC_INSN_REL_BE32
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
virtual std::vector< uint8_t > applyRelocations(rose_addr_t startVa, std::vector< uint8_t > replacement, const std::vector< Relocation > &relocations, size_t relocStart, const InstructionInfoMap &insnInfoMap)
Apply relocations to create a new encoding.
virtual bool prependInsns(const Partitioner2::BasicBlockPtr &, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Prepend code to a basic block.
virtual bool replaceBlockInsns(const Partitioner2::BasicBlockPtr &, size_t startIdx, size_t nInsns, std::vector< uint8_t > replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace instructions in basic block.
size_t chunkAllocationAlignment() const
Property: Alignment for large allocated chunks.
virtual bool replaceInsns(const std::vector< SgAsmInstruction * > &toReplace, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace exactly the specified instructions with some other encoding.
bool replaceInsnsAtFront(const Partitioner2::BasicBlockPtr &, size_t nInsns, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace instructions at front of basic block.
virtual bool replaceByOverwrite(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations, size_t relocStart, const InstructionInfoMap &insnInfoMap)
Insert new code by overwriting existing instructions.
size_t minChunkAllocationSize() const
Property: Minimum size of allocated chunks.
Container associating values with keys.
Holds a value or nothing.
Base class for machine instructions.
@ YES
Allocate memory for real.
@ NO
Only query an allocation.
Information about an instruction within the basic block being modified.
rose_addr_t originalVa
Original address of instruction.
Sawyer::Optional< rose_addr_t > newVaOffset
Offset of instruction from front of encoded insn vector.
size_t offset
Location of relocation in replacement code.
Relocation(size_t offset, RelocType type, rose_addr_t value)
Constructor.
RelocType type
Relocation algorithm.
rose_addr_t value
Argument for relocation algorithm.