From ccc0d14b66d5fc0b3404e177a6c11ca3eebb2908 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sat, 19 Jan 2019 18:29:24 +0100 Subject: [PATCH] linker/lib: Modernize code base and remote std.c/hpp --- Linker/lib/CMakeLists.txt | 4 +-- Linker/lib/dlltoexe.cpp | 9 +++---- Linker/lib/dlltoexe.hpp | 7 +---- Linker/lib/image_util.cpp | 26 +++++++++---------- Linker/lib/image_util.hpp | 6 +---- Linker/lib/linker.cpp | 54 ++++++++++++++++++--------------------- Linker/lib/std.cpp | 2 -- Linker/lib/std.hpp | 12 --------- 8 files changed, 45 insertions(+), 75 deletions(-) delete mode 100644 Linker/lib/std.cpp delete mode 100644 Linker/lib/std.hpp diff --git a/Linker/lib/CMakeLists.txt b/Linker/lib/CMakeLists.txt index 5eba799..e720fcb 100644 --- a/Linker/lib/CMakeLists.txt +++ b/Linker/lib/CMakeLists.txt @@ -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} diff --git a/Linker/lib/dlltoexe.cpp b/Linker/lib/dlltoexe.cpp index c230983..d25e7ac 100644 --- a/Linker/lib/dlltoexe.cpp +++ b/Linker/lib/dlltoexe.cpp @@ -1,8 +1,7 @@ - #include "dlltoexe.hpp" -#include "std.hpp" +#include -using namespace std; +#include #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; diff --git a/Linker/lib/dlltoexe.hpp b/Linker/lib/dlltoexe.hpp index dbe6c00..0ded15a 100644 --- a/Linker/lib/dlltoexe.hpp +++ b/Linker/lib/dlltoexe.hpp @@ -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 \ No newline at end of file diff --git a/Linker/lib/image_util.cpp b/Linker/lib/image_util.cpp index aeac11e..35b43f5 100644 --- a/Linker/lib/image_util.cpp +++ b/Linker/lib/image_util.cpp @@ -1,8 +1,8 @@ - #include "image_util.hpp" -#include "std.hpp" - -using namespace std; +#include +#include +#include +#include #ifndef DEMO @@ -61,10 +61,10 @@ struct Rdat { #pragma pack(pop) struct Rsrc { - int id; - void* data; - int data_sz; - vector kids; + int id; + void* data; + int data_sz; + std::vector 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 sections; +static std::vector 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(); diff --git a/Linker/lib/image_util.hpp b/Linker/lib/image_util.hpp index f824914..b6d08d2 100644 --- a/Linker/lib/image_util.hpp +++ b/Linker/lib/image_util.hpp @@ -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 \ No newline at end of file diff --git a/Linker/lib/linker.cpp b/Linker/lib/linker.cpp index c1d500f..cadc8d9 100644 --- a/Linker/lib/linker.cpp +++ b/Linker/lib/linker.cpp @@ -1,14 +1,19 @@ - #include "linker.hpp" #include "image_util.hpp" -#include "std.hpp" +#include +#include +#include +#include -using namespace std; +#include +#include + +#include 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 symbols; - map rel_relocs, abs_relocs; + std::map symbols; + std::map 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::iterator it; + std::map::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& rel = pcrel ? rel_relocs : abs_relocs; + std::map& 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::iterator it = symbols.find(t); + std::string t = std::string(sym); + std::map::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::iterator it; - map::iterator rit; + std::map::iterator it; + std::map::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 } diff --git a/Linker/lib/std.cpp b/Linker/lib/std.cpp deleted file mode 100644 index d5f561b..0000000 --- a/Linker/lib/std.cpp +++ /dev/null @@ -1,2 +0,0 @@ - -#include "std.hpp" diff --git a/Linker/lib/std.hpp b/Linker/lib/std.hpp deleted file mode 100644 index bf5ee24..0000000 --- a/Linker/lib/std.hpp +++ /dev/null @@ -1,12 +0,0 @@ - -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include "stdutil.hpp" - -#include