Improve Error Reporting
This commit is contained in:
parent
dd539e434a
commit
054dcba425
@ -10,6 +10,9 @@ extern "C" {
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
#define INFO(msg, ...) fprintf(stderr, "[INFO]: " msg "\n", __VA_ARGS__);
|
||||||
|
#define ERR(msg, ...) fprintf(stderr, "[ERR]: " msg "\n", __VA_ARGS__); exit(1);
|
||||||
|
|
||||||
#define HOOK(name, return_type, args) \
|
#define HOOK(name, return_type, args) \
|
||||||
typedef return_type (*name##_t)args; \
|
typedef return_type (*name##_t)args; \
|
||||||
static name##_t real_##name = NULL; \
|
static name##_t real_##name = NULL; \
|
||||||
@ -19,17 +22,13 @@ extern "C" {
|
|||||||
dlerror(); \
|
dlerror(); \
|
||||||
real_##name = (name##_t) dlsym(RTLD_NEXT, #name); \
|
real_##name = (name##_t) dlsym(RTLD_NEXT, #name); \
|
||||||
if (!real_##name) { \
|
if (!real_##name) { \
|
||||||
fprintf(stderr, "Error Resolving Symbol: "#name": %s\n", dlerror()); \
|
ERR("Error Resolving Symbol: "#name": %s", dlerror()); \
|
||||||
exit(1); \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
}; \
|
}; \
|
||||||
\
|
\
|
||||||
__attribute__((__used__)) return_type name args
|
__attribute__((__used__)) return_type name args
|
||||||
|
|
||||||
#define INFO(msg, ...) fprintf(stderr, "[INFO]: " msg "\n", __VA_ARGS__);
|
|
||||||
#define ERR(msg, ...) fprintf(stderr, "[ERR]: " msg "\n", __VA_ARGS__); exit(1);
|
|
||||||
|
|
||||||
void _overwrite_calls(const char *file, int line, void *start, void *target);
|
void _overwrite_calls(const char *file, int line, void *start, void *target);
|
||||||
#define overwrite_calls(start, target) _overwrite_calls(__FILE__, __LINE__, start, target);
|
#define overwrite_calls(start, target) _overwrite_calls(__FILE__, __LINE__, start, target);
|
||||||
|
|
||||||
|
@ -41,7 +41,6 @@ static void store_x11_window() {
|
|||||||
// Handle GLFW Error
|
// Handle GLFW Error
|
||||||
static void glfw_error(__attribute__((unused)) int error, const char *description) {
|
static void glfw_error(__attribute__((unused)) int error, const char *description) {
|
||||||
ERR("GLFW Error: %s", description);
|
ERR("GLFW Error: %s", description);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert GLFW Key To SDL Key
|
// Convert GLFW Key To SDL Key
|
||||||
@ -191,8 +190,7 @@ HOOK(SDL_WM_SetCaption, void, (const char *title, __attribute__((unused)) const
|
|||||||
glfwSetErrorCallback(glfw_error);
|
glfwSetErrorCallback(glfw_error);
|
||||||
|
|
||||||
if (!glfwInit()) {
|
if (!glfwInit()) {
|
||||||
fprintf(stderr, "Unable To Initialize GLFW\n");
|
ERR("%s", "Unable To Initialize GLFW");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create OpenGL ES 1.1 Context
|
// Create OpenGL ES 1.1 Context
|
||||||
@ -203,8 +201,7 @@ HOOK(SDL_WM_SetCaption, void, (const char *title, __attribute__((unused)) const
|
|||||||
|
|
||||||
glfw_window = glfwCreateWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, title, NULL, NULL);
|
glfw_window = glfwCreateWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, title, NULL, NULL);
|
||||||
if (!glfw_window) {
|
if (!glfw_window) {
|
||||||
fprintf(stderr, "Unable To Create GLFW Window\n");
|
ERR("%s", "Unable To Create GLFW Window");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't Process Events In Server Mode
|
// Don't Process Events In Server Mode
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <elf.h>
|
#include <elf.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include <libcore/libcore.h>
|
#include <libcore/libcore.h>
|
||||||
|
|
||||||
@ -75,18 +76,20 @@ struct overwrite_data {
|
|||||||
int line;
|
int line;
|
||||||
void *target;
|
void *target;
|
||||||
void *replacement;
|
void *replacement;
|
||||||
|
int found;
|
||||||
};
|
};
|
||||||
static void overwrite_calls_callback(void *section, Elf32_Word size, void *data) {
|
static void overwrite_calls_callback(void *section, Elf32_Word size, void *data) {
|
||||||
struct overwrite_data args = *(struct overwrite_data *) data;
|
struct overwrite_data *args = (struct overwrite_data *) data;
|
||||||
|
|
||||||
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;
|
||||||
if (addr[3] == BL_INSTRUCTION) {
|
if (addr[3] == BL_INSTRUCTION) {
|
||||||
uint32_t check_instruction = generate_bl_instruction(addr, args.target);
|
uint32_t check_instruction = generate_bl_instruction(addr, args->target);
|
||||||
unsigned char *check_instruction_array = (unsigned char *) &check_instruction;
|
unsigned char *check_instruction_array = (unsigned char *) &check_instruction;
|
||||||
if (addr[0] == check_instruction_array[0] && addr[1] == check_instruction_array[1] && addr[2] == check_instruction_array[2]) {
|
if (addr[0] == check_instruction_array[0] && addr[1] == check_instruction_array[1] && addr[2] == check_instruction_array[2]) {
|
||||||
uint32_t new_instruction = generate_bl_instruction(addr, args.replacement);
|
uint32_t new_instruction = generate_bl_instruction(addr, args->replacement);
|
||||||
_patch(args.file, args.line, addr, (unsigned char *) &new_instruction);
|
_patch(args->file, args->line, addr, (unsigned char *) &new_instruction);
|
||||||
|
args->found++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,14 +98,22 @@ static void overwrite_calls_callback(void *section, Elf32_Word size, void *data)
|
|||||||
// Limit To 512 overwrite_calls() Uses
|
// Limit To 512 overwrite_calls() Uses
|
||||||
#define CODE_BLOCK_SIZE 4096
|
#define CODE_BLOCK_SIZE 4096
|
||||||
static unsigned char *code_block = NULL;
|
static unsigned char *code_block = NULL;
|
||||||
|
#define CODE_SIZE 8
|
||||||
|
static int code_block_remaining = CODE_BLOCK_SIZE;
|
||||||
|
|
||||||
// Overwrite Function Calls
|
// Overwrite Function Calls
|
||||||
void _overwrite_calls(const char *file, int line, void *start, void *target) {
|
void _overwrite_calls(const char *file, int line, void *start, void *target) {
|
||||||
// BL Instructions Can Only Access A Limited Portion of Memory, So This Allocates Memory Closer To The Original Instruction, That When Run, Will Jump Into The Actual Target
|
// BL Instructions Can Only Access A Limited Portion of Memory, So This Allocates Memory Closer To The Original Instruction, That When Run, Will Jump Into The Actual Target
|
||||||
if (code_block == NULL) {
|
if (code_block == NULL) {
|
||||||
code_block = mmap((void *) 0x200000, CODE_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
code_block = mmap((void *) 0x200000, CODE_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||||
|
if (code_block == MAP_FAILED) {
|
||||||
|
ERR("Unable To Allocate Code Block: %s", strerror(errno));
|
||||||
|
}
|
||||||
INFO("Code Block Allocated At: 0x%08x", (uint32_t) code_block);
|
INFO("Code Block Allocated At: 0x%08x", (uint32_t) code_block);
|
||||||
}
|
}
|
||||||
|
if (code_block_remaining < CODE_SIZE) {
|
||||||
|
ERR("%s", "Maximum Amount Of overwrite_calls() Uses Reached");
|
||||||
|
}
|
||||||
_overwrite(NULL, -1, code_block, target);
|
_overwrite(NULL, -1, code_block, target);
|
||||||
|
|
||||||
struct overwrite_data data;
|
struct overwrite_data data;
|
||||||
@ -110,9 +121,16 @@ void _overwrite_calls(const char *file, int line, void *start, void *target) {
|
|||||||
data.line = line;
|
data.line = line;
|
||||||
data.target = start;
|
data.target = start;
|
||||||
data.replacement = code_block;
|
data.replacement = code_block;
|
||||||
|
data.found = 0;
|
||||||
|
|
||||||
iterate_text_section(overwrite_calls_callback, &data);
|
iterate_text_section(overwrite_calls_callback, &data);
|
||||||
|
|
||||||
code_block = code_block + 8;
|
code_block = code_block + CODE_SIZE;
|
||||||
|
code_block_remaining = code_block_remaining - CODE_SIZE;
|
||||||
|
|
||||||
|
if (data.found < 1) {
|
||||||
|
ERR("(%s:%i) Unable To Find Callsites For 0x%08x", file, line, (uint32_t) start);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite Function
|
// Overwrite Function
|
||||||
|
@ -4,15 +4,17 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <libcore/libcore.h>
|
||||||
|
|
||||||
#include "cxx11_util.h"
|
#include "cxx11_util.h"
|
||||||
|
|
||||||
// Convert A C-String into A C++11 String That Can be Acessed In C++03 Code
|
// Convert A C-String into A C++11 String That Can be Acessed In C++03 Code
|
||||||
cxx11_string create_cxx11_string(const char *str) {
|
cxx11_string create_cxx11_string(const char *str) {
|
||||||
std::string *new_str = new std::string(str);
|
std::string *new_str = new std::string(str);
|
||||||
int32_t new_size = sizeof (cxx11_string);
|
int32_t new_size = sizeof (cxx11_string);
|
||||||
int32_t old_size = sizeof *new_str;
|
int32_t old_size = sizeof (*new_str);
|
||||||
if (new_size != old_size) {
|
if (new_size != old_size) {
|
||||||
fprintf(stderr, "Mismatched String Size: Expected: %i Real: %i\n", new_size, old_size);
|
ERR("Mismatched String Size: Expected: %i Real: %i", new_size, old_size);
|
||||||
}
|
}
|
||||||
return *reinterpret_cast<cxx11_string *>(new_str);
|
return *reinterpret_cast<cxx11_string *>(new_str);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user