linker/lib: Modernize code base and remote std.c/hpp

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2019-01-19 18:29:24 +01:00
parent 10f664ce85
commit ccc0d14b66
8 changed files with 45 additions and 75 deletions
+1 -3
View File
@@ -1,14 +1,12 @@
project(linker_lib)
add_library(${PROJECT_NAME} SHARED
add_library(${PROJECT_NAME} STATIC
"dlltoexe.cpp"
"dlltoexe.hpp"
"image_util.cpp"
"image_util.hpp"
"linker.cpp"
"linker.hpp"
"std.cpp"
"std.hpp"
)
target_link_libraries(${PROJECT_NAME}
+4 -5
View File
@@ -1,8 +1,7 @@
#include "dlltoexe.hpp"
#include "std.hpp"
#include <fstream>
using namespace std;
#include <Windows.h>
#pragma pack(push, 1)
struct Head {
@@ -41,10 +40,10 @@ bool dllToExe(const char* exe_file, const char* dll_file, const char* entry_func
return false;
//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())
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())
return false;
+1 -6
View File
@@ -1,7 +1,2 @@
#ifndef DLLTOEXE_H
#define DLLTOEXE_H
#pragma once
bool dllToExe(const char* exe_file, const char* dll_file, const char* entry);
#endif
+10 -10
View File
@@ -1,8 +1,8 @@
#include "image_util.hpp"
#include "std.hpp"
using namespace std;
#include <fstream>
#include <istream>
#include <ostream>
#include <vector>
#ifndef DEMO
@@ -64,7 +64,7 @@ struct Rsrc {
int id;
void* data;
int data_sz;
vector<Rsrc*> kids;
std::vector<Rsrc*> kids;
Rsrc(int id, Rsrc* p) : id(id), data(0), data_sz(0)
{
@@ -104,7 +104,7 @@ static int opts_sz;
static DDir* ddir;
static int ddir_sz;
static vector<Section*> sections;
static std::vector<Section*> sections;
static Rsrc* rsrc_root;
@@ -259,7 +259,7 @@ static Rsrc* findRsrc(int type, int id, int lang)
return findRsrc(lang, r);
}
static void loadImage(istream& in)
static void loadImage(std::istream& in)
{
int k;
@@ -305,7 +305,7 @@ static void loadImage(istream& in)
}
}
static void saveImage(ostream& out)
static void saveImage(std::ostream& out)
{
int k;
@@ -337,7 +337,7 @@ bool openImage(const char* 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);
in.close();
return true;
@@ -384,7 +384,7 @@ void closeImage()
if (!img_file)
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);
out.close();
+1 -5
View File
@@ -1,10 +1,6 @@
#ifndef IMAGE_UTIL_H
#define IMAGE_UTIL_H
#pragma once
bool openImage(const char* img);
bool makeExe(int entry);
bool replaceRsrc(int type, int id, int land, void* data, int data_sz);
void closeImage();
#endif
+25 -29
View File
@@ -1,14 +1,19 @@
#include "linker.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 {
public:
BBModule();
BBModule(istream& in);
BBModule(std::istream& in);
~BBModule();
void* link(Module* libs);
@@ -30,16 +35,16 @@ class BBModule : public Module {
int data_sz, pc;
bool linked;
map<string, int> symbols;
map<int, string> rel_relocs, abs_relocs;
std::map<std::string, int> symbols;
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))
return true;
if (libs->findSymbol(t.c_str(), n))
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);
return false;
}
@@ -74,7 +79,7 @@ void* BBModule::link(Module* libs)
return data;
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);
memcpy(p, data, pc);
@@ -134,7 +139,7 @@ void BBModule::emitx(void* mem, int sz)
bool BBModule::addSymbol(const char* sym, int pc)
{
string t(sym);
std::string t(sym);
if (symbols.find(t) != symbols.end())
return false;
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)
{
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())
return false;
rel[pc] = string(dest_sym);
rel[pc] = std::string(dest_sym);
return true;
}
bool BBModule::findSymbol(const char* sym, int* pc)
{
string t = string(sym);
map<string, int>::iterator it = symbols.find(t);
std::string t = std::string(sym);
std::map<std::string, int>::iterator it = symbols.find(t);
if (it == symbols.end())
return false;
*pc = it->second + (int)data;
@@ -167,11 +172,7 @@ int Linker::version()
bool Linker::canCreateExe()
{
#ifdef DEMO
return false;
#else
return true;
#endif
}
Module* Linker::createModule()
@@ -192,10 +193,6 @@ Linker* _cdecl linkerGetLinker()
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)
@@ -221,10 +218,10 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
//num_abss: name,val...
//
qstreambuf buf;
iostream out(&buf);
std::iostream out(&buf);
map<string, int>::iterator it;
map<int, string>::iterator rit;
std::map<std::string, int>::iterator it;
std::map<int, std::string>::iterator rit;
//write the code
int sz = pc;
@@ -235,7 +232,7 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
sz = symbols.size();
out.write((char*)&sz, 4);
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());
sz = it->second;
out.write((char*)&sz, 4);
@@ -245,7 +242,7 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
sz = rel_relocs.size();
out.write((char*)&sz, 4);
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());
sz = rit->first;
out.write((char*)&sz, 4);
@@ -255,7 +252,7 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
sz = abs_relocs.size();
out.write((char*)&sz, 4);
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());
sz = rit->first;
out.write((char*)&sz, 4);
@@ -266,5 +263,4 @@ bool BBModule::createExe(const char* exe_file, const char* dll_file)
closeImage();
return true;
#endif
}
-2
View File
@@ -1,2 +0,0 @@
#include "std.hpp"
-12
View File
@@ -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>