xref: /csrg-svn/old/as.vax/assyms.c (revision 13522)
15828Srrh /*
25828Srrh  *	Copyright (c) 1982 Regents of the University of California
35828Srrh  */
45828Srrh #ifndef lint
5*13522Srrh static char sccsid[] = "@(#)assyms.c 4.12 06/30/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;
39613514Srrh 	static	struct	strdesc	strdp;
397600Sbill 
398600Sbill 	emptyslot = 0;
399600Sbill 	for (nprobes = 0, from = yytext;
400600Sbill 	     *from;
401600Sbill 	     nprobes <<= 2, nprobes += *from++)
402600Sbill 		continue;
403600Sbill 	nprobes += from[-1] << 5;
404600Sbill 	nprobes %= NHASH;
405600Sbill 	if (nprobes < 0)
406600Sbill 		nprobes += NHASH;
407600Sbill 
408600Sbill 	initialprobe = nprobes;
409600Sbill 	for (hdallop = htab; hdallop != 0; hdallop = hdallop->h_next){
410600Sbill 		for (hp = &(hdallop->h_htab[initialprobe]),
411600Sbill 				nprobes = 1,
412600Sbill 				hp_ub = &(hdallop->h_htab[NHASH]);
413600Sbill 		     (*hp) && (nprobes < NHASH);
414600Sbill 				hp += nprobes,
415600Sbill 				hp -= (hp >= hp_ub) ? NHASH:0,
416600Sbill 				nprobes += 2)
417600Sbill 		{
418600Sbill 			from = yytext;
41913514Srrh 			to = FETCHNAME(*hp);
420600Sbill 			while (*from && *to)
421600Sbill 				if (*from++ != *to++)
422600Sbill 					goto nextprobe;
423600Sbill 			if (*to == *from)	/*assert both are == 0*/
424600Sbill 				return(hp);
425*13522Srrh 		nextprobe: ;
426600Sbill 		}
427600Sbill 		if (*hp == 0 && emptyslot == 0 &&
428600Sbill 		    hdallop->h_nused < HASHCLOGGED) {
429600Sbill 			emptyslot = hp;
430600Sbill 			emptyhd = hdallop;
431600Sbill 		}
432600Sbill 	}
433600Sbill 	if (emptyslot == 0) {
434600Sbill 		htaballoc();
435600Sbill 		hdallop = htab->h_next;		/* aren't we smart! */
436600Sbill 		hp = &hdallop->h_htab[initialprobe];
437600Sbill 	} else {
438600Sbill 		hdallop = emptyhd;
439600Sbill 		hp = emptyslot;
440600Sbill 	}
441600Sbill 	if (instflg) {
442600Sbill 		*hp = symalloc();
443600Sbill 		hdallop->h_nused++;
44413447Srrh 		for (from = yytext, len = 0; *from++; len++)
445600Sbill 			continue;
44613447Srrh 		/*
44713514Srrh 		 *	save string and trailing null, both
44813514Srrh 		 *	internally, and in the string temporary file
44913447Srrh 		 */
45013514Srrh 		strdp.sd_stroff = strfilepos;
45113514Srrh 		strdp.sd_place = STR_BOTH;
45213514Srrh 		strdp.sd_strlen = len + 1;	/* length and null */
45313514Srrh 		fputs(yytext, strfile);		/* string */
45413514Srrh 		putc(0, strfile);		/* null */
45513514Srrh 		strfilepos += strdp.sd_strlen;
45613514Srrh 		(*hp)->s_name = (char *)savestr(yytext, &strdp);
457600Sbill 	}
458600Sbill 	return(hp);
459600Sbill }	/*end of lookup*/
46013447Srrh /*
46113514Srrh  *	save a string str, descriptor strdp, in the string pool
46213447Srrh  */
46313514Srrh struct strdesc *savestr(str, strdp)
46413447Srrh 	char	*str;
46513514Srrh 	struct	strdesc	*strdp;
466600Sbill {
46713514Srrh 	reg	struct	strdesc	*res;
46813514Srrh 		int	tlen;
469600Sbill 
47013514Srrh 	tlen = sizeof(struct strdesc) - sizeof(res->sd_string);
47113514Srrh 	if (strdp->sd_place & STR_FILE)
47213514Srrh 		tlen += strdp->sd_strlen;
47313514Srrh 
47413514Srrh 	if (tlen >= (STRPOOLDALLOP - strplhead->str_nalloc))
475600Sbill 		strpoolalloc();
47613514Srrh 	res = (struct strdesc *)(strplhead->str_names + strplhead->str_nalloc);
47713514Srrh 	res[0] = *strdp;
47813514Srrh 	if (strdp->sd_place & STR_FILE)
47913514Srrh 		movestr(res[0].sd_string, str, strdp->sd_strlen);
48013514Srrh 	strplhead->str_nalloc += tlen;
48113447Srrh 	return(res);
482600Sbill }
483600Sbill 
484600Sbill /*
485600Sbill  *	The relocation information is saved internally in an array of
486600Sbill  *	lists of relocation buffers.  The relocation buffers are
487600Sbill  *	exactly the same size as a token buffer; if we use VM for the
488600Sbill  *	temporary file we reclaim this storage, otherwise we create
489600Sbill  *	them by mallocing.
490600Sbill  */
491600Sbill #define	RELBUFLG	TOKBUFLG
492600Sbill #define	NRELOC		((TOKBUFLG - \
493600Sbill 			  (sizeof (int) + sizeof (struct relbufdesc *)) \
494600Sbill 			) / (sizeof (struct relocation_info)))
495600Sbill 
496600Sbill struct	relbufdesc{
497600Sbill 	int	rel_count;
498600Sbill 	struct	relbufdesc	*rel_next;
499600Sbill 	struct	relocation_info	rel_reloc[NRELOC];
500600Sbill };
501600Sbill extern	struct	relbufdesc	*tok_free;
502600Sbill #define	rel_free tok_free
503600Sbill static	struct	relbufdesc	*rel_temp;
504634Shenry struct	relocation_info r_can_1PC;
505634Shenry struct	relocation_info	r_can_0PC;
506600Sbill 
507600Sbill initoutrel()
508600Sbill {
509634Shenry 	r_can_0PC.r_address = 0;
510634Shenry 	r_can_0PC.r_symbolnum = 0;
511634Shenry 	r_can_0PC.r_pcrel = 0;
512634Shenry 	r_can_0PC.r_length = 0;
513634Shenry 	r_can_0PC.r_extern = 0;
514634Shenry 
515634Shenry 	r_can_1PC = r_can_0PC;
516600Sbill 	r_can_1PC.r_pcrel = 1;
517600Sbill }
518600Sbill 
519675Shenry outrel(xp, reloc_how)
520675Shenry 	register	struct	exp	*xp;
5215828Srrh 	int		reloc_how;	/* TYPB..TYPH + (possibly)RELOC_PCREL */
522600Sbill {
523675Shenry 	struct		relocation_info	reloc;
524675Shenry 	register	int	x_type_mask;
52513447Srrh 			int	pcrel;
526600Sbill 
527675Shenry 	x_type_mask = xp->e_xtype & ~XFORW;
528675Shenry 	pcrel = reloc_how & RELOC_PCREL;
529675Shenry 	reloc_how &= ~RELOC_PCREL;
530675Shenry 
531600Sbill 	if (bitoff&07)
532600Sbill 		yyerror("Padding error");
533675Shenry 	if (x_type_mask == XUNDEF)
534600Sbill 		yyerror("Undefined reference");
535600Sbill 
536675Shenry 	if ( (x_type_mask != XABS) || pcrel ) {
537675Shenry 		if (ty_NORELOC[reloc_how])
5385828Srrh 			yyerror("Illegal Relocation of floating or large int number.");
539675Shenry 		reloc = pcrel ? r_can_1PC : r_can_0PC;
540634Shenry 		reloc.r_address = dotp->e_xvalue -
541640Sbill 		    ( (dotp < &usedot[NLOC] || readonlydata) ? 0 : datbase );
542675Shenry 		reloc.r_length = ty_nlg[reloc_how];
543675Shenry 		switch(x_type_mask){
544600Sbill 			case XXTRN | XUNDEF:
545675Shenry 				reloc.r_symbolnum = xp->e_xname->s_index;
546600Sbill 				reloc.r_extern = 1;
547600Sbill 				break;
548600Sbill 			default:
549675Shenry 				if (readonlydata && (x_type_mask&~XXTRN) == XDATA)
550675Shenry 					x_type_mask = XTEXT | (x_type_mask&XXTRN);
551675Shenry 				reloc.r_symbolnum = x_type_mask;
552600Sbill 				break;
553600Sbill 		}
554600Sbill 		if ( (relfil == 0) || (relfil->rel_count >= NRELOC) ){
555600Sbill 			if (rel_free){
556600Sbill 				rel_temp = rel_free;
557600Sbill 				rel_free = rel_temp->rel_next;
558600Sbill 			} else {
559600Sbill 				rel_temp = (struct relbufdesc *)
560600Sbill 					Calloc(1,sizeof (struct relbufdesc));
561600Sbill 			}
562600Sbill 			rel_temp->rel_count = 0;
563600Sbill 			rel_temp->rel_next = relfil;
564600Sbill 			relfil = rusefile[dotp - &usedot[0]] = rel_temp;
565600Sbill 		}
566600Sbill 		relfil->rel_reloc[relfil->rel_count++] = reloc;
567600Sbill 	}
568600Sbill 	/*
569600Sbill 	 *	write the unrelocated value to the text file
570600Sbill 	 */
571675Shenry 	dotp->e_xvalue += ty_nbyte[reloc_how];
572675Shenry 	if (pcrel)
573675Shenry 		xp->e_xvalue -= dotp->e_xvalue;
5745828Srrh 	switch(reloc_how){
5755828Srrh 	case TYPO:
5765828Srrh 	case TYPQ:
5775828Srrh 
5785828Srrh 	case TYPF:
5795828Srrh 	case TYPD:
5805828Srrh 	case TYPG:
5815828Srrh 	case TYPH:
5825828Srrh 		bignumwrite(xp->e_number, reloc_how);
5835828Srrh 		break;
5845828Srrh 
5855828Srrh 	default:
5865828Srrh 		bwrite((char *)&(xp->e_xvalue), ty_nbyte[reloc_how], txtfil);
5875828Srrh 		break;
5885828Srrh 	}
589600Sbill }
590600Sbill /*
591600Sbill  *	Flush out all of the relocation information.
592600Sbill  *	Note that the individual lists of buffers are in
593600Sbill  *	reverse order, so we must reverse them
594600Sbill  */
595600Sbill off_t closeoutrel(relocfile)
596600Sbill 	BFILE	*relocfile;
597600Sbill {
598600Sbill 	int	locindex;
599600Sbill 	u_long	Closeoutrel();
600600Sbill 
601600Sbill 	trsize = 0;
602600Sbill 	for (locindex = 0; locindex < NLOC; locindex++){
603600Sbill 		trsize += Closeoutrel(rusefile[locindex], relocfile);
604600Sbill 	}
605600Sbill 	drsize = 0;
606600Sbill 	for (locindex = 0; locindex < NLOC; locindex++){
607600Sbill 		drsize += Closeoutrel(rusefile[NLOC + locindex], relocfile);
608600Sbill 	}
609600Sbill 	return(trsize + drsize);
610600Sbill }
611600Sbill 
612600Sbill u_long Closeoutrel(relfil, relocfile)
613600Sbill 	struct	relbufdesc	*relfil;
614600Sbill 	BFILE	*relocfile;
615600Sbill {
616600Sbill 	u_long	tail;
617600Sbill 	if (relfil == 0)
618600Sbill 		return(0L);
619600Sbill 	tail = Closeoutrel(relfil->rel_next, relocfile);
620600Sbill 	bwrite((char *)&relfil->rel_reloc[0],
621600Sbill 		relfil->rel_count * sizeof (struct relocation_info),
622600Sbill 		relocfile);
623600Sbill 	return(tail + relfil->rel_count * sizeof (struct relocation_info));
624600Sbill }
625600Sbill 
626634Shenry #define NOUTSYMS (nsyms - njxxx - nforgotten - (savelabels ? 0 : nlabels))
627600Sbill int sizesymtab()
628600Sbill {
629634Shenry 	return (sizeof (struct nlist) * NOUTSYMS);
630600Sbill }
631600Sbill /*
632600Sbill  *	Write out n symbols to file f, beginning at p
633600Sbill  *	ignoring symbols that are obsolete, jxxx instructions, and
634600Sbill  *	possibly, labels
635600Sbill  */
636600Sbill int symwrite(symfile)
637600Sbill 	BFILE *symfile;
638600Sbill {
63913514Srrh 		int	symsout;		/*those actually written*/
64013514Srrh 		int	symsdesired = NOUTSYMS;
64113514Srrh 	reg	struct	symtab *sp, *ub;
64213514Srrh 		char	*name;			/* temp to save the name */
64313514Srrh 		int	nread;
64413514Srrh 		char	rbuf[2048];
64513514Srrh 		int	i;
646634Shenry 	/*
647634Shenry 	 *	We use sp->s_index to hold the length of the
648634Shenry 	 *	name; it isn't used for anything else
649634Shenry 	 */
650600Sbill 	register	struct	allocbox	*allocwalk;
651600Sbill 
652600Sbill 	symsout = 0;
653600Sbill 	DECLITERATE(allocwalk, sp, ub)
654600Sbill 	{
655634Shenry 		if (sp->s_tag >= IGNOREBOUND)
656600Sbill 			continue;
65713514Srrh 		if ((FETCHNAME(sp)[0] == 'L') && (sp->s_tag == LABELID) && !savelabels)
658600Sbill 			continue;
659600Sbill 		symsout++;
660634Shenry 
661634Shenry 		name = sp->s_name;		/* save pointer */
66213447Srrh 		/*
66313447Srrh 		 *	the length of the symbol table string
66413447Srrh 		 *	always includes the trailing null
66513447Srrh 		 */
66613514Srrh 		if (sp->s_name && (sp->s_index = STRLEN(sp))){
66713514Srrh 			sp->s_nmx = STROFF(sp);	/* clobber */
668634Shenry 		} else {
66913447Srrh 			sp->s_nmx = 0;
670634Shenry 		}
671634Shenry 		sp->s_type = (sp->s_ptype != 0) ? sp->s_ptype : (sp->s_type & (~XFORW));
672640Sbill 		if (readonlydata && (sp->s_type&~N_EXT) == N_DATA)
673640Sbill 			sp->s_type = N_TEXT | (sp->s_type & N_EXT);
6745828Srrh 		bwrite((char *)&sp->s_nm, sizeof (struct nlist), symfile);
675634Shenry 		sp->s_name = name;		/* restore pointer */
676600Sbill 	}
677600Sbill 	if (symsout != symsdesired)
678600Sbill 		yyerror("INTERNAL ERROR: Wrote %d symbols, wanted to write %d symbols\n",
679600Sbill 			symsout, symsdesired);
680600Sbill 	/*
68113514Srrh 	 *	Copy the string temporary file to the symbol file,
68213514Srrh 	 *	copying all the strings and symbols we ever saw,
68313514Srrh 	 *	including labels, stabs strings, ascii strings, etc.
68413514Srrh 	 *	This is slightly wasteful.
685600Sbill 	 */
68613514Srrh 	i = 0;
68713514Srrh 	while((nread = read(strfile->_file, rbuf, sizeof(rbuf))) > 0){
68813514Srrh 		if (i == 0){
68913514Srrh 			((int *)rbuf)[0] = strfilepos;
69013447Srrh 		}
69113514Srrh 		bwrite(rbuf, nread, symfile);
69213514Srrh 		i++;
693600Sbill 	}
694600Sbill }
695