From: Michael Tremer Date: Fri, 3 Jan 2025 08:52:14 +0000 (+0000) Subject: ELF: Add a function to fetch the interpreter X-Git-Tag: 0.9.30~569 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=309450602dd25ec8c6a8f020e6035f687f12166b;p=pakfire.git ELF: Add a function to fetch the interpreter Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/elf.c b/src/pakfire/elf.c index c6c681fab..d6863d2e7 100644 --- a/src/pakfire/elf.c +++ b/src/pakfire/elf.c @@ -55,6 +55,9 @@ struct pakfire_elf { // Strings size_t shstrndx; + // Interpreter + const char* interpreter; + // GNU Build ID char* build_id; @@ -421,6 +424,42 @@ static int pakfire_elf_dyn_walk(struct pakfire_elf* self, return 0; } +static int pakfire_elf_fetch_interpreter( + struct pakfire_elf* self, const GElf_Phdr* phdr, void* data) { + Elf_Data* chunk = NULL; + + switch (phdr->p_type) { + case PT_INTERP: + chunk = elf_getdata_rawchunk(self->elf, phdr->p_offset, phdr->p_filesz, ELF_T_BYTE); + if (!chunk || !chunk->d_buf) { + ERROR(self->ctx, "Failed to fetch the interpreter\n"); + return -EINVAL; + } + + // Store the interpreter + self->interpreter = (const char*)chunk->d_buf; + break; + + default: + break; + } + + return 0; +} + +const char* pakfire_elf_interpreter(struct pakfire_elf* self) { + int r; + + // Fetch the interpreter if not available + if (!self->interpreter) { + r = pakfire_elf_foreach_program_header(self, pakfire_elf_fetch_interpreter, NULL); + if (r < 0) + return NULL; + } + + return self->interpreter; +} + int pakfire_elf_is_pie(struct pakfire_elf* self) { switch (pakfire_elf_type(self)) { // Shared Object files are good diff --git a/src/pakfire/elf.h b/src/pakfire/elf.h index f723d5224..a1d591797 100644 --- a/src/pakfire/elf.h +++ b/src/pakfire/elf.h @@ -43,6 +43,7 @@ int pakfire_elf_machine(struct pakfire_elf* self); int pakfire_elf_endianess(struct pakfire_elf* self); const char* pakfire_elf_build_id(struct pakfire_elf* self); const char* pakfire_elf_debuglink(struct pakfire_elf* self); +const char* pakfire_elf_interpreter(struct pakfire_elf* elf); int pakfire_elf_is_pie(struct pakfire_elf* self); int pakfire_elf_has_ssp(struct pakfire_elf* self);