WUMSLoader/source/module/ModuleDataFactory.cpp

384 lines
17 KiB
C++
Raw Normal View History

2020-04-28 14:43:07 +02:00
/****************************************************************************
* Copyright (C) 2018 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "ModuleDataFactory.h"
#include "ElfUtils.h"
#include "FunctionSymbolData.h"
2022-02-04 21:44:03 +01:00
#include "utils/utils.h"
#include <coreinit/cache.h>
#include <map>
#include <string>
#include <vector>
#include <wums.h>
2020-04-28 14:43:07 +02:00
using namespace ELFIO;
2021-12-07 18:23:18 +01:00
std::optional<std::shared_ptr<ModuleData>>
2022-01-27 12:52:45 +01:00
ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_ptr, uint32_t maximum_size, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length) {
2020-04-28 14:43:07 +02:00
elfio reader;
2021-12-07 18:23:18 +01:00
std::shared_ptr<ModuleData> moduleData = std::make_shared<ModuleData>();
2020-04-28 14:43:07 +02:00
// Load ELF data
if (!reader.load(path)) {
DEBUG_FUNCTION_LINE("Can't find or process %s", path.c_str());
return std::nullopt;
2020-04-28 14:43:07 +02:00
}
uint32_t sec_num = reader.sections.size();
2021-09-17 16:22:54 +02:00
auto **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num);
2020-04-28 14:43:07 +02:00
uint32_t baseOffset = *destination_address_ptr;
2020-04-28 14:43:07 +02:00
uint32_t offset_text = baseOffset;
uint32_t offset_data = offset_text;
uint32_t entrypoint = offset_text + (uint32_t) reader.get_entry() - 0x02000000;
2022-02-04 21:44:03 +01:00
uint32_t totalSize = 0;
uint32_t endAddress = 0;
2020-05-17 19:05:51 +02:00
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
2020-04-28 14:43:07 +02:00
if (psec->get_type() == 0x80000002) {
continue;
}
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
uint32_t sectionSize = psec->get_size();
totalSize += sectionSize;
2021-09-23 18:38:29 +02:00
if (totalSize > maximum_size) {
DEBUG_FUNCTION_LINE("Couldn't load setup module because it's too big.");
return {};
}
2021-09-17 16:22:54 +02:00
auto address = (uint32_t) psec->get_address();
2020-04-28 14:43:07 +02:00
destinations[psec->get_index()] = (uint8_t *) baseOffset;
uint32_t destination = baseOffset + address;
2020-05-17 19:05:51 +02:00
if ((address >= 0x02000000) && address < 0x10000000) {
2020-04-28 14:43:07 +02:00
destination -= 0x02000000;
destinations[psec->get_index()] -= 0x02000000;
baseOffset += sectionSize;
offset_data += sectionSize;
2020-05-17 19:05:51 +02:00
} else if ((address >= 0x10000000) && address < 0xC0000000) {
2020-04-28 14:43:07 +02:00
destination -= 0x10000000;
destinations[psec->get_index()] -= 0x10000000;
2020-05-17 19:05:51 +02:00
} else if (address >= 0xC0000000) {
2020-04-28 14:43:07 +02:00
destination -= 0xC0000000;
destinations[psec->get_index()] -= 0xC0000000;
} else {
DEBUG_FUNCTION_LINE("Unhandled case");
free(destinations);
return std::nullopt;
2020-04-28 14:43:07 +02:00
}
2020-05-17 19:05:51 +02:00
const char *p = reader.sections[i]->get_data();
2020-04-28 14:43:07 +02:00
2020-05-17 19:05:51 +02:00
if (psec->get_type() == SHT_NOBITS) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("memset section %s %08X to 0 (%d bytes)", psec->get_name().c_str(), destination, sectionSize);
2020-05-17 19:05:51 +02:00
memset((void *) destination, 0, sectionSize);
} else if (psec->get_type() == SHT_PROGBITS) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("Copy section %s %08X -> %08X (%d bytes)", psec->get_name().c_str(), p, destination, sectionSize);
2020-05-17 19:05:51 +02:00
memcpy((void *) destination, p, sectionSize);
2020-04-28 14:43:07 +02:00
}
//nextAddress = ROUNDUP(destination + sectionSize, 0x100);
2021-09-17 16:22:54 +02:00
if (psec->get_name() == ".bss") {
2021-12-07 18:23:18 +01:00
moduleData->setBSSLocation(destination, sectionSize);
2021-09-17 16:22:54 +02:00
memset(reinterpret_cast<void *>(destination), 0, sectionSize);
} else if (psec->get_name() == ".sbss") {
2021-12-07 18:23:18 +01:00
moduleData->setSBSSLocation(destination, sectionSize);
2021-09-17 16:22:54 +02:00
memset(reinterpret_cast<void *>(destination), 0, sectionSize);
2020-04-28 14:43:07 +02:00
}
2021-12-07 18:23:18 +01:00
auto sectionInfo = std::make_shared<SectionInfo>(psec->get_name(), destination, sectionSize);
moduleData->addSectionInfo(sectionInfo);
DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize);
2020-05-17 19:05:51 +02:00
if (endAddress < destination + sectionSize) {
endAddress = destination + sectionSize;
}
2020-05-17 19:05:51 +02:00
DCFlushRange((void *) destination, sectionSize);
ICInvalidateRange((void *) destination, sectionSize);
2020-04-28 14:43:07 +02:00
}
}
2020-05-17 19:05:51 +02:00
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
2020-04-28 14:43:07 +02:00
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
2020-05-17 19:05:51 +02:00
DEBUG_FUNCTION_LINE("Linking (%d)... %s", i, psec->get_name().c_str());
2022-01-27 12:52:45 +01:00
if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], offset_text, offset_data, trampoline_data, trampoline_data_length)) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("elfLink failed");
free(destinations);
return std::nullopt;
2020-04-28 14:43:07 +02:00
}
}
}
2021-12-07 18:23:18 +01:00
auto relocationData = getImportRelocationData(reader, destinations);
2020-04-28 14:43:07 +02:00
2022-02-04 21:44:03 +01:00
for (auto const &reloc : relocationData) {
2021-12-07 18:23:18 +01:00
moduleData->addRelocationData(reloc);
2020-04-28 14:43:07 +02:00
}
2021-12-07 18:23:18 +01:00
auto secInfo = moduleData->getSectionInfo(".wums.exports");
if (secInfo && secInfo.value()->getSize() > 0) {
size_t entries_count = secInfo.value()->getSize() / sizeof(wums_entry_t);
2022-02-04 21:44:03 +01:00
auto *entries = (wums_entry_t *) secInfo.value()->getAddress();
2021-09-17 16:22:54 +02:00
if (entries != nullptr) {
for (size_t j = 0; j < entries_count; j++) {
2020-06-07 14:18:34 +02:00
wums_entry_t *exp = &entries[j];
2022-02-04 21:44:03 +01:00
DEBUG_FUNCTION_LINE("Saving export of type %08X, name %s, target: %08X" /*,pluginData.getPluginInformation()->getName().c_str()*/, exp->type, exp->name, (void *) exp->address);
2021-12-07 18:23:18 +01:00
auto exportData = std::make_shared<ExportData>(exp->type, exp->name, exp->address);
moduleData->addExportData(exportData);
}
}
}
2021-12-07 18:23:18 +01:00
secInfo = moduleData->getSectionInfo(".wums.hooks");
if (secInfo && secInfo.value()->getSize() > 0) {
size_t entries_count = secInfo.value()->getSize() / sizeof(wums_hook_t);
2022-02-04 21:44:03 +01:00
auto *hooks = (wums_hook_t *) secInfo.value()->getAddress();
2021-09-17 16:22:54 +02:00
if (hooks != nullptr) {
for (size_t j = 0; j < entries_count; j++) {
2020-06-07 14:18:34 +02:00
wums_hook_t *hook = &hooks[j];
2022-02-04 21:44:03 +01:00
DEBUG_FUNCTION_LINE("Saving hook of type %08X, target: %08X" /*,pluginData.getPluginInformation()->getName().c_str()*/, hook->type, hook->target);
2021-12-07 18:23:18 +01:00
auto hookData = std::make_shared<HookData>(hook->type, hook->target);
moduleData->addHookData(hookData);
}
}
}
2021-12-07 18:23:18 +01:00
secInfo = moduleData->getSectionInfo(".wums.meta");
if (secInfo && secInfo.value()->getSize() > 0) {
auto *entries = (wums_entry_t *) secInfo.value()->getAddress();
2021-09-17 16:22:54 +02:00
if (entries != nullptr) {
2021-12-07 18:23:18 +01:00
char *curEntry = (char *) secInfo.value()->getAddress();
while ((uint32_t) curEntry < (uint32_t) secInfo.value()->getAddress() + secInfo.value()->getSize()) {
if (*curEntry == '\0') {
curEntry++;
continue;
}
2021-09-17 16:22:54 +02:00
auto firstFound = std::string(curEntry).find_first_of('=');
if (firstFound != std::string::npos) {
curEntry[firstFound] = '\0';
std::string key(curEntry);
std::string value(curEntry + firstFound + 1);
if (key == "export_name") {
DEBUG_FUNCTION_LINE("export_name = %s", value.c_str());
2021-12-07 18:23:18 +01:00
moduleData->setExportName(value);
2022-01-23 22:07:38 +01:00
} else if (key == "skipInitFini") {
if (value == "true") {
2022-01-23 22:07:38 +01:00
DEBUG_FUNCTION_LINE("skipInitFini = %s", value.c_str());
moduleData->setSkipInitFini(true);
} else {
2022-01-23 22:07:38 +01:00
moduleData->setSkipInitFini(false);
}
} else if (key == "initBeforeRelocationDoneHook") {
if (value == "true") {
DEBUG_FUNCTION_LINE("initBeforeRelocationDoneHook = %s", value.c_str());
2021-12-07 18:23:18 +01:00
moduleData->setInitBeforeRelocationDoneHook(true);
} else {
2021-12-07 18:23:18 +01:00
moduleData->setInitBeforeRelocationDoneHook(false);
}
2021-09-17 16:22:54 +02:00
} else if (key == "wums") {
if (value != "0.3") {
DEBUG_FUNCTION_LINE("Warning: Ignoring module - Unsupported WUMS version: %s.\n", value.c_str());
return std::nullopt;
}
}
}
curEntry += strlen(curEntry) + 1;
}
}
}
2022-02-04 21:44:03 +01:00
char *strTable = (char *) endAddress;
uint32_t strOffset = 0;
// Get the symbol for functions.
Elf_Half n = reader.sections.size();
for (Elf_Half i = 0; i < n; ++i) {
section *sec = reader.sections[i];
if (SHT_SYMTAB == sec->get_type()) {
symbol_section_accessor symbols(reader, sec);
auto sym_no = (uint32_t) symbols.get_symbols_num();
if (sym_no > 0) {
for (Elf_Half j = 0; j < sym_no; ++j) {
std::string name;
2022-02-04 21:44:03 +01:00
Elf64_Addr value = 0;
Elf_Xword size = 0;
unsigned char bind = 0;
unsigned char type = 0;
Elf_Half section = 0;
unsigned char other = 0;
if (symbols.get_symbol(j, name, value, size, bind, type, section, other)) {
if (type == STT_FUNC) { // We only care about functions.
auto sectionVal = reader.sections[section];
2022-02-04 21:44:03 +01:00
auto offsetVal = value - sectionVal->get_address();
2021-12-07 18:23:18 +01:00
auto sectionOpt = moduleData->getSectionInfo(sectionVal->get_name());
if (!sectionOpt.has_value()) {
continue;
}
2021-12-07 18:23:18 +01:00
auto finalAddress = offsetVal + sectionOpt.value()->getAddress();
uint32_t stringSize = name.size() + 1;
memcpy(strTable + strOffset, name.c_str(), stringSize);
2021-12-07 18:23:18 +01:00
moduleData->addFunctionSymbolData(std::make_shared<FunctionSymbolData>(strTable + strOffset, (void *) finalAddress, (uint32_t) size));
strOffset += stringSize;
totalSize += stringSize;
endAddress += stringSize;
}
}
}
break;
}
}
}
2020-06-07 14:18:34 +02:00
DCFlushRange((void *) *destination_address_ptr, totalSize);
ICInvalidateRange((void *) *destination_address_ptr, totalSize);
2020-04-28 14:43:07 +02:00
free(destinations);
2021-12-07 18:23:18 +01:00
moduleData->setEntrypoint(entrypoint);
moduleData->setStartAddress(*destination_address_ptr);
moduleData->setEndAddress(endAddress);
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint);
DEBUG_FUNCTION_LINE("Saved startAddress as %08X", *destination_address_ptr);
DEBUG_FUNCTION_LINE("Saved endAddress as %08X", endAddress);
2020-04-28 14:43:07 +02:00
*destination_address_ptr = (*destination_address_ptr + totalSize + 0x100) & 0xFFFFFF00;
2020-04-28 14:43:07 +02:00
return moduleData;
}
2021-12-07 18:23:18 +01:00
std::vector<std::shared_ptr<RelocationData>> ModuleDataFactory::getImportRelocationData(elfio &reader, uint8_t **destinations) {
std::vector<std::shared_ptr<RelocationData>> result;
2020-05-17 19:05:51 +02:00
std::map<uint32_t, std::string> infoMap;
2020-04-28 14:43:07 +02:00
uint32_t sec_num = reader.sections.size();
2020-05-17 19:05:51 +02:00
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
2020-04-28 14:43:07 +02:00
if (psec->get_type() == 0x80000002) {
infoMap[i] = psec->get_name();
}
}
2020-05-17 19:05:51 +02:00
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());
2020-04-28 14:43:07 +02:00
relocation_section_accessor rel(reader, psec);
2020-05-17 19:05:51 +02:00
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)) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("Failed to get relocation");
break;
}
2020-06-07 14:17:02 +02:00
// uint32_t adjusted_sym_value = (uint32_t) sym_value;
2020-06-07 14:18:34 +02:00
if (infoMap.count(sym_section_index) == 0) {
2020-04-28 14:43:07 +02:00
continue;
}
2021-12-07 18:23:18 +01:00
auto rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]);
2020-05-17 19:05:51 +02:00
if (!rplInfo) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("Failed to create import information");
break;
}
uint32_t section_index = psec->get_info();
// When these relocations are performed, we don't need the 0xC0000000 offset anymore.
2021-12-07 18:23:18 +01:00
auto relocationData = std::make_shared<RelocationData>(type, offset - 0x02000000, addend, (void *) (destinations[section_index] + 0x02000000), sym_name, rplInfo.value());
2020-04-28 14:43:07 +02:00
//relocationData->printInformation();
result.push_back(relocationData);
}
}
}
return result;
}
2020-05-17 19:05:51 +02:00
2022-01-27 12:52:45 +01:00
bool ModuleDataFactory::linkSection(elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampoline_entry_t *trampoline_data,
uint32_t trampoline_data_length) {
2020-04-28 14:43:07 +02:00
uint32_t sec_num = reader.sections.size();
2020-05-17 19:05:51 +02:00
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());
2020-04-28 14:43:07 +02:00
relocation_section_accessor rel(reader, psec);
2020-05-17 19:05:51 +02:00
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)) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("Failed to get relocation");
break;
}
2021-09-17 16:22:54 +02:00
auto adjusted_sym_value = (uint32_t) sym_value;
2020-05-17 19:05:51 +02:00
if ((adjusted_sym_value >= 0x02000000) && adjusted_sym_value < 0x10000000) {
2020-04-28 14:43:07 +02:00
adjusted_sym_value -= 0x02000000;
adjusted_sym_value += base_text;
2020-05-17 19:05:51 +02:00
} else if ((adjusted_sym_value >= 0x10000000) && adjusted_sym_value < 0xC0000000) {
2020-04-28 14:43:07 +02:00
adjusted_sym_value -= 0x10000000;
adjusted_sym_value += base_data;
2020-05-17 19:05:51 +02:00
} else if (adjusted_sym_value >= 0xC0000000) {
2020-04-28 14:43:07 +02:00
// Skip imports
continue;
2020-05-17 19:05:51 +02:00
} else if (adjusted_sym_value == 0x0) {
2020-04-28 14:43:07 +02:00
//
} else {
2020-05-17 19:05:51 +02:00
DEBUG_FUNCTION_LINE("Unhandled case %08X", adjusted_sym_value);
2020-04-28 14:43:07 +02:00
return false;
}
2020-05-17 19:05:51 +02:00
if (sym_section_index == SHN_ABS) {
2020-04-28 14:43:07 +02:00
//
2020-05-17 19:05:51 +02:00
} else if (sym_section_index > SHN_LORESERVE) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("NOT IMPLEMENTED: %04X", sym_section_index);
return false;
}
2022-01-27 12:52:45 +01:00
if (!ElfUtils::elfLinkOne(type, offset, addend, destination, adjusted_sym_value, trampoline_data, trampoline_data_length, RELOC_TYPE_FIXED)) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE("Link failed");
return false;
}
}
DEBUG_FUNCTION_LINE("done");
}
}
return true;
}