1*3778Sroot static char sccsid[] = "@(#)setup.c 4.3 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 993766Sroot fstat(fcor, &stb); 1003777Sroot if (stb.st_mode & S_IFREG) 1013777Sroot kcore = 1; 1023766Sroot datmap.b1 = 0; 1033766Sroot datmap.e1 = -1; 104*3778Sroot if (kernel == 0 && (stb.st_mode & S_IFREG)) 105*3778Sroot datmap.b1 = 0x80000000; 1063777Sroot lookup("_Sysmap"); 1073777Sroot sbr = cursym->n_value; 1083777Sroot lookup("_Syssize"); 1093777Sroot slr = cursym->n_value; 1103777Sroot printf("sbr %X slr %X\n", sbr, slr); 1113777Sroot lookup("_masterpcbb"); 1123777Sroot printf("masterpcbb at %X\n", cursym->n_value); 1133777Sroot physrw(fcor, cursym->n_value&0x7fffffff, &masterpcbb, 1); 1143777Sroot printf("masterpcbb value %X\n", masterpcbb); 1153777Sroot var[varchk('p')] = masterpcbb; 1163777Sroot getpcb(); 1173766Sroot return; 1183766Sroot } 1193766Sroot if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) || 1203766Sroot !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) { 1213766Sroot datmap.e1 = MAXFILE; 1223766Sroot return; 1233766Sroot } 1243766Sroot signo = u.u_arg[0]; 1253766Sroot sigcode = u.u_code; 1263766Sroot filhdr.a_text = ctob(u.u_tsize); 1273766Sroot filhdr.a_data = ctob(u.u_dsize); 1283766Sroot stksiz = ctob(u.u_ssize); 1293766Sroot switch (filhdr.a_magic) { 1303766Sroot 1313766Sroot case OMAGIC: 1323766Sroot datmap.b1 = 0; 1333766Sroot datmap.e1 = filhdr.a_text+filhdr.a_data; 1343766Sroot datmap.f2 = ctob(UPAGES) + datmap.e1; 1353766Sroot break; 1363766Sroot 1373766Sroot case NMAGIC: 1383766Sroot case ZMAGIC: 1393766Sroot datmap.b1 = round(filhdr.a_text, PAGSIZ); 1403766Sroot datmap.e1 = datmap.b1 + filhdr.a_data; 1413766Sroot datmap.f2 = ctob(UPAGES) + filhdr.a_data; 1423766Sroot break; 1433766Sroot } 1443766Sroot datbas = datmap.b1; 1453766Sroot datmap.f1 = ctob(UPAGES); 1463766Sroot datmap.b2 = MAXSTOR - stksiz; 1473766Sroot datmap.e2 = MAXSTOR; 1483766Sroot if (filhdr.a_magic && u.u_exdata.ux_mag && 1493766Sroot filhdr.a_magic != u.u_exdata.ux_mag) 1503766Sroot printf("corefile not from this program"); 1513766Sroot } 1523766Sroot 1533777Sroot getpcb() 1543777Sroot { 1553777Sroot printf("getpcb: masterpcbb is %X\n", masterpcbb); 1563777Sroot lseek(fcor, masterpcbb&~0x80000000, 0); 1573777Sroot read(fcor, &pcb, sizeof (struct pcb)); 1583777Sroot printf("p0br %X p0lr %X p1br %X p1lr %X\n", 1593777Sroot pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr); 1603777Sroot pcb.pcb_p0lr &= ~AST_CLR; 1613777Sroot } 1623777Sroot 1633766Sroot create(f) 1643766Sroot char *f; 1653766Sroot { 1663766Sroot register int fd; 1673766Sroot 1683766Sroot fd = creat(f, 0644); 1693766Sroot if (fd < 0) 1703766Sroot return (-1); 1713766Sroot close(fd); 1723766Sroot return (open(f, wtflag)); 1733766Sroot } 1743766Sroot 1753766Sroot getfile(filnam, cnt) 1763766Sroot char *filnam; 1773766Sroot { 1783766Sroot register int fsym; 1793766Sroot 1803766Sroot if (eqstr(filnam, "-")) 1813766Sroot return (-1); 1823766Sroot fsym = open(filnam, wtflag); 1833766Sroot if (fsym < 0 && xargc > cnt) { 1843766Sroot if (wtflag) 1853766Sroot fsym = create(filnam); 1863766Sroot if (fsym < 0) 1873766Sroot printf("cannot open `%s'\n", filnam); 1883766Sroot } 1893766Sroot return (fsym); 1903766Sroot } 1913766Sroot 1923766Sroot setvar() 1933766Sroot { 1943766Sroot 1953766Sroot var[varchk('b')] = datbas; 1963766Sroot var[varchk('d')] = filhdr.a_data; 1973766Sroot var[varchk('e')] = filhdr.a_entry; 1983766Sroot var[varchk('m')] = filhdr.a_magic; 1993766Sroot var[varchk('s')] = stksiz; 2003766Sroot var[varchk('t')] = filhdr.a_text; 2013766Sroot } 202