xref: /csrg-svn/old/ld/ld.c (revision 1752)
1*1752Sbill static	char sccsid[] = "@(#)ld.c 4.2 11/07/80";
2615Sbill /*
3898Sbill  * ld - string table version for VAX
4615Sbill  */
5615Sbill 
6615Sbill #include <sys/types.h>
7615Sbill #include <signal.h>
8615Sbill #include <stdio.h>
9615Sbill #include <ctype.h>
10650Sbill #include <ar.h>
11650Sbill #include <a.out.h>
12615Sbill #include <ranlib.h>
13615Sbill #include <stat.h>
14615Sbill #include <pagsiz.h>
15615Sbill 
16615Sbill /*
17615Sbill  * Basic strategy:
18615Sbill  *
19615Sbill  * The loader takes a number of files and libraries as arguments.
20615Sbill  * A first pass examines each file in turn.  Normal files are
21615Sbill  * unconditionally loaded, and the (external) symbols they define and require
22615Sbill  * are noted in the symbol table.   Libraries are searched, and the
23615Sbill  * library members which define needed symbols are remembered
24615Sbill  * in a special data structure so they can be selected on the second
25615Sbill  * pass.  Symbols defined and required by library members are also
26615Sbill  * recorded.
27615Sbill  *
28615Sbill  * After the first pass, the loader knows the size of the basic text
29615Sbill  * data, and bss segments from the sum of the sizes of the modules which
30615Sbill  * were required.  It has computed, for each ``common'' symbol, the
31615Sbill  * maximum size of any reference to it, and these symbols are then assigned
32615Sbill  * storage locations after their sizes are appropriately rounded.
33615Sbill  * The loader now knows all sizes for the eventual output file, and
34615Sbill  * can determine the final locations of external symbols before it
35615Sbill  * begins a second pass.
36615Sbill  *
37615Sbill  * On the second pass each normal file and required library member
38615Sbill  * is processed again.  The symbol table for each such file is
39615Sbill  * reread and relevant parts of it are placed in the output.  The offsets
40615Sbill  * in the local symbol table for externally defined symbols are recorded
41615Sbill  * since relocation information refers to symbols in this way.
42615Sbill  * Armed with all necessary information, the text and data segments
43615Sbill  * are relocated and the result is placed in the output file, which
44615Sbill  * is pasted together, ``in place'', by writing to it in several
45615Sbill  * different places concurrently.
46615Sbill  */
47615Sbill 
48615Sbill /*
49615Sbill  * Internal data structures
50615Sbill  *
51615Sbill  * All internal data structures are segmented and dynamically extended.
52615Sbill  * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT)
53615Sbill  * referenced library members, and 100 (NSYMPR) private (local) symbols
54615Sbill  * per object module.  For large programs and/or modules, these structures
55615Sbill  * expand to be up to 40 (NSEG) times as large as this as necessary.
56615Sbill  */
57615Sbill #define	NSEG	40		/* Number of segments, each data structure */
58615Sbill #define	NSYM	1103		/* Number of symbols per segment */
59615Sbill #define	NROUT	250		/* Number of library references per segment */
60615Sbill #define	NSYMPR	100		/* Number of private symbols per segment */
61615Sbill 
62615Sbill /*
63615Sbill  * Structure describing each symbol table segment.
64615Sbill  * Each segment has its own hash table.  We record the first
65615Sbill  * address in and first address beyond both the symbol and hash
66615Sbill  * tables, for use in the routine symx and the lookup routine respectively.
67615Sbill  * The symfree routine also understands this structure well as it used
68615Sbill  * to back out symbols from modules we decide that we don't need in pass 1.
69615Sbill  *
70615Sbill  * Csymseg points to the current symbol table segment;
71615Sbill  * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated,
72615Sbill  * (unless csymseg->sy_used == NSYM in which case we will allocate another
73615Sbill  * symbol table segment first.)
74615Sbill  */
75615Sbill struct	symseg {
76615Sbill 	struct	nlist *sy_first;	/* base of this alloc'ed segment */
77615Sbill 	struct	nlist *sy_last;		/* end of this segment, for n_strx */
78615Sbill 	int	sy_used;		/* symbols used in this seg */
79615Sbill 	struct	nlist **sy_hfirst;	/* base of hash table, this seg */
80615Sbill 	struct	nlist **sy_hlast;	/* end of hash table, this seg */
81615Sbill } symseg[NSEG], *csymseg;
82615Sbill 
83615Sbill /*
84615Sbill  * The lookup routine uses quadratic rehash.  Since a quadratic rehash
85615Sbill  * only probes 1/2 of the buckets in the table, and since the hash
86615Sbill  * table is segmented the same way the symbol table is, we make the
87615Sbill  * hash table have twice as many buckets as there are symbol table slots
88615Sbill  * in the segment.  This guarantees that the quadratic rehash will never
89615Sbill  * fail to find an empty bucket if the segment is not full and the
90615Sbill  * symbol is not there.
91615Sbill  */
92615Sbill #define	HSIZE	(NSYM*2)
93615Sbill 
94615Sbill /*
95615Sbill  * Xsym converts symbol table indices (ala x) into symbol table pointers.
96615Sbill  * Symx (harder, but never used in loops) inverts pointers into the symbol
97615Sbill  * table into indices using the symseg[] structure.
98615Sbill  */
99615Sbill #define	xsym(x)	(symseg[(x)/NSYM].sy_first+((x)%NSYM))
100615Sbill /* symx() is a function, defined below */
101615Sbill 
102615Sbill struct	nlist cursym;		/* current symbol */
103615Sbill struct	nlist *lastsym;		/* last symbol entered */
104615Sbill struct	nlist *nextsym;		/* next available symbol table entry */
105615Sbill struct	nlist *addsym;		/* first sym defined during incr load */
106615Sbill int	nsym;			/* pass2: number of local symbols in a.out */
107615Sbill /* nsym + symx(nextsym) is the symbol table size during pass2 */
108615Sbill 
109615Sbill struct	nlist **lookup(), **slookup();
110650Sbill struct	nlist *p_etext, *p_edata, *p_end, *entrypt;
111615Sbill 
112615Sbill /*
113615Sbill  * Definitions of segmentation for library member table.
114615Sbill  * For each library we encounter on pass 1 we record pointers to all
115615Sbill  * members which we will load on pass 2.  These are recorded as offsets
116615Sbill  * into the archive in the library member table.  Libraries are
117615Sbill  * separated in the table by the special offset value -1.
118615Sbill  */
119615Sbill off_t	li_init[NROUT];
120615Sbill struct	libseg {
121615Sbill 	off_t	*li_first;
122615Sbill 	int	li_used;
123615Sbill 	int	li_used2;
124615Sbill } libseg[NSEG] = {
125615Sbill 	li_init, 0, 0,
126615Sbill }, *clibseg = libseg;
127615Sbill 
128615Sbill /*
129615Sbill  * In processing each module on pass 2 we must relocate references
130615Sbill  * relative to external symbols.  These references are recorded
131615Sbill  * in the relocation information as relative to local symbol numbers
132615Sbill  * assigned to the external symbols when the module was created.
133615Sbill  * Thus before relocating the module in pass 2 we create a table
134615Sbill  * which maps these internal numbers to symbol table entries.
135615Sbill  * A hash table is constructed, based on the local symbol table indices,
136615Sbill  * for quick lookup of these symbols.
137615Sbill  */
138615Sbill #define	LHSIZ	31
139615Sbill struct	local {
140615Sbill 	int	l_index;		/* index to symbol in file */
141615Sbill 	struct	nlist *l_symbol;	/* ptr to symbol table */
142615Sbill 	struct	local *l_link;		/* hash link */
143615Sbill } *lochash[LHSIZ], lhinit[NSYMPR];
144615Sbill struct	locseg {
145615Sbill 	struct	local *lo_first;
146615Sbill 	int	lo_used;
147615Sbill } locseg[NSEG] = {
148615Sbill 	lhinit, 0
149615Sbill }, *clocseg;
150615Sbill 
151615Sbill /*
152615Sbill  * Libraries are typically built with a table of contents,
153615Sbill  * which is the first member of a library with special file
154615Sbill  * name __.SYMDEF and contains a list of symbol names
155615Sbill  * and with each symbol the offset of the library member which defines
156615Sbill  * it.  The loader uses this table to quickly tell which library members
157615Sbill  * are (potentially) useful.  The alternative, examining the symbol
158615Sbill  * table of each library member, is painfully slow for large archives.
159615Sbill  *
160615Sbill  * See <ranlib.h> for the definition of the ranlib structure and an
161615Sbill  * explanation of the __.SYMDEF file format.
162615Sbill  */
163615Sbill int	tnum;		/* number of symbols in table of contents */
164615Sbill int	ssiz;		/* size of string table for table of contents */
165615Sbill struct	ranlib *tab;	/* the table of contents (dynamically allocated) */
166615Sbill char	*tabstr;	/* string table for table of contents */
167615Sbill 
168615Sbill /*
169615Sbill  * We open each input file or library only once, but in pass2 we
170615Sbill  * (historically) read from such a file at 2 different places at the
171615Sbill  * same time.  These structures are remnants from those days,
172650Sbill  * and now serve only to catch ``Premature EOF''.
173615Sbill  */
174615Sbill typedef struct {
175615Sbill 	short	*fakeptr;
176615Sbill 	int	bno;
177615Sbill 	int	nibuf;
178615Sbill 	int	nuser;
179615Sbill 	char	buff[BSIZE];
180615Sbill } PAGE;
181615Sbill 
182615Sbill PAGE	page[2];
183615Sbill 
184615Sbill struct {
185615Sbill 	short	*fakeptr;
186615Sbill 	int	bno;
187615Sbill 	int	nibuf;
188615Sbill 	int	nuser;
189615Sbill } fpage;
190615Sbill 
191615Sbill typedef struct {
192615Sbill 	char	*ptr;
193615Sbill 	int	bno;
194615Sbill 	int	nibuf;
195615Sbill 	long	size;
196615Sbill 	long	pos;
197615Sbill 	PAGE	*pno;
198615Sbill } STREAM;
199615Sbill 
200615Sbill STREAM	text;
201615Sbill STREAM	reloc;
202615Sbill 
203615Sbill /*
204615Sbill  * Header from the a.out and the archive it is from (if any).
205615Sbill  */
206615Sbill struct	exec filhdr;
207615Sbill struct	ar_hdr archdr;
208615Sbill #define	OARMAG 0177545
209615Sbill 
210615Sbill /*
211615Sbill  * Options.
212615Sbill  */
213615Sbill int	trace;
214615Sbill int	xflag;		/* discard local symbols */
215615Sbill int	Xflag;		/* discard locals starting with 'L' */
216615Sbill int	Sflag;		/* discard all except locals and globals*/
217615Sbill int	rflag;		/* preserve relocation bits, don't define common */
218615Sbill int	arflag;		/* original copy of rflag */
219615Sbill int	sflag;		/* discard all symbols */
220898Sbill int	Mflag;		/* print rudimentary load map */
221615Sbill int	nflag;		/* pure procedure */
222615Sbill int	dflag;		/* define common even with rflag */
223650Sbill int	zflag;		/* demand paged  */
224615Sbill long	hsize;		/* size of hole at beginning of data to be squashed */
225615Sbill int	Aflag;		/* doing incremental load */
226650Sbill int	Nflag;		/* want impure a.out */
227615Sbill int	funding;	/* reading fundamental file for incremental load */
228898Sbill int	yflag;		/* number of symbols to be traced */
229898Sbill char	**ytab;		/* the symbols */
230615Sbill 
231615Sbill /*
232615Sbill  * These are the cumulative sizes, set in pass 1, which
233615Sbill  * appear in the a.out header when the loader is finished.
234615Sbill  */
235615Sbill off_t	tsize, dsize, bsize, trsize, drsize, ssize;
236615Sbill 
237615Sbill /*
238615Sbill  * Symbol relocation: c?rel is a scale factor which is
239615Sbill  * added to an old relocation to convert it to new units;
240615Sbill  * i.e. it is the difference between segment origins.
241650Sbill  * (Thus if we are loading from a data segment which began at location
242650Sbill  * 4 in a .o file into an a.out where it will be loaded starting at
243650Sbill  * 1024, cdrel will be 1020.)
244615Sbill  */
245615Sbill long	ctrel, cdrel, cbrel;
246615Sbill 
247615Sbill /*
248650Sbill  * Textbase is the start address of all text, 0 unless given by -T.
249615Sbill  * Database is the base of all data, computed before and used during pass2.
250650Sbill  */
251650Sbill long	textbase, database;
252650Sbill 
253650Sbill /*
254615Sbill  * The base addresses for the loaded text, data and bss from the
255615Sbill  * current module during pass2 are given by torigin, dorigin and borigin.
256615Sbill  */
257615Sbill long	torigin, dorigin, borigin;
258615Sbill 
259615Sbill /*
260615Sbill  * Errlev is nonzero when errors have occured.
261615Sbill  * Delarg is an implicit argument to the routine delexit
262615Sbill  * which is called on error.  We do ``delarg = errlev'' before normal
263615Sbill  * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the
264615Sbill  * result file executable.
265615Sbill  */
266615Sbill int	errlev;
267615Sbill int	delarg	= 4;
268615Sbill 
269615Sbill /*
270615Sbill  * The biobuf structure and associated routines are used to write
271615Sbill  * into one file at several places concurrently.  Calling bopen
272615Sbill  * with a biobuf structure sets it up to write ``biofd'' starting
273615Sbill  * at the specified offset.  You can then use ``bwrite'' and/or ``bputc''
274615Sbill  * to stuff characters in the stream, much like ``fwrite'' and ``fputc''.
275615Sbill  * Calling bflush drains all the buffers and MUST be done before exit.
276615Sbill  */
277615Sbill struct	biobuf {
278615Sbill 	short	b_nleft;		/* Number free spaces left in b_buf */
279615Sbill /* Initialize to be less than BUFSIZ initially, to boundary align in file */
280615Sbill 	char	*b_ptr;			/* Next place to stuff characters */
281615Sbill 	char	b_buf[BUFSIZ];		/* The buffer itself */
282615Sbill 	off_t	b_off;			/* Current file offset */
283615Sbill 	struct	biobuf *b_link;		/* Link in chain for bflush() */
284615Sbill } *biobufs;
285615Sbill #define	bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \
286615Sbill 		       : bflushc(b, c))
287615Sbill int	biofd;
288615Sbill off_t	boffset;
289615Sbill struct	biobuf *tout, *dout, *trout, *drout, *sout, *strout;
290615Sbill 
291615Sbill /*
292615Sbill  * Offset is the current offset in the string file.
293615Sbill  * Its initial value reflects the fact that we will
294615Sbill  * eventually stuff the size of the string table at the
295615Sbill  * beginning of the string table (i.e. offset itself!).
296615Sbill  */
297615Sbill off_t	offset = sizeof (off_t);
298615Sbill 
299615Sbill int	ofilfnd;		/* -o given; otherwise move l.out to a.out */
300615Sbill char	*ofilename = "l.out";
301615Sbill int	infil;			/* current input file descriptor */
302615Sbill char	*filname;		/* and its name */
303615Sbill 
304615Sbill /*
305615Sbill  * Base of the string table of the current module (pass1 and pass2).
306615Sbill  */
307615Sbill char	*curstr;
308615Sbill 
309615Sbill char 	get();
310615Sbill int	delexit();
311615Sbill char	*savestr();
312615Sbill 
313615Sbill main(argc, argv)
314615Sbill char **argv;
315615Sbill {
316615Sbill 	register int c, i;
317615Sbill 	int num;
318615Sbill 	register char *ap, **p;
319615Sbill 	char save;
320615Sbill 
321650Sbill 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
322615Sbill 		signal(SIGINT, delexit);
323650Sbill 		signal(SIGTERM, delexit);
324650Sbill 	}
325615Sbill 	if (argc == 1)
326615Sbill 		exit(4);
327615Sbill 	p = argv+1;
328615Sbill 
329650Sbill 	/*
330650Sbill 	 * Scan files once to find where symbols are defined.
331650Sbill 	 */
332615Sbill 	for (c=1; c<argc; c++) {
333615Sbill 		if (trace)
334615Sbill 			printf("%s:\n", *p);
335615Sbill 		filname = 0;
336615Sbill 		ap = *p++;
337615Sbill 		if (*ap != '-') {
338615Sbill 			load1arg(ap);
339615Sbill 			continue;
340615Sbill 		}
341615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
342615Sbill 
343615Sbill 		case 'o':
344615Sbill 			if (++c >= argc)
345615Sbill 				error(1, "-o where?");
346615Sbill 			ofilename = *p++;
347615Sbill 			ofilfnd++;
348615Sbill 			continue;
349615Sbill 		case 'u':
350615Sbill 		case 'e':
351615Sbill 			if (++c >= argc)
352615Sbill 				error(1, "-u or -c: arg missing");
353615Sbill 			enter(slookup(*p++));
354615Sbill 			if (ap[i]=='e')
355615Sbill 				entrypt = lastsym;
356615Sbill 			continue;
357615Sbill 		case 'H':
358615Sbill 			if (++c >= argc)
359615Sbill 				error(1, "-H: arg missing");
360615Sbill 			if (tsize!=0)
361615Sbill 				error(1, "-H: too late, some text already loaded");
362615Sbill 			hsize = atoi(*p++);
363615Sbill 			continue;
364615Sbill 		case 'A':
365615Sbill 			if (++c >= argc)
366615Sbill 				error(1, "-A: arg missing");
367615Sbill 			if (Aflag)
368615Sbill 				error(1, "-A: only one base file allowed");
369615Sbill 			Aflag = 1;
370615Sbill 			nflag = 0;
371615Sbill 			funding = 1;
372615Sbill 			load1arg(*p++);
373615Sbill 			trsize = drsize = tsize = dsize = bsize = 0;
374615Sbill 			ctrel = cdrel = cbrel = 0;
375615Sbill 			funding = 0;
376615Sbill 			addsym = nextsym;
377615Sbill 			continue;
378615Sbill 		case 'D':
379615Sbill 			if (++c >= argc)
380615Sbill 				error(1, "-D: arg missing");
381615Sbill 			num = htoi(*p++);
382615Sbill 			if (dsize > num)
383615Sbill 				error(1, "-D: too small");
384615Sbill 			dsize = num;
385615Sbill 			continue;
386615Sbill 		case 'T':
387615Sbill 			if (++c >= argc)
388615Sbill 				error(1, "-T: arg missing");
389615Sbill 			if (tsize!=0)
390615Sbill 				error(1, "-T: too late, some text already loaded");
391615Sbill 			textbase = htoi(*p++);
392615Sbill 			continue;
393615Sbill 		case 'l':
394615Sbill 			save = ap[--i];
395615Sbill 			ap[i]='-';
396615Sbill 			load1arg(&ap[i]);
397615Sbill 			ap[i]=save;
398615Sbill 			goto next;
399898Sbill 		case 'M':
400898Sbill 			Mflag++;
401898Sbill 			continue;
402615Sbill 		case 'x':
403615Sbill 			xflag++;
404615Sbill 			continue;
405615Sbill 		case 'X':
406615Sbill 			Xflag++;
407615Sbill 			continue;
408615Sbill 		case 'S':
409615Sbill 			Sflag++;
410615Sbill 			continue;
411615Sbill 		case 'r':
412615Sbill 			rflag++;
413615Sbill 			arflag++;
414615Sbill 			continue;
415615Sbill 		case 's':
416615Sbill 			sflag++;
417615Sbill 			xflag++;
418615Sbill 			continue;
419615Sbill 		case 'n':
420615Sbill 			nflag++;
421650Sbill 			Nflag = zflag = 0;
422615Sbill 			continue;
423615Sbill 		case 'N':
424650Sbill 			Nflag++;
425650Sbill 			nflag = zflag = 0;
426615Sbill 			continue;
427615Sbill 		case 'd':
428615Sbill 			dflag++;
429615Sbill 			continue;
430615Sbill 		case 'i':
431615Sbill 			printf("ld: -i ignored\n");
432615Sbill 			continue;
433615Sbill 		case 't':
434615Sbill 			trace++;
435615Sbill 			continue;
436898Sbill 		case 'y':
437898Sbill 			if (ap[i+1] == 0)
438898Sbill 				error(1, "-y: symbol name missing");
439898Sbill 			if (yflag == 0) {
440898Sbill 				ytab = (char **)calloc(argc, sizeof (char **));
441898Sbill 				if (ytab == 0)
442898Sbill 					error(1, "ran out of memory (-y)");
443898Sbill 			}
444898Sbill 			ytab[yflag++] = &ap[i+1];
445898Sbill 			goto next;
446615Sbill 		case 'z':
447615Sbill 			zflag++;
448650Sbill 			Nflag = nflag = 0;
449615Sbill 			continue;
450615Sbill 		default:
451615Sbill 			filname = savestr("-x");	/* kludge */
452615Sbill 			filname[1] = ap[i];		/* kludge */
453615Sbill 			archdr.ar_name[0] = 0;		/* kludge */
454615Sbill 			error(1, "bad flag");
455615Sbill 		}
456615Sbill next:
457615Sbill 		;
458615Sbill 	}
459650Sbill 	if (rflag == 0 && Nflag == 0 && nflag == 0)
460650Sbill 		zflag++;
461615Sbill 	endload(argc, argv);
462615Sbill 	exit(0);
463615Sbill }
464615Sbill 
465615Sbill /*
466615Sbill  * Convert a ascii string which is a hex number.
467615Sbill  * Used by -T and -D options.
468615Sbill  */
469615Sbill htoi(p)
470615Sbill 	register char *p;
471615Sbill {
472615Sbill 	register int c, n;
473615Sbill 
474615Sbill 	n = 0;
475615Sbill 	while (c = *p++) {
476615Sbill 		n <<= 4;
477615Sbill 		if (isdigit(c))
478615Sbill 			n += c - '0';
479615Sbill 		else if (c >= 'a' && c <= 'f')
480615Sbill 			n += 10 + (c - 'a');
481615Sbill 		else if (c >= 'A' && c <= 'F')
482615Sbill 			n += 10 + (c - 'A');
483615Sbill 		else
484615Sbill 			error(1, "badly formed hex number");
485615Sbill 	}
486615Sbill 	return (n);
487615Sbill }
488615Sbill 
489615Sbill delexit()
490615Sbill {
491615Sbill 
492615Sbill 	bflush();
493615Sbill 	unlink("l.out");
494615Sbill 	if (delarg==0 && Aflag==0)
495615Sbill 		chmod(ofilename, 0777 &~ umask(0));
496615Sbill 	exit (delarg);
497615Sbill }
498615Sbill 
499615Sbill endload(argc, argv)
500615Sbill 	int argc;
501615Sbill 	char **argv;
502615Sbill {
503615Sbill 	register int c, i;
504615Sbill 	long dnum;
505615Sbill 	register char *ap, **p;
506615Sbill 
507615Sbill 	clibseg = libseg;
508615Sbill 	filname = 0;
509615Sbill 	middle();
510615Sbill 	setupout();
511615Sbill 	p = argv+1;
512615Sbill 	for (c=1; c<argc; c++) {
513615Sbill 		ap = *p++;
514615Sbill 		if (trace)
515615Sbill 			printf("%s:\n", ap);
516615Sbill 		if (*ap != '-') {
517615Sbill 			load2arg(ap);
518615Sbill 			continue;
519615Sbill 		}
520615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
521615Sbill 
522615Sbill 		case 'D':
523615Sbill 			dnum = htoi(*p);
524615Sbill 			if (dorigin < dnum)
525615Sbill 				while (dorigin < dnum)
526615Sbill 					bputc(0, dout), dorigin++;
527615Sbill 			/* fall into ... */
528615Sbill 		case 'T':
529615Sbill 		case 'u':
530615Sbill 		case 'e':
531615Sbill 		case 'o':
532615Sbill 		case 'H':
533615Sbill 			++c;
534615Sbill 			++p;
535615Sbill 			/* fall into ... */
536615Sbill 		default:
537615Sbill 			continue;
538615Sbill 		case 'A':
539615Sbill 			funding = 1;
540615Sbill 			load2arg(*p++);
541615Sbill 			funding = 0;
542615Sbill 			c++;
543615Sbill 			continue;
544898Sbill 		case 'y':
545898Sbill 			goto next;
546615Sbill 		case 'l':
547615Sbill 			ap[--i]='-';
548615Sbill 			load2arg(&ap[i]);
549615Sbill 			goto next;
550615Sbill 		}
551615Sbill next:
552615Sbill 		;
553615Sbill 	}
554615Sbill 	finishout();
555615Sbill }
556615Sbill 
557615Sbill /*
558615Sbill  * Scan file to find defined symbols.
559615Sbill  */
560615Sbill load1arg(cp)
561615Sbill 	register char *cp;
562615Sbill {
563615Sbill 	register struct ranlib *tp;
564615Sbill 	off_t nloc;
565898Sbill 	int kind;
566615Sbill 
567898Sbill 	kind = getfile(cp);
568898Sbill 	if (Mflag)
569898Sbill 		printf("%s\n", filname);
570898Sbill 	switch (kind) {
571615Sbill 
572615Sbill 	/*
573615Sbill 	 * Plain file.
574615Sbill 	 */
575615Sbill 	case 0:
576615Sbill 		load1(0, 0L);
577615Sbill 		break;
578615Sbill 
579615Sbill 	/*
580615Sbill 	 * Archive without table of contents.
581615Sbill 	 * (Slowly) process each member.
582615Sbill 	 */
583615Sbill 	case 1:
584898Sbill 		error(-1,
585898Sbill "warning: archive has no table of contents; add one using ranlib(1)");
586615Sbill 		nloc = SARMAG;
587615Sbill 		while (step(nloc))
588615Sbill 			nloc += sizeof(archdr) +
589615Sbill 			    round(atol(archdr.ar_size), sizeof (short));
590615Sbill 		break;
591615Sbill 
592615Sbill 	/*
593615Sbill 	 * Archive with table of contents.
594615Sbill 	 * Read the table of contents and its associated string table.
595615Sbill 	 * Pass through the library resolving symbols until nothing changes
596615Sbill 	 * for an entire pass (i.e. you can get away with backward references
597615Sbill 	 * when there is a table of contents!)
598615Sbill 	 */
599615Sbill 	case 2:
600615Sbill 		nloc = SARMAG + sizeof (archdr);
601615Sbill 		dseek(&text, nloc, sizeof (tnum));
602615Sbill 		mget((char *)&tnum, sizeof (tnum), &text);
603615Sbill 		nloc += sizeof (tnum);
604615Sbill 		tab = (struct ranlib *)malloc(tnum);
605615Sbill 		if (tab == 0)
606615Sbill 			error(1, "ran out of memory (toc)");
607615Sbill 		dseek(&text, nloc, tnum);
608615Sbill 		mget((char *)tab, tnum, &text);
609615Sbill 		nloc += tnum;
610615Sbill 		tnum /= sizeof (struct ranlib);
611615Sbill 		dseek(&text, nloc, sizeof (ssiz));
612615Sbill 		mget((char *)&ssiz, sizeof (ssiz), &text);
613615Sbill 		nloc += sizeof (ssiz);
614615Sbill 		tabstr = (char *)malloc(ssiz);
615615Sbill 		if (tabstr == 0)
616615Sbill 			error(1, "ran out of memory (tocstr)");
617615Sbill 		dseek(&text, nloc, ssiz);
618615Sbill 		mget((char *)tabstr, ssiz, &text);
619615Sbill 		for (tp = &tab[tnum]; --tp >= tab;) {
620615Sbill 			if (tp->ran_un.ran_strx < 0 ||
621615Sbill 			    tp->ran_un.ran_strx >= ssiz)
622615Sbill 				error(1, "mangled archive table of contents");
623615Sbill 			tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx;
624615Sbill 		}
625615Sbill 		while (ldrand())
626615Sbill 			continue;
627615Sbill 		cfree((char *)tab);
628615Sbill 		cfree(tabstr);
629615Sbill 		nextlibp(-1);
630615Sbill 		break;
631615Sbill 
632615Sbill 	/*
633615Sbill 	 * Table of contents is out of date, so search
634615Sbill 	 * as a normal library (but skip the __.SYMDEF file).
635615Sbill 	 */
636615Sbill 	case 3:
637898Sbill 		error(-1,
638898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)");
639615Sbill 		nloc = SARMAG;
640615Sbill 		do
641615Sbill 			nloc += sizeof(archdr) +
642615Sbill 			    round(atol(archdr.ar_size), sizeof(short));
643615Sbill 		while (step(nloc));
644615Sbill 		break;
645615Sbill 	}
646615Sbill 	close(infil);
647615Sbill }
648615Sbill 
649615Sbill /*
650615Sbill  * Advance to the next archive member, which
651615Sbill  * is at offset nloc in the archive.  If the member
652615Sbill  * is useful, record its location in the liblist structure
653615Sbill  * for use in pass2.  Mark the end of the archive in libilst with a -1.
654615Sbill  */
655615Sbill step(nloc)
656615Sbill 	off_t nloc;
657615Sbill {
658615Sbill 
659615Sbill 	dseek(&text, nloc, (long) sizeof archdr);
660615Sbill 	if (text.size <= 0) {
661615Sbill 		nextlibp(-1);
662615Sbill 		return (0);
663615Sbill 	}
664615Sbill 	getarhdr();
665615Sbill 	if (load1(1, nloc + (sizeof archdr)))
666615Sbill 		nextlibp(nloc);
667615Sbill 	return (1);
668615Sbill }
669615Sbill 
670615Sbill /*
671615Sbill  * Record the location of a useful archive member.
672615Sbill  * Recording -1 marks the end of files from an archive.
673615Sbill  * The liblist data structure is dynamically extended here.
674615Sbill  */
675615Sbill nextlibp(val)
676615Sbill 	off_t val;
677615Sbill {
678615Sbill 
679615Sbill 	if (clibseg->li_used == NROUT) {
680615Sbill 		if (++clibseg == &libseg[NSEG])
681615Sbill 			error(1, "too many files loaded from libraries");
682615Sbill 		clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t));
683615Sbill 		if (clibseg->li_first == 0)
684615Sbill 			error(1, "ran out of memory (nextlibp)");
685615Sbill 	}
686615Sbill 	clibseg->li_first[clibseg->li_used++] = val;
687898Sbill 	if (val != -1 && Mflag)
688898Sbill 		printf("\t%s\n", archdr.ar_name);
689615Sbill }
690615Sbill 
691615Sbill /*
692615Sbill  * One pass over an archive with a table of contents.
693615Sbill  * Remember the number of symbols currently defined,
694615Sbill  * then call step on members which look promising (i.e.
695615Sbill  * that define a symbol which is currently externally undefined).
696615Sbill  * Indicate to our caller whether this process netted any more symbols.
697615Sbill  */
698615Sbill ldrand()
699615Sbill {
700615Sbill 	register struct nlist *sp, **hp;
701615Sbill 	register struct ranlib *tp, *tplast;
702615Sbill 	off_t loc;
703615Sbill 	int nsymt = symx(nextsym);
704615Sbill 
705615Sbill 	tplast = &tab[tnum-1];
706615Sbill 	for (tp = tab; tp <= tplast; tp++) {
707615Sbill 		if ((hp = slookup(tp->ran_un.ran_name)) == 0)
708615Sbill 			continue;
709615Sbill 		sp = *hp;
710615Sbill 		if (sp->n_type != N_EXT+N_UNDF)
711615Sbill 			continue;
712615Sbill 		step(tp->ran_off);
713615Sbill 		loc = tp->ran_off;
714615Sbill 		while (tp < tplast && (tp+1)->ran_off == loc)
715615Sbill 			tp++;
716615Sbill 	}
717615Sbill 	return (symx(nextsym) != nsymt);
718615Sbill }
719615Sbill 
720615Sbill /*
721615Sbill  * Examine a single file or archive member on pass 1.
722615Sbill  */
723615Sbill load1(libflg, loc)
724615Sbill 	off_t loc;
725615Sbill {
726615Sbill 	register struct nlist *sp;
727615Sbill 	struct nlist *savnext;
728615Sbill 	int ndef, nlocal, type, size, nsymt;
729615Sbill 	register int i;
730615Sbill 	off_t maxoff;
731615Sbill 	struct stat stb;
732615Sbill 
733615Sbill 	readhdr(loc);
734615Sbill 	if (filhdr.a_syms == 0) {
735615Sbill 		if (filhdr.a_text+filhdr.a_data == 0)
736615Sbill 			return (0);
737615Sbill 		error(1, "no namelist");
738615Sbill 	}
739615Sbill 	if (libflg)
740615Sbill 		maxoff = atol(archdr.ar_size);
741615Sbill 	else {
742615Sbill 		fstat(infil, &stb);
743615Sbill 		maxoff = stb.st_size;
744615Sbill 	}
745615Sbill 	if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff)
746615Sbill 		error(1, "too small (old format .o?)");
747615Sbill 	ctrel = tsize; cdrel += dsize; cbrel += bsize;
748615Sbill 	ndef = 0;
749615Sbill 	nlocal = sizeof(cursym);
750615Sbill 	savnext = nextsym;
751615Sbill 	loc += N_SYMOFF(filhdr);
752615Sbill 	dseek(&text, loc, filhdr.a_syms);
753615Sbill 	dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t));
754615Sbill 	mget(&size, sizeof (size), &reloc);
755615Sbill 	dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t));
756615Sbill 	curstr = (char *)malloc(size);
757615Sbill 	if (curstr == NULL)
758615Sbill 		error(1, "no space for string table");
759615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc);
760615Sbill 	while (text.size > 0) {
761615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
762615Sbill 		if (cursym.n_un.n_strx) {
763615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
764615Sbill 			    cursym.n_un.n_strx>=size)
765615Sbill 				error(1, "bad string table index (pass 1)");
766615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
767615Sbill 		}
768615Sbill 		type = cursym.n_type;
769615Sbill 		if ((type&N_EXT)==0) {
770615Sbill 			if (Xflag==0 || cursym.n_un.n_name[0]!='L' ||
771615Sbill 			    type & N_STAB)
772615Sbill 				nlocal += sizeof cursym;
773615Sbill 			continue;
774615Sbill 		}
775615Sbill 		symreloc();
776615Sbill 		if (enter(lookup()))
777615Sbill 			continue;
778615Sbill 		if ((sp = lastsym)->n_type != N_EXT+N_UNDF)
779615Sbill 			continue;
780615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
781615Sbill 			if (cursym.n_value > sp->n_value)
782615Sbill 				sp->n_value = cursym.n_value;
783615Sbill 			continue;
784615Sbill 		}
785615Sbill 		if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT)
786615Sbill 			continue;
787615Sbill 		ndef++;
788615Sbill 		sp->n_type = cursym.n_type;
789615Sbill 		sp->n_value = cursym.n_value;
790615Sbill 	}
791615Sbill 	if (libflg==0 || ndef) {
792615Sbill 		tsize += filhdr.a_text;
793615Sbill 		dsize += round(filhdr.a_data, sizeof (long));
794615Sbill 		bsize += round(filhdr.a_bss, sizeof (long));
795615Sbill 		ssize += nlocal;
796615Sbill 		trsize += filhdr.a_trsize;
797615Sbill 		drsize += filhdr.a_drsize;
798615Sbill 		if (funding)
799615Sbill 			textbase = (*slookup("_end"))->n_value;
800615Sbill 		nsymt = symx(nextsym);
801615Sbill 		for (i = symx(savnext); i < nsymt; i++) {
802615Sbill 			sp = xsym(i);
803615Sbill 			sp->n_un.n_name = savestr(sp->n_un.n_name);
804615Sbill 		}
805615Sbill 		free(curstr);
806615Sbill 		return (1);
807615Sbill 	}
808615Sbill 	/*
809615Sbill 	 * No symbols defined by this library member.
810615Sbill 	 * Rip out the hash table entries and reset the symbol table.
811615Sbill 	 */
812615Sbill 	symfree(savnext);
813615Sbill 	free(curstr);
814615Sbill 	return(0);
815615Sbill }
816615Sbill 
817615Sbill middle()
818615Sbill {
819615Sbill 	register struct nlist *sp;
820615Sbill 	long csize, t, corigin, ocsize;
821615Sbill 	int nund, rnd;
822615Sbill 	char s;
823615Sbill 	register int i;
824615Sbill 	int nsymt;
825615Sbill 
826615Sbill 	torigin = 0;
827615Sbill 	dorigin = 0;
828615Sbill 	borigin = 0;
829615Sbill 
830615Sbill 	p_etext = *slookup("_etext");
831615Sbill 	p_edata = *slookup("_edata");
832615Sbill 	p_end = *slookup("_end");
833615Sbill 	/*
834615Sbill 	 * If there are any undefined symbols, save the relocation bits.
835615Sbill 	 */
836615Sbill 	nsymt = symx(nextsym);
837615Sbill 	if (rflag==0) {
838615Sbill 		for (i = 0; i < nsymt; i++) {
839615Sbill 			sp = xsym(i);
840615Sbill 			if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 &&
841650Sbill 			    sp!=p_end && sp!=p_edata && sp!=p_etext) {
842615Sbill 				rflag++;
843615Sbill 				dflag = 0;
844615Sbill 				break;
845615Sbill 			}
846615Sbill 		}
847615Sbill 	}
848615Sbill 	if (rflag)
849615Sbill 		sflag = zflag = 0;
850615Sbill 	/*
851615Sbill 	 * Assign common locations.
852615Sbill 	 */
853615Sbill 	csize = 0;
854615Sbill 	if (!Aflag)
855615Sbill 		addsym = symseg[0].sy_first;
856615Sbill 	database = round(tsize+textbase,
857615Sbill 	    (nflag||zflag? PAGSIZ : sizeof (long)));
858615Sbill 	database += hsize;
859615Sbill 	if (dflag || rflag==0) {
860615Sbill 		ldrsym(p_etext, tsize, N_EXT+N_TEXT);
861615Sbill 		ldrsym(p_edata, dsize, N_EXT+N_DATA);
862615Sbill 		ldrsym(p_end, bsize, N_EXT+N_BSS);
863615Sbill 		for (i = symx(addsym); i < nsymt; i++) {
864615Sbill 			sp = xsym(i);
865615Sbill 			if ((s=sp->n_type)==N_EXT+N_UNDF &&
866615Sbill 			    (t = sp->n_value)!=0) {
867615Sbill 				if (t >= sizeof (double))
868615Sbill 					rnd = sizeof (double);
869615Sbill 				else if (t >= sizeof (long))
870615Sbill 					rnd = sizeof (long);
871615Sbill 				else
872615Sbill 					rnd = sizeof (short);
873615Sbill 				csize = round(csize, rnd);
874615Sbill 				sp->n_value = csize;
875615Sbill 				sp->n_type = N_EXT+N_COMM;
876615Sbill 				ocsize = csize;
877615Sbill 				csize += t;
878615Sbill 			}
879615Sbill 			if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) {
880615Sbill 				sp->n_value = ocsize;
881615Sbill 				sp->n_type = (s&N_STAB) | (N_EXT+N_COMM);
882615Sbill 			}
883615Sbill 		}
884615Sbill 	}
885615Sbill 	/*
886615Sbill 	 * Now set symbols to their final value
887615Sbill 	 */
888615Sbill 	csize = round(csize, sizeof (long));
889615Sbill 	torigin = textbase;
890615Sbill 	dorigin = database;
891615Sbill 	corigin = dorigin + dsize;
892615Sbill 	borigin = corigin + csize;
893615Sbill 	nund = 0;
894615Sbill 	nsymt = symx(nextsym);
895615Sbill 	for (i = symx(addsym); i<nsymt; i++) {
896615Sbill 		sp = xsym(i);
897615Sbill 		switch (sp->n_type & (N_TYPE+N_EXT)) {
898615Sbill 
899615Sbill 		case N_EXT+N_UNDF:
900615Sbill 			errlev |= 01;
901615Sbill 			if ((arflag==0 || dflag) && sp->n_value==0) {
902650Sbill 				if (sp==p_end || sp==p_etext || sp==p_edata)
903650Sbill 					continue;
904615Sbill 				if (nund==0)
905615Sbill 					printf("Undefined:\n");
906615Sbill 				nund++;
907615Sbill 				printf("%s\n", sp->n_un.n_name);
908615Sbill 			}
909615Sbill 			continue;
910615Sbill 		case N_EXT+N_ABS:
911615Sbill 		default:
912615Sbill 			continue;
913615Sbill 		case N_EXT+N_TEXT:
914615Sbill 			sp->n_value += torigin;
915615Sbill 			continue;
916615Sbill 		case N_EXT+N_DATA:
917615Sbill 			sp->n_value += dorigin;
918615Sbill 			continue;
919615Sbill 		case N_EXT+N_BSS:
920615Sbill 			sp->n_value += borigin;
921615Sbill 			continue;
922615Sbill 		case N_EXT+N_COMM:
923615Sbill 			sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS);
924615Sbill 			sp->n_value += corigin;
925615Sbill 			continue;
926615Sbill 		}
927615Sbill 	}
928615Sbill 	if (sflag || xflag)
929615Sbill 		ssize = 0;
930615Sbill 	bsize += csize;
931615Sbill 	nsym = ssize / (sizeof cursym);
932615Sbill 	if (Aflag) {
933615Sbill 		fixspec(p_etext,torigin);
934615Sbill 		fixspec(p_edata,dorigin);
935615Sbill 		fixspec(p_end,borigin);
936615Sbill 	}
937615Sbill }
938615Sbill 
939615Sbill fixspec(sym,offset)
940615Sbill 	struct nlist *sym;
941615Sbill 	long offset;
942615Sbill {
943615Sbill 
944615Sbill 	if(symx(sym) < symx(addsym) && sym!=0)
945615Sbill 		sym->n_value += offset;
946615Sbill }
947615Sbill 
948615Sbill ldrsym(sp, val, type)
949615Sbill 	register struct nlist *sp;
950615Sbill 	long val;
951615Sbill {
952615Sbill 
953615Sbill 	if (sp == 0)
954615Sbill 		return;
955615Sbill 	if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) {
956615Sbill 		printf("%s: ", sp->n_un.n_name);
957615Sbill 		error(0, "user attempt to redfine loader-defined symbol");
958615Sbill 		return;
959615Sbill 	}
960615Sbill 	sp->n_type = type;
961615Sbill 	sp->n_value = val;
962615Sbill }
963615Sbill 
964615Sbill off_t	wroff;
965615Sbill struct	biobuf toutb;
966615Sbill 
967615Sbill setupout()
968615Sbill {
969615Sbill 	int bss;
970898Sbill 	extern char *sys_errlist[];
971898Sbill 	extern int errno;
972615Sbill 
973615Sbill 	biofd = creat(ofilename, 0666);
974898Sbill 	if (biofd < 0) {
975898Sbill 		filname = ofilename;		/* kludge */
976898Sbill 		archdr.ar_name[0] = 0;		/* kludge */
977898Sbill 		error(1, sys_errlist[errno]);	/* kludge */
978898Sbill 	}
979615Sbill 	tout = &toutb;
980615Sbill 	bopen(tout, 0);
981615Sbill 	filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC);
982615Sbill 	filhdr.a_text = nflag ? tsize :
983615Sbill 	    round(tsize, zflag ? PAGSIZ : sizeof (long));
984615Sbill 	filhdr.a_data = zflag ? round(dsize, PAGSIZ) : dsize;
985615Sbill 	bss = bsize - (filhdr.a_data - dsize);
986615Sbill 	if (bss < 0)
987615Sbill 		bss = 0;
988615Sbill 	filhdr.a_bss = bss;
989615Sbill 	filhdr.a_trsize = trsize;
990615Sbill 	filhdr.a_drsize = drsize;
991615Sbill 	filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym));
992615Sbill 	if (entrypt) {
993615Sbill 		if (entrypt->n_type!=N_EXT+N_TEXT)
994615Sbill 			error(0, "entry point not in text");
995615Sbill 		else
996615Sbill 			filhdr.a_entry = entrypt->n_value;
997615Sbill 	} else
998615Sbill 		filhdr.a_entry = 0;
999615Sbill 	filhdr.a_trsize = (rflag ? trsize:0);
1000615Sbill 	filhdr.a_drsize = (rflag ? drsize:0);
1001615Sbill 	bwrite((char *)&filhdr, sizeof (filhdr), tout);
1002615Sbill 	if (zflag) {
1003615Sbill 		bflush1(tout);
1004615Sbill 		biobufs = 0;
1005615Sbill 		bopen(tout, PAGSIZ);
1006615Sbill 	}
1007615Sbill 	wroff = N_TXTOFF(filhdr) + filhdr.a_text;
1008615Sbill 	outb(&dout, filhdr.a_data);
1009615Sbill 	if (rflag) {
1010615Sbill 		outb(&trout, filhdr.a_trsize);
1011615Sbill 		outb(&drout, filhdr.a_drsize);
1012615Sbill 	}
1013615Sbill 	if (sflag==0 || xflag==0) {
1014615Sbill 		outb(&sout, filhdr.a_syms);
1015615Sbill 		wroff += sizeof (offset);
1016615Sbill 		outb(&strout, 0);
1017615Sbill 	}
1018615Sbill }
1019615Sbill 
1020615Sbill outb(bp, inc)
1021615Sbill 	register struct biobuf **bp;
1022615Sbill {
1023615Sbill 
1024615Sbill 	*bp = (struct biobuf *)malloc(sizeof (struct biobuf));
1025615Sbill 	if (*bp == 0)
1026615Sbill 		error(1, "ran out of memory (outb)");
1027615Sbill 	bopen(*bp, wroff);
1028615Sbill 	wroff += inc;
1029615Sbill }
1030615Sbill 
1031615Sbill load2arg(acp)
1032615Sbill char *acp;
1033615Sbill {
1034615Sbill 	register char *cp;
1035615Sbill 	off_t loc;
1036615Sbill 
1037615Sbill 	cp = acp;
1038615Sbill 	if (getfile(cp) == 0) {
1039615Sbill 		while (*cp)
1040615Sbill 			cp++;
1041615Sbill 		while (cp >= acp && *--cp != '/');
1042615Sbill 		mkfsym(++cp);
1043615Sbill 		load2(0L);
1044615Sbill 	} else {	/* scan archive members referenced */
1045615Sbill 		for (;;) {
1046615Sbill 			if (clibseg->li_used2 == clibseg->li_used) {
1047615Sbill 				if (clibseg->li_used < NROUT)
1048615Sbill 					error(1, "libseg botch");
1049615Sbill 				clibseg++;
1050615Sbill 			}
1051615Sbill 			loc = clibseg->li_first[clibseg->li_used2++];
1052615Sbill 			if (loc == -1)
1053615Sbill 				break;
1054615Sbill 			dseek(&text, loc, (long)sizeof(archdr));
1055615Sbill 			getarhdr();
1056615Sbill 			mkfsym(archdr.ar_name);
1057615Sbill 			load2(loc + (long)sizeof(archdr));
1058615Sbill 		}
1059615Sbill 	}
1060615Sbill 	close(infil);
1061615Sbill }
1062615Sbill 
1063615Sbill load2(loc)
1064615Sbill long loc;
1065615Sbill {
1066615Sbill 	int size;
1067615Sbill 	register struct nlist *sp;
1068615Sbill 	register struct local *lp;
1069615Sbill 	register int symno, i;
1070615Sbill 	int type;
1071615Sbill 
1072615Sbill 	readhdr(loc);
1073650Sbill 	if (!funding) {
1074615Sbill 		ctrel = torigin;
1075615Sbill 		cdrel += dorigin;
1076615Sbill 		cbrel += borigin;
1077615Sbill 	}
1078615Sbill 	/*
1079615Sbill 	 * Reread the symbol table, recording the numbering
1080615Sbill 	 * of symbols for fixing external references.
1081615Sbill 	 */
1082615Sbill 	for (i = 0; i < LHSIZ; i++)
1083615Sbill 		lochash[i] = 0;
1084615Sbill 	clocseg = locseg;
1085615Sbill 	clocseg->lo_used = 0;
1086615Sbill 	symno = -1;
1087615Sbill 	loc += N_TXTOFF(filhdr);
1088615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1089615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t));
1090615Sbill 	mget(&size, sizeof(size), &text);
1091615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1092615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t),
1093615Sbill 		size - sizeof(off_t));
1094615Sbill 	curstr = (char *)malloc(size);
1095615Sbill 	if (curstr == NULL)
1096615Sbill 		error(1, "out of space reading string table (pass 2)");
1097615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &text);
1098615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1099615Sbill 		filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms);
1100615Sbill 	while (text.size > 0) {
1101615Sbill 		symno++;
1102615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
1103615Sbill 		if (cursym.n_un.n_strx) {
1104615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
1105615Sbill 			    cursym.n_un.n_strx>=size)
1106615Sbill 				error(1, "bad string table index (pass 2)");
1107615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
1108615Sbill 		}
1109615Sbill /* inline expansion of symreloc() */
1110615Sbill 		switch (cursym.n_type & 017) {
1111615Sbill 
1112615Sbill 		case N_TEXT:
1113615Sbill 		case N_EXT+N_TEXT:
1114615Sbill 			cursym.n_value += ctrel;
1115615Sbill 			break;
1116615Sbill 		case N_DATA:
1117615Sbill 		case N_EXT+N_DATA:
1118615Sbill 			cursym.n_value += cdrel;
1119615Sbill 			break;
1120615Sbill 		case N_BSS:
1121615Sbill 		case N_EXT+N_BSS:
1122615Sbill 			cursym.n_value += cbrel;
1123615Sbill 			break;
1124615Sbill 		case N_EXT+N_UNDF:
1125615Sbill 			break;
1126615Sbill 		default:
1127615Sbill 			if (cursym.n_type&N_EXT)
1128615Sbill 				cursym.n_type = N_EXT+N_ABS;
1129615Sbill 		}
1130615Sbill /* end inline expansion of symreloc() */
1131615Sbill 		type = cursym.n_type;
1132898Sbill 		if (yflag && cursym.n_un.n_name)
1133898Sbill 			for (i = 0; i < yflag; i++)
1134898Sbill 				/* fast check for 2d character! */
1135898Sbill 				if (ytab[i][1] == cursym.n_un.n_name[1] &&
1136898Sbill 				    !strcmp(ytab[i], cursym.n_un.n_name)) {
1137898Sbill 					tracesym();
1138898Sbill 					break;
1139898Sbill 				}
1140615Sbill 		if ((type&N_EXT) == 0) {
1141615Sbill 			if (!sflag&&!xflag&&
1142615Sbill 			    (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB))
1143615Sbill 				symwrite(&cursym, sout);
1144615Sbill 			continue;
1145615Sbill 		}
1146615Sbill 		if (funding)
1147615Sbill 			continue;
1148615Sbill 		if ((sp = *lookup()) == 0)
1149615Sbill 			error(1, "internal error: symbol not found");
1150615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
1151615Sbill 			if (clocseg->lo_used == NSYMPR) {
1152615Sbill 				if (++clocseg == &locseg[NSEG])
1153615Sbill 					error(1, "local symbol overflow");
1154615Sbill 				clocseg->lo_used = 0;
1155615Sbill 			}
1156615Sbill 			if (clocseg->lo_first == 0) {
1157615Sbill 				clocseg->lo_first = (struct local *)
1158615Sbill 				    malloc(NSYMPR * sizeof (struct local));
1159615Sbill 				if (clocseg->lo_first == 0)
1160615Sbill 					error(1, "out of memory (clocseg)");
1161615Sbill 			}
1162615Sbill 			lp = &clocseg->lo_first[clocseg->lo_used++];
1163615Sbill 			lp->l_index = symno;
1164615Sbill 			lp->l_symbol = sp;
1165615Sbill 			lp->l_link = lochash[symno % LHSIZ];
1166615Sbill 			lochash[symno % LHSIZ] = lp;
1167615Sbill 			continue;
1168615Sbill 		}
1169615Sbill 		if (cursym.n_type & N_STAB)
1170615Sbill 			continue;
1171615Sbill 		if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) {
1172615Sbill 			printf("%s: ", cursym.n_un.n_name);
1173615Sbill 			error(0, "multiply defined");
1174615Sbill 		}
1175615Sbill 	}
1176615Sbill 	if (funding)
1177615Sbill 		return;
1178615Sbill 	dseek(&text, loc, filhdr.a_text);
1179615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize);
1180650Sbill 	load2td(ctrel, torigin - textbase, tout, trout);
1181615Sbill 	dseek(&text, loc+filhdr.a_text, filhdr.a_data);
1182615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize,
1183615Sbill 	    filhdr.a_drsize);
1184650Sbill 	load2td(cdrel, dorigin - database, dout, drout);
1185615Sbill 	while (filhdr.a_data & (sizeof(long)-1)) {
1186615Sbill 		bputc(0, dout);
1187615Sbill 		filhdr.a_data++;
1188615Sbill 	}
1189615Sbill 	torigin += filhdr.a_text;
1190*1752Sbill 	dorigin += round(filhdr.a_data, sizeof (long));
1191*1752Sbill 	borigin += round(filhdr.a_bss, sizeof (long));
1192615Sbill 	free(curstr);
1193615Sbill }
1194615Sbill 
1195898Sbill struct tynames {
1196898Sbill 	int	ty_value;
1197898Sbill 	char	*ty_name;
1198898Sbill } tynames[] = {
1199898Sbill 	N_UNDF,	"undefined",
1200898Sbill 	N_ABS,	"absolute",
1201898Sbill 	N_TEXT,	"text",
1202898Sbill 	N_DATA,	"data",
1203898Sbill 	N_BSS,	"bss",
1204898Sbill 	N_COMM,	"common",
1205898Sbill 	0,	0,
1206898Sbill };
1207898Sbill 
1208898Sbill tracesym()
1209898Sbill {
1210898Sbill 	register struct tynames *tp;
1211898Sbill 
1212898Sbill 	if (cursym.n_type & N_STAB)
1213898Sbill 		return;
1214898Sbill 	printf("%s", filname);
1215898Sbill 	if (archdr.ar_name[0])
1216898Sbill 		printf("(%s)", archdr.ar_name);
1217898Sbill 	printf(": ");
1218898Sbill 	if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) {
1219898Sbill 		printf("definition of common %s size %d\n",
1220898Sbill 		    cursym.n_un.n_name, cursym.n_value);
1221898Sbill 		return;
1222898Sbill 	}
1223898Sbill 	for (tp = tynames; tp->ty_name; tp++)
1224898Sbill 		if (tp->ty_value == (cursym.n_type&N_TYPE))
1225898Sbill 			break;
1226898Sbill 	printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to");
1227898Sbill 	if (cursym.n_type&N_EXT)
1228898Sbill 		printf(" external");
1229898Sbill 	if (tp->ty_name)
1230898Sbill 		printf(" %s", tp->ty_name);
1231898Sbill 	printf(" %s\n", cursym.n_un.n_name);
1232898Sbill }
1233898Sbill 
1234650Sbill /*
1235650Sbill  * This routine relocates the single text or data segment argument.
1236650Sbill  * Offsets from external symbols are resolved by adding the value
1237650Sbill  * of the external symbols.  Non-external reference are updated to account
1238650Sbill  * for the relative motion of the segments (ctrel, cdrel, ...).  If
1239650Sbill  * a relocation was pc-relative, then we update it to reflect the
1240650Sbill  * change in the positioning of the segments by adding the displacement
1241650Sbill  * of the referenced segment and subtracting the displacement of the
1242650Sbill  * current segment (creloc).
1243650Sbill  *
1244650Sbill  * If we are saving the relocation information, then we increase
1245650Sbill  * each relocation datum address by our base position in the new segment.
1246650Sbill  */
1247650Sbill load2td(creloc, position, b1, b2)
1248650Sbill 	long creloc, offset;
1249615Sbill 	struct biobuf *b1, *b2;
1250615Sbill {
1251615Sbill 	register struct nlist *sp;
1252615Sbill 	register struct local *lp;
1253615Sbill 	long tw;
1254615Sbill 	register struct relocation_info *rp, *rpend;
1255615Sbill 	struct relocation_info *relp;
1256615Sbill 	char *codep;
1257615Sbill 	register char *cp;
1258615Sbill 	int relsz, codesz;
1259615Sbill 
1260615Sbill 	relsz = reloc.size;
1261615Sbill 	relp = (struct relocation_info *)malloc(relsz);
1262615Sbill 	codesz = text.size;
1263615Sbill 	codep = (char *)malloc(codesz);
1264615Sbill 	if (relp == 0 || codep == 0)
1265615Sbill 		error(1, "out of memory (load2td)");
1266615Sbill 	mget((char *)relp, relsz, &reloc);
1267615Sbill 	rpend = &relp[relsz / sizeof (struct relocation_info)];
1268615Sbill 	mget(codep, codesz, &text);
1269615Sbill 	for (rp = relp; rp < rpend; rp++) {
1270615Sbill 		cp = codep + rp->r_address;
1271650Sbill 		/*
1272650Sbill 		 * Pick up previous value at location to be relocated.
1273650Sbill 		 */
1274615Sbill 		switch (rp->r_length) {
1275615Sbill 
1276615Sbill 		case 0:		/* byte */
1277615Sbill 			tw = *cp;
1278615Sbill 			break;
1279615Sbill 
1280615Sbill 		case 1:		/* word */
1281615Sbill 			tw = *(short *)cp;
1282615Sbill 			break;
1283615Sbill 
1284615Sbill 		case 2:		/* long */
1285615Sbill 			tw = *(long *)cp;
1286615Sbill 			break;
1287615Sbill 
1288615Sbill 		default:
1289615Sbill 			error(1, "load2td botch: bad length");
1290615Sbill 		}
1291650Sbill 		/*
1292650Sbill 		 * If relative to an external which is defined,
1293650Sbill 		 * resolve to a simpler kind of reference in the
1294650Sbill 		 * result file.  If the external is undefined, just
1295650Sbill 		 * convert the symbol number to the number of the
1296650Sbill 		 * symbol in the result file and leave it undefined.
1297650Sbill 		 */
1298615Sbill 		if (rp->r_extern) {
1299650Sbill 			/*
1300650Sbill 			 * Search the hash table which maps local
1301650Sbill 			 * symbol numbers to symbol tables entries
1302650Sbill 			 * in the new a.out file.
1303650Sbill 			 */
1304615Sbill 			lp = lochash[rp->r_symbolnum % LHSIZ];
1305615Sbill 			while (lp->l_index != rp->r_symbolnum) {
1306615Sbill 				lp = lp->l_link;
1307615Sbill 				if (lp == 0)
1308615Sbill 					error(1, "local symbol botch");
1309615Sbill 			}
1310615Sbill 			sp = lp->l_symbol;
1311615Sbill 			if (sp->n_type == N_EXT+N_UNDF)
1312615Sbill 				rp->r_symbolnum = nsym+symx(sp);
1313615Sbill 			else {
1314615Sbill 				rp->r_symbolnum = sp->n_type & N_TYPE;
1315615Sbill 				tw += sp->n_value;
1316615Sbill 				rp->r_extern = 0;
1317615Sbill 			}
1318615Sbill 		} else switch (rp->r_symbolnum & N_TYPE) {
1319650Sbill 		/*
1320650Sbill 		 * Relocation is relative to the loaded position
1321650Sbill 		 * of another segment.  Update by the change in position
1322650Sbill 		 * of that segment.
1323650Sbill 		 */
1324615Sbill 		case N_TEXT:
1325615Sbill 			tw += ctrel;
1326615Sbill 			break;
1327615Sbill 		case N_DATA:
1328615Sbill 			tw += cdrel;
1329615Sbill 			break;
1330615Sbill 		case N_BSS:
1331615Sbill 			tw += cbrel;
1332615Sbill 			break;
1333615Sbill 		case N_ABS:
1334615Sbill 			break;
1335615Sbill 		default:
1336615Sbill 			error(1, "relocation format botch (symbol type))");
1337615Sbill 		}
1338650Sbill 		/*
1339650Sbill 		 * Relocation is pc relative, so decrease the relocation
1340650Sbill 		 * by the amount the current segment is displaced.
1341650Sbill 		 * (E.g if we are a relative reference to a text location
1342650Sbill 		 * from data space, we added the increase in the text address
1343650Sbill 		 * above, and subtract the increase in our (data) address
1344650Sbill 		 * here, leaving the net change the relative change in the
1345650Sbill 		 * positioning of our text and data segments.)
1346650Sbill 		 */
1347615Sbill 		if (rp->r_pcrel)
1348615Sbill 			tw -= creloc;
1349650Sbill 		/*
1350650Sbill 		 * Put the value back in the segment,
1351650Sbill 		 * while checking for overflow.
1352650Sbill 		 */
1353615Sbill 		switch (rp->r_length) {
1354615Sbill 
1355615Sbill 		case 0:		/* byte */
1356615Sbill 			if (tw < -128 || tw > 127)
1357615Sbill 				error(0, "byte displacement overflow");
1358615Sbill 			*cp = tw;
1359615Sbill 			break;
1360615Sbill 		case 1:		/* word */
1361615Sbill 			if (tw < -32768 || tw > 32767)
1362615Sbill 				error(0, "word displacement overflow");
1363615Sbill 			*(short *)cp = tw;
1364615Sbill 			break;
1365615Sbill 		case 2:		/* long */
1366615Sbill 			*(long *)cp = tw;
1367615Sbill 			break;
1368615Sbill 		}
1369650Sbill 		/*
1370650Sbill 		 * If we are saving relocation information,
1371650Sbill 		 * we must convert the address in the segment from
1372650Sbill 		 * the old .o file into an address in the segment in
1373650Sbill 		 * the new a.out, by adding the position of our
1374650Sbill 		 * segment in the new larger segment.
1375650Sbill 		 */
1376615Sbill 		if (rflag)
1377650Sbill 			rp->r_address += position;
1378615Sbill 	}
1379615Sbill 	bwrite(codep, codesz, b1);
1380615Sbill 	if (rflag)
1381615Sbill 		bwrite(relp, relsz, b2);
1382615Sbill 	cfree((char *)relp);
1383615Sbill 	cfree(codep);
1384615Sbill }
1385615Sbill 
1386615Sbill finishout()
1387615Sbill {
1388615Sbill 	register int i;
1389615Sbill 	int nsymt;
1390615Sbill 
1391615Sbill 	if (sflag==0) {
1392615Sbill 		nsymt = symx(nextsym);
1393615Sbill 		for (i = 0; i < nsymt; i++)
1394615Sbill 			symwrite(xsym(i), sout);
1395615Sbill 		bwrite(&offset, sizeof offset, sout);
1396615Sbill 	}
1397615Sbill 	if (!ofilfnd) {
1398615Sbill 		unlink("a.out");
1399898Sbill 		if (link("l.out", "a.out") < 0)
1400898Sbill 			error(1, "cannot move l.out to a.out");
1401615Sbill 		ofilename = "a.out";
1402615Sbill 	}
1403615Sbill 	delarg = errlev;
1404615Sbill 	delexit();
1405615Sbill }
1406615Sbill 
1407615Sbill mkfsym(s)
1408615Sbill char *s;
1409615Sbill {
1410615Sbill 
1411615Sbill 	if (sflag || xflag)
1412615Sbill 		return;
1413615Sbill 	cursym.n_un.n_name = s;
1414615Sbill 	cursym.n_type = N_TEXT;
1415615Sbill 	cursym.n_value = torigin;
1416615Sbill 	symwrite(&cursym, sout);
1417615Sbill }
1418615Sbill 
1419615Sbill getarhdr()
1420615Sbill {
1421615Sbill 	register char *cp;
1422615Sbill 
1423615Sbill 	mget((char *)&archdr, sizeof archdr, &text);
1424615Sbill 	for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];)
1425615Sbill 		if (*cp++ == ' ') {
1426615Sbill 			cp[-1] = 0;
1427615Sbill 			return;
1428615Sbill 		}
1429615Sbill }
1430615Sbill 
1431615Sbill mget(loc, n, sp)
1432615Sbill register STREAM *sp;
1433615Sbill register char *loc;
1434615Sbill {
1435615Sbill 	register char *p;
1436615Sbill 	register int take;
1437615Sbill 
1438615Sbill top:
1439615Sbill 	if (n == 0)
1440615Sbill 		return;
1441615Sbill 	if (sp->size && sp->nibuf) {
1442615Sbill 		p = sp->ptr;
1443615Sbill 		take = sp->size;
1444615Sbill 		if (take > sp->nibuf)
1445615Sbill 			take = sp->nibuf;
1446615Sbill 		if (take > n)
1447615Sbill 			take = n;
1448615Sbill 		n -= take;
1449615Sbill 		sp->size -= take;
1450615Sbill 		sp->nibuf -= take;
1451615Sbill 		sp->pos += take;
1452615Sbill 		do
1453615Sbill 			*loc++ = *p++;
1454615Sbill 		while (--take > 0);
1455615Sbill 		sp->ptr = p;
1456615Sbill 		goto top;
1457615Sbill 	}
1458615Sbill 	if (n > BUFSIZ) {
1459615Sbill 		take = n - n % BSIZE;
1460615Sbill 		lseek(infil, (sp->bno+1)*BSIZE, 0);
1461615Sbill 		if (take > sp->size || read(infil, loc, take) != take)
1462615Sbill 			error(1, "premature EOF");
1463615Sbill 		loc += take;
1464615Sbill 		n -= take;
1465615Sbill 		sp->size -= take;
1466615Sbill 		sp->pos += take;
1467615Sbill 		dseek(sp, (sp->bno+1+take/BSIZE)*BSIZE, -1);
1468615Sbill 		goto top;
1469615Sbill 	}
1470615Sbill 	*loc++ = get(sp);
1471615Sbill 	--n;
1472615Sbill 	goto top;
1473615Sbill }
1474615Sbill 
1475615Sbill symwrite(sp, bp)
1476615Sbill 	struct nlist *sp;
1477615Sbill 	struct biobuf *bp;
1478615Sbill {
1479615Sbill 	register int len;
1480615Sbill 	register char *str;
1481615Sbill 
1482615Sbill 	str = sp->n_un.n_name;
1483615Sbill 	if (str) {
1484615Sbill 		sp->n_un.n_strx = offset;
1485615Sbill 		len = strlen(str) + 1;
1486615Sbill 		bwrite(str, len, strout);
1487615Sbill 		offset += len;
1488615Sbill 	}
1489615Sbill 	bwrite(sp, sizeof (*sp), bp);
1490615Sbill 	sp->n_un.n_name = str;
1491615Sbill }
1492615Sbill 
1493615Sbill dseek(sp, loc, s)
1494615Sbill register STREAM *sp;
1495615Sbill long loc, s;
1496615Sbill {
1497615Sbill 	register PAGE *p;
1498615Sbill 	register b, o;
1499615Sbill 	int n;
1500615Sbill 
1501615Sbill 	b = loc>>BSHIFT;
1502615Sbill 	o = loc&BMASK;
1503615Sbill 	if (o&01)
1504615Sbill 		error(1, "loader error; odd offset");
1505615Sbill 	--sp->pno->nuser;
1506615Sbill 	if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b)
1507615Sbill 		if (p->nuser==0 || (p = &page[0])->nuser==0) {
1508615Sbill 			if (page[0].nuser==0 && page[1].nuser==0)
1509615Sbill 				if (page[0].bno < page[1].bno)
1510615Sbill 					p = &page[0];
1511615Sbill 			p->bno = b;
1512615Sbill 			lseek(infil, loc & ~(long)BMASK, 0);
1513615Sbill 			if ((n = read(infil, p->buff, sizeof(p->buff))) < 0)
1514615Sbill 				n = 0;
1515615Sbill 			p->nibuf = n;
1516615Sbill 	} else
1517615Sbill 		error(1, "botch: no pages");
1518615Sbill 	++p->nuser;
1519615Sbill 	sp->bno = b;
1520615Sbill 	sp->pno = p;
1521615Sbill 	if (s != -1) {sp->size = s; sp->pos = 0;}
1522615Sbill 	sp->ptr = (char *)(p->buff + o);
1523615Sbill 	if ((sp->nibuf = p->nibuf-o) <= 0)
1524615Sbill 		sp->size = 0;
1525615Sbill }
1526615Sbill 
1527615Sbill char
1528615Sbill get(asp)
1529615Sbill STREAM *asp;
1530615Sbill {
1531615Sbill 	register STREAM *sp;
1532615Sbill 
1533615Sbill 	sp = asp;
1534615Sbill 	if ((sp->nibuf -= sizeof(char)) < 0) {
1535615Sbill 		dseek(sp, ((long)(sp->bno+1)<<BSHIFT), (long)-1);
1536615Sbill 		sp->nibuf -= sizeof(char);
1537615Sbill 	}
1538615Sbill 	if ((sp->size -= sizeof(char)) <= 0) {
1539615Sbill 		if (sp->size < 0)
1540615Sbill 			error(1, "premature EOF");
1541615Sbill 		++fpage.nuser;
1542615Sbill 		--sp->pno->nuser;
1543615Sbill 		sp->pno = (PAGE *) &fpage;
1544615Sbill 	}
1545615Sbill 	sp->pos += sizeof(char);
1546615Sbill 	return(*sp->ptr++);
1547615Sbill }
1548615Sbill 
1549615Sbill getfile(acp)
1550615Sbill char *acp;
1551615Sbill {
1552615Sbill 	register char *cp;
1553615Sbill 	register int c;
1554615Sbill 	char arcmag[SARMAG+1];
1555615Sbill 	struct stat stb;
1556615Sbill 
1557615Sbill 	cp = acp;
1558615Sbill 	infil = -1;
1559615Sbill 	archdr.ar_name[0] = '\0';
1560615Sbill 	filname = cp;
1561615Sbill 	if (cp[0]=='-' && cp[1]=='l') {
1562898Sbill 		char *locfilname = "/usr/local/lib/libxxxxxxxxxxxxxxx";
1563615Sbill 		if(cp[2] == '\0')
1564615Sbill 			cp = "-la";
1565898Sbill 		filname = "/usr/lib/libxxxxxxxxxxxxxxx";
1566615Sbill 		for(c=0; cp[c+2]; c++) {
1567615Sbill 			filname[c+12] = cp[c+2];
1568615Sbill 			locfilname[c+18] = cp[c+2];
1569615Sbill 		}
1570615Sbill 		filname[c+12] = locfilname[c+18] = '.';
1571615Sbill 		filname[c+13] = locfilname[c+19] = 'a';
1572615Sbill 		filname[c+14] = locfilname[c+20] = '\0';
1573615Sbill 		if ((infil = open(filname+4, 0)) >= 0) {
1574615Sbill 			filname += 4;
1575615Sbill 		} else if ((infil = open(filname, 0)) < 0) {
1576615Sbill 			filname = locfilname;
1577615Sbill 		}
1578615Sbill 	}
1579615Sbill 	if (infil == -1 && (infil = open(filname, 0)) < 0)
1580615Sbill 		error(1, "cannot open");
1581615Sbill 	page[0].bno = page[1].bno = -1;
1582615Sbill 	page[0].nuser = page[1].nuser = 0;
1583615Sbill 	text.pno = reloc.pno = (PAGE *) &fpage;
1584615Sbill 	fpage.nuser = 2;
1585615Sbill 	dseek(&text, 0L, SARMAG);
1586615Sbill 	if (text.size <= 0)
1587615Sbill 		error(1, "premature EOF");
1588615Sbill 	mget((char *)arcmag, SARMAG, &text);
1589615Sbill 	arcmag[SARMAG] = 0;
1590615Sbill 	if (strcmp(arcmag, ARMAG))
1591615Sbill 		return (0);
1592615Sbill 	dseek(&text, SARMAG, sizeof archdr);
1593615Sbill 	if(text.size <= 0)
1594615Sbill 		return (1);
1595615Sbill 	getarhdr();
1596615Sbill 	if (strncmp(archdr.ar_name, "__.SYMDEF", sizeof(archdr.ar_name)) != 0)
1597615Sbill 		return (1);
1598615Sbill 	fstat(infil, &stb);
1599615Sbill 	return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2);
1600615Sbill }
1601615Sbill 
1602615Sbill struct nlist **
1603615Sbill lookup()
1604615Sbill {
1605615Sbill 	register int sh;
1606615Sbill 	register struct nlist **hp;
1607615Sbill 	register char *cp, *cp1;
1608615Sbill 	register struct symseg *gp;
1609615Sbill 	register int i;
1610615Sbill 
1611615Sbill 	sh = 0;
1612615Sbill 	for (cp = cursym.n_un.n_name; *cp;)
1613615Sbill 		sh = (sh<<1) + *cp++;
1614615Sbill 	sh = (sh & 0x7fffffff) % HSIZE;
1615615Sbill 	for (gp = symseg; gp < &symseg[NSEG]; gp++) {
1616615Sbill 		if (gp->sy_first == 0) {
1617615Sbill 			gp->sy_first = (struct nlist *)
1618615Sbill 			    calloc(NSYM, sizeof (struct nlist));
1619615Sbill 			gp->sy_hfirst = (struct nlist **)
1620615Sbill 			    calloc(HSIZE, sizeof (struct nlist *));
1621615Sbill 			if (gp->sy_first == 0 || gp->sy_hfirst == 0)
1622615Sbill 				error(1, "ran out of space for symbol table");
1623615Sbill 			gp->sy_last = gp->sy_first + NSYM;
1624615Sbill 			gp->sy_hlast = gp->sy_hfirst + HSIZE;
1625615Sbill 		}
1626615Sbill 		if (gp > csymseg)
1627615Sbill 			csymseg = gp;
1628615Sbill 		hp = gp->sy_hfirst + sh;
1629615Sbill 		i = 1;
1630615Sbill 		do {
1631615Sbill 			if (*hp == 0) {
1632615Sbill 				if (gp->sy_used == NSYM)
1633615Sbill 					break;
1634615Sbill 				return (hp);
1635615Sbill 			}
1636615Sbill 			cp1 = (*hp)->n_un.n_name;
1637615Sbill 			for (cp = cursym.n_un.n_name; *cp == *cp1++;)
1638615Sbill 				if (*cp++ == 0)
1639615Sbill 					return (hp);
1640615Sbill 			hp += i;
1641615Sbill 			i += 2;
1642615Sbill 			if (hp >= gp->sy_hlast)
1643615Sbill 				hp -= HSIZE;
1644615Sbill 		} while (i < HSIZE);
1645615Sbill 		if (i > HSIZE)
1646615Sbill 			error(1, "hash table botch");
1647615Sbill 	}
1648615Sbill 	error(1, "symbol table overflow");
1649615Sbill 	/*NOTREACHED*/
1650615Sbill }
1651615Sbill 
1652615Sbill symfree(saved)
1653615Sbill 	struct nlist *saved;
1654615Sbill {
1655615Sbill 	register struct symseg *gp;
1656615Sbill 	register struct nlist *sp;
1657615Sbill 
1658615Sbill 	for (gp = csymseg; gp >= symseg; gp--, csymseg--) {
1659615Sbill 		sp = gp->sy_first + gp->sy_used;
1660615Sbill 		if (sp == saved) {
1661615Sbill 			nextsym = sp;
1662615Sbill 			return;
1663615Sbill 		}
1664615Sbill 		for (sp--; sp >= gp->sy_first; sp--) {
1665615Sbill 			gp->sy_hfirst[sp->n_hash] = 0;
1666615Sbill 			gp->sy_used--;
1667615Sbill 			if (sp == saved) {
1668615Sbill 				nextsym = sp;
1669615Sbill 				return;
1670615Sbill 			}
1671615Sbill 		}
1672615Sbill 	}
1673615Sbill 	if (saved == 0)
1674615Sbill 		return;
1675615Sbill 	error(1, "symfree botch");
1676615Sbill }
1677615Sbill 
1678615Sbill struct nlist **
1679615Sbill slookup(s)
1680615Sbill 	char *s;
1681615Sbill {
1682615Sbill 
1683615Sbill 	cursym.n_un.n_name = s;
1684615Sbill 	cursym.n_type = N_EXT+N_UNDF;
1685615Sbill 	cursym.n_value = 0;
1686615Sbill 	return (lookup());
1687615Sbill }
1688615Sbill 
1689615Sbill enter(hp)
1690615Sbill register struct nlist **hp;
1691615Sbill {
1692615Sbill 	register struct nlist *sp;
1693615Sbill 
1694615Sbill 	if (*hp==0) {
1695615Sbill 		if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast)
1696615Sbill 			error(1, "enter botch");
1697615Sbill 		*hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used;
1698615Sbill 		csymseg->sy_used++;
1699615Sbill 		sp->n_un.n_name = cursym.n_un.n_name;
1700615Sbill 		sp->n_type = cursym.n_type;
1701615Sbill 		sp->n_hash = hp - csymseg->sy_hfirst;
1702615Sbill 		sp->n_value = cursym.n_value;
1703615Sbill 		nextsym = lastsym + 1;
1704615Sbill 		return(1);
1705615Sbill 	} else {
1706615Sbill 		lastsym = *hp;
1707615Sbill 		return(0);
1708615Sbill 	}
1709615Sbill }
1710615Sbill 
1711615Sbill symx(sp)
1712615Sbill 	struct nlist *sp;
1713615Sbill {
1714615Sbill 	register struct symseg *gp;
1715615Sbill 
1716615Sbill 	if (sp == 0)
1717615Sbill 		return (0);
1718615Sbill 	for (gp = csymseg; gp >= symseg; gp--)
1719615Sbill 		/* <= is sloppy so nextsym will always work */
1720615Sbill 		if (sp >= gp->sy_first && sp <= gp->sy_last)
1721615Sbill 			return ((gp - symseg) * NSYM + sp - gp->sy_first);
1722615Sbill 	error(1, "symx botch");
1723615Sbill 	/*NOTREACHED*/
1724615Sbill }
1725615Sbill 
1726615Sbill symreloc()
1727615Sbill {
1728615Sbill 	if(funding) return;
1729615Sbill 	switch (cursym.n_type & 017) {
1730615Sbill 
1731615Sbill 	case N_TEXT:
1732615Sbill 	case N_EXT+N_TEXT:
1733615Sbill 		cursym.n_value += ctrel;
1734615Sbill 		return;
1735615Sbill 
1736615Sbill 	case N_DATA:
1737615Sbill 	case N_EXT+N_DATA:
1738615Sbill 		cursym.n_value += cdrel;
1739615Sbill 		return;
1740615Sbill 
1741615Sbill 	case N_BSS:
1742615Sbill 	case N_EXT+N_BSS:
1743615Sbill 		cursym.n_value += cbrel;
1744615Sbill 		return;
1745615Sbill 
1746615Sbill 	case N_EXT+N_UNDF:
1747615Sbill 		return;
1748615Sbill 
1749615Sbill 	default:
1750615Sbill 		if (cursym.n_type&N_EXT)
1751615Sbill 			cursym.n_type = N_EXT+N_ABS;
1752615Sbill 		return;
1753615Sbill 	}
1754615Sbill }
1755615Sbill 
1756615Sbill error(n, s)
1757615Sbill char *s;
1758615Sbill {
1759898Sbill 
1760615Sbill 	if (errlev==0)
1761615Sbill 		printf("ld:");
1762615Sbill 	if (filname) {
1763615Sbill 		printf("%s", filname);
1764615Sbill 		if (n != -1 && archdr.ar_name[0])
1765615Sbill 			printf("(%s)", archdr.ar_name);
1766615Sbill 		printf(": ");
1767615Sbill 	}
1768615Sbill 	printf("%s\n", s);
1769615Sbill 	if (n == -1)
1770615Sbill 		return;
1771615Sbill 	if (n)
1772615Sbill 		delexit();
1773615Sbill 	errlev = 2;
1774615Sbill }
1775615Sbill 
1776615Sbill readhdr(loc)
1777615Sbill off_t loc;
1778615Sbill {
1779615Sbill 
1780615Sbill 	dseek(&text, loc, (long)sizeof(filhdr));
1781615Sbill 	mget((short *)&filhdr, sizeof(filhdr), &text);
1782615Sbill 	if (N_BADMAG(filhdr)) {
1783615Sbill 		if (filhdr.a_magic == OARMAG)
1784615Sbill 			error(1, "old archive");
1785615Sbill 		error(1, "bad magic number");
1786615Sbill 	}
1787615Sbill 	if (filhdr.a_text&01 || filhdr.a_data&01)
1788615Sbill 		error(1, "text/data size odd");
1789615Sbill 	if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) {
1790615Sbill 		cdrel = -round(filhdr.a_text, PAGSIZ);
1791615Sbill 		cbrel = cdrel - filhdr.a_data;
1792615Sbill 	} else if (filhdr.a_magic == OMAGIC) {
1793615Sbill 		cdrel = -filhdr.a_text;
1794615Sbill 		cbrel = cdrel - filhdr.a_data;
1795615Sbill 	} else
1796615Sbill 		error(1, "bad format");
1797615Sbill }
1798615Sbill 
1799615Sbill round(v, r)
1800615Sbill 	int v;
1801615Sbill 	u_long r;
1802615Sbill {
1803615Sbill 
1804615Sbill 	r--;
1805615Sbill 	v += r;
1806615Sbill 	v &= ~(long)r;
1807615Sbill 	return(v);
1808615Sbill }
1809615Sbill 
1810615Sbill #define	NSAVETAB	8192
1811615Sbill char	*savetab;
1812615Sbill int	saveleft;
1813615Sbill 
1814615Sbill char *
1815615Sbill savestr(cp)
1816615Sbill 	register char *cp;
1817615Sbill {
1818615Sbill 	register int len;
1819615Sbill 
1820615Sbill 	len = strlen(cp) + 1;
1821615Sbill 	if (len > saveleft) {
1822615Sbill 		saveleft = NSAVETAB;
1823615Sbill 		if (len > saveleft)
1824615Sbill 			saveleft = len;
1825615Sbill 		savetab = (char *)malloc(saveleft);
1826615Sbill 		if (savetab == 0)
1827615Sbill 			error(1, "ran out of memory (savestr)");
1828615Sbill 	}
1829615Sbill 	strncpy(savetab, cp, len);
1830615Sbill 	cp = savetab;
1831615Sbill 	savetab += len;
1832615Sbill 	saveleft -= len;
1833615Sbill 	return (cp);
1834615Sbill }
1835615Sbill 
1836615Sbill bopen(bp, off)
1837615Sbill 	struct biobuf *bp;
1838615Sbill {
1839615Sbill 
1840615Sbill 	bp->b_ptr = bp->b_buf;
1841615Sbill 	bp->b_nleft = BUFSIZ - off % BUFSIZ;
1842615Sbill 	bp->b_off = off;
1843615Sbill 	bp->b_link = biobufs;
1844615Sbill 	biobufs = bp;
1845615Sbill }
1846615Sbill 
1847615Sbill int	bwrerror;
1848615Sbill 
1849615Sbill bwrite(p, cnt, bp)
1850615Sbill 	register char *p;
1851615Sbill 	register int cnt;
1852615Sbill 	register struct biobuf *bp;
1853615Sbill {
1854615Sbill 	register int put;
1855615Sbill 	register char *to;
1856615Sbill 
1857615Sbill top:
1858615Sbill 	if (cnt == 0)
1859615Sbill 		return;
1860615Sbill 	if (bp->b_nleft) {
1861615Sbill 		put = bp->b_nleft;
1862615Sbill 		if (put > cnt)
1863615Sbill 			put = cnt;
1864615Sbill 		bp->b_nleft -= put;
1865615Sbill 		to = bp->b_ptr;
1866615Sbill 		asm("movc3 r8,(r11),(r7)");
1867615Sbill 		bp->b_ptr += put;
1868615Sbill 		p += put;
1869615Sbill 		cnt -= put;
1870615Sbill 		goto top;
1871615Sbill 	}
1872615Sbill 	if (cnt >= BUFSIZ) {
1873615Sbill 		if (bp->b_ptr != bp->b_buf)
1874615Sbill 			bflush1(bp);
1875615Sbill 		put = cnt - cnt % BUFSIZ;
1876615Sbill 		if (boffset != bp->b_off)
1877615Sbill 			lseek(biofd, bp->b_off, 0);
1878615Sbill 		if (write(biofd, p, put) != put) {
1879615Sbill 			bwrerror = 1;
1880615Sbill 			error(1, "output write error");
1881615Sbill 		}
1882615Sbill 		bp->b_off += put;
1883615Sbill 		boffset = bp->b_off;
1884615Sbill 		p += put;
1885615Sbill 		cnt -= put;
1886615Sbill 		goto top;
1887615Sbill 	}
1888615Sbill 	bflush1(bp);
1889615Sbill 	goto top;
1890615Sbill }
1891615Sbill 
1892615Sbill bflush()
1893615Sbill {
1894615Sbill 	register struct biobuf *bp;
1895615Sbill 
1896615Sbill 	if (bwrerror)
1897615Sbill 		return;
1898615Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
1899615Sbill 		bflush1(bp);
1900615Sbill }
1901615Sbill 
1902615Sbill bflush1(bp)
1903615Sbill 	register struct biobuf *bp;
1904615Sbill {
1905615Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
1906615Sbill 
1907615Sbill 	if (cnt == 0)
1908615Sbill 		return;
1909615Sbill 	if (boffset != bp->b_off)
1910615Sbill 		lseek(biofd, bp->b_off, 0);
1911615Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
1912615Sbill 		bwrerror = 1;
1913615Sbill 		error(1, "output write error");
1914615Sbill 	}
1915615Sbill 	bp->b_off += cnt;
1916615Sbill 	boffset = bp->b_off;
1917615Sbill 	bp->b_ptr = bp->b_buf;
1918615Sbill 	bp->b_nleft = BUFSIZ;
1919615Sbill }
1920615Sbill 
1921615Sbill bflushc(bp, c)
1922615Sbill 	register struct biobuf *bp;
1923615Sbill {
1924615Sbill 
1925615Sbill 	bflush1(bp);
1926615Sbill 	bputc(c, bp);
1927615Sbill }
1928