xref: /inferno-os/utils/libmach/obj.c (revision 54d06533f3d8d73d9369206f7f5be71351cfbcf3)
1 /*
2  * obj.c
3  * routines universal to all object files
4  */
5 #include <lib9.h>
6 #include <bio.h>
7 #include <ar.h>
8 #include "mach.h"
9 #include "obj.h"
10 
11 #define islocal(t)	((t)=='a' || (t)=='p')
12 
13 enum
14 {
15 	NNAMES	= 50,
16 	MAXIS	= 8,		/* max length to determine if a file is a .? file */
17 	MAXOFF	= 0x7fffffff,	/* larger than any possible local offset */
18 	NHASH	= 1024,		/* must be power of two */
19 	HASHMUL	= 79L,
20 };
21 
22 int	_is2(char*),		/* in [$OS].c */
23 	_is5(char*),
24 	_is6(char*),
25 	_is7(char*),
26 	_is8(char*),
27 	_is9(char*),
28 	_isk(char*),
29 	_isq(char*),
30 	_isv(char*),
31 	_read2(Biobuf*, Prog*),
32 	_read5(Biobuf*, Prog*),
33 	_read6(Biobuf*, Prog*),
34 	_read7(Biobuf*, Prog*),
35 	_read8(Biobuf*, Prog*),
36 	_read9(Biobuf*, Prog*),
37 	_readk(Biobuf*, Prog*),
38 	_readq(Biobuf*, Prog*),
39 	_readv(Biobuf*, Prog*);
40 
41 typedef struct Obj	Obj;
42 typedef struct Symtab	Symtab;
43 
44 struct	Obj		/* functions to handle each intermediate (.$O) file */
45 {
46 	char	*name;				/* name of each $O file */
47 	int	(*is)(char*);			/* test for each type of $O file */
48 	int	(*read)(Biobuf*, Prog*);	/* read for each type of $O file*/
49 };
50 
51 static Obj	obj[] =
52 {			/* functions to identify and parse each type of obj */
53 	/*[Obj68020]*/	"68020 .2",	_is2, _read2,
54 	/*[ObjSparc]*/	"sparc .k",	_isk, _readk,
55 	/*[ObjMips]*/	"mips .v",	_isv, _readv,
56 	/*[Obj386]*/	"386 .8",	_is8, _read8,
57 	/*[Obj960]*/	{0, 0,},
58 	/*[Obj3210]*/	{0, 0,},
59 	/*[ObjMips2]*/	{0, 0,},
60 	/*[Obj29000]*/	{0, 0,},
61 	/*[ObjArm]*/	"arm .5",	_is5, _read5,
62 	/*[ObjPower]*/	"power .q",	_isq, _readq,
63 	/*[ObjMips2le]*/	{0, 0,},
64 	/*[ObjAlpha]*/	{0, 0,},
65 	/*[ObjSparc64]*/	{0, 0,},
66 	/*[ObjAmd64]*/	"amd64 .6",	_is6, _read6,
67 	/*[ObjSpim]*/	{0, 0,},
68 	/*[ObjPower64]*/	"power64 .9",	_is9, _read9,
69 	/*[Maxobjtype]*/	0, 0
70 };
71 
72 struct	Symtab
73 {
74 	struct	Sym 	s;
75 	struct	Symtab	*next;
76 };
77 
78 static	Symtab *hash[NHASH];
79 static	Sym	*names[NNAMES];	/* working set of active names */
80 
81 static	int	processprog(Prog*,int);	/* decode each symbol reference */
82 static	void	objreset(void);
83 static	void	objlookup(int, char *, int, uint);
84 static	void 	objupdate(int, int);
85 
86 int
87 objtype(Biobuf *bp, char **name)
88 {
89 	int i;
90 	char buf[MAXIS];
91 
92 	if(Bread(bp, buf, MAXIS) < MAXIS)
93 		return -1;
94 	Bseek(bp, -MAXIS, 1);
95 	for (i = 0; i < Maxobjtype; i++) {
96 		if (obj[i].is && (*obj[i].is)(buf)) {
97 			if (name)
98 				*name = obj[i].name;
99 			return i;
100 		}
101 	}
102 	return -1;
103 }
104 
105 int
106 isar(Biobuf *bp)
107 {
108 	int n;
109 	char magbuf[SARMAG];
110 
111 	n = Bread(bp, magbuf, SARMAG);
112 	if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
113 		return 1;
114 	return 0;
115 }
116 
117 /*
118  * determine what kind of object file this is and process it.
119  * return whether or not this was a recognized intermediate file.
120  */
121 int
122 readobj(Biobuf *bp, int objtype)
123 {
124 	Prog p;
125 
126 	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
127 		return 1;
128 	objreset();
129 	while ((*obj[objtype].read)(bp, &p))
130 		if (!processprog(&p, 1))
131 			return 0;
132 	return 1;
133 }
134 
135 int
136 readar(Biobuf *bp, int objtype, vlong end, int doautos)
137 {
138 	Prog p;
139 
140 	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
141 		return 1;
142 	objreset();
143 	while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
144 		if (!processprog(&p, doautos))
145 			return 0;
146 	return 1;
147 }
148 
149 /*
150  *	decode a symbol reference or definition
151  */
152 static	int
153 processprog(Prog *p, int doautos)
154 {
155 	if(p->kind == aNone)
156 		return 1;
157 	if(p->sym < 0 || p->sym >= NNAMES)
158 		return 0;
159 	switch(p->kind)
160 	{
161 	case aName:
162 		if (!doautos)
163 		if(p->type != 'U' && p->type != 'b')
164 			break;
165 		objlookup(p->sym, p->id, p->type, p->sig);
166 		break;
167 	case aText:
168 		objupdate(p->sym, 'T');
169 		break;
170 	case aData:
171 		objupdate(p->sym, 'D');
172 		break;
173 	default:
174 		break;
175 	}
176 	return 1;
177 }
178 
179 /*
180  * find the entry for s in the symbol array.
181  * make a new entry if it is not already there.
182  */
183 static void
184 objlookup(int id, char *name, int type, uint sig)
185 {
186 	long h;
187 	char *cp;
188 	Sym *s;
189 	Symtab *sp;
190 
191 	s = names[id];
192 	if(s && strcmp(s->name, name) == 0) {
193 		s->type = type;
194 		s->sig = sig;
195 		return;
196 	}
197 
198 	h = *name;
199 	for(cp = name+1; *cp; h += *cp++)
200 		h *= HASHMUL;
201 	if(h < 0)
202 		h = ~h;
203 	h &= (NHASH-1);
204 	if (type == 'U' || type == 'b' || islocal(type)) {
205 		for(sp = hash[h]; sp; sp = sp->next)
206 			if(strcmp(sp->s.name, name) == 0) {
207 				switch(sp->s.type) {
208 				case 'T':
209 				case 'D':
210 				case 'U':
211 					if (type == 'U') {
212 						names[id] = &sp->s;
213 						return;
214 					}
215 					break;
216 				case 't':
217 				case 'd':
218 				case 'b':
219 					if (type == 'b') {
220 						names[id] = &sp->s;
221 						return;
222 					}
223 					break;
224 				case 'a':
225 				case 'p':
226 					if (islocal(type)) {
227 						names[id] = &sp->s;
228 						return;
229 					}
230 					break;
231 				default:
232 					break;
233 				}
234 			}
235 	}
236 	sp = malloc(sizeof(Symtab));
237 	sp->s.name = name;
238 	sp->s.type = type;
239 	sp->s.sig = sig;
240 	sp->s.value = islocal(type) ? MAXOFF : 0;
241 	names[id] = &sp->s;
242 	sp->next = hash[h];
243 	hash[h] = sp;
244 	return;
245 }
246 /*
247  *	traverse the symbol lists
248  */
249 void
250 objtraverse(void (*fn)(Sym*, void*), void *pointer)
251 {
252 	int i;
253 	Symtab *s;
254 
255 	for(i = 0; i < NHASH; i++)
256 		for(s = hash[i]; s; s = s->next)
257 			(*fn)(&s->s, pointer);
258 }
259 
260 /*
261  * update the offset information for a 'a' or 'p' symbol in an intermediate file
262  */
263 void
264 _offset(int id, vlong off)
265 {
266 	Sym *s;
267 
268 	s = names[id];
269 	if (s && s->name[0] && islocal(s->type) && s->value > off)
270 		s->value = off;
271 }
272 
273 /*
274  * update the type of a global text or data symbol
275  */
276 static void
277 objupdate(int id, int type)
278 {
279 	Sym *s;
280 
281 	s = names[id];
282 	if (s && s->name[0])
283 		if (s->type == 'U')
284 			s->type = type;
285 		else if (s->type == 'b')
286 			s->type = tolower(type);
287 }
288 
289 /*
290  * look for the next file in an archive
291  */
292 int
293 nextar(Biobuf *bp, int offset, char *buf)
294 {
295 	struct ar_hdr a;
296 	int i, r;
297 	long arsize;
298 
299 	if (offset&01)
300 		offset++;
301 	Bseek(bp, offset, 0);
302 	r = Bread(bp, &a, SAR_HDR);
303 	if(r != SAR_HDR)
304 		return 0;
305 	if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
306 		return -1;
307 	for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
308 		buf[i] = a.name[i];
309 	buf[i] = 0;
310 	arsize = strtol(a.size, 0, 0);
311 	if (arsize&1)
312 		arsize++;
313 	return arsize + SAR_HDR;
314 }
315 
316 static void
317 objreset(void)
318 {
319 	int i;
320 	Symtab *s, *n;
321 
322 	for(i = 0; i < NHASH; i++) {
323 		for(s = hash[i]; s; s = n) {
324 			n = s->next;
325 			free(s->s.name);
326 			free(s);
327 		}
328 		hash[i] = 0;
329 	}
330 	memset(names, 0, sizeof names);
331 }
332