ld: more tidying
authorAlan Cox <alan@linux.intel.com>
Sun, 24 Jul 2016 20:51:36 +0000 (21:51 +0100)
committerAlan Cox <alan@linux.intel.com>
Sun, 24 Jul 2016 20:51:36 +0000 (21:51 +0100)
Applications/ld09/config.h
Applications/ld09/io.c
Applications/ld09/ld.c
Applications/ld09/linksyms.c
Applications/ld09/objdump86.c
Applications/ld09/table.c
Applications/ld09/type.h
Applications/ld09/writefuzix.c

index 9ab77f6..336bd34 100644 (file)
@@ -8,6 +8,8 @@
 #undef  I80386                 /* Intel 80386 */
 #define MC6809                 /* Motorola 6809 */
 
+#define MEM_SMALL              /* 16bit sbrk memory pool */
+
 /* one of these target operating systems must be defined */
 
 #define FUZIX                  /* FUZIX executable */
index 6363384..a024181 100644 (file)
@@ -8,18 +8,12 @@
 #include "globvar.h"
 #include "version.h"
 
-#define DRELBUFSIZE    3072
 #define ERR            (-1)
 #define ERRBUFSIZE     1024
 #define INBUFSIZE      1024
 #define OUTBUFSIZE     2048
 #define TRELBUFSIZE    1024
 
-#ifdef REL_OUTPUT
-static char *drelbuf;          /* extra output buffer for data relocations */
-static char *drelbufptr;       /* data relocation output buffer ptr */
-static char *drelbuftop;       /* data relocation output buffer top */
-#endif
 static char *errbuf;           /* error buffer (actually uses STDOUT) */
 static char *errbufptr;        /* error buffer ptr */
 static char *errbuftop;        /* error buffer top */
@@ -36,19 +30,10 @@ static int outfd;           /* output file descriptor */
 static mode_t outputperms;     /* permissions of output file */
 static char *outputname;       /* name of output file */
 static const char *refname;    /* name of program for error reference */
-#ifdef REL_OUTPUT
-static char *trelbuf;          /* extra output buffer for text relocations */
-static char *trelbufptr;       /* text relocation output buffer ptr */
-static char *trelbuftop;       /* text relocation output buffer top */
-static int trelfd;             /* text relocation output file descriptor */
-#endif
 static unsigned warncount;     /* count of warnings */
 
 static void errexit(char *message);
 static void flushout(void);
-#ifdef REL_OUTPUT
-static void flushtrel(void);
-#endif
 static void outhexdigs(bin_off_t num);
 static void outputerror(char *message);
 static void put04x(unsigned num);
@@ -56,7 +41,7 @@ static void putstrn(const char *message);
 static void refer(void);
 static void stderr_out(void);
 
-void ioinit(char *progname)
+void ioinit(const char *progname)
 {
     infd = ERR;
     if (*progname)
@@ -68,21 +53,17 @@ void ioinit(char *progname)
           refname=progname+1;
 
     /* FIXME: we need a malloc with error check here */
-#ifdef REL_OUTPUT
-    drelbuf = malloc(DRELBUFSIZE);
-    drelbuftop = drelbuf + DRELBUFSIZE;
-#endif
     errbuf = malloc(ERRBUFSIZE);
     errbufptr = errbuf;
     errbuftop = errbuf + ERRBUFSIZE;
     inbuf = malloc(INBUFSIZE);
     outbuf = malloc(OUTBUFSIZE);/* outbuf invalid if this fails but then */
                                /* will not be used - tableinit() aborts */
+    if (errbuf == NULL || inbuf == NULL || outbuf == NULL) {
+       outofmemory();
+       exit(1);
+    }
     outbuftop = outbuf + OUTBUFSIZE;
-#ifdef REL_OUTPUT
-    trelbuf = malloc(TRELBUFSIZE);
-    trelbuftop = trelbuf + TRELBUFSIZE;
-#endif
 }
 
 void closein(void)
@@ -94,23 +75,9 @@ void closein(void)
 
 void closeout()
 {
-#ifdef REL_OUTPUT
-    unsigned nbytes;
-#endif
-
     flushout();
-#ifdef REL_OUTPUT
-    flushtrel();
-    nbytes = drelbufptr - drelbuf;
-    if (write(trelfd, drelbuf, nbytes) != nbytes)
-       outputerror("cannot write");
-#endif
     if (close(outfd) == ERR)
        outputerror("cannot close");
-#ifdef REL_OUTPUT
-    if (close(trelfd) == ERR)
-       outputerror("cannot close");
-#endif
 }
 
 void errtrace(char *name, int level)
@@ -154,34 +121,13 @@ static void flushout(void)
     outbufptr = outbuf;
 }
 
-#ifdef REL_OUTPUT
-static void flushtrel(void)
-{
-    unsigned nbytes;
-
-    nbytes = trelbufptr - trelbuf;
-    if (write(trelfd, trelbuf, nbytes) != nbytes)
-       outputerror("cannot write");
-    trelbufptr = trelbuf;
-}
-#endif
-
 void openin(char *filename)
 {
-#if 0 /* XXX - this probably won't work with constructed lib names? */
-    if (infd == ERR || strcmp(inputname, filename) != 0)
-#endif
-    {
-       closein();
-       inputname = filename;   /* this relies on filename being static */
-#ifdef O_BINARY
-       if ((infd = open(filename, O_BINARY|O_RDONLY)) < 0)
-#else
-       if ((infd = open(filename, O_RDONLY)) < 0)
-#endif
-           inputerror("cannot open");
-       inbufptr = inbufend = inbuf;
-    }
+    closein();
+    inputname = filename;      /* this relies on filename being static */
+    if ((infd = open(filename, O_RDONLY)) < 0)
+       inputerror("cannot open");
+    inbufptr = inbufend = inbuf;
 }
 
 void openout(char *filename)
@@ -189,31 +135,14 @@ void openout(char *filename)
     mode_t oldmask;
 
     outputname = filename;
-#ifdef O_BINARY
-    if ((outfd = open(filename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, CREAT_PERMS)) == ERR)
-#else
     if ((outfd = creat(filename, CREAT_PERMS)) == ERR)
-#endif
        outputerror("cannot open");
 
-    /* Can't do this on MSDOS; it upsets share.exe */
     oldmask = umask(0); umask(oldmask);
     outputperms = ((CREAT_PERMS | EXEC_PERMS) & ~oldmask);
     chmod(filename, outputperms & ~EXEC_PERMS);
 
-#ifdef REL_OUTPUT
-    drelbufptr = drelbuf;
-#endif
     outbufptr = outbuf;
-#ifdef REL_OUTPUT
-#ifdef O_BINARY
-    if ((trelfd = open(filename, O_BINARY|O_WRONLY, CREAT_PERMS)) == ERR)
-#else
-    if ((trelfd = open(filename, O_WRONLY, CREAT_PERMS)) == ERR)
-#endif
-       outputerror("cannot reopen");
-    trelbufptr = trelbuf;
-#endif
 }
 
 static void outhexdigs(bin_off_t num)
@@ -351,15 +280,6 @@ void seekout(unsigned long offset)
        outputerror("cannot seek in");
 }
 
-#ifdef REL_OUTPUT
-void seektrel(unsigned long offset)
-{
-    flushtrel();
-    if (lseek(trelfd, (off_t) offset, SEEK_SET) != offset)
-       outputerror("cannot seek in");
-}
-#endif
-
 void writechar(int ch)
 {
     register char *obuf;
@@ -374,22 +294,6 @@ void writechar(int ch)
     outbufptr = obuf;
 }
 
-#ifdef REL_OUTPUT
-void writedrel(char *buf, unsigned count)
-{
-    register char *rbuf;
-
-    rbuf = drelbufptr;
-    while (count--)
-    {
-       if (rbuf >= drelbuftop)
-           inputerror("data relocation buffer full while processing");
-       *rbuf++ = *buf++;
-    }
-    drelbufptr = rbuf;
-}
-#endif
-
 void writeout(char *buf, unsigned count)
 {
     register char *obuf;
@@ -408,26 +312,6 @@ void writeout(char *buf, unsigned count)
     outbufptr = obuf;
 }
 
-#ifdef REL_OUTPUT
-void writetrel(char *buf, unsigned count)
-{
-    register char *rbuf;
-
-    rbuf = trelbufptr;
-    while (count--)
-    {
-       if (rbuf >= trelbuftop)
-       {
-           trelbufptr = rbuf;
-           flushtrel();
-           rbuf = trelbufptr;
-       }
-       *rbuf++ = *buf++;
-    }
-    trelbufptr = rbuf;
-}
-#endif
-
 /* error module */
 
 static void errexit(char *message)
@@ -559,28 +443,23 @@ void undefined(char *name)
 
 void usage(void)
 {
+    /* FIXME: a lot of these don't apply to all platforms */
     stderr_out();
     putstr("usage: ");
     putstr(refname);
-#ifdef REL_OUTPUT
-    errexit("\
- [-03NMdimrstz[-]] [-llib_extension] [-o outfile] [-Ccrtfile]\n\
-       [-Llibdir] [-Olibfile] [-Ttextaddr] [-Ddataaddr] [-Hheapsize] infile...");
-#else
     errexit("\
  [-03NMdimstz[-]] [-llib_extension] [-o outfile] [-Ccrtfile]\n\
        [-Llibdir] [-Olibfile] [-Ttextaddr] [-Ddataaddr] [-Hheapsize] infile...");
-#endif
 }
 
 void version_msg(void)
 {
     stderr_out();
 #ifdef VERSION
-    putstr("ld86 version: ");
+    putstr("ld version: ");
     errexit(VERSION);
 #else
-    errexit("ld86 version unknown");
+    errexit("ld version unknown");
 #endif
 }
 
index c5b8c64..3f99fac 100644 (file)
@@ -8,6 +8,7 @@
 #include "type.h"
 #include "globvar.h"
 
+/* FIXME: probably want more ?? */
 #define MAX_LIBS       5
 
 bin_off_t text_base_value = 0; /* XXX */
@@ -88,9 +89,6 @@ int main(int argc, char **argv)
            case 'r':           /* relocatable output */
            case 't':           /* trace modules linked */
               if (icount > 0) usage();
-#ifdef REL_OUTPUT
-           case 'B':           /* Broken -r for dosemu. */
-#endif
 #ifdef HAS_32BIT
            case '0':           /* use 16-bit libraries */
            case '3':           /* use 32-bit libraries */
@@ -171,14 +169,6 @@ int main(int argc, char **argv)
     }
     if(icount==0) usage();
 
-#ifdef REL_OUTPUT
-    if( flag['r'] && !flag['N'] )
-    {
-       /* Do a relocatable link -- actually fake it with 'ar.c' */
-       ld86r(argc, argv);
-    }
-#endif
-
     /* Headerless executables can't use symbols. */
     headerless = flag['d'];
     if( headerless ) flag['s'] = 1;
index 1a19622..fdf3748 100644 (file)
@@ -22,17 +22,6 @@ void linksyms(bool_pt argreloc_output)
     struct modstruct *modptr;
     struct symstruct *symptr;
 
-#ifdef REL_OUTPUT
-    reloc_output = argreloc_output;
-    if (argreloc_output)
-    {
-       if (modfirst->modnext != NUL_PTR)
-           fatalerror("relocatable output only works for one input file");
-       for (modptr = modfirst; modptr != NUL_PTR; modptr = modptr->modnext)
-           modptr->loadflag = TRUE;
-       return;
-    }
-#endif
     if ((symptr = findsym("_start")) != NUL_PTR ||
         (symptr = findsym("_main")) != NUL_PTR)
        entrysym(symptr);
@@ -80,4 +69,3 @@ static void linkrefs(struct modstruct *modptr)
        if (symptr->modptr->loadflag == FALSE)
            linkrefs(symptr->modptr);
 }
-
index f73d820..7cac4dc 100644 (file)
@@ -216,6 +216,7 @@ void do_module(char *fname, char *archive)
 
         if( read_syms() < 0 ) break;
 
+        /* FIXME: overflow */
         strtab = malloc((unsigned int)str_len+1);
         if( strtab == 0 ) { error("Out of memory"); break; }
         str_off = ftell(ifd);
index 0508da1..dc7304c 100644 (file)
@@ -28,8 +28,16 @@ void syminit(void)
 {
     unsigned i;
 
-    for (i = sizeof(int) <= 2 ? 0xE000 : (unsigned) 0x38000;
-        i != 0; i -= 512)
+    /* FIXME: use different methods for different system sizes */
+#ifdef MEM_SMALL
+    i = (unsigned char *)&i - (unsigned char *)sbrk(0) - 512;
+    if (i >= 0xFDFF)
+        outofmemory();
+    i &= 0xFE00;
+#else
+    i = sizeof(int) <= 2 ? 0xE000 : (unsigned) 0x38000;
+#endif
+    for (; i != 0; i -= 512)
        if ((tableptr = malloc(i)) != NUL_PTR)
            break;
     if (tableptr == NUL_PTR)
index de7bbf6..12663bd 100644 (file)
@@ -76,7 +76,7 @@ void dumpmods(void);
 void dumpsyms(void);
 
 /* io.c */
-void ioinit(char *progname);
+void ioinit(const char *progname);
 void closein(void);
 void closeout(void);
 void errtrace(char *name, int level);
index c842bd6..aa73428 100644 (file)
@@ -72,7 +72,6 @@ static bin_off_t edataoffset; /* end of data */
 static bin_off_t endoffset;    /* end of bss */
 static bin_off_t etextoffset;  /* end of text */
 static bin_off_t etextpadoff;  /* end of padded text */
-static unsigned nsym;          /* number of symbols written */
 static unsigned relocsize;     /* current relocation size 1, 2 or 4 */
 static bin_off_t segadj[NSEG]; /* adjusts (file offset - seg offset) */
                                /* depends on zero init */
@@ -221,6 +220,7 @@ void write_fuzix(char *outfilename, bool_pt argsepid, bool_pt argbits32,
 #endif
     {
        segpos[seg] = segbase[seg] = combase[seg - 1] + comsz[seg - 1];
+        /* Do we want this for IY relative tricks on some builds on Z80 ? */
 #if defined(MC6809) || defined(MC6502)
        if (seg == DPSEG)
        {