gather information about module.. again
This commit is contained in:
3
src/moduler/elf.c
Normal file
3
src/moduler/elf.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "elf.h"
|
||||
|
||||
#include <libelf.h>
|
||||
84
src/moduler/elf.h
Normal file
84
src/moduler/elf.h
Normal file
@@ -0,0 +1,84 @@
|
||||
#ifndef ELF_H_
|
||||
#define ELF_H_
|
||||
|
||||
#include <libelf.h>
|
||||
|
||||
static inline const char *dyn_type_to_str(unsigned type) {
|
||||
// clang-format off
|
||||
switch (type) {
|
||||
case DT_NULL: return "NULL";
|
||||
case DT_NEEDED: return "NEEDED";
|
||||
case DT_PLTRELSZ: return "PLTRELSZ";
|
||||
case DT_PLTGOT: return "PLTGOT";
|
||||
case DT_HASH: return "HASH";
|
||||
case DT_STRTAB: return "STRTAB";
|
||||
case DT_SYMTAB: return "SYMTAB";
|
||||
case DT_RELA: return "RELA";
|
||||
case DT_RELASZ: return "RELASZ";
|
||||
case DT_RELAENT: return "RELAENT";
|
||||
case DT_STRSZ: return "STRSZ";
|
||||
case DT_SYMENT: return "SYMENT";
|
||||
case DT_INIT: return "INIT";
|
||||
case DT_FINI: return "FINI";
|
||||
case DT_SONAME: return "SONAME";
|
||||
case DT_RPATH: return "RPATH";
|
||||
case DT_SYMBOLIC: return "SYMBOLIC";
|
||||
case DT_REL: return "REL";
|
||||
case DT_RELSZ: return "RELSZ";
|
||||
case DT_RELENT: return "RELENT";
|
||||
case DT_PLTREL: return "PLTREL";
|
||||
case DT_DEBUG: return "DEBUG";
|
||||
case DT_TEXTREL: return "TEXTREL";
|
||||
case DT_JMPREL: return "JMPREL";
|
||||
case DT_BIND_NOW: return "BIND_NOW";
|
||||
case DT_INIT_ARRAY: return "INIT_ARRAY";
|
||||
case DT_FINI_ARRAY: return "FINI_ARRAY";
|
||||
case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
|
||||
case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
|
||||
case DT_RUNPATH: return "RUNPATH";
|
||||
case DT_FLAGS: return "FLAGS";
|
||||
case DT_PREINIT_ARRAY: return "ENCODING/PREINIT_ARRAY";
|
||||
case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
|
||||
case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
|
||||
case DT_RELRSZ: return "RELRSZ";
|
||||
case DT_RELR: return "RELR";
|
||||
case DT_RELRENT: return "RELRENT";
|
||||
case DT_NUM: return "NUM";
|
||||
case DT_LOOS: return "LOOS";
|
||||
case DT_HIOS: return "HIOS";
|
||||
case DT_LOPROC: return "LOPROC";
|
||||
case DT_HIPROC: return "HIPROC";
|
||||
case DT_PROCNUM: return "PROCNUM";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
static inline const char *segment_type_to_str(unsigned type) {
|
||||
// clang-format off
|
||||
switch (type) {
|
||||
case PT_NULL: return "NULL";
|
||||
case PT_LOAD: return "LOAD";
|
||||
case PT_DYNAMIC: return "DYNAMIC";
|
||||
case PT_INTERP: return "INTERP";
|
||||
case PT_NOTE: return "NOTE";
|
||||
case PT_SHLIB: return "SHLIB";
|
||||
case PT_PHDR: return "PHDR";
|
||||
case PT_TLS: return "TLS";
|
||||
case PT_NUM: return "NUM";
|
||||
case PT_LOOS: return "LOOS";
|
||||
case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
|
||||
case PT_GNU_STACK: return "GNU_STACK";
|
||||
case PT_GNU_RELRO: return "GNU_RELRO";
|
||||
case PT_GNU_PROPERTY: return "GNU_PROPERTY";
|
||||
case PT_LOSUNW: return "LOSUNW/SUNWBSS";
|
||||
case PT_SUNWSTACK: return "SUNWSTACK";
|
||||
case PT_HIOS: return "HIOS";
|
||||
case PT_LOPROC: return "LOPROC";
|
||||
case PT_HIPROC: return "HIPROC";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
#endif
|
||||
48
src/moduler/moduler.c
Normal file
48
src/moduler/moduler.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "moduler.h"
|
||||
|
||||
#include "logger/logger.h"
|
||||
#include "moduler/elf.h"
|
||||
#include "memory.h"
|
||||
#include "types.h"
|
||||
|
||||
HiloadResult moduler_reload(ModuleInfos *modinfos,
|
||||
const struct sc_array_memreg *const memregs,
|
||||
int modindex) {
|
||||
const char *modname = sc_array_at(&modinfos->names, modindex);
|
||||
MemoryRegionSpan memspan = memory_get_module_span(memregs, modname);
|
||||
log_debugv("Module: %s - [%p, %p]\n", modname, memspan.region_start,
|
||||
memspan.region_end);
|
||||
|
||||
|
||||
Elf *elf = elf_memory((void*)memspan.region_start, memspan.region_end - memspan.region_start);
|
||||
int err = elf_errno();
|
||||
if (err) {
|
||||
log_error("%s\n", elf_errmsg(err));
|
||||
}
|
||||
|
||||
void *module_address = (void*)sc_array_at(&modinfos->addresses, modindex);
|
||||
|
||||
size_t phdrnum = 0;
|
||||
err = elf_getphdrnum(elf, &phdrnum);
|
||||
Elf64_Phdr *phdr = elf64_getphdr(elf);
|
||||
|
||||
for (size_t i = 0; i < phdrnum; ++i) {
|
||||
Elf64_Phdr *p = phdr + i;
|
||||
log_debugv("segment type: %s\n", segment_type_to_str(p->p_type));
|
||||
|
||||
size_t segment_size = p->p_memsz;
|
||||
void *segment_start = module_address + p->p_vaddr;
|
||||
void *segment_end = module_address + p->p_vaddr + segment_size;
|
||||
|
||||
if (p->p_type == PT_DYNAMIC) {
|
||||
Elf64_Dyn *dyn = (Elf64_Dyn*)segment_start;
|
||||
while ((void*)dyn < segment_end) {
|
||||
log_debugv(" dyn type: %s\n", dyn_type_to_str(dyn->d_tag));
|
||||
++dyn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
modinfos->state[modindex] = HI_MODULE_STATE_CLEAN;
|
||||
return HILOAD_OK;
|
||||
}
|
||||
31
src/moduler/moduler.h
Normal file
31
src/moduler/moduler.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef MODULER_H_
|
||||
#define MODULER_H_
|
||||
|
||||
#include "array/array.h"
|
||||
#include "memory.h"
|
||||
#include "symbols.h"
|
||||
#include "types.h"
|
||||
|
||||
struct HiloadContext;
|
||||
|
||||
sc_array_def(uptr, uptr);
|
||||
|
||||
enum HiModuleState {
|
||||
HI_MODULE_STATE_CLEAN = 0,
|
||||
HI_MODULE_STATE_DIRTY,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
struct sc_array_str names; // Array of library names
|
||||
struct sc_array_ptr handles; // Array of library handles
|
||||
struct sc_array_uptr addresses; // Array of library base addresses
|
||||
struct sc_array_syms symbols; // Symbol info for modules
|
||||
u8 state[256]; // Flag for when module needs to be changed
|
||||
size_t count; // Number of modules
|
||||
} ModuleInfos;
|
||||
|
||||
HiloadResult moduler_reload(ModuleInfos *modinfos,
|
||||
const struct sc_array_memreg *const memregs,
|
||||
int modindex);
|
||||
|
||||
#endif // MODULER_H_
|
||||
Reference in New Issue
Block a user