xref: /csrg-svn/old/as.vax/assyms.c (revision 13572)
15828Srrh /*
25828Srrh  *	Copyright (c) 1982 Regents of the University of California
35828Srrh  */
45828Srrh #ifndef lint
5*13572Srrh static char sccsid[] = "@(#)assyms.c 4.13 07/01/83";
65828Srrh #endif not lint
75828Srrh 
8600Sbill #include <stdio.h>
9600Sbill #include <ctype.h>
10600Sbill #include "as.h"
11600Sbill #include "asscan.h"
12600Sbill #include "assyms.h"
13600Sbill 
14600Sbill /*
15600Sbill  *	Managers for chunks of symbols allocated from calloc()
16600Sbill  *	We maintain a linked list of such chunks.
17600Sbill  *
18600Sbill  */
19600Sbill struct	allocbox	*allochead;	/*head of chunk list*/
20600Sbill struct	allocbox	*alloctail;	/*tail*/
21600Sbill struct	allocbox	*newbox;	/*for creating a new chunk*/
22600Sbill struct	symtab		*nextsym;	/*next symbol free*/
23600Sbill int			symsleft;	/*slots left in current chunk*/
24600Sbill 
25600Sbill struct	symtab		**symptrs;
26600Sbill struct	symtab		**symdelim[NLOC + NLOC +1];
27600Sbill struct	symtab		**symptrub;
28600Sbill /*
29600Sbill  *	Managers for the dynamically extendable hash table
30600Sbill  */
31600Sbill struct	hashdallop	*htab;
32600Sbill 
335828Srrh Iptr	*itab[NINST];	/*maps opcodes to instructions*/
34600Sbill /*
35600Sbill  *	Counts what went into the symbol table, so that the
36600Sbill  *	size of the symbol table can be computed.
37600Sbill  */
38600Sbill int	nsyms;		/* total number in the symbol table */
39600Sbill int	njxxx;		/* number of jxxx entrys */
40600Sbill int	nforgotten;	/* number of symbols erroneously entered */
41600Sbill int	nlabels;	/* number of label entries */
42600Sbill 
43600Sbill /*
44600Sbill  *	Managers of the symbol literal storage.
45600Sbill  */
46600Sbill struct	strpool		*strplhead = 0;
47600Sbill 
48600Sbill symtabinit()
49600Sbill {
50600Sbill 	allochead = 0;
51600Sbill 	alloctail = 0;
52600Sbill 	nextsym = 0;
53600Sbill 	symsleft = 0;
54600Sbill 	strpoolalloc();		/* get the first strpool storage area */
55600Sbill 	htab = 0;
56600Sbill 	htaballoc();		/* get the first part of the hash table */
57600Sbill }
58600Sbill 
59600Sbill /*
60600Sbill  *	Install all known instructions in the symbol table
61600Sbill  */
62600Sbill syminstall()
63600Sbill {
645828Srrh 	register	Iptr	ip;
65600Sbill 	register	struct	symtab	**hp;
66600Sbill 	register	char	*p1, *p2;
675828Srrh 	register	int	i;
68600Sbill 
695828Srrh 	for (i = 0; i < NINST; i++)
705828Srrh 		itab[i] = (Iptr*)BADPOINT;
715828Srrh 
7213514Srrh 	for (ip = (Iptr)instab; FETCHNAME(ip)[0]; ip++) {
7313514Srrh 		p1 = FETCHNAME(ip);
74600Sbill 		p2 = yytext;
75600Sbill 		while (*p2++ = *p1++);
76600Sbill 		hp = lookup(0);		/* 0 => don't install this*/
77600Sbill 		if (*hp==NULL) {
78600Sbill 			*hp = (struct symtab *)ip;
79634Shenry 			if (   (ip->s_tag!=INSTn)
80634Shenry 			    && (ip->s_tag!=INST0)
81634Shenry 			    && (ip->s_tag!=0))
82600Sbill 				continue; /* was pseudo-op */
835828Srrh 			if (itab[ip->i_eopcode] == (Iptr*)BADPOINT){
845828Srrh 				itab[ip->i_eopcode] =
855828Srrh 					(Iptr*)ClearCalloc(256, sizeof(Iptr));
865828Srrh 				for (i = 0; i < 256; i++)
875828Srrh 					itab[ip->i_eopcode][i] =
885828Srrh 						(Iptr)BADPOINT;
895828Srrh 			}
905828Srrh 			itab[ip->i_eopcode][ip->i_popcode] = ip;
91600Sbill 		}
92600Sbill 	}
93600Sbill }	/*end of syminstall*/
94600Sbill 
95600Sbill 
96600Sbill /*
97600Sbill  *	Assign final values to symbols,
98600Sbill  *	and overwrite the index field with its relative position in
99600Sbill  *	the symbol table we give to the loader.
100600Sbill  */
101600Sbill extern struct exec hdr;
102600Sbill 
103600Sbill freezesymtab()
104600Sbill {
105600Sbill 	register	struct	symtab	*sp;
106600Sbill 				long	bs;
107600Sbill 	register	int	relpos = 0;
108600Sbill 	register	struct	symtab		*ubsp;
109600Sbill 	register	struct	allocbox	*allocwalk;
110600Sbill 
111600Sbill 	DECLITERATE(allocwalk, sp, ubsp)
112600Sbill 	{
113634Shenry 		if (sp->s_tag >= IGNOREBOUND)
114600Sbill 			continue; 		/*totally ignore jxxx entries */
115600Sbill 		/*
116600Sbill 		 *	Ignore stabs, but give them a symbol table index
117600Sbill 		 */
118634Shenry 		if (sp->s_type & STABFLAG)
119600Sbill 			goto assignindex;
120634Shenry 		if ((sp->s_type&XTYPE)==XUNDEF)
121634Shenry 			sp->s_type = XXTRN+XUNDEF;
122634Shenry 		else if ((sp->s_type&XTYPE)==XDATA)
123634Shenry 			sp->s_value += usedot[sp->s_index].e_xvalue;
124634Shenry 		else if ((sp->s_type&XTYPE)==XTEXT)
125634Shenry 			sp->s_value += usedot[sp->s_index].e_xvalue;
126634Shenry 		else if ((sp->s_type&XTYPE)==XBSS) {
127634Shenry 			bs = sp->s_value;
128634Shenry 			sp->s_value = hdr.a_bss + datbase;
129600Sbill 			hdr.a_bss += bs;
130600Sbill 		}
131600Sbill 	   assignindex:
13213514Srrh 		if (    (FETCHNAME(sp)[0] != 'L')
133634Shenry 		     || (sp->s_tag != LABELID)
134600Sbill 		     || savelabels
135600Sbill 		     )			/*then, we will write it later on*/
136634Shenry 				sp->s_index = relpos++;
137600Sbill 	}
138600Sbill }
139600Sbill 
140600Sbill 
141600Sbill 
142600Sbill /*
143600Sbill  *	For all of the stabs that had their final value undefined during pass 1
144600Sbill  *	and during pass 2 assign a final value.
145600Sbill  *	We have already given stab entrys a initial approximation
146600Sbill  *	when we constsructed the sorted symbol table.
147600Sbill  *	Iteration order doesn't matter.
148600Sbill  */
14912592Scsvaf 
15012592Scsvaf stabfix()
15112592Scsvaf {
152600Sbill 	register struct symtab *sp, **cosp;
153600Sbill 	register struct symtab *p;
154600Sbill 
155600Sbill 	SYMITERATE(cosp, sp){
156634Shenry 		if(sp->s_ptype && (sp->s_type & STABFLAG)) {
157634Shenry 			p = sp->s_dest;
15812592Scsvaf /*
15912592Scsvaf  * STABFLOATING indicates that the offset has been saved in s_desc, s_other
16012592Scsvaf  */
16112592Scsvaf 			if(sp->s_tag == STABFLOATING) {
16212592Scsvaf 			  sp->s_value = ( ( ((unsigned char) sp->s_other) << 16)  					| ( (unsigned short) sp->s_desc )  );
16312592Scsvaf 			  sp->s_value = sp->s_value + p->s_value;
16412592Scsvaf 			}
16512592Scsvaf 			else sp->s_value = p->s_value;
166634Shenry 			sp->s_index = p->s_index;
167634Shenry 			sp->s_type = p->s_type;
16812592Scsvaf 
16912592Scsvaf 
170600Sbill 		}
171600Sbill 	}
172600Sbill }
173600Sbill 
174600Sbill char *Calloc(number, size)
175600Sbill 	int	number, size;
176600Sbill {
177600Sbill 	register	char *newstuff;
1785828Srrh 	char	*sbrk();
1795828Srrh 	newstuff = sbrk(number*size);
180600Sbill 	if ((int)newstuff == -1){
181600Sbill 		yyerror("Ran out of Memory");
182600Sbill 		delexit();
183600Sbill 	}
184600Sbill 	return(newstuff);
185600Sbill }
186600Sbill 
187600Sbill char *ClearCalloc(number, size)
188600Sbill 	int	number, size;
189600Sbill {
190600Sbill 	register	char	*newstuff;		/* r11 */
191600Sbill 	register	int	length = number * size;	/* r10 */
1925828Srrh #ifdef lint
1935828Srrh 	length = length;
1945828Srrh #endif length
195600Sbill 	newstuff = Calloc(number, size);
196600Sbill 	asm("movc5 $0, (r0), $0, r10, (r11)");
197600Sbill 	return(newstuff);
198600Sbill }
199600Sbill 
200600Sbill struct symtab *symalloc()
201600Sbill {
202600Sbill 	if (symsleft == 0){
203600Sbill 		newbox = (struct allocbox *)ClearCalloc(1,ALLOCQTY);
204600Sbill 		symsleft = SYMDALLOP;
205600Sbill 		nextsym = &newbox->symslots[0];
206600Sbill 		if (alloctail == 0){
207600Sbill 			allochead = alloctail = newbox;
208600Sbill 		} else {
209600Sbill 			alloctail->nextalloc = newbox;
210600Sbill 			alloctail = newbox;
211600Sbill 		}
212600Sbill 	}
213600Sbill 	--symsleft;
214600Sbill 	++nsyms;
215600Sbill 	return(nextsym++);
216600Sbill }
217600Sbill 
218600Sbill strpoolalloc()
219600Sbill {
220600Sbill 	register	struct	strpool	*new;
221600Sbill 
222600Sbill 	new = (struct strpool *)Calloc(1, sizeof (struct strpool));
223600Sbill 	new->str_nalloc = 0;
224600Sbill 	new->str_next = strplhead;
225600Sbill 	strplhead = new;
226600Sbill }
227600Sbill 
228600Sbill symcmp(Pptr, Qptr)
229600Sbill 	struct symtab **Pptr, **Qptr;
230600Sbill {
231600Sbill 	register struct symtab *p = *Pptr;
232600Sbill 	register struct symtab *q = *Qptr;
233634Shenry 	if (p->s_index < q->s_index)
234600Sbill 		return(-1);
235634Shenry 	if (p->s_index > q->s_index)
236600Sbill 		return(1);
237634Shenry 	if (p->s_value < q->s_value)
238600Sbill 		return(-1);
239634Shenry 	if (p->s_value > q->s_value)
240600Sbill 		return(1);
241600Sbill 	/*
242600Sbill 	 *	Force jxxx entries to virtually preceed labels defined
243600Sbill 	 *	to follow the jxxxx instruction, so that bumping the
244600Sbill 	 *	jxxx instruction correctly fixes up the following labels
245600Sbill 	 */
246634Shenry 	if (p->s_tag >= IGNOREBOUND)	/*p points to a jxxx*/
247600Sbill 		return(-1);
248634Shenry 	if (q->s_tag >= IGNOREBOUND)
249600Sbill 		return(1);
250600Sbill 	/*
251600Sbill 	 *	both are now just plain labels; the relative order doesn't
252600Sbill 	 *	matter.  Both can't be jxxxes, as they would have different
253600Sbill 	 *	values.
254600Sbill 	 */
255600Sbill 	return(0);
256600Sbill }	/*end of symcmp*/
257600Sbill 
258600Sbill /*
259600Sbill  *	We construct the auxiliary table of pointers, symptrs and
260600Sbill  *	symdelim
261600Sbill  *	We also assign preliminary values to stab entries that did not yet
262600Sbill  *	have an absolute value (because they initially referred to
263600Sbill  *	forward references). We don't worry about .stabds, as they
264600Sbill  *	already have an estimated final value
265600Sbill  */
266600Sbill 
267600Sbill sortsymtab()
268600Sbill {
269600Sbill 	register	struct	symtab	*sp;
270600Sbill 	register	struct	symtab	**cowalk;
271600Sbill 	register	struct	allocbox	*allocwalk;
272600Sbill 			struct	symtab	*ubsp;
273600Sbill 				int	segno;
274600Sbill 				int	slotno;
275600Sbill 				int	symsin;	/*number put into symptrs*/
276600Sbill 
277600Sbill 	symptrs =  (struct symtab **)Calloc(nsyms + 2, sizeof *symptrs);
278600Sbill 	/*
279600Sbill 	 *	Allocate one word at the beginning of the symptr array
280600Sbill 	 *	so that backwards scans through the symptr array will
281600Sbill 	 *	work correctly while scanning through the zeroth segment
282600Sbill 	 */
283600Sbill 	*symptrs++ = 0;
284600Sbill 	cowalk = symptrs;
285600Sbill 	symsin = 0;
286600Sbill 	DECLITERATE(allocwalk, sp, ubsp) {
287634Shenry 		if (sp->s_ptype && (sp->s_type &STABFLAG)){
288634Shenry 			sp->s_value = sp->s_dest->s_value;
289634Shenry 			sp->s_index = sp->s_dest->s_index;
290600Sbill 		}
291600Sbill 		if (symsin >= nsyms)
292600Sbill 			yyerror("INTERNAL ERROR: overfilled symbol table indirection table");
293600Sbill 		*cowalk++ = sp;
294600Sbill 		symsin++;
295600Sbill 	}
296600Sbill 	if (symsin != nsyms)
297600Sbill 		yyerror("INTERNAL ERROR: installed %d syms, should have installed %d",
298600Sbill 			symsin, nsyms);
299600Sbill 	symptrub = &symptrs[nsyms ];
300600Sbill 	qsort(symptrs, nsyms, sizeof *symptrs, symcmp);
301600Sbill 	symdelim[0] = symptrs;
302600Sbill 	for (cowalk = symptrs, sp = *cowalk, segno = 0, slotno = 1;
303600Sbill 	     segno < NLOC + NLOC;
304600Sbill 	     segno++, slotno++){
305634Shenry 		for (; sp && sp->s_index == segno; sp = *++cowalk);
306600Sbill 		symdelim[slotno] = cowalk;	/*forms the ub delimeter*/
307600Sbill 	}
308600Sbill }	/*end of sortsymtab*/
309600Sbill 
310600Sbill #ifdef DEBUG
311600Sbill dumpsymtab()
312600Sbill {
313600Sbill 	register	int	segno;
314600Sbill 	register	struct symtab *sp, **cosp, *ub;
315600Sbill 	char		*tagstring();
316600Sbill 
317600Sbill 	printf("Symbol Table dump:\n");
318600Sbill 	for (segno = 0; segno < NLOC + NLOC; segno++){
319600Sbill 		printf("Segment number: %d\n", segno);
320600Sbill 		SEGITERATE(segno, 0, 0, cosp, sp, ub, ++){
321600Sbill 			printf("\tSeg: %d \"%s\" value: %d index: %d tag %s\n",
32213514Srrh 				segno, FETCHNAME(sp),
323634Shenry 				sp->s_value, sp->s_index,
324634Shenry 				tagstring(sp->s_tag));
325600Sbill 			printf("\t\ttype: %d jxbump %d jxfear: %d\n",
326634Shenry 				sp->s_type, sp->s_jxbump, sp->s_jxfear);
327600Sbill 		}
328600Sbill 		printf("\n\n");
329600Sbill 	}
330600Sbill }
331600Sbill 
332600Sbill static	char tagbuff[4];
333600Sbill 
334600Sbill char *tagstring(tag)
335600Sbill 	unsigned	char	tag;
336600Sbill {
337600Sbill 	switch(tag){
338600Sbill 		case JXACTIVE:		return("active");
339600Sbill 		case JXNOTYET:		return("notyet");
340600Sbill 		case JXALIGN:		return("align");
341600Sbill 		case JXQUESTIONABLE:	return("jxquestionable");
342600Sbill 		case JXINACTIVE:	return("inactive");
343600Sbill 		case JXTUNNEL:		return("tunnel");
344600Sbill 		case OBSOLETE:		return("obsolete");
345600Sbill 		case IGNOREBOUND:	return("ignorebound");
346600Sbill 		case STABFLOATING:	return("stabfloating");
347600Sbill 		case STABFIXED:		return("stabfixed");
348600Sbill 		case LABELID:		return("labelid");
349600Sbill 		case OKTOBUMP:		return("oktobump");
350600Sbill 		case ISET:		return("iset");
351600Sbill 		case ILSYM:		return("ilsym");
352600Sbill 		default:		sprintf(tagbuff,"%d", tag);
353600Sbill 					return(tagbuff);
354600Sbill 	}
355600Sbill }
356600Sbill #endif DEBUG
357600Sbill 
358600Sbill htaballoc()
359600Sbill {
360600Sbill 	register	struct	hashdallop	*new;
361600Sbill 	new = (struct hashdallop *)ClearCalloc(1, sizeof (struct hashdallop));
362600Sbill 	if (htab == 0)
363600Sbill 		htab = new;
364600Sbill 	else {		/* add AFTER the 1st slot */
365600Sbill 		new->h_next = htab->h_next;
366600Sbill 		htab->h_next = new;
367600Sbill 	}
368600Sbill }
369600Sbill 
370600Sbill #define 	HASHCLOGGED	(NHASH / 2)
371600Sbill 
372600Sbill /*
373600Sbill  *	Lookup a symbol stored in extern yytext.
374600Sbill  *	All strings passed in via extern yytext had better have
375600Sbill  *	a trailing null.  Strings are placed in yytext for hashing by
376600Sbill  *	syminstall() and by yylex();
377600Sbill  *
378600Sbill  *	We take pains to avoid function calls; this functdion
379600Sbill  *	is called quite frequently, and the calls overhead
380600Sbill  *	in the vax contributes significantly to the overall
381600Sbill  *	execution speed of as.
382600Sbill  */
383600Sbill struct symtab **lookup(instflg)
384600Sbill 	int	instflg;		/* 0: don't install */
385600Sbill {
386600Sbill 	static	 int		initialprobe;
387600Sbill 	register struct	symtab 	**hp;
388600Sbill 	register char 		*from;
389600Sbill 	register char		*to;
390600Sbill 	register	int	len;
391600Sbill 	register	int	nprobes;
39213514Srrh 	static	struct	hashdallop *hdallop;
39313514Srrh 	static	struct	symtab	**emptyslot;
39413514Srrh 	static 	struct	hashdallop *emptyhd;
39513514Srrh 	static	struct	symtab	**hp_ub;
396600Sbill 
397600Sbill 	emptyslot = 0;
398600Sbill 	for (nprobes = 0, from = yytext;
399600Sbill 	     *from;
400600Sbill 	     nprobes <<= 2, nprobes += *from++)
401600Sbill 		continue;
402600Sbill 	nprobes += from[-1] << 5;
403600Sbill 	nprobes %= NHASH;
404600Sbill 	if (nprobes < 0)
405600Sbill 		nprobes += NHASH;
406600Sbill 
407600Sbill 	initialprobe = nprobes;
408600Sbill 	for (hdallop = htab; hdallop != 0; hdallop = hdallop->h_next){
409600Sbill 		for (hp = &(hdallop->h_htab[initialprobe]),
410600Sbill 				nprobes = 1,
411600Sbill 				hp_ub = &(hdallop->h_htab[NHASH]);
412600Sbill 		     (*hp) && (nprobes < NHASH);
413600Sbill 				hp += nprobes,
414600Sbill 				hp -= (hp >= hp_ub) ? NHASH:0,
415600Sbill 				nprobes += 2)
416600Sbill 		{
417600Sbill 			from = yytext;
41813514Srrh 			to = FETCHNAME(*hp);
419600Sbill 			while (*from && *to)
420600Sbill 				if (*from++ != *to++)
421600Sbill 					goto nextprobe;
422600Sbill 			if (*to == *from)	/*assert both are == 0*/
423600Sbill 				return(hp);
42413522Srrh 		nextprobe: ;
425600Sbill 		}
426600Sbill 		if (*hp == 0 && emptyslot == 0 &&
427600Sbill 		    hdallop->h_nused < HASHCLOGGED) {
428600Sbill 			emptyslot = hp;
429600Sbill 			emptyhd = hdallop;
430600Sbill 		}
431600Sbill 	}
432600Sbill 	if (emptyslot == 0) {
433600Sbill 		htaballoc();
434600Sbill 		hdallop = htab->h_next;		/* aren't we smart! */
435600Sbill 		hp = &hdallop->h_htab[initialprobe];
436600Sbill 	} else {
437600Sbill 		hdallop = emptyhd;
438600Sbill 		hp = emptyslot;
439600Sbill 	}
440600Sbill 	if (instflg) {
441600Sbill 		*hp = symalloc();
442600Sbill 		hdallop->h_nused++;
44313447Srrh 		for (from = yytext, len = 0; *from++; len++)
444600Sbill 			continue;
445*13572Srrh 		(*hp)->s_name = (char *)savestr(yytext, len + 1, STR_BOTH);
446600Sbill 	}
447600Sbill 	return(hp);
448600Sbill }	/*end of lookup*/
44913447Srrh /*
450*13572Srrh  *	save a string str with len in the places indicated by place
45113447Srrh  */
452*13572Srrh struct strdesc *savestr(str, len, place)
45313447Srrh 	char	*str;
454*13572Srrh 	int	len;
455*13572Srrh 	int	place;
456600Sbill {
45713514Srrh 	reg	struct	strdesc	*res;
45813514Srrh 		int	tlen;
459*13572Srrh 	/*
460*13572Srrh 	 *	Compute the total length of the record to live in core
461*13572Srrh 	 */
46213514Srrh 	tlen = sizeof(struct strdesc) - sizeof(res->sd_string);
463*13572Srrh 	if (place & STR_CORE)
464*13572Srrh 		tlen += len;
465*13572Srrh 	/*
466*13572Srrh 	 *	See if there is enough space for the record,
467*13572Srrh 	 *	and allocate the record.
468*13572Srrh 	 */
46913514Srrh 	if (tlen >= (STRPOOLDALLOP - strplhead->str_nalloc))
470600Sbill 		strpoolalloc();
47113514Srrh 	res = (struct strdesc *)(strplhead->str_names + strplhead->str_nalloc);
472*13572Srrh 	/*
473*13572Srrh 	 *	Save the string information that is always present
474*13572Srrh 	 */
475*13572Srrh 	res->sd_stroff = strfilepos;
476*13572Srrh 	res->sd_strlen = len;
477*13572Srrh 	res->sd_place = place;
478*13572Srrh 	/*
479*13572Srrh 	 *	Now, save the string itself.  If str is null, then
480*13572Srrh 	 *	the characters have already been dumped to the file
481*13572Srrh 	 */
482*13572Srrh 	if ((place & STR_CORE) && str)
483*13572Srrh 		movestr(res[0].sd_string, str, len);
484*13572Srrh 	if (place & STR_FILE){
485*13572Srrh 		if (str){
486*13572Srrh 			fwrite(str, 1, len, strfile);
487*13572Srrh 		}
488*13572Srrh 		strfilepos += len;
489*13572Srrh 	}
490*13572Srrh 	/*
491*13572Srrh 	 *	Adjust the in core string pool size
492*13572Srrh 	 */
49313514Srrh 	strplhead->str_nalloc += tlen;
49413447Srrh 	return(res);
495600Sbill }
496600Sbill /*
497600Sbill  *	The relocation information is saved internally in an array of
498600Sbill  *	lists of relocation buffers.  The relocation buffers are
499600Sbill  *	exactly the same size as a token buffer; if we use VM for the
500600Sbill  *	temporary file we reclaim this storage, otherwise we create
501600Sbill  *	them by mallocing.
502600Sbill  */
503600Sbill #define	RELBUFLG	TOKBUFLG
504600Sbill #define	NRELOC		((TOKBUFLG - \
505600Sbill 			  (sizeof (int) + sizeof (struct relbufdesc *)) \
506600Sbill 			) / (sizeof (struct relocation_info)))
507600Sbill 
508600Sbill struct	relbufdesc{
509600Sbill 	int	rel_count;
510600Sbill 	struct	relbufdesc	*rel_next;
511600Sbill 	struct	relocation_info	rel_reloc[NRELOC];
512600Sbill };
513600Sbill extern	struct	relbufdesc	*tok_free;
514600Sbill #define	rel_free tok_free
515600Sbill static	struct	relbufdesc	*rel_temp;
516634Shenry struct	relocation_info r_can_1PC;
517634Shenry struct	relocation_info	r_can_0PC;
518600Sbill 
519600Sbill initoutrel()
520600Sbill {
521634Shenry 	r_can_0PC.r_address = 0;
522634Shenry 	r_can_0PC.r_symbolnum = 0;
523634Shenry 	r_can_0PC.r_pcrel = 0;
524634Shenry 	r_can_0PC.r_length = 0;
525634Shenry 	r_can_0PC.r_extern = 0;
526634Shenry 
527634Shenry 	r_can_1PC = r_can_0PC;
528600Sbill 	r_can_1PC.r_pcrel = 1;
529600Sbill }
530600Sbill 
531675Shenry outrel(xp, reloc_how)
532675Shenry 	register	struct	exp	*xp;
5335828Srrh 	int		reloc_how;	/* TYPB..TYPH + (possibly)RELOC_PCREL */
534600Sbill {
535675Shenry 	struct		relocation_info	reloc;
536675Shenry 	register	int	x_type_mask;
53713447Srrh 			int	pcrel;
538600Sbill 
539675Shenry 	x_type_mask = xp->e_xtype & ~XFORW;
540675Shenry 	pcrel = reloc_how & RELOC_PCREL;
541675Shenry 	reloc_how &= ~RELOC_PCREL;
542675Shenry 
543600Sbill 	if (bitoff&07)
544600Sbill 		yyerror("Padding error");
545675Shenry 	if (x_type_mask == XUNDEF)
546600Sbill 		yyerror("Undefined reference");
547600Sbill 
548675Shenry 	if ( (x_type_mask != XABS) || pcrel ) {
549675Shenry 		if (ty_NORELOC[reloc_how])
5505828Srrh 			yyerror("Illegal Relocation of floating or large int number.");
551675Shenry 		reloc = pcrel ? r_can_1PC : r_can_0PC;
552634Shenry 		reloc.r_address = dotp->e_xvalue -
553640Sbill 		    ( (dotp < &usedot[NLOC] || readonlydata) ? 0 : datbase );
554675Shenry 		reloc.r_length = ty_nlg[reloc_how];
555675Shenry 		switch(x_type_mask){
556600Sbill 			case XXTRN | XUNDEF:
557675Shenry 				reloc.r_symbolnum = xp->e_xname->s_index;
558600Sbill 				reloc.r_extern = 1;
559600Sbill 				break;
560600Sbill 			default:
561675Shenry 				if (readonlydata && (x_type_mask&~XXTRN) == XDATA)
562675Shenry 					x_type_mask = XTEXT | (x_type_mask&XXTRN);
563675Shenry 				reloc.r_symbolnum = x_type_mask;
564600Sbill 				break;
565600Sbill 		}
566600Sbill 		if ( (relfil == 0) || (relfil->rel_count >= NRELOC) ){
567600Sbill 			if (rel_free){
568600Sbill 				rel_temp = rel_free;
569600Sbill 				rel_free = rel_temp->rel_next;
570600Sbill 			} else {
571600Sbill 				rel_temp = (struct relbufdesc *)
572600Sbill 					Calloc(1,sizeof (struct relbufdesc));
573600Sbill 			}
574600Sbill 			rel_temp->rel_count = 0;
575600Sbill 			rel_temp->rel_next = relfil;
576600Sbill 			relfil = rusefile[dotp - &usedot[0]] = rel_temp;
577600Sbill 		}
578600Sbill 		relfil->rel_reloc[relfil->rel_count++] = reloc;
579600Sbill 	}
580600Sbill 	/*
581600Sbill 	 *	write the unrelocated value to the text file
582600Sbill 	 */
583675Shenry 	dotp->e_xvalue += ty_nbyte[reloc_how];
584675Shenry 	if (pcrel)
585675Shenry 		xp->e_xvalue -= dotp->e_xvalue;
5865828Srrh 	switch(reloc_how){
5875828Srrh 	case TYPO:
5885828Srrh 	case TYPQ:
5895828Srrh 
5905828Srrh 	case TYPF:
5915828Srrh 	case TYPD:
5925828Srrh 	case TYPG:
5935828Srrh 	case TYPH:
5945828Srrh 		bignumwrite(xp->e_number, reloc_how);
5955828Srrh 		break;
5965828Srrh 
5975828Srrh 	default:
5985828Srrh 		bwrite((char *)&(xp->e_xvalue), ty_nbyte[reloc_how], txtfil);
5995828Srrh 		break;
6005828Srrh 	}
601600Sbill }
602600Sbill /*
603600Sbill  *	Flush out all of the relocation information.
604600Sbill  *	Note that the individual lists of buffers are in
605600Sbill  *	reverse order, so we must reverse them
606600Sbill  */
607600Sbill off_t closeoutrel(relocfile)
608600Sbill 	BFILE	*relocfile;
609600Sbill {
610600Sbill 	int	locindex;
611600Sbill 	u_long	Closeoutrel();
612600Sbill 
613600Sbill 	trsize = 0;
614600Sbill 	for (locindex = 0; locindex < NLOC; locindex++){
615600Sbill 		trsize += Closeoutrel(rusefile[locindex], relocfile);
616600Sbill 	}
617600Sbill 	drsize = 0;
618600Sbill 	for (locindex = 0; locindex < NLOC; locindex++){
619600Sbill 		drsize += Closeoutrel(rusefile[NLOC + locindex], relocfile);
620600Sbill 	}
621600Sbill 	return(trsize + drsize);
622600Sbill }
623600Sbill 
624600Sbill u_long Closeoutrel(relfil, relocfile)
625600Sbill 	struct	relbufdesc	*relfil;
626600Sbill 	BFILE	*relocfile;
627600Sbill {
628600Sbill 	u_long	tail;
629600Sbill 	if (relfil == 0)
630600Sbill 		return(0L);
631600Sbill 	tail = Closeoutrel(relfil->rel_next, relocfile);
632600Sbill 	bwrite((char *)&relfil->rel_reloc[0],
633600Sbill 		relfil->rel_count * sizeof (struct relocation_info),
634600Sbill 		relocfile);
635600Sbill 	return(tail + relfil->rel_count * sizeof (struct relocation_info));
636600Sbill }
637600Sbill 
638634Shenry #define NOUTSYMS (nsyms - njxxx - nforgotten - (savelabels ? 0 : nlabels))
639600Sbill int sizesymtab()
640600Sbill {
641634Shenry 	return (sizeof (struct nlist) * NOUTSYMS);
642600Sbill }
643600Sbill /*
644600Sbill  *	Write out n symbols to file f, beginning at p
645600Sbill  *	ignoring symbols that are obsolete, jxxx instructions, and
646600Sbill  *	possibly, labels
647600Sbill  */
648600Sbill int symwrite(symfile)
649600Sbill 	BFILE *symfile;
650600Sbill {
65113514Srrh 		int	symsout;		/*those actually written*/
65213514Srrh 		int	symsdesired = NOUTSYMS;
65313514Srrh 	reg	struct	symtab *sp, *ub;
65413514Srrh 		char	*name;			/* temp to save the name */
65513514Srrh 		int	nread;
65613514Srrh 		char	rbuf[2048];
65713514Srrh 		int	i;
658634Shenry 	/*
659634Shenry 	 *	We use sp->s_index to hold the length of the
660634Shenry 	 *	name; it isn't used for anything else
661634Shenry 	 */
662600Sbill 	register	struct	allocbox	*allocwalk;
663600Sbill 
664600Sbill 	symsout = 0;
665600Sbill 	DECLITERATE(allocwalk, sp, ub)
666600Sbill 	{
667634Shenry 		if (sp->s_tag >= IGNOREBOUND)
668600Sbill 			continue;
66913514Srrh 		if ((FETCHNAME(sp)[0] == 'L') && (sp->s_tag == LABELID) && !savelabels)
670600Sbill 			continue;
671600Sbill 		symsout++;
672634Shenry 
673634Shenry 		name = sp->s_name;		/* save pointer */
67413447Srrh 		/*
67513447Srrh 		 *	the length of the symbol table string
67613447Srrh 		 *	always includes the trailing null
67713447Srrh 		 */
67813514Srrh 		if (sp->s_name && (sp->s_index = STRLEN(sp))){
67913514Srrh 			sp->s_nmx = STROFF(sp);	/* clobber */
680634Shenry 		} else {
68113447Srrh 			sp->s_nmx = 0;
682634Shenry 		}
683634Shenry 		sp->s_type = (sp->s_ptype != 0) ? sp->s_ptype : (sp->s_type & (~XFORW));
684640Sbill 		if (readonlydata && (sp->s_type&~N_EXT) == N_DATA)
685640Sbill 			sp->s_type = N_TEXT | (sp->s_type & N_EXT);
6865828Srrh 		bwrite((char *)&sp->s_nm, sizeof (struct nlist), symfile);
687634Shenry 		sp->s_name = name;		/* restore pointer */
688600Sbill 	}
689600Sbill 	if (symsout != symsdesired)
690600Sbill 		yyerror("INTERNAL ERROR: Wrote %d symbols, wanted to write %d symbols\n",
691600Sbill 			symsout, symsdesired);
692600Sbill 	/*
69313514Srrh 	 *	Copy the string temporary file to the symbol file,
69413514Srrh 	 *	copying all the strings and symbols we ever saw,
69513514Srrh 	 *	including labels, stabs strings, ascii strings, etc.
69613514Srrh 	 *	This is slightly wasteful.
697600Sbill 	 */
69813514Srrh 	i = 0;
69913514Srrh 	while((nread = read(strfile->_file, rbuf, sizeof(rbuf))) > 0){
70013514Srrh 		if (i == 0){
70113514Srrh 			((int *)rbuf)[0] = strfilepos;
70213447Srrh 		}
70313514Srrh 		bwrite(rbuf, nread, symfile);
70413514Srrh 		i++;
705600Sbill 	}
706600Sbill }
707