From: Michael Tremer Date: Wed, 1 Jan 2025 17:08:11 +0000 (+0000) Subject: ELF: Add function to check if a file is stripped X-Git-Tag: 0.9.30~607 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ffc45efd5e570d7f5b9e47aa46744b6d4bde930;p=pakfire.git ELF: Add function to check if a file is stripped Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/elf.c b/src/libpakfire/elf.c index 5e59f74fe..4ad4b3e12 100644 --- a/src/libpakfire/elf.c +++ b/src/libpakfire/elf.c @@ -232,3 +232,54 @@ const char* pakfire_elf_build_id(struct pakfire_elf* self) { return self->build_id; } + +static int pakfire_elf_get_section(struct pakfire_elf* self, + const Elf64_Word type, Elf_Scn** section, GElf_Shdr* header, Elf_Data** data) { + Elf_Scn* s = NULL; + GElf_Shdr shdr; + + // Walk through all sections + for (;;) { + s = elf_nextscn(self->elf, s); + if (!s) + break; + + // Fetch the section header + gelf_getshdr(s, &shdr); + + // Return any matching sections + if (shdr.sh_type == type) { + *section = s; + + // Send header if requested + if (header) + gelf_getshdr(s, header); + + // Send data if requested + if (data) + *data = elf_getdata(s, NULL); + + return 0; + } + } + + // No section found + return 1; +} + +int pakfire_elf_is_stripped(struct pakfire_elf* self) { + Elf_Scn* symtab = NULL; + + switch (pakfire_elf_type(self)) { + // Do not check Relocatable Objects + case ET_REL: + return 0; + + // Check everything else + default: + break; + } + + // Fetch the symbol table + return pakfire_elf_get_section(self, SHT_SYMTAB, &symtab, NULL, NULL); +} diff --git a/src/libpakfire/include/pakfire/elf.h b/src/libpakfire/include/pakfire/elf.h index a5038030a..0ecf6a56b 100644 --- a/src/libpakfire/include/pakfire/elf.h +++ b/src/libpakfire/include/pakfire/elf.h @@ -41,6 +41,8 @@ const char* pakfire_elf_path(struct pakfire_elf* self); int pakfire_elf_type(struct pakfire_elf* self); const char* pakfire_elf_build_id(struct pakfire_elf* self); +int pakfire_elf_is_stripped(struct pakfire_elf* self); + #endif /* PAKFIRE_PRIVATE */ #endif /* PAKFIRE_ELF_H */