xref: /csrg-svn/old/ld/ld.c (revision 25483)
119842Sdist /*
219842Sdist  * Copyright (c) 1980 Regents of the University of California.
319842Sdist  * All rights reserved.  The Berkeley software License Agreement
419842Sdist  * specifies the terms and conditions for redistribution.
519842Sdist  */
619842Sdist 
712671Ssam #ifndef lint
819842Sdist char copyright[] =
919842Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1019842Sdist  All rights reserved.\n";
1119842Sdist #endif not lint
126414Smckusic 
1319842Sdist #ifndef lint
14*25483Slepreau static char sccsid[] = "@(#)ld.c	5.3 (Berkeley) 11/15/85";
1519842Sdist #endif not lint
1619842Sdist 
17615Sbill /*
18898Sbill  * ld - string table version for VAX
19615Sbill  */
20615Sbill 
2117133Ssam #include <sys/param.h>
22615Sbill #include <signal.h>
23615Sbill #include <stdio.h>
24615Sbill #include <ctype.h>
25650Sbill #include <ar.h>
26650Sbill #include <a.out.h>
27615Sbill #include <ranlib.h>
2813591Swnj #include <sys/stat.h>
2917133Ssam #include <sys/file.h>
30615Sbill 
31615Sbill /*
32615Sbill  * Basic strategy:
33615Sbill  *
34615Sbill  * The loader takes a number of files and libraries as arguments.
35615Sbill  * A first pass examines each file in turn.  Normal files are
36615Sbill  * unconditionally loaded, and the (external) symbols they define and require
37615Sbill  * are noted in the symbol table.   Libraries are searched, and the
38615Sbill  * library members which define needed symbols are remembered
39615Sbill  * in a special data structure so they can be selected on the second
40615Sbill  * pass.  Symbols defined and required by library members are also
41615Sbill  * recorded.
42615Sbill  *
43615Sbill  * After the first pass, the loader knows the size of the basic text
44615Sbill  * data, and bss segments from the sum of the sizes of the modules which
45615Sbill  * were required.  It has computed, for each ``common'' symbol, the
46615Sbill  * maximum size of any reference to it, and these symbols are then assigned
47615Sbill  * storage locations after their sizes are appropriately rounded.
48615Sbill  * The loader now knows all sizes for the eventual output file, and
49615Sbill  * can determine the final locations of external symbols before it
50615Sbill  * begins a second pass.
51615Sbill  *
52615Sbill  * On the second pass each normal file and required library member
53615Sbill  * is processed again.  The symbol table for each such file is
54615Sbill  * reread and relevant parts of it are placed in the output.  The offsets
55615Sbill  * in the local symbol table for externally defined symbols are recorded
56615Sbill  * since relocation information refers to symbols in this way.
57615Sbill  * Armed with all necessary information, the text and data segments
58615Sbill  * are relocated and the result is placed in the output file, which
59615Sbill  * is pasted together, ``in place'', by writing to it in several
60615Sbill  * different places concurrently.
61615Sbill  */
62615Sbill 
63615Sbill /*
64615Sbill  * Internal data structures
65615Sbill  *
66615Sbill  * All internal data structures are segmented and dynamically extended.
67615Sbill  * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT)
68615Sbill  * referenced library members, and 100 (NSYMPR) private (local) symbols
69615Sbill  * per object module.  For large programs and/or modules, these structures
70615Sbill  * expand to be up to 40 (NSEG) times as large as this as necessary.
71615Sbill  */
72615Sbill #define	NSEG	40		/* Number of segments, each data structure */
73615Sbill #define	NSYM	1103		/* Number of symbols per segment */
74615Sbill #define	NROUT	250		/* Number of library references per segment */
75615Sbill #define	NSYMPR	100		/* Number of private symbols per segment */
76615Sbill 
77615Sbill /*
78615Sbill  * Structure describing each symbol table segment.
79615Sbill  * Each segment has its own hash table.  We record the first
80615Sbill  * address in and first address beyond both the symbol and hash
81615Sbill  * tables, for use in the routine symx and the lookup routine respectively.
82615Sbill  * The symfree routine also understands this structure well as it used
83615Sbill  * to back out symbols from modules we decide that we don't need in pass 1.
84615Sbill  *
85615Sbill  * Csymseg points to the current symbol table segment;
86615Sbill  * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated,
87615Sbill  * (unless csymseg->sy_used == NSYM in which case we will allocate another
88615Sbill  * symbol table segment first.)
89615Sbill  */
90615Sbill struct	symseg {
91615Sbill 	struct	nlist *sy_first;	/* base of this alloc'ed segment */
92615Sbill 	struct	nlist *sy_last;		/* end of this segment, for n_strx */
93615Sbill 	int	sy_used;		/* symbols used in this seg */
94615Sbill 	struct	nlist **sy_hfirst;	/* base of hash table, this seg */
95615Sbill 	struct	nlist **sy_hlast;	/* end of hash table, this seg */
96615Sbill } symseg[NSEG], *csymseg;
97615Sbill 
98615Sbill /*
99615Sbill  * The lookup routine uses quadratic rehash.  Since a quadratic rehash
100615Sbill  * only probes 1/2 of the buckets in the table, and since the hash
101615Sbill  * table is segmented the same way the symbol table is, we make the
102615Sbill  * hash table have twice as many buckets as there are symbol table slots
103615Sbill  * in the segment.  This guarantees that the quadratic rehash will never
104615Sbill  * fail to find an empty bucket if the segment is not full and the
105615Sbill  * symbol is not there.
106615Sbill  */
107615Sbill #define	HSIZE	(NSYM*2)
108615Sbill 
109615Sbill /*
110615Sbill  * Xsym converts symbol table indices (ala x) into symbol table pointers.
111615Sbill  * Symx (harder, but never used in loops) inverts pointers into the symbol
112615Sbill  * table into indices using the symseg[] structure.
113615Sbill  */
114615Sbill #define	xsym(x)	(symseg[(x)/NSYM].sy_first+((x)%NSYM))
115615Sbill /* symx() is a function, defined below */
116615Sbill 
117615Sbill struct	nlist cursym;		/* current symbol */
118615Sbill struct	nlist *lastsym;		/* last symbol entered */
119615Sbill struct	nlist *nextsym;		/* next available symbol table entry */
120615Sbill struct	nlist *addsym;		/* first sym defined during incr load */
121615Sbill int	nsym;			/* pass2: number of local symbols in a.out */
122615Sbill /* nsym + symx(nextsym) is the symbol table size during pass2 */
123615Sbill 
124615Sbill struct	nlist **lookup(), **slookup();
125650Sbill struct	nlist *p_etext, *p_edata, *p_end, *entrypt;
126615Sbill 
127615Sbill /*
128615Sbill  * Definitions of segmentation for library member table.
129615Sbill  * For each library we encounter on pass 1 we record pointers to all
130615Sbill  * members which we will load on pass 2.  These are recorded as offsets
131615Sbill  * into the archive in the library member table.  Libraries are
132615Sbill  * separated in the table by the special offset value -1.
133615Sbill  */
134615Sbill off_t	li_init[NROUT];
135615Sbill struct	libseg {
136615Sbill 	off_t	*li_first;
137615Sbill 	int	li_used;
138615Sbill 	int	li_used2;
139615Sbill } libseg[NSEG] = {
140615Sbill 	li_init, 0, 0,
141615Sbill }, *clibseg = libseg;
142615Sbill 
143615Sbill /*
144615Sbill  * In processing each module on pass 2 we must relocate references
145615Sbill  * relative to external symbols.  These references are recorded
146615Sbill  * in the relocation information as relative to local symbol numbers
147615Sbill  * assigned to the external symbols when the module was created.
148615Sbill  * Thus before relocating the module in pass 2 we create a table
149615Sbill  * which maps these internal numbers to symbol table entries.
150615Sbill  * A hash table is constructed, based on the local symbol table indices,
151615Sbill  * for quick lookup of these symbols.
152615Sbill  */
153615Sbill #define	LHSIZ	31
154615Sbill struct	local {
155615Sbill 	int	l_index;		/* index to symbol in file */
156615Sbill 	struct	nlist *l_symbol;	/* ptr to symbol table */
157615Sbill 	struct	local *l_link;		/* hash link */
158615Sbill } *lochash[LHSIZ], lhinit[NSYMPR];
159615Sbill struct	locseg {
160615Sbill 	struct	local *lo_first;
161615Sbill 	int	lo_used;
162615Sbill } locseg[NSEG] = {
163615Sbill 	lhinit, 0
164615Sbill }, *clocseg;
165615Sbill 
166615Sbill /*
167615Sbill  * Libraries are typically built with a table of contents,
168615Sbill  * which is the first member of a library with special file
169615Sbill  * name __.SYMDEF and contains a list of symbol names
170615Sbill  * and with each symbol the offset of the library member which defines
171615Sbill  * it.  The loader uses this table to quickly tell which library members
172615Sbill  * are (potentially) useful.  The alternative, examining the symbol
173615Sbill  * table of each library member, is painfully slow for large archives.
174615Sbill  *
175615Sbill  * See <ranlib.h> for the definition of the ranlib structure and an
176615Sbill  * explanation of the __.SYMDEF file format.
177615Sbill  */
178615Sbill int	tnum;		/* number of symbols in table of contents */
179615Sbill int	ssiz;		/* size of string table for table of contents */
180615Sbill struct	ranlib *tab;	/* the table of contents (dynamically allocated) */
181615Sbill char	*tabstr;	/* string table for table of contents */
182615Sbill 
183615Sbill /*
184615Sbill  * We open each input file or library only once, but in pass2 we
185615Sbill  * (historically) read from such a file at 2 different places at the
186615Sbill  * same time.  These structures are remnants from those days,
187650Sbill  * and now serve only to catch ``Premature EOF''.
1886414Smckusic  * In order to make I/O more efficient, we provide routines which
18916068Sralph  * use the optimal block size returned by stat().
190615Sbill  */
1916414Smckusic #define BLKSIZE 1024
192615Sbill typedef struct {
193615Sbill 	short	*fakeptr;
194615Sbill 	int	bno;
195615Sbill 	int	nibuf;
196615Sbill 	int	nuser;
19716068Sralph 	char	*buff;
19816068Sralph 	int	bufsize;
199615Sbill } PAGE;
200615Sbill 
201615Sbill PAGE	page[2];
20216068Sralph int	p_blksize;
20316068Sralph int	p_blkshift;
20416068Sralph int	p_blkmask;
205615Sbill 
206615Sbill struct {
207615Sbill 	short	*fakeptr;
208615Sbill 	int	bno;
209615Sbill 	int	nibuf;
210615Sbill 	int	nuser;
211615Sbill } fpage;
212615Sbill 
213615Sbill typedef struct {
214615Sbill 	char	*ptr;
215615Sbill 	int	bno;
216615Sbill 	int	nibuf;
217615Sbill 	long	size;
218615Sbill 	long	pos;
219615Sbill 	PAGE	*pno;
220615Sbill } STREAM;
221615Sbill 
222615Sbill STREAM	text;
223615Sbill STREAM	reloc;
224615Sbill 
225615Sbill /*
226615Sbill  * Header from the a.out and the archive it is from (if any).
227615Sbill  */
228615Sbill struct	exec filhdr;
229615Sbill struct	ar_hdr archdr;
230615Sbill #define	OARMAG 0177545
231615Sbill 
232615Sbill /*
233615Sbill  * Options.
234615Sbill  */
235615Sbill int	trace;
236615Sbill int	xflag;		/* discard local symbols */
237615Sbill int	Xflag;		/* discard locals starting with 'L' */
238615Sbill int	Sflag;		/* discard all except locals and globals*/
239615Sbill int	rflag;		/* preserve relocation bits, don't define common */
240615Sbill int	arflag;		/* original copy of rflag */
241615Sbill int	sflag;		/* discard all symbols */
242898Sbill int	Mflag;		/* print rudimentary load map */
243615Sbill int	nflag;		/* pure procedure */
244615Sbill int	dflag;		/* define common even with rflag */
245650Sbill int	zflag;		/* demand paged  */
246615Sbill long	hsize;		/* size of hole at beginning of data to be squashed */
247615Sbill int	Aflag;		/* doing incremental load */
248650Sbill int	Nflag;		/* want impure a.out */
249615Sbill int	funding;	/* reading fundamental file for incremental load */
250898Sbill int	yflag;		/* number of symbols to be traced */
251898Sbill char	**ytab;		/* the symbols */
252615Sbill 
253615Sbill /*
254615Sbill  * These are the cumulative sizes, set in pass 1, which
255615Sbill  * appear in the a.out header when the loader is finished.
256615Sbill  */
257615Sbill off_t	tsize, dsize, bsize, trsize, drsize, ssize;
258615Sbill 
259615Sbill /*
260615Sbill  * Symbol relocation: c?rel is a scale factor which is
261615Sbill  * added to an old relocation to convert it to new units;
262615Sbill  * i.e. it is the difference between segment origins.
263650Sbill  * (Thus if we are loading from a data segment which began at location
264650Sbill  * 4 in a .o file into an a.out where it will be loaded starting at
265650Sbill  * 1024, cdrel will be 1020.)
266615Sbill  */
267615Sbill long	ctrel, cdrel, cbrel;
268615Sbill 
269615Sbill /*
270650Sbill  * Textbase is the start address of all text, 0 unless given by -T.
271615Sbill  * Database is the base of all data, computed before and used during pass2.
272650Sbill  */
273650Sbill long	textbase, database;
274650Sbill 
275650Sbill /*
276615Sbill  * The base addresses for the loaded text, data and bss from the
277615Sbill  * current module during pass2 are given by torigin, dorigin and borigin.
278615Sbill  */
279615Sbill long	torigin, dorigin, borigin;
280615Sbill 
281615Sbill /*
282615Sbill  * Errlev is nonzero when errors have occured.
283615Sbill  * Delarg is an implicit argument to the routine delexit
284615Sbill  * which is called on error.  We do ``delarg = errlev'' before normal
285615Sbill  * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the
286615Sbill  * result file executable.
287615Sbill  */
288615Sbill int	errlev;
289615Sbill int	delarg	= 4;
290615Sbill 
291615Sbill /*
292615Sbill  * The biobuf structure and associated routines are used to write
293615Sbill  * into one file at several places concurrently.  Calling bopen
294615Sbill  * with a biobuf structure sets it up to write ``biofd'' starting
295615Sbill  * at the specified offset.  You can then use ``bwrite'' and/or ``bputc''
296615Sbill  * to stuff characters in the stream, much like ``fwrite'' and ``fputc''.
297615Sbill  * Calling bflush drains all the buffers and MUST be done before exit.
298615Sbill  */
299615Sbill struct	biobuf {
300615Sbill 	short	b_nleft;		/* Number free spaces left in b_buf */
30116068Sralph /* Initialize to be less than b_bufsize initially, to boundary align in file */
302615Sbill 	char	*b_ptr;			/* Next place to stuff characters */
30316068Sralph 	char	*b_buf;			/* Pointer to the buffer */
30416068Sralph 	int	b_bufsize;		/* Size of the buffer */
305615Sbill 	off_t	b_off;			/* Current file offset */
306615Sbill 	struct	biobuf *b_link;		/* Link in chain for bflush() */
307615Sbill } *biobufs;
308615Sbill #define	bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \
309615Sbill 		       : bflushc(b, c))
310615Sbill int	biofd;
311615Sbill off_t	boffset;
312615Sbill struct	biobuf *tout, *dout, *trout, *drout, *sout, *strout;
313615Sbill 
314615Sbill /*
315615Sbill  * Offset is the current offset in the string file.
316615Sbill  * Its initial value reflects the fact that we will
317615Sbill  * eventually stuff the size of the string table at the
318615Sbill  * beginning of the string table (i.e. offset itself!).
319615Sbill  */
320615Sbill off_t	offset = sizeof (off_t);
321615Sbill 
322615Sbill int	ofilfnd;		/* -o given; otherwise move l.out to a.out */
323615Sbill char	*ofilename = "l.out";
3243606Ssklower int	ofilemode;		/* respect umask even for unsucessful ld's */
325615Sbill int	infil;			/* current input file descriptor */
326615Sbill char	*filname;		/* and its name */
327615Sbill 
32817133Ssam #define	NDIRS	25
32917133Ssam char	*dirs[NDIRS];		/* directories for library search */
33017133Ssam int	ndir;			/* number of directories */
33117133Ssam 
332615Sbill /*
333615Sbill  * Base of the string table of the current module (pass1 and pass2).
334615Sbill  */
335615Sbill char	*curstr;
336615Sbill 
33712671Ssam /*
33812671Ssam  * System software page size, as returned by getpagesize.
33912671Ssam  */
34012671Ssam int	pagesize;
34112671Ssam 
342615Sbill char 	get();
343615Sbill int	delexit();
344615Sbill char	*savestr();
34517133Ssam char	*malloc();
346615Sbill 
347615Sbill main(argc, argv)
348615Sbill char **argv;
349615Sbill {
350615Sbill 	register int c, i;
351615Sbill 	int num;
352615Sbill 	register char *ap, **p;
353615Sbill 	char save;
354615Sbill 
355650Sbill 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
356615Sbill 		signal(SIGINT, delexit);
357650Sbill 		signal(SIGTERM, delexit);
358650Sbill 	}
359615Sbill 	if (argc == 1)
360615Sbill 		exit(4);
36112671Ssam 	pagesize = getpagesize();
362615Sbill 
36317133Ssam 	/*
36417133Ssam 	 * Pull out search directories.
36517133Ssam 	 */
36617133Ssam 	for (c = 1; c < argc; c++) {
36717133Ssam 		ap = argv[c];
36817133Ssam 		if (ap[0] == '-' && ap[1] == 'L') {
36917133Ssam 			if (ap[2] == 0)
37017133Ssam 				error(1, "-L: pathname missing");
37117133Ssam 			if (ndir >= NDIRS)
37217133Ssam 				error(1, "-L: too many directories");
37317133Ssam 			dirs[ndir++] = &ap[2];
37417133Ssam 		}
37517133Ssam 	}
37617133Ssam 	/* add default search directories */
37717133Ssam 	dirs[ndir++] = "/lib";
37817133Ssam 	dirs[ndir++] = "/usr/lib";
37917133Ssam 	dirs[ndir++] = "/usr/local/lib";
38017133Ssam 
38117133Ssam 	p = argv+1;
382650Sbill 	/*
383650Sbill 	 * Scan files once to find where symbols are defined.
384650Sbill 	 */
385615Sbill 	for (c=1; c<argc; c++) {
386615Sbill 		if (trace)
387615Sbill 			printf("%s:\n", *p);
388615Sbill 		filname = 0;
389615Sbill 		ap = *p++;
390615Sbill 		if (*ap != '-') {
391615Sbill 			load1arg(ap);
392615Sbill 			continue;
393615Sbill 		}
394615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
395615Sbill 
396615Sbill 		case 'o':
397615Sbill 			if (++c >= argc)
398615Sbill 				error(1, "-o where?");
399615Sbill 			ofilename = *p++;
400615Sbill 			ofilfnd++;
401615Sbill 			continue;
402615Sbill 		case 'u':
403615Sbill 		case 'e':
404615Sbill 			if (++c >= argc)
405615Sbill 				error(1, "-u or -c: arg missing");
406615Sbill 			enter(slookup(*p++));
407615Sbill 			if (ap[i]=='e')
408615Sbill 				entrypt = lastsym;
409615Sbill 			continue;
410615Sbill 		case 'H':
411615Sbill 			if (++c >= argc)
412615Sbill 				error(1, "-H: arg missing");
413615Sbill 			if (tsize!=0)
414615Sbill 				error(1, "-H: too late, some text already loaded");
415615Sbill 			hsize = atoi(*p++);
416615Sbill 			continue;
417615Sbill 		case 'A':
418615Sbill 			if (++c >= argc)
419615Sbill 				error(1, "-A: arg missing");
420615Sbill 			if (Aflag)
421615Sbill 				error(1, "-A: only one base file allowed");
422615Sbill 			Aflag = 1;
423615Sbill 			nflag = 0;
424615Sbill 			funding = 1;
425615Sbill 			load1arg(*p++);
426615Sbill 			trsize = drsize = tsize = dsize = bsize = 0;
427615Sbill 			ctrel = cdrel = cbrel = 0;
428615Sbill 			funding = 0;
429615Sbill 			addsym = nextsym;
430615Sbill 			continue;
431615Sbill 		case 'D':
432615Sbill 			if (++c >= argc)
433615Sbill 				error(1, "-D: arg missing");
434615Sbill 			num = htoi(*p++);
435615Sbill 			if (dsize > num)
436615Sbill 				error(1, "-D: too small");
437615Sbill 			dsize = num;
438615Sbill 			continue;
439615Sbill 		case 'T':
440615Sbill 			if (++c >= argc)
441615Sbill 				error(1, "-T: arg missing");
442615Sbill 			if (tsize!=0)
443615Sbill 				error(1, "-T: too late, some text already loaded");
444615Sbill 			textbase = htoi(*p++);
445615Sbill 			continue;
446615Sbill 		case 'l':
447615Sbill 			save = ap[--i];
448615Sbill 			ap[i]='-';
449615Sbill 			load1arg(&ap[i]);
450615Sbill 			ap[i]=save;
451615Sbill 			goto next;
452898Sbill 		case 'M':
453898Sbill 			Mflag++;
454898Sbill 			continue;
455615Sbill 		case 'x':
456615Sbill 			xflag++;
457615Sbill 			continue;
458615Sbill 		case 'X':
459615Sbill 			Xflag++;
460615Sbill 			continue;
461615Sbill 		case 'S':
462615Sbill 			Sflag++;
463615Sbill 			continue;
464615Sbill 		case 'r':
465615Sbill 			rflag++;
466615Sbill 			arflag++;
467615Sbill 			continue;
468615Sbill 		case 's':
469615Sbill 			sflag++;
470615Sbill 			xflag++;
471615Sbill 			continue;
472615Sbill 		case 'n':
473615Sbill 			nflag++;
474650Sbill 			Nflag = zflag = 0;
475615Sbill 			continue;
476615Sbill 		case 'N':
477650Sbill 			Nflag++;
478650Sbill 			nflag = zflag = 0;
479615Sbill 			continue;
480615Sbill 		case 'd':
481615Sbill 			dflag++;
482615Sbill 			continue;
483615Sbill 		case 'i':
484615Sbill 			printf("ld: -i ignored\n");
485615Sbill 			continue;
486615Sbill 		case 't':
487615Sbill 			trace++;
488615Sbill 			continue;
489898Sbill 		case 'y':
490898Sbill 			if (ap[i+1] == 0)
491898Sbill 				error(1, "-y: symbol name missing");
492898Sbill 			if (yflag == 0) {
493898Sbill 				ytab = (char **)calloc(argc, sizeof (char **));
494898Sbill 				if (ytab == 0)
495898Sbill 					error(1, "ran out of memory (-y)");
496898Sbill 			}
497898Sbill 			ytab[yflag++] = &ap[i+1];
498898Sbill 			goto next;
499615Sbill 		case 'z':
500615Sbill 			zflag++;
501650Sbill 			Nflag = nflag = 0;
502615Sbill 			continue;
50317133Ssam 		case 'L':
50417133Ssam 			goto next;
505615Sbill 		default:
506615Sbill 			filname = savestr("-x");	/* kludge */
507615Sbill 			filname[1] = ap[i];		/* kludge */
508615Sbill 			archdr.ar_name[0] = 0;		/* kludge */
509615Sbill 			error(1, "bad flag");
510615Sbill 		}
511615Sbill next:
512615Sbill 		;
513615Sbill 	}
514650Sbill 	if (rflag == 0 && Nflag == 0 && nflag == 0)
515650Sbill 		zflag++;
516615Sbill 	endload(argc, argv);
517615Sbill 	exit(0);
518615Sbill }
519615Sbill 
520615Sbill /*
521615Sbill  * Convert a ascii string which is a hex number.
522615Sbill  * Used by -T and -D options.
523615Sbill  */
524615Sbill htoi(p)
525615Sbill 	register char *p;
526615Sbill {
527615Sbill 	register int c, n;
528615Sbill 
529615Sbill 	n = 0;
530615Sbill 	while (c = *p++) {
531615Sbill 		n <<= 4;
532615Sbill 		if (isdigit(c))
533615Sbill 			n += c - '0';
534615Sbill 		else if (c >= 'a' && c <= 'f')
535615Sbill 			n += 10 + (c - 'a');
536615Sbill 		else if (c >= 'A' && c <= 'F')
537615Sbill 			n += 10 + (c - 'A');
538615Sbill 		else
539615Sbill 			error(1, "badly formed hex number");
540615Sbill 	}
541615Sbill 	return (n);
542615Sbill }
543615Sbill 
544615Sbill delexit()
545615Sbill {
5469332Smckusick 	struct stat stbuf;
5479332Smckusick 	long size;
5489332Smckusick 	char c = 0;
549615Sbill 
550615Sbill 	bflush();
551615Sbill 	unlink("l.out");
5529332Smckusick 	/*
5539332Smckusick 	 * We have to insure that the last block of the data segment
55416068Sralph 	 * is allocated a full pagesize block. If the underlying
55516068Sralph 	 * file system allocates frags that are smaller than pagesize,
55616068Sralph 	 * a full zero filled pagesize block needs to be allocated so
5579332Smckusick 	 * that when it is demand paged, the paged in block will be
5589332Smckusick 	 * appropriately filled with zeros.
5599332Smckusick 	 */
5609332Smckusick 	fstat(biofd, &stbuf);
56116068Sralph 	size = round(stbuf.st_size, pagesize);
56210640Smckusick 	if (!rflag && size > stbuf.st_size) {
5639332Smckusick 		lseek(biofd, size - 1, 0);
56425419Sbloom 		if (write(biofd, &c, 1) != 1)
56525419Sbloom 			delarg |= 4;
5669332Smckusick 	}
56725419Sbloom 	if (delarg==0 && Aflag==0)
56825419Sbloom 		(void) chmod(ofilename, ofilemode);
569615Sbill 	exit (delarg);
570615Sbill }
571615Sbill 
572615Sbill endload(argc, argv)
573615Sbill 	int argc;
574615Sbill 	char **argv;
575615Sbill {
576615Sbill 	register int c, i;
577615Sbill 	long dnum;
578615Sbill 	register char *ap, **p;
579615Sbill 
580615Sbill 	clibseg = libseg;
581615Sbill 	filname = 0;
582615Sbill 	middle();
583615Sbill 	setupout();
584615Sbill 	p = argv+1;
585615Sbill 	for (c=1; c<argc; c++) {
586615Sbill 		ap = *p++;
587615Sbill 		if (trace)
588615Sbill 			printf("%s:\n", ap);
589615Sbill 		if (*ap != '-') {
590615Sbill 			load2arg(ap);
591615Sbill 			continue;
592615Sbill 		}
593615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
594615Sbill 
595615Sbill 		case 'D':
596615Sbill 			dnum = htoi(*p);
597615Sbill 			if (dorigin < dnum)
598615Sbill 				while (dorigin < dnum)
599615Sbill 					bputc(0, dout), dorigin++;
600615Sbill 			/* fall into ... */
601615Sbill 		case 'T':
602615Sbill 		case 'u':
603615Sbill 		case 'e':
604615Sbill 		case 'o':
605615Sbill 		case 'H':
606615Sbill 			++c;
607615Sbill 			++p;
608615Sbill 			/* fall into ... */
609615Sbill 		default:
610615Sbill 			continue;
611615Sbill 		case 'A':
612615Sbill 			funding = 1;
613615Sbill 			load2arg(*p++);
614615Sbill 			funding = 0;
615615Sbill 			c++;
616615Sbill 			continue;
617898Sbill 		case 'y':
61817133Ssam 		case 'L':
619898Sbill 			goto next;
620615Sbill 		case 'l':
621615Sbill 			ap[--i]='-';
622615Sbill 			load2arg(&ap[i]);
623615Sbill 			goto next;
624615Sbill 		}
625615Sbill next:
626615Sbill 		;
627615Sbill 	}
628615Sbill 	finishout();
629615Sbill }
630615Sbill 
631615Sbill /*
632615Sbill  * Scan file to find defined symbols.
633615Sbill  */
634615Sbill load1arg(cp)
635615Sbill 	register char *cp;
636615Sbill {
637615Sbill 	register struct ranlib *tp;
638615Sbill 	off_t nloc;
639898Sbill 	int kind;
640615Sbill 
641898Sbill 	kind = getfile(cp);
642898Sbill 	if (Mflag)
643898Sbill 		printf("%s\n", filname);
644898Sbill 	switch (kind) {
645615Sbill 
646615Sbill 	/*
647615Sbill 	 * Plain file.
648615Sbill 	 */
649615Sbill 	case 0:
650615Sbill 		load1(0, 0L);
651615Sbill 		break;
652615Sbill 
653615Sbill 	/*
654615Sbill 	 * Archive without table of contents.
655615Sbill 	 * (Slowly) process each member.
656615Sbill 	 */
657615Sbill 	case 1:
658898Sbill 		error(-1,
659898Sbill "warning: archive has no table of contents; add one using ranlib(1)");
660615Sbill 		nloc = SARMAG;
661615Sbill 		while (step(nloc))
662615Sbill 			nloc += sizeof(archdr) +
663615Sbill 			    round(atol(archdr.ar_size), sizeof (short));
664615Sbill 		break;
665615Sbill 
666615Sbill 	/*
667615Sbill 	 * Archive with table of contents.
668615Sbill 	 * Read the table of contents and its associated string table.
669615Sbill 	 * Pass through the library resolving symbols until nothing changes
670615Sbill 	 * for an entire pass (i.e. you can get away with backward references
671615Sbill 	 * when there is a table of contents!)
672615Sbill 	 */
673615Sbill 	case 2:
674615Sbill 		nloc = SARMAG + sizeof (archdr);
675615Sbill 		dseek(&text, nloc, sizeof (tnum));
676615Sbill 		mget((char *)&tnum, sizeof (tnum), &text);
677615Sbill 		nloc += sizeof (tnum);
678615Sbill 		tab = (struct ranlib *)malloc(tnum);
679615Sbill 		if (tab == 0)
680615Sbill 			error(1, "ran out of memory (toc)");
681615Sbill 		dseek(&text, nloc, tnum);
682615Sbill 		mget((char *)tab, tnum, &text);
683615Sbill 		nloc += tnum;
684615Sbill 		tnum /= sizeof (struct ranlib);
685615Sbill 		dseek(&text, nloc, sizeof (ssiz));
686615Sbill 		mget((char *)&ssiz, sizeof (ssiz), &text);
687615Sbill 		nloc += sizeof (ssiz);
688615Sbill 		tabstr = (char *)malloc(ssiz);
689615Sbill 		if (tabstr == 0)
690615Sbill 			error(1, "ran out of memory (tocstr)");
691615Sbill 		dseek(&text, nloc, ssiz);
692615Sbill 		mget((char *)tabstr, ssiz, &text);
693615Sbill 		for (tp = &tab[tnum]; --tp >= tab;) {
694615Sbill 			if (tp->ran_un.ran_strx < 0 ||
695615Sbill 			    tp->ran_un.ran_strx >= ssiz)
696615Sbill 				error(1, "mangled archive table of contents");
697615Sbill 			tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx;
698615Sbill 		}
699615Sbill 		while (ldrand())
700615Sbill 			continue;
701*25483Slepreau 		free((char *)tab);
702*25483Slepreau 		free(tabstr);
703615Sbill 		nextlibp(-1);
704615Sbill 		break;
705615Sbill 
706615Sbill 	/*
707615Sbill 	 * Table of contents is out of date, so search
708615Sbill 	 * as a normal library (but skip the __.SYMDEF file).
709615Sbill 	 */
710615Sbill 	case 3:
711898Sbill 		error(-1,
712898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)");
713615Sbill 		nloc = SARMAG;
714615Sbill 		do
715615Sbill 			nloc += sizeof(archdr) +
716615Sbill 			    round(atol(archdr.ar_size), sizeof(short));
717615Sbill 		while (step(nloc));
718615Sbill 		break;
719615Sbill 	}
720615Sbill 	close(infil);
721615Sbill }
722615Sbill 
723615Sbill /*
724615Sbill  * Advance to the next archive member, which
725615Sbill  * is at offset nloc in the archive.  If the member
726615Sbill  * is useful, record its location in the liblist structure
727615Sbill  * for use in pass2.  Mark the end of the archive in libilst with a -1.
728615Sbill  */
729615Sbill step(nloc)
730615Sbill 	off_t nloc;
731615Sbill {
732615Sbill 
733615Sbill 	dseek(&text, nloc, (long) sizeof archdr);
734615Sbill 	if (text.size <= 0) {
735615Sbill 		nextlibp(-1);
736615Sbill 		return (0);
737615Sbill 	}
738615Sbill 	getarhdr();
739615Sbill 	if (load1(1, nloc + (sizeof archdr)))
740615Sbill 		nextlibp(nloc);
741615Sbill 	return (1);
742615Sbill }
743615Sbill 
744615Sbill /*
745615Sbill  * Record the location of a useful archive member.
746615Sbill  * Recording -1 marks the end of files from an archive.
747615Sbill  * The liblist data structure is dynamically extended here.
748615Sbill  */
749615Sbill nextlibp(val)
750615Sbill 	off_t val;
751615Sbill {
752615Sbill 
753615Sbill 	if (clibseg->li_used == NROUT) {
754615Sbill 		if (++clibseg == &libseg[NSEG])
755615Sbill 			error(1, "too many files loaded from libraries");
756615Sbill 		clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t));
757615Sbill 		if (clibseg->li_first == 0)
758615Sbill 			error(1, "ran out of memory (nextlibp)");
759615Sbill 	}
760615Sbill 	clibseg->li_first[clibseg->li_used++] = val;
761898Sbill 	if (val != -1 && Mflag)
762898Sbill 		printf("\t%s\n", archdr.ar_name);
763615Sbill }
764615Sbill 
765615Sbill /*
766615Sbill  * One pass over an archive with a table of contents.
767615Sbill  * Remember the number of symbols currently defined,
768615Sbill  * then call step on members which look promising (i.e.
769615Sbill  * that define a symbol which is currently externally undefined).
770615Sbill  * Indicate to our caller whether this process netted any more symbols.
771615Sbill  */
772615Sbill ldrand()
773615Sbill {
774615Sbill 	register struct nlist *sp, **hp;
775615Sbill 	register struct ranlib *tp, *tplast;
776615Sbill 	off_t loc;
777615Sbill 	int nsymt = symx(nextsym);
778615Sbill 
779615Sbill 	tplast = &tab[tnum-1];
780615Sbill 	for (tp = tab; tp <= tplast; tp++) {
781*25483Slepreau 		if ((hp = slookup(tp->ran_un.ran_name)) == 0 || *hp == 0)
782615Sbill 			continue;
783615Sbill 		sp = *hp;
784615Sbill 		if (sp->n_type != N_EXT+N_UNDF)
785615Sbill 			continue;
786615Sbill 		step(tp->ran_off);
787615Sbill 		loc = tp->ran_off;
788615Sbill 		while (tp < tplast && (tp+1)->ran_off == loc)
789615Sbill 			tp++;
790615Sbill 	}
791615Sbill 	return (symx(nextsym) != nsymt);
792615Sbill }
793615Sbill 
794615Sbill /*
795615Sbill  * Examine a single file or archive member on pass 1.
796615Sbill  */
797615Sbill load1(libflg, loc)
798615Sbill 	off_t loc;
799615Sbill {
800615Sbill 	register struct nlist *sp;
801615Sbill 	struct nlist *savnext;
802615Sbill 	int ndef, nlocal, type, size, nsymt;
803615Sbill 	register int i;
804615Sbill 	off_t maxoff;
805615Sbill 	struct stat stb;
806615Sbill 
807615Sbill 	readhdr(loc);
808615Sbill 	if (filhdr.a_syms == 0) {
809615Sbill 		if (filhdr.a_text+filhdr.a_data == 0)
810615Sbill 			return (0);
811615Sbill 		error(1, "no namelist");
812615Sbill 	}
813615Sbill 	if (libflg)
814615Sbill 		maxoff = atol(archdr.ar_size);
815615Sbill 	else {
816615Sbill 		fstat(infil, &stb);
817615Sbill 		maxoff = stb.st_size;
818615Sbill 	}
819615Sbill 	if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff)
820615Sbill 		error(1, "too small (old format .o?)");
821615Sbill 	ctrel = tsize; cdrel += dsize; cbrel += bsize;
822615Sbill 	ndef = 0;
823615Sbill 	nlocal = sizeof(cursym);
824615Sbill 	savnext = nextsym;
825615Sbill 	loc += N_SYMOFF(filhdr);
826615Sbill 	dseek(&text, loc, filhdr.a_syms);
827615Sbill 	dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t));
828615Sbill 	mget(&size, sizeof (size), &reloc);
829615Sbill 	dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t));
830615Sbill 	curstr = (char *)malloc(size);
831615Sbill 	if (curstr == NULL)
832615Sbill 		error(1, "no space for string table");
833615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc);
834615Sbill 	while (text.size > 0) {
835615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
836615Sbill 		if (cursym.n_un.n_strx) {
837615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
838615Sbill 			    cursym.n_un.n_strx>=size)
839615Sbill 				error(1, "bad string table index (pass 1)");
840615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
841615Sbill 		}
842615Sbill 		type = cursym.n_type;
843615Sbill 		if ((type&N_EXT)==0) {
844615Sbill 			if (Xflag==0 || cursym.n_un.n_name[0]!='L' ||
845615Sbill 			    type & N_STAB)
846615Sbill 				nlocal += sizeof cursym;
847615Sbill 			continue;
848615Sbill 		}
849615Sbill 		symreloc();
850615Sbill 		if (enter(lookup()))
851615Sbill 			continue;
852615Sbill 		if ((sp = lastsym)->n_type != N_EXT+N_UNDF)
853615Sbill 			continue;
854615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
855615Sbill 			if (cursym.n_value > sp->n_value)
856615Sbill 				sp->n_value = cursym.n_value;
857615Sbill 			continue;
858615Sbill 		}
859615Sbill 		if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT)
860615Sbill 			continue;
861615Sbill 		ndef++;
862615Sbill 		sp->n_type = cursym.n_type;
863615Sbill 		sp->n_value = cursym.n_value;
864615Sbill 	}
865615Sbill 	if (libflg==0 || ndef) {
866615Sbill 		tsize += filhdr.a_text;
867615Sbill 		dsize += round(filhdr.a_data, sizeof (long));
868615Sbill 		bsize += round(filhdr.a_bss, sizeof (long));
869615Sbill 		ssize += nlocal;
870615Sbill 		trsize += filhdr.a_trsize;
871615Sbill 		drsize += filhdr.a_drsize;
872615Sbill 		if (funding)
873615Sbill 			textbase = (*slookup("_end"))->n_value;
874615Sbill 		nsymt = symx(nextsym);
875615Sbill 		for (i = symx(savnext); i < nsymt; i++) {
876615Sbill 			sp = xsym(i);
877615Sbill 			sp->n_un.n_name = savestr(sp->n_un.n_name);
878615Sbill 		}
879615Sbill 		free(curstr);
880615Sbill 		return (1);
881615Sbill 	}
882615Sbill 	/*
883615Sbill 	 * No symbols defined by this library member.
884615Sbill 	 * Rip out the hash table entries and reset the symbol table.
885615Sbill 	 */
886615Sbill 	symfree(savnext);
887615Sbill 	free(curstr);
888615Sbill 	return(0);
889615Sbill }
890615Sbill 
891615Sbill middle()
892615Sbill {
893615Sbill 	register struct nlist *sp;
894615Sbill 	long csize, t, corigin, ocsize;
895615Sbill 	int nund, rnd;
896615Sbill 	char s;
897615Sbill 	register int i;
898615Sbill 	int nsymt;
899615Sbill 
900615Sbill 	torigin = 0;
901615Sbill 	dorigin = 0;
902615Sbill 	borigin = 0;
903615Sbill 
904615Sbill 	p_etext = *slookup("_etext");
905615Sbill 	p_edata = *slookup("_edata");
906615Sbill 	p_end = *slookup("_end");
907615Sbill 	/*
908615Sbill 	 * If there are any undefined symbols, save the relocation bits.
909615Sbill 	 */
910615Sbill 	nsymt = symx(nextsym);
911615Sbill 	if (rflag==0) {
912615Sbill 		for (i = 0; i < nsymt; i++) {
913615Sbill 			sp = xsym(i);
914615Sbill 			if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 &&
915650Sbill 			    sp!=p_end && sp!=p_edata && sp!=p_etext) {
916615Sbill 				rflag++;
917615Sbill 				dflag = 0;
918615Sbill 				break;
919615Sbill 			}
920615Sbill 		}
921615Sbill 	}
922615Sbill 	if (rflag)
923615Sbill 		sflag = zflag = 0;
924615Sbill 	/*
925615Sbill 	 * Assign common locations.
926615Sbill 	 */
927615Sbill 	csize = 0;
928615Sbill 	if (!Aflag)
929615Sbill 		addsym = symseg[0].sy_first;
930615Sbill 	database = round(tsize+textbase,
93112671Ssam 	    (nflag||zflag? pagesize : sizeof (long)));
932615Sbill 	database += hsize;
933615Sbill 	if (dflag || rflag==0) {
934615Sbill 		ldrsym(p_etext, tsize, N_EXT+N_TEXT);
935615Sbill 		ldrsym(p_edata, dsize, N_EXT+N_DATA);
936615Sbill 		ldrsym(p_end, bsize, N_EXT+N_BSS);
937615Sbill 		for (i = symx(addsym); i < nsymt; i++) {
938615Sbill 			sp = xsym(i);
939615Sbill 			if ((s=sp->n_type)==N_EXT+N_UNDF &&
940615Sbill 			    (t = sp->n_value)!=0) {
941615Sbill 				if (t >= sizeof (double))
942615Sbill 					rnd = sizeof (double);
943615Sbill 				else if (t >= sizeof (long))
944615Sbill 					rnd = sizeof (long);
945615Sbill 				else
946615Sbill 					rnd = sizeof (short);
947615Sbill 				csize = round(csize, rnd);
948615Sbill 				sp->n_value = csize;
949615Sbill 				sp->n_type = N_EXT+N_COMM;
950615Sbill 				ocsize = csize;
951615Sbill 				csize += t;
952615Sbill 			}
953615Sbill 			if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) {
954615Sbill 				sp->n_value = ocsize;
955615Sbill 				sp->n_type = (s&N_STAB) | (N_EXT+N_COMM);
956615Sbill 			}
957615Sbill 		}
958615Sbill 	}
959615Sbill 	/*
960615Sbill 	 * Now set symbols to their final value
961615Sbill 	 */
962615Sbill 	csize = round(csize, sizeof (long));
963615Sbill 	torigin = textbase;
964615Sbill 	dorigin = database;
965615Sbill 	corigin = dorigin + dsize;
966615Sbill 	borigin = corigin + csize;
967615Sbill 	nund = 0;
968615Sbill 	nsymt = symx(nextsym);
969615Sbill 	for (i = symx(addsym); i<nsymt; i++) {
970615Sbill 		sp = xsym(i);
971615Sbill 		switch (sp->n_type & (N_TYPE+N_EXT)) {
972615Sbill 
973615Sbill 		case N_EXT+N_UNDF:
9742369Skre 			if (arflag == 0)
9752369Skre 				errlev |= 01;
976615Sbill 			if ((arflag==0 || dflag) && sp->n_value==0) {
977650Sbill 				if (sp==p_end || sp==p_etext || sp==p_edata)
978650Sbill 					continue;
979615Sbill 				if (nund==0)
980615Sbill 					printf("Undefined:\n");
981615Sbill 				nund++;
982615Sbill 				printf("%s\n", sp->n_un.n_name);
983615Sbill 			}
984615Sbill 			continue;
985615Sbill 		case N_EXT+N_ABS:
986615Sbill 		default:
987615Sbill 			continue;
988615Sbill 		case N_EXT+N_TEXT:
989615Sbill 			sp->n_value += torigin;
990615Sbill 			continue;
991615Sbill 		case N_EXT+N_DATA:
992615Sbill 			sp->n_value += dorigin;
993615Sbill 			continue;
994615Sbill 		case N_EXT+N_BSS:
995615Sbill 			sp->n_value += borigin;
996615Sbill 			continue;
997615Sbill 		case N_EXT+N_COMM:
998615Sbill 			sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS);
999615Sbill 			sp->n_value += corigin;
1000615Sbill 			continue;
1001615Sbill 		}
1002615Sbill 	}
1003615Sbill 	if (sflag || xflag)
1004615Sbill 		ssize = 0;
1005615Sbill 	bsize += csize;
1006615Sbill 	nsym = ssize / (sizeof cursym);
1007615Sbill 	if (Aflag) {
1008615Sbill 		fixspec(p_etext,torigin);
1009615Sbill 		fixspec(p_edata,dorigin);
1010615Sbill 		fixspec(p_end,borigin);
1011615Sbill 	}
1012615Sbill }
1013615Sbill 
1014615Sbill fixspec(sym,offset)
1015615Sbill 	struct nlist *sym;
1016615Sbill 	long offset;
1017615Sbill {
1018615Sbill 
1019615Sbill 	if(symx(sym) < symx(addsym) && sym!=0)
1020615Sbill 		sym->n_value += offset;
1021615Sbill }
1022615Sbill 
1023615Sbill ldrsym(sp, val, type)
1024615Sbill 	register struct nlist *sp;
1025615Sbill 	long val;
1026615Sbill {
1027615Sbill 
1028615Sbill 	if (sp == 0)
1029615Sbill 		return;
1030615Sbill 	if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) {
1031615Sbill 		printf("%s: ", sp->n_un.n_name);
1032615Sbill 		error(0, "user attempt to redfine loader-defined symbol");
1033615Sbill 		return;
1034615Sbill 	}
1035615Sbill 	sp->n_type = type;
1036615Sbill 	sp->n_value = val;
1037615Sbill }
1038615Sbill 
1039615Sbill off_t	wroff;
1040615Sbill struct	biobuf toutb;
1041615Sbill 
1042615Sbill setupout()
1043615Sbill {
1044615Sbill 	int bss;
104516068Sralph 	struct stat stbuf;
1046898Sbill 	extern char *sys_errlist[];
1047898Sbill 	extern int errno;
1048615Sbill 
10493606Ssklower 	ofilemode = 0777 & ~umask(0);
10503606Ssklower 	biofd = creat(ofilename, 0666 & ofilemode);
1051898Sbill 	if (biofd < 0) {
1052898Sbill 		filname = ofilename;		/* kludge */
1053898Sbill 		archdr.ar_name[0] = 0;		/* kludge */
1054898Sbill 		error(1, sys_errlist[errno]);	/* kludge */
1055898Sbill 	}
105616068Sralph 	fstat(biofd, &stbuf);		/* suppose file exists, wrong*/
105716068Sralph 	if (stbuf.st_mode & 0111) {	/* mode, ld fails? */
105816068Sralph 		chmod(ofilename, stbuf.st_mode & 0666);
105916068Sralph 		ofilemode = stbuf.st_mode;
106016068Sralph 	}
1061615Sbill 	filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC);
1062615Sbill 	filhdr.a_text = nflag ? tsize :
106312671Ssam 	    round(tsize, zflag ? pagesize : sizeof (long));
106412671Ssam 	filhdr.a_data = zflag ? round(dsize, pagesize) : dsize;
1065615Sbill 	bss = bsize - (filhdr.a_data - dsize);
1066615Sbill 	if (bss < 0)
1067615Sbill 		bss = 0;
1068615Sbill 	filhdr.a_bss = bss;
1069615Sbill 	filhdr.a_trsize = trsize;
1070615Sbill 	filhdr.a_drsize = drsize;
1071615Sbill 	filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym));
1072615Sbill 	if (entrypt) {
1073615Sbill 		if (entrypt->n_type!=N_EXT+N_TEXT)
1074615Sbill 			error(0, "entry point not in text");
1075615Sbill 		else
1076615Sbill 			filhdr.a_entry = entrypt->n_value;
1077615Sbill 	} else
1078615Sbill 		filhdr.a_entry = 0;
1079615Sbill 	filhdr.a_trsize = (rflag ? trsize:0);
1080615Sbill 	filhdr.a_drsize = (rflag ? drsize:0);
108116068Sralph 	tout = &toutb;
108216068Sralph 	bopen(tout, 0, stbuf.st_blksize);
1083615Sbill 	bwrite((char *)&filhdr, sizeof (filhdr), tout);
108416068Sralph 	if (zflag)
108516068Sralph 		bseek(tout, pagesize);
1086615Sbill 	wroff = N_TXTOFF(filhdr) + filhdr.a_text;
108716068Sralph 	outb(&dout, filhdr.a_data, stbuf.st_blksize);
1088615Sbill 	if (rflag) {
108916068Sralph 		outb(&trout, filhdr.a_trsize, stbuf.st_blksize);
109016068Sralph 		outb(&drout, filhdr.a_drsize, stbuf.st_blksize);
1091615Sbill 	}
1092615Sbill 	if (sflag==0 || xflag==0) {
109316068Sralph 		outb(&sout, filhdr.a_syms, stbuf.st_blksize);
1094615Sbill 		wroff += sizeof (offset);
109516068Sralph 		outb(&strout, 0, stbuf.st_blksize);
1096615Sbill 	}
1097615Sbill }
1098615Sbill 
109916068Sralph outb(bp, inc, bufsize)
1100615Sbill 	register struct biobuf **bp;
1101615Sbill {
1102615Sbill 
1103615Sbill 	*bp = (struct biobuf *)malloc(sizeof (struct biobuf));
1104615Sbill 	if (*bp == 0)
1105615Sbill 		error(1, "ran out of memory (outb)");
110616068Sralph 	bopen(*bp, wroff, bufsize);
1107615Sbill 	wroff += inc;
1108615Sbill }
1109615Sbill 
1110615Sbill load2arg(acp)
1111615Sbill char *acp;
1112615Sbill {
1113615Sbill 	register char *cp;
1114615Sbill 	off_t loc;
1115615Sbill 
1116615Sbill 	cp = acp;
1117615Sbill 	if (getfile(cp) == 0) {
1118615Sbill 		while (*cp)
1119615Sbill 			cp++;
1120615Sbill 		while (cp >= acp && *--cp != '/');
1121615Sbill 		mkfsym(++cp);
1122615Sbill 		load2(0L);
1123615Sbill 	} else {	/* scan archive members referenced */
1124615Sbill 		for (;;) {
1125615Sbill 			if (clibseg->li_used2 == clibseg->li_used) {
1126615Sbill 				if (clibseg->li_used < NROUT)
1127615Sbill 					error(1, "libseg botch");
1128615Sbill 				clibseg++;
1129615Sbill 			}
1130615Sbill 			loc = clibseg->li_first[clibseg->li_used2++];
1131615Sbill 			if (loc == -1)
1132615Sbill 				break;
1133615Sbill 			dseek(&text, loc, (long)sizeof(archdr));
1134615Sbill 			getarhdr();
1135615Sbill 			mkfsym(archdr.ar_name);
1136615Sbill 			load2(loc + (long)sizeof(archdr));
1137615Sbill 		}
1138615Sbill 	}
1139615Sbill 	close(infil);
1140615Sbill }
1141615Sbill 
1142615Sbill load2(loc)
1143615Sbill long loc;
1144615Sbill {
1145615Sbill 	int size;
1146615Sbill 	register struct nlist *sp;
1147615Sbill 	register struct local *lp;
1148615Sbill 	register int symno, i;
1149615Sbill 	int type;
1150615Sbill 
1151615Sbill 	readhdr(loc);
1152650Sbill 	if (!funding) {
1153615Sbill 		ctrel = torigin;
1154615Sbill 		cdrel += dorigin;
1155615Sbill 		cbrel += borigin;
1156615Sbill 	}
1157615Sbill 	/*
1158615Sbill 	 * Reread the symbol table, recording the numbering
1159615Sbill 	 * of symbols for fixing external references.
1160615Sbill 	 */
1161615Sbill 	for (i = 0; i < LHSIZ; i++)
1162615Sbill 		lochash[i] = 0;
1163615Sbill 	clocseg = locseg;
1164615Sbill 	clocseg->lo_used = 0;
1165615Sbill 	symno = -1;
1166615Sbill 	loc += N_TXTOFF(filhdr);
1167615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1168615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t));
1169615Sbill 	mget(&size, sizeof(size), &text);
1170615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1171615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t),
1172615Sbill 		size - sizeof(off_t));
1173615Sbill 	curstr = (char *)malloc(size);
1174615Sbill 	if (curstr == NULL)
1175615Sbill 		error(1, "out of space reading string table (pass 2)");
1176615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &text);
1177615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1178615Sbill 		filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms);
1179615Sbill 	while (text.size > 0) {
1180615Sbill 		symno++;
1181615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
1182615Sbill 		if (cursym.n_un.n_strx) {
1183615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
1184615Sbill 			    cursym.n_un.n_strx>=size)
1185615Sbill 				error(1, "bad string table index (pass 2)");
1186615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
1187615Sbill 		}
1188615Sbill /* inline expansion of symreloc() */
1189615Sbill 		switch (cursym.n_type & 017) {
1190615Sbill 
1191615Sbill 		case N_TEXT:
1192615Sbill 		case N_EXT+N_TEXT:
1193615Sbill 			cursym.n_value += ctrel;
1194615Sbill 			break;
1195615Sbill 		case N_DATA:
1196615Sbill 		case N_EXT+N_DATA:
1197615Sbill 			cursym.n_value += cdrel;
1198615Sbill 			break;
1199615Sbill 		case N_BSS:
1200615Sbill 		case N_EXT+N_BSS:
1201615Sbill 			cursym.n_value += cbrel;
1202615Sbill 			break;
1203615Sbill 		case N_EXT+N_UNDF:
1204615Sbill 			break;
1205615Sbill 		default:
1206615Sbill 			if (cursym.n_type&N_EXT)
1207615Sbill 				cursym.n_type = N_EXT+N_ABS;
1208615Sbill 		}
1209615Sbill /* end inline expansion of symreloc() */
1210615Sbill 		type = cursym.n_type;
1211898Sbill 		if (yflag && cursym.n_un.n_name)
1212898Sbill 			for (i = 0; i < yflag; i++)
1213898Sbill 				/* fast check for 2d character! */
1214898Sbill 				if (ytab[i][1] == cursym.n_un.n_name[1] &&
1215898Sbill 				    !strcmp(ytab[i], cursym.n_un.n_name)) {
1216898Sbill 					tracesym();
1217898Sbill 					break;
1218898Sbill 				}
1219615Sbill 		if ((type&N_EXT) == 0) {
1220615Sbill 			if (!sflag&&!xflag&&
1221615Sbill 			    (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB))
1222615Sbill 				symwrite(&cursym, sout);
1223615Sbill 			continue;
1224615Sbill 		}
1225615Sbill 		if (funding)
1226615Sbill 			continue;
1227615Sbill 		if ((sp = *lookup()) == 0)
1228615Sbill 			error(1, "internal error: symbol not found");
1229615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
1230615Sbill 			if (clocseg->lo_used == NSYMPR) {
1231615Sbill 				if (++clocseg == &locseg[NSEG])
1232615Sbill 					error(1, "local symbol overflow");
1233615Sbill 				clocseg->lo_used = 0;
1234615Sbill 			}
1235615Sbill 			if (clocseg->lo_first == 0) {
1236615Sbill 				clocseg->lo_first = (struct local *)
1237615Sbill 				    malloc(NSYMPR * sizeof (struct local));
1238615Sbill 				if (clocseg->lo_first == 0)
1239615Sbill 					error(1, "out of memory (clocseg)");
1240615Sbill 			}
1241615Sbill 			lp = &clocseg->lo_first[clocseg->lo_used++];
1242615Sbill 			lp->l_index = symno;
1243615Sbill 			lp->l_symbol = sp;
1244615Sbill 			lp->l_link = lochash[symno % LHSIZ];
1245615Sbill 			lochash[symno % LHSIZ] = lp;
1246615Sbill 			continue;
1247615Sbill 		}
1248615Sbill 		if (cursym.n_type & N_STAB)
1249615Sbill 			continue;
1250615Sbill 		if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) {
1251615Sbill 			printf("%s: ", cursym.n_un.n_name);
1252615Sbill 			error(0, "multiply defined");
1253615Sbill 		}
1254615Sbill 	}
1255615Sbill 	if (funding)
1256615Sbill 		return;
1257615Sbill 	dseek(&text, loc, filhdr.a_text);
1258615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize);
1259650Sbill 	load2td(ctrel, torigin - textbase, tout, trout);
1260615Sbill 	dseek(&text, loc+filhdr.a_text, filhdr.a_data);
1261615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize,
1262615Sbill 	    filhdr.a_drsize);
1263650Sbill 	load2td(cdrel, dorigin - database, dout, drout);
1264615Sbill 	while (filhdr.a_data & (sizeof(long)-1)) {
1265615Sbill 		bputc(0, dout);
1266615Sbill 		filhdr.a_data++;
1267615Sbill 	}
1268615Sbill 	torigin += filhdr.a_text;
12691752Sbill 	dorigin += round(filhdr.a_data, sizeof (long));
12701752Sbill 	borigin += round(filhdr.a_bss, sizeof (long));
1271615Sbill 	free(curstr);
1272615Sbill }
1273615Sbill 
1274898Sbill struct tynames {
1275898Sbill 	int	ty_value;
1276898Sbill 	char	*ty_name;
1277898Sbill } tynames[] = {
1278898Sbill 	N_UNDF,	"undefined",
1279898Sbill 	N_ABS,	"absolute",
1280898Sbill 	N_TEXT,	"text",
1281898Sbill 	N_DATA,	"data",
1282898Sbill 	N_BSS,	"bss",
1283898Sbill 	N_COMM,	"common",
1284898Sbill 	0,	0,
1285898Sbill };
1286898Sbill 
1287898Sbill tracesym()
1288898Sbill {
1289898Sbill 	register struct tynames *tp;
1290898Sbill 
1291898Sbill 	if (cursym.n_type & N_STAB)
1292898Sbill 		return;
1293898Sbill 	printf("%s", filname);
1294898Sbill 	if (archdr.ar_name[0])
1295898Sbill 		printf("(%s)", archdr.ar_name);
1296898Sbill 	printf(": ");
1297898Sbill 	if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) {
1298898Sbill 		printf("definition of common %s size %d\n",
1299898Sbill 		    cursym.n_un.n_name, cursym.n_value);
1300898Sbill 		return;
1301898Sbill 	}
1302898Sbill 	for (tp = tynames; tp->ty_name; tp++)
1303898Sbill 		if (tp->ty_value == (cursym.n_type&N_TYPE))
1304898Sbill 			break;
1305898Sbill 	printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to");
1306898Sbill 	if (cursym.n_type&N_EXT)
1307898Sbill 		printf(" external");
1308898Sbill 	if (tp->ty_name)
1309898Sbill 		printf(" %s", tp->ty_name);
1310898Sbill 	printf(" %s\n", cursym.n_un.n_name);
1311898Sbill }
1312898Sbill 
1313650Sbill /*
1314650Sbill  * This routine relocates the single text or data segment argument.
1315650Sbill  * Offsets from external symbols are resolved by adding the value
1316650Sbill  * of the external symbols.  Non-external reference are updated to account
1317650Sbill  * for the relative motion of the segments (ctrel, cdrel, ...).  If
1318650Sbill  * a relocation was pc-relative, then we update it to reflect the
1319650Sbill  * change in the positioning of the segments by adding the displacement
1320650Sbill  * of the referenced segment and subtracting the displacement of the
1321650Sbill  * current segment (creloc).
1322650Sbill  *
1323650Sbill  * If we are saving the relocation information, then we increase
1324650Sbill  * each relocation datum address by our base position in the new segment.
1325650Sbill  */
1326650Sbill load2td(creloc, position, b1, b2)
1327650Sbill 	long creloc, offset;
1328615Sbill 	struct biobuf *b1, *b2;
1329615Sbill {
1330615Sbill 	register struct nlist *sp;
1331615Sbill 	register struct local *lp;
1332615Sbill 	long tw;
1333615Sbill 	register struct relocation_info *rp, *rpend;
1334615Sbill 	struct relocation_info *relp;
1335615Sbill 	char *codep;
1336615Sbill 	register char *cp;
1337615Sbill 	int relsz, codesz;
1338615Sbill 
1339615Sbill 	relsz = reloc.size;
1340615Sbill 	relp = (struct relocation_info *)malloc(relsz);
1341615Sbill 	codesz = text.size;
1342615Sbill 	codep = (char *)malloc(codesz);
1343615Sbill 	if (relp == 0 || codep == 0)
1344615Sbill 		error(1, "out of memory (load2td)");
1345615Sbill 	mget((char *)relp, relsz, &reloc);
1346615Sbill 	rpend = &relp[relsz / sizeof (struct relocation_info)];
1347615Sbill 	mget(codep, codesz, &text);
1348615Sbill 	for (rp = relp; rp < rpend; rp++) {
1349615Sbill 		cp = codep + rp->r_address;
1350650Sbill 		/*
1351650Sbill 		 * Pick up previous value at location to be relocated.
1352650Sbill 		 */
1353615Sbill 		switch (rp->r_length) {
1354615Sbill 
1355615Sbill 		case 0:		/* byte */
1356615Sbill 			tw = *cp;
1357615Sbill 			break;
1358615Sbill 
1359615Sbill 		case 1:		/* word */
1360615Sbill 			tw = *(short *)cp;
1361615Sbill 			break;
1362615Sbill 
1363615Sbill 		case 2:		/* long */
1364615Sbill 			tw = *(long *)cp;
1365615Sbill 			break;
1366615Sbill 
1367615Sbill 		default:
1368615Sbill 			error(1, "load2td botch: bad length");
1369615Sbill 		}
1370650Sbill 		/*
1371650Sbill 		 * If relative to an external which is defined,
1372650Sbill 		 * resolve to a simpler kind of reference in the
1373650Sbill 		 * result file.  If the external is undefined, just
1374650Sbill 		 * convert the symbol number to the number of the
1375650Sbill 		 * symbol in the result file and leave it undefined.
1376650Sbill 		 */
1377615Sbill 		if (rp->r_extern) {
1378650Sbill 			/*
1379650Sbill 			 * Search the hash table which maps local
1380650Sbill 			 * symbol numbers to symbol tables entries
1381650Sbill 			 * in the new a.out file.
1382650Sbill 			 */
1383615Sbill 			lp = lochash[rp->r_symbolnum % LHSIZ];
1384615Sbill 			while (lp->l_index != rp->r_symbolnum) {
1385615Sbill 				lp = lp->l_link;
1386615Sbill 				if (lp == 0)
1387615Sbill 					error(1, "local symbol botch");
1388615Sbill 			}
1389615Sbill 			sp = lp->l_symbol;
1390615Sbill 			if (sp->n_type == N_EXT+N_UNDF)
1391615Sbill 				rp->r_symbolnum = nsym+symx(sp);
1392615Sbill 			else {
1393615Sbill 				rp->r_symbolnum = sp->n_type & N_TYPE;
1394615Sbill 				tw += sp->n_value;
1395615Sbill 				rp->r_extern = 0;
1396615Sbill 			}
1397615Sbill 		} else switch (rp->r_symbolnum & N_TYPE) {
1398650Sbill 		/*
1399650Sbill 		 * Relocation is relative to the loaded position
1400650Sbill 		 * of another segment.  Update by the change in position
1401650Sbill 		 * of that segment.
1402650Sbill 		 */
1403615Sbill 		case N_TEXT:
1404615Sbill 			tw += ctrel;
1405615Sbill 			break;
1406615Sbill 		case N_DATA:
1407615Sbill 			tw += cdrel;
1408615Sbill 			break;
1409615Sbill 		case N_BSS:
1410615Sbill 			tw += cbrel;
1411615Sbill 			break;
1412615Sbill 		case N_ABS:
1413615Sbill 			break;
1414615Sbill 		default:
1415615Sbill 			error(1, "relocation format botch (symbol type))");
1416615Sbill 		}
1417650Sbill 		/*
1418650Sbill 		 * Relocation is pc relative, so decrease the relocation
1419650Sbill 		 * by the amount the current segment is displaced.
1420650Sbill 		 * (E.g if we are a relative reference to a text location
1421650Sbill 		 * from data space, we added the increase in the text address
1422650Sbill 		 * above, and subtract the increase in our (data) address
1423650Sbill 		 * here, leaving the net change the relative change in the
1424650Sbill 		 * positioning of our text and data segments.)
1425650Sbill 		 */
1426615Sbill 		if (rp->r_pcrel)
1427615Sbill 			tw -= creloc;
1428650Sbill 		/*
1429650Sbill 		 * Put the value back in the segment,
1430650Sbill 		 * while checking for overflow.
1431650Sbill 		 */
1432615Sbill 		switch (rp->r_length) {
1433615Sbill 
1434615Sbill 		case 0:		/* byte */
1435615Sbill 			if (tw < -128 || tw > 127)
1436615Sbill 				error(0, "byte displacement overflow");
1437615Sbill 			*cp = tw;
1438615Sbill 			break;
1439615Sbill 		case 1:		/* word */
1440615Sbill 			if (tw < -32768 || tw > 32767)
1441615Sbill 				error(0, "word displacement overflow");
1442615Sbill 			*(short *)cp = tw;
1443615Sbill 			break;
1444615Sbill 		case 2:		/* long */
1445615Sbill 			*(long *)cp = tw;
1446615Sbill 			break;
1447615Sbill 		}
1448650Sbill 		/*
1449650Sbill 		 * If we are saving relocation information,
1450650Sbill 		 * we must convert the address in the segment from
1451650Sbill 		 * the old .o file into an address in the segment in
1452650Sbill 		 * the new a.out, by adding the position of our
1453650Sbill 		 * segment in the new larger segment.
1454650Sbill 		 */
1455615Sbill 		if (rflag)
1456650Sbill 			rp->r_address += position;
1457615Sbill 	}
1458615Sbill 	bwrite(codep, codesz, b1);
1459615Sbill 	if (rflag)
1460615Sbill 		bwrite(relp, relsz, b2);
1461*25483Slepreau 	free((char *)relp);
1462*25483Slepreau 	free(codep);
1463615Sbill }
1464615Sbill 
1465615Sbill finishout()
1466615Sbill {
1467615Sbill 	register int i;
1468615Sbill 	int nsymt;
1469615Sbill 
1470615Sbill 	if (sflag==0) {
1471615Sbill 		nsymt = symx(nextsym);
1472615Sbill 		for (i = 0; i < nsymt; i++)
1473615Sbill 			symwrite(xsym(i), sout);
1474615Sbill 		bwrite(&offset, sizeof offset, sout);
1475615Sbill 	}
1476615Sbill 	if (!ofilfnd) {
1477615Sbill 		unlink("a.out");
1478898Sbill 		if (link("l.out", "a.out") < 0)
1479898Sbill 			error(1, "cannot move l.out to a.out");
1480615Sbill 		ofilename = "a.out";
1481615Sbill 	}
1482615Sbill 	delarg = errlev;
1483615Sbill 	delexit();
1484615Sbill }
1485615Sbill 
1486615Sbill mkfsym(s)
1487615Sbill char *s;
1488615Sbill {
1489615Sbill 
1490615Sbill 	if (sflag || xflag)
1491615Sbill 		return;
1492615Sbill 	cursym.n_un.n_name = s;
1493615Sbill 	cursym.n_type = N_TEXT;
1494615Sbill 	cursym.n_value = torigin;
1495615Sbill 	symwrite(&cursym, sout);
1496615Sbill }
1497615Sbill 
1498615Sbill getarhdr()
1499615Sbill {
1500615Sbill 	register char *cp;
1501615Sbill 
1502615Sbill 	mget((char *)&archdr, sizeof archdr, &text);
1503615Sbill 	for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];)
1504615Sbill 		if (*cp++ == ' ') {
1505615Sbill 			cp[-1] = 0;
1506615Sbill 			return;
1507615Sbill 		}
1508615Sbill }
1509615Sbill 
1510615Sbill mget(loc, n, sp)
1511615Sbill register STREAM *sp;
1512615Sbill register char *loc;
1513615Sbill {
1514615Sbill 	register char *p;
1515615Sbill 	register int take;
1516615Sbill 
1517615Sbill top:
1518615Sbill 	if (n == 0)
1519615Sbill 		return;
1520615Sbill 	if (sp->size && sp->nibuf) {
1521615Sbill 		p = sp->ptr;
1522615Sbill 		take = sp->size;
1523615Sbill 		if (take > sp->nibuf)
1524615Sbill 			take = sp->nibuf;
1525615Sbill 		if (take > n)
1526615Sbill 			take = n;
1527615Sbill 		n -= take;
1528615Sbill 		sp->size -= take;
1529615Sbill 		sp->nibuf -= take;
1530615Sbill 		sp->pos += take;
1531615Sbill 		do
1532615Sbill 			*loc++ = *p++;
1533615Sbill 		while (--take > 0);
1534615Sbill 		sp->ptr = p;
1535615Sbill 		goto top;
1536615Sbill 	}
153716068Sralph 	if (n > p_blksize) {
153816068Sralph 		take = n - n % p_blksize;
153916068Sralph 		lseek(infil, (sp->bno+1)<<p_blkshift, 0);
1540615Sbill 		if (take > sp->size || read(infil, loc, take) != take)
1541615Sbill 			error(1, "premature EOF");
1542615Sbill 		loc += take;
1543615Sbill 		n -= take;
1544615Sbill 		sp->size -= take;
1545615Sbill 		sp->pos += take;
154616068Sralph 		dseek(sp, (sp->bno+1+(take>>p_blkshift))<<p_blkshift, -1);
1547615Sbill 		goto top;
1548615Sbill 	}
1549615Sbill 	*loc++ = get(sp);
1550615Sbill 	--n;
1551615Sbill 	goto top;
1552615Sbill }
1553615Sbill 
1554615Sbill symwrite(sp, bp)
1555615Sbill 	struct nlist *sp;
1556615Sbill 	struct biobuf *bp;
1557615Sbill {
1558615Sbill 	register int len;
1559615Sbill 	register char *str;
1560615Sbill 
1561615Sbill 	str = sp->n_un.n_name;
1562615Sbill 	if (str) {
1563615Sbill 		sp->n_un.n_strx = offset;
1564615Sbill 		len = strlen(str) + 1;
1565615Sbill 		bwrite(str, len, strout);
1566615Sbill 		offset += len;
1567615Sbill 	}
1568615Sbill 	bwrite(sp, sizeof (*sp), bp);
1569615Sbill 	sp->n_un.n_name = str;
1570615Sbill }
1571615Sbill 
1572615Sbill dseek(sp, loc, s)
1573615Sbill register STREAM *sp;
1574615Sbill long loc, s;
1575615Sbill {
1576615Sbill 	register PAGE *p;
1577615Sbill 	register b, o;
1578615Sbill 	int n;
1579615Sbill 
158016068Sralph 	b = loc>>p_blkshift;
158116068Sralph 	o = loc&p_blkmask;
1582615Sbill 	if (o&01)
1583615Sbill 		error(1, "loader error; odd offset");
1584615Sbill 	--sp->pno->nuser;
1585615Sbill 	if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b)
1586615Sbill 		if (p->nuser==0 || (p = &page[0])->nuser==0) {
1587615Sbill 			if (page[0].nuser==0 && page[1].nuser==0)
1588615Sbill 				if (page[0].bno < page[1].bno)
1589615Sbill 					p = &page[0];
1590615Sbill 			p->bno = b;
159116068Sralph 			lseek(infil, loc & ~(long)p_blkmask, 0);
159216068Sralph 			if ((n = read(infil, p->buff, p_blksize)) < 0)
1593615Sbill 				n = 0;
1594615Sbill 			p->nibuf = n;
159516068Sralph 		} else
159616068Sralph 			error(1, "botch: no pages");
1597615Sbill 	++p->nuser;
1598615Sbill 	sp->bno = b;
1599615Sbill 	sp->pno = p;
1600615Sbill 	if (s != -1) {sp->size = s; sp->pos = 0;}
1601615Sbill 	sp->ptr = (char *)(p->buff + o);
1602615Sbill 	if ((sp->nibuf = p->nibuf-o) <= 0)
1603615Sbill 		sp->size = 0;
1604615Sbill }
1605615Sbill 
1606615Sbill char
1607615Sbill get(asp)
1608615Sbill STREAM *asp;
1609615Sbill {
1610615Sbill 	register STREAM *sp;
1611615Sbill 
1612615Sbill 	sp = asp;
1613615Sbill 	if ((sp->nibuf -= sizeof(char)) < 0) {
161416068Sralph 		dseek(sp, ((long)(sp->bno+1)<<p_blkshift), (long)-1);
1615615Sbill 		sp->nibuf -= sizeof(char);
1616615Sbill 	}
1617615Sbill 	if ((sp->size -= sizeof(char)) <= 0) {
1618615Sbill 		if (sp->size < 0)
1619615Sbill 			error(1, "premature EOF");
1620615Sbill 		++fpage.nuser;
1621615Sbill 		--sp->pno->nuser;
1622615Sbill 		sp->pno = (PAGE *) &fpage;
1623615Sbill 	}
1624615Sbill 	sp->pos += sizeof(char);
1625615Sbill 	return(*sp->ptr++);
1626615Sbill }
1627615Sbill 
1628615Sbill getfile(acp)
1629615Sbill char *acp;
1630615Sbill {
1631615Sbill 	register int c;
1632615Sbill 	char arcmag[SARMAG+1];
1633615Sbill 	struct stat stb;
1634615Sbill 
1635615Sbill 	archdr.ar_name[0] = '\0';
163617133Ssam 	filname = acp;
163717133Ssam 	if (filname[0] == '-' && filname[1] == 'l')
163817133Ssam 		infil = libopen(filname + 2, O_RDONLY);
163917133Ssam 	else
164017133Ssam 		infil = open(filname, O_RDONLY);
164117133Ssam 	if (infil < 0)
1642615Sbill 		error(1, "cannot open");
164316068Sralph 	fstat(infil, &stb);
1644615Sbill 	page[0].bno = page[1].bno = -1;
1645615Sbill 	page[0].nuser = page[1].nuser = 0;
164616068Sralph 	c = stb.st_blksize;
164716068Sralph 	if (c == 0 || (c & (c - 1)) != 0) {
164816068Sralph 		/* use default size if not a power of two */
164916068Sralph 		c = BLKSIZE;
165016068Sralph 	}
165116068Sralph 	if (p_blksize != c) {
165216068Sralph 		p_blksize = c;
165316068Sralph 		p_blkmask = c - 1;
165416068Sralph 		for (p_blkshift = 0; c > 1 ; p_blkshift++)
165516068Sralph 			c >>= 1;
165616068Sralph 		if (page[0].buff != NULL)
165716068Sralph 			free(page[0].buff);
165816068Sralph 		page[0].buff = (char *)malloc(p_blksize);
165916068Sralph 		if (page[0].buff == NULL)
166016068Sralph 			error(1, "ran out of memory (getfile)");
166116068Sralph 		if (page[1].buff != NULL)
166216068Sralph 			free(page[1].buff);
166316068Sralph 		page[1].buff = (char *)malloc(p_blksize);
166416068Sralph 		if (page[1].buff == NULL)
166516068Sralph 			error(1, "ran out of memory (getfile)");
166616068Sralph 	}
1667615Sbill 	text.pno = reloc.pno = (PAGE *) &fpage;
1668615Sbill 	fpage.nuser = 2;
1669615Sbill 	dseek(&text, 0L, SARMAG);
1670615Sbill 	if (text.size <= 0)
1671615Sbill 		error(1, "premature EOF");
1672615Sbill 	mget((char *)arcmag, SARMAG, &text);
1673615Sbill 	arcmag[SARMAG] = 0;
1674615Sbill 	if (strcmp(arcmag, ARMAG))
1675615Sbill 		return (0);
1676615Sbill 	dseek(&text, SARMAG, sizeof archdr);
167717133Ssam 	if (text.size <= 0)
1678615Sbill 		return (1);
1679615Sbill 	getarhdr();
1680615Sbill 	if (strncmp(archdr.ar_name, "__.SYMDEF", sizeof(archdr.ar_name)) != 0)
1681615Sbill 		return (1);
1682615Sbill 	return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2);
1683615Sbill }
1684615Sbill 
168517133Ssam /*
168617133Ssam  * Search for a library with given name
168717133Ssam  * using the directory search array.
168817133Ssam  */
168917133Ssam libopen(name, oflags)
169017133Ssam 	char *name;
169117133Ssam 	int oflags;
169217133Ssam {
169317133Ssam 	register char *p, *cp;
169417133Ssam 	register int i;
169517133Ssam 	static char buf[MAXPATHLEN+1];
169617133Ssam 	int fd = -1;
169717133Ssam 
169817133Ssam 	if (*name == '\0')			/* backwards compat */
169917133Ssam 		name = "a";
170017133Ssam 	for (i = 0; i < ndir && fd == -1; i++) {
170117133Ssam 		p = buf;
170217133Ssam 		for (cp = dirs[i]; *cp; *p++ = *cp++)
170317133Ssam 			;
170417133Ssam 		*p++ = '/';
170517133Ssam 		for (cp = "lib"; *cp; *p++ = *cp++)
170617133Ssam 			;
170717133Ssam 		for (cp = name; *cp; *p++ = *cp++)
170817133Ssam 			;
170917133Ssam 		cp = ".a";
171017133Ssam 		while (*p++ = *cp++)
171117133Ssam 			;
171217133Ssam 		fd = open(buf, oflags);
171317133Ssam 	}
171417133Ssam 	if (fd != -1)
171517133Ssam 		filname = buf;
171617133Ssam 	return (fd);
171717133Ssam }
171817133Ssam 
1719615Sbill struct nlist **
1720615Sbill lookup()
1721615Sbill {
1722615Sbill 	register int sh;
1723615Sbill 	register struct nlist **hp;
1724615Sbill 	register char *cp, *cp1;
1725615Sbill 	register struct symseg *gp;
1726615Sbill 	register int i;
1727615Sbill 
1728615Sbill 	sh = 0;
1729615Sbill 	for (cp = cursym.n_un.n_name; *cp;)
1730615Sbill 		sh = (sh<<1) + *cp++;
1731615Sbill 	sh = (sh & 0x7fffffff) % HSIZE;
1732615Sbill 	for (gp = symseg; gp < &symseg[NSEG]; gp++) {
1733615Sbill 		if (gp->sy_first == 0) {
1734615Sbill 			gp->sy_first = (struct nlist *)
1735615Sbill 			    calloc(NSYM, sizeof (struct nlist));
1736615Sbill 			gp->sy_hfirst = (struct nlist **)
1737615Sbill 			    calloc(HSIZE, sizeof (struct nlist *));
1738615Sbill 			if (gp->sy_first == 0 || gp->sy_hfirst == 0)
1739615Sbill 				error(1, "ran out of space for symbol table");
1740615Sbill 			gp->sy_last = gp->sy_first + NSYM;
1741615Sbill 			gp->sy_hlast = gp->sy_hfirst + HSIZE;
1742615Sbill 		}
1743615Sbill 		if (gp > csymseg)
1744615Sbill 			csymseg = gp;
1745615Sbill 		hp = gp->sy_hfirst + sh;
1746615Sbill 		i = 1;
1747615Sbill 		do {
1748615Sbill 			if (*hp == 0) {
1749615Sbill 				if (gp->sy_used == NSYM)
1750615Sbill 					break;
1751615Sbill 				return (hp);
1752615Sbill 			}
1753615Sbill 			cp1 = (*hp)->n_un.n_name;
1754615Sbill 			for (cp = cursym.n_un.n_name; *cp == *cp1++;)
1755615Sbill 				if (*cp++ == 0)
1756615Sbill 					return (hp);
1757615Sbill 			hp += i;
1758615Sbill 			i += 2;
1759615Sbill 			if (hp >= gp->sy_hlast)
1760615Sbill 				hp -= HSIZE;
1761615Sbill 		} while (i < HSIZE);
1762615Sbill 		if (i > HSIZE)
1763615Sbill 			error(1, "hash table botch");
1764615Sbill 	}
1765615Sbill 	error(1, "symbol table overflow");
1766615Sbill 	/*NOTREACHED*/
1767615Sbill }
1768615Sbill 
1769615Sbill symfree(saved)
1770615Sbill 	struct nlist *saved;
1771615Sbill {
1772615Sbill 	register struct symseg *gp;
1773615Sbill 	register struct nlist *sp;
1774615Sbill 
1775615Sbill 	for (gp = csymseg; gp >= symseg; gp--, csymseg--) {
1776615Sbill 		sp = gp->sy_first + gp->sy_used;
1777615Sbill 		if (sp == saved) {
1778615Sbill 			nextsym = sp;
1779615Sbill 			return;
1780615Sbill 		}
1781615Sbill 		for (sp--; sp >= gp->sy_first; sp--) {
1782615Sbill 			gp->sy_hfirst[sp->n_hash] = 0;
1783615Sbill 			gp->sy_used--;
1784615Sbill 			if (sp == saved) {
1785615Sbill 				nextsym = sp;
1786615Sbill 				return;
1787615Sbill 			}
1788615Sbill 		}
1789615Sbill 	}
1790615Sbill 	if (saved == 0)
1791615Sbill 		return;
1792615Sbill 	error(1, "symfree botch");
1793615Sbill }
1794615Sbill 
1795615Sbill struct nlist **
1796615Sbill slookup(s)
1797615Sbill 	char *s;
1798615Sbill {
1799615Sbill 
1800615Sbill 	cursym.n_un.n_name = s;
1801615Sbill 	cursym.n_type = N_EXT+N_UNDF;
1802615Sbill 	cursym.n_value = 0;
1803615Sbill 	return (lookup());
1804615Sbill }
1805615Sbill 
1806615Sbill enter(hp)
1807615Sbill register struct nlist **hp;
1808615Sbill {
1809615Sbill 	register struct nlist *sp;
1810615Sbill 
1811615Sbill 	if (*hp==0) {
1812615Sbill 		if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast)
1813615Sbill 			error(1, "enter botch");
1814615Sbill 		*hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used;
1815615Sbill 		csymseg->sy_used++;
1816615Sbill 		sp->n_un.n_name = cursym.n_un.n_name;
1817615Sbill 		sp->n_type = cursym.n_type;
1818615Sbill 		sp->n_hash = hp - csymseg->sy_hfirst;
1819615Sbill 		sp->n_value = cursym.n_value;
1820615Sbill 		nextsym = lastsym + 1;
1821615Sbill 		return(1);
1822615Sbill 	} else {
1823615Sbill 		lastsym = *hp;
1824615Sbill 		return(0);
1825615Sbill 	}
1826615Sbill }
1827615Sbill 
1828615Sbill symx(sp)
1829615Sbill 	struct nlist *sp;
1830615Sbill {
1831615Sbill 	register struct symseg *gp;
1832615Sbill 
1833615Sbill 	if (sp == 0)
1834615Sbill 		return (0);
1835615Sbill 	for (gp = csymseg; gp >= symseg; gp--)
1836615Sbill 		/* <= is sloppy so nextsym will always work */
1837615Sbill 		if (sp >= gp->sy_first && sp <= gp->sy_last)
1838615Sbill 			return ((gp - symseg) * NSYM + sp - gp->sy_first);
1839615Sbill 	error(1, "symx botch");
1840615Sbill 	/*NOTREACHED*/
1841615Sbill }
1842615Sbill 
1843615Sbill symreloc()
1844615Sbill {
1845615Sbill 	if(funding) return;
1846615Sbill 	switch (cursym.n_type & 017) {
1847615Sbill 
1848615Sbill 	case N_TEXT:
1849615Sbill 	case N_EXT+N_TEXT:
1850615Sbill 		cursym.n_value += ctrel;
1851615Sbill 		return;
1852615Sbill 
1853615Sbill 	case N_DATA:
1854615Sbill 	case N_EXT+N_DATA:
1855615Sbill 		cursym.n_value += cdrel;
1856615Sbill 		return;
1857615Sbill 
1858615Sbill 	case N_BSS:
1859615Sbill 	case N_EXT+N_BSS:
1860615Sbill 		cursym.n_value += cbrel;
1861615Sbill 		return;
1862615Sbill 
1863615Sbill 	case N_EXT+N_UNDF:
1864615Sbill 		return;
1865615Sbill 
1866615Sbill 	default:
1867615Sbill 		if (cursym.n_type&N_EXT)
1868615Sbill 			cursym.n_type = N_EXT+N_ABS;
1869615Sbill 		return;
1870615Sbill 	}
1871615Sbill }
1872615Sbill 
1873615Sbill error(n, s)
1874615Sbill char *s;
1875615Sbill {
1876898Sbill 
1877615Sbill 	if (errlev==0)
1878615Sbill 		printf("ld:");
1879615Sbill 	if (filname) {
1880615Sbill 		printf("%s", filname);
1881615Sbill 		if (n != -1 && archdr.ar_name[0])
1882615Sbill 			printf("(%s)", archdr.ar_name);
1883615Sbill 		printf(": ");
1884615Sbill 	}
1885615Sbill 	printf("%s\n", s);
1886615Sbill 	if (n == -1)
1887615Sbill 		return;
1888615Sbill 	if (n)
1889615Sbill 		delexit();
1890615Sbill 	errlev = 2;
1891615Sbill }
1892615Sbill 
1893615Sbill readhdr(loc)
1894615Sbill off_t loc;
1895615Sbill {
1896615Sbill 
1897615Sbill 	dseek(&text, loc, (long)sizeof(filhdr));
1898615Sbill 	mget((short *)&filhdr, sizeof(filhdr), &text);
1899615Sbill 	if (N_BADMAG(filhdr)) {
1900615Sbill 		if (filhdr.a_magic == OARMAG)
1901615Sbill 			error(1, "old archive");
1902615Sbill 		error(1, "bad magic number");
1903615Sbill 	}
1904615Sbill 	if (filhdr.a_text&01 || filhdr.a_data&01)
1905615Sbill 		error(1, "text/data size odd");
1906615Sbill 	if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) {
190712671Ssam 		cdrel = -round(filhdr.a_text, pagesize);
1908615Sbill 		cbrel = cdrel - filhdr.a_data;
1909615Sbill 	} else if (filhdr.a_magic == OMAGIC) {
1910615Sbill 		cdrel = -filhdr.a_text;
1911615Sbill 		cbrel = cdrel - filhdr.a_data;
1912615Sbill 	} else
1913615Sbill 		error(1, "bad format");
1914615Sbill }
1915615Sbill 
1916615Sbill round(v, r)
1917615Sbill 	int v;
1918615Sbill 	u_long r;
1919615Sbill {
1920615Sbill 
1921615Sbill 	r--;
1922615Sbill 	v += r;
1923615Sbill 	v &= ~(long)r;
1924615Sbill 	return(v);
1925615Sbill }
1926615Sbill 
1927615Sbill #define	NSAVETAB	8192
1928615Sbill char	*savetab;
1929615Sbill int	saveleft;
1930615Sbill 
1931615Sbill char *
1932615Sbill savestr(cp)
1933615Sbill 	register char *cp;
1934615Sbill {
1935615Sbill 	register int len;
1936615Sbill 
1937615Sbill 	len = strlen(cp) + 1;
1938615Sbill 	if (len > saveleft) {
1939615Sbill 		saveleft = NSAVETAB;
1940615Sbill 		if (len > saveleft)
1941615Sbill 			saveleft = len;
194217133Ssam 		savetab = malloc(saveleft);
1943615Sbill 		if (savetab == 0)
1944615Sbill 			error(1, "ran out of memory (savestr)");
1945615Sbill 	}
1946615Sbill 	strncpy(savetab, cp, len);
1947615Sbill 	cp = savetab;
1948615Sbill 	savetab += len;
1949615Sbill 	saveleft -= len;
1950615Sbill 	return (cp);
1951615Sbill }
1952615Sbill 
195316068Sralph bopen(bp, off, bufsize)
195416068Sralph 	register struct biobuf *bp;
1955615Sbill {
1956615Sbill 
195717133Ssam 	bp->b_ptr = bp->b_buf = malloc(bufsize);
195816068Sralph 	if (bp->b_ptr == (char *)0)
195916068Sralph 		error(1, "ran out of memory (bopen)");
196016068Sralph 	bp->b_bufsize = bufsize;
196116068Sralph 	bp->b_nleft = bufsize - (off % bufsize);
1962615Sbill 	bp->b_off = off;
1963615Sbill 	bp->b_link = biobufs;
1964615Sbill 	biobufs = bp;
1965615Sbill }
1966615Sbill 
1967615Sbill int	bwrerror;
1968615Sbill 
1969615Sbill bwrite(p, cnt, bp)
1970615Sbill 	register char *p;
1971615Sbill 	register int cnt;
1972615Sbill 	register struct biobuf *bp;
1973615Sbill {
1974615Sbill 	register int put;
1975615Sbill 	register char *to;
1976615Sbill 
1977615Sbill top:
1978615Sbill 	if (cnt == 0)
1979615Sbill 		return;
1980615Sbill 	if (bp->b_nleft) {
1981615Sbill 		put = bp->b_nleft;
1982615Sbill 		if (put > cnt)
1983615Sbill 			put = cnt;
1984615Sbill 		bp->b_nleft -= put;
1985615Sbill 		to = bp->b_ptr;
198625419Sbloom 		bcopy(p, to, put);
1987615Sbill 		bp->b_ptr += put;
1988615Sbill 		p += put;
1989615Sbill 		cnt -= put;
1990615Sbill 		goto top;
1991615Sbill 	}
199216068Sralph 	if (cnt >= bp->b_bufsize) {
1993615Sbill 		if (bp->b_ptr != bp->b_buf)
1994615Sbill 			bflush1(bp);
199516068Sralph 		put = cnt - cnt % bp->b_bufsize;
1996615Sbill 		if (boffset != bp->b_off)
1997615Sbill 			lseek(biofd, bp->b_off, 0);
1998615Sbill 		if (write(biofd, p, put) != put) {
1999615Sbill 			bwrerror = 1;
2000615Sbill 			error(1, "output write error");
2001615Sbill 		}
2002615Sbill 		bp->b_off += put;
2003615Sbill 		boffset = bp->b_off;
2004615Sbill 		p += put;
2005615Sbill 		cnt -= put;
2006615Sbill 		goto top;
2007615Sbill 	}
2008615Sbill 	bflush1(bp);
2009615Sbill 	goto top;
2010615Sbill }
2011615Sbill 
2012615Sbill bflush()
2013615Sbill {
2014615Sbill 	register struct biobuf *bp;
2015615Sbill 
2016615Sbill 	if (bwrerror)
2017615Sbill 		return;
2018615Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
2019615Sbill 		bflush1(bp);
2020615Sbill }
2021615Sbill 
2022615Sbill bflush1(bp)
2023615Sbill 	register struct biobuf *bp;
2024615Sbill {
2025615Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
2026615Sbill 
2027615Sbill 	if (cnt == 0)
2028615Sbill 		return;
2029615Sbill 	if (boffset != bp->b_off)
2030615Sbill 		lseek(biofd, bp->b_off, 0);
2031615Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
2032615Sbill 		bwrerror = 1;
2033615Sbill 		error(1, "output write error");
2034615Sbill 	}
2035615Sbill 	bp->b_off += cnt;
2036615Sbill 	boffset = bp->b_off;
2037615Sbill 	bp->b_ptr = bp->b_buf;
203816068Sralph 	bp->b_nleft = bp->b_bufsize;
2039615Sbill }
2040615Sbill 
2041615Sbill bflushc(bp, c)
2042615Sbill 	register struct biobuf *bp;
2043615Sbill {
2044615Sbill 
2045615Sbill 	bflush1(bp);
2046615Sbill 	bputc(c, bp);
2047615Sbill }
204816068Sralph 
204916068Sralph bseek(bp, off)
205016068Sralph 	register struct biobuf *bp;
205116068Sralph 	register off_t off;
205216068Sralph {
205316068Sralph 	bflush1(bp);
205416068Sralph 
205516068Sralph 	bp->b_nleft = bp->b_bufsize - (off % bp->b_bufsize);
205616068Sralph 	bp->b_off = off;
205716068Sralph }
2058