refactor(kernel/elf): simplify dynamic tag and section handling in ELF parsing

This commit is contained in:
2025-04-18 12:35:44 +00:00
parent d3fd61c068
commit 366fd97c0a
3 changed files with 10 additions and 12 deletions

View File

@ -25,24 +25,22 @@ namespace Execute
{
std::vector<Elf_Shdr> ret;
Elf_Ehdr ehdr{};
Elf_Ehdr ehdr;
fd->Read(&ehdr, sizeof(Elf_Ehdr), 0);
Elf_Shdr *sections = new Elf_Shdr[ehdr.e_shnum];
fd->Read(sections, sizeof(Elf_Shdr) * ehdr.e_shnum, ehdr.e_shoff);
std::unique_ptr<Elf_Shdr[]> sections(new Elf_Shdr[ehdr.e_shnum]);
fd->Read(sections.get(), sizeof(Elf_Shdr) * ehdr.e_shnum, ehdr.e_shoff);
char *sectionNames = new char[sections[ehdr.e_shstrndx].sh_size];
fd->Read(sectionNames, sections[ehdr.e_shstrndx].sh_size, sections[ehdr.e_shstrndx].sh_offset);
std::string sectionNames(sections[ehdr.e_shstrndx].sh_size, '\0');
fd->Read(sectionNames.data(), sections[ehdr.e_shstrndx].sh_size, sections[ehdr.e_shstrndx].sh_offset);
for (Elf_Half i = 0; i < ehdr.e_shnum; ++i)
{
const char *Name = sectionNames + sections[i].sh_name;
const char *Name = sectionNames.data() + sections[i].sh_name;
if (strcmp(Name, SectionName) == 0)
ret.push_back(sections[i]);
}
delete[] sections;
delete[] sectionNames;
return ret;
}
}