9 char *noname = "<none>";
10 char symname[] = SYMDEF;
12 char *thestring = "mips";
15 * -H0 -T0x40004C -D0x10000000 is abbrev unix
16 * -H1 -T0x80020000 -R4 is bootp() format for 3k
17 * -H2 -T4128 -R4096 is plan9 format
18 * -H3 -T0x80020000 -R8 is bootp() format for 4k
19 * -H4 -T0x400000 -R4 is sgi unix coff executable
20 * -H5 -T0x4000A0 -R4 is sgi unix elf executable
27 main(int argc, char *argv[])
32 Binit(&bso, 1, OWRITE);
47 if(c >= 0 && c < sizeof(debug))
58 case 'L': /* for little-endian mips */
66 INITTEXT = atolwhex(a);
71 INITDAT = atolwhex(a);
76 INITRND = atolwhex(a);
81 HEADTYPE = atolwhex(a);
82 /* do something about setting INITTEXT */
89 diag("usage: %cl [-options] objects", thechar);
92 if(!debug['9'] && !debug['U'] && !debug['B'])
104 diag("unknown -H option");
107 case 0: /* unix simple */
110 INITTEXT = 0x40004CL;
112 INITDAT = 0x10000000L;
116 case 1: /* boot for 3k */
119 INITTEXT = 0x80020000L;
134 case 3: /* boot for 4k */
135 HEADR = 20L+56L+3*40L;
137 INITTEXT = 0x80020000L;
143 case 4: /* sgi unix coff executable */
144 HEADR = 20L+56L+3*40L;
146 INITTEXT = 0x00400000L+HEADR;
148 INITDAT = 0x10000000;
152 case 5: /* sgi unix elf executable */
153 HEADR = rnd(52L+3*32L, 16);
155 INITTEXT = 0x00400000L+HEADR;
157 INITDAT = 0x10000000;
161 case 6: /* headerless */
164 INITTEXT = 0x80000000L+HEADR;
171 if(INITDAT != 0 && INITRND != 0)
172 print("warning: -D0x%lux is ignored because of -R0x%lux\n",
175 Bprint(&bso, "HEADER = -H0x%d -T0x%lux -D0x%lux -R0x%lux\n",
176 HEADTYPE, INITTEXT, INITDAT, INITRND);
180 zprg.from.name = D_NONE;
181 zprg.from.type = D_NONE;
182 zprg.from.reg = NREG;
191 static char name[20];
193 snprint(name, sizeof name, "%c.out", thechar);
196 cout = create(outfile, 1, 0775);
198 diag("%s: cannot create", outfile);
205 cbc = sizeof(buf.cbuf);
212 INITENTRY = "_mainp";
214 lookup(INITENTRY, 0)->type = SXREF;
216 lookup(INITENTRY, 0)->type = SXREF;
222 firstp = firstp->link;
242 Bprint(&bso, "%5.2f cpu time\n", cputime());
243 Bprint(&bso, "%ld memory used\n", thunk);
244 Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
245 Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
260 for(i=0; i<libraryp; i++) {
262 Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]);
266 for(h=0; h<nelem(hash); h++)
267 for(s = hash[h]; s != S; s = s->link)
287 long off, esym, cnt, l;
291 char name[100], pname[150];
293 char *e, *start, *stop;
295 if(file[0] == '-' && file[1] == 'l') {
297 sprint(name, "/%s/lib/lib", thestring);
299 sprint(name, "/usr/%clib/lib", thechar);
300 strcat(name, file+2);
305 Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
309 diag("cannot open file: %s", file);
312 l = read(f, magbuf, SARMAG);
313 if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
314 /* load it as a regular file */
323 Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file);
324 l = read(f, &arhdr, SAR_HDR);
326 diag("%s: short read on archive file symbol header", file);
329 if(strncmp(arhdr.name, symname, strlen(symname))) {
330 diag("%s: first entry not symbol header", file);
334 esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
335 off = SARMAG + SAR_HDR;
338 * just bang the whole symbol file into memory
342 start = malloc(cnt + 10);
343 cnt = read(f, start, cnt);
354 Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
357 for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
361 sprint(pname, "%s(%s)", file, s->name);
363 Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
366 l |= (e[2] & 0xff) << 8;
367 l |= (e[3] & 0xff) << 16;
368 l |= (e[4] & 0xff) << 24;
370 l = read(f, &arhdr, SAR_HDR);
373 if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
375 l = atolwhex(arhdr.size);
377 if(s->type == SXREF) {
378 diag("%s: failed to load: %s", file, s->name);
388 diag("%s: bad or out of date archive", file);
394 zaddr(uchar *p, Adr *a, Sym *h[])
402 if(c < 0 || c > NSYM){
403 print("sym out of range: %d\n", c);
413 if(a->reg < 0 || a->reg > NREG) {
414 print("register out of range %d\n", a->reg);
416 return 0; /* force real diagnostic */
421 print("unknown type %d\n", a->type);
423 return 0; /* force real diagnostic */
438 a->offset = p[4] | (p[5]<<8) |
439 (p[6]<<16) | (p[7]<<24);
444 while(nhunk < NSNAME)
446 a->sval = (char*)hunk;
450 memmove(a->sval, p+4, NSNAME);
455 while(nhunk < sizeof(Ieee))
457 a->ieee = (Ieee*)hunk;
461 a->ieee->l = p[4] | (p[5]<<8) |
462 (p[6]<<16) | (p[7]<<24);
463 a->ieee->h = p[8] | (p[9]<<8) |
464 (p[10]<<16) | (p[11]<<24);
472 if(i != D_AUTO && i != D_PARAM)
476 for(u=curauto; u; u=u->link)
484 while(nhunk < sizeof(Auto))
487 nhunk -= sizeof(Auto);
488 hunk += sizeof(Auto);
501 char name[1024], comp[256], *p;
507 if(histfrog[0]->name[1] == '/') {
511 if(histfrog[0]->name[1] == '.') {
516 sprint(name, "/%s/lib", thestring);
518 sprint(name, "/usr/%clib", thechar);
522 for(; i<histfrogp; i++) {
523 snprint(comp, sizeof comp, histfrog[i]->name+1);
525 p = strstr(comp, "$O");
528 memmove(p+1, p+2, strlen(p+2)+1);
532 p = strstr(comp, "$M");
535 if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
536 diag("library component too long");
539 memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
540 memmove(p, thestring, strlen(thestring));
542 if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
543 diag("library component too long");
549 for(i=0; i<libraryp; i++)
550 if(strcmp(name, library[i]) == 0)
552 if(libraryp == nelem(library)){
553 diag("too many autolibs; skipping %s", name);
557 p = malloc(strlen(name) + 1);
559 library[libraryp] = p;
560 p = malloc(strlen(obj) + 1);
562 libraryobj[libraryp] = p;
567 addhist(long line, int type)
573 u = malloc(sizeof(Auto));
574 s = malloc(sizeof(Sym));
575 s->name = malloc(2*(histfrogp+1) + 1);
584 for(i=0; i<histfrogp; i++) {
585 k = histfrog[i]->value;
610 * bad encoding of path components only allows
611 * MAXHIST components. if there is an overflow,
612 * first try to collapse xxx/..
614 for(i=1; i<histfrogp; i++)
615 if(strcmp(histfrog[i]->name+1, "..") == 0) {
616 memmove(histfrog+i-1, histfrog+i+1,
617 (histfrogp-i-1)*sizeof(histfrog[0]));
623 * next try to collapse .
625 for(i=0; i<histfrogp; i++)
626 if(strcmp(histfrog[i]->name+1, ".") == 0) {
627 memmove(histfrog+i, histfrog+i+1,
628 (histfrogp-i-1)*sizeof(histfrog[0]));
633 * last chance, just truncate from front
635 memmove(histfrog+0, histfrog+1,
636 (histfrogp-1)*sizeof(histfrog[0]));
639 histfrog[histfrogp-1] = s;
646 p->from.type = D_NONE;
651 readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
656 memmove(buf, good, stop - good);
661 n = read(f, stop, n);
668 ldobj(int f, long c, char *pn)
672 uchar *bloc, *bsize, *stop;
673 Sym *h[NSYM], *s, *di;
681 memset(h, 0, sizeof(h));
691 if(r < 100 && r < c) { /* enough for largest prog */
692 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
698 o = bloc[0]; /* as */
699 if(o <= AXXX || o >= ALAST) {
700 diag("%s: line %ld: opcode out of range %d", pn, pc-ipc, o);
701 print(" probably not a .%c file\n", thechar);
704 if(o == ANAME || o == ASIGNAME) {
709 stop = memchr(&bloc[3], 0, bsize-&bloc[3]);
711 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
715 stop = memchr(&bloc[3], 0, bsize-&bloc[3]);
717 fprint(2, "%s: name too long\n", pn);
721 v = bloc[1]; /* type */
722 o = bloc[2]; /* sym */
729 s = lookup((char*)bloc, r);
730 c -= &stop[1] - bloc;
734 print(" ANAME %s\n", s->name);
736 if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
739 if(s->type != SFILE) {
744 if(histfrogp < MAXHIST) {
745 histfrog[histfrogp] = s;
753 if(nhunk < sizeof(Prog))
756 nhunk -= sizeof(Prog);
757 hunk += sizeof(Prog);
760 p->reg = bloc[1] & 0x7f;
763 p->line = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24);
765 r = zaddr(bloc+6, &p->from, h) + 6;
766 r += zaddr(bloc+r, &p->to, h);
770 if(p->reg < 0 || p->reg > NREG)
771 diag("register out of range %d", p->reg);
781 if(p->to.offset == -1) {
786 addhist(p->line, D_FILE); /* 'z' */
788 addhist(p->to.offset, D_FILE1); /* 'Z' */
795 curtext->to.autom = curauto;
805 diag("GLOBL must have a name\n%P", p);
808 if(s->type == 0 || s->type == SXREF) {
812 if(s->type != SBSS) {
813 diag("redefinition: %s\n%P", s->name, p);
817 if(p->to.offset > s->value)
818 s->value = p->to.offset;
823 diag("DYNT without a sym\n%P", p);
828 if(di->type == SXREF) {
830 Bprint(&bso, "%P set to %d\n", p, dtype);
838 p->from.offset = di->value;
839 p->from.sym->type = SDATA;
841 diag("DYNT not in text: %P", p);
844 p->to.sym = curtext->from.sym;
845 p->to.type = D_CONST;
851 if(p->from.sym == S) {
852 diag("INIT without a sym\n%P", p);
856 diag("INIT without previous DYNT\n%P", p);
859 p->from.offset = di->value;
860 p->from.sym->type = SDATA;
866 if(p->from.sym == S) {
867 diag("DATA without a sym\n%P", p);
875 diag("unknown opcode\n%P", p);
883 curtext->to.autom = curauto;
888 autosize = (p->to.offset+3L) & ~3L;
889 p->to.offset = autosize;
893 diag("TEXT must have a name\n%P", p);
896 if(s->type != 0 && s->type != SXREF) {
901 diag("redefinition: %s\n%P", s->name, p);
920 if(p->from.type == D_CONST)
921 if(p->from.name == D_NONE) {
922 p->from.offset = -p->from.offset;
934 if(p->from.type == D_FCONST) {
936 sprint(literal, "$%lux", ieeedtof(p->from.ieee));
937 s = lookup(literal, 0);
944 t->from.type = D_OREG;
946 t->from.name = D_EXTERN;
952 p->from.type = D_OREG;
954 p->from.name = D_EXTERN;
963 if(p->from.type == D_FCONST) {
965 sprint(literal, "$%lux.%lux",
966 p->from.ieee->l, p->from.ieee->h);
967 s = lookup(literal, 0);
974 t->from.type = D_OREG;
976 t->from.name = D_EXTERN;
982 p->from.type = D_OREG;
984 p->from.name = D_EXTERN;
994 if(p->to.type == D_BRANCH)
1005 diag("truncated object file: %s", pn);
1009 lookup(char *symb, int v)
1017 for(p=symb; c = *p; p++)
1023 for(s = hash[h]; s != S; s = s->link)
1025 if(memcmp(s->name, symb, l) == 0)
1028 while(nhunk < sizeof(Sym))
1031 nhunk -= sizeof(Sym);
1032 hunk += sizeof(Sym);
1034 s->name = malloc(l);
1035 memmove(s->name, symb, l);
1050 while(nhunk < sizeof(Prog))
1053 nhunk -= sizeof(Prog);
1054 hunk += sizeof(Prog);
1067 if(thunk >= 5L*NHUNK) {
1069 if(thunk >= 25L*NHUNK)
1073 if(h == (char*)-1) {
1074 diag("out of memory");
1090 Bprint(&bso, "%5.2f profile 1\n", cputime());
1092 s = lookup("__mcount", 0);
1094 for(p = firstp->link; p != P; p = p->link) {
1095 if(p->as == ATEXT) {
1101 q->from.type = D_OREG;
1102 q->from.name = D_EXTERN;
1103 q->from.offset = n*4;
1107 q->to.type = D_CONST;
1116 p->from.type = D_OREG;
1117 p->from.name = D_EXTERN;
1119 p->from.offset = n*4 + 4;
1130 p->from.type = D_CONST;
1142 p->from.type = D_REG;
1143 p->from.reg = REGTMP;
1144 p->to.type = D_OREG;
1145 p->to.name = D_EXTERN;
1147 p->to.offset = n*4 + 4;
1159 q->from.type = D_OREG;
1160 q->from.name = D_EXTERN;
1163 q->to.type = D_CONST;
1174 Prog *p, *q, *q2, *ps2, *ps4;
1177 Bprint(&bso, "%5.2f profile 2\n", cputime());
1181 s2 = lookup("_tracein", 0);
1182 s4 = lookup("_traceout", 0);
1184 s2 = lookup("_profin", 0);
1185 s4 = lookup("_profout", 0);
1187 if(s2->type != STEXT || s4->type != STEXT) {
1189 diag("_tracein/_traceout not defined %d %d", s2->type, s4->type);
1191 diag("_profin/_profout not defined");
1197 for(p = firstp; p != P; p = p->link) {
1198 if(p->as == ATEXT) {
1199 if(p->from.sym == s2) {
1203 if(p->from.sym == s4) {
1209 for(p = firstp; p != P; p = p->link) {
1210 if(p->as == ATEXT) {
1211 if(p->reg & NOPROF) {
1230 if(debug['e']){ /* embedded tracing */
1239 q2->to.type = D_BRANCH;
1240 q2->to.sym = p->to.sym;
1246 p->to.type = D_BRANCH;
1256 if(debug['e']){ /* embedded tracing */
1278 p->from = zprg.from;
1280 p->to.type = D_BRANCH;
1297 if (!little) { /* normal big-endian case */
1298 c = find1(0x01020304L, i+1);
1306 } else { /* oddball little-endian case */
1307 c = find1(0x04030201L, i+1);
1318 Bprint(&bso, "inuxi = ");
1320 Bprint(&bso, "%d", inuxi1[i]);
1323 Bprint(&bso, "%d", inuxi2[i]);
1326 Bprint(&bso, "%d", inuxi4[i]);
1327 Bprint(&bso, "\nfnuxi = ");
1329 Bprint(&bso, "%d", fnuxi8[i]);
1335 find1(long l, int c)
1348 ieeedtof(Ieee *ieeep)
1355 exp = (ieeep->h>>20) & ((1L<<11)-1L);
1356 exp -= (1L<<10) - 2L;
1357 v = (ieeep->h & 0xfffffL) << 3;
1358 v |= (ieeep->l >> 29) & 0x7L;
1359 if((ieeep->l >> 28) & 1) {
1362 v = (v & 0x7fffffL) >> 1;
1366 if(exp <= -126 || exp >= 130)
1367 diag("double fp to single fp overflow");
1368 v |= ((exp + 126) & 0xffL) << 23;
1369 v |= ieeep->h & 0x80000000L;
1374 ieeedtod(Ieee *ieeep)
1380 if(ieeep->h & (1L<<31)) {
1381 e.h = ieeep->h & ~(1L<<31);
1383 return -ieeedtod(&e);
1385 if(ieeep->l == 0 && ieeep->h == 0)
1387 fr = ieeep->l & ((1L<<16)-1L);
1389 fr += (ieeep->l>>16) & ((1L<<16)-1L);
1391 fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
1393 exp = (ieeep->h>>20) & ((1L<<11)-1L);
1394 exp -= (1L<<10) - 2L;
1395 return ldexp(fr, exp);