1*47822Sbostic /*-
2*47822Sbostic * Copyright (c) 1991 The Regents of the University of California.
3*47822Sbostic * All rights reserved.
4*47822Sbostic *
5*47822Sbostic * %sccs.include.proprietary.c%
6*47822Sbostic */
7*47822Sbostic
836559Sbostic #ifndef lint
9*47822Sbostic static char sccsid[] = "@(#)setup.c 5.5 (Berkeley) 04/04/91";
10*47822Sbostic #endif /* not lint */
1136559Sbostic
1236559Sbostic /*
1336559Sbostic * adb - routines to read a.out and core files at startup
1436559Sbostic */
1536559Sbostic
1636559Sbostic #include "defs.h"
1736559Sbostic #include <sys/file.h>
1836559Sbostic
1936559Sbostic static struct exec filhdr;
2036559Sbostic
2136559Sbostic off_t lseek();
2236559Sbostic char *malloc();
2336559Sbostic
2436559Sbostic /* NB. the following works only with letter (alpha) variables */
2536559Sbostic #define setavar(name, value) (var[(name) - 'a' + 10] = (value))
2636559Sbostic
setsym()2736559Sbostic setsym()
2836559Sbostic {
2936559Sbostic register struct nlist *sp;
3036559Sbostic int strsize;
3136559Sbostic char *strtab;
3236559Sbostic off_t loc, dbase;
3336559Sbostic
3436559Sbostic txtmap.ufd = symfile.fd = getfile(1);
3536559Sbostic if (read(symfile.fd, (char *)&filhdr, sizeof(filhdr)) != sizeof(filhdr) ||
3636559Sbostic N_BADMAG(filhdr)) {
3736559Sbostic bzero((char *)&filhdr, sizeof(filhdr));
3836559Sbostic txtmap.m1.e = -(addr_t)1;
3936559Sbostic return;
4036559Sbostic }
4136559Sbostic loc = filhdr.a_text + filhdr.a_data;
4236559Sbostic txtmap.m1.f = txtmap.m2.f = N_TXTOFF(filhdr);
4336559Sbostic switch ((int)filhdr.a_magic) {
4436559Sbostic
4536559Sbostic case OMAGIC:
4636559Sbostic /* text map 1 is empty; map 2 goes from 0 to loc */
4736559Sbostic txtmap.m2.b = dbase = 0;
4836559Sbostic txtmap.m2.e = loc;
4936559Sbostic break;
5036559Sbostic
5136559Sbostic case ZMAGIC:
5236559Sbostic case NMAGIC:
5336559Sbostic /* text map 1 maps text segment, map 2 maps data */
5436559Sbostic txtmap.m1.e = filhdr.a_text;
5536559Sbostic txtmap.m2.b = dbase = roundup(filhdr.a_text, CLBYTES);
5636559Sbostic txtmap.m2.e = dbase + filhdr.a_data;
5736559Sbostic txtmap.m2.f += txtmap.m1.e;
5836559Sbostic break;
5936559Sbostic }
6036559Sbostic
6136559Sbostic /* save data segment base in variable b */
6236559Sbostic setavar('b', dbase);
6336559Sbostic
6436559Sbostic if (filhdr.a_syms != 0) {
6536559Sbostic loc = N_SYMOFF(filhdr);
6636559Sbostic symtab = (struct nlist *)malloc((u_int)filhdr.a_syms);
6736559Sbostic if (symtab == NULL)
6836559Sbostic goto nospace;
6936559Sbostic esymtab = &symtab[filhdr.a_syms / sizeof(struct nlist)];
7036559Sbostic (void) lseek(symfile.fd, loc, L_SET);
7136559Sbostic
7236559Sbostic #define rd(a, n) \
7336559Sbostic if (read(symfile.fd, (char *)(a), (int)(n)) != (n)) \
7436559Sbostic goto readerr
7536559Sbostic
7636559Sbostic rd(symtab, filhdr.a_syms);
7736559Sbostic rd(&strsize, sizeof(strsize));
7836559Sbostic /*
7936559Sbostic * offsets in the string table are relative to the offset
8036559Sbostic * of the number we just read; we adjust for it here.
8136559Sbostic */
8236559Sbostic strsize -= sizeof(strsize);
8336559Sbostic if ((strtab = malloc((u_int)strsize)) == NULL)
8436559Sbostic goto nospace;
8536559Sbostic rd(strtab, strsize);
8636559Sbostic for (sp = symtab; sp < esymtab; sp++) {
8736559Sbostic if (sp->n_un.n_strx == 0)
8836559Sbostic continue;
8936559Sbostic sp->n_un.n_strx -= sizeof(strsize);
9036559Sbostic if ((u_long)sp->n_un.n_strx >= strsize) {
9136559Sbostic adbprintf("bad string index %D in symtab\n",
9236559Sbostic (expr_t)sp->n_un.n_strx);
9336559Sbostic sp->n_un.n_name = "";
9436559Sbostic } else
9536559Sbostic sp->n_un.n_name = strtab + sp->n_un.n_strx;
9636559Sbostic }
9736559Sbostic #undef rd
9836559Sbostic }
9936559Sbostic if (INKERNEL(filhdr.a_entry)) {
10036559Sbostic txtmap.m1.b += KERNTEXTOFF;
10136559Sbostic txtmap.m1.e += KERNTEXTOFF;
10236559Sbostic txtmap.m2.b += KERNTEXTOFF;
10336559Sbostic txtmap.m2.e += KERNTEXTOFF;
10436559Sbostic }
10536559Sbostic return;
10636559Sbostic
10736559Sbostic readerr:
10836559Sbostic prints("Error reading symbol|string table (old format a.out?)\n");
10936559Sbostic exit(1);
11036559Sbostic /* NOTREACHED */
11136559Sbostic
11236559Sbostic nospace:
11336559Sbostic prints("Not enough space for symbol|string table\n");
11436559Sbostic exit(1);
11536559Sbostic /* NOTREACHED */
11636559Sbostic }
11736559Sbostic
setcore()11836559Sbostic setcore()
11936559Sbostic {
12036559Sbostic off_t stacksize;
12136559Sbostic
12236559Sbostic datmap.m1.e = -(addr_t)1;
12336559Sbostic if ((datmap.ufd = corefile.fd = getfile(2)) < 0)
12436559Sbostic goto ret;
12536559Sbostic if (kernel && INKERNEL(filhdr.a_entry) && getkcore()) {
12636559Sbostic kcore = 1;
12736559Sbostic goto ret;
12836559Sbostic }
12936559Sbostic if (read(corefile.fd, (char *)&u, ctob(UPAGES)) != ctob(UPAGES) ||
13036559Sbostic !udot()) {
13136559Sbostic adbprintf("not core file = %s\n", corefile.name);
13236559Sbostic goto ret;
13336559Sbostic }
13443884Smckusick signo = u.u_sig;
13536559Sbostic sigcode = u.u_code;
13636559Sbostic filhdr.a_text = ctob(u.u_tsize);
13736559Sbostic filhdr.a_data = ctob(u.u_dsize);
13836559Sbostic stacksize = ctob(u.u_ssize);
13936559Sbostic switch ((int)filhdr.a_magic) {
14036559Sbostic
14136559Sbostic case OMAGIC:
14236559Sbostic datmap.m1.b = 0;
14336559Sbostic datmap.m1.e = filhdr.a_text + filhdr.a_data;
14436559Sbostic datmap.m2.f = ctob(UPAGES) + datmap.m1.e;
14536559Sbostic break;
14636559Sbostic
14736559Sbostic case NMAGIC:
14836559Sbostic case ZMAGIC:
14936559Sbostic datmap.m1.b = roundup(filhdr.a_text, CLBYTES);
15036559Sbostic datmap.m1.e = datmap.m1.b + filhdr.a_data;
15136559Sbostic datmap.m2.f = ctob(UPAGES) + filhdr.a_data;
15236559Sbostic break;
15336559Sbostic }
15436559Sbostic /* save (possibly new) data segment base, and save stack size */
15536559Sbostic setavar('b', datmap.m1.b);
15636559Sbostic setavar('s', stacksize);
15736559Sbostic datmap.m1.f = ctob(UPAGES);
15836559Sbostic datmap.m2.b = KERNBASE - ctob(UPAGES) - stacksize;
15936559Sbostic datmap.m2.e = KERNBASE - ctob(UPAGES);
16036559Sbostic ret:
16136559Sbostic u.u_ar0 = (int *)((caddr_t)&u + ctob(UPAGES)); /* XXX */
16236559Sbostic setavar('d', filhdr.a_data);
16336559Sbostic setavar('e', filhdr.a_entry);
16436559Sbostic setavar('m', filhdr.a_magic);
16536559Sbostic setavar('t', filhdr.a_text);
16636559Sbostic }
167