Skip to content

Commit

Permalink
libkmod: stop copying symbol names in kmod_elf_get_modversions()
Browse files Browse the repository at this point in the history
Since the caller already copies the strings as needed, we can just
point to the elf data directly.

Signed-off-by: Emil Velikov <[email protected]>
Reviewed-by: Tobias Stoeckmann <[email protected]>
Link: #210
Signed-off-by: Lucas De Marchi <[email protected]>
  • Loading branch information
evelikov authored and lucasdemarchi committed Nov 15, 2024
1 parent b5c9f24 commit 069d314
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions libkmod/libkmod-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,9 @@ static inline void elf_get_modversion_lengths(const struct kmod_elf *elf, size_t
/* array will be allocated with strings in a single malloc, just free *array */
int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion **array)
{
size_t off, crclen, namlen, slen, verlen;
size_t off, crclen, namlen, verlen;
uint64_t sec_off, size;
struct kmod_modversion *a;
char *itr;
int i, count, err;

elf_get_modversion_lengths(elf, &verlen, &crclen, &namlen);
Expand All @@ -505,10 +504,12 @@ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion

count = size / verlen;

off = sec_off;
slen = 0;
*array = a = malloc(sizeof(struct kmod_modversion) * count);
if (*array == NULL)
return -errno;

for (i = 0; i < count; i++, off += verlen) {
for (i = 0, off = sec_off; i < count; i++, off += verlen) {
uint64_t crc = elf_get_uint(elf, off, crclen);
const char *symbol = elf_get_mem(elf, off + crclen);
size_t nlen = strnlen(symbol, namlen);

Expand All @@ -517,33 +518,12 @@ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion
return -EINVAL;
}

if (symbol[0] == '.')
nlen--;

slen += nlen + 1;
}

*array = a = malloc(sizeof(struct kmod_modversion) * count + slen);
if (*array == NULL)
return -errno;

itr = (char *)(a + count);
off = sec_off;

for (i = 0; i < count; i++, off += verlen) {
uint64_t crc = elf_get_uint(elf, off, crclen);
const char *symbol = elf_get_mem(elf, off + crclen);
size_t symbollen;

if (symbol[0] == '.')
symbol++;

a[i].crc = crc;
a[i].bind = KMOD_SYMBOL_UNDEF;
a[i].symbol = itr;
symbollen = strlen(symbol) + 1;
memcpy(itr, symbol, symbollen);
itr += symbollen;
a[i].symbol = symbol;
}

return count;
Expand Down

0 comments on commit 069d314

Please sign in to comment.