Resetting the entire state of a statically linked library at runtime without modifying the library's source code or API is a challenging task. However, there are a few possible approaches you could explore:
- Using a linker script: As you mentioned, you could use a linker script to isolate the library's global data in their own sections. This would allow you to re initialize the library's in-memory sections by emulating the operating system's loader.
Here's an example of how you could use a linker script to isolate the library's global data:
Suppose the library's global data is stored in the sections .data and .bss. You could create a linker script that redefines these sections to point to a new location:
LIBRARY_DATA_ADDR = 0x10000000
LIBRARY_BSS_ADDR = 0x20000000
seyaml_data = .data
seyaml_bss = .bss
ifeq ($(TARGET_ARCH), x86_64)
sed -i 's/^.data.*$/ieseymal_data = .data; \
sed -i 's/^.bss.*$/eseyamp_bss = .bss;/' Makefile
else if $(TARGET_ARCH) == ARM
sed -i 's/^.data.*$/eseyamp_data = .data; \
sed -i 's/^.bss.*$/eseyamp_bss = .bss;/' Makefile
endif
This linker script redefines the .data and .bss sections to point to the address LIBRARY_DATA_ADDR and LIBRARY_BSS_ADDR, respectively. You can then use the objcopy command to copy the library's sections to a new address:
objcopy -idy libseyaml.a libseyaml_reshaped.a
This will create a new object file libseyaml_reshaped.a that contains the reinitialized library.
- Using a programmable loader: Another approach would be to use a programmable loader that allows you to modify the library's state at runtime. There are several programmable loaders available, such as
ld.so on Linux, LoadLibraryEx on Windows, or dlopen on macOS.
With a programmable loader, you could use a technique called "relocation" to modify the library's address space at runtime. This would allow you to reset the library's state without modifying its source code or API.
However, using a programmable loader can be more complex than using a linker script, as you would need to understand the loader's architecture and API. Additionally, some loaders may not support modifying the library's state at runtime.
- Using a library resetter: Another option would be to use a library resetter, which is a separate program that resets the library's state at runtime. The library resetter could be designed to modify the library's address space, or to modify the library's global data directly.
Here's an example of how you could use a library resetter to reset the library's state:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LIBRARY_NAME "seyaml"
int main() {
// Load the library
void *library = dlopen(LIBRARY_NAME, RTLD_LAZY);
if (!library) {
fprintf(stderr, "Failed to load library %s\n", LIBRARY_NAME);
return 1;
}
// Reset the library's state
dlclose(library);
library = dlopen(LIBRARY_NAME, RTLD_LAZY);
if (!library) {
fprintf(stderr, "Failed to reset library %s\n", LIBRARY_NAME);
return 1;
}
// Use the library as usual
// ...
return 0;
}
This program loads the seyaml library using dlopen, and then resets its state using dlclose. The library is then reloaded using dlopen again.