xref: /csrg-svn/old/adb/common_source/setup.c (revision 36559)
1*36559Sbostic #ifndef lint
2*36559Sbostic static char sccsid[] = "@(#)setup.c	5.1 (Berkeley) 01/16/89";
3*36559Sbostic #endif
4*36559Sbostic 
5*36559Sbostic /*
6*36559Sbostic  * adb - routines to read a.out and core files at startup
7*36559Sbostic  */
8*36559Sbostic 
9*36559Sbostic #include "defs.h"
10*36559Sbostic #include <sys/file.h>
11*36559Sbostic 
12*36559Sbostic static struct exec filhdr;
13*36559Sbostic 
14*36559Sbostic off_t	lseek();
15*36559Sbostic char	*malloc();
16*36559Sbostic 
17*36559Sbostic /* NB. the following works only with letter (alpha) variables */
18*36559Sbostic #define	setavar(name, value) (var[(name) - 'a' + 10] = (value))
19*36559Sbostic 
20*36559Sbostic setsym()
21*36559Sbostic {
22*36559Sbostic 	register struct nlist *sp;
23*36559Sbostic 	int strsize;
24*36559Sbostic 	char *strtab;
25*36559Sbostic 	off_t loc, dbase;
26*36559Sbostic 
27*36559Sbostic 	txtmap.ufd = symfile.fd = getfile(1);
28*36559Sbostic 	if (read(symfile.fd, (char *)&filhdr, sizeof(filhdr)) != sizeof(filhdr) ||
29*36559Sbostic 	    N_BADMAG(filhdr)) {
30*36559Sbostic 		bzero((char *)&filhdr, sizeof(filhdr));
31*36559Sbostic 		txtmap.m1.e = -(addr_t)1;
32*36559Sbostic 		return;
33*36559Sbostic 	}
34*36559Sbostic 	loc = filhdr.a_text + filhdr.a_data;
35*36559Sbostic 	txtmap.m1.f = txtmap.m2.f = N_TXTOFF(filhdr);
36*36559Sbostic 	switch ((int)filhdr.a_magic) {
37*36559Sbostic 
38*36559Sbostic 	case OMAGIC:
39*36559Sbostic 		/* text map 1 is empty; map 2 goes from 0 to loc */
40*36559Sbostic 		txtmap.m2.b = dbase = 0;
41*36559Sbostic 		txtmap.m2.e = loc;
42*36559Sbostic 		break;
43*36559Sbostic 
44*36559Sbostic 	case ZMAGIC:
45*36559Sbostic 	case NMAGIC:
46*36559Sbostic 		/* text map 1 maps text segment, map 2 maps data */
47*36559Sbostic 		txtmap.m1.e = filhdr.a_text;
48*36559Sbostic 		txtmap.m2.b = dbase = roundup(filhdr.a_text, CLBYTES);
49*36559Sbostic 		txtmap.m2.e = dbase + filhdr.a_data;
50*36559Sbostic 		txtmap.m2.f += txtmap.m1.e;
51*36559Sbostic 		break;
52*36559Sbostic 	}
53*36559Sbostic 
54*36559Sbostic 	/* save data segment base in variable b */
55*36559Sbostic 	setavar('b', dbase);
56*36559Sbostic 
57*36559Sbostic 	if (filhdr.a_syms != 0) {
58*36559Sbostic 		loc = N_SYMOFF(filhdr);
59*36559Sbostic 		symtab = (struct nlist *)malloc((u_int)filhdr.a_syms);
60*36559Sbostic 		if (symtab == NULL)
61*36559Sbostic 			goto nospace;
62*36559Sbostic 		esymtab = &symtab[filhdr.a_syms / sizeof(struct nlist)];
63*36559Sbostic 		(void) lseek(symfile.fd, loc, L_SET);
64*36559Sbostic 
65*36559Sbostic #define	rd(a, n) \
66*36559Sbostic 	if (read(symfile.fd, (char *)(a), (int)(n)) != (n)) \
67*36559Sbostic 		goto readerr
68*36559Sbostic 
69*36559Sbostic 		rd(symtab, filhdr.a_syms);
70*36559Sbostic 		rd(&strsize, sizeof(strsize));
71*36559Sbostic 		/*
72*36559Sbostic 		 * offsets in the string table are relative to the offset
73*36559Sbostic 		 * of the number we just read; we adjust for it here.
74*36559Sbostic 		 */
75*36559Sbostic 		strsize -= sizeof(strsize);
76*36559Sbostic 		if ((strtab = malloc((u_int)strsize)) == NULL)
77*36559Sbostic 			goto nospace;
78*36559Sbostic 		rd(strtab, strsize);
79*36559Sbostic 		for (sp = symtab; sp < esymtab; sp++) {
80*36559Sbostic 			if (sp->n_un.n_strx == 0)
81*36559Sbostic 				continue;
82*36559Sbostic 			sp->n_un.n_strx -= sizeof(strsize);
83*36559Sbostic 			if ((u_long)sp->n_un.n_strx >= strsize) {
84*36559Sbostic 				adbprintf("bad string index %D in symtab\n",
85*36559Sbostic 				    (expr_t)sp->n_un.n_strx);
86*36559Sbostic 				sp->n_un.n_name = "";
87*36559Sbostic 			} else
88*36559Sbostic 				sp->n_un.n_name = strtab + sp->n_un.n_strx;
89*36559Sbostic 		}
90*36559Sbostic #undef rd
91*36559Sbostic 	}
92*36559Sbostic 	if (INKERNEL(filhdr.a_entry)) {
93*36559Sbostic 		txtmap.m1.b += KERNTEXTOFF;
94*36559Sbostic 		txtmap.m1.e += KERNTEXTOFF;
95*36559Sbostic 		txtmap.m2.b += KERNTEXTOFF;
96*36559Sbostic 		txtmap.m2.e += KERNTEXTOFF;
97*36559Sbostic 	}
98*36559Sbostic 	return;
99*36559Sbostic 
100*36559Sbostic readerr:
101*36559Sbostic 	prints("Error reading symbol|string table (old format a.out?)\n");
102*36559Sbostic 	exit(1);
103*36559Sbostic 	/* NOTREACHED */
104*36559Sbostic 
105*36559Sbostic nospace:
106*36559Sbostic 	prints("Not enough space for symbol|string table\n");
107*36559Sbostic 	exit(1);
108*36559Sbostic 	/* NOTREACHED */
109*36559Sbostic }
110*36559Sbostic 
111*36559Sbostic setcore()
112*36559Sbostic {
113*36559Sbostic 	off_t stacksize;
114*36559Sbostic 
115*36559Sbostic 	datmap.m1.e = -(addr_t)1;
116*36559Sbostic 	if ((datmap.ufd = corefile.fd = getfile(2)) < 0)
117*36559Sbostic 		goto ret;
118*36559Sbostic 	if (kernel && INKERNEL(filhdr.a_entry) && getkcore()) {
119*36559Sbostic 		kcore = 1;
120*36559Sbostic 		goto ret;
121*36559Sbostic 	}
122*36559Sbostic 	if (read(corefile.fd, (char *)&u, ctob(UPAGES)) != ctob(UPAGES) ||
123*36559Sbostic 	    !udot()) {
124*36559Sbostic 		adbprintf("not core file = %s\n", corefile.name);
125*36559Sbostic 		goto ret;
126*36559Sbostic 	}
127*36559Sbostic 	signo = u.u_arg[0];
128*36559Sbostic 	sigcode = u.u_code;
129*36559Sbostic 	filhdr.a_text = ctob(u.u_tsize);
130*36559Sbostic 	filhdr.a_data = ctob(u.u_dsize);
131*36559Sbostic 	stacksize = ctob(u.u_ssize);
132*36559Sbostic 	switch ((int)filhdr.a_magic) {
133*36559Sbostic 
134*36559Sbostic 	case OMAGIC:
135*36559Sbostic 		datmap.m1.b = 0;
136*36559Sbostic 		datmap.m1.e = filhdr.a_text + filhdr.a_data;
137*36559Sbostic 		datmap.m2.f = ctob(UPAGES) + datmap.m1.e;
138*36559Sbostic 		break;
139*36559Sbostic 
140*36559Sbostic 	case NMAGIC:
141*36559Sbostic 	case ZMAGIC:
142*36559Sbostic 		datmap.m1.b = roundup(filhdr.a_text, CLBYTES);
143*36559Sbostic 		datmap.m1.e = datmap.m1.b + filhdr.a_data;
144*36559Sbostic 		datmap.m2.f = ctob(UPAGES) + filhdr.a_data;
145*36559Sbostic 		break;
146*36559Sbostic 	}
147*36559Sbostic 	/* save (possibly new) data segment base, and save stack size */
148*36559Sbostic 	setavar('b', datmap.m1.b);
149*36559Sbostic 	setavar('s', stacksize);
150*36559Sbostic 	datmap.m1.f = ctob(UPAGES);
151*36559Sbostic 	datmap.m2.b = KERNBASE - ctob(UPAGES) - stacksize;
152*36559Sbostic 	datmap.m2.e = KERNBASE - ctob(UPAGES);
153*36559Sbostic ret:
154*36559Sbostic 	u.u_ar0 = (int *)((caddr_t)&u + ctob(UPAGES));	/* XXX */
155*36559Sbostic 	setavar('d', filhdr.a_data);
156*36559Sbostic 	setavar('e', filhdr.a_entry);
157*36559Sbostic 	setavar('m', filhdr.a_magic);
158*36559Sbostic 	setavar('t', filhdr.a_text);
159*36559Sbostic }
160