Patch Kernel to resolve symbols names of plugins

This commit is contained in:
Maschell
2021-12-15 17:04:30 +01:00
parent 719ae8e4da
commit 64f78ccec4
10 changed files with 292 additions and 12 deletions

View File

@@ -0,0 +1,55 @@
/****************************************************************************
* Copyright (C) 2021 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/>.
****************************************************************************/
#pragma once
#include <string>
class FunctionSymbolData {
public:
FunctionSymbolData(const FunctionSymbolData &o2) = default;
FunctionSymbolData(std::string &name, void *address, uint32_t size) :
name(name),
address(address),
size(size) {
}
bool operator<(const FunctionSymbolData &rhs) const {
return (uint32_t) address < (uint32_t) rhs.address; //assume that you compare the record based on a
}
virtual ~FunctionSymbolData() = default;
[[nodiscard]] const std::string &getName() const {
return name;
}
[[nodiscard]] void *getAddress() const {
return address;
}
[[nodiscard]] uint32_t getSize() const {
return size;
}
private:
std::string name;
void *address;
uint32_t size;
};

View File

@@ -7,7 +7,7 @@
#include "PluginDataPersistence.h"
#include "DynamicLinkingHelper.h"
bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformation, PluginContainer &plugin) {
bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformation, PluginContainer &plugin, MEMHeapHandle heapHandle) {
int32_t plugin_count = pluginInformation->number_used_plugins;
auto pluginName = plugin.getMetaInformation().getName();
@@ -160,6 +160,48 @@ bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformat
plugin_data->info.allocatedDataMemoryAddress = pluginInfo.allocatedDataMemoryAddress;
uint32_t entryCount = pluginInfo.getFunctionSymbolDataList().size();
if (entryCount > 0) {
/* Saving SectionInfos */
uint32_t funcSymStringLen = 1;
for (auto &curFuncSym: pluginInfo.getFunctionSymbolDataList()) {
funcSymStringLen += curFuncSym.getName().length() + 1;
}
char *stringTable = (char *) MEMAllocFromExpHeapEx(heapHandle, funcSymStringLen, 0x4);
if (stringTable == nullptr) {
DEBUG_FUNCTION_LINE("Failed alloc memory to store string table for function symbol data");
return false;
}
memset(stringTable, 0, funcSymStringLen);
DEBUG_FUNCTION_LINE("Allocated %d for the function symbol string table", funcSymStringLen);
auto *entryTable = (plugin_function_symbol_data_t *) MEMAllocFromExpHeapEx(heapHandle, entryCount * sizeof(plugin_function_symbol_data_t), 0x4);
if (entryTable == nullptr) {
MEMFreeToExpHeap((MEMHeapHandle) heapHandle, stringTable);
free(stringTable);
DEBUG_FUNCTION_LINE("Failed alloc memory to store function symbol data");
return false;
}
DEBUG_FUNCTION_LINE("Allocated %d for the function symbol data", entryCount * sizeof(plugin_function_symbol_data_t));
uint32_t curStringOffset = 0;
uint32_t curEntryIndex = 0;
for (auto &curFuncSym: pluginInfo.getFunctionSymbolDataList()) {
entryTable[curEntryIndex].address = curFuncSym.getAddress();
entryTable[curEntryIndex].name = &stringTable[curStringOffset];
entryTable[curEntryIndex].size = curFuncSym.getSize();
auto len = curFuncSym.getName().length() + 1;
memcpy(stringTable + curStringOffset, curFuncSym.getName().c_str(), len);
curStringOffset += len;
curEntryIndex++;
}
plugin_data->info.allocatedFuncSymStringTableAddress = stringTable;
plugin_data->info.function_symbol_data = entryTable;
}
plugin_data->info.number_function_symbol_data = entryCount;
/* Copy plugin data */
auto pluginData = plugin.getPluginData();
auto plugin_data_data = &plugin_data->data;
@@ -239,13 +281,13 @@ std::vector<PluginContainer> PluginContainerPersistence::loadPlugins(plugin_info
}
bool storageHasId = true;
for(auto const &value : curPluginInformation.getHookDataList()){
if(value.getType() == WUPS_LOADER_HOOK_INIT_STORAGE &&
metaInformation.getStorageId().empty()){
for (auto const &value: curPluginInformation.getHookDataList()) {
if (value.getType() == WUPS_LOADER_HOOK_INIT_STORAGE &&
metaInformation.getStorageId().empty()) {
storageHasId = false;
}
}
if(!storageHasId){
if (!storageHasId) {
DEBUG_FUNCTION_LINE("Plugin is using the storage API but has not set an ID");
continue;
}
@@ -287,6 +329,14 @@ std::vector<PluginContainer> PluginContainerPersistence::loadPlugins(plugin_info
curPluginInformation.addRelocationData(reloc);
}
/* load function symbol data */
for (uint32_t j = 0; j < plugin_data->info.number_function_symbol_data; j++) {
auto symbol_data = &plugin_data->info.function_symbol_data[j];
std::string symbol_name = symbol_data->name;
FunctionSymbolData funSymbolData(symbol_name, (void *) symbol_data->address, symbol_data->size);
curPluginInformation.addFunctionSymbolData(funSymbolData);
}
PluginContainer container;
container.setMetaInformation(metaInformation);
container.setPluginData(pluginData);

View File

@@ -5,7 +5,7 @@
class PluginContainerPersistence {
public:
static bool savePlugin(plugin_information_t *pluginInformation, PluginContainer &plugin);
static bool savePlugin(plugin_information_t *pluginInformation, PluginContainer &plugin, MEMHeapHandle heapHandle);
static std::vector<PluginContainer> loadPlugins(plugin_information_t *pluginInformation);
};

View File

@@ -10,6 +10,9 @@ PluginInformation::PluginInformation(const PluginInformation &other) {
for (const auto &i: other.relocation_data_list) {
relocation_data_list.push_back(i);
}
for (const auto &i: other.symbol_data_list) {
symbol_data_list.insert(i);
}
section_info_list = other.section_info_list;
trampolinId = other.trampolinId;
allocatedTextMemoryAddress = other.allocatedTextMemoryAddress;

View File

@@ -18,6 +18,7 @@
#pragma once
#include <map>
#include <set>
#include <optional>
#include <string>
#include <vector>
@@ -26,6 +27,7 @@
#include "HookData.h"
#include "FunctionData.h"
#include "SectionInfo.h"
#include "FunctionSymbolData.h"
class PluginInformation {
public:
@@ -59,6 +61,14 @@ public:
return relocation_data_list;
}
void addFunctionSymbolData(const FunctionSymbolData &symbol_data) {
symbol_data_list.insert(symbol_data);
}
[[nodiscard]] const std::set<FunctionSymbolData> &getFunctionSymbolDataList() const {
return symbol_data_list;
}
void addSectionInfo(const SectionInfo &sectionInfo) {
section_info_list[sectionInfo.getName()] = sectionInfo;
}
@@ -86,6 +96,7 @@ private:
std::vector<HookData> hook_data_list;
std::vector<FunctionData> function_data_list;
std::vector<RelocationData> relocation_data_list;
std::set<FunctionSymbolData> symbol_data_list;
std::map<std::string, SectionInfo> section_info_list;
uint8_t trampolinId = 0;

View File

@@ -200,7 +200,44 @@ PluginInformationFactory::load(const PluginData &pluginData, MEMHeapHandle heapH
}
}
// Save the addresses for the allocated. This way we can free it again :)
// 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;
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];
auto offsetVal = value - sectionVal->get_address();
auto sectionOpt = pluginInfo.getSectionInfo(sectionVal->get_name());
if (!sectionOpt.has_value()) {
continue;
}
auto finalAddress = offsetVal + sectionOpt->getAddress();
pluginInfo.addFunctionSymbolData(FunctionSymbolData(name, (void *) finalAddress, (uint32_t) size));
}
}
}
break;
}
}
}
// Save the addresses for the allocated memory. This way we can free it again :)
pluginInfo.allocatedDataMemoryAddress = data_data;
pluginInfo.allocatedTextMemoryAddress = text_data;
@@ -332,7 +369,7 @@ bool PluginInformationFactory::linkSection(const elfio &reader, uint32_t section
DEBUG_FUNCTION_LINE("NOT IMPLEMENTED: %04X", sym_section_index);
return false;
}
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);
// 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);
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");