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