1*3777Sroot static char sccsid[] = "@(#)setup.c 4.2 05/14/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*3777Sroot kernel = 1; 1003766Sroot fstat(fcor, &stb); 101*3777Sroot if (stb.st_mode & S_IFREG) 102*3777Sroot kcore = 1; 103*3777Sroot /* this is phoney baloney... it is boogered in access.c */ 1043766Sroot datmap.b1 = 0; 1053766Sroot datmap.e1 = -1; 106*3777Sroot /* end phoney baloney */ 107*3777Sroot lookup("_Sysmap"); 108*3777Sroot sbr = cursym->n_value; 109*3777Sroot lookup("_Syssize"); 110*3777Sroot slr = cursym->n_value; 111*3777Sroot printf("sbr %X slr %X\n", sbr, slr); 112*3777Sroot lookup("_masterpcbb"); 113*3777Sroot printf("masterpcbb at %X\n", cursym->n_value); 114*3777Sroot physrw(fcor, cursym->n_value&0x7fffffff, &masterpcbb, 1); 115*3777Sroot printf("masterpcbb value %X\n", masterpcbb); 116*3777Sroot var[varchk('p')] = masterpcbb; 117*3777Sroot getpcb(); 1183766Sroot return; 1193766Sroot } 1203766Sroot if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) || 1213766Sroot !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) { 1223766Sroot datmap.e1 = MAXFILE; 1233766Sroot return; 1243766Sroot } 1253766Sroot signo = u.u_arg[0]; 1263766Sroot sigcode = u.u_code; 1273766Sroot filhdr.a_text = ctob(u.u_tsize); 1283766Sroot filhdr.a_data = ctob(u.u_dsize); 1293766Sroot stksiz = ctob(u.u_ssize); 1303766Sroot switch (filhdr.a_magic) { 1313766Sroot 1323766Sroot case OMAGIC: 1333766Sroot datmap.b1 = 0; 1343766Sroot datmap.e1 = filhdr.a_text+filhdr.a_data; 1353766Sroot datmap.f2 = ctob(UPAGES) + datmap.e1; 1363766Sroot break; 1373766Sroot 1383766Sroot case NMAGIC: 1393766Sroot case ZMAGIC: 1403766Sroot datmap.b1 = round(filhdr.a_text, PAGSIZ); 1413766Sroot datmap.e1 = datmap.b1 + filhdr.a_data; 1423766Sroot datmap.f2 = ctob(UPAGES) + filhdr.a_data; 1433766Sroot break; 1443766Sroot } 1453766Sroot datbas = datmap.b1; 1463766Sroot datmap.f1 = ctob(UPAGES); 1473766Sroot datmap.b2 = MAXSTOR - stksiz; 1483766Sroot datmap.e2 = MAXSTOR; 1493766Sroot if (filhdr.a_magic && u.u_exdata.ux_mag && 1503766Sroot filhdr.a_magic != u.u_exdata.ux_mag) 1513766Sroot printf("corefile not from this program"); 1523766Sroot } 1533766Sroot 154*3777Sroot getpcb() 155*3777Sroot { 156*3777Sroot printf("getpcb: masterpcbb is %X\n", masterpcbb); 157*3777Sroot lseek(fcor, masterpcbb&~0x80000000, 0); 158*3777Sroot read(fcor, &pcb, sizeof (struct pcb)); 159*3777Sroot printf("p0br %X p0lr %X p1br %X p1lr %X\n", 160*3777Sroot pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr); 161*3777Sroot pcb.pcb_p0lr &= ~AST_CLR; 162*3777Sroot } 163*3777Sroot 1643766Sroot create(f) 1653766Sroot char *f; 1663766Sroot { 1673766Sroot register int fd; 1683766Sroot 1693766Sroot fd = creat(f, 0644); 1703766Sroot if (fd < 0) 1713766Sroot return (-1); 1723766Sroot close(fd); 1733766Sroot return (open(f, wtflag)); 1743766Sroot } 1753766Sroot 1763766Sroot getfile(filnam, cnt) 1773766Sroot char *filnam; 1783766Sroot { 1793766Sroot register int fsym; 1803766Sroot 1813766Sroot if (eqstr(filnam, "-")) 1823766Sroot return (-1); 1833766Sroot fsym = open(filnam, wtflag); 1843766Sroot if (fsym < 0 && xargc > cnt) { 1853766Sroot if (wtflag) 1863766Sroot fsym = create(filnam); 1873766Sroot if (fsym < 0) 1883766Sroot printf("cannot open `%s'\n", filnam); 1893766Sroot } 1903766Sroot return (fsym); 1913766Sroot } 1923766Sroot 1933766Sroot setvar() 1943766Sroot { 1953766Sroot 1963766Sroot var[varchk('b')] = datbas; 1973766Sroot var[varchk('d')] = filhdr.a_data; 1983766Sroot var[varchk('e')] = filhdr.a_entry; 1993766Sroot var[varchk('m')] = filhdr.a_magic; 2003766Sroot var[varchk('s')] = stksiz; 2013766Sroot var[varchk('t')] = filhdr.a_text; 2023766Sroot } 203