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;
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;

View File

@ -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<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) :
heapHandle(heapHandle),
memoryType(memoryType),
@ -78,7 +57,6 @@ PluginData::PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMe
this->buffer = data_copy;
break;
}
loadReader();
}
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 {
public:
const std::optional<elfio *> &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<uint8_t> buffer);
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);
std::optional<elfio *> reader;
elfio *nReader = NULL;
friend class PluginDataFactory;

View File

@ -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;
}

View File

@ -34,15 +34,19 @@
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) {
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<PluginInformation> 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<PluginInformation> 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<PluginInformation> 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<PluginInformation> 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<PluginInformation> PluginInformationFactory::load(const PluginData
}
}
}
std::vector<RelocationData> relocationData = getImportRelocationData(pluginData, destinations);
std::vector<RelocationData> relocationData = getImportRelocationData(reader, destinations);
for (auto const &reloc : relocationData) {
pluginInfo.addRelocationData(reloc);
@ -209,30 +211,25 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
return pluginInfo;
}
std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(const PluginData &pluginData, uint8_t **destinations) {
auto readerOpt = pluginData.getReader();
std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(const elfio & reader, uint8_t **destinations) {
std::vector<RelocationData> result;
if (!readerOpt) {
return result;
}
auto reader = readerOpt.value();
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) {
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<RelocationData> 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;

View File

@ -31,8 +31,7 @@ class PluginInformationFactory {
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 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<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;
std::optional<PluginMetaInformation> 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<PluginMetaInformation> 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<PluginMetaInformation> 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<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(elfio *reader) {
DEBUG_FUNCTION_LINE("Found elfio reader");
std::optional<PluginMetaInformation> 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)) {

View File

@ -31,5 +31,5 @@ public:
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);
};