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