From: Michael Tremer Date: Fri, 17 Mar 2023 15:45:18 +0000 (+0000) Subject: file: Unify fetching ELF sections X-Git-Tag: 0.9.29~260 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0ef205797318e7da4d1c94c4de2824e9b6c2c407;p=pakfire.git file: Unify fetching ELF sections Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index aed2284a7..37a1ac4e2 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -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, §ion_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)