From: Michael Tremer Date: Sat, 18 Mar 2023 11:23:12 +0000 (+0000) Subject: file: Make fetch more information from ELF sections easier X-Git-Tag: 0.9.29~259 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e9d5d508fe07398192a8bf622b379c4fee1a798d;p=pakfire.git file: Make fetch more information from ELF sections easier Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 37a1ac4e2..b31f04747 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -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);