ref: 2ceb6eb753269256d560b2af3d5d1cb042723ea2
parent: 47571de9dfaf45169a8bc2b89c984819e3a98649
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed May 9 15:05:57 EDT 2018
[ld] Add basic options parsing In this version we force to have all the options before any file name. -l options are considered file names as they are shorthands for libx names.
--- a/inc/syslibs.def.h
+++ b/inc/syslibs.def.h
@@ -1,4 +1,7 @@
-char *syslibs[] = {
+
+#define MAX_LIB_PATHS 32
+
+char *syslibs[MAX_LIB_PATHS + 1] = {
PREFIX "/lib/scc/" ,
/* configure below your standard sys include paths */
PREFIX "/lib/",
--- a/ld/Makefile
+++ b/ld/Makefile
@@ -28,8 +28,8 @@
coff32.c: ld.h
formats.c: ld.h
main.c: ../inc/ar.h
-main.c: ../inc/arg.h
main.c: ../inc/scc.h
+main.c: ../inc/syslibs.h
main.c: ld.h
obj.c: ../inc/scc.h
obj.c: ld.h
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -1,5 +1,6 @@
typedef struct obj Obj;
+typedef struct symbol Symbol;
struct obj {
char *fname;
@@ -8,6 +9,10 @@
struct obj *next;
};
+struct symbol {
+ char *name;
+};
+
#ifdef stdin
struct objfile {
int (*probe)(char *fname, char *member, FILE *fp);
@@ -18,3 +23,4 @@
/* obj.c */
extern Obj *newobj(char *fname);
+extern Symbol *lookup(char *name);
--- a/ld/main.c
+++ b/ld/main.c
@@ -7,13 +7,15 @@
#include <stdlib.h>
#include <string.h>
-#include "../inc/arg.h"
#include "../inc/scc.h"
#include "../inc/ar.h"
+#include "../inc/syslibs.h"
#include "ld.h"
char *argv0;
+char *output = "a.out", *entry, *datasiz;
int pass;
+int sflag, xflag, Xflag, rflag, dflag;
static int
object(char *fname, char *member, FILE *fp)
@@ -148,36 +150,95 @@
static void
usage(void)
{
- fputs("usage: ld [options] [@file] file ...\n", stderr);
+ fputs("usage: ld [options] file ...\n", stderr);
exit(1);
}
+static void
+Lpath(char *path)
+{
+ char **bp;
+
+ for (bp = syslibs; bp < &syslibs[MAX_LIB_PATHS] && *bp; ++bp)
+ ;
+ if (bp == &syslibs[MAX_LIB_PATHS]) {
+ fputs("ld: too many -L options\n", stderr);
+ exit(1);
+ }
+ *bp = path;
+}
+
int
main(int argc, char *argv[])
{
- unsigned i;
+ char *cp, **p;
- ARGBEGIN {
- case 's':
- case 'u':
- case 'l':
- case 'x':
- case 'X':
- case 'r':
- case 'd':
- case 'n':
- case 'i':
- case 'o':
- case 'e':
- case 'O':
- case 'D':
- break;
- default:
- usage();
- } ARGEND
+ for (--argc; *++argv; --argc) {
+ if (argv[0][0] != '-' || argv[0][1] == 'l')
+ break;
+ if (argv[0][1] == '-') {
+ --argc, ++argv;
+ break;
+ |
+ for (cp = &argv[0][1]; *cp; ++cp) {
+ switch (*cp) {
+ case 's':
+ sflag = 1;
+ break;
+ case 'x':
+ xflag = 1;
+ break;
+ case 'X':
+ Xflag = 1;
+ break;
+ case 'r':
+ rflag = 1;
+ break;
+ case 'd':
+ dflag = 1;
+ break;
+ case 'i':
+ case 'n':
+ /* TODO */
+ break;
+ case 'L':
+ if (argc == 0)
+ goto usage;
+ ++argv, --argc;
+ Lpath(*argv);
+ break;
+ case 'u':
+ if (argc == 0)
+ goto usage;
+ ++argv, --argc;
+ lookup(*argv);
+ break;
+ case 'o':
+ if (argc == 0)
+ goto usage;
+ ++argv, --argc;
+ output = *argv;
+ break;
+ case 'e':
+ if (argc == 0)
+ goto usage;
+ ++argv, --argc;
+ entry = *argv;
+ break;
+ case 'D':
+ if (argc == 0)
+ goto usage;
+ ++argv, --argc;
+ datasiz = *argv;
+ break;
+ default:
+ usage:
+ usage();
+ }
+ }
+ }
-
- if (argc == 0)
+ if (argc < 0)
usage();
pass1(argc, argv);
--- a/ld/obj.c
+++ b/ld/obj.c
@@ -31,3 +31,9 @@
return obj;
}
+
+Symbol *
+lookup(char *name)
+{
+ return NULL;
+}