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