]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: stop copying symbol names in kmod_elf_get_symbols()
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 23 Oct 2024 16:19:53 +0000 (17:19 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 15 Nov 2024 16:03:46 +0000 (10:03 -0600)
Since the caller already copies the strings as needed, we can just
point to the elf data directly.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-elf.c

index 2055a07264995e5e3127273f7688534c8426c146..3411e3f8774fb365db1ca035e11d6f4098b44659 100644 (file)
@@ -756,8 +756,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
        static const size_t crc_strlen = sizeof(crc_str) - 1;
        uint64_t strtablen, symtablen, str_sec_off, sym_sec_off, str_off, sym_off;
        struct kmod_modversion *a;
-       char *itr;
-       size_t i, count, symcount, slen, symlen;
+       size_t i, count, symcount, symlen;
        int err;
 
        err = kmod_elf_get_section(elf, ".strtab", &str_sec_off, &strtablen);
@@ -787,7 +786,6 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
 
        symcount = symtablen / symlen;
        count = 0;
-       slen = 0;
        str_off = str_sec_off;
        sym_off = sym_sec_off + symlen;
        for (i = 1; i < symcount; i++, sym_off += symlen) {
@@ -819,18 +817,16 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
 
                if (strncmp(name, crc_str, crc_strlen) != 0)
                        continue;
-               slen += strlen(name + crc_strlen) + 1;
                count++;
        }
 
        if (count == 0)
                goto fallback;
 
-       *array = a = malloc(sizeof(struct kmod_modversion) * count + slen);
+       *array = a = malloc(sizeof(struct kmod_modversion) * count);
        if (*array == NULL)
                return -errno;
 
-       itr = (char *)(a + count);
        count = 0;
        str_off = str_sec_off;
        sym_off = sym_sec_off + symlen;
@@ -871,11 +867,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
 
                a[count].crc = kmod_elf_resolve_crc(elf, crc, shndx);
                a[count].bind = kmod_symbol_bind_from_elf(bind);
-               a[count].symbol = itr;
-               slen = strlen(name);
-               memcpy(itr, name, slen);
-               itr[slen] = '\0';
-               itr += slen + 1;
+               a[count].symbol = name;
                count++;
        }
        return count;