diff --git a/source/main.cpp b/source/main.cpp index 67609b6..a7912a3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -95,7 +95,7 @@ WUMS_APPLICATION_STARTS() { BOOL doDelete = true; DEBUG_FUNCTION_LINE("Check if we can delete %08X", plugin->data.buffer); 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); doDelete = false; break; diff --git a/source/plugin/PluginData.cpp b/source/plugin/PluginData.cpp index e290904..0d5c49b 100644 --- a/source/plugin/PluginData.cpp +++ b/source/plugin/PluginData.cpp @@ -6,7 +6,6 @@ PluginData::PluginData(const PluginData &obj) { this->heapHandle = obj.heapHandle; this->memoryType = obj.memoryType; this->length = obj.length; - loadReader(); } void PluginData::freeMemory() { @@ -30,26 +29,6 @@ void PluginData::freeMemory() { PluginData::PluginData(std::vector 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 input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) : heapHandle(heapHandle), memoryType(memoryType), @@ -78,7 +57,6 @@ PluginData::PluginData(std::vector input, MEMHeapHandle heapHandle, eMe this->buffer = data_copy; break; } - loadReader(); } std::optional PluginData::createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length) { diff --git a/source/plugin/PluginData.h b/source/plugin/PluginData.h index 0f3ebb5..f4a5d8d 100644 --- a/source/plugin/PluginData.h +++ b/source/plugin/PluginData.h @@ -33,45 +33,28 @@ enum eMemoryTypes { class PluginData { public: - const std::optional &getReader() const { - return reader; - } - ~PluginData() { - if (nReader != NULL) { - delete nReader; - nReader = NULL; - } } void freeMemory(); - void *getBuffer() const { - return this->buffer; - } - PluginData(const PluginData &obj); PluginData() { } - void *buffer; + void *buffer = NULL; MEMHeapHandle heapHandle; eMemoryTypes memoryType; - size_t length; + size_t length = 0; private: PluginData(std::vector buffer); PluginData(std::vector input, MEMHeapHandle heapHandle, eMemoryTypes memoryType); - void loadReader(); - static std::optional createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length); - std::optional reader; - - elfio *nReader = NULL; friend class PluginDataFactory; diff --git a/source/plugin/PluginDataPersistence.cpp b/source/plugin/PluginDataPersistence.cpp index 3234ecb..64506da 100644 --- a/source/plugin/PluginDataPersistence.cpp +++ b/source/plugin/PluginDataPersistence.cpp @@ -20,6 +20,5 @@ PluginData PluginDataPersistence::load(plugin_data_t *pluginDataStruct) { pluginData.length = pluginDataStruct->bufferLength; pluginData.memoryType = (eMemoryTypes) pluginDataStruct->memoryType; pluginData.heapHandle = (MEMHeapHandle) pluginDataStruct->heapHandle; - pluginData.loadReader(); return pluginData; } \ No newline at end of file diff --git a/source/plugin/PluginInformationFactory.cpp b/source/plugin/PluginInformationFactory.cpp index 435d1a1..f6d8af5 100644 --- a/source/plugin/PluginInformationFactory.cpp +++ b/source/plugin/PluginInformationFactory.cpp @@ -34,15 +34,19 @@ using namespace ELFIO; std::optional 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 (!readerOpt) { - DEBUG_FUNCTION_LINE("Can't find or process ELF file"); + if(pluginData.buffer == NULL){ + DEBUG_FUNCTION_LINE("Buffer was NULL"); 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; - 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); uint32_t totalSize = 0; @@ -51,7 +55,7 @@ std::optional PluginInformationFactory::load(const PluginData uint32_t data_size = 0; for (uint32_t i = 0; i < sec_num; ++i) { - section *psec = reader->sections[i]; + section *psec = reader.sections[i]; if (psec->get_type() == 0x80000002) { continue; } @@ -69,9 +73,7 @@ std::optional PluginInformationFactory::load(const PluginData } } } - void *text_data = MEMAllocFromExpHeapEx(heapHandle, text_size, 0x1000); - if (text_data == NULL) { DEBUG_FUNCTION_LINE("Failed to alloc memory for the .text section (%d bytes)\n", text_size); @@ -87,10 +89,10 @@ std::optional PluginInformationFactory::load(const PluginData } 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) { - section *psec = reader->sections[i]; + section *psec = reader.sections[i]; if (psec->get_type() == 0x80000002) { continue; } @@ -142,11 +144,11 @@ std::optional PluginInformationFactory::load(const PluginData } 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)) { 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"); free(destinations); MEMFreeToExpHeap(heapHandle, text_data); @@ -155,7 +157,7 @@ std::optional PluginInformationFactory::load(const PluginData } } } - std::vector relocationData = getImportRelocationData(pluginData, destinations); + std::vector relocationData = getImportRelocationData(reader, destinations); for (auto const &reloc : relocationData) { pluginInfo.addRelocationData(reloc); @@ -209,30 +211,25 @@ std::optional PluginInformationFactory::load(const PluginData return pluginInfo; } -std::vector PluginInformationFactory::getImportRelocationData(const PluginData &pluginData, uint8_t **destinations) { - auto readerOpt = pluginData.getReader(); - +std::vector PluginInformationFactory::getImportRelocationData(const elfio & reader, uint8_t **destinations) { std::vector result; - if (!readerOpt) { - return result; - } - auto reader = readerOpt.value(); + std::map infoMap; - uint32_t sec_num = reader->sections.size(); + uint32_t sec_num = reader.sections.size(); for (uint32_t i = 0; i < sec_num; ++i) { - section *psec = reader->sections[i]; + section *psec = reader.sections[i]; if (psec->get_type() == 0x80000002) { infoMap[i] = psec->get_name(); } } 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) { 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) { Elf64_Addr offset; Elf_Word type; @@ -283,20 +280,15 @@ std::vector PluginInformationFactory::getImportRelocationData(co 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) { - auto readerOpt = pluginData.getReader(); - if (!readerOpt) { - return false; - } - auto reader = readerOpt.value(); - uint32_t sec_num = reader->sections.size(); + uint32_t sec_num = reader.sections.size(); 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) { 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) { Elf64_Addr offset; Elf_Word type; diff --git a/source/plugin/PluginInformationFactory.h b/source/plugin/PluginInformationFactory.h index c4868ea..801a299 100644 --- a/source/plugin/PluginInformationFactory.h +++ b/source/plugin/PluginInformationFactory.h @@ -31,8 +31,7 @@ class PluginInformationFactory { public: static std::optional load(const PluginData &pluginData, MEMHeapHandle heaphandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId); - static bool - 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 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); - static std::vector getImportRelocationData(const PluginData &pluginData, uint8_t **destinations); + static std::vector getImportRelocationData(const elfio &reader, uint8_t **destinations); }; diff --git a/source/plugin/PluginMetaInformationFactory.cpp b/source/plugin/PluginMetaInformationFactory.cpp index 473d264..a3e02d5 100644 --- a/source/plugin/PluginMetaInformationFactory.cpp +++ b/source/plugin/PluginMetaInformationFactory.cpp @@ -27,50 +27,48 @@ using namespace ELFIO; std::optional PluginMetaInformationFactory::loadPlugin(const PluginData &pluginData) { - auto readerOpt = pluginData.getReader(); - - // Load ELF data - if (!readerOpt) { - DEBUG_FUNCTION_LINE("Can't find or process ELF file"); + if(pluginData.buffer == NULL){ + DEBUG_FUNCTION_LINE("Buffer was NULL"); 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 PluginMetaInformationFactory::loadPlugin(const std::string filePath) { - auto reader = new elfio; - if (reader == NULL || !reader->load(filePath)) { + elfio reader; + if (!reader.load(filePath)) { DEBUG_FUNCTION_LINE("Can't find or process ELF file\n"); - delete reader; return std::nullopt; } return loadPlugin(reader); } std::optional PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size) { - auto reader = new elfio; - if (reader == NULL || !reader->load(buffer, size)) { + elfio reader; + if (!reader.load(buffer, size)) { DEBUG_FUNCTION_LINE("Can't find or process ELF file\n"); - delete reader; return std::nullopt; } return loadPlugin(reader); } -std::optional PluginMetaInformationFactory::loadPlugin(elfio *reader) { - DEBUG_FUNCTION_LINE("Found elfio reader"); - +std::optional PluginMetaInformationFactory::loadPlugin(const elfio& reader) { size_t pluginSize = 0; 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); for (uint32_t i = 0; i < sec_num; ++i) { - section *psec = reader->sections[i]; + section *psec = reader.sections[i]; // Calculate total size: if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) { diff --git a/source/plugin/PluginMetaInformationFactory.h b/source/plugin/PluginMetaInformationFactory.h index 86a0431..6c8de5e 100644 --- a/source/plugin/PluginMetaInformationFactory.h +++ b/source/plugin/PluginMetaInformationFactory.h @@ -31,5 +31,5 @@ public: static std::optional loadPlugin(char *buffer, size_t size); - static std::optional loadPlugin(elfio *reader); + static std::optional loadPlugin(const elfio& reader); };