]> git.ipfire.org Git - pakfire.git/commitdiff
file: Unify fetching ELF sections
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Mar 2023 15:45:18 +0000 (15:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Mar 2023 15:45:18 +0000 (15:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/file.c

index aed2284a7afcb271b9948069c8c468f03bc939f0..37a1ac4e209908e442bbdddea2b462883a80593a 100644 (file)
@@ -1675,6 +1675,29 @@ ERROR:
        return r;
 }
 
+static Elf_Scn* pakfire_file_get_elf_section(struct pakfire_file* file,
+               Elf* elf, const Elf64_Word type) {
+       Elf_Scn* section = NULL;
+       GElf_Shdr shdr;
+
+       // Walk through all sections
+       for (;;) {
+               section = elf_nextscn(elf, section);
+               if (!section)
+                       break;
+
+               // Fetch the section header
+               gelf_getshdr(section, &shdr);
+
+               // Return any matching sections
+               if (shdr.sh_type == type)
+                       return section;
+       }
+
+       // No section found
+       return NULL;
+}
+
 static int __pakfire_file_get_elf_type(struct pakfire_file* file, Elf* elf, void* data) {
        int* type = (int*)data;
        GElf_Ehdr ehdr;
@@ -1703,30 +1726,16 @@ static int pakfire_file_get_elf_type(struct pakfire_file* file) {
 }
 
 static int __pakfire_file_check_debuginfo(struct pakfire_file* file, Elf* elf, void* data) {
-       Elf_Scn* section = NULL;
-       GElf_Shdr shdr;
-
-       // Walk through all sections
-       for (;;) {
-               section = elf_nextscn(elf, section);
-               if (!section)
-                       break;
-
-               // Fetch the section header
-               gelf_getshdr(section, &shdr);
-
-               switch (shdr.sh_type) {
-                       // Break if we found the symbol table
-                       case SHT_SYMTAB:
-                               return 0;
-               }
-       }
+       // Fetch the symbol table
+       Elf_Scn* symtab = pakfire_file_get_elf_section(file, elf, SHT_SYMTAB);
 
        // Not found
-       DEBUG(file->pakfire, "%s has no debug sections\n", file->path);
+       if (!symtab) {
+               DEBUG(file->pakfire, "%s has no debug sections\n", file->path);
 
-       // Store the result
-       file->issues |= PAKFIRE_FILE_MISSING_DEBUGINFO;
+               // Store the result
+               file->issues |= PAKFIRE_FILE_MISSING_DEBUGINFO;
+       }
 
        return 0;
 }
@@ -1747,40 +1756,34 @@ static int pakfire_file_check_debuginfo(struct pakfire_file* file) {
 
 static int __pakfire_file_check_ssp(
                struct pakfire_file* file, Elf* elf, void* data) {
-       Elf_Scn* section = NULL;
-       GElf_Shdr section_header;
+       Elf_Scn* symtab = NULL;
+       GElf_Shdr shdr;
        Elf_Data* elf_data = NULL;
        GElf_Sym symbol;
        const char* name = NULL;
 
-       // Count any global functions
-       size_t counter = 0;
-
-       // Walk through all sections
-       for (;;) {
-               section = elf_nextscn(elf, section);
-               if (!section) {
-                       ERROR(file->pakfire, "%s has no symbol table\n", file->path);
-                       return 1;
-               }
+       // Fetch the symbol table
+       symtab = pakfire_file_get_elf_section(file, elf, SHT_SYMTAB);
+       if (!symtab) {
+               ERROR(file->pakfire, "%s has no symbol table\n", file->path);
+               return 1;
+       }
 
-               // Fetch the section header
-               gelf_getshdr(section, &section_header);
+       // Fetch the section header
+       gelf_getshdr(symtab, &shdr);
 
-               // Break if we found the symbol table
-               if (section_header.sh_type == SHT_SYMTAB)
-                       break;
-       }
+       // Count any global functions
+       size_t counter = 0;
 
        // Fetch a pointer to the section data
-       elf_data = elf_getdata(section, NULL);
+       elf_data = elf_getdata(symtab, NULL);
 
        // Walk through all symbols
-       for (unsigned int i = 0; i < section_header.sh_size / section_header.sh_entsize; i++) {
+       for (unsigned int i = 0; i < shdr.sh_size / shdr.sh_entsize; i++) {
                gelf_getsym(elf_data, i, &symbol);
 
                // Fetch the symbol name
-               name = elf_strptr(elf, section_header.sh_link, symbol.st_name);
+               name = elf_strptr(elf, shdr.sh_link, symbol.st_name);
 
                // Skip empty section names
                if (!name || !*name)