00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _PROG_H_
00022 #define _PROG_H_
00023
00024 #include <map>
00025 #include "BinaryFile.h"
00026 #include "frontend.h"
00027 #include "type.h"
00028 #include "cluster.h"
00029
00030 class RTLInstDict;
00031 class Proc;
00032 class UserProc;
00033 class LibProc;
00034 class Signature;
00035 class Statement;
00036 class StatementSet;
00037 class Cluster;
00038 class XMLProgParser;
00039
00040 typedef std::map<ADDRESS, Proc*, std::less<ADDRESS> > PROGMAP;
00041
00042 class Global {
00043 private:
00044 Type *type;
00045 ADDRESS uaddr;
00046 std::string nam;
00047
00048 public:
00049 Global(Type *type, ADDRESS uaddr, const char *nam) : type(type), uaddr(uaddr), nam(nam) { }
00050 virtual ~Global();
00051
00052 Type *getType() { return type; }
00053 void setType(Type* ty) { type = ty; }
00054 void meetType(Type* ty);
00055 ADDRESS getAddress() { return uaddr; }
00056 const char *getName() { return nam.c_str(); }
00057 Exp* getInitialValue(Prog* prog);
00058
00059 void print(std::ostream& os, Prog* prog);
00060
00061 protected:
00062 Global() : type(NULL), uaddr(0), nam("") { }
00063 friend class XMLProgParser;
00064 };
00065
00066 class Prog {
00067 public:
00068 Prog();
00069 virtual ~Prog();
00070 Prog(const char* name);
00071 void setFrontEnd(FrontEnd* fe);
00072 void setName(const char *name);
00073 Proc* setNewProc(ADDRESS uNative);
00074
00075 Proc* newProc(const char* name, ADDRESS uNative, bool bLib = false);
00076 void remProc(UserProc* proc);
00077 void removeProc(const char *name);
00078 char* getName();
00079 const char *getPath() { return m_path.c_str(); }
00080 const char *getPathAndName() {return (m_path+m_name).c_str(); }
00081 int getNumProcs();
00082 int getNumUserProcs();
00083 Proc* getProc(int i) const;
00084
00085 Proc* findProc(ADDRESS uAddr) const;
00086
00087 Proc* findProc(const char *name) const;
00088
00089 Proc* findContainingProc(ADDRESS uAddr) const;
00090 bool isProcLabel (ADDRESS addr);
00091
00092 bool createDotFile(const char*, bool bMainOnly = false) const;
00093
00094 std::string getNameNoPath() const;
00095 std::string getNameNoPathNoExt() const;
00096
00097
00098 Proc* getFirstProc(PROGMAP::const_iterator& it);
00099 Proc* getNextProc(PROGMAP::const_iterator& it);
00100
00101
00102
00103 UserProc* getFirstUserProc(std::list<Proc*>::iterator& it);
00104 UserProc* getNextUserProc (std::list<Proc*>::iterator& it);
00105
00106
00107 std::list<UserProc*> entryProcs;
00108
00109
00110 void clear();
00111
00112
00113
00114 const void* getCodeInfo(ADDRESS uAddr, const char*& last, int& delta);
00115
00116 const char *getRegName(int idx) { return pFE->getRegName(idx); }
00117 int getRegSize(int idx) { return pFE->getRegSize(idx); }
00118
00119 void decodeEntryPoint(ADDRESS a);
00120 void setEntryPoint(ADDRESS a);
00121 void decodeEverythingUndecoded();
00122 void decodeFragment(UserProc* proc, ADDRESS a);
00123
00124
00125 void reDecode(UserProc* proc);
00126
00127
00128 bool wellForm();
00129
00130
00131 void finishDecode();
00132
00133
00134 void recoverReturnLocs();
00135
00136
00137 void removeInterprocEdges();
00138
00139
00140 void decompile();
00141
00142
00143
00144 void decompileProcs();
00145
00146
00147 void removeNullStmts();
00148 void removeUnusedStmts();
00149 void removeUnusedGlobals();
00150 void removeUnusedLocals();
00151 void removeRestoreStmts(StatementSet& rs);
00152
00153
00154 void processConstants();
00155
00156
00157 void globalTypeAnalysis();
00158
00159
00160
00161 bool removeUnusedReturns();
00162
00163
00164 void fromSSAform();
00165
00166
00167 void conTypeAnalysis();
00168 void dfaTypeAnalysis();
00169
00170
00171 void rangeAnalysis();
00172
00173
00174 void generateDotFile();
00175
00176
00177 void generateCode(std::ostream &os);
00178 void generateCode(Cluster *cluster = NULL, UserProc *proc = NULL, bool intermixRTL = false);
00179 void generateRTL(Cluster *cluster = NULL, UserProc *proc = NULL);
00180
00181
00182 void print(std::ostream &out);
00183
00184
00185 LibProc *getLibraryProc(const char *nam);
00186
00187
00188 Signature *getLibSignature(const char *name);
00189 void rereadLibSignatures();
00190
00191 Statement *getStmtAtLex(Cluster *cluster, unsigned int begin, unsigned int end);
00192
00193
00194 platform getFrontEndId();
00195
00196 std::map<ADDRESS, std::string> &getSymbols();
00197
00198 Signature *getDefaultSignature(const char *name);
00199
00200 std::vector<Exp*> &getDefaultParams();
00201 std::vector<Exp*> &getDefaultReturns();
00202
00203
00204 bool isWin32();
00205
00206
00207 const char *getGlobalName(ADDRESS uaddr);
00208 ADDRESS getGlobalAddr(char *nam);
00209 Global* getGlobal(char *nam);
00210
00211
00212 const char *newGlobalName(ADDRESS uaddr);
00213
00214
00215 Type *guessGlobalType(const char *nam, ADDRESS u);
00216
00217
00218 ArrayType* makeArrayType(ADDRESS u, Type* t);
00219
00220
00221 bool globalUsed(ADDRESS uaddr, Type* knownType = NULL);
00222
00223
00224 Type *getGlobalType(char* nam);
00225
00226
00227 void setGlobalType(const char* name, Type* ty);
00228
00229
00230 void dumpGlobals();
00231
00232
00233 char *getStringConstant(ADDRESS uaddr, bool knownString = false);
00234 double getFloatConstant(ADDRESS uaddr, bool &ok, int bits = 64);
00235
00236
00237 MACHINE getMachine()
00238 { return pBF->GetMachine();}
00239 const char* symbolByAddress(ADDRESS dest)
00240 { return pBF->SymbolByAddress(dest);}
00241 PSectionInfo getSectionInfoByAddr(ADDRESS a)
00242 { return pBF->GetSectionInfoByAddr(a);}
00243 ADDRESS getLimitTextLow() {return pBF->getLimitTextLow();}
00244 ADDRESS getLimitTextHigh() {return pBF->getLimitTextHigh();}
00245 bool isReadOnly(ADDRESS a) { return pBF->isReadOnly(a); }
00246
00247 int readNative1(ADDRESS a) {return pBF->readNative1(a);}
00248 int readNative2(ADDRESS a) {return pBF->readNative2(a);}
00249 int readNative4(ADDRESS a) {return pBF->readNative4(a);}
00250 float readNativeFloat4(ADDRESS a) {return pBF->readNativeFloat4(a);}
00251 double readNativeFloat8(ADDRESS a) {return pBF->readNativeFloat8(a);}
00252 QWord readNative8(ADDRESS a) {return pBF->readNative8(a);}
00253 Exp *readNativeAs(ADDRESS uaddr, Type *type);
00254 int getTextDelta() { return pBF->getTextDelta(); }
00255
00256 bool isDynamicLinkedProcPointer(ADDRESS dest) { return pBF->IsDynamicLinkedProcPointer(dest); }
00257 const char* GetDynamicProcName(ADDRESS uNative) { return pBF->GetDynamicProcName(uNative); }
00258
00259 bool processProc(int addr, UserProc* proc)
00260 { std::ofstream os; return pFE->processProc((unsigned)addr, proc, os);}
00261
00262 void readSymbolFile(const char *fname);
00263 unsigned getImageSize() { return pBF->getImageSize(); }
00264 ADDRESS getImageBase() { return pBF->getImageBase(); }
00265
00266
00267
00268 bool bRegisterJump;
00269 bool bRegisterCall;
00270
00271 void printSymbolsToFile();
00272 void printCallGraph();
00273 void printCallGraphXML();
00274
00275 Cluster *getRootCluster() { return m_rootCluster; }
00276 Cluster *findCluster(const char *name) { return m_rootCluster->find(name); }
00277 Cluster *getDefaultCluster(const char *name);
00278 bool clusterUsed(Cluster *c);
00279
00280
00281 void addDecodedRtl(ADDRESS a, RTL* rtl) {
00282 pFE->addDecodedRtl(a, rtl); }
00283
00284
00285
00286 Exp *addReloc(Exp *e, ADDRESS lc);
00287
00288 protected:
00289 BinaryFile* pBF;
00290 FrontEnd *pFE;
00291
00292
00293 std::string m_name, m_path;
00294 std::list<Proc*> m_procs;
00295 PROGMAP m_procLabels;
00296
00297 std::set<Global*> globals;
00298
00299 DataIntervalMap globalMap;
00300 int m_iNumberedProc;
00301 Cluster *m_rootCluster;
00302
00303 friend class XMLProgParser;
00304 };
00305
00306 #endif