diff --git a/include/interfaces/irdb/STARSFunction.h b/include/interfaces/irdb/STARSFunction.h index 37a22155f0dd154491633d941eef35ad47935713..ebacbcdb3489b3c6b91390bbf5c8d643e28c3f10 100644 --- a/include/interfaces/irdb/STARSFunction.h +++ b/include/interfaces/irdb/STARSFunction.h @@ -2,6 +2,7 @@ #define STARS_IRDB_Function_h #include "interfaces/STARSTypes.h" +#include <libIRDB-core.hpp> class SMPFunction; @@ -9,45 +10,54 @@ class STARS_IRDB_Function_t : public STARS_Function_t { public: + STARS_IRDB_Function_t(libIRDB::Function_t* f): the_func(f) + { + } + // obvious - virtual char* GetFunctionName(const char* name, const int len) const =0; + virtual char* GetFunctionName(const char* name, const int len) const { assert(0); } // get entry point of function - virtual STARS_ea_t get_startEA()=0; + virtual STARS_ea_t get_startEA(){ return the_func->GetEntryPoint()->GetBaseID(); } // clc needs to review and get back to me. - virtual STARS_ea_t get_endEA()=0; + virtual STARS_ea_t get_endEA(){ assert(0); } // init class-local variable with IRDB value // and these are accessors to that. - virtual std::size_t GetFrameSize()=0; - virtual void SetFrameSize(std::size_t size) = 0; + virtual std::size_t GetFrameSize(){ return the_func -> GetStackFrameSize(); } + virtual void SetFrameSize(std::size_t size) { assert(0); } // todo for clc: audit these functions and see if they are really getting info from idapro // or if these can be removed from the interface because they are calculated by STARS // interface-independent routines. - virtual std::size_t GetSavedRegSize() = 0; - virtual std::size_t GetIncomingArgumentSize() = 0; - virtual std::size_t GetFrameReturnAddressSize() = 0; - - - - virtual bool FunctionUsesFP()=0; - virtual bool IsStaticFunction()=0; - virtual bool IsLibraryFunction()=0; - virtual bool IsStackPointerAnalyzed()=0; - virtual STARS_sval_t get_spd(STARS_ea_t ea)=0; - virtual bool HasReturnPoints()=0; - virtual bool IsMultiEntry()=0; - virtual void MarkSharedChunks()=0; - virtual bool HasSharedChunks() const =0; - virtual void SetSharedChunks(bool v) =0; - virtual void UpdateXrefs()=0; - virtual void BuildFuncIR(SMPFunction *func)=0; - virtual bool FindDistantCodeFragment(SMPFunction* func, STARS_ea_t TargetAddr)=0; - virtual void FillInLocalVarTable(SMPFunction *CurrFunc) = 0; // get stack frame fine-grained info - virtual bool AnalyzeInstAsCallTarget(SMPFunction *CurrFunc, bool &IsIndirectCallTarget, bool &IsTailCallTarget) = 0; // return success or failure of analysis - virtual bool IsChunkUnshared(STARS_ea_t ChunkAddr, STARS_ea_t FuncHeadStart, STARS_ea_t FuncHeadEnd) = 0; + virtual std::size_t GetSavedRegSize() { assert(0); } + virtual std::size_t GetIncomingArgumentSize() { assert(0); } + virtual std::size_t GetFrameReturnAddressSize() { assert(0); } + + + + virtual bool FunctionUsesFP(){ return the_func->GetUseFramePointer(); } + virtual bool IsStaticFunction(){ return false; } + virtual bool IsLibraryFunction(){ return false; } + + + virtual bool IsStackPointerAnalyzed(){ assert(0); } + virtual STARS_sval_t get_spd(STARS_ea_t ea){ assert(0); } + virtual bool HasReturnPoints(){ assert(0); } + virtual bool IsMultiEntry(){ assert(0); } + virtual void MarkSharedChunks(){ assert(0); } + virtual bool HasSharedChunks() const { assert(0); } + virtual void SetSharedChunks(bool v) { assert(0); } + virtual void UpdateXrefs(){ assert(0); } + virtual void BuildFuncIR(SMPFunction *func){ assert(0); } + virtual bool FindDistantCodeFragment(SMPFunction* func, STARS_ea_t TargetAddr){ assert(0); } + virtual void FillInLocalVarTable(SMPFunction *CurrFunc) { assert(0); } + virtual bool AnalyzeInstAsCallTarget(SMPFunction *CurrFunc, bool &IsIndirectCallTarget, bool &IsTailCallTarget) { assert(0); } + virtual bool IsChunkUnshared(STARS_ea_t ChunkAddr, STARS_ea_t FuncHeadStart, STARS_ea_t FuncHeadEnd) { assert(0); } + +private: + libIRDB::Function_t* the_func; }; diff --git a/include/interfaces/irdb/STARSInterface.h b/include/interfaces/irdb/STARSInterface.h index a7d71ae130aa15a240d76acada000bcc25ea4fc9..3f095916dd5a4d0d987947c2e663298b33f633e4 100644 --- a/include/interfaces/irdb/STARSInterface.h +++ b/include/interfaces/irdb/STARSInterface.h @@ -4,6 +4,7 @@ #include "interfaces/abstract/STARSInterface.h" #include "interfaces/irdb/STARSSegment.h" +#include "interfaces/irdb/STARSFunction.h" #include <elfio/elfio.hpp> #include <elfio/elfio_dump.hpp> @@ -22,30 +23,80 @@ public: firp(p_firp), pqxx_interface(p_pqxx_interface) { InitSegments(); + InitFunctions(); + } + + ~STARS_IRDB_Interface_t() + { + for(int j=0;j<segments.size();j++) + { + delete segments[j]; + segments[j]=0; + } + for(int j=0;j<functions.size();j++) + { + delete functions[j]; + functions[j]=0; + } + delete elfiop; } // Segment accessors - virtual STARS_Segment_t *getseg(const STARS_ea_t &addr) { /* iterate through sections */ assert(0); return NULL; } + + // get the segment that contains the address. + virtual STARS_Segment_t *getseg(const STARS_ea_t &addr) + { + for(int i=0;i<segments.size();++i) + { + if(segments[i]->get_startEA() <= addr && addr < segments[i]->get_endEA()) + return segments[i]; + } + } + // get the Nth segment virtual STARS_Segment_t *getnseg(const int &index) { return segments[index]; } + + // get the number of segments. virtual std::size_t get_segm_qty() { return segments.size(); } - virtual STARS_Segment_t *get_next_seg(const STARS_ea_t &addr) { /* iterate-solution */assert(0); return NULL; } + + // get the next segment after addr. + virtual STARS_Segment_t *get_next_seg(const STARS_ea_t &addr) + { + if(addr < segments[0]->get_startEA()) + return segments[0]; + for(int i=0;i<segments.size();++i) + { + // found seg? + if(segments[i]->get_startEA() <= addr && addr < segments[i]->get_endEA()) + { + if(i+1 <= segments.size()) + // return next seg. + return segments[i+1]; + return NULL; + } + } + return NULL; + } // Function accessors // find out how many functions there are - virtual std::size_t get_func_qty() { return firp->GetFunctions().size(); } + virtual std::size_t get_func_qty() + { return functions.size(); } // get the index-th function virtual STARS_Function_t *getn_func(int index) - { /* easy */assert(0); return 0; }; + { return functions[index]; } // get the function that has the instruction 'instruction_id' as the entry point. virtual STARS_Function_t *get_func(STARS_ea_t instruction_id) - { / assert(0); return NULL; }; + { return instr_id_to_func_map[instruction_id]; }; virtual void get_func_name(const STARS_ea_t &ea, char* name, const std::size_t &len) - { /* easy */assert(0); }; + { + STARS_Function_t * f=this->get_func(ea); + assert(f); + } // Instruction creation. virtual STARS_Instruction_t *CreateInst(STARS_InstructionID_t InstID) @@ -85,7 +136,7 @@ public: /* remove comments, etc. from disassembly.*/ // do strcpy from instr to buf if(instr != buf) - strcpy(buf,instr, bufsize); + strncpy(buf,instr, bufsize); return 0; // success } virtual bool STARS_generate_disasm_line(STARS_ea_t addr, char *buf, std::size_t bufsize, int flags = 0) @@ -147,17 +198,42 @@ private: ELFIO::dump::section_headers(cout,*elfiop); segments.resize(elfiop->sections.size()); - for(int i=0;i<elfiop->sections.size(); i++) + for(int i=0,j=0;i<elfiop->sections.size(); i++) { - segments[i]=new STARS_IRDB_Segment_t(elfiop->sections[i],i); + if((elfiop->sections[i]->get_flags() & SHF_ALLOC) == SHF_ALLOC) + { + cout<<"Found 'segment'["<<dec<<i<<"] named "<<elfiop->sections[i]->get_name(); + cout<<" start: "<<hex<<elfiop->sections[i]->get_address()<<endl; + segments[j]=new STARS_IRDB_Segment_t(elfiop->sections[i],j); + j++; + } } } - vector<STARS_Segment_t*> segments; -// unique_ptr<ELFIO::elfio> elfiop; + void InitFunctions() + { + functions.resize(firp->GetFunctions().size()); + int i=0; + for(libIRDB::FunctionSet_t::iterator it=firp->GetFunctions().begin(); + it!=firp->GetFunctions().end(); + ++it) + { + libIRDB::Function_t* f=*it; + if(!f->GetEntryPoint()) + continue; + STARS_IRDB_Function_t* sf=new STARS_IRDB_Function_t(f); + functions[i++]=sf; + instr_id_to_func_map[f->GetEntryPoint()->GetBaseID()]=sf; + } + } + + std::vector<STARS_IRDB_Segment_t*> segments; + std::map<STARS_ea_t, STARS_IRDB_Function_t*> instr_id_to_func_map; + std::vector<STARS_IRDB_Function_t*> functions; ELFIO::elfio* elfiop; + libIRDB::FileIR_t *firp; diff --git a/include/interfaces/irdb/STARSProgram.h b/include/interfaces/irdb/STARSProgram.h index 40f05a3eb339d399a941f08e1d8687013d848d94..a34b35658a6214f6cdd6b0d5d4231b502ac171db 100644 --- a/include/interfaces/irdb/STARSProgram.h +++ b/include/interfaces/irdb/STARSProgram.h @@ -17,7 +17,6 @@ class STARS_IRDB_Program_t : public STARS_Program_t { public: STARS_IRDB_Program_t(libIRDB::FileIR_t* p_firp) : m_firp(p_firp) {} // Data initialization -// virtual void InitData(void); void DetermineRootFileName(void); bool OpenFiles(void); void CloseFiles(void); diff --git a/include/interfaces/irdb/STARSSegment.h b/include/interfaces/irdb/STARSSegment.h index 3d3ff0400534d205e5ca8b5ed12c823974c0eaf6..c020f41c662af6190e59a07c447b5603ac391111 100644 --- a/include/interfaces/irdb/STARSSegment.h +++ b/include/interfaces/irdb/STARSSegment.h @@ -14,22 +14,46 @@ class STARS_IRDB_Segment_t : public STARS_Segment_t { public: + // create STARS_IRDB_Segment_t(ELFIO::section* seg, int p_segno) : the_seg(seg), segno(p_segno) {} + + // get starting address virtual STARS_ea_t get_startEA() - { return the_seg->get_virtual_address(); } + { return the_seg->get_address(); } + + // get ending address virtual STARS_ea_t get_endEA() - { return the_seg->get_virtual_address()+the_seg->get_size(); } - virtual bool IsCodeSegment() { assert(0); /* perms include x */ } - virtual bool IsXternSegment() { assert(0); /* return false" */} - virtual bool IsDataSegment() { assert(0); /* rw */} - virtual bool IsCommonSegment() { assert(0); /* leave as error */} - virtual bool IsBSSSegment() { assert(0); /* NOBITS */ } - virtual bool IsReadableSegment() { assert(0); } - virtual bool IsWriteableSegment() { assert(0); } - virtual STARS_ssize_t GetSegmentName(char* name, int len) { assert(0); } + { return the_seg->get_address()+the_seg->get_size(); } + + // return if the EXE bit is set. + virtual bool IsCodeSegment() + { return (the_seg->get_flags() & SHF_EXECINSTR) == SHF_EXECINSTR; } + + virtual bool IsDataSegment() + { return IsReadableSegment() && IsWriteableSegment(); } + + virtual bool IsBSSSegment() + { return (the_seg->get_type() & SHT_NOBITS) == SHT_NOBITS; } + + virtual bool IsReadableSegment() + { return (the_seg->get_flags() & SHF_ALLOC) == SHF_ALLOC; } + + virtual bool IsWriteableSegment() + { return (the_seg->get_flags() & SHF_WRITE) == SHF_WRITE; } + + virtual STARS_ssize_t GetSegmentName(char* name, int len) + { + strncpy(name,the_seg->get_name().c_str(), len); + return the_seg->get_name().length(); + } + virtual int GetSegmentNumber() { return segno; } + // these aren't used, impl. later if we know what it means. + virtual bool IsXternSegment() { assert(0); /* leave as error */} + virtual bool IsCommonSegment() { assert(0); /* leave as error */} + private: ELFIO::section* the_seg; int segno;