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