2026-01-06 09:42:54 +08:00
|
|
|
#include "util/binary.h"
|
2024-08-08 15:03:10 +08:00
|
|
|
|
2024-11-03 08:35:11 +08:00
|
|
|
#if defined(FF_HAVE_ELF) || defined(__sun) || (defined(__FreeBSD__) && !defined(__DragonFly__)) || defined(__OpenBSD__) || defined(__NetBSD__)
|
2024-08-08 15:03:10 +08:00
|
|
|
|
2026-01-06 09:13:40 +08:00
|
|
|
#include "util/io.h"
|
2026-01-05 16:19:47 +08:00
|
|
|
#include "util/library.h"
|
2024-08-08 15:03:10 +08:00
|
|
|
#include "util/stringUtils.h"
|
|
|
|
|
|
2024-09-07 15:47:52 +08:00
|
|
|
#include <libelf.h> // #1254
|
2024-08-08 15:03:10 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
/**
|
|
|
|
|
* Structure to hold dynamically loaded libelf function pointers
|
|
|
|
|
*/
|
2024-08-20 16:37:54 +08:00
|
|
|
struct FFElfData {
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf_version)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf_begin)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf_getshdrstrndx)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf_nextscn)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf64_getshdr)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf32_getshdr)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf_getdata)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf_strptr)
|
|
|
|
|
FF_LIBRARY_SYMBOL(elf_end)
|
|
|
|
|
|
|
|
|
|
bool inited;
|
|
|
|
|
} elfData;
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
/**
|
|
|
|
|
* Extracts string literals from an ELF (Linux/Unix) binary file
|
|
|
|
|
*
|
|
|
|
|
* This function loads the libelf library dynamically, opens the ELF file,
|
|
|
|
|
* locates the .rodata section (which contains string literals), and
|
|
|
|
|
* scans it for valid strings. Each string found is passed to the
|
|
|
|
|
* callback function for processing.
|
|
|
|
|
*
|
|
|
|
|
* The function supports both 32-bit and 64-bit ELF formats.
|
|
|
|
|
*/
|
2024-08-27 16:19:05 +08:00
|
|
|
const char* ffBinaryExtractStrings(const char* elfFile, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength)
|
2024-08-08 15:03:10 +08:00
|
|
|
{
|
2025-04-02 10:27:25 +08:00
|
|
|
// Initialize libelf if not already done
|
2024-08-20 16:37:54 +08:00
|
|
|
if (!elfData.inited)
|
|
|
|
|
{
|
|
|
|
|
elfData.inited = true;
|
2026-01-05 14:51:27 +08:00
|
|
|
FF_LIBRARY_LOAD_MESSAGE(libelf, "libelf" FF_LIBRARY_EXTENSION, 1);
|
2024-08-20 16:37:54 +08:00
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf_version)
|
|
|
|
|
if (elfData.ffelf_version(EV_CURRENT) == EV_NONE) return "elf_version() failed";
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
// Load all required libelf functions
|
2024-08-20 16:37:54 +08:00
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf_begin)
|
|
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf_getshdrstrndx)
|
|
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf_nextscn)
|
|
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf64_getshdr)
|
|
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf32_getshdr)
|
|
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf_getdata)
|
|
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf_strptr)
|
|
|
|
|
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libelf, elfData, elf_end)
|
|
|
|
|
|
|
|
|
|
libelf = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elfData.ffelf_end == NULL)
|
|
|
|
|
return "load libelf failed";
|
2024-08-08 15:03:10 +08:00
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
// Open the ELF file
|
2025-06-19 22:41:30 +08:00
|
|
|
FF_AUTO_CLOSE_FD int fd = open(elfFile, O_RDONLY | O_CLOEXEC);
|
2024-08-08 15:03:10 +08:00
|
|
|
if (fd < 0) return "open() failed";
|
|
|
|
|
|
2024-08-20 16:37:54 +08:00
|
|
|
Elf* elf = elfData.ffelf_begin(fd, ELF_C_READ, NULL);
|
2024-08-08 15:03:10 +08:00
|
|
|
if (elf == NULL) return "elf_begin() failed";
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
// Get the section header string table index
|
2024-08-08 15:03:10 +08:00
|
|
|
size_t shstrndx = 0;
|
2024-08-20 16:37:54 +08:00
|
|
|
if (elfData.ffelf_getshdrstrndx(elf, &shstrndx) < 0)
|
2024-08-08 15:03:10 +08:00
|
|
|
{
|
2024-08-20 16:37:54 +08:00
|
|
|
elfData.ffelf_end(elf);
|
2024-08-08 15:03:10 +08:00
|
|
|
return "elf_getshdrstrndx() failed";
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
// Iterate through all sections, looking for .rodata which contains string literals
|
2024-08-08 15:03:10 +08:00
|
|
|
Elf_Scn* scn = NULL;
|
2024-08-20 16:37:54 +08:00
|
|
|
while ((scn = elfData.ffelf_nextscn(elf, scn)) != NULL)
|
2024-08-08 15:03:10 +08:00
|
|
|
{
|
2025-04-02 10:27:25 +08:00
|
|
|
// Try 64-bit section header first, then 32-bit if that fails
|
2024-08-20 16:37:54 +08:00
|
|
|
Elf64_Shdr* shdr64 = elfData.ffelf64_getshdr(scn);
|
2024-08-08 15:03:10 +08:00
|
|
|
Elf32_Shdr* shdr32 = NULL;
|
|
|
|
|
if (shdr64 == NULL)
|
|
|
|
|
{
|
2024-08-20 16:37:54 +08:00
|
|
|
shdr32 = elfData.ffelf32_getshdr(scn);
|
2024-08-08 15:03:10 +08:00
|
|
|
if (shdr32 == NULL) continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
// Get the section name and check if it's .rodata
|
2024-08-20 16:37:54 +08:00
|
|
|
const char* name = elfData.ffelf_strptr(elf, shstrndx, shdr64 ? shdr64->sh_name : shdr32->sh_name);
|
2024-08-08 15:03:10 +08:00
|
|
|
if (name == NULL || !ffStrEquals(name, ".rodata")) continue;
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
// Get the section data
|
2024-08-20 16:37:54 +08:00
|
|
|
Elf_Data* data = elfData.ffelf_getdata(scn, NULL);
|
2024-08-08 15:03:10 +08:00
|
|
|
if (data == NULL) continue;
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
// Scan the section for string literals
|
2024-08-08 15:03:10 +08:00
|
|
|
for (size_t off = 0; off < data->d_size; ++off)
|
|
|
|
|
{
|
|
|
|
|
const char* p = (const char*) data->d_buf + off;
|
|
|
|
|
if (*p == '\0') continue;
|
|
|
|
|
uint32_t len = (uint32_t) strlen(p);
|
2024-08-27 16:19:05 +08:00
|
|
|
if (len < minLength) continue;
|
2025-04-02 10:27:25 +08:00
|
|
|
// Only process printable ASCII characters
|
2024-08-08 15:03:10 +08:00
|
|
|
if (*p >= ' ' && *p <= '~') // Ignore control characters
|
|
|
|
|
{
|
|
|
|
|
if (!cb(p, len, userdata)) break;
|
|
|
|
|
}
|
|
|
|
|
off += len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 16:37:54 +08:00
|
|
|
elfData.ffelf_end(elf);
|
2024-08-08 15:03:10 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2025-04-02 10:27:25 +08:00
|
|
|
/**
|
|
|
|
|
* Fallback implementation when libelf is not available
|
|
|
|
|
*/
|
2024-08-28 09:41:08 +08:00
|
|
|
const char* ffBinaryExtractStrings(const char* file, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength)
|
2024-08-08 15:03:10 +08:00
|
|
|
{
|
2024-08-28 09:41:08 +08:00
|
|
|
FF_UNUSED(file, cb, userdata, minLength);
|
2024-08-08 15:03:10 +08:00
|
|
|
return "Fastfetch was built without libelf support";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|