From bf8c8f9dad33ff377710b8248ed30d82025c6e0e Mon Sep 17 00:00:00 2001 From: Kasper Date: Tue, 18 Mar 2025 19:22:43 +0200 Subject: [PATCH] Fixing merge conflict resolution --- CMakeLists.txt | 2 ++ include/hiload/hiload.h | 1 + src/files.c | 15 +++++++++++ src/files.h | 9 +++++++ src/hiload.c | 54 ++++++++++++++++++++++++-------------- src/memory.c | 14 ++++++++++ src/memory.h | 28 ++++++++++++++++++++ src/types.h | 19 ++++++++++++++ test/manual/CMakeLists.txt | 7 ----- test/manual/minimal.cpp | 1 - 10 files changed, 123 insertions(+), 27 deletions(-) create mode 100644 src/files.c create mode 100644 src/files.h create mode 100644 src/memory.c create mode 100644 src/memory.h create mode 100644 src/types.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 839297d..dc40a17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,8 @@ endif() add_library(hiload SHARED src/hiload.c src/symbols.c + src/files.c + src/memory.c # dependencies src/str.c diff --git a/include/hiload/hiload.h b/include/hiload/hiload.h index 61e010b..04dc457 100644 --- a/include/hiload/hiload.h +++ b/include/hiload/hiload.h @@ -29,6 +29,7 @@ void hi_print_module_infos(); ReloadResult hi_reload_module(const char *module_name); #pragma GCC visibility pop + #ifdef __cplusplus } #endif diff --git a/src/files.c b/src/files.c new file mode 100644 index 0000000..8a69622 --- /dev/null +++ b/src/files.c @@ -0,0 +1,15 @@ +#include "files.h" + +#include "logger.h" +#include "str.h" +#include "types.h" + +HiloadResult read_file_to_str(str *s, const char *filename) { + + int copied = str_from_file(s, filename); + if (copied == 0) { + sc_log_error("Failed to read file: %s\n", filename); + return HILOAD_FAIL; + } + return HILOAD_OK; +} diff --git a/src/files.h b/src/files.h new file mode 100644 index 0000000..cd8d3b9 --- /dev/null +++ b/src/files.h @@ -0,0 +1,9 @@ +#ifndef FILES_H_ +#define FILES_H_ + +#include "str.h" +#include "types.h" + +HiloadResult read_file_to_str(str *s, const char *); + +#endif // FILES_H_ diff --git a/src/hiload.c b/src/hiload.c index a7836ce..7ea7516 100644 --- a/src/hiload.c +++ b/src/hiload.c @@ -1,4 +1,7 @@ #include "hiload/hiload.h" + +#include "logger.h" +#include "memory.h" #include "symbols.h" #include @@ -51,8 +54,10 @@ static int gather_module_infos_callback(struct dl_phdr_info *info, size_t size, infos->handles[infos->count] = dlopen(info->dlpi_name, RTLD_LAZY | RTLD_NOLOAD); - if (hi_create_symbol_info(&infos->symbols[infos->count], info) != CREATE_SUCCESS) { - fprintf(stderr, "Failed to create symbol info for %s\n", infos->names[infos->count]); + if (hi_create_symbol_info(&infos->symbols[infos->count], info) != + CREATE_SUCCESS) { + fprintf(stderr, "Failed to create symbol info for %s\n", + infos->names[infos->count]); } infos->count++; return 0; // Continue iteration @@ -110,23 +115,6 @@ static ModuleInfos *gather_shared_libraries(void) { return result; } -int hi_init() { - assert(!module_infos); - - ModuleInfos *infos = gather_shared_libraries(); - if (!infos) { - fprintf(stderr, "Failed to gather module infos.\n"); - return 1; - } - if (module_infos) { - free_module_infos(module_infos); - } - module_infos = infos; - return 0; -} - -void hi_deinit() { free_module_infos(module_infos); } - /** * Reloads a shared library module * @@ -261,3 +249,31 @@ void hi_print_module_infos() { printf("\n"); } } + +int hi_init() { + assert(!module_infos); + + if (sc_log_init() != 0) { + fprintf(stderr, "Failed to init logger.\n"); + return 1; + } + sc_log_set_level("DEBUG"); + + read_memory_maps_self(); + + ModuleInfos *infos = gather_shared_libraries(); + if (!infos) { + fprintf(stderr, "Failed to gather module infos.\n"); + return 1; + } + if (module_infos) { + free_module_infos(module_infos); + } + module_infos = infos; + return 0; +} + +void hi_deinit() { + free_module_infos(module_infos); + sc_log_term(); +} diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..8df883d --- /dev/null +++ b/src/memory.c @@ -0,0 +1,14 @@ +#include "memory.h" + +#include "logger.h" +#include "files.h" +#include "types.h" + +str read_memory_maps_self() { + str memory_str = str_null; + + HiloadResult res = read_file_to_str(&memory_str, "/proc/self/maps"); + sc_log_debug("Memory Map (/proc/self/maps):\n%s\n", str_ptr(memory_str)); + + return memory_str; +} diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..6b03588 --- /dev/null +++ b/src/memory.h @@ -0,0 +1,28 @@ +#ifndef MEMORY_H_ +#define MEMORY_H_ + +#include "str.h" +#include "types.h" +#include "array.h" + +enum MemoryPermissions { + HI_MEMORY_READ = 1 << 0, + HI_MEMORY_WRITE = 1 << 1, + HI_MEMORY_EXECUTE = 1 << 2, + HI_MEMORY_SHARED = 1 << 3, + HI_MEMORY_PRIVATE = 1 << 4 +}; + +typedef struct { + void *region_start; + void *redion_end; + u32 region_flags; // enum MemoryPermissions + u32 offset; + str pathname; +} MemoryRegion; + +str read_memory_maps_self(); + +sc_array_def(MemoryRegion, memreg); + +#endif // MEMORY_H_ diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..08c03a1 --- /dev/null +++ b/src/types.h @@ -0,0 +1,19 @@ +#ifndef TYPES_H_ +#define TYPES_H_ + +#include + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef float f32; +typedef double f64; + +typedef enum { HILOAD_OK = 0, HILOAD_FAIL } HiloadResult; + +#endif // TYPES_H_ diff --git a/test/manual/CMakeLists.txt b/test/manual/CMakeLists.txt index 9e245f3..064e2eb 100644 --- a/test/manual/CMakeLists.txt +++ b/test/manual/CMakeLists.txt @@ -11,11 +11,4 @@ add_library(mini SHARED ) -# find_library(HILOAD_LIB NAMES hiload PATHS ${CMAKE_BINARY_DIR}) -# if (${HILOAD_LIB} STREQUAL 'HILOAD_LIB-NOTFOUND') -# message(FATAL_ERROR "Couldn't find hiload library") -# endif() - -# target_link_libraries(minimal mini ${HILOAD_LIB}) - target_link_libraries(minimal mini hiload) diff --git a/test/manual/minimal.cpp b/test/manual/minimal.cpp index 4d9990f..e243f24 100644 --- a/test/manual/minimal.cpp +++ b/test/manual/minimal.cpp @@ -1,6 +1,5 @@ #include "minimal_lib.h" -// We're not installing anything, so this is a bit weird here #include "../../include/hiload/hiload.h" #include