9 char symname[] = SYMDEF;
11 char *thestring = "68000";
14 * -H0 -T0x40004C -D0x10000000 is garbage unix
15 * -H1 -T0x80020000 -R4 is garbage format
16 * -H2 -T8224 -R8192 is plan9 format
17 * -H3 -Tx -Rx is next boot
18 * -H4 -T0 -D0 is pilot relocatable
22 main(int argc, char *argv[])
27 Binit(&bso, 1, OWRITE);
30 memset(debug, 0, sizeof(debug));
42 if(c >= 0 && c < sizeof(debug))
45 case 'o': /* output to (next arg) */
56 HEADTYPE = atolwhex(a);
61 INITTEXT = atolwhex(a);
66 INITDAT = atolwhex(a);
71 INITRND = atolwhex(a);
78 diag("usage: 1l [-options] objects");
81 if(!debug['9'] && !debug['U'] && !debug['B'])
91 if(INITDAT != -1 && INITRND == -1)
95 diag("unknown -H option %ld", HEADTYPE);
98 case 0: /* this is garbage */
101 INITTEXT = 0x40004CL;
103 INITDAT = 0x10000000L;
104 if(INITDAT != 0 && INITRND == -1)
109 case 1: /* plan9 boot data goes into text */
115 if(INITDAT != 0 && INITRND == -1)
126 if(INITDAT != 0 && INITRND == -1)
131 case 3: /* next boot */
132 HEADR = 28+124+192+24;
134 INITTEXT = 0x04002000;
137 if(INITDAT != 0 && INITRND == -1)
142 case 4: /* preprocess pilot */
152 if(INITDAT != 0 && INITRND != 0)
153 print("warning: -D0x%lux is ignored because of -R0x%lux\n",
156 Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n",
157 HEADTYPE, INITTEXT, INITDAT, INITRND);
159 for(i=1; optab[i].as; i++)
160 if(i != optab[i].as) {
161 diag("phase error in optab: %d", i);
169 zprg.from.type = D_NONE;
172 memset(special, 0, sizeof(special));
185 special[D_FPIAR] = 1;
191 special[D_MMUSR] = 1;
194 memset(simple, 0177, sizeof(simple));
197 simple[D_F0+i] = i+0100;
198 simple[D_A0+i] = i+010;
199 simple[D_A0+I_INDIR+i] = i+020;
200 simple[D_A0+I_INDINC+i] = i+030;
201 simple[D_A0+I_INDDEC+i] = i+040;
208 cout = create(outfile, 1, 0775);
210 diag("cannot create %s", outfile);
215 cbc = sizeof(buf.cbuf);
222 INITENTRY = "_mainp";
224 lookup(INITENTRY, 0)->type = SXREF;
226 lookup(INITENTRY, 0)->type = SXREF;
233 firstp = firstp->link;
250 Bprint(&bso, "%5.2f cpu time\n", cputime());
251 Bprint(&bso, "%ld data statements\n", ndata);
252 Bprint(&bso, "%ld symbols\n", nsymbol);
253 Bprint(&bso, "%ld memory used\n", thunk);
254 Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
255 Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
271 for(i=0; i<libraryp; i++) {
273 Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]);
277 for(h=0; h<nelem(hash); h++)
278 for(s = hash[h]; s != S; s = s->link)
298 long off, esym, cnt, l;
302 char name[100], pname[150];
304 char *e, *start, *stop;
306 if(file[0] == '-' && file[1] == 'l') {
307 snprint(name, sizeof name, "/%s/lib/lib%s.a", thestring, file+2);
311 Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
315 diag("cannot open file: %s", file);
318 l = read(f, magbuf, SARMAG);
319 if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
320 /* load it as a regular file */
328 l = read(f, &arhdr, SAR_HDR);
330 diag("%s: short read on archive file symbol header", file);
333 if(strncmp(arhdr.name, symname, strlen(symname))) {
334 diag("%s: first entry not symbol header", file);
338 esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
339 off = SARMAG + SAR_HDR;
342 * just bang the whole symbol file into memory
346 start = malloc(cnt + 10);
347 cnt = read(f, start, cnt);
358 Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
361 for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
365 snprint(pname, sizeof pname, "%s(%s)", file, s->name);
367 Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
370 l |= (e[2] & 0xff) << 8;
371 l |= (e[3] & 0xff) << 16;
372 l |= (e[4] & 0xff) << 24;
374 l = read(f, &arhdr, SAR_HDR);
377 if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
379 l = atolwhex(arhdr.size);
381 if(s->type == SXREF) {
382 diag("%s: failed to load: %s", file, s->name);
392 diag("%s: bad or out of date archive", file);
398 zaddr(uchar *p, Adr *a, Sym *h[])
408 * first try the high-time formats
415 a->offset = p[1] | (p[2]<<8) | (p[3]<<16) | (p[4]<<24);
419 if(t == (T_OFFSET|T_SYM)) {
420 a->offset = p[1] | (p[2]<<8) | (p[3]<<16) | (p[4]<<24);
434 if(t == (T_INDEX|T_OFFSET|T_SYM)) {
435 a->displace = p[4] | (p[5]<<8) | (p[6]<<16) | (p[7]<<24);
436 a->offset = p[8] | (p[9]<<8) | (p[10]<<16) | (p[11]<<24);
445 * now do it the hard way
449 a->field = p[c] | (p[c+1]<<8);
453 a->displace = p[c+3] | (p[c+4]<<8) | (p[c+5]<<16) | (p[c+6]<<24);
457 a->offset = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
465 a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
466 a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
471 for(i=0; i<NSNAME; i++)
477 a->type = p[c] | (p[c+1]<<8);
488 t = a->type & D_MASK;
489 if(t != D_AUTO && t != D_PARAM)
492 for(u=curauto; u; u=u->link) {
501 while(nhunk < sizeof(Auto))
504 nhunk -= sizeof(Auto);
505 hunk += sizeof(Auto);
518 char name[1024], comp[256], *p;
524 if(histfrog[0]->name[1] == '/') {
528 if(histfrog[0]->name[1] == '.') {
529 snprint(name, sizeof name, ".");
533 snprint(name, sizeof name, "/%s/lib", thestring);
535 snprint(name, sizeof name, "/usr/%clib", thechar);
539 for(; i<histfrogp; i++) {
540 snprint(comp, sizeof comp, histfrog[i]->name+1);
542 p = strstr(comp, "$O");
545 memmove(p+1, p+2, strlen(p+2)+1);
549 p = strstr(comp, "$M");
552 if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
553 diag("library component too long");
556 memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
557 memmove(p, thestring, strlen(thestring));
559 if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
560 diag("library component too long");
566 for(i=0; i<libraryp; i++)
567 if(strcmp(name, library[i]) == 0)
569 if(libraryp == nelem(library)){
570 diag("too many autolibs; skipping %s", name);
574 p = malloc(strlen(name) + 1);
576 library[libraryp] = p;
577 p = malloc(strlen(obj) + 1);
579 libraryobj[libraryp] = p;
583 addhist(long line, int type)
589 u = malloc(sizeof(Auto));
590 s = malloc(sizeof(Sym));
591 s->name = malloc(2*(histfrogp+1) + 1);
600 for(i=0; i<histfrogp; i++) {
601 k = histfrog[i]->value;
626 * bad encoding of path components only allows
627 * MAXHIST components. if there is an overflow,
628 * first try to collapse xxx/..
630 for(i=1; i<histfrogp; i++)
631 if(strcmp(histfrog[i]->name+1, "..") == 0) {
632 memmove(histfrog+i-1, histfrog+i+1,
633 (histfrogp-i-1)*sizeof(histfrog[0]));
639 * next try to collapse .
641 for(i=0; i<histfrogp; i++)
642 if(strcmp(histfrog[i]->name+1, ".") == 0) {
643 memmove(histfrog+i, histfrog+i+1,
644 (histfrogp-i-1)*sizeof(histfrog[0]));
649 * last chance, just truncate from front
651 memmove(histfrog+0, histfrog+1,
652 (histfrogp-1)*sizeof(histfrog[0]));
655 histfrog[histfrogp-1] = s;
659 readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
664 memmove(buf, good, stop - good);
669 n = read(f, stop, n);
676 ldobj(int f, long c, char *pn)
683 uchar *bloc, *bsize, *stop;
689 memset(h, 0, sizeof(h));
698 if(r < 100 && r < c) { /* enough for largest prog */
699 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
705 o = bloc[0] | (bloc[1] << 8);
706 if(o <= AXXX || o >= ALAST) {
709 diag("%s: opcode out of range %d", pn, o);
710 print(" probably not a .%c file\n", thechar);
714 if(o == ANAME || o == ASIGNAME) {
719 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
721 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
725 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
727 fprint(2, "%s: name too long\n", pn);
731 v = bloc[2]; /* type */
732 o = bloc[3]; /* sym */
739 s = lookup((char*)bloc, r);
740 c -= &stop[1] - bloc;
744 print(" ANAME %s\n", s->name);
746 if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
749 if(s->type != SFILE) {
754 if(histfrogp < MAXHIST) {
755 histfrog[histfrogp] = s;
763 while(nhunk < sizeof(Prog))
766 nhunk -= sizeof(Prog);
767 hunk += sizeof(Prog);
770 p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
772 r = zaddr(bloc+6, &p->from, h) + 6;
773 r += zaddr(bloc+r, &p->to, h);
782 if(p->to.offset == -1) {
787 addhist(p->line, D_FILE); /* 'z' */
789 addhist(p->to.offset, D_FILE1); /* 'Z' */
796 curtext->to.autom = curauto;
805 if(s->type == 0 || s->type == SXREF) {
809 if(s->type != SBSS) {
810 diag("%s: redefinition: %s in %s",
815 if(p->to.offset > s->value)
816 s->value = p->to.offset;
826 diag("%s: unknown opcode in %s", pn, TNAME);
833 curtext->to.autom = curauto;
841 if(s->type != 0 && s->type != SXREF)
842 diag("%s: redefinition: %s", pn, s->name);
860 if(p->to.type != D_EXTERN && p->to.type != D_STATIC)
867 if(p->from.type != D_CONST)
870 if(lv >= -128 && lv < 128)
871 if(p->to.type >= D_R0 && p->to.type < D_R0+8) {
872 p->from.type = D_QUICK;
876 if(lv >= -0x7fff && lv <= 0x7fff)
877 if(p->to.type >= D_A0 && p->to.type < D_A0+8)
885 if(p->from.type != D_CONST)
902 p->from.type = D_QUICK;
908 if(p->from.type != D_CONST)
925 p->from.type = D_QUICK;
947 if(p->from.type == D_CONST)
948 if(p->from.offset > 0)
949 if(p->from.offset <= 8)
950 p->from.type = D_QUICK;
954 if(p->to.type >= D_A0 && p->to.type < D_A0+8) {
957 p->to.type = D_CONST;
963 if(p->to.type != D_CONST)
966 if(lv >= -0x7fff && lv <= 0x7fff)
967 if(p->from.type >= D_A0 && p->from.type < D_A0+8)
972 if(p->to.type >= D_A0 && p->to.type < D_A0+8) {
974 p->from.type = D_CONST;
981 if(p->from.type == D_FCONST)
982 if(optab[p->as].fas != AXXX) {
983 dv = ieeedtod(&p->from.ieee);
984 if(dv >= -(1L<<30) && dv <= (1L<<30)) {
987 p->as = optab[p->as].fas;
988 p->from.type = D_CONST;
990 p->from.displace = 0;
994 if(p->to.type == D_BRANCH)
1005 diag("%s: truncated object file in %s", pn, TNAME);
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 + 1);
1035 memmove(s->name, symb, l);
1051 while(nhunk < sizeof(Prog))
1054 nhunk -= sizeof(Prog);
1055 hunk += sizeof(Prog);
1069 q->stkoff = p->stkoff;
1092 if(thunk >= 5L*NHUNK) {
1094 if(thunk >= 25L*NHUNK)
1098 if(h == (char*)-1) {
1099 diag("out of memory");
1115 Bprint(&bso, "%5.2f profile 1\n", cputime());
1117 s = lookup("__mcount", 0);
1119 for(p = firstp->link; p != P; p = p->link) {
1120 if(p->as == ATEXT) {
1127 q->from.type = D_CONST;
1129 q->to.type = D_EXTERN;
1131 q->to.offset = n*4 + 4;
1138 q->from.type = D_EXTERN;
1140 q->from.offset = n*4;
1141 q->from.displace = 4;
1142 q->to.type = D_EXTERN;
1143 q->to.sym = p->from.sym;
1153 q->from.type = D_EXTERN;
1155 q->from.displace = 4;
1156 q->to.type = D_CONST;
1166 Prog *p, *q, *q2, *ps2, *ps4;
1169 Bprint(&bso, "%5.2f profile 2\n", cputime());
1173 s2 = lookup("_tracein", 0);
1174 s4 = lookup("_traceout", 0);
1176 s2 = lookup("_profin", 0);
1177 s4 = lookup("_profout", 0);
1179 if(s2->type != STEXT || s4->type != STEXT) {
1181 diag("_tracein/_traceout not defined %d %d", s2->type, s4->type);
1183 diag("_profin/_profout not defined");
1189 for(p = firstp; p != P; p = p->link) {
1190 if(p->as == ATEXT) {
1191 if(p->from.sym == s2) {
1193 p->from.displace = 1;
1195 if(p->from.sym == s4) {
1197 p->from.displace = 1;
1201 for(p = firstp; p != P; p = p->link) {
1202 if(p->as == ATEXT) {
1203 if(p->from.displace != 0) {
1219 if(debug['e']){ /* embedded tracing */
1228 q2->to.type = D_BRANCH;
1229 q2->to.sym = p->to.sym;
1230 q2->pcond = q->link;
1235 p->to.type = D_BRANCH;
1245 if(debug['e']){ /* embedded tracing */
1267 p->from = zprg.from;
1269 p->to.type = D_BRANCH;
1281 reuse(Prog *r, Sym *s)
1288 for(p = datap; p != r; p = p->link)
1290 return p->from.offset;
1299 for(i=0; i<4; i++) {
1300 c = find1(0x01020304L, i+1);
1308 c = find2(0x01020304L, i+1);
1313 Bprint(&bso, "inuxi = ");
1315 Bprint(&bso, "%d", inuxi1[i]);
1318 Bprint(&bso, "%d", inuxi2[i]);
1321 Bprint(&bso, "%d", inuxi4[i]);
1322 Bprint(&bso, "\n[fg]nuxi = ");
1324 Bprint(&bso, "%d", fnuxi8[i]);
1327 Bprint(&bso, "%d", gnuxi8[i]);
1334 find1(long l, int c)
1347 find2(long l, int c)
1353 for(i=0; i<4; i+=2) {
1354 if(((*p >> 8) & 0xff) == c)
1356 if((*p++ & 0xff) == c)
1370 exp = (e->h>>20) & ((1L<<11)-1L);
1371 exp -= (1L<<10) - 2L;
1372 v = (e->h & 0xfffffL) << 3;
1373 v |= (e->l >> 29) & 0x7L;
1374 if((e->l >> 28) & 1) {
1377 v = (v & 0x7fffffL) >> 1;
1381 if(exp <= -126 || exp >= 130)
1382 diag("double fp to single fp overflow");
1383 v |= ((exp + 126) & 0xffL) << 23;
1384 v |= e->h & 0x80000000L;
1389 ieeedtod(Ieee *ieeep)
1395 if(ieeep->h & (1L<<31)) {
1396 e.h = ieeep->h & ~(1L<<31);
1398 return -ieeedtod(&e);
1400 if(ieeep->l == 0 && ieeep->h == 0)
1402 fr = ieeep->l & ((1L<<16)-1L);
1404 fr += (ieeep->l>>16) & ((1L<<16)-1L);
1406 fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
1408 exp = (ieeep->h>>20) & ((1L<<11)-1L);
1409 exp -= (1L<<10) - 2L;
1410 return ldexp(fr, exp);