9 char *noname = "<none>";
10 char symname[] = SYMDEF;
12 char *thestring = "amd64";
13 char *paramspace = "FP";
16 * -H2 -T0x200028 -R0x200000 is plan9 format (was -T4136 -R4096)
17 * -H3 -T4128 -R4096 is plan9 32-bit format
18 * -H5 -T0x80110000 -R4096 is ELF32
20 * options used: 189BLQSWabcjlnpsvz
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: 6l [-options] objects");
113 if(!debug['9'] && !debug['U'] && !debug['B'])
123 diag("unknown -H option");
128 INITTEXT = 0x200000+HEADR;
137 INITTEXT = 4096+HEADR;
143 case 5: /* elf32 executable */
144 HEADR = rnd(52L+3*32L, 16);
146 INITTEXT = 0xf0110000L;
153 if(INITDAT != 0 && INITRND != 0)
154 print("warning: -D0x%llux is ignored because of -R0x%lux\n",
157 Bprint(&bso, "HEADER = -H%ld -T0x%llux -D0x%llux -R0x%lux\n",
158 HEADTYPE, INITTEXT, INITDAT, INITRND);
160 for(i=1; optab[i].as; i++) {
162 if(opindex[c] != nil) {
163 diag("phase error in optab: %d (%A)", i, c);
166 opindex[c] = &optab[i];
169 for(i=0; i<Ymax; i++)
170 ycover[i*Ymax + i] = 1;
172 ycover[Yi0*Ymax + Yi8] = 1;
173 ycover[Yi1*Ymax + Yi8] = 1;
175 ycover[Yi0*Ymax + Ys32] = 1;
176 ycover[Yi1*Ymax + Ys32] = 1;
177 ycover[Yi8*Ymax + Ys32] = 1;
179 ycover[Yi0*Ymax + Yi32] = 1;
180 ycover[Yi1*Ymax + Yi32] = 1;
181 ycover[Yi8*Ymax + Yi32] = 1;
182 ycover[Ys32*Ymax + Yi32] = 1;
184 ycover[Yi0*Ymax + Yi64] = 1;
185 ycover[Yi1*Ymax + Yi64] = 1;
186 ycover[Yi8*Ymax + Yi64] = 1;
187 ycover[Ys32*Ymax + Yi64] = 1;
188 ycover[Yi32*Ymax + Yi64] = 1;
190 ycover[Yal*Ymax + Yrb] = 1;
191 ycover[Ycl*Ymax + Yrb] = 1;
192 ycover[Yax*Ymax + Yrb] = 1;
193 ycover[Ycx*Ymax + Yrb] = 1;
194 ycover[Yrx*Ymax + Yrb] = 1;
195 ycover[Yrl*Ymax + Yrb] = 1;
197 ycover[Ycl*Ymax + Ycx] = 1;
199 ycover[Yax*Ymax + Yrx] = 1;
200 ycover[Ycx*Ymax + Yrx] = 1;
202 ycover[Yax*Ymax + Yrl] = 1;
203 ycover[Ycx*Ymax + Yrl] = 1;
204 ycover[Yrx*Ymax + Yrl] = 1;
206 ycover[Yf0*Ymax + Yrf] = 1;
208 ycover[Yal*Ymax + Ymb] = 1;
209 ycover[Ycl*Ymax + Ymb] = 1;
210 ycover[Yax*Ymax + Ymb] = 1;
211 ycover[Ycx*Ymax + Ymb] = 1;
212 ycover[Yrx*Ymax + Ymb] = 1;
213 ycover[Yrb*Ymax + Ymb] = 1;
214 ycover[Yrl*Ymax + Ymb] = 1;
215 ycover[Ym*Ymax + Ymb] = 1;
217 ycover[Yax*Ymax + Yml] = 1;
218 ycover[Ycx*Ymax + Yml] = 1;
219 ycover[Yrx*Ymax + Yml] = 1;
220 ycover[Yrl*Ymax + Yml] = 1;
221 ycover[Ym*Ymax + Yml] = 1;
223 ycover[Yax*Ymax + Ymm] = 1;
224 ycover[Ycx*Ymax + Ymm] = 1;
225 ycover[Yrx*Ymax + Ymm] = 1;
226 ycover[Yrl*Ymax + Ymm] = 1;
227 ycover[Ym*Ymax + Ymm] = 1;
228 ycover[Ymr*Ymax + Ymm] = 1;
230 ycover[Yax*Ymax + Yxm] = 1;
231 ycover[Ycx*Ymax + Yxm] = 1;
232 ycover[Yrx*Ymax + Yxm] = 1;
233 ycover[Yrl*Ymax + Yxm] = 1;
234 ycover[Ym*Ymax + Yxm] = 1;
235 ycover[Yxr*Ymax + Yxm] = 1;
237 for(i=0; i<D_NONE; i++) {
239 if(i >= D_AL && i <= D_R15B) {
240 reg[i] = (i-D_AL) & 7;
241 if(i >= D_SPB && i <= D_DIB)
243 if(i >= D_R8B && i <= D_R15B)
244 regrex[i] = Rxr | Rxx | Rxb;
246 if(i >= D_AH && i<= D_BH)
247 reg[i] = 4 + ((i-D_AH) & 7);
248 if(i >= D_AX && i <= D_R15) {
249 reg[i] = (i-D_AX) & 7;
251 regrex[i] = Rxr | Rxx | Rxb;
253 if(i >= D_F0 && i <= D_F0+7)
254 reg[i] = (i-D_F0) & 7;
255 if(i >= D_M0 && i <= D_M0+7)
256 reg[i] = (i-D_M0) & 7;
257 if(i >= D_X0 && i <= D_X0+15) {
258 reg[i] = (i-D_X0) & 7;
260 regrex[i] = Rxr | Rxx | Rxb;
262 if(i >= D_CR+8 && i <= D_CR+15)
270 zprg.from.type = D_NONE;
271 zprg.from.index = D_NONE;
284 cout = create(outfile, 1, 0775);
286 diag("cannot create %s", outfile);
291 cbc = sizeof(buf.cbuf);
298 INITENTRY = "_mainp";
300 lookup(INITENTRY, 0)->type = SXREF;
301 } else if(!(*INITENTRY >= '0' && *INITENTRY <= '9'))
302 lookup(INITENTRY, 0)->type = SXREF;
308 firstp = firstp->link;
312 EXPTAB = "_exporttab";
331 paramspace = "SP"; /* (FP) now (SP) on output */
342 Bprint(&bso, "%5.2f cpu time\n", cputime());
343 Bprint(&bso, "%ld symbols\n", nsymbol);
344 Bprint(&bso, "%ld memory used\n", thunk);
345 Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
346 Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
362 for(i=0; i<libraryp; i++) {
364 Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]);
368 for(h=0; h<nelem(hash); h++)
369 for(s = hash[h]; s != S; s = s->link)
389 long off, esym, cnt, l;
393 char name[100], pname[150];
395 char *e, *start, *stop;
397 if(file[0] == '-' && file[1] == 'l') {
399 sprint(name, "/%s/lib/lib", thestring);
401 sprint(name, "/usr/%clib/lib", thechar);
402 strcat(name, file+2);
407 Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
411 diag("cannot open file: %s", file);
414 l = read(f, magbuf, SARMAG);
415 if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
416 /* load it as a regular file */
424 l = read(f, &arhdr, SAR_HDR);
426 diag("%s: short read on archive file symbol header", file);
429 if(strncmp(arhdr.name, symname, strlen(symname))) {
430 diag("%s: first entry not symbol header", file);
434 esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
435 off = SARMAG + SAR_HDR;
438 * just bang the whole symbol file into memory
442 start = malloc(cnt + 10);
443 cnt = read(f, start, cnt);
454 Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
457 for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
461 sprint(pname, "%s(%s)", file, s->name);
463 Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
466 l |= (e[2] & 0xff) << 8;
467 l |= (e[3] & 0xff) << 16;
468 l |= (e[4] & 0xff) << 24;
470 l = read(f, &arhdr, SAR_HDR);
473 if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
475 l = atolwhex(arhdr.size);
477 if(s->type == SXREF) {
478 diag("%s: failed to load: %s", file, s->name);
488 diag("%s: bad or out of date archive", file);
494 zaddr(uchar *p, Adr *a, Sym *h[])
514 * Hack until Charles fixes the compiler.
515 a->offset = (long)(p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24));
517 l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
521 l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
522 a->offset = ((vlong)l<<32) | (a->offset & 0xFFFFFFFFUL);
533 a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
534 a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
539 for(i=0; i<NSNAME; i++)
553 if(t != D_AUTO && t != D_PARAM)
556 for(u=curauto; u; u=u->link) {
565 while(nhunk < sizeof(Auto))
568 nhunk -= sizeof(Auto);
569 hunk += sizeof(Auto);
582 char name[1024], comp[256], *p;
588 if(histfrog[0]->name[1] == '/') {
592 if(histfrog[0]->name[1] == '.') {
597 sprint(name, "/%s/lib", thestring);
599 sprint(name, "/usr/%clib", thechar);
603 for(; i<histfrogp; i++) {
604 snprint(comp, sizeof comp, histfrog[i]->name+1);
606 p = strstr(comp, "$O");
609 memmove(p+1, p+2, strlen(p+2)+1);
613 p = strstr(comp, "$M");
616 if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
617 diag("library component too long");
620 memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
621 memmove(p, thestring, strlen(thestring));
623 if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
624 diag("library component too long");
630 for(i=0; i<libraryp; i++)
631 if(strcmp(name, library[i]) == 0)
633 if(libraryp == nelem(library)){
634 diag("too many autolibs; skipping %s", name);
638 p = malloc(strlen(name) + 1);
640 library[libraryp] = p;
641 p = malloc(strlen(obj) + 1);
643 libraryobj[libraryp] = p;
648 addhist(long line, int type)
654 u = malloc(sizeof(Auto));
655 s = malloc(sizeof(Sym));
656 s->name = malloc(2*(histfrogp+1) + 1);
665 for(i=0; i<histfrogp; i++) {
666 k = histfrog[i]->value;
691 * bad encoding of path components only allows
692 * MAXHIST components. if there is an overflow,
693 * first try to collapse xxx/..
695 for(i=1; i<histfrogp; i++)
696 if(strcmp(histfrog[i]->name+1, "..") == 0) {
697 memmove(histfrog+i-1, histfrog+i+1,
698 (histfrogp-i-1)*sizeof(histfrog[0]));
704 * next try to collapse .
706 for(i=0; i<histfrogp; i++)
707 if(strcmp(histfrog[i]->name+1, ".") == 0) {
708 memmove(histfrog+i, histfrog+i+1,
709 (histfrogp-i-1)*sizeof(histfrog[0]));
714 * last chance, just truncate from front
716 memmove(histfrog+0, histfrog+1,
717 (histfrogp-1)*sizeof(histfrog[0]));
720 histfrog[histfrogp-1] = s;
727 p->from.type = D_NONE;
732 readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
737 memmove(buf, good, stop - good);
742 n = read(f, stop, n);
749 ldobj(int f, long c, char *pn)
753 uchar *bloc, *bsize, *stop;
754 int v, o, r, skip, mode;
755 Sym *h[NSYM], *s, *di;
762 nfilen = malloc((files+16)*sizeof(char*));
763 memmove(nfilen, filen, files*sizeof(char*));
767 filen[files++] = strdup(pn);
774 memset(h, 0, sizeof(h));
785 if(r < 100 && r < c) { /* enough for largest prog */
786 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
792 o = bloc[0] | (bloc[1] << 8);
793 if(o <= AXXX || o >= ALAST) {
796 diag("%s: opcode out of range %d", pn, o);
797 print(" probably not a .6 file\n");
801 if(o == ANAME || o == ASIGNAME) {
804 sig = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24);
808 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
810 bsize = readsome(f, buf.xbuf, bloc, bsize, c);
814 stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
816 fprint(2, "%s: name too long\n", pn);
820 v = bloc[2]; /* type */
821 o = bloc[3]; /* sym */
828 s = lookup((char*)bloc, r);
829 c -= &stop[1] - bloc;
832 if(debug['S'] && r == 0)
835 if(s->sig != 0 && s->sig != sig)
836 diag("incompatible type signatures %lux(%s) and %lux(%s) for %s", s->sig, filen[s->file], sig, pn, s->name);
842 print(" ANAME %s\n", s->name);
844 if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
847 if(s->type != SFILE) {
852 if(histfrogp < MAXHIST) {
853 histfrog[histfrogp] = s;
861 while(nhunk < sizeof(Prog))
864 nhunk -= sizeof(Prog);
865 hunk += sizeof(Prog);
868 p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
871 r = zaddr(bloc+6, &p->from, h) + 6;
872 r += zaddr(bloc+r, &p->to, h);
881 if(p->to.offset == -1) {
886 addhist(p->line, D_FILE); /* 'z' */
888 addhist(p->to.offset, D_FILE1); /* 'Z' */
895 curtext->to.autom = curauto;
904 if(s->type == 0 || s->type == SXREF) {
908 if(s->type != SBSS) {
909 diag("%s: redefinition: %s in %s",
914 if(p->to.offset > s->value)
915 s->value = p->to.offset;
920 diag("DYNT without a sym\n%P", p);
925 if(di->type == SXREF) {
927 Bprint(&bso, "%P set to %d\n", p, dtype);
935 p->from.offset = di->value;
936 p->from.sym->type = SDATA;
938 diag("DYNT not in text: %P", p);
941 p->to.sym = curtext->from.sym;
943 p->to.index = D_EXTERN;
947 if(p->from.sym == S) {
948 diag("INIT without a sym\n%P", p);
952 diag("INIT without previous DYNT\n%P", p);
955 p->from.offset = di->value;
956 p->from.sym->type = SDATA;
970 diag("%s: GOK opcode in %s", pn, TNAME);
977 curtext->to.autom = curauto;
984 diag("%s: no TEXT symbol: %P", pn, p);
987 if(s->type != 0 && s->type != SXREF) {
988 if(p->from.scale & DUPOK) {
992 diag("%s: redefinition: %s\n%P", pn, s->name, p);
1010 if(p->from.type == D_CONST || p->from.type == D_INDIR+D_NONE){
1011 switch((int)p->from.offset){
1012 case 16: case 32: case 64:
1013 mode = p->from.offset;
1037 if(p->from.type == D_FCONST) {
1039 sprint(literal, "$%lux", ieeedtof(&p->from.ieee));
1040 s = lookup(literal, 0);
1047 t->from.type = D_EXTERN;
1058 p->from.type = D_EXTERN;
1082 if(p->from.type == D_FCONST) {
1083 /* size sb 18 max */
1084 sprint(literal, "$%lux.%lux",
1085 p->from.ieee.l, p->from.ieee.h);
1086 s = lookup(literal, 0);
1093 t->from.type = D_EXTERN;
1104 p->from.type = D_EXTERN;
1115 if(p->to.type == D_BRANCH)
1116 p->to.offset += ipc;
1126 diag("truncated object file: %s", pn);
1130 lookup(char *symb, int v)
1138 for(p=symb; c = *p; p++)
1144 for(s = hash[h]; s != S; s = s->link)
1146 if(memcmp(s->name, symb, l) == 0)
1149 while(nhunk < sizeof(Sym))
1152 nhunk -= sizeof(Sym);
1153 hunk += sizeof(Sym);
1155 s->name = malloc(l + 1);
1156 memmove(s->name, symb, l);
1173 while(nhunk < sizeof(Prog))
1176 nhunk -= sizeof(Prog);
1177 hunk += sizeof(Prog);
1213 if(thunk >= 5L*NHUNK) {
1215 if(thunk >= 25L*NHUNK)
1219 if(h == (char*)-1) {
1220 diag("out of memory");
1236 Bprint(&bso, "%5.2f profile 1\n", cputime());
1238 s = lookup("__mcount", 0);
1240 for(p = firstp->link; p != P; p = p->link) {
1241 if(p->as == ATEXT) {
1247 q->from.type = D_EXTERN;
1248 q->from.offset = n*4;
1252 q->to.type = D_CONST;
1261 p->from.type = D_CONST;
1263 p->to.type = D_EXTERN;
1265 p->to.offset = n*4 + 4;
1277 q->from.type = D_EXTERN;
1280 q->to.type = D_CONST;
1291 Prog *p, *q, *q2, *ps2, *ps4;
1294 Bprint(&bso, "%5.2f profile 2\n", cputime());
1298 s2 = lookup("_tracein", 0);
1299 s4 = lookup("_traceout", 0);
1301 s2 = lookup("_profin", 0);
1302 s4 = lookup("_profout", 0);
1304 if(s2->type != STEXT || s4->type != STEXT) {
1306 diag("_tracein/_traceout not defined %d %d", s2->type, s4->type);
1308 diag("_profin/_profout not defined");
1314 for(p = firstp; p != P; p = p->link) {
1315 if(p->as == ATEXT) {
1316 if(p->from.sym == s2) {
1320 if(p->from.sym == s4) {
1326 for(p = firstp; p != P; p = p->link) {
1327 if(p->as == ATEXT) {
1330 if(p->from.scale & NOPROF) { /* dont profile */
1349 if(debug['e']){ /* embedded tracing */
1358 q2->to.type = D_BRANCH;
1359 q2->to.sym = p->to.sym;
1360 q2->pcond = q->link;
1365 p->to.type = D_BRANCH;
1375 if(debug['e']){ /* embedded tracing */
1397 p->from = zprg.from;
1399 p->to.type = D_BRANCH;
1415 for(i=0; i<4; i++) {
1416 c = find1(0x04030201L, i+1);
1429 Bprint(&bso, "inuxi = ");
1431 Bprint(&bso, "%d", inuxi1[i]);
1434 Bprint(&bso, "%d", inuxi2[i]);
1437 Bprint(&bso, "%d", inuxi4[i]);
1440 Bprint(&bso, "%d", inuxi8[i]);
1441 Bprint(&bso, "\nfnuxi = ");
1443 Bprint(&bso, "%d", fnuxi4[i]);
1446 Bprint(&bso, "%d", fnuxi8[i]);
1453 find1(long l, int c)
1466 find2(long l, int c)
1472 for(i=0; i<4; i+=2) {
1473 if(((*p >> 8) & 0xff) == c)
1475 if((*p++ & 0xff) == c)
1489 exp = (e->h>>20) & ((1L<<11)-1L);
1490 exp -= (1L<<10) - 2L;
1491 v = (e->h & 0xfffffL) << 3;
1492 v |= (e->l >> 29) & 0x7L;
1493 if((e->l >> 28) & 1) {
1496 v = (v & 0x7fffffL) >> 1;
1500 if(exp <= -126 || exp >= 130)
1501 diag("double fp to single fp overflow");
1502 v |= ((exp + 126) & 0xffL) << 23;
1503 v |= e->h & 0x80000000L;
1508 ieeedtod(Ieee *ieeep)
1514 if(ieeep->h & (1L<<31)) {
1515 e.h = ieeep->h & ~(1L<<31);
1517 return -ieeedtod(&e);
1519 if(ieeep->l == 0 && ieeep->h == 0)
1521 fr = ieeep->l & ((1L<<16)-1L);
1523 fr += (ieeep->l>>16) & ((1L<<16)-1L);
1525 fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
1527 exp = (ieeep->h>>20) & ((1L<<11)-1L);
1528 exp -= (1L<<10) - 2L;
1529 return ldexp(fr, exp);
1539 diag("value != 0 on SXREF");
1541 diag("import index %d out of range", n);
1542 s->value = n<<Roffset;
1557 readundefs(char *f, int t)
1562 char *l, buf[256], *fields[64];
1566 b = Bopen(f, OREAD);
1568 diag("could not open %s: %r", f);
1571 while((l = Brdline(b, '\n')) != nil){
1573 if(n >= sizeof(buf)){
1574 diag("%s: line too long", f);
1579 n = getfields(buf, fields, nelem(fields), 1, " \t\r\n");
1580 if(n == nelem(fields)){
1581 diag("%s: bad format", f);
1584 for(i = 0; i < n; i++){
1585 s = lookup(fields[i], 0);