xref: /original-bsd/old/adb/adb.vax/setup.c (revision b3b53e97)
1 static	char sccsid[] = "@(#)setup.c	4.5 82/04/01";
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 (kernel && fcor != -1 && INKERNEL(filhdr.a_entry)) {
97 		struct stat stb;
98 
99 		kcore = 1;
100 		fstat(fcor, &stb);
101 		datmap.b1 = 0;
102 		datmap.e1 = -1;
103 		if (kernel == 0 && (stb.st_mode & S_IFREG))
104 			datmap.b1 = 0x80000000;
105 		lookup("_Sysmap");
106 		sbr = cursym->n_value;
107 		lookup("_Syssize");
108 		slr = cursym->n_value;
109 		printf("sbr %X slr %X\n", sbr, slr);
110 		lookup("_masterpaddr");
111 		physrw(fcor, cursym->n_value&0x7fffffff, &masterpcbb, 1);
112 		masterpcbb = (masterpcbb&PG_PFNUM)*512;
113 		getpcb();
114 		return;
115 	}
116 	if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) ||
117 	   !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) {
118 		datmap.e1 = MAXFILE;
119 		return;
120 	}
121 	signo = u.u_arg[0];
122 	sigcode = u.u_code;
123 	filhdr.a_text = ctob(u.u_tsize);
124 	filhdr.a_data = ctob(u.u_dsize);
125 	stksiz = ctob(u.u_ssize);
126 	switch (filhdr.a_magic) {
127 
128 	case OMAGIC:
129 		datmap.b1 = 0;
130 		datmap.e1 = filhdr.a_text+filhdr.a_data;
131 		datmap.f2 = ctob(UPAGES) + datmap.e1;
132 		break;
133 
134 	case NMAGIC:
135 	case ZMAGIC:
136 		datmap.b1 = round(filhdr.a_text, PAGSIZ);
137 		datmap.e1 = datmap.b1 + filhdr.a_data;
138 		datmap.f2 = ctob(UPAGES) + filhdr.a_data;
139 		break;
140 	}
141 	datbas = datmap.b1;
142 	datmap.f1 = ctob(UPAGES);
143 	datmap.b2 = MAXSTOR - stksiz;
144 	datmap.e2 = MAXSTOR;
145 	if (filhdr.a_magic && u.u_exdata.ux_mag &&
146 	    filhdr.a_magic != u.u_exdata.ux_mag)
147 		printf("corefile not from this program");
148 }
149 
150 getpcb()
151 {
152 
153 	lseek(fcor, masterpcbb&~0x80000000, 0);
154 	read(fcor, &pcb, sizeof (struct pcb));
155 	pcb.pcb_p0lr &= ~AST_CLR;
156 	printf("p0br %X p0lr %X p1br %X p1lr %X\n",
157 	    pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr);
158 }
159 
160 create(f)
161 	char *f;
162 {
163 	register int fd;
164 
165 	fd = creat(f, 0644);
166 	if (fd < 0)
167 		return (-1);
168 	close(fd);
169 	return (open(f, wtflag));
170 }
171 
172 getfile(filnam, cnt)
173 	char *filnam;
174 {
175 	register int fsym;
176 
177 	if (eqstr(filnam, "-"))
178 		return (-1);
179 	fsym = open(filnam, wtflag);
180 	if (fsym < 0 && xargc > cnt) {
181 		if (wtflag)
182 			fsym = create(filnam);
183 		if (fsym < 0)
184 			printf("cannot open `%s'\n", filnam);
185 	}
186 	return (fsym);
187 }
188 
189 setvar()
190 {
191 
192 	var[varchk('b')] = datbas;
193 	var[varchk('d')] = filhdr.a_data;
194 	var[varchk('e')] = filhdr.a_entry;
195 	var[varchk('m')] = filhdr.a_magic;
196 	var[varchk('s')] = stksiz;
197 	var[varchk('t')] = filhdr.a_text;
198 }
199