Replace PatcheLF With LIEF
parent
eb3c5d2e6f
commit
bedd5ea53a
@ -0,0 +1,39 @@
|
||||
project(LIEF)
|
||||
|
||||
# Silence Warnings
|
||||
add_compile_options(-w -Wno-psabi)
|
||||
|
||||
## LIEF
|
||||
|
||||
# Options
|
||||
set(BUILD_SHARED_LIBS TRUE CACHE BOOL "" FORCE)
|
||||
set(LIEF_C_API FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_EXAMPLES FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_PYTHON_API FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_TESTS FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_USE_CCACHE FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_LOGGING FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_LOGGING_DEBUG FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_ENABLE_JSON FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_ELF TRUE CACHE BOOL "" FORCE)
|
||||
set(LIEF_PE FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_MACHO FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_DEX FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_ART FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_OAT FALSE CACHE BOOL "" FORCE)
|
||||
set(LIEF_VDEX FALSE CACHE BOOL "" FORCE)
|
||||
|
||||
# Download
|
||||
add_subdirectory(src EXCLUDE_FROM_ALL)
|
||||
|
||||
# Ensure Build
|
||||
add_custom_target(LIEF-build ALL DEPENDS LIB_LIEF)
|
||||
|
||||
# Install
|
||||
install(TARGETS LIB_LIEF DESTINATION "${MCPI_LIB_DIR}")
|
||||
if(BUILD_ARM_COMPONENTS)
|
||||
install(TARGETS LIB_LIEF EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
|
||||
endif()
|
||||
|
||||
# License
|
||||
install(FILES src/LICENSE DESTINATION "${MCPI_LEGAL_DIR}/LIEF")
|
@ -0,0 +1 @@
|
||||
Subproject commit c7b3ce3b2ce6917855a72709f73ef6d00b50e1f7
|
@ -1,4 +0,0 @@
|
||||
project(launcher-dependencies)
|
||||
|
||||
# PatchELF
|
||||
add_subdirectory(patchelf)
|
@ -1,17 +0,0 @@
|
||||
project(patchelf)
|
||||
|
||||
# Silence Warnings
|
||||
add_compile_options(-w)
|
||||
|
||||
## PatchELF
|
||||
|
||||
# Build
|
||||
add_executable(patchelf src/src/patchelf.cc)
|
||||
target_compile_definitions(patchelf PRIVATE -D_FILE_OFFSET_BITS=64)
|
||||
set_target_properties(patchelf PROPERTIES CXX_STANDARD 17)
|
||||
|
||||
# Install
|
||||
install(TARGETS patchelf DESTINATION "${MCPI_BIN_DIR}")
|
||||
|
||||
# License
|
||||
install(FILES src/COPYING DESTINATION "${MCPI_LEGAL_DIR}/patchelf")
|
@ -1 +0,0 @@
|
||||
Subproject commit c2b419dc2a0d6095eaa69b65ad5854ce847bdd01
|
@ -1,136 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <libreborn/libreborn.h>
|
||||
|
||||
#include "bootstrap.h"
|
||||
#include "patchelf.h"
|
||||
|
||||
// Duplicate MCPI Executable Into /tmp
|
||||
static void duplicate_mcpi_executable(const char *original_path, char *new_path) {
|
||||
// Ensure Temporary Directory
|
||||
{
|
||||
// Check If It Exists
|
||||
struct stat tmp_stat;
|
||||
int exists = stat(MCPI_PATCHED_DIR, &tmp_stat) != 0 ? 0 : S_ISDIR(tmp_stat.st_mode);
|
||||
if (!exists) {
|
||||
// Doesn't Exist
|
||||
if (mkdir(MCPI_PATCHED_DIR, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
|
||||
ERR("Unable To Create Temporary Folder: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate New File
|
||||
int new_file_fd = mkstemp(new_path);
|
||||
if (new_file_fd == -1) {
|
||||
ERR("Unable To Create Temporary File: %s", strerror(errno));
|
||||
}
|
||||
FILE *new_file = fdopen(new_file_fd, "wb");
|
||||
if (new_file == NULL) {
|
||||
ERR("Unable To Open Temporary File: %s", strerror(errno));
|
||||
}
|
||||
|
||||
// Copy Original File
|
||||
{
|
||||
// Open Original File
|
||||
FILE *original_file = fopen(original_path, "rb");
|
||||
if (original_file == NULL) {
|
||||
ERR("Unable To Open File: %s", original_path);
|
||||
}
|
||||
|
||||
// Copy
|
||||
#define BUFFER_SIZE 1024
|
||||
char buf[BUFFER_SIZE];
|
||||
size_t bytes_read = 0;
|
||||
while ((bytes_read = fread((void *) buf, 1, BUFFER_SIZE, original_file)) > 0) {
|
||||
fwrite((void *) buf, 1, bytes_read, new_file);
|
||||
if (ferror(new_file) != 0) {
|
||||
ERR("Unable To Write File: %s", new_path);
|
||||
}
|
||||
}
|
||||
if (ferror(original_file) != 0) {
|
||||
ERR("Unable To Read File: %s", original_path);
|
||||
}
|
||||
|
||||
// Close Original File
|
||||
fclose(original_file);
|
||||
}
|
||||
|
||||
// Close New File
|
||||
fclose(new_file);
|
||||
close(new_file_fd);
|
||||
}
|
||||
|
||||
// Fix MCPI Dependencies
|
||||
#define patch_mcpi_elf_dependencies_with_extra_patchelf_args(...) \
|
||||
({ \
|
||||
const char *const _macro_command[] = { \
|
||||
"patchelf", \
|
||||
##__VA_ARGS__, \
|
||||
"--remove-needed", "libbcm_host.so", \
|
||||
"--remove-needed", "libX11.so.6", \
|
||||
"--remove-needed", "libEGL.so", \
|
||||
"--remove-needed", "libGLESv2.so", \
|
||||
"--remove-needed", "libSDL-1.2.so.0", \
|
||||
"--add-needed", "libmedia-layer-core.so", \
|
||||
new_path, \
|
||||
NULL \
|
||||
}; \
|
||||
int _macro_return_code = 0; \
|
||||
char *_macro_output = run_command(_macro_command, &_macro_return_code); \
|
||||
if (_macro_output != NULL) { \
|
||||
free(_macro_output); \
|
||||
} \
|
||||
_macro_return_code; \
|
||||
})
|
||||
void patch_mcpi_elf_dependencies(const char *original_path, char *new_path, const char *linker) {
|
||||
// Duplicate MCPI executable into /tmp so it can be modified.
|
||||
duplicate_mcpi_executable(original_path, new_path);
|
||||
|
||||
// Run patchelf
|
||||
int return_code;
|
||||
if (linker == NULL) {
|
||||
return_code = patch_mcpi_elf_dependencies_with_extra_patchelf_args();
|
||||
} else {
|
||||
return_code = patch_mcpi_elf_dependencies_with_extra_patchelf_args("--set-interpreter", linker);
|
||||
}
|
||||
if (!is_exit_status_success(return_code)) {
|
||||
char *exit_status_line = NULL;
|
||||
get_exit_status_string(return_code, &exit_status_line);
|
||||
ERR("patchelf Failed%s", exit_status_line);
|
||||
}
|
||||
|
||||
// Fix Permissions
|
||||
if (chmod(new_path, S_IRUSR | S_IXUSR) != 0) {
|
||||
ERR("Unable To Set File Permissions: %s: %s", new_path, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
// Get Interpreter
|
||||
char *patch_get_interpreter(const char *file) {
|
||||
// Run
|
||||
const char *const command[] = {
|
||||
"patchelf",
|
||||
"--print-interpreter",
|
||||
file,
|
||||
NULL
|
||||
};
|
||||
int return_code;
|
||||
char *output = run_command(command, &return_code);
|
||||
if (!is_exit_status_success(return_code)) {
|
||||
char *exit_status_line = NULL;
|
||||
get_exit_status_string(return_code, &exit_status_line);
|
||||
ERR("patchelf Failed%s", exit_status_line);
|
||||
}
|
||||
if (output != NULL) {
|
||||
// Trim
|
||||
int length = strlen(output);
|
||||
if (output[length - 1] == '\n') {
|
||||
output[length - 1] = '\0';
|
||||
}
|
||||
}
|
||||
// Return
|
||||
return output;
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
#include <cstdlib>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <LIEF/ELF.hpp>
|
||||
|
||||
#include <libreborn/libreborn.h>
|
||||
|
||||
#include "patchelf.h"
|
||||
|
||||
// Duplicate MCPI Executable Into /tmp
|
||||
static void duplicate_mcpi_executable(char *new_path) {
|
||||
// Ensure Temporary Directory
|
||||
{
|
||||
// Check If It Exists
|
||||
struct stat tmp_stat;
|
||||
int exists = stat(MCPI_PATCHED_DIR, &tmp_stat) != 0 ? 0 : S_ISDIR(tmp_stat.st_mode);
|
||||
if (!exists) {
|
||||
// Doesn't Exist
|
||||
if (mkdir(MCPI_PATCHED_DIR, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
|
||||
ERR("Unable To Create Temporary Folder: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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"
|
||||
};
|
||||
void patch_mcpi_elf_dependencies(const char *original_path, char *new_path, const char *linker) {
|
||||
// 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);
|
||||
if (linker != NULL) {
|
||||
binary->interpreter(linker);
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
// Get Interpreter
|
||||
static int dl_iterate_callback(struct dl_phdr_info *info, __attribute__((unused)) size_t size, void *data) {
|
||||
// Only Search Current Program
|
||||
if (strcmp(info->dlpi_name, "") == 0) {
|
||||
for (int i = 0; i < info->dlpi_phnum; i++) {
|
||||
if (info->dlpi_phdr[i].p_type == PT_INTERP) {
|
||||
// Callback
|
||||
*(char **) data = (char *) info->dlpi_phdr[i].p_vaddr;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
char *patch_get_interpreter() {
|
||||
char *interpreter = NULL;
|
||||
dl_iterate_phdr(dl_iterate_callback, &interpreter);
|
||||
if (interpreter != NULL) {
|
||||
interpreter = strdup(interpreter);
|
||||
}
|
||||
return interpreter;
|
||||
}
|
Loading…
Reference in New Issue