xref: /csrg-svn/old/adb/adb.vax/setup.c (revision 3766)
1*3766Sroot static	char sccsid[] = "@(#)setup.c 4.1 05/14/81";
2*3766Sroot /*
3*3766Sroot  * adb - routines to read a.out+core at startup
4*3766Sroot  */
5*3766Sroot #include "defs.h"
6*3766Sroot #include <stat.h>
7*3766Sroot 
8*3766Sroot off_t	datbas;			/* offset of the base of the data segment */
9*3766Sroot off_t	stksiz;			/* stack size in the core image */
10*3766Sroot INT	sigcode;	/* belongs in head.h */
11*3766Sroot 
12*3766Sroot char	*symfil	= "a.out";
13*3766Sroot char	*corfil	= "core";
14*3766Sroot 
15*3766Sroot setsym()
16*3766Sroot {
17*3766Sroot 	off_t loc;
18*3766Sroot 	struct exec hdr;
19*3766Sroot 	register struct nlist *sp;
20*3766Sroot 	int ssiz;
21*3766Sroot 	char *strtab;
22*3766Sroot 
23*3766Sroot 	fsym = getfile(symfil, 1);
24*3766Sroot 	txtmap.ufd = fsym;
25*3766Sroot 	if (read(fsym, (char *)&hdr, sizeof hdr) != sizeof hdr ||
26*3766Sroot 	    N_BADMAG(hdr)) {
27*3766Sroot 		txtmap.e1 = MAXFILE;
28*3766Sroot 		return;
29*3766Sroot 	}
30*3766Sroot 	filhdr = hdr;
31*3766Sroot 	loc = filhdr.a_text+filhdr.a_data;
32*3766Sroot 	txtmap.f1 = txtmap.f2 = N_TXTOFF(filhdr);
33*3766Sroot 	txtmap.b1 = 0;
34*3766Sroot 	switch (filhdr.a_magic) {
35*3766Sroot 
36*3766Sroot 	case OMAGIC:
37*3766Sroot 		txtmap.b1 = txtmap.e1 = 0;
38*3766Sroot 		txtmap.b2 = datbas = 0;
39*3766Sroot 		txtmap.e2 = loc;
40*3766Sroot 		break;
41*3766Sroot 
42*3766Sroot 	case ZMAGIC:
43*3766Sroot 	case NMAGIC:
44*3766Sroot 		txtmap.e1 = filhdr.a_text;
45*3766Sroot 		txtmap.b2 = datbas = round(filhdr.a_text, PAGSIZ);
46*3766Sroot 		txtmap.e2 = datbas + filhdr.a_data;
47*3766Sroot 		txtmap.f2 += txtmap.e1;
48*3766Sroot 	}
49*3766Sroot 	loc = N_SYMOFF(filhdr);
50*3766Sroot 	symtab = (struct nlist *) malloc(filhdr.a_syms);
51*3766Sroot 	esymtab = &symtab[filhdr.a_syms / sizeof (struct nlist)];
52*3766Sroot 	if (symtab == NULL)
53*3766Sroot 		goto nospac;
54*3766Sroot 	lseek(fsym, loc, 0);
55*3766Sroot 	if (filhdr.a_syms == 0)
56*3766Sroot 		goto nosymt;
57*3766Sroot 	/* SHOULD SQUISH OUT STABS HERE!!! */
58*3766Sroot 	if (read(fsym, symtab, filhdr.a_syms) != filhdr.a_syms)
59*3766Sroot 		goto readerr;
60*3766Sroot 	if (read(fsym, &ssiz, sizeof (ssiz)) != sizeof (ssiz))
61*3766Sroot 		goto oldfmt;
62*3766Sroot 	strtab = (char *) malloc(ssiz);
63*3766Sroot 	if (strtab == 0)
64*3766Sroot 		goto nospac;
65*3766Sroot 	*(int *)strtab = ssiz;
66*3766Sroot 	ssiz -= sizeof (ssiz);
67*3766Sroot 	if (read(fsym, strtab + sizeof (ssiz), ssiz) != ssiz)
68*3766Sroot 		goto readerr;
69*3766Sroot 	for (sp = symtab; sp < esymtab; sp++)
70*3766Sroot 		if (sp->n_strx)
71*3766Sroot 			/* SHOULD PERFORM RANGE CHECK HERE */
72*3766Sroot 			sp->n_un.n_name = strtab + sp->n_un.n_strx;
73*3766Sroot nosymt:
74*3766Sroot 	if (INKERNEL(filhdr.a_entry)) {
75*3766Sroot 		txtmap.b1 += KERNOFF;
76*3766Sroot 		txtmap.e1 += KERNOFF;
77*3766Sroot 		txtmap.b2 += KERNOFF;
78*3766Sroot 		txtmap.e2 += KERNOFF;
79*3766Sroot 	}
80*3766Sroot 	return;
81*3766Sroot readerr:
82*3766Sroot 	printf("Error reading symbol|string table\n");
83*3766Sroot 	exit(1);
84*3766Sroot nospac:
85*3766Sroot 	printf("Not enough space for symbol|string table\n");
86*3766Sroot 	exit(1);
87*3766Sroot oldfmt:
88*3766Sroot 	printf("Old format a.out - no string table\n");
89*3766Sroot 	exit(1);
90*3766Sroot }
91*3766Sroot 
92*3766Sroot setcor()
93*3766Sroot {
94*3766Sroot 
95*3766Sroot 	fcor = datmap.ufd = getfile(corfil,2);
96*3766Sroot 	if (fcor != -1 && INKERNEL(filhdr.a_entry)) {
97*3766Sroot 		struct stat stb;
98*3766Sroot 
99*3766Sroot 		fstat(fcor, &stb);
100*3766Sroot 		datmap.b1 = 0;
101*3766Sroot 		datmap.e1 = -1;
102*3766Sroot 		if ((stb.st_mode&S_IFMT) == S_IFREG)
103*3766Sroot 			datmap.b1 = 0x80000000;
104*3766Sroot 		return;
105*3766Sroot 	}
106*3766Sroot 	if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) ||
107*3766Sroot 	   !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) {
108*3766Sroot 		datmap.e1 = MAXFILE;
109*3766Sroot 		return;
110*3766Sroot 	}
111*3766Sroot 	signo = u.u_arg[0];
112*3766Sroot 	sigcode = u.u_code;
113*3766Sroot 	filhdr.a_text = ctob(u.u_tsize);
114*3766Sroot 	filhdr.a_data = ctob(u.u_dsize);
115*3766Sroot 	stksiz = ctob(u.u_ssize);
116*3766Sroot 	switch (filhdr.a_magic) {
117*3766Sroot 
118*3766Sroot 	case OMAGIC:
119*3766Sroot 		datmap.b1 = 0;
120*3766Sroot 		datmap.e1 = filhdr.a_text+filhdr.a_data;
121*3766Sroot 		datmap.f2 = ctob(UPAGES) + datmap.e1;
122*3766Sroot 		break;
123*3766Sroot 
124*3766Sroot 	case NMAGIC:
125*3766Sroot 	case ZMAGIC:
126*3766Sroot 		datmap.b1 = round(filhdr.a_text, PAGSIZ);
127*3766Sroot 		datmap.e1 = datmap.b1 + filhdr.a_data;
128*3766Sroot 		datmap.f2 = ctob(UPAGES) + filhdr.a_data;
129*3766Sroot 		break;
130*3766Sroot 	}
131*3766Sroot 	datbas = datmap.b1;
132*3766Sroot 	datmap.f1 = ctob(UPAGES);
133*3766Sroot 	datmap.b2 = MAXSTOR - stksiz;
134*3766Sroot 	datmap.e2 = MAXSTOR;
135*3766Sroot 	if (filhdr.a_magic && u.u_exdata.ux_mag &&
136*3766Sroot 	    filhdr.a_magic != u.u_exdata.ux_mag)
137*3766Sroot 		printf("corefile not from this program");
138*3766Sroot }
139*3766Sroot 
140*3766Sroot create(f)
141*3766Sroot 	char *f;
142*3766Sroot {
143*3766Sroot 	register int fd;
144*3766Sroot 
145*3766Sroot 	fd = creat(f, 0644);
146*3766Sroot 	if (fd < 0)
147*3766Sroot 		return (-1);
148*3766Sroot 	close(fd);
149*3766Sroot 	return (open(f, wtflag));
150*3766Sroot }
151*3766Sroot 
152*3766Sroot getfile(filnam, cnt)
153*3766Sroot 	char *filnam;
154*3766Sroot {
155*3766Sroot 	register int fsym;
156*3766Sroot 
157*3766Sroot 	if (eqstr(filnam, "-"))
158*3766Sroot 		return (-1);
159*3766Sroot 	fsym = open(filnam, wtflag);
160*3766Sroot 	if (fsym < 0 && xargc > cnt) {
161*3766Sroot 		if (wtflag)
162*3766Sroot 			fsym = create(filnam);
163*3766Sroot 		if (fsym < 0)
164*3766Sroot 			printf("cannot open `%s'\n", filnam);
165*3766Sroot 	}
166*3766Sroot 	return (fsym);
167*3766Sroot }
168*3766Sroot 
169*3766Sroot setvar()
170*3766Sroot {
171*3766Sroot 
172*3766Sroot 	var[varchk('b')] = datbas;
173*3766Sroot 	var[varchk('d')] = filhdr.a_data;
174*3766Sroot 	var[varchk('e')] = filhdr.a_entry;
175*3766Sroot 	var[varchk('m')] = filhdr.a_magic;
176*3766Sroot 	var[varchk('s')] = stksiz;
177*3766Sroot 	var[varchk('t')] = filhdr.a_text;
178*3766Sroot }
179