#include "l.h"#include <ar.h> #ifndef DEFAULT#define DEFAULT '9'#endif char *noname = "<none>";char symname[] = SYMDEF;char thechar = '6';char *thestring = "960"; /* * -H0 -T0x40004C -D0x10000000 is garbage unix * -H1 -T0xd0 -R4 is unix coff * -H2 -T4128 -R4096 is plan9 format * -H3 -Tx -Rx is msdos boot * -H4 -T0x8000 -R4 is Intel 960 coff */ voidmain(int argc, char *argv[]){ int i, c; char *a; Binit(&bso, 1, OWRITE); cout = -1; listinit(); memset(debug, 0, sizeof(debug)); nerrors = 0; outfile = "6.out"; HEADTYPE = -1; INITTEXT = -1; INITDAT = -1; INITRND = -1; INITENTRY = 0; ARGBEGIN { default: c = ARGC(); if(c >= 0 && c < sizeof(debug)) debug[c]++; break; case 'o': /* output to (next arg) */ outfile = ARGF(); break; case 'E': a = ARGF(); if(a) INITENTRY = a; break; case 'H': a = ARGF(); if(a) HEADTYPE = atolwhex(a); break; case 'T': a = ARGF(); if(a) INITTEXT = atolwhex(a); break; case 'D': a = ARGF(); if(a) INITDAT = atolwhex(a); break; case 'R': a = ARGF(); if(a) INITRND = atolwhex(a); break; } ARGEND USED(argc); if(*argv == 0) { diag("usage: 6l [-options] objects\n"); errorexit(); } if(!debug['9'] && !debug['U'] && !debug['B']) debug[DEFAULT] = 1; if(HEADTYPE == -1) { if(debug['U']) HEADTYPE = 1; if(debug['B']) HEADTYPE = 2; if(debug['9']) HEADTYPE = 2; } switch(HEADTYPE) { default: diag("unknown -H option"); errorexit(); case 0: /* this is garbage */ HEADR = 20L+56L; if(INITTEXT == -1) INITTEXT = 0x40004CL; if(INITDAT == -1) INITDAT = 0x10000000L; if(INITRND == -1) INITRND = 0; break; case 1: /* is unix coff */ HEADR = 0xd0L; if(INITTEXT == -1) INITTEXT = 0xd0; if(INITDAT == -1) INITDAT = 0x400000; if(INITRND == -1) INITRND = 0; break; case 2: /* plan 9 */ HEADR = 32L; if(INITTEXT == -1) INITTEXT = 4096+32; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4096; break; case 3: /* msdos boot */ HEADR = 0; if(INITTEXT == -1) INITTEXT = 0x0100; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4; break; case 4: /* is 960 coff */ HEADR = 0x8CL; if(INITTEXT == -1) INITTEXT = 0x8000; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4; break; } if(INITDAT != 0 && INITRND != 0) print("warning: -D0x%lux is ignored because of -R0x%lux\n", INITDAT, INITRND); if(debug['v']) Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n", HEADTYPE, INITTEXT, INITDAT, INITRND); Bflush(&bso); for(i=1; optab[i].as; i++) if(i != optab[i].as) { diag("phase error in optab: %d\n", i); errorexit(); } maxop = i; for(i=0; i<Ymax; i++) ycover[i*Ymax + i] = 1; ycover[Yi5*Ymax + Yri5] = 1; ycover[Yr*Ymax + Yri5] = 1; ycover[Yi5*Ymax + Ynri5] = 1; ycover[Yr*Ymax + Ynri5] = 1; ycover[Ynone*Ymax + Ynri5] = 1; ycover[Yi5*Ymax + Yi32] = 1; zprg.link = P; zprg.cond = P; zprg.back = 2; zprg.as = AGOK; zprg.type = D_NONE; zprg.offset = 0; zprg.from.type = D_NONE; zprg.from.index = D_NONE; zprg.from.scale = 1; zprg.to = zprg.from; pcstr = "%.6lux "; nuxiinit(); histgen = 0; textp = P; datap = P; edatap = P; pc = 0; cout = create(outfile, 1, 0775); if(cout < 0) { diag("cannot create %s\n", outfile); errorexit(); } version = 0; cbp = buf.cbuf; cbc = sizeof(buf.cbuf); firstp = prg(); lastp = firstp; if(INITENTRY == 0) { INITENTRY = "_main"; if(debug['p']) INITENTRY = "_mainp"; if(!debug['l']) lookup(INITENTRY, 0)->type = SXREF; } else lookup(INITENTRY, 0)->type = SXREF; while(*argv) objfile(*argv++); if(!debug['l']) loadlib(0, libraryp); firstp = firstp->link; if(firstp == P) errorexit(); patch(); if(debug['p']) doprof2(); dodata(); follow(); noops(); span(); doinit(); asmb(); undef(); if(debug['v']) { Bprint(&bso, "%5.2f cpu time\n", cputime()); Bprint(&bso, "%ld symbols\n", nsymbol); Bprint(&bso, "%ld memory used\n", thunk); Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); } Bflush(&bso); errorexit();} voidloadlib(int beg, int end){ int i, t; for(i=end-1; i>=beg; i--) { t = libraryp; if(debug['v']) Bprint(&bso, "%5.2f autolib: %s\n", cputime(), library[i]); objfile(library[i]); if(t != libraryp) loadlib(t, libraryp); }} voiderrorexit(void){ if(nerrors) { if(cout >= 0) remove(outfile); exits("error"); } exits(0);} voidobjfile(char *file){ long off, esym, cnt, l; int f, work; Sym *s; char magbuf[SARMAG]; char name[100], pname[150]; struct ar_hdr arhdr; char *e, *start, *stop; if(file[0] == '-' && file[1] == 'l') { if(debug['9']) sprint(name, "/%s/lib/lib", thestring); else sprint(name, "/usr/%clib/lib", thechar); strcat(name, file+2); strcat(name, ".a"); file = name; } if(debug['v']) Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file); Bflush(&bso); f = open(file, 0); if(f < 0) { diag("cannot open file: %s\n", file); errorexit(); } l = read(f, magbuf, SARMAG); if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ /* load it as a regular file */ l = seek(f, 0L, 2); seek(f, 0L, 0); ldobj(f, l, file); close(f); return; } if(debug['v']) Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file); l = read(f, &arhdr, SAR_HDR); if(l != SAR_HDR) { diag("%s: short read on archive file symbol header\n", file); goto out; } if(strncmp(arhdr.name, symname, strlen(symname))) { diag("%s: first entry not symbol header\n", file); goto out; } esym = SARMAG + SAR_HDR + atolwhex(arhdr.size); off = SARMAG + SAR_HDR; /* * just bang the whole symbol file into memory */ seek(f, off, 0); cnt = esym - off; start = malloc(cnt + 10); cnt = read(f, start, cnt); if(cnt <= 0){ close(f); return; } stop = &start[cnt]; memset(stop, 0, 10); work = 1; while(work){ if(debug['v']) Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file); Bflush(&bso); work = 0; for(e = start; e < stop; e = strchr(e+5, 0) + 1) { s = lookup(e+5, 0); if(s->type != SXREF) continue; sprint(pname, "%s(%s)", file, s->name); if(debug['v']) Bprint(&bso, "%5.2f library: %s\n", cputime(), pname); Bflush(&bso); l = e[1] & 0xff; l |= (e[2] & 0xff) << 8; l |= (e[3] & 0xff) << 16; l |= (e[4] & 0xff) << 24; seek(f, l, 0); l = read(f, &arhdr, SAR_HDR); if(l != SAR_HDR) goto bad; if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag))) goto bad; l = atolwhex(arhdr.size); ldobj(f, l, pname); if(s->type == SXREF) { diag("%s: failed to load: %s\n", file, s->name); errorexit(); } work = 1; } } return; bad: diag("%s: bad or out of date archive\n", file);out: close(f);} intzaddr(uchar *p, Adr *a, Sym *h[]){ int c, t, i; long l; Sym *s; Auto *u; t = p[0]; c = 1; if(t & T_INDEX) { a->index = p[c]; a->scale = p[c+1]; c += 2; } else { a->index = D_NONE; a->scale = 0; } a->offset = 0; if(t & T_OFFSET) { a->offset = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24); c += 4; } a->sym = S; if(t & T_SYM) { a->sym = h[p[c]]; c++; } a->type = D_NONE; if(t & T_FCONST) { a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24); a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24); c += 8; a->type = D_FCONST; } else if(t & T_SCONST) { for(i=0; i<NSNAME; i++) a->scon[i] = p[c+i]; c += NSNAME; a->type = D_SCONST; } if(t & T_TYPE) { a->type = p[c]; c++; } s = a->sym; if(s == S) return c; t = a->type; if(t != D_AUTO && t != D_PARAM) return c; l = a->offset; for(u=curauto; u; u=u->link) { if(u->sym == s) if(u->type == t) { if(u->offset > l) u->offset = l; return c; } } while(nhunk < sizeof(Auto)) gethunk(); u = (Auto*)hunk; nhunk -= sizeof(Auto); hunk += sizeof(Auto); u->link = curauto; curauto = u; u->sym = s; u->offset = l; u->type = t; return c;} voidaddlib(char *obj){ char name[1024], comp[256], *p; int i; if(histfrogp <= 0) return; if(histfrog[0]->name[1] == '/') { sprint(name, ""); i = 1; } else if(histfrog[0]->name[1] == '.') { sprint(name, "."); i = 0; } else { if(debug['9']) sprint(name, "/%s/lib", thestring); else sprint(name, "/usr/%clib", thechar); i = 0; } for(; i<histfrogp; i++) { snprint(comp, sizeof comp, histfrog[i]->name+1); for(;;) { p = strstr(comp, "$O"); if(p == 0) break; memmove(p+1, p+2, strlen(p+2)+1); p[0] = thechar; } for(;;) { p = strstr(comp, "$M"); if(p == 0) break; if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) { diag("library component too long"); return; } memmove(p+strlen(thestring), p+2, strlen(p+2)+1); memmove(p, thestring, strlen(thestring)); } if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) { diag("library component too long"); return; } strcat(name, "/"); strcat(name, comp); } for(i=0; i<libraryp; i++) if(strcmp(name, library[i]) == 0) return; if(libraryp == nelem(library)){ diag("too many autolibs; skipping %s", name);Lines 1–500 of 1111. Load more
#include "l.h" #include <ar.h> #ifndef DEFAULT #define DEFAULT '9' #endif char *noname = "<none>"; char symname[] = SYMDEF; char thechar = '6'; char *thestring = "960"; /* * -H0 -T0x40004C -D0x10000000 is garbage unix * -H1 -T0xd0 -R4 is unix coff * -H2 -T4128 -R4096 is plan9 format * -H3 -Tx -Rx is msdos boot * -H4 -T0x8000 -R4 is Intel 960 coff */ void main(int argc, char *argv[]) { int i, c; char *a; Binit(&bso, 1, OWRITE); cout = -1; listinit(); memset(debug, 0, sizeof(debug)); nerrors = 0; outfile = "6.out"; HEADTYPE = -1; INITTEXT = -1; INITDAT = -1; INITRND = -1; INITENTRY = 0; ARGBEGIN { default: c = ARGC(); if(c >= 0 && c < sizeof(debug)) debug[c]++; break; case 'o': /* output to (next arg) */ outfile = ARGF(); break; case 'E': a = ARGF(); if(a) INITENTRY = a; break; case 'H': a = ARGF(); if(a) HEADTYPE = atolwhex(a); break; case 'T': a = ARGF(); if(a) INITTEXT = atolwhex(a); break; case 'D': a = ARGF(); if(a) INITDAT = atolwhex(a); break; case 'R': a = ARGF(); if(a) INITRND = atolwhex(a); break; } ARGEND USED(argc); if(*argv == 0) { diag("usage: 6l [-options] objects\n"); errorexit(); } if(!debug['9'] && !debug['U'] && !debug['B']) debug[DEFAULT] = 1; if(HEADTYPE == -1) { if(debug['U']) HEADTYPE = 1; if(debug['B']) HEADTYPE = 2; if(debug['9']) HEADTYPE = 2; } switch(HEADTYPE) { default: diag("unknown -H option"); errorexit(); case 0: /* this is garbage */ HEADR = 20L+56L; if(INITTEXT == -1) INITTEXT = 0x40004CL; if(INITDAT == -1) INITDAT = 0x10000000L; if(INITRND == -1) INITRND = 0; break; case 1: /* is unix coff */ HEADR = 0xd0L; if(INITTEXT == -1) INITTEXT = 0xd0; if(INITDAT == -1) INITDAT = 0x400000; if(INITRND == -1) INITRND = 0; break; case 2: /* plan 9 */ HEADR = 32L; if(INITTEXT == -1) INITTEXT = 4096+32; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4096; break; case 3: /* msdos boot */ HEADR = 0; if(INITTEXT == -1) INITTEXT = 0x0100; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4; break; case 4: /* is 960 coff */ HEADR = 0x8CL; if(INITTEXT == -1) INITTEXT = 0x8000; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4; break; } if(INITDAT != 0 && INITRND != 0) print("warning: -D0x%lux is ignored because of -R0x%lux\n", INITDAT, INITRND); if(debug['v']) Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n", HEADTYPE, INITTEXT, INITDAT, INITRND); Bflush(&bso); for(i=1; optab[i].as; i++) if(i != optab[i].as) { diag("phase error in optab: %d\n", i); errorexit(); } maxop = i; for(i=0; i<Ymax; i++) ycover[i*Ymax + i] = 1; ycover[Yi5*Ymax + Yri5] = 1; ycover[Yr*Ymax + Yri5] = 1; ycover[Yi5*Ymax + Ynri5] = 1; ycover[Yr*Ymax + Ynri5] = 1; ycover[Ynone*Ymax + Ynri5] = 1; ycover[Yi5*Ymax + Yi32] = 1; zprg.link = P; zprg.cond = P; zprg.back = 2; zprg.as = AGOK; zprg.type = D_NONE; zprg.offset = 0; zprg.from.type = D_NONE; zprg.from.index = D_NONE; zprg.from.scale = 1; zprg.to = zprg.from; pcstr = "%.6lux "; nuxiinit(); histgen = 0; textp = P; datap = P; edatap = P; pc = 0; cout = create(outfile, 1, 0775); if(cout < 0) { diag("cannot create %s\n", outfile); errorexit(); } version = 0; cbp = buf.cbuf; cbc = sizeof(buf.cbuf); firstp = prg(); lastp = firstp; if(INITENTRY == 0) { INITENTRY = "_main"; if(debug['p']) INITENTRY = "_mainp"; if(!debug['l']) lookup(INITENTRY, 0)->type = SXREF; } else lookup(INITENTRY, 0)->type = SXREF; while(*argv) objfile(*argv++); if(!debug['l']) loadlib(0, libraryp); firstp = firstp->link; if(firstp == P) errorexit(); patch(); if(debug['p']) doprof2(); dodata(); follow(); noops(); span(); doinit(); asmb(); undef(); if(debug['v']) { Bprint(&bso, "%5.2f cpu time\n", cputime()); Bprint(&bso, "%ld symbols\n", nsymbol); Bprint(&bso, "%ld memory used\n", thunk); Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); } Bflush(&bso); errorexit(); } void loadlib(int beg, int end) { int i, t; for(i=end-1; i>=beg; i--) { t = libraryp; if(debug['v']) Bprint(&bso, "%5.2f autolib: %s\n", cputime(), library[i]); objfile(library[i]); if(t != libraryp) loadlib(t, libraryp); } } void errorexit(void) { if(nerrors) { if(cout >= 0) remove(outfile); exits("error"); } exits(0); } void objfile(char *file) { long off, esym, cnt, l; int f, work; Sym *s; char magbuf[SARMAG]; char name[100], pname[150]; struct ar_hdr arhdr; char *e, *start, *stop; if(file[0] == '-' && file[1] == 'l') { if(debug['9']) sprint(name, "/%s/lib/lib", thestring); else sprint(name, "/usr/%clib/lib", thechar); strcat(name, file+2); strcat(name, ".a"); file = name; } if(debug['v']) Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file); Bflush(&bso); f = open(file, 0); if(f < 0) { diag("cannot open file: %s\n", file); errorexit(); } l = read(f, magbuf, SARMAG); if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ /* load it as a regular file */ l = seek(f, 0L, 2); seek(f, 0L, 0); ldobj(f, l, file); close(f); return; } if(debug['v']) Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file); l = read(f, &arhdr, SAR_HDR); if(l != SAR_HDR) { diag("%s: short read on archive file symbol header\n", file); goto out; } if(strncmp(arhdr.name, symname, strlen(symname))) { diag("%s: first entry not symbol header\n", file); goto out; } esym = SARMAG + SAR_HDR + atolwhex(arhdr.size); off = SARMAG + SAR_HDR; /* * just bang the whole symbol file into memory */ seek(f, off, 0); cnt = esym - off; start = malloc(cnt + 10); cnt = read(f, start, cnt); if(cnt <= 0){ close(f); return; } stop = &start[cnt]; memset(stop, 0, 10); work = 1; while(work){ if(debug['v']) Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file); Bflush(&bso); work = 0; for(e = start; e < stop; e = strchr(e+5, 0) + 1) { s = lookup(e+5, 0); if(s->type != SXREF) continue; sprint(pname, "%s(%s)", file, s->name); if(debug['v']) Bprint(&bso, "%5.2f library: %s\n", cputime(), pname); Bflush(&bso); l = e[1] & 0xff; l |= (e[2] & 0xff) << 8; l |= (e[3] & 0xff) << 16; l |= (e[4] & 0xff) << 24; seek(f, l, 0); l = read(f, &arhdr, SAR_HDR); if(l != SAR_HDR) goto bad; if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag))) goto bad; l = atolwhex(arhdr.size); ldobj(f, l, pname); if(s->type == SXREF) { diag("%s: failed to load: %s\n", file, s->name); errorexit(); } work = 1; } } return; bad: diag("%s: bad or out of date archive\n", file); out: close(f); } int zaddr(uchar *p, Adr *a, Sym *h[]) { int c, t, i; long l; Sym *s; Auto *u; t = p[0]; c = 1; if(t & T_INDEX) { a->index = p[c]; a->scale = p[c+1]; c += 2; } else { a->index = D_NONE; a->scale = 0; } a->offset = 0; if(t & T_OFFSET) { a->offset = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24); c += 4; } a->sym = S; if(t & T_SYM) { a->sym = h[p[c]]; c++; } a->type = D_NONE; if(t & T_FCONST) { a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24); a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24); c += 8; a->type = D_FCONST; } else if(t & T_SCONST) { for(i=0; i<NSNAME; i++) a->scon[i] = p[c+i]; c += NSNAME; a->type = D_SCONST; } if(t & T_TYPE) { a->type = p[c]; c++; } s = a->sym; if(s == S) return c; t = a->type; if(t != D_AUTO && t != D_PARAM) return c; l = a->offset; for(u=curauto; u; u=u->link) { if(u->sym == s) if(u->type == t) { if(u->offset > l) u->offset = l; return c; } } while(nhunk < sizeof(Auto)) gethunk(); u = (Auto*)hunk; nhunk -= sizeof(Auto); hunk += sizeof(Auto); u->link = curauto; curauto = u; u->sym = s; u->offset = l; u->type = t; return c; } void addlib(char *obj) { char name[1024], comp[256], *p; int i; if(histfrogp <= 0) return; if(histfrog[0]->name[1] == '/') { sprint(name, ""); i = 1; } else if(histfrog[0]->name[1] == '.') { sprint(name, "."); i = 0; } else { if(debug['9']) sprint(name, "/%s/lib", thestring); else sprint(name, "/usr/%clib", thechar); i = 0; } for(; i<histfrogp; i++) { snprint(comp, sizeof comp, histfrog[i]->name+1); for(;;) { p = strstr(comp, "$O"); if(p == 0) break; memmove(p+1, p+2, strlen(p+2)+1); p[0] = thechar; } for(;;) { p = strstr(comp, "$M"); if(p == 0) break; if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) { diag("library component too long"); return; } memmove(p+strlen(thestring), p+2, strlen(p+2)+1); memmove(p, thestring, strlen(thestring)); } if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) { diag("library component too long"); return; } strcat(name, "/"); strcat(name, comp); } for(i=0; i<libraryp; i++) if(strcmp(name, library[i]) == 0) return; if(libraryp == nelem(library)){ diag("too many autolibs; skipping %s", name);