1*3779Sroot static char sccsid[] = "@(#)setup.c 4.4 05/15/81"; 23766Sroot /* 33766Sroot * adb - routines to read a.out+core at startup 43766Sroot */ 53766Sroot #include "defs.h" 63766Sroot #include <stat.h> 73766Sroot 83766Sroot off_t datbas; /* offset of the base of the data segment */ 93766Sroot off_t stksiz; /* stack size in the core image */ 103766Sroot INT sigcode; /* belongs in head.h */ 113766Sroot 123766Sroot char *symfil = "a.out"; 133766Sroot char *corfil = "core"; 143766Sroot 153766Sroot setsym() 163766Sroot { 173766Sroot off_t loc; 183766Sroot struct exec hdr; 193766Sroot register struct nlist *sp; 203766Sroot int ssiz; 213766Sroot char *strtab; 223766Sroot 233766Sroot fsym = getfile(symfil, 1); 243766Sroot txtmap.ufd = fsym; 253766Sroot if (read(fsym, (char *)&hdr, sizeof hdr) != sizeof hdr || 263766Sroot N_BADMAG(hdr)) { 273766Sroot txtmap.e1 = MAXFILE; 283766Sroot return; 293766Sroot } 303766Sroot filhdr = hdr; 313766Sroot loc = filhdr.a_text+filhdr.a_data; 323766Sroot txtmap.f1 = txtmap.f2 = N_TXTOFF(filhdr); 333766Sroot txtmap.b1 = 0; 343766Sroot switch (filhdr.a_magic) { 353766Sroot 363766Sroot case OMAGIC: 373766Sroot txtmap.b1 = txtmap.e1 = 0; 383766Sroot txtmap.b2 = datbas = 0; 393766Sroot txtmap.e2 = loc; 403766Sroot break; 413766Sroot 423766Sroot case ZMAGIC: 433766Sroot case NMAGIC: 443766Sroot txtmap.e1 = filhdr.a_text; 453766Sroot txtmap.b2 = datbas = round(filhdr.a_text, PAGSIZ); 463766Sroot txtmap.e2 = datbas + filhdr.a_data; 473766Sroot txtmap.f2 += txtmap.e1; 483766Sroot } 493766Sroot loc = N_SYMOFF(filhdr); 503766Sroot symtab = (struct nlist *) malloc(filhdr.a_syms); 513766Sroot esymtab = &symtab[filhdr.a_syms / sizeof (struct nlist)]; 523766Sroot if (symtab == NULL) 533766Sroot goto nospac; 543766Sroot lseek(fsym, loc, 0); 553766Sroot if (filhdr.a_syms == 0) 563766Sroot goto nosymt; 573766Sroot /* SHOULD SQUISH OUT STABS HERE!!! */ 583766Sroot if (read(fsym, symtab, filhdr.a_syms) != filhdr.a_syms) 593766Sroot goto readerr; 603766Sroot if (read(fsym, &ssiz, sizeof (ssiz)) != sizeof (ssiz)) 613766Sroot goto oldfmt; 623766Sroot strtab = (char *) malloc(ssiz); 633766Sroot if (strtab == 0) 643766Sroot goto nospac; 653766Sroot *(int *)strtab = ssiz; 663766Sroot ssiz -= sizeof (ssiz); 673766Sroot if (read(fsym, strtab + sizeof (ssiz), ssiz) != ssiz) 683766Sroot goto readerr; 693766Sroot for (sp = symtab; sp < esymtab; sp++) 703766Sroot if (sp->n_strx) 713766Sroot /* SHOULD PERFORM RANGE CHECK HERE */ 723766Sroot sp->n_un.n_name = strtab + sp->n_un.n_strx; 733766Sroot nosymt: 743766Sroot if (INKERNEL(filhdr.a_entry)) { 753766Sroot txtmap.b1 += KERNOFF; 763766Sroot txtmap.e1 += KERNOFF; 773766Sroot txtmap.b2 += KERNOFF; 783766Sroot txtmap.e2 += KERNOFF; 793766Sroot } 803766Sroot return; 813766Sroot readerr: 823766Sroot printf("Error reading symbol|string table\n"); 833766Sroot exit(1); 843766Sroot nospac: 853766Sroot printf("Not enough space for symbol|string table\n"); 863766Sroot exit(1); 873766Sroot oldfmt: 883766Sroot printf("Old format a.out - no string table\n"); 893766Sroot exit(1); 903766Sroot } 913766Sroot 923766Sroot setcor() 933766Sroot { 943766Sroot 953766Sroot fcor = datmap.ufd = getfile(corfil,2); 963766Sroot if (fcor != -1 && INKERNEL(filhdr.a_entry)) { 973766Sroot struct stat stb; 983766Sroot 99*3779Sroot kcore = 1; 1003766Sroot fstat(fcor, &stb); 1013766Sroot datmap.b1 = 0; 1023766Sroot datmap.e1 = -1; 1033778Sroot if (kernel == 0 && (stb.st_mode & S_IFREG)) 1043778Sroot datmap.b1 = 0x80000000; 1053777Sroot lookup("_Sysmap"); 1063777Sroot sbr = cursym->n_value; 1073777Sroot lookup("_Syssize"); 1083777Sroot slr = cursym->n_value; 1093777Sroot printf("sbr %X slr %X\n", sbr, slr); 1103777Sroot lookup("_masterpcbb"); 1113777Sroot physrw(fcor, cursym->n_value&0x7fffffff, &masterpcbb, 1); 1123777Sroot getpcb(); 1133766Sroot return; 1143766Sroot } 1153766Sroot if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) || 1163766Sroot !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) { 1173766Sroot datmap.e1 = MAXFILE; 1183766Sroot return; 1193766Sroot } 1203766Sroot signo = u.u_arg[0]; 1213766Sroot sigcode = u.u_code; 1223766Sroot filhdr.a_text = ctob(u.u_tsize); 1233766Sroot filhdr.a_data = ctob(u.u_dsize); 1243766Sroot stksiz = ctob(u.u_ssize); 1253766Sroot switch (filhdr.a_magic) { 1263766Sroot 1273766Sroot case OMAGIC: 1283766Sroot datmap.b1 = 0; 1293766Sroot datmap.e1 = filhdr.a_text+filhdr.a_data; 1303766Sroot datmap.f2 = ctob(UPAGES) + datmap.e1; 1313766Sroot break; 1323766Sroot 1333766Sroot case NMAGIC: 1343766Sroot case ZMAGIC: 1353766Sroot datmap.b1 = round(filhdr.a_text, PAGSIZ); 1363766Sroot datmap.e1 = datmap.b1 + filhdr.a_data; 1373766Sroot datmap.f2 = ctob(UPAGES) + filhdr.a_data; 1383766Sroot break; 1393766Sroot } 1403766Sroot datbas = datmap.b1; 1413766Sroot datmap.f1 = ctob(UPAGES); 1423766Sroot datmap.b2 = MAXSTOR - stksiz; 1433766Sroot datmap.e2 = MAXSTOR; 1443766Sroot if (filhdr.a_magic && u.u_exdata.ux_mag && 1453766Sroot filhdr.a_magic != u.u_exdata.ux_mag) 1463766Sroot printf("corefile not from this program"); 1473766Sroot } 1483766Sroot 1493777Sroot getpcb() 1503777Sroot { 151*3779Sroot 1523777Sroot lseek(fcor, masterpcbb&~0x80000000, 0); 1533777Sroot read(fcor, &pcb, sizeof (struct pcb)); 154*3779Sroot pcb.pcb_p0lr &= ~AST_CLR; 1553777Sroot printf("p0br %X p0lr %X p1br %X p1lr %X\n", 1563777Sroot pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr); 1573777Sroot } 1583777Sroot 1593766Sroot create(f) 1603766Sroot char *f; 1613766Sroot { 1623766Sroot register int fd; 1633766Sroot 1643766Sroot fd = creat(f, 0644); 1653766Sroot if (fd < 0) 1663766Sroot return (-1); 1673766Sroot close(fd); 1683766Sroot return (open(f, wtflag)); 1693766Sroot } 1703766Sroot 1713766Sroot getfile(filnam, cnt) 1723766Sroot char *filnam; 1733766Sroot { 1743766Sroot register int fsym; 1753766Sroot 1763766Sroot if (eqstr(filnam, "-")) 1773766Sroot return (-1); 1783766Sroot fsym = open(filnam, wtflag); 1793766Sroot if (fsym < 0 && xargc > cnt) { 1803766Sroot if (wtflag) 1813766Sroot fsym = create(filnam); 1823766Sroot if (fsym < 0) 1833766Sroot printf("cannot open `%s'\n", filnam); 1843766Sroot } 1853766Sroot return (fsym); 1863766Sroot } 1873766Sroot 1883766Sroot setvar() 1893766Sroot { 1903766Sroot 1913766Sroot var[varchk('b')] = datbas; 1923766Sroot var[varchk('d')] = filhdr.a_data; 1933766Sroot var[varchk('e')] = filhdr.a_entry; 1943766Sroot var[varchk('m')] = filhdr.a_magic; 1953766Sroot var[varchk('s')] = stksiz; 1963766Sroot var[varchk('t')] = filhdr.a_text; 1973766Sroot } 198