.o file reader source files

develop
Warmist 2011-08-04 21:52:22 +03:00
parent e66a03e654
commit 89c1dba637
6 changed files with 283 additions and 13 deletions

@ -0,0 +1,126 @@
#ifndef OUTFILE_H
#define OUTFILE_H
#include <string>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
namespace OutFile
{
struct Header
{
unsigned short machinetype;
unsigned short sectioncount;
unsigned long time;
unsigned long symbolptr;
unsigned long symbolcount;
unsigned short opthead;
unsigned short flags;
void PrintData()
{
std::cout<<"Symbol start:"<<symbolptr<<"\n";
}
};
struct Section
{
char name[8];
unsigned long Vsize;
unsigned long Vstart;
unsigned long size;
unsigned long start;
unsigned long ptrRel;
unsigned long ptrLine;
unsigned short numRel;
unsigned short numLine;
unsigned long flags;
void PrintData()
{
std::cout<<name<<" size:"<<size<<" start:"<<start<<"\n";
}
};
struct Symbol
{
std::string name;
unsigned long pos;
unsigned short sectnumb;
unsigned short type;
unsigned char storageclass;
unsigned char auxsymbs;
//char unk2[6];
void Read(std::iostream &s,unsigned long strptr)
{
union
{
char buf[8];
struct
{
unsigned long zeros;
unsigned long strptr;
};
}data;
s.read((char*)&data,8);
s.read((char*)&pos,4);
s.read((char*)&sectnumb,2);
s.read((char*)&type,2);
s.read((char*)&storageclass,1);
s.read((char*)&auxsymbs,1);
if(data.zeros!=0)
{
name=data.buf;
name=name.substr(0,8);
}
else
{
//name="";
//std::cout<<"Name in symbol table\n";
char buf[256];
s.seekg(strptr+data.strptr);
s.get(buf,256,'\0');
name=buf;
}
//s.seekp(6,std::ios::cur);
}
void PrintData()
{
std::cout<<name<<" section:"<<sectnumb<<" pos:"<<pos<<"\n";
}
};
struct Relocation
{
unsigned long ptr;
unsigned long tblIndex;
unsigned short type;
};
typedef std::vector<Symbol> vSymbol;
class File
{
public:
File(std::string path);
virtual ~File();
void GetText(char *ptr);
size_t GetTextSize();
void LoadSymbols();
vSymbol GetSymbols(){LoadSymbols();return symbols;};
void PrintSymbols();
void PrintRelocations();
protected:
private:
typedef std::map<std::string,Section> secMap;
secMap sections;
vSymbol symbols;
Section &GetSection(std::string name);
std::fstream mystream;
Header myhead;
// Section Text;
//Section Data;
// Section Bss;
};
}
#endif // OUTFILE_H

@ -0,0 +1,13 @@
#ifndef LUA_MISC_H
#define LUA_MISC_H
#include "luamain.h"
namespace lua
{
void RegisterMisc(lua::state &st);
}
#endif

@ -5,8 +5,8 @@ WORD=2
BYTE=3 BYTE=3
function GetTextRegion() function GetTextRegion()
local ranges=Process.getMemRanges() ranges__=ranges__ or Process.getMemRanges()
for k,v in pairs(ranges) do for k,v in pairs(ranges__) do
--for k2,v2 in pairs(v) do --for k2,v2 in pairs(v) do
-- print(string.format("%d %s->%s",k,tostring(k2),tostring(v2))) -- print(string.format("%d %s->%s",k,tostring(k2),tostring(v2)))
--end --end
@ -24,8 +24,8 @@ function GetTextRegion()
return nil return nil
end end
function GetRegionIn(pos) function GetRegionIn(pos)
local ranges=Process.getMemRanges() ranges__=ranges__ or Process.getMemRanges()
for k,v in pairs(ranges) do for k,v in pairs(ranges__) do
--for k2,v2 in pairs(v) do --for k2,v2 in pairs(v) do
-- print(string.format("%d %s->%s",k,tostring(k2),tostring(v2))) -- print(string.format("%d %s->%s",k,tostring(k2),tostring(v2)))
--end --end
@ -34,13 +34,31 @@ function GetRegionIn(pos)
--if(v["read"])then num=num+1 end --if(v["read"])then num=num+1 end
--if(v["write"])then num=num+10 end --if(v["write"])then num=num+10 end
--if(v["execute"]) then num=num+100 end --if(v["execute"]) then num=num+100 end
--print(string.format("%d %x->%x %s %d",k,v["start"],v["end"],v.name,num)) print(string.format("%d %x->%x %s %x",k,v["start"],v["end"],v.name,pos))
if pos>=v.start and pos<=v["end"] then if pos>=v.start and pos<=v["end"] then
return v return v
end end
end end
return nil return nil
end end
function ValidOffset(pos)
ranges__=ranges__ or Process.getMemRanges()
for k,v in pairs(ranges__) do
--for k2,v2 in pairs(v) do
-- print(string.format("%d %s->%s",k,tostring(k2),tostring(v2)))
--end
--local num
--num=0
--if(v["read"])then num=num+1 end
--if(v["write"])then num=num+10 end
--if(v["execute"]) then num=num+100 end
--print(string.format("%d %x->%x %s %d",k,v["start"],v["end"],v.name,num))
if pos>=v.start and pos<=v["end"] then
return true
end
end
return false
end
function unlockDF() function unlockDF()
local reg=GetTextRegion() local reg=GetTextRegion()
reg["write"]=true reg["write"]=true

@ -57,13 +57,22 @@ offsets.new("CreaturePtr",f_creatureptr)
function f_creaturegloss() --creature race vector function f_creaturegloss() --creature race vector
for k,v in pairs(offsets.getvectors()) do for k,v in pairs(offsets.getvectors()) do
if k~=0 then local reg
--print("Looking into:"..string.format("%x",k).." used:"..v) reg=GetRegionIn(k)
if reg ~=nil then
print(string.format("looking into %x wich is in %s",k,reg.name or ""))
else
print(string.format("looking into %x in nil region",k))
end
if ValidOffset(k) then
print("Looking into:"..string.format("%x",k).." used:"..v)
local vec=engine.peek(k,ptr_vector) local vec=engine.peek(k,ptr_vector)
if vec:size()>0 and vec:size()<100000 and vec:getval(0)~=0 then if vec:size()>0 and vec:size()<100000 and vec:getval(0)~=0 then
--print("\tval:"..string.format("%x",vec:getval(0))) local toff=vec:getval(0)
local token=engine.peek(vec:getval(0),ptt_dfstring) if ValidOffset(toff) then
print("\tval:"..string.format("%x",vec:getval(0)))
local token=engine.peek(toff,ptt_dfstring)
--print("\t\tval:".. token:getval()) --print("\t\tval:".. token:getval())
if token:getval()=="TOAD" then -- more offsets could be found this way if token:getval()=="TOAD" then -- more offsets could be found this way
return k-offsets.base() return k-offsets.base()
@ -71,6 +80,7 @@ function f_creaturegloss() --creature race vector
end end
end end
end end
end
return 0 return 0
end end
offsets.new("CreatureGloss",f_creaturegloss) offsets.new("CreatureGloss",f_creaturegloss)

@ -0,0 +1,100 @@
#include "OutFile.h"
using namespace OutFile;
File::File(std::string path)
{
//mystream.exceptions ( std::fstream::eofbit | std::fstream::failbit | std::fstream::badbit );
mystream.open(path.c_str(),std::fstream::binary|std::ios::in|std::ios::out);
mystream.read((char*)&myhead,sizeof(myhead));
for(unsigned i=0;i<myhead.sectioncount;i++)
{
Section x;
mystream.read((char*)&x,sizeof(Section));
sections[x.name]=x;
}
if(mystream)
{
//std::cout<<"Sizeof:"<<sizeof(Section)<<"\n";
/*myhead.PrintData();
for(auto it=sections.begin();it!=sections.end();it++)
{
it->second.PrintData();
}*/
}
else
{
std::cout<<"Error opening file!"<<std::endl;
}
}
Section &File::GetSection(std::string name)
{
return sections[name];
}
void File::GetText(char *ptr)
{
Section &s=GetSection(".text");
mystream.seekg(s.start);
mystream.read(ptr,s.size);
}
size_t File::GetTextSize()
{
Section &s=GetSection(".text");
return s.size;
}
void File::PrintRelocations()
{
for(auto it=sections.begin();it!=sections.end();it++)
{
std::cout<<it->first<<":\n";
for(unsigned i=0;i<it->second.numRel;i++)
{
Relocation r;
mystream.seekg(it->second.ptrRel+10*i);
mystream.read((char*)&r,10);
std::cout<<r.ptr<<" -- "<<r.tblIndex<<":"<</*symbols[r.tblIndex].name<<*/" type:"<<r.type<<"\n";
}
}
}
void File::PrintSymbols()
{
std::cout<<"Sizeof symbol:"<<sizeof(Symbol)<<std::endl;
std::cout<<"Symbol count:"<<myhead.symbolcount<<std::endl;
for(unsigned i=0;i<myhead.symbolcount;i++)
{
mystream.seekg(myhead.symbolptr+i*18);
Symbol s;
std::cout<<i<<"\t";
s.Read(mystream,myhead.symbolptr+18*myhead.symbolcount);
//mystream.read((char*)&s,sizeof(Symbol));
s.PrintData();
symbols.push_back(s);
if(s.auxsymbs>0)
{
i+=s.auxsymbs;
}
}
}
void File::LoadSymbols()
{
symbols.clear();
for(unsigned i=0;i<myhead.symbolcount;i++)
{
mystream.seekg(myhead.symbolptr+i*18);
Symbol s;
s.Read(mystream,myhead.symbolptr+18*myhead.symbolcount);
symbols.push_back(s);
if(s.auxsymbs>0)
{
i+=s.auxsymbs;
}
}
}
File::~File()
{
}

@ -0,0 +1,3 @@
#include "lua_Misc.h"
void lua::RegisterMisc(lua::state &st);