]> git.ipfire.org Git - pakfire.git/commitdiff
file: Make fetch more information from ELF sections easier
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 18 Mar 2023 11:23:12 +0000 (11:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 18 Mar 2023 11:23:12 +0000 (11:23 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/file.c

index 37a1ac4e209908e442bbdddea2b462883a80593a..b31f04747f5b3c0f03452007f99e02db8ec441e2 100644 (file)
@@ -1675,27 +1675,39 @@ 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;
+static int pakfire_file_get_elf_section(struct pakfire_file* file,
+               Elf* elf, 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 (;;) {
-               section = elf_nextscn(elf, section);
-               if (!section)
+               s = elf_nextscn(elf, s);
+               if (!s)
                        break;
 
                // Fetch the section header
-               gelf_getshdr(section, &shdr);
+               gelf_getshdr(s, &shdr);
 
                // Return any matching sections
-               if (shdr.sh_type == type)
-                       return section;
+               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 NULL;
+       return 1;
 }
 
 static int __pakfire_file_get_elf_type(struct pakfire_file* file, Elf* elf, void* data) {
@@ -1726,11 +1738,14 @@ 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* symtab = NULL;
+       int r;
+
        // Fetch the symbol table
-       Elf_Scn* symtab = pakfire_file_get_elf_section(file, elf, SHT_SYMTAB);
+       r = pakfire_file_get_elf_section(file, elf, SHT_SYMTAB, &symtab, NULL, NULL);
 
        // Not found
-       if (!symtab) {
+       if (r) {
                DEBUG(file->pakfire, "%s has no debug sections\n", file->path);
 
                // Store the result
@@ -1761,23 +1776,18 @@ static int __pakfire_file_check_ssp(
        Elf_Data* elf_data = NULL;
        GElf_Sym symbol;
        const char* name = NULL;
+       int r;
 
        // Fetch the symbol table
-       symtab = pakfire_file_get_elf_section(file, elf, SHT_SYMTAB);
-       if (!symtab) {
+       r = pakfire_file_get_elf_section(file, elf, SHT_SYMTAB, &symtab, &shdr, &elf_data);
+       if (r) {
                ERROR(file->pakfire, "%s has no symbol table\n", file->path);
                return 1;
        }
 
-       // Fetch the section header
-       gelf_getshdr(symtab, &shdr);
-
        // Count any global functions
        size_t counter = 0;
 
-       // Fetch a pointer to the section data
-       elf_data = elf_getdata(symtab, NULL);
-
        // Walk through all symbols
        for (unsigned int i = 0; i < shdr.sh_size / shdr.sh_entsize; i++) {
                gelf_getsym(elf_data, i, &symbol);