From dd78f5309aef0a70024c9f6c00c1283a64dc3ef0 Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sun, 3 Apr 2022 16:38:58 -0700 Subject: [PATCH] added spdlog, started to dev on 3.0 now... still need to workout relocs... --- .gitmodules | 3 +++ CMakeLists.txt | 1 + cmake.toml | 2 +- dependencies/CMakeLists.txt | 10 ++++++++++ dependencies/cmake.toml | 4 +++- dependencies/spdlog | 1 + include/comp/comp.hpp | 33 +++++++++++++++++---------------- include/comp/symbol_table.hpp | 2 ++ include/decomp/decomp.hpp | 19 ++++++++++++------- include/decomp/routine.hpp | 8 +++----- include/theo.hpp | 26 +++++++++++++++++++++++++- src/tests/demo/CMakeLists.txt | 1 + src/tests/demo/cmake.toml | 2 +- src/tests/demo/main.cpp | 33 +++++++++++++++++++++++++++++++++ src/theo/comp/comp.hpp | 0 src/theo/comp/symbol_table.hpp | 0 src/theo/decomp/decomp.hpp | 0 src/theo/decomp/routine.hpp | 0 src/theo/decomp/symbol.hpp | 0 src/theo/theo.cpp | 18 ++++++++++++++++++ 20 files changed, 131 insertions(+), 32 deletions(-) create mode 160000 dependencies/spdlog create mode 100644 src/theo/comp/comp.hpp create mode 100644 src/theo/comp/symbol_table.hpp create mode 100644 src/theo/decomp/decomp.hpp create mode 100644 src/theo/decomp/routine.hpp create mode 100644 src/theo/decomp/symbol.hpp diff --git a/.gitmodules b/.gitmodules index f1523d6..2e27b33 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "dependencies/mbuild"] path = dependencies/mbuild url = https://github.com/intelxed/mbuild.git +[submodule "dependencies/spdlog"] + path = dependencies/spdlog + url = https://github.com/gabime/spdlog.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 3878a57..2a3c36f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ target_include_directories(Theodosius PUBLIC target_link_libraries(Theodosius PUBLIC linux-pe + spdlog ) unset(CMKR_TARGET) diff --git a/cmake.toml b/cmake.toml index 1c9f097..2de60c6 100644 --- a/cmake.toml +++ b/cmake.toml @@ -11,4 +11,4 @@ sources = ["include/**.hpp", "src/theo/**.cpp"] include-directories = ["include"] compile-features = ["cxx_std_20"] compile-definitions = ["NOMINMAX"] -link-libraries = ["linux-pe"] \ No newline at end of file +link-libraries = ["linux-pe", "spdlog"] \ No newline at end of file diff --git a/dependencies/CMakeLists.txt b/dependencies/CMakeLists.txt index ba1e60d..8ecc545 100644 --- a/dependencies/CMakeLists.txt +++ b/dependencies/CMakeLists.txt @@ -6,6 +6,16 @@ if(CMKR_ROOT_PROJECT) configure_file(cmake.toml cmake.toml COPYONLY) endif() +# spdlog +set(CMKR_CMAKE_FOLDER ${CMAKE_FOLDER}) +if(CMAKE_FOLDER) + set(CMAKE_FOLDER "${CMAKE_FOLDER}/spdlog") +else() + set(CMAKE_FOLDER spdlog) +endif() +add_subdirectory(spdlog) +set(CMAKE_FOLDER ${CMKR_CMAKE_FOLDER}) + # Target linux-pe set(CMKR_TARGET linux-pe) set(linux-pe_SOURCES "") diff --git a/dependencies/cmake.toml b/dependencies/cmake.toml index 354c425..5085f79 100644 --- a/dependencies/cmake.toml +++ b/dependencies/cmake.toml @@ -1,3 +1,5 @@ [target.linux-pe] type = "interface" -include-directories = ["linux-pe/includes/"] \ No newline at end of file +include-directories = ["linux-pe/includes/"] + +[subdir.spdlog] \ No newline at end of file diff --git a/dependencies/spdlog b/dependencies/spdlog new file mode 160000 index 0000000..fc51c09 --- /dev/null +++ b/dependencies/spdlog @@ -0,0 +1 @@ +Subproject commit fc51c095bae108490bf62c3345a5e3b37a5e7630 diff --git a/include/comp/comp.hpp b/include/comp/comp.hpp index 308beb2..b4de1af 100644 --- a/include/comp/comp.hpp +++ b/include/comp/comp.hpp @@ -3,28 +3,29 @@ #include namespace theo::comp { -using resolve_t = std::function; -using copy_t = std::function; -using alloc_t = std::function; +using resolver_t = std::function; +using copier_t = std::function; +using allocator_t = + std::function; class comp_t { public: - explicit comp_t(decomp::decomp_t& dcmp, comp::symbol_table_t syms); - explicit comp_t(decomp::decomp_t& dcmp, - comp::symbol_table_t syms, - alloc_t alloc, - copy_t copy, - resolve_t resolve); + explicit comp_t(decomp::decomp_t* dcmp); + explicit comp_t(decomp::decomp_t* dcmp, + allocator_t alloc, + copier_t copy, + resolver_t resolve); - void allocator(alloc_t alloc); - void copier(copy_t copy); - void resolver(resolve_t resolve); + void allocator(allocator_t alloc); + void copier(copier_t copy); + void resolver(resolver_t resolve); std::uintptr_t resolve(std::string&& sym); private: - resolve_t m_resolver; - copy_t m_copier; - alloc_t m_allocator; + decomp::decomp_t* m_dcmp; + resolver_t m_resolver; + copier_t m_copier; + allocator_t m_allocator; }; } // namespace theo::comp \ No newline at end of file diff --git a/include/comp/symbol_table.hpp b/include/comp/symbol_table.hpp index 584254f..d67d210 100644 --- a/include/comp/symbol_table.hpp +++ b/include/comp/symbol_table.hpp @@ -18,6 +18,8 @@ class symbol_table_t { void update(std::string& name, std::uintptr_t location); void for_each(std::function fn); + std::uint32_t size(); + private: std::map m_table; }; diff --git a/include/decomp/decomp.hpp b/include/decomp/decomp.hpp index a39badf..c1da28a 100644 --- a/include/decomp/decomp.hpp +++ b/include/decomp/decomp.hpp @@ -3,22 +3,27 @@ #include #include #include +#include -#include #include +#include namespace theo::decomp { class decomp_t { public: - explicit decomp_t(std::vector& lib_data); - std::vector& rtns(); - std::vector& lib(); - std::vector& objs(); + explicit decomp_t(std::vector& lib_data, + comp::symbol_table_t* syms); + + std::vector rtns(); + std::vector lib(); + std::vector objs(); + comp::symbol_table_t* syms(); + std::optional decompose(); private: - void decompose(); std::vector m_lib_data; std::vector m_obj_imgs; - std::vector m_rtns; + std::vector m_rtns; + comp::symbol_table_t* m_syms; }; } // namespace theo::decomp \ No newline at end of file diff --git a/include/decomp/routine.hpp b/include/decomp/routine.hpp index f72ac34..95f1341 100644 --- a/include/decomp/routine.hpp +++ b/include/decomp/routine.hpp @@ -6,18 +6,16 @@ #include "symbol.hpp" namespace theo::decomp { -class routine { +class routine_t { public: - explicit routine(coff::section_header_t scn_hdr, + explicit routine_t(coff::section_header_t scn_hdr, std::vector& rtn_data); - std::vector syms(); + std::vector decompose(); coff::section_header_t scn_hdr(); std::vector data(); private: - void decompose(); std::vector m_data; - std::vector m_orig_syms; coff::section_header_t m_scn_hdr; }; } // namespace theo::decomp \ No newline at end of file diff --git a/include/theo.hpp b/include/theo.hpp index 819e64a..1eea6e2 100644 --- a/include/theo.hpp +++ b/include/theo.hpp @@ -1,3 +1,27 @@ #pragma once #include -#include \ No newline at end of file +#include +#include + +#include +#include +#include + +namespace theo { +using lnk_fns_t = + std::tuple; + +class theo_t { + public: + explicit theo_t( + std::vector& lib, lnk_fns_t lnkr_fns); + + std::optional decompose(); + std::uintptr_t compose(const std::string&& entry_sym); + std::uintptr_t resolve(const std::string&& sym); + private: + decomp::decomp_t m_dcmp; + comp::comp_t m_cmp; + comp::symbol_table_t m_sym_tbl; +}; +} // namespace theo \ No newline at end of file diff --git a/src/tests/demo/CMakeLists.txt b/src/tests/demo/CMakeLists.txt index 1389251..67e059b 100644 --- a/src/tests/demo/CMakeLists.txt +++ b/src/tests/demo/CMakeLists.txt @@ -57,6 +57,7 @@ source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${usermode-demo_SOURCES}) target_link_libraries(usermode-demo PRIVATE Theodosius + spdlog ) unset(CMKR_TARGET) diff --git a/src/tests/demo/cmake.toml b/src/tests/demo/cmake.toml index bfb83b1..d7b90fc 100644 --- a/src/tests/demo/cmake.toml +++ b/src/tests/demo/cmake.toml @@ -5,4 +5,4 @@ name = "usermode-demo" type = "executable" sources = ["*.cpp"] -link-libraries = ["Theodosius"] \ No newline at end of file +link-libraries = ["Theodosius", "spdlog"] \ No newline at end of file diff --git a/src/tests/demo/main.cpp b/src/tests/demo/main.cpp index e69de29..590cbcf 100644 --- a/src/tests/demo/main.cpp +++ b/src/tests/demo/main.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include +#include + +namespace fs = std::filesystem; + +int main(int argc, char* argv[]) { + spdlog::info("this is info..."); + spdlog::error("this is an error..."); + + if (argc < 2) + return -1; + + // read in lib file... + std::ifstream f(argv[1], std::ios::binary); + auto fsize = fs::file_size(fs::path(argv[1])); + std::vector fdata; + fdata.resize(fsize); + f.read((char*)fdata.data(), fsize); + + theo::theo_t t(fdata, {}); + auto res = t.decompose(); + + if (!res.has_value()) { + spdlog::error("decomposition failed...\n"); + return -1; + } + + spdlog::info("decomposed {1} symbols...", res.value()); +} \ No newline at end of file diff --git a/src/theo/comp/comp.hpp b/src/theo/comp/comp.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/theo/comp/symbol_table.hpp b/src/theo/comp/symbol_table.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/theo/decomp/decomp.hpp b/src/theo/decomp/decomp.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/theo/decomp/routine.hpp b/src/theo/decomp/routine.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/theo/decomp/symbol.hpp b/src/theo/decomp/symbol.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/theo/theo.cpp b/src/theo/theo.cpp index e69de29..f263bc2 100644 --- a/src/theo/theo.cpp +++ b/src/theo/theo.cpp @@ -0,0 +1,18 @@ +#include + +namespace theo { +theo_t::theo_t(std::vector& lib, lnk_fns_t lnkr_fns) + : m_dcmp(lib, &m_sym_tbl), m_cmp(&m_dcmp) {} + +std::optional theo_t::decompose() { + auto res = m_dcmp.decompose(); + + if (!res.has_value()) { + spdlog::error("failed to decompose...\n"); + return {}; + } + + spdlog::info("decompose successful... {1} symbols", res.value()->size()); + return res.value()->size(); +} +} // namespace theo \ No newline at end of file