Fix elf interpreter

This commit is contained in:
Alex 2023-04-20 08:07:39 +03:00
parent c20aae9504
commit baacbbdbd7
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
4 changed files with 41 additions and 41 deletions

View File

@ -54,7 +54,10 @@
{
"name": "Fennix x32 (Linux, GCC, debug)",
"includePath": [
"${workspaceFolder}/include/**"
"${workspaceFolder}/libc/include/**",
"${workspaceFolder}/out/system/include/**",
"${workspaceFolder}/libs/include/**",
"${workspaceFolder}/libs/include"
],
"defines": [
"__debug_vscode__",

View File

@ -20,26 +20,26 @@ int main(int argc, char *argv[], char *envp[])
for (int i = 0; i < 14; i++)
PutCharToKernelConsole("\nHello World!\n"[i]);
print("%p %p %p\n", (void *)(uint64_t)&argc, (void *)&argv, (void *)&envp);
// print("I have %d arguments\n", argc);
// for (int i = 0; i < argc; i++)
// print("argv[%d] = (%p) %s\n", i, argv[i], argv[i]);
print("I have %d arguments\n", argc);
for (int i = 0; i < argc; i++)
print("argv[%d] = (%p) %s\n", i, argv[i], argv[i]);
// int envc = 0;
// while (envp[envc] != NULL)
// envc++;
int envc = 0;
while (envp[envc] != NULL)
envc++;
// print("I have %d environment variables\n", envc);
// for (int i = 0; i < envc; i++)
// print("envp[%d] = (%p) %s\n", i, envp[i], envp[i]);
print("I have %d environment variables\n", envc);
for (int i = 0; i < envc; i++)
print("envp[%d] = (%p) %s\n", i, envp[i], envp[i]);
// Elf64_auxv_t *auxv;
// char **e = envp;
Elf64_auxv_t *auxv;
char **e = envp;
// while (*e++ != NULL)
// ;
while (*e++ != NULL)
;
// for (auxv = (Elf64_auxv_t *)e; auxv->a_type != AT_NULL; auxv++)
// print("auxv: %ld %#lx\n", auxv->a_type, auxv->a_un.a_val);
for (auxv = (Elf64_auxv_t *)e; auxv->a_type != AT_NULL; auxv++)
print("auxv: %ld %#lx\n", auxv->a_type, auxv->a_un.a_val);
File *test = FileOpen("/Test.txt", FILE_READ);
if (test == NULL)

View File

@ -246,16 +246,16 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibAddressCollection *Info, long RelIndex))(
if (Info)
{
struct LibAddressCollection *CurLib = Info;
PrintNL("_______");
// PrintNL("_______");
// The last entry is the null entry (Valid == false) which determines the end of the list.
while (CurLib->Valid)
{
Print("-- ");
Print(CurLib->Name);
Print(" ");
ltoa(RelIndex, DbgBuff, 10);
Print(DbgBuff);
PrintNL(" --");
// Print("-- ");
// Print(CurLib->Name);
// Print(" ");
// ltoa(RelIndex, DbgBuff, 10);
// Print(DbgBuff);
// PrintNL(" --");
uintptr_t lib_BaseAddress = __UINTPTR_MAX__;
uintptr_t app_BaseAddress = __UINTPTR_MAX__;
@ -335,7 +335,7 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibAddressCollection *Info, long RelIndex))(
{
case R_X86_64_NONE:
{
PrintNL("R_X86_64_NONE");
// PrintNL("R_X86_64_NONE");
if (*GOTEntry == 0)
{
PrintNL("GOTEntry is 0");
@ -346,34 +346,34 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibAddressCollection *Info, long RelIndex))(
}
case R_X86_64_JUMP_SLOT:
{
PrintNL("R_X86_64_JUMP_SLOT");
// PrintNL("R_X86_64_JUMP_SLOT");
int SymIndex = ELF64_R_SYM(Rel->r_info);
Elf64_Sym *Sym = _app_SymTab + SymIndex;
if (Sym->st_name)
{
char *SymName = app_DynStr + Sym->st_name;
Print("SymName: ");
PrintNL(SymName);
// Print("SymName: ");
// PrintNL(SymName);
Elf64_Sym *LibSym = ELFLookupSymbol(CurLib->ElfFile, SymName);
Print("LibSym: 0x");
ltoa((long)LibSym, DbgBuff, 16);
PrintNL(DbgBuff);
// Print("LibSym: 0x");
// ltoa((long)LibSym, DbgBuff, 16);
// PrintNL(DbgBuff);
if (LibSym)
{
*GOTEntry = (Elf64_Addr)(CurLib->MemoryImage + LibSym->st_value);
ltoa(*GOTEntry, DbgBuff, 16);
Print("*GOTEntry: 0x");
PrintNL(DbgBuff);
// ltoa(*GOTEntry, DbgBuff, 16);
// Print("*GOTEntry: 0x");
// PrintNL(DbgBuff);
Lock = 0;
return (void (*)()) * GOTEntry;
}
PrintNL("Not found in lib");
// PrintNL("Not found in lib");
}
break;
}
@ -387,7 +387,7 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibAddressCollection *Info, long RelIndex))(
}
RetryNextLib:
PrintNL("Retrying next lib");
// PrintNL("Retrying next lib");
CurLib = CurLib->Next;
}
}
@ -508,8 +508,8 @@ int ld_load(int argc, char *argv[], char *envp[])
CurrentLibsForLazyResolver->Valid = true;
CurrentLibsForLazyResolver->ElfFile = (uintptr_t)lib_addr;
CurrentLibsForLazyResolver->MemoryImage = (uintptr_t)lib_mm_image;
LibsForLazyResolver->ParentElfFile = (uintptr_t)IPCBuffer->ElfFile;
LibsForLazyResolver->ParentMemoryImage = (uintptr_t)IPCBuffer->MemoryImage;
CurrentLibsForLazyResolver->ParentElfFile = (uintptr_t)IPCBuffer->ElfFile;
CurrentLibsForLazyResolver->ParentMemoryImage = (uintptr_t)IPCBuffer->MemoryImage;
for (size_t j = 0; j < 32; j++)
CurrentLibsForLazyResolver->Name[j] = IPCBuffer->Libraries[i].Name[j];

View File

@ -1,6 +1,5 @@
/* This function is a mess and needs to be cleaned up. */
/*
bool ELFDynamicReallocation(void *ElfFile, void *MemoryImage)
{
debug("ELF dynamic reallocation for image at %#lx.", ElfFile);
@ -177,9 +176,7 @@ bool ELFDynamicReallocation(void *ElfFile, void *MemoryImage)
}
return true;
}
*/
/*
ELFDynamicReallocation(ElfFile, MemoryImage);
@ -250,4 +247,4 @@ bool ELFDynamicReallocation(void *ElfFile, void *MemoryImage)
}
ELFAddLazyResolverToGOT(ElfFile, MemoryImage, LibsForLazyResolver);
*/