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

This commit is contained in:
EnderIce2 2025-04-18 12:35:44 +00:00
parent d3fd61c068
commit 366fd97c0a
Signed by: enderice2
GPG Key ID: FEB6B8A8507BA62E
3 changed files with 10 additions and 12 deletions

View File

@ -26,7 +26,7 @@ namespace Execute
std::vector<Elf_Dyn> ret;
std::vector<Elf_Phdr> phdrs = ELFGetSymbolType(fd, PT_DYNAMIC);
if (phdrs.size() < 1)
if (phdrs.empty())
{
debug("No dynamic phdrs found.");
return ret;
@ -34,7 +34,7 @@ namespace Execute
for (auto phdr : phdrs)
{
Elf_Dyn dyn{};
Elf_Dyn dyn;
for (size_t i = 0; i < phdr.p_filesz / sizeof(Elf_Dyn); i++)
{
fd->Read(&dyn, sizeof(Elf_Dyn), phdr.p_offset + (i * sizeof(Elf_Dyn)));

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;
}
}

View File

@ -25,10 +25,10 @@ namespace Execute
{
std::vector<Elf_Phdr> ret;
Elf_Ehdr ehdr{};
Elf_Ehdr ehdr;
fd->Read(&ehdr, sizeof(Elf_Ehdr), 0);
Elf_Phdr phdr{};
Elf_Phdr phdr;
fd->Read(&phdr, sizeof(Elf_Phdr), ehdr.e_phoff);
off_t off = ehdr.e_phoff;