| 1 | static char sccsid[] = "@(#)setup.c 4.1 %G%"; |
| 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 | fstat(fcor, &stb); |
| 100 | datmap.b1 = 0; |
| 101 | datmap.e1 = -1; |
| 102 | if ((stb.st_mode&S_IFMT) == S_IFREG) |
| 103 | datmap.b1 = 0x80000000; |
| 104 | return; |
| 105 | } |
| 106 | if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) || |
| 107 | !INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) { |
| 108 | datmap.e1 = MAXFILE; |
| 109 | return; |
| 110 | } |
| 111 | signo = u.u_arg[0]; |
| 112 | sigcode = u.u_code; |
| 113 | filhdr.a_text = ctob(u.u_tsize); |
| 114 | filhdr.a_data = ctob(u.u_dsize); |
| 115 | stksiz = ctob(u.u_ssize); |
| 116 | switch (filhdr.a_magic) { |
| 117 | |
| 118 | case OMAGIC: |
| 119 | datmap.b1 = 0; |
| 120 | datmap.e1 = filhdr.a_text+filhdr.a_data; |
| 121 | datmap.f2 = ctob(UPAGES) + datmap.e1; |
| 122 | break; |
| 123 | |
| 124 | case NMAGIC: |
| 125 | case ZMAGIC: |
| 126 | datmap.b1 = round(filhdr.a_text, PAGSIZ); |
| 127 | datmap.e1 = datmap.b1 + filhdr.a_data; |
| 128 | datmap.f2 = ctob(UPAGES) + filhdr.a_data; |
| 129 | break; |
| 130 | } |
| 131 | datbas = datmap.b1; |
| 132 | datmap.f1 = ctob(UPAGES); |
| 133 | datmap.b2 = MAXSTOR - stksiz; |
| 134 | datmap.e2 = MAXSTOR; |
| 135 | if (filhdr.a_magic && u.u_exdata.ux_mag && |
| 136 | filhdr.a_magic != u.u_exdata.ux_mag) |
| 137 | printf("corefile not from this program"); |
| 138 | } |
| 139 | |
| 140 | create(f) |
| 141 | char *f; |
| 142 | { |
| 143 | register int fd; |
| 144 | |
| 145 | fd = creat(f, 0644); |
| 146 | if (fd < 0) |
| 147 | return (-1); |
| 148 | close(fd); |
| 149 | return (open(f, wtflag)); |
| 150 | } |
| 151 | |
| 152 | getfile(filnam, cnt) |
| 153 | char *filnam; |
| 154 | { |
| 155 | register int fsym; |
| 156 | |
| 157 | if (eqstr(filnam, "-")) |
| 158 | return (-1); |
| 159 | fsym = open(filnam, wtflag); |
| 160 | if (fsym < 0 && xargc > cnt) { |
| 161 | if (wtflag) |
| 162 | fsym = create(filnam); |
| 163 | if (fsym < 0) |
| 164 | printf("cannot open `%s'\n", filnam); |
| 165 | } |
| 166 | return (fsym); |
| 167 | } |
| 168 | |
| 169 | setvar() |
| 170 | { |
| 171 | |
| 172 | var[varchk('b')] = datbas; |
| 173 | var[varchk('d')] = filhdr.a_data; |
| 174 | var[varchk('e')] = filhdr.a_entry; |
| 175 | var[varchk('m')] = filhdr.a_magic; |
| 176 | var[varchk('s')] = stksiz; |
| 177 | var[varchk('t')] = filhdr.a_text; |
| 178 | } |