9 char *noname = "<none>";
10 char symname[] = SYMDEF;
12 char *thestring = "386";
15 * -H0 -T0x40004C -D0x10000000 is garbage unix
16 * -H1 -T0xd0 -R4 is unix coff
17 * -H2 -T4128 -R4096 is plan9 format
18 * -H3 -Tx -Rx is MS-DOS .COM
19 * -H4 -Tx -Rx is fake MS-DOS .EXE
20 * -H5 -T0x80100020 -R4096 is ELF
28 char buf1[5], buf2[SARMAG];
33 n = Bread(b, buf1, 5);
34 if(n == 5 && (buf1[2] == 1 && buf1[3] == '<' || buf1[3] == 1 && buf1[4] == '<'))
35 v = 1; /* good enough for our purposes */
38 n = Bread(b, buf2, SARMAG);
39 v = n == SARMAG && strncmp(buf2, ARMAG, SARMAG) == 0;
46 main(int argc, char *argv[])
51 Binit(&bso, 1, OWRITE);
54 memset(debug, 0, sizeof(debug));
65 if(c >= 0 && c < sizeof(debug))
68 case 'o': /* output to (next arg) */
79 HEADTYPE = atolwhex(a);
84 INITTEXT = atolwhex(a);
89 INITDAT = atolwhex(a);
94 INITRND = atolwhex(a);
96 case 'x': /* produce export table */
98 if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1])){
100 if(strcmp(a, "*") == 0)
103 readundefs(a, SEXPORT);
106 case 'u': /* produce dynamically loadable module */
109 if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1]))
110 readundefs(ARGF(), SIMPORT);
115 diag("usage: 8l [-options] objects");
118 if(!debug['9'] && !debug['U'] && !debug['B'])
130 diag("unknown -H option");
133 case 0: /* this is garbage */
136 INITTEXT = 0x40004CL;
138 INITDAT = 0x10000000L;
142 case 1: /* is unix coff */
160 case 3: /* MS-DOS .COM */
169 case 4: /* fake MS-DOS .EXE */
177 HEADR += (INITTEXT & 0xFFFF);
179 Bprint(&bso, "HEADR = 0x%ld\n", HEADR);
181 case 5: /* elf executable */
182 HEADR = rnd(52L+3*32L, 16);
184 INITTEXT = 0x80100020L;
191 if(INITDAT != 0 && INITRND != 0)
192 print("warning: -D0x%lux is ignored because of -R0x%lux\n",
195 Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n",
196 HEADTYPE, INITTEXT, INITDAT, INITRND);
198 for(i=1; optab[i].as; i++) {
200 if(opindex[c] != nil) {
201 diag("phase error in optab: %d (%A)", i, c);
204 opindex[c] = &optab[i];
207 for(i=0; i<Ymax; i++)
208 ycover[i*Ymax + i] = 1;
210 ycover[Yi0*Ymax + Yi8] = 1;
211 ycover[Yi1*Ymax + Yi8] = 1;
213 ycover[Yi0*Ymax + Yi32] = 1;
214 ycover[Yi1*Ymax + Yi32] = 1;
215 ycover[Yi8*Ymax + Yi32] = 1;
217 ycover[Yal*Ymax + Yrb] = 1;
218 ycover[Ycl*Ymax + Yrb] = 1;
219 ycover[Yax*Ymax + Yrb] = 1;
220 ycover[Ycx*Ymax + Yrb] = 1;
221 ycover[Yrx*Ymax + Yrb] = 1;
223 ycover[Yax*Ymax + Yrx] = 1;
224 ycover[Ycx*Ymax + Yrx] = 1;
226 ycover[Yax*Ymax + Yrl] = 1;
227 ycover[Ycx*Ymax + Yrl] = 1;
228 ycover[Yrx*Ymax + Yrl] = 1;
230 ycover[Yf0*Ymax + Yrf] = 1;
232 ycover[Yal*Ymax + Ymb] = 1;
233 ycover[Ycl*Ymax + Ymb] = 1;
234 ycover[Yax*Ymax + Ymb] = 1;
235 ycover[Ycx*Ymax + Ymb] = 1;
236 ycover[Yrx*Ymax + Ymb] = 1;
237 ycover[Yrb*Ymax + Ymb] = 1;
238 ycover[Ym*Ymax + Ymb] = 1;
240 ycover[Yax*Ymax + Yml] = 1;
241 ycover[Ycx*Ymax + Yml] = 1;
242 ycover[Yrx*Ymax + Yml] = 1;
243 ycover[Yrl*Ymax + Yml] = 1;
244 ycover[Ym*Ymax + Yml] = 1;
246 ycover[Ym*Ymax + Ymm] = 1;
247 ycover[Ymr*Ymax + Ymm] = 1;
249 ycover[Ym*Ymax + Yxm] = 1;
250 ycover[Yxr*Ymax + Yxm] = 1;
252 for(i=0; i<D_XNONE; i++) {
254 if(i >= D_AL && i <= D_BH)
255 reg[i] = (i-D_AL) & 7;
256 if(i >= D_AX && i <= D_DI)
257 reg[i] = (i-D_AX) & 7;
258 if(i >= D_F0 && i <= D_F0+7)
259 reg[i] = (i-D_F0) & 7;
260 if(i >= D_M0 && i <= D_M0+7)
261 reg[i] = (i-D_M0) & 7;
262 if(i >= D_X0 && i <= D_X0+7)
263 reg[i] = (i-D_X0) & 7;
270 zprg.from.type = D_NONE;
271 zprg.from.index = D_NONE;
283 cout = create(outfile, 1, 0775);
285 diag("cannot create %s", outfile);
290 cbc = sizeof(buf.cbuf);
297 INITENTRY = "_mainp";
299 lookup(INITENTRY, 0)->type = SXREF;
301 lookup(INITENTRY, 0)->type = SXREF;
307 firstp = firstp->link;
311 EXPTAB = "_exporttab";
319 INITTEXT = INITDAT = 0;
339 Bprint(&bso, "%5.2f cpu time\n", cputime());
340 Bprint(&bso, "%ld symbols\n", nsymbol);
341 Bprint(&bso, "%ld memory used\n", thunk);
342 Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
343 Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
359 for(i=0; i<libraryp; i++) {
361 Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]);
365 for(h=0; h<nelem(hash); h++)
366 for(s = hash[h]; s != S; s = s->link)
386 long off, esym, cnt, l;
390 char name[100], pname[150];
392 char *e, *start, *stop;
394 if(file[0] == '-' && file[1] == 'l') {
396 snprint(name, sizeof name, "/%s/lib/lib%s.a", thestring, file+2);
398 snprint(name, sizeof name, "/usr/%clib/lib%s.a", thechar, file+2);
402 Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
406 diag("cannot open file: %s", file);
409 l = read(f, magbuf, SARMAG);
410 if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
411 /* load it as a regular file */
419 l = read(f, &arhdr, SAR_HDR);
421 diag("%s: short read on archive file symbol header", file);
424 if(strncmp(arhdr.name, symname, strlen(symname))) {
425 diag("%s: first entry not symbol header", file);
429 esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
430 off = SARMAG + SAR_HDR;
433 * just bang the whole symbol file into memory
437 start = malloc(cnt + 10);
438 cnt = read(f, start, cnt);
449 Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
452 for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
456 snprint(pname, sizeof pname, "%s(%s)", file, s->name);
458 Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
461 l |= (e[2] & 0xff) << 8;
462 l |= (e[3] & 0xff) << 16;
463 l |= (e[4] & 0xff) << 24;
465 l = read(f, &arhdr, SAR_HDR);
468 if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
470 l = atolwhex(arhdr.size);
472 if(s->type == SXREF) {
473 diag("%s: failed to load: %s", file, s->name);
483 diag("%s: bad or out of date archive", file);
489 zaddr(uchar *p, Adr *a, Sym *h[])
509 a->offset = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
519 a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
520 a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
525 for(i=0; i<NSNAME; i++)
539 if(t != D_AUTO && t != D_PARAM)
542 for(u=curauto; u; u=u->link) {
551 while(nhunk < sizeof(Auto))
554 nhunk -= sizeof(Auto);
555 hunk += sizeof(Auto);
568 char name[1024], comp[256], *p;
574 if(histfrog[0]->name[1] == '/') {
578 if(histfrog[0]->name[1] == '.') {
579 snprint(name, sizeof name, ".");
583 snprint(name, sizeof name, "/%s/lib", thestring);
585 snprint(name, sizeof name, "/usr/%clib", thechar);
589 for(; i<histfrogp; i++) {
590 snprint(comp, sizeof comp, histfrog[i]->name+1);
592 p = strstr(comp, "$O");
595 memmove(p+1, p+2, strlen(p+2)+1);
599 p = strstr(comp, "$M");
602 if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
603 diag("library component too long");
606 memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
607 memmove(p, thestring, strlen(thestring));
609 if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
610 diag("library component too long");
616 for(i=0; i<libraryp; i++)
617 if(strcmp(name, library[i]) == 0)
619 if(libraryp == nelem(library)){
620 diag("too many autolibs; skipping %s", name);
624 p = malloc(strlen(name) + 1);
626 library[libraryp] = p;
627 p = malloc(strlen(obj) + 1);
629 libraryobj[libraryp] = p;
634 addhist(long line, int type)
640 u = malloc(sizeof(Auto));
641 s = malloc(sizeof(Sym));
642 s->name = malloc(2*(histfrogp+1) + 1);
651 for(i=0; i<histfrogp; i++) {
652 k = histfrog[i]->value;
677 * bad encoding of path components only allows
678 * MAXHIST components. if there is an overflow,
679 * first try to collapse xxx/..
681 for(i=1; i<histfrogp; i++)
682 if(strcmp(histfrog[i]->name+1, "..") == 0) {
683 memmove(histfrog+i-1, histfrog+i+1,
684 (histfrogp-i-1)*sizeof(histfrog[0]));
690 * next try to collapse .
692 for(i=0; i<histfrogp; i++)
693 if(strcmp(histfrog[i]->name+1, ".") == 0) {
694 memmove(histfrog+i, histfrog+i+1,
695 (histfrogp-i-1)*sizeof(histfrog[0]));
700 * last chance, just truncate from front
702 memmove(histfrog+0, histfrog+1,
703 (histfrogp-1)*sizeof(histfrog[0]));
706 histfrog[histfrogp-1] = s;
713 p->from.type = D_NONE;
718 readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
723 memmove(buf, good, stop - good);
728 n = read(f, stop, n);
735 ldobj(int f, long c, char *pn)
739 uchar *bloc, *bsize, *stop;
741 Sym *h[NSYM], *s, *di;
748 nfilen = malloc((files+16)*sizeof(char*));
749 memmove(nfilen, filen, files*sizeof(char*));
753 filen[files++] = strdup(pn);
760 memset(h, 0, sizeof(h));
770 if(r < 100 && r < c) { /* enough for largest prog */
771 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
777 o = bloc[0] | (bloc[1] << 8);
778 if(o <= AXXX || o >= ALAST) {
781 diag("%s: opcode out of range %d", pn, o);
782 print(" probably not a .8 file\n");
786 if(o == ANAME || o == ASIGNAME) {
789 sig = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24);
793 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
795 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
799 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
801 fprint(2, "%s: name too long\n", pn);
805 v = bloc[2]; /* type */
806 o = bloc[3]; /* sym */
813 s = lookup((char*)bloc, r);
814 c -= &stop[1] - bloc;
817 if(debug['S'] && r == 0)
820 if(s->sig != 0 && s->sig != sig)
821 diag("incompatible type signatures %lux(%s) and %lux(%s) for %s", s->sig, filen[s->file], sig, pn, s->name);
827 print(" ANAME %s\n", s->name);
829 if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
832 if(s->type != SFILE) {
837 if(histfrogp < MAXHIST) {
838 histfrog[histfrogp] = s;
846 while(nhunk < sizeof(Prog))
849 nhunk -= sizeof(Prog);
850 hunk += sizeof(Prog);
853 p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
855 r = zaddr(bloc+6, &p->from, h) + 6;
856 r += zaddr(bloc+r, &p->to, h);
865 if(p->to.offset == -1) {
870 addhist(p->line, D_FILE); /* 'z' */
872 addhist(p->to.offset, D_FILE1); /* 'Z' */
879 curtext->to.autom = curauto;
888 if(p->from.scale & DUPOK)
890 if(s->type == 0 || s->type == SXREF) {
894 if(s->type != SBSS) {
895 diag("%s: redefinition: %s in %s",
900 if(p->to.offset > s->value)
901 s->value = p->to.offset;
906 diag("DYNT without a sym\n%P", p);
911 if(di->type == SXREF) {
913 Bprint(&bso, "%P set to %d\n", p, dtype);
921 p->from.offset = di->value;
922 p->from.sym->type = SDATA;
924 diag("DYNT not in text: %P", p);
927 p->to.sym = curtext->from.sym;
929 p->to.index = D_EXTERN;
933 if(p->from.sym == S) {
934 diag("INIT without a sym\n%P", p);
938 diag("INIT without previous DYNT\n%P", p);
941 p->from.offset = di->value;
942 p->from.sym->type = SDATA;
956 diag("%s: GOK opcode in %s", pn, TNAME);
963 curtext->to.autom = curauto;
970 diag("%s: no TEXT symbol: %P", pn, p);
973 if(s->type != 0 && s->type != SXREF) {
974 if(p->from.scale & DUPOK) {
978 diag("%s: redefinition: %s\n%P", pn, s->name, p);
1013 if(p->from.type == D_FCONST) {
1015 snprint(literal, sizeof literal, "$%lux", ieeedtof(&p->from.ieee));
1016 s = lookup(literal, 0);
1023 t->from.type = D_EXTERN;
1034 p->from.type = D_EXTERN;
1058 if(p->from.type == D_FCONST) {
1059 /* size sb 18 max */
1060 snprint(literal, sizeof literal, "$%lux.%lux",
1061 p->from.ieee.l, p->from.ieee.h);
1062 s = lookup(literal, 0);
1069 t->from.type = D_EXTERN;
1080 p->from.type = D_EXTERN;
1091 if(p->to.type == D_BRANCH)
1092 p->to.offset += ipc;
1102 diag("truncated object file: %s", pn);
1106 lookup(char *symb, int v)
1114 for(p=symb; c = *p; p++)
1120 for(s = hash[h]; s != S; s = s->link)
1122 if(memcmp(s->name, symb, l) == 0)
1125 while(nhunk < sizeof(Sym))
1128 nhunk -= sizeof(Sym);
1129 hunk += sizeof(Sym);
1131 s->name = malloc(l + 1);
1132 memmove(s->name, symb, l);
1150 while(nhunk < sizeof(Prog))
1153 nhunk -= sizeof(Prog);
1154 hunk += sizeof(Prog);
1189 if(thunk >= 5L*NHUNK) {
1191 if(thunk >= 25L*NHUNK)
1195 if(h == (char*)-1) {
1196 diag("out of memory");
1212 Bprint(&bso, "%5.2f profile 1\n", cputime());
1214 s = lookup("__mcount", 0);
1216 for(p = firstp->link; p != P; p = p->link) {
1217 if(p->as == ATEXT) {
1223 q->from.type = D_EXTERN;
1224 q->from.offset = n*4;
1228 q->to.type = D_CONST;
1237 p->from.type = D_CONST;
1239 p->to.type = D_EXTERN;
1241 p->to.offset = n*4 + 4;
1253 q->from.type = D_EXTERN;
1256 q->to.type = D_CONST;
1267 Prog *p, *q, *q2, *ps2, *ps4;
1270 Bprint(&bso, "%5.2f profile 2\n", cputime());
1274 s2 = lookup("_tracein", 0);
1275 s4 = lookup("_traceout", 0);
1277 s2 = lookup("_profin", 0);
1278 s4 = lookup("_profout", 0);
1280 if(s2->type != STEXT || s4->type != STEXT) {
1282 diag("_tracein/_traceout not defined %d %d", s2->type, s4->type);
1284 diag("_profin/_profout not defined");
1290 for(p = firstp; p != P; p = p->link) {
1291 if(p->as == ATEXT) {
1292 if(p->from.sym == s2) {
1296 if(p->from.sym == s4) {
1302 for(p = firstp; p != P; p = p->link) {
1303 if(p->as == ATEXT) {
1306 if(p->from.scale & NOPROF) { /* dont profile */
1325 if(debug['e']){ /* embedded tracing */
1334 q2->to.type = D_BRANCH;
1335 q2->to.sym = p->to.sym;
1336 q2->pcond = q->link;
1341 p->to.type = D_BRANCH;
1351 if(debug['e']){ /* embedded tracing */
1373 p->from = zprg.from;
1375 p->to.type = D_BRANCH;
1391 for(i=0; i<4; i++) {
1392 c = find1(0x04030201L, i+1);
1403 Bprint(&bso, "inuxi = ");
1405 Bprint(&bso, "%d", inuxi1[i]);
1408 Bprint(&bso, "%d", inuxi2[i]);
1411 Bprint(&bso, "%d", inuxi4[i]);
1412 Bprint(&bso, "\nfnuxi = ");
1414 Bprint(&bso, "%d", fnuxi4[i]);
1417 Bprint(&bso, "%d", fnuxi8[i]);
1424 find1(long l, int c)
1437 find2(long l, int c)
1443 for(i=0; i<4; i+=2) {
1444 if(((*p >> 8) & 0xff) == c)
1446 if((*p++ & 0xff) == c)
1460 exp = (e->h>>20) & ((1L<<11)-1L);
1461 exp -= (1L<<10) - 2L;
1462 v = (e->h & 0xfffffL) << 3;
1463 v |= (e->l >> 29) & 0x7L;
1464 if((e->l >> 28) & 1) {
1467 v = (v & 0x7fffffL) >> 1;
1471 if(exp <= -126 || exp >= 130)
1472 diag("double fp to single fp overflow");
1473 v |= ((exp + 126) & 0xffL) << 23;
1474 v |= e->h & 0x80000000L;
1479 ieeedtod(Ieee *ieeep)
1485 if(ieeep->h & (1L<<31)) {
1486 e.h = ieeep->h & ~(1L<<31);
1488 return -ieeedtod(&e);
1490 if(ieeep->l == 0 && ieeep->h == 0)
1492 fr = ieeep->l & ((1L<<16)-1L);
1494 fr += (ieeep->l>>16) & ((1L<<16)-1L);
1496 fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
1498 exp = (ieeep->h>>20) & ((1L<<11)-1L);
1499 exp -= (1L<<10) - 2L;
1500 return ldexp(fr, exp);
1510 diag("value != 0 on SXREF");
1512 diag("import index %d out of range", n);
1513 s->value = n<<Roffset;
1528 readundefs(char *f, int t)
1533 char *l, buf[256], *fields[64];
1537 b = Bopen(f, OREAD);
1539 diag("could not open %s: %r", f);
1542 while((l = Brdline(b, '\n')) != nil){
1544 if(n >= sizeof(buf)){
1545 diag("%s: line too long", f);
1550 n = getfields(buf, fields, nelem(fields), 1, " \t\r\n");
1551 if(n == nelem(fields)){
1552 diag("%s: bad format", f);
1555 for(i = 0; i < n; i++){
1556 s = lookup(fields[i], 0);