linker: Formatting

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2019-01-18 17:03:57 +01:00
parent a16218e1d5
commit cc1340190e
8 changed files with 499 additions and 405 deletions
+18 -10
View File
@@ -1,6 +1,6 @@
#include "std.hpp"
#include "dlltoexe.hpp"
#include "std.hpp"
using namespace std;
@@ -28,17 +28,25 @@ struct Sect{
};
#pragma pack(pop)
bool dllToExe( const char *exe_file,const char *dll_file,const char *entry_func ){
bool dllToExe(const char* exe_file, const char* dll_file, const char* entry_func)
{
//find proc address of bbWinMain
HMODULE hmod=LoadLibrary( dll_file );if( !hmod ) return false;
HMODULE hmod = LoadLibrary(dll_file);
if (!hmod)
return false;
int proc = (int)GetProcAddress(hmod, entry_func);
int entry=proc-(int)hmod;FreeLibrary( hmod );
if( !proc ) return false;
int entry = proc - (int)hmod;
FreeLibrary(hmod);
if (!proc)
return false;
//Convert dll to exe
fstream in( dll_file,ios_base::binary|ios_base::in );if( !in.is_open() ) return false;
fstream out( exe_file,ios::binary|ios_base::out|ios_base::trunc );if( !out.is_open() ) return false;
fstream in(dll_file, ios_base::binary | ios_base::in);
if (!in.is_open())
return false;
fstream out(exe_file, ios::binary | ios_base::out | ios_base::trunc);
if (!out.is_open())
return false;
int offs;
in.seekg(0x3c);
@@ -46,7 +54,8 @@ bool dllToExe( const char *exe_file,const char *dll_file,const char *entry_func
//copy first bit...
in.seekg(0);
for( int k=0;k<offs+4;++k ) out.put( in.get() );
for (int k = 0; k < offs + 4; ++k)
out.put(in.get());
//reader file header
Head head = {0};
@@ -71,4 +80,3 @@ bool dllToExe( const char *exe_file,const char *dll_file,const char *entry_func
return true;
}
+75 -42
View File
@@ -1,6 +1,6 @@
#include "std.hpp"
#include "image_util.hpp"
#include "std.hpp"
using namespace std;
@@ -66,13 +66,17 @@ struct Rsrc{
int data_sz;
vector<Rsrc*> kids;
Rsrc( int id,Rsrc *p ):id(id),data(0),data_sz(0){
if( p ) p->kids.push_back( this );
Rsrc(int id, Rsrc* p) : id(id), data(0), data_sz(0)
{
if (p)
p->kids.push_back(this);
// cout<<"res id:"<<dec<<id<<hex<<endl;
}
~Rsrc(){
for( ;kids.size();kids.pop_back() ) delete kids.back();
~Rsrc()
{
for (; kids.size(); kids.pop_back())
delete kids.back();
delete data;
}
};
@@ -82,7 +86,10 @@ struct Section{
char* data;
Section() : data(0) {}
~Section(){ delete[] data; }
~Section()
{
delete[] data;
}
};
static char* stub;
@@ -103,7 +110,8 @@ static Rsrc *rsrc_root;
static const char* img_file;
static void openRsrcDir( Section *s,int off,Rsrc *p ){
static void openRsrcDir(Section* s, int off, Rsrc* p)
{
char* data = (char*)s->data;
Rdir* dir = (Rdir*)(data + off);
@@ -125,13 +133,16 @@ static void openRsrcDir( Section *s,int off,Rsrc *p ){
}
}
static void openRsrcTree( Section *s ){
static void openRsrcTree(Section* s)
{
rsrc_root = new Rsrc(0, 0);
openRsrcDir(s, 0, rsrc_root);
}
static int rsrcSize( Rsrc *r ){
if( r->data ) return (sizeof(Rdat)+r->data_sz+7)&~7;
static int rsrcSize(Rsrc* r)
{
if (r->data)
return (sizeof(Rdat) + r->data_sz + 7) & ~7;
int sz = sizeof(Rdir);
for (int k = 0; k < r->kids.size(); ++k) {
sz += sizeof(Rent) + rsrcSize(r->kids[k]);
@@ -139,8 +150,8 @@ static int rsrcSize( Rsrc *r ){
return sz;
}
static void closeRsrcDir( Section *s,int off,Rsrc *p ){
static void closeRsrcDir(Section* s, int off, Rsrc* p)
{
int t, k;
char* data = (char*)s->data;
@@ -160,7 +171,8 @@ static void closeRsrcDir( Section *s,int off,Rsrc *p ){
Rsrc* r = p->kids[k];
ent->id = r->id;
ent->data = t;
if( !r->data ) ent->data|=0x80000000;
if (!r->data)
ent->data |= 0x80000000;
t += rsrcSize(r);
}
@@ -182,23 +194,24 @@ static void closeRsrcDir( Section *s,int off,Rsrc *p ){
}
}
static int fileAlign( int n ){
static int fileAlign(int n)
{
return (n + (opts->file_align - 1)) & ~(opts->file_align - 1);
}
static int sectAlign( int n ){
static int sectAlign(int n)
{
return (n + (opts->sect_align - 1)) & ~(opts->sect_align - 1);
}
static void closeRsrcTree( Section *s ){
static void closeRsrcTree(Section* s)
{
int virt_sz = rsrcSize(rsrc_root);
int data_sz = fileAlign(virt_sz);
int virt_delta = sectAlign(virt_sz) - sectAlign(s->sect.virt_size);
int data_delta = fileAlign(virt_sz) - fileAlign(s->sect.virt_size);
for (int k = 0; k < sections.size(); ++k) {
Section* t = sections[k];
if (t->sect.virt_addr > s->sect.virt_addr) {
@@ -226,28 +239,37 @@ static void closeRsrcTree( Section *s ){
rsrc_root = 0;
}
static Rsrc *findRsrc( int id,Rsrc *p ){
static Rsrc* findRsrc(int id, Rsrc* p)
{
for (int k = 0; k < p->kids.size(); ++k) {
if( p->kids[k]->id==id ) return p->kids[k];
if (p->kids[k]->id == id)
return p->kids[k];
}
return 0;
}
static Rsrc *findRsrc( int type,int id,int lang ){
Rsrc *r=findRsrc( type,rsrc_root );if( !r ) return 0;
r=findRsrc( id,r );if( !r ) return 0;
static Rsrc* findRsrc(int type, int id, int lang)
{
Rsrc* r = findRsrc(type, rsrc_root);
if (!r)
return 0;
r = findRsrc(id, r);
if (!r)
return 0;
return findRsrc(lang, r);
}
static void loadImage( istream &in ){
static void loadImage(istream& in)
{
int k;
//read stub
in.seekg(0x3c);
in.read((char*)&stub_sz, 4);
stub_sz+=4;stub=new char[stub_sz];
in.seekg( 0 );in.read( stub,stub_sz );
stub_sz += 4;
stub = new char[stub_sz];
in.seekg(0);
in.read(stub, stub_sz);
//read head
head = new Head;
@@ -273,7 +295,8 @@ static void loadImage( istream &in ){
for (k = 0; k < head->num_sects; ++k) {
Section* s = sections[k];
if( !s->sect.data_addr ) continue;
if (!s->sect.data_addr)
continue;
int data_sz = s->sect.data_size;
s->data = new char[data_sz]; //char[s->sect.virt_size];
//memset( s->data,0,s->sect.virt_size );
@@ -282,8 +305,8 @@ static void loadImage( istream &in ){
}
}
static void saveImage( ostream &out ){
static void saveImage(ostream& out)
{
int k;
out.write((char*)stub, stub_sz);
@@ -298,9 +321,11 @@ static void saveImage( ostream &out ){
for (k = 0; k < head->num_sects; ++k) {
Section* s = sections[k];
if( !s->sect.data_addr ) continue;
if (!s->sect.data_addr)
continue;
//assumes sect data is in order!!!!!
while( out.tellp()<s->sect.data_addr ) out.put( (char)0xbb );
while (out.tellp() < s->sect.data_addr)
out.put((char)0xbb);
out.seekp(s->sect.data_addr);
out.write(s->data, s->sect.data_size);
}
@@ -308,7 +333,8 @@ static void saveImage( ostream &out ){
/********************** PUBLIC STUFF ***********************/
bool openImage( const char *img ){
bool openImage(const char* img)
{
img_file = img;
fstream in(img_file, ios_base::binary | ios_base::in);
@@ -317,8 +343,10 @@ bool openImage( const char *img ){
return true;
}
bool makeExe( int entry ){
if( !img_file ) return false;
bool makeExe(int entry)
{
if (!img_file)
return false;
head->chars |= 0x0002; //executable
head->chars &= ~0x2000; //not Dll
@@ -327,12 +355,15 @@ bool makeExe( int entry ){
return true;
}
bool replaceRsrc( int type,int id,int lang,void *data,int data_sz ){
if( !img_file ) return false;
bool replaceRsrc(int type, int id, int lang, void* data, int data_sz)
{
if (!img_file)
return false;
for (int k = 0; k < sections.size(); ++k) {
Section* s = sections[k];
if( strcmp( s->sect.name,".rsrc" ) ) continue;
if (strcmp(s->sect.name, ".rsrc"))
continue;
openRsrcTree(s);
if (Rsrc* r = findRsrc(type, id, lang)) {
@@ -348,14 +379,17 @@ bool replaceRsrc( int type,int id,int lang,void *data,int data_sz ){
return false;
}
void closeImage(){
if( !img_file ) return;
void closeImage()
{
if (!img_file)
return;
fstream out(img_file, ios_base::binary | ios_base::out | ios_base::trunc);
saveImage(out);
out.close();
for( ;sections.size();sections.pop_back() ) delete sections.back();
for (; sections.size(); sections.pop_back())
delete sections.back();
delete[] ddir;
delete opts;
delete head;
@@ -364,4 +398,3 @@ void closeImage(){
}
#endif
+110 -56
View File
@@ -1,7 +1,7 @@
#include "std.hpp"
#include "linker.hpp"
#include "image_util.hpp"
#include "std.hpp"
using namespace std;
@@ -33,18 +33,24 @@ private:
map<string, int> symbols;
map<int, string> rel_relocs, abs_relocs;
bool findSym( const string &t,Module *libs,int *n ){
if( findSymbol( t.c_str(),n ) ) return true;
if( libs->findSymbol( t.c_str(),n ) ) return true;
bool findSym(const string& t, Module* libs, int* n)
{
if (findSymbol(t.c_str(), n))
return true;
if (libs->findSymbol(t.c_str(), n))
return true;
string err = "Symbol '" + t + "' not found";
MessageBox(GetDesktopWindow(), err.c_str(), "Blitz Linker Error", MB_TOPMOST | MB_SETFOREGROUND);
return false;
}
void ensure( int n ){
if( pc+n<=data_sz ) return;
void ensure(int n)
{
if (pc + n <= data_sz)
return;
data_sz = data_sz / 2 + data_sz;
if( data_sz<pc+n ) data_sz=pc+n;
if (data_sz < pc + n)
data_sz = pc + n;
char* old_data = data;
data = new char[data_sz];
memcpy(data, old_data, pc);
@@ -52,17 +58,20 @@ private:
}
};
BBModule::BBModule():data(0),data_sz(0),pc(0),linked(false){
BBModule::BBModule() : data(0), data_sz(0), pc(0), linked(false) {}
BBModule::~BBModule()
{
if (linked)
VirtualFree(data, 0, MEM_RELEASE);
else
delete[] data;
}
BBModule::~BBModule(){
if( linked ) VirtualFree( data,0,MEM_RELEASE );
else delete[] data;
}
void *BBModule::link( Module *libs ){
if( linked ) return data;
void* BBModule::link(Module* libs)
{
if (linked)
return data;
int dest;
map<int, string>::iterator it;
@@ -75,63 +84,89 @@ void *BBModule::link( Module *libs ){
linked = true;
for (it = rel_relocs.begin(); it != rel_relocs.end(); ++it) {
if( !findSym( it->second,libs,&dest ) ) return 0;
int *p=(int*)(data+it->first);*p+=(dest-(int)p);
if (!findSym(it->second, libs, &dest))
return 0;
int* p = (int*)(data + it->first);
*p += (dest - (int)p);
}
for (it = abs_relocs.begin(); it != abs_relocs.end(); ++it) {
if( !findSym( it->second,libs,&dest ) ) return 0;
int *p=(int*)(data+it->first);*p+=dest;
if (!findSym(it->second, libs, &dest))
return 0;
int* p = (int*)(data + it->first);
*p += dest;
}
return data;
}
int BBModule::getPC(){
int BBModule::getPC()
{
return pc;
}
void BBModule::emit( int byte ){
ensure(1);data[pc++]=byte;
void BBModule::emit(int byte)
{
ensure(1);
data[pc++] = byte;
}
void BBModule::emitw( int word ){
ensure(2);*(short*)(data+pc)=word;pc+=2;
void BBModule::emitw(int word)
{
ensure(2);
*(short*)(data + pc) = word;
pc += 2;
}
void BBModule::emitd( int dword ){
ensure(4);*(int*)(data+pc)=dword;pc+=4;
void BBModule::emitd(int dword)
{
ensure(4);
*(int*)(data + pc) = dword;
pc += 4;
}
void BBModule::emitx( void *mem,int sz ){
ensure(sz);memcpy( data+pc,mem,sz );pc+=sz;
void BBModule::emitx(void* mem, int sz)
{
ensure(sz);
memcpy(data + pc, mem, sz);
pc += sz;
}
bool BBModule::addSymbol( const char *sym,int pc ){
bool BBModule::addSymbol(const char* sym, int pc)
{
string t(sym);
if( symbols.find( t )!=symbols.end() ) return false;
symbols[t]=pc;return true;
if (symbols.find(t) != symbols.end())
return false;
symbols[t] = pc;
return true;
}
bool BBModule::addReloc( const char *dest_sym,int pc,bool pcrel ){
bool BBModule::addReloc(const char* dest_sym, int pc, bool pcrel)
{
map<int, string>& rel = pcrel ? rel_relocs : abs_relocs;
if( rel.find( pc )!=rel.end() ) return false;
rel[pc]=string(dest_sym);return true;
if (rel.find(pc) != rel.end())
return false;
rel[pc] = string(dest_sym);
return true;
}
bool BBModule::findSymbol( const char *sym,int *pc ){
bool BBModule::findSymbol(const char* sym, int* pc)
{
string t = string(sym);
map<string, int>::iterator it = symbols.find(t);
if( it==symbols.end() ) return false;
if (it == symbols.end())
return false;
*pc = it->second + (int)data;
return true;
}
int Linker::version(){
int Linker::version()
{
return VERSION;
}
bool Linker::canCreateExe(){
bool Linker::canCreateExe()
{
#ifdef DEMO
return false;
#else
@@ -139,32 +174,43 @@ bool Linker::canCreateExe(){
#endif
}
Module *Linker::createModule(){
Module* Linker::createModule()
{
return new BBModule();
}
void Linker::deleteModule( Module *mod ){
void Linker::deleteModule(Module* mod)
{
delete mod;
}
Linker *_cdecl linkerGetLinker(){
static Linker linker;return &linker;
Linker* _cdecl linkerGetLinker()
{
static Linker linker;
return &linker;
}
bool BBModule::createExe( const char *exe_file,const char *dll_file ){
bool BBModule::createExe(const char* exe_file, const char* dll_file)
{
#ifdef DEMO
return false;
#else
//find proc address of bbWinMain
HMODULE hmod=LoadLibrary( dll_file );if( !hmod ) return false;
HMODULE hmod = LoadLibrary(dll_file);
if (!hmod)
return false;
int proc = (int)GetProcAddress(hmod, "_bbWinMain@0");
int entry=proc-(int)hmod;FreeLibrary( hmod );if( !proc ) return false;
int entry = proc - (int)hmod;
FreeLibrary(hmod);
if (!proc)
return false;
if( !CopyFile( dll_file,exe_file,false ) ) return false;
if (!CopyFile(dll_file, exe_file, false))
return false;
if( !openImage( exe_file ) ) return false;
if (!openImage(exe_file))
return false;
makeExe(entry);
@@ -181,30 +227,38 @@ bool BBModule::createExe( const char *exe_file,const char *dll_file ){
map<int, string>::iterator rit;
//write the code
int sz=pc;out.write( (char*)&sz,4 );out.write( data,pc );
int sz = pc;
out.write((char*)&sz, 4);
out.write(data, pc);
//write symbols
sz=symbols.size();out.write( (char*)&sz,4 );
sz = symbols.size();
out.write((char*)&sz, 4);
for (it = symbols.begin(); it != symbols.end(); ++it) {
string t = it->first + '\0';
out.write(t.data(), t.size());
sz=it->second;out.write( (char*)&sz,4 );
sz = it->second;
out.write((char*)&sz, 4);
}
//write relative relocs
sz=rel_relocs.size();out.write( (char*)&sz,4 );
sz = rel_relocs.size();
out.write((char*)&sz, 4);
for (rit = rel_relocs.begin(); rit != rel_relocs.end(); ++rit) {
string t = rit->second + '\0';
out.write(t.data(), t.size());
sz=rit->first;out.write( (char*)&sz,4 );
sz = rit->first;
out.write((char*)&sz, 4);
}
//write absolute relocs
sz=abs_relocs.size();out.write( (char*)&sz,4 );
sz = abs_relocs.size();
out.write((char*)&sz, 4);
for (rit = abs_relocs.begin(); rit != abs_relocs.end(); ++rit) {
string t = rit->second + '\0';
out.write(t.data(), t.size());
sz=rit->first;out.write( (char*)&sz,4 );
sz = rit->first;
out.write((char*)&sz, 4);
}
replaceRsrc(10, 1111, 1033, buf.data(), buf.size());
+4 -4
View File
@@ -1,12 +1,12 @@
#pragma once
#include <map>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include "stdutil.hpp"
#include <windows.h>
+4 -5
View File
@@ -1,11 +1,11 @@
#include "std.hpp"
#include "linker.hpp"
#include "std.hpp"
#include <windows.h>
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved){
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
linkerGetLinker();
@@ -13,8 +13,7 @@ BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
default:
;
default:;
}
return TRUE;
}