diff --git a/libIRDB/include/core/file.hpp b/libIRDB/include/core/file.hpp index 575cad1e63455fcf732722cd85ddd6da11ab7790..f0b9d1e2329f9fef192687d32241f543c0a19af3 100644 --- a/libIRDB/include/core/file.hpp +++ b/libIRDB/include/core/file.hpp @@ -12,6 +12,7 @@ class File_t : public BaseObj_t std::string GetAddressTableName() { return address_table_name; } std::string GetFunctionTableName() { return function_table_name; } std::string GetInstructionTableName() { return instruction_table_name; } + std::string GetURL() { return url; } void CreateTables(); diff --git a/libIRDB/src/core/fileir.cpp b/libIRDB/src/core/fileir.cpp index 20a71f040bf9c2543c418146a41fdcd244772472..f4937af722a363c3345c41b354e12b2ed953bcbb 100644 --- a/libIRDB/src/core/fileir.cpp +++ b/libIRDB/src/core/fileir.cpp @@ -12,7 +12,7 @@ static map<Function_t*,db_id_t> entry_points; static void UpdateEntryPoints(std::map<db_id_t,Instruction_t*> &insnMap) { /* for each function, look up the instruction that's the entry point */ - for( static map<Function_t*,db_id_t>::const_iterator it=entry_points.begin(); + for( map<Function_t*,db_id_t>::const_iterator it=entry_points.begin(); it!=entry_points.end(); ++it ) @@ -22,6 +22,7 @@ static void UpdateEntryPoints(std::map<db_id_t,Instruction_t*> &insnMap) assert(insnMap[func_entry_id]); func->SetEntryPoint(insnMap[func_entry_id]); +// cout<<"Function named "<<func->GetName()<< " getting entry point set to "<<insnMap[func_entry_id]->GetComment()<<"."<<endl; } } diff --git a/libIRDB/src/core/variantid.cpp b/libIRDB/src/core/variantid.cpp index 9df7bf33e432a179102880986064a79de998ef98..a839ffb79c7b48421d95ddc3e2f480a53b769006 100644 --- a/libIRDB/src/core/variantid.cpp +++ b/libIRDB/src/core/variantid.cpp @@ -343,7 +343,18 @@ void VariantID_t::DropFromDB() File_t* VariantID_t::GetMainFile() const { - return *(files.begin()); + for( + set<File_t*>::iterator it=files.begin(); + it!=files.end(); + ++it + ) + { + const char* name=(*it)->GetURL().c_str(); + if(strstr(name,"a.ncexe")!=NULL) + return *it; + } + /* we should have found the main file somewhere. */ + assert(0); } diff --git a/libIRDB/test/fill_in_cfg.cpp b/libIRDB/test/fill_in_cfg.cpp index ac4b198e3e58318d80dc09c7f8cc8a0a436d6591..751f205c58415bbba094653ec37bf168786e34a3 100644 --- a/libIRDB/test/fill_in_cfg.cpp +++ b/libIRDB/test/fill_in_cfg.cpp @@ -28,7 +28,7 @@ pqxxDB_t pqxx_interface; void populate_instruction_map ( map< pair<db_id_t,virtual_offset_t>, Instruction_t*> &insnMap, - FileIR_t *virp + FileIR_t *firp ) { /* start from scratch each time */ @@ -37,8 +37,8 @@ void populate_instruction_map /* for each instruction in the IR */ for( - set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -57,7 +57,7 @@ void populate_instruction_map void set_fallthrough ( map< pair<db_id_t,virtual_offset_t>, Instruction_t*> &insnMap, - DISASM *disasm, Instruction_t *insn, FileIR_t *virp + DISASM *disasm, Instruction_t *insn, FileIR_t *firp ) { assert(disasm); @@ -105,7 +105,7 @@ void set_fallthrough void set_target ( map< pair<db_id_t,virtual_offset_t>, Instruction_t*> &insnMap, - DISASM *disasm, Instruction_t *insn, FileIR_t *virp + DISASM *disasm, Instruction_t *insn, FileIR_t *firp ) { @@ -173,10 +173,10 @@ void set_target } } -File_t* find_file(FileIR_t* virp, db_id_t fileid) +File_t* find_file(FileIR_t* firp, db_id_t fileid) { #if 0 - set<File_t*> &files=virp->GetFiles(); + set<File_t*> &files=firp->GetFiles(); for( set<File_t*>::iterator it=files.begin(); @@ -190,12 +190,12 @@ File_t* find_file(FileIR_t* virp, db_id_t fileid) } return NULL; #endif - assert(virp->GetFile()->GetBaseID()==fileid); - return virp->GetFile(); + assert(firp->GetFile()->GetBaseID()==fileid); + return firp->GetFile(); } -void add_new_instructions(FileIR_t *virp) +void add_new_instructions(FileIR_t *firp) { int found_instructions=0; for( @@ -211,7 +211,7 @@ void add_new_instructions(FileIR_t *virp) db_id_t missed_fileid=(*it).first; /* figure out which file we're looking at */ - File_t* filep=find_file(virp,missed_fileid); + File_t* filep=find_file(firp,missed_fileid); assert(filep); /* get the OID of the file */ @@ -323,8 +323,8 @@ void add_new_instructions(FileIR_t *virp) /* fallthrough/target/is indirect will be set later */ /* insert into the IR */ - virp->GetInstructions().insert(newinsn); - virp->GetAddresses().insert(newaddr); + firp->GetInstructions().insert(newinsn); + firp->GetAddresses().insert(newaddr); cout<<"Found new instruction, "<<newinsn->GetComment()<<", at "<<std::hex<<newinsn->GetAddress()->GetVirtualOffset()<<" in file "<<"<no name yet>"<<"."<<endl; @@ -345,7 +345,7 @@ void add_new_instructions(FileIR_t *virp) } -void fill_in_cfg(FileIR_t *virp) +void fill_in_cfg(FileIR_t *firp) { int round=0; @@ -357,14 +357,14 @@ void fill_in_cfg(FileIR_t *virp) missed_instructions.clear(); map< pair<db_id_t,virtual_offset_t>, Instruction_t*> insnMap; - populate_instruction_map(insnMap, virp); + populate_instruction_map(insnMap, firp); - cout << "Found "<<virp->GetInstructions().size()<<" instructions." <<endl; + cout << "Found "<<firp->GetInstructions().size()<<" instructions." <<endl; /* for each instruction, disassemble it and set the target/fallthrough */ for( - set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -380,8 +380,8 @@ void fill_in_cfg(FileIR_t *virp) assert(instr_len==insn->GetDataBits().size()); - set_fallthrough(insnMap, &disasm, insn, virp); - set_target(insnMap, &disasm, insn, virp); + set_fallthrough(insnMap, &disasm, insn, firp); + set_target(insnMap, &disasm, insn, firp); } if(bad_target_count>0) @@ -390,7 +390,7 @@ void fill_in_cfg(FileIR_t *virp) cout<<"Found "<<bad_fallthrough_count<<" bad fallthroughs at round "<<round<<endl; cout<<"Missed instruction count="<<missed_instructions.size()<<endl; - add_new_instructions(virp); + add_new_instructions(firp); round++; @@ -412,14 +412,14 @@ void fill_in_cfg(FileIR_t *virp) /* set the base IDs for all instructions */ - virp->SetBaseIDS(); + firp->SetBaseIDS(); /* for each instruction, set the original address id to be that of the address id, as fill_in_cfg is * designed to work on only original programs. */ for( - std::set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + std::set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -432,6 +432,7 @@ void fill_in_cfg(FileIR_t *virp) } + main(int argc, char* argv[]) { @@ -442,7 +443,7 @@ main(int argc, char* argv[]) } VariantID_t *pidp=NULL; - FileIR_t * virp=NULL; + FileIR_t * firp=NULL; try { @@ -455,13 +456,27 @@ main(int argc, char* argv[]) cout<<"New Variant, after reading registration, is: "<<*pidp << endl; - // read the db - virp=new FileIR_t(*pidp); + for(set<File_t*>::iterator it=pidp->GetFiles().begin(); + it!=pidp->GetFiles().end(); + ++it + ) + { + File_t* this_file=*it; + assert(this_file); + + cout<<"Filling in cfg for "<<this_file->GetURL()<<endl; + + // read the db + firp=new FileIR_t(*pidp, this_file); + fill_in_cfg(firp); + + // write the DB back and commit our changes + firp->WriteToDB(); + delete firp; + + } - fill_in_cfg(virp); - // write the DB back and commit our changes - virp->WriteToDB(); pqxx_interface.Commit(); } @@ -471,9 +486,8 @@ main(int argc, char* argv[]) exit(-1); } - assert(virp && pidp); + assert(firp && pidp); - delete virp; delete pidp; } diff --git a/libIRDB/test/fill_in_indtargs.cpp b/libIRDB/test/fill_in_indtargs.cpp index a92ea0f7bf593b46d9e184264b82d75529b66362..28ca0ad43041fef0da941e7aaf90bb7ddc686f37 100644 --- a/libIRDB/test/fill_in_indtargs.cpp +++ b/libIRDB/test/fill_in_indtargs.cpp @@ -27,6 +27,7 @@ set<int> targets; set< pair< int, int> > ranges; + void range(int start, int end) { pair<int,int> foo(start,end); @@ -50,11 +51,11 @@ bool is_in_range(int p) return false; } -void process_ranges(FileIR_t* virp) +void process_ranges(FileIR_t* firp) { for( - set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -102,11 +103,11 @@ void handle_argument(ARGTYPE *arg) possible_target(arg->Memory.Displacement); } -void mark_targets(FileIR_t *virp) +void mark_targets(FileIR_t *firp) { for( - set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -121,7 +122,7 @@ void mark_targets(FileIR_t *virp) newaddr->SetVirtualOffset(insn->GetAddress()->GetVirtualOffset()); insn->SetIndirectBranchTargetAddress(newaddr); - virp->GetAddresses().insert(newaddr); + firp->GetAddresses().insert(newaddr); } else insn->SetIndirectBranchTargetAddress(NULL); @@ -129,11 +130,11 @@ void mark_targets(FileIR_t *virp) } } -void get_instruction_targets(FileIR_t *virp) +void get_instruction_targets(FileIR_t *firp) { for( - set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -180,7 +181,7 @@ void get_instruction_targets(FileIR_t *virp) } -void get_executable_bounds(Elf32_Shdr *shdr, FILE* fp, FileIR_t *virp) +void get_executable_bounds(Elf32_Shdr *shdr, pqxx::largeobjectaccess &loa, FileIR_t *firp) { int flags = shdr->sh_flags; @@ -200,7 +201,7 @@ void get_executable_bounds(Elf32_Shdr *shdr, FILE* fp, FileIR_t *virp) } -void infer_targets(Elf32_Shdr *shdr, FILE* fp, FileIR_t *virp) +void infer_targets(Elf32_Shdr *shdr, pqxx::largeobjectaccess &loa, FileIR_t *firp) { int flags = shdr->sh_flags; @@ -218,10 +219,12 @@ void infer_targets(Elf32_Shdr *shdr, FILE* fp, FileIR_t *virp) char* data=(char*)malloc(shdr->sh_size); - fseek(fp,shdr->sh_offset, SEEK_SET); + //fseek(fp,shdr->sh_offset, SEEK_SET); + loa.seek(shdr->sh_offset, std::ios_base::beg); + - int res=fread(data, shdr->sh_size, 1, fp); - assert(res==1); + //int res=fread(data, shdr->sh_size, 1, fp); + loa.cread((char*)data, shdr->sh_size* 1); for(int i=0;i<=shdr->sh_size-sizeof(void*);i++) { @@ -271,12 +274,12 @@ void print_targets() * wmemcpy, wmemmove, wmemcmp, wmemchr, memset * */ -void add_num_handle_fn_watches(FileIR_t * virp) +void add_num_handle_fn_watches(FileIR_t * firp) { /* Loop over the set of functions */ for( - set<Function_t*>::const_iterator it=virp->GetFunctions().begin(); - it!=virp->GetFunctions().end(); + set<Function_t*>::const_iterator it=firp->GetFunctions().begin(); + it!=firp->GetFunctions().end(); ++it ) { @@ -322,44 +325,52 @@ void add_num_handle_fn_watches(FileIR_t * virp) } -void fill_in_indtargs(FileIR_t* virp, string elf_file, pqxxDB_t &pqxx_interface) +void fill_in_indtargs(FileIR_t* firp, pqxxDB_t &pqxx_interface) { + // reset global vars + bounds.clear(); + ranges.clear(); + targets.clear(); + Elf32_Off sec_hdr_off, sec_off; Elf32_Half secnum, strndx, secndx; Elf32_Word secsize; - FILE *fp; - fp = fopen(elf_file.c_str(),"rb"); + //fp = fopen(elf_file.c_str(),"rb"); + int elfoid=firp->GetFile()->GetELFOID(); + pqxx::largeobjectaccess loa(pqxx_interface.GetTransaction(), elfoid, PGSTD::ios::in); - if(!fp) - { - cerr<<"Cannot open "<<elf_file<<"."<<endl; - exit(-1); - } + + // if(!fp) + // { + // cerr<<"Cannot open "<<elf_file<<"."<<endl; + // exit(-1); + // } /* allcoate memory */ Elf32_Ehdr elfhdr; /* Read ELF header */ - int res=fread(&elfhdr, sizeof(Elf32_Ehdr), 1, fp); - assert(res==1); + //int res=fread(&elfhdr, sizeof(Elf32_Ehdr), 1, fp); + loa.cread((char*)&elfhdr, sizeof(Elf32_Ehdr)* 1); sec_hdr_off = elfhdr.e_shoff; secnum = elfhdr.e_shnum; strndx = elfhdr.e_shstrndx; /* Read Section headers */ Elf32_Shdr *sechdrs=(Elf32_Shdr*)malloc(sizeof(Elf32_Shdr)*secnum); - fseek(fp, sec_hdr_off, SEEK_SET); - res=fread(sechdrs, sizeof(Elf32_Shdr), secnum, fp); - assert(res==secnum); + //fseek(fp, sec_hdr_off, SEEK_SET); + loa.seek(sec_hdr_off, std::ios_base::beg); + //res=fread(sechdrs, sizeof(Elf32_Shdr), secnum, fp); + loa.cread((char*)sechdrs, sizeof(Elf32_Shdr)* secnum); /* look through each section and record bounds */ for (secndx=1; secndx<secnum; secndx++) - get_executable_bounds(&sechdrs[secndx], fp, virp); + get_executable_bounds(&sechdrs[secndx], loa, firp); /* look through each section and look for target possibilities */ for (secndx=1; secndx<secnum; secndx++) - infer_targets(&sechdrs[secndx], fp, virp); + infer_targets(&sechdrs[secndx], loa, firp); cout<<"========================================="<<endl; @@ -369,7 +380,7 @@ void fill_in_indtargs(FileIR_t* virp, string elf_file, pqxxDB_t &pqxx_interface) cout<<"========================================="<<endl; /* look through the instructions in the program for targets */ - get_instruction_targets(virp); + get_instruction_targets(firp); /* mark the entry point as a target */ possible_target(elfhdr.e_entry); @@ -382,8 +393,8 @@ void fill_in_indtargs(FileIR_t* virp, string elf_file, pqxxDB_t &pqxx_interface) cout<<"========================================="<<endl; /* Read the exception handler frame so that those indirect branches are accounted for */ - void read_ehframe(FileIR_t* virp, pqxxDB_t& pqxx_interface); - read_ehframe(virp, pqxx_interface); + void read_ehframe(FileIR_t* firp, pqxxDB_t& pqxx_interface); + read_ehframe(firp, pqxx_interface); cout<<"========================================="<<endl; cout<<"All targets from data+instruction+eh_header sections are: " << endl; @@ -393,7 +404,7 @@ void fill_in_indtargs(FileIR_t* virp, string elf_file, pqxxDB_t &pqxx_interface) /* now process the ranges that have exception handling */ - process_ranges(virp); + process_ranges(firp); cout<<"========================================="<<endl; cout<<"All targets from data+instruction+eh_header sections+eh_header_ranges are: " << endl; cout<<"# ATTRIBUTE total_indirect_targets_pass4="<<std::dec<<targets.size()<<endl; @@ -401,10 +412,10 @@ void fill_in_indtargs(FileIR_t* virp, string elf_file, pqxxDB_t &pqxx_interface) cout<<"========================================="<<endl; /* Add functions containing unsigned int params to the list */ - add_num_handle_fn_watches(virp); + add_num_handle_fn_watches(firp); /* set the IR to have some instructions marked as IB targets */ - mark_targets(virp); + mark_targets(firp); } @@ -415,9 +426,9 @@ void fill_in_indtargs(FileIR_t* virp, string elf_file, pqxxDB_t &pqxx_interface) main(int argc, char* argv[]) { - if(argc!=3) + if(argc!=2) { - cerr<<"Usage: fill_in_indtargs <id> <elffile>"<<endl; + cerr<<"Usage: fill_in_indtargs <id>"<<endl; exit(-1); } @@ -425,7 +436,7 @@ main(int argc, char* argv[]) VariantID_t *pidp=NULL; - FileIR_t * virp=NULL; + FileIR_t * firp=NULL; try { @@ -439,14 +450,26 @@ main(int argc, char* argv[]) cout<<"New Variant, after reading registration, is: "<<*pidp << endl; - // read the db - virp=new FileIR_t(*pidp); + for(set<File_t*>::iterator it=pidp->GetFiles().begin(); + it!=pidp->GetFiles().end(); + ++it + ) + { + File_t* this_file=*it; + assert(this_file); + + // read the db + firp=new FileIR_t(*pidp, this_file); - // find all indirect branch targets - fill_in_indtargs(virp,argv[2], pqxx_interface); + // find all indirect branch targets + fill_in_indtargs(firp, pqxx_interface); + + // write the DB back and commit our changes + + firp->WriteToDB(); + delete firp; + } - // write the DB back and commit our changes - virp->WriteToDB(); pqxx_interface.Commit(); } @@ -456,9 +479,8 @@ main(int argc, char* argv[]) exit(-1); } - assert(virp && pidp); + assert(firp && pidp); delete pidp; - delete virp; } diff --git a/libIRDB/test/fix_calls.cpp b/libIRDB/test/fix_calls.cpp index ee5aa31e9bf06822bb5fd3e21c6f2dc709d1df72..955874f0fba1682a30380ad35aa1d31b7a2f830a 100644 --- a/libIRDB/test/fix_calls.cpp +++ b/libIRDB/test/fix_calls.cpp @@ -167,7 +167,7 @@ bool call_needs_fix(Instruction_t* insn) -void fix_call(Instruction_t* insn, FileIR_t *virp) +void fix_call(Instruction_t* insn, FileIR_t *firp) { /* record the possibly new indirect branch target if this call gets fixed */ Instruction_t* newindirtarg=insn->GetFallthrough(); @@ -243,8 +243,8 @@ void fix_call(Instruction_t* insn, FileIR_t *virp) callinsn->SetDataBits(newbits); /* add the new insn and new address into the list of valid calls and addresses */ - virp->GetAddresses().insert(calladdr); - virp->GetInstructions().insert(callinsn); + firp->GetAddresses().insert(calladdr); + firp->GetInstructions().insert(callinsn); /* Convert the old call instruction into a push return_address instruction */ insn->SetFallthrough(callinsn); @@ -271,7 +271,7 @@ void fix_call(Instruction_t* insn, FileIR_t *virp) /* set the insturction and include this address in the list of addrs */ newindirtarg->SetIndirectBranchTargetAddress(newaddr); - virp->GetAddresses().insert(newaddr); + firp->GetAddresses().insert(newaddr); } @@ -299,10 +299,10 @@ bool is_call(Instruction_t* insn) return (disasm.Instruction.BranchType==CallType); } -File_t* find_file(FileIR_t* virp, db_id_t fileid) +File_t* find_file(FileIR_t* firp, db_id_t fileid) { #if 0 - set<File_t*> &files=virp->GetFiles(); + set<File_t*> &files=firp->GetFiles(); for( set<File_t*>::iterator it=files.begin(); @@ -316,8 +316,8 @@ File_t* find_file(FileIR_t* virp, db_id_t fileid) } return NULL; #endif - assert(virp->GetFile()->GetBaseID()==fileid); - return virp->GetFile(); + assert(firp->GetFile()->GetBaseID()==fileid); + return firp->GetFile(); } @@ -327,15 +327,15 @@ File_t* find_file(FileIR_t* virp, db_id_t fileid) // fix_all_calls - convert calls to push/jump pairs in the IR. if fix_all is true, all calls are converted, // else we attempt to detect the calls it is safe to convert. // -void fix_all_calls(FileIR_t* virp, bool print_stats, bool fix_all) +void fix_all_calls(FileIR_t* firp, bool print_stats, bool fix_all) { long long fixed_calls=0, not_fixed_calls=0, not_calls=0; for( - set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -347,7 +347,7 @@ void fix_all_calls(FileIR_t* virp, bool print_stats, bool fix_all) if(fix_all || call_needs_fix(insn)) { fixed_calls++; - fix_call(insn, virp); + fix_call(insn, firp); } else not_fixed_calls++; @@ -401,7 +401,7 @@ main(int argc, char* argv[]) } VariantID_t *pidp=NULL; - FileIR_t *virp=NULL; + FileIR_t *firp=NULL; /* setup the interface to the sql server */ BaseObj_t::SetInterface(&pqxx_interface); @@ -411,11 +411,31 @@ main(int argc, char* argv[]) { pidp=new VariantID_t(atoi(argv[1])); + cout<<"Fixing calls->push/jmp in variant "<<*pidp<< "." <<endl; assert(pidp->IsRegistered()==true); - // read the db - virp=new FileIR_t(*pidp); + for(set<File_t*>::iterator it=pidp->GetFiles().begin(); + it!=pidp->GetFiles().end(); + ++it + ) + { + File_t* this_file=*it; + assert(this_file); + + // read the db + firp=new FileIR_t(*pidp,this_file); + + assert(firp && pidp); + + fix_all_calls(firp,true,fix_all); + firp->WriteToDB(); + cout<<"Done!"<<endl; + delete firp; + + } + cout<<"Writing variant "<<*pidp<<" back to database." << endl; + pqxx_interface.Commit(); } @@ -425,19 +445,8 @@ main(int argc, char* argv[]) exit(-1); } - assert(virp && pidp); - - cout<<"Fixing calls->push/jmp in variant "<<*pidp<< "." <<endl; - - fix_all_calls(virp,true,fix_all); - - cout<<"Writing variant "<<*pidp<<" back to database." << endl; - virp->WriteToDB(); - pqxx_interface.Commit(); - cout<<"Done!"<<endl; - delete virp; delete pidp; } diff --git a/libIRDB/test/ilr.cpp b/libIRDB/test/ilr.cpp index e3256f69bf47be48e4d2273f5cb93bf373ff57f0..c8a9e389ce6c3edcdba1681ecd1e41443852d548 100644 --- a/libIRDB/test/ilr.cpp +++ b/libIRDB/test/ilr.cpp @@ -7,42 +7,10 @@ using namespace libIRDB; using namespace std; -main(int argc, char* argv[]) +void do_ilr(VariantID_t *pidp, FileIR_t* firp) { - if(argc!=2) - { - cerr<<"Usage: ilr <id>"<<endl; - exit(-1); - } - - VariantID_t *pidp=NULL; - FileIR_t *virp=NULL; - - /* setup the interface to the sql server */ - pqxxDB_t pqxx_interface; - BaseObj_t::SetInterface(&pqxx_interface); - - cout<<"Reading variant "<<string(argv[1])<<" from database." << endl; - try - { - - pidp=new VariantID_t(atoi(argv[1])); - - assert(pidp->IsRegistered()==true); - - // read the db - virp=new FileIR_t(*pidp); - - - } - catch (DatabaseError_t pnide) - { - cout<<"Unexpected database error: "<<pnide<<endl; - exit(-1); - } - - assert(virp && pidp); + assert(firp && pidp); cout<<"Applying ILR to variant "<<*pidp<< "." <<endl; @@ -50,8 +18,8 @@ main(int argc, char* argv[]) set<AddressID_t*> newaddressset; for( - set<Instruction_t*>::const_iterator it=virp->GetInstructions().begin(); - it!=virp->GetInstructions().end(); + set<Instruction_t*>::const_iterator it=firp->GetInstructions().begin(); + it!=firp->GetInstructions().end(); ++it ) { @@ -71,19 +39,70 @@ main(int argc, char* argv[]) newaddressset.insert(newaddr); } - virp->GetAddresses()=newaddressset; + firp->GetAddresses()=newaddressset; + cout << "# ATTRIBUTE filename="<<firp->GetFile()->GetURL()<<endl; cout << "# ATTRIBUTE unmoved_instructions="<<std::dec<<unmoved_instr<<endl; cout << "# ATTRIBUTE moved_instructions="<<std::dec<<moved_instr<<endl; cout << "# ATTRIBUTE moved_ratio="<<std::dec<<(float)moved_instr/(moved_instr+unmoved_instr)<<endl; cout<<"Writing variant "<<*pidp<<" back to database." << endl; - virp->WriteToDB(); + firp->WriteToDB(); +} +main(int argc, char* argv[]) +{ + + if(argc!=2) + { + cerr<<"Usage: ilr <id>"<<endl; + exit(-1); + } + + VariantID_t *pidp=NULL; + FileIR_t *firp=NULL; + + /* setup the interface to the sql server */ + pqxxDB_t pqxx_interface; + BaseObj_t::SetInterface(&pqxx_interface); + + cout<<"Reading variant "<<string(argv[1])<<" from database." << endl; + try + { + + pidp=new VariantID_t(atoi(argv[1])); + assert(pidp->IsRegistered()==true); + + + for(set<File_t*>::iterator it=pidp->GetFiles().begin(); + it!=pidp->GetFiles().end(); + ++it + ) + { + File_t* this_file=*it; + assert(this_file); + + // read the db + firp=new FileIR_t(*pidp,this_file); + + // do the ILRing. + do_ilr(pidp, firp); + + delete firp; + } + + + pqxx_interface.Commit(); + + } + catch (DatabaseError_t pnide) + { + cout<<"Unexpected database error: "<<pnide<<endl; + exit(-1); + } - pqxx_interface.Commit(); cout<<"Done!"<<endl; - delete virp; delete pidp; } + diff --git a/tools/meds2pdb/meds2pdb.cpp b/tools/meds2pdb/meds2pdb.cpp index 298050163e5f7a6bdf6a1d6d2de66c403203274d..c5635642e063508c45b0f86fa3729c6d622c4e0b 100644 --- a/tools/meds2pdb/meds2pdb.cpp +++ b/tools/meds2pdb/meds2pdb.cpp @@ -4,12 +4,18 @@ #include <string.h> #include "rewriter.h" #include <pqxx/pqxx> +#include <stdlib.h> using namespace std; using namespace pqxx; #include <sstream> +string functionTable; +string addressTable; +string instructionTable; + + template <class T> inline std::string my_to_string (const T& t) { @@ -48,7 +54,7 @@ int get_file_id(char *progName, char *md5hash) // insert addresses & instructions into DB -void insert_instructions(string programName, int fileID, vector<wahoo::Instruction*> instructions, vector<wahoo::Function*> functions) +void insert_instructions(int fileID, vector<wahoo::Instruction*> instructions, vector<wahoo::Function*> functions) { cerr << "Inserting instructions in the DB"<<endl; connection conn; @@ -62,11 +68,9 @@ void insert_instructions(string programName, int fileID, vector<wahoo::Instructi for (int i = 0; i < instructions.size(); i ++ ) { char buf[128]; - string addressTable = programName + "_" + "address"; string query = "INSERT INTO " + addressTable; query += " (address_id, file_id, vaddress_offset) VALUES "; - string instructionTable = programName + "_" + "instruction"; string query2 = "INSERT INTO " + instructionTable; query2 += " (instruction_id,address_id, parent_function_id, orig_address_id, data, comment) VALUES "; @@ -136,7 +140,7 @@ void insert_instructions(string programName, int fileID, vector<wahoo::Instructi } -void insert_functions( string programName, int fileID, const vector<wahoo::Function*> &functions ) +void insert_functions(int fileID, const vector<wahoo::Function*> &functions ) { connection conn; work txn(conn); @@ -146,7 +150,6 @@ void insert_functions( string programName, int fileID, const vector<wahoo::Funct int count = 0; for (int i = 0; i < functions.size(); i += STRIDE) { - string functionTable = string(programName) + "_" + "function"; string query = "INSERT INTO " + functionTable; query += " (function_id, name, stack_frame_size, out_args_region_size, use_frame_pointer) VALUES "; @@ -181,7 +184,7 @@ void insert_functions( string programName, int fileID, const vector<wahoo::Funct txn.commit(); // must commit o/w everything will be rolled back } -void update_functions( string programName, int fileID, const vector<wahoo::Function*> &functions ) +void update_functions(int fileID, const vector<wahoo::Function*> &functions ) { connection conn; work txn(conn); @@ -200,7 +203,6 @@ void update_functions( string programName, int fileID, const vector<wahoo::Funct bool useFP = f->getUseFramePointer(); int insnid=address_to_instructionid_map[functionAddress]; - string functionTable = string(programName) + "_" + "function"; query += "update " + functionTable; query += " set entry_point_id = " + txn.quote(my_to_string(insnid)); query += " where function_id = " + txn.quote(my_to_string(function_id)); @@ -213,45 +215,55 @@ void update_functions( string programName, int fileID, const vector<wahoo::Funct txn.commit(); // must commit o/w everything will be rolled back } + + int main(int argc, char **argv) { - if (argc < 5) - { - cerr << "usage: " << argv[0] << " <programName> <elfFile> <md5ElfFile> <annotationFile> " << endl; - return 1; - } + if (argc != 7) + { + cerr << "usage: " << argv[0] << " <annotations file> <file id> <func tab name> <insn tab name> <addr tab name> <elf file>" << endl; + return 1; + } - char *programName = argv[1]; - char *elfFile = argv[2]; - char *md5hash = argv[3]; - char *annotFile = argv[4]; + char *annotFile = argv[1]; + char *fid=argv[2]; + char *myFunctionTable=argv[3]; + char *myInstructionTable=argv[4]; + char *myAddressTable=argv[5]; + char *elfFile=argv[6]; - cerr << "program name:" << programName << endl; - cerr << "elf file:" << elfFile << endl; - cerr << "hash-md5:" << md5hash << endl; - cerr << "annotation file:" << annotFile << endl; + cout<<"Annotation file: "<< annotFile<<endl; + cout<<"File ID: "<< fid<<endl; + cout<<"FTN:: "<< myFunctionTable<<endl; + cout<<"ITN: "<< myInstructionTable<<endl; + cout<<"ATN: "<< myAddressTable<<endl; + // set global vars for importing. + functionTable=myFunctionTable; + addressTable=myAddressTable; + instructionTable=myInstructionTable; - Rewriter *rewriter = new Rewriter(elfFile, annotFile); - int fileID = get_file_id(programName, md5hash); - if (fileID < 0) - { - cerr << argv[0] << ": Error retrieving file id for: " << programName << endl; - return 1; - } - else - cerr << "File id is: " << fileID << endl; + Rewriter *rewriter = new Rewriter(elfFile, annotFile); + + int fileID = atoi(fid); + if(fileID<=0) + { + cerr << "Bad fileID: " << fid <<endl; + exit(1); + } + + // get functions & instructions from MEDS + vector<wahoo::Function*> functions = rewriter->getAllFunctions(); + vector<wahoo::Instruction*> instructions = rewriter->getAllInstructions(); - // get functions & instructions from MEDS - vector<wahoo::Function*> functions = rewriter->getAllFunctions(); - vector<wahoo::Instruction*> instructions = rewriter->getAllInstructions(); + cerr << "Number of functions: " << functions.size() << endl; + cerr << "Number of instructions: " << instructions.size() << endl; - cerr << "Number of functions: " << functions.size() << endl; - cerr << "Number of instructions: " << instructions.size() << endl; + insert_functions(fileID, functions); + insert_instructions(fileID, instructions, functions); + update_functions(fileID, functions); - insert_functions(programName, fileID, functions); - insert_instructions(programName, fileID, instructions, functions); - update_functions(programName, fileID, functions); + exit(0); }