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