xref: /inferno-os/utils/5coff/readcoff.c (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
1 #include	<lib9.h>
2 #include	<bio.h>
3 #include	<mach.h>
4 
5 int fd;
6 static void readf(void);
7 
8 static void
usage(char * msg)9 usage(char *msg)
10 {
11 	fprint(2, "***Error: %s\n", msg);
12 	exits("usage");
13 }
14 
15 static int
cget(void)16 cget(void)
17 {
18 	uchar b[1];
19 
20 	if(read(fd, b, 1) != 1){
21 		fprint(2, "bad cget\n");
22 		exits("cget");
23 	}
24 	return b[0];
25 }
26 
27 static int
hget(void)28 hget(void)
29 {
30 	uchar b[2];
31 
32 	if(read(fd, b, 2) != 2){
33 		fprint(2, "bad hget\n");
34 		exits("hget");
35 	}
36 	return b[1]<<8 | b[0];
37 }
38 
39 static int
lget(void)40 lget(void)
41 {
42 	uchar b[4];
43 
44 	if(read(fd, b, 4) != 4){
45 		fprint(2, "bad lget\n");
46 		exits("lget");
47 	}
48 	return b[3]<<24 | b[2]<<16 | b[1]<<8 | b[0];
49 }
50 
51 static char *
sget(char * st)52 sget(char *st)
53 {
54 	int i;
55 	static uchar buf[8+1];
56 
57 	for(i = 0; i < 8+1; i++)
58 		buf[i] = 0;
59 	if(read(fd, buf, 8) != 8){
60 		fprint(2, "bad sget\n");
61 		exits("sget");
62 	}
63 	if(buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
64 		return st+(buf[7]<<24|buf[6]<<16|buf[5]<<8|buf[4]);
65 	return (char*)buf;
66 }
67 
68 void
main(int argc,char * argv[])69 main(int argc, char	*argv[])
70 {
71 	if (argc != 2)
72 		usage("Wrong number of arguments");
73 
74 	fd = open(argv[1], OREAD);
75 	if (fd < 0) {
76 		fprint(2, "5coff: open %s: %r\n", argv[1]);
77 		exits("open");
78 	}
79 	readf();
80 	exits(0);
81 }
82 
83 static void
section(int i,char * st,int * linoff,int * linn)84 section(int i, char *st, int *linoff, int *linn)
85 {
86 	int pa, va, sz, off, rel, lin, nrel, nlin, f, res, pno;
87 	char *nm;
88 
89 	nm = sget(st);
90 	pa = lget();
91 	va = lget();
92 	sz = lget();
93 	off = lget();
94 	rel = lget();
95 	lin = lget();
96 	nrel = lget();
97 	nlin = lget();
98 	f = lget();
99 	res = hget();
100 	pno = hget();
101 	print("sect %d %s: pa=0x%x va=0x%x sz=%d off=%d rel=%d lin=%d nrel=%d nlin=%d f=0x%x res=%d pno=%d\n", i, nm, pa, va, sz, off, rel, lin, nrel, nlin, f, res, pno);
102 	*linoff = lin;
103 	*linn = nlin;
104 }
105 
106 static void
opthdr(void)107 opthdr(void)
108 {
109 	int mag, ver, textsz, datasz, bsssz, entry, text, data;
110 
111 	mag = hget();
112 	ver = hget();
113 	textsz = lget();
114 	datasz = lget();
115 	bsssz = lget();
116 	entry = lget();
117 	text = lget();
118 	data = lget();
119 	print("opt: mag=0x%x ver=%d txtsz=%d datsz=%d bsssz=%d ent=0x%x txt=0x%x dat=0x%x\n", mag, ver, textsz, datasz, bsssz, entry, text, data);
120 }
121 
122 static void
readhdr(int * o,int * ns,int * sy,int * nsy)123 readhdr(int *o, int *ns, int *sy, int *nsy)
124 {
125 	int vid, nsec, date, sym, nsym, opt, f, tid;
126 
127 	vid = hget();
128 	nsec = hget();
129 	date = lget();
130 	sym = lget();
131 	nsym = lget();
132 	opt = hget();
133 	f = hget();
134 	tid = hget();
135 	print("hdr: vid=0x%x nsect=%d date=%d sym=%d nsym=%d opt=%d f=0x%x tid=0x%x\n", vid, nsec, date, sym, nsym, opt, f, tid);
136 	*o = opt;
137 	*ns = nsec;
138 	*sy = sym;
139 	*nsy = nsym;
140 }
141 
142 static void
readauxsect(int i)143 readauxsect(int i)
144 {
145 	int sz, nrel, ln;
146 
147 	sz = lget();
148 	nrel = hget();
149 	ln = hget();
150 	lget();
151 	hget();
152 	lget();
153 	print("sym auxsect %d: sz=%d nrel=%d ln=%d\n", i, sz, nrel, ln);
154 }
155 
156 static void
readauxfun(int i)157 readauxfun(int i)
158 {
159 	int ind, sz, fpln, nind;
160 
161 	ind = lget();
162 	sz = lget();
163 	fpln = lget();
164 	nind = lget();
165 	hget();
166 	print("sym auxfun %d: ind=%d sz=%d fpln=%d nind=%d\n", i, ind, sz, fpln, nind);
167 }
168 
169 static void
readauxbf(int i)170 readauxbf(int i)
171 {
172 	int rsav, lno, lns, fsz, nind;
173 
174 	rsav = lget();
175 	lno = hget();
176 	lns = hget();
177 	fsz = lget();
178 	nind = lget();
179 	hget();
180 	print("sym auxbf %d: rsav=%x lno=%d lns=%d fsz=%d nind=%d\n", i, rsav, lno, lns, fsz, nind);
181 }
182 
183 static void
readauxef(int i)184 readauxef(int i)
185 {
186 	int lno;
187 
188 	lget();
189 	lno = hget();
190 	lget();
191 	lget();
192 	lget();
193 	print("sym auxef %d: lno=%d\n", i, lno);
194 }
195 
196 static void
readauxother(int i)197 readauxother(int i)
198 {
199 	lget();
200 	lget();
201 	hget();
202 	lget();
203 	lget();
204 	print("sym auxother %d\n", i);
205 }
206 
207 static int
readsym(int i,char * st)208 readsym(int i, char *st)
209 {
210 	int v, s, t, c, aux;
211 	char *nm;
212 
213 	nm = sget(st);
214 	v = lget();
215 	s = hget();
216 	t = hget();
217 	c = cget();
218 	aux = cget();
219 	print("sym %d %s: val=%d sec=%d type=%d class=%d aux=%d\n", i, nm, v, s, t, c, aux);
220 	if(aux){
221 		i++;
222 		if(strcmp(nm, ".text") == 0 || strcmp(nm, ".data") == 0 || strcmp(nm, ".bss") == 0)
223 			readauxsect(i);
224 		else if(strcmp(nm, ".bf") == 0)
225 			readauxbf(i);
226 		else if(strcmp(nm, ".ef") == 0)
227 			readauxef(i);
228 		else if((t&0x30) == 0x20)	// will do
229 			readauxfun(i);
230 		else
231 			readauxother(i);
232 		return 1;
233 	}
234 	return 0;
235 }
236 
237 static char *
readstr(int n)238 readstr(int n)
239 {
240 	char *s = malloc(n);
241 
242 	if(read(fd, s+4, n-4) != n-4){
243 		fprint(2, "bad readstr\n");
244 		exits("sget");
245 	}
246 	return s;
247 }
248 
249 static void
readln(int i)250 readln(int i)
251 {
252 	int a, l;
253 
254 	a = lget();
255 	l = hget();
256 	if(l == 0)
257 		print("line %d: sym=%d\n", i, a);
258 	else
259 		print("line %d: addr=0x%x line=%d\n", i, a, l);
260 }
261 
262 static void
readf()263 readf()
264 {
265 	int i, opt, nsec, sym, nsym, stoff, strsz, linoff, nlin, lino, linn;
266 	char *st;
267 
268 	seek(fd, 0, 0);
269 	readhdr(&opt, &nsec, &sym, &nsym);
270 	if(opt)
271 		opthdr();
272 	stoff = sym+18*nsym;
273 	seek(fd, stoff, 0);
274 	strsz = lget();
275 	st = readstr(strsz);
276 	linoff = nlin = 0;
277 	seek(fd, 22+28, 0);
278 	for(i = 0; i < nsec; i++){
279 		section(i, st, &lino, &linn);
280 		if(linn != 0){
281 			if(nlin == 0){
282 				nlin = linn;
283 				linoff = lino;
284 			}
285 			else
286 				print("multiple line no. tables\n");
287 		}
288 	}
289 	seek(fd, sym, 0);
290 	for(i = 0; i < nsym; i++)
291 		i += readsym(i, st);
292 	print("strsz = %d\n", strsz);
293 	if(nlin != 0){
294 		seek(fd, linoff, 0);
295 		for(i = 0; i < nlin; i++)
296 			readln(i);
297 	}
298 }
299