Files
hmmmm/src/hmmmm.c
2025-01-21 22:03:23 +03:00

238 lines
7.0 KiB
C

#include "hmmmm.h"
#include <dlfcn.h>
#include <stdio.h>
typedef size_t (*_dlib_pubExtractPcounter_t)(device_public_context_t* devContext);
typedef size_t (*_dlib_pubExtractOpcode_t)(device_mem_t* devMem, size_t _programCounter);
typedef uint8_t(*_dlib_pubExtractPcounterSizeWords_t)();
typedef device_public_context_t* (*_dlib_dev_init_t)(void* specs, char* errbuf);
typedef uint8_t (*_dlib_makeDeviceTick_t)(device_public_context_t* devInfo);
typedef uint8_t (*_dlib_deviceType_t)();
typedef void* (*_dlib_pubExtractPcounterPtr_t)(device_public_context_t* devContext);
typedef void* (*_dlib_parseSpecsFromConfig_t)(const conf_dev_t* devConf, char* errbuf);
typedef void (*_dlib_freeSpecs_t)(void* specs);
typedef void (*_dlib_freeDevMem_t)(device_mem_t* mem);
typedef void (*_dlib_fillSmartReadSpecs_t)(void* specs, smart_read_spec_t* smartReadSpecs, uint64_t smartReadSpecsCount);
typedef void (*_dlib_fillSmartWriteSpecs_t)(void* specs, smart_write_spec_t* smartWriteSpecs, uint64_t smartWriteSpecsCount);
instruction_simul_handlers_t* _fillInstructionSimul(void* handle)
{
instruction_simul_handlers_t* ret = malloc(sizeof(instruction_simul_handlers_t));
if (ret == NULL)
{
return NULL;
}
_dlib_pubExtractPcounterPtr_t _dlib_pubExtractPcounterPtr = (_dlib_pubExtractPcounterPtr_t)(uintptr_t)dlsym(handle, "pubExtractPcounterPtr");
const char *dlib_pubExtractPcounterPtr_error = dlerror();
if (dlib_pubExtractPcounterPtr_error) {
dlclose(handle);
free(ret);
return NULL;
}
ret->extractPcounterPtr = _dlib_pubExtractPcounterPtr;
_dlib_pubExtractPcounter_t _dlib_pubExtractPcounter = (_dlib_pubExtractPcounter_t)(uintptr_t)dlsym(handle, "pubExtractPcounter");
const char *dlib_pubExtractPcounter_error = dlerror();
if (dlib_pubExtractPcounter_error) {
dlclose(handle);
free(ret);
return NULL;
}
ret->extractPcounter = _dlib_pubExtractPcounter;
_dlib_pubExtractOpcode_t _dlib_pubExtractOpcode = (_dlib_pubExtractOpcode_t)(uintptr_t)dlsym(handle, "pubExtractOpcode");
const char *dlib_pubExtractOpcode_error = dlerror();
if (dlib_pubExtractOpcode_error) {
dlclose(handle);
free(ret);
return NULL;
}
ret->extractOpcode = _dlib_pubExtractOpcode;
_dlib_pubExtractPcounterSizeWords_t _dlib_pubExtractPcounterSizeWords = (_dlib_pubExtractPcounterSizeWords_t)(uintptr_t)dlsym(handle, "pubExtractPcounterSizeWords");
const char *dlib_pubExtractPcounterSizeWords_error = dlerror();
if (dlib_pubExtractPcounterSizeWords_error) {
dlclose(handle);
free(ret);
return NULL;
}
ret->extractPcounterSizeWords = _dlib_pubExtractPcounterSizeWords;
return ret;
}
device_lib_t* loadDeviceLib(const char *libpath, char* errbuf)
{
device_lib_t* dev = malloc(sizeof(device_lib_t));
if (dev == NULL)
{
sprintf(errbuf, "unable to allocate device lib struct");
return NULL;
}
void *handle = dlopen(libpath, RTLD_NOW);
if (!handle) {
sprintf(errbuf, "unable to open dl handle");
free(dev);
return NULL;
}
dlerror();
_dlib_dev_init_t _dlib_dev_init = (_dlib_dev_init_t)(uintptr_t)dlsym(handle, "init");
const char *dlsym_init_error = dlerror();
if (dlsym_init_error) {
sprintf(errbuf, "unable to find init symbol: %s", dlsym_init_error);
dlclose(handle);
free(dev);
return NULL;
}
_dlib_makeDeviceTick_t _dlib_makeDeviceTick = (_dlib_makeDeviceTick_t)(uintptr_t)dlsym(handle, "makeDeviceTick");
const char *dlsym_maketick_error = dlerror();
if (dlsym_maketick_error) {
sprintf(errbuf, "unable to find makeDeviceTick symbol: %s", dlsym_maketick_error);
dlclose(handle);
free(dev);
return NULL;
}
_dlib_parseSpecsFromConfig_t _dlib_parseSpecsFromConfig = (_dlib_parseSpecsFromConfig_t)(uintptr_t)dlsym(handle, "parseSpecsFromConfig");
const char *_dlib_parseSpecsFromConfig_error = dlerror();
if (_dlib_parseSpecsFromConfig_error) {
sprintf(errbuf, "unable to find parseSpecsFromConfig symbol: %s", _dlib_parseSpecsFromConfig_error);
dlclose(handle);
free(dev);
return NULL;
}
_dlib_fillSmartReadSpecs_t _dlib_fillSmartReadSpecs = (_dlib_fillSmartReadSpecs_t)(uintptr_t)dlsym(handle, "fillSmartReadSpecs");
const char *_dlib_fillSmartReadSpecs_error = dlerror();
if (_dlib_fillSmartReadSpecs_error) {
sprintf(errbuf, "unable to find fillSmartReadSpecs symbol: %s", _dlib_fillSmartReadSpecs_error);
dlclose(handle);
free(dev);
return NULL;
}
_dlib_fillSmartWriteSpecs_t _dlib_fillSmartWriteSpecs = (_dlib_fillSmartWriteSpecs_t)(uintptr_t)dlsym(handle, "fillSmartWriteSpecs");
const char *_dlib_fillSmartWriteSpecs_error = dlerror();
if (_dlib_fillSmartWriteSpecs_error) {
sprintf(errbuf, "unable to find fillSmartWriteSpecs symbol: %s", _dlib_fillSmartWriteSpecs_error);
dlclose(handle);
free(dev);
return NULL;
}
_dlib_deviceType_t _dlib_deviceType = (_dlib_deviceType_t)(uintptr_t)dlsym(handle, "pubDeviceType");
const char *dlib_deviceType_error = dlerror();
if (dlib_deviceType_error) {
sprintf(errbuf, "unable to find pubDeviceType symbol: %s", dlib_deviceType_error);
dlclose(handle);
free(dev);
return NULL;
}
_dlib_freeSpecs_t _dlib_freeSpecs = (_dlib_freeSpecs_t)(uintptr_t)dlsym(handle, "freeDevSpecs");
const char *dlib_freeSpecs_error = dlerror();
if (dlib_freeSpecs_error) {
sprintf(errbuf, "unable to find freeSpecs symbol: %s", dlib_freeSpecs_error);
dlclose(handle);
free(dev);
return NULL;
}
_dlib_freeDevMem_t _dlib_freeDevMem = (_dlib_freeDevMem_t)(uintptr_t)dlsym(handle, "freeDevSpecs");
const char *dlib_freeDevMem_error = dlerror();
if (dlib_freeDevMem_error) {
sprintf(errbuf, "unable to find freeDevMem symbol: %s", dlib_freeDevMem_error);
dlclose(handle);
free(dev);
return NULL;
}
dev->devContext = NULL;
dev->init = _dlib_dev_init;
dev->makeDeviceTick = _dlib_makeDeviceTick;
dev->parseSpecsFromConfig = _dlib_parseSpecsFromConfig;
dev->fillSmartReadSpecs = _dlib_fillSmartReadSpecs;
dev->fillSmartWriteSpecs = _dlib_fillSmartWriteSpecs;
dev->freeSpecs = _dlib_freeSpecs;
dev->freeDevMem = _dlib_freeDevMem;
uint8_t devType = _dlib_deviceType();
dev->devType = devType;
if (devType == EXTENDED_DEVICE_TYPE_DUMMY)
{
dev->extendedHandlers = NULL;
}
else if (devType == EXTENDED_DEVICE_TYPE_INSTR_SIMUL)
{
dev->extendedHandlers = _fillInstructionSimul(handle);
if (dev->extendedHandlers == NULL)
{
free(dev);
dlclose(handle);
return NULL;
}
}
else
{
free(dev);
dlclose(handle);
return NULL;
}
dev->_dlhandl = handle;
return dev;
}