xref: /plan9-contrib/sys/src/cmd/6l/obj.c (revision 40d015479ed36701ae6dcfd8814f849fc6285e8d)
1 #define	EXTERN
2 #include	"l.h"
3 #include	<ar.h>
4 
5 #ifndef	DEFAULT
6 #define	DEFAULT	'9'
7 #endif
8 
9 char	*noname		= "<none>";
10 char	symname[]	= SYMDEF;
11 char	thechar		= '6';
12 char	*thestring 	= "amd64";
13 char	*paramspace	= "FP";
14 
15 char**	libdir;
16 int	nlibdir	= 0;
17 static	int	maxlibdir = 0;
18 
19 /*
20  *	-H2 -T0x200028 -R0x200000	is plan9 format (was -T4136 -R4096)
21  *	-H5 -T0x80110000 -R4096		is ELF32
22  *	-H6 -T0x2000e8 -R0x200000	is ELF64
23  *
24  *	options used: 189BLPQSVWabcjlnpsvz
25  */
26 
27 void
usage(void)28 usage(void)
29 {
30 	diag("usage: %s [-options] objects", argv0);
31 	errorexit();
32 }
33 
34 static int
isobjfile(char * f)35 isobjfile(char *f)
36 {
37 	int n, v;
38 	Biobuf *b;
39 	char buf1[5], buf2[SARMAG];
40 
41 	b = Bopen(f, OREAD);
42 	if(b == nil)
43 		return 0;
44 	n = Bread(b, buf1, 5);
45 	if(n == 5 && (buf1[2] == 1 && buf1[3] == '<' || buf1[3] == 1 && buf1[4] == '<'))
46 		v = 1;	/* good enough for our purposes */
47 	else{
48 		Bseek(b, 0, 0);
49 		n = Bread(b, buf2, SARMAG);
50 		v = n == SARMAG && strncmp(buf2, ARMAG, SARMAG) == 0;
51 	}
52 	Bterm(b);
53 	return v;
54 }
55 
56 void
main(int argc,char * argv[])57 main(int argc, char *argv[])
58 {
59 	int i, c;
60 	char *a;
61 	char name[LIBNAMELEN];
62 
63 	Binit(&bso, 1, OWRITE);
64 	cout = -1;
65 	listinit();
66 	memset(debug, 0, sizeof(debug));
67 	nerrors = 0;
68 	outfile = "6.out";
69 	HEADTYPE = -1;
70 	INITTEXT = -1;
71 	INITTEXTP = -1;
72 	INITDAT = -1;
73 	INITRND = -1;
74 	INITENTRY = 0;
75 	ARGBEGIN {
76 	default:
77 		c = ARGC();
78 		if(c >= 0 && c < sizeof(debug))
79 			debug[c]++;
80 		break;
81 	case 'o': /* output to (next arg) */
82 		outfile = ARGF();
83 		break;
84 	case 'E':
85 		a = ARGF();
86 		if(a)
87 			INITENTRY = a;
88 		break;
89 	case 'H':
90 		a = ARGF();
91 		if(a)
92 			HEADTYPE = atolwhex(a);
93 		break;
94 	case 'L':
95 		addlibpath(EARGF(usage()));
96 		break;
97 	case 'T':
98 		a = ARGF();
99 		if(a)
100 			INITTEXT = atolwhex(a);
101 		break;
102 	case 'P':
103 		a = ARGF();
104 		if(a)
105 			INITTEXTP = atolwhex(a);
106 		break;
107 	case 'D':
108 		a = ARGF();
109 		if(a)
110 			INITDAT = atolwhex(a);
111 		break;
112 	case 'R':
113 		a = ARGF();
114 		if(a)
115 			INITRND = atolwhex(a);
116 		break;
117 	case 'x':	/* produce export table */
118 		doexp = 1;
119 		if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1]))
120 			readundefs(ARGF(), SEXPORT);
121 		break;
122 	case 'u':	/* produce dynamically loadable module */
123 		dlm = 1;
124 		debug['l']++;
125 		if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1]))
126 			readundefs(ARGF(), SIMPORT);
127 		break;
128 	} ARGEND
129 	USED(argc);
130 	if(*argv == nil)
131 		usage();
132 	if(!debug['9'] && !debug['U'] && !debug['B'])
133 		debug[DEFAULT] = 1;
134 	a = getenv("ccroot");
135 	if(a != nil && *a != '\0') {
136 		if(!fileexists(a)) {
137 			diag("nonexistent $ccroot: %s", a);
138 			errorexit();
139 		}
140 	}else
141 		a = "";
142 	snprint(name, sizeof(name), "%s/%s/lib", a, thestring);
143 	addlibpath(name);
144 	if(HEADTYPE == -1) {
145 		if(debug['B'])
146 			HEADTYPE = 2;
147 		if(debug['9'])
148 			HEADTYPE = 2;
149 	}
150 	switch(HEADTYPE) {
151 	default:
152 		diag("unknown -H option");
153 		errorexit();
154 	case 2:	/* plan 9 */
155 		HEADR = 32L+8L;
156 		if(INITTEXT == -1)
157 			INITTEXT = 0x200000+HEADR;
158 		if(INITDAT == -1)
159 			INITDAT = 0;
160 		if(INITRND == -1)
161 			INITRND = 0x200000;
162 		break;
163 	case 5:	/* elf32 executable */
164 		HEADR = rnd(Ehdr32sz+3*Phdr32sz, 16);
165 		if(INITTEXT == -1)
166 			INITTEXT = 0xf0110000L;
167 		if(INITDAT == -1)
168 			INITDAT = 0;
169 		if(INITRND == -1)
170 			INITRND = 4096;
171 		break;
172 	case 6:	/* ELF64 executable */
173 		HEADR = rnd(Ehdr64sz+3*Phdr64sz, 16);
174 		if(INITTEXT == -1)
175 			INITTEXT = 0x200000+HEADR;
176 		if(INITDAT == -1)
177 			INITDAT = 0;
178 		if(INITRND == -1)
179 			INITRND = 0x200000;
180 		break;
181 	}
182 	if (INITTEXTP == -1)
183 		INITTEXTP = INITTEXT;
184 	if(INITDAT != 0 && INITRND != 0)
185 		print("warning: -D0x%llux is ignored because of -R0x%lux\n",
186 			INITDAT, INITRND);
187 	if(debug['v'])
188 		Bprint(&bso, "HEADER = -H%ld -T0x%llux -D0x%llux -R0x%lux\n",
189 			HEADTYPE, INITTEXT, INITDAT, INITRND);
190 	Bflush(&bso);
191 	for(i=1; optab[i].as; i++) {
192 		c = optab[i].as;
193 		if(opindex[c] != nil) {
194 			diag("phase error in optab: %d (%A)", i, c);
195 			errorexit();
196 		}
197 		opindex[c] = &optab[i];
198 	}
199 
200 	for(i=0; i<Ymax; i++)
201 		ycover[i*Ymax + i] = 1;
202 
203 	ycover[Yi0*Ymax + Yi8] = 1;
204 	ycover[Yi1*Ymax + Yi8] = 1;
205 
206 	ycover[Yi0*Ymax + Ys32] = 1;
207 	ycover[Yi1*Ymax + Ys32] = 1;
208 	ycover[Yi8*Ymax + Ys32] = 1;
209 
210 	ycover[Yi0*Ymax + Yi32] = 1;
211 	ycover[Yi1*Ymax + Yi32] = 1;
212 	ycover[Yi8*Ymax + Yi32] = 1;
213 	ycover[Ys32*Ymax + Yi32] = 1;
214 
215 	ycover[Yi0*Ymax + Yi64] = 1;
216 	ycover[Yi1*Ymax + Yi64] = 1;
217 	ycover[Yi8*Ymax + Yi64] = 1;
218 	ycover[Ys32*Ymax + Yi64] = 1;
219 	ycover[Yi32*Ymax + Yi64] = 1;
220 
221 	ycover[Yal*Ymax + Yrb] = 1;
222 	ycover[Ycl*Ymax + Yrb] = 1;
223 	ycover[Yax*Ymax + Yrb] = 1;
224 	ycover[Ycx*Ymax + Yrb] = 1;
225 	ycover[Yrx*Ymax + Yrb] = 1;
226 	ycover[Yrl*Ymax + Yrb] = 1;	// 8l disables this
227 	ycover[Ycl*Ymax + Ycx] = 1;	// 8l disables this
228 
229 	ycover[Yax*Ymax + Yrx] = 1;
230 	ycover[Ycx*Ymax + Yrx] = 1;
231 
232 	ycover[Yax*Ymax + Yrl] = 1;
233 	ycover[Ycx*Ymax + Yrl] = 1;
234 	ycover[Yrx*Ymax + Yrl] = 1;
235 
236 	ycover[Yf0*Ymax + Yrf] = 1;
237 
238 	ycover[Yal*Ymax + Ymb] = 1;
239 	ycover[Ycl*Ymax + Ymb] = 1;
240 	ycover[Yax*Ymax + Ymb] = 1;
241 	ycover[Ycx*Ymax + Ymb] = 1;
242 	ycover[Yrx*Ymax + Ymb] = 1;
243 	ycover[Yrb*Ymax + Ymb] = 1;
244 	ycover[Yrl*Ymax + Ymb] = 1;	// 8l disables this
245 	ycover[Ym*Ymax + Ymb] = 1;
246 
247 	ycover[Yax*Ymax + Yml] = 1;
248 	ycover[Ycx*Ymax + Yml] = 1;
249 	ycover[Yrx*Ymax + Yml] = 1;
250 	ycover[Yrl*Ymax + Yml] = 1;
251 	ycover[Ym*Ymax + Yml] = 1;
252 
253 	ycover[Yax*Ymax + Ymm] = 1;
254 	ycover[Ycx*Ymax + Ymm] = 1;
255 	ycover[Yrx*Ymax + Ymm] = 1;
256 	ycover[Yrl*Ymax + Ymm] = 1;
257 	ycover[Ym*Ymax + Ymm] = 1;
258 	ycover[Ymr*Ymax + Ymm] = 1;
259 
260 	ycover[Yax*Ymax + Yxm] = 1;
261 	ycover[Ycx*Ymax + Yxm] = 1;
262 	ycover[Yrx*Ymax + Yxm] = 1;
263 	ycover[Yrl*Ymax + Yxm] = 1;
264 	ycover[Ym*Ymax + Yxm] = 1;
265 	ycover[Yxr*Ymax + Yxm] = 1;
266 	ycover[Yxr*Ymax + Yxyr] = 1;
267 
268 	ycover[Yyr*Ymax + Yxm] = 1;
269 	ycover[Yyr*Ymax + Yxyr] = 1;
270 
271 	for(i=0; i<D_NONE; i++) {
272 		reg[i] = -1;
273 		if(i >= D_AL && i <= D_R15B) {
274 			reg[i] = (i-D_AL) & 7;
275 			if(i >= D_SPB && i <= D_DIB)
276 				regrex[i] = 0x40;
277 			if(i >= D_R8B && i <= D_R15B)
278 				regrex[i] = Rxr | Rxx | Rxb;
279 		}
280 		if(i >= D_AH && i<= D_BH)
281 			reg[i] = 4 + ((i-D_AH) & 7);
282 		if(i >= D_AX && i <= D_R15) {
283 			reg[i] = (i-D_AX) & 7;
284 			if(i >= D_R8)
285 				regrex[i] = Rxr | Rxx | Rxb;
286 		}
287 /*
288 		if(i >= D_F0 && i <= D_F0+7)
289 			reg[i] = (i-D_F0) & 7;
290 */
291 		if(i >= D_M0 && i <= D_M0+7)
292 			reg[i] = (i-D_M0) & 7;
293 		if(i >= D_X0 && i <= D_X0+15) {
294 			reg[i] = (i-D_X0) & 7;
295 			if(i >= D_X0+8)
296 				regrex[i] = Rxr | Rxx | Rxb;
297 		}
298 		if(i >= D_Y0 && i <= D_Y0+15) {
299 			reg[i] = (i-D_Y0) & 7;
300 			if(i >= D_Y0+8)
301 				regrex[i] = Rxr | Rxx | Rxb;
302 		}
303 		if(i >= D_CR+8 && i <= D_CR+15)
304 			regrex[i] = Rxr;
305 	}
306 
307 	zprg.link = P;
308 	zprg.pcond = P;
309 	zprg.back = 2;
310 	zprg.as = AGOK;
311 	zprg.from.type = D_NONE;
312 	zprg.from.index = D_NONE;
313 	zprg.from.scale = 1;
314 	zprg.to = zprg.from;
315 	zprg.mode = 64;
316 
317 	pcstr = "%.6llux ";
318 	nuxiinit();
319 	histgen = 0;
320 	textp = P;
321 	datap = P;
322 	edatap = P;
323 	pc = 0;
324 	dtype = 4;
325 	cout = create(outfile, 1, 0775);
326 	if(cout < 0) {
327 		diag("cannot create %s: %r", outfile);
328 		errorexit();
329 	}
330 	version = 0;
331 	cbp = buf.cbuf;
332 	cbc = sizeof(buf.cbuf);
333 	firstp = prg();
334 	lastp = firstp;
335 
336 	if(INITENTRY == 0) {
337 		INITENTRY = "_main";
338 		if(debug['p'])
339 			INITENTRY = "_mainp";
340 		if(!debug['l'])
341 			lookup(INITENTRY, 0)->type = SXREF;
342 	} else if(!(*INITENTRY >= '0' && *INITENTRY <= '9'))
343 		lookup(INITENTRY, 0)->type = SXREF;
344 
345 	while(*argv)
346 		objfile(*argv++);
347 	if(!debug['l'])
348 		loadlib();
349 	firstp = firstp->link;
350 	if(firstp == P)
351 		errorexit();
352 	if(doexp || dlm){
353 		EXPTAB = "_exporttab";
354 		zerosig(EXPTAB);
355 		zerosig("etext");
356 		zerosig("edata");
357 		zerosig("end");
358 		if(dlm){
359 			import();
360 			HEADTYPE = 2;
361 			INITTEXT = 0;
362 			INITDAT = 0;
363 			INITRND = 8;
364 			INITENTRY = EXPTAB;
365 		}
366 		export();
367 	}
368 	patch();
369 	follow();
370 	dodata();
371 	dostkoff();
372 	paramspace = "SP";	/* (FP) now (SP) on output */
373 	if(debug['p'])
374 		if(debug['1'])
375 			doprof1();
376 		else
377 			doprof2();
378 	span();
379 	doinit();
380 	asmb();
381 	undef();
382 	if(debug['v']) {
383 		Bprint(&bso, "%5.2f cpu time\n", cputime());
384 		Bprint(&bso, "%ld symbols\n", nsymbol);
385 		Bprint(&bso, "%ld memory used\n", thunk);
386 		Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
387 		Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
388 	}
389 	Bflush(&bso);
390 
391 	errorexit();
392 }
393 
394 void
addlibpath(char * arg)395 addlibpath(char *arg)
396 {
397 	char **p;
398 
399 	if(nlibdir >= maxlibdir) {
400 		if(maxlibdir == 0)
401 			maxlibdir = 8;
402 		else
403 			maxlibdir *= 2;
404 		p = malloc(maxlibdir*sizeof(*p));
405 		if(p == nil) {
406 			diag("out of memory");
407 			errorexit();
408 		}
409 		memmove(p, libdir, nlibdir*sizeof(*p));
410 		free(libdir);
411 		libdir = p;
412 	}
413 	libdir[nlibdir++] = strdup(arg);
414 }
415 
416 char*
findlib(char * file)417 findlib(char *file)
418 {
419 	int i;
420 	char name[LIBNAMELEN];
421 
422 	for(i = 0; i < nlibdir; i++) {
423 		snprint(name, sizeof(name), "%s/%s", libdir[i], file);
424 		if(fileexists(name))
425 			return libdir[i];
426 	}
427 	return nil;
428 }
429 
430 void
loadlib(void)431 loadlib(void)
432 {
433 	int i;
434 	long h;
435 	Sym *s;
436 
437 loop:
438 	xrefresolv = 0;
439 	for(i=0; i<libraryp; i++) {
440 		if(debug['v'])
441 			Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]);
442 		objfile(library[i]);
443 	}
444 	if(xrefresolv)
445 	for(h=0; h<nelem(hash); h++)
446 	for(s = hash[h]; s != S; s = s->link)
447 		if(s->type == SXREF)
448 			goto loop;
449 }
450 
451 void
errorexit(void)452 errorexit(void)
453 {
454 
455 	if(nerrors) {
456 		if(cout >= 0)
457 			remove(outfile);
458 		exits("error");
459 	}
460 	exits(0);
461 }
462 
463 void
objfile(char * file)464 objfile(char *file)
465 {
466 	long off, esym, cnt, l;
467 	int f, work;
468 	Sym *s;
469 	char magbuf[SARMAG];
470 	char name[LIBNAMELEN], pname[LIBNAMELEN];
471 	struct ar_hdr arhdr;
472 	char *e, *start, *stop;
473 
474 	if(debug['v'])
475 		Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
476 	Bflush(&bso);
477 	if(file[0] == '-' && file[1] == 'l') {
478 		snprint(pname, sizeof(pname), "lib%s.a", file+2);
479 		e = findlib(pname);
480 		if(e == nil) {
481 			diag("cannot find library: %s", file);
482 			errorexit();
483 		}
484 		snprint(name, sizeof(name), "%s/%s", e, pname);
485 		file = name;
486 	}
487 	f = open(file, 0);
488 	if(f < 0) {
489 		diag("cannot open %s: %r", file);
490 		errorexit();
491 	}
492 	l = read(f, magbuf, SARMAG);
493 	if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
494 		/* load it as a regular file */
495 		l = seek(f, 0L, 2);
496 		seek(f, 0L, 0);
497 		ldobj(f, l, file);
498 		close(f);
499 		return;
500 	}
501 
502 	l = read(f, &arhdr, SAR_HDR);
503 	if(l != SAR_HDR) {
504 		diag("%s: short read on archive file symbol header", file);
505 		goto out;
506 	}
507 	if(strncmp(arhdr.name, symname, strlen(symname))) {
508 		diag("%s: first entry not symbol header", file);
509 		goto out;
510 	}
511 
512 	esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
513 	off = SARMAG + SAR_HDR;
514 
515 	/*
516 	 * just bang the whole symbol file into memory
517 	 */
518 	seek(f, off, 0);
519 	cnt = esym - off;
520 	start = malloc(cnt + 10);
521 	cnt = read(f, start, cnt);
522 	if(cnt <= 0){
523 		close(f);
524 		return;
525 	}
526 	stop = &start[cnt];
527 	memset(stop, 0, 10);
528 
529 	work = 1;
530 	while(work) {
531 		if(debug['v'])
532 			Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
533 		Bflush(&bso);
534 		work = 0;
535 		for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
536 			s = lookup(e+5, 0);
537 			if(s->type != SXREF)
538 				continue;
539 			sprint(pname, "%s(%s)", file, s->name);
540 			if(debug['v'])
541 				Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
542 			Bflush(&bso);
543 			l = e[1] & 0xff;
544 			l |= (e[2] & 0xff) << 8;
545 			l |= (e[3] & 0xff) << 16;
546 			l |= (e[4] & 0xff) << 24;
547 			seek(f, l, 0);
548 			l = read(f, &arhdr, SAR_HDR);
549 			if(l != SAR_HDR)
550 				goto bad;
551 			if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
552 				goto bad;
553 			l = atolwhex(arhdr.size);
554 			ldobj(f, l, pname);
555 			if(s->type == SXREF) {
556 				diag("%s: failed to load: %s", file, s->name);
557 				errorexit();
558 			}
559 			work = 1;
560 			xrefresolv = 1;
561 		}
562 	}
563 	return;
564 
565 bad:
566 	diag("%s: bad or out of date archive", file);
567 out:
568 	close(f);
569 }
570 
571 int
zaddr(uchar * p,Adr * a,Sym * h[])572 zaddr(uchar *p, Adr *a, Sym *h[])
573 {
574 	int c, t, i;
575 	int l;
576 	Sym *s;
577 	Auto *u;
578 
579 	t = p[0];
580 	c = 1;
581 	if(t & T_INDEX) {
582 		a->index = p[c];
583 		a->scale = p[c+1];
584 		c += 2;
585 	} else {
586 		a->index = D_NONE;
587 		a->scale = 0;
588 	}
589 	a->offset = 0;
590 	if(t & T_OFFSET) {
591 		l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
592 		a->offset = l;
593 		c += 4;
594 		if(t & T_64) {
595 			l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
596 			a->offset = ((vlong)l<<32) | (a->offset & 0xFFFFFFFFUL);
597 			c += 4;
598 		}
599 	}
600 	a->sym = S;
601 	if(t & T_SYM) {
602 		a->sym = h[p[c]];
603 		c++;
604 	}
605 	a->type = D_NONE;
606 	if(t & T_FCONST) {
607 		a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
608 		a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
609 		c += 8;
610 		a->type = D_FCONST;
611 	} else
612 	if(t & T_SCONST) {
613 		for(i=0; i<NSNAME; i++)
614 			a->scon[i] = p[c+i];
615 		c += NSNAME;
616 		a->type = D_SCONST;
617 	}
618 	if(t & T_TYPE) {
619 		a->type = p[c];
620 		c++;
621 	}
622 	s = a->sym;
623 	if(s == S)
624 		return c;
625 
626 	t = a->type;
627 	if(t != D_AUTO && t != D_PARAM)
628 		return c;
629 	l = a->offset;
630 	for(u=curauto; u; u=u->link) {
631 		if(u->asym == s)
632 		if(u->type == t) {
633 			if(u->aoffset > l)
634 				u->aoffset = l;
635 			return c;
636 		}
637 	}
638 
639 	while(nhunk < sizeof(Auto))
640 		gethunk();
641 	u = (Auto*)hunk;
642 	nhunk -= sizeof(Auto);
643 	hunk += sizeof(Auto);
644 
645 	u->link = curauto;
646 	curauto = u;
647 	u->asym = s;
648 	u->aoffset = l;
649 	u->type = t;
650 	return c;
651 }
652 
653 void
addlib(char * obj)654 addlib(char *obj)
655 {
656 	char fn1[LIBNAMELEN], fn2[LIBNAMELEN], comp[LIBNAMELEN], *p, *name;
657 	int i, search;
658 
659 	if(histfrogp <= 0)
660 		return;
661 
662 	name = fn1;
663 	search = 0;
664 	if(histfrog[0]->name[1] == '/') {
665 		sprint(name, "");
666 		i = 1;
667 	} else if(histfrog[0]->name[1] == '.') {
668 		sprint(name, ".");
669 		i = 0;
670 	} else {
671 		sprint(name, "");
672 		i = 0;
673 		search = 1;
674 	}
675 
676 	for(; i<histfrogp; i++) {
677 		snprint(comp, sizeof comp, histfrog[i]->name+1);
678 		for(;;) {
679 			p = strstr(comp, "$O");
680 			if(p == 0)
681 				break;
682 			memmove(p+1, p+2, strlen(p+2)+1);
683 			p[0] = thechar;
684 		}
685 		for(;;) {
686 			p = strstr(comp, "$M");
687 			if(p == 0)
688 				break;
689 			if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
690 				diag("library component too long");
691 				return;
692 			}
693 			memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
694 			memmove(p, thestring, strlen(thestring));
695 		}
696 		if(strlen(fn1) + strlen(comp) + 3 >= sizeof(fn1)) {
697 			diag("library component too long");
698 			return;
699 		}
700 		if(i > 0 || !search)
701 			strcat(fn1, "/");
702 		strcat(fn1, comp);
703 	}
704 
705 	cleanname(name);
706 
707 	if(search){
708 		p = findlib(name);
709 		if(p != nil){
710 			snprint(fn2, sizeof(fn2), "%s/%s", p, name);
711 			name = fn2;
712 		}
713 	}
714 
715 	for(i=0; i<libraryp; i++)
716 		if(strcmp(name, library[i]) == 0)
717 			return;
718 	if(libraryp == nelem(library)){
719 		diag("too many autolibs; skipping %s", name);
720 		return;
721 	}
722 
723 	p = malloc(strlen(name) + 1);
724 	strcpy(p, name);
725 	library[libraryp] = p;
726 	p = malloc(strlen(obj) + 1);
727 	strcpy(p, obj);
728 	libraryobj[libraryp] = p;
729 	libraryp++;
730 }
731 
732 void
addhist(long line,int type)733 addhist(long line, int type)
734 {
735 	Auto *u;
736 	Sym *s;
737 	int i, j, k;
738 
739 	u = malloc(sizeof(Auto));
740 	s = malloc(sizeof(Sym));
741 	s->name = malloc(2*(histfrogp+1) + 1);
742 
743 	u->asym = s;
744 	u->type = type;
745 	u->aoffset = line;
746 	u->link = curhist;
747 	curhist = u;
748 
749 	j = 1;
750 	for(i=0; i<histfrogp; i++) {
751 		k = histfrog[i]->value;
752 		s->name[j+0] = k>>8;
753 		s->name[j+1] = k;
754 		j += 2;
755 	}
756 }
757 
758 void
histtoauto(void)759 histtoauto(void)
760 {
761 	Auto *l;
762 
763 	while(l = curhist) {
764 		curhist = l->link;
765 		l->link = curauto;
766 		curauto = l;
767 	}
768 }
769 
770 void
collapsefrog(Sym * s)771 collapsefrog(Sym *s)
772 {
773 	int i;
774 
775 	/*
776 	 * bad encoding of path components only allows
777 	 * MAXHIST components. if there is an overflow,
778 	 * first try to collapse xxx/..
779 	 */
780 	for(i=1; i<histfrogp; i++)
781 		if(strcmp(histfrog[i]->name+1, "..") == 0) {
782 			memmove(histfrog+i-1, histfrog+i+1,
783 				(histfrogp-i-1)*sizeof(histfrog[0]));
784 			histfrogp--;
785 			goto out;
786 		}
787 
788 	/*
789 	 * next try to collapse .
790 	 */
791 	for(i=0; i<histfrogp; i++)
792 		if(strcmp(histfrog[i]->name+1, ".") == 0) {
793 			memmove(histfrog+i, histfrog+i+1,
794 				(histfrogp-i-1)*sizeof(histfrog[0]));
795 			goto out;
796 		}
797 
798 	/*
799 	 * last chance, just truncate from front
800 	 */
801 	memmove(histfrog+0, histfrog+1,
802 		(histfrogp-1)*sizeof(histfrog[0]));
803 
804 out:
805 	histfrog[histfrogp-1] = s;
806 }
807 
808 void
nopout(Prog * p)809 nopout(Prog *p)
810 {
811 	p->as = ANOP;
812 	p->from.type = D_NONE;
813 	p->to.type = D_NONE;
814 }
815 
816 uchar*
readsome(int f,uchar * buf,uchar * good,uchar * stop,int max)817 readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
818 {
819 	int n;
820 
821 	n = stop - good;
822 	memmove(buf, good, stop - good);
823 	stop = buf + n;
824 	n = MAXIO - n;
825 	if(n > max)
826 		n = max;
827 	n = read(f, stop, n);
828 	if(n <= 0)
829 		return 0;
830 	return stop + n;
831 }
832 
833 void
ldobj(int f,long c,char * pn)834 ldobj(int f, long c, char *pn)
835 {
836 	vlong ipc;
837 	Prog *p, *t;
838 	uchar *bloc, *bsize, *stop;
839 	int v, o, r, skip, mode;
840 	Sym *h[NSYM], *s, *di;
841 	ulong sig;
842 	static int files;
843 	static char **filen;
844 	char **nfilen;
845 
846 	if((files&15) == 0){
847 		nfilen = malloc((files+16)*sizeof(char*));
848 		memmove(nfilen, filen, files*sizeof(char*));
849 		free(filen);
850 		filen = nfilen;
851 	}
852 	filen[files++] = strdup(pn);
853 
854 	bsize = buf.xbuf;
855 	bloc = buf.xbuf;
856 	di = S;
857 
858 newloop:
859 	memset(h, 0, sizeof(h));
860 	version++;
861 	histfrogp = 0;
862 	ipc = pc;
863 	skip = 0;
864 	mode = 64;
865 
866 loop:
867 	if(c <= 0)
868 		goto eof;
869 	r = bsize - bloc;
870 	if(r < 100 && r < c) {		/* enough for largest prog */
871 		bsize = readsome(f, buf.xbuf, bloc, bsize, c);
872 		if(bsize == 0)
873 			goto eof;
874 		bloc = buf.xbuf;
875 		goto loop;
876 	}
877 	o = bloc[0] | (bloc[1] << 8);
878 	if(o <= AXXX || o >= ALAST) {
879 		if(o < 0)
880 			goto eof;
881 		diag("%s: opcode out of range %d", pn, o);
882 		print("	probably not a .6 file\n");
883 		errorexit();
884 	}
885 
886 	if(o == ANAME || o == ASIGNAME) {
887 		sig = 0;
888 		if(o == ASIGNAME) {
889 			sig = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24);
890 			bloc += 4;
891 			c -= 4;
892 		}
893 		stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
894 		if(stop == 0){
895 			bsize = readsome(f, buf.xbuf, bloc, bsize, c);
896 			if(bsize == 0)
897 				goto eof;
898 			bloc = buf.xbuf;
899 			stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
900 			if(stop == 0){
901 				fprint(2, "%s: name too long\n", pn);
902 				errorexit();
903 			}
904 		}
905 		v = bloc[2];	/* type */
906 		o = bloc[3];	/* sym */
907 		bloc += 4;
908 		c -= 4;
909 
910 		r = 0;
911 		if(v == D_STATIC)
912 			r = version;
913 		s = lookup((char*)bloc, r);
914 		c -= &stop[1] - bloc;
915 		bloc = stop + 1;
916 
917 		if(debug['S'] && r == 0)
918 			sig = 1729;
919 		if(sig != 0){
920 			if(s->sig != 0 && s->sig != sig)
921 				diag("incompatible type signatures %lux(%s) and %lux(%s) for %s", s->sig, filen[s->file], sig, pn, s->name);
922 			s->sig = sig;
923 			s->file = files-1;
924 		}
925 
926 		if(debug['W'])
927 			print("	ANAME	%s\n", s->name);
928 		h[o] = s;
929 		if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
930 			s->type = SXREF;
931 		if(v == D_FILE) {
932 			if(s->type != SFILE) {
933 				histgen++;
934 				s->type = SFILE;
935 				s->value = histgen;
936 			}
937 			if(histfrogp < MAXHIST) {
938 				histfrog[histfrogp] = s;
939 				histfrogp++;
940 			} else
941 				collapsefrog(s);
942 		}
943 		goto loop;
944 	}
945 
946 	while(nhunk < sizeof(Prog))
947 		gethunk();
948 	p = (Prog*)hunk;
949 	nhunk -= sizeof(Prog);
950 	hunk += sizeof(Prog);
951 
952 	p->as = o;
953 	p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
954 	p->back = 2;
955 	p->mode = mode;
956 	r = zaddr(bloc+6, &p->from, h) + 6;
957 	r += zaddr(bloc+r, &p->to, h);
958 	bloc += r;
959 	c -= r;
960 
961 	if(debug['W'])
962 		print("%P\n", p);
963 
964 	switch(p->as) {
965 	case AHISTORY:
966 		if(p->to.offset == -1) {
967 			addlib(pn);
968 			histfrogp = 0;
969 			goto loop;
970 		}
971 		addhist(p->line, D_FILE);		/* 'z' */
972 		if(p->to.offset)
973 			addhist(p->to.offset, D_FILE1);	/* 'Z' */
974 		histfrogp = 0;
975 		goto loop;
976 
977 	case AEND:
978 		histtoauto();
979 		if(curtext != P)
980 			curtext->to.autom = curauto;
981 		curauto = 0;
982 		curtext = P;
983 		if(c)
984 			goto newloop;
985 		return;
986 
987 	case AGLOBL:
988 		s = p->from.sym;
989 		if(s->type == 0 || s->type == SXREF) {
990 			s->type = SBSS;
991 			s->value = 0;
992 		}
993 		if(s->type != SBSS) {
994 			diag("%s: redefinition: %s in %s",
995 				pn, s->name, TNAME);
996 			s->type = SBSS;
997 			s->value = 0;
998 		}
999 		if(p->to.offset > s->value)
1000 			s->value = p->to.offset;
1001 		goto loop;
1002 
1003 	case ADYNT:
1004 		if(p->to.sym == S) {
1005 			diag("DYNT without a sym\n%P", p);
1006 			break;
1007 		}
1008 		di = p->to.sym;
1009 		p->from.scale = 4;
1010 		if(di->type == SXREF) {
1011 			if(debug['z'])
1012 				Bprint(&bso, "%P set to %d\n", p, dtype);
1013 			di->type = SCONST;
1014 			di->value = dtype;
1015 			dtype += 4;
1016 		}
1017 		if(p->from.sym == S)
1018 			break;
1019 
1020 		p->from.offset = di->value;
1021 		p->from.sym->type = SDATA;
1022 		if(curtext == P) {
1023 			diag("DYNT not in text: %P", p);
1024 			break;
1025 		}
1026 		p->to.sym = curtext->from.sym;
1027 		p->to.type = D_ADDR;
1028 		p->to.index = D_EXTERN;
1029 		goto data;
1030 
1031 	case AINIT:
1032 		if(p->from.sym == S) {
1033 			diag("INIT without a sym\n%P", p);
1034 			break;
1035 		}
1036 		if(di == S) {
1037 			diag("INIT without previous DYNT\n%P", p);
1038 			break;
1039 		}
1040 		p->from.offset = di->value;
1041 		p->from.sym->type = SDATA;
1042 		goto data;
1043 
1044 	case ADATA:
1045 	data:
1046 		if(edatap == P)
1047 			datap = p;
1048 		else
1049 			edatap->link = p;
1050 		edatap = p;
1051 		p->link = P;
1052 		goto loop;
1053 
1054 	case AGOK:
1055 		diag("%s: GOK opcode in %s", pn, TNAME);
1056 		pc++;
1057 		goto loop;
1058 
1059 	case ATEXT:
1060 		if(curtext != P) {
1061 			histtoauto();
1062 			curtext->to.autom = curauto;
1063 			curauto = 0;
1064 		}
1065 		skip = 0;
1066 		curtext = p;
1067 		s = p->from.sym;
1068 		if(s == S) {
1069 			diag("%s: no TEXT symbol: %P", pn, p);
1070 			errorexit();
1071 		}
1072 		if(s->type != 0 && s->type != SXREF) {
1073 			if(p->from.scale & DUPOK) {
1074 				skip = 1;
1075 				goto casdef;
1076 			}
1077 			diag("%s: redefinition: %s\n%P", pn, s->name, p);
1078 		}
1079 		s->type = STEXT;
1080 		s->value = pc;
1081 		lastp->link = p;
1082 		lastp = p;
1083 		p->pc = pc;
1084 		pc++;
1085 		if(textp == P) {
1086 			textp = p;
1087 			etextp = p;
1088 			goto loop;
1089 		}
1090 		etextp->pcond = p;
1091 		etextp = p;
1092 		goto loop;
1093 
1094 	case AMODE:
1095 		if(p->from.type == D_CONST || p->from.type == D_INDIR+D_NONE){
1096 			switch((int)p->from.offset){
1097 			case 16: case 32: case 64:
1098 				mode = p->from.offset;
1099 				break;
1100 			}
1101 		}
1102 		goto loop;
1103 
1104 	case AFMOVF:
1105 	case AFADDF:
1106 	case AFSUBF:
1107 	case AFSUBRF:
1108 	case AFMULF:
1109 	case AFDIVF:
1110 	case AFDIVRF:
1111 	case AFCOMF:
1112 	case AFCOMFP:
1113 	case AMOVSS:
1114 	case AADDSS:
1115 	case ASUBSS:
1116 	case AMULSS:
1117 	case ADIVSS:
1118 	case ACOMISS:
1119 	case AUCOMISS:
1120 		if(skip)
1121 			goto casdef;
1122 		if(p->from.type == D_FCONST) {
1123 			/* size sb 9 max */
1124 			sprint(literal, "$%lux", ieeedtof(&p->from.ieee));
1125 			s = lookup(literal, 0);
1126 			if(s->type == 0) {
1127 				s->type = SBSS;
1128 				s->value = 4;
1129 				t = prg();
1130 				t->as = ADATA;
1131 				t->line = p->line;
1132 				t->from.type = D_EXTERN;
1133 				t->from.sym = s;
1134 				t->from.scale = 4;
1135 				t->to = p->from;
1136 				if(edatap == P)
1137 					datap = t;
1138 				else
1139 					edatap->link = t;
1140 				edatap = t;
1141 				t->link = P;
1142 			}
1143 			p->from.type = D_EXTERN;
1144 			p->from.sym = s;
1145 			p->from.offset = 0;
1146 		}
1147 		goto casdef;
1148 
1149 	case AFMOVD:
1150 	case AFADDD:
1151 	case AFSUBD:
1152 	case AFSUBRD:
1153 	case AFMULD:
1154 	case AFDIVD:
1155 	case AFDIVRD:
1156 	case AFCOMD:
1157 	case AFCOMDP:
1158 	case AMOVSD:
1159 	case AADDSD:
1160 	case ASUBSD:
1161 	case AMULSD:
1162 	case ADIVSD:
1163 	case ACOMISD:
1164 	case AUCOMISD:
1165 		if(skip)
1166 			goto casdef;
1167 		if(p->from.type == D_FCONST) {
1168 			/* size sb 18 max */
1169 			sprint(literal, "$%lux.%lux",
1170 				p->from.ieee.l, p->from.ieee.h);
1171 			s = lookup(literal, 0);
1172 			if(s->type == 0) {
1173 				s->type = SBSS;
1174 				s->value = 8;
1175 				t = prg();
1176 				t->as = ADATA;
1177 				t->line = p->line;
1178 				t->from.type = D_EXTERN;
1179 				t->from.sym = s;
1180 				t->from.scale = 8;
1181 				t->to = p->from;
1182 				if(edatap == P)
1183 					datap = t;
1184 				else
1185 					edatap->link = t;
1186 				edatap = t;
1187 				t->link = P;
1188 			}
1189 			p->from.type = D_EXTERN;
1190 			p->from.sym = s;
1191 			p->from.offset = 0;
1192 		}
1193 		goto casdef;
1194 
1195 	casdef:
1196 	default:
1197 		if(skip)
1198 			nopout(p);
1199 
1200 		if(p->to.type == D_BRANCH)
1201 			p->to.offset += ipc;
1202 		lastp->link = p;
1203 		lastp = p;
1204 		p->pc = pc;
1205 		pc++;
1206 		goto loop;
1207 	}
1208 	goto loop;
1209 
1210 eof:
1211 	diag("truncated object file: %s", pn);
1212 }
1213 
1214 Sym*
lookup(char * symb,int v)1215 lookup(char *symb, int v)
1216 {
1217 	Sym *s;
1218 	char *p;
1219 	long h;
1220 	int l, c;
1221 
1222 	h = v;
1223 	for(p=symb; c = *p; p++)
1224 		h = h+h+h + c;
1225 	l = (p - symb) + 1;
1226 	h &= 0xffffff;
1227 	h %= NHASH;
1228 	for(s = hash[h]; s != S; s = s->link)
1229 		if(s->version == v)
1230 		if(memcmp(s->name, symb, l) == 0)
1231 			return s;
1232 
1233 	while(nhunk < sizeof(Sym))
1234 		gethunk();
1235 	s = (Sym*)hunk;
1236 	nhunk -= sizeof(Sym);
1237 	hunk += sizeof(Sym);
1238 
1239 	s->name = malloc(l + 1);
1240 	memmove(s->name, symb, l);
1241 
1242 	s->link = hash[h];
1243 	s->type = 0;
1244 	s->version = v;
1245 	s->value = 0;
1246 	s->sig = 0;
1247 	hash[h] = s;
1248 	nsymbol++;
1249 	return s;
1250 }
1251 
1252 Prog*
prg(void)1253 prg(void)
1254 {
1255 	Prog *p;
1256 
1257 	while(nhunk < sizeof(Prog))
1258 		gethunk();
1259 	p = (Prog*)hunk;
1260 	nhunk -= sizeof(Prog);
1261 	hunk += sizeof(Prog);
1262 
1263 	*p = zprg;
1264 	return p;
1265 }
1266 
1267 Prog*
copyp(Prog * q)1268 copyp(Prog *q)
1269 {
1270 	Prog *p;
1271 
1272 	p = prg();
1273 	*p = *q;
1274 	return p;
1275 }
1276 
1277 Prog*
appendp(Prog * q)1278 appendp(Prog *q)
1279 {
1280 	Prog *p;
1281 
1282 	p = prg();
1283 	p->link = q->link;
1284 	q->link = p;
1285 	p->line = q->line;
1286 	p->mode = q->mode;
1287 	return p;
1288 }
1289 
1290 void
gethunk(void)1291 gethunk(void)
1292 {
1293 	char *h;
1294 	long nh;
1295 
1296 	nh = NHUNK;
1297 	if(thunk >= 5L*NHUNK) {
1298 		nh = 5L*NHUNK;
1299 		if(thunk >= 25L*NHUNK)
1300 			nh = 25L*NHUNK;
1301 	}
1302 	h = mysbrk(nh);
1303 	if(h == (char*)-1) {
1304 		diag("out of memory");
1305 		errorexit();
1306 	}
1307 	hunk = h;
1308 	nhunk = nh;
1309 	thunk += nh;
1310 }
1311 
1312 void
doprof1(void)1313 doprof1(void)
1314 {
1315 	Sym *s;
1316 	long n;
1317 	Prog *p, *q;
1318 
1319 	if(debug['v'])
1320 		Bprint(&bso, "%5.2f profile 1\n", cputime());
1321 	Bflush(&bso);
1322 	s = lookup("__mcount", 0);
1323 	n = 1;
1324 	for(p = firstp->link; p != P; p = p->link) {
1325 		if(p->as == ATEXT) {
1326 			q = prg();
1327 			q->line = p->line;
1328 			q->link = datap;
1329 			datap = q;
1330 			q->as = ADATA;
1331 			q->from.type = D_EXTERN;
1332 			q->from.offset = n*4;
1333 			q->from.sym = s;
1334 			q->from.scale = 4;
1335 			q->to = p->from;
1336 			q->to.type = D_CONST;
1337 
1338 			q = prg();
1339 			q->line = p->line;
1340 			q->pc = p->pc;
1341 			q->link = p->link;
1342 			p->link = q;
1343 			p = q;
1344 			p->as = AADDL;
1345 			p->from.type = D_CONST;
1346 			p->from.offset = 1;
1347 			p->to.type = D_EXTERN;
1348 			p->to.sym = s;
1349 			p->to.offset = n*4 + 4;
1350 
1351 			n += 2;
1352 			continue;
1353 		}
1354 	}
1355 	q = prg();
1356 	q->line = 0;
1357 	q->link = datap;
1358 	datap = q;
1359 
1360 	q->as = ADATA;
1361 	q->from.type = D_EXTERN;
1362 	q->from.sym = s;
1363 	q->from.scale = 4;
1364 	q->to.type = D_CONST;
1365 	q->to.offset = n;
1366 
1367 	s->type = SBSS;
1368 	s->value = n*4;
1369 }
1370 
1371 void
doprof2(void)1372 doprof2(void)
1373 {
1374 	Sym *s2, *s4;
1375 	Prog *p, *q, *q2, *ps2, *ps4;
1376 
1377 	if(debug['v'])
1378 		Bprint(&bso, "%5.2f profile 2\n", cputime());
1379 	Bflush(&bso);
1380 
1381 	if(debug['e']){
1382 		s2 = lookup("_tracein", 0);
1383 		s4 = lookup("_traceout", 0);
1384 	}else{
1385 		s2 = lookup("_profin", 0);
1386 		s4 = lookup("_profout", 0);
1387 	}
1388 	if(s2->type != STEXT || s4->type != STEXT) {
1389 		if(debug['e'])
1390 			diag("_tracein/_traceout not defined %d %d", s2->type, s4->type);
1391 		else
1392 			diag("_profin/_profout not defined");
1393 		return;
1394 	}
1395 
1396 	ps2 = P;
1397 	ps4 = P;
1398 	for(p = firstp; p != P; p = p->link) {
1399 		if(p->as == ATEXT) {
1400 			if(p->from.sym == s2) {
1401 				p->from.scale = 1;
1402 				ps2 = p;
1403 			}
1404 			if(p->from.sym == s4) {
1405 				p->from.scale = 1;
1406 				ps4 = p;
1407 			}
1408 		}
1409 	}
1410 	for(p = firstp; p != P; p = p->link) {
1411 		if(p->as == ATEXT) {
1412 			curtext = p;
1413 
1414 			if(p->from.scale & NOPROF) {	/* dont profile */
1415 				for(;;) {
1416 					q = p->link;
1417 					if(q == P)
1418 						break;
1419 					if(q->as == ATEXT)
1420 						break;
1421 					p = q;
1422 				}
1423 				continue;
1424 			}
1425 
1426 			/*
1427 			 * JMPL	profin
1428 			 */
1429 			q = prg();
1430 			q->line = p->line;
1431 			q->pc = p->pc;
1432 			q->link = p->link;
1433 			if(debug['e']){		/* embedded tracing */
1434 				q2 = prg();
1435 				p->link = q2;
1436 				q2->link = q;
1437 
1438 				q2->line = p->line;
1439 				q2->pc = p->pc;
1440 
1441 				q2->as = AJMP;
1442 				q2->to.type = D_BRANCH;
1443 				q2->to.sym = p->to.sym;
1444 				q2->pcond = q->link;
1445 			}else
1446 				p->link = q;
1447 			p = q;
1448 			p->as = ACALL;
1449 			p->to.type = D_BRANCH;
1450 			p->pcond = ps2;
1451 			p->to.sym = s2;
1452 
1453 			continue;
1454 		}
1455 		if(p->as == ARET) {
1456 			/*
1457 			 * RET (default)
1458 			 */
1459 			if(debug['e']){		/* embedded tracing */
1460 				q = prg();
1461 				q->line = p->line;
1462 				q->pc = p->pc;
1463 				q->link = p->link;
1464 				p->link = q;
1465 				p = q;
1466 			}
1467 			/*
1468 			 * RET
1469 			 */
1470 			q = prg();
1471 			q->as = ARET;
1472 			q->from = p->from;
1473 			q->to = p->to;
1474 			q->link = p->link;
1475 			p->link = q;
1476 
1477 			/*
1478 			 * JAL	profout
1479 			 */
1480 			p->as = ACALL;
1481 			p->from = zprg.from;
1482 			p->to = zprg.to;
1483 			p->to.type = D_BRANCH;
1484 			p->pcond = ps4;
1485 			p->to.sym = s4;
1486 
1487 			p = q;
1488 
1489 			continue;
1490 		}
1491 	}
1492 }
1493 
1494 void
nuxiinit(void)1495 nuxiinit(void)
1496 {
1497 	int i, c;
1498 
1499 	for(i=0; i<4; i++) {
1500 		c = find1(0x04030201L, i+1);
1501 		if(i < 2)
1502 			inuxi2[i] = c;
1503 		if(i < 1)
1504 			inuxi1[i] = c;
1505 		inuxi4[i] = c;
1506 		inuxi8[i] = c;
1507 		inuxi8[i+4] = c+4;
1508 		fnuxi4[i] = c;
1509 		fnuxi8[i] = c;
1510 		fnuxi8[i+4] = c+4;
1511 	}
1512 	if(debug['v']) {
1513 		Bprint(&bso, "inuxi = ");
1514 		for(i=0; i<1; i++)
1515 			Bprint(&bso, "%d", inuxi1[i]);
1516 		Bprint(&bso, " ");
1517 		for(i=0; i<2; i++)
1518 			Bprint(&bso, "%d", inuxi2[i]);
1519 		Bprint(&bso, " ");
1520 		for(i=0; i<4; i++)
1521 			Bprint(&bso, "%d", inuxi4[i]);
1522 		Bprint(&bso, " ");
1523 		for(i=0; i<8; i++)
1524 			Bprint(&bso, "%d", inuxi8[i]);
1525 		Bprint(&bso, "\nfnuxi = ");
1526 		for(i=0; i<4; i++)
1527 			Bprint(&bso, "%d", fnuxi4[i]);
1528 		Bprint(&bso, " ");
1529 		for(i=0; i<8; i++)
1530 			Bprint(&bso, "%d", fnuxi8[i]);
1531 		Bprint(&bso, "\n");
1532 	}
1533 	Bflush(&bso);
1534 }
1535 
1536 int
find1(long l,int c)1537 find1(long l, int c)
1538 {
1539 	char *p;
1540 	int i;
1541 
1542 	p = (char*)&l;
1543 	for(i=0; i<4; i++)
1544 		if(*p++ == c)
1545 			return i;
1546 	return 0;
1547 }
1548 
1549 int
find2(long l,int c)1550 find2(long l, int c)
1551 {
1552 	short *p;
1553 	int i;
1554 
1555 	p = (short*)&l;
1556 	for(i=0; i<4; i+=2) {
1557 		if(((*p >> 8) & 0xff) == c)
1558 			return i;
1559 		if((*p++ & 0xff) == c)
1560 			return i+1;
1561 	}
1562 	return 0;
1563 }
1564 
1565 long
ieeedtof(Ieee * e)1566 ieeedtof(Ieee *e)
1567 {
1568 	int exp;
1569 	long v;
1570 
1571 	if(e->h == 0)
1572 		return 0;
1573 	exp = (e->h>>20) & ((1L<<11)-1L);
1574 	exp -= (1L<<10) - 2L;
1575 	v = (e->h & 0xfffffL) << 3;
1576 	v |= (e->l >> 29) & 0x7L;
1577 	if((e->l >> 28) & 1) {
1578 		v++;
1579 		if(v & 0x800000L) {
1580 			v = (v & 0x7fffffL) >> 1;
1581 			exp++;
1582 		}
1583 	}
1584 	if(exp <= -126 || exp >= 130)
1585 		diag("double fp to single fp overflow");
1586 	v |= ((exp + 126) & 0xffL) << 23;
1587 	v |= e->h & 0x80000000L;
1588 	return v;
1589 }
1590 
1591 double
ieeedtod(Ieee * ieeep)1592 ieeedtod(Ieee *ieeep)
1593 {
1594 	Ieee e;
1595 	double fr;
1596 	int exp;
1597 
1598 	if(ieeep->h & (1L<<31)) {
1599 		e.h = ieeep->h & ~(1L<<31);
1600 		e.l = ieeep->l;
1601 		return -ieeedtod(&e);
1602 	}
1603 	if(ieeep->l == 0 && ieeep->h == 0)
1604 		return 0;
1605 	fr = ieeep->l & ((1L<<16)-1L);
1606 	fr /= 1L<<16;
1607 	fr += (ieeep->l>>16) & ((1L<<16)-1L);
1608 	fr /= 1L<<16;
1609 	fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
1610 	fr /= 1L<<21;
1611 	exp = (ieeep->h>>20) & ((1L<<11)-1L);
1612 	exp -= (1L<<10) - 2L;
1613 	return ldexp(fr, exp);
1614 }
1615 
1616 void
undefsym(Sym * s)1617 undefsym(Sym *s)
1618 {
1619 	int n;
1620 
1621 	n = imports;
1622 	if(s->value != 0)
1623 		diag("value != 0 on SXREF");
1624 	if(n >= 1<<Rindex)
1625 		diag("import index %d out of range", n);
1626 	s->value = n<<Roffset;
1627 	s->type = SUNDEF;
1628 	imports++;
1629 }
1630 
1631 void
zerosig(char * sp)1632 zerosig(char *sp)
1633 {
1634 	Sym *s;
1635 
1636 	s = lookup(sp, 0);
1637 	s->sig = 0;
1638 }
1639 
1640 void
readundefs(char * f,int t)1641 readundefs(char *f, int t)
1642 {
1643 	int i, n;
1644 	Sym *s;
1645 	Biobuf *b;
1646 	char *l, buf[256], *fields[64];
1647 
1648 	if(f == nil)
1649 		return;
1650 	b = Bopen(f, OREAD);
1651 	if(b == nil){
1652 		diag("could not open %s: %r", f);
1653 		errorexit();
1654 	}
1655 	while((l = Brdline(b, '\n')) != nil){
1656 		n = Blinelen(b);
1657 		if(n >= sizeof(buf)){
1658 			diag("%s: line too long", f);
1659 			errorexit();
1660 		}
1661 		memmove(buf, l, n);
1662 		buf[n-1] = '\0';
1663 		n = getfields(buf, fields, nelem(fields), 1, " \t\r\n");
1664 		if(n == nelem(fields)){
1665 			diag("%s: bad format", f);
1666 			errorexit();
1667 		}
1668 		for(i = 0; i < n; i++){
1669 			s = lookup(fields[i], 0);
1670 			s->type = SXREF;
1671 			s->subtype = t;
1672 			if(t == SIMPORT)
1673 				nimports++;
1674 			else
1675 				nexports++;
1676 		}
1677 	}
1678 	Bterm(b);
1679 }
1680