Fix ranlib and linking problems, add a hello world program for testing
authorNick Downing <downing.nick@gmail.com>
Tue, 3 Jan 2017 06:15:36 +0000 (17:15 +1100)
committerNick Downing <downing.nick@gmail.com>
Tue, 3 Jan 2017 06:15:36 +0000 (17:15 +1100)
14 files changed:
.gitignore
bin/cc/Makefile
bin/cc/cc.c
bin/cc/n.sh
bin/ld/Makefile
bin/ld/ld.c
bin/ld/n.sh
include/ranlib.h
n.sh
test/file2mt.py [new file with mode: 0755]
test/hello.c [new file with mode: 0644]
test/mt2file.py [new file with mode: 0755]
test/n.sh [new file with mode: 0755]
usr.bin/ranlib/n.sh

index fd8e509..a690c4a 100644 (file)
@@ -46,6 +46,9 @@ lib/libc/stdlib/stdliblib_p
 lib/libc/string/stringlib
 lib/libc/string/stringlib_p
 stage
+test/hello
+test/hello.i
+test/hello.s
 usr.lib/libvmf/genlib
 usr.lib/libvmf/genlib_p
 usr.bin/lorder
index 13079ff..824e58c 100644 (file)
@@ -3,7 +3,7 @@
 #
 
 SEPFLAG=-i
-CPPFLAGS=-DDESTDIR=\"${DESTDIR}\" -DSTAGEDIR=\"${STAGEDIR}\"
+CPPFLAGS=-DCROSSDIR=\"${CROSSDIR}\" -DSTAGEDIR=\"${STAGEDIR}\"
 
 all: cc
 
index 650ab49..e3df6c7 100644 (file)
@@ -19,13 +19,13 @@ static      char sccsid[] = "@(#)cc.c 4.13 9/18/85";
 #include <unistd.h>
 #include "krcompat.h"
 
-char   *cpp = DESTDIR "/lib/cpp";
-char   *ccom = DESTDIR "/lib/c0";
-char   *ccom1 = DESTDIR "/lib/c1";
-char   *c2 = DESTDIR "/lib/c2";
-char   *as = DESTDIR "/bin/as";
-char   *ld = DESTDIR "/bin/ld";
-char   *crt0 = DESTDIR "/lib/crt0.o";
+char   *cpp = CROSSDIR "/lib/cpp";
+char   *ccom = CROSSDIR "/lib/c0";
+char   *ccom1 = CROSSDIR "/lib/c1";
+char   *c2 = CROSSDIR "/lib/c2";
+char   *as = CROSSDIR "/bin/as";
+char   *ld = CROSSDIR "/bin/ld";
+char   *crt0 = STAGEDIR "/lib/crt0.o";
 
 char   tmp0[30];               /* big enough for /tmp/ctm%05.5d */
 char   *tmp1, *tmp2, *tmp3, *tmp4, *tmp5;
index d3f8b59..b24cc21 100755 (executable)
@@ -2,4 +2,4 @@
 ROOT="`pwd |sed -e 's/\/bin\/cc$//'`"
 CFLAGS="-Wall -Wno-char-subscripts -Wno-deprecated-declarations -Wno-format -Wno-parentheses"
 mkdir --parents "$ROOT/cross/bin"
-make SEPFLAG= DESTDIR="$ROOT/cross" STAGEDIR="$ROOT/stage" CFLAGS="$CFLAGS" install
+make SEPFLAG= CROSSDIR="$ROOT/cross" STAGEDIR="$ROOT/stage" CFLAGS="$CFLAGS" DESTDIR="$ROOT/cross" install
index 51d7d99..a1dcdfc 100644 (file)
@@ -20,8 +20,7 @@ ld: ${OBJS}
 install: all
 #      install -c -s -o bin -g bin -m 755 ld ${DESTDIR}/bin/ld
 #      install -c -o bin -g bin -m 444 ${MAN} ${DESTDIR}/usr/man/cat1
-       install -c -m 755 ld ${DESTDIR}/bin/ld
-#-s FIX THIS
+       install -c -s -m 755 ld ${DESTDIR}/bin/ld
        install -c -m 444 ${MAN} ${DESTDIR}/usr/man/cat1
 
 lint:
index d5e6a4a..42c17fc 100644 (file)
@@ -697,11 +697,13 @@ void load1arg(cp, flag) register char *cp; int flag; {
                        (temp[2] & 0377L) |
                        ((temp[3] & 0377L) << 8);
 #endif
+ /*fprintf(stderr, "ltnum=%08lx\n", ltnum);*/
 /*
  * calculate string table position.  Add in the ranlib size (4 bytes),
  * the size of the ranlib structures (ltnum) from above.
 */
                nloc += (sizeof (ltnum) + ltnum);
+ /*fprintf(stderr, "nloc=%08lx\n", nloc);*/
                tnum = ltnum / sizeof (struct ranlib);
 
                rstrtab = NULL;
@@ -715,6 +717,7 @@ void load1arg(cp, flag) register char *cp; int flag; {
                        (temp[2] & 0377L) |
                        ((temp[3] & 0377L) << 8);
 #endif
+ /*fprintf(stderr, "strsize=%08lx\n", strsize);*/
                if      (strsize <= 8192L)
                        {
                        rstrtab = (char *)malloc((int)strsize);
@@ -788,6 +791,7 @@ int ldrand(totnum, sloc) int totnum; OFF_T sloc; {
        LIBLIST *oldp = libp;
        int amt, tnum = totnum;
        OFF_T   loc;
+ /*fprintf(stderr, "sloc=%08lx tnum=%d\n", sloc, tnum);*/
 /*
  * 'ar' header + member header + SYMDEF table.of.contents + long filename
 */
@@ -807,6 +811,7 @@ int ldrand(totnum, sloc) int totnum; OFF_T sloc; {
                        ntab = TABSZ;
                else
                        ntab = tnum;
+ /*fprintf(stderr, "ntab=%d opos=%08lx\n", ntab, opos);*/
                tplast = &tab[ntab - 1];
                (void)lseek(infil, opos, L_SET);
                amt = ntab * sizeof (struct ranlib);
@@ -849,6 +854,7 @@ int ldrand(totnum, sloc) int totnum; OFF_T sloc; {
                                        sizeof (OFF_T), 07777);
                                mgets(localname, NNAMESIZE, STRINGS2);
                                }
+ /*fprintf(stderr, "localname=%s\n", localname);*/
                        hp = slookup(localname);
                        if      (*hp == -1)
                                continue;
@@ -1829,41 +1835,38 @@ void mkfsym(s) char *s; {
        fwrite(&cursym, sizeof (cursym), 1, soutb);
 }
 
-/* THIS STUFF NEEDS TO BE REWRITTEN TO HANDLE BLOCK BOUNDARIES INTELLIGENTLY */
 void mget(loc, an, which) register int *loc; int an; int which; {
        register int n;
        register STREAM *tp = &Input[which];
-#ifndef pdp11
-       int temp;
-#endif
 
        n = an >> 1;
+#if 1
+       if ((tp->nsize -= n) < 0)
+               error(1, "premature EOF#1");
+       while (tp->nibuf < n) {
+               bcopy(tp->Iptr, loc, tp->nibuf * 2);
+               tp->Iptr += tp->nibuf;
+               loc += tp->nibuf;
+               n -= tp->nibuf;
+               dseek(which, (OFF_T)(tp->bno + 1) * tp->bsize, -1);
+       }
+       bcopy(tp->Iptr, loc, n * 2);
+       tp->Iptr += n;
+       tp->nibuf -= n;
+#else
        if ((tp->nibuf -= n) >= 0) {
                if ((tp->nsize -= n) > 0) {
-#ifdef pdp11
                        bcopy(tp->ptr, loc, an);
                        tp->Iptr += n;
-#else
-                       bcopy(tp->Cptr, loc, an);
-                       tp->Cptr += n << 1;
-#endif
                        return;
                }
                tp->nsize += n;
        }
        tp->nibuf += n;
-       do {
-#ifdef pdp11
-               *loc++ = get(which);
-#else
-               /* kludgey, as we are converting to host byte order in temp, */
-               /* then back to target byte order for caller to convert again */
-               /* (luckily it only occurs if request crosses block boundary) */
-               temp = get(which);
-               ((char *)loc)[0] = temp & 0377;
-               ((char *)loc++)[1] = (temp >> 8) & 0377;
+       do
+               *loc++ = get(which); /* no good for big-endian cross-compile */
+       while (--n);
 #endif
-       } while (--n);
 }
 
 #ifndef pdp11
@@ -2083,6 +2086,7 @@ void error(n, s) int n; char *s; {
 }
 
 void readhdr(loc) OFF_T loc; {
+ /*fprintf(stderr, "loc=%08lx\n", loc);*/
        dseek(TEXT, loc, sizeof filhdr);
 #ifdef pdp11
        mget((int *)&filhdr.e, sizeof filhdr.e, TEXT);
index 01e59ee..9b83c2f 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 ROOT="`pwd |sed -e 's/\/bin\/ld$//'`"
-CFLAGS="-g -Wall -Wno-char-subscripts -Wno-deprecated-declarations -Wno-format -Wno-parentheses"
+CFLAGS="-Wall -Wno-char-subscripts -Wno-deprecated-declarations -Wno-format -Wno-parentheses"
 mkdir --parents "$ROOT/cross/bin"
 mkdir --parents "$ROOT/cross/usr/man/cat1"
 make SEPFLAG= DESTDIR="$ROOT/cross" STAGEDIR="$ROOT/stage" CFLAGS="$CFLAGS" LDFLAGS="-L$ROOT/cross/usr/lib" install
index ffee9ef..d8ec2f4 100644 (file)
 struct ranlib {
        union {
                OFF_T ran_strx;         /* string table index */
+/* we do not support ran_name when cross compiling, it breaks on 64-bit hosts */
+#ifdef pdp11
                char *ran_name;         /* in memory symbol name */
+#endif
        } ran_un;
        OFF_T ran_off;                  /* archive file offset */
 };
diff --git a/n.sh b/n.sh
index 027f34a..82dd901 100755 (executable)
--- a/n.sh
+++ b/n.sh
@@ -14,7 +14,7 @@ mkdir --parents "$ROOT/stage/usr/lib"
 make clean
 make -C bin/ar SEPFLAG= DESTDIR="$ROOT/cross" CFLAGS="$CFLAGS" install
 make -C bin/as SEPFLAG= DESTDIR="$ROOT/cross" CFLAGS="$CFLAGS" install
-make -C bin/cc SEPFLAG= DESTDIR="$ROOT/cross" STAGEDIR="$ROOT/stage" CFLAGS="$CFLAGS" install
+make -C bin/cc SEPFLAG= CROSSDIR="$ROOT/cross" STAGEDIR="$ROOT/stage" CFLAGS="$CFLAGS" DESTDIR="$ROOT/cross" install
 make -C bin/ld SEPFLAG= DESTDIR="$ROOT/cross" STAGEDIR="$ROOT/stage" CFLAGS="$CFLAGS" LDFLAGS="-L$ROOT/cross/usr/lib" install
 make -C bin/nm SEPFLAG= DESTDIR="$ROOT/cross" CFLAGS="$CFLAGS" install
 make -C include SHARED=copies DESTDIR="$ROOT/stage" SYSDIR="$ROOT/sys" install
diff --git a/test/file2mt.py b/test/file2mt.py
new file mode 100755 (executable)
index 0000000..552fe3a
--- /dev/null
@@ -0,0 +1,16 @@
+#!/usr/bin/env python3
+
+import sys
+
+a = sys.stdin.buffer.read()
+i = 0
+while i < len(a):
+  l = min(0x400, len(a) - i)
+  sys.stderr.write('{0:08x} {1:08x} {2:08x}\n'.format(i, len(a), l))
+  sys.stdout.buffer.write(l.to_bytes(4, byteorder = 'little'))
+  sys.stdout.buffer.write(a[i:i + l])
+  i += l
+  sys.stdout.buffer.write(l.to_bytes(4, byteorder = 'little'))
+l = 0
+sys.stdout.buffer.write(l.to_bytes(4, byteorder = 'little'))
+sys.stdout.buffer.write(l.to_bytes(4, byteorder = 'little'))
diff --git a/test/hello.c b/test/hello.c
new file mode 100644 (file)
index 0000000..3b433f9
--- /dev/null
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main() {
+       printf("hello, world\n");
+       return 0;
+}
diff --git a/test/mt2file.py b/test/mt2file.py
new file mode 100755 (executable)
index 0000000..dbadeab
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+
+import sys
+
+a = sys.stdin.buffer.read()
+i = 0
+while i < len(a):
+  l = int.from_bytes(a[i:i + 4], byteorder = 'little')
+  sys.stderr.write('{0:08x} {1:08x} {2:08x}\n'.format(i, len(a), l))
+  i += 4
+  b = a[i:i + l]
+  sys.stdout.buffer.write(b)
+  i += l
+  assert l == int.from_bytes(a[i:i + 4], byteorder = 'little')
+  i += 4
+  if l == 0:
+    break
diff --git a/test/n.sh b/test/n.sh
new file mode 100755 (executable)
index 0000000..0e9df60
--- /dev/null
+++ b/test/n.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+../cross/bin/cc -E hello.c >hello.i
+../cross/bin/cc -S hello.c
+../cross/bin/cc -c hello.c
+../cross/bin/cc -o hello hello.o
index 9197bd7..7f46e52 100755 (executable)
@@ -1,5 +1,5 @@
 #!/bin/sh
 ROOT="`pwd |sed -e 's/\/usr\.bin\/ranlib$//'`"
-CFLAGS="-g -Wall -Wno-char-subscripts -Wno-deprecated-declarations -Wno-format -Wno-parentheses"
+CFLAGS="-Wall -Wno-char-subscripts -Wno-deprecated-declarations -Wno-format -Wno-parentheses"
 mkdir --parents "$ROOT/cross/usr/bin"
 make SEPFLAG= DESTDIR="$ROOT/cross" CFLAGS="$CFLAGS" install