mirror of
https://github.com/AuxXxilium/kmod.git
synced 2025-03-10 07:53:48 +07:00
libkmod-module: mangle the section header, not the section
When we are told to remove the "__versions" section we were mangling that section instead of tweaking the SHF_ALLOC flag in its header.
This commit is contained in:
parent
d196b8d99f
commit
a4578669ca
@ -375,6 +375,31 @@ const void *kmod_elf_get_memory(const struct kmod_elf *elf)
|
|||||||
return elf->memory;
|
return elf->memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int elf_find_section(const struct kmod_elf *elf, const char *section)
|
||||||
|
{
|
||||||
|
uint64_t nameslen;
|
||||||
|
const char *names = elf_get_strings_section(elf, &nameslen);
|
||||||
|
uint16_t i;
|
||||||
|
|
||||||
|
for (i = 1; i < elf->header.section.count; i++) {
|
||||||
|
uint64_t off, size;
|
||||||
|
uint32_t nameoff;
|
||||||
|
const char *n;
|
||||||
|
int err = elf_get_section_info(elf, i, &off, &size, &nameoff);
|
||||||
|
if (err < 0)
|
||||||
|
continue;
|
||||||
|
if (nameoff >= nameslen)
|
||||||
|
continue;
|
||||||
|
n = names + nameoff;
|
||||||
|
if (!streq(section, n))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
int kmod_elf_get_section(const struct kmod_elf *elf, const char *section, const void **buf, uint64_t *buf_size)
|
int kmod_elf_get_section(const struct kmod_elf *elf, const char *section, const void **buf, uint64_t *buf_size)
|
||||||
{
|
{
|
||||||
uint64_t nameslen;
|
uint64_t nameslen;
|
||||||
@ -550,26 +575,29 @@ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion
|
|||||||
|
|
||||||
int kmod_elf_strip_section(struct kmod_elf *elf, const char *section)
|
int kmod_elf_strip_section(struct kmod_elf *elf, const char *section)
|
||||||
{
|
{
|
||||||
uint64_t size, off;
|
uint64_t off, size;
|
||||||
const void *buf;
|
const void *buf;
|
||||||
int err = kmod_elf_get_section(elf, section, &buf, &size);
|
int idx = elf_find_section(elf, section);
|
||||||
if (err < 0)
|
uint64_t val;
|
||||||
return err;
|
|
||||||
|
|
||||||
|
if (idx < 0)
|
||||||
|
return idx;
|
||||||
|
|
||||||
|
buf = elf_get_section_header(elf, idx);
|
||||||
off = (const uint8_t *)buf - elf->memory;
|
off = (const uint8_t *)buf - elf->memory;
|
||||||
|
|
||||||
#define WRITEV(field, value) \
|
|
||||||
elf_set_uint(elf, off + offsetof(typeof(*hdr), field), sizeof(hdr->field), value)
|
|
||||||
if (elf->class & KMOD_ELF_32) {
|
if (elf->class & KMOD_ELF_32) {
|
||||||
const Elf32_Shdr *hdr _unused_ = buf;
|
off += offsetof(Elf32_Shdr, sh_flags);
|
||||||
uint32_t val = ~(uint32_t)SHF_ALLOC;
|
size = sizeof(((Elf32_Shdr *)buf)->sh_flags);
|
||||||
return WRITEV(sh_flags, val);
|
|
||||||
} else {
|
} else {
|
||||||
const Elf64_Shdr *hdr _unused_ = buf;
|
off += offsetof(Elf64_Shdr, sh_flags);
|
||||||
uint64_t val = ~(uint64_t)SHF_ALLOC;
|
size = sizeof(((Elf64_Shdr *)buf)->sh_flags);
|
||||||
return WRITEV(sh_flags, val);
|
|
||||||
}
|
}
|
||||||
#undef WRITEV
|
|
||||||
|
val = elf_get_uint(elf, off, size);
|
||||||
|
val &= ~(uint64_t)SHF_ALLOC;
|
||||||
|
|
||||||
|
return elf_set_uint(elf, off, size, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
int kmod_elf_strip_vermagic(struct kmod_elf *elf)
|
int kmod_elf_strip_vermagic(struct kmod_elf *elf)
|
||||||
|
Loading…
Reference in New Issue
Block a user