Even more formatting

This commit is contained in:
Maschell
2020-05-03 12:30:15 +02:00
parent 4bf4a0eeea
commit 0c0680be77
41 changed files with 1036 additions and 984 deletions

View File

@@ -33,9 +33,9 @@
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(!readerOpt) {
if (!readerOpt) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file");
return std::nullopt;
}
@@ -50,8 +50,8 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
uint32_t text_size = 0;
uint32_t data_size = 0;
for(uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader->sections[i];
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i];
if (psec->get_type() == 0x80000002) {
continue;
}
@@ -59,35 +59,35 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
uint32_t sectionSize = psec->get_size();
uint32_t address = (uint32_t) psec->get_address();
if((address >= 0x02000000) && address < 0x10000000) {
if ((address >= 0x02000000) && address < 0x10000000) {
text_size += sectionSize;
} else if((address >= 0x10000000) && address < 0xC0000000) {
} else if ((address >= 0x10000000) && address < 0xC0000000) {
data_size += sectionSize;
}
}
}
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);
return std::nullopt;
}
DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", text_size/1024);
void * data_data = MEMAllocFromExpHeapEx(heapHandle, data_size, 0x1000);
if(data_data == NULL) {
DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", text_size / 1024);
void *data_data = MEMAllocFromExpHeapEx(heapHandle, data_size, 0x1000);
if (data_data == NULL) {
DEBUG_FUNCTION_LINE("Failed to alloc memory for the .data section (%d bytes)\n", data_size);
MEMFreeToExpHeap(heapHandle, text_data);
return std::nullopt;
}
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 ) {
section* psec = reader->sections[i];
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader->sections[i];
if (psec->get_type() == 0x80000002) {
continue;
}
@@ -97,15 +97,15 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
uint32_t address = (uint32_t) psec->get_address();
uint32_t destination = address;
if((address >= 0x02000000) && address < 0x10000000) {
if ((address >= 0x02000000) && address < 0x10000000) {
destination += (uint32_t) text_data;
destination -= 0x02000000;
destinations[psec->get_index()] = (uint8_t *) text_data;
} else if((address >= 0x10000000) && address < 0xC0000000) {
} else if ((address >= 0x10000000) && address < 0xC0000000) {
destination += (uint32_t) data_data;
destination -= 0x10000000;
destinations[psec->get_index()] = (uint8_t *) data_data;
} else if(address >= 0xC0000000) {
} else if (address >= 0xC0000000) {
destination += (uint32_t) data_data;
destination -= 0xC0000000;
//destinations[psec->get_index()] = (uint8_t *) data_data;
@@ -118,14 +118,14 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
return std::nullopt;
}
const char* p = psec->get_data();
const char *p = psec->get_data();
if(psec->get_type() == SHT_NOBITS) {
if (psec->get_type() == SHT_NOBITS) {
DEBUG_FUNCTION_LINE("memset section %s %08X to 0 (%d bytes)", psec->get_name().c_str(), destination, sectionSize);
memset((void*) destination, 0, sectionSize);
} else if(psec->get_type() == SHT_PROGBITS) {
memset((void *) destination, 0, sectionSize);
} else if (psec->get_type() == SHT_PROGBITS) {
DEBUG_FUNCTION_LINE("Copy section %s %08X -> %08X (%d bytes)", psec->get_name().c_str(), p, destination, sectionSize);
memcpy((void*) destination, p, sectionSize);
memcpy((void *) destination, p, sectionSize);
}
pluginInfo.addSectionInfo(SectionInfo(psec->get_name(), destination, sectionSize));
@@ -133,15 +133,15 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
totalSize += sectionSize;
DCFlushRange((void*)destination, sectionSize);
ICInvalidateRange((void*)destination, sectionSize);
DCFlushRange((void *) destination, sectionSize);
ICInvalidateRange((void *) destination, sectionSize);
}
}
for(uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader->sections[i];
for (uint32_t i = 0; i < sec_num; ++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()]);
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)) {
DEBUG_FUNCTION_LINE("elfLink failed");
@@ -154,14 +154,14 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
}
std::vector<RelocationData> relocationData = getImportRelocationData(pluginData, destinations);
for (auto const& reloc : relocationData) {
for (auto const &reloc : relocationData) {
pluginInfo.addRelocationData(reloc);
}
DCFlushRange((void*)text_data, text_size);
ICInvalidateRange((void*)text_data, text_size);
DCFlushRange((void*)data_data, data_size);
ICInvalidateRange((void*)data_data, data_size);
DCFlushRange((void *) text_data, text_size);
ICInvalidateRange((void *) text_data, text_size);
DCFlushRange((void *) data_data, data_size);
ICInvalidateRange((void *) data_data, data_size);
free(destinations);
@@ -170,28 +170,30 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint);
std::optional<SectionInfo> secInfo = pluginInfo.getSectionInfo(".wups.hooks");
if(secInfo && secInfo->getSize() > 0) {
if (secInfo && secInfo->getSize() > 0) {
size_t entries_count = secInfo->getSize() / sizeof(wups_loader_hook_t);
wups_loader_hook_t * entries = (wups_loader_hook_t *) secInfo->getAddress();
if(entries != NULL) {
for(size_t j=0; j<entries_count; j++) {
wups_loader_hook_t * hook = &entries[j];
DEBUG_FUNCTION_LINE("Saving hook of plugin Type: %08X, target: %08X"/*,pluginData.getPluginInformation()->getName().c_str()*/,hook->type,(void*) hook->target);
HookData hook_data((void *) hook->target,hook->type);
wups_loader_hook_t *entries = (wups_loader_hook_t *) secInfo->getAddress();
if (entries != NULL) {
for (size_t j = 0; j < entries_count; j++) {
wups_loader_hook_t *hook = &entries[j];
DEBUG_FUNCTION_LINE("Saving hook of plugin Type: %08X, target: %08X"/*,pluginData.getPluginInformation()->getName().c_str()*/, hook->type, (void *) hook->target);
HookData hook_data((void *) hook->target, hook->type);
pluginInfo.addHookData(hook_data);
}
}
}
secInfo = pluginInfo.getSectionInfo(".wups.load");
if(secInfo && secInfo->getSize() > 0) {
if (secInfo && secInfo->getSize() > 0) {
size_t entries_count = secInfo->getSize() / sizeof(wups_loader_entry_t);
wups_loader_entry_t * entries = (wups_loader_entry_t *) secInfo->getAddress();
if(entries != NULL) {
for(size_t j=0; j<entries_count; j++) {
wups_loader_entry_t * cur_function = &entries[j];
DEBUG_FUNCTION_LINE("Saving function \"%s\" of plugin . PA:%08X VA:%08X Library: %08X, target: %08X, call_addr: %08X",cur_function->_function.name/*,pluginData.getPluginInformation()->getName().c_str()*/,cur_function->_function.physical_address,cur_function->_function.virtual_address, cur_function->_function.library,cur_function->_function.target, (void *) cur_function->_function.call_addr);
FunctionData function_data((void *) cur_function->_function.physical_address,(void *) cur_function->_function.virtual_address, cur_function->_function.name, cur_function->_function.library, (void *) cur_function->_function.target, (void *) cur_function->_function.call_addr);
wups_loader_entry_t *entries = (wups_loader_entry_t *) secInfo->getAddress();
if (entries != NULL) {
for (size_t j = 0; j < entries_count; j++) {
wups_loader_entry_t *cur_function = &entries[j];
DEBUG_FUNCTION_LINE("Saving function \"%s\" of plugin . PA:%08X VA:%08X Library: %08X, target: %08X, call_addr: %08X", cur_function->_function.name/*,pluginData.getPluginInformation()->getName().c_str()*/,
cur_function->_function.physical_address, cur_function->_function.virtual_address, cur_function->_function.library, cur_function->_function.target, (void *) cur_function->_function.call_addr);
FunctionData function_data((void *) cur_function->_function.physical_address, (void *) cur_function->_function.virtual_address, cur_function->_function.name, cur_function->_function.library,
(void *) cur_function->_function.target, (void *) cur_function->_function.call_addr);
pluginInfo.addFunctionData(function_data);
}
}
@@ -204,45 +206,45 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
return pluginInfo;
}
std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(const PluginData &pluginData, uint8_t ** destinations) {
std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(const PluginData &pluginData, uint8_t **destinations) {
auto readerOpt = pluginData.getReader();
std::vector<RelocationData> result;
if(!readerOpt) {
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();
for ( uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader->sections[i];
for (uint32_t i = 0; i < sec_num; ++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];
if(psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) {
DEBUG_FUNCTION_LINE("Found relocation section %s",psec->get_name().c_str());
for (uint32_t i = 0; i < sec_num; ++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);
for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
if (!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
DEBUG_FUNCTION_LINE("Failed to get relocation");
break;
}
uint32_t adjusted_sym_value = (uint32_t) sym_value;
if(adjusted_sym_value < 0xC0000000) {
if (adjusted_sym_value < 0xC0000000) {
continue;
}
@@ -254,7 +256,7 @@ std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(co
std::string rplName = "";
std::string rawSectionName = infoMap[sym_section_index];
if(rawSectionName.size() < fimport.size()) {
if (rawSectionName.size() < fimport.size()) {
DEBUG_FUNCTION_LINE("Section name was shorter than expected, skipping this relocation");
continue;
} else if (std::equal(fimport.begin(), fimport.end(), rawSectionName.begin())) {
@@ -271,75 +273,77 @@ std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(co
uint32_t section_index = psec->get_info();
result.push_back(RelocationData(type, offset - 0x02000000, addend, (void*)(destinations[section_index]), sym_name, rplInfo));
result.push_back(RelocationData(type, offset - 0x02000000, addend, (void *) (destinations[section_index]), sym_name, rplInfo));
}
}
}
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, uint8_t trampolinId) {
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,
uint8_t trampolinId) {
auto readerOpt = pluginData.getReader();
if(!readerOpt) {
if (!readerOpt) {
return false;
}
auto reader = readerOpt.value();
uint32_t sec_num = reader->sections.size();
for (uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader->sections[i];
if(psec->get_info() == section_index) {
DEBUG_FUNCTION_LINE("Found relocation section %s",psec->get_name().c_str());
for (uint32_t i = 0; i < sec_num; ++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);
for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
if (!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
DEBUG_FUNCTION_LINE("Failed to get relocation");
break;
}
uint32_t adjusted_sym_value = (uint32_t) sym_value;
if((adjusted_sym_value >= 0x02000000) && adjusted_sym_value < 0x10000000) {
if ((adjusted_sym_value >= 0x02000000) && adjusted_sym_value < 0x10000000) {
adjusted_sym_value -= 0x02000000;
adjusted_sym_value += base_text;
} else if((adjusted_sym_value >= 0x10000000) && adjusted_sym_value < 0xC0000000) {
} else if ((adjusted_sym_value >= 0x10000000) && adjusted_sym_value < 0xC0000000) {
adjusted_sym_value -= 0x10000000;
adjusted_sym_value += base_data;
} else if(adjusted_sym_value >= 0xC0000000) {
} else if (adjusted_sym_value >= 0xC0000000) {
//DEBUG_FUNCTION_LINE("Skip imports");
// Skip imports
continue;
} else if(adjusted_sym_value == 0x0) {
} else if (adjusted_sym_value == 0x0) {
//
} else {
DEBUG_FUNCTION_LINE("Unhandled case %08X",adjusted_sym_value);
DEBUG_FUNCTION_LINE("Unhandled case %08X", adjusted_sym_value);
return false;
}
uint32_t adjusted_offset = (uint32_t) offset;
if((offset >= 0x02000000) && offset < 0x10000000) {
if ((offset >= 0x02000000) && offset < 0x10000000) {
adjusted_offset -= 0x02000000;
} else if((adjusted_offset >= 0x10000000) && adjusted_offset < 0xC0000000) {
} else if ((adjusted_offset >= 0x10000000) && adjusted_offset < 0xC0000000) {
adjusted_offset -= 0x10000000;
} else if(adjusted_offset >= 0xC0000000) {
} else if (adjusted_offset >= 0xC0000000) {
adjusted_offset -= 0xC0000000;
}
if(sym_section_index == SHN_ABS) {
if (sym_section_index == SHN_ABS) {
//
} else if(sym_section_index > SHN_LORESERVE) {
} else if (sym_section_index > SHN_LORESERVE) {
DEBUG_FUNCTION_LINE("NOT IMPLEMENTED: %04X", sym_section_index);
return false;
}
if(false) {
if (false) {
DEBUG_FUNCTION_LINE("sym_value %08X adjusted_sym_value %08X offset %08X adjusted_offset %08X", (uint32_t) sym_value, adjusted_sym_value, (uint32_t) offset, adjusted_offset);
}
if(!ElfUtils::elfLinkOne(type, adjusted_offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED, trampolinId)) {
if (!ElfUtils::elfLinkOne(type, adjusted_offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED, trampolinId)) {
DEBUG_FUNCTION_LINE("Link failed");
return false;
}