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/Partitioner2/BasicTypes.h>
7 #include <Sawyer/Map.h>
10 namespace BinaryAnalysis {
123 : offset(offset), type(type), value(value) {}
132 : originalVa(insn->get_address()) {}
146 size_t minChunkAllocationSize_;
147 size_t chunkAllocationAlignment_;
148 std::string chunkAllocationName_;
151 unsigned aggregationDirection_;
255 size_t startIdx,
size_t nInsns, std::vector<uint8_t> replacement,
256 const std::vector<Relocation> &relocations = std::vector<Relocation>());
264 const std::vector<Relocation> &relocations = std::vector<Relocation>());
272 const std::vector<Relocation> &relocations = std::vector<Relocation>());
281 const std::vector<Relocation> &relocations = std::vector<Relocation>());
290 const std::vector<Relocation> &relocations = std::vector<Relocation>());
308 virtual bool replaceInsns(
const std::vector<SgAsmInstruction*> &toReplace,
const std::vector<uint8_t> &replacement,
309 const std::vector<Relocation> &relocations = std::vector<Relocation>());
322 virtual std::vector<uint8_t>
encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa);
329 virtual std::vector<uint8_t>
applyRelocations(rose_addr_t startVa, std::vector<uint8_t> replacement,
330 const std::vector<Relocation> &relocations,
size_t relocStart,
331 const InstructionInfoMap &insnInfoMap);
365 const std::vector<uint8_t> &replacement,
const std::vector<Relocation> &relocations,
366 size_t relocStart,
const InstructionInfoMap &insnInfoMap);
378 const std::vector<SgAsmInstruction*> &toReplace,
const std::vector<uint8_t> &replacement,
379 const std::vector<Relocation> &relocations,
size_t relocStart,
380 const InstructionInfoMap &insnInfoMap);
388 size_t startIdx,
size_t nDeleted);
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
RelocType type
Relocation algorithm.
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.
rose_addr_t value
Argument for relocation algorithm.
Move preceding instructions in CFG.
void minChunkAllocationSize(size_t n)
Property: Minimum size of allocated chunks.
Base class for machine instructions.
AddressIntervalSet instructionLocations(const std::vector< SgAsmInstruction * > &)
Given a list of functions, return all addresses that the instructions occupy.
virtual void fillWithRandom(const AddressIntervalSet &where)
Fill the specified memory with random data.
Only query an allocation.
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
virtual void fillWithNops(const AddressIntervalSet &where)
Fill the specified memory with no-op instructions.
size_t offset
Location of relocation in replacement code.
virtual AddressInterval allocateMemory(size_t nBytes, rose_addr_t jmpTargetVa, Commit::Boolean commit=Commit::YES)
Allocate virtual memory in the partitioner memory map.
rose_addr_t originalVa
Original address of instruction.
unsigned aggregationDirection() const
Property: Whether additional instructions can be moved.
AggregationDirection
What other instructions can be moved to make room.
void chunkAllocationRegion(const AddressInterval &i)
Property: Where chunks are allocated.
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.
static void initDiagnostics()
Initialize diagnostic streams.
Add random data to the end of replacements.
const std::string & chunkAllocationName() const
Property: Name for newly allocated regions of memory.
Main namespace for the ROSE library.
Sawyer::Container::Map< int, InstructionInfo > InstructionInfoMap
Information about instructions within the basic block being modified.
Relocation(size_t offset, RelocType type, rose_addr_t value)
Constructor.
Add no-ops to the end of replacements.
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.
size_t minChunkAllocationSize() const
Property: Minimum size of allocated chunks.
void commitAllocation(const AddressInterval &where, Commit::Boolean commit=Commit::YES)
Commit previous allocation.
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
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.
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
virtual std::vector< uint8_t > encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa)
Encode an unconditional branch.
Move succeeding instructions in CFG.
const AddressIntervalSet & allocatedChunks() const
Returns the parts of the virtual address space that were allocated for new instructions.
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
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.
size_t chunkAllocationAlignment() const
Property: Alignment for large allocated chunks.
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
void chunkAllocationName(const std::string &s)
Property: Name for newly allocated regions of memory.
Insert new code in place of existing instructions.
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
Allocate memory for real.
void nopPadding(NopPadding p)
Property: Where to add no-ops when padding.
RelocType
Type of relocation to perform.
Information about an instruction within the basic block being modified.
Add no-ops to the front of replacements.
NopPadding nopPadding() const
Property: Where to add no-ops when padding.
static Diagnostics::Facility mlog
Facility for emitting diagnostics.
Sawyer::Optional< rose_addr_t > newVaOffset
Offset of instruction from front of encoded insn vector.
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.
void aggregationDirection(unsigned d)
Property: Whether additional instructions can be moved.
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 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.
InstructionInfoMap computeInstructionInfoMap(const Partitioner2::BasicBlockPtr &, size_t startIdx, size_t nDeleted)
Obtain info about instructions for the basic block being modified.
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
NopPadding
How to pad with no-ops.
Container associating values with keys.
const AddressInterval & chunkAllocationRegion() const
Property: Where chunks are allocated.
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.