xref: /plan9-contrib/sys/src/cmd/ld/pobj.c (revision 40d015479ed36701ae6dcfd8814f849fc6285e8d)
1 #include	"l.h"
2 #include	<ar.h>
3 
4 char	*noname		= "<none>";
5 char	symname[]	= SYMDEF;
6 
7 char**	libdir;
8 int	nlibdir	= 0;
9 static	int	maxlibdir = 0;
10 
11 int
isobjfile(char * f)12 isobjfile(char *f)
13 {
14 	int n, v;
15 	Biobuf *b;
16 	char buf1[5], buf2[SARMAG];
17 
18 	b = Bopen(f, OREAD);
19 	if(b == nil)
20 		return 0;
21 	n = Bread(b, buf1, 5);
22 	if(n == 5 && (buf1[2] == 1 && buf1[3] == '<' || buf1[3] == 1 && buf1[4] == '<'))
23 		v = 1;	/* good enough for our purposes */
24 	else{
25 		Bseek(b, 0, 0);
26 		n = Bread(b, buf2, SARMAG);
27 		v = n == SARMAG && strncmp(buf2, ARMAG, SARMAG) == 0;
28 	}
29 	Bterm(b);
30 	return v;
31 }
32 
33 void
loadlib(void)34 loadlib(void)
35 {
36 	int i;
37 	long h;
38 	Sym *s;
39 
40 loop:
41 	xrefresolv = 0;
42 	for(i=0; i<libraryp; i++) {
43 		if(debug['v'])
44 			Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]);
45 		objfile(library[i]);
46 	}
47 	if(xrefresolv)
48 	for(h=0; h<nelem(hash); h++)
49 	for(s = hash[h]; s != S; s = s->link)
50 		if(s->type == SXREF)
51 			goto loop;
52 }
53 
54 void
objfile(char * file)55 objfile(char *file)
56 {
57 	long off, esym, cnt, l;
58 	int f, work;
59 	Sym *s;
60 	char magbuf[SARMAG];
61 	char name[LIBNAMELEN], pname[LIBNAMELEN];
62 	struct ar_hdr arhdr;
63 	char *e, *start, *stop;
64 
65 	if(debug['v'])
66 		Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
67 	Bflush(&bso);
68 	if(file[0] == '-' && file[1] == 'l') {
69 		snprint(pname, sizeof(pname), "lib%s.a", file+2);
70 		e = findlib(pname);
71 		if(e == nil) {
72 			diag("cannot find library: %s", file);
73 			errorexit();
74 		}
75 		snprint(name, sizeof(name), "%s/%s", e, pname);
76 		file = name;
77 	}
78 	f = open(file, 0);
79 	if(f < 0) {
80 		diag("cannot open %s: %r", file);
81 		errorexit();
82 	}
83 	l = read(f, magbuf, SARMAG);
84 	if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
85 		/* load it as a regular file */
86 		l = seek(f, 0L, 2);
87 		seek(f, 0L, 0);
88 		ldobj(f, l, file);
89 		close(f);
90 		return;
91 	}
92 
93 	if(debug['v'])
94 		Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file);
95 	l = read(f, &arhdr, SAR_HDR);
96 	if(l != SAR_HDR) {
97 		diag("%s: short read on archive file symbol header", file);
98 		goto out;
99 	}
100 	if(strncmp(arhdr.name, symname, strlen(symname))) {
101 		diag("%s: first entry not symbol header", file);
102 		goto out;
103 	}
104 
105 	esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
106 	off = SARMAG + SAR_HDR;
107 
108 	/*
109 	 * just bang the whole symbol file into memory
110 	 */
111 	seek(f, off, 0);
112 	cnt = esym - off;
113 	start = malloc(cnt + 10);
114 	cnt = read(f, start, cnt);
115 	if(cnt <= 0){
116 		close(f);
117 		return;
118 	}
119 	stop = &start[cnt];
120 	memset(stop, 0, 10);
121 
122 	work = 1;
123 	while(work){
124 		if(debug['v'])
125 			Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
126 		Bflush(&bso);
127 		work = 0;
128 		for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
129 			s = lookup(e+5, 0);
130 			if(s->type != SXREF)
131 				continue;
132 			sprint(pname, "%s(%s)", file, s->name);
133 			if(debug['v'])
134 				Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
135 			Bflush(&bso);
136 			l = e[1] & 0xff;
137 			l |= (e[2] & 0xff) << 8;
138 			l |= (e[3] & 0xff) << 16;
139 			l |= (e[4] & 0xff) << 24;
140 			seek(f, l, 0);
141 			l = read(f, &arhdr, SAR_HDR);
142 			if(l != SAR_HDR)
143 				goto bad;
144 			if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
145 				goto bad;
146 			l = atolwhex(arhdr.size);
147 			ldobj(f, l, pname);
148 			if(s->type == SXREF) {
149 				diag("%s: failed to load: %s", file, s->name);
150 				errorexit();
151 			}
152 			work = 1;
153 			xrefresolv = 1;
154 		}
155 	}
156 	return;
157 
158 bad:
159 	diag("%s: bad or out of date archive", file);
160 out:
161 	close(f);
162 }
163 
164 void
addlibpath(char * arg)165 addlibpath(char *arg)
166 {
167 	char **p;
168 
169 	if(nlibdir >= maxlibdir) {
170 		if(maxlibdir == 0)
171 			maxlibdir = 8;
172 		else
173 			maxlibdir *= 2;
174 		p = malloc(maxlibdir*sizeof(*p));
175 		if(p == nil) {
176 			diag("out of memory");
177 			errorexit();
178 		}
179 		memmove(p, libdir, nlibdir*sizeof(*p));
180 		free(libdir);
181 		libdir = p;
182 	}
183 	libdir[nlibdir++] = strdup(arg);
184 }
185 
186 char*
findlib(char * file)187 findlib(char *file)
188 {
189 	int i;
190 	char name[LIBNAMELEN];
191 
192 	for(i = 0; i < nlibdir; i++) {
193 		snprint(name, sizeof(name), "%s/%s", libdir[i], file);
194 		if(fileexists(name))
195 			return libdir[i];
196 	}
197 	return nil;
198 }
199 
200 void
addlibroot(void)201 addlibroot(void)
202 {
203 	char *a;
204 	char name[LIBNAMELEN];
205 
206 	a = getenv("ccroot");
207 	if(a != nil && *a != '\0') {
208 		if(!fileexists(a)) {
209 			diag("nonexistent $ccroot: %s", a);
210 			errorexit();
211 		}
212 	}else
213 		a = "";
214 	snprint(name, sizeof(name), "%s/%s/lib", a, thestring);
215 	addlibpath(name);
216 }
217 
218 void
addlib(char * obj)219 addlib(char *obj)
220 {
221 	char fn1[LIBNAMELEN], fn2[LIBNAMELEN], comp[LIBNAMELEN], *p, *name;
222 	int i, search;
223 
224 	if(histfrogp <= 0)
225 		return;
226 
227 	name = fn1;
228 	search = 0;
229 	if(histfrog[0]->name[1] == '/') {
230 		sprint(name, "");
231 		i = 1;
232 	} else if(histfrog[0]->name[1] == '.') {
233 		sprint(name, ".");
234 		i = 0;
235 	} else {
236 		sprint(name, "");
237 		i = 0;
238 		search = 1;
239 	}
240 
241 	for(; i<histfrogp; i++) {
242 		snprint(comp, sizeof comp, histfrog[i]->name+1);
243 		for(;;) {
244 			p = strstr(comp, "$O");
245 			if(p == 0)
246 				break;
247 			memmove(p+1, p+2, strlen(p+2)+1);
248 			p[0] = thechar;
249 		}
250 		for(;;) {
251 			p = strstr(comp, "$M");
252 			if(p == 0)
253 				break;
254 			if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
255 				diag("library component too long");
256 				return;
257 			}
258 			memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
259 			memmove(p, thestring, strlen(thestring));
260 		}
261 		if(strlen(fn1) + strlen(comp) + 3 >= sizeof(fn1)) {
262 			diag("library component too long");
263 			return;
264 		}
265 		if(i > 0 || !search)
266 			strcat(fn1, "/");
267 		strcat(fn1, comp);
268 	}
269 
270 	cleanname(name);
271 
272 	if(search){
273 		p = findlib(name);
274 		if(p != nil){
275 			snprint(fn2, sizeof(fn2), "%s/%s", p, name);
276 			name = fn2;
277 		}
278 	}
279 
280 	for(i=0; i<libraryp; i++)
281 		if(strcmp(name, library[i]) == 0)
282 			return;
283 	if(libraryp == nelem(library)){
284 		diag("too many autolibs; skipping %s", name);
285 		return;
286 	}
287 
288 	p = malloc(strlen(name) + 1);
289 	strcpy(p, name);
290 	library[libraryp] = p;
291 	p = malloc(strlen(obj) + 1);
292 	strcpy(p, obj);
293 	libraryobj[libraryp] = p;
294 	libraryp++;
295 }
296 
297 void
addhist(long line,int type)298 addhist(long line, int type)
299 {
300 	Auto *u;
301 	Sym *s;
302 	int i, j, k;
303 
304 	u = malloc(sizeof(Auto));
305 	s = malloc(sizeof(Sym));
306 	s->name = malloc(2*(histfrogp+1) + 1);
307 
308 	u->asym = s;
309 	u->type = type;
310 	u->aoffset = line;
311 	u->link = curhist;
312 	curhist = u;
313 
314 	j = 1;
315 	for(i=0; i<histfrogp; i++) {
316 		k = histfrog[i]->value;
317 		s->name[j+0] = k>>8;
318 		s->name[j+1] = k;
319 		j += 2;
320 	}
321 }
322 
323 void
histtoauto(void)324 histtoauto(void)
325 {
326 	Auto *l;
327 
328 	while((l = curhist) != nil) {
329 		curhist = l->link;
330 		l->link = curauto;
331 		curauto = l;
332 	}
333 }
334 
335 void
collapsefrog(Sym * s)336 collapsefrog(Sym *s)
337 {
338 	int i;
339 
340 	/*
341 	 * bad encoding of path components only allows
342 	 * MAXHIST components. if there is an overflow,
343 	 * first try to collapse xxx/..
344 	 */
345 	for(i=1; i<histfrogp; i++)
346 		if(strcmp(histfrog[i]->name+1, "..") == 0) {
347 			memmove(histfrog+i-1, histfrog+i+1,
348 				(histfrogp-i-1)*sizeof(histfrog[0]));
349 			histfrogp--;
350 			goto out;
351 		}
352 
353 	/*
354 	 * next try to collapse .
355 	 */
356 	for(i=0; i<histfrogp; i++)
357 		if(strcmp(histfrog[i]->name+1, ".") == 0) {
358 			memmove(histfrog+i, histfrog+i+1,
359 				(histfrogp-i-1)*sizeof(histfrog[0]));
360 			goto out;
361 		}
362 
363 	/*
364 	 * last chance, just truncate from front
365 	 */
366 	memmove(histfrog+0, histfrog+1,
367 		(histfrogp-1)*sizeof(histfrog[0]));
368 
369 out:
370 	histfrog[histfrogp-1] = s;
371 }
372 
373 uchar*
readsome(int f,uchar * buf,uchar * good,uchar * stop,int max)374 readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
375 {
376 	int n;
377 
378 	n = stop - good;
379 	memmove(buf, good, stop - good);
380 	stop = buf + n;
381 	n = MAXIO - n;
382 	if(n > max)
383 		n = max;
384 	n = read(f, stop, n);
385 	if(n <= 0)
386 		return 0;
387 	return stop + n;
388 }
389 
390 Sym*
lookup(char * symb,int v)391 lookup(char *symb, int v)
392 {
393 	Sym *s;
394 	char *p;
395 	long h;
396 	int c, l;
397 
398 	h = v;
399 	for(p=symb; c = *p; p++)
400 		h = h+h+h + c;
401 	l = (p - symb) + 1;
402 	if(h < 0)
403 		h = ~h;
404 	h %= NHASH;
405 	for(s = hash[h]; s != S; s = s->link)
406 		if(s->version == v)
407 		if(memcmp(s->name, symb, l) == 0)
408 			return s;
409 
410 	s = halloc(sizeof(Sym));
411 	s->name = malloc(l + 1);
412 	memmove(s->name, symb, l);
413 
414 	s->link = hash[h];
415 	s->type = 0;
416 	s->version = v;
417 	s->value = 0;
418 	s->sig = 0;
419 	hash[h] = s;
420 	return s;
421 }
422 
423 int
find1(long l,int c)424 find1(long l, int c)
425 {
426 	char *p;
427 	int i;
428 
429 	p = (char*)&l;
430 	for(i=0; i<4; i++)
431 		if(*p++ == c)
432 			return i;
433 	return 0;
434 }
435 
436 long
ieeedtof(Ieee * ieeep)437 ieeedtof(Ieee *ieeep)
438 {
439 	int exp;
440 	long v;
441 
442 	if(ieeep->h == 0)
443 		return 0;
444 	exp = (ieeep->h>>20) & ((1L<<11)-1L);
445 	exp -= (1L<<10) - 2L;
446 	v = (ieeep->h & 0xfffffL) << 3;
447 	v |= (ieeep->l >> 29) & 0x7L;
448 	if((ieeep->l >> 28) & 1) {
449 		v++;
450 		if(v & 0x800000L) {
451 			v = (v & 0x7fffffL) >> 1;
452 			exp++;
453 		}
454 	}
455 	if(exp <= -126 || exp >= 130)
456 		diag("double fp to single fp overflow");
457 	v |= ((exp + 126) & 0xffL) << 23;
458 	v |= ieeep->h & 0x80000000L;
459 	return v;
460 }
461 
462 double
ieeedtod(Ieee * ieeep)463 ieeedtod(Ieee *ieeep)
464 {
465 	Ieee e;
466 	double fr;
467 	int exp;
468 
469 	if(ieeep->h & (1L<<31)) {
470 		e.h = ieeep->h & ~(1L<<31);
471 		e.l = ieeep->l;
472 		return -ieeedtod(&e);
473 	}
474 	if(ieeep->l == 0 && ieeep->h == 0)
475 		return 0;
476 	fr = ieeep->l & ((1L<<16)-1L);
477 	fr /= 1L<<16;
478 	fr += (ieeep->l>>16) & ((1L<<16)-1L);
479 	fr /= 1L<<16;
480 	fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
481 	fr /= 1L<<21;
482 	exp = (ieeep->h>>20) & ((1L<<11)-1L);
483 	exp -= (1L<<10) - 2L;
484 	return ldexp(fr, exp);
485 }
486