ref: 7ae4cdf7ae71be76936061c036d07e7e34545ec4
parent: 42da25d1f1b2108c6fb982129e8a749a5f5d2000
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Feb 10 09:06:49 EST 2019
[ld] Search libraries in -L path
--- a/src/cmd/ld/main.c
+++ b/src/cmd/ld/main.c
@@ -270,12 +270,13 @@
static FILE *
openfile(char *name, char *buffer)
{
- size_t len;
+ size_t pathlen, len;
FILE *fp;
+ char **bp, **base, **end;
+ char libname[FILENAME_MAX];
filename = name;
membname = NULL;
-
if (name[0] != '-' || name[1] != 'l') {
if ((fp = fopen(name, "rb")) == NULL)
error(errstr());
@@ -282,16 +283,33 @@
return fp;
}
- len = strlen(name+2);
- if (len > FILENAME_MAX-4) {
+ len = strlen(name+2) + 3;
+ if (len > FILENAME_MAX-1) {
error("library name too long");
return NULL;
}
-
strcat(strcpy(buffer, "lib"), name+2);
+
filename = buffer;
+ if ((fp = fopen(libname, "rb")) != NULL)
+ return fp;
- /* TODO: search the library now */
+ base = syslibs;
+ end = &syslibs[MAX_LIB_PATHS];
+ for (bp = base; bp < end && *bp; ++bp) {
+ pathlen = strlen(*bp);
+ if (pathlen + len > FILENAME_MAX-1)
+ continue;
+ memcpy(libname, *bp, pathlen);
+ memcpy(libname+pathlen+1, buffer, len);
+ buffer[pathlen] = '/';
+
+ if ((fp = fopen(buffer, "rb")) != NULL)
+ return fp;
+ }
+
+ error("not found");
+ return NULL;
}
static void