xref: /csrg-svn/old/ld/ld.c (revision 40407)
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*40407Smckusick static char sccsid[] = "@(#)ld.c	5.13 (Berkeley) 03/11/90";
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>
3037031Sbostic #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 */
324*40407Smckusick char	*defaultname;		/* l.out */
325*40407Smckusick char	*ofilename;		/* name given to -o */
3263606Ssklower int	ofilemode;		/* respect umask even for unsucessful ld's */
327615Sbill int	infil;			/* current input file descriptor */
328615Sbill char	*filname;		/* and its name */
329615Sbill 
33017133Ssam #define	NDIRS	25
33125533Sbloom #define NDEFDIRS 3		/* number of default directories in dirs[] */
33217133Ssam char	*dirs[NDIRS];		/* directories for library search */
33317133Ssam int	ndir;			/* number of directories */
33417133Ssam 
335615Sbill /*
336615Sbill  * Base of the string table of the current module (pass1 and pass2).
337615Sbill  */
338615Sbill char	*curstr;
339615Sbill 
34012671Ssam /*
34112671Ssam  * System software page size, as returned by getpagesize.
34212671Ssam  */
34312671Ssam int	pagesize;
34412671Ssam 
345615Sbill char 	get();
346615Sbill int	delexit();
347615Sbill char	*savestr();
34817133Ssam char	*malloc();
349615Sbill 
350615Sbill main(argc, argv)
351615Sbill char **argv;
352615Sbill {
353615Sbill 	register int c, i;
354615Sbill 	int num;
355615Sbill 	register char *ap, **p;
356615Sbill 	char save;
357615Sbill 
358650Sbill 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
359615Sbill 		signal(SIGINT, delexit);
360650Sbill 		signal(SIGTERM, delexit);
361650Sbill 	}
362615Sbill 	if (argc == 1)
363615Sbill 		exit(4);
364*40407Smckusick 	ofilename = defaultname = (char *)genbuildname("l.out");
36512671Ssam 	pagesize = getpagesize();
366615Sbill 
36717133Ssam 	/*
36817133Ssam 	 * Pull out search directories.
36917133Ssam 	 */
37017133Ssam 	for (c = 1; c < argc; c++) {
37117133Ssam 		ap = argv[c];
37217133Ssam 		if (ap[0] == '-' && ap[1] == 'L') {
37317133Ssam 			if (ap[2] == 0)
37417133Ssam 				error(1, "-L: pathname missing");
37525533Sbloom 			if (ndir >= NDIRS - NDEFDIRS)
37617133Ssam 				error(1, "-L: too many directories");
37717133Ssam 			dirs[ndir++] = &ap[2];
37817133Ssam 		}
37917133Ssam 	}
38017133Ssam 	/* add default search directories */
38137031Sbostic 	dirs[ndir++] = _PATH_USRLIB;
38237031Sbostic 	dirs[ndir++] = _PATH_LOCALLIB;
38317133Ssam 
38417133Ssam 	p = argv+1;
385650Sbill 	/*
386650Sbill 	 * Scan files once to find where symbols are defined.
387650Sbill 	 */
388615Sbill 	for (c=1; c<argc; c++) {
389615Sbill 		if (trace)
390615Sbill 			printf("%s:\n", *p);
391615Sbill 		filname = 0;
392615Sbill 		ap = *p++;
393615Sbill 		if (*ap != '-') {
394615Sbill 			load1arg(ap);
395615Sbill 			continue;
396615Sbill 		}
397615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
398615Sbill 
399615Sbill 		case 'o':
400615Sbill 			if (++c >= argc)
401615Sbill 				error(1, "-o where?");
402*40407Smckusick 			ofilename = (char *)genbuildname(*p++);
403615Sbill 			ofilfnd++;
404615Sbill 			continue;
405615Sbill 		case 'u':
406615Sbill 		case 'e':
407615Sbill 			if (++c >= argc)
40833997Sbostic 				error(1, " -u or -e: arg missing");
409615Sbill 			enter(slookup(*p++));
410615Sbill 			if (ap[i]=='e')
411615Sbill 				entrypt = lastsym;
412615Sbill 			continue;
413615Sbill 		case 'H':
414615Sbill 			if (++c >= argc)
415615Sbill 				error(1, "-H: arg missing");
416615Sbill 			if (tsize!=0)
417615Sbill 				error(1, "-H: too late, some text already loaded");
418615Sbill 			hsize = atoi(*p++);
419615Sbill 			continue;
420615Sbill 		case 'A':
421615Sbill 			if (++c >= argc)
422615Sbill 				error(1, "-A: arg missing");
423615Sbill 			if (Aflag)
424615Sbill 				error(1, "-A: only one base file allowed");
425615Sbill 			Aflag = 1;
426615Sbill 			nflag = 0;
427615Sbill 			funding = 1;
428615Sbill 			load1arg(*p++);
429615Sbill 			trsize = drsize = tsize = dsize = bsize = 0;
430615Sbill 			ctrel = cdrel = cbrel = 0;
431615Sbill 			funding = 0;
432615Sbill 			addsym = nextsym;
433615Sbill 			continue;
434615Sbill 		case 'D':
435615Sbill 			if (++c >= argc)
436615Sbill 				error(1, "-D: arg missing");
437615Sbill 			num = htoi(*p++);
438615Sbill 			if (dsize > num)
439615Sbill 				error(1, "-D: too small");
440615Sbill 			dsize = num;
441615Sbill 			continue;
442615Sbill 		case 'T':
443615Sbill 			if (++c >= argc)
444615Sbill 				error(1, "-T: arg missing");
445615Sbill 			if (tsize!=0)
446615Sbill 				error(1, "-T: too late, some text already loaded");
447615Sbill 			textbase = htoi(*p++);
448615Sbill 			continue;
449615Sbill 		case 'l':
450615Sbill 			save = ap[--i];
451615Sbill 			ap[i]='-';
452615Sbill 			load1arg(&ap[i]);
453615Sbill 			ap[i]=save;
454615Sbill 			goto next;
455898Sbill 		case 'M':
456898Sbill 			Mflag++;
457898Sbill 			continue;
458615Sbill 		case 'x':
459615Sbill 			xflag++;
460615Sbill 			continue;
461615Sbill 		case 'X':
462615Sbill 			Xflag++;
463615Sbill 			continue;
464615Sbill 		case 'S':
465615Sbill 			Sflag++;
466615Sbill 			continue;
467615Sbill 		case 'r':
468615Sbill 			rflag++;
469615Sbill 			arflag++;
470615Sbill 			continue;
471615Sbill 		case 's':
472615Sbill 			sflag++;
473615Sbill 			xflag++;
474615Sbill 			continue;
475615Sbill 		case 'n':
476615Sbill 			nflag++;
477650Sbill 			Nflag = zflag = 0;
478615Sbill 			continue;
479615Sbill 		case 'N':
480650Sbill 			Nflag++;
481650Sbill 			nflag = zflag = 0;
482615Sbill 			continue;
483615Sbill 		case 'd':
484615Sbill 			dflag++;
485615Sbill 			continue;
486615Sbill 		case 'i':
487615Sbill 			printf("ld: -i ignored\n");
488615Sbill 			continue;
489615Sbill 		case 't':
490615Sbill 			trace++;
491615Sbill 			continue;
492898Sbill 		case 'y':
493898Sbill 			if (ap[i+1] == 0)
494898Sbill 				error(1, "-y: symbol name missing");
495898Sbill 			if (yflag == 0) {
496898Sbill 				ytab = (char **)calloc(argc, sizeof (char **));
497898Sbill 				if (ytab == 0)
498898Sbill 					error(1, "ran out of memory (-y)");
499898Sbill 			}
500898Sbill 			ytab[yflag++] = &ap[i+1];
501898Sbill 			goto next;
502615Sbill 		case 'z':
503615Sbill 			zflag++;
504650Sbill 			Nflag = nflag = 0;
505615Sbill 			continue;
50617133Ssam 		case 'L':
50717133Ssam 			goto next;
508615Sbill 		default:
509615Sbill 			filname = savestr("-x");	/* kludge */
510615Sbill 			filname[1] = ap[i];		/* kludge */
511615Sbill 			archdr.ar_name[0] = 0;		/* kludge */
512615Sbill 			error(1, "bad flag");
513615Sbill 		}
514615Sbill next:
515615Sbill 		;
516615Sbill 	}
517650Sbill 	if (rflag == 0 && Nflag == 0 && nflag == 0)
518650Sbill 		zflag++;
519615Sbill 	endload(argc, argv);
520615Sbill 	exit(0);
521615Sbill }
522615Sbill 
523615Sbill /*
524615Sbill  * Convert a ascii string which is a hex number.
525615Sbill  * Used by -T and -D options.
526615Sbill  */
527615Sbill htoi(p)
528615Sbill 	register char *p;
529615Sbill {
530615Sbill 	register int c, n;
531615Sbill 
532615Sbill 	n = 0;
533615Sbill 	while (c = *p++) {
534615Sbill 		n <<= 4;
535615Sbill 		if (isdigit(c))
536615Sbill 			n += c - '0';
537615Sbill 		else if (c >= 'a' && c <= 'f')
538615Sbill 			n += 10 + (c - 'a');
539615Sbill 		else if (c >= 'A' && c <= 'F')
540615Sbill 			n += 10 + (c - 'A');
541615Sbill 		else
542615Sbill 			error(1, "badly formed hex number");
543615Sbill 	}
544615Sbill 	return (n);
545615Sbill }
546615Sbill 
547615Sbill delexit()
548615Sbill {
5499332Smckusick 	struct stat stbuf;
5509332Smckusick 	long size;
5519332Smckusick 	char c = 0;
552615Sbill 
553615Sbill 	bflush();
554*40407Smckusick 	unlink(defaultname);
5559332Smckusick 	/*
5569332Smckusick 	 * We have to insure that the last block of the data segment
55716068Sralph 	 * is allocated a full pagesize block. If the underlying
55816068Sralph 	 * file system allocates frags that are smaller than pagesize,
55916068Sralph 	 * a full zero filled pagesize block needs to be allocated so
5609332Smckusick 	 * that when it is demand paged, the paged in block will be
5619332Smckusick 	 * appropriately filled with zeros.
5629332Smckusick 	 */
5639332Smckusick 	fstat(biofd, &stbuf);
56416068Sralph 	size = round(stbuf.st_size, pagesize);
56510640Smckusick 	if (!rflag && size > stbuf.st_size) {
5669332Smckusick 		lseek(biofd, size - 1, 0);
56725419Sbloom 		if (write(biofd, &c, 1) != 1)
56825419Sbloom 			delarg |= 4;
5699332Smckusick 	}
57025419Sbloom 	if (delarg==0 && Aflag==0)
57125419Sbloom 		(void) chmod(ofilename, ofilemode);
572615Sbill 	exit (delarg);
573615Sbill }
574615Sbill 
575615Sbill endload(argc, argv)
576615Sbill 	int argc;
577615Sbill 	char **argv;
578615Sbill {
579615Sbill 	register int c, i;
580615Sbill 	long dnum;
581615Sbill 	register char *ap, **p;
582615Sbill 
583615Sbill 	clibseg = libseg;
584615Sbill 	filname = 0;
585615Sbill 	middle();
586615Sbill 	setupout();
587615Sbill 	p = argv+1;
588615Sbill 	for (c=1; c<argc; c++) {
589615Sbill 		ap = *p++;
590615Sbill 		if (trace)
591615Sbill 			printf("%s:\n", ap);
592615Sbill 		if (*ap != '-') {
593615Sbill 			load2arg(ap);
594615Sbill 			continue;
595615Sbill 		}
596615Sbill 		for (i=1; ap[i]; i++) switch (ap[i]) {
597615Sbill 
598615Sbill 		case 'D':
599615Sbill 			dnum = htoi(*p);
600615Sbill 			if (dorigin < dnum)
601615Sbill 				while (dorigin < dnum)
602615Sbill 					bputc(0, dout), dorigin++;
603615Sbill 			/* fall into ... */
604615Sbill 		case 'T':
605615Sbill 		case 'u':
606615Sbill 		case 'e':
607615Sbill 		case 'o':
608615Sbill 		case 'H':
609615Sbill 			++c;
610615Sbill 			++p;
611615Sbill 			/* fall into ... */
612615Sbill 		default:
613615Sbill 			continue;
614615Sbill 		case 'A':
615615Sbill 			funding = 1;
616615Sbill 			load2arg(*p++);
617615Sbill 			funding = 0;
618615Sbill 			c++;
619615Sbill 			continue;
620898Sbill 		case 'y':
62117133Ssam 		case 'L':
622898Sbill 			goto next;
623615Sbill 		case 'l':
624615Sbill 			ap[--i]='-';
625615Sbill 			load2arg(&ap[i]);
626615Sbill 			goto next;
627615Sbill 		}
628615Sbill next:
629615Sbill 		;
630615Sbill 	}
631615Sbill 	finishout();
632615Sbill }
633615Sbill 
634615Sbill /*
635615Sbill  * Scan file to find defined symbols.
636615Sbill  */
637615Sbill load1arg(cp)
638615Sbill 	register char *cp;
639615Sbill {
640615Sbill 	register struct ranlib *tp;
641615Sbill 	off_t nloc;
642898Sbill 	int kind;
643615Sbill 
644898Sbill 	kind = getfile(cp);
645898Sbill 	if (Mflag)
646898Sbill 		printf("%s\n", filname);
647898Sbill 	switch (kind) {
648615Sbill 
649615Sbill 	/*
650615Sbill 	 * Plain file.
651615Sbill 	 */
652615Sbill 	case 0:
653615Sbill 		load1(0, 0L);
654615Sbill 		break;
655615Sbill 
656615Sbill 	/*
657615Sbill 	 * Archive without table of contents.
658615Sbill 	 * (Slowly) process each member.
659615Sbill 	 */
660615Sbill 	case 1:
661898Sbill 		error(-1,
662898Sbill "warning: archive has no table of contents; add one using ranlib(1)");
663615Sbill 		nloc = SARMAG;
664615Sbill 		while (step(nloc))
665615Sbill 			nloc += sizeof(archdr) +
666615Sbill 			    round(atol(archdr.ar_size), sizeof (short));
667615Sbill 		break;
668615Sbill 
669615Sbill 	/*
670615Sbill 	 * Archive with table of contents.
671615Sbill 	 * Read the table of contents and its associated string table.
672615Sbill 	 * Pass through the library resolving symbols until nothing changes
673615Sbill 	 * for an entire pass (i.e. you can get away with backward references
674615Sbill 	 * when there is a table of contents!)
675615Sbill 	 */
676615Sbill 	case 2:
677615Sbill 		nloc = SARMAG + sizeof (archdr);
678615Sbill 		dseek(&text, nloc, sizeof (tnum));
679615Sbill 		mget((char *)&tnum, sizeof (tnum), &text);
680615Sbill 		nloc += sizeof (tnum);
681615Sbill 		tab = (struct ranlib *)malloc(tnum);
682615Sbill 		if (tab == 0)
683615Sbill 			error(1, "ran out of memory (toc)");
684615Sbill 		dseek(&text, nloc, tnum);
685615Sbill 		mget((char *)tab, tnum, &text);
686615Sbill 		nloc += tnum;
687615Sbill 		tnum /= sizeof (struct ranlib);
688615Sbill 		dseek(&text, nloc, sizeof (ssiz));
689615Sbill 		mget((char *)&ssiz, sizeof (ssiz), &text);
690615Sbill 		nloc += sizeof (ssiz);
691615Sbill 		tabstr = (char *)malloc(ssiz);
692615Sbill 		if (tabstr == 0)
693615Sbill 			error(1, "ran out of memory (tocstr)");
694615Sbill 		dseek(&text, nloc, ssiz);
695615Sbill 		mget((char *)tabstr, ssiz, &text);
696615Sbill 		for (tp = &tab[tnum]; --tp >= tab;) {
697615Sbill 			if (tp->ran_un.ran_strx < 0 ||
698615Sbill 			    tp->ran_un.ran_strx >= ssiz)
699615Sbill 				error(1, "mangled archive table of contents");
700615Sbill 			tp->ran_un.ran_name = tabstr + tp->ran_un.ran_strx;
701615Sbill 		}
702615Sbill 		while (ldrand())
703615Sbill 			continue;
70425483Slepreau 		free((char *)tab);
70525483Slepreau 		free(tabstr);
706615Sbill 		nextlibp(-1);
707615Sbill 		break;
708615Sbill 
709615Sbill 	/*
710615Sbill 	 * Table of contents is out of date, so search
711615Sbill 	 * as a normal library (but skip the __.SYMDEF file).
712615Sbill 	 */
713615Sbill 	case 3:
714898Sbill 		error(-1,
715898Sbill "warning: table of contents for archive is out of date; rerun ranlib(1)");
716615Sbill 		nloc = SARMAG;
717615Sbill 		do
718615Sbill 			nloc += sizeof(archdr) +
719615Sbill 			    round(atol(archdr.ar_size), sizeof(short));
720615Sbill 		while (step(nloc));
721615Sbill 		break;
722615Sbill 	}
723615Sbill 	close(infil);
724615Sbill }
725615Sbill 
726615Sbill /*
727615Sbill  * Advance to the next archive member, which
728615Sbill  * is at offset nloc in the archive.  If the member
729615Sbill  * is useful, record its location in the liblist structure
730615Sbill  * for use in pass2.  Mark the end of the archive in libilst with a -1.
731615Sbill  */
732615Sbill step(nloc)
733615Sbill 	off_t nloc;
734615Sbill {
735615Sbill 
736615Sbill 	dseek(&text, nloc, (long) sizeof archdr);
737615Sbill 	if (text.size <= 0) {
738615Sbill 		nextlibp(-1);
739615Sbill 		return (0);
740615Sbill 	}
741615Sbill 	getarhdr();
742615Sbill 	if (load1(1, nloc + (sizeof archdr)))
743615Sbill 		nextlibp(nloc);
744615Sbill 	return (1);
745615Sbill }
746615Sbill 
747615Sbill /*
748615Sbill  * Record the location of a useful archive member.
749615Sbill  * Recording -1 marks the end of files from an archive.
750615Sbill  * The liblist data structure is dynamically extended here.
751615Sbill  */
752615Sbill nextlibp(val)
753615Sbill 	off_t val;
754615Sbill {
755615Sbill 
756615Sbill 	if (clibseg->li_used == NROUT) {
757615Sbill 		if (++clibseg == &libseg[NSEG])
758615Sbill 			error(1, "too many files loaded from libraries");
759615Sbill 		clibseg->li_first = (off_t *)malloc(NROUT * sizeof (off_t));
760615Sbill 		if (clibseg->li_first == 0)
761615Sbill 			error(1, "ran out of memory (nextlibp)");
762615Sbill 	}
763615Sbill 	clibseg->li_first[clibseg->li_used++] = val;
764898Sbill 	if (val != -1 && Mflag)
765898Sbill 		printf("\t%s\n", archdr.ar_name);
766615Sbill }
767615Sbill 
768615Sbill /*
769615Sbill  * One pass over an archive with a table of contents.
770615Sbill  * Remember the number of symbols currently defined,
771615Sbill  * then call step on members which look promising (i.e.
772615Sbill  * that define a symbol which is currently externally undefined).
773615Sbill  * Indicate to our caller whether this process netted any more symbols.
774615Sbill  */
775615Sbill ldrand()
776615Sbill {
777615Sbill 	register struct nlist *sp, **hp;
778615Sbill 	register struct ranlib *tp, *tplast;
779615Sbill 	off_t loc;
780615Sbill 	int nsymt = symx(nextsym);
781615Sbill 
782615Sbill 	tplast = &tab[tnum-1];
783615Sbill 	for (tp = tab; tp <= tplast; tp++) {
78425483Slepreau 		if ((hp = slookup(tp->ran_un.ran_name)) == 0 || *hp == 0)
785615Sbill 			continue;
786615Sbill 		sp = *hp;
787615Sbill 		if (sp->n_type != N_EXT+N_UNDF)
788615Sbill 			continue;
789615Sbill 		step(tp->ran_off);
790615Sbill 		loc = tp->ran_off;
791615Sbill 		while (tp < tplast && (tp+1)->ran_off == loc)
792615Sbill 			tp++;
793615Sbill 	}
794615Sbill 	return (symx(nextsym) != nsymt);
795615Sbill }
796615Sbill 
797615Sbill /*
798615Sbill  * Examine a single file or archive member on pass 1.
799615Sbill  */
800615Sbill load1(libflg, loc)
801615Sbill 	off_t loc;
802615Sbill {
803615Sbill 	register struct nlist *sp;
804615Sbill 	struct nlist *savnext;
805615Sbill 	int ndef, nlocal, type, size, nsymt;
806615Sbill 	register int i;
807615Sbill 	off_t maxoff;
808615Sbill 	struct stat stb;
809615Sbill 
810615Sbill 	readhdr(loc);
811615Sbill 	if (filhdr.a_syms == 0) {
81229999Sbostic 		if (filhdr.a_text+filhdr.a_data == 0) {
81329999Sbostic 			/* load2() adds a symbol for the file name */
81429999Sbostic 			if (!libflg)
81529999Sbostic 				ssize += sizeof (cursym);
816615Sbill 			return (0);
81729999Sbostic 		}
818615Sbill 		error(1, "no namelist");
819615Sbill 	}
820615Sbill 	if (libflg)
821615Sbill 		maxoff = atol(archdr.ar_size);
822615Sbill 	else {
823615Sbill 		fstat(infil, &stb);
824615Sbill 		maxoff = stb.st_size;
825615Sbill 	}
826615Sbill 	if (N_STROFF(filhdr) + sizeof (off_t) >= maxoff)
827615Sbill 		error(1, "too small (old format .o?)");
828615Sbill 	ctrel = tsize; cdrel += dsize; cbrel += bsize;
829615Sbill 	ndef = 0;
830615Sbill 	nlocal = sizeof(cursym);
831615Sbill 	savnext = nextsym;
832615Sbill 	loc += N_SYMOFF(filhdr);
833615Sbill 	dseek(&text, loc, filhdr.a_syms);
834615Sbill 	dseek(&reloc, loc + filhdr.a_syms, sizeof(off_t));
835615Sbill 	mget(&size, sizeof (size), &reloc);
836615Sbill 	dseek(&reloc, loc + filhdr.a_syms+sizeof (off_t), size-sizeof (off_t));
837615Sbill 	curstr = (char *)malloc(size);
838615Sbill 	if (curstr == NULL)
839615Sbill 		error(1, "no space for string table");
840615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &reloc);
841615Sbill 	while (text.size > 0) {
842615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
843615Sbill 		if (cursym.n_un.n_strx) {
844615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
845615Sbill 			    cursym.n_un.n_strx>=size)
846615Sbill 				error(1, "bad string table index (pass 1)");
847615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
848615Sbill 		}
849615Sbill 		type = cursym.n_type;
850615Sbill 		if ((type&N_EXT)==0) {
851615Sbill 			if (Xflag==0 || cursym.n_un.n_name[0]!='L' ||
852615Sbill 			    type & N_STAB)
853615Sbill 				nlocal += sizeof cursym;
854615Sbill 			continue;
855615Sbill 		}
856615Sbill 		symreloc();
857615Sbill 		if (enter(lookup()))
858615Sbill 			continue;
859615Sbill 		if ((sp = lastsym)->n_type != N_EXT+N_UNDF)
860615Sbill 			continue;
861615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
862615Sbill 			if (cursym.n_value > sp->n_value)
863615Sbill 				sp->n_value = cursym.n_value;
864615Sbill 			continue;
865615Sbill 		}
866615Sbill 		if (sp->n_value != 0 && cursym.n_type == N_EXT+N_TEXT)
867615Sbill 			continue;
868615Sbill 		ndef++;
869615Sbill 		sp->n_type = cursym.n_type;
870615Sbill 		sp->n_value = cursym.n_value;
871615Sbill 	}
872615Sbill 	if (libflg==0 || ndef) {
873615Sbill 		tsize += filhdr.a_text;
874615Sbill 		dsize += round(filhdr.a_data, sizeof (long));
875615Sbill 		bsize += round(filhdr.a_bss, sizeof (long));
876615Sbill 		ssize += nlocal;
877615Sbill 		trsize += filhdr.a_trsize;
878615Sbill 		drsize += filhdr.a_drsize;
879615Sbill 		if (funding)
880615Sbill 			textbase = (*slookup("_end"))->n_value;
881615Sbill 		nsymt = symx(nextsym);
882615Sbill 		for (i = symx(savnext); i < nsymt; i++) {
883615Sbill 			sp = xsym(i);
884615Sbill 			sp->n_un.n_name = savestr(sp->n_un.n_name);
885615Sbill 		}
886615Sbill 		free(curstr);
887615Sbill 		return (1);
888615Sbill 	}
889615Sbill 	/*
890615Sbill 	 * No symbols defined by this library member.
891615Sbill 	 * Rip out the hash table entries and reset the symbol table.
892615Sbill 	 */
893615Sbill 	symfree(savnext);
894615Sbill 	free(curstr);
895615Sbill 	return(0);
896615Sbill }
897615Sbill 
898615Sbill middle()
899615Sbill {
900615Sbill 	register struct nlist *sp;
901615Sbill 	long csize, t, corigin, ocsize;
902615Sbill 	int nund, rnd;
903615Sbill 	char s;
904615Sbill 	register int i;
905615Sbill 	int nsymt;
906615Sbill 
907615Sbill 	torigin = 0;
908615Sbill 	dorigin = 0;
909615Sbill 	borigin = 0;
910615Sbill 
911615Sbill 	p_etext = *slookup("_etext");
912615Sbill 	p_edata = *slookup("_edata");
913615Sbill 	p_end = *slookup("_end");
914615Sbill 	/*
915615Sbill 	 * If there are any undefined symbols, save the relocation bits.
916615Sbill 	 */
917615Sbill 	nsymt = symx(nextsym);
918615Sbill 	if (rflag==0) {
919615Sbill 		for (i = 0; i < nsymt; i++) {
920615Sbill 			sp = xsym(i);
921615Sbill 			if (sp->n_type==N_EXT+N_UNDF && sp->n_value==0 &&
922650Sbill 			    sp!=p_end && sp!=p_edata && sp!=p_etext) {
923615Sbill 				rflag++;
924615Sbill 				dflag = 0;
925615Sbill 				break;
926615Sbill 			}
927615Sbill 		}
928615Sbill 	}
929615Sbill 	if (rflag)
930615Sbill 		sflag = zflag = 0;
931615Sbill 	/*
932615Sbill 	 * Assign common locations.
933615Sbill 	 */
934615Sbill 	csize = 0;
935615Sbill 	if (!Aflag)
936615Sbill 		addsym = symseg[0].sy_first;
937615Sbill 	database = round(tsize+textbase,
93812671Ssam 	    (nflag||zflag? pagesize : sizeof (long)));
939615Sbill 	database += hsize;
940615Sbill 	if (dflag || rflag==0) {
941615Sbill 		ldrsym(p_etext, tsize, N_EXT+N_TEXT);
942615Sbill 		ldrsym(p_edata, dsize, N_EXT+N_DATA);
943615Sbill 		ldrsym(p_end, bsize, N_EXT+N_BSS);
944615Sbill 		for (i = symx(addsym); i < nsymt; i++) {
945615Sbill 			sp = xsym(i);
946615Sbill 			if ((s=sp->n_type)==N_EXT+N_UNDF &&
947615Sbill 			    (t = sp->n_value)!=0) {
948615Sbill 				if (t >= sizeof (double))
949615Sbill 					rnd = sizeof (double);
950615Sbill 				else if (t >= sizeof (long))
951615Sbill 					rnd = sizeof (long);
952615Sbill 				else
953615Sbill 					rnd = sizeof (short);
954615Sbill 				csize = round(csize, rnd);
955615Sbill 				sp->n_value = csize;
956615Sbill 				sp->n_type = N_EXT+N_COMM;
957615Sbill 				ocsize = csize;
958615Sbill 				csize += t;
959615Sbill 			}
960615Sbill 			if (s&N_EXT && (s&N_TYPE)==N_UNDF && s&N_STAB) {
961615Sbill 				sp->n_value = ocsize;
962615Sbill 				sp->n_type = (s&N_STAB) | (N_EXT+N_COMM);
963615Sbill 			}
964615Sbill 		}
965615Sbill 	}
966615Sbill 	/*
967615Sbill 	 * Now set symbols to their final value
968615Sbill 	 */
969615Sbill 	csize = round(csize, sizeof (long));
970615Sbill 	torigin = textbase;
971615Sbill 	dorigin = database;
972615Sbill 	corigin = dorigin + dsize;
973615Sbill 	borigin = corigin + csize;
974615Sbill 	nund = 0;
975615Sbill 	nsymt = symx(nextsym);
976615Sbill 	for (i = symx(addsym); i<nsymt; i++) {
977615Sbill 		sp = xsym(i);
978615Sbill 		switch (sp->n_type & (N_TYPE+N_EXT)) {
979615Sbill 
980615Sbill 		case N_EXT+N_UNDF:
9812369Skre 			if (arflag == 0)
9822369Skre 				errlev |= 01;
983615Sbill 			if ((arflag==0 || dflag) && sp->n_value==0) {
984650Sbill 				if (sp==p_end || sp==p_etext || sp==p_edata)
985650Sbill 					continue;
986615Sbill 				if (nund==0)
987615Sbill 					printf("Undefined:\n");
988615Sbill 				nund++;
989615Sbill 				printf("%s\n", sp->n_un.n_name);
990615Sbill 			}
991615Sbill 			continue;
992615Sbill 		case N_EXT+N_ABS:
993615Sbill 		default:
994615Sbill 			continue;
995615Sbill 		case N_EXT+N_TEXT:
996615Sbill 			sp->n_value += torigin;
997615Sbill 			continue;
998615Sbill 		case N_EXT+N_DATA:
999615Sbill 			sp->n_value += dorigin;
1000615Sbill 			continue;
1001615Sbill 		case N_EXT+N_BSS:
1002615Sbill 			sp->n_value += borigin;
1003615Sbill 			continue;
1004615Sbill 		case N_EXT+N_COMM:
1005615Sbill 			sp->n_type = (sp->n_type & N_STAB) | (N_EXT+N_BSS);
1006615Sbill 			sp->n_value += corigin;
1007615Sbill 			continue;
1008615Sbill 		}
1009615Sbill 	}
1010615Sbill 	if (sflag || xflag)
1011615Sbill 		ssize = 0;
1012615Sbill 	bsize += csize;
1013615Sbill 	nsym = ssize / (sizeof cursym);
1014615Sbill 	if (Aflag) {
1015615Sbill 		fixspec(p_etext,torigin);
1016615Sbill 		fixspec(p_edata,dorigin);
1017615Sbill 		fixspec(p_end,borigin);
1018615Sbill 	}
1019615Sbill }
1020615Sbill 
1021615Sbill fixspec(sym,offset)
1022615Sbill 	struct nlist *sym;
1023615Sbill 	long offset;
1024615Sbill {
1025615Sbill 
1026615Sbill 	if(symx(sym) < symx(addsym) && sym!=0)
1027615Sbill 		sym->n_value += offset;
1028615Sbill }
1029615Sbill 
1030615Sbill ldrsym(sp, val, type)
1031615Sbill 	register struct nlist *sp;
1032615Sbill 	long val;
1033615Sbill {
1034615Sbill 
1035615Sbill 	if (sp == 0)
1036615Sbill 		return;
1037615Sbill 	if ((sp->n_type != N_EXT+N_UNDF || sp->n_value) && !Aflag) {
1038615Sbill 		printf("%s: ", sp->n_un.n_name);
1039615Sbill 		error(0, "user attempt to redfine loader-defined symbol");
1040615Sbill 		return;
1041615Sbill 	}
1042615Sbill 	sp->n_type = type;
1043615Sbill 	sp->n_value = val;
1044615Sbill }
1045615Sbill 
1046615Sbill off_t	wroff;
1047615Sbill struct	biobuf toutb;
1048615Sbill 
1049615Sbill setupout()
1050615Sbill {
1051615Sbill 	int bss;
105216068Sralph 	struct stat stbuf;
1053898Sbill 	extern char *sys_errlist[];
1054898Sbill 	extern int errno;
1055615Sbill 
10563606Ssklower 	ofilemode = 0777 & ~umask(0);
10573606Ssklower 	biofd = creat(ofilename, 0666 & ofilemode);
1058898Sbill 	if (biofd < 0) {
1059898Sbill 		filname = ofilename;		/* kludge */
1060898Sbill 		archdr.ar_name[0] = 0;		/* kludge */
1061898Sbill 		error(1, sys_errlist[errno]);	/* kludge */
1062898Sbill 	}
106316068Sralph 	fstat(biofd, &stbuf);		/* suppose file exists, wrong*/
106416068Sralph 	if (stbuf.st_mode & 0111) {	/* mode, ld fails? */
106516068Sralph 		chmod(ofilename, stbuf.st_mode & 0666);
106616068Sralph 		ofilemode = stbuf.st_mode;
106716068Sralph 	}
1068615Sbill 	filhdr.a_magic = nflag ? NMAGIC : (zflag ? ZMAGIC : OMAGIC);
1069615Sbill 	filhdr.a_text = nflag ? tsize :
107012671Ssam 	    round(tsize, zflag ? pagesize : sizeof (long));
107112671Ssam 	filhdr.a_data = zflag ? round(dsize, pagesize) : dsize;
1072615Sbill 	bss = bsize - (filhdr.a_data - dsize);
1073615Sbill 	if (bss < 0)
1074615Sbill 		bss = 0;
1075615Sbill 	filhdr.a_bss = bss;
1076615Sbill 	filhdr.a_trsize = trsize;
1077615Sbill 	filhdr.a_drsize = drsize;
1078615Sbill 	filhdr.a_syms = sflag? 0: (ssize + (sizeof cursym)*symx(nextsym));
1079615Sbill 	if (entrypt) {
1080615Sbill 		if (entrypt->n_type!=N_EXT+N_TEXT)
1081615Sbill 			error(0, "entry point not in text");
1082615Sbill 		else
1083615Sbill 			filhdr.a_entry = entrypt->n_value;
1084615Sbill 	} else
1085615Sbill 		filhdr.a_entry = 0;
1086615Sbill 	filhdr.a_trsize = (rflag ? trsize:0);
1087615Sbill 	filhdr.a_drsize = (rflag ? drsize:0);
108816068Sralph 	tout = &toutb;
108916068Sralph 	bopen(tout, 0, stbuf.st_blksize);
1090615Sbill 	bwrite((char *)&filhdr, sizeof (filhdr), tout);
109116068Sralph 	if (zflag)
109216068Sralph 		bseek(tout, pagesize);
1093615Sbill 	wroff = N_TXTOFF(filhdr) + filhdr.a_text;
109416068Sralph 	outb(&dout, filhdr.a_data, stbuf.st_blksize);
1095615Sbill 	if (rflag) {
109616068Sralph 		outb(&trout, filhdr.a_trsize, stbuf.st_blksize);
109716068Sralph 		outb(&drout, filhdr.a_drsize, stbuf.st_blksize);
1098615Sbill 	}
1099615Sbill 	if (sflag==0 || xflag==0) {
110016068Sralph 		outb(&sout, filhdr.a_syms, stbuf.st_blksize);
1101615Sbill 		wroff += sizeof (offset);
110216068Sralph 		outb(&strout, 0, stbuf.st_blksize);
1103615Sbill 	}
1104615Sbill }
1105615Sbill 
110616068Sralph outb(bp, inc, bufsize)
1107615Sbill 	register struct biobuf **bp;
1108615Sbill {
1109615Sbill 
1110615Sbill 	*bp = (struct biobuf *)malloc(sizeof (struct biobuf));
1111615Sbill 	if (*bp == 0)
1112615Sbill 		error(1, "ran out of memory (outb)");
111316068Sralph 	bopen(*bp, wroff, bufsize);
1114615Sbill 	wroff += inc;
1115615Sbill }
1116615Sbill 
1117615Sbill load2arg(acp)
1118615Sbill char *acp;
1119615Sbill {
1120615Sbill 	register char *cp;
1121615Sbill 	off_t loc;
1122615Sbill 
1123615Sbill 	cp = acp;
1124615Sbill 	if (getfile(cp) == 0) {
1125615Sbill 		while (*cp)
1126615Sbill 			cp++;
1127615Sbill 		while (cp >= acp && *--cp != '/');
1128615Sbill 		mkfsym(++cp);
1129615Sbill 		load2(0L);
1130615Sbill 	} else {	/* scan archive members referenced */
1131615Sbill 		for (;;) {
1132615Sbill 			if (clibseg->li_used2 == clibseg->li_used) {
1133615Sbill 				if (clibseg->li_used < NROUT)
1134615Sbill 					error(1, "libseg botch");
1135615Sbill 				clibseg++;
1136615Sbill 			}
1137615Sbill 			loc = clibseg->li_first[clibseg->li_used2++];
1138615Sbill 			if (loc == -1)
1139615Sbill 				break;
1140615Sbill 			dseek(&text, loc, (long)sizeof(archdr));
1141615Sbill 			getarhdr();
1142615Sbill 			mkfsym(archdr.ar_name);
1143615Sbill 			load2(loc + (long)sizeof(archdr));
1144615Sbill 		}
1145615Sbill 	}
1146615Sbill 	close(infil);
1147615Sbill }
1148615Sbill 
1149615Sbill load2(loc)
1150615Sbill long loc;
1151615Sbill {
1152615Sbill 	int size;
1153615Sbill 	register struct nlist *sp;
1154615Sbill 	register struct local *lp;
1155615Sbill 	register int symno, i;
1156615Sbill 	int type;
1157615Sbill 
1158615Sbill 	readhdr(loc);
1159650Sbill 	if (!funding) {
1160615Sbill 		ctrel = torigin;
1161615Sbill 		cdrel += dorigin;
1162615Sbill 		cbrel += borigin;
1163615Sbill 	}
1164615Sbill 	/*
1165615Sbill 	 * Reread the symbol table, recording the numbering
1166615Sbill 	 * of symbols for fixing external references.
1167615Sbill 	 */
1168615Sbill 	for (i = 0; i < LHSIZ; i++)
1169615Sbill 		lochash[i] = 0;
1170615Sbill 	clocseg = locseg;
1171615Sbill 	clocseg->lo_used = 0;
1172615Sbill 	symno = -1;
1173615Sbill 	loc += N_TXTOFF(filhdr);
1174615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1175615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms, sizeof(off_t));
1176615Sbill 	mget(&size, sizeof(size), &text);
1177615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1178615Sbill 		filhdr.a_trsize+filhdr.a_drsize+filhdr.a_syms+sizeof(off_t),
1179615Sbill 		size - sizeof(off_t));
1180615Sbill 	curstr = (char *)malloc(size);
1181615Sbill 	if (curstr == NULL)
1182615Sbill 		error(1, "out of space reading string table (pass 2)");
1183615Sbill 	mget(curstr+sizeof(off_t), size-sizeof(off_t), &text);
1184615Sbill 	dseek(&text, loc+filhdr.a_text+filhdr.a_data+
1185615Sbill 		filhdr.a_trsize+filhdr.a_drsize, filhdr.a_syms);
1186615Sbill 	while (text.size > 0) {
1187615Sbill 		symno++;
1188615Sbill 		mget((char *)&cursym, sizeof(struct nlist), &text);
1189615Sbill 		if (cursym.n_un.n_strx) {
1190615Sbill 			if (cursym.n_un.n_strx<sizeof(size) ||
1191615Sbill 			    cursym.n_un.n_strx>=size)
1192615Sbill 				error(1, "bad string table index (pass 2)");
1193615Sbill 			cursym.n_un.n_name = curstr + cursym.n_un.n_strx;
1194615Sbill 		}
1195615Sbill /* inline expansion of symreloc() */
1196615Sbill 		switch (cursym.n_type & 017) {
1197615Sbill 
1198615Sbill 		case N_TEXT:
1199615Sbill 		case N_EXT+N_TEXT:
1200615Sbill 			cursym.n_value += ctrel;
1201615Sbill 			break;
1202615Sbill 		case N_DATA:
1203615Sbill 		case N_EXT+N_DATA:
1204615Sbill 			cursym.n_value += cdrel;
1205615Sbill 			break;
1206615Sbill 		case N_BSS:
1207615Sbill 		case N_EXT+N_BSS:
1208615Sbill 			cursym.n_value += cbrel;
1209615Sbill 			break;
1210615Sbill 		case N_EXT+N_UNDF:
1211615Sbill 			break;
1212615Sbill 		default:
1213615Sbill 			if (cursym.n_type&N_EXT)
1214615Sbill 				cursym.n_type = N_EXT+N_ABS;
1215615Sbill 		}
1216615Sbill /* end inline expansion of symreloc() */
1217615Sbill 		type = cursym.n_type;
1218898Sbill 		if (yflag && cursym.n_un.n_name)
1219898Sbill 			for (i = 0; i < yflag; i++)
1220898Sbill 				/* fast check for 2d character! */
1221898Sbill 				if (ytab[i][1] == cursym.n_un.n_name[1] &&
1222898Sbill 				    !strcmp(ytab[i], cursym.n_un.n_name)) {
1223898Sbill 					tracesym();
1224898Sbill 					break;
1225898Sbill 				}
1226615Sbill 		if ((type&N_EXT) == 0) {
1227615Sbill 			if (!sflag&&!xflag&&
1228615Sbill 			    (!Xflag||cursym.n_un.n_name[0]!='L'||type&N_STAB))
1229615Sbill 				symwrite(&cursym, sout);
1230615Sbill 			continue;
1231615Sbill 		}
1232615Sbill 		if (funding)
1233615Sbill 			continue;
1234615Sbill 		if ((sp = *lookup()) == 0)
1235615Sbill 			error(1, "internal error: symbol not found");
1236615Sbill 		if (cursym.n_type == N_EXT+N_UNDF) {
1237615Sbill 			if (clocseg->lo_used == NSYMPR) {
1238615Sbill 				if (++clocseg == &locseg[NSEG])
1239615Sbill 					error(1, "local symbol overflow");
1240615Sbill 				clocseg->lo_used = 0;
1241615Sbill 			}
1242615Sbill 			if (clocseg->lo_first == 0) {
1243615Sbill 				clocseg->lo_first = (struct local *)
1244615Sbill 				    malloc(NSYMPR * sizeof (struct local));
1245615Sbill 				if (clocseg->lo_first == 0)
1246615Sbill 					error(1, "out of memory (clocseg)");
1247615Sbill 			}
1248615Sbill 			lp = &clocseg->lo_first[clocseg->lo_used++];
1249615Sbill 			lp->l_index = symno;
1250615Sbill 			lp->l_symbol = sp;
1251615Sbill 			lp->l_link = lochash[symno % LHSIZ];
1252615Sbill 			lochash[symno % LHSIZ] = lp;
1253615Sbill 			continue;
1254615Sbill 		}
1255615Sbill 		if (cursym.n_type & N_STAB)
1256615Sbill 			continue;
1257615Sbill 		if (cursym.n_type!=sp->n_type || cursym.n_value!=sp->n_value) {
1258615Sbill 			printf("%s: ", cursym.n_un.n_name);
1259615Sbill 			error(0, "multiply defined");
1260615Sbill 		}
1261615Sbill 	}
1262615Sbill 	if (funding)
1263615Sbill 		return;
1264615Sbill 	dseek(&text, loc, filhdr.a_text);
1265615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data, filhdr.a_trsize);
1266650Sbill 	load2td(ctrel, torigin - textbase, tout, trout);
1267615Sbill 	dseek(&text, loc+filhdr.a_text, filhdr.a_data);
1268615Sbill 	dseek(&reloc, loc+filhdr.a_text+filhdr.a_data+filhdr.a_trsize,
1269615Sbill 	    filhdr.a_drsize);
1270650Sbill 	load2td(cdrel, dorigin - database, dout, drout);
1271615Sbill 	while (filhdr.a_data & (sizeof(long)-1)) {
1272615Sbill 		bputc(0, dout);
1273615Sbill 		filhdr.a_data++;
1274615Sbill 	}
1275615Sbill 	torigin += filhdr.a_text;
12761752Sbill 	dorigin += round(filhdr.a_data, sizeof (long));
12771752Sbill 	borigin += round(filhdr.a_bss, sizeof (long));
1278615Sbill 	free(curstr);
1279615Sbill }
1280615Sbill 
1281898Sbill struct tynames {
1282898Sbill 	int	ty_value;
1283898Sbill 	char	*ty_name;
1284898Sbill } tynames[] = {
1285898Sbill 	N_UNDF,	"undefined",
1286898Sbill 	N_ABS,	"absolute",
1287898Sbill 	N_TEXT,	"text",
1288898Sbill 	N_DATA,	"data",
1289898Sbill 	N_BSS,	"bss",
1290898Sbill 	N_COMM,	"common",
1291898Sbill 	0,	0,
1292898Sbill };
1293898Sbill 
1294898Sbill tracesym()
1295898Sbill {
1296898Sbill 	register struct tynames *tp;
1297898Sbill 
1298898Sbill 	if (cursym.n_type & N_STAB)
1299898Sbill 		return;
1300898Sbill 	printf("%s", filname);
1301898Sbill 	if (archdr.ar_name[0])
1302898Sbill 		printf("(%s)", archdr.ar_name);
1303898Sbill 	printf(": ");
1304898Sbill 	if ((cursym.n_type&N_TYPE) == N_UNDF && cursym.n_value) {
1305898Sbill 		printf("definition of common %s size %d\n",
1306898Sbill 		    cursym.n_un.n_name, cursym.n_value);
1307898Sbill 		return;
1308898Sbill 	}
1309898Sbill 	for (tp = tynames; tp->ty_name; tp++)
1310898Sbill 		if (tp->ty_value == (cursym.n_type&N_TYPE))
1311898Sbill 			break;
1312898Sbill 	printf((cursym.n_type&N_TYPE) ? "definition of" : "reference to");
1313898Sbill 	if (cursym.n_type&N_EXT)
1314898Sbill 		printf(" external");
1315898Sbill 	if (tp->ty_name)
1316898Sbill 		printf(" %s", tp->ty_name);
1317898Sbill 	printf(" %s\n", cursym.n_un.n_name);
1318898Sbill }
1319898Sbill 
132029845Ssam #if !defined(tahoe)
132129845Ssam /* for machines which allow arbitrarily aligned word and longword accesses */
132229845Ssam #define	getw(cp)	(*(short *)(cp))
132329845Ssam #define	getl(cp)	(*(long *)(cp))
132429845Ssam #define	putw(cp, w)	(*(short *)(cp) = (w))
132529845Ssam #define	putl(cp, l)	(*(long *)(cp) = (l))
132629845Ssam #else
132729845Ssam short
132829845Ssam getw(cp)
132929845Ssam 	char *cp;
133029845Ssam {
133129845Ssam 	union {
133229845Ssam 		short	w;
133329845Ssam 		char	c[2];
133429845Ssam 	} w;
133529845Ssam 
133629845Ssam 	w.c[0] = *cp++;
133729845Ssam 	w.c[1] = *cp++;
133829845Ssam 	return (w.w);
133929845Ssam }
134029845Ssam 
134129845Ssam getl(cp)
134229845Ssam 	char *cp;
134329845Ssam {
134429845Ssam 	union {
134529845Ssam 		long	l;
134629845Ssam 		char	c[4];
134729845Ssam 	} l;
134829845Ssam 
134929845Ssam 	l.c[0] = *cp++;
135029845Ssam 	l.c[1] = *cp++;
135129845Ssam 	l.c[2] = *cp++;
135229845Ssam 	l.c[3] = *cp++;
135329845Ssam 	return (l.l);
135429845Ssam }
135529845Ssam 
135629845Ssam putw(cp, v)
135729845Ssam 	char *cp;
135829845Ssam 	short v;
135929845Ssam {
136029845Ssam 	union {
136129845Ssam 		short	w;
136229845Ssam 		char	c[2];
136329845Ssam 	} w;
136429845Ssam 
136529845Ssam 	w.w = v;
136629845Ssam 	*cp++ = w.c[0];
136729845Ssam 	*cp++ = w.c[1];
136829845Ssam }
136929845Ssam 
137029845Ssam putl(cp, v)
137129845Ssam 	char *cp;
137229845Ssam 	long v;
137329845Ssam {
137429845Ssam 	union {
137529845Ssam 		long	l;
137629845Ssam 		char	c[4];
137729845Ssam 	} l;
137829845Ssam 
137929845Ssam 	l.l = v;
138029845Ssam 	*cp++ = l.c[0];
138129845Ssam 	*cp++ = l.c[1];
138229845Ssam 	*cp++ = l.c[2];
138329845Ssam 	*cp++ = l.c[3];
138429845Ssam }
138529845Ssam #endif
138629845Ssam 
1387650Sbill /*
1388650Sbill  * This routine relocates the single text or data segment argument.
1389650Sbill  * Offsets from external symbols are resolved by adding the value
1390650Sbill  * of the external symbols.  Non-external reference are updated to account
1391650Sbill  * for the relative motion of the segments (ctrel, cdrel, ...).  If
1392650Sbill  * a relocation was pc-relative, then we update it to reflect the
1393650Sbill  * change in the positioning of the segments by adding the displacement
1394650Sbill  * of the referenced segment and subtracting the displacement of the
1395650Sbill  * current segment (creloc).
1396650Sbill  *
1397650Sbill  * If we are saving the relocation information, then we increase
1398650Sbill  * each relocation datum address by our base position in the new segment.
1399650Sbill  */
1400650Sbill load2td(creloc, position, b1, b2)
140130647Slepreau 	long creloc, position;
1402615Sbill 	struct biobuf *b1, *b2;
1403615Sbill {
1404615Sbill 	register struct nlist *sp;
1405615Sbill 	register struct local *lp;
1406615Sbill 	long tw;
1407615Sbill 	register struct relocation_info *rp, *rpend;
1408615Sbill 	struct relocation_info *relp;
1409615Sbill 	char *codep;
1410615Sbill 	register char *cp;
1411615Sbill 	int relsz, codesz;
1412615Sbill 
1413615Sbill 	relsz = reloc.size;
1414615Sbill 	relp = (struct relocation_info *)malloc(relsz);
1415615Sbill 	codesz = text.size;
1416615Sbill 	codep = (char *)malloc(codesz);
1417615Sbill 	if (relp == 0 || codep == 0)
1418615Sbill 		error(1, "out of memory (load2td)");
1419615Sbill 	mget((char *)relp, relsz, &reloc);
1420615Sbill 	rpend = &relp[relsz / sizeof (struct relocation_info)];
1421615Sbill 	mget(codep, codesz, &text);
1422615Sbill 	for (rp = relp; rp < rpend; rp++) {
1423615Sbill 		cp = codep + rp->r_address;
1424650Sbill 		/*
1425650Sbill 		 * Pick up previous value at location to be relocated.
1426650Sbill 		 */
1427615Sbill 		switch (rp->r_length) {
1428615Sbill 
1429615Sbill 		case 0:		/* byte */
1430615Sbill 			tw = *cp;
1431615Sbill 			break;
1432615Sbill 
1433615Sbill 		case 1:		/* word */
143429845Ssam 			tw = getw(cp);
1435615Sbill 			break;
1436615Sbill 
1437615Sbill 		case 2:		/* long */
143829845Ssam 			tw = getl(cp);
1439615Sbill 			break;
1440615Sbill 
1441615Sbill 		default:
1442615Sbill 			error(1, "load2td botch: bad length");
1443615Sbill 		}
1444650Sbill 		/*
1445650Sbill 		 * If relative to an external which is defined,
1446650Sbill 		 * resolve to a simpler kind of reference in the
1447650Sbill 		 * result file.  If the external is undefined, just
1448650Sbill 		 * convert the symbol number to the number of the
1449650Sbill 		 * symbol in the result file and leave it undefined.
1450650Sbill 		 */
1451615Sbill 		if (rp->r_extern) {
1452650Sbill 			/*
1453650Sbill 			 * Search the hash table which maps local
1454650Sbill 			 * symbol numbers to symbol tables entries
1455650Sbill 			 * in the new a.out file.
1456650Sbill 			 */
1457615Sbill 			lp = lochash[rp->r_symbolnum % LHSIZ];
1458615Sbill 			while (lp->l_index != rp->r_symbolnum) {
1459615Sbill 				lp = lp->l_link;
1460615Sbill 				if (lp == 0)
1461615Sbill 					error(1, "local symbol botch");
1462615Sbill 			}
1463615Sbill 			sp = lp->l_symbol;
1464615Sbill 			if (sp->n_type == N_EXT+N_UNDF)
1465615Sbill 				rp->r_symbolnum = nsym+symx(sp);
1466615Sbill 			else {
1467615Sbill 				rp->r_symbolnum = sp->n_type & N_TYPE;
1468615Sbill 				tw += sp->n_value;
1469615Sbill 				rp->r_extern = 0;
1470615Sbill 			}
1471615Sbill 		} else switch (rp->r_symbolnum & N_TYPE) {
1472650Sbill 		/*
1473650Sbill 		 * Relocation is relative to the loaded position
1474650Sbill 		 * of another segment.  Update by the change in position
1475650Sbill 		 * of that segment.
1476650Sbill 		 */
1477615Sbill 		case N_TEXT:
1478615Sbill 			tw += ctrel;
1479615Sbill 			break;
1480615Sbill 		case N_DATA:
1481615Sbill 			tw += cdrel;
1482615Sbill 			break;
1483615Sbill 		case N_BSS:
1484615Sbill 			tw += cbrel;
1485615Sbill 			break;
1486615Sbill 		case N_ABS:
1487615Sbill 			break;
1488615Sbill 		default:
1489615Sbill 			error(1, "relocation format botch (symbol type))");
1490615Sbill 		}
1491650Sbill 		/*
1492650Sbill 		 * Relocation is pc relative, so decrease the relocation
1493650Sbill 		 * by the amount the current segment is displaced.
1494650Sbill 		 * (E.g if we are a relative reference to a text location
1495650Sbill 		 * from data space, we added the increase in the text address
1496650Sbill 		 * above, and subtract the increase in our (data) address
1497650Sbill 		 * here, leaving the net change the relative change in the
1498650Sbill 		 * positioning of our text and data segments.)
1499650Sbill 		 */
1500615Sbill 		if (rp->r_pcrel)
1501615Sbill 			tw -= creloc;
1502650Sbill 		/*
1503650Sbill 		 * Put the value back in the segment,
1504650Sbill 		 * while checking for overflow.
1505650Sbill 		 */
1506615Sbill 		switch (rp->r_length) {
1507615Sbill 
1508615Sbill 		case 0:		/* byte */
1509615Sbill 			if (tw < -128 || tw > 127)
1510615Sbill 				error(0, "byte displacement overflow");
1511615Sbill 			*cp = tw;
1512615Sbill 			break;
1513615Sbill 		case 1:		/* word */
1514615Sbill 			if (tw < -32768 || tw > 32767)
1515615Sbill 				error(0, "word displacement overflow");
151629845Ssam 			putw(cp, tw);
1517615Sbill 			break;
1518615Sbill 		case 2:		/* long */
151929845Ssam 			putl(cp, tw);
1520615Sbill 			break;
1521615Sbill 		}
1522650Sbill 		/*
1523650Sbill 		 * If we are saving relocation information,
1524650Sbill 		 * we must convert the address in the segment from
1525650Sbill 		 * the old .o file into an address in the segment in
1526650Sbill 		 * the new a.out, by adding the position of our
1527650Sbill 		 * segment in the new larger segment.
1528650Sbill 		 */
1529615Sbill 		if (rflag)
1530650Sbill 			rp->r_address += position;
1531615Sbill 	}
1532615Sbill 	bwrite(codep, codesz, b1);
1533615Sbill 	if (rflag)
1534615Sbill 		bwrite(relp, relsz, b2);
153525483Slepreau 	free((char *)relp);
153625483Slepreau 	free(codep);
1537615Sbill }
1538615Sbill 
1539615Sbill finishout()
1540615Sbill {
1541615Sbill 	register int i;
1542*40407Smckusick 	char *newname;
1543615Sbill 	int nsymt;
1544615Sbill 
1545615Sbill 	if (sflag==0) {
1546615Sbill 		nsymt = symx(nextsym);
1547615Sbill 		for (i = 0; i < nsymt; i++)
1548615Sbill 			symwrite(xsym(i), sout);
1549615Sbill 		bwrite(&offset, sizeof offset, sout);
1550615Sbill 	}
1551615Sbill 	if (!ofilfnd) {
1552*40407Smckusick 		newname = (char *)genbuildname("a.out");
1553*40407Smckusick 		unlink(newname);
1554*40407Smckusick 		if (link(defaultname, newname) < 0)
1555898Sbill 			error(1, "cannot move l.out to a.out");
1556*40407Smckusick 		ofilename = newname;
1557615Sbill 	}
1558615Sbill 	delarg = errlev;
1559615Sbill 	delexit();
1560615Sbill }
1561615Sbill 
1562615Sbill mkfsym(s)
1563615Sbill char *s;
1564615Sbill {
1565615Sbill 
1566615Sbill 	if (sflag || xflag)
1567615Sbill 		return;
1568615Sbill 	cursym.n_un.n_name = s;
156930836Sbostic 	cursym.n_type = N_EXT | N_FN;
1570615Sbill 	cursym.n_value = torigin;
1571615Sbill 	symwrite(&cursym, sout);
1572615Sbill }
1573615Sbill 
1574615Sbill getarhdr()
1575615Sbill {
1576615Sbill 	register char *cp;
1577615Sbill 
1578615Sbill 	mget((char *)&archdr, sizeof archdr, &text);
1579615Sbill 	for (cp=archdr.ar_name; cp<&archdr.ar_name[sizeof(archdr.ar_name)];)
1580615Sbill 		if (*cp++ == ' ') {
1581615Sbill 			cp[-1] = 0;
1582615Sbill 			return;
1583615Sbill 		}
1584615Sbill }
1585615Sbill 
1586615Sbill mget(loc, n, sp)
1587615Sbill register STREAM *sp;
1588615Sbill register char *loc;
1589615Sbill {
1590615Sbill 	register char *p;
1591615Sbill 	register int take;
1592615Sbill 
1593615Sbill top:
1594615Sbill 	if (n == 0)
1595615Sbill 		return;
1596615Sbill 	if (sp->size && sp->nibuf) {
1597615Sbill 		p = sp->ptr;
1598615Sbill 		take = sp->size;
1599615Sbill 		if (take > sp->nibuf)
1600615Sbill 			take = sp->nibuf;
1601615Sbill 		if (take > n)
1602615Sbill 			take = n;
1603615Sbill 		n -= take;
1604615Sbill 		sp->size -= take;
1605615Sbill 		sp->nibuf -= take;
1606615Sbill 		sp->pos += take;
1607615Sbill 		do
1608615Sbill 			*loc++ = *p++;
1609615Sbill 		while (--take > 0);
1610615Sbill 		sp->ptr = p;
1611615Sbill 		goto top;
1612615Sbill 	}
161316068Sralph 	if (n > p_blksize) {
161416068Sralph 		take = n - n % p_blksize;
161516068Sralph 		lseek(infil, (sp->bno+1)<<p_blkshift, 0);
1616615Sbill 		if (take > sp->size || read(infil, loc, take) != take)
1617615Sbill 			error(1, "premature EOF");
1618615Sbill 		loc += take;
1619615Sbill 		n -= take;
1620615Sbill 		sp->size -= take;
1621615Sbill 		sp->pos += take;
162216068Sralph 		dseek(sp, (sp->bno+1+(take>>p_blkshift))<<p_blkshift, -1);
1623615Sbill 		goto top;
1624615Sbill 	}
1625615Sbill 	*loc++ = get(sp);
1626615Sbill 	--n;
1627615Sbill 	goto top;
1628615Sbill }
1629615Sbill 
1630615Sbill symwrite(sp, bp)
1631615Sbill 	struct nlist *sp;
1632615Sbill 	struct biobuf *bp;
1633615Sbill {
1634615Sbill 	register int len;
1635615Sbill 	register char *str;
1636615Sbill 
1637615Sbill 	str = sp->n_un.n_name;
1638615Sbill 	if (str) {
1639615Sbill 		sp->n_un.n_strx = offset;
1640615Sbill 		len = strlen(str) + 1;
1641615Sbill 		bwrite(str, len, strout);
1642615Sbill 		offset += len;
1643615Sbill 	}
1644615Sbill 	bwrite(sp, sizeof (*sp), bp);
1645615Sbill 	sp->n_un.n_name = str;
1646615Sbill }
1647615Sbill 
1648615Sbill dseek(sp, loc, s)
1649615Sbill register STREAM *sp;
1650615Sbill long loc, s;
1651615Sbill {
1652615Sbill 	register PAGE *p;
1653615Sbill 	register b, o;
1654615Sbill 	int n;
1655615Sbill 
165616068Sralph 	b = loc>>p_blkshift;
165716068Sralph 	o = loc&p_blkmask;
1658615Sbill 	if (o&01)
1659615Sbill 		error(1, "loader error; odd offset");
1660615Sbill 	--sp->pno->nuser;
1661615Sbill 	if ((p = &page[0])->bno!=b && (p = &page[1])->bno!=b)
1662615Sbill 		if (p->nuser==0 || (p = &page[0])->nuser==0) {
1663615Sbill 			if (page[0].nuser==0 && page[1].nuser==0)
1664615Sbill 				if (page[0].bno < page[1].bno)
1665615Sbill 					p = &page[0];
1666615Sbill 			p->bno = b;
166716068Sralph 			lseek(infil, loc & ~(long)p_blkmask, 0);
166816068Sralph 			if ((n = read(infil, p->buff, p_blksize)) < 0)
1669615Sbill 				n = 0;
1670615Sbill 			p->nibuf = n;
167116068Sralph 		} else
167216068Sralph 			error(1, "botch: no pages");
1673615Sbill 	++p->nuser;
1674615Sbill 	sp->bno = b;
1675615Sbill 	sp->pno = p;
1676615Sbill 	if (s != -1) {sp->size = s; sp->pos = 0;}
1677615Sbill 	sp->ptr = (char *)(p->buff + o);
1678615Sbill 	if ((sp->nibuf = p->nibuf-o) <= 0)
1679615Sbill 		sp->size = 0;
1680615Sbill }
1681615Sbill 
1682615Sbill char
1683615Sbill get(asp)
1684615Sbill STREAM *asp;
1685615Sbill {
1686615Sbill 	register STREAM *sp;
1687615Sbill 
1688615Sbill 	sp = asp;
1689615Sbill 	if ((sp->nibuf -= sizeof(char)) < 0) {
169016068Sralph 		dseek(sp, ((long)(sp->bno+1)<<p_blkshift), (long)-1);
1691615Sbill 		sp->nibuf -= sizeof(char);
1692615Sbill 	}
1693615Sbill 	if ((sp->size -= sizeof(char)) <= 0) {
1694615Sbill 		if (sp->size < 0)
1695615Sbill 			error(1, "premature EOF");
1696615Sbill 		++fpage.nuser;
1697615Sbill 		--sp->pno->nuser;
1698615Sbill 		sp->pno = (PAGE *) &fpage;
1699615Sbill 	}
1700615Sbill 	sp->pos += sizeof(char);
1701615Sbill 	return(*sp->ptr++);
1702615Sbill }
1703615Sbill 
1704615Sbill getfile(acp)
1705615Sbill char *acp;
1706615Sbill {
1707615Sbill 	register int c;
1708615Sbill 	char arcmag[SARMAG+1];
1709615Sbill 	struct stat stb;
1710615Sbill 
1711615Sbill 	archdr.ar_name[0] = '\0';
171217133Ssam 	filname = acp;
171317133Ssam 	if (filname[0] == '-' && filname[1] == 'l')
171417133Ssam 		infil = libopen(filname + 2, O_RDONLY);
171517133Ssam 	else
1716*40407Smckusick 		infil = open((char *)genbuildname(filname), O_RDONLY);
171717133Ssam 	if (infil < 0)
1718615Sbill 		error(1, "cannot open");
171916068Sralph 	fstat(infil, &stb);
1720615Sbill 	page[0].bno = page[1].bno = -1;
1721615Sbill 	page[0].nuser = page[1].nuser = 0;
172216068Sralph 	c = stb.st_blksize;
172316068Sralph 	if (c == 0 || (c & (c - 1)) != 0) {
172416068Sralph 		/* use default size if not a power of two */
172516068Sralph 		c = BLKSIZE;
172616068Sralph 	}
172716068Sralph 	if (p_blksize != c) {
172816068Sralph 		p_blksize = c;
172916068Sralph 		p_blkmask = c - 1;
173016068Sralph 		for (p_blkshift = 0; c > 1 ; p_blkshift++)
173116068Sralph 			c >>= 1;
173216068Sralph 		if (page[0].buff != NULL)
173316068Sralph 			free(page[0].buff);
173416068Sralph 		page[0].buff = (char *)malloc(p_blksize);
173516068Sralph 		if (page[0].buff == NULL)
173616068Sralph 			error(1, "ran out of memory (getfile)");
173716068Sralph 		if (page[1].buff != NULL)
173816068Sralph 			free(page[1].buff);
173916068Sralph 		page[1].buff = (char *)malloc(p_blksize);
174016068Sralph 		if (page[1].buff == NULL)
174116068Sralph 			error(1, "ran out of memory (getfile)");
174216068Sralph 	}
1743615Sbill 	text.pno = reloc.pno = (PAGE *) &fpage;
1744615Sbill 	fpage.nuser = 2;
1745615Sbill 	dseek(&text, 0L, SARMAG);
1746615Sbill 	if (text.size <= 0)
1747615Sbill 		error(1, "premature EOF");
1748615Sbill 	mget((char *)arcmag, SARMAG, &text);
1749615Sbill 	arcmag[SARMAG] = 0;
1750615Sbill 	if (strcmp(arcmag, ARMAG))
1751615Sbill 		return (0);
1752615Sbill 	dseek(&text, SARMAG, sizeof archdr);
175317133Ssam 	if (text.size <= 0)
1754615Sbill 		return (1);
1755615Sbill 	getarhdr();
175630828Sbostic 	if (strncmp(archdr.ar_name, RANLIBMAG, sizeof(archdr.ar_name)) != 0)
1757615Sbill 		return (1);
1758615Sbill 	return (stb.st_mtime > atol(archdr.ar_date) ? 3 : 2);
1759615Sbill }
1760615Sbill 
176117133Ssam /*
176217133Ssam  * Search for a library with given name
176317133Ssam  * using the directory search array.
176417133Ssam  */
176517133Ssam libopen(name, oflags)
176617133Ssam 	char *name;
176717133Ssam 	int oflags;
176817133Ssam {
176917133Ssam 	register char *p, *cp;
177017133Ssam 	register int i;
177117133Ssam 	static char buf[MAXPATHLEN+1];
177217133Ssam 	int fd = -1;
177317133Ssam 
177417133Ssam 	if (*name == '\0')			/* backwards compat */
177517133Ssam 		name = "a";
177617133Ssam 	for (i = 0; i < ndir && fd == -1; i++) {
177717133Ssam 		p = buf;
177817133Ssam 		for (cp = dirs[i]; *cp; *p++ = *cp++)
177917133Ssam 			;
178017133Ssam 		*p++ = '/';
178117133Ssam 		for (cp = "lib"; *cp; *p++ = *cp++)
178217133Ssam 			;
178317133Ssam 		for (cp = name; *cp; *p++ = *cp++)
178417133Ssam 			;
178517133Ssam 		cp = ".a";
178617133Ssam 		while (*p++ = *cp++)
178717133Ssam 			;
178817133Ssam 		fd = open(buf, oflags);
178917133Ssam 	}
179017133Ssam 	if (fd != -1)
179117133Ssam 		filname = buf;
179217133Ssam 	return (fd);
179317133Ssam }
179417133Ssam 
1795615Sbill struct nlist **
1796615Sbill lookup()
1797615Sbill {
1798615Sbill 	register int sh;
1799615Sbill 	register struct nlist **hp;
1800615Sbill 	register char *cp, *cp1;
1801615Sbill 	register struct symseg *gp;
1802615Sbill 	register int i;
1803615Sbill 
1804615Sbill 	sh = 0;
1805615Sbill 	for (cp = cursym.n_un.n_name; *cp;)
1806615Sbill 		sh = (sh<<1) + *cp++;
1807615Sbill 	sh = (sh & 0x7fffffff) % HSIZE;
1808615Sbill 	for (gp = symseg; gp < &symseg[NSEG]; gp++) {
1809615Sbill 		if (gp->sy_first == 0) {
1810615Sbill 			gp->sy_first = (struct nlist *)
1811615Sbill 			    calloc(NSYM, sizeof (struct nlist));
1812615Sbill 			gp->sy_hfirst = (struct nlist **)
1813615Sbill 			    calloc(HSIZE, sizeof (struct nlist *));
1814615Sbill 			if (gp->sy_first == 0 || gp->sy_hfirst == 0)
1815615Sbill 				error(1, "ran out of space for symbol table");
1816615Sbill 			gp->sy_last = gp->sy_first + NSYM;
1817615Sbill 			gp->sy_hlast = gp->sy_hfirst + HSIZE;
1818615Sbill 		}
1819615Sbill 		if (gp > csymseg)
1820615Sbill 			csymseg = gp;
1821615Sbill 		hp = gp->sy_hfirst + sh;
1822615Sbill 		i = 1;
1823615Sbill 		do {
1824615Sbill 			if (*hp == 0) {
1825615Sbill 				if (gp->sy_used == NSYM)
1826615Sbill 					break;
1827615Sbill 				return (hp);
1828615Sbill 			}
1829615Sbill 			cp1 = (*hp)->n_un.n_name;
1830615Sbill 			for (cp = cursym.n_un.n_name; *cp == *cp1++;)
1831615Sbill 				if (*cp++ == 0)
1832615Sbill 					return (hp);
1833615Sbill 			hp += i;
1834615Sbill 			i += 2;
1835615Sbill 			if (hp >= gp->sy_hlast)
1836615Sbill 				hp -= HSIZE;
1837615Sbill 		} while (i < HSIZE);
1838615Sbill 		if (i > HSIZE)
1839615Sbill 			error(1, "hash table botch");
1840615Sbill 	}
1841615Sbill 	error(1, "symbol table overflow");
1842615Sbill 	/*NOTREACHED*/
1843615Sbill }
1844615Sbill 
1845615Sbill symfree(saved)
1846615Sbill 	struct nlist *saved;
1847615Sbill {
1848615Sbill 	register struct symseg *gp;
1849615Sbill 	register struct nlist *sp;
1850615Sbill 
1851615Sbill 	for (gp = csymseg; gp >= symseg; gp--, csymseg--) {
1852615Sbill 		sp = gp->sy_first + gp->sy_used;
1853615Sbill 		if (sp == saved) {
1854615Sbill 			nextsym = sp;
1855615Sbill 			return;
1856615Sbill 		}
1857615Sbill 		for (sp--; sp >= gp->sy_first; sp--) {
1858615Sbill 			gp->sy_hfirst[sp->n_hash] = 0;
1859615Sbill 			gp->sy_used--;
1860615Sbill 			if (sp == saved) {
1861615Sbill 				nextsym = sp;
1862615Sbill 				return;
1863615Sbill 			}
1864615Sbill 		}
1865615Sbill 	}
1866615Sbill 	if (saved == 0)
1867615Sbill 		return;
1868615Sbill 	error(1, "symfree botch");
1869615Sbill }
1870615Sbill 
1871615Sbill struct nlist **
1872615Sbill slookup(s)
1873615Sbill 	char *s;
1874615Sbill {
1875615Sbill 
1876615Sbill 	cursym.n_un.n_name = s;
1877615Sbill 	cursym.n_type = N_EXT+N_UNDF;
1878615Sbill 	cursym.n_value = 0;
1879615Sbill 	return (lookup());
1880615Sbill }
1881615Sbill 
1882615Sbill enter(hp)
1883615Sbill register struct nlist **hp;
1884615Sbill {
1885615Sbill 	register struct nlist *sp;
1886615Sbill 
1887615Sbill 	if (*hp==0) {
1888615Sbill 		if (hp < csymseg->sy_hfirst || hp >= csymseg->sy_hlast)
1889615Sbill 			error(1, "enter botch");
1890615Sbill 		*hp = lastsym = sp = csymseg->sy_first + csymseg->sy_used;
1891615Sbill 		csymseg->sy_used++;
1892615Sbill 		sp->n_un.n_name = cursym.n_un.n_name;
1893615Sbill 		sp->n_type = cursym.n_type;
1894615Sbill 		sp->n_hash = hp - csymseg->sy_hfirst;
1895615Sbill 		sp->n_value = cursym.n_value;
1896615Sbill 		nextsym = lastsym + 1;
1897615Sbill 		return(1);
1898615Sbill 	} else {
1899615Sbill 		lastsym = *hp;
1900615Sbill 		return(0);
1901615Sbill 	}
1902615Sbill }
1903615Sbill 
1904615Sbill symx(sp)
1905615Sbill 	struct nlist *sp;
1906615Sbill {
1907615Sbill 	register struct symseg *gp;
1908615Sbill 
1909615Sbill 	if (sp == 0)
1910615Sbill 		return (0);
1911615Sbill 	for (gp = csymseg; gp >= symseg; gp--)
1912615Sbill 		/* <= is sloppy so nextsym will always work */
1913615Sbill 		if (sp >= gp->sy_first && sp <= gp->sy_last)
1914615Sbill 			return ((gp - symseg) * NSYM + sp - gp->sy_first);
1915615Sbill 	error(1, "symx botch");
1916615Sbill 	/*NOTREACHED*/
1917615Sbill }
1918615Sbill 
1919615Sbill symreloc()
1920615Sbill {
1921615Sbill 	if(funding) return;
1922615Sbill 	switch (cursym.n_type & 017) {
1923615Sbill 
1924615Sbill 	case N_TEXT:
1925615Sbill 	case N_EXT+N_TEXT:
1926615Sbill 		cursym.n_value += ctrel;
1927615Sbill 		return;
1928615Sbill 
1929615Sbill 	case N_DATA:
1930615Sbill 	case N_EXT+N_DATA:
1931615Sbill 		cursym.n_value += cdrel;
1932615Sbill 		return;
1933615Sbill 
1934615Sbill 	case N_BSS:
1935615Sbill 	case N_EXT+N_BSS:
1936615Sbill 		cursym.n_value += cbrel;
1937615Sbill 		return;
1938615Sbill 
1939615Sbill 	case N_EXT+N_UNDF:
1940615Sbill 		return;
1941615Sbill 
1942615Sbill 	default:
1943615Sbill 		if (cursym.n_type&N_EXT)
1944615Sbill 			cursym.n_type = N_EXT+N_ABS;
1945615Sbill 		return;
1946615Sbill 	}
1947615Sbill }
1948615Sbill 
1949615Sbill error(n, s)
1950615Sbill char *s;
1951615Sbill {
1952898Sbill 
1953615Sbill 	if (errlev==0)
1954615Sbill 		printf("ld:");
1955615Sbill 	if (filname) {
1956615Sbill 		printf("%s", filname);
1957615Sbill 		if (n != -1 && archdr.ar_name[0])
1958615Sbill 			printf("(%s)", archdr.ar_name);
1959615Sbill 		printf(": ");
1960615Sbill 	}
1961615Sbill 	printf("%s\n", s);
1962615Sbill 	if (n == -1)
1963615Sbill 		return;
1964615Sbill 	if (n)
1965615Sbill 		delexit();
1966615Sbill 	errlev = 2;
1967615Sbill }
1968615Sbill 
1969615Sbill readhdr(loc)
1970615Sbill off_t loc;
1971615Sbill {
1972615Sbill 
1973615Sbill 	dseek(&text, loc, (long)sizeof(filhdr));
1974615Sbill 	mget((short *)&filhdr, sizeof(filhdr), &text);
1975615Sbill 	if (N_BADMAG(filhdr)) {
1976615Sbill 		if (filhdr.a_magic == OARMAG)
1977615Sbill 			error(1, "old archive");
1978615Sbill 		error(1, "bad magic number");
1979615Sbill 	}
1980615Sbill 	if (filhdr.a_text&01 || filhdr.a_data&01)
1981615Sbill 		error(1, "text/data size odd");
1982615Sbill 	if (filhdr.a_magic == NMAGIC || filhdr.a_magic == ZMAGIC) {
198312671Ssam 		cdrel = -round(filhdr.a_text, pagesize);
1984615Sbill 		cbrel = cdrel - filhdr.a_data;
1985615Sbill 	} else if (filhdr.a_magic == OMAGIC) {
1986615Sbill 		cdrel = -filhdr.a_text;
1987615Sbill 		cbrel = cdrel - filhdr.a_data;
1988615Sbill 	} else
1989615Sbill 		error(1, "bad format");
1990615Sbill }
1991615Sbill 
1992615Sbill round(v, r)
1993615Sbill 	int v;
1994615Sbill 	u_long r;
1995615Sbill {
1996615Sbill 
1997615Sbill 	r--;
1998615Sbill 	v += r;
1999615Sbill 	v &= ~(long)r;
2000615Sbill 	return(v);
2001615Sbill }
2002615Sbill 
2003615Sbill #define	NSAVETAB	8192
2004615Sbill char	*savetab;
2005615Sbill int	saveleft;
2006615Sbill 
2007615Sbill char *
2008615Sbill savestr(cp)
2009615Sbill 	register char *cp;
2010615Sbill {
2011615Sbill 	register int len;
2012615Sbill 
2013615Sbill 	len = strlen(cp) + 1;
2014615Sbill 	if (len > saveleft) {
2015615Sbill 		saveleft = NSAVETAB;
2016615Sbill 		if (len > saveleft)
2017615Sbill 			saveleft = len;
201817133Ssam 		savetab = malloc(saveleft);
2019615Sbill 		if (savetab == 0)
2020615Sbill 			error(1, "ran out of memory (savestr)");
2021615Sbill 	}
2022615Sbill 	strncpy(savetab, cp, len);
2023615Sbill 	cp = savetab;
2024615Sbill 	savetab += len;
2025615Sbill 	saveleft -= len;
2026615Sbill 	return (cp);
2027615Sbill }
2028615Sbill 
202916068Sralph bopen(bp, off, bufsize)
203016068Sralph 	register struct biobuf *bp;
2031615Sbill {
2032615Sbill 
203317133Ssam 	bp->b_ptr = bp->b_buf = malloc(bufsize);
203416068Sralph 	if (bp->b_ptr == (char *)0)
203516068Sralph 		error(1, "ran out of memory (bopen)");
203616068Sralph 	bp->b_bufsize = bufsize;
203716068Sralph 	bp->b_nleft = bufsize - (off % bufsize);
2038615Sbill 	bp->b_off = off;
2039615Sbill 	bp->b_link = biobufs;
2040615Sbill 	biobufs = bp;
2041615Sbill }
2042615Sbill 
2043615Sbill int	bwrerror;
2044615Sbill 
2045615Sbill bwrite(p, cnt, bp)
2046615Sbill 	register char *p;
2047615Sbill 	register int cnt;
2048615Sbill 	register struct biobuf *bp;
2049615Sbill {
2050615Sbill 	register int put;
2051615Sbill 	register char *to;
2052615Sbill 
2053615Sbill top:
2054615Sbill 	if (cnt == 0)
2055615Sbill 		return;
2056615Sbill 	if (bp->b_nleft) {
2057615Sbill 		put = bp->b_nleft;
2058615Sbill 		if (put > cnt)
2059615Sbill 			put = cnt;
2060615Sbill 		bp->b_nleft -= put;
2061615Sbill 		to = bp->b_ptr;
206225419Sbloom 		bcopy(p, to, put);
2063615Sbill 		bp->b_ptr += put;
2064615Sbill 		p += put;
2065615Sbill 		cnt -= put;
2066615Sbill 		goto top;
2067615Sbill 	}
206816068Sralph 	if (cnt >= bp->b_bufsize) {
2069615Sbill 		if (bp->b_ptr != bp->b_buf)
2070615Sbill 			bflush1(bp);
207116068Sralph 		put = cnt - cnt % bp->b_bufsize;
2072615Sbill 		if (boffset != bp->b_off)
2073615Sbill 			lseek(biofd, bp->b_off, 0);
2074615Sbill 		if (write(biofd, p, put) != put) {
2075615Sbill 			bwrerror = 1;
2076615Sbill 			error(1, "output write error");
2077615Sbill 		}
2078615Sbill 		bp->b_off += put;
2079615Sbill 		boffset = bp->b_off;
2080615Sbill 		p += put;
2081615Sbill 		cnt -= put;
2082615Sbill 		goto top;
2083615Sbill 	}
2084615Sbill 	bflush1(bp);
2085615Sbill 	goto top;
2086615Sbill }
2087615Sbill 
2088615Sbill bflush()
2089615Sbill {
2090615Sbill 	register struct biobuf *bp;
2091615Sbill 
2092615Sbill 	if (bwrerror)
2093615Sbill 		return;
2094615Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
2095615Sbill 		bflush1(bp);
2096615Sbill }
2097615Sbill 
2098615Sbill bflush1(bp)
2099615Sbill 	register struct biobuf *bp;
2100615Sbill {
2101615Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
2102615Sbill 
2103615Sbill 	if (cnt == 0)
2104615Sbill 		return;
2105615Sbill 	if (boffset != bp->b_off)
2106615Sbill 		lseek(biofd, bp->b_off, 0);
2107615Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
2108615Sbill 		bwrerror = 1;
2109615Sbill 		error(1, "output write error");
2110615Sbill 	}
2111615Sbill 	bp->b_off += cnt;
2112615Sbill 	boffset = bp->b_off;
2113615Sbill 	bp->b_ptr = bp->b_buf;
211416068Sralph 	bp->b_nleft = bp->b_bufsize;
2115615Sbill }
2116615Sbill 
2117615Sbill bflushc(bp, c)
2118615Sbill 	register struct biobuf *bp;
2119615Sbill {
2120615Sbill 
2121615Sbill 	bflush1(bp);
2122615Sbill 	bputc(c, bp);
2123615Sbill }
212416068Sralph 
212516068Sralph bseek(bp, off)
212616068Sralph 	register struct biobuf *bp;
212716068Sralph 	register off_t off;
212816068Sralph {
212916068Sralph 	bflush1(bp);
213016068Sralph 
213116068Sralph 	bp->b_nleft = bp->b_bufsize - (off % bp->b_bufsize);
213216068Sralph 	bp->b_off = off;
213316068Sralph }
2134