.o file reader source files
parent
e66a03e654
commit
89c1dba637
@ -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*)§numb,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
|
@ -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);
|
Loading…
Reference in New Issue