minecraft-pi-reborn/launcher/src/patchelf.cpp

99 lines
2.9 KiB
C++
Raw Normal View History

2022-09-25 19:47:36 +00:00
#include <cstdlib>
#include <sys/stat.h>
2024-10-20 05:19:08 +00:00
#include <ranges>
2022-09-25 19:47:36 +00:00
#include <LIEF/ELF.hpp>
2024-02-12 05:44:38 +00:00
#include <dlfcn.h>
#include <link.h>
2022-09-25 19:47:36 +00:00
#include <libreborn/libreborn.h>
#include "patchelf.h"
// Duplicate MCPI Executable Into /tmp
static void duplicate_mcpi_executable(char *new_path) {
// Ensure Temporary Directory
ensure_directory(MCPI_PATCHED_DIR);
2022-09-25 19:47:36 +00:00
// Generate New File
2024-10-20 05:19:08 +00:00
const int new_file_fd = mkstemp(new_path);
2022-09-25 19:47:36 +00:00
if (new_file_fd == -1) {
ERR("Unable To Create Temporary File: %s", strerror(errno));
}
close(new_file_fd);
}
// Fix MCPI Dependencies
2024-10-20 05:19:08 +00:00
static std::vector<std::string> needed_libraries = {
"libmedia-layer-core.so",
"libpng12.so.0",
"libstdc++.so.6",
"libm.so.6",
"libgcc_s.so.1",
"libc.so.6",
"libpthread.so.0"
2022-09-25 19:47:36 +00:00
};
2024-10-20 05:19:08 +00:00
static std::vector<std::string> function_prefixes_to_patch = {
"SDL_",
"gl"
2022-09-25 19:47:36 +00:00
};
2024-10-20 05:19:08 +00:00
void patch_mcpi_elf_dependencies(const std::string &original_path, char *new_path, const std::string &interpreter, const std::vector<std::string> &rpath, const std::vector<std::string> &mods) {
2022-09-25 19:47:36 +00:00
// Duplicate MCPI executable into /tmp so it can be modified.
duplicate_mcpi_executable(new_path);
2024-10-20 05:19:08 +00:00
// Load Binary
std::unique_ptr<LIEF::ELF::Binary> binary = LIEF::ELF::Parser::parse(original_path);
// Set Interpreter
if (!interpreter.empty()) {
binary->interpreter(interpreter);
}
// Remove Existing Needed Libraries
std::vector<std::string> to_remove;
for (const LIEF::ELF::DynamicEntry &entry : binary->dynamic_entries()) {
const LIEF::ELF::DynamicEntryLibrary *library = dynamic_cast<const LIEF::ELF::DynamicEntryLibrary *>(&entry);
if (library) {
to_remove.push_back(library->name());
2022-09-25 19:47:36 +00:00
}
2024-10-20 05:19:08 +00:00
}
for (const std::string &library : to_remove) {
binary->remove_library(library);
}
// Setup RPath
binary->add(LIEF::ELF::DynamicEntryRpath(rpath));
// Add Libraries
std::vector<std::string> all_libraries;
for (const std::vector<std::string> &list : {mods, needed_libraries}) {
all_libraries.insert(all_libraries.end(), list.begin(), list.end());
}
for (const std::string &library : all_libraries | std::views::reverse) {
binary->add_library(library);
}
// Fix Symbol Names
for (LIEF::ELF::Symbol &symbol : binary->dynamic_symbols()) {
if (symbol.is_function()) {
for (const std::string &prefix : function_prefixes_to_patch) {
if (symbol.name().rfind(prefix, 0) == 0) {
symbol.name("media_" + symbol.name());
break;
}
}
2022-09-25 19:47:36 +00:00
}
}
2024-10-20 05:19:08 +00:00
// Write Binary
LIEF::ELF::Builder builder{*binary};
builder.build();
builder.write(new_path);
2022-09-25 19:47:36 +00:00
// Fix Permissions
if (chmod(new_path, S_IRUSR | S_IXUSR) != 0) {
ERR("Unable To Set File Permissions: %s: %s", new_path, strerror(errno));
}
}