diff options
Diffstat (limited to 'binaryninjaapi.h')
| -rw-r--r-- | binaryninjaapi.h | 274 |
1 files changed, 200 insertions, 74 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index a3db346a..93fa9eda 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -19,7 +19,6 @@ // IN THE SOFTWARE. #pragma once - #ifdef WIN32 #define NOMINMAX #include <windows.h> @@ -34,6 +33,7 @@ #include <set> #include <mutex> #include <memory> +#include <cstdint> #include "binaryninjacore.h" #include "json/json.h" @@ -644,53 +644,6 @@ namespace BinaryNinja std::string GetUniqueIdentifierString(); - class QualifiedName - { - std::vector<std::string> m_name; - - public: - QualifiedName(); - QualifiedName(const std::string& name); - QualifiedName(const std::vector<std::string>& name); - QualifiedName(const QualifiedName& name); - - QualifiedName& operator=(const std::string& name); - QualifiedName& operator=(const std::vector<std::string>& name); - QualifiedName& operator=(const QualifiedName& name); - - bool operator==(const QualifiedName& other) const; - bool operator!=(const QualifiedName& other) const; - bool operator<(const QualifiedName& other) const; - - QualifiedName operator+(const QualifiedName& other) const; - - std::string& operator[](size_t i); - const std::string& operator[](size_t i) const; - std::vector<std::string>::iterator begin(); - std::vector<std::string>::iterator end(); - std::vector<std::string>::const_iterator begin() const; - std::vector<std::string>::const_iterator end() const; - std::string& front(); - const std::string& front() const; - std::string& back(); - const std::string& back() const; - void insert(std::vector<std::string>::iterator loc, const std::string& name); - void insert(std::vector<std::string>::iterator loc, std::vector<std::string>::iterator b, - std::vector<std::string>::iterator e); - void erase(std::vector<std::string>::iterator i); - void clear(); - void push_back(const std::string& name); - // Returns count of names - size_t size() const; - // Returns size of output string - size_t StringSize() const; - std::string GetString() const; - - BNQualifiedName GetAPIObject() const; - static void FreeAPIObject(BNQualifiedName* name); - static QualifiedName FromAPIObject(BNQualifiedName* name); - }; - class DataBuffer { BNDataBuffer* m_buffer; @@ -856,7 +809,6 @@ namespace BinaryNinja BinaryNinja::Ref<BinaryNinja::BinaryView> GetViewOfType(const std::string& name); }; - class BinaryView; class Function; struct DataVariable; @@ -938,12 +890,10 @@ namespace BinaryNinja class Function; class BasicBlock; - class Symbol: public CoreRefCountObject<BNSymbol, BNNewSymbolReference, BNFreeSymbol> { public: - Symbol(BNSymbolType type, const std::string& shortName, const std::string& fullName, - const std::string& rawName, uint64_t addr); + Symbol(BNSymbolType type, const std::string& shortName, const std::string& fullName, const std::string& rawName, uint64_t addr); Symbol(BNSymbolType type, const std::string& name, uint64_t addr); Symbol(BNSymbol* sym); @@ -953,7 +903,6 @@ namespace BinaryNinja std::string GetRawName() const; uint64_t GetAddress() const; bool IsAutoDefined() const; - void SetAutoDefined(bool val); static Ref<Symbol> ImportedFunctionFromImportAddressSymbol(Symbol* sym, uint64_t addr); }; @@ -1054,28 +1003,56 @@ namespace BinaryNinja bool autoDiscovered; }; - struct Segment + class Relocation; + class Segment: public CoreRefCountObject<BNSegment, BNNewSegmentReference, BNFreeSegment> { - uint64_t start, length; - uint64_t dataOffset, dataLength; - uint32_t flags; - bool autoDefined; + public: + Segment(BNSegment* seg); + uint64_t GetStart() const; + uint64_t GetLength() const; + uint64_t GetEnd() const; + uint64_t GetDataEnd() const; + uint64_t GetDataOffset() const; + uint64_t GetDataLength() const; + uint32_t GetFlags() const; + bool IsAutoDefined() const; + + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRanges() const; + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesAtAddress(uint64_t addr) const; + std::vector<Ref<Relocation>> GetRelocationsInRange(uint64_t addr, uint64_t size) const; + uint64_t GetRelocationsCount() const; + + void SetStart(uint64_t newSegmentBase); + void SetLength(uint64_t length); + void SetDataOffset(uint64_t dataOffset); + void SetDataLength(uint64_t dataLength); + void SetFlags(uint64_t flags); + + size_t Read(BinaryView* view, uint8_t* dest, uint64_t offset, size_t len); }; - struct Section + class Section: public CoreRefCountObject<BNSection, BNNewSectionReference, BNFreeSection> { - std::string name, type; - uint64_t start, length; - std::string linkedSection, infoSection; - uint64_t infoData; - uint64_t align, entrySize; - BNSectionSemantics semantics; - bool autoDefined; + public: + Section(BNSection* sec); + Section(const std::string& name, uint64_t start, uint64_t length, BNSectionSemantics semantics, + const std::string& type, uint64_t align, uint64_t entrySize, const std::string& linkedSection, + const std::string& infoSection, uint64_t infoData, bool autoDefined); + std::string GetName() const; + std::string GetType() const; + uint64_t GetStart() const; + uint64_t GetLength() const; + uint64_t GetInfoData() const; + uint64_t GetAlignment() const; + uint64_t GetEntrySize() const; + std::string GetLinkedSection() const; + std::string GetInfoSection() const; + BNSectionSemantics GetSemantics() const; + bool AutoDefined() const; }; struct QualifiedNameAndType; class Metadata; - class QueryMetadataException: public std::exception { const std::string m_error; @@ -1126,7 +1103,8 @@ namespace BinaryNinja virtual size_t PerformGetAddressSize() const; virtual bool PerformSave(FileAccessor* file); - + void PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc); + void PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> sym, uint64_t reloc); void NotifyDataWritten(uint64_t offset, size_t len); void NotifyDataInserted(uint64_t offset, size_t len); void NotifyDataRemoved(uint64_t offset, uint64_t len); @@ -1153,7 +1131,8 @@ namespace BinaryNinja static bool IsRelocatableCallback(void* ctxt); static size_t GetAddressSizeCallback(void* ctxt); static bool SaveCallback(void* ctxt, BNFileAccessor* file); - + static void DefineRelocationCallback(void* ctxt, BNArchitecture* arch, BNRelocationInfo* info, uint64_t target, uint64_t reloc); + static void DefineSymbolRelocationCallback(void* ctxt, BNArchitecture* arch, BNRelocationInfo* info, BNSymbol* target, uint64_t reloc); public: BinaryView(BNBinaryView* view); @@ -1204,6 +1183,7 @@ namespace BinaryNinja bool IsOffsetBackedByFile(uint64_t offset) const; bool IsOffsetCodeSemantics(uint64_t offset) const; bool IsOffsetWritableSemantics(uint64_t offset) const; + bool IsOffsetExternSemantics(uint64_t offset) const; uint64_t GetNextValidOffset(uint64_t offset) const; uint64_t GetStart() const; @@ -1225,6 +1205,11 @@ namespace BinaryNinja bool Save(FileAccessor* file); bool Save(const std::string& path); + void DefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc); + void DefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> target, uint64_t reloc); + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRanges() const; + std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesAtAddress(uint64_t addr) const; + void RegisterNotification(BinaryDataNotification* notify); void UnregisterNotification(BinaryDataNotification* notify); @@ -1344,8 +1329,8 @@ namespace BinaryNinja void RemoveAutoSegment(uint64_t start, uint64_t length); void AddUserSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags); void RemoveUserSegment(uint64_t start, uint64_t length); - std::vector<Segment> GetSegments(); - bool GetSegmentAt(uint64_t addr, Segment& result); + std::vector<Ref<Segment>> GetSegments(); + Ref<Segment> GetSegmentAt(uint64_t addr); bool GetAddressForDataOffset(uint64_t offset, uint64_t& addr); void AddAutoSection(const std::string& name, uint64_t start, uint64_t length, @@ -1358,9 +1343,9 @@ namespace BinaryNinja uint64_t align = 1, uint64_t entrySize = 0, const std::string& linkedSection = "", const std::string& infoSection = "", uint64_t infoData = 0); void RemoveUserSection(const std::string& name); - std::vector<Section> GetSections(); - std::vector<Section> GetSectionsAt(uint64_t addr); - bool GetSectionByName(const std::string& name, Section& result); + std::vector<Ref<Section>> GetSections(); + std::vector<Ref<Section>> GetSectionsAt(uint64_t addr); + Ref<Section> GetSectionByName(const std::string& name); std::vector<std::string> GetUniqueSectionNames(const std::vector<std::string>& names); @@ -1381,6 +1366,19 @@ namespace BinaryNinja void SetNewAutoFunctionAnalysisSuppressed(bool suppress); }; + + class Relocation: public CoreRefCountObject<BNRelocation, BNNewRelocationReference, BNFreeRelocation> + { + public: + Relocation(BNRelocation* reloc); + BNRelocationInfo GetInfo() const; + Architecture* GetArchitecture() const; + uint64_t GetTarget() const; + uint64_t GetAddress() const; + Ref<Symbol> GetSymbol() const; + }; + + class BinaryData: public BinaryView { public: @@ -1621,6 +1619,7 @@ namespace BinaryNinja class MediumLevelILFunction; class FunctionRecognizer; class CallingConvention; + class RelocationHandler; typedef size_t ExprId; @@ -1851,6 +1850,8 @@ namespace BinaryNinja virtual bool SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t len, uint64_t value); void RegisterFunctionRecognizer(FunctionRecognizer* recog); + void RegisterRelocationHandler(const std::string& viewName, RelocationHandler* handler); + Ref<RelocationHandler> GetRelocationHandler(const std::string& viewName); bool IsBinaryViewTypeConstantDefined(const std::string& type, const std::string& name); uint64_t GetBinaryViewTypeConstant(const std::string& type, const std::string& name, @@ -1892,6 +1893,7 @@ namespace BinaryNinja virtual std::string GetRegisterName(uint32_t reg) override; virtual std::string GetFlagName(uint32_t flag) override; virtual std::string GetFlagWriteTypeName(uint32_t flags) override; + virtual std::string GetSemanticFlagClassName(uint32_t semClass) override; virtual std::string GetSemanticFlagGroupName(uint32_t semGroup) override; virtual std::vector<uint32_t> GetFullWidthRegisters() override; @@ -2054,6 +2056,89 @@ namespace BinaryNinja Variable location; }; + class NameList + { + protected: + std::string m_join; + std::vector<std::string> m_name; + public: + NameList(const std::string& join); + NameList(const std::string& name, const std::string& join); + NameList(const std::vector<std::string>& name, const std::string& join); + NameList(const NameList& name, const std::string& join); + + virtual NameList& operator=(const std::string& name); + virtual NameList& operator=(const std::vector<std::string>& name); + virtual NameList& operator=(const NameList& name); + + virtual bool operator==(const NameList& other) const; + virtual bool operator!=(const NameList& other) const; + virtual bool operator<(const NameList& other) const; + + virtual NameList operator+(const NameList& other) const; + + virtual std::string& operator[](size_t i); + virtual const std::string& operator[](size_t i) const; + virtual std::vector<std::string>::iterator begin(); + virtual std::vector<std::string>::iterator end(); + virtual std::vector<std::string>::const_iterator begin() const; + virtual std::vector<std::string>::const_iterator end() const; + virtual std::string& front(); + virtual const std::string& front() const; + virtual std::string& back(); + virtual const std::string& back() const; + virtual void insert(std::vector<std::string>::iterator loc, const std::string& name); + virtual void insert(std::vector<std::string>::iterator loc, std::vector<std::string>::iterator b, + std::vector<std::string>::iterator e); + virtual void erase(std::vector<std::string>::iterator i); + virtual void clear(); + virtual void push_back(const std::string& name); + virtual size_t size() const; + virtual size_t StringSize() const; + + virtual std::string GetString() const; + + BNNameList GetAPIObject() const; + static void FreeAPIObject(BNNameList* name); + static NameList FromAPIObject(BNNameList* name); + }; + + class QualifiedName: public NameList + { + public: + QualifiedName(); + QualifiedName(const std::string& name); + QualifiedName(const std::vector<std::string>& name); + QualifiedName(const QualifiedName& name); + + virtual QualifiedName& operator=(const std::string& name); + virtual QualifiedName& operator=(const std::vector<std::string>& name); + virtual QualifiedName& operator=(const QualifiedName& name); + virtual QualifiedName operator+(const QualifiedName& other) const; + + BNQualifiedName GetAPIObject() const; + static void FreeAPIObject(BNQualifiedName* name); + static QualifiedName FromAPIObject(BNQualifiedName* name); + }; + + class NameSpace: public NameList + { + public: + NameSpace(); + NameSpace(const std::string& name); + NameSpace(const std::vector<std::string>& name); + NameSpace(const NameSpace& name); + + virtual NameSpace& operator=(const std::string& name); + virtual NameSpace& operator=(const std::vector<std::string>& name); + virtual NameSpace& operator=(const NameSpace& name); + virtual NameSpace operator+(const NameSpace& other) const; + + BNNameSpace GetAPIObject() const; + static void FreeAPIObject(BNNameSpace* name); + static NameSpace FromAPIObject(BNNameSpace* name); + }; + struct QualifiedNameAndType { QualifiedName name; @@ -2337,6 +2422,7 @@ namespace BinaryNinja { BNRegisterValueType state; int64_t value; + int64_t offset; RegisterValue(); static RegisterValue FromAPIObject(const BNRegisterValue& value); @@ -2347,6 +2433,7 @@ namespace BinaryNinja { BNRegisterValueType state; int64_t value; + int64_t offset; std::vector<BNValueRange> ranges; std::set<int64_t> valueSet; std::vector<LookupTableEntry> table; @@ -2736,6 +2823,7 @@ namespace BinaryNinja uint32_t reg, const ILSourceLocation& loc = ILSourceLocation()); ExprId Const(size_t size, uint64_t val, const ILSourceLocation& loc = ILSourceLocation()); ExprId ConstPointer(size_t size, uint64_t val, const ILSourceLocation& loc = ILSourceLocation()); + ExprId ExternPointer(size_t size, uint64_t val, uint64_t offset, const ILSourceLocation& loc = ILSourceLocation()); ExprId FloatConstRaw(size_t size, uint64_t val, const ILSourceLocation& loc = ILSourceLocation()); ExprId FloatConstSingle(float val, const ILSourceLocation& loc = ILSourceLocation()); ExprId FloatConstDouble(double val, const ILSourceLocation& loc = ILSourceLocation()); @@ -2983,6 +3071,8 @@ namespace BinaryNinja size_t GetMediumLevelILExprIndex(size_t expr) const; size_t GetMappedMediumLevelILInstructionIndex(size_t instr) const; size_t GetMappedMediumLevelILExprIndex(size_t expr) const; + + static bool IsConstantType(BNLowLevelILOperation type) { return type == LLIL_CONST || type == LLIL_CONST_PTR || type == LLIL_EXTERN_PTR; } }; struct MediumLevelILLabel: public BNMediumLevelILLabel @@ -3069,6 +3159,7 @@ namespace BinaryNinja const ILSourceLocation& loc = ILSourceLocation()); ExprId Const(size_t size, uint64_t val, const ILSourceLocation& loc = ILSourceLocation()); ExprId ConstPointer(size_t size, uint64_t val, const ILSourceLocation& loc = ILSourceLocation()); + ExprId ExternPointer(size_t size, uint64_t val, uint64_t offset, const ILSourceLocation& loc = ILSourceLocation()); ExprId FloatConstRaw(size_t size, uint64_t val, const ILSourceLocation& loc = ILSourceLocation()); ExprId FloatConstSingle(float val, const ILSourceLocation& loc = ILSourceLocation()); ExprId FloatConstDouble(double val, const ILSourceLocation& loc = ILSourceLocation()); @@ -3313,6 +3404,8 @@ namespace BinaryNinja Confidence<Ref<Type>> GetExprType(size_t expr); Confidence<Ref<Type>> GetExprType(const MediumLevelILInstruction& expr); + + static bool IsConstantType(BNMediumLevelILOperation op) { return op == MLIL_CONST || op == MLIL_CONST_PTR || op == MLIL_EXTERN_PTR; } }; class FunctionRecognizer @@ -3330,6 +3423,39 @@ namespace BinaryNinja virtual bool RecognizeMediumLevelIL(BinaryView* data, Function* func, MediumLevelILFunction* il); }; + class RelocationHandler: public CoreRefCountObject<BNRelocationHandler, + BNNewRelocationHandlerReference, BNFreeRelocationHandler> + { + static bool GetRelocationInfoCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, + BNRelocationInfo* result, size_t resultCount); + static bool ApplyRelocationCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, + uint8_t* dest, size_t len); + static size_t GetOperandForExternalRelocationCallback(void* ctxt, const uint8_t* data, uint64_t addr, + size_t length, BNLowLevelILFunction* il, BNRelocation* relocation); + protected: + RelocationHandler(); + RelocationHandler(BNRelocationHandler* handler); + static void FreeCallback(void* ctxt); + + public: + virtual bool GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result); + virtual bool ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest, + size_t len); + virtual size_t GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length, + Ref<LowLevelILFunction> il, Ref<Relocation> relocation); + }; + + class CoreRelocationHandler: public RelocationHandler + { + public: + CoreRelocationHandler(BNRelocationHandler* handler); + virtual bool GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result) override; + virtual bool ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest, + size_t len) override; + virtual size_t GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length, + Ref<LowLevelILFunction> il, Ref<Relocation> relocation) override; + }; + class UpdateException: public std::exception { const std::string m_desc; |
