xref: /csrg-svn/old/adb/adb.vax/setup.c (revision 8927)
1*8927Srrh static	char sccsid[] = "@(#)setup.c	4.6 82/10/28";
23766Sroot /*
33766Sroot  * adb - routines to read a.out+core at startup
43766Sroot  */
53766Sroot #include "defs.h"
6*8927Srrh #include <sys/stat.h>
73766Sroot 
83766Sroot off_t	datbas;			/* offset of the base of the data segment */
93766Sroot off_t	stksiz;			/* stack size in the core image */
103766Sroot INT	sigcode;	/* belongs in head.h */
113766Sroot 
123766Sroot char	*symfil	= "a.out";
133766Sroot char	*corfil	= "core";
143766Sroot 
153766Sroot setsym()
163766Sroot {
173766Sroot 	off_t loc;
183766Sroot 	struct exec hdr;
193766Sroot 	register struct nlist *sp;
203766Sroot 	int ssiz;
213766Sroot 	char *strtab;
223766Sroot 
233766Sroot 	fsym = getfile(symfil, 1);
243766Sroot 	txtmap.ufd = fsym;
253766Sroot 	if (read(fsym, (char *)&hdr, sizeof hdr) != sizeof hdr ||
263766Sroot 	    N_BADMAG(hdr)) {
273766Sroot 		txtmap.e1 = MAXFILE;
283766Sroot 		return;
293766Sroot 	}
303766Sroot 	filhdr = hdr;
313766Sroot 	loc = filhdr.a_text+filhdr.a_data;
323766Sroot 	txtmap.f1 = txtmap.f2 = N_TXTOFF(filhdr);
333766Sroot 	txtmap.b1 = 0;
343766Sroot 	switch (filhdr.a_magic) {
353766Sroot 
363766Sroot 	case OMAGIC:
373766Sroot 		txtmap.b1 = txtmap.e1 = 0;
383766Sroot 		txtmap.b2 = datbas = 0;
393766Sroot 		txtmap.e2 = loc;
403766Sroot 		break;
413766Sroot 
423766Sroot 	case ZMAGIC:
433766Sroot 	case NMAGIC:
443766Sroot 		txtmap.e1 = filhdr.a_text;
453766Sroot 		txtmap.b2 = datbas = round(filhdr.a_text, PAGSIZ);
463766Sroot 		txtmap.e2 = datbas + filhdr.a_data;
473766Sroot 		txtmap.f2 += txtmap.e1;
483766Sroot 	}
493766Sroot 	loc = N_SYMOFF(filhdr);
503766Sroot 	symtab = (struct nlist *) malloc(filhdr.a_syms);
513766Sroot 	esymtab = &symtab[filhdr.a_syms / sizeof (struct nlist)];
523766Sroot 	if (symtab == NULL)
533766Sroot 		goto nospac;
543766Sroot 	lseek(fsym, loc, 0);
553766Sroot 	if (filhdr.a_syms == 0)
563766Sroot 		goto nosymt;
573766Sroot 	/* SHOULD SQUISH OUT STABS HERE!!! */
583766Sroot 	if (read(fsym, symtab, filhdr.a_syms) != filhdr.a_syms)
593766Sroot 		goto readerr;
603766Sroot 	if (read(fsym, &ssiz, sizeof (ssiz)) != sizeof (ssiz))
613766Sroot 		goto oldfmt;
623766Sroot 	strtab = (char *) malloc(ssiz);
633766Sroot 	if (strtab == 0)
643766Sroot 		goto nospac;
653766Sroot 	*(int *)strtab = ssiz;
663766Sroot 	ssiz -= sizeof (ssiz);
673766Sroot 	if (read(fsym, strtab + sizeof (ssiz), ssiz) != ssiz)
683766Sroot 		goto readerr;
693766Sroot 	for (sp = symtab; sp < esymtab; sp++)
703766Sroot 		if (sp->n_strx)
713766Sroot 			/* SHOULD PERFORM RANGE CHECK HERE */
723766Sroot 			sp->n_un.n_name = strtab + sp->n_un.n_strx;
733766Sroot nosymt:
743766Sroot 	if (INKERNEL(filhdr.a_entry)) {
753766Sroot 		txtmap.b1 += KERNOFF;
763766Sroot 		txtmap.e1 += KERNOFF;
773766Sroot 		txtmap.b2 += KERNOFF;
783766Sroot 		txtmap.e2 += KERNOFF;
793766Sroot 	}
803766Sroot 	return;
813766Sroot readerr:
823766Sroot 	printf("Error reading symbol|string table\n");
833766Sroot 	exit(1);
843766Sroot nospac:
853766Sroot 	printf("Not enough space for symbol|string table\n");
863766Sroot 	exit(1);
873766Sroot oldfmt:
883766Sroot 	printf("Old format a.out - no string table\n");
893766Sroot 	exit(1);
903766Sroot }
913766Sroot 
923766Sroot setcor()
933766Sroot {
943766Sroot 
953766Sroot 	fcor = datmap.ufd = getfile(corfil,2);
966416Sroot 	if (kernel && fcor != -1 && INKERNEL(filhdr.a_entry)) {
973766Sroot 		struct stat stb;
983766Sroot 
993779Sroot 		kcore = 1;
1003766Sroot 		fstat(fcor, &stb);
1013766Sroot 		datmap.b1 = 0;
1023766Sroot 		datmap.e1 = -1;
1033778Sroot 		if (kernel == 0 && (stb.st_mode & S_IFREG))
1043778Sroot 			datmap.b1 = 0x80000000;
1053777Sroot 		lookup("_Sysmap");
1063777Sroot 		sbr = cursym->n_value;
1073777Sroot 		lookup("_Syssize");
1083777Sroot 		slr = cursym->n_value;
1093777Sroot 		printf("sbr %X slr %X\n", sbr, slr);
1106416Sroot 		lookup("_masterpaddr");
1113777Sroot 		physrw(fcor, cursym->n_value&0x7fffffff, &masterpcbb, 1);
1126416Sroot 		masterpcbb = (masterpcbb&PG_PFNUM)*512;
1133777Sroot 		getpcb();
1143766Sroot 		return;
1153766Sroot 	}
1163766Sroot 	if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) ||
1173766Sroot 	   !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) {
1183766Sroot 		datmap.e1 = MAXFILE;
1193766Sroot 		return;
1203766Sroot 	}
1213766Sroot 	signo = u.u_arg[0];
1223766Sroot 	sigcode = u.u_code;
1233766Sroot 	filhdr.a_text = ctob(u.u_tsize);
1243766Sroot 	filhdr.a_data = ctob(u.u_dsize);
1253766Sroot 	stksiz = ctob(u.u_ssize);
1263766Sroot 	switch (filhdr.a_magic) {
1273766Sroot 
1283766Sroot 	case OMAGIC:
1293766Sroot 		datmap.b1 = 0;
1303766Sroot 		datmap.e1 = filhdr.a_text+filhdr.a_data;
1313766Sroot 		datmap.f2 = ctob(UPAGES) + datmap.e1;
1323766Sroot 		break;
1333766Sroot 
1343766Sroot 	case NMAGIC:
1353766Sroot 	case ZMAGIC:
1363766Sroot 		datmap.b1 = round(filhdr.a_text, PAGSIZ);
1373766Sroot 		datmap.e1 = datmap.b1 + filhdr.a_data;
1383766Sroot 		datmap.f2 = ctob(UPAGES) + filhdr.a_data;
1393766Sroot 		break;
1403766Sroot 	}
1413766Sroot 	datbas = datmap.b1;
1423766Sroot 	datmap.f1 = ctob(UPAGES);
1433766Sroot 	datmap.b2 = MAXSTOR - stksiz;
1443766Sroot 	datmap.e2 = MAXSTOR;
1453766Sroot 	if (filhdr.a_magic && u.u_exdata.ux_mag &&
1463766Sroot 	    filhdr.a_magic != u.u_exdata.ux_mag)
1473766Sroot 		printf("corefile not from this program");
1483766Sroot }
1493766Sroot 
1503777Sroot getpcb()
1513777Sroot {
1523779Sroot 
1533777Sroot 	lseek(fcor, masterpcbb&~0x80000000, 0);
1543777Sroot 	read(fcor, &pcb, sizeof (struct pcb));
1553779Sroot 	pcb.pcb_p0lr &= ~AST_CLR;
1563777Sroot 	printf("p0br %X p0lr %X p1br %X p1lr %X\n",
1573777Sroot 	    pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr);
1583777Sroot }
1593777Sroot 
1603766Sroot create(f)
1613766Sroot 	char *f;
1623766Sroot {
1633766Sroot 	register int fd;
1643766Sroot 
1653766Sroot 	fd = creat(f, 0644);
1663766Sroot 	if (fd < 0)
1673766Sroot 		return (-1);
1683766Sroot 	close(fd);
1693766Sroot 	return (open(f, wtflag));
1703766Sroot }
1713766Sroot 
1723766Sroot getfile(filnam, cnt)
1733766Sroot 	char *filnam;
1743766Sroot {
1753766Sroot 	register int fsym;
1763766Sroot 
1773766Sroot 	if (eqstr(filnam, "-"))
1783766Sroot 		return (-1);
1793766Sroot 	fsym = open(filnam, wtflag);
1803766Sroot 	if (fsym < 0 && xargc > cnt) {
1813766Sroot 		if (wtflag)
1823766Sroot 			fsym = create(filnam);
1833766Sroot 		if (fsym < 0)
1843766Sroot 			printf("cannot open `%s'\n", filnam);
1853766Sroot 	}
1863766Sroot 	return (fsym);
1873766Sroot }
1883766Sroot 
1893766Sroot setvar()
1903766Sroot {
1913766Sroot 
1923766Sroot 	var[varchk('b')] = datbas;
1933766Sroot 	var[varchk('d')] = filhdr.a_data;
1943766Sroot 	var[varchk('e')] = filhdr.a_entry;
1953766Sroot 	var[varchk('m')] = filhdr.a_magic;
1963766Sroot 	var[varchk('s')] = stksiz;
1973766Sroot 	var[varchk('t')] = filhdr.a_text;
1983766Sroot }
199