From: Michael Tremer Date: Thu, 2 Jan 2025 16:42:07 +0000 (+0000) Subject: ELF: Improve the stripped check X-Git-Tag: 0.9.30~585 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4b62e90ec193c269e509e1a33a2e38dcecf8f41d;p=pakfire.git ELF: Improve the stripped check This now checks if we have relocation information and any sections starting with .debug_* or .zdebug_*. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/elf.c b/src/libpakfire/elf.c index 1cc5d9fbc..f3bff5bf5 100644 --- a/src/libpakfire/elf.c +++ b/src/libpakfire/elf.c @@ -791,8 +791,46 @@ int pakfire_elf_has_runpaths(struct pakfire_elf* self, char*** runpaths) { return pakfire_elf_dyn_walk(self, pakfire_elf_check_runpath, runpaths); } +static int __pakfire_elf_is_stripped(struct pakfire_elf* self, + const Elf_Scn* section, const Elf64_Shdr* shdr, Elf_Data* data) { + // Fetch the section name + const char* name = elf_strptr(self->elf, self->shstrndx, shdr->sh_name); + if (!name) + return -EINVAL; + + switch (shdr->sh_type) { + // We should not have .symtab + case SHT_SYMTAB: + return 1; + + // We should not have .symstr + case SHT_STRTAB: + if (pakfire_string_equals(name, ".symstr")) + return 1; + + break; + + // Sometimes we need to check by name... + default: + // There should be nothing here starting with .debug_* or .zdebug_* + if (pakfire_string_startswith(name, ".debug_")) + return 1; + + else if (pakfire_string_startswith(name, ".zdebug_*")) + return 1; + + // GDB symbol lookup + else if (pakfire_string_equals(name, ".gdb_index")) + return 1; + + break; + } + + return 0; +} + int pakfire_elf_is_stripped(struct pakfire_elf* self) { - Elf_Scn* symtab = NULL; + int r; switch (pakfire_elf_type(self)) { // Do not check Relocatable Objects @@ -804,6 +842,15 @@ int pakfire_elf_is_stripped(struct pakfire_elf* self) { break; } - // Fetch the symbol table - return pakfire_elf_get_section(self, SHT_SYMTAB, NULL, &symtab, NULL, NULL); + // Run through all sections + r = pakfire_elf_foreach_section(self, SHT_NULL, __pakfire_elf_is_stripped); + if (r < 0) + return r; + + // If the callback found something, the binary is not stripped + else if (r == 1) + return 0; + + // Otherwise we assume the binary being stripped + return 1; }