This commit is contained in:
2025-03-18 14:19:32 +02:00
parent db4b51ab2d
commit 97c5ff206a
4 changed files with 33 additions and 31 deletions

View File

@@ -7,16 +7,18 @@ extern "C" {
// Return codes for reload_module
typedef enum {
RELOAD_SUCCESS = 0,
RELOAD_NOT_FOUND,
RELOAD_CLOSE_ERROR,
RELOAD_OPEN_ERROR
HI_RELOAD_SUCCESS = 0,
HI_RELOAD_NOT_FOUND,
HI_RELOAD_CLOSE_ERROR,
HI_RELOAD_OPEN_ERROR
} ReloadResult;
int he_init();
void he_deinit();
void he_print_module_infos();
ReloadResult he_reload_module(const char *module_name);
/* Initialiez the module. Must be called before anything else */
int hi_init();
/* Frees memory and cleans up */
void hi_deinit();
void hi_print_module_infos();
ReloadResult hi_reload_module(const char *module_name);
#ifdef __cplusplus
}

View File

@@ -1,5 +1,5 @@
#include "hiload/hiload.h"
#include "hiload/symbols.h"
#include "symbols.h"
#include <assert.h>
#include <dlfcn.h>
@@ -51,7 +51,7 @@ 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 (he_create_symbol_info(&infos->symbols[infos->count], info) != CREATE_SUCCESS) {
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++;
@@ -65,7 +65,7 @@ static void free_module_infos(ModuleInfos *modules) {
for (size_t i = 0; i < modules->count; i++) {
if (modules->names[i])
free(modules->names[i]);
he_free_symbol_info(&modules->symbols[i]);
hi_free_symbol_info(&modules->symbols[i]);
}
free(modules->names);
@@ -110,7 +110,7 @@ static ModuleInfos *gather_shared_libraries(void) {
return result;
}
int he_init() {
int hi_init() {
assert(!module_infos);
ModuleInfos *infos = gather_shared_libraries();
@@ -125,7 +125,7 @@ int he_init() {
return 0;
}
void he_deinit() { free_module_infos(module_infos); }
void hi_deinit() { free_module_infos(module_infos); }
/**
* Reloads a shared library module
@@ -138,7 +138,7 @@ void he_deinit() { free_module_infos(module_infos); }
static ReloadResult reload_module(ModuleInfos *modules, const char *filename,
void **updated_handle) {
if (!modules || !filename) {
return RELOAD_NOT_FOUND;
return HI_RELOAD_NOT_FOUND;
}
// Find the module by filename
@@ -165,20 +165,20 @@ static ReloadResult reload_module(ModuleInfos *modules, const char *filename,
}
if (!found) {
return RELOAD_NOT_FOUND;
return HI_RELOAD_NOT_FOUND;
}
// Save the full path
char *fullpath = strdup(modules->names[index]);
if (!fullpath) {
return RELOAD_OPEN_ERROR;
return HI_RELOAD_OPEN_ERROR;
}
// Close the old handle
if (modules->handles[index]) {
if (dlclose(modules->handles[index]) != 0) {
free(fullpath);
return RELOAD_CLOSE_ERROR;
return HI_RELOAD_CLOSE_ERROR;
}
}
@@ -187,7 +187,7 @@ static ReloadResult reload_module(ModuleInfos *modules, const char *filename,
if (!new_handle) {
fprintf(stderr, "Error reloading module: %s\n", dlerror());
free(fullpath);
return RELOAD_OPEN_ERROR;
return HI_RELOAD_OPEN_ERROR;
}
// Update the handle in our structure
@@ -199,23 +199,23 @@ static ReloadResult reload_module(ModuleInfos *modules, const char *filename,
}
free(fullpath);
return RELOAD_SUCCESS;
return HI_RELOAD_SUCCESS;
}
/**
* Helper function to print the result of a module reload
*/
static void print_reload_result(ReloadResult result, const char *filename) {
switch (result) {
case RELOAD_SUCCESS:
case HI_RELOAD_SUCCESS:
printf("Successfully reloaded module: %s\n", filename);
break;
case RELOAD_NOT_FOUND:
case HI_RELOAD_NOT_FOUND:
printf("Module not found: %s\n", filename);
break;
case RELOAD_CLOSE_ERROR:
case HI_RELOAD_CLOSE_ERROR:
printf("Error closing module: %s\n", filename);
break;
case RELOAD_OPEN_ERROR:
case HI_RELOAD_OPEN_ERROR:
printf("Error reopening module: %s\n", filename);
break;
default:
@@ -223,7 +223,7 @@ static void print_reload_result(ReloadResult result, const char *filename) {
}
}
ReloadResult he_reload_module(const char *module_name) {
ReloadResult hi_reload_module(const char *module_name) {
assert(module_infos);
void *new_handle = NULL;
@@ -233,7 +233,7 @@ ReloadResult he_reload_module(const char *module_name) {
return result;
}
void he_print_module_infos() {
void hi_print_module_infos() {
assert(module_infos);
const ModuleInfos *modules = module_infos;

View File

@@ -1,4 +1,4 @@
#include "hiload/symbols.h"
#include "symbols.h"
#include <dlfcn.h>
#include <elf.h>
@@ -14,13 +14,13 @@
* Will clear and free the given SymbolInfo struct. Allocates enough memory to
* hold found symbols.
*/
CreateResult he_create_symbol_info(SymbolInfos *symbols,
CreateResult hi_create_symbol_info(SymbolInfos *symbols,
struct dl_phdr_info *info) {
if (!symbols)
return CREATE_FAILED;
he_free_symbol_info(symbols);
hi_free_symbol_info(symbols);
for (int i = 0; i < info->dlpi_phnum; i++) {
const ElfW(Phdr) *phdr = &info->dlpi_phdr[i];
@@ -103,7 +103,7 @@ CreateResult he_create_symbol_info(SymbolInfos *symbols,
return CREATE_SUCCESS;
}
void he_free_symbol_info(SymbolInfos *symbols) {
void hi_free_symbol_info(SymbolInfos *symbols) {
for (size_t i = 0; i < symbols->count; i++) {
free(symbols->names[i]);
}

View File

@@ -16,7 +16,7 @@ typedef enum { CREATE_SUCCESS = 0, CREATE_FAILED } CreateResult;
struct dl_phdr_info;
CreateResult he_create_symbol_info(SymbolInfos *, struct dl_phdr_info *);
void he_free_symbol_info(SymbolInfos *);
CreateResult hi_create_symbol_info(SymbolInfos *, struct dl_phdr_info *);
void hi_free_symbol_info(SymbolInfos *);
#endif // SYMBOLS_H_