mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-13 08:19:18 +00:00
Update kernel
This commit is contained in:
398
Core/Symbols.cpp
398
Core/Symbols.cpp
@ -25,220 +25,228 @@
|
||||
|
||||
namespace SymbolResolver
|
||||
{
|
||||
const NIF char *Symbols::GetSymbolFromAddress(uintptr_t Address)
|
||||
{
|
||||
Symbols::SymbolTable Result{0, (char *)"<unknown>"};
|
||||
for (int64_t i = 0; i < this->TotalEntries; i++)
|
||||
if (this->SymTable[i].Address <= Address && this->SymTable[i].Address > Result.Address)
|
||||
Result = this->SymTable[i];
|
||||
return Result.FunctionName;
|
||||
}
|
||||
const NIF char *Symbols::GetSymbolFromAddress(uintptr_t Address)
|
||||
{
|
||||
SymbolTable Result{};
|
||||
foreach (auto tbl in this->SymTable)
|
||||
{
|
||||
if (tbl.Address <= Address &&
|
||||
tbl.Address > Result.Address)
|
||||
Result = tbl;
|
||||
}
|
||||
// debug("Address: %#lx, Function: %s",
|
||||
// Address, Result.FunctionName);
|
||||
return Result.FunctionName;
|
||||
}
|
||||
|
||||
void Symbols::AddSymbol(uintptr_t Address, const char *Name)
|
||||
{
|
||||
if (this->TotalEntries >= 0x10000)
|
||||
{
|
||||
error("Symbol table is full");
|
||||
return;
|
||||
}
|
||||
void Symbols::AddSymbol(uintptr_t Address, const char *Name)
|
||||
{
|
||||
SymbolTable tbl{};
|
||||
tbl.Address = Address;
|
||||
tbl.FunctionName = (char *)Name;
|
||||
this->SymTable.push_back(tbl);
|
||||
this->SymbolTableExists = true;
|
||||
}
|
||||
|
||||
this->SymTable[this->TotalEntries].Address = Address;
|
||||
strcpy(this->SymTable[this->TotalEntries].FunctionName, Name);
|
||||
this->TotalEntries++;
|
||||
}
|
||||
|
||||
__no_sanitize("alignment") void Symbols::AddBySymbolInfo(uint64_t Num, uint64_t EntSize, uint64_t Shndx, uintptr_t Sections)
|
||||
{
|
||||
__no_sanitize("alignment") void Symbols::AddSymbolInfoFromGRUB(uint64_t Num,
|
||||
uint64_t EntSize,
|
||||
__unused uint64_t Shndx,
|
||||
uintptr_t Sections)
|
||||
{
|
||||
#ifdef a32
|
||||
fixme("Function not working on 32-bit");
|
||||
return;
|
||||
fixme("Function not working on 32-bit");
|
||||
return;
|
||||
#endif
|
||||
if (this->TotalEntries >= 0x10000)
|
||||
{
|
||||
error("Symbol table is full");
|
||||
return;
|
||||
}
|
||||
char *sections = reinterpret_cast<char *>(Sections);
|
||||
|
||||
Elf_Sym *Symbols = nullptr;
|
||||
uint8_t *StringAddress = nullptr;
|
||||
|
||||
#if defined(a64) || defined(aa64)
|
||||
Elf64_Shdr *ElfSections = (Elf64_Shdr *)(Sections);
|
||||
Elf64_Sym *ElfSymbols = nullptr;
|
||||
Elf64_Xword SymbolSize = 0;
|
||||
// Elf64_Xword StringSize = 0;
|
||||
#elif defined(a32)
|
||||
Elf32_Shdr *ElfSections = (Elf32_Shdr *)(Sections);
|
||||
Elf32_Sym *ElfSymbols = nullptr;
|
||||
Elf32_Word SymbolSize = 0;
|
||||
// Elf32_Word StringSize = 0;
|
||||
#endif
|
||||
char *strtab = nullptr;
|
||||
int64_t TotalEntries = 0;
|
||||
|
||||
for (size_t i = 0; i < Num; ++i)
|
||||
{
|
||||
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)
|
||||
{
|
||||
Symbols = (Elf_Sym *)sym->sh_addr;
|
||||
StringAddress = (uint8_t *)str->sh_addr;
|
||||
SymbolSize = (int)sym->sh_size;
|
||||
// StringSize = (int)str->sh_size;
|
||||
// TotalEntries = Section.sh_size / sizeof(Elf64_Sym)
|
||||
TotalEntries = sym->sh_size / sym->sh_entsize;
|
||||
debug("Symbol table found, %d entries",
|
||||
SymbolSize / sym->sh_entsize);
|
||||
UNUSED(SymbolSize);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (Symbols != nullptr && StringAddress != nullptr)
|
||||
{
|
||||
int64_t Index, MinimumIndex;
|
||||
for (int64_t i = 0; i < TotalEntries - 1; i++)
|
||||
{
|
||||
MinimumIndex = i;
|
||||
for (Index = i + 1; Index < TotalEntries; Index++)
|
||||
if (Symbols[Index].st_value < Symbols[MinimumIndex].st_value)
|
||||
MinimumIndex = Index;
|
||||
Elf_Sym tmp = Symbols[MinimumIndex];
|
||||
Symbols[MinimumIndex] = Symbols[i];
|
||||
Symbols[i] = tmp;
|
||||
}
|
||||
|
||||
while (Symbols[0].st_value == 0)
|
||||
{
|
||||
if (TotalEntries <= 0)
|
||||
break;
|
||||
Symbols++;
|
||||
TotalEntries--;
|
||||
}
|
||||
|
||||
if (TotalEntries <= 0)
|
||||
{
|
||||
error("Symbol table is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
debug("Symbol table loaded, %d entries (%ld KiB)",
|
||||
TotalEntries, TO_KiB(TotalEntries * sizeof(SymbolTable)));
|
||||
Elf_Sym *sym = nullptr;
|
||||
const char *name = nullptr;
|
||||
for (int64_t i = 0, g = TotalEntries; i < g; i++)
|
||||
{
|
||||
sym = &Symbols[i];
|
||||
name = (const char *)&StringAddress[Symbols[i].st_name];
|
||||
if (strlen(name) == 0)
|
||||
continue;
|
||||
SymbolTable tbl{};
|
||||
tbl.Address = sym->st_value;
|
||||
tbl.FunctionName = (char *)name;
|
||||
this->SymTable.push_back(tbl);
|
||||
this->SymbolTableExists = true;
|
||||
|
||||
// debug("Symbol %d: %#lx %s(%#lx)",
|
||||
// i, tbl.Address,
|
||||
// tbl.FunctionName,
|
||||
// name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Symbols::AppendSymbols(uintptr_t ImageAddress, uintptr_t BaseAddress)
|
||||
{
|
||||
/* FIXME: Get only the required headers instead of the whole file */
|
||||
|
||||
if (ImageAddress == 0 || Memory::Virtual().Check((void *)ImageAddress) == false)
|
||||
{
|
||||
error("Invalid image address %#lx", ImageAddress);
|
||||
return;
|
||||
}
|
||||
debug("Solving symbols for address: %#llx", ImageAddress);
|
||||
|
||||
for (uint64_t i = 0; i < Num; i++)
|
||||
switch (ElfSections[i].sh_type)
|
||||
{
|
||||
case SHT_SYMTAB:
|
||||
#if defined(a64) || defined(aa64)
|
||||
ElfSymbols = (Elf64_Sym *)(Sections + ElfSections[i].sh_offset);
|
||||
this->TotalEntries = ElfSections[i].sh_size / sizeof(Elf64_Sym);
|
||||
Elf64_Ehdr *Header = (Elf64_Ehdr *)ImageAddress;
|
||||
#elif defined(a32)
|
||||
ElfSymbols = (Elf32_Sym *)(Sections + ElfSections[i].sh_offset);
|
||||
this->TotalEntries = ElfSections[i].sh_size / sizeof(Elf32_Sym);
|
||||
Elf32_Ehdr *Header = (Elf32_Ehdr *)ImageAddress;
|
||||
#endif
|
||||
if (this->TotalEntries >= 0x10000)
|
||||
this->TotalEntries = 0x10000 - 1;
|
||||
if (Header->e_ident[0] != 0x7F &&
|
||||
Header->e_ident[1] != 'E' &&
|
||||
Header->e_ident[2] != 'L' &&
|
||||
Header->e_ident[3] != 'F')
|
||||
{
|
||||
error("Invalid ELF header");
|
||||
return;
|
||||
}
|
||||
|
||||
debug("Symbol table found, %d entries", this->TotalEntries);
|
||||
break;
|
||||
case SHT_STRTAB:
|
||||
if (Shndx == i)
|
||||
{
|
||||
debug("String table found, %d entries", ElfSections[i].sh_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
strtab = (char *)(Sections + ElfSections[i].sh_offset);
|
||||
debug("String table found, %d entries", ElfSections[i].sh_size);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Elf_Shdr *ElfSections = (Elf_Shdr *)(ImageAddress + Header->e_shoff);
|
||||
Elf_Sym *ElfSymbols = nullptr;
|
||||
char *strtab = nullptr;
|
||||
int64_t TotalEntries = 0;
|
||||
|
||||
if (ElfSymbols != nullptr && strtab != nullptr)
|
||||
{
|
||||
int64_t Index, MinimumIndex;
|
||||
for (int64_t i = 0; i < this->TotalEntries - 1; i++)
|
||||
{
|
||||
MinimumIndex = i;
|
||||
for (Index = i + 1; Index < this->TotalEntries; Index++)
|
||||
if (ElfSymbols[Index].st_value < ElfSymbols[MinimumIndex].st_value)
|
||||
MinimumIndex = Index;
|
||||
#if defined(a64) || defined(aa64)
|
||||
Elf64_Sym tmp = ElfSymbols[MinimumIndex];
|
||||
#elif defined(a32)
|
||||
Elf32_Sym tmp = ElfSymbols[MinimumIndex];
|
||||
#endif
|
||||
ElfSymbols[MinimumIndex] = ElfSymbols[i];
|
||||
ElfSymbols[i] = tmp;
|
||||
}
|
||||
for (uint16_t i = 0; i < Header->e_shnum; i++)
|
||||
{
|
||||
switch (ElfSections[i].sh_type)
|
||||
{
|
||||
case SHT_SYMTAB:
|
||||
ElfSymbols = (Elf_Sym *)(ImageAddress + ElfSections[i].sh_offset);
|
||||
TotalEntries = ElfSections[i].sh_size / sizeof(Elf_Sym);
|
||||
debug("Symbol table found, %d entries", TotalEntries);
|
||||
break;
|
||||
case SHT_STRTAB:
|
||||
if (Header->e_shstrndx == i)
|
||||
{
|
||||
debug("String table found, %d entries", ElfSections[i].sh_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
strtab = (char *)(ImageAddress + ElfSections[i].sh_offset);
|
||||
debug("String table found, %d entries", ElfSections[i].sh_size);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (ElfSymbols[0].st_value == 0)
|
||||
{
|
||||
if (this->TotalEntries <= 0)
|
||||
break;
|
||||
ElfSymbols++;
|
||||
this->TotalEntries--;
|
||||
}
|
||||
if (ElfSymbols != nullptr && strtab != nullptr)
|
||||
{
|
||||
int64_t Index, MinimumIndex;
|
||||
for (int64_t i = 0; i < TotalEntries - 1; i++)
|
||||
{
|
||||
MinimumIndex = i;
|
||||
for (Index = i + 1; Index < TotalEntries; Index++)
|
||||
if (ElfSymbols[Index].st_value < ElfSymbols[MinimumIndex].st_value)
|
||||
MinimumIndex = Index;
|
||||
Elf_Sym tmp = ElfSymbols[MinimumIndex];
|
||||
ElfSymbols[MinimumIndex] = ElfSymbols[i];
|
||||
ElfSymbols[i] = tmp;
|
||||
}
|
||||
|
||||
if (this->TotalEntries <= 0)
|
||||
{
|
||||
error("Symbol table is empty");
|
||||
return;
|
||||
}
|
||||
while (ElfSymbols[0].st_value == 0)
|
||||
{
|
||||
ElfSymbols++;
|
||||
TotalEntries--;
|
||||
}
|
||||
|
||||
trace("Symbol table loaded, %d entries (%ldKB)", this->TotalEntries, TO_KB(this->TotalEntries * sizeof(SymbolTable)));
|
||||
for (int64_t i = 0, g = this->TotalEntries; i < g; i++)
|
||||
{
|
||||
this->SymTable[i].Address = ElfSymbols[i].st_value;
|
||||
this->SymTable[i].FunctionName = &strtab[ElfSymbols[i].st_name];
|
||||
trace("Symbol table loaded, %d entries (%ld KiB)",
|
||||
TotalEntries, TO_KiB(TotalEntries * sizeof(SymbolTable)));
|
||||
|
||||
// debug("Symbol %d: %#llx %s", i, this->SymTable[i].Address, this->SymTable[i].FunctionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* TODO: maybe a checker for duplicated addresses? */
|
||||
Elf_Sym *sym = nullptr;
|
||||
const char *name = nullptr;
|
||||
for (int64_t i = 0, g = TotalEntries; i < g; i++)
|
||||
{
|
||||
sym = &ElfSymbols[i];
|
||||
name = &strtab[ElfSymbols[i].st_name];
|
||||
SymbolTable tbl{};
|
||||
tbl.Address = sym->st_value + BaseAddress;
|
||||
tbl.FunctionName = (char *)name;
|
||||
this->SymTable.push_back(tbl);
|
||||
this->SymbolTableExists = true;
|
||||
|
||||
Symbols::Symbols(uintptr_t ImageAddress)
|
||||
{
|
||||
if (ImageAddress == 0 || Memory::Virtual().Check((void *)ImageAddress) == false)
|
||||
{
|
||||
error("Invalid image address %#lx", ImageAddress);
|
||||
return;
|
||||
}
|
||||
// debug("Symbol %d: %#llx %s", i,
|
||||
// this->SymTable[i].Address,
|
||||
// this->SymTable[i].FunctionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->Image = (void *)ImageAddress;
|
||||
debug("Solving symbols for address: %#llx", ImageAddress);
|
||||
#if defined(a64) || defined(aa64)
|
||||
Elf64_Ehdr *Header = (Elf64_Ehdr *)ImageAddress;
|
||||
#elif defined(a32)
|
||||
Elf32_Ehdr *Header = (Elf32_Ehdr *)ImageAddress;
|
||||
#endif
|
||||
if (Header->e_ident[0] != 0x7F &&
|
||||
Header->e_ident[1] != 'E' &&
|
||||
Header->e_ident[2] != 'L' &&
|
||||
Header->e_ident[3] != 'F')
|
||||
{
|
||||
error("Invalid ELF header");
|
||||
return;
|
||||
}
|
||||
#if defined(a64) || defined(aa64)
|
||||
Elf64_Shdr *ElfSections = (Elf64_Shdr *)(ImageAddress + Header->e_shoff);
|
||||
Elf64_Sym *ElfSymbols = nullptr;
|
||||
#elif defined(a32)
|
||||
Elf32_Shdr *ElfSections = (Elf32_Shdr *)(ImageAddress + Header->e_shoff);
|
||||
Elf32_Sym *ElfSymbols = nullptr;
|
||||
#endif
|
||||
char *strtab = nullptr;
|
||||
Symbols::Symbols(uintptr_t ImageAddress)
|
||||
{
|
||||
this->Image = (void *)ImageAddress;
|
||||
this->AppendSymbols(ImageAddress);
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < Header->e_shnum; i++)
|
||||
switch (ElfSections[i].sh_type)
|
||||
{
|
||||
case SHT_SYMTAB:
|
||||
#if defined(a64) || defined(aa64)
|
||||
ElfSymbols = (Elf64_Sym *)(ImageAddress + ElfSections[i].sh_offset);
|
||||
this->TotalEntries = ElfSections[i].sh_size / sizeof(Elf64_Sym);
|
||||
#elif defined(a32)
|
||||
ElfSymbols = (Elf32_Sym *)(ImageAddress + ElfSections[i].sh_offset);
|
||||
this->TotalEntries = ElfSections[i].sh_size / sizeof(Elf32_Sym);
|
||||
#endif
|
||||
if (this->TotalEntries >= 0x10000)
|
||||
this->TotalEntries = 0x10000 - 1;
|
||||
|
||||
debug("Symbol table found, %d entries", this->TotalEntries);
|
||||
break;
|
||||
case SHT_STRTAB:
|
||||
if (Header->e_shstrndx == i)
|
||||
{
|
||||
debug("String table found, %d entries", ElfSections[i].sh_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
strtab = (char *)(ImageAddress + ElfSections[i].sh_offset);
|
||||
debug("String table found, %d entries", ElfSections[i].sh_size);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (ElfSymbols != nullptr && strtab != nullptr)
|
||||
{
|
||||
int64_t Index, MinimumIndex;
|
||||
for (int64_t i = 0; i < this->TotalEntries - 1; i++)
|
||||
{
|
||||
MinimumIndex = i;
|
||||
for (Index = i + 1; Index < this->TotalEntries; Index++)
|
||||
if (ElfSymbols[Index].st_value < ElfSymbols[MinimumIndex].st_value)
|
||||
MinimumIndex = Index;
|
||||
#if defined(a64) || defined(aa64)
|
||||
Elf64_Sym tmp = ElfSymbols[MinimumIndex];
|
||||
#elif defined(a32)
|
||||
Elf32_Sym tmp = ElfSymbols[MinimumIndex];
|
||||
#endif
|
||||
ElfSymbols[MinimumIndex] = ElfSymbols[i];
|
||||
ElfSymbols[i] = tmp;
|
||||
}
|
||||
|
||||
while (ElfSymbols[0].st_value == 0)
|
||||
{
|
||||
ElfSymbols++;
|
||||
this->TotalEntries--;
|
||||
}
|
||||
|
||||
trace("Symbol table loaded, %d entries (%ldKB)", this->TotalEntries, TO_KB(this->TotalEntries * sizeof(SymbolTable)));
|
||||
for (int64_t i = 0, g = this->TotalEntries; i < g; i++)
|
||||
{
|
||||
this->SymTable[i].Address = ElfSymbols[i].st_value;
|
||||
this->SymTable[i].FunctionName = &strtab[ElfSymbols[i].st_name];
|
||||
|
||||
// debug("Symbol %d: %#llx %s", i, this->SymTable[i].Address, this->SymTable[i].FunctionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Symbols::~Symbols() {}
|
||||
Symbols::~Symbols() {}
|
||||
}
|
||||
|
Reference in New Issue
Block a user