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