]> git.ipfire.org Git - pakfire.git/commitdiff
ELF: Add function to check if a file is stripped
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 17:08:11 +0000 (17:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 17:08:11 +0000 (17:08 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/elf.c
src/libpakfire/include/pakfire/elf.h

index 5e59f74fe2b16d48ffeed189baa26bd031d5d212..4ad4b3e127e757ff493aae1bff0894ec5656839b 100644 (file)
@@ -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);
+}
index a5038030a7194443bf72ee3606667d562b99b9ab..0ecf6a56b078f907f40255138575aed78bf4927d 100644 (file)
@@ -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 */