style(kernel/elf): change code style
Some checks failed
Build OS / Build Cross-Compiler & Toolchain (push) Has been cancelled
Deploy Website / Deploy Website to GitHub Pages (push) Has been cancelled
Build OS / Analyze (c-cpp) (push) Has been cancelled
Build OS / Build OS (push) Has been cancelled

Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
2025-04-03 12:06:23 +00:00
parent fe6d7f4b08
commit 0041300a00
9 changed files with 238 additions and 304 deletions

View File

@ -25,77 +25,72 @@ namespace Execute
{
/* Originally from https://wiki.osdev.org/ELF_Tutorial */
void ELFLoadRel(void *BaseImage,
const char *Name,
Tasking::PCB *Process)
void ELFLoadRel(void *BaseImage, const char *Name, Tasking::PCB *Process)
{
#if defined(__amd64__)
UNUSED(Name);
debug("Relocatable");
/* TODO: I have to fully implement this, but for now I will leave it as it is now. */
warn("Relocatable ELF is not fully supported yet");
Elf64_Shdr *shdr = GetELFSheader(((Elf64_Ehdr *)BaseImage));
for (Elf64_Half i = 0; i < ((Elf64_Ehdr *)BaseImage)->e_shnum; i++)
Elf_Shdr *shdr = GetELFSheader(((Elf_Ehdr *)BaseImage));
for (Elf_Half i = 0; i < ((Elf_Ehdr *)BaseImage)->e_shnum; i++)
{
Elf64_Shdr *Section = &shdr[i];
if (Section->sh_type == SHT_NOBITS)
Elf_Shdr *section = &shdr[i];
if (section->sh_type == SHT_NOBITS)
{
if (!Section->sh_size)
if (!section->sh_size)
continue;
if (Section->sh_flags & SHF_ALLOC)
if (section->sh_flags & SHF_ALLOC)
{
void *Buffer = KernelAllocator.RequestPages(TO_PAGES(Section->sh_size + 1));
memset(Buffer, 0, Section->sh_size);
void *buffer = KernelAllocator.RequestPages(TO_PAGES(section->sh_size + 1));
memset(buffer, 0, section->sh_size);
Memory::Virtual(Process->PageTable).Map((void *)Buffer, (void *)Buffer, Section->sh_size, Memory::PTFlag::RW | Memory::PTFlag::US);
Memory::Virtual(Process->PageTable).Map((void *)buffer, (void *)buffer, section->sh_size, Memory::PTFlag::RW | Memory::PTFlag::US);
Section->sh_offset = (uintptr_t)Buffer - (uintptr_t)BaseImage;
debug("Section %ld", Section->sh_size);
section->sh_offset = (uintptr_t)buffer - (uintptr_t)BaseImage;
debug("Section %ld", section->sh_size);
}
}
}
for (Elf64_Half i = 0; i < ((Elf64_Ehdr *)BaseImage)->e_shnum; i++)
for (Elf_Half i = 0; i < ((Elf_Ehdr *)BaseImage)->e_shnum; i++)
{
Elf64_Shdr *Section = &shdr[i];
if (Section->sh_type == SHT_REL)
Elf_Shdr *section = &shdr[i];
if (section->sh_type == SHT_REL)
{
for (size_t Index = 0; Index < Section->sh_size / Section->sh_entsize; Index++)
for (size_t i = 0; i < section->sh_size / section->sh_entsize; i++)
{
Elf64_Rel *RelTable = &((Elf64_Rel *)((uintptr_t)BaseImage + Section->sh_offset))[Index];
Elf64_Shdr *Target = GetELFSection(((Elf64_Ehdr *)BaseImage), Section->sh_info);
Elf_Rel *rel = &((Elf_Rel *)((uintptr_t)BaseImage + section->sh_offset))[i];
Elf_Shdr *target = GetELFSection(((Elf_Ehdr *)BaseImage), section->sh_info);
uintptr_t *RelAddress = (uintptr_t *)(((uintptr_t)BaseImage + Target->sh_offset) + RelTable->r_offset);
uint64_t SymbolValue = 0;
uintptr_t *relPtr = (uintptr_t *)(((uintptr_t)BaseImage + target->sh_offset) + rel->r_offset);
uintptr_t value = 0;
if (ELF64_R_SYM(RelTable->r_info) != SHN_UNDEF)
if (ELF64_R_SYM(rel->r_info) != SHN_UNDEF)
{
SymbolValue = ELFGetSymbolValue(((Elf64_Ehdr *)BaseImage), Section->sh_link, ELF64_R_SYM(RelTable->r_info));
if (SymbolValue == 0xdead)
value = ELFGetSymbolValue(((Elf_Ehdr *)BaseImage), section->sh_link, ELF64_R_SYM(rel->r_info));
if (value == (uintptr_t)-1)
return;
}
switch (ELF64_R_TYPE(RelTable->r_info))
switch (ELF64_R_TYPE(rel->r_info))
{
case R_386_NONE:
break;
case R_386_32:
*RelAddress = DO_64_64(SymbolValue, *RelAddress);
*relPtr = DO_64_64(value, *relPtr);
break;
case R_386_PC32:
*RelAddress = DO_64_PC32(SymbolValue, *RelAddress, (uintptr_t)RelAddress);
*relPtr = DO_64_PC32(value, *relPtr, (uintptr_t)relPtr);
break;
default:
{
error("Unsupported relocation type: %d", ELF64_R_TYPE(RelTable->r_info));
error("Unsupported relocation type: %d", ELF64_R_TYPE(rel->r_info));
return;
}
}
debug("Symbol value: %#lx", SymbolValue);
debug("Symbol value: %#lx", value);
}
}
}
#elif defined(__i386__)
#endif
}
}