Fix Crash On ARM Systems

This commit is contained in:
TheBrokenRail 2021-08-15 23:11:03 -04:00
parent 578bb1c89f
commit 99b709fea7
8 changed files with 33 additions and 30 deletions

View File

@ -1 +1 @@
2.1.7 2.1.8

View File

@ -8,7 +8,6 @@ include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
minecraft-pi minecraft-pi
URL "${CMAKE_CURRENT_SOURCE_DIR}/minecraft-pi-0.1.1.tar.gz" URL "${CMAKE_CURRENT_SOURCE_DIR}/minecraft-pi-0.1.1.tar.gz"
URL_HASH "SHA256=e0d68918874cdd403de1fd399380ae2930913fcefdbf60a3fbfebb62e2cfacab"
) )
FetchContent_Populate(minecraft-pi) FetchContent_Populate(minecraft-pi)

View File

@ -1,5 +1,8 @@
# Changelog # Changelog
**2.1.8**
* Fix Crash On ARM Systems
**2.1.7** **2.1.7**
* Fix On 64-Bit ARM Systems * Fix On 64-Bit ARM Systems

View File

@ -8,6 +8,7 @@
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#define FORCE_PROC_FOR_ROOT_PATH
#include <libreborn/libreborn.h> #include <libreborn/libreborn.h>
#include "bootstrap.h" #include "bootstrap.h"
@ -129,6 +130,7 @@ void bootstrap(int argc, char *argv[]) {
// Get Binary Directory // Get Binary Directory
char *binary_directory = get_binary_directory(); char *binary_directory = get_binary_directory();
setenv("MCPI_ROOT_PATH", binary_directory, 1);
// Configure LD_LIBRARY_PATH // Configure LD_LIBRARY_PATH
{ {

View File

@ -2,6 +2,7 @@
#include <cstring> #include <cstring>
#include <cerrno> #include <cerrno>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/stat.h>
#include <vector> #include <vector>
#include <functional> #include <functional>

View File

@ -1,18 +1,25 @@
#pragma once #pragma once
#ifdef __arm__
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <elf.h> #include <elf.h>
#include "log.h" #include "log.h"
#include "exec.h"
// Find And Iterate Over All .text Sections In Current Binary // Find And Iterate Over All .text Sections In Current Binary
typedef void (*text_section_callback_t)(void *section, Elf32_Word size, void *data); typedef void (*text_section_callback_t)(Elf32_Addr section, Elf32_Word size, void *data);
static inline void iterate_text_sections(text_section_callback_t callback, void *data) { static inline void iterate_text_sections(text_section_callback_t callback, void *data) {
// Find Main Binary
char *real_path = NULL;
{
char *binary_directory = get_binary_directory();
safe_asprintf(&real_path, "%s/minecraft-pi", binary_directory);
free(binary_directory);
}
// Load Main Binary // Load Main Binary
char *real_path = realpath("/proc/self/exe", NULL);
FILE *file_obj = fopen(real_path, "rb"); FILE *file_obj = fopen(real_path, "rb");
// Verify Binary // Verify Binary
@ -47,7 +54,7 @@ static inline void iterate_text_sections(text_section_callback_t callback, void
// Check Section Type // Check Section Type
if (strcmp(name, ".text") == 0) { if (strcmp(name, ".text") == 0) {
// .text Section // .text Section
(*callback)((void *) header.sh_addr, header.sh_size, data); (*callback)(header.sh_addr, header.sh_size, data);
text_sections++; text_sections++;
} }
} }
@ -64,4 +71,3 @@ static inline void iterate_text_sections(text_section_callback_t callback, void
munmap(file_map, size); munmap(file_map, size);
fclose(file_obj); fclose(file_obj);
} }
#endif // #ifdef __arm__

View File

@ -3,8 +3,6 @@
#include <unistd.h> #include <unistd.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h>
#include <limits.h>
#include <string.h> #include <string.h>
#include "log.h" #include "log.h"
@ -24,30 +22,23 @@ __attribute__((noreturn)) static inline void safe_execvpe(const char *pathname,
// Get Binary Directory (Remember To Free) // Get Binary Directory (Remember To Free)
#define EXE_PATH "/proc/self/exe" #define EXE_PATH "/proc/self/exe"
static inline char *get_binary_directory() { static inline char *get_binary_directory() {
#ifndef FORCE_PROC_FOR_ROOT_PATH
{
// Check Environment
char *specified_root = getenv("MCPI_ROOT_PATH");
if (specified_root != NULL) {
return strdup(specified_root);
}
}
#endif
// Get Path To Current Executable // Get Path To Current Executable
// Get Symlink Path Size char *exe = realpath("/proc/self/exe", NULL);
struct stat sb;
if (lstat(EXE_PATH, &sb) == -1) {
ERR("Unable To Get " EXE_PATH " Symlink Size: %s", strerror(errno));
}
ssize_t path_size = sb.st_size;
if (sb.st_size <= 0) {
path_size = PATH_MAX;
}
char *exe = (char *) malloc(path_size + 1);
ALLOC_CHECK(exe); ALLOC_CHECK(exe);
// Read Link
ssize_t r = readlink(EXE_PATH, exe, path_size);
if (r < 0) {
ERR("Unable To Read " EXE_PATH " Symlink: %s", strerror(errno));
}
if (r > path_size) {
ERR("%s", "Size Of Symlink " EXE_PATH " Changed");
}
exe[r + 1] = '\0';
// Chop Off Last Component // Chop Off Last Component
for (int i = r; i >= 0; i--) { int length = strlen(exe);
for (int i = length - 1; i >= 0; i--) {
if (exe[i] == '/') { if (exe[i] == '/') {
exe[i] = '\0'; exe[i] = '\0';
break; break;

View File

@ -44,8 +44,9 @@ struct overwrite_data {
void *replacement; void *replacement;
int found; int found;
}; };
static void overwrite_calls_callback(void *section, Elf32_Word size, void *data) { static void overwrite_calls_callback(Elf32_Addr section_addr, Elf32_Word size, void *data) {
struct overwrite_data *args = (struct overwrite_data *) data; struct overwrite_data *args = (struct overwrite_data *) data;
void *section = (void *) section_addr;
for (uint32_t i = 0; i < size; i = i + 4) { for (uint32_t i = 0; i < size; i = i + 4) {
unsigned char *addr = ((unsigned char *) section) + i; unsigned char *addr = ((unsigned char *) section) + i;