]> git.ipfire.org Git - pakfire.git/commitdiff
ELF: Fetch the string index only once
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Jan 2025 16:36:11 +0000 (16:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Jan 2025 16:36:11 +0000 (16:36 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/elf.c

index d0faadb507f3b0ccabe2150d6b2c04ad590173e1..1cc5d9fbc692bcbe22cb6c617ffc85b90caed6bd 100644 (file)
@@ -48,6 +48,9 @@ struct pakfire_elf {
        // ELF Header
        GElf_Ehdr ehdr;
 
+       // Strings
+       size_t shstrndx;
+
        // GNU Build ID
        char* build_id;
 
@@ -98,6 +101,13 @@ static int pakfire_elf_open_elf(struct pakfire_elf* self) {
                return -EINVAL;
        }
 
+       // Find the strings
+       r = elf_getshdrstrndx(self->elf, &self->shstrndx);
+       if (r < 0) {
+               ERROR(self->ctx, "elf_getshdrstrndx() failed: %s\n", elf_errmsg(-1));
+               return -EINVAL;
+       }
+
        return 0;
 }
 
@@ -265,16 +275,6 @@ static int pakfire_elf_get_section(struct pakfire_elf* self,
        const char* sname = NULL;
        Elf_Scn* s = NULL;
        GElf_Shdr shdr;
-       int r;
-
-       size_t shstrndx = 0;
-
-       // Find the strings
-       r = elf_getshdrstrndx(self->elf, &shstrndx);
-       if (r < 0) {
-               ERROR(self->ctx, "elf_getshdrstrndx() failed: %s\n", elf_errmsg(-1));
-               return r;
-       }
 
        // Walk through all sections
        for (;;) {
@@ -289,7 +289,7 @@ static int pakfire_elf_get_section(struct pakfire_elf* self,
                if (shdr.sh_type == type) {
                        // If we have a name, check it too
                        if (name) {
-                               sname = elf_strptr(self->elf, shstrndx, shdr.sh_name);
+                               sname = elf_strptr(self->elf, self->shstrndx, shdr.sh_name);
                                if (!sname)
                                        continue;