twn_textures.c: support for .so rodata inference

This commit is contained in:
veclav talica 2024-09-23 12:50:44 +03:00
parent 0ef8a6233f
commit ce582d8a80
8 changed files with 52 additions and 23 deletions

View File

@ -49,7 +49,12 @@ add_subdirectory(third-party/libxm SYSTEM)
if(LINUX)
set(SYSTEM_SOURCE_FILES src/system/linux/twn_elf.c)
set(SYSTEM_SOURCE_FILES
src/system/linux/twn_elf.c
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_linux_game_object.c>)
elseif(WIN32)
set(SYSTEM_SOURCE_FILES
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_win32_game_object.c>)
else()
set(SYSTEM_SOURCE_FILES)
endif()
@ -80,7 +85,6 @@ set(TWN_SOURCE_FILES
src/twn_util.c include/twn_util.h
src/twn_input.c include/twn_input.h
src/twn_camera.c include/twn_camera.h
src/twn_game_object.c
src/twn_textures.c src/twn_textures_c.h
src/rendering/twn_rendering.c src/rendering/twn_rendering_c.h
@ -90,7 +94,8 @@ set(TWN_SOURCE_FILES
src/rendering/twn_circles.c
# for dynamic load based solution main is compiled in a separate target
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:src/twn_main.c>
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:src/twn_main.c
src/game_object/twn_static_game_object.c>
${SYSTEM_SOURCE_FILES})

View File

@ -140,3 +140,10 @@ bool game_object_try_reloading(void) {
void game_object_tick(void) {
game_tick_callback();
}
void *game_object_get_game_tick_address(void) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
return (void *)&game_tick_callback;
#pragma GCC diagnostic pop
}

View File

@ -19,3 +19,10 @@ bool game_object_try_reloading(void) {
void game_object_tick(void) {
game_tick();
}
void *game_object_get_game_tick_address(void) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
return (void *)&game_tick;
#pragma GCC diagnostic pop
}

View File

@ -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;
}
}

View File

@ -1,12 +0,0 @@
#if defined(TWN_FEATURE_DYNLIB_GAME)
#if defined(__linux__)
#include "game_object/twn_linux_game_object_c.h"
#elif defined(_WIN32)
#include "game_object/twn_win32_game_object_c.h"
#else
#warning "TWN_FEATURE_DYNLIB_GAME is set, but not supported"
#include "game_object/twn_static_game_object_c.h"
#endif
#else
#include "game_object/twn_static_game_object_c.h"
#endif

View File

@ -18,5 +18,6 @@ bool game_object_try_reloading(void);
void game_object_tick(void);
void *game_object_get_game_tick_address(void);
#endif

View File

@ -413,7 +413,7 @@ void textures_update_atlas(struct texture_cache *cache) {
}
/* EXPERIMANTAL: LIKELY TO BE REMOVED! */
#if defined(__linux__) && !defined(HOT_RELOAD_SUPPORT) /* use rodata elf section for fast lookups of repeating textures */
#if defined(__linux__) /* use rodata elf section for fast lookups of repeating textures */
#include "system/linux/twn_elf.h"