From 25713e0f1323b0f58be49a9ce6f7dfdc36019c54 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Mon, 7 Apr 2025 07:30:48 +0000 Subject: [PATCH] refactor(kernel): improve code readability and formatting --- Kernel/core/memory/reserve_essentials.cpp | 16 +++++----------- Kernel/core/symbols.cpp | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Kernel/core/memory/reserve_essentials.cpp b/Kernel/core/memory/reserve_essentials.cpp index b1253b8e..b46f4566 100644 --- a/Kernel/core/memory/reserve_essentials.cpp +++ b/Kernel/core/memory/reserve_essentials.cpp @@ -80,12 +80,9 @@ namespace Memory { char *sections = r_cst(char *, bInfo.Kernel.Symbols.Sections); debug("Reserving sections region %#lx-%#lx...", - sections, - (void *)((uintptr_t)sections + bInfo.Kernel.Symbols.EntSize * - bInfo.Kernel.Symbols.Num)); + sections, (uintptr_t)sections + bInfo.Kernel.Symbols.EntSize * bInfo.Kernel.Symbols.Num); - this->ReservePages(sections, TO_PAGES(bInfo.Kernel.Symbols.EntSize * - bInfo.Kernel.Symbols.Num)); + this->ReservePages(sections, TO_PAGES(bInfo.Kernel.Symbols.EntSize * bInfo.Kernel.Symbols.Num)); Elf_Sym *Symbols = nullptr; uint8_t *StringAddress = nullptr; @@ -101,11 +98,9 @@ namespace Memory for (size_t i = 0; i < bInfo.Kernel.Symbols.Num; ++i) { Elf_Shdr *sym = (Elf_Shdr *)§ions[bInfo.Kernel.Symbols.EntSize * i]; - Elf_Shdr *str = (Elf_Shdr *)§ions[bInfo.Kernel.Symbols.EntSize * - sym->sh_link]; + Elf_Shdr *str = (Elf_Shdr *)§ions[bInfo.Kernel.Symbols.EntSize * sym->sh_link]; - if (sym->sh_type == SHT_SYMTAB && - str->sh_type == SHT_STRTAB) + if (sym->sh_type == SHT_SYMTAB && str->sh_type == SHT_STRTAB) { Symbols = (Elf_Sym *)sym->sh_addr; StringAddress = (uint8_t *)str->sh_addr; @@ -145,8 +140,7 @@ namespace Memory bInfo.Modules[i].Address, (void *)((uintptr_t)bInfo.Modules[i].Address + bInfo.Modules[i].Size)); - this->ReservePages((void *)bInfo.Modules[i].Address, - TO_PAGES(bInfo.Modules[i].Size)); + this->ReservePages((void *)bInfo.Modules[i].Address, TO_PAGES(bInfo.Modules[i].Size)); } #if defined(__amd64__) || defined(__i386__) diff --git a/Kernel/core/symbols.cpp b/Kernel/core/symbols.cpp index d0fbcd2b..b26a6b58 100644 --- a/Kernel/core/symbols.cpp +++ b/Kernel/core/symbols.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -115,8 +116,7 @@ namespace SymbolResolver Elf_Shdr *sym = (Elf_Shdr *)§ions[EntSize * i]; Elf_Shdr *str = (Elf_Shdr *)§ions[EntSize * sym->sh_link]; - if (sym->sh_type == SHT_SYMTAB && - str->sh_type == SHT_STRTAB) + if (sym->sh_type == SHT_SYMTAB && str->sh_type == SHT_STRTAB) { Symbols = (Elf_Sym *)sym->sh_addr; StringAddress = (uint8_t *)str->sh_addr; @@ -124,9 +124,7 @@ namespace SymbolResolver // StringSize = (int)str->sh_size; // TotalEntries = Section.sh_size / sizeof(Elf64_Sym) TotalEntries = sym->sh_size / sym->sh_entsize; - trace("Symbol table found, %d entries", - SymbolSize / sym->sh_entsize); - UNUSED(SymbolSize); + trace("Symbol table found, %d entries", SymbolSize / sym->sh_entsize); break; } } @@ -138,13 +136,20 @@ namespace SymbolResolver { MinimumIndex = i; for (Index = i + 1; Index < TotalEntries; Index++) - if (Symbols[Index].st_value < Symbols[MinimumIndex].st_value) + { + bool condition = Symbols[Index].st_value < Symbols[MinimumIndex].st_value; + if (condition) MinimumIndex = Index; + } + Elf_Sym tmp = Symbols[MinimumIndex]; Symbols[MinimumIndex] = Symbols[i]; Symbols[i] = tmp; } + // std::sort(Symbols, Symbols + TotalEntries, [](const Elf_Sym &a, const Elf_Sym &b) + // { return a.st_value < b.st_value; }); + while (Symbols[0].st_value == 0) { if (TotalEntries <= 0) @@ -159,8 +164,7 @@ namespace SymbolResolver return; } - trace("Symbol table loaded, %d entries (%ld KiB)", - TotalEntries, TO_KiB(TotalEntries * sizeof(SymbolTable))); + trace("Symbol table loaded, %d entries (%ld KiB)", TotalEntries, TO_KiB(TotalEntries * sizeof(SymbolTable))); Elf_Sym *sym; const char *name; Memory::Virtual vmm;