summaryrefslogtreecommitdiff
path: root/src/libmach/obj.c
blob: 729a3eab8093bdc5dbe57b22278f75d3dabd7b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Inferno libmach/obj.c
// http://code.google.com/p/inferno-os/source/browse/utils/libmach/obj.c
//
// 	Copyright © 1994-1999 Lucent Technologies Inc.
// 	Power PC support Copyright © 1995-2004 C H Forsyth (forsyth@terzarima.net).
// 	Portions Copyright © 1997-1999 Vita Nuova Limited.
// 	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com).
// 	Revisions Copyright © 2000-2004 Lucent Technologies Inc. and others.
//	Portions Copyright © 2009 The Go Authors.  All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/*
 * obj.c
 * routines universal to all object files
 */
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ar.h>
#include <mach.h>
#include "obj.h"

#define islocal(t)	((t)=='a' || (t)=='p')

enum
{
	NNAMES	= 50,
	MAXIS	= 8,		/* max length to determine if a file is a .? file */
	MAXOFF	= 0x7fffffff,	/* larger than any possible local offset */
	NHASH	= 1024,		/* must be power of two */
	HASHMUL	= 79L,
};

int	_is2(char*),		/* in [$OS].c */
	_is5(char*),
	_is6(char*),
	_is7(char*),
	_is8(char*),
	_is9(char*),
	_isk(char*),
	_isq(char*),
	_isv(char*),
	_isu(char*),
	_read2(Biobuf*, Prog*),
	_read5(Biobuf*, Prog*),
	_read6(Biobuf*, Prog*),
	_read7(Biobuf*, Prog*),
	_read8(Biobuf*, Prog*),
	_read9(Biobuf*, Prog*),
	_readk(Biobuf*, Prog*),
	_readq(Biobuf*, Prog*),
	_readv(Biobuf*, Prog*),
	_readu(Biobuf*, Prog*);

typedef struct Obj	Obj;
typedef struct Symtab	Symtab;

struct	Obj		/* functions to handle each intermediate (.$O) file */
{
	char	*name;				/* name of each $O file */
	int	(*is)(char*);			/* test for each type of $O file */
	int	(*read)(Biobuf*, Prog*);	/* read for each type of $O file*/
};

static Obj	obj[] =
{			/* functions to identify and parse each type of obj */
	[Obj68020]   = { "68020 .2",	_is2, _read2 },
	[ObjAmd64]   = { "amd64 .6",	_is6 , _read6 },
	[ObjArm]     = { "arm .5",	_is5, _read5 },
	[ObjAlpha]   = { "alpha .7",	_is7, _read7 },
	[Obj386]     = { "386 .8",	_is8, _read8 },
	[ObjSparc]   = { "sparc .k",	_isk, _readk },
	[ObjPower]   = { "power .q",	_isq, _readq },
	[ObjMips]    = { "mips .v",	_isv, _readv },
	[ObjSparc64] = { "sparc64 .u",  _isu, _readu },
	[ObjPower64] = { "power64 .9",	_is9, _read9 },
	[Maxobjtype] = { 0, 0, 0 }
};

struct	Symtab
{
	struct	Sym 	s;
	struct	Symtab	*next;
};

static	Symtab *hash[NHASH];
static	Sym	*names[NNAMES];	/* working set of active names */

static	int	processprog(Prog*,int);	/* decode each symbol reference */
static	void	objreset(void);
static	void	objlookup(int, char *, int, uint);
static	void 	objupdate(int, int);

static	int	sequence;

int
objtype(Biobuf *bp, char **name)
{
	int i;
	char buf[MAXIS];
	int c;
	char *p;

	/*
	 * Look for import block.
	 */
	p = Brdline(bp, '\n');
	if(p == nil)
		return -1;
	if(Blinelen(bp) < 10 || strncmp(p, "go object ", 10) != 0)
		return -1;
	Bseek(bp, -1, 1);

	/*
	 * Found one.  Skip until "\n!\n"
	 */
	for(;;) {
		if((c = BGETC(bp)) == Beof)
			return -1;
		if(c != '\n')
			continue;
		c = BGETC(bp);
		if(c != '!'){
			Bungetc(bp);
			continue;
		}
		c = BGETC(bp);
		if(c != '\n'){
			Bungetc(bp);
			continue;
		}
		break;
	}

	if(Bread(bp, buf, MAXIS) < MAXIS)
		return -1;
	Bseek(bp, -MAXIS, 1);
	for (i = 0; i < Maxobjtype; i++) {
		if (obj[i].is && (*obj[i].is)(buf)) {
			if (name)
				*name = obj[i].name;
			return i;
		}
	}

	return -1;
}

int
isar(Biobuf *bp)
{
	int n;
	char magbuf[SARMAG];

	n = Bread(bp, magbuf, SARMAG);
	if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
		return 1;
	return 0;
}

/*
 * determine what kind of object file this is and process it.
 * return whether or not this was a recognized intermediate file.
 */
int
readobj(Biobuf *bp, int objtype)
{
	Prog p;

	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
		return 1;
	objreset();
	while ((*obj[objtype].read)(bp, &p))
		if (!processprog(&p, 1))
			return 0;
	return 1;
}

int
readar(Biobuf *bp, int objtype, vlong end, int doautos)
{
	Prog p;

	if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
		return 1;
	objreset();
	while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
		if (!processprog(&p, doautos))
			return 0;
	return 1;
}

/*
 *	decode a symbol reference or definition
 */
static	int
processprog(Prog *p, int doautos)
{
	if(p->kind == aNone)
		return 1;
	if((schar)p->sym < 0 || p->sym >= NNAMES)
		return 0;
	switch(p->kind)
	{
	case aName:
		if (!doautos)
		if(p->type != 'U' && p->type != 'b')
			break;
		objlookup(p->sym, p->id, p->type, p->sig);
		break;
	case aText:
		objupdate(p->sym, 'T');
		break;
	case aData:
		objupdate(p->sym, 'D');
		break;
	default:
		break;
	}
	return 1;
}

/*
 * find the entry for s in the symbol array.
 * make a new entry if it is not already there.
 */
static void
objlookup(int id, char *name, int type, uint sig)
{
	uint32 h;
	char *cp;
	Sym *s;
	Symtab *sp;

	s = names[id];
	if(s && strcmp(s->name, name) == 0) {
		s->type = type;
		s->sig = sig;
		return;
	}

	h = *name;
	for(cp = name+1; *cp; h += *cp++)
		h *= HASHMUL;
	h &= NHASH-1;
	if (type == 'U' || type == 'b' || islocal(type)) {
		for(sp = hash[h]; sp; sp = sp->next)
			if(strcmp(sp->s.name, name) == 0) {
				switch(sp->s.type) {
				case 'T':
				case 'D':
				case 'U':
					if (type == 'U') {
						names[id] = &sp->s;
						return;
					}
					break;
				case 't':
				case 'd':
				case 'b':
					if (type == 'b') {
						names[id] = &sp->s;
						return;
					}
					break;
				case 'a':
				case 'p':
					if (islocal(type)) {
						names[id] = &sp->s;
						return;
					}
					break;
				default:
					break;
				}
			}
	}
	sp = malloc(sizeof(Symtab));
	if(sp == nil)
		sysfatal("out of memory");
	sp->s.name = name;
	sp->s.type = type;
	sp->s.sig = sig;
	sp->s.value = islocal(type) ? MAXOFF : 0;
	sp->s.sequence = sequence++;
	names[id] = &sp->s;
	sp->next = hash[h];
	hash[h] = sp;
	return;
}
/*
 *	traverse the symbol lists
 */
void
objtraverse(void (*fn)(Sym*, void*), void *pointer)
{
	int i;
	Symtab *s;

	for(i = 0; i < NHASH; i++)
		for(s = hash[i]; s; s = s->next)
			(*fn)(&s->s, pointer);
}

/*
 * update the offset information for a 'a' or 'p' symbol in an intermediate file
 */
void
_offset(int id, vlong off)
{
	Sym *s;

	s = names[id];
	if (s && s->name[0] && islocal(s->type) && s->value > off)
		s->value = off;
}

/*
 * update the type of a global text or data symbol
 */
static void
objupdate(int id, int type)
{
	Sym *s;

	s = names[id];
	if (s && s->name[0])
		if (s->type == 'U')
			s->type = type;
		else if (s->type == 'b')
			s->type = tolower(type);
}

/*
 * look for the next file in an archive
 */
int
nextar(Biobuf *bp, int offset, char *buf)
{
	struct ar_hdr a;
	int i, r;
	int32 arsize;

	if (offset&01)
		offset++;
	Bseek(bp, offset, 0);
	r = Bread(bp, &a, SAR_HDR);
	if(r != SAR_HDR)
		return 0;
	if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
		return -1;
	for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
		buf[i] = a.name[i];
	buf[i] = 0;
	arsize = strtol(a.size, 0, 0);
	if (arsize&1)
		arsize++;
	return arsize + SAR_HDR;
}

static void
objreset(void)
{
	int i;
	Symtab *s, *n;

	for(i = 0; i < NHASH; i++) {
		for(s = hash[i]; s; s = n) {
			n = s->next;
			free(s->s.name);
			free(s);
		}
		hash[i] = 0;
	}
	memset(names, 0, sizeof names);
}