From baacbbdbd789b989d97d56bae1a3ede99abbcbae Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 20 Apr 2023 08:07:39 +0300 Subject: [PATCH] Fix elf interpreter --- .vscode/c_cpp_properties.json | 5 +++- apps/system/init/init.c | 30 +++++++++++------------ libc/ElfInterpreter/ld.c | 42 ++++++++++++++++----------------- libc/ElfInterpreter/unused_code | 5 +--- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 6aba97b..3957d6c 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -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__", diff --git a/apps/system/init/init.c b/apps/system/init/init.c index fe85701..038ae65 100644 --- a/apps/system/init/init.c +++ b/apps/system/init/init.c @@ -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) diff --git a/libc/ElfInterpreter/ld.c b/libc/ElfInterpreter/ld.c index 57b6719..b7224e1 100644 --- a/libc/ElfInterpreter/ld.c +++ b/libc/ElfInterpreter/ld.c @@ -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]; diff --git a/libc/ElfInterpreter/unused_code b/libc/ElfInterpreter/unused_code index 5001d96..6e995e4 100644 --- a/libc/ElfInterpreter/unused_code +++ b/libc/ElfInterpreter/unused_code @@ -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); -*/ +