]> git.ipfire.org Git - pakfire.git/commitdiff
ELF: Improve the stripped check
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Jan 2025 16:42:07 +0000 (16:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Jan 2025 16:42:07 +0000 (16:42 +0000)
This now checks if we have relocation information and any sections
starting with .debug_* or .zdebug_*.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/elf.c

index 1cc5d9fbc692bcbe22cb6c617ffc85b90caed6bd..f3bff5bf5982e9defd64c4d5bef2d5eb22acfacc 100644 (file)
@@ -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;
 }