From 5b01ae4b1e352a7f1b72a0d0cf4e0c3d1b53f244 Mon Sep 17 00:00:00 2001 From: Kevin Lange Date: Fri, 2 Dec 2016 12:53:38 +0900 Subject: [PATCH] Look up libraries in something resembling a search path --- linker.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/linker.c b/linker.c index c660b837..308ec5a7 100644 --- a/linker.c +++ b/linker.c @@ -5,6 +5,7 @@ #include #include #include +#include #define TRACE_APP_NAME "ld.so" @@ -53,9 +54,46 @@ typedef struct elf_object { } elf_t; +static char * find_lib(const char * file) { + + if (file[0] == '/') return strdup(file); + + char * path = getenv("LD_LIBRARY_PATH"); + if (!path) { + path = "/usr/lib:/lib"; + } + char * xpath = strdup(path); + int found = 0; + char * p, * tokens[10], * last; + int i = 0; + for ((p = strtok_r(xpath, ":", &last)); p; p = strtok_r(NULL, ":", &last)) { + int r; + struct stat stat_buf; + char * exe = malloc(strlen(p) + strlen(file) + 2); + strcpy(exe, p); + strcat(exe, "/"); + strcat(exe, file); + + r = stat(exe, &stat_buf); + if (r != 0) { + free(exe); + continue; + } + return exe; + } + free(xpath); + + return NULL; +} + static elf_t * open_object(const char * path) { - FILE * f = fopen(path, "r"); + const char * file = find_lib(path); + if (!file) return NULL; + + FILE * f = fopen(file, "r"); + + free(file); if (!f) { return NULL;