1 #ifndef ROSE_BinaryAnalysis_CallingConvention_H
2 #define ROSE_BinaryAnalysis_CallingConvention_H
4 #include <featureTests.h>
5 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
7 #include <BaseSemantics2.h>
8 #include <BinaryVariables.h>
9 #include <Partitioner2/BasicTypes.h>
10 #include <Registers.h>
11 #include <RegisterParts.h>
13 #include <boost/serialization/access.hpp>
14 #include <boost/serialization/set.hpp>
15 #include <boost/serialization/string.hpp>
16 #include <boost/serialization/vector.hpp>
17 #include <Sawyer/SharedObject.h>
18 #include <Sawyer/SharedPointer.h>
36 namespace CallingConvention {
105 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
107 friend class boost::serialization::access;
110 void serialize(S &s,
const unsigned ) {
111 s & BOOST_SERIALIZATION_NVP(type_);
112 s & BOOST_SERIALIZATION_NVP(reg_);
114 s & BOOST_SERIALIZATION_NVP(offset_);
116 s & BOOST_SERIALIZATION_NVP(va_);
132 : type_(
REGISTER), reg_(reg), offset_(0) {}
136 : type_(
STACK), reg_(reg), offset_(offset) {}
167 return STACK == type_ ? offset_ : (int64_t)0;
175 return ABSOLUTE == type_ ? va_ : (rose_addr_t)0;
182 return type_ == other.type_ && reg_ == other.reg_ && offset_ == other.offset_;
189 return type_ != other.type_ || reg_ != other.reg_ || offset_ != other.offset_;
194 std::ostringstream ss;
208 case REGISTER: out <<regnames(reg_);
break;
209 case STACK: out <<
"mem[" <<regnames(reg_) <<
"+" <<offset_ <<
"]";
break;
235 std::string comment_;
238 std::vector<ParameterLocation> inputParameters_;
239 std::vector<ParameterLocation> outputParameters_;
242 size_t nonParameterStackSize_;
243 size_t stackAlignment_;
247 std::set<RegisterDescriptor> calleeSavedRegisters_;
248 std::set<RegisterDescriptor> scratchRegisters_;
250 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
252 friend class boost::serialization::access;
255 void serialize(S &s,
const unsigned ) {
256 s & BOOST_SERIALIZATION_NVP(name_);
257 s & BOOST_SERIALIZATION_NVP(comment_);
258 s & BOOST_SERIALIZATION_NVP(wordWidth_);
259 s & BOOST_SERIALIZATION_NVP(regDict_);
260 s & BOOST_SERIALIZATION_NVP(inputParameters_);
261 s & BOOST_SERIALIZATION_NVP(outputParameters_);
262 s & BOOST_SERIALIZATION_NVP(stackParameterOrder_);
263 s & BOOST_SERIALIZATION_NVP(stackPointerRegister_);
264 s & BOOST_SERIALIZATION_NVP(nonParameterStackSize_);
265 s & BOOST_SERIALIZATION_NVP(stackAlignment_);
266 s & BOOST_SERIALIZATION_NVP(stackDirection_);
267 s & BOOST_SERIALIZATION_NVP(stackCleanup_);
268 s & BOOST_SERIALIZATION_NVP(thisParameter_);
269 s & BOOST_SERIALIZATION_NVP(calleeSavedRegisters_);
270 s & BOOST_SERIALIZATION_NVP(scratchRegisters_);
279 : wordWidth_(0), regDict_(NULL), stackParameterOrder_(
ORDER_UNSPECIFIED), nonParameterStackSize_(0),
288 : name_(name), comment_(comment), wordWidth_(wordWidth), regDict_(regDict), stackParameterOrder_(
ORDER_UNSPECIFIED),
290 ASSERT_require2(0 == (wordWidth & 7) && wordWidth > 0,
"word size must be a positive multiple of eight");
338 const std::string&
name()
const {
return name_; }
339 void name(
const std::string &s) { name_ = s; }
349 const std::string&
comment()
const {
return comment_; }
350 void comment(
const std::string &s) { comment_ = s; }
364 ASSERT_require2(nBits > 0 && 0 == (nBits & 7),
"word size must be a positive multiple of eight");
382 const std::vector<ParameterLocation>&
inputParameters()
const {
return inputParameters_; }
415 const std::vector<ParameterLocation>&
outputParameters()
const {
return outputParameters_; }
479 return nonParameterStackSize_;
482 nonParameterStackSize_ = nBytes;
651 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
653 friend class boost::serialization::access;
656 void serialize(S &s,
const unsigned ) {
657 s & BOOST_SERIALIZATION_NVP(cpu_);
658 s & BOOST_SERIALIZATION_NVP(regDict_);
659 s & BOOST_SERIALIZATION_NVP(defaultCc_);
660 s & BOOST_SERIALIZATION_NVP(hasResults_);
661 s & BOOST_SERIALIZATION_NVP(didConverge_);
662 s & BOOST_SERIALIZATION_NVP(restoredRegisters_);
663 s & BOOST_SERIALIZATION_NVP(inputRegisters_);
664 s & BOOST_SERIALIZATION_NVP(outputRegisters_);
665 s & BOOST_SERIALIZATION_NVP(inputStackParameters_);
666 s & BOOST_SERIALIZATION_NVP(outputStackParameters_);
667 s & BOOST_SERIALIZATION_NVP(stackDelta_);
678 : regDict_(NULL), hasResults_(false), didConverge_(false) {}
684 : regDict_(NULL), hasResults_(false), didConverge_(false) {
695 : cpu_(cpu), regDict_(NULL), hasResults_(false), didConverge_(false) {}
794 Dictionary
match(
const Dictionary&)
const;
800 void print(std::ostream&,
bool multiLine=
false)
const;
830 std::ostream& operator<<(std::ostream&,
const Definition&);
831 std::ostream& operator<<(std::ostream&,
const Analysis&);
void print(std::ostream &out, const RegisterNames ®names) const
Print location.
void thisParameter(rose_addr_t va)
Property: Object pointer parameter.
size_t wordWidth() const
Property: Word size in bits.
const Dictionary & dictionaryX86()
Common calling conventions for 32-bit x86.
const std::vector< ParameterLocation > & outputParameters() const
Property: List of output parameters.
void registerDictionary(const RegisterDictionary *d)
Property: Register dictionary.
StackParameterOrder stackParameterOrder() const
Property: Stack parameter order.
size_t stackAlignment() const
Property: Stack alignment.
const Dictionary & dictionaryM68k()
Common calling conventions for m68k.
void print(std::ostream &out, const RegisterDictionary *regdict) const
Print location.
Analysis()
Default constructor.
StackDirection stackDirection() const
Property: Direction that stack grows for a push operation.
The called function pops all stack parameters.
std::string toString(const RegisterDictionary *regdict) const
String representation.
const Variables::StackVariables & outputStackParameters() const
Output stack parameters.
void wordWidth(size_t nBits)
Property: Word size in bits.
void stackDirection(StackDirection x)
Property: Direction that stack grows for a push operation.
void appendInputParameter(const ParameterLocation &)
Append input parameter.
RegisterDescriptor reg() const
Register part of location.
static Ptr x86_32bit_cdecl()
Returns a predefined, cached calling convention.
const RegisterDictionary * registerDictionary() const
Property: Register dictionary.
void appendOutputParameter(rose_addr_t va)
Append output parameter.
const Dictionary & dictionaryAarch64()
Common calling conventions for ARM AArch64.
Sawyer::SharedPointer< class Definition > DefinitionPtr
Reference counting pointer to calling convention definition.
void registerDictionary(const RegisterDictionary *d)
Property: Register dictionary.
static Ptr x86_32bit_stdcall()
Allocating constructor.
A push decrements the stack pointer.
static Ptr ppc_32bit_ibm()
Allocating constructor.
void thisParameter(RegisterDescriptor reg, int64_t offset)
Property: Object pointer parameter.
ParameterLocation(RegisterDescriptor reg)
Constructs a parameter in a register location.
Used by default-constructed locations.
ParameterLocation(RegisterDescriptor reg, int64_t offset)
Constructs a parameter at a register-relative memory address.
void thisParameter(const ParameterLocation &x)
Property: Object pointer parameter.
const Dictionary & dictionaryPowerpc64()
Common calling conventions for PowerPC-64.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
StackDirection
The direction in which the stack grows.
Analysis(Disassembler *d)
Construct an analyzer using a specified disassembler.
The caller pops all stack parameters.
bool operator!=(const ParameterLocation &other) const
Inequality.
ParameterLocation()
Default constructed no-location.
const Dictionary & dictionaryAmd64()
Common calling conventions for amd64 (x86-64).
Function calling convention.
void appendInputParameter(rose_addr_t va)
Append input parameter.
Definition::Ptr defaultCallingConvention() const
Property: Default calling convention.
void thisParameter(RegisterDescriptor reg)
Property: Object pointer parameter.
void stackCleanup(StackCleanup x)
Property: Who pops stack parameters.
rose_addr_t address() const
Fixed address location.
Main namespace for the ROSE library.
RegisterParts inputRegisterParts() const
Compute the set of input registers.
RegisterParts scratchRegisterParts() const
Computes the set of scratch registers.
Holds a set of registers without regard for register boundaries.
StackCleanup stackCleanup() const
Property: Who pops stack parameters.
static Ptr x86_fastcall(const RegisterDictionary *)
Constructs a new pre-defined calling convention based on a register dictionary.
Sawyer::Optional< int64_t > stackDelta() const
Concrete stack delta.
Parameter in memory relative to a register.
bool hasResults() const
Whether a function has been analyzed.
Sawyer::SharedPointer< Definition > Ptr
Reference counting pointer to calling convention definition.
const ParameterLocation & thisParameter() const
Property: Object pointer parameter.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
void comment(const std::string &s)
Property: Full name of calling convention.
const RegisterParts & calleeSavedRegisters() const
Callee-saved registers.
void stackParameterOrder(StackParameterOrder x)
Property: Stack parameter order.
const RegisterParts & inputRegisters() const
Input registers.
void print(std::ostream &, bool multiLine=false) const
Print information about the analysis results.
const Dictionary & dictionaryPowerpc32()
Common calling conventions for PowerPC-32.
bool operator==(const ParameterLocation &other) const
Equality.
std::vector< Definition::Ptr > Dictionary
A ordered collection of calling convention definitions.
const Dictionary & dictionaryMips()
Common calling conventions for MIPS.
void appendOutputParameter(const ParameterLocation &)
Append output parameter.
static Ptr x86_cdecl(const RegisterDictionary *)
Constructs a new pre-defined calling convention based on a register dictionary.
static Ptr instance(size_t wordWidth, const std::string &name, const std::string &comment, const RegisterDictionary *regs)
Allocating constructor.
const Variables::StackVariables & inputStackParameters() const
Input stack parameters.
const std::set< RegisterDescriptor > & calleeSavedRegisters() const
Property: Callee-saved registers.
Describes (part of) a physical CPU register.
Parameter is at a fixed memory address.
void appendInputParameter(RegisterDescriptor reg)
Append input parameter.
ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits=0)
Convert a virtual address to a string.
void nonParameterStackSize(size_t nBytes)
Property: Size of non-parameter stack area.
Abstract parameter location.
StackParameterOrder
The order that arguments are pushed onto the stack.
bool isValid() const
Predicate to determine if location is valid.
bool didConverge() const
Whether the analysis results are valid.
Information about calling conventions.
std::set< RegisterDescriptor > & scratchRegisters()
Property: Scratch registers.
Sawyer::Message::Facility mlog
Facility for diagnostic output.
Prints a register name even when no dictionary is available or when the dictionary doesn't contain an...
void clearOutputParameters()
Erase output parameters.
void appendInputParameter(RegisterDescriptor reg, int64_t offset)
Append input parameter.
void analyzeFunction(const Partitioner2::Partitioner &, const Sawyer::SharedPointer< Partitioner2::Function > &)
Analyze one function.
void print(std::ostream &, const RegisterDictionary *regDict=NULL) const
Print detailed information about this calling convention.
size_t nonParameterStackSize() const
Property: Size of non-parameter stack area.
Analysis(const InstructionSemantics2::BaseSemantics::DispatcherPtr &cpu)
Construct an analysis using a specified dispatcher.
static Ptr x86_stdcall(const RegisterDictionary *)
Constructs a new pre-defined calling convention based on a register dictionary.
RegisterParts outputRegisterParts() const
Computes the set of output registers.
bool match(const Definition::Ptr &) const
Determine whether a definition matches.
const RegisterDescriptor stackPointerRegister() const
Property: Register for implied stack parameters.
RegisterParts getUsedRegisterParts() const
Returns all registers mentioned in this definition.
const RegisterParts & outputRegisters() const
Output registers.
static Ptr x86_64bit_cdecl()
Allocating constructor.
void name(const std::string &s)
Property: Short name of calling convention.
Stack parameters pushed left to right (Pascal order).
ParameterLocation(rose_addr_t va)
Constructs a parameter at a fixed memory address.
StackCleanup
Who is responsible for popping stack parameters.
Parameter is in a register.
const RegisterDictionary * registerDictionary() const
Property: Register dictionary.
int64_t offset() const
Offset part of location.
Base class for reference counted objects.
void clearNonResults()
Clears everything but results.
void initDiagnostics()
Initialize diagnostics.
void clearResults()
Clear analysis results.
const std::vector< ParameterLocation > & inputParameters() const
Property: Enumerated input parameters.
Definition()
Default constructor.
Stack parameters pushed right to left (C order).
void clearInputParameters()
Erase enumerated input parameters.
void appendOutputParameter(RegisterDescriptor reg)
Append output parameter.
const std::string & name() const
Property: Short name of calling convention.
void stackAlignment(size_t nBytes)
Property: Stack alignment.
Stack parameter cleanup is unknown or unspecified.
Type type() const
Type of parameter location.
const Dictionary & dictionaryAarch32()
Common calling conventions for ARM AArch32.
Definition(size_t wordWidth, const std::string &name, const std::string &comment, const RegisterDictionary *regDict)
Allocating constructor.
void clearParameters()
Erase all parameters.
Defines registers available for a particular architecture.
Stack parameter order is unknown or unspecified.
RegisterParts calleeSavedRegisterParts() const
Compute the set of callee-saved registers.
void defaultCallingConvention(const Definition::Ptr &x)
Property: Default calling convention.
const std::string & comment() const
Property: Full name of calling convention.
const std::set< RegisterDescriptor > & scratchRegisters() const
Property: Scratch registers.
static Ptr x86_32bit_fastcall()
Allocating constructor.
Partitions instructions into basic blocks and functions.
A push increments the stack pointer.
static Ptr x86_64bit_sysv()
Allocating constructor.
Virtual base class for instruction disassemblers.
static Ptr x86_64bit_stdcall()
Allocating constructor.
std::set< RegisterDescriptor > & calleeSavedRegisters()
Property: Callee-saved registers.
static Ptr ppc_ibm(const RegisterDictionary *)
Constructs a new pre-defined calling convention based on a register dictionary.
void stackPointerRegister(RegisterDescriptor r)
Property: Register for implied stack parameters.
void appendOutputParameter(RegisterDescriptor reg, int64_t offset)
Append output parameter.