Skip to content
Snippets Groups Projects
Commit c8e61172 authored by an7s's avatar an7s
Browse files

Optimized inserts for addresses and instructions

Former-commit-id: 684d4aeef75b411d2b300d7ecee2fc8f98c559a3
parent e21c08bc
No related branches found
No related tags found
No related merge requests found
......@@ -27,7 +27,7 @@ class AddressID_t : public BaseObj_t
void SetVirtualOffset(virtual_offset_t voff) { virtual_offset=voff; }
void WriteToDB() { assert(0); }
std::string WriteToDB(File_t *vid, db_id_t newid);
std::string WriteToDB(File_t *vid, db_id_t newid, bool p_withHeader);
inline bool operator<(const AddressID_t& cmp) const
{ return fileID < cmp.fileID || (fileID == cmp.fileID && virtual_offset < cmp.virtual_offset);}
......
......@@ -24,7 +24,6 @@ class Instruction_t : public BaseObj_t
std::string GetCallback() const { return callback; }
std::string GetComment() const { return comment; }
void SetAddress(AddressID_t* newaddr) { my_address=newaddr; }
void SetFunction(Function_t* func ) { my_function=func;}
void SetOriginalAddressID(db_id_t origid) { orig_address_id=origid; /* you shouldn't do this, unless you know what you're doing! */}
......@@ -38,7 +37,7 @@ class Instruction_t : public BaseObj_t
void SetIndirectBranchTargetAddress(AddressID_t* myIndTarg) { indTarg=myIndTarg; }
void WriteToDB() { assert(0); }
std::string WriteToDB(File_t *fid, db_id_t newid);
std::string WriteToDB(File_t *fid, db_id_t newid, bool p_withHeader);
int Disassemble(DISASM &d);
std::string getDisassembly();
bool Assemble(std::string assembly);
......
......@@ -6,22 +6,28 @@ using namespace libIRDB;
using namespace std;
string AddressID_t::WriteToDB(File_t *fid, db_id_t newid)
string AddressID_t::WriteToDB(File_t *fid, db_id_t newid, bool p_withHeader)
{
assert(fid);
if(GetBaseID()==NOT_IN_DATABASE)
SetBaseID(newid);
string q=string("insert into ")+fid->address_table_name +
string("(address_id , file_id , vaddress_offset , doip_id)") +
string(" values ") +
string q;
if (p_withHeader)
q=string("insert into ")+fid->address_table_name +
string("(address_id , file_id , vaddress_offset , doip_id)") +
string(" values ");
else
q = ",";
q +=
string("(") +
string("'") + to_string(GetBaseID()) + string("', ") +
string("'") + to_string(fileID) + string("', ") +
string("'") + to_string(virtual_offset) + string("', ") +
string("'") + to_string(GetDoipID()) + string("' ") +
string(");");
string(")");
return q;
}
......
......@@ -409,42 +409,60 @@ void FileIR_t::WriteToDB()
dbintr->IssueQuery(string("TRUNCATE TABLE ")+ fileptr->relocs_table_name + string(" cascade;"));
/* and now that everything has an ID, let's write to the DB */
bool withHeader;
withHeader = true;
string q=string("");
for(std::set<Function_t*>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i)
for(std::set<Function_t*>::const_iterator f=funcs.begin(); f!=funcs.end(); ++f)
{
q+=(*i)->WriteToDB(fileptr,j);
q+=(*f)->WriteToDB(fileptr,j);
if(q.size()>1024*1024)
{
dbintr->IssueQuery(q);
q=string("");
}
}
dbintr->IssueQuery(q);
withHeader = true;
q=string("");
for(std::set<AddressID_t*>::const_iterator i=addrs.begin(); i!=addrs.end(); ++i)
for(std::set<AddressID_t*>::const_iterator a=addrs.begin(); a!=addrs.end(); ++a)
{
q+=(*i)->WriteToDB(fileptr,j);
q+=(*a)->WriteToDB(fileptr,j,withHeader);
withHeader = false;
if(q.size()>1024*1024)
{
q+=";";
dbintr->IssueQuery(q);
q=string("");
withHeader = true;
}
}
dbintr->IssueQuery(q);
withHeader = true;
q=string("");
for(std::set<Instruction_t*>::const_iterator i=insns.begin(); i!=insns.end(); ++i)
{
q+=(*i)->WriteToDB(fileptr,j);
q+=(*i)->WriteToDB(fileptr,j,withHeader);
withHeader = false;
if(q.size()>1024*1024)
{
q+=";";
dbintr->IssueQuery(q);
q=string("");
withHeader = true;
}
}
string r="";
std::set<Relocation_t*> relocs = (*i)->GetRelocations();
for(set<Relocation_t*>::iterator it=relocs.begin(); it!=relocs.end(); ++it)
{
Relocation_t* reloc=*it;
r+=reloc->WriteToDB(fileptr,*i);
}
dbintr->IssueQuery(r);
}
dbintr->IssueQuery(q);
}
......
#include <all.hpp>
#include <utils.hpp>
#include <stdlib.h>
#include <stdlib.h>
#include <fstream>
......@@ -132,7 +132,7 @@ bool Instruction_t::Assemble(string assembly)
}
string Instruction_t::WriteToDB(File_t *fid, db_id_t newid)
string Instruction_t::WriteToDB(File_t *fid, db_id_t newid, bool p_withHeader)
{
assert(fid);
assert(my_address);
......@@ -156,11 +156,15 @@ string Instruction_t::WriteToDB(File_t *fid, db_id_t newid)
if(indTarg)
indirect_bt_id=indTarg->GetBaseID();
string q=
string("insert into ")+fid->instruction_table_name +
string(" (instruction_id, address_id, parent_function_id, orig_address_id, fallthrough_address_id, target_address_id, data, callback, comment, ind_target_address_id, doip_id) ")+
string(" VALUES (") +
string("'") + to_string(GetBaseID()) + string("', ") +
string q;
if (p_withHeader)
q = string("insert into ")+fid->instruction_table_name +
string(" (instruction_id, address_id, parent_function_id, orig_address_id, fallthrough_address_id, target_address_id, data, callback, comment, ind_target_address_id, doip_id) VALUES ");
else
q = ",";
q += string("('") + to_string(GetBaseID()) + string("', ") +
string("'") + to_string(my_address->GetBaseID()) + string("', ") +
string("'") + to_string(func_id) + string("', ") +
string("'") + to_string(orig_address_id) + string("', ") +
......@@ -171,14 +175,7 @@ string Instruction_t::WriteToDB(File_t *fid, db_id_t newid)
string("'") + callback + string("', ") +
string("'") + comment + string("', ") +
string("'") + to_string(indirect_bt_id) + string("', ") +
string("'") + to_string(GetDoipID()) + string("') ; ") ;
// for each relocation in this instruction
for(set<Relocation_t*>::iterator it=relocs.begin(); it!=relocs.end(); ++it)
{
Relocation_t* reloc=*it;
q+=reloc->WriteToDB(fid,this);
}
string("'") + to_string(GetDoipID()) + string("') ") ;
return q;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment