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]))
99 readundefs(ARGF(), SEXPORT);
101 case 'u': /* produce dynamically loadable module */
104 if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1]))
105 readundefs(ARGF(), SIMPORT);
110 diag("usage: 8l [-options] objects");
113 if(!debug['9'] && !debug['U'] && !debug['B'])
125 diag("unknown -H option");
128 case 0: /* this is garbage */
131 INITTEXT = 0x40004CL;
133 INITDAT = 0x10000000L;
137 case 1: /* is unix coff */
155 case 3: /* MS-DOS .COM */
164 case 4: /* fake MS-DOS .EXE */
172 HEADR += (INITTEXT & 0xFFFF);
174 Bprint(&bso, "HEADR = 0x%ld\n", HEADR);
176 case 5: /* elf executable */
177 HEADR = rnd(52L+3*32L, 16);
179 INITTEXT = 0x80100020L;
186 if(INITDAT != 0 && INITRND != 0)
187 print("warning: -D0x%lux is ignored because of -R0x%lux\n",
190 Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n",
191 HEADTYPE, INITTEXT, INITDAT, INITRND);
193 for(i=1; optab[i].as; i++)
194 if(i != optab[i].as) {
195 diag("phase error in optab: %d", i);
199 for(i=0; i<Ymax; i++)
200 ycover[i*Ymax + i] = 1;
202 ycover[Yi0*Ymax + Yi8] = 1;
203 ycover[Yi1*Ymax + Yi8] = 1;
205 ycover[Yi0*Ymax + Yi32] = 1;
206 ycover[Yi1*Ymax + Yi32] = 1;
207 ycover[Yi8*Ymax + Yi32] = 1;
209 ycover[Yal*Ymax + Yrb] = 1;
210 ycover[Ycl*Ymax + Yrb] = 1;
211 ycover[Yax*Ymax + Yrb] = 1;
212 ycover[Ycx*Ymax + Yrb] = 1;
213 ycover[Yrx*Ymax + Yrb] = 1;
215 ycover[Yax*Ymax + Yrx] = 1;
216 ycover[Ycx*Ymax + Yrx] = 1;
218 ycover[Yax*Ymax + Yrl] = 1;
219 ycover[Ycx*Ymax + Yrl] = 1;
220 ycover[Yrx*Ymax + Yrl] = 1;
222 ycover[Yf0*Ymax + Yrf] = 1;
224 ycover[Yal*Ymax + Ymb] = 1;
225 ycover[Ycl*Ymax + Ymb] = 1;
226 ycover[Yax*Ymax + Ymb] = 1;
227 ycover[Ycx*Ymax + Ymb] = 1;
228 ycover[Yrx*Ymax + Ymb] = 1;
229 ycover[Yrb*Ymax + Ymb] = 1;
230 ycover[Ym*Ymax + Ymb] = 1;
232 ycover[Yax*Ymax + Yml] = 1;
233 ycover[Ycx*Ymax + Yml] = 1;
234 ycover[Yrx*Ymax + Yml] = 1;
235 ycover[Yrl*Ymax + Yml] = 1;
236 ycover[Ym*Ymax + Yml] = 1;
238 for(i=0; i<D_NONE; i++) {
240 if(i >= D_AL && i <= D_BH)
241 reg[i] = (i-D_AL) & 7;
242 if(i >= D_AX && i <= D_DI)
243 reg[i] = (i-D_AX) & 7;
244 if(i >= D_F0 && i <= D_F0+7)
245 reg[i] = (i-D_F0) & 7;
252 zprg.from.type = D_NONE;
253 zprg.from.index = D_NONE;
265 cout = create(outfile, 1, 0775);
267 diag("cannot create %s", outfile);
272 cbc = sizeof(buf.cbuf);
279 INITENTRY = "_mainp";
281 lookup(INITENTRY, 0)->type = SXREF;
283 lookup(INITENTRY, 0)->type = SXREF;
289 firstp = firstp->link;
293 EXPTAB = "_exporttab";
301 INITTEXT = INITDAT = 0;
321 Bprint(&bso, "%5.2f cpu time\n", cputime());
322 Bprint(&bso, "%ld symbols\n", nsymbol);
323 Bprint(&bso, "%ld memory used\n", thunk);
324 Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
325 Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
341 for(i=0; i<libraryp; i++) {
343 Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]);
347 for(h=0; h<nelem(hash); h++)
348 for(s = hash[h]; s != S; s = s->link)
368 long off, esym, cnt, l;
372 char name[100], pname[150];
374 char *e, *start, *stop;
376 if(file[0] == '-' && file[1] == 'l') {
378 sprint(name, "/%s/lib/lib", thestring);
380 sprint(name, "/usr/%clib/lib", thechar);
381 strcat(name, file+2);
386 Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
390 diag("cannot open file: %s", file);
393 l = read(f, magbuf, SARMAG);
394 if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
395 /* load it as a regular file */
403 l = read(f, &arhdr, SAR_HDR);
405 diag("%s: short read on archive file symbol header", file);
408 if(strncmp(arhdr.name, symname, strlen(symname))) {
409 diag("%s: first entry not symbol header", file);
413 esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
414 off = SARMAG + SAR_HDR;
417 * just bang the whole symbol file into memory
421 start = malloc(cnt + 10);
422 cnt = read(f, start, cnt);
433 Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
436 for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
440 sprint(pname, "%s(%s)", file, s->name);
442 Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
445 l |= (e[2] & 0xff) << 8;
446 l |= (e[3] & 0xff) << 16;
447 l |= (e[4] & 0xff) << 24;
449 l = read(f, &arhdr, SAR_HDR);
452 if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
454 l = atolwhex(arhdr.size);
456 if(s->type == SXREF) {
457 diag("%s: failed to load: %s", file, s->name);
467 diag("%s: bad or out of date archive", file);
473 zaddr(uchar *p, Adr *a, Sym *h[])
493 a->offset = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
503 a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
504 a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
509 for(i=0; i<NSNAME; i++)
523 if(t != D_AUTO && t != D_PARAM)
526 for(u=curauto; u; u=u->link) {
535 while(nhunk < sizeof(Auto))
538 nhunk -= sizeof(Auto);
539 hunk += sizeof(Auto);
552 char name[1024], comp[256], *p;
558 if(histfrog[0]->name[1] == '/') {
562 if(histfrog[0]->name[1] == '.') {
567 sprint(name, "/%s/lib", thestring);
569 sprint(name, "/usr/%clib", thechar);
573 for(; i<histfrogp; i++) {
574 snprint(comp, sizeof comp, histfrog[i]->name+1);
576 p = strstr(comp, "$O");
579 memmove(p+1, p+2, strlen(p+2)+1);
583 p = strstr(comp, "$M");
586 if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
587 diag("library component too long");
590 memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
591 memmove(p, thestring, strlen(thestring));
593 if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
594 diag("library component too long");
600 for(i=0; i<libraryp; i++)
601 if(strcmp(name, library[i]) == 0)
603 if(libraryp == nelem(library)){
604 diag("too many autolibs; skipping %s", name);
608 p = malloc(strlen(name) + 1);
610 library[libraryp] = p;
611 p = malloc(strlen(obj) + 1);
613 libraryobj[libraryp] = p;
618 addhist(long line, int type)
624 u = malloc(sizeof(Auto));
625 s = malloc(sizeof(Sym));
626 s->name = malloc(2*(histfrogp+1) + 1);
635 for(i=0; i<histfrogp; i++) {
636 k = histfrog[i]->value;
661 * bad encoding of path components only allows
662 * MAXHIST components. if there is an overflow,
663 * first try to collapse xxx/..
665 for(i=1; i<histfrogp; i++)
666 if(strcmp(histfrog[i]->name+1, "..") == 0) {
667 memmove(histfrog+i-1, histfrog+i+1,
668 (histfrogp-i-1)*sizeof(histfrog[0]));
674 * next try to collapse .
676 for(i=0; i<histfrogp; i++)
677 if(strcmp(histfrog[i]->name+1, ".") == 0) {
678 memmove(histfrog+i, histfrog+i+1,
679 (histfrogp-i-1)*sizeof(histfrog[0]));
684 * last chance, just truncate from front
686 memmove(histfrog+0, histfrog+1,
687 (histfrogp-1)*sizeof(histfrog[0]));
690 histfrog[histfrogp-1] = s;
697 p->from.type = D_NONE;
702 readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
707 memmove(buf, good, stop - good);
712 n = read(f, stop, n);
719 ldobj(int f, long c, char *pn)
723 uchar *bloc, *bsize, *stop;
725 Sym *h[NSYM], *s, *di;
732 nfilen = malloc((files+16)*sizeof(char*));
733 memmove(nfilen, filen, files*sizeof(char*));
737 filen[files++] = strdup(pn);
744 memset(h, 0, sizeof(h));
754 if(r < 100 && r < c) { /* enough for largest prog */
755 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
761 o = bloc[0] | (bloc[1] << 8);
762 if(o <= AXXX || o >= ALAST) {
765 diag("%s: opcode out of range %d", pn, o);
766 print(" probably not a .8 file\n");
770 if(o == ANAME || o == ASIGNAME) {
773 sig = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24);
777 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
779 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
783 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
785 fprint(2, "%s: name too long\n", pn);
789 v = bloc[2]; /* type */
790 o = bloc[3]; /* sym */
797 s = lookup((char*)bloc, r);
798 c -= &stop[1] - bloc;
801 if(debug['S'] && r == 0)
804 if(s->sig != 0 && s->sig != sig)
805 diag("incompatible type signatures %lux(%s) and %lux(%s) for %s", s->sig, filen[s->file], sig, pn, s->name);
811 print(" ANAME %s\n", s->name);
813 if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
816 if(s->type != SFILE) {
821 if(histfrogp < MAXHIST) {
822 histfrog[histfrogp] = s;
830 while(nhunk < sizeof(Prog))
833 nhunk -= sizeof(Prog);
834 hunk += sizeof(Prog);
837 p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
839 r = zaddr(bloc+6, &p->from, h) + 6;
840 r += zaddr(bloc+r, &p->to, h);
849 if(p->to.offset == -1) {
854 addhist(p->line, D_FILE); /* 'z' */
856 addhist(p->to.offset, D_FILE1); /* 'Z' */
863 curtext->to.autom = curauto;
872 if(s->type == 0 || s->type == SXREF) {
876 if(s->type != SBSS) {
877 diag("%s: redefinition: %s in %s",
882 if(p->to.offset > s->value)
883 s->value = p->to.offset;
888 diag("DYNT without a sym\n%P", p);
893 if(di->type == SXREF) {
895 Bprint(&bso, "%P set to %d\n", p, dtype);
903 p->from.offset = di->value;
904 p->from.sym->type = SDATA;
906 diag("DYNT not in text: %P", p);
909 p->to.sym = curtext->from.sym;
911 p->to.index = D_EXTERN;
915 if(p->from.sym == S) {
916 diag("INIT without a sym\n%P", p);
920 diag("INIT without previous DYNT\n%P", p);
923 p->from.offset = di->value;
924 p->from.sym->type = SDATA;
938 diag("%s: GOK opcode in %s", pn, TNAME);
945 curtext->to.autom = curauto;
952 diag("%s: no TEXT symbol: %P", pn, p);
955 if(s->type != 0 && s->type != SXREF) {
956 if(p->from.scale & DUPOK) {
960 diag("%s: redefinition: %s\n%P", pn, s->name, p);
988 if(p->from.type == D_FCONST) {
990 sprint(literal, "$%lux", ieeedtof(&p->from.ieee));
991 s = lookup(literal, 0);
998 t->from.type = D_EXTERN;
1009 p->from.type = D_EXTERN;
1026 if(p->from.type == D_FCONST) {
1027 /* size sb 18 max */
1028 sprint(literal, "$%lux.%lux",
1029 p->from.ieee.l, p->from.ieee.h);
1030 s = lookup(literal, 0);
1037 t->from.type = D_EXTERN;
1048 p->from.type = D_EXTERN;
1059 if(p->to.type == D_BRANCH)
1060 p->to.offset += ipc;
1070 diag("truncated object file: %s", pn);
1074 lookup(char *symb, int v)
1082 for(p=symb; c = *p; p++)
1088 for(s = hash[h]; s != S; s = s->link)
1090 if(memcmp(s->name, symb, l) == 0)
1093 while(nhunk < sizeof(Sym))
1096 nhunk -= sizeof(Sym);
1097 hunk += sizeof(Sym);
1099 s->name = malloc(l + 1);
1100 memmove(s->name, symb, l);
1117 while(nhunk < sizeof(Prog))
1120 nhunk -= sizeof(Prog);
1121 hunk += sizeof(Prog);
1156 if(thunk >= 5L*NHUNK) {
1158 if(thunk >= 25L*NHUNK)
1162 if(h == (char*)-1) {
1163 diag("out of memory");
1179 Bprint(&bso, "%5.2f profile 1\n", cputime());
1181 s = lookup("__mcount", 0);
1183 for(p = firstp->link; p != P; p = p->link) {
1184 if(p->as == ATEXT) {
1190 q->from.type = D_EXTERN;
1191 q->from.offset = n*4;
1195 q->to.type = D_CONST;
1204 p->from.type = D_CONST;
1206 p->to.type = D_EXTERN;
1208 p->to.offset = n*4 + 4;
1220 q->from.type = D_EXTERN;
1223 q->to.type = D_CONST;
1234 Prog *p, *q, *q2, *ps2, *ps4;
1237 Bprint(&bso, "%5.2f profile 2\n", cputime());
1241 s2 = lookup("_tracein", 0);
1242 s4 = lookup("_traceout", 0);
1244 s2 = lookup("_profin", 0);
1245 s4 = lookup("_profout", 0);
1247 if(s2->type != STEXT || s4->type != STEXT) {
1249 diag("_tracein/_traceout not defined %d %d", s2->type, s4->type);
1251 diag("_profin/_profout not defined");
1257 for(p = firstp; p != P; p = p->link) {
1258 if(p->as == ATEXT) {
1259 if(p->from.sym == s2) {
1263 if(p->from.sym == s4) {
1269 for(p = firstp; p != P; p = p->link) {
1270 if(p->as == ATEXT) {
1273 if(p->from.scale & NOPROF) { /* dont profile */
1292 if(debug['e']){ /* embedded tracing */
1301 q2->to.type = D_BRANCH;
1302 q2->to.sym = p->to.sym;
1303 q2->pcond = q->link;
1308 p->to.type = D_BRANCH;
1318 if(debug['e']){ /* embedded tracing */
1340 p->from = zprg.from;
1342 p->to.type = D_BRANCH;
1358 for(i=0; i<4; i++) {
1359 c = find1(0x04030201L, i+1);
1370 Bprint(&bso, "inuxi = ");
1372 Bprint(&bso, "%d", inuxi1[i]);
1375 Bprint(&bso, "%d", inuxi2[i]);
1378 Bprint(&bso, "%d", inuxi4[i]);
1379 Bprint(&bso, "\nfnuxi = ");
1381 Bprint(&bso, "%d", fnuxi4[i]);
1384 Bprint(&bso, "%d", fnuxi8[i]);
1391 find1(long l, int c)
1404 find2(long l, int c)
1410 for(i=0; i<4; i+=2) {
1411 if(((*p >> 8) & 0xff) == c)
1413 if((*p++ & 0xff) == c)
1427 exp = (e->h>>20) & ((1L<<11)-1L);
1428 exp -= (1L<<10) - 2L;
1429 v = (e->h & 0xfffffL) << 3;
1430 v |= (e->l >> 29) & 0x7L;
1431 if((e->l >> 28) & 1) {
1434 v = (v & 0x7fffffL) >> 1;
1438 if(exp <= -126 || exp >= 130)
1439 diag("double fp to single fp overflow");
1440 v |= ((exp + 126) & 0xffL) << 23;
1441 v |= e->h & 0x80000000L;
1446 ieeedtod(Ieee *ieeep)
1452 if(ieeep->h & (1L<<31)) {
1453 e.h = ieeep->h & ~(1L<<31);
1455 return -ieeedtod(&e);
1457 if(ieeep->l == 0 && ieeep->h == 0)
1459 fr = ieeep->l & ((1L<<16)-1L);
1461 fr += (ieeep->l>>16) & ((1L<<16)-1L);
1463 fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
1465 exp = (ieeep->h>>20) & ((1L<<11)-1L);
1466 exp -= (1L<<10) - 2L;
1467 return ldexp(fr, exp);
1477 diag("value != 0 on SXREF");
1479 diag("import index %d out of range", n);
1480 s->value = n<<Roffset;
1495 readundefs(char *f, int t)
1500 char *l, buf[256], *fields[64];
1504 b = Bopen(f, OREAD);
1506 diag("could not open %s: %r", f);
1509 while((l = Brdline(b, '\n')) != nil){
1511 if(n >= sizeof(buf)){
1512 diag("%s: line too long", f);
1517 n = getfields(buf, fields, nelem(fields), 1, " \t\r\n");
1518 if(n == nelem(fields)){
1519 diag("%s: bad format", f);
1522 for(i = 0; i < n; i++){
1523 s = lookup(fields[i], 0);