xref: /csrg-svn/old/ld/ld.c (revision 37031)
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*37031Sbostic static char sccsid[] = "@(#)ld.c	5.11 (Berkeley) 03/05/89";
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>
30*37031Sbostic #include "pathnames.h"
31615Sbill 
32615Sbill /*
33615Sbill  * Basic strategy:
34615Sbill  *
35615Sbill  * The loader takes a number of files and libraries as arguments.
36615Sbill  * A first pass examines each file in turn.  Normal files are
37615Sbill  * unconditionally loaded, and the (external) symbols they define and require
38615Sbill  * are noted in the symbol table.   Libraries are searched, and the
39615Sbill  * library members which define needed symbols are remembered
40615Sbill  * in a special data structure so they can be selected on the second
41615Sbill  * pass.  Symbols defined and required by library members are also
42615Sbill  * recorded.
43615Sbill  *
44615Sbill  * After the first pass, the loader knows the size of the basic text
45615Sbill  * data, and bss segments from the sum of the sizes of the modules which
46615Sbill  * were required.  It has computed, for each ``common'' symbol, the
47615Sbill  * maximum size of any reference to it, and these symbols are then assigned
48615Sbill  * storage locations after their sizes are appropriately rounded.
49615Sbill  * The loader now knows all sizes for the eventual output file, and
50615Sbill  * can determine the final locations of external symbols before it
51615Sbill  * begins a second pass.
52615Sbill  *
53615Sbill  * On the second pass each normal file and required library member
54615Sbill  * is processed again.  The symbol table for each such file is
55615Sbill  * reread and relevant parts of it are placed in the output.  The offsets
56615Sbill  * in the local symbol table for externally defined symbols are recorded
57615Sbill  * since relocation information refers to symbols in this way.
58615Sbill  * Armed with all necessary information, the text and data segments
59615Sbill  * are relocated and the result is placed in the output file, which
60615Sbill  * is pasted together, ``in place'', by writing to it in several
61615Sbill  * different places concurrently.
62615Sbill  */
63615Sbill 
64615Sbill /*
65615Sbill  * Internal data structures
66615Sbill  *
67615Sbill  * All internal data structures are segmented and dynamically extended.
68615Sbill  * The basic structures hold 1103 (NSYM) symbols, ~~200 (NROUT)
69615Sbill  * referenced library members, and 100 (NSYMPR) private (local) symbols
70615Sbill  * per object module.  For large programs and/or modules, these structures
71615Sbill  * expand to be up to 40 (NSEG) times as large as this as necessary.
72615Sbill  */
73615Sbill #define	NSEG	40		/* Number of segments, each data structure */
74615Sbill #define	NSYM	1103		/* Number of symbols per segment */
75615Sbill #define	NROUT	250		/* Number of library references per segment */
76615Sbill #define	NSYMPR	100		/* Number of private symbols per segment */
77615Sbill 
78615Sbill /*
79615Sbill  * Structure describing each symbol table segment.
80615Sbill  * Each segment has its own hash table.  We record the first
81615Sbill  * address in and first address beyond both the symbol and hash
82615Sbill  * tables, for use in the routine symx and the lookup routine respectively.
83615Sbill  * The symfree routine also understands this structure well as it used
84615Sbill  * to back out symbols from modules we decide that we don't need in pass 1.
85615Sbill  *
86615Sbill  * Csymseg points to the current symbol table segment;
87615Sbill  * csymseg->sy_first[csymseg->sy_used] is the next symbol slot to be allocated,
88615Sbill  * (unless csymseg->sy_used == NSYM in which case we will allocate another
89615Sbill  * symbol table segment first.)
90615Sbill  */
91615Sbill struct	symseg {
92615Sbill 	struct	nlist *sy_first;	/* base of this alloc'ed segment */
93615Sbill 	struct	nlist *sy_last;		/* end of this segment, for n_strx */
94615Sbill 	int	sy_used;		/* symbols used in this seg */
95615Sbill 	struct	nlist **sy_hfirst;	/* base of hash table, this seg */
96615Sbill 	struct	nlist **sy_hlast;	/* end of hash table, this seg */
97615Sbill } symseg[NSEG], *csymseg;
98615Sbill 
99615Sbill /*
100615Sbill  * The lookup routine uses quadratic rehash.  Since a quadratic rehash
101615Sbill  * only probes 1/2 of the buckets in the table, and since the hash
102615Sbill  * table is segmented the same way the symbol table is, we make the
103615Sbill  * hash table have twice as many buckets as there are symbol table slots
104615Sbill  * in the segment.  This guarantees that the quadratic rehash will never
105615Sbill  * fail to find an empty bucket if the segment is not full and the
106615Sbill  * symbol is not there.
107615Sbill  */
108615Sbill #define	HSIZE	(NSYM*2)
109615Sbill 
110615Sbill /*
111615Sbill  * Xsym converts symbol table indices (ala x) into symbol table pointers.
112615Sbill  * Symx (harder, but never used in loops) inverts pointers into the symbol
113615Sbill  * table into indices using the symseg[] structure.
114615Sbill  */
115615Sbill #define	xsym(x)	(symseg[(x)/NSYM].sy_first+((x)%NSYM))
116615Sbill /* symx() is a function, defined below */
117615Sbill 
118615Sbill struct	nlist cursym;		/* current symbol */
119615Sbill struct	nlist *lastsym;		/* last symbol entered */
120615Sbill struct	nlist *nextsym;		/* next available symbol table entry */
121615Sbill struct	nlist *addsym;		/* first sym defined during incr load */
122615Sbill int	nsym;			/* pass2: number of local symbols in a.out */
123615Sbill /* nsym + symx(nextsym) is the symbol table size during pass2 */
124615Sbill 
125615Sbill struct	nlist **lookup(), **slookup();
126650Sbill struct	nlist *p_etext, *p_edata, *p_end, *entrypt;
127615Sbill 
128615Sbill /*
129615Sbill  * Definitions of segmentation for library member table.
130615Sbill  * For each library we encounter on pass 1 we record pointers to all
131615Sbill  * members which we will load on pass 2.  These are recorded as offsets
132615Sbill  * into the archive in the library member table.  Libraries are
133615Sbill  * separated in the table by the special offset value -1.
134615Sbill  */
135615Sbill off_t	li_init[NROUT];
136615Sbill struct	libseg {
137615Sbill 	off_t	*li_first;
138615Sbill 	int	li_used;
139615Sbill 	int	li_used2;
140615Sbill } libseg[NSEG] = {
141615Sbill 	li_init, 0, 0,
142615Sbill }, *clibseg = libseg;
143615Sbill 
144615Sbill /*
145615Sbill  * In processing each module on pass 2 we must relocate references
146615Sbill  * relative to external symbols.  These references are recorded
147615Sbill  * in the relocation information as relative to local symbol numbers
148615Sbill  * assigned to the external symbols when the module was created.
149615Sbill  * Thus before relocating the module in pass 2 we create a table
150615Sbill  * which maps these internal numbers to symbol table entries.
151615Sbill  * A hash table is constructed, based on the local symbol table indices,
152615Sbill  * for quick lookup of these symbols.
153615Sbill  */
154615Sbill #define	LHSIZ	31
155615Sbill struct	local {
156615Sbill 	int	l_index;		/* index to symbol in file */
157615Sbill 	struct	nlist *l_symbol;	/* ptr to symbol table */
158615Sbill 	struct	local *l_link;		/* hash link */
159615Sbill } *lochash[LHSIZ], lhinit[NSYMPR];
160615Sbill struct	locseg {
161615Sbill 	struct	local *lo_first;
162615Sbill 	int	lo_used;
163615Sbill } locseg[NSEG] = {
164615Sbill 	lhinit, 0
165615Sbill }, *clocseg;
166615Sbill 
167615Sbill /*
168615Sbill  * Libraries are typically built with a table of contents,
169615Sbill  * which is the first member of a library with special file
170615Sbill  * name __.SYMDEF and contains a list of symbol names
171615Sbill  * and with each symbol the offset of the library member which defines
172615Sbill  * it.  The loader uses this table to quickly tell which library members
173615Sbill  * are (potentially) useful.  The alternative, examining the symbol
174615Sbill  * table of each library member, is painfully slow for large archives.
175615Sbill  *
176615Sbill  * See <ranlib.h> for the definition of the ranlib structure and an
177615Sbill  * explanation of the __.SYMDEF file format.
178615Sbill  */
179615Sbill int	tnum;		/* number of symbols in table of contents */
180615Sbill int	ssiz;		/* size of string table for table of contents */
181615Sbill struct	ranlib *tab;	/* the table of contents (dynamically allocated) */
182615Sbill char	*tabstr;	/* string table for table of contents */
183615Sbill 
184615Sbill /*
185615Sbill  * We open each input file or library only once, but in pass2 we
186615Sbill  * (historically) read from such a file at 2 different places at the
187615Sbill  * same time.  These structures are remnants from those days,
188650Sbill  * and now serve only to catch ``Premature EOF''.
1896414Smckusic  * In order to make I/O more efficient, we provide routines which
19016068Sralph  * use the optimal block size returned by stat().
191615Sbill  */
1926414Smckusic #define BLKSIZE 1024
193615Sbill typedef struct {
194615Sbill 	short	*fakeptr;
195615Sbill 	int	bno;
196615Sbill 	int	nibuf;
197615Sbill 	int	nuser;
19816068Sralph 	char	*buff;
19916068Sralph 	int	bufsize;
200615Sbill } PAGE;
201615Sbill 
202615Sbill PAGE	page[2];
20316068Sralph int	p_blksize;
20416068Sralph int	p_blkshift;
20516068Sralph int	p_blkmask;
206615Sbill 
207615Sbill struct {
208615Sbill 	short	*fakeptr;
209615Sbill 	int	bno;
210615Sbill 	int	nibuf;
211615Sbill 	int	nuser;
212615Sbill } fpage;
213615Sbill 
214615Sbill typedef struct {
215615Sbill 	char	*ptr;
216615Sbill 	int	bno;
217615Sbill 	int	nibuf;
218615Sbill 	long	size;
219615Sbill 	long	pos;
220615Sbill 	PAGE	*pno;
221615Sbill } STREAM;
222615Sbill 
223615Sbill STREAM	text;
224615Sbill STREAM	reloc;
225615Sbill 
226615Sbill /*
227615Sbill  * Header from the a.out and the archive it is from (if any).
228615Sbill  */
229615Sbill struct	exec filhdr;
230615Sbill struct	ar_hdr archdr;
231615Sbill #define	OARMAG 0177545
232615Sbill 
233615Sbill /*
234615Sbill  * Options.
235615Sbill  */
236615Sbill int	trace;
237615Sbill int	xflag;		/* discard local symbols */
238615Sbill int	Xflag;		/* discard locals starting with 'L' */
239615Sbill int	Sflag;		/* discard all except locals and globals*/
240615Sbill int	rflag;		/* preserve relocation bits, don't define common */
241615Sbill int	arflag;		/* original copy of rflag */
242615Sbill int	sflag;		/* discard all symbols */
243898Sbill int	Mflag;		/* print rudimentary load map */
244615Sbill int	nflag;		/* pure procedure */
245615Sbill int	dflag;		/* define common even with rflag */
246650Sbill int	zflag;		/* demand paged  */
247615Sbill long	hsize;		/* size of hole at beginning of data to be squashed */
248615Sbill int	Aflag;		/* doing incremental load */
249650Sbill int	Nflag;		/* want impure a.out */
250615Sbill int	funding;	/* reading fundamental file for incremental load */
251898Sbill int	yflag;		/* number of symbols to be traced */
252898Sbill char	**ytab;		/* the symbols */
253615Sbill 
254615Sbill /*
255615Sbill  * These are the cumulative sizes, set in pass 1, which
256615Sbill  * appear in the a.out header when the loader is finished.
257615Sbill  */
258615Sbill off_t	tsize, dsize, bsize, trsize, drsize, ssize;
259615Sbill 
260615Sbill /*
261615Sbill  * Symbol relocation: c?rel is a scale factor which is
262615Sbill  * added to an old relocation to convert it to new units;
263615Sbill  * i.e. it is the difference between segment origins.
264650Sbill  * (Thus if we are loading from a data segment which began at location
265650Sbill  * 4 in a .o file into an a.out where it will be loaded starting at
266650Sbill  * 1024, cdrel will be 1020.)
267615Sbill  */
268615Sbill long	ctrel, cdrel, cbrel;
269615Sbill 
270615Sbill /*
271650Sbill  * Textbase is the start address of all text, 0 unless given by -T.
272615Sbill  * Database is the base of all data, computed before and used during pass2.
273650Sbill  */
274650Sbill long	textbase, database;
275650Sbill 
276650Sbill /*
277615Sbill  * The base addresses for the loaded text, data and bss from the
278615Sbill  * current module during pass2 are given by torigin, dorigin and borigin.
279615Sbill  */
280615Sbill long	torigin, dorigin, borigin;
281615Sbill 
282615Sbill /*
283615Sbill  * Errlev is nonzero when errors have occured.
284615Sbill  * Delarg is an implicit argument to the routine delexit
285615Sbill  * which is called on error.  We do ``delarg = errlev'' before normal
286615Sbill  * exits, and only if delarg is 0 (i.e. errlev was 0) do we make the
287615Sbill  * result file executable.
288615Sbill  */
289615Sbill int	errlev;
290615Sbill int	delarg	= 4;
291615Sbill 
292615Sbill /*
293615Sbill  * The biobuf structure and associated routines are used to write
294615Sbill  * into one file at several places concurrently.  Calling bopen
295615Sbill  * with a biobuf structure sets it up to write ``biofd'' starting
296615Sbill  * at the specified offset.  You can then use ``bwrite'' and/or ``bputc''
297615Sbill  * to stuff characters in the stream, much like ``fwrite'' and ``fputc''.
298615Sbill  * Calling bflush drains all the buffers and MUST be done before exit.
299615Sbill  */
300615Sbill struct	biobuf {
301615Sbill 	short	b_nleft;		/* Number free spaces left in b_buf */
30216068Sralph /* Initialize to be less than b_bufsize initially, to boundary align in file */
303615Sbill 	char	*b_ptr;			/* Next place to stuff characters */
30416068Sralph 	char	*b_buf;			/* Pointer to the buffer */
30516068Sralph 	int	b_bufsize;		/* Size of the buffer */
306615Sbill 	off_t	b_off;			/* Current file offset */
307615Sbill 	struct	biobuf *b_link;		/* Link in chain for bflush() */
308615Sbill } *biobufs;
309615Sbill #define	bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \
310615Sbill 		       : bflushc(b, c))
311615Sbill int	biofd;
312615Sbill off_t	boffset;
313615Sbill struct	biobuf *tout, *dout, *trout, *drout, *sout, *strout;
314615Sbill 
315615Sbill /*
316615Sbill  * Offset is the current offset in the string file.
317615Sbill  * Its initial value reflects the fact that we will
318615Sbill  * eventually stuff the size of the string table at the
319615Sbill  * beginning of the string table (i.e. offset itself!).
320615Sbill  */
321615Sbill off_t	offset = sizeof (off_t);
322615Sbill 
323615Sbill int	ofilfnd;		/* -o given; otherwise move l.out to a.out */
324615Sbill char	*ofilename = "l.out";
3253606Ssklower int	ofilemode;		/* respect umask even for unsucessful ld's */
326615Sbill int	infil;			/* current input file descriptor */
327615Sbill char	*filname;		/* and its name */
328615Sbill 
32917133Ssam #define	NDIRS	25
33025533Sbloom #define NDEFDIRS 3		/* number of default directories in dirs[] */
33117133Ssam char	*dirs[NDIRS];		/* directories for library search */
33217133Ssam int	ndir;			/* number of directories */
33317133Ssam 
334615Sbill /*
335615Sbill  * Base of the string table of the current module (pass1 and pass2).
336615Sbill  */
337615Sbill char	*curstr;
338615Sbill 
33912671Ssam /*
34012671Ssam  * System software page size, as returned by getpagesize.
34112671Ssam  */
34212671Ssam int	pagesize;
34312671Ssam 
344615Sbill char 	get();
345615Sbill int	delexit();
346615Sbill char	*savestr();
34717133Ssam char	*malloc();
348615Sbill 
349615Sbill main(argc, argv)
350615Sbill char **argv;
351615Sbill {
352615Sbill 	register int c, i;
353615Sbill 	int num;
354615Sbill 	register char *ap, **p;
355615Sbill 	char save;
356615Sbill 
357650Sbill 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
358615Sbill 		signal(SIGINT, delexit);
359650Sbill 		signal(SIGTERM, delexit);
360650Sbill 	}
361615Sbill 	if (argc == 1)
362615Sbill 		exit(4);
36312671Ssam 	pagesize = getpagesize();
364615Sbill 
36517133Ssam 	/*
36617133Ssam 	 * Pull out search directories.
36717133Ssam 	 */
36817133Ssam 	for (c = 1; c < argc; c++) {
36917133Ssam 		ap = argv[c];
37017133Ssam 		if (ap[0] == '-' && ap[1] == 'L') {
37117133Ssam 			if (ap[2] == 0)
37217133Ssam 				error(1, "-L: pathname missing");
37325533Sbloom 			if (ndir >= NDIRS - NDEFDIRS)
37417133Ssam 				error(1, "-L: too many directories");
37517133Ssam 			dirs[ndir++] = &ap[2];
37617133Ssam 		}
37717133Ssam 	}
37817133Ssam 	/* add default search directories */
379*37031Sbostic 	dirs[ndir++] = _PATH_LIB;
380*37031Sbostic 	dirs[ndir++] = _PATH_USRLIB;
381*37031Sbostic 	dirs[ndir++] = _PATH_LOCALLIB;
38217133Ssam 
38317133Ssam 	p = argv+1;
384650Sbill 	/*
385650Sbill 	 * Scan files once to find where symbols are defined.
386650Sbill 	 */
387615Sbill 	for (c=1; c<argc; c++) {
388615Sbill 		if (trace)
389615Sbill 			printf("%s:\n", *p);
390615Sbill 		filname = 0;
391615Sbill 		ap = *p++;
392615Sbill 		if (*ap != '-') {
393615Sbill 			load1arg(ap);
394615Sbill 			continue;
395615Sbill 		}
396615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
397615Sbill 
398615Sbill 		case 'o':
399615Sbill 			if (++c >= argc)
400615Sbill 				error(1, "-o where?");
401615Sbill 			ofilename = *p++;
402615Sbill 			ofilfnd++;
403615Sbill 			continue;
404615Sbill 		case 'u':
405615Sbill 		case 'e':
406615Sbill 			if (++c >= argc)
40733997Sbostic 				error(1, " -u or -e: arg missing");
408615Sbill 			enter(slookup(*p++));
409615Sbill 			if (ap[i]=='e')
410615Sbill 				entrypt = lastsym;
411615Sbill 			continue;
412615Sbill 		case 'H':
413615Sbill 			if (++c >= argc)
414615Sbill 				error(1, "-H: arg missing");
415615Sbill 			if (tsize!=0)
416615Sbill 				error(1, "-H: too late, some text already loaded");
417615Sbill 			hsize = atoi(*p++);
418615Sbill 			continue;
419615Sbill 		case 'A':
420615Sbill 			if (++c >= argc)
421615Sbill 				error(1, "-A: arg missing");
422615Sbill 			if (Aflag)
423615Sbill 				error(1, "-A: only one base file allowed");
424615Sbill 			Aflag = 1;
425615Sbill 			nflag = 0;
426615Sbill 			funding = 1;
427615Sbill 			load1arg(*p++);
428615Sbill 			trsize = drsize = tsize = dsize = bsize = 0;
429615Sbill 			ctrel = cdrel = cbrel = 0;
430615Sbill 			funding = 0;
431615Sbill 			addsym = nextsym;
432615Sbill 			continue;
433615Sbill 		case 'D':
434615Sbill 			if (++c >= argc)
435615Sbill 				error(1, "-D: arg missing");
436615Sbill 			num = htoi(*p++);
437615Sbill 			if (dsize > num)
438615Sbill 				error(1, "-D: too small");
439615Sbill 			dsize = num;
440615Sbill 			continue;
441615Sbill 		case 'T':
442615Sbill 			if (++c >= argc)
443615Sbill 				error(1, "-T: arg missing");
444615Sbill 			if (tsize!=0)
445615Sbill 				error(1, "-T: too late, some text already loaded");
446615Sbill 			textbase = htoi(*p++);
447615Sbill 			continue;
448615Sbill 		case 'l':
449615Sbill 			save = ap[--i];
450615Sbill 			ap[i]='-';
451615Sbill 			load1arg(&ap[i]);
452615Sbill 			ap[i]=save;
453615Sbill 			goto next;
454898Sbill 		case 'M':
455898Sbill 			Mflag++;
456898Sbill 			continue;
457615Sbill 		case 'x':
458615Sbill 			xflag++;
459615Sbill 			continue;
460615Sbill 		case 'X':
461615Sbill 			Xflag++;
462615Sbill 			continue;
463615Sbill 		case 'S':
464615Sbill 			Sflag++;
465615Sbill 			continue;
466615Sbill 		case 'r':
467615Sbill 			rflag++;
468615Sbill 			arflag++;
469615Sbill 			continue;
470615Sbill 		case 's':
471615Sbill 			sflag++;
472615Sbill 			xflag++;
473615Sbill 			continue;
474615Sbill 		case 'n':
475615Sbill 			nflag++;
476650Sbill 			Nflag = zflag = 0;
477615Sbill 			continue;
478615Sbill 		case 'N':
479650Sbill 			Nflag++;
480650Sbill 			nflag = zflag = 0;
481615Sbill 			continue;
482615Sbill 		case 'd':
483615Sbill 			dflag++;
484615Sbill 			continue;
485615Sbill 		case 'i':
486615Sbill 			printf("ld: -i ignored\n");
487615Sbill 			continue;
488615Sbill 		case 't':
489615Sbill 			trace++;
490615Sbill 			continue;
491898Sbill 		case 'y':
492898Sbill 			if (ap[i+1] == 0)
493898Sbill 				error(1, "-y: symbol name missing");
494898Sbill 			if (yflag == 0) {
495898Sbill 				ytab = (char **)calloc(argc, sizeof (char **));
496898Sbill 				if (ytab == 0)
497898Sbill 					error(1, "ran out of memory (-y)");
498898Sbill 			}
499898Sbill 			ytab[yflag++] = &ap[i+1];
500898Sbill 			goto next;
501615Sbill 		case 'z':
502615Sbill 			zflag++;
503650Sbill 			Nflag = nflag = 0;
504615Sbill 			continue;
50517133Ssam 		case 'L':
50617133Ssam 			goto next;
507615Sbill 		default:
508615Sbill 			filname = savestr("-x");	/* kludge */
509615Sbill 			filname[1] = ap[i];		/* kludge */
510615Sbill 			archdr.ar_name[0] = 0;		/* kludge */
511615Sbill 			error(1, "bad flag");
512615Sbill 		}
513615Sbill next:
514615Sbill 		;
515615Sbill 	}
516650Sbill 	if (rflag == 0 && Nflag == 0 && nflag == 0)
517650Sbill 		zflag++;
518615Sbill 	endload(argc, argv);
519615Sbill 	exit(0);
520615Sbill }
521615Sbill 
522615Sbill /*
523615Sbill  * Convert a ascii string which is a hex number.
524615Sbill  * Used by -T and -D options.
525615Sbill  */
526615Sbill htoi(p)
527615Sbill 	register char *p;
528615Sbill {
529615Sbill 	register int c, n;
530615Sbill 
531615Sbill 	n = 0;
532615Sbill 	while (c = *p++) {
533615Sbill 		n <<= 4;
534615Sbill 		if (isdigit(c))
535615Sbill 			n += c - '0';
536615Sbill 		else if (c >= 'a' && c <= 'f')
537615Sbill 			n += 10 + (c - 'a');
538615Sbill 		else if (c >= 'A' && c <= 'F')
539615Sbill 			n += 10 + (c - 'A');
540615Sbill 		else
541615Sbill 			error(1, "badly formed hex number");
542615Sbill 	}
543615Sbill 	return (n);
544615Sbill }
545615Sbill 
546615Sbill delexit()
547615Sbill {
5489332Smckusick 	struct stat stbuf;
5499332Smckusick 	long size;
5509332Smckusick 	char c = 0;
551615Sbill 
552615Sbill 	bflush();
553615Sbill 	unlink("l.out");
5549332Smckusick 	/*
5559332Smckusick 	 * We have to insure that the last block of the data segment
55616068Sralph 	 * is allocated a full pagesize block. If the underlying
55716068Sralph 	 * file system allocates frags that are smaller than pagesize,
55816068Sralph 	 * a full zero filled pagesize block needs to be allocated so
5599332Smckusick 	 * that when it is demand paged, the paged in block will be
5609332Smckusick 	 * appropriately filled with zeros.
5619332Smckusick 	 */
5629332Smckusick 	fstat(biofd, &stbuf);
56316068Sralph 	size = round(stbuf.st_size, pagesize);
56410640Smckusick 	if (!rflag && size > stbuf.st_size) {
5659332Smckusick 		lseek(biofd, size - 1, 0);
56625419Sbloom 		if (write(biofd, &c, 1) != 1)
56725419Sbloom 			delarg |= 4;
5689332Smckusick 	}
56925419Sbloom 	if (delarg==0 && Aflag==0)
57025419Sbloom 		(void) chmod(ofilename, ofilemode);
571615Sbill 	exit (delarg);
572615Sbill }
573615Sbill 
574615Sbill endload(argc, argv)
575615Sbill 	int argc;
576615Sbill 	char **argv;
577615Sbill {
578615Sbill 	register int c, i;
579615Sbill 	long dnum;
580615Sbill 	register char *ap, **p;
581615Sbill 
582615Sbill 	clibseg = libseg;
583615Sbill 	filname = 0;
584615Sbill 	middle();
585615Sbill 	setupout();
586615Sbill 	p = argv+1;
587615Sbill 	for (c=1; c<argc; c++) {
588615Sbill 		ap = *p++;
589615Sbill 		if (trace)
590615Sbill 			printf("%s:\n", ap);
591615Sbill 		if (*ap != '-') {
592615Sbill 			load2arg(ap);
593615Sbill 			continue;
594615Sbill 		}
595615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
596615Sbill 
597615Sbill 		case 'D':
598615Sbill 			dnum = htoi(*p);
599615Sbill 			if (dorigin < dnum)
600615Sbill 				while (dorigin < dnum)
601615Sbill 					bputc(0, dout), dorigin++;
602615Sbill 			/* fall into ... */
603615Sbill 		case 'T':
604615Sbill 		case 'u':
605615Sbill 		case 'e':
606615Sbill 		case 'o':
607615Sbill 		case 'H':
608615Sbill 			++c;
609615Sbill 			++p;
610615Sbill 			/* fall into ... */
611615Sbill 		default:
612615Sbill 			continue;
613615Sbill 		case 'A':
614615Sbill 			funding = 1;
615615Sbill 			load2arg(*p++);
616615Sbill 			funding = 0;
617615Sbill 			c++;
618615Sbill 			continue;
619898Sbill 		case 'y':
62017133Ssam 		case 'L':
621898Sbill 			goto next;
622615Sbill 		case 'l':
623615Sbill 			ap[--i]='-';
624615Sbill 			load2arg(&ap[i]);
625615Sbill 			goto next;
626615Sbill 		}
627615Sbill next:
628615Sbill 		;
629615Sbill 	}
630615Sbill 	finishout();
631615Sbill }
632615Sbill 
633615Sbill /*
634615Sbill  * Scan file to find defined symbols.
635615Sbill  */
636615Sbill load1arg(cp)
637615Sbill 	register char *cp;
638615Sbill {
639615Sbill 	register struct ranlib *tp;
640615Sbill 	off_t nloc;
641898Sbill 	int kind;
642615Sbill 
643898Sbill 	kind = getfile(cp);
644898Sbill 	if (Mflag)
645898Sbill 		printf("%s\n", filname);
646898Sbill 	switch (kind) {
647615Sbill 
648615Sbill 	/*
649615Sbill 	 * Plain file.
650615Sbill 	 */
651615Sbill 	case 0:
652615Sbill 		load1(0, 0L);
653615Sbill 		break;
654615Sbill 
655615Sbill 	/*
656615Sbill 	 * Archive without table of contents.
657615Sbill 	 * (Slowly) process each member.
658615Sbill 	 */
659615Sbill 	case 1:
660898Sbill 		error(-1,
661898Sbill "warning: archive has no table of contents; add one using ranlib(1)");
662615Sbill 		nloc = SARMAG;
663615Sbill 		while (step(nloc))
664615Sbill 			nloc += sizeof(archdr) +
665615Sbill 			    round(atol(archdr.ar_size), sizeof (short));
666615Sbill 		break;
667615Sbill 
668615Sbill 	/*
669615Sbill 	 * Archive with table of contents.
670615Sbill 	 * Read the table of contents and its associated string table.
671615Sbill 	 * Pass through the library resolving symbols until nothing changes
672615Sbill 	 * for an entire pass (i.e. you can get away with backward references
673615Sbill 	 * when there is a table of contents!)
674615Sbill 	 */
675615Sbill 	case 2:
676615Sbill 		nloc = SARMAG + sizeof (archdr);
677615Sbill 		dseek(&text, nloc, sizeof (tnum));
678615Sbill 		mget((char *)&tnum, sizeof (tnum), &text);
679615Sbill 		nloc += sizeof (tnum);
680615Sbill 		tab = (struct ranlib *)malloc(tnum);
681615Sbill 		if (tab == 0)
682615Sbill 			error(1, "ran out of memory (toc)");
683615Sbill 		dseek(&text, nloc, tnum);
684615Sbill 		mget((char *)tab, tnum, &text);
685615Sbill 		nloc += tnum;
686615Sbill 		tnum /= sizeof (struct ranlib);
687615Sbill 		dseek(&text, nloc, sizeof (ssiz));
688615Sbill 		mget((char *)&ssiz, sizeof (ssiz), &text);
689615Sbill 		nloc += sizeof (ssiz);
690615Sbill 		tabstr = (char *)malloc(ssiz);
691615Sbill 		if (tabstr == 0)
692615Sbill 			error(1, "ran out of memory (tocstr)");
693615Sbill 		dseek(&text, nloc, ssiz);
694615Sbill 		mget((char *)tabstr, ssiz, &text);
695615Sbill 		for (tp = &tab[tnum]; --tp >= tab;) {
696615Sbill 			if (tp->ran_un.ran_strx < 0 ||
697615Sbill 			    tp->ran_un.ran_strx >= ssiz)
698615Sbill 				error(1, "mangled archive table of contents");
699615Sbill 			tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx;
700615Sbill 		}
701615Sbill 		while (ldrand())
702615Sbill 			continue;
70325483Slepreau 		free((char *)tab);
70425483Slepreau 		free(tabstr);
705615Sbill 		nextlibp(-1);
706615Sbill 		break;
707615Sbill 
708615Sbill 	/*
709615Sbill 	 * Table of contents is out of date, so search
710615Sbill 	 * as a normal library (but skip the __.SYMDEF file).
711615Sbill 	 */
712615Sbill 	case 3:
713898Sbill 		error(-1,
714898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)");
715615Sbill 		nloc = SARMAG;
716615Sbill 		do
717615Sbill 			nloc += sizeof(archdr) +
718615Sbill 			    round(atol(archdr.ar_size), sizeof(short));
719615Sbill 		while (step(nloc));
720615Sbill 		break;
721615Sbill 	}
722615Sbill 	close(infil);
723615Sbill }
724615Sbill 
725615Sbill /*
726615Sbill  * Advance to the next archive member, which
727615Sbill  * is at offset nloc in the archive.  If the member
728615Sbill  * is useful, record its location in the liblist structure
729615Sbill  * for use in pass2.  Mark the end of the archive in libilst with a -1.
730615Sbill  */
731615Sbill step(nloc)
732615Sbill 	off_t nloc;
733615Sbill {
734615Sbill 
735615Sbill 	dseek(&text, nloc, (long) sizeof archdr);
736615Sbill 	if (text.size <= 0) {
737615Sbill 		nextlibp(-1);
738615Sbill 		return (0);
739615Sbill 	}
740615Sbill 	getarhdr();
741615Sbill 	if (load1(1, nloc + (sizeof archdr)))
742615Sbill 		nextlibp(nloc);
743615Sbill 	return (1);
744615Sbill }
745615Sbill 
746615Sbill /*
747615Sbill  * Record the location of a useful archive member.
748615Sbill  * Recording -1 marks the end of files from an archive.
749615Sbill  * The liblist data structure is dynamically extended here.
750615Sbill  */
751615Sbill nextlibp(val)
752615Sbill 	off_t val;
753615Sbill {
754615Sbill 
755615Sbill 	if (clibseg->li_used == NROUT) {
756615Sbill 		if (++clibseg == &libseg[NSEG])
757615Sbill 			error(1, "too many files loaded from libraries");
758615Sbill 		clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t));
759615Sbill 		if (clibseg->li_first == 0)
760615Sbill 			error(1, "ran out of memory (nextlibp)");
761615Sbill 	}
762615Sbill 	clibseg->li_first[clibseg->li_used++] = val;
763898Sbill 	if (val != -1 && Mflag)
764898Sbill 		printf("\t%s\n", archdr.ar_name);
765615Sbill }
766615Sbill 
767615Sbill /*
768615Sbill  * One pass over an archive with a table of contents.
769615Sbill  * Remember the number of symbols currently defined,
770615Sbill  * then call step on members which look promising (i.e.
771615Sbill  * that define a symbol which is currently externally undefined).
772615Sbill  * Indicate to our caller whether this process netted any more symbols.
773615Sbill  */
774615Sbill ldrand()
775615Sbill {
776615Sbill 	register struct nlist *sp, **hp;
777615Sbill 	register struct ranlib *tp, *tplast;
778615Sbill 	off_t loc;
779615Sbill 	int nsymt = symx(nextsym);
780615Sbill 
781615Sbill 	tplast = &tab[tnum-1];
782615Sbill 	for (tp = tab; tp <= tplast; tp++) {
78325483Slepreau 		if ((hp = slookup(tp->ran_un.ran_name)) == 0 || *hp == 0)
784615Sbill 			continue;
785615Sbill 		sp = *hp;
786615Sbill 		if (sp->n_type != N_EXT+N_UNDF)
787615Sbill 			continue;
788615Sbill 		step(tp->ran_off);
789615Sbill 		loc = tp->ran_off;
790615Sbill 		while (tp < tplast && (tp+1)->ran_off == loc)
791615Sbill 			tp++;
792615Sbill 	}
793615Sbill 	return (symx(nextsym) != nsymt);
794615Sbill }
795615Sbill 
796615Sbill /*
797615Sbill  * Examine a single file or archive member on pass 1.
798615Sbill  */
799615Sbill load1(libflg, loc)
800615Sbill 	off_t loc;
801615Sbill {
802615Sbill 	register struct nlist *sp;
803615Sbill 	struct nlist *savnext;
804615Sbill 	int ndef, nlocal, type, size, nsymt;
805615Sbill 	register int i;
806615Sbill 	off_t maxoff;
807615Sbill 	struct stat stb;
808615Sbill 
809615Sbill 	readhdr(loc);
810615Sbill 	if (filhdr.a_syms == 0) {
81129999Sbostic 		if (filhdr.a_text+filhdr.a_data == 0) {
81229999Sbostic 			/* load2() adds a symbol for the file name */
81329999Sbostic 			if (!libflg)
81429999Sbostic 				ssize += sizeof (cursym);
815615Sbill 			return (0);
81629999Sbostic 		}
817615Sbill 		error(1, "no namelist");
818615Sbill 	}
819615Sbill 	if (libflg)
820615Sbill 		maxoff = atol(archdr.ar_size);
821615Sbill 	else {
822615Sbill 		fstat(infil, &stb);
823615Sbill 		maxoff = stb.st_size;
824615Sbill 	}
825615Sbill 	if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff)
826615Sbill 		error(1, "too small (old format .o?)");
827615Sbill 	ctrel = tsize; cdrel += dsize; cbrel += bsize;
828615Sbill 	ndef = 0;
829615Sbill 	nlocal = sizeof(cursym);
830615Sbill 	savnext = nextsym;
831615Sbill 	loc += N_SYMOFF(filhdr);
832615Sbill 	dseek(&text, loc, filhdr.a_syms);
833615Sbill 	dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t));
834615Sbill 	mget(&size, sizeof (size), &reloc);
835615Sbill 	dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t));
836615Sbill 	curstr = (char *)malloc(size);
837615Sbill 	if (curstr == NULL)
838615Sbill 		error(1, "no space for string table");
839615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc);
840615Sbill 	while (text.size > 0) {
841615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
842615Sbill 		if (cursym.n_un.n_strx) {
843615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
844615Sbill 			    cursym.n_un.n_strx>=size)
845615Sbill 				error(1, "bad string table index (pass 1)");
846615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
847615Sbill 		}
848615Sbill 		type = cursym.n_type;
849615Sbill 		if ((type&N_EXT)==0) {
850615Sbill 			if (Xflag==0 || cursym.n_un.n_name[0]!='L' ||
851615Sbill 			    type & N_STAB)
852615Sbill 				nlocal += sizeof cursym;
853615Sbill 			continue;
854615Sbill 		}
855615Sbill 		symreloc();
856615Sbill 		if (enter(lookup()))
857615Sbill 			continue;
858615Sbill 		if ((sp = lastsym)->n_type != N_EXT+N_UNDF)
859615Sbill 			continue;
860615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
861615Sbill 			if (cursym.n_value > sp->n_value)
862615Sbill 				sp->n_value = cursym.n_value;
863615Sbill 			continue;
864615Sbill 		}
865615Sbill 		if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT)
866615Sbill 			continue;
867615Sbill 		ndef++;
868615Sbill 		sp->n_type = cursym.n_type;
869615Sbill 		sp->n_value = cursym.n_value;
870615Sbill 	}
871615Sbill 	if (libflg==0 || ndef) {
872615Sbill 		tsize += filhdr.a_text;
873615Sbill 		dsize += round(filhdr.a_data, sizeof (long));
874615Sbill 		bsize += round(filhdr.a_bss, sizeof (long));
875615Sbill 		ssize += nlocal;
876615Sbill 		trsize += filhdr.a_trsize;
877615Sbill 		drsize += filhdr.a_drsize;
878615Sbill 		if (funding)
879615Sbill 			textbase = (*slookup("_end"))->n_value;
880615Sbill 		nsymt = symx(nextsym);
881615Sbill 		for (i = symx(savnext); i < nsymt; i++) {
882615Sbill 			sp = xsym(i);
883615Sbill 			sp->n_un.n_name = savestr(sp->n_un.n_name);
884615Sbill 		}
885615Sbill 		free(curstr);
886615Sbill 		return (1);
887615Sbill 	}
888615Sbill 	/*
889615Sbill 	 * No symbols defined by this library member.
890615Sbill 	 * Rip out the hash table entries and reset the symbol table.
891615Sbill 	 */
892615Sbill 	symfree(savnext);
893615Sbill 	free(curstr);
894615Sbill 	return(0);
895615Sbill }
896615Sbill 
897615Sbill middle()
898615Sbill {
899615Sbill 	register struct nlist *sp;
900615Sbill 	long csize, t, corigin, ocsize;
901615Sbill 	int nund, rnd;
902615Sbill 	char s;
903615Sbill 	register int i;
904615Sbill 	int nsymt;
905615Sbill 
906615Sbill 	torigin = 0;
907615Sbill 	dorigin = 0;
908615Sbill 	borigin = 0;
909615Sbill 
910615Sbill 	p_etext = *slookup("_etext");
911615Sbill 	p_edata = *slookup("_edata");
912615Sbill 	p_end = *slookup("_end");
913615Sbill 	/*
914615Sbill 	 * If there are any undefined symbols, save the relocation bits.
915615Sbill 	 */
916615Sbill 	nsymt = symx(nextsym);
917615Sbill 	if (rflag==0) {
918615Sbill 		for (i = 0; i < nsymt; i++) {
919615Sbill 			sp = xsym(i);
920615Sbill 			if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 &&
921650Sbill 			    sp!=p_end && sp!=p_edata && sp!=p_etext) {
922615Sbill 				rflag++;
923615Sbill 				dflag = 0;
924615Sbill 				break;
925615Sbill 			}
926615Sbill 		}
927615Sbill 	}
928615Sbill 	if (rflag)
929615Sbill 		sflag = zflag = 0;
930615Sbill 	/*
931615Sbill 	 * Assign common locations.
932615Sbill 	 */
933615Sbill 	csize = 0;
934615Sbill 	if (!Aflag)
935615Sbill 		addsym = symseg[0].sy_first;
936615Sbill 	database = round(tsize+textbase,
93712671Ssam 	    (nflag||zflag? pagesize : sizeof (long)));
938615Sbill 	database += hsize;
939615Sbill 	if (dflag || rflag==0) {
940615Sbill 		ldrsym(p_etext, tsize, N_EXT+N_TEXT);
941615Sbill 		ldrsym(p_edata, dsize, N_EXT+N_DATA);
942615Sbill 		ldrsym(p_end, bsize, N_EXT+N_BSS);
943615Sbill 		for (i = symx(addsym); i < nsymt; i++) {
944615Sbill 			sp = xsym(i);
945615Sbill 			if ((s=sp->n_type)==N_EXT+N_UNDF &&
946615Sbill 			    (t = sp->n_value)!=0) {
947615Sbill 				if (t >= sizeof (double))
948615Sbill 					rnd = sizeof (double);
949615Sbill 				else if (t >= sizeof (long))
950615Sbill 					rnd = sizeof (long);
951615Sbill 				else
952615Sbill 					rnd = sizeof (short);
953615Sbill 				csize = round(csize, rnd);
954615Sbill 				sp->n_value = csize;
955615Sbill 				sp->n_type = N_EXT+N_COMM;
956615Sbill 				ocsize = csize;
957615Sbill 				csize += t;
958615Sbill 			}
959615Sbill 			if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) {
960615Sbill 				sp->n_value = ocsize;
961615Sbill 				sp->n_type = (s&N_STAB) | (N_EXT+N_COMM);
962615Sbill 			}
963615Sbill 		}
964615Sbill 	}
965615Sbill 	/*
966615Sbill 	 * Now set symbols to their final value
967615Sbill 	 */
968615Sbill 	csize = round(csize, sizeof (long));
969615Sbill 	torigin = textbase;
970615Sbill 	dorigin = database;
971615Sbill 	corigin = dorigin + dsize;
972615Sbill 	borigin = corigin + csize;
973615Sbill 	nund = 0;
974615Sbill 	nsymt = symx(nextsym);
975615Sbill 	for (i = symx(addsym); i<nsymt; i++) {
976615Sbill 		sp = xsym(i);
977615Sbill 		switch (sp->n_type & (N_TYPE+N_EXT)) {
978615Sbill 
979615Sbill 		case N_EXT+N_UNDF:
9802369Skre 			if (arflag == 0)
9812369Skre 				errlev |= 01;
982615Sbill 			if ((arflag==0 || dflag) && sp->n_value==0) {
983650Sbill 				if (sp==p_end || sp==p_etext || sp==p_edata)
984650Sbill 					continue;
985615Sbill 				if (nund==0)
986615Sbill 					printf("Undefined:\n");
987615Sbill 				nund++;
988615Sbill 				printf("%s\n", sp->n_un.n_name);
989615Sbill 			}
990615Sbill 			continue;
991615Sbill 		case N_EXT+N_ABS:
992615Sbill 		default:
993615Sbill 			continue;
994615Sbill 		case N_EXT+N_TEXT:
995615Sbill 			sp->n_value += torigin;
996615Sbill 			continue;
997615Sbill 		case N_EXT+N_DATA:
998615Sbill 			sp->n_value += dorigin;
999615Sbill 			continue;
1000615Sbill 		case N_EXT+N_BSS:
1001615Sbill 			sp->n_value += borigin;
1002615Sbill 			continue;
1003615Sbill 		case N_EXT+N_COMM:
1004615Sbill 			sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS);
1005615Sbill 			sp->n_value += corigin;
1006615Sbill 			continue;
1007615Sbill 		}
1008615Sbill 	}
1009615Sbill 	if (sflag || xflag)
1010615Sbill 		ssize = 0;
1011615Sbill 	bsize += csize;
1012615Sbill 	nsym = ssize / (sizeof cursym);
1013615Sbill 	if (Aflag) {
1014615Sbill 		fixspec(p_etext,torigin);
1015615Sbill 		fixspec(p_edata,dorigin);
1016615Sbill 		fixspec(p_end,borigin);
1017615Sbill 	}
1018615Sbill }
1019615Sbill 
1020615Sbill fixspec(sym,offset)
1021615Sbill 	struct nlist *sym;
1022615Sbill 	long offset;
1023615Sbill {
1024615Sbill 
1025615Sbill 	if(symx(sym) < symx(addsym) && sym!=0)
1026615Sbill 		sym->n_value += offset;
1027615Sbill }
1028615Sbill 
1029615Sbill ldrsym(sp, val, type)
1030615Sbill 	register struct nlist *sp;
1031615Sbill 	long val;
1032615Sbill {
1033615Sbill 
1034615Sbill 	if (sp == 0)
1035615Sbill 		return;
1036615Sbill 	if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) {
1037615Sbill 		printf("%s: ", sp->n_un.n_name);
1038615Sbill 		error(0, "user attempt to redfine loader-defined symbol");
1039615Sbill 		return;
1040615Sbill 	}
1041615Sbill 	sp->n_type = type;
1042615Sbill 	sp->n_value = val;
1043615Sbill }
1044615Sbill 
1045615Sbill off_t	wroff;
1046615Sbill struct	biobuf toutb;
1047615Sbill 
1048615Sbill setupout()
1049615Sbill {
1050615Sbill 	int bss;
105116068Sralph 	struct stat stbuf;
1052898Sbill 	extern char *sys_errlist[];
1053898Sbill 	extern int errno;
1054615Sbill 
10553606Ssklower 	ofilemode = 0777 & ~umask(0);
10563606Ssklower 	biofd = creat(ofilename, 0666 & ofilemode);
1057898Sbill 	if (biofd < 0) {
1058898Sbill 		filname = ofilename;		/* kludge */
1059898Sbill 		archdr.ar_name[0] = 0;		/* kludge */
1060898Sbill 		error(1, sys_errlist[errno]);	/* kludge */
1061898Sbill 	}
106216068Sralph 	fstat(biofd, &stbuf);		/* suppose file exists, wrong*/
106316068Sralph 	if (stbuf.st_mode & 0111) {	/* mode, ld fails? */
106416068Sralph 		chmod(ofilename, stbuf.st_mode & 0666);
106516068Sralph 		ofilemode = stbuf.st_mode;
106616068Sralph 	}
1067615Sbill 	filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC);
1068615Sbill 	filhdr.a_text = nflag ? tsize :
106912671Ssam 	    round(tsize, zflag ? pagesize : sizeof (long));
107012671Ssam 	filhdr.a_data = zflag ? round(dsize, pagesize) : dsize;
1071615Sbill 	bss = bsize - (filhdr.a_data - dsize);
1072615Sbill 	if (bss < 0)
1073615Sbill 		bss = 0;
1074615Sbill 	filhdr.a_bss = bss;
1075615Sbill 	filhdr.a_trsize = trsize;
1076615Sbill 	filhdr.a_drsize = drsize;
1077615Sbill 	filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym));
1078615Sbill 	if (entrypt) {
1079615Sbill 		if (entrypt->n_type!=N_EXT+N_TEXT)
1080615Sbill 			error(0, "entry point not in text");
1081615Sbill 		else
1082615Sbill 			filhdr.a_entry = entrypt->n_value;
1083615Sbill 	} else
1084615Sbill 		filhdr.a_entry = 0;
1085615Sbill 	filhdr.a_trsize = (rflag ? trsize:0);
1086615Sbill 	filhdr.a_drsize = (rflag ? drsize:0);
108716068Sralph 	tout = &toutb;
108816068Sralph 	bopen(tout, 0, stbuf.st_blksize);
1089615Sbill 	bwrite((char *)&filhdr, sizeof (filhdr), tout);
109016068Sralph 	if (zflag)
109116068Sralph 		bseek(tout, pagesize);
1092615Sbill 	wroff = N_TXTOFF(filhdr) + filhdr.a_text;
109316068Sralph 	outb(&dout, filhdr.a_data, stbuf.st_blksize);
1094615Sbill 	if (rflag) {
109516068Sralph 		outb(&trout, filhdr.a_trsize, stbuf.st_blksize);
109616068Sralph 		outb(&drout, filhdr.a_drsize, stbuf.st_blksize);
1097615Sbill 	}
1098615Sbill 	if (sflag==0 || xflag==0) {
109916068Sralph 		outb(&sout, filhdr.a_syms, stbuf.st_blksize);
1100615Sbill 		wroff += sizeof (offset);
110116068Sralph 		outb(&strout, 0, stbuf.st_blksize);
1102615Sbill 	}
1103615Sbill }
1104615Sbill 
110516068Sralph outb(bp, inc, bufsize)
1106615Sbill 	register struct biobuf **bp;
1107615Sbill {
1108615Sbill 
1109615Sbill 	*bp = (struct biobuf *)malloc(sizeof (struct biobuf));
1110615Sbill 	if (*bp == 0)
1111615Sbill 		error(1, "ran out of memory (outb)");
111216068Sralph 	bopen(*bp, wroff, bufsize);
1113615Sbill 	wroff += inc;
1114615Sbill }
1115615Sbill 
1116615Sbill load2arg(acp)
1117615Sbill char *acp;
1118615Sbill {
1119615Sbill 	register char *cp;
1120615Sbill 	off_t loc;
1121615Sbill 
1122615Sbill 	cp = acp;
1123615Sbill 	if (getfile(cp) == 0) {
1124615Sbill 		while (*cp)
1125615Sbill 			cp++;
1126615Sbill 		while (cp >= acp && *--cp != '/');
1127615Sbill 		mkfsym(++cp);
1128615Sbill 		load2(0L);
1129615Sbill 	} else {	/* scan archive members referenced */
1130615Sbill 		for (;;) {
1131615Sbill 			if (clibseg->li_used2 == clibseg->li_used) {
1132615Sbill 				if (clibseg->li_used < NROUT)
1133615Sbill 					error(1, "libseg botch");
1134615Sbill 				clibseg++;
1135615Sbill 			}
1136615Sbill 			loc = clibseg->li_first[clibseg->li_used2++];
1137615Sbill 			if (loc == -1)
1138615Sbill 				break;
1139615Sbill 			dseek(&text, loc, (long)sizeof(archdr));
1140615Sbill 			getarhdr();
1141615Sbill 			mkfsym(archdr.ar_name);
1142615Sbill 			load2(loc + (long)sizeof(archdr));
1143615Sbill 		}
1144615Sbill 	}
1145615Sbill 	close(infil);
1146615Sbill }
1147615Sbill 
1148615Sbill load2(loc)
1149615Sbill long loc;
1150615Sbill {
1151615Sbill 	int size;
1152615Sbill 	register struct nlist *sp;
1153615Sbill 	register struct local *lp;
1154615Sbill 	register int symno, i;
1155615Sbill 	int type;
1156615Sbill 
1157615Sbill 	readhdr(loc);
1158650Sbill 	if (!funding) {
1159615Sbill 		ctrel = torigin;
1160615Sbill 		cdrel += dorigin;
1161615Sbill 		cbrel += borigin;
1162615Sbill 	}
1163615Sbill 	/*
1164615Sbill 	 * Reread the symbol table, recording the numbering
1165615Sbill 	 * of symbols for fixing external references.
1166615Sbill 	 */
1167615Sbill 	for (i = 0; i < LHSIZ; i++)
1168615Sbill 		lochash[i] = 0;
1169615Sbill 	clocseg = locseg;
1170615Sbill 	clocseg->lo_used = 0;
1171615Sbill 	symno = -1;
1172615Sbill 	loc += N_TXTOFF(filhdr);
1173615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1174615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t));
1175615Sbill 	mget(&size, sizeof(size), &text);
1176615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1177615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t),
1178615Sbill 		size - sizeof(off_t));
1179615Sbill 	curstr = (char *)malloc(size);
1180615Sbill 	if (curstr == NULL)
1181615Sbill 		error(1, "out of space reading string table (pass 2)");
1182615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &text);
1183615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1184615Sbill 		filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms);
1185615Sbill 	while (text.size > 0) {
1186615Sbill 		symno++;
1187615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
1188615Sbill 		if (cursym.n_un.n_strx) {
1189615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
1190615Sbill 			    cursym.n_un.n_strx>=size)
1191615Sbill 				error(1, "bad string table index (pass 2)");
1192615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
1193615Sbill 		}
1194615Sbill /* inline expansion of symreloc() */
1195615Sbill 		switch (cursym.n_type & 017) {
1196615Sbill 
1197615Sbill 		case N_TEXT:
1198615Sbill 		case N_EXT+N_TEXT:
1199615Sbill 			cursym.n_value += ctrel;
1200615Sbill 			break;
1201615Sbill 		case N_DATA:
1202615Sbill 		case N_EXT+N_DATA:
1203615Sbill 			cursym.n_value += cdrel;
1204615Sbill 			break;
1205615Sbill 		case N_BSS:
1206615Sbill 		case N_EXT+N_BSS:
1207615Sbill 			cursym.n_value += cbrel;
1208615Sbill 			break;
1209615Sbill 		case N_EXT+N_UNDF:
1210615Sbill 			break;
1211615Sbill 		default:
1212615Sbill 			if (cursym.n_type&N_EXT)
1213615Sbill 				cursym.n_type = N_EXT+N_ABS;
1214615Sbill 		}
1215615Sbill /* end inline expansion of symreloc() */
1216615Sbill 		type = cursym.n_type;
1217898Sbill 		if (yflag && cursym.n_un.n_name)
1218898Sbill 			for (i = 0; i < yflag; i++)
1219898Sbill 				/* fast check for 2d character! */
1220898Sbill 				if (ytab[i][1] == cursym.n_un.n_name[1] &&
1221898Sbill 				    !strcmp(ytab[i], cursym.n_un.n_name)) {
1222898Sbill 					tracesym();
1223898Sbill 					break;
1224898Sbill 				}
1225615Sbill 		if ((type&N_EXT) == 0) {
1226615Sbill 			if (!sflag&&!xflag&&
1227615Sbill 			    (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB))
1228615Sbill 				symwrite(&cursym, sout);
1229615Sbill 			continue;
1230615Sbill 		}
1231615Sbill 		if (funding)
1232615Sbill 			continue;
1233615Sbill 		if ((sp = *lookup()) == 0)
1234615Sbill 			error(1, "internal error: symbol not found");
1235615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
1236615Sbill 			if (clocseg->lo_used == NSYMPR) {
1237615Sbill 				if (++clocseg == &locseg[NSEG])
1238615Sbill 					error(1, "local symbol overflow");
1239615Sbill 				clocseg->lo_used = 0;
1240615Sbill 			}
1241615Sbill 			if (clocseg->lo_first == 0) {
1242615Sbill 				clocseg->lo_first = (struct local *)
1243615Sbill 				    malloc(NSYMPR * sizeof (struct local));
1244615Sbill 				if (clocseg->lo_first == 0)
1245615Sbill 					error(1, "out of memory (clocseg)");
1246615Sbill 			}
1247615Sbill 			lp = &clocseg->lo_first[clocseg->lo_used++];
1248615Sbill 			lp->l_index = symno;
1249615Sbill 			lp->l_symbol = sp;
1250615Sbill 			lp->l_link = lochash[symno % LHSIZ];
1251615Sbill 			lochash[symno % LHSIZ] = lp;
1252615Sbill 			continue;
1253615Sbill 		}
1254615Sbill 		if (cursym.n_type & N_STAB)
1255615Sbill 			continue;
1256615Sbill 		if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) {
1257615Sbill 			printf("%s: ", cursym.n_un.n_name);
1258615Sbill 			error(0, "multiply defined");
1259615Sbill 		}
1260615Sbill 	}
1261615Sbill 	if (funding)
1262615Sbill 		return;
1263615Sbill 	dseek(&text, loc, filhdr.a_text);
1264615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize);
1265650Sbill 	load2td(ctrel, torigin - textbase, tout, trout);
1266615Sbill 	dseek(&text, loc+filhdr.a_text, filhdr.a_data);
1267615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize,
1268615Sbill 	    filhdr.a_drsize);
1269650Sbill 	load2td(cdrel, dorigin - database, dout, drout);
1270615Sbill 	while (filhdr.a_data & (sizeof(long)-1)) {
1271615Sbill 		bputc(0, dout);
1272615Sbill 		filhdr.a_data++;
1273615Sbill 	}
1274615Sbill 	torigin += filhdr.a_text;
12751752Sbill 	dorigin += round(filhdr.a_data, sizeof (long));
12761752Sbill 	borigin += round(filhdr.a_bss, sizeof (long));
1277615Sbill 	free(curstr);
1278615Sbill }
1279615Sbill 
1280898Sbill struct tynames {
1281898Sbill 	int	ty_value;
1282898Sbill 	char	*ty_name;
1283898Sbill } tynames[] = {
1284898Sbill 	N_UNDF,	"undefined",
1285898Sbill 	N_ABS,	"absolute",
1286898Sbill 	N_TEXT,	"text",
1287898Sbill 	N_DATA,	"data",
1288898Sbill 	N_BSS,	"bss",
1289898Sbill 	N_COMM,	"common",
1290898Sbill 	0,	0,
1291898Sbill };
1292898Sbill 
1293898Sbill tracesym()
1294898Sbill {
1295898Sbill 	register struct tynames *tp;
1296898Sbill 
1297898Sbill 	if (cursym.n_type & N_STAB)
1298898Sbill 		return;
1299898Sbill 	printf("%s", filname);
1300898Sbill 	if (archdr.ar_name[0])
1301898Sbill 		printf("(%s)", archdr.ar_name);
1302898Sbill 	printf(": ");
1303898Sbill 	if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) {
1304898Sbill 		printf("definition of common %s size %d\n",
1305898Sbill 		    cursym.n_un.n_name, cursym.n_value);
1306898Sbill 		return;
1307898Sbill 	}
1308898Sbill 	for (tp = tynames; tp->ty_name; tp++)
1309898Sbill 		if (tp->ty_value == (cursym.n_type&N_TYPE))
1310898Sbill 			break;
1311898Sbill 	printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to");
1312898Sbill 	if (cursym.n_type&N_EXT)
1313898Sbill 		printf(" external");
1314898Sbill 	if (tp->ty_name)
1315898Sbill 		printf(" %s", tp->ty_name);
1316898Sbill 	printf(" %s\n", cursym.n_un.n_name);
1317898Sbill }
1318898Sbill 
131929845Ssam #if !defined(tahoe)
132029845Ssam /* for machines which allow arbitrarily aligned word and longword accesses */
132129845Ssam #define	getw(cp)	(*(short *)(cp))
132229845Ssam #define	getl(cp)	(*(long *)(cp))
132329845Ssam #define	putw(cp, w)	(*(short *)(cp) = (w))
132429845Ssam #define	putl(cp, l)	(*(long *)(cp) = (l))
132529845Ssam #else
132629845Ssam short
132729845Ssam getw(cp)
132829845Ssam 	char *cp;
132929845Ssam {
133029845Ssam 	union {
133129845Ssam 		short	w;
133229845Ssam 		char	c[2];
133329845Ssam 	} w;
133429845Ssam 
133529845Ssam 	w.c[0] = *cp++;
133629845Ssam 	w.c[1] = *cp++;
133729845Ssam 	return (w.w);
133829845Ssam }
133929845Ssam 
134029845Ssam getl(cp)
134129845Ssam 	char *cp;
134229845Ssam {
134329845Ssam 	union {
134429845Ssam 		long	l;
134529845Ssam 		char	c[4];
134629845Ssam 	} l;
134729845Ssam 
134829845Ssam 	l.c[0] = *cp++;
134929845Ssam 	l.c[1] = *cp++;
135029845Ssam 	l.c[2] = *cp++;
135129845Ssam 	l.c[3] = *cp++;
135229845Ssam 	return (l.l);
135329845Ssam }
135429845Ssam 
135529845Ssam putw(cp, v)
135629845Ssam 	char *cp;
135729845Ssam 	short v;
135829845Ssam {
135929845Ssam 	union {
136029845Ssam 		short	w;
136129845Ssam 		char	c[2];
136229845Ssam 	} w;
136329845Ssam 
136429845Ssam 	w.w = v;
136529845Ssam 	*cp++ = w.c[0];
136629845Ssam 	*cp++ = w.c[1];
136729845Ssam }
136829845Ssam 
136929845Ssam putl(cp, v)
137029845Ssam 	char *cp;
137129845Ssam 	long v;
137229845Ssam {
137329845Ssam 	union {
137429845Ssam 		long	l;
137529845Ssam 		char	c[4];
137629845Ssam 	} l;
137729845Ssam 
137829845Ssam 	l.l = v;
137929845Ssam 	*cp++ = l.c[0];
138029845Ssam 	*cp++ = l.c[1];
138129845Ssam 	*cp++ = l.c[2];
138229845Ssam 	*cp++ = l.c[3];
138329845Ssam }
138429845Ssam #endif
138529845Ssam 
1386650Sbill /*
1387650Sbill  * This routine relocates the single text or data segment argument.
1388650Sbill  * Offsets from external symbols are resolved by adding the value
1389650Sbill  * of the external symbols.  Non-external reference are updated to account
1390650Sbill  * for the relative motion of the segments (ctrel, cdrel, ...).  If
1391650Sbill  * a relocation was pc-relative, then we update it to reflect the
1392650Sbill  * change in the positioning of the segments by adding the displacement
1393650Sbill  * of the referenced segment and subtracting the displacement of the
1394650Sbill  * current segment (creloc).
1395650Sbill  *
1396650Sbill  * If we are saving the relocation information, then we increase
1397650Sbill  * each relocation datum address by our base position in the new segment.
1398650Sbill  */
1399650Sbill load2td(creloc, position, b1, b2)
140030647Slepreau 	long creloc, position;
1401615Sbill 	struct biobuf *b1, *b2;
1402615Sbill {
1403615Sbill 	register struct nlist *sp;
1404615Sbill 	register struct local *lp;
1405615Sbill 	long tw;
1406615Sbill 	register struct relocation_info *rp, *rpend;
1407615Sbill 	struct relocation_info *relp;
1408615Sbill 	char *codep;
1409615Sbill 	register char *cp;
1410615Sbill 	int relsz, codesz;
1411615Sbill 
1412615Sbill 	relsz = reloc.size;
1413615Sbill 	relp = (struct relocation_info *)malloc(relsz);
1414615Sbill 	codesz = text.size;
1415615Sbill 	codep = (char *)malloc(codesz);
1416615Sbill 	if (relp == 0 || codep == 0)
1417615Sbill 		error(1, "out of memory (load2td)");
1418615Sbill 	mget((char *)relp, relsz, &reloc);
1419615Sbill 	rpend = &relp[relsz / sizeof (struct relocation_info)];
1420615Sbill 	mget(codep, codesz, &text);
1421615Sbill 	for (rp = relp; rp < rpend; rp++) {
1422615Sbill 		cp = codep + rp->r_address;
1423650Sbill 		/*
1424650Sbill 		 * Pick up previous value at location to be relocated.
1425650Sbill 		 */
1426615Sbill 		switch (rp->r_length) {
1427615Sbill 
1428615Sbill 		case 0:		/* byte */
1429615Sbill 			tw = *cp;
1430615Sbill 			break;
1431615Sbill 
1432615Sbill 		case 1:		/* word */
143329845Ssam 			tw = getw(cp);
1434615Sbill 			break;
1435615Sbill 
1436615Sbill 		case 2:		/* long */
143729845Ssam 			tw = getl(cp);
1438615Sbill 			break;
1439615Sbill 
1440615Sbill 		default:
1441615Sbill 			error(1, "load2td botch: bad length");
1442615Sbill 		}
1443650Sbill 		/*
1444650Sbill 		 * If relative to an external which is defined,
1445650Sbill 		 * resolve to a simpler kind of reference in the
1446650Sbill 		 * result file.  If the external is undefined, just
1447650Sbill 		 * convert the symbol number to the number of the
1448650Sbill 		 * symbol in the result file and leave it undefined.
1449650Sbill 		 */
1450615Sbill 		if (rp->r_extern) {
1451650Sbill 			/*
1452650Sbill 			 * Search the hash table which maps local
1453650Sbill 			 * symbol numbers to symbol tables entries
1454650Sbill 			 * in the new a.out file.
1455650Sbill 			 */
1456615Sbill 			lp = lochash[rp->r_symbolnum % LHSIZ];
1457615Sbill 			while (lp->l_index != rp->r_symbolnum) {
1458615Sbill 				lp = lp->l_link;
1459615Sbill 				if (lp == 0)
1460615Sbill 					error(1, "local symbol botch");
1461615Sbill 			}
1462615Sbill 			sp = lp->l_symbol;
1463615Sbill 			if (sp->n_type == N_EXT+N_UNDF)
1464615Sbill 				rp->r_symbolnum = nsym+symx(sp);
1465615Sbill 			else {
1466615Sbill 				rp->r_symbolnum = sp->n_type & N_TYPE;
1467615Sbill 				tw += sp->n_value;
1468615Sbill 				rp->r_extern = 0;
1469615Sbill 			}
1470615Sbill 		} else switch (rp->r_symbolnum & N_TYPE) {
1471650Sbill 		/*
1472650Sbill 		 * Relocation is relative to the loaded position
1473650Sbill 		 * of another segment.  Update by the change in position
1474650Sbill 		 * of that segment.
1475650Sbill 		 */
1476615Sbill 		case N_TEXT:
1477615Sbill 			tw += ctrel;
1478615Sbill 			break;
1479615Sbill 		case N_DATA:
1480615Sbill 			tw += cdrel;
1481615Sbill 			break;
1482615Sbill 		case N_BSS:
1483615Sbill 			tw += cbrel;
1484615Sbill 			break;
1485615Sbill 		case N_ABS:
1486615Sbill 			break;
1487615Sbill 		default:
1488615Sbill 			error(1, "relocation format botch (symbol type))");
1489615Sbill 		}
1490650Sbill 		/*
1491650Sbill 		 * Relocation is pc relative, so decrease the relocation
1492650Sbill 		 * by the amount the current segment is displaced.
1493650Sbill 		 * (E.g if we are a relative reference to a text location
1494650Sbill 		 * from data space, we added the increase in the text address
1495650Sbill 		 * above, and subtract the increase in our (data) address
1496650Sbill 		 * here, leaving the net change the relative change in the
1497650Sbill 		 * positioning of our text and data segments.)
1498650Sbill 		 */
1499615Sbill 		if (rp->r_pcrel)
1500615Sbill 			tw -= creloc;
1501650Sbill 		/*
1502650Sbill 		 * Put the value back in the segment,
1503650Sbill 		 * while checking for overflow.
1504650Sbill 		 */
1505615Sbill 		switch (rp->r_length) {
1506615Sbill 
1507615Sbill 		case 0:		/* byte */
1508615Sbill 			if (tw < -128 || tw > 127)
1509615Sbill 				error(0, "byte displacement overflow");
1510615Sbill 			*cp = tw;
1511615Sbill 			break;
1512615Sbill 		case 1:		/* word */
1513615Sbill 			if (tw < -32768 || tw > 32767)
1514615Sbill 				error(0, "word displacement overflow");
151529845Ssam 			putw(cp, tw);
1516615Sbill 			break;
1517615Sbill 		case 2:		/* long */
151829845Ssam 			putl(cp, tw);
1519615Sbill 			break;
1520615Sbill 		}
1521650Sbill 		/*
1522650Sbill 		 * If we are saving relocation information,
1523650Sbill 		 * we must convert the address in the segment from
1524650Sbill 		 * the old .o file into an address in the segment in
1525650Sbill 		 * the new a.out, by adding the position of our
1526650Sbill 		 * segment in the new larger segment.
1527650Sbill 		 */
1528615Sbill 		if (rflag)
1529650Sbill 			rp->r_address += position;
1530615Sbill 	}
1531615Sbill 	bwrite(codep, codesz, b1);
1532615Sbill 	if (rflag)
1533615Sbill 		bwrite(relp, relsz, b2);
153425483Slepreau 	free((char *)relp);
153525483Slepreau 	free(codep);
1536615Sbill }
1537615Sbill 
1538615Sbill finishout()
1539615Sbill {
1540615Sbill 	register int i;
1541615Sbill 	int nsymt;
1542615Sbill 
1543615Sbill 	if (sflag==0) {
1544615Sbill 		nsymt = symx(nextsym);
1545615Sbill 		for (i = 0; i < nsymt; i++)
1546615Sbill 			symwrite(xsym(i), sout);
1547615Sbill 		bwrite(&offset, sizeof offset, sout);
1548615Sbill 	}
1549615Sbill 	if (!ofilfnd) {
1550615Sbill 		unlink("a.out");
1551898Sbill 		if (link("l.out", "a.out") < 0)
1552898Sbill 			error(1, "cannot move l.out to a.out");
1553615Sbill 		ofilename = "a.out";
1554615Sbill 	}
1555615Sbill 	delarg = errlev;
1556615Sbill 	delexit();
1557615Sbill }
1558615Sbill 
1559615Sbill mkfsym(s)
1560615Sbill char *s;
1561615Sbill {
1562615Sbill 
1563615Sbill 	if (sflag || xflag)
1564615Sbill 		return;
1565615Sbill 	cursym.n_un.n_name = s;
156630836Sbostic 	cursym.n_type = N_EXT | N_FN;
1567615Sbill 	cursym.n_value = torigin;
1568615Sbill 	symwrite(&cursym, sout);
1569615Sbill }
1570615Sbill 
1571615Sbill getarhdr()
1572615Sbill {
1573615Sbill 	register char *cp;
1574615Sbill 
1575615Sbill 	mget((char *)&archdr, sizeof archdr, &text);
1576615Sbill 	for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];)
1577615Sbill 		if (*cp++ == ' ') {
1578615Sbill 			cp[-1] = 0;
1579615Sbill 			return;
1580615Sbill 		}
1581615Sbill }
1582615Sbill 
1583615Sbill mget(loc, n, sp)
1584615Sbill register STREAM *sp;
1585615Sbill register char *loc;
1586615Sbill {
1587615Sbill 	register char *p;
1588615Sbill 	register int take;
1589615Sbill 
1590615Sbill top:
1591615Sbill 	if (n == 0)
1592615Sbill 		return;
1593615Sbill 	if (sp->size && sp->nibuf) {
1594615Sbill 		p = sp->ptr;
1595615Sbill 		take = sp->size;
1596615Sbill 		if (take > sp->nibuf)
1597615Sbill 			take = sp->nibuf;
1598615Sbill 		if (take > n)
1599615Sbill 			take = n;
1600615Sbill 		n -= take;
1601615Sbill 		sp->size -= take;
1602615Sbill 		sp->nibuf -= take;
1603615Sbill 		sp->pos += take;
1604615Sbill 		do
1605615Sbill 			*loc++ = *p++;
1606615Sbill 		while (--take > 0);
1607615Sbill 		sp->ptr = p;
1608615Sbill 		goto top;
1609615Sbill 	}
161016068Sralph 	if (n > p_blksize) {
161116068Sralph 		take = n - n % p_blksize;
161216068Sralph 		lseek(infil, (sp->bno+1)<<p_blkshift, 0);
1613615Sbill 		if (take > sp->size || read(infil, loc, take) != take)
1614615Sbill 			error(1, "premature EOF");
1615615Sbill 		loc += take;
1616615Sbill 		n -= take;
1617615Sbill 		sp->size -= take;
1618615Sbill 		sp->pos += take;
161916068Sralph 		dseek(sp, (sp->bno+1+(take>>p_blkshift))<<p_blkshift, -1);
1620615Sbill 		goto top;
1621615Sbill 	}
1622615Sbill 	*loc++ = get(sp);
1623615Sbill 	--n;
1624615Sbill 	goto top;
1625615Sbill }
1626615Sbill 
1627615Sbill symwrite(sp, bp)
1628615Sbill 	struct nlist *sp;
1629615Sbill 	struct biobuf *bp;
1630615Sbill {
1631615Sbill 	register int len;
1632615Sbill 	register char *str;
1633615Sbill 
1634615Sbill 	str = sp->n_un.n_name;
1635615Sbill 	if (str) {
1636615Sbill 		sp->n_un.n_strx = offset;
1637615Sbill 		len = strlen(str) + 1;
1638615Sbill 		bwrite(str, len, strout);
1639615Sbill 		offset += len;
1640615Sbill 	}
1641615Sbill 	bwrite(sp, sizeof (*sp), bp);
1642615Sbill 	sp->n_un.n_name = str;
1643615Sbill }
1644615Sbill 
1645615Sbill dseek(sp, loc, s)
1646615Sbill register STREAM *sp;
1647615Sbill long loc, s;
1648615Sbill {
1649615Sbill 	register PAGE *p;
1650615Sbill 	register b, o;
1651615Sbill 	int n;
1652615Sbill 
165316068Sralph 	b = loc>>p_blkshift;
165416068Sralph 	o = loc&p_blkmask;
1655615Sbill 	if (o&01)
1656615Sbill 		error(1, "loader error; odd offset");
1657615Sbill 	--sp->pno->nuser;
1658615Sbill 	if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b)
1659615Sbill 		if (p->nuser==0 || (p = &page[0])->nuser==0) {
1660615Sbill 			if (page[0].nuser==0 && page[1].nuser==0)
1661615Sbill 				if (page[0].bno < page[1].bno)
1662615Sbill 					p = &page[0];
1663615Sbill 			p->bno = b;
166416068Sralph 			lseek(infil, loc & ~(long)p_blkmask, 0);
166516068Sralph 			if ((n = read(infil, p->buff, p_blksize)) < 0)
1666615Sbill 				n = 0;
1667615Sbill 			p->nibuf = n;
166816068Sralph 		} else
166916068Sralph 			error(1, "botch: no pages");
1670615Sbill 	++p->nuser;
1671615Sbill 	sp->bno = b;
1672615Sbill 	sp->pno = p;
1673615Sbill 	if (s != -1) {sp->size = s; sp->pos = 0;}
1674615Sbill 	sp->ptr = (char *)(p->buff + o);
1675615Sbill 	if ((sp->nibuf = p->nibuf-o) <= 0)
1676615Sbill 		sp->size = 0;
1677615Sbill }
1678615Sbill 
1679615Sbill char
1680615Sbill get(asp)
1681615Sbill STREAM *asp;
1682615Sbill {
1683615Sbill 	register STREAM *sp;
1684615Sbill 
1685615Sbill 	sp = asp;
1686615Sbill 	if ((sp->nibuf -= sizeof(char)) < 0) {
168716068Sralph 		dseek(sp, ((long)(sp->bno+1)<<p_blkshift), (long)-1);
1688615Sbill 		sp->nibuf -= sizeof(char);
1689615Sbill 	}
1690615Sbill 	if ((sp->size -= sizeof(char)) <= 0) {
1691615Sbill 		if (sp->size < 0)
1692615Sbill 			error(1, "premature EOF");
1693615Sbill 		++fpage.nuser;
1694615Sbill 		--sp->pno->nuser;
1695615Sbill 		sp->pno = (PAGE *) &fpage;
1696615Sbill 	}
1697615Sbill 	sp->pos += sizeof(char);
1698615Sbill 	return(*sp->ptr++);
1699615Sbill }
1700615Sbill 
1701615Sbill getfile(acp)
1702615Sbill char *acp;
1703615Sbill {
1704615Sbill 	register int c;
1705615Sbill 	char arcmag[SARMAG+1];
1706615Sbill 	struct stat stb;
1707615Sbill 
1708615Sbill 	archdr.ar_name[0] = '\0';
170917133Ssam 	filname = acp;
171017133Ssam 	if (filname[0] == '-' && filname[1] == 'l')
171117133Ssam 		infil = libopen(filname + 2, O_RDONLY);
171217133Ssam 	else
171317133Ssam 		infil = open(filname, O_RDONLY);
171417133Ssam 	if (infil < 0)
1715615Sbill 		error(1, "cannot open");
171616068Sralph 	fstat(infil, &stb);
1717615Sbill 	page[0].bno = page[1].bno = -1;
1718615Sbill 	page[0].nuser = page[1].nuser = 0;
171916068Sralph 	c = stb.st_blksize;
172016068Sralph 	if (c == 0 || (c & (c - 1)) != 0) {
172116068Sralph 		/* use default size if not a power of two */
172216068Sralph 		c = BLKSIZE;
172316068Sralph 	}
172416068Sralph 	if (p_blksize != c) {
172516068Sralph 		p_blksize = c;
172616068Sralph 		p_blkmask = c - 1;
172716068Sralph 		for (p_blkshift = 0; c > 1 ; p_blkshift++)
172816068Sralph 			c >>= 1;
172916068Sralph 		if (page[0].buff != NULL)
173016068Sralph 			free(page[0].buff);
173116068Sralph 		page[0].buff = (char *)malloc(p_blksize);
173216068Sralph 		if (page[0].buff == NULL)
173316068Sralph 			error(1, "ran out of memory (getfile)");
173416068Sralph 		if (page[1].buff != NULL)
173516068Sralph 			free(page[1].buff);
173616068Sralph 		page[1].buff = (char *)malloc(p_blksize);
173716068Sralph 		if (page[1].buff == NULL)
173816068Sralph 			error(1, "ran out of memory (getfile)");
173916068Sralph 	}
1740615Sbill 	text.pno = reloc.pno = (PAGE *) &fpage;
1741615Sbill 	fpage.nuser = 2;
1742615Sbill 	dseek(&text, 0L, SARMAG);
1743615Sbill 	if (text.size <= 0)
1744615Sbill 		error(1, "premature EOF");
1745615Sbill 	mget((char *)arcmag, SARMAG, &text);
1746615Sbill 	arcmag[SARMAG] = 0;
1747615Sbill 	if (strcmp(arcmag, ARMAG))
1748615Sbill 		return (0);
1749615Sbill 	dseek(&text, SARMAG, sizeof archdr);
175017133Ssam 	if (text.size <= 0)
1751615Sbill 		return (1);
1752615Sbill 	getarhdr();
175330828Sbostic 	if (strncmp(archdr.ar_name, RANLIBMAG, sizeof(archdr.ar_name)) != 0)
1754615Sbill 		return (1);
1755615Sbill 	return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2);
1756615Sbill }
1757615Sbill 
175817133Ssam /*
175917133Ssam  * Search for a library with given name
176017133Ssam  * using the directory search array.
176117133Ssam  */
176217133Ssam libopen(name, oflags)
176317133Ssam 	char *name;
176417133Ssam 	int oflags;
176517133Ssam {
176617133Ssam 	register char *p, *cp;
176717133Ssam 	register int i;
176817133Ssam 	static char buf[MAXPATHLEN+1];
176917133Ssam 	int fd = -1;
177017133Ssam 
177117133Ssam 	if (*name == '\0')			/* backwards compat */
177217133Ssam 		name = "a";
177317133Ssam 	for (i = 0; i < ndir && fd == -1; i++) {
177417133Ssam 		p = buf;
177517133Ssam 		for (cp = dirs[i]; *cp; *p++ = *cp++)
177617133Ssam 			;
177717133Ssam 		*p++ = '/';
177817133Ssam 		for (cp = "lib"; *cp; *p++ = *cp++)
177917133Ssam 			;
178017133Ssam 		for (cp = name; *cp; *p++ = *cp++)
178117133Ssam 			;
178217133Ssam 		cp = ".a";
178317133Ssam 		while (*p++ = *cp++)
178417133Ssam 			;
178517133Ssam 		fd = open(buf, oflags);
178617133Ssam 	}
178717133Ssam 	if (fd != -1)
178817133Ssam 		filname = buf;
178917133Ssam 	return (fd);
179017133Ssam }
179117133Ssam 
1792615Sbill struct nlist **
1793615Sbill lookup()
1794615Sbill {
1795615Sbill 	register int sh;
1796615Sbill 	register struct nlist **hp;
1797615Sbill 	register char *cp, *cp1;
1798615Sbill 	register struct symseg *gp;
1799615Sbill 	register int i;
1800615Sbill 
1801615Sbill 	sh = 0;
1802615Sbill 	for (cp = cursym.n_un.n_name; *cp;)
1803615Sbill 		sh = (sh<<1) + *cp++;
1804615Sbill 	sh = (sh & 0x7fffffff) % HSIZE;
1805615Sbill 	for (gp = symseg; gp < &symseg[NSEG]; gp++) {
1806615Sbill 		if (gp->sy_first == 0) {
1807615Sbill 			gp->sy_first = (struct nlist *)
1808615Sbill 			    calloc(NSYM, sizeof (struct nlist));
1809615Sbill 			gp->sy_hfirst = (struct nlist **)
1810615Sbill 			    calloc(HSIZE, sizeof (struct nlist *));
1811615Sbill 			if (gp->sy_first == 0 || gp->sy_hfirst == 0)
1812615Sbill 				error(1, "ran out of space for symbol table");
1813615Sbill 			gp->sy_last = gp->sy_first + NSYM;
1814615Sbill 			gp->sy_hlast = gp->sy_hfirst + HSIZE;
1815615Sbill 		}
1816615Sbill 		if (gp > csymseg)
1817615Sbill 			csymseg = gp;
1818615Sbill 		hp = gp->sy_hfirst + sh;
1819615Sbill 		i = 1;
1820615Sbill 		do {
1821615Sbill 			if (*hp == 0) {
1822615Sbill 				if (gp->sy_used == NSYM)
1823615Sbill 					break;
1824615Sbill 				return (hp);
1825615Sbill 			}
1826615Sbill 			cp1 = (*hp)->n_un.n_name;
1827615Sbill 			for (cp = cursym.n_un.n_name; *cp == *cp1++;)
1828615Sbill 				if (*cp++ == 0)
1829615Sbill 					return (hp);
1830615Sbill 			hp += i;
1831615Sbill 			i += 2;
1832615Sbill 			if (hp >= gp->sy_hlast)
1833615Sbill 				hp -= HSIZE;
1834615Sbill 		} while (i < HSIZE);
1835615Sbill 		if (i > HSIZE)
1836615Sbill 			error(1, "hash table botch");
1837615Sbill 	}
1838615Sbill 	error(1, "symbol table overflow");
1839615Sbill 	/*NOTREACHED*/
1840615Sbill }
1841615Sbill 
1842615Sbill symfree(saved)
1843615Sbill 	struct nlist *saved;
1844615Sbill {
1845615Sbill 	register struct symseg *gp;
1846615Sbill 	register struct nlist *sp;
1847615Sbill 
1848615Sbill 	for (gp = csymseg; gp >= symseg; gp--, csymseg--) {
1849615Sbill 		sp = gp->sy_first + gp->sy_used;
1850615Sbill 		if (sp == saved) {
1851615Sbill 			nextsym = sp;
1852615Sbill 			return;
1853615Sbill 		}
1854615Sbill 		for (sp--; sp >= gp->sy_first; sp--) {
1855615Sbill 			gp->sy_hfirst[sp->n_hash] = 0;
1856615Sbill 			gp->sy_used--;
1857615Sbill 			if (sp == saved) {
1858615Sbill 				nextsym = sp;
1859615Sbill 				return;
1860615Sbill 			}
1861615Sbill 		}
1862615Sbill 	}
1863615Sbill 	if (saved == 0)
1864615Sbill 		return;
1865615Sbill 	error(1, "symfree botch");
1866615Sbill }
1867615Sbill 
1868615Sbill struct nlist **
1869615Sbill slookup(s)
1870615Sbill 	char *s;
1871615Sbill {
1872615Sbill 
1873615Sbill 	cursym.n_un.n_name = s;
1874615Sbill 	cursym.n_type = N_EXT+N_UNDF;
1875615Sbill 	cursym.n_value = 0;
1876615Sbill 	return (lookup());
1877615Sbill }
1878615Sbill 
1879615Sbill enter(hp)
1880615Sbill register struct nlist **hp;
1881615Sbill {
1882615Sbill 	register struct nlist *sp;
1883615Sbill 
1884615Sbill 	if (*hp==0) {
1885615Sbill 		if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast)
1886615Sbill 			error(1, "enter botch");
1887615Sbill 		*hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used;
1888615Sbill 		csymseg->sy_used++;
1889615Sbill 		sp->n_un.n_name = cursym.n_un.n_name;
1890615Sbill 		sp->n_type = cursym.n_type;
1891615Sbill 		sp->n_hash = hp - csymseg->sy_hfirst;
1892615Sbill 		sp->n_value = cursym.n_value;
1893615Sbill 		nextsym = lastsym + 1;
1894615Sbill 		return(1);
1895615Sbill 	} else {
1896615Sbill 		lastsym = *hp;
1897615Sbill 		return(0);
1898615Sbill 	}
1899615Sbill }
1900615Sbill 
1901615Sbill symx(sp)
1902615Sbill 	struct nlist *sp;
1903615Sbill {
1904615Sbill 	register struct symseg *gp;
1905615Sbill 
1906615Sbill 	if (sp == 0)
1907615Sbill 		return (0);
1908615Sbill 	for (gp = csymseg; gp >= symseg; gp--)
1909615Sbill 		/* <= is sloppy so nextsym will always work */
1910615Sbill 		if (sp >= gp->sy_first && sp <= gp->sy_last)
1911615Sbill 			return ((gp - symseg) * NSYM + sp - gp->sy_first);
1912615Sbill 	error(1, "symx botch");
1913615Sbill 	/*NOTREACHED*/
1914615Sbill }
1915615Sbill 
1916615Sbill symreloc()
1917615Sbill {
1918615Sbill 	if(funding) return;
1919615Sbill 	switch (cursym.n_type & 017) {
1920615Sbill 
1921615Sbill 	case N_TEXT:
1922615Sbill 	case N_EXT+N_TEXT:
1923615Sbill 		cursym.n_value += ctrel;
1924615Sbill 		return;
1925615Sbill 
1926615Sbill 	case N_DATA:
1927615Sbill 	case N_EXT+N_DATA:
1928615Sbill 		cursym.n_value += cdrel;
1929615Sbill 		return;
1930615Sbill 
1931615Sbill 	case N_BSS:
1932615Sbill 	case N_EXT+N_BSS:
1933615Sbill 		cursym.n_value += cbrel;
1934615Sbill 		return;
1935615Sbill 
1936615Sbill 	case N_EXT+N_UNDF:
1937615Sbill 		return;
1938615Sbill 
1939615Sbill 	default:
1940615Sbill 		if (cursym.n_type&N_EXT)
1941615Sbill 			cursym.n_type = N_EXT+N_ABS;
1942615Sbill 		return;
1943615Sbill 	}
1944615Sbill }
1945615Sbill 
1946615Sbill error(n, s)
1947615Sbill char *s;
1948615Sbill {
1949898Sbill 
1950615Sbill 	if (errlev==0)
1951615Sbill 		printf("ld:");
1952615Sbill 	if (filname) {
1953615Sbill 		printf("%s", filname);
1954615Sbill 		if (n != -1 && archdr.ar_name[0])
1955615Sbill 			printf("(%s)", archdr.ar_name);
1956615Sbill 		printf(": ");
1957615Sbill 	}
1958615Sbill 	printf("%s\n", s);
1959615Sbill 	if (n == -1)
1960615Sbill 		return;
1961615Sbill 	if (n)
1962615Sbill 		delexit();
1963615Sbill 	errlev = 2;
1964615Sbill }
1965615Sbill 
1966615Sbill readhdr(loc)
1967615Sbill off_t loc;
1968615Sbill {
1969615Sbill 
1970615Sbill 	dseek(&text, loc, (long)sizeof(filhdr));
1971615Sbill 	mget((short *)&filhdr, sizeof(filhdr), &text);
1972615Sbill 	if (N_BADMAG(filhdr)) {
1973615Sbill 		if (filhdr.a_magic == OARMAG)
1974615Sbill 			error(1, "old archive");
1975615Sbill 		error(1, "bad magic number");
1976615Sbill 	}
1977615Sbill 	if (filhdr.a_text&01 || filhdr.a_data&01)
1978615Sbill 		error(1, "text/data size odd");
1979615Sbill 	if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) {
198012671Ssam 		cdrel = -round(filhdr.a_text, pagesize);
1981615Sbill 		cbrel = cdrel - filhdr.a_data;
1982615Sbill 	} else if (filhdr.a_magic == OMAGIC) {
1983615Sbill 		cdrel = -filhdr.a_text;
1984615Sbill 		cbrel = cdrel - filhdr.a_data;
1985615Sbill 	} else
1986615Sbill 		error(1, "bad format");
1987615Sbill }
1988615Sbill 
1989615Sbill round(v, r)
1990615Sbill 	int v;
1991615Sbill 	u_long r;
1992615Sbill {
1993615Sbill 
1994615Sbill 	r--;
1995615Sbill 	v += r;
1996615Sbill 	v &= ~(long)r;
1997615Sbill 	return(v);
1998615Sbill }
1999615Sbill 
2000615Sbill #define	NSAVETAB	8192
2001615Sbill char	*savetab;
2002615Sbill int	saveleft;
2003615Sbill 
2004615Sbill char *
2005615Sbill savestr(cp)
2006615Sbill 	register char *cp;
2007615Sbill {
2008615Sbill 	register int len;
2009615Sbill 
2010615Sbill 	len = strlen(cp) + 1;
2011615Sbill 	if (len > saveleft) {
2012615Sbill 		saveleft = NSAVETAB;
2013615Sbill 		if (len > saveleft)
2014615Sbill 			saveleft = len;
201517133Ssam 		savetab = malloc(saveleft);
2016615Sbill 		if (savetab == 0)
2017615Sbill 			error(1, "ran out of memory (savestr)");
2018615Sbill 	}
2019615Sbill 	strncpy(savetab, cp, len);
2020615Sbill 	cp = savetab;
2021615Sbill 	savetab += len;
2022615Sbill 	saveleft -= len;
2023615Sbill 	return (cp);
2024615Sbill }
2025615Sbill 
202616068Sralph bopen(bp, off, bufsize)
202716068Sralph 	register struct biobuf *bp;
2028615Sbill {
2029615Sbill 
203017133Ssam 	bp->b_ptr = bp->b_buf = malloc(bufsize);
203116068Sralph 	if (bp->b_ptr == (char *)0)
203216068Sralph 		error(1, "ran out of memory (bopen)");
203316068Sralph 	bp->b_bufsize = bufsize;
203416068Sralph 	bp->b_nleft = bufsize - (off % bufsize);
2035615Sbill 	bp->b_off = off;
2036615Sbill 	bp->b_link = biobufs;
2037615Sbill 	biobufs = bp;
2038615Sbill }
2039615Sbill 
2040615Sbill int	bwrerror;
2041615Sbill 
2042615Sbill bwrite(p, cnt, bp)
2043615Sbill 	register char *p;
2044615Sbill 	register int cnt;
2045615Sbill 	register struct biobuf *bp;
2046615Sbill {
2047615Sbill 	register int put;
2048615Sbill 	register char *to;
2049615Sbill 
2050615Sbill top:
2051615Sbill 	if (cnt == 0)
2052615Sbill 		return;
2053615Sbill 	if (bp->b_nleft) {
2054615Sbill 		put = bp->b_nleft;
2055615Sbill 		if (put > cnt)
2056615Sbill 			put = cnt;
2057615Sbill 		bp->b_nleft -= put;
2058615Sbill 		to = bp->b_ptr;
205925419Sbloom 		bcopy(p, to, put);
2060615Sbill 		bp->b_ptr += put;
2061615Sbill 		p += put;
2062615Sbill 		cnt -= put;
2063615Sbill 		goto top;
2064615Sbill 	}
206516068Sralph 	if (cnt >= bp->b_bufsize) {
2066615Sbill 		if (bp->b_ptr != bp->b_buf)
2067615Sbill 			bflush1(bp);
206816068Sralph 		put = cnt - cnt % bp->b_bufsize;
2069615Sbill 		if (boffset != bp->b_off)
2070615Sbill 			lseek(biofd, bp->b_off, 0);
2071615Sbill 		if (write(biofd, p, put) != put) {
2072615Sbill 			bwrerror = 1;
2073615Sbill 			error(1, "output write error");
2074615Sbill 		}
2075615Sbill 		bp->b_off += put;
2076615Sbill 		boffset = bp->b_off;
2077615Sbill 		p += put;
2078615Sbill 		cnt -= put;
2079615Sbill 		goto top;
2080615Sbill 	}
2081615Sbill 	bflush1(bp);
2082615Sbill 	goto top;
2083615Sbill }
2084615Sbill 
2085615Sbill bflush()
2086615Sbill {
2087615Sbill 	register struct biobuf *bp;
2088615Sbill 
2089615Sbill 	if (bwrerror)
2090615Sbill 		return;
2091615Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
2092615Sbill 		bflush1(bp);
2093615Sbill }
2094615Sbill 
2095615Sbill bflush1(bp)
2096615Sbill 	register struct biobuf *bp;
2097615Sbill {
2098615Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
2099615Sbill 
2100615Sbill 	if (cnt == 0)
2101615Sbill 		return;
2102615Sbill 	if (boffset != bp->b_off)
2103615Sbill 		lseek(biofd, bp->b_off, 0);
2104615Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
2105615Sbill 		bwrerror = 1;
2106615Sbill 		error(1, "output write error");
2107615Sbill 	}
2108615Sbill 	bp->b_off += cnt;
2109615Sbill 	boffset = bp->b_off;
2110615Sbill 	bp->b_ptr = bp->b_buf;
211116068Sralph 	bp->b_nleft = bp->b_bufsize;
2112615Sbill }
2113615Sbill 
2114615Sbill bflushc(bp, c)
2115615Sbill 	register struct biobuf *bp;
2116615Sbill {
2117615Sbill 
2118615Sbill 	bflush1(bp);
2119615Sbill 	bputc(c, bp);
2120615Sbill }
212116068Sralph 
212216068Sralph bseek(bp, off)
212316068Sralph 	register struct biobuf *bp;
212416068Sralph 	register off_t off;
212516068Sralph {
212616068Sralph 	bflush1(bp);
212716068Sralph 
212816068Sralph 	bp->b_nleft = bp->b_bufsize - (off % bp->b_bufsize);
212916068Sralph 	bp->b_off = off;
213016068Sralph }
2131