#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 */
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);
static void refer(void);
static void stderr_out(void);
-void ioinit(char *progname)
+void ioinit(const char *progname)
{
infd = ERR;
if (*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)
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)
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)
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)
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;
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;
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)
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
}