1 static char sccsid[] = "@(#)setup.c 4.2 05/14/81"; 2 /* 3 * adb - routines to read a.out+core at startup 4 */ 5 #include "defs.h" 6 #include <stat.h> 7 8 off_t datbas; /* offset of the base of the data segment */ 9 off_t stksiz; /* stack size in the core image */ 10 INT sigcode; /* belongs in head.h */ 11 12 char *symfil = "a.out"; 13 char *corfil = "core"; 14 15 setsym() 16 { 17 off_t loc; 18 struct exec hdr; 19 register struct nlist *sp; 20 int ssiz; 21 char *strtab; 22 23 fsym = getfile(symfil, 1); 24 txtmap.ufd = fsym; 25 if (read(fsym, (char *)&hdr, sizeof hdr) != sizeof hdr || 26 N_BADMAG(hdr)) { 27 txtmap.e1 = MAXFILE; 28 return; 29 } 30 filhdr = hdr; 31 loc = filhdr.a_text+filhdr.a_data; 32 txtmap.f1 = txtmap.f2 = N_TXTOFF(filhdr); 33 txtmap.b1 = 0; 34 switch (filhdr.a_magic) { 35 36 case OMAGIC: 37 txtmap.b1 = txtmap.e1 = 0; 38 txtmap.b2 = datbas = 0; 39 txtmap.e2 = loc; 40 break; 41 42 case ZMAGIC: 43 case NMAGIC: 44 txtmap.e1 = filhdr.a_text; 45 txtmap.b2 = datbas = round(filhdr.a_text, PAGSIZ); 46 txtmap.e2 = datbas + filhdr.a_data; 47 txtmap.f2 += txtmap.e1; 48 } 49 loc = N_SYMOFF(filhdr); 50 symtab = (struct nlist *) malloc(filhdr.a_syms); 51 esymtab = &symtab[filhdr.a_syms / sizeof (struct nlist)]; 52 if (symtab == NULL) 53 goto nospac; 54 lseek(fsym, loc, 0); 55 if (filhdr.a_syms == 0) 56 goto nosymt; 57 /* SHOULD SQUISH OUT STABS HERE!!! */ 58 if (read(fsym, symtab, filhdr.a_syms) != filhdr.a_syms) 59 goto readerr; 60 if (read(fsym, &ssiz, sizeof (ssiz)) != sizeof (ssiz)) 61 goto oldfmt; 62 strtab = (char *) malloc(ssiz); 63 if (strtab == 0) 64 goto nospac; 65 *(int *)strtab = ssiz; 66 ssiz -= sizeof (ssiz); 67 if (read(fsym, strtab + sizeof (ssiz), ssiz) != ssiz) 68 goto readerr; 69 for (sp = symtab; sp < esymtab; sp++) 70 if (sp->n_strx) 71 /* SHOULD PERFORM RANGE CHECK HERE */ 72 sp->n_un.n_name = strtab + sp->n_un.n_strx; 73 nosymt: 74 if (INKERNEL(filhdr.a_entry)) { 75 txtmap.b1 += KERNOFF; 76 txtmap.e1 += KERNOFF; 77 txtmap.b2 += KERNOFF; 78 txtmap.e2 += KERNOFF; 79 } 80 return; 81 readerr: 82 printf("Error reading symbol|string table\n"); 83 exit(1); 84 nospac: 85 printf("Not enough space for symbol|string table\n"); 86 exit(1); 87 oldfmt: 88 printf("Old format a.out - no string table\n"); 89 exit(1); 90 } 91 92 setcor() 93 { 94 95 fcor = datmap.ufd = getfile(corfil,2); 96 if (fcor != -1 && INKERNEL(filhdr.a_entry)) { 97 struct stat stb; 98 99 kernel = 1; 100 fstat(fcor, &stb); 101 if (stb.st_mode & S_IFREG) 102 kcore = 1; 103 /* this is phoney baloney... it is boogered in access.c */ 104 datmap.b1 = 0; 105 datmap.e1 = -1; 106 /* end phoney baloney */ 107 lookup("_Sysmap"); 108 sbr = cursym->n_value; 109 lookup("_Syssize"); 110 slr = cursym->n_value; 111 printf("sbr %X slr %X\n", sbr, slr); 112 lookup("_masterpcbb"); 113 printf("masterpcbb at %X\n", cursym->n_value); 114 physrw(fcor, cursym->n_value&0x7fffffff, &masterpcbb, 1); 115 printf("masterpcbb value %X\n", masterpcbb); 116 var[varchk('p')] = masterpcbb; 117 getpcb(); 118 return; 119 } 120 if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) || 121 !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) { 122 datmap.e1 = MAXFILE; 123 return; 124 } 125 signo = u.u_arg[0]; 126 sigcode = u.u_code; 127 filhdr.a_text = ctob(u.u_tsize); 128 filhdr.a_data = ctob(u.u_dsize); 129 stksiz = ctob(u.u_ssize); 130 switch (filhdr.a_magic) { 131 132 case OMAGIC: 133 datmap.b1 = 0; 134 datmap.e1 = filhdr.a_text+filhdr.a_data; 135 datmap.f2 = ctob(UPAGES) + datmap.e1; 136 break; 137 138 case NMAGIC: 139 case ZMAGIC: 140 datmap.b1 = round(filhdr.a_text, PAGSIZ); 141 datmap.e1 = datmap.b1 + filhdr.a_data; 142 datmap.f2 = ctob(UPAGES) + filhdr.a_data; 143 break; 144 } 145 datbas = datmap.b1; 146 datmap.f1 = ctob(UPAGES); 147 datmap.b2 = MAXSTOR - stksiz; 148 datmap.e2 = MAXSTOR; 149 if (filhdr.a_magic && u.u_exdata.ux_mag && 150 filhdr.a_magic != u.u_exdata.ux_mag) 151 printf("corefile not from this program"); 152 } 153 154 getpcb() 155 { 156 printf("getpcb: masterpcbb is %X\n", masterpcbb); 157 lseek(fcor, masterpcbb&~0x80000000, 0); 158 read(fcor, &pcb, sizeof (struct pcb)); 159 printf("p0br %X p0lr %X p1br %X p1lr %X\n", 160 pcb.pcb_p0br, pcb.pcb_p0lr, pcb.pcb_p1br, pcb.pcb_p1lr); 161 pcb.pcb_p0lr &= ~AST_CLR; 162 } 163 164 create(f) 165 char *f; 166 { 167 register int fd; 168 169 fd = creat(f, 0644); 170 if (fd < 0) 171 return (-1); 172 close(fd); 173 return (open(f, wtflag)); 174 } 175 176 getfile(filnam, cnt) 177 char *filnam; 178 { 179 register int fsym; 180 181 if (eqstr(filnam, "-")) 182 return (-1); 183 fsym = open(filnam, wtflag); 184 if (fsym < 0 && xargc > cnt) { 185 if (wtflag) 186 fsym = create(filnam); 187 if (fsym < 0) 188 printf("cannot open `%s'\n", filnam); 189 } 190 return (fsym); 191 } 192 193 setvar() 194 { 195 196 var[varchk('b')] = datbas; 197 var[varchk('d')] = filhdr.a_data; 198 var[varchk('e')] = filhdr.a_entry; 199 var[varchk('m')] = filhdr.a_magic; 200 var[varchk('s')] = stksiz; 201 var[varchk('t')] = filhdr.a_text; 202 } 203