Remove the elfio reader from the PluginData class, use it when it's needed

This commit is contained in:
Maschell 2020-06-03 18:34:31 +02:00
parent 1917ff9fb4
commit 2b517b5699
8 changed files with 46 additions and 97 deletions

View File

@ -95,7 +95,7 @@ WUMS_APPLICATION_STARTS() {
BOOL doDelete = true; BOOL doDelete = true;
DEBUG_FUNCTION_LINE("Check if we can delete %08X", plugin->data.buffer); DEBUG_FUNCTION_LINE("Check if we can delete %08X", plugin->data.buffer);
for (auto &pluginData: pluginDataList) { for (auto &pluginData: pluginDataList) {
if (pluginData.getBuffer() == plugin->data.buffer) { if (pluginData.buffer == plugin->data.buffer) {
DEBUG_FUNCTION_LINE("We can keep buffer %08X", plugin->data.buffer); DEBUG_FUNCTION_LINE("We can keep buffer %08X", plugin->data.buffer);
doDelete = false; doDelete = false;
break; break;

View File

@ -6,7 +6,6 @@ PluginData::PluginData(const PluginData &obj) {
this->heapHandle = obj.heapHandle; this->heapHandle = obj.heapHandle;
this->memoryType = obj.memoryType; this->memoryType = obj.memoryType;
this->length = obj.length; this->length = obj.length;
loadReader();
} }
void PluginData::freeMemory() { void PluginData::freeMemory() {
@ -30,26 +29,6 @@ void PluginData::freeMemory() {
PluginData::PluginData(std::vector<uint8_t> buffer) : PluginData(buffer, 0, eMemTypeMEM2) { PluginData::PluginData(std::vector<uint8_t> buffer) : PluginData(buffer, 0, eMemTypeMEM2) {
} }
void PluginData::loadReader() {
if (this->buffer == NULL) {
this->reader = std::nullopt;
} else {
elfio *nReader = new elfio;
if (nReader != NULL && nReader->load((char *) this->buffer, length)) {
DEBUG_FUNCTION_LINE("Loading was okay");
this->reader = nReader;
} else {
if (nReader) {
delete nReader;
nReader = NULL;
}
DEBUG_FUNCTION_LINE("Loading failed");
this->reader = std::nullopt;
}
}
}
PluginData::PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) : PluginData::PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) :
heapHandle(heapHandle), heapHandle(heapHandle),
memoryType(memoryType), memoryType(memoryType),
@ -78,7 +57,6 @@ PluginData::PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMe
this->buffer = data_copy; this->buffer = data_copy;
break; break;
} }
loadReader();
} }
std::optional<PluginData> PluginData::createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length) { std::optional<PluginData> PluginData::createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length) {

View File

@ -33,45 +33,28 @@ enum eMemoryTypes {
class PluginData { class PluginData {
public: public:
const std::optional<elfio *> &getReader() const {
return reader;
}
~PluginData() { ~PluginData() {
if (nReader != NULL) {
delete nReader;
nReader = NULL;
}
} }
void freeMemory(); void freeMemory();
void *getBuffer() const {
return this->buffer;
}
PluginData(const PluginData &obj); PluginData(const PluginData &obj);
PluginData() { PluginData() {
} }
void *buffer; void *buffer = NULL;
MEMHeapHandle heapHandle; MEMHeapHandle heapHandle;
eMemoryTypes memoryType; eMemoryTypes memoryType;
size_t length; size_t length = 0;
private: private:
PluginData(std::vector<uint8_t> buffer); PluginData(std::vector<uint8_t> buffer);
PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMemoryTypes memoryType); PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMemoryTypes memoryType);
void loadReader();
static std::optional<PluginData> createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length); static std::optional<PluginData> createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length);
std::optional<elfio *> reader;
elfio *nReader = NULL;
friend class PluginDataFactory; friend class PluginDataFactory;

View File

@ -20,6 +20,5 @@ PluginData PluginDataPersistence::load(plugin_data_t *pluginDataStruct) {
pluginData.length = pluginDataStruct->bufferLength; pluginData.length = pluginDataStruct->bufferLength;
pluginData.memoryType = (eMemoryTypes) pluginDataStruct->memoryType; pluginData.memoryType = (eMemoryTypes) pluginDataStruct->memoryType;
pluginData.heapHandle = (MEMHeapHandle) pluginDataStruct->heapHandle; pluginData.heapHandle = (MEMHeapHandle) pluginDataStruct->heapHandle;
pluginData.loadReader();
return pluginData; return pluginData;
} }

View File

@ -34,15 +34,19 @@
using namespace ELFIO; using namespace ELFIO;
std::optional<PluginInformation> PluginInformationFactory::load(const PluginData &pluginData, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId) { std::optional<PluginInformation> PluginInformationFactory::load(const PluginData &pluginData, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId) {
auto readerOpt = pluginData.getReader(); if(pluginData.buffer == NULL){
if (!readerOpt) { DEBUG_FUNCTION_LINE("Buffer was NULL");
DEBUG_FUNCTION_LINE("Can't find or process ELF file");
return std::nullopt; return std::nullopt;
} }
auto reader = readerOpt.value(); elfio reader;
if (! reader.load((char*) pluginData.buffer, pluginData.length)) {
DEBUG_FUNCTION_LINE("Can't process PluginData in elfio");
return std::nullopt;
}
PluginInformation pluginInfo; PluginInformation pluginInfo;
uint32_t sec_num = reader->sections.size(); uint32_t sec_num = reader.sections.size();
uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num); uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num);
uint32_t totalSize = 0; uint32_t totalSize = 0;
@ -51,7 +55,7 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
uint32_t data_size = 0; uint32_t data_size = 0;
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i]; section *psec = reader.sections[i];
if (psec->get_type() == 0x80000002) { if (psec->get_type() == 0x80000002) {
continue; continue;
} }
@ -69,9 +73,7 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
} }
} }
} }
void *text_data = MEMAllocFromExpHeapEx(heapHandle, text_size, 0x1000); void *text_data = MEMAllocFromExpHeapEx(heapHandle, text_size, 0x1000);
if (text_data == NULL) { if (text_data == NULL) {
DEBUG_FUNCTION_LINE("Failed to alloc memory for the .text section (%d bytes)\n", text_size); DEBUG_FUNCTION_LINE("Failed to alloc memory for the .text section (%d bytes)\n", text_size);
@ -87,10 +89,10 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
} }
DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", data_size / 1024); DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", data_size / 1024);
uint32_t entrypoint = (uint32_t) text_data + (uint32_t) reader->get_entry() - 0x02000000; uint32_t entrypoint = (uint32_t) text_data + (uint32_t) reader.get_entry() - 0x02000000;
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i]; section *psec = reader.sections[i];
if (psec->get_type() == 0x80000002) { if (psec->get_type() == 0x80000002) {
continue; continue;
} }
@ -142,11 +144,11 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
} }
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i]; section *psec = reader.sections[i];
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) { if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
DEBUG_FUNCTION_LINE("Linking (%d)... %s at %08X", i, psec->get_name().c_str(), destinations[psec->get_index()]); DEBUG_FUNCTION_LINE("Linking (%d)... %s at %08X", i, psec->get_name().c_str(), destinations[psec->get_index()]);
if (!linkSection(pluginData, psec->get_index(), (uint32_t) destinations[psec->get_index()], (uint32_t) text_data, (uint32_t) data_data, trampolin_data, trampolin_data_length, trampolinId)) { if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], (uint32_t) text_data, (uint32_t) data_data, trampolin_data, trampolin_data_length, trampolinId)) {
DEBUG_FUNCTION_LINE("elfLink failed"); DEBUG_FUNCTION_LINE("elfLink failed");
free(destinations); free(destinations);
MEMFreeToExpHeap(heapHandle, text_data); MEMFreeToExpHeap(heapHandle, text_data);
@ -155,7 +157,7 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
} }
} }
} }
std::vector<RelocationData> relocationData = getImportRelocationData(pluginData, destinations); std::vector<RelocationData> relocationData = getImportRelocationData(reader, destinations);
for (auto const &reloc : relocationData) { for (auto const &reloc : relocationData) {
pluginInfo.addRelocationData(reloc); pluginInfo.addRelocationData(reloc);
@ -209,30 +211,25 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
return pluginInfo; return pluginInfo;
} }
std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(const PluginData &pluginData, uint8_t **destinations) { std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(const elfio & reader, uint8_t **destinations) {
auto readerOpt = pluginData.getReader();
std::vector<RelocationData> result; std::vector<RelocationData> result;
if (!readerOpt) {
return result;
}
auto reader = readerOpt.value();
std::map<uint32_t, std::string> infoMap; std::map<uint32_t, std::string> infoMap;
uint32_t sec_num = reader->sections.size(); uint32_t sec_num = reader.sections.size();
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i]; section *psec = reader.sections[i];
if (psec->get_type() == 0x80000002) { if (psec->get_type() == 0x80000002) {
infoMap[i] = psec->get_name(); infoMap[i] = psec->get_name();
} }
} }
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i]; section *psec = reader.sections[i];
if (psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) { if (psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) {
DEBUG_FUNCTION_LINE("Found relocation section %s", psec->get_name().c_str()); DEBUG_FUNCTION_LINE("Found relocation section %s", psec->get_name().c_str());
relocation_section_accessor rel(*reader, psec); relocation_section_accessor rel(reader, psec);
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) { for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
Elf64_Addr offset; Elf64_Addr offset;
Elf_Word type; Elf_Word type;
@ -283,20 +280,15 @@ std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(co
return result; return result;
} }
bool PluginInformationFactory::linkSection(const PluginData &pluginData, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, bool PluginInformationFactory::linkSection(const elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length,
uint8_t trampolinId) { uint8_t trampolinId) {
auto readerOpt = pluginData.getReader(); uint32_t sec_num = reader.sections.size();
if (!readerOpt) {
return false;
}
auto reader = readerOpt.value();
uint32_t sec_num = reader->sections.size();
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i]; section *psec = reader.sections[i];
if (psec->get_info() == section_index) { if (psec->get_info() == section_index) {
DEBUG_FUNCTION_LINE("Found relocation section %s", psec->get_name().c_str()); DEBUG_FUNCTION_LINE("Found relocation section %s", psec->get_name().c_str());
relocation_section_accessor rel(*reader, psec); relocation_section_accessor rel(reader, psec);
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) { for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
Elf64_Addr offset; Elf64_Addr offset;
Elf_Word type; Elf_Word type;

View File

@ -31,8 +31,7 @@ class PluginInformationFactory {
public: public:
static std::optional<PluginInformation> load(const PluginData &pluginData, MEMHeapHandle heaphandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId); static std::optional<PluginInformation> load(const PluginData &pluginData, MEMHeapHandle heaphandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId);
static bool static bool linkSection(const elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId);
linkSection(const PluginData &pluginData, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId);
static std::vector<RelocationData> getImportRelocationData(const PluginData &pluginData, uint8_t **destinations); static std::vector<RelocationData> getImportRelocationData(const elfio &reader, uint8_t **destinations);
}; };

View File

@ -27,50 +27,48 @@
using namespace ELFIO; using namespace ELFIO;
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const PluginData &pluginData) { std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const PluginData &pluginData) {
auto readerOpt = pluginData.getReader(); if(pluginData.buffer == NULL){
DEBUG_FUNCTION_LINE("Buffer was NULL");
// Load ELF data
if (!readerOpt) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file");
return std::nullopt; return std::nullopt;
} }
return loadPlugin(readerOpt.value()); elfio reader;
if (! reader.load((char*) pluginData.buffer, pluginData.length)) {
DEBUG_FUNCTION_LINE("Can't process PluginData in elfio");
return std::nullopt;
}
return loadPlugin(reader);
} }
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const std::string filePath) { std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const std::string filePath) {
auto reader = new elfio; elfio reader;
if (reader == NULL || !reader->load(filePath)) { if (!reader.load(filePath)) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file\n"); DEBUG_FUNCTION_LINE("Can't find or process ELF file\n");
delete reader;
return std::nullopt; return std::nullopt;
} }
return loadPlugin(reader); return loadPlugin(reader);
} }
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size) { std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size) {
auto reader = new elfio; elfio reader;
if (reader == NULL || !reader->load(buffer, size)) { if (!reader.load(buffer, size)) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file\n"); DEBUG_FUNCTION_LINE("Can't find or process ELF file\n");
delete reader;
return std::nullopt; return std::nullopt;
} }
return loadPlugin(reader); return loadPlugin(reader);
} }
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(elfio *reader) { std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const elfio& reader) {
DEBUG_FUNCTION_LINE("Found elfio reader");
size_t pluginSize = 0; size_t pluginSize = 0;
PluginMetaInformation pluginInfo; PluginMetaInformation pluginInfo;
uint32_t sec_num = reader->sections.size(); uint32_t sec_num = reader.sections.size();
DEBUG_FUNCTION_LINE("%d number of sections", sec_num); DEBUG_FUNCTION_LINE("%d number of sections", sec_num);
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i]; section *psec = reader.sections[i];
// Calculate total size: // Calculate total size:
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) { if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {

View File

@ -31,5 +31,5 @@ public:
static std::optional<PluginMetaInformation> loadPlugin(char *buffer, size_t size); static std::optional<PluginMetaInformation> loadPlugin(char *buffer, size_t size);
static std::optional<PluginMetaInformation> loadPlugin(elfio *reader); static std::optional<PluginMetaInformation> loadPlugin(const elfio& reader);
}; };