fix(kernel): add LD_LIBRARY_PATH

Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
EnderIce2 2025-03-26 02:33:03 +00:00
parent 92fe4bdd81
commit 4e9d25143e
Signed by: enderice2
GPG Key ID: FEB6B8A8507BA62E
2 changed files with 57 additions and 6 deletions

View File

@ -33,8 +33,9 @@
cold int SpawnInit()
{
const char *envp[6] = {
const char *envp[7] = {
"PATH=/bin:/usr/bin",
"LD_LIBRARY_PATH=/sys/lib:/usr/lib",
"TERM=tty",
"HOME=/root",
"USER=root",

View File

@ -22,6 +22,8 @@
#include <stddef.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
@ -359,15 +361,63 @@ void ProcessNeededLibraries(Elf_Dyn *elem, ElfInfo *Info)
ElfInfo *info = NULL;
char fullLibPath[PATH_MAX];
strcpy(fullLibPath, "/lib/");
strcat(fullLibPath, libPath);
/* TODO: more checks and also check environment variables */
if (sysdep(Access)(fullLibPath, F_OK) != 0)
int found = 0;
char *ldLibPath = getenv("LD_LIBRARY_PATH");
if (ldLibPath)
{
printf("dl: Can't access %s\n", fullLibPath);
char *pathCopy = strdup(ldLibPath);
char *path = strtok(pathCopy, ":");
while (path)
{
strcpy(fullLibPath, path);
if (fullLibPath[strlen(fullLibPath) - 1] != '/')
strcat(fullLibPath, "/");
strcat(fullLibPath, libPath);
if (sysdep(Access)(fullLibPath, F_OK) == 0)
{
found = 1;
break;
}
path = strtok(NULL, ":");
}
free(pathCopy);
if (found)
goto load_lib;
}
const char *standardPaths[] = {
"/sys/lib/",
"/usr/lib/",
"/lib/",
"/usr/local/lib/",
"/usr/local/lib64/",
"/usr/lib64/",
"/lib64/"};
for (size_t i = 0; i < sizeof(standardPaths) / sizeof(standardPaths[0]); i++)
{
strcpy(fullLibPath, standardPaths[i]);
strcat(fullLibPath, libPath);
if (sysdep(Access)(fullLibPath, F_OK) == 0)
{
found = 1;
break;
}
}
if (!found)
{
printf("dl: Library %s not found in search paths\n", libPath);
return;
}
load_lib:
int fd = sysdep(Open)(fullLibPath, O_RDONLY, 0644);
int status = LoadElf(fd, fullLibPath, &info);
elem->d_un.d_ptr = (uintptr_t)info; /* if LoadElf fails, info will still be NULL */