2020-04-29 18:02:36 +02:00
|
|
|
/****************************************************************************
|
2020-12-26 14:17:50 +01:00
|
|
|
* Copyright (C) 2018-2020 Maschell
|
2020-04-29 18:02:36 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
#include "PluginInformationFactory.h"
|
|
|
|
#include "../utils/ElfUtils.h"
|
|
|
|
#include "../utils/utils.h"
|
2023-01-04 02:05:57 +01:00
|
|
|
#include "utils/membuf.hpp"
|
|
|
|
#include "utils/wiiu_zlib.hpp"
|
2022-02-04 16:25:44 +01:00
|
|
|
#include <coreinit/cache.h>
|
|
|
|
#include <map>
|
2021-12-15 17:09:30 +01:00
|
|
|
#include <memory>
|
2020-04-29 18:02:36 +02:00
|
|
|
#include <string>
|
2022-02-11 22:18:15 +01:00
|
|
|
#include <wups/function_patching.h>
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
using namespace ELFIO;
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
std::optional<std::unique_ptr<PluginInformation>>
|
|
|
|
PluginInformationFactory::load(const std::shared_ptr<PluginData> &pluginData, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length,
|
2022-01-27 12:47:33 +01:00
|
|
|
uint8_t trampolineId) {
|
2022-05-14 14:00:20 +02:00
|
|
|
if (!pluginData->buffer) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Buffer was nullptr");
|
2022-05-14 14:00:20 +02:00
|
|
|
return {};
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
2023-01-04 02:05:57 +01:00
|
|
|
elfio reader(new wiiu_zlib);
|
|
|
|
membuf sbuf((char *) pluginData->buffer.get(), (char *) pluginData->buffer.get() + pluginData->length);
|
|
|
|
std::istream in(&sbuf);
|
|
|
|
|
|
|
|
if (!reader.load(in)) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Can't process PluginData in elfio");
|
2022-05-14 14:00:20 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pluginInfo = make_unique_nothrow<PluginInformation>();
|
|
|
|
if (!pluginInfo) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to allocate PluginInformation");
|
|
|
|
return {};
|
2020-06-03 18:34:31 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
uint32_t sec_num = reader.sections.size();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
auto destinations = make_unique_nothrow<uint8_t *[]>(sec_num);
|
|
|
|
if (!destinations) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed alloc memory for destinations array");
|
|
|
|
return {};
|
|
|
|
}
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
uint32_t totalSize = 0;
|
|
|
|
|
|
|
|
uint32_t text_size = 0;
|
|
|
|
uint32_t data_size = 0;
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
2020-06-03 18:34:31 +02:00
|
|
|
section *psec = reader.sections[i];
|
2020-04-29 18:02:36 +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();
|
2022-02-04 16:25:44 +01:00
|
|
|
auto address = (uint32_t) psec->get_address();
|
2020-05-03 12:30:15 +02:00
|
|
|
if ((address >= 0x02000000) && address < 0x10000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
text_size += sectionSize;
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if ((address >= 0x10000000) && address < 0xC0000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
data_size += sectionSize;
|
|
|
|
}
|
2022-05-14 14:01:52 +02:00
|
|
|
if (psec->get_name().starts_with(".wups.")) {
|
2020-05-03 13:51:39 +02:00
|
|
|
data_size += sectionSize;
|
|
|
|
}
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
auto text_data = make_unique_nothrow<uint8_t[]>(text_size);
|
|
|
|
if (!text_data) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to alloc memory for the .text section (%d bytes)", text_size);
|
|
|
|
return {};
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Allocated %d kb", text_size / 1024);
|
|
|
|
|
|
|
|
auto data_data = make_unique_nothrow<uint8_t[]>(data_size);
|
|
|
|
if (!data_data) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to alloc memory for the .data section (%d bytes)", data_size);
|
|
|
|
return {};
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Allocated %d kb", data_size / 1024);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
2020-06-03 18:34:31 +02:00
|
|
|
section *psec = reader.sections[i];
|
2022-05-14 14:01:52 +02:00
|
|
|
if (psec->get_type() == 0x80000002 || psec->get_name() == ".wut_load_bounds") {
|
2020-04-29 18:02:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
|
|
|
|
uint32_t sectionSize = psec->get_size();
|
2022-02-04 16:25:44 +01:00
|
|
|
auto address = (uint32_t) psec->get_address();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
uint32_t destination = address;
|
2020-05-03 12:30:15 +02:00
|
|
|
if ((address >= 0x02000000) && address < 0x10000000) {
|
2022-05-14 14:00:20 +02:00
|
|
|
destination += (uint32_t) text_data.get();
|
2020-04-29 18:02:36 +02:00
|
|
|
destination -= 0x02000000;
|
2022-05-14 14:00:20 +02:00
|
|
|
destinations[psec->get_index()] = (uint8_t *) text_data.get();
|
2022-05-14 14:01:52 +02:00
|
|
|
|
|
|
|
if (destination + sectionSize > (uint32_t) text_data.get() + text_size) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Tried to overflow .text buffer. %08X > %08X", destination + sectionSize, (uint32_t) text_data.get() + text_size);
|
|
|
|
OSFatal("WUPSLoader: Tried to overflow buffer");
|
|
|
|
}
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if ((address >= 0x10000000) && address < 0xC0000000) {
|
2022-05-14 14:00:20 +02:00
|
|
|
destination += (uint32_t) data_data.get();
|
2020-04-29 18:02:36 +02:00
|
|
|
destination -= 0x10000000;
|
2022-05-14 14:00:20 +02:00
|
|
|
destinations[psec->get_index()] = (uint8_t *) data_data.get();
|
2022-05-14 14:01:52 +02:00
|
|
|
|
|
|
|
if (destination + sectionSize > (uint32_t) data_data.get() + data_size) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Tried to overflow .data buffer. %08X > %08X", destination + sectionSize, (uint32_t) data_data.get() + data_size);
|
|
|
|
OSFatal("WUPSLoader: Tried to overflow buffer");
|
|
|
|
}
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (address >= 0xC0000000) {
|
2022-05-14 14:01:52 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Loading section from 0xC0000000 is NOT supported");
|
|
|
|
return std::nullopt;
|
2020-04-29 18:02:36 +02:00
|
|
|
} else {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Unhandled case");
|
2020-04-29 18:02:36 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
const char *p = psec->get_data();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
if (psec->get_type() == SHT_NOBITS) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("memset section %s %08X to 0 (%d bytes)", psec->get_name().c_str(), destination, sectionSize);
|
2020-05-03 12:30:15 +02:00
|
|
|
memset((void *) destination, 0, sectionSize);
|
|
|
|
} else if (psec->get_type() == SHT_PROGBITS) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Copy section %s %08X -> %08X (%d bytes)", psec->get_name().c_str(), p, destination, sectionSize);
|
2020-05-03 12:30:15 +02:00
|
|
|
memcpy((void *) destination, p, sectionSize);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
auto sectionInfo = make_unique_nothrow<SectionInfo>(psec->get_name(), destination, sectionSize);
|
|
|
|
if (!sectionInfo) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to allocat SectionInfo");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginInfo->addSectionInfo(std::move(sectionInfo));
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
totalSize += sectionSize;
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
DCFlushRange((void *) destination, sectionSize);
|
|
|
|
ICInvalidateRange((void *) destination, sectionSize);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
2020-06-03 18:34:31 +02:00
|
|
|
section *psec = reader.sections[i];
|
2020-04-29 18:02:36 +02:00
|
|
|
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Linking (%d)... %s at %08X", i, psec->get_name().c_str(), destinations[psec->get_index()]);
|
2022-05-14 14:00:20 +02:00
|
|
|
if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], (uint32_t) text_data.get(), (uint32_t) data_data.get(), trampoline_data, trampoline_data_length,
|
2022-01-27 12:47:33 +01:00
|
|
|
trampolineId)) {
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("linkSection failed");
|
|
|
|
return {};
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
if (!PluginInformationFactory::addImportRelocationData(pluginInfo, reader, destinations)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("addImportRelocationData failed");
|
|
|
|
return {};
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
DCFlushRange((void *) text_data.get(), text_size);
|
|
|
|
ICInvalidateRange((void *) text_data.get(), text_size);
|
|
|
|
DCFlushRange((void *) data_data.get(), data_size);
|
|
|
|
ICInvalidateRange((void *) data_data.get(), data_size);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-01-27 12:47:33 +01:00
|
|
|
pluginInfo->setTrampolineId(trampolineId);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2021-12-15 17:09:30 +01:00
|
|
|
auto secInfo = pluginInfo->getSectionInfo(".wups.hooks");
|
|
|
|
if (secInfo && secInfo.value()->getSize() > 0) {
|
|
|
|
size_t entries_count = secInfo.value()->getSize() / sizeof(wups_loader_hook_t);
|
2022-02-04 16:25:44 +01:00
|
|
|
auto *entries = (wups_loader_hook_t *) secInfo.value()->getAddress();
|
2020-12-26 14:17:50 +01:00
|
|
|
if (entries != nullptr) {
|
2020-05-03 12:30:15 +02:00
|
|
|
for (size_t j = 0; j < entries_count; j++) {
|
|
|
|
wups_loader_hook_t *hook = &entries[j];
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Saving hook of plugin Type: %08X, target: %08X", hook->type, (void *) hook->target);
|
|
|
|
auto hookData = make_unique_nothrow<HookData>((void *) hook->target, hook->type);
|
|
|
|
if (!hookData) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to allocate HookData");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
pluginInfo->addHookData(std::move(hookData));
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 17:09:30 +01:00
|
|
|
secInfo = pluginInfo->getSectionInfo(".wups.load");
|
|
|
|
if (secInfo && secInfo.value()->getSize() > 0) {
|
|
|
|
size_t entries_count = secInfo.value()->getSize() / sizeof(wups_loader_entry_t);
|
2022-02-04 16:25:44 +01:00
|
|
|
auto *entries = (wups_loader_entry_t *) secInfo.value()->getAddress();
|
2021-09-25 14:26:18 +02:00
|
|
|
if (entries != nullptr) {
|
2020-05-03 12:30:15 +02:00
|
|
|
for (size_t j = 0; j < entries_count; j++) {
|
|
|
|
wups_loader_entry_t *cur_function = &entries[j];
|
2021-09-25 14:26:18 +02:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Saving function \"%s\" of plugin . PA:%08X VA:%08X Library: %08X, target: %08X, call_addr: %08X",
|
2022-02-04 16:25:44 +01:00
|
|
|
cur_function->_function.name /*,pluginData->getPluginInformation()->getName().c_str()*/,
|
2021-09-25 14:26:18 +02:00
|
|
|
cur_function->_function.physical_address, cur_function->_function.virtual_address, cur_function->_function.library, cur_function->_function.target,
|
|
|
|
(void *) cur_function->_function.call_addr);
|
2022-05-14 14:00:20 +02:00
|
|
|
|
|
|
|
auto functionData = make_unique_nothrow<FunctionData>((void *) cur_function->_function.physical_address,
|
|
|
|
(void *) cur_function->_function.virtual_address,
|
|
|
|
cur_function->_function.name,
|
|
|
|
(function_replacement_library_type_t) cur_function->_function.library,
|
|
|
|
(void *) cur_function->_function.target,
|
|
|
|
(void *) cur_function->_function.call_addr,
|
|
|
|
(FunctionPatcherTargetProcess) cur_function->_function.targetProcess);
|
|
|
|
if (!functionData) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to allocate FunctionData");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
pluginInfo->addFunctionData(std::move(functionData));
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 17:04:30 +01:00
|
|
|
// 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 16:25:44 +01:00
|
|
|
Elf64_Addr value = 0;
|
|
|
|
Elf_Xword size = 0;
|
|
|
|
unsigned char bind = 0;
|
|
|
|
unsigned char type = 0;
|
|
|
|
Elf_Half section = 0;
|
2021-12-15 17:04:30 +01:00
|
|
|
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 16:25:44 +01:00
|
|
|
auto offsetVal = value - sectionVal->get_address();
|
2021-12-15 17:09:30 +01:00
|
|
|
auto sectionOpt = pluginInfo->getSectionInfo(sectionVal->get_name());
|
2021-12-15 17:04:30 +01:00
|
|
|
if (!sectionOpt.has_value()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
auto finalAddress = offsetVal + sectionOpt.value()->getAddress();
|
|
|
|
auto functionSymbolData = make_unique_nothrow<FunctionSymbolData>(name, (void *) finalAddress, (uint32_t) size);
|
|
|
|
if (!functionSymbolData) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to allocate FunctionSymbolData");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
pluginInfo->addFunctionSymbolData(std::move(functionSymbolData));
|
2021-12-15 17:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
if (totalSize > text_size + data_size) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("We didn't allocate enough memory!!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-12-15 17:04:30 +01:00
|
|
|
// Save the addresses for the allocated memory. This way we can free it again :)
|
2022-05-14 14:00:20 +02:00
|
|
|
pluginInfo->allocatedDataMemoryAddress = std::move(data_data);
|
|
|
|
pluginInfo->allocatedTextMemoryAddress = std::move(text_data);
|
2020-05-03 12:23:50 +02:00
|
|
|
|
2020-04-29 18:02:36 +02:00
|
|
|
return pluginInfo;
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
bool PluginInformationFactory::addImportRelocationData(const std::unique_ptr<PluginInformation> &pluginInfo, const elfio &reader, const std::unique_ptr<uint8_t *[]> &destinations) {
|
|
|
|
std::map<uint32_t, std::shared_ptr<ImportRPLInformation>> infoMap;
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-06-03 18:34:31 +02:00
|
|
|
uint32_t sec_num = reader.sections.size();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
2022-05-14 14:00:20 +02:00
|
|
|
auto *psec = reader.sections[i];
|
2020-04-29 18:02:36 +02:00
|
|
|
if (psec->get_type() == 0x80000002) {
|
2022-05-14 14:00:20 +02:00
|
|
|
auto info = make_shared_nothrow<ImportRPLInformation>(psec->get_name());
|
|
|
|
if (!info) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
infoMap[i] = std::move(info);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
2020-06-03 18:34:31 +02:00
|
|
|
section *psec = reader.sections[i];
|
2020-05-03 12:30:15 +02:00
|
|
|
if (psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Found relocation section %s", psec->get_name().c_str());
|
2020-06-03 18:34:31 +02:00
|
|
|
relocation_section_accessor rel(reader, psec);
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
|
2023-01-04 02:05:57 +01:00
|
|
|
Elf_Word symbol = 0;
|
2020-05-03 12:30:15 +02:00
|
|
|
Elf64_Addr offset;
|
|
|
|
Elf_Word type;
|
|
|
|
Elf_Sxword addend;
|
|
|
|
std::string sym_name;
|
|
|
|
Elf64_Addr sym_value;
|
|
|
|
|
2023-01-04 02:05:57 +01:00
|
|
|
if (!rel.get_entry(j, offset, symbol, type, addend)) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get relocation");
|
2022-05-14 14:00:20 +02:00
|
|
|
return false;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
2023-01-04 02:05:57 +01:00
|
|
|
symbol_section_accessor symbols(reader, reader.sections[(Elf_Half) psec->get_link()]);
|
|
|
|
|
|
|
|
// Find the symbol
|
|
|
|
Elf_Xword size;
|
|
|
|
unsigned char bind;
|
|
|
|
unsigned char symbolType;
|
|
|
|
Elf_Half sym_section_index;
|
|
|
|
unsigned char other;
|
|
|
|
|
|
|
|
if (!symbols.get_symbol(symbol, sym_name, sym_value, size,
|
|
|
|
bind, symbolType, sym_section_index, other)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get symbol");
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
auto adjusted_sym_value = (uint32_t) sym_value;
|
2020-05-03 12:30:15 +02:00
|
|
|
if (adjusted_sym_value < 0xC0000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
uint32_t section_index = psec->get_info();
|
|
|
|
if (!infoMap.contains(sym_section_index)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Relocation is referencing a unknown section. %d destination: %08X sym_name %s", section_index, destinations[section_index], sym_name.c_str());
|
|
|
|
return false;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
auto relocationData = make_unique_nothrow<RelocationData>(type,
|
|
|
|
offset - 0x02000000,
|
|
|
|
addend,
|
|
|
|
(void *) (destinations[section_index]),
|
|
|
|
sym_name,
|
|
|
|
infoMap[sym_section_index]);
|
|
|
|
if (!relocationData) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to allocate RelocationData");
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
pluginInfo->addRelocationData(std::move(relocationData));
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
return true;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
2020-05-03 12:30:15 +02:00
|
|
|
|
2022-01-27 12:47:33 +01:00
|
|
|
bool PluginInformationFactory::linkSection(const 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,
|
|
|
|
uint8_t trampolineId) {
|
2020-06-03 18:34:31 +02:00
|
|
|
uint32_t sec_num = reader.sections.size();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
2020-06-03 18:34:31 +02:00
|
|
|
section *psec = reader.sections[i];
|
2020-05-03 12:30:15 +02:00
|
|
|
if (psec->get_info() == section_index) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Found relocation section %s", psec->get_name().c_str());
|
2020-06-03 18:34:31 +02:00
|
|
|
relocation_section_accessor rel(reader, psec);
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
|
2023-01-04 02:05:57 +01:00
|
|
|
Elf_Word symbol = 0;
|
2020-05-03 12:30:15 +02:00
|
|
|
Elf64_Addr offset;
|
|
|
|
Elf_Word type;
|
|
|
|
Elf_Sxword addend;
|
|
|
|
std::string sym_name;
|
|
|
|
Elf64_Addr sym_value;
|
|
|
|
|
2023-01-04 02:05:57 +01:00
|
|
|
if (!rel.get_entry(j, offset, symbol, type, addend)) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get relocation");
|
2023-01-04 02:05:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
symbol_section_accessor symbols(reader, reader.sections[(Elf_Half) psec->get_link()]);
|
|
|
|
|
|
|
|
// Find the symbol
|
|
|
|
Elf_Xword size;
|
|
|
|
unsigned char bind;
|
|
|
|
unsigned char symbolType;
|
|
|
|
Elf_Half sym_section_index;
|
|
|
|
unsigned char other;
|
|
|
|
|
|
|
|
if (!symbols.get_symbol(symbol, sym_name, sym_value, size,
|
|
|
|
bind, symbolType, sym_section_index, other)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get symbol");
|
|
|
|
return false;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
auto adjusted_sym_value = (uint32_t) sym_value;
|
2020-05-03 12:30:15 +02:00
|
|
|
if ((adjusted_sym_value >= 0x02000000) && adjusted_sym_value < 0x10000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
adjusted_sym_value -= 0x02000000;
|
|
|
|
adjusted_sym_value += base_text;
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if ((adjusted_sym_value >= 0x10000000) && adjusted_sym_value < 0xC0000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
adjusted_sym_value -= 0x10000000;
|
|
|
|
adjusted_sym_value += base_data;
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (adjusted_sym_value >= 0xC0000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
//DEBUG_FUNCTION_LINE("Skip imports");
|
|
|
|
// Skip imports
|
|
|
|
continue;
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (adjusted_sym_value == 0x0) {
|
2020-04-29 18:02:36 +02:00
|
|
|
//
|
|
|
|
} else {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Unhandled case %08X", adjusted_sym_value);
|
2020-04-29 18:02:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
auto adjusted_offset = (uint32_t) offset;
|
2020-05-03 12:30:15 +02:00
|
|
|
if ((offset >= 0x02000000) && offset < 0x10000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
adjusted_offset -= 0x02000000;
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if ((adjusted_offset >= 0x10000000) && adjusted_offset < 0xC0000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
adjusted_offset -= 0x10000000;
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (adjusted_offset >= 0xC0000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
adjusted_offset -= 0xC0000000;
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
if (sym_section_index == SHN_ABS) {
|
2020-04-29 18:02:36 +02:00
|
|
|
//
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (sym_section_index > SHN_LORESERVE) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED: %04X", sym_section_index);
|
2020-04-29 18:02:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-15 17:04:30 +01:00
|
|
|
// DEBUG_FUNCTION_LINE_VERBOSE("sym_value %08X adjusted_sym_value %08X offset %08X adjusted_offset %08X", (uint32_t) sym_value, adjusted_sym_value, (uint32_t) offset, adjusted_offset);
|
2021-09-25 14:26:18 +02:00
|
|
|
|
2022-01-27 12:47:33 +01:00
|
|
|
if (!ElfUtils::elfLinkOne(type, adjusted_offset, addend, destination, adjusted_sym_value, trampoline_data, trampoline_data_length, RELOC_TYPE_FIXED, trampolineId)) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Link failed");
|
2020-04-29 18:02:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("done");
|
2020-04-29 18:02:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|