mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-05-28 15:34:31 +00:00
refactor(kernel/elf): simplify dynamic tag and section handling in ELF parsing
This commit is contained in:
parent
d3fd61c068
commit
366fd97c0a
@ -26,7 +26,7 @@ namespace Execute
|
|||||||
std::vector<Elf_Dyn> ret;
|
std::vector<Elf_Dyn> ret;
|
||||||
std::vector<Elf_Phdr> phdrs = ELFGetSymbolType(fd, PT_DYNAMIC);
|
std::vector<Elf_Phdr> phdrs = ELFGetSymbolType(fd, PT_DYNAMIC);
|
||||||
|
|
||||||
if (phdrs.size() < 1)
|
if (phdrs.empty())
|
||||||
{
|
{
|
||||||
debug("No dynamic phdrs found.");
|
debug("No dynamic phdrs found.");
|
||||||
return ret;
|
return ret;
|
||||||
@ -34,7 +34,7 @@ namespace Execute
|
|||||||
|
|
||||||
for (auto phdr : phdrs)
|
for (auto phdr : phdrs)
|
||||||
{
|
{
|
||||||
Elf_Dyn dyn{};
|
Elf_Dyn dyn;
|
||||||
for (size_t i = 0; i < phdr.p_filesz / sizeof(Elf_Dyn); i++)
|
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)));
|
fd->Read(&dyn, sizeof(Elf_Dyn), phdr.p_offset + (i * sizeof(Elf_Dyn)));
|
||||||
|
@ -25,24 +25,22 @@ namespace Execute
|
|||||||
{
|
{
|
||||||
std::vector<Elf_Shdr> ret;
|
std::vector<Elf_Shdr> ret;
|
||||||
|
|
||||||
Elf_Ehdr ehdr{};
|
Elf_Ehdr ehdr;
|
||||||
fd->Read(&ehdr, sizeof(Elf_Ehdr), 0);
|
fd->Read(&ehdr, sizeof(Elf_Ehdr), 0);
|
||||||
|
|
||||||
Elf_Shdr *sections = new Elf_Shdr[ehdr.e_shnum];
|
std::unique_ptr<Elf_Shdr[]> sections(new Elf_Shdr[ehdr.e_shnum]);
|
||||||
fd->Read(sections, sizeof(Elf_Shdr) * ehdr.e_shnum, ehdr.e_shoff);
|
fd->Read(sections.get(), sizeof(Elf_Shdr) * ehdr.e_shnum, ehdr.e_shoff);
|
||||||
|
|
||||||
char *sectionNames = new char[sections[ehdr.e_shstrndx].sh_size];
|
std::string sectionNames(sections[ehdr.e_shstrndx].sh_size, '\0');
|
||||||
fd->Read(sectionNames, sections[ehdr.e_shstrndx].sh_size, sections[ehdr.e_shstrndx].sh_offset);
|
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)
|
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)
|
if (strcmp(Name, SectionName) == 0)
|
||||||
ret.push_back(sections[i]);
|
ret.push_back(sections[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete[] sections;
|
|
||||||
delete[] sectionNames;
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,10 +25,10 @@ namespace Execute
|
|||||||
{
|
{
|
||||||
std::vector<Elf_Phdr> ret;
|
std::vector<Elf_Phdr> ret;
|
||||||
|
|
||||||
Elf_Ehdr ehdr{};
|
Elf_Ehdr ehdr;
|
||||||
fd->Read(&ehdr, sizeof(Elf_Ehdr), 0);
|
fd->Read(&ehdr, sizeof(Elf_Ehdr), 0);
|
||||||
|
|
||||||
Elf_Phdr phdr{};
|
Elf_Phdr phdr;
|
||||||
fd->Read(&phdr, sizeof(Elf_Phdr), ehdr.e_phoff);
|
fd->Read(&phdr, sizeof(Elf_Phdr), ehdr.e_phoff);
|
||||||
|
|
||||||
off_t off = ehdr.e_phoff;
|
off_t off = ehdr.e_phoff;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user