linker/lib: Modernize code base and remote std.c/hpp
This commit is contained in:
@@ -1,14 +1,12 @@
|
|||||||
project(linker_lib)
|
project(linker_lib)
|
||||||
|
|
||||||
add_library(${PROJECT_NAME} SHARED
|
add_library(${PROJECT_NAME} STATIC
|
||||||
"dlltoexe.cpp"
|
"dlltoexe.cpp"
|
||||||
"dlltoexe.hpp"
|
"dlltoexe.hpp"
|
||||||
"image_util.cpp"
|
"image_util.cpp"
|
||||||
"image_util.hpp"
|
"image_util.hpp"
|
||||||
"linker.cpp"
|
"linker.cpp"
|
||||||
"linker.hpp"
|
"linker.hpp"
|
||||||
"std.cpp"
|
|
||||||
"std.hpp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
|
|
||||||
#include "dlltoexe.hpp"
|
#include "dlltoexe.hpp"
|
||||||
#include "std.hpp"
|
#include <fstream>
|
||||||
|
|
||||||
using namespace std;
|
#include <Windows.h>
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
#pragma pack(push, 1)
|
||||||
struct Head {
|
struct Head {
|
||||||
@@ -41,10 +40,10 @@ bool dllToExe(const char* exe_file, const char* dll_file, const char* entry_func
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
//Convert dll to exe
|
//Convert dll to exe
|
||||||
fstream in(dll_file, ios_base::binary | ios_base::in);
|
std::fstream in(dll_file, std::ios_base::binary | std::ios_base::in);
|
||||||
if (!in.is_open())
|
if (!in.is_open())
|
||||||
return false;
|
return false;
|
||||||
fstream out(exe_file, ios::binary | ios_base::out | ios_base::trunc);
|
std::fstream out(exe_file, std::ios::binary | std::ios_base::out | std::ios_base::trunc);
|
||||||
if (!out.is_open())
|
if (!out.is_open())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,2 @@
|
|||||||
|
#pragma once
|
||||||
#ifndef DLLTOEXE_H
|
|
||||||
#define DLLTOEXE_H
|
|
||||||
|
|
||||||
bool dllToExe(const char* exe_file, const char* dll_file, const char* entry);
|
bool dllToExe(const char* exe_file, const char* dll_file, const char* entry);
|
||||||
|
|
||||||
#endif
|
|
||||||
+10
-10
@@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
#include "image_util.hpp"
|
#include "image_util.hpp"
|
||||||
#include "std.hpp"
|
#include <fstream>
|
||||||
|
#include <istream>
|
||||||
using namespace std;
|
#include <ostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#ifndef DEMO
|
#ifndef DEMO
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ struct Rsrc {
|
|||||||
int id;
|
int id;
|
||||||
void* data;
|
void* data;
|
||||||
int data_sz;
|
int data_sz;
|
||||||
vector<Rsrc*> kids;
|
std::vector<Rsrc*> kids;
|
||||||
|
|
||||||
Rsrc(int id, Rsrc* p) : id(id), data(0), data_sz(0)
|
Rsrc(int id, Rsrc* p) : id(id), data(0), data_sz(0)
|
||||||
{
|
{
|
||||||
@@ -104,7 +104,7 @@ static int opts_sz;
|
|||||||
static DDir* ddir;
|
static DDir* ddir;
|
||||||
static int ddir_sz;
|
static int ddir_sz;
|
||||||
|
|
||||||
static vector<Section*> sections;
|
static std::vector<Section*> sections;
|
||||||
|
|
||||||
static Rsrc* rsrc_root;
|
static Rsrc* rsrc_root;
|
||||||
|
|
||||||
@@ -259,7 +259,7 @@ static Rsrc* findRsrc(int type, int id, int lang)
|
|||||||
return findRsrc(lang, r);
|
return findRsrc(lang, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void loadImage(istream& in)
|
static void loadImage(std::istream& in)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
@@ -305,7 +305,7 @@ static void loadImage(istream& in)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void saveImage(ostream& out)
|
static void saveImage(std::ostream& out)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
@@ -337,7 +337,7 @@ bool openImage(const char* img)
|
|||||||
{
|
{
|
||||||
img_file = img;
|
img_file = img;
|
||||||
|
|
||||||
fstream in(img_file, ios_base::binary | ios_base::in);
|
std::fstream in(img_file, std::ios_base::binary | std::ios_base::in);
|
||||||
loadImage(in);
|
loadImage(in);
|
||||||
in.close();
|
in.close();
|
||||||
return true;
|
return true;
|
||||||
@@ -384,7 +384,7 @@ void closeImage()
|
|||||||
if (!img_file)
|
if (!img_file)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fstream out(img_file, ios_base::binary | ios_base::out | ios_base::trunc);
|
std::fstream out(img_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
|
||||||
saveImage(out);
|
saveImage(out);
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
#ifndef IMAGE_UTIL_H
|
|
||||||
#define IMAGE_UTIL_H
|
|
||||||
|
|
||||||
bool openImage(const char* img);
|
bool openImage(const char* img);
|
||||||
bool makeExe(int entry);
|
bool makeExe(int entry);
|
||||||
bool replaceRsrc(int type, int id, int land, void* data, int data_sz);
|
bool replaceRsrc(int type, int id, int land, void* data, int data_sz);
|
||||||
void closeImage();
|
void closeImage();
|
||||||
|
|
||||||
#endif
|
|
||||||
+25
-29
@@ -1,14 +1,19 @@
|
|||||||
|
|
||||||
#include "linker.hpp"
|
#include "linker.hpp"
|
||||||
#include "image_util.hpp"
|
#include "image_util.hpp"
|
||||||
#include "std.hpp"
|
#include <istream>
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <streambuf>
|
||||||
|
|
||||||
using namespace std;
|
#include <config.hpp>
|
||||||
|
#include <stdutil.hpp>
|
||||||
|
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
class BBModule : public Module {
|
class BBModule : public Module {
|
||||||
public:
|
public:
|
||||||
BBModule();
|
BBModule();
|
||||||
BBModule(istream& in);
|
BBModule(std::istream& in);
|
||||||
~BBModule();
|
~BBModule();
|
||||||
|
|
||||||
void* link(Module* libs);
|
void* link(Module* libs);
|
||||||
@@ -30,16 +35,16 @@ class BBModule : public Module {
|
|||||||
int data_sz, pc;
|
int data_sz, pc;
|
||||||
bool linked;
|
bool linked;
|
||||||
|
|
||||||
map<string, int> symbols;
|
std::map<std::string, int> symbols;
|
||||||
map<int, string> rel_relocs, abs_relocs;
|
std::map<int, std::string> rel_relocs, abs_relocs;
|
||||||
|
|
||||||
bool findSym(const string& t, Module* libs, int* n)
|
bool findSym(const std::string& t, Module* libs, int* n)
|
||||||
{
|
{
|
||||||
if (findSymbol(t.c_str(), n))
|
if (findSymbol(t.c_str(), n))
|
||||||
return true;
|
return true;
|
||||||
if (libs->findSymbol(t.c_str(), n))
|
if (libs->findSymbol(t.c_str(), n))
|
||||||
return true;
|
return true;
|
||||||
string err = "Symbol '" + t + "' not found";
|
std::string err = "Symbol '" + t + "' not found";
|
||||||
MessageBox(GetDesktopWindow(), err.c_str(), "Blitz Linker Error", MB_TOPMOST | MB_SETFOREGROUND);
|
MessageBox(GetDesktopWindow(), err.c_str(), "Blitz Linker Error", MB_TOPMOST | MB_SETFOREGROUND);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -74,7 +79,7 @@ void* BBModule::link(Module* libs)
|
|||||||
return data;
|
return data;
|
||||||
|
|
||||||
int dest;
|
int dest;
|
||||||
map<int, string>::iterator it;
|
std::map<int, std::string>::iterator it;
|
||||||
|
|
||||||
char* p = (char*)VirtualAlloc(0, pc, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
|
char* p = (char*)VirtualAlloc(0, pc, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
|
||||||
memcpy(p, data, pc);
|
memcpy(p, data, pc);
|
||||||
@@ -134,7 +139,7 @@ void BBModule::emitx(void* mem, int sz)
|
|||||||
|
|
||||||
bool BBModule::addSymbol(const char* sym, int pc)
|
bool BBModule::addSymbol(const char* sym, int pc)
|
||||||
{
|
{
|
||||||
string t(sym);
|
std::string t(sym);
|
||||||
if (symbols.find(t) != symbols.end())
|
if (symbols.find(t) != symbols.end())
|
||||||
return false;
|
return false;
|
||||||
symbols[t] = pc;
|
symbols[t] = pc;
|
||||||
@@ -143,17 +148,17 @@ bool BBModule::addSymbol(const char* sym, int pc)
|
|||||||
|
|
||||||
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;
|
std::map<int, std::string>& rel = pcrel ? rel_relocs : abs_relocs;
|
||||||
if (rel.find(pc) != rel.end())
|
if (rel.find(pc) != rel.end())
|
||||||
return false;
|
return false;
|
||||||
rel[pc] = string(dest_sym);
|
rel[pc] = std::string(dest_sym);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BBModule::findSymbol(const char* sym, int* pc)
|
bool BBModule::findSymbol(const char* sym, int* pc)
|
||||||
{
|
{
|
||||||
string t = string(sym);
|
std::string t = std::string(sym);
|
||||||
map<string, int>::iterator it = symbols.find(t);
|
std::map<std::string, int>::iterator it = symbols.find(t);
|
||||||
if (it == symbols.end())
|
if (it == symbols.end())
|
||||||
return false;
|
return false;
|
||||||
*pc = it->second + (int)data;
|
*pc = it->second + (int)data;
|
||||||
@@ -167,11 +172,7 @@ int Linker::version()
|
|||||||
|
|
||||||
bool Linker::canCreateExe()
|
bool Linker::canCreateExe()
|
||||||
{
|
{
|
||||||
#ifdef DEMO
|
|
||||||
return false;
|
|
||||||
#else
|
|
||||||
return true;
|
return true;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Module* Linker::createModule()
|
Module* Linker::createModule()
|
||||||
@@ -192,10 +193,6 @@ Linker* _cdecl linkerGetLinker()
|
|||||||
|
|
||||||
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
|
//find proc address of bbWinMain
|
||||||
HMODULE hmod = LoadLibrary(dll_file);
|
HMODULE hmod = LoadLibrary(dll_file);
|
||||||
if (!hmod)
|
if (!hmod)
|
||||||
@@ -221,10 +218,10 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
|
|||||||
//num_abss: name,val...
|
//num_abss: name,val...
|
||||||
//
|
//
|
||||||
qstreambuf buf;
|
qstreambuf buf;
|
||||||
iostream out(&buf);
|
std::iostream out(&buf);
|
||||||
|
|
||||||
map<string, int>::iterator it;
|
std::map<std::string, int>::iterator it;
|
||||||
map<int, string>::iterator rit;
|
std::map<int, std::string>::iterator rit;
|
||||||
|
|
||||||
//write the code
|
//write the code
|
||||||
int sz = pc;
|
int sz = pc;
|
||||||
@@ -235,7 +232,7 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
|
|||||||
sz = symbols.size();
|
sz = symbols.size();
|
||||||
out.write((char*)&sz, 4);
|
out.write((char*)&sz, 4);
|
||||||
for (it = symbols.begin(); it != symbols.end(); ++it) {
|
for (it = symbols.begin(); it != symbols.end(); ++it) {
|
||||||
string t = it->first + '\0';
|
std::string t = it->first + '\0';
|
||||||
out.write(t.data(), t.size());
|
out.write(t.data(), t.size());
|
||||||
sz = it->second;
|
sz = it->second;
|
||||||
out.write((char*)&sz, 4);
|
out.write((char*)&sz, 4);
|
||||||
@@ -245,7 +242,7 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
|
|||||||
sz = rel_relocs.size();
|
sz = rel_relocs.size();
|
||||||
out.write((char*)&sz, 4);
|
out.write((char*)&sz, 4);
|
||||||
for (rit = rel_relocs.begin(); rit != rel_relocs.end(); ++rit) {
|
for (rit = rel_relocs.begin(); rit != rel_relocs.end(); ++rit) {
|
||||||
string t = rit->second + '\0';
|
std::string t = rit->second + '\0';
|
||||||
out.write(t.data(), t.size());
|
out.write(t.data(), t.size());
|
||||||
sz = rit->first;
|
sz = rit->first;
|
||||||
out.write((char*)&sz, 4);
|
out.write((char*)&sz, 4);
|
||||||
@@ -255,7 +252,7 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
|
|||||||
sz = abs_relocs.size();
|
sz = abs_relocs.size();
|
||||||
out.write((char*)&sz, 4);
|
out.write((char*)&sz, 4);
|
||||||
for (rit = abs_relocs.begin(); rit != abs_relocs.end(); ++rit) {
|
for (rit = abs_relocs.begin(); rit != abs_relocs.end(); ++rit) {
|
||||||
string t = rit->second + '\0';
|
std::string t = rit->second + '\0';
|
||||||
out.write(t.data(), t.size());
|
out.write(t.data(), t.size());
|
||||||
sz = rit->first;
|
sz = rit->first;
|
||||||
out.write((char*)&sz, 4);
|
out.write((char*)&sz, 4);
|
||||||
@@ -266,5 +263,4 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
|
|||||||
closeImage();
|
closeImage();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
#include "std.hpp"
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
#pragma once
|
|
||||||
#include <fstream>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <iostream>
|
|
||||||
#include <list>
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include "stdutil.hpp"
|
|
||||||
|
|
||||||
#include <windows.h>
|
|
||||||
Reference in New Issue
Block a user