twn_textures.c: support for .so rodata inference
This commit is contained in:
@ -1,10 +1,13 @@
|
||||
#include "elf.h"
|
||||
#include "twn_elf.h"
|
||||
#include "twn_game_object_c.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/auxv.h>
|
||||
#include <elf.h>
|
||||
#include <linux/limits.h>
|
||||
#define __USE_GNU
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
@ -17,10 +20,21 @@ bool infer_elf_section_bounds(const char *const restrict name,
|
||||
{
|
||||
bool result = false;
|
||||
char buf[PATH_MAX];
|
||||
ssize_t l = readlink("/proc/self/exe", buf, PATH_MAX);
|
||||
if (l == -1)
|
||||
goto ERR_CANT_READLINK;
|
||||
buf[l] = 0; /* readlink() doesn't write a terminator */
|
||||
bool from_shared_object;
|
||||
Dl_info info;
|
||||
|
||||
if (!dladdr(game_object_get_game_tick_address(), &info)) {
|
||||
/* statically linked */
|
||||
from_shared_object = false;
|
||||
ssize_t l = readlink("/proc/self/exe", buf, PATH_MAX);
|
||||
if (l == -1)
|
||||
goto ERR_CANT_READLINK;
|
||||
buf[l] = 0; /* readlink() doesn't write a terminator */
|
||||
} else {
|
||||
/* dynamically linked */
|
||||
from_shared_object = true;
|
||||
memcpy(buf, "libgame.so", sizeof "libgame.so");
|
||||
}
|
||||
|
||||
int elf = open(buf, O_RDONLY);
|
||||
if (elf == -1)
|
||||
@ -51,8 +65,15 @@ bool infer_elf_section_bounds(const char *const restrict name,
|
||||
|
||||
if (strcmp(&sh[shdr.sh_name], name) == 0) {
|
||||
result = true;
|
||||
*vm_start = getauxval(AT_ENTRY) - ehdr.e_entry + (char *)shdr.sh_addr;
|
||||
*vm_end = getauxval(AT_ENTRY) - ehdr.e_entry + (char *)shdr.sh_addr + shdr.sh_size;
|
||||
|
||||
if (!from_shared_object) {
|
||||
*vm_start = getauxval(AT_ENTRY) - ehdr.e_entry + (char *)shdr.sh_addr;
|
||||
*vm_end = getauxval(AT_ENTRY) - ehdr.e_entry + (char *)shdr.sh_addr + shdr.sh_size;
|
||||
} else {
|
||||
*vm_start = (uintptr_t)info.dli_fbase + (char *)shdr.sh_addr;
|
||||
*vm_end = (uintptr_t)info.dli_fbase + (char *)shdr.sh_addr + shdr.sh_size;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user