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

60 lines
1.6 KiB
C++
Raw Normal View History

2022-09-25 19:47:36 +00:00
#include <cstdlib>
#include <sys/stat.h>
#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
int new_file_fd = mkstemp(new_path);
if (new_file_fd == -1) {
ERR("Unable To Create Temporary File: %s", strerror(errno));
}
close(new_file_fd);
}
// Fix MCPI Dependencies
static const char *libraries_to_remove[] = {
"libbcm_host.so",
"libX11.so.6",
"libEGL.so",
"libGLESv2.so",
"libSDL-1.2.so.0"
};
static const char *libraries_to_add[] = {
"libmedia-layer-core.so"
};
2024-05-20 20:37:55 +00:00
void patch_mcpi_elf_dependencies(const char *original_path, char *new_path) {
2022-09-25 19:47:36 +00:00
// Duplicate MCPI executable into /tmp so it can be modified.
duplicate_mcpi_executable(new_path);
// Patch File
{
std::unique_ptr<LIEF::ELF::Binary> binary = LIEF::ELF::Parser::parse(original_path);
for (size_t i = 0; i < (sizeof (libraries_to_remove) / sizeof (const char *)); i++) {
binary->remove_library(libraries_to_remove[i]);
}
for (size_t i = 0; i < (sizeof (libraries_to_add) / sizeof (const char *)); i++) {
binary->add_library(libraries_to_add[i]);
}
LIEF::ELF::Builder builder{*binary};
builder.build();
builder.write(new_path);
}
// Fix Permissions
if (chmod(new_path, S_IRUSR | S_IXUSR) != 0) {
ERR("Unable To Set File Permissions: %s: %s", new_path, strerror(errno));
}
}